Skip to content
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
15 changes: 10 additions & 5 deletions src/torchcodec/decoders/_core/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1522,17 +1522,22 @@ std::optional<torch::Tensor> VideoDecoder::maybeFlushSwrBuffers() {
return std::nullopt;
}

torch::Tensor lastSamples = torch::empty(
{getNumChannels(streamInfo.codecContext), numRemainingSamples},
torch::kFloat32);
uint8_t* lastSamplesData = static_cast<uint8_t*>(lastSamples.data_ptr());
auto numChannels = getNumChannels(streamInfo.codecContext);
torch::Tensor lastSamples =
torch::empty({numChannels, numRemainingSamples}, torch::kFloat32);

std::vector<uint8_t*> outputBuffers(numChannels);
for (auto i = 0; i < numChannels; i++) {
outputBuffers[i] = static_cast<uint8_t*>(lastSamples[i].data_ptr());
}

auto actualNumRemainingSamples = swr_convert(
streamInfo.swrContext.get(),
&lastSamplesData,
outputBuffers.data(),
numRemainingSamples,
nullptr,
0);

return lastSamples.narrow(
/*dim=*/1, /*start=*/0, /*length=*/actualNumRemainingSamples);
}
Expand Down
8 changes: 8 additions & 0 deletions test/decoders/test_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,14 @@ def test_sample_rate_conversion(self, start_seconds, stop_seconds):
rtol=rtol,
)

def test_sample_rate_conversion_stereo(self):
# Non-regression test for https://github.com/pytorch/torchcodec/pull/584
asset = NASA_AUDIO_MP3
assert asset.sample_rate == 8000
assert asset.num_channels == 2
decoder = AudioDecoder(asset.path, sample_rate=44_100)
decoder.get_samples_played_in_range(start_seconds=0)

def test_s16_ffmpeg4_bug(self):
# s16 fails on FFmpeg4 but can be decoded on other versions.
# Debugging logs show that we're hitting:
Expand Down
Loading