Skip to content

Commit cad69da

Browse files
committed
Merge branch 'main' of github.com:pytorch/torchcodec into audioooooooo
2 parents 29e0b8d + e52d864 commit cad69da

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
@@ -303,12 +303,11 @@ void VideoDecoder::scanFileAndUpdateMetadataAndIndex() {
303303
}
304304

305305
// Reset the seek-cursor back to the beginning.
306-
int ffmepgStatus =
307-
avformat_seek_file(formatContext_.get(), 0, INT64_MIN, 0, 0, 0);
308-
if (ffmepgStatus < 0) {
306+
int status = avformat_seek_file(formatContext_.get(), 0, INT64_MIN, 0, 0, 0);
307+
if (status < 0) {
309308
throw std::runtime_error(
310309
"Could not seek file to pts=0: " +
311-
getFFMPEGErrorStringFromErrorCode(ffmepgStatus));
310+
getFFMPEGErrorStringFromErrorCode(status));
312311
}
313312

314313
// Sort all frames by their pts.
@@ -967,17 +966,17 @@ void VideoDecoder::maybeSeekToBeforeDesiredPts() {
967966
desiredPts = streamInfo.keyFrames[desiredKeyFrameIndex].pts;
968967
}
969968

970-
int ffmepgStatus = avformat_seek_file(
969+
int status = avformat_seek_file(
971970
formatContext_.get(),
972971
streamInfo.streamIndex,
973972
INT64_MIN,
974973
desiredPts,
975974
desiredPts,
976975
0);
977-
if (ffmepgStatus < 0) {
976+
if (status < 0) {
978977
throw std::runtime_error(
979978
"Could not seek file to pts=" + std::to_string(desiredPts) + ": " +
980-
getFFMPEGErrorStringFromErrorCode(ffmepgStatus));
979+
getFFMPEGErrorStringFromErrorCode(status));
981980
}
982981
decodeStats_.numFlushes++;
983982
avcodec_flush_buffers(streamInfo.codecContext.get());
@@ -1668,6 +1667,9 @@ int64_t VideoDecoder::secondsToIndexLowerBound(double seconds) {
16681667
case SeekMode::approximate: {
16691668
auto& streamMetadata =
16701669
containerMetadata_.allStreamMetadata[activeStreamIndex_];
1670+
TORCH_CHECK(
1671+
streamMetadata.averageFps.has_value(),
1672+
"Cannot use approximate mode since we couldn't find the average fps from the metadata.");
16711673
return std::floor(seconds * streamMetadata.averageFps.value());
16721674
}
16731675
default:
@@ -1692,6 +1694,9 @@ int64_t VideoDecoder::secondsToIndexUpperBound(double seconds) {
16921694
case SeekMode::approximate: {
16931695
auto& streamMetadata =
16941696
containerMetadata_.allStreamMetadata[activeStreamIndex_];
1697+
TORCH_CHECK(
1698+
streamMetadata.averageFps.has_value(),
1699+
"Cannot use approximate mode since we couldn't find the average fps from the metadata.");
16951700
return std::ceil(seconds * streamMetadata.averageFps.value());
16961701
}
16971702
default:
@@ -1707,6 +1712,9 @@ int64_t VideoDecoder::getPts(int64_t frameIndex) {
17071712
case SeekMode::approximate: {
17081713
auto& streamMetadata =
17091714
containerMetadata_.allStreamMetadata[activeStreamIndex_];
1715+
TORCH_CHECK(
1716+
streamMetadata.averageFps.has_value(),
1717+
"Cannot use approximate mode since we couldn't find the average fps from the metadata.");
17101718
return secondsToClosestPts(
17111719
frameIndex / streamMetadata.averageFps.value(), streamInfo.timeBase);
17121720
}
@@ -1723,8 +1731,12 @@ int64_t VideoDecoder::getNumFrames(const StreamMetadata& streamMetadata) {
17231731
switch (seekMode_) {
17241732
case SeekMode::exact:
17251733
return streamMetadata.numFramesFromScan.value();
1726-
case SeekMode::approximate:
1734+
case SeekMode::approximate: {
1735+
TORCH_CHECK(
1736+
streamMetadata.numFrames.has_value(),
1737+
"Cannot use approximate mode since we couldn't find the number of frames from the metadata.");
17271738
return streamMetadata.numFrames.value();
1739+
}
17281740
default:
17291741
throw std::runtime_error("Unknown SeekMode");
17301742
}
@@ -1745,8 +1757,12 @@ double VideoDecoder::getMaxSeconds(const StreamMetadata& streamMetadata) {
17451757
switch (seekMode_) {
17461758
case SeekMode::exact:
17471759
return streamMetadata.maxPtsSecondsFromScan.value();
1748-
case SeekMode::approximate:
1760+
case SeekMode::approximate: {
1761+
TORCH_CHECK(
1762+
streamMetadata.durationSeconds.has_value(),
1763+
"Cannot use approximate mode since we couldn't find the duration from the metadata.");
17491764
return streamMetadata.durationSeconds.value();
1765+
}
17501766
default:
17511767
throw std::runtime_error("Unknown SeekMode");
17521768
}

0 commit comments

Comments
 (0)