title: av_seek_frame breaks avcodec_decode_video2 when codec uses p-frames
body: <p>I'm trying to understand how to seek in videos encoded with p-frames (e.g. H.264)-- right now I only know how to seek in videos encoded with codecs in which every frame is a keyframe (e.g. MJPEG). </p>
  
  <p>Given a timestamp, ultimately it should play the frame for that timestamp. <em>But for now, I'm just trying to play the keyframe near the timestamp.</em> So, first-- </p>
  
  <pre><code>ret = av_seek_frame(pFormatCtx, videoStream, avTime, 0); 
  avcodec_flush_buffers(pCodecCtx);
  </code></pre>
  
  <p>Then some intervening code, including <code>av_read_frame</code>. Then-- </p>
  
  <pre><code>ret = avcodec_decode_video2(pCodecCtx, captureFrame, &amp;gotPicture, &amp;packet); 
  </code></pre>
  
  <p>Observations: </p>
  
  <ul>
  <li><p>For some videos, this works. For others, <code>avcodec_decode_video2</code> returns an error (negative number). Example of a working video: <a href="http://www.dvdloc8.com/clip.php?movieid=12954&amp;clipid=1" rel="nofollow">here</a>. Example of a non-working video: <a href="http://www.dvdloc8.com/clip.php?movieid=12167&amp;clipid=2" rel="nofollow">here</a>. </p></li>
  <li><p>For MJPEG videos, this works. </p></li>
  <li><p>If I take out the <code>av_seek_frame</code>, so that it just plays through the frames in sequence, it works. </p></li>
  </ul>