summaryrefslogtreecommitdiff
path: root/libavcodec/cbs_mpeg2.c
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2019-06-20 01:45:12 +0200
committerMark Thompson <sw@jkqxz.net>2019-07-27 22:00:43 +0100
commitd9182f04caa59c3ba2659981183238ada340f814 (patch)
tree78c65c5caac077addfde2c74d0c4c556fe373773 /libavcodec/cbs_mpeg2.c
parentb71a0367a6e763d631b8dcd608f98d42c05fa57c (diff)
downloadffmpeg-d9182f04caa59c3ba2659981183238ada340f814.tar.gz
cbs_mpeg2: Fix parsing of picture and slice headers
1. The extra information in slice headers was parsed incorrectly: In the first reading pass to derive the length of the extra information, one should look at bits n, n + 9, n + 18, ... and check whether they equal one (further extra information) or zero (end of extra information), but instead bits n, n + 8, n + 16, ... were inspected. The second pass of reading (where the length is already known and the bytes between the length-determining bits are copied into a buffer) did not record what was in bits n, n + 9, n + 18, ..., presuming they equal one. And during writing, the bytes in the buffer are interleaved with set bits and written. This means that if the detected length of the extra information was greater than the real length, the output was corrupted. Fortunately no sample is known that made use of this mechanism: The extra information in slices is still marked as reserved in the specifications. cbs_mpeg2 is now ready in case this changes. 2. Furthermore, the buffer is now padded and slightly different, but very similar code for reading resp. writing has been replaced by code used for both. This was made possible by a new macro, the equivalent to cbs_h2645's fixed(). 3. These changes also made it possible to remove the extra_bit_slice element from the MPEG2RawSliceHeader structure. Said element was always zero except when the detected length of the extra information was less than the real length. 4. The extra information in picture headers (which uses essentially the same syntax as the extra information in slice headers) has simply been forgotten. This meant that if this extra information was present, it was discarded during reading; and unfortunately writing created invalid bitstreams in this case (an extra_bit_picture - the last set bit of the whole unit - indicated that there would be a further byte of data, although the output didn't contain said data). This has been fixed; both types of extra information are now parsed via the same code and essentially passed through. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Diffstat (limited to 'libavcodec/cbs_mpeg2.c')
-rw-r--r--libavcodec/cbs_mpeg2.c43
1 files changed, 24 insertions, 19 deletions
diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 2466f569f6..516b728a64 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -48,17 +48,26 @@
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 xui(width, name, var, range_min, range_max, subs, ...) \
+ xuia(width, #name, var, range_min, range_max, subs, __VA_ARGS__)
#define sis(width, name, subs, ...) \
xsi(width, name, current->name, subs, __VA_ARGS__)
+#define marker_bit() \
+ bit("marker_bit", 1)
+#define bit(string, value) do { \
+ av_unused uint32_t bit = value; \
+ xuia(1, string, bit, value, value, 0); \
+ } while (0)
+
#define READ
#define READWRITE read
#define RWContext GetBitContext
-#define xui(width, name, var, range_min, range_max, subs, ...) do { \
+#define xuia(width, string, var, range_min, range_max, subs, ...) do { \
uint32_t value; \
- CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
+ CHECK(ff_cbs_read_unsigned(ctx, rw, width, string, \
SUBSCRIPTS(subs, __VA_ARGS__), \
&value, range_min, range_max)); \
var = value; \
@@ -73,11 +82,6 @@
var = value; \
} while (0)
-#define marker_bit() do { \
- av_unused uint32_t one; \
- CHECK(ff_cbs_read_unsigned(ctx, rw, 1, "marker_bit", NULL, &one, 1, 1)); \
- } while (0)
-
#define nextbits(width, compare, var) \
(get_bits_left(rw) >= width && \
(var = show_bits(rw, width)) == (compare))
@@ -91,9 +95,8 @@
#undef READ
#undef READWRITE
#undef RWContext
-#undef xui
+#undef xuia
#undef xsi
-#undef marker_bit
#undef nextbits
#undef infer
@@ -102,8 +105,8 @@
#define READWRITE write
#define RWContext PutBitContext
-#define xui(width, name, var, range_min, range_max, subs, ...) do { \
- CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
+#define xuia(width, string, var, range_min, range_max, subs, ...) do { \
+ CHECK(ff_cbs_write_unsigned(ctx, rw, width, string, \
SUBSCRIPTS(subs, __VA_ARGS__), \
var, range_min, range_max)); \
} while (0)
@@ -115,10 +118,6 @@
MAX_INT_BITS(width))); \
} while (0)
-#define marker_bit() do { \
- CHECK(ff_cbs_write_unsigned(ctx, rw, 1, "marker_bit", NULL, 1, 1, 1)); \
- } while (0)
-
#define nextbits(width, compare, var) (var)
#define infer(name, value) do { \
@@ -135,13 +134,19 @@
#undef WRITE
#undef READWRITE
#undef RWContext
-#undef xui
+#undef xuia
#undef xsi
-#undef marker_bit
#undef nextbits
#undef infer
+static void cbs_mpeg2_free_picture_header(void *unit, uint8_t *content)
+{
+ MPEG2RawPictureHeader *picture = (MPEG2RawPictureHeader*)content;
+ av_buffer_unref(&picture->extra_information_picture.extra_information_ref);
+ av_freep(&content);
+}
+
static void cbs_mpeg2_free_user_data(void *unit, uint8_t *content)
{
MPEG2RawUserData *user = (MPEG2RawUserData*)content;
@@ -152,7 +157,7 @@ static void cbs_mpeg2_free_user_data(void *unit, uint8_t *content)
static void cbs_mpeg2_free_slice(void *unit, uint8_t *content)
{
MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
- av_buffer_unref(&slice->header.extra_information_ref);
+ av_buffer_unref(&slice->header.extra_information_slice.extra_information_ref);
av_buffer_unref(&slice->data_ref);
av_freep(&content);
}
@@ -255,7 +260,7 @@ static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
} \
break;
START(MPEG2_START_PICTURE, MPEG2RawPictureHeader,
- picture_header, NULL);
+ picture_header, &cbs_mpeg2_free_picture_header);
START(MPEG2_START_USER_DATA, MPEG2RawUserData,
user_data, &cbs_mpeg2_free_user_data);
START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader,