* [FFmpeg-devel] [PATCH] avcodec: Avoid relocations for color ranges (PR #20828)
@ 2025-11-03 10:17 mkver via ffmpeg-devel
0 siblings, 0 replies; only message in thread
From: mkver via ffmpeg-devel @ 2025-11-03 10:17 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: mkver
PR #20828 opened by mkver
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20828
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20828.patch
Also add a few more tests to the lavc/tests/avcodec.c.
>From c35dff046e1f7dbcb8f356eba7d39fdfdda29e44 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 3 Nov 2025 00:17:18 +0100
Subject: [PATCH 1/8] avcodec/avcodec: Remove always-true branches
avcodec_get_supported_config() always sets a dummy for
the pointer to the number of entries in case it is not set.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libavcodec/avcodec.c | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index db24b4ed52..78453e2467 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -723,14 +723,12 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr
if (codec->type != (allowed_type)) \
return AVERROR(EINVAL); \
*out_configs = (field); \
- if (out_num_configs) { \
for (int i = 0;; i++) { \
if (!(field) || !memcmp(&(field)[i], &end, sizeof(end))) { \
*out_num_configs = i; \
break; \
} \
} \
- } \
return 0; \
} while (0)
@@ -779,14 +777,12 @@ FF_ENABLE_DEPRECATION_WARNINGS
if (codec->type != AVMEDIA_TYPE_VIDEO)
return AVERROR(EINVAL);
*out_configs = color_range_table[ffcodec(codec)->color_ranges];
- if (out_num_configs)
- *out_num_configs = av_popcount(ffcodec(codec)->color_ranges);
+ *out_num_configs = av_popcount(ffcodec(codec)->color_ranges);
return 0;
case AV_CODEC_CONFIG_COLOR_SPACE:
*out_configs = NULL;
- if (out_num_configs)
- *out_num_configs = 0;
+ *out_num_configs = 0;
return 0;
case AV_CODEC_CONFIG_ALPHA_MODE:
--
2.49.1
>From 7bac71e2d3ef3b9645753fd553aea1f5451c0531 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 3 Nov 2025 07:41:40 +0100
Subject: [PATCH 2/8] avcodec/avcodec: Avoid relocations for color ranges
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libavcodec/avcodec.c | 29 +++++++++++++----------------
1 file changed, 13 insertions(+), 16 deletions(-)
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 78453e2467..00f5447b52 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -732,22 +732,15 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr
return 0; \
} while (0)
-static const enum AVColorRange color_range_jpeg[] = {
- AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED
+static const enum AVColorRange color_range_tab[] = {
+ AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED,
+ AVCOL_RANGE_MPEG, AVCOL_RANGE_UNSPECIFIED,
};
-static const enum AVColorRange color_range_mpeg[] = {
- AVCOL_RANGE_MPEG, AVCOL_RANGE_UNSPECIFIED
-};
-
-static const enum AVColorRange color_range_all[] = {
- AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED
-};
-
-static const enum AVColorRange *color_range_table[] = {
- [AVCOL_RANGE_MPEG] = color_range_mpeg,
- [AVCOL_RANGE_JPEG] = color_range_jpeg,
- [AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG] = color_range_all,
+static const uint8_t color_range_offsets[] = {
+ [AVCOL_RANGE_MPEG] = 3,
+ [AVCOL_RANGE_JPEG] = 1,
+ [AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG] = 0,
};
int ff_default_get_supported_config(const AVCodecContext *avctx,
@@ -776,8 +769,12 @@ FF_ENABLE_DEPRECATION_WARNINGS
case AV_CODEC_CONFIG_COLOR_RANGE:
if (codec->type != AVMEDIA_TYPE_VIDEO)
return AVERROR(EINVAL);
- *out_configs = color_range_table[ffcodec(codec)->color_ranges];
- *out_num_configs = av_popcount(ffcodec(codec)->color_ranges);
+ unsigned color_ranges = codec2->color_ranges;
+ if (color_ranges)
+ *out_configs = color_range_tab + color_range_offsets[color_ranges];
+ else
+ *out_configs = NULL;
+ *out_num_configs = av_popcount(color_ranges);
return 0;
case AV_CODEC_CONFIG_COLOR_SPACE:
--
2.49.1
>From 05352edf16132235428d32c32dcbff8a2b4d1b53 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 3 Nov 2025 08:22:59 +0100
Subject: [PATCH 3/8] avcodec/codec_internal: Use bitfield for alpha_modes
Right now, FFCodec contains a dedicated pointer for
alpha_mode; this is wasteful, as there is only a very limited
range of options for this value, namely four.
So store it as a two-bit bitfield like color_ranges.
This reduces sizeof(FFCodec) by 16 here (the placement
of alpha_mode entailed unnecessary padding) and
saves 11328B of .data.rel.ro here (on a standard build with
no external libraries).
(If it were not for effective-type violations, one could
share the code and the table with AV_CODEC_CONFIG_COLOR_RANGE.)
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libavcodec/avcodec.c | 29 +++++++++++++++++++++++------
libavcodec/codec_internal.h | 13 +++++++------
libavcodec/exrenc.c | 4 +---
libavcodec/libjxlenc.c | 8 ++------
libavcodec/pngenc.c | 8 ++------
5 files changed, 35 insertions(+), 27 deletions(-)
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 00f5447b52..9df6f7efd4 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -23,6 +23,8 @@
* AVCodecContext functions for libavcodec
*/
+#include <assert.h>
+
#include "config.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
@@ -30,7 +32,6 @@
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/emms.h"
-#include "libavutil/fifo.h"
#include "libavutil/imgutils.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
@@ -41,12 +42,10 @@
#include "codec_desc.h"
#include "codec_internal.h"
#include "decode.h"
-#include "encode.h"
#include "frame_thread_encoder.h"
#include "hwconfig.h"
#include "internal.h"
#include "libavutil/refstruct.h"
-#include "thread.h"
/**
* Maximum size in bytes of extradata.
@@ -737,7 +736,17 @@ static const enum AVColorRange color_range_tab[] = {
AVCOL_RANGE_MPEG, AVCOL_RANGE_UNSPECIFIED,
};
-static const uint8_t color_range_offsets[] = {
+static const enum AVAlphaMode alpha_mode_tab[] = {
+ AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_UNSPECIFIED,
+ AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_UNSPECIFIED
+};
+
+static_assert((int)AVCOL_RANGE_MPEG == (int)AVALPHA_MODE_PREMULTIPLIED, "unexpected enum values");
+static_assert((int)AVCOL_RANGE_JPEG == (int)AVALPHA_MODE_STRAIGHT, "unexpected enum values");
+static_assert(AVCOL_RANGE_UNSPECIFIED == 0 && AVALPHA_MODE_UNSPECIFIED == 0, "unexpected enum values");
+static_assert(AVCOL_RANGE_NB == 3 && AVALPHA_MODE_NB == 3, "unexpected enum values");
+
+static const uint8_t offset_tab[] = {
[AVCOL_RANGE_MPEG] = 3,
[AVCOL_RANGE_JPEG] = 1,
[AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG] = 0,
@@ -771,7 +780,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
return AVERROR(EINVAL);
unsigned color_ranges = codec2->color_ranges;
if (color_ranges)
- *out_configs = color_range_tab + color_range_offsets[color_ranges];
+ *out_configs = color_range_tab + offset_tab[color_ranges];
else
*out_configs = NULL;
*out_num_configs = av_popcount(color_ranges);
@@ -783,7 +792,15 @@ FF_ENABLE_DEPRECATION_WARNINGS
return 0;
case AV_CODEC_CONFIG_ALPHA_MODE:
- WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, codec2->alpha_modes, enum AVAlphaMode, AVALPHA_MODE_UNSPECIFIED);
+ if (codec->type != AVMEDIA_TYPE_VIDEO)
+ return AVERROR(EINVAL);
+ unsigned alpha_modes = codec2->alpha_modes;
+ if (alpha_modes)
+ *out_configs = alpha_mode_tab + offset_tab[alpha_modes];
+ else
+ *out_configs = NULL;
+ *out_num_configs = av_popcount(alpha_modes);
+ return 0;
default:
return AVERROR(EINVAL);
diff --git a/libavcodec/codec_internal.h b/libavcodec/codec_internal.h
index 0d13a50fed..eea982e56c 100644
--- a/libavcodec/codec_internal.h
+++ b/libavcodec/codec_internal.h
@@ -133,7 +133,7 @@ typedef struct FFCodec {
/**
* Internal codec capabilities FF_CODEC_CAP_*.
*/
- unsigned caps_internal:26;
+ unsigned caps_internal:24;
/**
* Is this a decoder?
@@ -146,6 +146,12 @@ typedef struct FFCodec {
*/
unsigned color_ranges:2;
+ /**
+ * This field determines the alpha modes supported by an encoder.
+ * Should be set to a bitmask of AVALPHA_MODE_PREMULTIPLIED and AVALPHA_MODE_STRAIGHT.
+ */
+ unsigned alpha_modes:2;
+
/**
* This field determines the type of the codec (decoder/encoder)
* and also the exact callback cb implemented by the codec.
@@ -153,11 +159,6 @@ typedef struct FFCodec {
*/
unsigned cb_type:3;
- /**
- * This field determines the alpha modes supported by an encoder.
- */
- const enum AVAlphaMode *alpha_modes;
-
int priv_data_size;
/**
* @name Frame-level threading support functions
diff --git a/libavcodec/exrenc.c b/libavcodec/exrenc.c
index 54f63d9eb6..68eb683aa5 100644
--- a/libavcodec/exrenc.c
+++ b/libavcodec/exrenc.c
@@ -553,7 +553,5 @@ const FFCodec ff_exr_encoder = {
FF_CODEC_ENCODE_CB(encode_frame),
.close = encode_close,
CODEC_PIXFMTS(AV_PIX_FMT_GRAYF32, AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32),
- .alpha_modes = (const enum AVAlphaMode[]) {
- AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_UNSPECIFIED
- },
+ .alpha_modes = AVALPHA_MODE_PREMULTIPLIED,
};
diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index da538518a5..a2fec89560 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -793,9 +793,7 @@ const FFCodec ff_libjxl_encoder = {
FF_CODEC_CAP_AUTO_THREADS | FF_CODEC_CAP_INIT_CLEANUP |
FF_CODEC_CAP_ICC_PROFILES,
CODEC_PIXFMTS_ARRAY(libjxl_supported_pixfmts),
- .alpha_modes = (const enum AVAlphaMode[]) {
- AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_UNSPECIFIED
- },
+ .alpha_modes = AVALPHA_MODE_STRAIGHT | AVALPHA_MODE_PREMULTIPLIED,
.p.priv_class = &libjxl_encode_class,
.p.wrapper_name = "libjxl",
};
@@ -816,9 +814,7 @@ const FFCodec ff_libjxl_anim_encoder = {
FF_CODEC_CAP_AUTO_THREADS | FF_CODEC_CAP_INIT_CLEANUP |
FF_CODEC_CAP_ICC_PROFILES,
CODEC_PIXFMTS_ARRAY(libjxl_supported_pixfmts),
- .alpha_modes = (const enum AVAlphaMode[]) {
- AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_UNSPECIFIED
- },
+ .alpha_modes = AVALPHA_MODE_STRAIGHT | AVALPHA_MODE_PREMULTIPLIED,
.p.priv_class = &libjxl_encode_class,
.p.wrapper_name = "libjxl",
};
diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index 96894bf7fd..e627bf83fc 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -1299,9 +1299,7 @@ const FFCodec ff_png_encoder = {
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE,
AV_PIX_FMT_MONOBLACK),
- .alpha_modes = (const enum AVAlphaMode[]) {
- AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_UNSPECIFIED
- },
+ .alpha_modes = AVALPHA_MODE_STRAIGHT,
.p.priv_class = &pngenc_class,
.caps_internal = FF_CODEC_CAP_ICC_PROFILES,
};
@@ -1322,9 +1320,7 @@ const FFCodec ff_apng_encoder = {
AV_PIX_FMT_PAL8,
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A,
AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE),
- .alpha_modes = (const enum AVAlphaMode[]) {
- AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_UNSPECIFIED
- },
+ .alpha_modes = AVALPHA_MODE_STRAIGHT,
.p.priv_class = &pngenc_class,
.caps_internal = FF_CODEC_CAP_ICC_PROFILES,
};
--
2.49.1
>From 8ba58660234fc10ba4b89db3b3e63db7ffc64451 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 3 Nov 2025 08:51:09 +0100
Subject: [PATCH 4/8] avcodec/tests/avcodec: Test color_ranges and alpha_modes
Test that they are only set by video codecs.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libavcodec/tests/avcodec.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libavcodec/tests/avcodec.c b/libavcodec/tests/avcodec.c
index dde8226384..d1a28c1935 100644
--- a/libavcodec/tests/avcodec.c
+++ b/libavcodec/tests/avcodec.c
@@ -89,7 +89,8 @@ FF_ENABLE_DEPRECATION_WARNINGS
}
if (codec->type != AVMEDIA_TYPE_VIDEO) {
FF_DISABLE_DEPRECATION_WARNINGS
- if (codec->pix_fmts || codec->supported_framerates)
+ if (codec->pix_fmts || codec->supported_framerates ||
+ codec2->color_ranges || codec2->alpha_modes)
ERR("Non-video codec %s has video-only fields set\n");
FF_ENABLE_DEPRECATION_WARNINGS
if (codec2->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)
--
2.49.1
>From 2429f0d29b114323713336b52aea142084f91c79 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 3 Nov 2025 10:47:06 +0100
Subject: [PATCH 5/8] avcodec/amfdec: Don't set AVCodec.pix_fmts
It is not supposed to be set by decoders (where format negotiation
happens via the get_format callback).
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libavcodec/amfdec.c | 9 ---------
1 file changed, 9 deletions(-)
diff --git a/libavcodec/amfdec.c b/libavcodec/amfdec.c
index 7b038c6181..1a2eb9392c 100644
--- a/libavcodec/amfdec.c
+++ b/libavcodec/amfdec.c
@@ -44,14 +44,6 @@
//will be in public headers soon
#define AMF_VIDEO_DECODER_OUTPUT_FORMAT L"OutputDecodeFormat"
-const enum AVPixelFormat amf_dec_pix_fmts[] = {
- AV_PIX_FMT_NV12,
- AV_PIX_FMT_P010,
- AV_PIX_FMT_P012,
- AV_PIX_FMT_AMF_SURFACE,
- AV_PIX_FMT_NONE
-};
-
static const AVCodecHWConfigInternal *const amf_hw_configs[] = {
&(const AVCodecHWConfigInternal) {
.public = {
@@ -716,7 +708,6 @@ const FFCodec ff_##x##_amf_decoder = { \
.bsfs = bsf_name, \
.p.capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \
.p.priv_class = &amf_decode_class, \
- CODEC_PIXFMTS_ARRAY(amf_dec_pix_fmts), \
.hw_configs = amf_hw_configs, \
.p.wrapper_name = "amf", \
.caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \
--
2.49.1
>From 86ad5e2b4ce440eaf73db4287ce21b7bafafc3c4 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 3 Nov 2025 09:10:22 +0100
Subject: [PATCH 6/8] avcodec/tests/avcodec: Test that decoders don't set
encoder-only fields
For decoders, the pixel format is negotiated via the get_format callback
(if there is a choice at all), so decoders should not set pix_fmts.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libavcodec/tests/avcodec.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/libavcodec/tests/avcodec.c b/libavcodec/tests/avcodec.c
index d1a28c1935..b45bd9d08c 100644
--- a/libavcodec/tests/avcodec.c
+++ b/libavcodec/tests/avcodec.c
@@ -185,6 +185,10 @@ FF_ENABLE_DEPRECATION_WARNINGS
codec2->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS)
ERR("Decoder %s is marked as setting pkt_dts when it doesn't have"
"any effect\n");
+FF_DISABLE_DEPRECATION_WARNINGS
+ if (codec->type == AVMEDIA_TYPE_VIDEO && (codec->pix_fmts || codec->supported_framerates))
+ ERR("Decoder %s sets pix_fmts or supported_framerates.\n");
+FF_ENABLE_DEPRECATION_WARNINGS
}
if (priv_data_size_wrong(codec2))
ERR_EXT("Private context of codec %s is impossibly-sized (size %d).",
--
2.49.1
>From 691ab84d0f9b0995b91d9be8bdd93888a746c788 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 3 Nov 2025 10:32:20 +0100
Subject: [PATCH 7/8] avcodec/tests/avcodec: Check codec {pix,sample}_fmt etc.
arrays
E.g. check that the ordinary entries are valid (e.g. no negative sample
rates, must have pix fmt descriptor) and also that the sentinels
are valid where they contain redundant information.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libavcodec/tests/avcodec.c | 40 ++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/libavcodec/tests/avcodec.c b/libavcodec/tests/avcodec.c
index b45bd9d08c..893175c285 100644
--- a/libavcodec/tests/avcodec.c
+++ b/libavcodec/tests/avcodec.c
@@ -17,6 +17,7 @@
*/
#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
#include "libavcodec/codec.h"
#include "libavcodec/codec_desc.h"
#include "libavcodec/codec_internal.h"
@@ -55,6 +56,30 @@ static int priv_data_size_wrong(const FFCodec *codec)
return 0;
}
+#define ARRAY_CHECK(field, var, type, is_sentinel, check, sentinel_check) \
+do { \
+ const type *ptr = codec->field; \
+ if (!ptr) \
+ break; \
+ type var = *ptr; \
+ if (is_sentinel) { \
+ ERR("Codec %s sets " #field ", but without valid elements.\n"); \
+ break; \
+ } \
+ do { \
+ if (!(check)) { \
+ ERR("Codec's %s " #field " array contains invalid element\n");\
+ break; \
+ } \
+ ++ptr; \
+ var = *ptr; \
+ } while (!(is_sentinel)); \
+ if (!(sentinel_check)) { \
+ ERR("Codec's %s " #field " array has malformed sentinel\n"); \
+ break; \
+ } \
+} while (0)
+
int main(void){
void *iter = NULL;
const AVCodec *codec = NULL;
@@ -86,6 +111,16 @@ FF_ENABLE_DEPRECATION_WARNINGS
AV_CODEC_CAP_CHANNEL_CONF |
AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
ERR("Non-audio codec %s has audio-only capabilities set\n");
+ } else {
+FF_DISABLE_DEPRECATION_WARNINGS
+ ARRAY_CHECK(supported_samplerates, sample_rate, int, sample_rate == 0,
+ sample_rate > 0, 1);
+ ARRAY_CHECK(sample_fmts, sample_fmt, enum AVSampleFormat, sample_fmt == AV_SAMPLE_FMT_NONE,
+ (unsigned)sample_fmt < AV_SAMPLE_FMT_NB, 1);
+ static const AVChannelLayout zero_channel_layout = { 0 };
+ ARRAY_CHECK(ch_layouts, ch_layout, AVChannelLayout, ch_layout.nb_channels == 0,
+ av_channel_layout_check(&ch_layout), !memcmp(ptr, &zero_channel_layout, sizeof(ch_layout)));
+FF_ENABLE_DEPRECATION_WARNINGS
}
if (codec->type != AVMEDIA_TYPE_VIDEO) {
FF_DISABLE_DEPRECATION_WARNINGS
@@ -146,6 +181,11 @@ FF_DISABLE_DEPRECATION_WARNINGS
av_log(NULL, AV_LOG_FATAL, "Encoder %s is missing the sample_fmts field\n", codec->name);
ret = 1;
}
+ } else if (codec->type == AVMEDIA_TYPE_VIDEO) {
+ ARRAY_CHECK(pix_fmts, pix_fmt, enum AVPixelFormat, pix_fmt == AV_PIX_FMT_NONE,
+ av_pix_fmt_desc_get(pix_fmt), 1);
+ ARRAY_CHECK(supported_framerates, framerate, AVRational, framerate.num == 0,
+ framerate.num > 0 && framerate.den > 0, framerate.den == 0);
FF_ENABLE_DEPRECATION_WARNINGS
}
if (codec2->caps_internal & (FF_CODEC_CAP_USES_PROGRESSFRAMES |
--
2.49.1
>From dcf44df87587c59db71f3941deb75cc808b89937 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Mon, 3 Nov 2025 11:02:46 +0100
Subject: [PATCH 8/8] avcodec/avcodec: Simplify sentinel checks
There is no need to check the whole AVChannelLayout; checking
its nb_channels is enough.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
libavcodec/avcodec.c | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 9df6f7efd4..14aca0f164 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -716,18 +716,22 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr
return ff_encode_receive_frame(avctx, frame);
}
-#define WRAP_CONFIG(allowed_type, field, field_type, terminator) \
+#define WRAP_CONFIG(allowed_type, field, var, field_type, sentinel_check) \
do { \
- static const field_type end = terminator; \
if (codec->type != (allowed_type)) \
return AVERROR(EINVAL); \
- *out_configs = (field); \
+ const field_type *ptr = codec->field; \
+ *out_configs = ptr; \
+ if (ptr) { \
for (int i = 0;; i++) { \
- if (!(field) || !memcmp(&(field)[i], &end, sizeof(end))) { \
+ const field_type var = ptr[i]; \
+ if (sentinel_check) { \
*out_num_configs = i; \
break; \
} \
} \
+ } else \
+ *out_num_configs = 0; \
return 0; \
} while (0)
@@ -764,15 +768,15 @@ int ff_default_get_supported_config(const AVCodecContext *avctx,
switch (config) {
FF_DISABLE_DEPRECATION_WARNINGS
case AV_CODEC_CONFIG_PIX_FORMAT:
- WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, codec->pix_fmts, enum AVPixelFormat, AV_PIX_FMT_NONE);
+ WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, pix_fmts, pix_fmt, enum AVPixelFormat, pix_fmt == AV_PIX_FMT_NONE);
case AV_CODEC_CONFIG_FRAME_RATE:
- WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, codec->supported_framerates, AVRational, {0});
+ WRAP_CONFIG(AVMEDIA_TYPE_VIDEO, supported_framerates, framerate, AVRational, framerate.num == 0);
case AV_CODEC_CONFIG_SAMPLE_RATE:
- WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, codec->supported_samplerates, int, 0);
+ WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, supported_samplerates, samplerate, int, samplerate == 0);
case AV_CODEC_CONFIG_SAMPLE_FORMAT:
- WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, codec->sample_fmts, enum AVSampleFormat, AV_SAMPLE_FMT_NONE);
+ WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, sample_fmts, sample_fmt, enum AVSampleFormat, sample_fmt == AV_SAMPLE_FMT_NONE);
case AV_CODEC_CONFIG_CHANNEL_LAYOUT:
- WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, codec->ch_layouts, AVChannelLayout, {0});
+ WRAP_CONFIG(AVMEDIA_TYPE_AUDIO, ch_layouts, ch_layout, AVChannelLayout, ch_layout.nb_channels == 0);
FF_ENABLE_DEPRECATION_WARNINGS
case AV_CODEC_CONFIG_COLOR_RANGE:
--
2.49.1
_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2025-11-03 10:18 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-03 10:17 [FFmpeg-devel] [PATCH] avcodec: Avoid relocations for color ranges (PR #20828) mkver via ffmpeg-devel
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git