Skip to content

Commit b422035

Browse files
committed
mpeg2_metadata, cbs_mpeg2: Fix handling of colour_description
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@gmail.com>
1 parent 909bced commit b422035

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

libavcodec/cbs_mpeg2.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282
(get_bits_left(rw) >= width && \
8383
(var = show_bits(rw, width)) == (compare))
8484

85+
#define infer(name, value) do { \
86+
current->name = value; \
87+
} while (0)
88+
8589
#include "cbs_mpeg2_syntax_template.c"
8690

8791
#undef READ
@@ -91,6 +95,7 @@
9195
#undef xsi
9296
#undef marker_bit
9397
#undef nextbits
98+
#undef infer
9499

95100

96101
#define WRITE
@@ -116,6 +121,15 @@
116121

117122
#define nextbits(width, compare, var) (var)
118123

124+
#define infer(name, value) do { \
125+
if (current->name != (value)) { \
126+
av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
127+
"%s does not match inferred value: " \
128+
"%"PRId64", but should be %"PRId64".\n", \
129+
#name, (int64_t)current->name, (int64_t)(value)); \
130+
} \
131+
} while (0)
132+
119133
#include "cbs_mpeg2_syntax_template.c"
120134

121135
#undef READ
@@ -125,6 +139,7 @@
125139
#undef xsi
126140
#undef marker_bit
127141
#undef nextbits
142+
#undef infer
128143

129144

130145
static void cbs_mpeg2_free_user_data(void *unit, uint8_t *content)

libavcodec/cbs_mpeg2_syntax_template.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ static int FUNC(sequence_display_extension)(CodedBitstreamContext *ctx, RWContex
144144
uir(8, transfer_characteristics);
145145
uir(8, matrix_coefficients);
146146
#endif
147+
} else {
148+
infer(colour_primaries, 2);
149+
infer(transfer_characteristics, 2);
150+
infer(matrix_coefficients, 2);
147151
}
148152

149153
ui(14, display_horizontal_size);

libavcodec/mpeg2_metadata_bsf.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ static int mpeg2_metadata_update_fragment(AVBSFContext *bsf,
111111
}
112112

113113
if (ctx->video_format >= 0 ||
114-
ctx->colour_primaries >= 0 ||
115-
ctx->transfer_characteristics >= 0 ||
116-
ctx->matrix_coefficients >= 0) {
114+
ctx->colour_primaries > 0 ||
115+
ctx->transfer_characteristics > 0 ||
116+
ctx->matrix_coefficients > 0) {
117117
if (!sde) {
118118
add_sde = 1;
119119
ctx->sequence_display_extension.extension_start_code =
@@ -140,25 +140,19 @@ static int mpeg2_metadata_update_fragment(AVBSFContext *bsf,
140140
if (ctx->video_format >= 0)
141141
sde->video_format = ctx->video_format;
142142

143-
if (ctx->colour_primaries >= 0 ||
144-
ctx->transfer_characteristics >= 0 ||
145-
ctx->matrix_coefficients >= 0) {
143+
if (ctx->colour_primaries > 0 ||
144+
ctx->transfer_characteristics > 0 ||
145+
ctx->matrix_coefficients > 0) {
146146
sde->colour_description = 1;
147147

148-
if (ctx->colour_primaries >= 0)
148+
if (ctx->colour_primaries > 0)
149149
sde->colour_primaries = ctx->colour_primaries;
150-
else if (add_sde)
151-
sde->colour_primaries = 2;
152150

153-
if (ctx->transfer_characteristics >= 0)
151+
if (ctx->transfer_characteristics > 0)
154152
sde->transfer_characteristics = ctx->transfer_characteristics;
155-
else if (add_sde)
156-
sde->transfer_characteristics = 2;
157153

158-
if (ctx->matrix_coefficients >= 0)
154+
if (ctx->matrix_coefficients > 0)
159155
sde->matrix_coefficients = ctx->matrix_coefficients;
160-
else if (add_sde)
161-
sde->matrix_coefficients = 2;
162156
}
163157
}
164158

@@ -283,13 +277,13 @@ static const AVOption mpeg2_metadata_options[] = {
283277
{ .i64 = -1 }, -1, 7, FLAGS },
284278
{ "colour_primaries", "Set colour primaries (table 6-7)",
285279
OFFSET(colour_primaries), AV_OPT_TYPE_INT,
286-
{ .i64 = -1 }, -1, 255, FLAGS },
280+
{ .i64 = 0 }, 0, 255, FLAGS },
287281
{ "transfer_characteristics", "Set transfer characteristics (table 6-8)",
288282
OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
289-
{ .i64 = -1 }, -1, 255, FLAGS },
283+
{ .i64 = 0 }, 0, 255, FLAGS },
290284
{ "matrix_coefficients", "Set matrix coefficients (table 6-9)",
291285
OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
292-
{ .i64 = -1 }, -1, 255, FLAGS },
286+
{ .i64 = 0 }, 0, 255, FLAGS },
293287

294288
{ NULL }
295289
};

0 commit comments

Comments
 (0)