Skip to content

Commit af4e88a

Browse files
committed
Nit
1 parent 7b09315 commit af4e88a

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

src/torchcodec/decoders/_audio_decoder.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ def __init__(
4242
decoder=self._decoder, stream_index=stream_index, media_type="audio"
4343
)
4444
assert isinstance(self.metadata, core.AudioStreamMetadata) # mypy
45-
self._source_sample_rate = self.metadata.sample_rate
4645
self._desired_sample_rate = (
47-
sample_rate if sample_rate is not None else self._source_sample_rate
46+
sample_rate if sample_rate is not None else self.metadata.sample_rate
4847
)
4948

5049
def get_samples_played_in_range(

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,9 +1431,9 @@ UniqueAVFrame VideoDecoder::convertAudioAVFrameSampleFormatAndSampleRate(
14311431
if (sourceSampleRate != desiredSampleRate) {
14321432
// Note that this is an upper bound on the number of output samples.
14331433
// `swr_convert()` will likely not fill convertedAVFrame with that many
1434-
// samples, it will buffer the last few ones because those require future
1435-
// samples. That's also why we reset nb_samples after the call to
1436-
// `swr_convert()`.
1434+
// samples if sample rate conversion is needed. It will buffer the last few
1435+
// ones because those require future samples. That's also why we reset
1436+
// nb_samples after the call to `swr_convert()`.
14371437
convertedAVFrame->nb_samples = av_rescale_rnd(
14381438
swr_get_delay(streamInfo.swrContext.get(), sourceSampleRate) +
14391439
srcAVFrame->nb_samples,
@@ -1464,7 +1464,6 @@ UniqueAVFrame VideoDecoder::convertAudioAVFrameSampleFormatAndSampleRate(
14641464

14651465
// See comment above about nb_samples
14661466
convertedAVFrame->nb_samples = numConvertedSamples;
1467-
// TODO need to flush properly to retrieve the last few samples.
14681467

14691468
return convertedAVFrame;
14701469
}

test/decoders/test_decoders.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,8 +1104,11 @@ def test_format_conversion(self):
11041104
def test_sample_rate_conversion(self, start_seconds, stop_seconds):
11051105
# When start_seconds is not exactly 0, we have to increase the tolerance
11061106
# a bit. This is because sample_rate conversion relies on a sliding
1107-
# window of samples: if we start a stream in the middle, the first few
1108-
# samples aren't able to take advantage of the preceeding samples.
1107+
# window of samples: if we start decoding a stream in the middle, the
1108+
# first few samples we're decoding aren't able to take advantage of the
1109+
# preceeding samples for sample-rate conversion. This leads to a
1110+
# slightly different sample-rate conversion that we would otherwise get,
1111+
# had we started the stream from the beginning.
11091112
atol = 1e-4 if start_seconds == 0 else 1e-2
11101113
rtol = 1e-6
11111114

0 commit comments

Comments
 (0)