[FFmpeg-devel] [PATCH] support for ordered chapters/segment linking in Matroska

John B jbfaad
Wed Aug 13 00:17:50 CEST 2008

Sorry for the spam, but I took a look at the appropriate places and I
think this *might* be possible, and also give ffmpeg a nice feature
(concatenation of Vorbis streams) in the process.

Vorbis has 3 header packets for setup. The first one has a few
critical values that must match (samplerate, channels and both
blocksizes [window sizes] - incidentally, mkvmerge doesn't check
blocksizes). The 2nd header is metadata and isn't needed for decoding.

The third is the setup header and is where things turn interesting -
it includes a number of (VLC) codebooks, (spectral) floors, residues,
(channel) mappings and finally modes. A mode references a block size
and a mapping, which references floors and residues, which reference

Every audio packet has a single bit header (a 0) followed as few bits
as possible [ilog(modes)] to select a mode number for decoding, and
then comes the actual packet data.

The idea here is that you can theoretically build a codec setup header
that contains all setups from all the concatenated segments. This
would increase the size of every individual audio packet an average of
log2(n) bits, where n is the total number of setups you're merging.
You'd have to modify the audio packets to point to the correct setup.
A more refined implementation would try to re-use matching modes
whenever possible.

This still isn't infallible since there are limits on the number of
stuff that can fit in there. Blocksizes must match, the official
encoder and aoTuV, at 44100/48000Hz, use 512/4096 window sizes for
qualities < 0 (~ bitrates < 64Kbps), and 256/2048 for everything else,
which is somewhat good news. There are limits of 257 codebooks, 65
floors, 65 residues, 65 mappings and 65 modes. I don't know how many
of them are usually used by a single stream, maybe somebody can
comment on this to see if it's feasible. Also, maybe I've missed other
limitations that make this impossible.

Just so you know, in case you get really bored :)

More information about the ffmpeg-devel mailing list