Skip to content

Commit 9ae0a10

Browse files
authored
Minor cleanup for firstFramePtsSeconds (#583)
1 parent 901e5d5 commit 9ae0a10

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ VideoDecoder::AudioFramesOutput VideoDecoder::getFramesPlayedInRangeAudio(
908908
// sample rate, so in theory we know the number of output samples.
909909
std::vector<torch::Tensor> frames;
910910

911-
double firstFramePtsSeconds = std::numeric_limits<double>::max();
911+
std::optional<double> firstFramePtsSeconds = std::nullopt;
912912
auto stopPts = secondsToClosestPts(stopSeconds, streamInfo.timeBase);
913913
auto finished = false;
914914
while (!finished) {
@@ -918,8 +918,9 @@ VideoDecoder::AudioFramesOutput VideoDecoder::getFramesPlayedInRangeAudio(
918918
return startPts < avFrame->pts + getDuration(avFrame);
919919
});
920920
auto frameOutput = convertAVFrameToFrameOutput(avFrame);
921-
firstFramePtsSeconds =
922-
std::min(firstFramePtsSeconds, frameOutput.ptsSeconds);
921+
if (!firstFramePtsSeconds.has_value()) {
922+
firstFramePtsSeconds = frameOutput.ptsSeconds;
923+
}
923924
frames.push_back(frameOutput.data);
924925
} catch (const EndOfFileException& e) {
925926
finished = true;
@@ -940,7 +941,13 @@ VideoDecoder::AudioFramesOutput VideoDecoder::getFramesPlayedInRangeAudio(
940941
frames.push_back(*lastSamples);
941942
}
942943

943-
return AudioFramesOutput{torch::cat(frames, 1), firstFramePtsSeconds};
944+
TORCH_CHECK(
945+
frames.size() > 0 && firstFramePtsSeconds.has_value(),
946+
"No audio frames were decoded. ",
947+
"This should probably not happen. ",
948+
"Please report an issue on the TorchCodec repo.");
949+
950+
return AudioFramesOutput{torch::cat(frames, 1), *firstFramePtsSeconds};
944951
}
945952

946953
// --------------------------------------------------------------------------

0 commit comments

Comments
 (0)