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!

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:

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:

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.

7 comments | Feb 22, 2007 at 03:22 | default

Comments

Comments posted:

Hi
Totem can play the howdy.avi file. Can that be considered a bug in the GStreamer backend?

Hieu Hoang - 22 02 07 - 08:32

WTF! Why would anybody find a need to f*** with this code. Sounds like the guilty party should read your previous blog on fixing working code.

IanB - 22 02 07 - 21:42

Hello,

...I tried to play the example .avi file under Windows XP with mplayer2.exe, WMPlayer.exe, Nero ShowTime and VirtualDub 1.7.1. All four programs opened and played the file. However, the results were different. Nero ShowTime and VirtualDub showed the same result (color pattern opens like a door). mplayer2.exe and WMPlayer.exe showed another result (color pattern moves from left to the right).
Which "interpretation" is the correct one?

Regards,
derJörg

derJörg - 25 02 07 - 01:29

The DirectShow players are showing the correct video. What you're seeing in Nero and VirtualDub is an offset in the displayed data; if it were a compressed video, it wouldn't decode at all.

Phaeron - 25 02 07 - 01:42

The bug still exists in the release version of Windows Vista. 12 bytes of the movi file header are zeroed out. If you replace the avifil32.dll on Vista with the version from Windows XP avi file export in applications using the library works ok. Not easy to replace that dll, good luck with that. Lets hope that Microsoft will release a bug fix for it.

pavelb - 20 03 07 - 17:37

pavelb, how did you replace the file? i tried regular and safe mode xp, vista, tried taking over ownership. i couldn't figure out how to take over the SYSTEM file, tho. i tried FileAssassin (which has its own methods as well as telling XP to delete it before it starts up), no go.