<div dir="ltr"><div>Hi all.</div><div><br></div><div>I'm currently working on a simple media player that has to seek to exact specified frame.</div><div>I'm currently using av_seek_frame(); function with AVSEEK_FLAG_BACKWARD flag which will put me to the first</div><div>I-Frame back of my specified location and then I calculate the current frame based on this function and just the just <br></div><div>decode all frames until my specified frames is reached.<br></div><div><br></div><div>int64_t PtsToFrame(AVStream* pavStream, int64_t pts)<br>{<br> long double timestamp = pts * av_q2d(videoState->video->ptrVideoStream->time_base);<br> long double fps = av_q2d(videoState->video->ptrVideoStream->r_frame_rate);<br> long double est_frame_f = timestamp * fps;<br> long double rounded = round(est_frame_f);<br> auto est_frame_i = static_cast<std::int64_t>(rounded);<br><br> return est_frame_i;<br>}</div><div><br></div><div>I also use this function to calculate PTS from specified frame</div><div><br></div><div>
int64_t FrameToPts(AVStream* pavStream, int frame) <br>{<br> int64_t target_dts_usecs = (int64_t)round(frame * (double)pavStream->r_frame_rate.den / pavStream->r_frame_rate.num * AV_TIME_BASE);<br> auto first_dts_usecs = (int64_t)round(pavStream->first_dts * (double)pavStream->time_base.num / pavStream->time_base.den * AV_TIME_BASE);<br> target_dts_usecs += first_dts_usecs;</div><div><br></div><div> return target_dts_usecs;<br>}
</div><div><br></div><div>This works perfectly for almost every format. But now I have few *.mpg files that have incorrect timestamps.</div><div>This has also messed up my normal seek-by-slider functionality. <br></div><div><br></div><div>I found that ffplay solves this issue by checking if it is currently opening a file that allow for timestamp discontinuities</div><div>and then use seek_by_bytes flag when seeking through video.</div><div><br></div><div>This has fixed my seeking-by-slider issues but now, on these files my </div><div>av_seek_frame(videoState->ptrFormatCtx_, -1, seekTarget, AVSEEK_FLAG_BACKWARD); <br></div><div>will just seek to exact frame (seekTarget) instead of the first I frame specified.</div><div><br></div><div>My
PtsToFrame function also returns incorrect frames (adds 10-13 frames).</div><div><br></div><div>To further clarify. <br></div><div>Presume that first I frame in the video is frame 1, the second is frame 250<br></div><div><br></div><div> JumpToFrame(50); <br></div><div> int64_t seekTarget =
FrameToPts(avStream, frame); <br></div><div> ret = av_seek_frame(videoState_->ptrFormatCtx_, -1, seekTarget, AVSEEK_FLAG_BACKWARD); // should jump to frame 1<br></div><div> if (ret > 0) <br></div><div> {</div><div> FastForwardToFrame(frame,seekTarget); // ff to desired frame<br></div><div> // ... flush queue<br></div><div> }<br></div><div><br></div><div>
FastForwardToFrame(...)</div><div> {<br></div><div></div><div> // ... decode frame and put result in pFrame</div><div> if (gotFrame)</div><div> {</div><div> current_frame = PtsToFrame(...); <br></div><div> }<br></div><div> }<br></div><div><br></div><div> Current frame at this point will be the exact frame specified, but since it's not an I frame the picture glitched.</div><div> I can also confirm that it's the exact frame because I processed the video with ffmpeg and I have a frame number subtitles on the video.</div><div><br></div><div>I presume this happens because of the format and incorrect timestamps in the file, but I just recently started with libav and can't figure out</div><div>where the issue is. Perhaps in the
FrameToPts() function? Because when I hard code the seek frame I can get the FastForward function to</div><div>run correctly (although still with an incorrect frame offset).</div><div><br></div><div>Anyone had an issue like this?</div><div>Thanks.<br></div><div><br></div><div><br></div></div>