title: Repeating ffmpeg stream (libavcodec/libavformat)
body: <p>I am using the various APIs from ffmpeg to draw videos in my application. So far this works very well. Since I also have gifs I want to do looping without having to load the file over and over again.</p>
  
  <p>In my code the decoder loop looks like this:</p>
  
  <pre><code> AVPacket packet = {};
   av_init_packet(&amp;packet);
   while (mIsRunning) {
       int error = av_read_frame(mContext, &amp;packet);
       if (error == AVERROR_EOF) {
           if(mRepeat) {
               logger.info("EOF-repeat");
               auto stream = mContext-&gt;streams[mVideoStream];
               av_seek_frame(mContext, mVideoStream, 0, 0);
               continue;
           }
           if (mReadVideo) {
               avcodec_send_packet(mVideoCodec, nullptr);
           }
           if (mReadAudio) {
               avcodec_send_packet(mAudioCodec, nullptr);
           }
           break;
       }
  
       if (error &lt; 0) {
           char err[AV_ERROR_MAX_STRING_SIZE];
           av_make_error_string(err, AV_ERROR_MAX_STRING_SIZE, error);
           logger.error("Failed to read next frame from stream: ", err);
           throw std::runtime_error("Stream reading failed");
       }
  
       if (packet.stream_index == mVideoStream &amp;&amp; mReadVideo) {
           int32 err;
           {
               std::lock_guard&lt;std::mutex&gt; l(mVideoLock);
               err = avcodec_send_packet(mVideoCodec, &amp;packet);
           }
           mImageEvent.notify_all();
           while (err == AVERROR(EAGAIN) &amp;&amp; mIsRunning) {
               {
                   std::unique_lock&lt;std::mutex&gt; l(mReaderLock);
                   mReaderEvent.wait(l);
               }
               {
                   std::lock_guard&lt;std::mutex&gt; l(mVideoLock);
                   err = avcodec_send_packet(mVideoCodec, &amp;packet);
               }
          } 
       }
  
       av_packet_unref(&amp;packet);
   }
  </code></pre>
  
  <p>Reading a video to the end works perfectly well and if I dont set <code>mRepeat</code> to true it properly EOFs and stops parsing. However when I use looping the following happens:</p>
  
  <ul>
  <li>The video ends</li>
  <li><code>AVERROR_EOF</code> happens at <code>av_read_frame</code></li>
  <li><code>EOF-repeat</code> is printed</li>
  <li>A random frame is read from the stream (and rendered)</li>
  <li><code>AVERROR_EOF</code> happens at <code>av_read_frame</code></li>
  <li><code>EOF-repeat</code> is printed</li>
  <li>A random frame is read from the stream (and rendered)</li>
  <li>...</li>
  </ul>
  
  <p>You can imagine it like I have a gif of a spinning globe and after one full turn it just starts randomly jumping around, sometimes for a fraction of a second correctly, sometimes backwards and sometimes just randomly everywhere.</p>
  
  <p>I have also tried several versions with <code>avformat_seek_file</code> what other way would there be to reset everything to the beginning and start from scratch again?</p>