diff mbox series

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

Message ID 20240225021819.1258-1-kasper93@gmail.com
State New
Headers show
Series [FFmpeg-devel,v2] avcodec/h2645_sei: validate Mastering Display Colour Volume SEI values | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Kacper Michajlow Feb. 25, 2024, 2:17 a.m. UTC
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@gmail.com>
---
 libavcodec/h2645_sei.c | 53 +++++++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 16 deletions(-)

Comments

Niklas Haas Feb. 26, 2024, 12:04 p.m. UTC | #1
On Sun, 25 Feb 2024 03:17:55 +0100 Kacper Michajłow <kasper93@gmail.com> wrote:
> -        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;

    ...                                    &&
    sei->mastering.display.min_luminance <
    sei->mastering_display.max_luminance;

According to spec, at least. Handles the edge case of min = max = 50000.
diff mbox series

Patch

diff --git a/libavcodec/h2645_sei.c b/libavcodec/h2645_sei.c
index cb6be0594b..e3581e8136 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[1] >= 5 &&
+                                   sei->mastering_display.white_point[1] <= 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) {