[Libav-user] about flushing decoder at the end of file

YIRAN LI mrfun.china at gmail.com
Wed Feb 19 10:35:19 CET 2014

Hi friends,

I know when av_read_frame(ctx, pkt) returns < 0 we can set pkt.data = NULL
and pkt.size = 0 to flush any frames in decoder if the decoder has the

The example in src/doc/examples/demuxing.c does these

while (av_read_frame(ctx, &pkt) >=0 ) {
    decode_packet(&got_frame, 0);

pkt.data = NULL;
pkt.size = 0;
do {
    decode_packet(&got_frame, 1);
} while (got_frame);

so my question is how if both Audio and Video has something to flush?

Because decode_packet() depends on pkt.stream_index to call audio or video
but pkt.stream_index is set in the last call to av_read_frame, so it never
changes in whole
do {} while.

My second question is how to flush all video frame quickly. Can I do this:

while (av_read_frame(ctx, &pkt) >=0 ) {
    decode_packet(&got_frame, 0);

// will it have problem if there're only audio frames wait to flush but no
video frame
// can I explicitly set pkt index to video and call decode_video? (I only
want video frames)
pkt.stream_index = video_index;
pkt.data = NULL;
pkt.size = 0;
do {
} while(got_frame)

I set steam_index to video index because I'm afraid that pkt.stream_index
may be the index of audio after return from av_read_frame().

Just imagine this case: when av_read_frame() returns < 0, there're audio
frames wait to flush but no video frames left. Will my call to
avcodec_decode_video2(,&got_frame,..) cause any crash? or it's guaranteed
to return  got_frame == false so that I know there's no video frame left?

Great thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20140219/f3358769/attachment.html>

More information about the Libav-user mailing list