@@ -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+ reinterpret_cast <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+ reinterpret_cast <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+
5999int getNumChannels (const UniqueAVFrame& avFrame) {
60100#if LIBAVFILTER_VERSION_MAJOR > 8 || \
61101 (LIBAVFILTER_VERSION_MAJOR == 8 && LIBAVFILTER_VERSION_MINOR >= 44 )
@@ -109,7 +149,31 @@ void setDefaultChannelLayout(UniqueAVFrame& avFrame, int numChannels) {
109149}
110150
111151void validateNumChannels (const AVCodec& avCodec, int numChannels) {
112- #if LIBAVFILTER_VERSION_MAJOR > 7 // FFmpeg > 4
152+ #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61, 13, 100) // FFmpeg >= 7
153+ std::stringstream supportedNumChannels;
154+ const AVChannelLayout* supported_layouts = nullptr ;
155+ int num_layouts = 0 ;
156+ int ret = avcodec_get_supported_config (
157+ nullptr ,
158+ &avCodec,
159+ AV_CODEC_CONFIG_CHANNEL_LAYOUT,
160+ 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." );
165+ return ;
166+ }
167+ for (int i = 0 ; supported_layouts[i].nb_channels != 0 ; ++i) {
168+ if (i > 0 ) {
169+ supportedNumChannels << " , " ;
170+ }
171+ supportedNumChannels << supported_layouts[i].nb_channels ;
172+ if (numChannels == supported_layouts[i].nb_channels ) {
173+ return ;
174+ }
175+ }
176+ #elif LIBAVFILTER_VERSION_MAJOR > 7 // FFmpeg > 4
113177 if (avCodec.ch_layouts == nullptr ) {
114178 // If we can't validate, we must assume it'll be fine. If not, FFmpeg will
115179 // eventually raise.
@@ -131,7 +195,7 @@ void validateNumChannels(const AVCodec& avCodec, int numChannels) {
131195 }
132196 supportedNumChannels << avCodec.ch_layouts [i].nb_channels ;
133197 }
134- #else
198+ #else // FFmpeg <= 4
135199 if (avCodec.channel_layouts == nullptr ) {
136200 // can't validate, same as above.
137201 return ;
0 commit comments