[Libav-user] H.264 codec frames lost

BIGLER Don (Framatome) don.bigler at framatome.com
Fri Mar 1 16:10:09 EET 2019

>From: Libav-user [mailto:libav-user-bounces at ffmpeg.org] On Behalf Of Gustav González
>Sent: Friday, March 01, 2019 12:30 AM
>To: This list is about using libavcodec, libavformat, libavutil, libavdevice and libavfilter.
>Subject: [Libav-user] H.264 codec frames lost
>Security Notice: Please be aware that this email was sent by an external sender. 
>I implemented a basic tool to convertt an array of images into a video file, following some of the ffmpeg/libav examples available in several forums.
>The code works great using the MPEG4 codec, but when I try the H,264 something strange occurs: I lost 30% of the frames (usually the last ones).
>Checking on the code, I found out that this instructions is related to the issue: 
>int ret = avcodec_encode_video2(c, &pkt, videoFrame, &got_output);
>The variable got_output gets NULL value in many of the iterations while I am creating the video file:
>if (got_output) {
>    if (c->coded_frame->key_frame)
>        pkt.flags |= AV_PKT_FLAG_KEY;
>    pkt.stream_index = video_st->index;
>    ret = av_interleaved_write_frame(oc, &pkt);
 >    av_free_packet(&pkt);
 >} else {
 >       ret = 0;
> }
>So I wonder what I could be missing related to the H,264 codec, understanding that using exactly the same code, the codec MPEG4 works perfectly. 
>Any codec settings that I should care about? Any suggestion?
>  Gustav Gonzalez
>  xtingray at gmail.com

H.264 encoding uses temporal compression techniques.  So it queues frames in order to compute compression over multiple frames.  That means got_ouput will be 0 at times before producing a compressed packet.  Since you observed that the missing packets are at the end I suspect that you are not flushing the codec after feeding it all of the images.  See the flush_encoder() function in the transcoding example (https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/transcoding.c).

Don Bigler

More information about the Libav-user mailing list