Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify get_frame_played_at #545

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions src/torchcodec/decoders/_core/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,24 +698,9 @@ VideoDecoder::FrameOutput VideoDecoder::getFramePlayedAt(double seconds) {
}

setCursorPtsInSeconds(seconds);
AVFrameStream avFrameStream =
decodeAVFrame([seconds, this](AVFrame* avFrame) {
StreamInfo& streamInfo = streamInfos_[activeStreamIndex_];
double frameStartTime = ptsToSeconds(avFrame->pts, streamInfo.timeBase);
double frameEndTime = ptsToSeconds(
avFrame->pts + getDuration(avFrame), streamInfo.timeBase);
if (frameStartTime > seconds) {
// FFMPEG seeked past the frame we are looking for even though we
// set max_ts to be our needed timestamp in avformat_seek_file()
// in maybeSeekToBeforeDesiredPts().
// This could be a bug in FFMPEG: https://trac.ffmpeg.org/ticket/11137
// In this case we return the very next frame instead of throwing an
// exception.
// TODO: Maybe log to stderr for Debug builds?
return true;
}
return seconds >= frameStartTime && seconds < frameEndTime;
});
AVFrameStream avFrameStream = decodeAVFrame([this](AVFrame* avFrame) {
return cursor_ < avFrame->pts + getDuration(avFrame);
});

// Convert the frame to tensor.
FrameOutput frameOutput = convertAVFrameToFrameOutput(avFrameStream);
Expand Down
Loading