diff mbox series

[FFmpeg-devel,v1,10/12] avcodec/libsvtav1: add support for writing out CLL and MDCV

Message ID 20230331162205.428735-11-jeebjp@gmail.com
State New
Headers show
Series encoder AVCodecContext configuration side data | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Jan Ekström March 31, 2023, 4:22 p.m. UTC
These two were added in 28e23d7f348c78d49a726c7469f9d4e38edec341
and 3558c1f2e97455e0b89edef31b9a72ab7fa30550 for version 0.9.0 of
SVT-AV1, which is also our minimum requirement right now.

In other words, no additional version limiting conditions seem
to be required.
---
 libavcodec/libsvtav1.c | 70 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
diff mbox series

Patch

diff --git a/libavcodec/libsvtav1.c b/libavcodec/libsvtav1.c
index 9174e2753c..1bbda0b705 100644
--- a/libavcodec/libsvtav1.c
+++ b/libavcodec/libsvtav1.c
@@ -24,9 +24,11 @@ 
 #include <EbSvtAv1ErrorCodes.h>
 #include <EbSvtAv1Enc.h>
 
+#include "libavutil/bswap.h"
 #include "libavutil/common.h"
 #include "libavutil/frame.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/mastering_display_metadata.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/avassert.h"
@@ -146,6 +148,72 @@  static int alloc_buffer(EbSvtAv1EncConfiguration *config, SvtContext *svt_enc)
 
 }
 
+static void handle_mdcv(struct EbSvtAv1MasteringDisplayInfo *dst,
+                        const AVMasteringDisplayMetadata *mdcv)
+{
+    struct EbSvtAv1ChromaPoints *points[] = {
+        &dst->r,
+        &dst->g,
+        &dst->b,
+    };
+
+    if (!mdcv->has_primaries)
+        goto skip_primaries;
+
+    for (int i = 0; i < 3; i++) {
+        struct EbSvtAv1ChromaPoints *dst = points[i];
+        const  AVRational           *src = mdcv->display_primaries[i];
+
+        dst->x =
+            AV_BSWAP16C(av_rescale_q(1, src[0],
+                                     (AVRational){ 1, (1 << 16) }));
+        dst->y =
+            AV_BSWAP16C(av_rescale_q(1, src[1],
+                                     (AVRational){ 1, (1 << 16) }));
+    }
+
+    dst->white_point.x =
+        AV_BSWAP16C(av_rescale_q(1, mdcv->white_point[0],
+                                 (AVRational){ 1, (1 << 16) }));
+    dst->white_point.y =
+        AV_BSWAP16C(av_rescale_q(1, mdcv->white_point[1],
+                                 (AVRational){ 1, (1 << 16) }));
+
+skip_primaries:
+    if (!mdcv->has_luminance)
+        return;
+
+    dst->max_luma =
+        AV_BSWAP32C(av_rescale_q(1, mdcv->max_luminance,
+                                 (AVRational){ 1, (1 << 8) }));
+    dst->min_luma =
+        AV_BSWAP32C(av_rescale_q(1, mdcv->min_luminance,
+                                 (AVRational){ 1, (1 << 14) }));
+}
+
+static void handle_side_data(AVCodecContext *avctx,
+                             EbSvtAv1EncConfiguration *param)
+{
+    const AVFrameSideDataSet set = avctx->side_data_set;
+    const AVFrameSideData *cll_sd =
+        av_get_side_data_from_set(set, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
+    const AVFrameSideData *mdcv_sd =
+        av_get_side_data_from_set(set, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
+
+    if (cll_sd) {
+        const AVContentLightMetadata *cll =
+            (AVContentLightMetadata *)cll_sd->data;
+
+        param->content_light_level.max_cll  = AV_BSWAP16C(cll->MaxCLL);
+        param->content_light_level.max_fall = AV_BSWAP16C(cll->MaxFALL);
+    }
+
+    if (mdcv_sd) {
+        handle_mdcv(&param->mastering_display,
+                    (AVMasteringDisplayMetadata *)mdcv_sd->data);
+    }
+}
+
 static int config_enc_params(EbSvtAv1EncConfiguration *param,
                              AVCodecContext *avctx)
 {
@@ -256,6 +324,8 @@  static int config_enc_params(EbSvtAv1EncConfiguration *param,
     /* 2 = IDR, closed GOP, 1 = CRA, open GOP */
     param->intra_refresh_type = avctx->flags & AV_CODEC_FLAG_CLOSED_GOP ? 2 : 1;
 
+    handle_side_data(avctx, param);
+
 #if SVT_AV1_CHECK_VERSION(0, 9, 1)
     while ((en = av_dict_get(svt_enc->svtav1_opts, "", en, AV_DICT_IGNORE_SUFFIX))) {
         EbErrorType ret = svt_av1_enc_parse_parameter(param, en->key, en->value);