Skip to content

Fix pts -> index conversion in get_frame_at_timestamps[_in_range] #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/torchcodec/decoders/_core/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,12 +1125,6 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesDisplayedByTimestamps(
return ptsToSeconds(info.nextPts, stream.timeBase) <= framePts;
});
int64_t frameIndex = it - stream.allFrames.begin();
// If the frame index is larger than the size of allFrames, that means we
// couldn't match the pts value to the pts value of a NEXT FRAME. And
// that means that this timestamp falls during the time between when the
// last frame is displayed, and the video ends. Hence, it should map to the
// index of the last frame.
frameIndex = std::min(frameIndex, (int64_t)stream.allFrames.size() - 1);
frameIndices[i] = frameIndex;
}

Expand Down
7 changes: 6 additions & 1 deletion src/torchcodec/decoders/_core/VideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,12 @@ class VideoDecoder {
private:
struct FrameInfo {
int64_t pts = 0;
int64_t nextPts = 0;
// The value of this default is important: the last frame's nextPts will be
// INT64_MAX, which ensures that the allFrames vec contains FrameInfo
// structs with *increasing* nextPts values. That's a necessary condition
// for the binary searches on those values to work properly (as typically
// done during pts -> index conversions.)
int64_t nextPts = INT64_MAX;
};
struct FilterState {
UniqueAVFilterGraph filterGraph;
Expand Down
60 changes: 60 additions & 0 deletions test/decoders/test_video_decoder_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,66 @@ def test_get_frames_by_pts(self):
with pytest.raises(AssertionError):
assert_tensor_equal(frames[0], frames[-1])

def test_pts_apis_against_index_ref(self):
# Non-regression test for https://github.com/pytorch/torchcodec/pull/287
# Get all frames in the video, then query all frames with all time-based
# APIs exactly where those frames are supposed to start. We assert that
# we get the expected frame.
decoder = create_from_file(str(NASA_VIDEO.path))
scan_all_streams_to_update_metadata(decoder)
add_video_stream(decoder)

metadata = get_json_metadata(decoder)
metadata_dict = json.loads(metadata)
num_frames = metadata_dict["numFrames"]
assert num_frames == 390

stream_index = 3
_, all_pts_seconds_ref, _ = zip(
*[
get_frame_at_index(
decoder, stream_index=stream_index, frame_index=frame_index
)
for frame_index in range(num_frames)
]
)
all_pts_seconds_ref = torch.tensor(all_pts_seconds_ref)

assert len(all_pts_seconds_ref.unique() == len(all_pts_seconds_ref))

_, pts_seconds, _ = zip(
*[get_frame_at_pts(decoder, seconds=pts) for pts in all_pts_seconds_ref]
)
pts_seconds = torch.tensor(pts_seconds)
assert_tensor_equal(pts_seconds, all_pts_seconds_ref)

_, pts_seconds, _ = get_frames_by_pts_in_range(
decoder,
stream_index=stream_index,
start_seconds=0,
stop_seconds=all_pts_seconds_ref[-1] + 1e-4,
)
assert_tensor_equal(pts_seconds, all_pts_seconds_ref)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: all test-cases above were passing on main.

The ones below this line were failing. We never caught the one on get_frames_by_pts_in_range because it was only triggered when start_seconds was later than the second-to-last frame. And the error message is quite.... interesting!

    def __call__(self, /, *args, **kwargs):
>       return self._op(*args, **kwargs)
E       RuntimeError: Trying to create tensor with negative dimension -1: [-1, 270, 480, 3]

_, pts_seconds, _ = zip(
*[
get_frames_by_pts_in_range(
decoder,
stream_index=stream_index,
start_seconds=pts,
stop_seconds=pts + 1e-4,
)
for pts in all_pts_seconds_ref
]
)
pts_seconds = torch.tensor(pts_seconds)
assert_tensor_equal(pts_seconds, all_pts_seconds_ref)

_, pts_seconds, _ = get_frames_by_pts(
decoder, stream_index=stream_index, timestamps=all_pts_seconds_ref.tolist()
)
assert_tensor_equal(pts_seconds, all_pts_seconds_ref)

def test_get_frames_in_range(self):
decoder = create_from_file(str(NASA_VIDEO.path))
scan_all_streams_to_update_metadata(decoder)
Expand Down
Loading