[FFmpeg-devel] [PATCH] avcodec/dovi_rpu: make ff_dovi_configure_ext() take an AVCodecContext as input argument

James Almer jamrial at gmail.com
Wed Nov 27 15:31:35 EET 2024


There's a single AVCodecParameters user vs two (or strictly speaking, five) for
AVCodecContext, so move the roundtrip logic to the single user instead.

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavcodec/bsf/dovi_rpu.c | 18 ++++++++++-
 libavcodec/dovi_rpu.h     |  4 +--
 libavcodec/dovi_rpuenc.c  | 63 ++++++++++++++++-----------------------
 3 files changed, 44 insertions(+), 41 deletions(-)

diff --git a/libavcodec/bsf/dovi_rpu.c b/libavcodec/bsf/dovi_rpu.c
index ae04d16360..86395da0a6 100644
--- a/libavcodec/bsf/dovi_rpu.c
+++ b/libavcodec/bsf/dovi_rpu.c
@@ -20,6 +20,7 @@
 #include "libavutil/mem.h"
 #include "libavutil/opt.h"
 
+#include "avcodec.h"
 #include "bsf.h"
 #include "bsf_internal.h"
 #include "cbs.h"
@@ -222,10 +223,25 @@ static int dovi_rpu_init(AVBSFContext *bsf)
 
             s->enc.cfg = *cfg;
         } else {
+            AVCodecContext *avctx;
             av_log(bsf, AV_LOG_WARNING, "No Dolby Vision configuration record "
                    "found? Generating one, but results may be invalid.\n");
-            ret = ff_dovi_configure_ext(&s->enc, bsf->par_out, NULL, s->compression,
+            avctx = avcodec_alloc_context3(NULL);
+            if (!avctx)
+                return AVERROR(ENOMEM);
+            ret = avcodec_parameters_to_context(avctx, bsf->par_in);
+            if (ret < 0) {
+                avcodec_free_context(&avctx);
+                return ret;
+            }
+            ret = ff_dovi_configure_ext(&s->enc, avctx, NULL, s->compression,
                                         FF_COMPLIANCE_NORMAL);
+            if (ret < 0) {
+                avcodec_free_context(&avctx);
+                return ret;
+            }
+            ret = avcodec_parameters_from_context(bsf->par_out, avctx);
+            avcodec_free_context(&avctx);
             if (ret < 0)
                 return ret;
             /* Be conservative in accepting all compressed RPUs */
diff --git a/libavcodec/dovi_rpu.h b/libavcodec/dovi_rpu.h
index f3ccc27ae8..e5224446d8 100644
--- a/libavcodec/dovi_rpu.h
+++ b/libavcodec/dovi_rpu.h
@@ -134,7 +134,7 @@ int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame);
 /**
  * Configure the encoder for Dolby Vision encoding. Generates a configuration
  * record in s->cfg, and attaches it to avctx->coded_side_data. Sets the correct
- * profile and compatibility ID based on the tagged AVCodecParameters colorspace
+ * profile and compatibility ID based on the tagged AVCodecContext colorspace
  * metadata, and the correct level based on the resolution and tagged framerate.
  *
  * `metadata` should point to the first frame's RPU, if available. If absent,
@@ -143,7 +143,7 @@ int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame);
  *
  * Returns 0 or a negative error code.
  */
-int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar,
+int ff_dovi_configure_ext(DOVIContext *s, AVCodecContext *avctx,
                           const AVDOVIMetadata *metadata,
                           enum AVDOVICompression compression,
                           int strict_std_compliance);
diff --git a/libavcodec/dovi_rpuenc.c b/libavcodec/dovi_rpuenc.c
index 8113ec44bf..e3a5247035 100644
--- a/libavcodec/dovi_rpuenc.c
+++ b/libavcodec/dovi_rpuenc.c
@@ -52,7 +52,7 @@ static struct {
     [13] = {7680*4320*120u, 7680, 240, 800},
 };
 
-int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar,
+int ff_dovi_configure_ext(DOVIContext *s, AVCodecContext *avctx,
                           const AVDOVIMetadata *metadata,
                           enum AVDOVICompression compression,
                           int strict_std_compliance)
@@ -76,7 +76,7 @@ int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar,
         compression > AV_DOVI_COMPRESSION_EXTENDED)
         return AVERROR(EINVAL);
 
-    switch (codecpar->codec_id) {
+    switch (avctx->codec_id) {
     case AV_CODEC_ID_AV1:  dv_profile = 10; break;
     case AV_CODEC_ID_H264: dv_profile = 9;  break;
     case AV_CODEC_ID_HEVC:
@@ -86,9 +86,9 @@ int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar,
         }
 
         /* This is likely to be proprietary IPTPQc2 */
-        if (codecpar->color_space == AVCOL_SPC_IPT_C2 ||
-            (codecpar->color_space == AVCOL_SPC_UNSPECIFIED &&
-             codecpar->color_trc == AVCOL_TRC_UNSPECIFIED))
+        if (avctx->colorspace == AVCOL_SPC_IPT_C2 ||
+            (avctx->colorspace == AVCOL_SPC_UNSPECIFIED &&
+             avctx->color_trc == AVCOL_TRC_UNSPECIFIED))
             dv_profile = 5;
         else
             dv_profile = 8;
@@ -101,10 +101,10 @@ int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar,
 
     if (strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
         if (dv_profile == 9) {
-            if (codecpar->format != AV_PIX_FMT_YUV420P)
+            if (avctx->pix_fmt != AV_PIX_FMT_YUV420P)
                 dv_profile = 0;
         } else {
-            if (codecpar->format != AV_PIX_FMT_YUV420P10)
+            if (avctx->pix_fmt != AV_PIX_FMT_YUV420P10)
                 dv_profile = 0;
         }
     }
@@ -131,17 +131,17 @@ int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar,
         }
         /* fall through */
     case 8: /* HEVC (or AV1) with BL compatibility */
-        if (codecpar->color_space == AVCOL_SPC_BT2020_NCL &&
-            codecpar->color_primaries == AVCOL_PRI_BT2020 &&
-            codecpar->color_trc == AVCOL_TRC_SMPTE2084) {
+        if (avctx->colorspace == AVCOL_SPC_BT2020_NCL &&
+            avctx->color_primaries == AVCOL_PRI_BT2020 &&
+            avctx->color_trc == AVCOL_TRC_SMPTE2084) {
             bl_compat_id = 1;
-        } else if (codecpar->color_space == AVCOL_SPC_BT2020_NCL &&
-                   codecpar->color_primaries == AVCOL_PRI_BT2020 &&
-                   codecpar->color_trc == AVCOL_TRC_ARIB_STD_B67) {
+        } else if (avctx->colorspace == AVCOL_SPC_BT2020_NCL &&
+                   avctx->color_primaries == AVCOL_PRI_BT2020 &&
+                   avctx->color_trc == AVCOL_TRC_ARIB_STD_B67) {
             bl_compat_id = 4;
-        } else if (codecpar->color_space == AVCOL_SPC_BT709 &&
-                   codecpar->color_primaries == AVCOL_PRI_BT709 &&
-                   codecpar->color_trc == AVCOL_TRC_BT709) {
+        } else if (avctx->colorspace == AVCOL_SPC_BT709 &&
+                   avctx->color_primaries == AVCOL_PRI_BT709 &&
+                   avctx->color_trc == AVCOL_TRC_BT709) {
             bl_compat_id = 2;
         }
     }
@@ -175,9 +175,9 @@ int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar,
         }
     }
 
-    pps = codecpar->width * codecpar->height;
-    if (codecpar->framerate.num) {
-        pps = pps * codecpar->framerate.num / codecpar->framerate.den;
+    pps = avctx->width * avctx->height;
+    if (avctx->framerate.num) {
+        pps = pps * avctx->framerate.num / avctx->framerate.den;
     } else {
         pps *= 25; /* sanity fallback */
     }
@@ -186,7 +186,7 @@ int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar,
     for (int i = 1; i < FF_ARRAY_ELEMS(dv_levels); i++) {
         if (pps > dv_levels[i].pps)
             continue;
-        if (codecpar->width > dv_levels[i].width)
+        if (avctx->width > dv_levels[i].width)
             continue;
         /* In theory, we should also test the bitrate when known, and
          * distinguish between main and high tier. In practice, just ignore
@@ -199,12 +199,12 @@ int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar,
     if (!dv_level) {
         if (strict_std_compliance >= FF_COMPLIANCE_STRICT) {
             av_log(s->logctx, AV_LOG_ERROR, "Coded PPS (%"PRIu64") and width (%d) "
-                   "exceed Dolby Vision limitations\n", pps, codecpar->width);
+                   "exceed Dolby Vision limitations\n", pps, avctx->width);
             return AVERROR(EINVAL);
         } else {
             av_log(s->logctx, AV_LOG_WARNING, "Coded PPS (%"PRIu64") and width (%d) "
                    "exceed Dolby Vision limitations. Ignoring, resulting file "
-                   "may be non-conforming.\n", pps, codecpar->width);
+                   "may be non-conforming.\n", pps, avctx->width);
             dv_level = FF_ARRAY_ELEMS(dv_levels) - 1;
         }
     }
@@ -213,8 +213,8 @@ int ff_dovi_configure_ext(DOVIContext *s, AVCodecParameters *codecpar,
     if (!cfg)
         return AVERROR(ENOMEM);
 
-    if (!av_packet_side_data_add(&codecpar->coded_side_data,
-                                 &codecpar->nb_coded_side_data,
+    if (!av_packet_side_data_add(&avctx->coded_side_data,
+                                 &avctx->nb_coded_side_data,
                                  AV_PKT_DATA_DOVI_CONF, cfg, cfg_size, 0)) {
         av_free(cfg);
         return AVERROR(ENOMEM);
@@ -243,13 +243,6 @@ int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx)
     int ret;
     const AVFrameSideData *sd;
     const AVDOVIMetadata *metadata = NULL;
-    AVCodecParameters *codecpar = avcodec_parameters_alloc();
-    if (!codecpar)
-        return AVERROR(ENOMEM);
-
-    ret = avcodec_parameters_from_context(codecpar, avctx);
-    if (ret < 0)
-        goto fail;
 
     sd = av_frame_side_data_get(avctx->decoded_side_data,
                                 avctx->nb_decoded_side_data,
@@ -258,15 +251,9 @@ int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx)
         metadata = (const AVDOVIMetadata *) sd->data;
 
     /* Current encoders cannot handle metadata compression during encoding */
-    ret = ff_dovi_configure_ext(s, codecpar, metadata, AV_DOVI_COMPRESSION_NONE,
+    ret = ff_dovi_configure_ext(s, avctx, metadata, AV_DOVI_COMPRESSION_NONE,
                                 avctx->strict_std_compliance);
-    if (ret < 0)
-        goto fail;
-
-    ret = avcodec_parameters_to_context(avctx, codecpar);
 
-fail:
-    avcodec_parameters_free(&codecpar);
     return ret;
 }
 
-- 
2.47.0



More information about the ffmpeg-devel mailing list