[Libav-user] Buffered frames in avcodec h264 decoder

Hung Nguyen Hung.Nguyen at ambientdigitalgroup.com
Fri Dec 5 03:17:44 CET 2014

When open input decoder, there is an option that set how many frame will be buffer on decoder side:

for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        AVStream *stream;
        AVCodecContext *codec_ctx;
        stream = ifmt_ctx->streams[i];
        codec_ctx = stream->codec;
        codec_ctx->delay = 5;

Notice option delay = 5, with that option set, there’s always 5 frames buffered even when you’ve finished decode/encode steps (at least with h264 decoder)
To disable delay, set coder_ctx->delay = 0 (correct me if I am wrong) or you have to do something like this:

1. set a counting variable to count how many frame was left on decoder:
int skip_frame = 0;

2. Whenever decoder fail to produce frame when decoding avpacket, ++ skip_frame:

if (!got_frame): ++ skip_frame;

3. When everything is finished, do something like this:

 for (int i = skip_frame; i > 0; i--) {
        stream_index = packet.stream_index;
        type = ifmt_ctx->streams[packet.stream_index]->codec->codec_type;
        if (type == AVMEDIA_TYPE_VIDEO) {
            frame = av_frame_alloc();
            ret = avcodec_decode_video2(ifmt_ctx->streams[stream_index]->codec
                    , frame, &got_frame, &packet);
            if (got_frame) {
                frame->pts = av_frame_get_best_effort_timestamp(frame);
                ret = filter_encode_write_frame(frame, stream_index);
                if (ret < 0)
                    goto end;

That’s what I figured out after weeks working with libavcodec.

Hope this help.


Hung Nguyen
hungnv at opensource.com<mailto:hungnv at opensource.com>.vn

On Dec 5, 2014, at 5:12 AM, Hendrik Leppkes <h.leppkes at gmail.com<mailto:h.leppkes at gmail.com>> wrote:

On Thu, Dec 4, 2014 at 2:31 PM, Alina lifshits <alina at vicon.co.il<mailto:alina at vicon.co.il>> wrote:

I am using avcodec decoder to decode h264 stream. I noticed that the decoder buffers a few first frames before returning a decoded picture.
The stream is from a live source and does not contains B frames. Is there a way to tell the decoder not to buffer frames, or maybe to buffer just 1 or 2 at most?
Since I am working with a live video, it very important for me to keep small latency.

To minimize latency, make sure you turn off frame-threaded decoding. You can still use slice-threading (if your video has slices), but frame-threading has an inherent delay.
Additionally, there is a flag to force low delay decoding, but I'm not certain it work for H.264. You could look into that.

- Hendrik
Libav-user mailing list
Libav-user at ffmpeg.org<mailto:Libav-user at ffmpeg.org>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20141205/de60d783/attachment.html>

More information about the Libav-user mailing list