title: Is it possible to decode MPEG4 frames without delay with ffmpeg?
body: <p>I use <strong>ffmpeg's MPEG4 decoder</strong>. The decoder has <strong>CODEC_CAP_DELAY</strong> capability among others. It means the decoder will give me decoded frames with latency of 1 frame.</p>
  
  <p>I have a set of MPEG4 (I- &amp; P- )frames from AVI file and feed ffmpeg decoder with these frames. For the very first I-frame decoder gives me nothing, but decodes the frames successfully. I can force the decoder to get the decoded frame with the second call of <strong>avcodec_decode_video2</strong> and providing nulls (flush it), but if I do so for each frame I get artifacts for the first group of pictures (e.g. second decoded P-frame is of gray color).</p>
  
  <p>If I do not force ffmpeg decoder to give me decoded frame right now, then it works flawlessly and without artifacts.</p>
  
  <p><strong><em>Question:</em></strong> But is it possible to get decoded frame without giving the decoder next frame and without artifacts?</p>
  
  <p>Small example of how decoding is implemented for each frame:                </p>
  
  <pre class="lang-cpp prettyprint-override"><code>        // decode
          int got_frame = 0;
          int err = 0;
          int tries = 5;
          do
          {
              err = avcodec_decode_video2(m_CodecContext, m_Frame, &amp;got_frame, &amp;m_Packet);
              /* some codecs, such as MPEG, transmit the I and P frame with a
              latency of one frame. You must do the following to have a
              chance to get the last frame of the video */
              m_Packet.data = NULL;
              m_Packet.size = 0;
              --tries;
          }
          while (err &gt;= 0 &amp;&amp; got_frame == 0 &amp;&amp; tries &gt; 0);
  </code></pre>
  
  <p>But as I said that gave me artifacts for the first gop.</p>