[FFmpeg-devel] [PATCH v2 04/17] swscale/utils: read HDR mastering metadata from AVFrame

Niklas Haas ffmpeg at haasn.xyz
Mon Dec 16 13:17:25 EET 2024


From: Niklas Haas <git at haasn.dev>

---
 libswscale/utils.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/libswscale/utils.c b/libswscale/utils.c
index 2d8fc10beb..3d3fcadacc 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -46,6 +46,7 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/libm.h"
+#include "libavutil/mastering_display_metadata.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/mem.h"
 #include "libavutil/opt.h"
@@ -2655,6 +2656,7 @@ SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field)
 {
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
     const AVColorPrimariesDesc *primaries;
+    AVFrameSideData *sd;
 
     SwsFormat fmt = {
         .width  = frame->width,
@@ -2727,6 +2729,26 @@ SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field)
     if (primaries)
         fmt.color.gamut = primaries->prim;
 
+    if ((sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA))) {
+        const AVMasteringDisplayMetadata *mdm = (const AVMasteringDisplayMetadata *) sd->data;
+        if (mdm->has_luminance) {
+            fmt.color.min_luma = mdm->min_luminance;
+            fmt.color.max_luma = mdm->max_luminance;
+        }
+
+        if (mdm->has_primaries) {
+            /* Ignore mastering display white point as it has no bearance on
+             * the underlying content */
+            fmt.color.gamut.r = (AVCIExy) { mdm->display_primaries[0][0], mdm->display_primaries[0][1] };
+            fmt.color.gamut.g = (AVCIExy) { mdm->display_primaries[1][0], mdm->display_primaries[1][1] };
+            fmt.color.gamut.b = (AVCIExy) { mdm->display_primaries[2][0], mdm->display_primaries[2][1] };
+        }
+    }
+
+    /* PQ is always scaled down to absolute zero, so ignore mastering metadata */
+    if (fmt.color.trc == AVCOL_TRC_SMPTE2084)
+        fmt.color.min_luma = av_make_q(0, 1);
+
     return fmt;
 }
 
-- 
2.47.0



More information about the ffmpeg-devel mailing list