What is VirtualDub?
VirtualDub is a video capture/processing utility for 32-bit Windows platforms (95/98/ME/NT4/2000/XP), licensed under the GNU General Public License (GPL). It lacks the editing power of a general-purpose editor such as Adobe Premiere, but is streamlined for fast linear operations over video. It has batch-processing capabilities for processing large numbers of files and can be extended with third-party video filters. VirtualDub is mainly geared toward processing AVI files, although it can read (not write) MPEG-1 and also handle sets of BMP images.
I basically started VirtualDub in college to do some quick capture-and-encoding that I wanted done; from there it's basically grown into a more general utility that can trim and clean up video before exporting to tape or processing with another program. I released it on the web and others found it useful, so I've been tinkering around with its code ever since. If you have the time, please download and enjoy.
§ ¶Wine's DirectDraw implementation
I had a support question about VirtualDub blanking out screens again in Wine, so I decided to take another look out of curiosity.
A long time ago, when I released VirtualDub 1.5.5, I started receiving reports of issues with Wine. Specifically, as soon as VirtualDub tried to display any video, the entire screen would be overwritten. Wine was fairly immature at the time and still had a lot of major problems with commercial applications, so I wasn't surprised to determine that it was an issue with Wine and left it as such. That was the first version that had full support for DirectDraw video display, and so the workaround was to disable it in Options and fall back to GDI.
Surprisingly, there are still display compatibility issues with VirtualDub running under Wine.
VirtualDub doesn't do anything too fancy in its default DirectDraw mode. It creates a single off-screen surface in default memory (usually video, can be system) and then calls Blt() to draw it onto the primary surface through a clipper. I use the older DirectDraw 3 API, because that was the highest version supported by Windows NT 4.0 and there isn't anything in DirectDraw 6 or 7 that I need. There was a problem with Windows Vista where the panes sometimes wouldn't update (mixed DX and GDI in the same window no longer allowed, first I'd ever heard of it), but I fixed that by splitting off the display minidrivers to a child window, at the cost of flickering at mode switches. As far as I know, everything works now, both in XP and Vista. But it doesn't work in Wine, at least not so well.
In Wine 1.0, the current stable release, the old screen blanking problem is still around. I didn't have a debugging setup that I could use to check the Wine code -- realistically, I shouldn't even have been installing and running Wine on a running MythTV box -- but as best as I could tell from behavior and the Wine source, here's what's going on:
- In the default configuration and on that system, Wine needs to emulate the blit.
- Like in the Microsoft HEL (hardware emulation layer), the blit is emulated by locking both the primary and the off-screen buffer, and copying the surface data over using the CPU.
- X doesn't support locking the primary buffer, so that must be emulated by maintaining a backing store and updating the screen from it.
- The other programs don't know about the backing store, so the backing store for the primary is black everywhere except for where the blit happened.
- The emulated Blt() routine locks the entire primary surface, thus causing the whole screen to be updated -- and turn black everywhere except for the video panes.
Note that VirtualDub is blitting with both a destination subrect and a clipper attached, so the entire screen should definitely not be getting overwritten in this way.
In the current alpha release -- I think it was Wine-1.1.2 that I tested -- the situation is a bit better, as the video panes no longer blank the screen. There appear to be two changes possibly involved, although I'm not sure which are in the mainline:
- CodeWeavers had a hack to better emulate locking the primary by blitting from the screen into the backing store. That sort of works, I guess, but boy is it slooooooowwww, and it's also extraneous if the data is going to be overwritten anyway.
- Blt() was changed to lock only the pertinent subrect of the destination. I'm guessing this was the change that made the real difference.
The clipping situation is still pretty bad in the current version, as blits that are partially outside of the primary surface still fail, even if a clipper is attached. It seems that the top-level Blt() function immediately rejects any blits where the destination rect overlaps the right or bottom boundary, clipper or not. The underlying blit code in wined3d seems to be able to handle clipping to some extent, but unfortunately it lacks the ability to clip a stretchblt. It's a relatively minor issue, but it looks like the fractional stepping on the stretchblt case is also off by half a pixel.
How hard would it be to fix? I'm not sure. It's easy to be an armchair programmer, and I've never worked on Wine before. The clip and stretch problems, I'd say, shouldn't be too bad. As far as I know, the real DirectDraw only clips blits to pixel precision on both destination and source, which is easier but leads to seam artifacts. There is a path that does clip properly, but it's only used by Direct3D Present() calls. A more worrisome problem is that the old Microsoft DirectDraw Test (ddtest.exe) program doesn't work at all under Wine, as it displays garbage in its status panes and crashes out as soon as you try to create the primary surface. I suspect the reason might be that the DX3 interfaces aren't implemented properly and are expecting and returning DX7 structures, which would require a much bigger fix than just a few tweaks to the clipping and scaling routines.
So, if you're wondering why VirtualDub has drawing problems on Wine... there's your answer.
(Read more....)
§ ¶Undoing indentation hell
Have you ever looked at a piece of code and seen something like this?
if (...) {
if (...) {
...
} else {
if (...) {
...
} else if (...) {
...
} else {
...
}
}
...
I'm going into style matters again and I know this is going to be contentious, but personally, I find that code is hard to read when it looks like a big tree view. You have to mentally match the braces or highlight them individually to have the editor show the matching, and that's hard to do when there are so many of them. It's even harder when the top-level statements don't even fit on one screen. The worst case I ever saw had something like 12 levels of indenting and was more than 17 pages long... and I use a small font size. Most of the time this happens because a routine snowballs until it grows out of control, and no one has the time/experience/constitution to refactor it. This happens to me as well, and eventually I get annoyed enough that I have to perform a nesting exorcism.
I attack such monstrosities by a series of small, incremental transformations. This increases the chances that the code will actually still work by the time I'm done with it. I should note that these are best done only if you own the code or already have to do major work on it for other reasons; doing transformations like this willy-nilly is a good way to completely scramble history in a source code control system and nuke any chances of your team members successfully merging your changes with theirs.
(Read more....)