git: 04363fa19ecf - main - multimedia/ffmpeg: backport more SVTAV1 fixes

From: Jan Beich <jbeich_at_FreeBSD.org>
Date: Wed, 18 May 2022 15:34:16 UTC
The branch main has been updated by jbeich:

URL: https://cgit.FreeBSD.org/ports/commit/?id=04363fa19ecf6ffc10e9bf58cfeed090bd7abdba

commit 04363fa19ecf6ffc10e9bf58cfeed090bd7abdba
Author:     Jan Beich <jbeich@FreeBSD.org>
AuthorDate: 2022-05-18 15:18:38 +0000
Commit:     Jan Beich <jbeich@FreeBSD.org>
CommitDate: 2022-05-18 15:33:29 +0000

    multimedia/ffmpeg: backport more SVTAV1 fixes
    
    Bump PORTREVISION twice to catch up with 2022Q2 branch.
---
 multimedia/ffmpeg/Makefile           |   2 +-
 multimedia/ffmpeg/files/patch-svtav1 | 222 ++++++++++++++++++++++-------------
 2 files changed, 139 insertions(+), 85 deletions(-)

diff --git a/multimedia/ffmpeg/Makefile b/multimedia/ffmpeg/Makefile
index 513635181915..250249f9ac1e 100644
--- a/multimedia/ffmpeg/Makefile
+++ b/multimedia/ffmpeg/Makefile
@@ -2,7 +2,7 @@
 
 PORTNAME=	ffmpeg
 PORTVERSION=	4.4.2
-PORTREVISION=	2
+PORTREVISION=	4
 PORTEPOCH=	1
 CATEGORIES=	multimedia audio net
 MASTER_SITES=	https://ffmpeg.org/releases/
diff --git a/multimedia/ffmpeg/files/patch-svtav1 b/multimedia/ffmpeg/files/patch-svtav1
index 9ee7e8ff3271..8fc0711e3511 100644
--- a/multimedia/ffmpeg/files/patch-svtav1
+++ b/multimedia/ffmpeg/files/patch-svtav1
@@ -10,6 +10,10 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/50bc87263576
 https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/d794b36a7788
 https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/51c0b9e829be
 https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/e3c4442b249a
+https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/6fd1533057ff
+https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/ded0334d214f
+https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/70887d44ffa3
+https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/fe100bc556d7
 
 --- configure.orig	2021-10-24 20:47:11 UTC
 +++ configure
@@ -103,14 +107,12 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/e3c4442b249a
  } SvtContext;
  
  static const struct {
-@@ -151,49 +154,62 @@ 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;
@@ -118,22 +120,11 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/e3c4442b249a
 +    param->scene_change_detection   = svt_enc->scd;
 +    param->tile_columns             = svt_enc->tile_columns;
 +    param->tile_rows                = svt_enc->tile_rows;
- 
--    desc = av_pix_fmt_desc_get(avctx->pix_fmt);
--    param->encoder_bit_depth = desc->comp[0].depth;
++
 +    if (svt_enc->la_depth >= 0)
 +        param->look_ahead_distance  = svt_enc->la_depth;
 +#endif
- 
--    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 (svt_enc->enc_mode >= 0)
 +        param->enc_mode             = svt_enc->enc_mode;
 +
@@ -146,10 +137,10 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/e3c4442b249a
 +
 +        param->max_qp_allowed       = avctx->qmax;
 +        param->min_qp_allowed       = avctx->qmin;
-     }
++    }
 +    param->max_bit_rate             = avctx->rc_max_rate;
 +    param->vbv_bufsize              = avctx->rc_buffer_size;
- 
++
 +    if (svt_enc->crf > 0) {
 +        param->qp                   = svt_enc->crf;
 +        param->rate_control_mode    = 0;
@@ -170,41 +161,50 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/e3c4442b249a
 +    else
 +        param->color_range = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
 +
-     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");
--        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 +221,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 SVT_AV1_CHECK_VERSION(1, 0, 0)
++    if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
++        const char *name =
++            av_chroma_location_name(avctx->chroma_sample_location);
++
++        switch (avctx->chroma_sample_location) {
++        case AVCHROMA_LOC_LEFT:
++            param->chroma_sample_position = EB_CSP_VERTICAL;
++            break;
++        case AVCHROMA_LOC_TOPLEFT:
++            param->chroma_sample_position = EB_CSP_COLOCATED;
++            break;
++        default:
++            if (!name)
++                break;
++
++            av_log(avctx, AV_LOG_WARNING,
++                   "Specified chroma sample location %s is unsupported "
++                   "on the AV1 bit stream level. Usage of a container that "
++                   "allows passing this information - such as Matroska - "
++                   "is recommended.\n",
++                   name);
++            break;
++        }
++    }
++#endif
++
++    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;
 +
@@ -217,7 +217,7 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/e3c4442b249a
 +            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;
@@ -227,43 +227,97 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/e3c4442b249a
 +            return AVERROR(ENOSYS);
 +    }
 +#endif
++
+     param->source_width     = avctx->width;
+     param->source_height    = avctx->height;
  
--    param->intra_refresh_type       = 2; /* Real keyframes only */
-+    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 */
+-
 -    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);
-+    }
+-
+     return 0;
+ }
  
-+    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;
+@@ -350,6 +443,16 @@ static int eb_send_frame(AVCodecContext *avctx, const 
+     headerPtr->p_app_private = NULL;
+     headerPtr->pts           = frame->pts;
+ 
++    switch (frame->pict_type) {
++    case AV_PICTURE_TYPE_I:
++        headerPtr->pic_type = EB_AV1_KEY_PICTURE;
++        break;
++    default:
++        // Actually means auto, or default.
++        headerPtr->pic_type = EB_AV1_INVALID_PICTURE;
++        break;
 +    }
 +
-+    avctx->bit_rate                 = param->target_bit_rate;
-+
-     return 0;
- }
+     svt_av1_enc_send_picture(svt_enc->svt_handle, headerPtr);
  
-@@ -472,21 +525,22 @@ static const AVOption options[] = {
+     return 0;
+@@ -472,21 +575,22 @@ static const AVOption options[] = {
  #define OFFSET(x) offsetof(SvtContext, x)
  #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  static const AVOption options[] = {
@@ -295,7 +349,7 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/e3c4442b249a
  
      FF_AV1_PROFILE_OPTS
  
-@@ -518,21 +572,20 @@ static const AVOption options[] = {
+@@ -518,21 +622,20 @@ static const AVOption options[] = {
          { LEVEL("7.3", 73) },
  #undef LEVEL
  
@@ -328,7 +382,7 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/e3c4442b249a
      {NULL},
  };
  
-@@ -544,9 +597,10 @@ static const AVCodecDefault eb_enc_defaults[] = {
+@@ -544,9 +647,10 @@ static const AVCodecDefault eb_enc_defaults[] = {
  };
  
  static const AVCodecDefault eb_enc_defaults[] = {
@@ -341,7 +395,7 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/e3c4442b249a
      { "qmax",      "63"    },
      { NULL },
  };
-@@ -561,12 +615,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,