Skip to content

Commit 8b94de5

Browse files
committed
lavc: replace deprecated items
Some AVCodec properties like pix_fmts or sample_fmts are marked as deprecated and should be replaced by avcodec_get_supported_config() in current FFmpeg. Created compat functions to get rid of deprecate warnings running with both old and new FFmpeg.
1 parent 875c585 commit 8b94de5

File tree

4 files changed

+88
-16
lines changed

4 files changed

+88
-16
lines changed

src/audio/codec/libavcodec.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,10 @@ static void *libavcodec_init(audio_codec_t audio_codec, audio_codec_direction_t
299299
}
300300

301301
/* check that a given sample format is supported by the encoder */
302-
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
302+
static int
303+
check_sample_fmt(const AVCodecContext *ctx, enum AVSampleFormat sample_fmt)
303304
{
304-
const enum AVSampleFormat *p = codec->sample_fmts;
305+
const enum AVSampleFormat *p = avc_get_supported_sample_fmts(ctx, NULL);
305306

306307
while (*p != AV_SAMPLE_FMT_NONE) {
307308
if (*p == sample_fmt)
@@ -367,18 +368,20 @@ static bool reinitialize_encoder(struct libavcodec_codec_state *s, struct audio_
367368
s->codec_ctx->sample_fmt = AV_SAMPLE_FMT_NONE;
368369

369370
for (int i = 0; i < count; ++i) {
370-
if (check_sample_fmt(s->codec, sample_fmts[i])) {
371+
if (check_sample_fmt(s->codec_ctx, sample_fmts[i])) {
371372
s->codec_ctx->sample_fmt = sample_fmts[i];
372373
break;
373374
}
374375
}
375376

376377
if (s->codec_ctx->sample_fmt == AV_SAMPLE_FMT_NONE) {
377378
int i = 0;
378-
while (s->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE) {
379-
if (s->codec->sample_fmts[i] != AV_SAMPLE_FMT_DBL &&
380-
s->codec->sample_fmts[i] != AV_SAMPLE_FMT_DBLP) {
381-
s->codec_ctx->sample_fmt = s->codec->sample_fmts[i];
379+
const enum AVSampleFormat *sample_fmts =
380+
avc_get_supported_sample_fmts(s->codec_ctx, NULL);
381+
while (sample_fmts[i] != AV_SAMPLE_FMT_NONE) {
382+
if (sample_fmts[i] != AV_SAMPLE_FMT_DBL &&
383+
sample_fmts[i] != AV_SAMPLE_FMT_DBLP) {
384+
s->codec_ctx->sample_fmt = sample_fmts[i];
382385
break;
383386
}
384387
i++;
@@ -736,7 +739,7 @@ static const int *libavcodec_get_sample_rates(void *state)
736739
{
737740
struct libavcodec_codec_state *s = (struct libavcodec_codec_state *) state;
738741

739-
return s->codec->supported_samplerates;
742+
return avc_get_supported_sample_rates(s->codec_ctx, NULL);
740743
}
741744

742745
static void cleanup_common(struct libavcodec_codec_state *s)

src/libavcodec/lavc_common.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,61 @@ get_avpixfmts_names(const enum AVPixelFormat *pixfmts)
347347
}
348348
return buf;
349349
}
350+
351+
/**
352+
* @param ctx may be nullptr if codec is not
353+
* @param codec may be nullptr if ctx is not
354+
*
355+
* If passed ctx, values such as `strict_std_compliance` may afect the result.
356+
*/
357+
static const void *
358+
avc_get_supported_config(const AVCodecContext *ctx, const AVCodec *codec,
359+
enum AVCodecConfig config)
360+
{
361+
#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(61, 13, 100)
362+
const void *ret = NULL;
363+
int unused_count = 0;
364+
const int rc = avcodec_get_supported_config(
365+
ctx, codec, config, /*flags*/ 0, &ret,
366+
&unused_count);
367+
if (rc != 0) {
368+
MSG(ERROR, "Cannot get list of supported config %d for %s!\n",
369+
(int) config, codec->name);
370+
return NULL;
371+
}
372+
373+
return ret;
374+
#else
375+
abort(); // cannot reach here (shouldn't be called)
376+
#endif
377+
}
378+
///< @copydoc avc_get_supported_config
379+
const enum AVPixelFormat *
380+
avc_get_supported_pix_fmts(const AVCodecContext *ctx, const AVCodec *codec)
381+
{
382+
#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(61, 13, 100)
383+
return avc_get_supported_config(ctx, codec, AV_CODEC_CONFIG_PIX_FORMAT);
384+
#else
385+
return codec->pix_fmts;
386+
#endif
387+
}
388+
///< @copydoc avc_get_supported_config
389+
const enum AVSampleFormat *
390+
avc_get_supported_sample_fmts(const AVCodecContext *ctx, const AVCodec *codec)
391+
{
392+
#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(61, 13, 100)
393+
return avc_get_supported_config(ctx, codec, AV_CODEC_CONFIG_SAMPLE_FORMAT);
394+
#else
395+
return codec->pix_fmts;
396+
#endif
397+
}
398+
///< @copydoc avc_get_supported_config
399+
const int *avc_get_supported_sample_rates(const AVCodecContext *ctx, const AVCodec *codec)
400+
{
401+
#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(61, 13, 100)
402+
return avc_get_supported_config(ctx, codec, AV_CODEC_CONFIG_SAMPLE_RATE);
403+
#else
404+
return codec->supported_samplerates;
405+
#endif
406+
}
350407
/* vi: set expandtab sw=8: */

src/libavcodec/lavc_common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ struct audio_desc audio_desc_from_av_frame(const AVFrame *frm);
122122
enum AVSampleFormat audio_bps_to_av_sample_fmt(int bps, bool planar);
123123
const char *get_avpixfmts_names(const enum AVPixelFormat *pixfmts);
124124

125+
const enum AVPixelFormat *avc_get_supported_pix_fmts(const AVCodecContext *ctx,
126+
const AVCodec *codec);
127+
const enum AVSampleFormat *
128+
avc_get_supported_sample_fmts(const AVCodecContext *ctx, const AVCodec *codec);
129+
const int *avc_get_supported_sample_rates(const AVCodecContext *ctx,
130+
const AVCodec *codec);
131+
125132
#ifdef __cplusplus
126133
}
127134
#endif

src/video_compress/libavcodec.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,8 @@ handle_help(bool full, string const &req_encoder, string const &req_codec)
480480
avcodec_find_encoder_by_name(req_encoder.c_str());
481481
if (codec != nullptr) {
482482
cout << "\n";
483-
print_codec_supp_pix_fmts(codec->pix_fmts);
483+
print_codec_supp_pix_fmts(
484+
avc_get_supported_pix_fmts(nullptr, codec));
484485
} else {
485486
MSG(ERROR, "Cannot open encoder: %s\n",
486487
req_encoder.c_str());
@@ -1123,11 +1124,11 @@ try_open_remaining_pixfmts(state_video_compress_libav *s, video_desc desc,
11231124
return AV_PIX_FMT_NONE;
11241125
#endif
11251126
unsigned usable_fmt_cnt = 0;
1126-
if (codec->pix_fmts == nullptr) {
1127+
if (avc_get_supported_pix_fmts(nullptr, codec) == nullptr) {
11271128
return AV_PIX_FMT_NONE;
11281129
}
1129-
for (const auto *pix = codec->pix_fmts; *pix != AV_PIX_FMT_NONE;
1130-
++pix) {
1130+
for (const auto *pix = avc_get_supported_pix_fmts(nullptr, codec);
1131+
*pix != AV_PIX_FMT_NONE; ++pix) {
11311132
usable_fmt_cnt += 1;
11321133
}
11331134
if (usable_fmt_cnt == fmts_tried.size()) {
@@ -1136,8 +1137,8 @@ try_open_remaining_pixfmts(state_video_compress_libav *s, video_desc desc,
11361137
LOG(LOG_LEVEL_WARNING) << MOD_NAME "No direct decoder format for: "
11371138
<< get_codec_name(desc.color_spec)
11381139
<< ". Trying to convert with swscale instead.\n";
1139-
for (const auto *pix = codec->pix_fmts; *pix != AV_PIX_FMT_NONE;
1140-
++pix) {
1140+
for (const auto *pix = avc_get_supported_pix_fmts(nullptr, codec);
1141+
*pix != AV_PIX_FMT_NONE; ++pix) {
11411142
const AVPixFmtDescriptor *fmt_desc = av_pix_fmt_desc_get(*pix);
11421143
if (fmts_tried.count(*pix) == 1 || fmt_desc == nullptr ||
11431144
(fmt_desc->flags & AV_PIX_FMT_FLAG_HWACCEL) != 0U) {
@@ -1179,15 +1180,19 @@ static bool configure_with(struct state_video_compress_libav *s, struct video_de
11791180
apply_blacklist(requested_pix_fmt, codec->name);
11801181
auto requested_pix_fmt_it = requested_pix_fmt.cbegin();
11811182
set<AVPixelFormat> fmts_tried;
1182-
while ((pix_fmt = get_first_matching_pix_fmt(requested_pix_fmt_it, requested_pix_fmt.cend(), codec->pix_fmts)) != AV_PIX_FMT_NONE) {
1183+
while ((pix_fmt = get_first_matching_pix_fmt(
1184+
requested_pix_fmt_it, requested_pix_fmt.cend(),
1185+
avc_get_supported_pix_fmts(nullptr, codec))) !=
1186+
AV_PIX_FMT_NONE) {
11831187
fmts_tried.insert(pix_fmt);
11841188
if(try_open_codec(s, pix_fmt, desc, ug_codec, codec)){
11851189
break;
11861190
}
11871191
}
11881192

11891193
if (pix_fmt == AV_PIX_FMT_NONE || log_level >= LOG_LEVEL_VERBOSE) {
1190-
print_pix_fmts(requested_pix_fmt, codec->pix_fmts);
1194+
print_pix_fmts(requested_pix_fmt,
1195+
avc_get_supported_pix_fmts(nullptr, codec));
11911196
}
11921197

11931198
if (pix_fmt == AV_PIX_FMT_NONE && get_commandline_param("lavc-use-codec") == NULL) {

0 commit comments

Comments
 (0)