Skip to content

Commit abb647f

Browse files
committed
Simplify secondsToIndex* and getPts methods
1 parent b80757e commit abb647f

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ VideoDecoder::FrameOutput VideoDecoder::getFrameAtIndexInternal(
562562
containerMetadata_.allStreamMetadata[activeStreamIndex_];
563563
validateFrameIndex(streamMetadata, frameIndex);
564564

565-
int64_t pts = getPts(streamInfo, streamMetadata, frameIndex);
565+
int64_t pts = getPts(frameIndex);
566566
setCursorPtsInSeconds(ptsToSeconds(pts, streamInfo.timeBase));
567567
return getNextFrameInternal(preAllocatedOutputTensor);
568568
}
@@ -721,8 +721,7 @@ VideoDecoder::FrameBatchOutput VideoDecoder::getFramesPlayedAt(
721721
"; must be in range [" + std::to_string(minSeconds) + ", " +
722722
std::to_string(maxSeconds) + ").");
723723

724-
frameIndices[i] =
725-
secondsToIndexLowerBound(frameSeconds);
724+
frameIndices[i] = secondsToIndexLowerBound(frameSeconds);
726725
}
727726

728727
return getFramesAtIndices(frameIndices);
@@ -793,10 +792,8 @@ VideoDecoder::FrameBatchOutput VideoDecoder::getFramesPlayedInRange(
793792
// particular frame, we need to figure out if it is ordered after the
794793
// frame's pts, but before the next frames's pts.
795794

796-
int64_t startFrameIndex =
797-
secondsToIndexLowerBound(startSeconds);
798-
int64_t stopFrameIndex =
799-
secondsToIndexUpperBound(stopSeconds, streamInfo, streamMetadata);
795+
int64_t startFrameIndex = secondsToIndexLowerBound(startSeconds);
796+
int64_t stopFrameIndex = secondsToIndexUpperBound(stopSeconds);
800797
int64_t numFrames = stopFrameIndex - startFrameIndex;
801798

802799
FrameBatchOutput frameBatchOutput(
@@ -1513,18 +1510,17 @@ int64_t VideoDecoder::secondsToIndexLowerBound(double seconds) {
15131510
return frame - streamInfo.allFrames.begin();
15141511
}
15151512
case SeekMode::approximate: {
1516-
auto& streamMetadata = containerMetadata_.allStreamMetadata[activeStreamIndex_];
1513+
auto& streamMetadata =
1514+
containerMetadata_.allStreamMetadata[activeStreamIndex_];
15171515
return std::floor(seconds * streamMetadata.averageFps.value());
15181516
}
15191517
default:
15201518
throw std::runtime_error("Unknown SeekMode");
15211519
}
15221520
}
15231521

1524-
int64_t VideoDecoder::secondsToIndexUpperBound(
1525-
double seconds,
1526-
const StreamInfo& streamInfo,
1527-
const StreamMetadata& streamMetadata) {
1522+
int64_t VideoDecoder::secondsToIndexUpperBound(double seconds) {
1523+
auto& streamInfo = streamInfos_[activeStreamIndex_];
15281524
switch (seekMode_) {
15291525
case SeekMode::exact: {
15301526
auto frame = std::upper_bound(
@@ -1537,23 +1533,27 @@ int64_t VideoDecoder::secondsToIndexUpperBound(
15371533

15381534
return frame - streamInfo.allFrames.begin();
15391535
}
1540-
case SeekMode::approximate:
1536+
case SeekMode::approximate: {
1537+
auto& streamMetadata =
1538+
containerMetadata_.allStreamMetadata[activeStreamIndex_];
15411539
return std::ceil(seconds * streamMetadata.averageFps.value());
1540+
}
15421541
default:
15431542
throw std::runtime_error("Unknown SeekMode");
15441543
}
15451544
}
15461545

1547-
int64_t VideoDecoder::getPts(
1548-
const StreamInfo& streamInfo,
1549-
const StreamMetadata& streamMetadata,
1550-
int64_t frameIndex) {
1546+
int64_t VideoDecoder::getPts(int64_t frameIndex) {
1547+
auto& streamInfo = streamInfos_[activeStreamIndex_];
15511548
switch (seekMode_) {
15521549
case SeekMode::exact:
15531550
return streamInfo.allFrames[frameIndex].pts;
1554-
case SeekMode::approximate:
1551+
case SeekMode::approximate: {
1552+
auto& streamMetadata =
1553+
containerMetadata_.allStreamMetadata[activeStreamIndex_];
15551554
return secondsToClosestPts(
15561555
frameIndex / streamMetadata.averageFps.value(), streamInfo.timeBase);
1556+
}
15571557
default:
15581558
throw std::runtime_error("Unknown SeekMode");
15591559
}

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,15 +416,9 @@ class VideoDecoder {
416416

417417
int64_t secondsToIndexLowerBound(double seconds);
418418

419-
int64_t secondsToIndexUpperBound(
420-
double seconds,
421-
const StreamInfo& streamInfo,
422-
const StreamMetadata& streamMetadata);
419+
int64_t secondsToIndexUpperBound(double seconds);
423420

424-
int64_t getPts(
425-
const StreamInfo& streamInfo,
426-
const StreamMetadata& streamMetadata,
427-
int64_t frameIndex);
421+
int64_t getPts(int64_t frameIndex);
428422

429423
// --------------------------------------------------------------------------
430424
// STREAM AND METADATA APIS

0 commit comments

Comments
 (0)