FFmpeg: add fixes for the SVT-AV1 encoder, from Brad

This commit is contained in:
sthen 2022-05-26 20:20:35 +00:00
parent 878141c4e9
commit f1a7cbd6af
2 changed files with 97 additions and 89 deletions

View File

@ -2,7 +2,7 @@ COMMENT= audio/video converter and streamer
V= 4.4.2
DISTNAME= ffmpeg-${V}
REVISION= 0
REVISION= 1
EPOCH= 1
CATEGORIES= graphics multimedia
MASTER_SITES= https://ffmpeg.org/releases/

View File

@ -2,8 +2,7 @@
- avcodec/libsvtav1: make coded GOP type configurable
- avcodec/libsvtav1: Fix value range for rc mode
- avcodec/libsvtav1: properly enforce CQP mode when set in wrapper
- avcodec/libsvtav1: add a svtav1-params option to pass a list of key=value
parameters
- avcodec/libsvtav1: add a svtav1-params option to pass a list of key=value parameters
- avcodec/libsvtav1: update some options and defaults
- avcodec/libsvtav1: deprecate some options
- avcodec/libsvtav1: fine tune qp mode settings
@ -11,6 +10,8 @@
- avcodec/libsvtav1: give svtav1-params priority over avctx values
- avcodec/libsvtav1: pass pict_type to library
- avcodec/libsvtav1: add support for setting chroma sample location
- avcodec/libsvtav1: update avctx bit rate according to RC mode
- avcodec/libsvtav1: signal CPB properties through side data
Index: libavcodec/libsvtav1.c
--- libavcodec/libsvtav1.c.orig
@ -39,14 +40,12 @@ Index: libavcodec/libsvtav1.c
} SvtContext;
static const struct {
@@ -151,23 +154,82 @@ static int config_enc_params(EbSvtAv1EncConfiguration
@@ -151,11 +154,126 @@ static int config_enc_params(EbSvtAv1EncConfiguration
{
SvtContext *svt_enc = avctx->priv_data;
const AVPixFmtDescriptor *desc;
+ AVDictionaryEntry *en = NULL;
- param->source_width = avctx->width;
- param->source_height = avctx->height;
+ // Update param from options
+#if FF_API_SVTAV1_OPTS
+ param->hierarchical_levels = svt_enc->hierarchical_level;
@ -54,7 +53,7 @@ Index: libavcodec/libsvtav1.c
+ param->scene_change_detection = svt_enc->scd;
+ param->tile_columns = svt_enc->tile_columns;
+ param->tile_rows = svt_enc->tile_rows;
+
+ if (svt_enc->la_depth >= 0)
+ param->look_ahead_distance = svt_enc->la_depth;
+#endif
@ -84,22 +83,12 @@ Index: libavcodec/libsvtav1.c
+ param->enable_adaptive_quantization = 0;
+ }
+
desc = av_pix_fmt_desc_get(avctx->pix_fmt);
- param->encoder_bit_depth = desc->comp[0].depth;
+ desc = av_pix_fmt_desc_get(avctx->pix_fmt);
+ param->color_primaries = avctx->color_primaries;
+ param->matrix_coefficients = (desc->flags & AV_PIX_FMT_FLAG_RGB) ?
+ AVCOL_SPC_RGB : avctx->colorspace;
+ param->transfer_characteristics = avctx->color_trc;
- if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1)
- param->encoder_color_format = EB_YUV420;
- else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0)
- param->encoder_color_format = EB_YUV422;
- else if (!desc->log2_chroma_w && !desc->log2_chroma_h)
- param->encoder_color_format = EB_YUV444;
- else {
- av_log(avctx, AV_LOG_ERROR , "Unsupported pixel format\n");
- return AVERROR(EINVAL);
+
+ if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED)
+ param->color_range = avctx->color_range == AVCOL_RANGE_JPEG;
+ else
@ -129,44 +118,26 @@ Index: libavcodec/libsvtav1.c
+ name);
+ break;
+ }
}
+ }
+#endif
if (avctx->profile != FF_PROFILE_UNKNOWN)
param->profile = avctx->profile;
@@ -175,25 +237,6 @@ static int config_enc_params(EbSvtAv1EncConfiguration
if (avctx->level != FF_LEVEL_UNKNOWN)
param->level = avctx->level;
- if ((param->encoder_color_format == EB_YUV422 || param->encoder_bit_depth > 10)
- && param->profile != FF_PROFILE_AV1_PROFESSIONAL ) {
- av_log(avctx, AV_LOG_WARNING, "Forcing Professional profile\n");
- param->profile = FF_PROFILE_AV1_PROFESSIONAL;
- } else if (param->encoder_color_format == EB_YUV444 && param->profile != FF_PROFILE_AV1_HIGH) {
- av_log(avctx, AV_LOG_WARNING, "Forcing High profile\n");
- param->profile = FF_PROFILE_AV1_HIGH;
- }
-
- // Update param from options
- param->hierarchical_levels = svt_enc->hierarchical_level;
- param->enc_mode = svt_enc->enc_mode;
- param->tier = svt_enc->tier;
- param->rate_control_mode = svt_enc->rc_mode;
- param->scene_change_detection = svt_enc->scd;
- param->qp = svt_enc->qp;
-
- param->target_bit_rate = avctx->bit_rate;
-
if (avctx->gop_size > 0)
param->intra_period_length = avctx->gop_size - 1;
@@ -205,19 +248,56 @@ static int config_enc_params(EbSvtAv1EncConfiguration
param->frame_rate_denominator = avctx->time_base.num * avctx->ticks_per_frame;
}
- if (param->rate_control_mode) {
- param->max_qp_allowed = avctx->qmax;
- param->min_qp_allowed = avctx->qmin;
+
+ if (avctx->profile != FF_PROFILE_UNKNOWN)
+ param->profile = avctx->profile;
+
+ if (avctx->level != FF_LEVEL_UNKNOWN)
+ param->level = avctx->level;
+
+ if (avctx->gop_size > 0)
+ param->intra_period_length = avctx->gop_size - 1;
+
+ if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
+ param->frame_rate_numerator = avctx->framerate.num;
+ param->frame_rate_denominator = avctx->framerate.den;
+ } else {
+ param->frame_rate_numerator = avctx->time_base.den;
+ param->frame_rate_denominator = avctx->time_base.num * avctx->ticks_per_frame;
+ }
+
+ /* 2 = IDR, closed GOP, 1 = CRA, open GOP */
+ param->intra_refresh_type = avctx->flags & AV_CODEC_FLAG_CLOSED_GOP ? 2 : 1;
+
@ -179,7 +150,7 @@ Index: libavcodec/libsvtav1.c
+ if (avctx->err_recognition & AV_EF_EXPLODE)
+ return AVERROR(EINVAL);
+ }
}
+ }
+#else
+ if ((en = av_dict_get(svt_enc->svtav1_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
+ int level = (avctx->err_recognition & AV_EF_EXPLODE) ? AV_LOG_ERROR : AV_LOG_WARNING;
@ -189,43 +160,80 @@ Index: libavcodec/libsvtav1.c
+ return AVERROR(ENOSYS);
+ }
+#endif
+
param->source_width = avctx->width;
param->source_height = avctx->height;
- desc = av_pix_fmt_desc_get(avctx->pix_fmt);
param->encoder_bit_depth = desc->comp[0].depth;
if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1)
@@ -169,12 +287,6 @@ static int config_enc_params(EbSvtAv1EncConfiguration
return AVERROR(EINVAL);
}
- if (avctx->profile != FF_PROFILE_UNKNOWN)
- param->profile = avctx->profile;
-
- if (avctx->level != FF_LEVEL_UNKNOWN)
- param->level = avctx->level;
-
if ((param->encoder_color_format == EB_YUV422 || param->encoder_bit_depth > 10)
&& param->profile != FF_PROFILE_AV1_PROFESSIONAL ) {
av_log(avctx, AV_LOG_WARNING, "Forcing Professional profile\n");
@@ -184,40 +296,21 @@ static int config_enc_params(EbSvtAv1EncConfiguration
param->profile = FF_PROFILE_AV1_HIGH;
}
- // Update param from options
- param->hierarchical_levels = svt_enc->hierarchical_level;
- param->enc_mode = svt_enc->enc_mode;
- param->tier = svt_enc->tier;
- param->rate_control_mode = svt_enc->rc_mode;
- param->scene_change_detection = svt_enc->scd;
- param->qp = svt_enc->qp;
+ avctx->bit_rate = param->rate_control_mode > 0 ?
+ param->target_bit_rate : 0;
+ avctx->rc_max_rate = param->max_bit_rate;
+ avctx->rc_buffer_size = param->vbv_bufsize;
- param->target_bit_rate = avctx->bit_rate;
+ if (avctx->bit_rate || avctx->rc_max_rate || avctx->rc_buffer_size) {
+ AVCPBProperties *cpb_props = ff_add_cpb_side_data(avctx);
+ if (!cpb_props)
+ return AVERROR(ENOMEM);
- if (avctx->gop_size > 0)
- param->intra_period_length = avctx->gop_size - 1;
-
- if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
- param->frame_rate_numerator = avctx->framerate.num;
- param->frame_rate_denominator = avctx->framerate.den;
- } else {
- param->frame_rate_numerator = avctx->time_base.den;
- param->frame_rate_denominator = avctx->time_base.num * avctx->ticks_per_frame;
+ cpb_props->buffer_size = avctx->rc_buffer_size;
+ cpb_props->max_bitrate = avctx->rc_max_rate;
+ cpb_props->avg_bitrate = avctx->bit_rate;
}
- if (param->rate_control_mode) {
- param->max_qp_allowed = avctx->qmax;
- param->min_qp_allowed = avctx->qmin;
- }
-
- param->intra_refresh_type = 2; /* Real keyframes only */
+ param->source_width = avctx->width;
+ param->source_height = avctx->height;
-
- if (svt_enc->la_depth >= 0)
- param->look_ahead_distance = svt_enc->la_depth;
+ param->encoder_bit_depth = desc->comp[0].depth;
-
- param->tile_columns = svt_enc->tile_columns;
- param->tile_rows = svt_enc->tile_rows;
+ if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1)
+ param->encoder_color_format = EB_YUV420;
+ else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0)
+ param->encoder_color_format = EB_YUV422;
+ else if (!desc->log2_chroma_w && !desc->log2_chroma_h)
+ param->encoder_color_format = EB_YUV444;
+ else {
+ av_log(avctx, AV_LOG_ERROR , "Unsupported pixel format\n");
+ return AVERROR(EINVAL);
+ }
+ if ((param->encoder_color_format == EB_YUV422 || param->encoder_bit_depth > 10)
+ && param->profile != FF_PROFILE_AV1_PROFESSIONAL ) {
+ av_log(avctx, AV_LOG_WARNING, "Forcing Professional profile\n");
+ param->profile = FF_PROFILE_AV1_PROFESSIONAL;
+ } else if (param->encoder_color_format == EB_YUV444 && param->profile != FF_PROFILE_AV1_HIGH) {
+ av_log(avctx, AV_LOG_WARNING, "Forcing High profile\n");
+ param->profile = FF_PROFILE_AV1_HIGH;
+ }
+
+ avctx->bit_rate = param->target_bit_rate;
+
-
return 0;
}
@@ -350,6 +430,16 @@ static int eb_send_frame(AVCodecContext *avctx, const
@@ -350,6 +443,16 @@ static int eb_send_frame(AVCodecContext *avctx, const
headerPtr->p_app_private = NULL;
headerPtr->pts = frame->pts;
@ -242,7 +250,7 @@ Index: libavcodec/libsvtav1.c
svt_av1_enc_send_picture(svt_enc->svt_handle, headerPtr);
return 0;
@@ -472,21 +562,22 @@ static av_cold int eb_enc_close(AVCodecContext *avctx)
@@ -472,21 +575,22 @@ static av_cold int eb_enc_close(AVCodecContext *avctx)
#define OFFSET(x) offsetof(SvtContext, x)
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = {
@ -274,7 +282,7 @@ Index: libavcodec/libsvtav1.c
FF_AV1_PROFILE_OPTS
@@ -518,21 +609,20 @@ static const AVOption options[] = {
@@ -518,21 +622,20 @@ static const AVOption options[] = {
{ LEVEL("7.3", 73) },
#undef LEVEL
@ -307,7 +315,7 @@ Index: libavcodec/libsvtav1.c
{NULL},
};
@@ -544,9 +634,10 @@ static const AVClass class = {
@@ -544,9 +647,10 @@ static const AVClass class = {
};
static const AVCodecDefault eb_enc_defaults[] = {
@ -320,7 +328,7 @@ Index: libavcodec/libsvtav1.c
{ "qmax", "63" },
{ NULL },
};
@@ -561,12 +652,11 @@ AVCodec ff_libsvtav1_encoder = {
@@ -561,12 +665,11 @@ AVCodec ff_libsvtav1_encoder = {
.receive_packet = eb_receive_packet,
.close = eb_enc_close,
.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,