Skip to content

Commit 9af4bc8

Browse files
committed
WI:P
1 parent 2d97555 commit 9af4bc8

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,10 +1365,20 @@ void VideoDecoder::convertAudioAVFrameToFrameOutputOnCPU(
13651365
static_cast<AVSampleFormat>(avFrameStream.avFrame->format);
13661366
AVSampleFormat desiredSampleFormat = AV_SAMPLE_FMT_FLTP;
13671367

1368+
int sourceSampleRate = avFrameStream.avFrame->sample_rate;
1369+
int desiredSampleRate =
1370+
streamInfos_[activeStreamIndex_].audioStreamOptions.sampleRate.value_or(
1371+
sourceSampleRate);
1372+
13681373
UniqueAVFrame convertedAVFrame;
1369-
if (sourceSampleFormat != desiredSampleFormat) {
1370-
convertedAVFrame = convertAudioAVFrameSampleFormat(
1371-
avFrameStream.avFrame, sourceSampleFormat, desiredSampleFormat);
1374+
if (sourceSampleFormat != desiredSampleFormat ||
1375+
sourceSampleRate != desiredSampleRate) {
1376+
convertedAVFrame = convertAudioAVFrameSampleFormatAndSampleRate(
1377+
avFrameStream.avFrame,
1378+
sourceSampleFormat,
1379+
desiredSampleFormat,
1380+
sourceSampleRate,
1381+
desiredSampleRate);
13721382
}
13731383
const UniqueAVFrame& avFrame = (sourceSampleFormat != desiredSampleFormat)
13741384
? convertedAVFrame
@@ -1398,12 +1408,12 @@ void VideoDecoder::convertAudioAVFrameToFrameOutputOnCPU(
13981408
frameOutput.data = outputData;
13991409
}
14001410

1401-
UniqueAVFrame VideoDecoder::convertAudioAVFrameSampleFormat(
1411+
UniqueAVFrame VideoDecoder::convertAudioAVFrameSampleFormatAndSampleRate(
14021412
const UniqueAVFrame& avFrame,
14031413
AVSampleFormat sourceSampleFormat,
1404-
AVSampleFormat desiredSampleFormat
1405-
1406-
) {
1414+
AVSampleFormat desiredSampleFormat,
1415+
int sourceSampleRate,
1416+
int desiredSampleRate) {
14071417
auto& streamInfo = streamInfos_[activeStreamIndex_];
14081418
const auto& streamMetadata =
14091419
containerMetadata_.allStreamMetadata[activeStreamIndex_];

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ class VideoDecoder {
142142
struct AudioStreamOptions {
143143
AudioStreamOptions() {}
144144

145-
// explicit AudioStreamOptions(const std::string& optionsString);
146145
std::optional<int> sampleRate;
147146
};
148147

@@ -413,10 +412,12 @@ class VideoDecoder {
413412
const AVFrame* avFrame,
414413
torch::Tensor& outputTensor);
415414

416-
UniqueAVFrame convertAudioAVFrameSampleFormat(
415+
UniqueAVFrame convertAudioAVFrameSampleFormatAndSampleRate(
417416
const UniqueAVFrame& avFrame,
418417
AVSampleFormat sourceSampleFormat,
419-
AVSampleFormat desiredSampleFormat);
418+
AVSampleFormat desiredSampleFormat,
419+
int sourceSampleRate,
420+
int desiredSampleRate);
420421

421422
// --------------------------------------------------------------------------
422423
// COLOR CONVERSION LIBRARIES HANDLERS CREATION

0 commit comments

Comments
 (0)