Skip to content

Commit 8f4b5c6

Browse files
author
pytorchbot
committed
2025-03-07 nightly release (e52d864)
1 parent 5dd63e2 commit 8f4b5c6

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,11 @@ void VideoDecoder::scanFileAndUpdateMetadataAndIndex() {
289289
}
290290

291291
// Reset the seek-cursor back to the beginning.
292-
int ffmepgStatus =
293-
avformat_seek_file(formatContext_.get(), 0, INT64_MIN, 0, 0, 0);
294-
if (ffmepgStatus < 0) {
292+
int status = avformat_seek_file(formatContext_.get(), 0, INT64_MIN, 0, 0, 0);
293+
if (status < 0) {
295294
throw std::runtime_error(
296295
"Could not seek file to pts=0: " +
297-
getFFMPEGErrorStringFromErrorCode(ffmepgStatus));
296+
getFFMPEGErrorStringFromErrorCode(status));
298297
}
299298

300299
// Sort all frames by their pts.
@@ -923,17 +922,17 @@ void VideoDecoder::maybeSeekToBeforeDesiredPts() {
923922
desiredPts = streamInfo.keyFrames[desiredKeyFrameIndex].pts;
924923
}
925924

926-
int ffmepgStatus = avformat_seek_file(
925+
int status = avformat_seek_file(
927926
formatContext_.get(),
928927
streamInfo.streamIndex,
929928
INT64_MIN,
930929
desiredPts,
931930
desiredPts,
932931
0);
933-
if (ffmepgStatus < 0) {
932+
if (status < 0) {
934933
throw std::runtime_error(
935934
"Could not seek file to pts=" + std::to_string(desiredPts) + ": " +
936-
getFFMPEGErrorStringFromErrorCode(ffmepgStatus));
935+
getFFMPEGErrorStringFromErrorCode(status));
937936
}
938937
decodeStats_.numFlushes++;
939938
avcodec_flush_buffers(streamInfo.codecContext.get());
@@ -1537,6 +1536,9 @@ int64_t VideoDecoder::secondsToIndexLowerBound(double seconds) {
15371536
case SeekMode::approximate: {
15381537
auto& streamMetadata =
15391538
containerMetadata_.allStreamMetadata[activeStreamIndex_];
1539+
TORCH_CHECK(
1540+
streamMetadata.averageFps.has_value(),
1541+
"Cannot use approximate mode since we couldn't find the average fps from the metadata.");
15401542
return std::floor(seconds * streamMetadata.averageFps.value());
15411543
}
15421544
default:
@@ -1561,6 +1563,9 @@ int64_t VideoDecoder::secondsToIndexUpperBound(double seconds) {
15611563
case SeekMode::approximate: {
15621564
auto& streamMetadata =
15631565
containerMetadata_.allStreamMetadata[activeStreamIndex_];
1566+
TORCH_CHECK(
1567+
streamMetadata.averageFps.has_value(),
1568+
"Cannot use approximate mode since we couldn't find the average fps from the metadata.");
15641569
return std::ceil(seconds * streamMetadata.averageFps.value());
15651570
}
15661571
default:
@@ -1576,6 +1581,9 @@ int64_t VideoDecoder::getPts(int64_t frameIndex) {
15761581
case SeekMode::approximate: {
15771582
auto& streamMetadata =
15781583
containerMetadata_.allStreamMetadata[activeStreamIndex_];
1584+
TORCH_CHECK(
1585+
streamMetadata.averageFps.has_value(),
1586+
"Cannot use approximate mode since we couldn't find the average fps from the metadata.");
15791587
return secondsToClosestPts(
15801588
frameIndex / streamMetadata.averageFps.value(), streamInfo.timeBase);
15811589
}
@@ -1592,8 +1600,12 @@ int64_t VideoDecoder::getNumFrames(const StreamMetadata& streamMetadata) {
15921600
switch (seekMode_) {
15931601
case SeekMode::exact:
15941602
return streamMetadata.numFramesFromScan.value();
1595-
case SeekMode::approximate:
1603+
case SeekMode::approximate: {
1604+
TORCH_CHECK(
1605+
streamMetadata.numFrames.has_value(),
1606+
"Cannot use approximate mode since we couldn't find the number of frames from the metadata.");
15961607
return streamMetadata.numFrames.value();
1608+
}
15971609
default:
15981610
throw std::runtime_error("Unknown SeekMode");
15991611
}
@@ -1614,8 +1626,12 @@ double VideoDecoder::getMaxSeconds(const StreamMetadata& streamMetadata) {
16141626
switch (seekMode_) {
16151627
case SeekMode::exact:
16161628
return streamMetadata.maxPtsSecondsFromScan.value();
1617-
case SeekMode::approximate:
1629+
case SeekMode::approximate: {
1630+
TORCH_CHECK(
1631+
streamMetadata.durationSeconds.has_value(),
1632+
"Cannot use approximate mode since we couldn't find the duration from the metadata.");
16181633
return streamMetadata.durationSeconds.value();
1634+
}
16191635
default:
16201636
throw std::runtime_error("Unknown SeekMode");
16211637
}

0 commit comments

Comments
 (0)