Skip to content

Commit 27718be

Browse files
author
Molly Xu
committed
update pr to reflect refactor
1 parent 95eedb1 commit 27718be

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

src/torchcodec/_core/Metadata.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ std::optional<double> StreamMetadata::getDurationSeconds(
2929
return static_cast<double>(numFramesFromHeader.value()) /
3030
averageFpsFromHeader.value();
3131
}
32+
if (durationSecondsFromContainer.has_value()) {
33+
return durationSecondsFromContainer.value();
34+
}
3235
return std::nullopt;
3336
default:
3437
TORCH_CHECK(false, "Unknown SeekMode");
@@ -80,13 +83,13 @@ std::optional<int64_t> StreamMetadata::getNumFrames(SeekMode seekMode) const {
8083
numFramesFromContent.has_value(), "Missing numFramesFromContent");
8184
return numFramesFromContent.value();
8285
case SeekMode::approximate: {
86+
auto durationSeconds = getDurationSeconds(seekMode);
8387
if (numFramesFromHeader.has_value()) {
8488
return numFramesFromHeader.value();
8589
}
86-
if (averageFpsFromHeader.has_value() &&
87-
durationSecondsFromHeader.has_value()) {
90+
if (averageFpsFromHeader.has_value() && durationSeconds.has_value()) {
8891
return static_cast<int64_t>(
89-
averageFpsFromHeader.value() * durationSecondsFromHeader.value());
92+
averageFpsFromHeader.value() * durationSeconds.value());
9093
}
9194
return std::nullopt;
9295
}

src/torchcodec/_core/Metadata.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ struct StreamMetadata {
3535
std::optional<double> averageFpsFromHeader;
3636
std::optional<double> bitRate;
3737

38+
// Used as fallback in approximate mode when stream duration is unavailable.
39+
std::optional<double> durationSecondsFromContainer;
40+
3841
// More accurate duration, obtained by scanning the file.
3942
// These presentation timestamps are in time base.
4043
std::optional<int64_t> beginStreamPtsFromContent;

src/torchcodec/_core/SingleStreamDecoder.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,6 @@ void SingleStreamDecoder::initializeDecoder() {
158158
ptsToSeconds(formatContext_->duration, defaultTimeBase);
159159
}
160160

161-
// Use container duration as fallback for streams missing stream duration
162-
if (containerMetadata_.durationSecondsFromHeader.has_value()) {
163-
for (auto& streamMetadata : containerMetadata_.allStreamMetadata) {
164-
if (!streamMetadata.durationSecondsFromHeader.has_value()) {
165-
streamMetadata.durationSecondsFromHeader =
166-
containerMetadata_.durationSecondsFromHeader;
167-
}
168-
}
169-
}
170-
171161
if (formatContext_->bit_rate > 0) {
172162
containerMetadata_.bitRate = formatContext_->bit_rate;
173163
}
@@ -182,6 +172,13 @@ void SingleStreamDecoder::initializeDecoder() {
182172
containerMetadata_.bestAudioStreamIndex = bestAudioStream;
183173
}
184174

175+
if (containerMetadata_.durationSecondsFromHeader.has_value()) {
176+
for (auto& streamMetadata : containerMetadata_.allStreamMetadata) {
177+
streamMetadata.durationSecondsFromContainer =
178+
containerMetadata_.durationSecondsFromHeader;
179+
}
180+
}
181+
185182
if (seekMode_ == SeekMode::exact) {
186183
scanFileAndUpdateMetadataAndIndex();
187184
}

0 commit comments

Comments
 (0)