-
Notifications
You must be signed in to change notification settings - Fork 46
Support negative index in SimpleVideoDecoder #743
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
Conversation
decoder.get_frames_at([-1]) | ||
expected_converted_index = -10000 + len(decoder) | ||
with pytest.raises( | ||
RuntimeError, match=f"Invalid frame index={expected_converted_index}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error is from SingleStreamDecoder.cpp
. Should we also be checking valid indices at the Python level, in _video_decoder.py
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good observation - at the Python level, we currently don't check the input when we get a list of indices (get_frames_at()
) or a list of timestamps (get_frames_played_at()
). The weak rationale was that we'd have to walk the list to do the check, and that seemed wasteful. Now that we're walking the list in get_frames_at()
to deal with negative indices, we might as well do error checking at the same time.
The question, then, is what to do in get_frames_played_at()
? For completeness, I think it does make sense to do error checking at the Python level there, but let's do that in a separate PR. We can also do some refactoring there, and make _getitem_slice()
call VideoDecoder.get_frames_in_range()
in order to get that error checking at the Python level as well.
@@ -520,8 +549,11 @@ def test_get_frames_at(self, device, seek_mode): | |||
def test_get_frames_at_fails(self, device, seek_mode): | |||
decoder = VideoDecoder(NASA_VIDEO.path, device=device, seek_mode=seek_mode) | |||
|
|||
with pytest.raises(RuntimeError, match="Invalid frame index=-1"): | |||
decoder.get_frames_at([-1]) | |||
expected_converted_index = -10000 + len(decoder) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not necessary to get the expected index here, but I added it to match the other tests. Alternatively, the test can simply match the text: "Invalid frame index".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine to do, but I usually default to just matching the text that doesn't change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do the error checking in get_frames_at()
now too, but approved to unblock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Dan-Flores , LGTM!
I forgot to write it down in #150, but eventually it'd be good to accept the same semantics for get_frames_in_range()
. We we can merge this PR as-is though.
This PR adds the missing functionality described in #150, specifically adding support for negative indexing in
get_frame_at()
andget_frames_at()
.In both functions, a negative index
n
is converted tolen(decoder) - n
. The tests are updated to reflect this support as well.The other item in that issue, allowing "upper bound in slices to be greated than
len(decoder)
", is already supported bygetitem_slice
. Specifically, this section of_video_decoder.py
handles negative and out-of-range indices:I added a new test case to
test_getitem_slice
to make this more explicit.