[FFmpeg-devel] [PATCH v2 11/11] avcodec/libsvtav1: implement dolby vision coding

Niklas Haas ffmpeg at haasn.xyz
Tue Apr 9 15:57:31 EEST 2024


From: Niklas Haas <git at haasn.dev>

---
 libavcodec/libsvtav1.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/libavcodec/libsvtav1.c b/libavcodec/libsvtav1.c
index 105c3369c0f..cd62103dba4 100644
--- a/libavcodec/libsvtav1.c
+++ b/libavcodec/libsvtav1.c
@@ -23,6 +23,7 @@
 #include <stdint.h>
 #include <EbSvtAv1ErrorCodes.h>
 #include <EbSvtAv1Enc.h>
+#include <EbSvtAv1Metadata.h>
 
 #include "libavutil/common.h"
 #include "libavutil/frame.h"
@@ -35,6 +36,7 @@
 #include "libavutil/avassert.h"
 
 #include "codec_internal.h"
+#include "dovi_rpu.h"
 #include "encode.h"
 #include "packet_internal.h"
 #include "avcodec.h"
@@ -62,6 +64,8 @@ typedef struct SvtContext {
 
     EOS_STATUS eos_flag;
 
+    DOVIContext dovi;
+
     // User options.
     AVDictionary *svtav1_opts;
     int enc_mode;
@@ -418,6 +422,7 @@ static int read_in_data(EbSvtAv1EncConfiguration *param, const AVFrame *frame,
     in_data->cr_stride = AV_CEIL_RSHIFT(frame->linesize[2], bytes_shift);
 
     header_ptr->n_filled_len = frame_size;
+    svt_metadata_array_free(&header_ptr->metadata);
 
     return 0;
 }
@@ -451,6 +456,11 @@ static av_cold int eb_enc_init(AVCodecContext *avctx)
         return svt_print_error(avctx, svt_ret, "Error initializing encoder");
     }
 
+    svt_enc->dovi.logctx = avctx;
+    ret = ff_dovi_configure(&svt_enc->dovi, avctx);
+    if (ret < 0)
+        return ret;
+
     if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
         EbBufferHeaderType *headerPtr = NULL;
 
@@ -486,6 +496,7 @@ static int eb_send_frame(AVCodecContext *avctx, const AVFrame *frame)
 {
     SvtContext           *svt_enc = avctx->priv_data;
     EbBufferHeaderType  *headerPtr = svt_enc->in_buf;
+    AVFrameSideData *sd;
     int ret;
 
     if (!frame) {
@@ -524,6 +535,24 @@ static int eb_send_frame(AVCodecContext *avctx, const AVFrame *frame)
     if (avctx->gop_size == 1)
         headerPtr->pic_type = EB_AV1_KEY_PICTURE;
 
+    sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOVI_METADATA);
+    if (svt_enc->dovi.cfg.dv_profile && sd) {
+        const AVDOVIMetadata *metadata = (const AVDOVIMetadata *)sd->data;
+        uint8_t *t35;
+        int size;
+        if ((ret = ff_dovi_rpu_generate(&svt_enc->dovi, metadata, &t35, &size)) < 0)
+            return ret;
+        ret = svt_add_metadata(headerPtr, EB_AV1_METADATA_TYPE_ITUT_T35, t35, size);
+        av_free(t35);
+        if (ret < 0)
+            return AVERROR(ENOMEM);
+    } else if (svt_enc->dovi.cfg.dv_profile) {
+        av_log(avctx, AV_LOG_ERROR, "Dolby Vision enabled, but received frame "
+               "without AV_FRAME_DATA_DOVI_METADATA\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+
     svt_av1_enc_send_picture(svt_enc->svt_handle, headerPtr);
 
     return 0;
@@ -644,11 +673,13 @@ static av_cold int eb_enc_close(AVCodecContext *avctx)
     }
     if (svt_enc->in_buf) {
         av_free(svt_enc->in_buf->p_buffer);
+        svt_metadata_array_free(&svt_enc->in_buf->metadata);
         av_freep(&svt_enc->in_buf);
     }
 
     av_buffer_pool_uninit(&svt_enc->pool);
     av_frame_free(&svt_enc->frame);
+    ff_dovi_ctx_unref(&svt_enc->dovi);
 
     return 0;
 }
@@ -695,6 +726,9 @@ static const AVOption options[] = {
       AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 63, 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 },
 
+    { "dolbyvision", "Enable Dolby Vision RPU coding", OFFSET(dovi.enable), AV_OPT_TYPE_BOOL, {.i64 = FF_DOVI_AUTOMATIC }, -1, 1, VE, .unit = "dovi" },
+    {   "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_DOVI_AUTOMATIC}, .flags = VE, .unit = "dovi" },
+
     {NULL},
 };
 
-- 
2.44.0



More information about the ffmpeg-devel mailing list