Skip to content

Commit 30704cf

Browse files
Dan-FloresDaniel Flores
andauthored
Remove TORCH_CHECKS on avcodec_get_supported_config output (meta-pytorch#904)
Co-authored-by: Daniel Flores <[email protected]>
1 parent 8c05c6e commit 30704cf

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/torchcodec/_core/FFMPEGCommon.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int64_t getDuration(const UniqueAVFrame& avFrame) {
5858

5959
const int* getSupportedSampleRates(const AVCodec& avCodec) {
6060
const int* supportedSampleRates = nullptr;
61-
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100)
61+
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100) // FFmpeg >= 7.1
6262
int numSampleRates = 0;
6363
int ret = avcodec_get_supported_config(
6464
nullptr,
@@ -68,7 +68,8 @@ const int* getSupportedSampleRates(const AVCodec& avCodec) {
6868
reinterpret_cast<const void**>(&supportedSampleRates),
6969
&numSampleRates);
7070
if (ret < 0 || supportedSampleRates == nullptr) {
71-
TORCH_CHECK(false, "Couldn't get supported sample rates from encoder.");
71+
// Return nullptr to skip validation in validateSampleRate.
72+
return nullptr;
7273
}
7374
#else
7475
supportedSampleRates = avCodec.supported_samplerates;
@@ -78,7 +79,7 @@ const int* getSupportedSampleRates(const AVCodec& avCodec) {
7879

7980
const AVSampleFormat* getSupportedOutputSampleFormats(const AVCodec& avCodec) {
8081
const AVSampleFormat* supportedSampleFormats = nullptr;
81-
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100) // FFmpeg >= 7
82+
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100) // FFmpeg >= 7.1
8283
int numSampleFormats = 0;
8384
int ret = avcodec_get_supported_config(
8485
nullptr,
@@ -88,7 +89,9 @@ const AVSampleFormat* getSupportedOutputSampleFormats(const AVCodec& avCodec) {
8889
reinterpret_cast<const void**>(&supportedSampleFormats),
8990
&numSampleFormats);
9091
if (ret < 0 || supportedSampleFormats == nullptr) {
91-
TORCH_CHECK(false, "Couldn't get supported sample formats from encoder.");
92+
// Return nullptr to use default output format in
93+
// findBestOutputSampleFormat.
94+
return nullptr;
9295
}
9396
#else
9497
supportedSampleFormats = avCodec.sample_fmts;
@@ -149,27 +152,28 @@ void setDefaultChannelLayout(UniqueAVFrame& avFrame, int numChannels) {
149152
}
150153

151154
void validateNumChannels(const AVCodec& avCodec, int numChannels) {
152-
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100) // FFmpeg >= 7
155+
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100) // FFmpeg >= 7.1
153156
std::stringstream supportedNumChannels;
154-
const AVChannelLayout* supported_layouts = nullptr;
155-
int num_layouts = 0;
157+
const AVChannelLayout* supportedLayouts = nullptr;
158+
int numLayouts = 0;
156159
int ret = avcodec_get_supported_config(
157160
nullptr,
158161
&avCodec,
159162
AV_CODEC_CONFIG_CHANNEL_LAYOUT,
160163
0,
161-
reinterpret_cast<const void**>(&supported_layouts),
162-
&num_layouts);
163-
if (ret < 0 || supported_layouts == nullptr) {
164-
TORCH_CHECK(false, "Couldn't get supported channel layouts from encoder.");
164+
reinterpret_cast<const void**>(&supportedLayouts),
165+
&numLayouts);
166+
if (ret < 0 || supportedLayouts == nullptr) {
167+
// If we can't validate, we must assume it'll be fine. If not, FFmpeg will
168+
// eventually raise.
165169
return;
166170
}
167-
for (int i = 0; supported_layouts[i].nb_channels != 0; ++i) {
171+
for (int i = 0; i < numLayouts; ++i) {
168172
if (i > 0) {
169173
supportedNumChannels << ", ";
170174
}
171-
supportedNumChannels << supported_layouts[i].nb_channels;
172-
if (numChannels == supported_layouts[i].nb_channels) {
175+
supportedNumChannels << supportedLayouts[i].nb_channels;
176+
if (numChannels == supportedLayouts[i].nb_channels) {
173177
return;
174178
}
175179
}

0 commit comments

Comments
 (0)