From 4da22ec6fc5e752b3e6871090920f29599fde53f Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Mon, 10 Mar 2025 17:00:36 +0100 Subject: [PATCH 3/3] avcodec/codec_internal: Add inlined version of av_codec_is_(de|en)coder These functions check whether the AVCodec* is NULL, but this has already been checked at a lot of places in our codebase, so that it boils down to checking the is_decoder flag. Signed-off-by: Andreas Rheinhardt --- libavcodec/allcodecs.c | 8 ++++---- libavcodec/avcodec.c | 16 ++++++++-------- libavcodec/codec_internal.h | 30 +++++++++++++++++++++++++----- libavcodec/options.c | 2 +- libavcodec/pthread_slice.c | 2 +- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 3be33f5cc4..34a94ae93d 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -1013,12 +1013,12 @@ static const AVCodec *find_codec(enum AVCodecID id, int (*x)(const AVCodec *)) const AVCodec *avcodec_find_encoder(enum AVCodecID id) { - return find_codec(id, av_codec_is_encoder); + return find_codec(id, ff_codec_is_encoder); } const AVCodec *avcodec_find_decoder(enum AVCodecID id) { - return find_codec(id, av_codec_is_decoder); + return find_codec(id, ff_codec_is_decoder); } static const AVCodec *find_codec_by_name(const char *name, int (*x)(const AVCodec *)) @@ -1041,10 +1041,10 @@ static const AVCodec *find_codec_by_name(const char *name, int (*x)(const AVCode const AVCodec *avcodec_find_encoder_by_name(const char *name) { - return find_codec_by_name(name, av_codec_is_encoder); + return find_codec_by_name(name, ff_codec_is_encoder); } const AVCodec *avcodec_find_decoder_by_name(const char *name) { - return find_codec_by_name(name, av_codec_is_decoder); + return find_codec_by_name(name, ff_codec_is_decoder); } diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c index 64c1788c57..8263f7904e 100644 --- a/libavcodec/avcodec.c +++ b/libavcodec/avcodec.c @@ -190,7 +190,7 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code return AVERROR(EINVAL); } - avci = av_codec_is_decoder(codec) ? + avci = ff_codec_is_decoder(codec) ? ff_decode_internal_alloc() : ff_encode_internal_alloc(); if (!avci) { @@ -270,7 +270,7 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && !avctx->ch_layout.nb_channels && !(codec->capabilities & AV_CODEC_CAP_CHANNEL_CONF)) { av_log(avctx, AV_LOG_ERROR, "%s requires channel layout to be set\n", - av_codec_is_decoder(codec) ? "Decoder" : "Encoder"); + ff_codec_is_decoder(codec) ? "Decoder" : "Encoder"); ret = AVERROR(EINVAL); goto free_and_end; } @@ -290,13 +290,13 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { - const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder"; + const char *codec_string = ff_codec_is_encoder(codec) ? "encoder" : "decoder"; const AVCodec *codec2; av_log(avctx, AV_LOG_ERROR, "The %s '%s' is experimental but experimental codecs are not enabled, " "add '-strict %d' if you want to use it.\n", codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL); - codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id); + codec2 = ff_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id); if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL)) av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n", codec_string, codec2->name); @@ -310,7 +310,7 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code avctx->time_base.den = avctx->sample_rate; } - if (av_codec_is_encoder(avctx->codec)) + if (ff_codec_is_encoder(avctx->codec)) ret = ff_encode_preinit(avctx); else ret = ff_decode_preinit(avctx); @@ -345,7 +345,7 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code ret=0; - if (av_codec_is_decoder(avctx->codec)) { + if (ff_codec_is_decoder(avctx->codec)) { if (!avctx->bit_rate) avctx->bit_rate = get_bit_rate(avctx); @@ -718,10 +718,10 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr { av_frame_unref(frame); - if (!avcodec_is_open(avctx)) + if (!avcodec_is_open(avctx) || !avctx->codec) return AVERROR(EINVAL); - if (av_codec_is_decoder(avctx->codec)) + if (ff_codec_is_decoder(avctx->codec)) return ff_decode_receive_frame(avctx, frame); return ff_encode_receive_frame(avctx, frame); } diff --git a/libavcodec/codec_internal.h b/libavcodec/codec_internal.h index 2b9c7354a0..7fa3097aa9 100644 --- a/libavcodec/codec_internal.h +++ b/libavcodec/codec_internal.h @@ -281,6 +281,31 @@ typedef struct FFCodec { int *out_num_configs); } FFCodec; +static av_always_inline const FFCodec *ffcodec(const AVCodec *codec) +{ + return (const FFCodec*)codec; +} + +/** + * Internal version of av_codec_is_encoder(). Must not be called with + * a NULL AVCodec*. + */ +static inline int ff_codec_is_encoder(const AVCodec *avcodec) +{ + const FFCodec *const codec = ffcodec(avcodec); + return !codec->is_decoder; +} + +/** + * Internal version of av_codec_is_decoder(). Must not be called with + * a NULL AVCodec*. + */ +static inline int ff_codec_is_decoder(const AVCodec *avcodec) +{ + const FFCodec *const codec = ffcodec(avcodec); + return codec->is_decoder; +} + /** * Default implementation for avcodec_get_supported_config(). Will return the * relevant fields from AVCodec if present, or NULL otherwise. @@ -366,9 +391,4 @@ int ff_default_get_supported_config(const struct AVCodecContext *avctx, .p.field = (array) \ ENABLE_DEPRECATION_WARNINGS -static av_always_inline const FFCodec *ffcodec(const AVCodec *codec) -{ - return (const FFCodec*)codec; -} - #endif /* AVCODEC_CODEC_INTERNAL_H */ diff --git a/libavcodec/options.c b/libavcodec/options.c index f60c41bdc3..834beb5757 100644 --- a/libavcodec/options.c +++ b/libavcodec/options.c @@ -69,7 +69,7 @@ static const AVClass *codec_child_class_iterate(void **iter) static AVClassCategory get_category(void *ptr) { AVCodecContext* avctx = ptr; - if (avctx->codec && av_codec_is_decoder(avctx->codec)) + if (avctx->codec && ff_codec_is_decoder(avctx->codec)) return AV_CLASS_CATEGORY_DECODER; else return AV_CLASS_CATEGORY_ENCODER; diff --git a/libavcodec/pthread_slice.c b/libavcodec/pthread_slice.c index ac455e48ed..f7b5679e0b 100644 --- a/libavcodec/pthread_slice.c +++ b/libavcodec/pthread_slice.c @@ -119,7 +119,7 @@ int ff_slice_thread_init(AVCodecContext *avctx) void (*mainfunc)(void *); // We cannot do this in the encoder init as the threads are created before - if (av_codec_is_encoder(avctx->codec) && + if (ff_codec_is_encoder(avctx->codec) && avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && avctx->height > 2800) thread_count = avctx->thread_count = 1; -- 2.45.2