Skip to content

Commit 2c4863c

Browse files
author
Daniel Flores
committed
move ifdef into ffmpegcommon
1 parent 599556d commit 2c4863c

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

src/torchcodec/_core/Encoder.cpp

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,7 @@ torch::Tensor validateSamples(const torch::Tensor& samples) {
3333
}
3434

3535
void validateSampleRate(const AVCodec& avCodec, int sampleRate) {
36-
const int* supportedSampleRates = nullptr;
37-
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100)
38-
int numSampleRates = 0;
39-
int ret = avcodec_get_supported_config(
40-
nullptr,
41-
&avCodec,
42-
AV_CODEC_CONFIG_SAMPLE_RATE,
43-
0,
44-
(const void**)&supportedSampleRates,
45-
&numSampleRates);
46-
if (ret < 0 || supportedSampleRates == nullptr) {
47-
TORCH_CHECK(false, "Couldn't get supported sample rates from encoder.");
48-
}
49-
#else
50-
supportedSampleRates = avCodec.supported_samplerates;
51-
#endif
52-
36+
const int* supportedSampleRates = getSupportedSampleRates(avCodec);
5337
if (supportedSampleRates == nullptr) {
5438
return;
5539
}
@@ -90,22 +74,8 @@ static const std::vector<AVSampleFormat> preferredFormatsOrder = {
9074
AV_SAMPLE_FMT_U8};
9175

9276
AVSampleFormat findBestOutputSampleFormat(const AVCodec& avCodec) {
93-
const AVSampleFormat* supportedSampleFormats = nullptr;
94-
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100) // FFmpeg >= 7
95-
int numSampleFormats = 0;
96-
int ret = avcodec_get_supported_config(
97-
nullptr,
98-
&avCodec,
99-
AV_CODEC_CONFIG_SAMPLE_FORMAT,
100-
0,
101-
(const void**)&supportedSampleFormats,
102-
&numSampleFormats);
103-
if (ret < 0 || supportedSampleFormats == nullptr) {
104-
TORCH_CHECK(false, "Couldn't get supported sample formats from encoder.");
105-
}
106-
#else
107-
supportedSampleFormats = avCodec.sample_fmts;
108-
#endif
77+
const AVSampleFormat* supportedSampleFormats =
78+
getSupportedOutputSampleFormats(avCodec);
10979

11080
// Find a sample format that the encoder supports. We prefer using FLT[P],
11181
// since this is the format of the input samples. If FLTP isn't supported

src/torchcodec/_core/FFMPEGCommon.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,46 @@ int64_t getDuration(const UniqueAVFrame& avFrame) {
5656
#endif
5757
}
5858

59+
const int* getSupportedSampleRates(const AVCodec& avCodec) {
60+
const int* supportedSampleRates = nullptr;
61+
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100)
62+
int numSampleRates = 0;
63+
int ret = avcodec_get_supported_config(
64+
nullptr,
65+
&avCodec,
66+
AV_CODEC_CONFIG_SAMPLE_RATE,
67+
0,
68+
(const void**)&supportedSampleRates,
69+
&numSampleRates);
70+
if (ret < 0 || supportedSampleRates == nullptr) {
71+
TORCH_CHECK(false, "Couldn't get supported sample rates from encoder.");
72+
}
73+
#else
74+
supportedSampleRates = avCodec.supported_samplerates;
75+
#endif
76+
return supportedSampleRates;
77+
}
78+
79+
const AVSampleFormat* getSupportedOutputSampleFormats(const AVCodec& avCodec) {
80+
const AVSampleFormat* supportedSampleFormats = nullptr;
81+
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100) // FFmpeg >= 7
82+
int numSampleFormats = 0;
83+
int ret = avcodec_get_supported_config(
84+
nullptr,
85+
&avCodec,
86+
AV_CODEC_CONFIG_SAMPLE_FORMAT,
87+
0,
88+
(const void**)&supportedSampleFormats,
89+
&numSampleFormats);
90+
if (ret < 0 || supportedSampleFormats == nullptr) {
91+
TORCH_CHECK(false, "Couldn't get supported sample formats from encoder.");
92+
}
93+
#else
94+
supportedSampleFormats = avCodec.sample_fmts;
95+
#endif
96+
return supportedSampleFormats;
97+
}
98+
5999
int getNumChannels(const UniqueAVFrame& avFrame) {
60100
#if LIBAVFILTER_VERSION_MAJOR > 8 || \
61101
(LIBAVFILTER_VERSION_MAJOR == 8 && LIBAVFILTER_VERSION_MINOR >= 44)

src/torchcodec/_core/FFMPEGCommon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ std::string getFFMPEGErrorStringFromErrorCode(int errorCode);
162162
// support.
163163
int64_t getDuration(const UniqueAVFrame& frame);
164164

165+
const int* getSupportedSampleRates(const AVCodec& avCodec);
166+
const AVSampleFormat* getSupportedOutputSampleFormats(const AVCodec& avCodec);
167+
165168
int getNumChannels(const UniqueAVFrame& avFrame);
166169
int getNumChannels(const UniqueAVCodecContext& avCodecContext);
167170

0 commit comments

Comments
 (0)