Skip to content

Commit 8378487

Browse files
committed
Don't use raw pointer
1 parent 640a82d commit 8378487

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

src/torchcodec/decoders/_core/FFMPEGCommon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ int64_t getDuration(const AVFrame* frame) {
6060
#endif
6161
}
6262

63-
int getNumChannels(const AVFrame* avFrame) {
63+
int getNumChannels(const UniqueAVFrame& avFrame) {
6464
#if LIBAVFILTER_VERSION_MAJOR > 8 || \
6565
(LIBAVFILTER_VERSION_MAJOR == 8 && LIBAVFILTER_VERSION_MINOR >= 44)
6666
return avFrame->ch_layout.nb_channels;

src/torchcodec/decoders/_core/FFMPEGCommon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ std::string getFFMPEGErrorStringFromErrorCode(int errorCode);
142142
int64_t getDuration(const UniqueAVFrame& frame);
143143
int64_t getDuration(const AVFrame* frame);
144144

145-
int getNumChannels(const AVFrame* avFrame);
145+
int getNumChannels(const UniqueAVFrame& avFrame);
146146
int getNumChannels(const UniqueAVCodecContext& avCodecContext);
147147

148148
// Returns true if sws_scale can handle unaligned data.

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,22 +1351,20 @@ void VideoDecoder::convertAudioAVFrameToFrameOutputOnCPU(
13511351
!preAllocatedOutputTensor.has_value(),
13521352
"pre-allocated audio tensor not supported yet.");
13531353

1354-
const UniqueAVFrame& avFrame = avFrameStream.avFrame;
1355-
13561354
AVSampleFormat sourceSampleFormat =
1357-
static_cast<AVSampleFormat>(avFrame->format);
1355+
static_cast<AVSampleFormat>(avFrameStream.avFrame->format);
13581356
AVSampleFormat desiredSampleFormat = AV_SAMPLE_FMT_FLTP;
1359-
AVFrame* rawAVFrame = nullptr;
1357+
13601358
UniqueAVFrame convertedAVFrame;
1361-
if (sourceSampleFormat == desiredSampleFormat) {
1362-
rawAVFrame = avFrame.get();
1363-
} else {
1359+
if (sourceSampleFormat != desiredSampleFormat) {
13641360
convertedAVFrame = convertAudioAVFrameSampleFormat(
1365-
avFrame, sourceSampleFormat, desiredSampleFormat);
1366-
rawAVFrame = convertedAVFrame.get();
1361+
avFrameStream.avFrame, sourceSampleFormat, desiredSampleFormat);
13671362
}
1363+
const UniqueAVFrame& avFrame = (sourceSampleFormat != desiredSampleFormat)
1364+
? convertedAVFrame
1365+
: avFrameStream.avFrame;
13681366

1369-
AVSampleFormat format = static_cast<AVSampleFormat>(rawAVFrame->format);
1367+
AVSampleFormat format = static_cast<AVSampleFormat>(avFrame->format);
13701368
TORCH_CHECK(
13711369
format == desiredSampleFormat,
13721370
"Something went wrong, the frame didn't get converted to the desired format. ",
@@ -1375,8 +1373,8 @@ void VideoDecoder::convertAudioAVFrameToFrameOutputOnCPU(
13751373
"source format = ",
13761374
av_get_sample_fmt_name(format));
13771375

1378-
auto numSamples = rawAVFrame->nb_samples; // per channel
1379-
auto numChannels = getNumChannels(rawAVFrame);
1376+
auto numSamples = avFrame->nb_samples; // per channel
1377+
auto numChannels = getNumChannels(avFrame);
13801378
torch::Tensor outputData =
13811379
torch::empty({numChannels, numSamples}, torch::kFloat32);
13821380

@@ -1385,9 +1383,7 @@ void VideoDecoder::convertAudioAVFrameToFrameOutputOnCPU(
13851383
for (auto channel = 0; channel < numChannels;
13861384
++channel, outputChannelData += numBytesPerChannel) {
13871385
memcpy(
1388-
outputChannelData,
1389-
rawAVFrame->extended_data[channel],
1390-
numBytesPerChannel);
1386+
outputChannelData, avFrame->extended_data[channel], numBytesPerChannel);
13911387
}
13921388
frameOutput.data = outputData;
13931389
}

0 commit comments

Comments
 (0)