Skip to content

Fix pts <-> seconds conversions #682

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

Merged
merged 2 commits into from
May 15, 2025
Merged
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
12 changes: 5 additions & 7 deletions src/torchcodec/_core/SingleStreamDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
namespace facebook::torchcodec {
namespace {

double ptsToSeconds(int64_t pts, int den) {
return static_cast<double>(pts) / den;
}

double ptsToSeconds(int64_t pts, const AVRational& timeBase) {
return ptsToSeconds(pts, timeBase.den);
return static_cast<double>(pts) * timeBase.num / timeBase.den;
}

int64_t secondsToClosestPts(double seconds, const AVRational& timeBase) {
return static_cast<int64_t>(std::round(seconds * timeBase.den));
return static_cast<int64_t>(
std::round(seconds * timeBase.den / timeBase.num));
}

} // namespace
Expand Down Expand Up @@ -151,8 +148,9 @@ void SingleStreamDecoder::initializeDecoder() {
}

if (formatContext_->duration > 0) {
AVRational defaultTimeBase{1, AV_TIME_BASE};
containerMetadata_.durationSeconds =
ptsToSeconds(formatContext_->duration, AV_TIME_BASE);
ptsToSeconds(formatContext_->duration, defaultTimeBase);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by: I've been annoyed by the existence of double ptsToSeconds(int64_t pts, int den), so I'm removing it.

defaultTimeBase corresponds to FFmpeg's AV_TIME_BASE_Q, which we can't use due to a C++ pendantic compilation error.

}

if (formatContext_->bit_rate > 0) {
Expand Down
Loading