git: 1c6198d6c673 - main - multimedia/ffmpeg: backport DAV1D and SVTAV1 fixes

From: Jan Beich <jbeich_at_FreeBSD.org>
Date: Sun, 27 Mar 2022 20:28:14 UTC
The branch main has been updated by jbeich:

URL: https://cgit.FreeBSD.org/ports/commit/?id=1c6198d6c6736b2d25787a8cbd6304b8992f666b

commit 1c6198d6c6736b2d25787a8cbd6304b8992f666b
Author:     Jan Beich <jbeich@FreeBSD.org>
AuthorDate: 2022-03-27 19:43:19 +0000
Commit:     Jan Beich <jbeich@FreeBSD.org>
CommitDate: 2022-03-27 20:27:52 +0000

    multimedia/ffmpeg: backport DAV1D and SVTAV1 fixes
---
 multimedia/ffmpeg/Makefile           |   2 +-
 multimedia/ffmpeg/files/patch-dav1d  |  39 +++++++++
 multimedia/ffmpeg/files/patch-svtav1 | 151 ++++++++++++++++++++++++-----------
 3 files changed, 144 insertions(+), 48 deletions(-)

diff --git a/multimedia/ffmpeg/Makefile b/multimedia/ffmpeg/Makefile
index 7aba1beef13d..4726bd11e64f 100644
--- a/multimedia/ffmpeg/Makefile
+++ b/multimedia/ffmpeg/Makefile
@@ -2,7 +2,7 @@
 
 PORTNAME=	ffmpeg
 PORTVERSION=	4.4.1
-PORTREVISION=	10
+PORTREVISION=	11
 PORTEPOCH=	1
 CATEGORIES=	multimedia audio net
 MASTER_SITES=	https://ffmpeg.org/releases/
diff --git a/multimedia/ffmpeg/files/patch-dav1d b/multimedia/ffmpeg/files/patch-dav1d
new file mode 100644
index 000000000000..4af5b8b518e5
--- /dev/null
+++ b/multimedia/ffmpeg/files/patch-dav1d
@@ -0,0 +1,39 @@
+https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/61ed1182eeb2
+https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/cc33e73618a9
+https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/a4e1dd694014
+
+--- libavcodec/libdav1d.c.orig	2021-10-24 20:47:11 UTC
++++ libavcodec/libdav1d.c
+@@ -127,7 +127,11 @@ static av_cold int libdav1d_init(AVCodecContext *c)
+ {
+     Libdav1dContext *dav1d = c->priv_data;
+     Dav1dSettings s;
++#if FF_DAV1D_VERSION_AT_LEAST(6,0)
++    int threads = c->thread_count;
++#else
+     int threads = (c->thread_count ? c->thread_count : av_cpu_count()) * 3 / 2;
++#endif
+     int res;
+ 
+     av_log(c, AV_LOG_INFO, "libdav1d %s\n", dav1d_version());
+@@ -153,7 +157,7 @@ static av_cold int libdav1d_init(AVCodecContext *c)
+         s.n_threads = FFMAX(dav1d->frame_threads, dav1d->tile_threads);
+     else
+         s.n_threads = FFMIN(threads, DAV1D_MAX_THREADS);
+-    s.max_frame_delay = (c->flags & AV_CODEC_FLAG_LOW_DELAY) ? 1 : s.n_threads;
++    s.max_frame_delay = (c->flags & AV_CODEC_FLAG_LOW_DELAY) ? 1 : 0;
+     av_log(c, AV_LOG_DEBUG, "Using %d threads, %d max_frame_delay\n",
+            s.n_threads, s.max_frame_delay);
+ #else
+@@ -244,8 +248,10 @@ static int libdav1d_receive_frame(AVCodecContext *c, A
+     if (res < 0) {
+         if (res == AVERROR(EINVAL))
+             res = AVERROR_INVALIDDATA;
+-        if (res != AVERROR(EAGAIN))
++        if (res != AVERROR(EAGAIN)) {
++            dav1d_data_unref(data);
+             return res;
++        }
+     }
+ 
+     res = dav1d_get_picture(dav1d->c, p);
diff --git a/multimedia/ffmpeg/files/patch-svtav1 b/multimedia/ffmpeg/files/patch-svtav1
index e960c3d2f816..5cb3b47c42eb 100644
--- a/multimedia/ffmpeg/files/patch-svtav1
+++ b/multimedia/ffmpeg/files/patch-svtav1
@@ -5,10 +5,14 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/c5f314309067
 https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/c33b4048859a
 https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/a2b090da7932
 https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1dddb930aaf0
+https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/4e47ebf38b97
+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
 
 --- configure.orig	2021-10-24 20:47:11 UTC
 +++ configure
-@@ -6430,7 +6430,7 @@ enabled libsoxr           && require libsoxr soxr.h so
+@@ -6430,7 +6430,7 @@ enabled libsrt            && require_pkg_config libsrt
  enabled libssh            && require_pkg_config libssh libssh libssh/sftp.h sftp_init
  enabled libspeex          && require_pkg_config libspeex speex speex/speex.h speex_decoder_init
  enabled libsrt            && require_pkg_config libsrt "srt >= 1.3.0" srt/srt.h srt_socket
@@ -19,7 +23,7 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1dddb930aaf0
  enabled libtheora         && require libtheora theora/theoraenc.h th_info_init -ltheoraenc -ltheoradec -logg
 --- doc/encoders.texi.orig	2021-10-24 20:47:07 UTC
 +++ doc/encoders.texi
-@@ -1754,28 +1754,15 @@ Set the operating point level.
+@@ -1754,28 +1754,15 @@ Set the operating point tier.
  @item tier
  Set the operating point tier.
  
@@ -51,7 +55,7 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1dddb930aaf0
  @item qp
  Set the quantizer used in cqp rate control mode (0-63).
  
-@@ -1786,14 +1773,18 @@ Enable scene change detection.
+@@ -1786,14 +1773,18 @@ Set number of frames to look ahead (0-120).
  Set number of frames to look ahead (0-120).
  
  @item preset
@@ -74,44 +78,51 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1dddb930aaf0
  
 --- libavcodec/libsvtav1.c.orig	2021-10-24 20:47:07 UTC
 +++ libavcodec/libsvtav1.c
-@@ -37,6 +37,10 @@
- #include "avcodec.h"
- #include "profiles.h"
- 
-+#ifndef SVT_AV1_CHECK_VERSION
-+#define SVT_AV1_CHECK_VERSION(major, minor, patch) 0
-+#endif
-+
- typedef enum eos_status {
-     EOS_NOT_REACHED = 0,
-     EOS_SENT,
-@@ -60,10 +64,11 @@ typedef struct SvtContext {
+@@ -60,17 +60,20 @@ typedef struct SvtContext {
      EOS_STATUS eos_flag;
  
      // User options.
 +    AVDictionary *svtav1_opts;
++#if FF_API_SVTAV1_OPTS
      int hierarchical_level;
      int la_depth;
-     int enc_mode;
+-    int enc_mode;
 -    int rc_mode;
-+    int crf;
      int scd;
-     int qp;
+-    int qp;
+ 
+     int tier;
  
-@@ -151,7 +156,63 @@ static int config_enc_params(EbSvtAv1EncConfiguration 
+     int tile_columns;
+     int tile_rows;
++#endif
++    int enc_mode;
++    int crf;
++    int qp;
+ } SvtContext;
+ 
+ static const struct {
+@@ -151,7 +154,62 @@ static int config_enc_params(EbSvtAv1EncConfiguration 
  {
      SvtContext *svt_enc = avctx->priv_data;
      const AVPixFmtDescriptor *desc;
 +    AVDictionaryEntry *en = NULL;
  
 +    // Update param from options
++#if FF_API_SVTAV1_OPTS
 +    param->hierarchical_levels      = svt_enc->hierarchical_level;
++    param->tier                     = svt_enc->tier;
++    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
 +
 +    if (svt_enc->enc_mode >= 0)
 +        param->enc_mode             = svt_enc->enc_mode;
 +
-+    param->tier                     = svt_enc->tier;
-+
 +    if (avctx->bit_rate) {
 +        param->target_bit_rate      = avctx->bit_rate;
 +        if (avctx->rc_max_rate != avctx->bit_rate)
@@ -125,19 +136,11 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1dddb930aaf0
 +    if (svt_enc->crf > 0) {
 +        param->qp                   = svt_enc->crf;
 +        param->rate_control_mode    = 0;
-+        param->enable_tpl_la        = 1;
 +    } else if (svt_enc->qp > 0) {
 +        param->qp                   = svt_enc->qp;
 +        param->rate_control_mode    = 0;
-+        param->enable_tpl_la        = 0;
++        param->enable_adaptive_quantization = 0;
 +    }
-+    param->scene_change_detection   = svt_enc->scd;
-+
-+    if (svt_enc->la_depth >= 0)
-+        param->look_ahead_distance  = svt_enc->la_depth;
-+
-+    param->tile_columns = svt_enc->tile_columns;
-+    param->tile_rows    = svt_enc->tile_rows;
 +
 +#if SVT_AV1_CHECK_VERSION(0, 9, 1)
 +    while ((en = av_dict_get(svt_enc->svtav1_opts, "", en, AV_DICT_IGNORE_SUFFIX))) {
@@ -162,7 +165,24 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1dddb930aaf0
      param->source_width     = avctx->width;
      param->source_height    = avctx->height;
  
-@@ -184,16 +245,6 @@ static int config_enc_params(EbSvtAv1EncConfiguration 
+@@ -169,6 +227,16 @@ static int config_enc_params(EbSvtAv1EncConfiguration 
+         return AVERROR(EINVAL);
+     }
+ 
++    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 (avctx->color_range != AVCOL_RANGE_UNSPECIFIED)
++        param->color_range = avctx->color_range == AVCOL_RANGE_JPEG;
++    else
++        param->color_range = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
++
+     if (avctx->profile != FF_PROFILE_UNKNOWN)
+         param->profile = avctx->profile;
+ 
+@@ -184,16 +252,6 @@ static int config_enc_params(EbSvtAv1EncConfiguration 
          param->profile = FF_PROFILE_AV1_HIGH;
      }
  
@@ -179,7 +199,7 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1dddb930aaf0
      if (avctx->gop_size > 0)
          param->intra_period_length  = avctx->gop_size - 1;
  
-@@ -205,19 +256,15 @@ static int config_enc_params(EbSvtAv1EncConfiguration 
+@@ -205,19 +263,15 @@ static int config_enc_params(EbSvtAv1EncConfiguration 
          param->frame_rate_denominator = avctx->time_base.num * avctx->ticks_per_frame;
      }
  
@@ -203,18 +223,39 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1dddb930aaf0
      return 0;
  }
  
-@@ -480,8 +527,8 @@ static const AVOption options[] = {
-     { "la_depth", "Look ahead distance [0, 120]", OFFSET(la_depth),
-       AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 120, VE },
+@@ -472,21 +526,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[] = {
+-    { "hielevel", "Hierarchical prediction levels setting", OFFSET(hierarchical_level),
+-      AV_OPT_TYPE_INT, { .i64 = 4 }, 3, 4, VE , "hielevel"},
++#if FF_API_SVTAV1_OPTS
++    { "hielevel", "Hierarchical prediction levels setting (Deprecated, use svtav1-params)", OFFSET(hierarchical_level),
++      AV_OPT_TYPE_INT, { .i64 = 4 }, 3, 4, VE | AV_OPT_FLAG_DEPRECATED , "hielevel"},
+         { "3level", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 },  INT_MIN, INT_MAX, VE, "hielevel" },
+         { "4level", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 4 },  INT_MIN, INT_MAX, VE, "hielevel" },
+ 
+-    { "la_depth", "Look ahead distance [0, 120]", OFFSET(la_depth),
+-      AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 120, VE },
++    { "la_depth", "Look ahead distance [0, 120] (Deprecated, use svtav1-params)", OFFSET(la_depth),
++      AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 120, VE | AV_OPT_FLAG_DEPRECATED },
  
 -    { "preset", "Encoding preset [0, 8]",
 -      OFFSET(enc_mode), AV_OPT_TYPE_INT, { .i64 = MAX_ENC_PRESET }, 0, MAX_ENC_PRESET, VE },
+-
+-    { "tier", "Set operating point tier", OFFSET(tier),
+-      AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "tier" },
++    { "tier", "Set operating point tier (Deprecated, use svtav1-params)", OFFSET(tier),
++      AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE | AV_OPT_FLAG_DEPRECATED, "tier" },
+         { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, VE, "tier" },
+         { "high", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, VE, "tier" },
++#endif
 +    { "preset", "Encoding preset",
 +      OFFSET(enc_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_ENC_PRESET, VE },
  
-     { "tier", "Set operating point tier", OFFSET(tier),
-       AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "tier" },
-@@ -518,21 +565,19 @@ static const AVOption options[] = {
+     FF_AV1_PROFILE_OPTS
+ 
+@@ -518,21 +573,20 @@ static const AVOption options[] = {
          { LEVEL("7.3", 73) },
  #undef LEVEL
  
@@ -227,22 +268,27 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1dddb930aaf0
 +      AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 63, VE },
 +    { "qp", "Initial Quantizer level value", OFFSET(qp),
 +      AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 63, VE },
++#if FF_API_SVTAV1_OPTS
++    { "sc_detection", "Scene change detection (Deprecated, use svtav1-params)", OFFSET(scd),
++      AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE | AV_OPT_FLAG_DEPRECATED },
  
 -    { "qp", "Quantizer to use with cqp rate control mode", OFFSET(qp),
 -      AV_OPT_TYPE_INT, { .i64 = 50 }, 0, 63, VE },
--
-     { "sc_detection", "Scene change detection", OFFSET(scd),
-       AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
- 
-     { "tile_columns", "Log2 of number of tile columns to use", OFFSET(tile_columns), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, VE},
-     { "tile_rows", "Log2 of number of tile rows to use", OFFSET(tile_rows), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 6, VE},
++    { "tile_columns", "Log2 of number of tile columns to use (Deprecated, use svtav1-params)", OFFSET(tile_columns), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 4, VE | AV_OPT_FLAG_DEPRECATED },
++    { "tile_rows", "Log2 of number of tile rows to use (Deprecated, use svtav1-params)", OFFSET(tile_rows), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 6, VE | AV_OPT_FLAG_DEPRECATED },
++#endif
  
+-    { "sc_detection", "Scene change detection", OFFSET(scd),
+-      AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
 +    { "svtav1-params", "Set the SVT-AV1 configuration using a :-separated list of key=value parameters", OFFSET(svtav1_opts), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
-+
+ 
+-    { "tile_columns", "Log2 of number of tile columns to use", OFFSET(tile_columns), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, VE},
+-    { "tile_rows", "Log2 of number of tile rows to use", OFFSET(tile_rows), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 6, VE},
+-
      {NULL},
  };
  
-@@ -544,9 +589,10 @@ static const AVClass class = {
+@@ -544,9 +598,10 @@ static const AVCodecDefault eb_enc_defaults[] = {
  };
  
  static const AVCodecDefault eb_enc_defaults[] = {
@@ -255,7 +301,7 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1dddb930aaf0
      { "qmax",      "63"    },
      { NULL },
  };
-@@ -561,12 +607,11 @@ AVCodec ff_libsvtav1_encoder = {
+@@ -561,12 +616,11 @@ AVCodec ff_libsvtav1_encoder = {
      .receive_packet = eb_receive_packet,
      .close          = eb_enc_close,
      .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
@@ -269,3 +315,14 @@ https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1dddb930aaf0
 -    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
      .wrapper_name   = "libsvtav1",
  };
+--- libavcodec/version.h.orig	2021-10-24 20:47:07 UTC
++++ libavcodec/version.h
+@@ -168,5 +168,8 @@
+ #ifndef FF_API_INIT_PACKET
+ #define FF_API_INIT_PACKET         (LIBAVCODEC_VERSION_MAJOR < 60)
+ #endif
++#ifndef FF_API_SVTAV1_OPTS
++#define FF_API_SVTAV1_OPTS         (LIBAVCODEC_VERSION_MAJOR < 60)
++#endif
+ 
+ #endif /* AVCODEC_VERSION_H */