Skip to content

Commit 3dd7214

Browse files
committed
Error check in get_frames_at, adjust indices in get_frames_in_range
1 parent e654189 commit 3dd7214

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/torchcodec/decoders/_video_decoder.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,14 @@ def get_frames_at(self, indices: list[int]) -> FrameBatch:
220220
Returns:
221221
FrameBatch: The frames at the given indices.
222222
"""
223-
indices = [
224-
index if index >= 0 else index + self._num_frames for index in indices
225-
]
223+
for i, index in enumerate(indices):
224+
index = index if index >= 0 else index + self._num_frames
225+
if not 0 <= index < self._num_frames:
226+
raise IndexError(
227+
f"Index {index} is out of bounds; must be in the range [0, {self._num_frames})."
228+
)
229+
else:
230+
indices[i] = index
226231

227232
data, pts_seconds, duration_seconds = core.get_frames_at_indices(
228233
self._decoder, frame_indices=indices
@@ -247,6 +252,8 @@ def get_frames_in_range(self, start: int, stop: int, step: int = 1) -> FrameBatc
247252
Returns:
248253
FrameBatch: The frames within the specified range.
249254
"""
255+
start = start if start >= 0 else start + self._num_frames
256+
stop = min(stop if stop >= 0 else stop + self._num_frames, self._num_frames)
250257
if not 0 <= start < self._num_frames:
251258
raise IndexError(
252259
f"Start index {start} is out of bounds; must be in the range [0, {self._num_frames})."

0 commit comments

Comments
 (0)