Skip to content

Audio: allow next(), disallow seek() #563

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 2 commits into from
Mar 17, 2025
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
13 changes: 10 additions & 3 deletions src/torchcodec/decoders/_core/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,13 +566,15 @@ void VideoDecoder::addAudioStream(int streamIndex) {

VideoDecoder::FrameOutput VideoDecoder::getNextFrame() {
auto output = getNextFrameInternal();
output.data = maybePermuteHWC2CHW(output.data);
if (streamInfos_[activeStreamIndex_].avMediaType == AVMEDIA_TYPE_VIDEO) {
output.data = maybePermuteHWC2CHW(output.data);
}
return output;
}

VideoDecoder::FrameOutput VideoDecoder::getNextFrameInternal(
std::optional<torch::Tensor> preAllocatedOutputTensor) {
validateActiveStream(AVMEDIA_TYPE_VIDEO);
validateActiveStream();
AVFrameStream avFrameStream = decodeAVFrame(
[this](AVFrame* avFrame) { return avFrame->pts >= cursor_; });
return convertAVFrameToFrameOutput(avFrameStream, preAllocatedOutputTensor);
Expand Down Expand Up @@ -868,7 +870,7 @@ VideoDecoder::AudioFramesOutput VideoDecoder::getFramesPlayedInRangeAudio(
// If we need to seek backwards, then we have to seek back to the beginning
// of the stream.
// TODO-AUDIO: document why this is needed in a big comment.
setCursorPtsInSeconds(INT64_MIN);
setCursorPtsInSecondsInternal(INT64_MIN);
}

// TODO-AUDIO Pre-allocate a long-enough tensor instead of creating a vec +
Expand Down Expand Up @@ -914,6 +916,11 @@ VideoDecoder::AudioFramesOutput VideoDecoder::getFramesPlayedInRangeAudio(
// --------------------------------------------------------------------------

void VideoDecoder::setCursorPtsInSeconds(double seconds) {
validateActiveStream(AVMEDIA_TYPE_VIDEO);
setCursorPtsInSecondsInternal(seconds);
}

void VideoDecoder::setCursorPtsInSecondsInternal(double seconds) {
cursorWasJustSet_ = true;
cursor_ =
secondsToClosestPts(seconds, streamInfos_[activeStreamIndex_].timeBase);
Expand Down
1 change: 1 addition & 0 deletions src/torchcodec/decoders/_core/VideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ class VideoDecoder {
// DECODING APIS AND RELATED UTILS
// --------------------------------------------------------------------------

void setCursorPtsInSecondsInternal(double seconds);
bool canWeAvoidSeeking() const;

void maybeSeekToBeforeDesiredPts();
Expand Down
18 changes: 17 additions & 1 deletion test/decoders/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ class TestAudioOps:
partial(get_frames_in_range, start=4, stop=5),
partial(get_frame_at_pts, seconds=2),
partial(get_frames_by_pts, timestamps=[0, 1.5]),
partial(get_next_frame),
partial(seek_to_pts, seconds=5),
),
)
def test_audio_bad_method(self, method):
Expand All @@ -642,6 +642,22 @@ def test_audio_bad_seek_mode(self):
):
add_audio_stream(decoder)

@pytest.mark.parametrize("asset", (NASA_AUDIO, NASA_AUDIO_MP3))
def test_next(self, asset):
decoder = create_from_file(str(asset.path), seek_mode="approximate")
add_audio_stream(decoder)

frame_index = 0
while True:
try:
frame, *_ = get_next_frame(decoder)
except IndexError:
break
torch.testing.assert_close(
frame, asset.get_frame_data_by_index(frame_index)
)
frame_index += 1

@pytest.mark.parametrize(
"range",
(
Expand Down
Loading