[Ffmpeg-devel] truncated audio output

Justin Ruggles jruggle
Mon Jun 19 02:56:00 CEST 2006


I've been working on an FFmpeg patch to add a FLAC encoder, and I came
across a problem that's really hindering FFmpeg being useful with FLAC
or any other lossless audio encoder (besides WAV or raw PCM).

The problem comes from the fact that FFmpeg only sends audio samples to
the codec in chunks of AVCodecContext->frame_size samples.  The codec
must set the fixed frame size in AVCodec->init().  The issue is that if
the input audio is not an even multiple of frame_size, the output will
be truncated, and thus, not lossless.

The best solution I could come up with was to add a codec capability,
CODEC_CAP_VAR_FRAME_SIZE, which would indicate that the codec can handle
inputs of different sizes.  The frame size could be indicated by
AVCodecContext->frame_size being changed by the user (i.e. ffmpeg.c)
prior to encoding a frame.  In this case, the encoder could decide what
to do if it gets less samples than a full frame.  FFmpeg would be
altered so that if CODEC_CAP_VAR_FRAME_SIZE was set, the remaining data
in the output stream fifo would be sent to the encoder after all other
encoding is complete so as not to leave any samples stranded in the
fifo.  A lossless codec could write this smaller frame.  Other codecs
could could choose to use these final samples, pad with zeros, and write
another frame.

I did a test to see if this works and it does.  I made a dummy audio
encoder which just does a raw sample copy and keeps track of the number
of input samples passing through it.

If this solution sounds like a crazy hack, just let me know. :)  It
seems to work pretty well though.  I can submit a patch showing how my
"pcm2" audio encoder works just as a proof-of-concept if anyone would like.

Something similar is happening on the decoding end as well, but I
haven't delved into that yet.  For example, the flac decoder seems to
leave off the last 2 frames.


More information about the ffmpeg-devel mailing list