Skip to content

Commit 5013118

Browse files
authored
Fix pts <-> seconds conversions (#682)
1 parent 721c315 commit 5013118

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/torchcodec/_core/SingleStreamDecoder.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717
namespace facebook::torchcodec {
1818
namespace {
1919

20-
double ptsToSeconds(int64_t pts, int den) {
21-
return static_cast<double>(pts) / den;
22-
}
23-
2420
double ptsToSeconds(int64_t pts, const AVRational& timeBase) {
25-
return ptsToSeconds(pts, timeBase.den);
21+
return static_cast<double>(pts) * timeBase.num / timeBase.den;
2622
}
2723

2824
int64_t secondsToClosestPts(double seconds, const AVRational& timeBase) {
29-
return static_cast<int64_t>(std::round(seconds * timeBase.den));
25+
return static_cast<int64_t>(
26+
std::round(seconds * timeBase.den / timeBase.num));
3027
}
3128

3229
} // namespace
@@ -151,8 +148,9 @@ void SingleStreamDecoder::initializeDecoder() {
151148
}
152149

153150
if (formatContext_->duration > 0) {
151+
AVRational defaultTimeBase{1, AV_TIME_BASE};
154152
containerMetadata_.durationSeconds =
155-
ptsToSeconds(formatContext_->duration, AV_TIME_BASE);
153+
ptsToSeconds(formatContext_->duration, defaultTimeBase);
156154
}
157155

158156
if (formatContext_->bit_rate > 0) {

0 commit comments

Comments
 (0)