[FFmpeg-devel] [PATCH 06/11] avcodec/libaomenc: add support for encoder reconfiguration
James Almer
jamrial at gmail.com
Tue Feb 18 15:08:08 EET 2025
Signed-off-by: James Almer <jamrial at gmail.com>
---
libavcodec/libaomenc.c | 47 +++++++++++++++++++++++++++++++++++++-----
libavcodec/libx264.c | 27 ++++++++----------------
2 files changed, 51 insertions(+), 23 deletions(-)
diff --git a/libavcodec/libaomenc.c b/libavcodec/libaomenc.c
index 38bdff6a38..d040898b0e 100644
--- a/libavcodec/libaomenc.c
+++ b/libavcodec/libaomenc.c
@@ -1488,10 +1488,41 @@ static av_cold int av1_init(AVCodecContext *avctx)
return aom_init(avctx, aom_codec_av1_cx());
}
+static av_cold int av1_reconf(AVCodecContext *avctx, AVDictionary **dict)
+{
+ AOMContext *ctx = avctx->priv_data;
+ int loglevel;
+ int res;
+
+ res = ff_encode_reconf_parse_dict(avctx, dict);
+ if (res < 0)
+ return res;
+
+ res = aom_config(avctx, ctx->encoder.iface);
+ if (res < 0)
+ return res;
+
+ res = aom_codec_enc_config_set(&ctx->encoder, &ctx->enccfg);
+ loglevel = res != AOM_CODEC_OK ? AV_LOG_WARNING : AV_LOG_DEBUG;
+ av_log(avctx, loglevel, "Reconfigure options:\n");
+ dump_enc_cfg(avctx, &ctx->enccfg, loglevel);
+ if (res != AOM_CODEC_OK) {
+ log_encoder_error(avctx, "Failed to reconfigure encoder");
+ return AVERROR(EINVAL);
+ }
+
+ res = aom_codecctl(avctx);
+ if (res < 0)
+ return res;
+
+ return 0;
+}
+
#define OFFSET(x) offsetof(AOMContext, x)
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+#define VER VE | AV_OPT_FLAG_RUNTIME_PARAM
static const AVOption options[] = {
- { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 8, VE},
+ { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 8, VER},
{ "auto-alt-ref", "Enable use of alternate reference "
"frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
{ "lag-in-frames", "Number of frames to look ahead at for "
@@ -1505,7 +1536,7 @@ static const AVOption options[] = {
{ "cyclic", "Cyclic Refresh Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, .unit = "aq_mode"},
{ "error-resilience", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, .unit = "er"},
{ "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = AOM_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, .unit = "er"},
- { "crf", "Select the quality for constant quality mode", offsetof(AOMContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE },
+ { "crf", "Select the quality for constant quality mode", offsetof(AOMContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VER },
{ "static-thresh", "A change threshold on blocks below which they will be skipped by the encoder", OFFSET(static_thresh), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
{ "drop-threshold", "Frame drop threshold", offsetof(AOMContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE },
{ "denoise-noise-level", "Amount of noise to be removed", OFFSET(denoise_noise_level), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
@@ -1569,9 +1600,13 @@ static const AVOption options[] = {
};
static const FFCodecDefault defaults[] = {
- { "b", "0" },
- { "qmin", "-1" },
- { "qmax", "-1" },
+ { "b", "0", AV_OPT_FLAG_RUNTIME_PARAM },
+ { "bufsize", "0", AV_OPT_FLAG_RUNTIME_PARAM },
+ { "maxrate", "0", AV_OPT_FLAG_RUNTIME_PARAM },
+ { "minrate", "0", AV_OPT_FLAG_RUNTIME_PARAM },
+ { "qmin", "-1", AV_OPT_FLAG_RUNTIME_PARAM },
+ { "qmax", "-1", AV_OPT_FLAG_RUNTIME_PARAM },
+ { "sar", "0", AV_OPT_FLAG_RUNTIME_PARAM },
{ "g", "-1" },
{ "keyint_min", "-1" },
{ NULL },
@@ -1591,6 +1626,7 @@ FFCodec ff_libaom_av1_encoder = {
.p.id = AV_CODEC_ID_AV1,
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
AV_CODEC_CAP_ENCODER_RECON_FRAME |
+ AV_CODEC_CAP_RECONF |
AV_CODEC_CAP_OTHER_THREADS,
.color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG,
.p.profiles = NULL_IF_CONFIG_SMALL(ff_av1_profiles),
@@ -1598,6 +1634,7 @@ FFCodec ff_libaom_av1_encoder = {
.p.wrapper_name = "libaom",
.priv_data_size = sizeof(AOMContext),
.init = av1_init,
+ .reconf = av1_reconf,
FF_CODEC_ENCODE_CB(aom_encode),
.close = aom_free,
.caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index d2adfa7bc5..d9cf7f5428 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -771,22 +771,9 @@ static void X264_flush(AVCodecContext *avctx)
static av_cold int X264_reconf(AVCodecContext *avctx, AVDictionary **dict)
{
- static const AVOption global_opts[] = {
- { "aspect", "sample aspect ratio", offsetof(AVCodecContext, sample_aspect_ratio), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM},
- { "sar", "sample aspect ratio", offsetof(AVCodecContext, sample_aspect_ratio), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, 10, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM},
- { "bufsize", "set ratecontrol buffer size (in bits)", offsetof(AVCodecContext, rc_buffer_size), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM},
- { "b", "set bitrate (in bits/s)", offsetof(AVCodecContext, bit_rate), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM},
- { NULL },
- };
- static const AVOption private_opts[] = {
- { "crf", "Select the quality for constant quality mode", offsetof(X264Context, crf), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
- { "crf_max", "In CRF mode, prevents VBV from lowering quality beyond this point.", offsetof(X264Context, crf_max), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
- { "qp", "Constant quantization parameter rate control method", offsetof(X264Context, cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
- { NULL },
- };
int ret;
- ret = ff_encode_reconf_parse_dict(avctx, global_opts, private_opts, dict);
+ ret = ff_encode_reconf_parse_dict(avctx, dict);
if (ret < 0)
return ret;
@@ -1536,6 +1523,7 @@ static const enum AVPixelFormat pix_fmts_8bit_rgb[] = {
#define OFFSET(x) offsetof(X264Context, x)
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+#define VER VE | AV_OPT_FLAG_RUNTIME_PARAM
static const AVOption options[] = {
{ "preset", "Set the encoding preset (cf. x264 --fullhelp)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},
{ "tune", "Tune the encoding params (cf. x264 --fullhelp)", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
@@ -1546,9 +1534,9 @@ static const AVOption options[] = {
{"wpredp", "Weighted prediction for P-frames", OFFSET(wpredp), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
{"a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VE},
{"x264opts", "x264 options", OFFSET(x264opts), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
- { "crf", "Select the quality for constant quality mode", OFFSET(crf), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE },
- { "crf_max", "In CRF mode, prevents VBV from lowering quality beyond this point.",OFFSET(crf_max), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE },
- { "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },
+ { "crf", "Select the quality for constant quality mode", OFFSET(crf), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VER },
+ { "crf_max", "In CRF mode, prevents VBV from lowering quality beyond this point.",OFFSET(crf_max), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VER },
+ { "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VER },
{ "aq-mode", "AQ method", OFFSET(aq_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, .unit = "aq_mode"},
{ "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX, VE, .unit = "aq_mode" },
{ "variance", "Variance AQ (complexity mask)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX, VE, .unit = "aq_mode" },
@@ -1618,7 +1606,10 @@ static const AVOption options[] = {
};
static const FFCodecDefault x264_defaults[] = {
- { "b", "0" },
+ { "sar", "0", AV_OPT_FLAG_RUNTIME_PARAM },
+ { "b", "0", AV_OPT_FLAG_RUNTIME_PARAM },
+ { "bufsize", "0", AV_OPT_FLAG_RUNTIME_PARAM },
+ { "maxrate", "0", AV_OPT_FLAG_RUNTIME_PARAM },
{ "bf", "-1" },
{ "flags2", "0" },
{ "g", "-1" },
--
2.48.1
More information about the ffmpeg-devel
mailing list