¶AVIFile library in Windows Vista creates invalid AVI files
I was tipped off by a reader about some problems with AVI files in Windows Vista, based on this MSDN forum link:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=844438&SiteID=1
I ran test the test application and can confirm that this bug occurs in Windows Vista RTM. Essentially, every program that uses the Microsoft AVIFile APIs (avifil32.dll) to generate AVI files will produce them with a malformed RIFF structure!
(Sigh.... one more reason to avoid Vista....)
Here's the file produced by the program under Vista: vista-avifile-output.zip (57K)
The specific problem is a rather odd chunk arrangement near the beginning of the file where the 'movi' LIST chunk lies. This chunk holds all of the stream data in the file, but it's a bit odd here:
000000a0: 40 01 F0 00 73 74 72 66-28 00 00 00 28 00 00 00 |@...strf(...(...| 000000b0: 40 01 00 00 F0 00 00 00-01 00 10 00 4D 53 56 43 |@...........MSVC| 000000c0: 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |................| 000000d0: 00 00 00 00 4A 55 4E 4B-00 00 00 00 4C 49 53 54 |....JUNK....LIST| 000000e0: 48 37 0D 00 6D 6F 76 69-00 00 00 00 00 00 00 00 |H7..movi........| ... 000007f0: 00 00 00 00 4C 49 53 54-30 30 0D 00 6D 6F 76 69 |....LIST00..movi| 00000800: 30 30 64 62 82 70 00 00-00 00 00 00 00 00 00 00 |00db.p..........| 00000810: 00 00 01 00 00 00 00 00-02 00 00 00 00 00 03 00 |................| 00000820: 00 00 00 00 04 00 00 00-00 00 05 00 00 00 00 00 |................|
There's two of them, and one's generated in a very odd place. Worse yet, LIST chunks are supposed to be containers for other chunks, but the first 'movi' LIST starts with garbage.
What's going on here: In most cases, it isn't possible to generate an AVI in a single pass. The reason is that AVI, like any other RIFF file, uses chunks which are prefixed with the data length, and it isn't generally possible to predict the size of the 'movi' chunk when generating the AVI incrementally, nor is the data small enough to buffer everything in memory. To get around this, the AVI file is generated with dummy size fields, and when the file is finished, those size fields are backpatched with the correct sizes, which are known after all of the data has been written. It looks like someone either broke the backpatching code in Vista so that it fails to rewrite the 'movi' header in the right place, aligned to 2K - 12 bytes, instead effectively extending it backwards, or somehow managed to open two 'movi' chunks.
I should note that VirtualDub does write correct AVI files when running under Windows Vista, because it has its own internal routines for doing so. It's still affected by this bug, though — more on that in a bit.
So, what's the correct fix? Well, it's not so simple. Here is a hex dump of the index at the end of the file:
000d3820: 00 00 0A 74 00 00 00 00-09 74 00 00 69 64 78 31 |...t.....t..idx1| 000d3830: E0 01 00 00 30 30 64 62-10 00 00 00 1C 07 00 00 |....00db........| 000d3840: 82 70 00 00 30 30 64 62-10 00 00 00 A6 77 00 00 |.p..00db.....w..| 000d3850: 82 70 00 00 30 30 64 62-10 00 00 00 30 E8 00 00 |.p..00db....0...| 000d3860: 82 70 00 00 30 30 64 62-10 00 00 00 BA 58 01 00 |.p..00db.....X..| 000d3870: 82 70 00 00 30 30 64 62-10 00 00 00 44 C9 01 00 |.p..00db....D...| 000d3880: 82 70 00 00 30 30 64 62-10 00 00 00 CE 39 02 00 |.p..00db.....9..|
The field highlighted in red is the position value for the first video frame in the file, and is supposed to be an relative index within the 'movi' LIST chunk to the start of the sample chunk header. Well, the first 'movi' LIST is at 0xDC, and 0xDC + 0x08 + 0x71C = 0x800, which is where the first sample in video stream 0 lies ('00db'). So the first 'movi' chunk is the one that is consistent with the index. We can fix the RIFF structure of the file by turning the garbage at the start of the 'movi' chunk into another padding (JUNK) chunk:
000000d0: 00 00 00 00 4A 55 4E 4B-00 00 00 00 4C 49 53 54 |....JUNK....LIST| 000000e0: 48 37 0D 00 6D 6F 76 69-4A 55 4E 4B 10 07 00 00 |H7..moviJUNK....|
Now the file is consistent... but it still isn't compatible. It turns out that VirtualDub still won't read the file because the oddball positioning conflicts with some code I have to try to detect relative vs. absolute indexing. (There are some other applications that have similar problems.) To make matters worse, the AVIFile library in Windows XP SP2 also has problems with the file, so the "AVIFile input driver (compat.)" mode in VirtualDub, which tells it to use AVIFile, won't work either. In fact, when I tried to use the old Media Player (mplay32.exe) to play the file, since it can be forced to use AVIFile through the Video for Windows MCI driver, the Windows XP AVIFile library actually crashed in memmove(). Great.
So, basically, I don't have a good solution at this time, other than to... chastise... the nearest Microsoft representative. I can tell you that VirtualDub 1.7.2 will be able to read such files, because I was tipped off to this problem and checked a fix into my dev tree before I became aware that Windows Vista itself was the culprit. For those of you who are shipping or have shipped applications that write AVI files through AVIFile, I don't know what to tell you. I haven't dug into the Vista avifil32.dll and couldn't tell you a fix, and DirectShow is a very rough way to write an AVI file from scratch. AVIFile writes relatively simple AVI files compared to what's flying around, though — it doesn't handle indexing beyond 2GB — so I'd recommend looking into writing a replacement. It's not that hard to generate a basic AVI file.