title: Seeking with FFmpeg gives old frames with new packet timestamp
body: <p>I'm using <strong>FFmpeg 2.7</strong> to write a sample application that would do the following:</p>
  
  <ol>
  <li>Read the video packets from the given .mov file</li>
  <li>Seek to a specified position after 10 sec of decoded video and continue for another 10 sec</li>
  <li>Decode all video packets before and after the seek and write them into a target file.</li>
  </ol>
  
  <p>In the process, I am also dumping all the video frames received into raw image files, using the PTS of the original packet as the filename. I notice that even after the seek has happened (noted by the jump in timestamp), the first few frames after the seek contain the new timestamp, but the old frames (i.e., frames I would have received without seek).</p>
  
  <p><a href="https://i.stack.imgur.com/I2LXO.jpg" rel="nofollow noreferrer">These images illustrate the problem.</a></p>
  
  <p>The following is a snippet of the code I'm using:</p>
  
  <pre><code>int SeekAndTranscode::_TranscodePacket(AVPacket &amp;packet, AVFrame *frame)
  {
      if(packet.stream_index == _videoStreamIndex)
      {
          int status = avcodec_decode_video2(&amp;_videoDecContext, frame, &amp;gotFrame, &amp;packet);
          if (status &lt; 0)
          {
              fprintf(stderr, "\nError decoding video frame\n");
              exit(1);
          }
  
          if (gotFrame)
          {
              if (frame-&gt;width != _width || frame-&gt;height != _height ||
                  frame-&gt;format != _pixelFormat)
              {
                  return -1;
              }
  
              vector&lt;uint8_t&gt; rgbData;
              rgbData.resize(_width * _height * 4);
  
              uint8_t *rgbSrc[3]= {&amp;rgbData[0], NULL, NULL};
              int rgbStride[3]={4 * _width, 0, 0};
  
              sws_scale(_csc, frame-&gt;data, frame-&gt;linesize, 0, _height, rgbSrc, rgbStride);
  
              int64_t pts = packet.pts * AV_TIME_BASE / _videoDecContext.time_base.den;
              FILE *file;
              stringstream filename;
              filename &lt;&lt; "C:\\Bipul\\" &lt;&lt; pts &lt;&lt; ".raw";
  
              file = fopen(filename.str().c_str(), "wb");
              fwrite(&amp;rgbData[0], 1, rgbData.size(), file);
              fclose(file);
  
              fwrite(&amp;rgbData[0], 1, rgbData.size(), _videoOutputFile);
          }
      }
  
      return decodedSize;
  }
  
  
  void SeekAndTranscode::TranscodeIntoFile()
  {
      AVFrame* frame = NULL;
      AVPacket packet;
      int videoSeeks = 0;
      int frameCount = 0;
      int framerate = 24;
      try
      {
          frame = av_frame_alloc();
          if (!frame)
          {
              fprintf(stderr, "\nCould not allocate frame for decoding\n");
              exit(1);
          }
  
          av_init_packet(&amp;packet);
          packet.data = NULL;
          packet.size = 0;
  
          while (av_read_frame(_formatContext, &amp;packet) &gt;= 0)
          {
              if(packet.stream_index == _videoStreamIndex)
              {
                  frameCount++;
                  if(frameCount % (10*framerate) == 0)
                  {
                      if(videoSeeks == 1)
                          break;
                      av_seek_frame(_formatContext, -1, _seekPosition * AV_TIME_BASE, 0);
                      videoSeeks++;
                      continue;
                  }
              }
              AVPacket originalPacket = packet;
              do
              {
                  int status = _TranscodePacket(packet, frame);
  
                  if (status &lt; 0)
                      break;
                  packet.data += status;
                  packet.size -= status;
              } while (packet.size &gt; 0);
              av_packet_unref(&amp;originalPacket);
          }
  
          if (frame)
          {
              av_frame_free(&amp;frame);
          }
  
          swr_free(&amp;_resampler);
      }
      catch(const exception&amp; e)
      {
          fprintf(stderr, "\n%s\n", e.what());
          exit(1);
      }
  }
  </code></pre>
  
  <p>Is this a known issue with this version of FFmpeg? Has this been fixed in later versions? Is there something I'm doing wrong? Any leads will be appreciated.</p>