Skip to content

Commit 324b509

Browse files
authored
Fix ASAN error when num_samples is 0 (#600)
1 parent a713765 commit 324b509

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,18 +1431,21 @@ void VideoDecoder::convertAudioAVFrameToFrameOutputOnCPU(
14311431

14321432
auto numSamples = avFrame->nb_samples; // per channel
14331433
auto numChannels = getNumChannels(avFrame);
1434-
torch::Tensor outputData =
1435-
torch::empty({numChannels, numSamples}, torch::kFloat32);
14361434

1437-
uint8_t* outputChannelData = static_cast<uint8_t*>(outputData.data_ptr());
1438-
auto numBytesPerChannel = numSamples * av_get_bytes_per_sample(format);
1439-
for (auto channel = 0; channel < numChannels;
1440-
++channel, outputChannelData += numBytesPerChannel) {
1441-
memcpy(
1442-
outputChannelData, avFrame->extended_data[channel], numBytesPerChannel);
1435+
frameOutput.data = torch::empty({numChannels, numSamples}, torch::kFloat32);
1436+
1437+
if (numSamples > 0) {
1438+
uint8_t* outputChannelData =
1439+
static_cast<uint8_t*>(frameOutput.data.data_ptr());
1440+
auto numBytesPerChannel = numSamples * av_get_bytes_per_sample(format);
1441+
for (auto channel = 0; channel < numChannels;
1442+
++channel, outputChannelData += numBytesPerChannel) {
1443+
memcpy(
1444+
outputChannelData,
1445+
avFrame->extended_data[channel],
1446+
numBytesPerChannel);
1447+
}
14431448
}
1444-
1445-
frameOutput.data = outputData;
14461449
}
14471450

14481451
UniqueAVFrame VideoDecoder::convertAudioAVFrameSampleFormatAndSampleRate(

0 commit comments

Comments
 (0)