[FFmpeg-devel] [PATCH 17/18] cbs_h265: Add functions to turn HDR metadata into SEI

Mark Thompson sw at jkqxz.net
Thu Oct 3 02:04:52 EEST 2019


---
With intent to add some sort of support for this in H.265 metadata, though it's not in this set.

Is there any thought on what a human-usable text serialisation of AVMasteringDisplayMetadata should look like?  Ideally it wants to be something which would require no escaping when using it on the command-line.  (This is relevant to video filters as well.)


 libavcodec/Makefile   |  2 +-
 libavcodec/cbs_h265.c | 94 +++++++++++++++++++++++++++++++++++++++++++
 libavcodec/cbs_h265.h | 18 +++++++++
 3 files changed, 113 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/cbs_h265.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 86e512b605..d383d4a539 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -65,7 +65,7 @@ OBJS-$(CONFIG_CABAC)                   += cabac.o
 OBJS-$(CONFIG_CBS)                     += cbs.o
 OBJS-$(CONFIG_CBS_AV1)                 += cbs_av1.o
 OBJS-$(CONFIG_CBS_H264)                += cbs_h2645.o cbs_h264.o h2645_parse.o
-OBJS-$(CONFIG_CBS_H265)                += cbs_h2645.o h2645_parse.o
+OBJS-$(CONFIG_CBS_H265)                += cbs_h2645.o cbs_h265.o h2645_parse.o
 OBJS-$(CONFIG_CBS_JPEG)                += cbs_jpeg.o
 OBJS-$(CONFIG_CBS_MPEG2)               += cbs_mpeg2.o
 OBJS-$(CONFIG_CBS_VP9)                 += cbs_vp9.o
diff --git a/libavcodec/cbs_h265.c b/libavcodec/cbs_h265.c
new file mode 100644
index 0000000000..b4850bd565
--- /dev/null
+++ b/libavcodec/cbs_h265.c
@@ -0,0 +1,94 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/mathematics.h"
+#include "libavutil/mastering_display_metadata.h"
+
+#include "cbs_h265.h"
+
+
+static unsigned int rescale(AVRational value, unsigned int max)
+{
+    int64_t scaled = av_rescale(max, value.num, value.den);
+    return av_clip(scaled, 0, max);
+}
+
+void ff_cbs_h265_fill_sei_mastering_display(H265RawSEIMasteringDisplayColourVolume *mdcv,
+                                            const AVMasteringDisplayMetadata *mdm)
+{
+    memset(mdcv, 0, sizeof(*mdcv));
+
+    if (mdm->has_primaries) {
+        // The values in the metadata are rationals in the range [0, 1],
+        // while the SEI structure contains fixed-point values with an
+        // increment of 0.00002.  So, scale up by 50000 to convert between
+        // them.
+
+        for (int a = 0; a < 3; a++) {
+            // The metadata stores this in RGB order, but the SEI wants it
+            // in GBR order.
+            int b = (a + 1) % 3;
+            mdcv->display_primaries_x[a] =
+                rescale(mdm->display_primaries[b][0], 50000);
+            mdcv->display_primaries_y[a] =
+                rescale(mdm->display_primaries[b][1], 50000);
+        }
+
+        mdcv->white_point_x = rescale(mdm->white_point[0], 50000);
+        mdcv->white_point_y = rescale(mdm->white_point[1], 50000);
+    }
+
+    if (mdm->has_luminance) {
+        // Metadata are rational values in candelas per square metre, SEI
+        // contains fixed point in units of 0.0001 candelas per square
+        // metre.  So, scale up by 10000 to convert between them.
+
+        mdcv->max_display_mastering_luminance =
+            rescale(mdm->max_luminance, 10000);
+        mdcv->min_display_mastering_luminance =
+            rescale(mdm->min_luminance, 10000);
+
+        // The spec has a hard requirement that min is less than the max,
+        // and the SEI-writing code enforces that.
+        if (!(mdcv->min_display_mastering_luminance <
+              mdcv->max_display_mastering_luminance)) {
+            if (mdcv->max_display_mastering_luminance == 10000)
+                mdcv->min_display_mastering_luminance =
+                    mdcv->max_display_mastering_luminance - 1;
+            else
+                mdcv->max_display_mastering_luminance =
+                    mdcv->min_display_mastering_luminance + 1;
+        }
+    } else {
+        mdcv->max_display_mastering_luminance = 1;
+        mdcv->min_display_mastering_luminance = 0;
+    }
+}
+
+void ff_cbs_h265_fill_sei_content_light_level(H265RawSEIContentLightLevelInfo *cll,
+                                              const AVContentLightMetadata *clm)
+{
+    memset(cll, 0, sizeof(*cll));
+
+    // Both the metadata and the SEI are in units of candelas per square
+    // metre, so we only need to clip to ensure that they are in the valid
+    // range.
+
+    cll->max_content_light_level     = av_clip_uintp2(clm->MaxCLL, 16);
+    cll->max_pic_average_light_level = av_clip_uintp2(clm->MaxCLL, 16);
+}
diff --git a/libavcodec/cbs_h265.h b/libavcodec/cbs_h265.h
index f5eb5af5b2..793675ad41 100644
--- a/libavcodec/cbs_h265.h
+++ b/libavcodec/cbs_h265.h
@@ -746,4 +746,22 @@ typedef struct CodedBitstreamH265Context {
 } CodedBitstreamH265Context;
 
 
+struct AVMasteringDisplayMetadata;
+struct AVContentLightMetadata;
+
+/**
+ * Fill an SEI Mastering Display Colour Volume structure with values derived
+ * from the AVMasteringDisplayMetadata side-data structure.
+ */
+void ff_cbs_h265_fill_sei_mastering_display(H265RawSEIMasteringDisplayColourVolume *mdcv,
+                                            const struct AVMasteringDisplayMetadata *mdm);
+
+/**
+ * Fill an SEI Content Light Level Info structure with values derived from
+ * the AVContentLightMetadata side-data structure.
+ */
+void ff_cbs_h265_fill_sei_content_light_level(H265RawSEIContentLightLevelInfo *cll,
+                                              const struct AVContentLightMetadata *clm);
+
+
 #endif /* AVCODEC_CBS_H265_H */
-- 
2.20.1



More information about the ffmpeg-devel mailing list