Skip to content

Commit 20a7227

Browse files
author
Molly Xu
committed
address feedback
1 parent 35febfb commit 20a7227

File tree

3 files changed

+15
-34
lines changed

3 files changed

+15
-34
lines changed

src/torchcodec/_core/SingleStreamDecoder.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ FrameOutput SingleStreamDecoder::getFrameAtIndexInternal(
615615
const auto& streamMetadata =
616616
containerMetadata_.allStreamMetadata[activeStreamIndex_];
617617

618-
std::optional<int64_t> numFrames = getNumFrames(streamMetadata);
618+
std::optional<int64_t> numFrames = streamMetadata.getNumFrames(seekMode_);
619619
if (numFrames.has_value()) {
620620
// If the frameIndex is negative, we convert it to a positive index
621621
frameIndex = frameIndex >= 0 ? frameIndex : frameIndex + numFrames.value();
@@ -709,7 +709,7 @@ FrameBatchOutput SingleStreamDecoder::getFramesInRange(
709709

710710
// Note that if we do not have the number of frames available in our
711711
// metadata, then we assume that the upper part of the range is valid.
712-
std::optional<int64_t> numFrames = getNumFrames(streamMetadata);
712+
std::optional<int64_t> numFrames = streamMetadata.getNumFrames(seekMode_);
713713
if (numFrames.has_value()) {
714714
TORCH_CHECK(
715715
stop <= numFrames.value(),
@@ -783,8 +783,9 @@ FrameBatchOutput SingleStreamDecoder::getFramesPlayedAt(
783783
const auto& streamMetadata =
784784
containerMetadata_.allStreamMetadata[activeStreamIndex_];
785785

786-
double minSeconds = getMinSeconds(streamMetadata);
787-
std::optional<double> maxSeconds = getMaxSeconds(streamMetadata);
786+
double minSeconds = streamMetadata.getBeginStreamSeconds(seekMode_);
787+
std::optional<double> maxSeconds =
788+
streamMetadata.getEndStreamSeconds(seekMode_);
788789

789790
// The frame played at timestamp t and the one played at timestamp `t +
790791
// eps` are probably the same frame, with the same index. The easiest way to
@@ -861,7 +862,7 @@ FrameBatchOutput SingleStreamDecoder::getFramesPlayedInRange(
861862
return frameBatchOutput;
862863
}
863864

864-
double minSeconds = getMinSeconds(streamMetadata);
865+
double minSeconds = streamMetadata.getBeginStreamSeconds(seekMode_);
865866
TORCH_CHECK(
866867
startSeconds >= minSeconds,
867868
"Start seconds is " + std::to_string(startSeconds) +
@@ -870,7 +871,8 @@ FrameBatchOutput SingleStreamDecoder::getFramesPlayedInRange(
870871

871872
// Note that if we can't determine the maximum seconds from the metadata,
872873
// then we assume upper range is valid.
873-
std::optional<double> maxSeconds = getMaxSeconds(streamMetadata);
874+
std::optional<double> maxSeconds =
875+
streamMetadata.getEndStreamSeconds(seekMode_);
874876
if (maxSeconds.has_value()) {
875877
TORCH_CHECK(
876878
startSeconds < maxSeconds.value(),
@@ -1443,21 +1445,6 @@ int64_t SingleStreamDecoder::getPts(int64_t frameIndex) {
14431445
// STREAM AND METADATA APIS
14441446
// --------------------------------------------------------------------------
14451447

1446-
std::optional<int64_t> SingleStreamDecoder::getNumFrames(
1447-
const StreamMetadata& streamMetadata) {
1448-
return streamMetadata.getNumFrames(seekMode_);
1449-
}
1450-
1451-
double SingleStreamDecoder::getMinSeconds(
1452-
const StreamMetadata& streamMetadata) {
1453-
return streamMetadata.getBeginStreamSeconds(seekMode_);
1454-
}
1455-
1456-
std::optional<double> SingleStreamDecoder::getMaxSeconds(
1457-
const StreamMetadata& streamMetadata) {
1458-
return streamMetadata.getEndStreamSeconds(seekMode_);
1459-
}
1460-
14611448
// --------------------------------------------------------------------------
14621449
// VALIDATION UTILS
14631450
// --------------------------------------------------------------------------
@@ -1507,7 +1494,7 @@ void SingleStreamDecoder::validateFrameIndex(
15071494

15081495
// Note that if we do not have the number of frames available in our
15091496
// metadata, then we assume that the frameIndex is valid.
1510-
std::optional<int64_t> numFrames = getNumFrames(streamMetadata);
1497+
std::optional<int64_t> numFrames = streamMetadata.getNumFrames(seekMode_);
15111498
if (numFrames.has_value()) {
15121499
if (frameIndex >= numFrames.value()) {
15131500
throw std::out_of_range(

src/torchcodec/_core/SingleStreamDecoder.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,6 @@ class SingleStreamDecoder {
314314
// index. Note that this index may be truncated for some files.
315315
int getBestStreamIndex(AVMediaType mediaType);
316316

317-
std::optional<int64_t> getNumFrames(const StreamMetadata& streamMetadata);
318-
double getMinSeconds(const StreamMetadata& streamMetadata);
319-
std::optional<double> getMaxSeconds(const StreamMetadata& streamMetadata);
320-
321317
// --------------------------------------------------------------------------
322318
// VALIDATION UTILS
323319
// --------------------------------------------------------------------------

src/torchcodec/_core/_metadata.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ class StreamMetadata:
4040

4141
# Computed fields (computed in C++ with fallback logic)
4242
duration_seconds: Optional[float]
43-
"""Duration of the stream in seconds. Computed in C++ with fallback logic:
44-
tries to calculate from content if scan was performed, otherwise falls back
45-
to header values."""
43+
"""Duration of the stream in seconds. Tries to calculate from content
44+
if :term:`scan` was performed, otherwise falls back to header values."""
4645
begin_stream_seconds: Optional[float]
47-
"""Beginning of the stream, in seconds. Computed in C++ with fallback logic."""
46+
"""Beginning of the stream, in seconds."""
4847

4948
def __repr__(self):
5049
s = self.__class__.__name__ + ":\n"
@@ -98,15 +97,14 @@ class VideoStreamMetadata(StreamMetadata):
9897
# Computed fields (computed in C++ with fallback logic)
9998
end_stream_seconds: Optional[float]
10099
"""End of the stream, in seconds (float or None).
101-
Conceptually, this corresponds to last_frame.pts + last_frame.duration.
102-
Computed in C++ with fallback logic."""
100+
Conceptually, this corresponds to last_frame.pts + last_frame.duration."""
103101
num_frames: Optional[int]
104102
"""Number of frames in the stream (int or None).
105-
Computed in C++ with fallback logic: uses content if scan was performed,
103+
Uses content if :term:`scan` was performed,
106104
otherwise falls back to header values or calculates from duration and fps."""
107105
average_fps: Optional[float]
108106
"""Average fps of the stream (float or None).
109-
Computed in C++ with fallback logic: if scan was performed, computes from
107+
if :term:`scan` was performed, computes from
110108
num_frames and duration, otherwise uses header value."""
111109

112110

0 commit comments

Comments
 (0)