[FFmpeg-devel] [PATCH] avcodec/h2645_sei: validate Mastering Display Colour Volume SEI values

Kacper Michajłow kasper93 at gmail.com
Sun Feb 25 03:59:46 EET 2024


As we can read in ST 2086:

Values outside the specified ranges of luminance and chromaticity values
are not reserved by SMPTE, and can be used for purposes outside the
scope of this standard.

This is further acknowledged by ITU-T H.264 and ITU-T H.265. Which says
that values out of range are unknown or unspecified or specified by
other means not specified in this Specification.

Signed-off-by: Kacper Michajłow <kasper93 at gmail.com>
---
 libavcodec/h2645_sei.c | 53 +++++++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 16 deletions(-)

diff --git a/libavcodec/h2645_sei.c b/libavcodec/h2645_sei.c
index cb6be0594b..f3ac8004a9 100644
--- a/libavcodec/h2645_sei.c
+++ b/libavcodec/h2645_sei.c
@@ -715,38 +715,59 @@ int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei,
         if (!metadata)
             return AVERROR(ENOMEM);
 
+        metadata->has_luminance = 1;
+        metadata->has_primaries = 1;
+
         for (i = 0; i < 3; i++) {
             const int j = mapping[i];
             metadata->display_primaries[i][0].num = sei->mastering_display.display_primaries[j][0];
             metadata->display_primaries[i][0].den = chroma_den;
+            metadata->has_primaries &= sei->mastering_display.display_primaries[j][0] >= 5 &&
+                                       sei->mastering_display.display_primaries[j][0] <= 37000;
+
             metadata->display_primaries[i][1].num = sei->mastering_display.display_primaries[j][1];
             metadata->display_primaries[i][1].den = chroma_den;
+            metadata->has_primaries &= sei->mastering_display.display_primaries[j][1] >= 5 &&
+                                       sei->mastering_display.display_primaries[j][1] <= 42000;
         }
         metadata->white_point[0].num = sei->mastering_display.white_point[0];
         metadata->white_point[0].den = chroma_den;
+        metadata->has_primaries &= sei->mastering_display.white_point[0] >= 5 &&
+                                   sei->mastering_display.white_point[0] <= 37000;
+
         metadata->white_point[1].num = sei->mastering_display.white_point[1];
         metadata->white_point[1].den = chroma_den;
+        metadata->has_primaries &= sei->mastering_display.white_point[0] >= 5 &&
+                                   sei->mastering_display.white_point[0] <= 42000;
 
         metadata->max_luminance.num = sei->mastering_display.max_luminance;
         metadata->max_luminance.den = luma_den;
+        metadata->has_luminance &= sei->mastering_display.max_luminance >= 50000 &&
+                                   sei->mastering_display.max_luminance <= 100000000;
+
         metadata->min_luminance.num = sei->mastering_display.min_luminance;
         metadata->min_luminance.den = luma_den;
-        metadata->has_luminance = 1;
-        metadata->has_primaries = 1;
-
-        av_log(avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
-        av_log(avctx, AV_LOG_DEBUG,
-               "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
-               av_q2d(metadata->display_primaries[0][0]),
-               av_q2d(metadata->display_primaries[0][1]),
-               av_q2d(metadata->display_primaries[1][0]),
-               av_q2d(metadata->display_primaries[1][1]),
-               av_q2d(metadata->display_primaries[2][0]),
-               av_q2d(metadata->display_primaries[2][1]),
-               av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
-        av_log(avctx, AV_LOG_DEBUG,
-               "min_luminance=%f, max_luminance=%f\n",
-               av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
+        metadata->has_luminance &= sei->mastering_display.min_luminance >= 1 &&
+                                   sei->mastering_display.min_luminance <= 50000;
+
+        if (metadata->has_luminance || metadata->has_primaries)
+            av_log(avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
+        if (metadata->has_primaries) {
+            av_log(avctx, AV_LOG_DEBUG,
+                   "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
+                   av_q2d(metadata->display_primaries[0][0]),
+                   av_q2d(metadata->display_primaries[0][1]),
+                   av_q2d(metadata->display_primaries[1][0]),
+                   av_q2d(metadata->display_primaries[1][1]),
+                   av_q2d(metadata->display_primaries[2][0]),
+                   av_q2d(metadata->display_primaries[2][1]),
+                   av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
+        }
+        if (metadata->has_luminance) {
+            av_log(avctx, AV_LOG_DEBUG,
+                   "min_luminance=%f, max_luminance=%f\n",
+                   av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
+        }
     }
 
     if (sei->content_light.present) {
-- 
2.42.1



More information about the ffmpeg-devel mailing list