[FFmpeg-devel] Wave header

Dave Rice dave at dericed.com
Thu Apr 18 06:11:20 CEST 2013

On Apr 17, 2013, at 5:36 PM, Виталий <vitalik1231 at yandex.ru> wrote:

> Hello, how patch wav format ? When I convert audio into wav, size of header is 80 bytes (or 46 if comment ff_riff_write_info in libavformat/wavenc.c),

I wouldn't usually consider these bytes as a header. The current git master will happen to write 80 bytes from the beginning of the file to the beginning of the payload of the data chunk. This is comprised of the RIFF chunk description (id, size, and format is 12 bytes), the 'fmt ' chunk (26 bytes in this case), the LIST-INFO chunk (default to 34 bytes but this will change based on supplied metadata or changes to the libavformat version), and then the chunk id and size of the 'data' chunk (8 bytes). So this is 80 bytes or 46 if you manage to suppress the LIST-INFO chunk. [Using `ffmpeg -f lavfi -i sine -t 1 test.wav` for these measurements]

> I want wav which have 44 bytes header (and without any metadata),

By the measurement of header you use (beginning of file to beginning of data chunk payload), you would have to use a 24 byte 'fmt ' chunk rather than the existing 26 bytes to 24, to get to an overall 44 byte header. It's true that other wav muxers typically use 24 byte 'fmt ' chunks, but ffmpeg uses 2 optional bytes at the end, cbSize, which doesn't need to be used with PCM data. I see the issue was written about here: http://blog.bangsplatpresents.com/?p=1017. Resources on the WAVE specification, such as http://www-mmsp.ece.mcgill.ca/documents/AudioFormats/WAVE/WAVE.html, imply that these extra bytes are only to be used with non-PCM data. This may be a bug.

> I must use "ffmpeg -i ... -f s16le tmp.dat" and then "mplayer -demuxer rawaudio -rawaudio rate=44100:channels=2:samplesize=2 -ao pcm tmp.dat" to get "normal" wav file, many program can't read ffmpeg's wav file, they read 44 bytes of header, and others data use as sound, sometimes it caused broken byteorder, or they say that file is broken and can't read file at all. So I want get "correct" wav from ffmpeg directly. Sorry for my bad english :)

I can't find original Microsoft documentation that indicates that unnecessary use of cbSize is not 'correct'. The best I can quickly find is msdn.microsoft.com/en-us/library/windows/desktop/dd757713(v=vs.85).aspx which states for cbSize that "If no extra information is required by the wFormatTag, this member must be set to 0. For WAVE_FORMAT_PCM formats (and only WAVE_FORMAT_PCM formats), this member is ignored." FFmpeg does set cbSize to zero and then provides no extra information requires for the 'fmt ' chunk beyond offering to cbSize value to state explicitely that there is no format chunk extension data. My opinion would be to omit the two bytes of cbSize when the extensible wave format chunk is unnecessary, but I don't find the current practice to be not 'correct'.

On the other hand presuming that data beyond the first 44 bytes of a wav file is audio data is very odd. The wav specification allows many types of chunks to preceed the data chunk which would produce a header much larger than 44 bytes. Many applications will insert a junk or free chunk before the data chunk simply to allow the easy option of converting from RIFF to RF64 when necessary.

Dave Rice

More information about the ffmpeg-devel mailing list