Skip to content

Commit 939655a

Browse files
committed
Simplify UniqueAVFrame handling in audio decoder
1 parent 5713507 commit 939655a

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,14 +1361,8 @@ void VideoDecoder::convertAudioAVFrameToFrameOutputOnCPU(
13611361
static_cast<AVSampleFormat>(avFrameStream.avFrame->format);
13621362
AVSampleFormat desiredSampleFormat = AV_SAMPLE_FMT_FLTP;
13631363

1364-
UniqueAVFrame convertedAVFrame;
1365-
if (sourceSampleFormat != desiredSampleFormat) {
1366-
convertedAVFrame = convertAudioAVFrameSampleFormat(
1367-
avFrameStream.avFrame, sourceSampleFormat, desiredSampleFormat);
1368-
}
1369-
const UniqueAVFrame& avFrame = (sourceSampleFormat != desiredSampleFormat)
1370-
? convertedAVFrame
1371-
: avFrameStream.avFrame;
1364+
const UniqueAVFrame avFrame = convertAudioAVFrameSampleFormat(
1365+
avFrameStream.avFrame, sourceSampleFormat, desiredSampleFormat);
13721366

13731367
AVSampleFormat format = static_cast<AVSampleFormat>(avFrame->format);
13741368
TORCH_CHECK(
@@ -1395,11 +1389,13 @@ void VideoDecoder::convertAudioAVFrameToFrameOutputOnCPU(
13951389
}
13961390

13971391
UniqueAVFrame VideoDecoder::convertAudioAVFrameSampleFormat(
1398-
const UniqueAVFrame& avFrame,
1392+
UniqueAVFrame& avFrame,
13991393
AVSampleFormat sourceSampleFormat,
1400-
AVSampleFormat desiredSampleFormat
1394+
AVSampleFormat desiredSampleFormat) {
1395+
if (sourceSampleFormat == desiredSampleFormat) {
1396+
return std::move(avFrame);
1397+
}
14011398

1402-
) {
14031399
auto& streamInfo = streamInfos_[activeStreamIndex_];
14041400
const auto& streamMetadata =
14051401
containerMetadata_.allStreamMetadata[activeStreamIndex_];

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ class VideoDecoder {
404404
torch::Tensor& outputTensor);
405405

406406
UniqueAVFrame convertAudioAVFrameSampleFormat(
407-
const UniqueAVFrame& avFrame,
407+
UniqueAVFrame& avFrame,
408408
AVSampleFormat sourceSampleFormat,
409409
AVSampleFormat desiredSampleFormat);
410410

0 commit comments

Comments
 (0)