[FFmpeg-devel] [PATCH 1/4] mpeg2_metadata, cbs_mpeg2: Fix handling of colour_description

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Mon Jun 3 01:37:27 EEST 2019


If a sequence display extension is read with colour_description equal to
zero, but a user wants to add one or more of the colour_description
elements, then the colour_description elements the user did not explicitly
request to be set are set to zero and not to the value equal to
unknown/unspecified (namely 2). A value of zero is not only inappropriate,
but explicitly forbidden. This is fixed by inferring the right default
values during the reading process if the elements are absent; moreover,
changing any of the colour_description elements to zero is now no longer
possible.

Furthermore, if a sequence display extension has to be added, the
earlier code set some fields to their default value twice. This has been
changed, too.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
I implemented your solution, because it is easy to add the check I just
removed back in should it be needed some day because of the
AVCodecParameter stuff.
 libavcodec/cbs_mpeg2.c                 | 15 +++++++++++++
 libavcodec/cbs_mpeg2_syntax_template.c |  4 ++++
 libavcodec/mpeg2_metadata_bsf.c        | 30 +++++++++++---------------
 3 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index ce22e32c15..5e971f3a54 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -82,6 +82,10 @@
     (get_bits_left(rw) >= width && \
      (var = show_bits(rw, width)) == (compare))
 
+#define infer(name, value) do { \
+        current->name = value; \
+    } while (0)
+
 #include "cbs_mpeg2_syntax_template.c"
 
 #undef READ
@@ -91,6 +95,7 @@
 #undef xsi
 #undef marker_bit
 #undef nextbits
+#undef infer
 
 
 #define WRITE
@@ -116,6 +121,15 @@
 
 #define nextbits(width, compare, var) (var)
 
+#define infer(name, value) do { \
+        if (current->name != (value)) { \
+            av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
+                   "%s does not match inferred value: " \
+                   "%"PRId64", but should be %"PRId64".\n", \
+                   #name, (int64_t)current->name, (int64_t)(value)); \
+        } \
+    } while (0)
+
 #include "cbs_mpeg2_syntax_template.c"
 
 #undef READ
@@ -125,6 +139,7 @@
 #undef xsi
 #undef marker_bit
 #undef nextbits
+#undef infer
 
 
 static void cbs_mpeg2_free_user_data(void *unit, uint8_t *content)
diff --git a/libavcodec/cbs_mpeg2_syntax_template.c b/libavcodec/cbs_mpeg2_syntax_template.c
index e0cf716874..d9ef480f39 100644
--- a/libavcodec/cbs_mpeg2_syntax_template.c
+++ b/libavcodec/cbs_mpeg2_syntax_template.c
@@ -144,6 +144,10 @@ static int FUNC(sequence_display_extension)(CodedBitstreamContext *ctx, RWContex
         uir(8, transfer_characteristics);
         uir(8, matrix_coefficients);
 #endif
+    } else {
+        infer(colour_primaries,         2);
+        infer(transfer_characteristics, 2);
+        infer(matrix_coefficients,      2);
     }
 
     ui(14, display_horizontal_size);
diff --git a/libavcodec/mpeg2_metadata_bsf.c b/libavcodec/mpeg2_metadata_bsf.c
index ba3a74afda..57eded0808 100644
--- a/libavcodec/mpeg2_metadata_bsf.c
+++ b/libavcodec/mpeg2_metadata_bsf.c
@@ -111,9 +111,9 @@ static int mpeg2_metadata_update_fragment(AVBSFContext *bsf,
     }
 
     if (ctx->video_format             >= 0 ||
-        ctx->colour_primaries         >= 0 ||
-        ctx->transfer_characteristics >= 0 ||
-        ctx->matrix_coefficients      >= 0) {
+        ctx->colour_primaries         >  0 ||
+        ctx->transfer_characteristics >  0 ||
+        ctx->matrix_coefficients      >  0) {
         if (!sde) {
             add_sde = 1;
             ctx->sequence_display_extension.extension_start_code =
@@ -140,25 +140,19 @@ static int mpeg2_metadata_update_fragment(AVBSFContext *bsf,
         if (ctx->video_format >= 0)
             sde->video_format = ctx->video_format;
 
-        if (ctx->colour_primaries         >= 0 ||
-            ctx->transfer_characteristics >= 0 ||
-            ctx->matrix_coefficients      >= 0) {
+        if (ctx->colour_primaries         > 0 ||
+            ctx->transfer_characteristics > 0 ||
+            ctx->matrix_coefficients      > 0) {
             sde->colour_description = 1;
 
-            if (ctx->colour_primaries >= 0)
+            if (ctx->colour_primaries > 0)
                 sde->colour_primaries = ctx->colour_primaries;
-            else if (add_sde)
-                sde->colour_primaries = 2;
 
-            if (ctx->transfer_characteristics >= 0)
+            if (ctx->transfer_characteristics > 0)
                 sde->transfer_characteristics = ctx->transfer_characteristics;
-            else if (add_sde)
-                sde->transfer_characteristics = 2;
 
-            if (ctx->matrix_coefficients >= 0)
+            if (ctx->matrix_coefficients > 0)
                 sde->matrix_coefficients = ctx->matrix_coefficients;
-            else if (add_sde)
-                sde->matrix_coefficients = 2;
         }
     }
 
@@ -283,13 +277,13 @@ static const AVOption mpeg2_metadata_options[] = {
         { .i64 = -1 }, -1, 7, FLAGS },
     { "colour_primaries", "Set colour primaries (table 6-7)",
         OFFSET(colour_primaries), AV_OPT_TYPE_INT,
-        { .i64 = -1 }, -1, 255, FLAGS },
+        { .i64 = 0 }, 0, 255, FLAGS },
     { "transfer_characteristics", "Set transfer characteristics (table 6-8)",
         OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
-        { .i64 = -1 }, -1, 255, FLAGS },
+        { .i64 = 0 }, 0, 255, FLAGS },
     { "matrix_coefficients", "Set matrix coefficients (table 6-9)",
         OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
-        { .i64 = -1 }, -1, 255, FLAGS },
+        { .i64 = 0 }, 0, 255, FLAGS },
 
     { NULL }
 };
-- 
2.21.0



More information about the ffmpeg-devel mailing list