<div dir="ltr">Hello,<div>I have an application that decodes an input stream into AVFrames and stores these in a ring buffer. On a trigger, I can create an output file containing video from the past N seconds until I stop.</div><div>I am seeing a problem with the output video that it sometimes contains duplicate frames and sometimes contains frames from "the future" (i.e. the wrong point in the ringbuffer). I have added debug and it appears that avcodec_receive_frame() sometimes returns the same data buffer for multiple decoded frames, even though I have not unref'ed the previous AVFrame.</div><div>Specifically I am checking that the returned av_frame->data[0] is not being used by any other AVFrames and this is failing.</div><div><br></div><div>This is on Ubuntu bionic (18.04) using the bionic backports of v4.1.3 and v4.2.0 from <a href="https://launchpad.net/~jonathonf/+archive/ubuntu/ffmpeg-4">https://launchpad.net/~jonathonf/+archive/ubuntu/ffmpeg-4</a>.</div><div>The source of the video stream is a v4l2 device.</div><div><br></div><div>Cut-down example of the frame retrieve function:</div><div><font face="monospace">void *reader_run()</font></div><div><font face="monospace">{</font></div><div><font face="monospace">  AVFrame *ff = NULL;</font></div><div><font face="monospace">  while (is_running)</font></div><div><font face="monospace">  {</font></div><div><font face="monospace">    AVPacket;</font></div><div><font face="monospace">    av_init_packet(&pkt);</font></div><div><font face="monospace">    ff = av_alloc_frame();</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    /* Get packet from input */</font></div><div><font face="monospace">    ret = av_read_frame(reader_fctx, &pkt);</font></div><div><font face="monospace">    /* error checking ... */</font></div><div><font face="monospace">    ret = avcodec_send_packet(decoder_cctx, &pkt);</font></div><div><font face="monospace">    /* error </font><span style="font-family:monospace">check</span><font face="monospace">ing ... */</font></div><div><font face="monospace">    ret = avcodec_receive_frame(decoder_cctx, ff);</font></div><div><font face="monospace">    /* error </font><span style="font-family:monospace">check</span><font face="monospace">ing ... */</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    /* Debug to check buffers in other frames */</font></div><div><font face="monospace">    for (int i = 0; i< ringbuffer->size; i++)</font></div><div><font face="monospace">    {</font></div><div><font face="monospace">      if ringbuffer->items[i]->data[0] == ff->data[0]</font></div><div><font face="monospace">      {</font></div><div><font face="monospace">        ERROR("The buffer at %u is a duplicate when inserting %u %p", i, ringbuffer->next, ff->data[0]);</font></div><div><font face="monospace">      }</font></div><div><font face="monospace">    } /* End of debug */</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    av_packet_unref(&pkt);</font></div><div><font face="monospace">    rb_push(ringbuffer, ff); // Push onto head of ringbuffer and free oldest frame</font></div><div><font face="monospace">  }</font></div><div><font face="monospace">  /* Exiting. Flush decoder ... */</font></div><div><font face="monospace">}</font></div><div><font face="monospace"><br></font></div><div><span style="font-family:monospace">The buffer at 0 is a duplicate</span><font face="monospace"> when inserting 1 0x7f1eb881e000</font></div><div><font face="monospace">The buffer at 0 is a duplicate when inserting 2 0x7f1eb881e000</font></div><div><font face="monospace">The buffer at 1 is a duplicate when inserting 2 0x7f1eb881e000</font></div><div><span style="font-family:monospace">The buffer at 3 is a duplicate</span><span style="font-family:monospace"> when inserting 301 </span><span style="font-family:monospace">0x7f1eb8a55000</span><br></div><div><br></div><div>The AVFrame's are not duplicated (new one alloc'ed each time), but they are being populated with the same "data[0]" as other AVFrames. Is there anything I am misunderstanding about these buffers?</div><div>This usually happens 2-3 times as soon as the decoder starts, and then again when the buffer is full and these frames are being freed. It is not the same buffer pointers each time.</div><div><br></div><div>This code is being run in a thread, but it is the only thread using this decoder. It happens even when not passing the frames to an encoder.</div><div><br></div><div>Can anyone see something that would cause this?</div><div><br></div><div>I am going to build my own copy of the libraries, but if this exhibits the same issue what can I check further to find the source of this problem (e.g. any data that would be useful in a bug report)</div><div><br></div><div>Many thanks,</div><div>Hugh</div></div>