Skip to content

Commit 57899ee

Browse files
authored
Do nullptr check on result from av_get_sample_fmt_name() (#596)
1 parent 90e1262 commit 57899ee

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,15 @@ void VideoDecoder::initializeDecoder() {
161161
} else if (avStream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
162162
AVSampleFormat format =
163163
static_cast<AVSampleFormat>(avStream->codecpar->format);
164-
streamMetadata.sampleFormat = av_get_sample_fmt_name(format);
164+
165+
// If the AVSampleFormat is not recognized, we get back nullptr. We have
166+
// to make sure we don't initialize a std::string with nullptr. There's
167+
// nothing to do on the else branch because we're already using an
168+
// optional; it'll just remain empty.
169+
const char* rawSampleFormat = av_get_sample_fmt_name(format);
170+
if (rawSampleFormat != nullptr) {
171+
streamMetadata.sampleFormat = std::string(rawSampleFormat);
172+
}
165173
containerMetadata_.numAudioStreams++;
166174
}
167175

0 commit comments

Comments
 (0)