Bug 1876438 - Add SVC parameter support to libaomenc.c via av_opt_set r=media-playback-reviewers,padenot

This patch updates libaomenc.c to accept parameters for SVC (Scalable
Video Coding) settings via the FFmpeg API `av_opt_set`. The SVC
configuration is applied based on the provided parameters. As libaom's
SVC functionality only operates with constant bitrate encoding [1],
these parameters will only take effect when the bitrate is set to
constant.

To facilitate future updates of libaomenc.c in FFmpeg, the modifications
are packed into a patch, allowing for easy application on top of the
FFmpeg sources when needed

[1] https://aomedia.googlesource.com/aom/+/a7ef80c44bfb34b08254194b1ab72d4e93ff4b07/av1/encoder/svc_layercontext.h#115

Depends on D211952

Differential Revision: https://phabricator.services.mozilla.com/D211951
This commit is contained in:
Chun-Min Chang 2024-06-03 18:45:57 +00:00
parent b8ba8c4790
commit 4440cf6c5d
3 changed files with 280 additions and 2 deletions

View File

@ -125,7 +125,7 @@ with the thread flavor Gecko prefers on Windows (win32, not pthreads).
- Run ffmpeg's `./configure` with the options needed:
> ./configure --disable-all --enable-avcodec --enable-decoder=vp8,vp9,mp3,flac
--enable-parser=vp8,vp9 --disable-static --enable-shared
--enable-parser=vp8,vp9 --disable-static --enable-shared
--disable-autodetect --enable-w32threads --toolchain=msvc
- Copy `config.h` and `config.asm` as `config_win64.h` and `config_win64.asm`.
@ -165,4 +165,4 @@ Finally, apply the patches:
- no-unicode-stdio.patch to avoid passing the infity symbol in unicode to an
stdio.h function, that causes bug 1879740 issue on Windows.
- opusenc-dtx.patch to allow enabling DTX in the opus encoder.
- libaomenc-svc.patch to allow configuring SVC in the libaom encoder.

View File

@ -0,0 +1,202 @@
diff --git a/media/ffvpx/libavcodec/libaomenc.c b/media/ffvpx/libavcodec/libaomenc.c
index d660afab4ecc2..b09a31bb8631f 100644
--- a/media/ffvpx/libavcodec/libaomenc.c
+++ b/media/ffvpx/libavcodec/libaomenc.c
@@ -25,16 +25,17 @@
#include <limits.h>
#define AOM_DISABLE_CTRL_TYPECHECKS 1
#include <aom/aom_encoder.h>
#include <aom/aomcx.h>
#include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
#include "libavutil/base64.h"
#include "libavutil/common.h"
#include "libavutil/cpu.h"
#include "libavutil/imgutils.h"
#include "libavutil/mathematics.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
@@ -130,16 +131,17 @@ typedef struct AOMEncoderContext {
int enable_masked_comp;
int enable_obmc;
int enable_onesided_comp;
int enable_reduced_reference_set;
int enable_smooth_interintra;
int enable_diff_wtd_comp;
int enable_dist_wtd_comp;
int enable_dual_filter;
+ AVDictionary *svc_parameters;
AVDictionary *aom_params;
} AOMContext;
static const char *const ctlidstr[] = {
[AOME_SET_CPUUSED] = "AOME_SET_CPUUSED",
[AOME_SET_CQ_LEVEL] = "AOME_SET_CQ_LEVEL",
[AOME_SET_ENABLEAUTOALTREF] = "AOME_SET_ENABLEAUTOALTREF",
[AOME_SET_ARNR_MAXFRAMES] = "AOME_SET_ARNR_MAXFRAMES",
@@ -210,16 +212,17 @@ static const char *const ctlidstr[] = {
#endif
#ifdef AOM_CTRL_AV1E_GET_SEQ_LEVEL_IDX
[AV1E_GET_SEQ_LEVEL_IDX] = "AV1E_GET_SEQ_LEVEL_IDX",
#endif
#ifdef AOM_CTRL_AV1E_GET_TARGET_SEQ_LEVEL_IDX
[AV1E_GET_TARGET_SEQ_LEVEL_IDX] = "AV1E_GET_TARGET_SEQ_LEVEL_IDX",
#endif
[AV1_GET_NEW_FRAME_IMAGE] = "AV1_GET_NEW_FRAME_IMAGE",
+ [AV1E_SET_SVC_PARAMS] = "AV1E_SET_SVC_PARAMS",
};
static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
{
AOMContext *ctx = avctx->priv_data;
const char *error = aom_codec_error(&ctx->encoder);
const char *detail = aom_codec_error_detail(&ctx->encoder);
@@ -391,16 +394,41 @@ static av_cold int codecctl_imgp(AVCodecContext *avctx,
ctlidstr[id]);
log_encoder_error(avctx, buf);
return AVERROR(EINVAL);
}
return 0;
}
+static av_cold int codecctl_svcp(AVCodecContext *avctx,
+#ifdef UENUM1BYTE
+ aome_enc_control_id id,
+#else
+ enum aome_enc_control_id id,
+#endif
+ aom_svc_params_t *svc_params)
+{
+ AOMContext *ctx = avctx->priv_data;
+ char buf[80];
+ int res;
+
+ snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
+
+ res = aom_codec_control(&ctx->encoder, id, svc_params);
+ if (res != AOM_CODEC_OK) {
+ snprintf(buf, sizeof(buf), "Failed to get %s codec control",
+ ctlidstr[id]);
+ log_encoder_error(avctx, buf);
+ return AVERROR(EINVAL);
+ }
+
+ return 0;
+}
+
static av_cold int aom_free(AVCodecContext *avctx)
{
AOMContext *ctx = avctx->priv_data;
#if defined(AOM_CTRL_AV1E_GET_NUM_OPERATING_POINTS) && \
defined(AOM_CTRL_AV1E_GET_SEQ_LEVEL_IDX) && \
defined(AOM_CTRL_AV1E_GET_TARGET_SEQ_LEVEL_IDX)
if (ctx->encoder.iface && !(avctx->flags & AV_CODEC_FLAG_PASS1)) {
@@ -680,16 +708,28 @@ static int choose_tiling(AVCodecContext *avctx,
enccfg->tile_heights[i] = tile_size +
(i < rounding / 2 ||
i > ctx->tile_rows - 1 - (rounding + 1) / 2);
}
return 0;
}
+static void aom_svc_parse_int_array(int *dest, char *value, int max_entries)
+{
+ int dest_idx = 0;
+ char *saveptr = NULL;
+ char *token = av_strtok(value, ",", &saveptr);
+
+ while (token && dest_idx < max_entries) {
+ dest[dest_idx++] = strtoul(token, NULL, 10);
+ token = av_strtok(NULL, ",", &saveptr);
+ }
+}
+
static av_cold int aom_init(AVCodecContext *avctx,
const struct aom_codec_iface *iface)
{
AOMContext *ctx = avctx->priv_data;
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
struct aom_codec_enc_cfg enccfg = { 0 };
#ifdef AOM_FRAME_IS_INTRAONLY
aom_codec_flags_t flags =
@@ -993,16 +1033,51 @@ static av_cold int aom_init(AVCodecContext *avctx,
if (ctx->row_mt >= 0)
codecctl_int(avctx, AV1E_SET_ROW_MT, ctx->row_mt);
#endif
#ifdef AOM_CTRL_AV1E_SET_ENABLE_INTRABC
if (ctx->enable_intrabc >= 0)
codecctl_int(avctx, AV1E_SET_ENABLE_INTRABC, ctx->enable_intrabc);
#endif
+ if (enccfg.rc_end_usage == AOM_CBR) {
+ aom_svc_params_t svc_params = {};
+ svc_params.framerate_factor[0] = 1;
+ svc_params.number_spatial_layers = 1;
+ svc_params.number_temporal_layers = 1;
+
+ AVDictionaryEntry *en = NULL;
+ while ((en = av_dict_get(ctx->svc_parameters, "", en, AV_DICT_IGNORE_SUFFIX))) {
+ if (!strlen(en->value))
+ return AVERROR(EINVAL);
+
+ if (!strcmp(en->key, "number_spatial_layers"))
+ svc_params.number_spatial_layers = strtoul(en->value, NULL, 10);
+ else if (!strcmp(en->key, "number_temporal_layers"))
+ svc_params.number_temporal_layers = strtoul(en->value, NULL, 10);
+ else if (!strcmp(en->key, "max_quantizers"))
+ aom_svc_parse_int_array(svc_params.max_quantizers, en->value, AOM_MAX_LAYERS);
+ else if (!strcmp(en->key, "min_quantizers"))
+ aom_svc_parse_int_array(svc_params.min_quantizers, en->value, AOM_MAX_LAYERS);
+ else if (!strcmp(en->key, "scaling_factor_num"))
+ aom_svc_parse_int_array(svc_params.scaling_factor_num, en->value, AOM_MAX_SS_LAYERS);
+ else if (!strcmp(en->key, "scaling_factor_den"))
+ aom_svc_parse_int_array(svc_params.scaling_factor_den, en->value, AOM_MAX_SS_LAYERS);
+ else if (!strcmp(en->key, "layer_target_bitrate"))
+ aom_svc_parse_int_array(svc_params.layer_target_bitrate, en->value, AOM_MAX_LAYERS);
+ else if (!strcmp(en->key, "framerate_factor"))
+ aom_svc_parse_int_array(svc_params.framerate_factor, en->value, AOM_MAX_TS_LAYERS);
+ }
+
+ res = codecctl_svcp(avctx, AV1E_SET_SVC_PARAMS, &svc_params);
+ if (res < 0)
+ return res;
+ }
+
+
#if AOM_ENCODER_ABI_VERSION >= 23
{
AVDictionaryEntry *en = NULL;
while ((en = av_dict_get(ctx->aom_params, "", en, AV_DICT_IGNORE_SUFFIX))) {
int ret = aom_codec_set_option(&ctx->encoder, en->key, en->value);
if (ret != AOM_CODEC_OK) {
log_encoder_error(avctx, en->key);
@@ -1525,16 +1600,17 @@ static const AVOption options[] = {
{ "enable-diff-wtd-comp", "Enable difference-weighted compound", OFFSET(enable_diff_wtd_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
{ "enable-dist-wtd-comp", "Enable distance-weighted compound", OFFSET(enable_dist_wtd_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
{ "enable-onesided-comp", "Enable one sided compound", OFFSET(enable_onesided_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
{ "enable-interinter-wedge", "Enable interinter wedge compound", OFFSET(enable_interinter_wedge), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
{ "enable-interintra-wedge", "Enable interintra wedge compound", OFFSET(enable_interintra_wedge), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
{ "enable-masked-comp", "Enable masked compound", OFFSET(enable_masked_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
{ "enable-interintra-comp", "Enable interintra compound", OFFSET(enable_interintra_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
{ "enable-smooth-interintra", "Enable smooth interintra mode", OFFSET(enable_smooth_interintra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
+ { "svc-parameters", "SVC configuration using a :-separated list of key=value parameters (only applied in CBR mode)", OFFSET(svc_parameters), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE},
#if AOM_ENCODER_ABI_VERSION >= 23
{ "aom-params", "Set libaom options using a :-separated list of key=value pairs", OFFSET(aom_params), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
#endif
{ NULL },
};
static const FFCodecDefault defaults[] = {
{ "b", "0" },
--
2.34.1

View File

@ -30,6 +30,7 @@
#include <aom/aomcx.h>
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/base64.h"
#include "libavutil/common.h"
#include "libavutil/cpu.h"
@ -135,6 +136,7 @@ typedef struct AOMEncoderContext {
int enable_diff_wtd_comp;
int enable_dist_wtd_comp;
int enable_dual_filter;
AVDictionary *svc_parameters;
AVDictionary *aom_params;
} AOMContext;
@ -215,6 +217,7 @@ static const char *const ctlidstr[] = {
[AV1E_GET_TARGET_SEQ_LEVEL_IDX] = "AV1E_GET_TARGET_SEQ_LEVEL_IDX",
#endif
[AV1_GET_NEW_FRAME_IMAGE] = "AV1_GET_NEW_FRAME_IMAGE",
[AV1E_SET_SVC_PARAMS] = "AV1E_SET_SVC_PARAMS",
};
static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
@ -396,6 +399,31 @@ static av_cold int codecctl_imgp(AVCodecContext *avctx,
return 0;
}
static av_cold int codecctl_svcp(AVCodecContext *avctx,
#ifdef UENUM1BYTE
aome_enc_control_id id,
#else
enum aome_enc_control_id id,
#endif
aom_svc_params_t *svc_params)
{
AOMContext *ctx = avctx->priv_data;
char buf[80];
int res;
snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
res = aom_codec_control(&ctx->encoder, id, svc_params);
if (res != AOM_CODEC_OK) {
snprintf(buf, sizeof(buf), "Failed to get %s codec control",
ctlidstr[id]);
log_encoder_error(avctx, buf);
return AVERROR(EINVAL);
}
return 0;
}
static av_cold int aom_free(AVCodecContext *avctx)
{
AOMContext *ctx = avctx->priv_data;
@ -685,6 +713,18 @@ static int choose_tiling(AVCodecContext *avctx,
return 0;
}
static void aom_svc_parse_int_array(int *dest, char *value, int max_entries)
{
int dest_idx = 0;
char *saveptr = NULL;
char *token = av_strtok(value, ",", &saveptr);
while (token && dest_idx < max_entries) {
dest[dest_idx++] = strtoul(token, NULL, 10);
token = av_strtok(NULL, ",", &saveptr);
}
}
static av_cold int aom_init(AVCodecContext *avctx,
const struct aom_codec_iface *iface)
{
@ -998,6 +1038,41 @@ static av_cold int aom_init(AVCodecContext *avctx,
codecctl_int(avctx, AV1E_SET_ENABLE_INTRABC, ctx->enable_intrabc);
#endif
if (enccfg.rc_end_usage == AOM_CBR) {
aom_svc_params_t svc_params = {};
svc_params.framerate_factor[0] = 1;
svc_params.number_spatial_layers = 1;
svc_params.number_temporal_layers = 1;
AVDictionaryEntry *en = NULL;
while ((en = av_dict_get(ctx->svc_parameters, "", en, AV_DICT_IGNORE_SUFFIX))) {
if (!strlen(en->value))
return AVERROR(EINVAL);
if (!strcmp(en->key, "number_spatial_layers"))
svc_params.number_spatial_layers = strtoul(en->value, NULL, 10);
else if (!strcmp(en->key, "number_temporal_layers"))
svc_params.number_temporal_layers = strtoul(en->value, NULL, 10);
else if (!strcmp(en->key, "max_quantizers"))
aom_svc_parse_int_array(svc_params.max_quantizers, en->value, AOM_MAX_LAYERS);
else if (!strcmp(en->key, "min_quantizers"))
aom_svc_parse_int_array(svc_params.min_quantizers, en->value, AOM_MAX_LAYERS);
else if (!strcmp(en->key, "scaling_factor_num"))
aom_svc_parse_int_array(svc_params.scaling_factor_num, en->value, AOM_MAX_SS_LAYERS);
else if (!strcmp(en->key, "scaling_factor_den"))
aom_svc_parse_int_array(svc_params.scaling_factor_den, en->value, AOM_MAX_SS_LAYERS);
else if (!strcmp(en->key, "layer_target_bitrate"))
aom_svc_parse_int_array(svc_params.layer_target_bitrate, en->value, AOM_MAX_LAYERS);
else if (!strcmp(en->key, "framerate_factor"))
aom_svc_parse_int_array(svc_params.framerate_factor, en->value, AOM_MAX_TS_LAYERS);
}
res = codecctl_svcp(avctx, AV1E_SET_SVC_PARAMS, &svc_params);
if (res < 0)
return res;
}
#if AOM_ENCODER_ABI_VERSION >= 23
{
AVDictionaryEntry *en = NULL;
@ -1530,6 +1605,7 @@ static const AVOption options[] = {
{ "enable-masked-comp", "Enable masked compound", OFFSET(enable_masked_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
{ "enable-interintra-comp", "Enable interintra compound", OFFSET(enable_interintra_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
{ "enable-smooth-interintra", "Enable smooth interintra mode", OFFSET(enable_smooth_interintra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
{ "svc-parameters", "SVC configuration using a :-separated list of key=value parameters (only applied in CBR mode)", OFFSET(svc_parameters), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE},
#if AOM_ENCODER_ABI_VERSION >= 23
{ "aom-params", "Set libaom options using a :-separated list of key=value pairs", OFFSET(aom_params), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
#endif