[libav-api] must av_init_packet(&dummy_packet) when draining final frames from video using avcodec_decode_video2()

Bernd Pfrommer bernd.pfrommer at gmail.com
Wed May 13 20:49:14 CEST 2015

I hit an issue today with draining the last few frames of a video
stream, using libav v12_dev0:

The documentation
https://libav.org/doxygen/master/group__lavc__decoding.html says:

"Some decoders (those marked with CODEC_CAP_DELAY) have a delay between
input and output. This means that for some packets they will not
immediately produce decoded output and need to be flushed at the end of
decoding to get all the decoded data. Flushing is done by calling this
function with packets with avpkt->data set to NULL and avpkt->size set
to 0 until it stops returning samples."

But that is not sufficient.

I got a core dump with the following stack trace:

#0  0x00007fffeaf9f6bd in av_packet_get_side_data (pkt=0x7fffffffc2d0,
type=AV_PKT_DATA_PARAM_CHANGE, size=0x7fffffffc1fc) at
#1  0x00007fffeb2c59e0 in apply_param_change (avpkt=0x7fffffffc2d0,
avctx=0x1ddaca0) at libavcodec/utils.c:1549
#2  avcodec_decode_video2 (avctx=0x1ddaca0, picture=0x9f0000,
got_picture_ptr=0x7fffffffc260, avpkt=0x7fffffffc2d0) at

and in another run, I got a core dump where it tried to access the
packets "buf" field. Setting side_data, side_data_elems, and buf to zero
eventually worked. This can be accomplished by using av_init_packet():

    AVPacket emptyPacket;
    av_init_packet(&emptyPacket);  // <---------- must initialize side
data and buf!!!!
    emptyPacket.data = NULL;
    emptyPacket.size = 0;

    int frameFinished;
    AVFrame *tmpFrame = av_frame_alloc();
    int len = avcodec_decode_video2(context, tmpFrame, &frameFinished,

I suggest updating the documents to say that av_init_packet() is
required. This is particulary important because there is example code
that does not call av_init_packet():


More information about the libav-api mailing list