[FFmpeg-cvslog] cbs_mpeg2: Improve checks for invalid values
Andreas Rheinhardt
git at videolan.org
Wed May 29 02:36:36 EEST 2019
ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at gmail.com> | Wed May 22 03:04:32 2019 +0200| [9c3f2a8894a66d6b5b9285caa25f91fbfca7b3bc] | committer: Mark Thompson
cbs_mpeg2: Improve checks for invalid values
MPEG-2 contains several elements that mustn't be zero according to the
specifications: horizontal/vertical_size_value, aspect_ratio_information,
frame_rate_code, the quantiser matrices, the colour_description
elements, picture_coding_type, the f_code[r][s] values and
quantiser_scale_code. It is now checked that the invalid values don't
occur.
The colour_description elements are treated specially in this regard:
Given that there are files in the wild which use illegal values for the
colour_description elements (some of them created by mpeg2_metadata),
they will be corrected to the value meaning "unknown" (namely 2) during
reading. This has been done in such a way that trace_headers will
nevertheless report the original value, together with a message about
the fixup.
Furthermore, the trace_headers output of user_data has been beautified.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=9c3f2a8894a66d6b5b9285caa25f91fbfca7b3bc
---
libavcodec/cbs_mpeg2.c | 16 +++++----
libavcodec/cbs_mpeg2_syntax_template.c | 64 +++++++++++++++++++++-------------
2 files changed, 50 insertions(+), 30 deletions(-)
diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index f9f1d86b49..4605b2056e 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -41,20 +41,24 @@
#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
#define ui(width, name) \
- xui(width, name, current->name, 0)
+ xui(width, name, current->name, 0, MAX_UINT_BITS(width), 0)
+#define uir(width, name) \
+ xui(width, name, current->name, 1, MAX_UINT_BITS(width), 0)
#define uis(width, name, subs, ...) \
- xui(width, name, current->name, subs, __VA_ARGS__)
+ xui(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
+#define uirs(width, name, subs, ...) \
+ xui(width, name, current->name, 1, MAX_UINT_BITS(width), subs, __VA_ARGS__)
#define READ
#define READWRITE read
#define RWContext GetBitContext
-#define xui(width, name, var, subs, ...) do { \
+#define xui(width, name, var, range_min, range_max, subs, ...) do { \
uint32_t value = 0; \
CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
SUBSCRIPTS(subs, __VA_ARGS__), \
- &value, 0, (1 << width) - 1)); \
+ &value, range_min, range_max)); \
var = value; \
} while (0)
@@ -81,10 +85,10 @@
#define READWRITE write
#define RWContext PutBitContext
-#define xui(width, name, var, subs, ...) do { \
+#define xui(width, name, var, range_min, range_max, subs, ...) do { \
CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
SUBSCRIPTS(subs, __VA_ARGS__), \
- var, 0, (1 << width) - 1)); \
+ var, range_min, range_max)); \
} while (0)
#define marker_bit() do { \
diff --git a/libavcodec/cbs_mpeg2_syntax_template.c b/libavcodec/cbs_mpeg2_syntax_template.c
index 10aaea7734..b9d53682fe 100644
--- a/libavcodec/cbs_mpeg2_syntax_template.c
+++ b/libavcodec/cbs_mpeg2_syntax_template.c
@@ -26,14 +26,14 @@ static int FUNC(sequence_header)(CodedBitstreamContext *ctx, RWContext *rw,
ui(8, sequence_header_code);
- ui(12, horizontal_size_value);
- ui(12, vertical_size_value);
+ uir(12, horizontal_size_value);
+ uir(12, vertical_size_value);
mpeg2->horizontal_size = current->horizontal_size_value;
mpeg2->vertical_size = current->vertical_size_value;
- ui(4, aspect_ratio_information);
- ui(4, frame_rate_code);
+ uir(4, aspect_ratio_information);
+ uir(4, frame_rate_code);
ui(18, bit_rate_value);
marker_bit();
@@ -44,13 +44,13 @@ static int FUNC(sequence_header)(CodedBitstreamContext *ctx, RWContext *rw,
ui(1, load_intra_quantiser_matrix);
if (current->load_intra_quantiser_matrix) {
for (i = 0; i < 64; i++)
- uis(8, intra_quantiser_matrix[i], 1, i);
+ uirs(8, intra_quantiser_matrix[i], 1, i);
}
ui(1, load_non_intra_quantiser_matrix);
if (current->load_non_intra_quantiser_matrix) {
for (i = 0; i < 64; i++)
- uis(8, non_intra_quantiser_matrix[i], 1, i);
+ uirs(8, non_intra_quantiser_matrix[i], 1, i);
}
return 0;
@@ -79,7 +79,7 @@ static int FUNC(user_data)(CodedBitstreamContext *ctx, RWContext *rw,
#endif
for (k = 0; k < current->user_data_length; k++)
- xui(8, user_data, current->user_data[k], 0);
+ uis(8, user_data[k], 1, k);
return 0;
}
@@ -125,9 +125,25 @@ static int FUNC(sequence_display_extension)(CodedBitstreamContext *ctx, RWContex
ui(1, colour_description);
if (current->colour_description) {
- ui(8, colour_primaries);
- ui(8, transfer_characteristics);
- ui(8, matrix_coefficients);
+#ifdef READ
+#define READ_AND_PATCH(name) do { \
+ ui(8, name); \
+ if (current->name == 0) { \
+ current->name = 2; \
+ av_log(ctx->log_ctx, AV_LOG_WARNING, "%s in a sequence display " \
+ "extension had the invalid value 0. Setting it to 2 " \
+ "(meaning unknown) instead.\n", #name); \
+ } \
+ } while (0)
+ READ_AND_PATCH(colour_primaries);
+ READ_AND_PATCH(transfer_characteristics);
+ READ_AND_PATCH(matrix_coefficients);
+#undef READ_AND_PATCH
+#else
+ uir(8, colour_primaries);
+ uir(8, transfer_characteristics);
+ uir(8, matrix_coefficients);
+#endif
}
ui(14, display_horizontal_size);
@@ -163,7 +179,7 @@ static int FUNC(picture_header)(CodedBitstreamContext *ctx, RWContext *rw,
ui(8, picture_start_code);
ui(10, temporal_reference);
- ui(3, picture_coding_type);
+ uir(3, picture_coding_type);
ui(16, vbv_delay);
if (current->picture_coding_type == 2 ||
@@ -190,10 +206,10 @@ static int FUNC(picture_coding_extension)(CodedBitstreamContext *ctx, RWContext
HEADER("Picture Coding Extension");
- ui(4, f_code[0][0]);
- ui(4, f_code[0][1]);
- ui(4, f_code[1][0]);
- ui(4, f_code[1][1]);
+ uir(4, f_code[0][0]);
+ uir(4, f_code[0][1]);
+ uir(4, f_code[1][0]);
+ uir(4, f_code[1][1]);
ui(2, intra_dc_precision);
ui(2, picture_structure);
@@ -250,25 +266,25 @@ static int FUNC(quant_matrix_extension)(CodedBitstreamContext *ctx, RWContext *r
ui(1, load_intra_quantiser_matrix);
if (current->load_intra_quantiser_matrix) {
for (i = 0; i < 64; i++)
- uis(8, intra_quantiser_matrix[i], 1, i);
+ uirs(8, intra_quantiser_matrix[i], 1, i);
}
ui(1, load_non_intra_quantiser_matrix);
if (current->load_non_intra_quantiser_matrix) {
for (i = 0; i < 64; i++)
- uis(8, non_intra_quantiser_matrix[i], 1, i);
+ uirs(8, non_intra_quantiser_matrix[i], 1, i);
}
ui(1, load_chroma_intra_quantiser_matrix);
if (current->load_chroma_intra_quantiser_matrix) {
for (i = 0; i < 64; i++)
- uis(8, intra_quantiser_matrix[i], 1, i);
+ uirs(8, intra_quantiser_matrix[i], 1, i);
}
ui(1, load_chroma_non_intra_quantiser_matrix);
if (current->load_chroma_non_intra_quantiser_matrix) {
for (i = 0; i < 64; i++)
- uis(8, chroma_non_intra_quantiser_matrix[i], 1, i);
+ uirs(8, chroma_non_intra_quantiser_matrix[i], 1, i);
}
return 0;
@@ -342,7 +358,7 @@ static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
ui(7, priority_breakpoint);
}
- ui(5, quantiser_scale_code);
+ uir(5, quantiser_scale_code);
if (nextbits(1, 1, current->slice_extension_flag)) {
ui(1, slice_extension_flag);
@@ -366,16 +382,16 @@ static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
if (!current->extra_information)
return AVERROR(ENOMEM);
for (k = 0; k < current->extra_information_length; k++) {
- xui(1, extra_bit_slice, bit, 0);
+ xui(1, extra_bit_slice, bit, 1, 1, 0);
xui(8, extra_information_slice[k],
- current->extra_information[k], 1, k);
+ current->extra_information[k], 0, 255, 1, k);
}
}
#else
for (k = 0; k < current->extra_information_length; k++) {
- xui(1, extra_bit_slice, 1, 0);
+ xui(1, extra_bit_slice, 1, 1, 1, 0);
xui(8, extra_information_slice[k],
- current->extra_information[k], 1, k);
+ current->extra_information[k], 0, 255, 1, k);
}
#endif
}
More information about the ffmpeg-cvslog
mailing list