Skip to content

Fix audio seeks bugs #599

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 27, 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
31 changes: 16 additions & 15 deletions src/torchcodec/decoders/_core/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,16 +890,15 @@ VideoDecoder::AudioFramesOutput VideoDecoder::getFramesPlayedInRangeAudio(
std::optional<double> stopSecondsOptional) {
validateActiveStream(AVMEDIA_TYPE_AUDIO);

double stopSeconds =
stopSecondsOptional.value_or(std::numeric_limits<double>::max());

TORCH_CHECK(
startSeconds <= stopSeconds,
"Start seconds (" + std::to_string(startSeconds) +
") must be less than or equal to stop seconds (" +
std::to_string(stopSeconds) + ").");
if (stopSecondsOptional.has_value()) {
TORCH_CHECK(
startSeconds <= *stopSecondsOptional,
"Start seconds (" + std::to_string(startSeconds) +
") must be less than or equal to stop seconds (" +
std::to_string(*stopSecondsOptional) + ").");
}

if (startSeconds == stopSeconds) {
if (stopSecondsOptional.has_value() && startSeconds == *stopSecondsOptional) {
// For consistency with video
return AudioFramesOutput{torch::empty({0, 0}), 0.0};
}
Expand All @@ -912,7 +911,7 @@ VideoDecoder::AudioFramesOutput VideoDecoder::getFramesPlayedInRangeAudio(
// If we need to seek backwards, then we have to seek back to the beginning
// of the stream.
// See [Audio Decoding Design].
setCursorPtsInSecondsInternal(INT64_MIN);
setCursor(INT64_MIN);
}

// TODO-AUDIO Pre-allocate a long-enough tensor instead of creating a vec +
Expand All @@ -921,7 +920,9 @@ VideoDecoder::AudioFramesOutput VideoDecoder::getFramesPlayedInRangeAudio(
std::vector<torch::Tensor> frames;

std::optional<double> firstFramePtsSeconds = std::nullopt;
auto stopPts = secondsToClosestPts(stopSeconds, streamInfo.timeBase);
auto stopPts = stopSecondsOptional.has_value()
? secondsToClosestPts(*stopSecondsOptional, streamInfo.timeBase)
: INT64_MAX;
auto finished = false;
while (!finished) {
try {
Expand Down Expand Up @@ -971,13 +972,13 @@ void VideoDecoder::setCursorPtsInSeconds(double seconds) {
// We don't allow public audio decoding APIs to seek, see [Audio Decoding
// Design]
validateActiveStream(AVMEDIA_TYPE_VIDEO);
setCursorPtsInSecondsInternal(seconds);
setCursor(
secondsToClosestPts(seconds, streamInfos_[activeStreamIndex_].timeBase));
}

void VideoDecoder::setCursorPtsInSecondsInternal(double seconds) {
void VideoDecoder::setCursor(int64_t pts) {
cursorWasJustSet_ = true;
cursor_ =
secondsToClosestPts(seconds, streamInfos_[activeStreamIndex_].timeBase);
cursor_ = pts;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/torchcodec/decoders/_core/VideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class VideoDecoder {
// DECODING APIS AND RELATED UTILS
// --------------------------------------------------------------------------

void setCursorPtsInSecondsInternal(double seconds);
void setCursor(int64_t pts);
bool canWeAvoidSeeking() const;

void maybeSeekToBeforeDesiredPts();
Expand Down
Loading