2017-05-05 00:03:03 +02:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
|
|
|
|
#include "cbs.h"
|
|
|
|
#include "cbs_internal.h"
|
|
|
|
#include "cbs_mpeg2.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define HEADER(name) do { \
|
|
|
|
ff_cbs_trace_header(ctx, name); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define CHECK(call) do { \
|
|
|
|
err = (call); \
|
|
|
|
if (err < 0) \
|
|
|
|
return err; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
|
|
|
|
#define FUNC_MPEG2(rw, name) FUNC_NAME(rw, mpeg2, name)
|
|
|
|
#define FUNC(name) FUNC_MPEG2(READWRITE, name)
|
|
|
|
|
2018-04-30 23:35:30 +02:00
|
|
|
#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
|
|
|
|
|
|
|
|
#define ui(width, name) \
|
2020-03-22 21:34:21 +02:00
|
|
|
xui(width, name, current->name, 0, MAX_UINT_BITS(width), 0, )
|
2019-05-22 03:04:32 +02:00
|
|
|
#define uir(width, name) \
|
2020-03-22 21:34:21 +02:00
|
|
|
xui(width, name, current->name, 1, MAX_UINT_BITS(width), 0, )
|
2018-04-30 23:35:30 +02:00
|
|
|
#define uis(width, name, subs, ...) \
|
2019-05-22 03:04:32 +02:00
|
|
|
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__)
|
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>
2019-06-20 01:45:12 +02:00
|
|
|
#define xui(width, name, var, range_min, range_max, subs, ...) \
|
|
|
|
xuia(width, #name, var, range_min, range_max, subs, __VA_ARGS__)
|
2019-05-22 03:04:34 +02:00
|
|
|
#define sis(width, name, subs, ...) \
|
|
|
|
xsi(width, name, current->name, subs, __VA_ARGS__)
|
2018-04-30 23:35:30 +02:00
|
|
|
|
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>
2019-06-20 01:45:12 +02:00
|
|
|
#define marker_bit() \
|
|
|
|
bit("marker_bit", 1)
|
|
|
|
#define bit(string, value) do { \
|
|
|
|
av_unused uint32_t bit = value; \
|
2020-03-22 21:34:21 +02:00
|
|
|
xuia(1, string, bit, value, value, 0, ); \
|
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>
2019-06-20 01:45:12 +02:00
|
|
|
} while (0)
|
|
|
|
|
2017-05-05 00:03:03 +02:00
|
|
|
|
|
|
|
#define READ
|
|
|
|
#define READWRITE read
|
|
|
|
#define RWContext GetBitContext
|
|
|
|
|
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>
2019-06-20 01:45:12 +02:00
|
|
|
#define xuia(width, string, var, range_min, range_max, subs, ...) do { \
|
2019-06-20 01:45:11 +02:00
|
|
|
uint32_t value; \
|
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>
2019-06-20 01:45:12 +02:00
|
|
|
CHECK(ff_cbs_read_unsigned(ctx, rw, width, string, \
|
2018-04-30 23:35:30 +02:00
|
|
|
SUBSCRIPTS(subs, __VA_ARGS__), \
|
2019-05-22 03:04:32 +02:00
|
|
|
&value, range_min, range_max)); \
|
2017-05-05 00:03:03 +02:00
|
|
|
var = value; \
|
|
|
|
} while (0)
|
|
|
|
|
2019-05-22 03:04:34 +02:00
|
|
|
#define xsi(width, name, var, subs, ...) do { \
|
|
|
|
int32_t value; \
|
|
|
|
CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
|
|
|
|
SUBSCRIPTS(subs, __VA_ARGS__), &value, \
|
|
|
|
MIN_INT_BITS(width), \
|
|
|
|
MAX_INT_BITS(width))); \
|
|
|
|
var = value; \
|
|
|
|
} while (0)
|
|
|
|
|
2017-05-05 00:03:03 +02:00
|
|
|
#define nextbits(width, compare, var) \
|
|
|
|
(get_bits_left(rw) >= width && \
|
|
|
|
(var = show_bits(rw, width)) == (compare))
|
|
|
|
|
2019-06-20 01:45:10 +02:00
|
|
|
#define infer(name, value) do { \
|
|
|
|
current->name = value; \
|
|
|
|
} while (0)
|
|
|
|
|
2017-05-05 00:03:03 +02:00
|
|
|
#include "cbs_mpeg2_syntax_template.c"
|
|
|
|
|
|
|
|
#undef READ
|
|
|
|
#undef READWRITE
|
|
|
|
#undef RWContext
|
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>
2019-06-20 01:45:12 +02:00
|
|
|
#undef xuia
|
2019-05-22 03:04:34 +02:00
|
|
|
#undef xsi
|
2017-05-05 00:03:03 +02:00
|
|
|
#undef nextbits
|
2019-06-20 01:45:10 +02:00
|
|
|
#undef infer
|
2017-05-05 00:03:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
#define WRITE
|
|
|
|
#define READWRITE write
|
|
|
|
#define RWContext PutBitContext
|
|
|
|
|
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>
2019-06-20 01:45:12 +02:00
|
|
|
#define xuia(width, string, var, range_min, range_max, subs, ...) do { \
|
|
|
|
CHECK(ff_cbs_write_unsigned(ctx, rw, width, string, \
|
2018-04-30 23:35:30 +02:00
|
|
|
SUBSCRIPTS(subs, __VA_ARGS__), \
|
2019-05-22 03:04:32 +02:00
|
|
|
var, range_min, range_max)); \
|
2017-05-05 00:03:03 +02:00
|
|
|
} while (0)
|
|
|
|
|
2019-05-22 03:04:34 +02:00
|
|
|
#define xsi(width, name, var, subs, ...) do { \
|
|
|
|
CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
|
|
|
|
SUBSCRIPTS(subs, __VA_ARGS__), var, \
|
|
|
|
MIN_INT_BITS(width), \
|
|
|
|
MAX_INT_BITS(width))); \
|
|
|
|
} while (0)
|
|
|
|
|
2017-05-05 00:03:03 +02:00
|
|
|
#define nextbits(width, compare, var) (var)
|
|
|
|
|
2019-06-20 01:45:10 +02:00
|
|
|
#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)
|
|
|
|
|
2017-05-05 00:03:03 +02:00
|
|
|
#include "cbs_mpeg2_syntax_template.c"
|
|
|
|
|
2019-06-07 02:27:11 +02:00
|
|
|
#undef WRITE
|
2017-05-05 00:03:03 +02:00
|
|
|
#undef READWRITE
|
|
|
|
#undef RWContext
|
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>
2019-06-20 01:45:12 +02:00
|
|
|
#undef xuia
|
2019-05-22 03:04:34 +02:00
|
|
|
#undef xsi
|
2017-05-05 00:03:03 +02:00
|
|
|
#undef nextbits
|
2019-06-20 01:45:10 +02:00
|
|
|
#undef infer
|
2017-05-05 00:03:03 +02:00
|
|
|
|
|
|
|
|
2019-07-29 21:56:52 +02:00
|
|
|
static void cbs_mpeg2_free_picture_header(void *opaque, uint8_t *content)
|
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>
2019-06-20 01:45:12 +02:00
|
|
|
{
|
|
|
|
MPEG2RawPictureHeader *picture = (MPEG2RawPictureHeader*)content;
|
|
|
|
av_buffer_unref(&picture->extra_information_picture.extra_information_ref);
|
|
|
|
av_freep(&content);
|
|
|
|
}
|
|
|
|
|
2019-07-29 21:56:52 +02:00
|
|
|
static void cbs_mpeg2_free_user_data(void *opaque, uint8_t *content)
|
2018-02-11 02:51:33 +02:00
|
|
|
{
|
|
|
|
MPEG2RawUserData *user = (MPEG2RawUserData*)content;
|
|
|
|
av_buffer_unref(&user->user_data_ref);
|
|
|
|
av_freep(&content);
|
|
|
|
}
|
|
|
|
|
2019-07-29 21:56:52 +02:00
|
|
|
static void cbs_mpeg2_free_slice(void *opaque, uint8_t *content)
|
2018-02-11 02:51:33 +02:00
|
|
|
{
|
|
|
|
MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
|
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>
2019-06-20 01:45:12 +02:00
|
|
|
av_buffer_unref(&slice->header.extra_information_slice.extra_information_ref);
|
2018-02-11 02:51:33 +02:00
|
|
|
av_buffer_unref(&slice->data_ref);
|
|
|
|
av_freep(&content);
|
|
|
|
}
|
|
|
|
|
2017-05-05 00:03:03 +02:00
|
|
|
static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
|
|
|
|
CodedBitstreamFragment *frag,
|
|
|
|
int header)
|
|
|
|
{
|
|
|
|
const uint8_t *start, *end;
|
2019-07-29 21:56:55 +02:00
|
|
|
CodedBitstreamUnitType unit_type;
|
|
|
|
uint32_t start_code = -1;
|
2017-05-05 00:03:03 +02:00
|
|
|
size_t unit_size;
|
2019-07-29 21:56:56 +02:00
|
|
|
int err, i, final = 0;
|
2017-05-05 00:03:03 +02:00
|
|
|
|
|
|
|
start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
|
|
|
|
&start_code);
|
2019-07-29 21:56:56 +02:00
|
|
|
if (start_code >> 8 != 0x000001) {
|
|
|
|
// No start code found.
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2017-05-05 00:03:03 +02:00
|
|
|
for (i = 0;; i++) {
|
|
|
|
unit_type = start_code & 0xff;
|
|
|
|
|
2019-07-29 21:56:56 +02:00
|
|
|
if (start == frag->data + frag->data_size) {
|
|
|
|
// The last four bytes form a start code which constitutes
|
|
|
|
// a unit of its own. In this situation avpriv_find_start_code
|
|
|
|
// won't modify start_code at all so modify start_code so that
|
|
|
|
// the next unit will be treated as the last unit.
|
|
|
|
start_code = 0;
|
|
|
|
}
|
|
|
|
|
2019-07-29 21:56:55 +02:00
|
|
|
end = avpriv_find_start_code(start--, frag->data + frag->data_size,
|
|
|
|
&start_code);
|
|
|
|
|
|
|
|
// start points to the byte containing the start_code_identifier
|
2019-07-29 21:56:56 +02:00
|
|
|
// (may be the last byte of fragment->data); end points to the byte
|
2019-07-29 21:56:55 +02:00
|
|
|
// following the byte containing the start code identifier (or to
|
|
|
|
// the end of fragment->data).
|
2019-07-29 21:56:56 +02:00
|
|
|
if (start_code >> 8 == 0x000001) {
|
2017-05-05 00:03:03 +02:00
|
|
|
// Unit runs from start to the beginning of the start code
|
|
|
|
// pointed to by end (including any padding zeroes).
|
2019-07-29 21:56:55 +02:00
|
|
|
unit_size = (end - 4) - start;
|
2019-07-29 21:56:56 +02:00
|
|
|
} else {
|
|
|
|
// We didn't find a start code, so this is the final unit.
|
|
|
|
unit_size = end - start;
|
|
|
|
final = 1;
|
2017-05-05 00:03:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 21:56:55 +02:00
|
|
|
err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type, (uint8_t*)start,
|
|
|
|
unit_size, frag->data_ref);
|
2018-04-26 00:35:46 +02:00
|
|
|
if (err < 0)
|
2017-05-05 00:03:03 +02:00
|
|
|
return err;
|
|
|
|
|
2019-07-29 21:56:56 +02:00
|
|
|
if (final)
|
2017-05-05 00:03:03 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
|
|
|
|
CodedBitstreamUnit *unit)
|
|
|
|
{
|
|
|
|
GetBitContext gbc;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (MPEG2_START_IS_SLICE(unit->type)) {
|
|
|
|
MPEG2RawSlice *slice;
|
|
|
|
int pos, len;
|
|
|
|
|
2018-02-11 02:51:33 +02:00
|
|
|
err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
|
|
|
|
&cbs_mpeg2_free_slice);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
slice = unit->content;
|
|
|
|
|
2017-05-05 00:03:03 +02:00
|
|
|
err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
|
2018-02-11 02:51:33 +02:00
|
|
|
if (err < 0)
|
2017-05-05 00:03:03 +02:00
|
|
|
return err;
|
|
|
|
|
2019-12-10 00:26:04 +02:00
|
|
|
if (!get_bits_left(&gbc))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2017-05-05 00:03:03 +02:00
|
|
|
pos = get_bits_count(&gbc);
|
|
|
|
len = unit->data_size;
|
|
|
|
|
|
|
|
slice->data_size = len - pos / 8;
|
2018-04-28 00:08:42 +02:00
|
|
|
slice->data_ref = av_buffer_ref(unit->data_ref);
|
2018-02-11 02:51:33 +02:00
|
|
|
if (!slice->data_ref)
|
2017-05-05 00:03:03 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2018-04-28 00:08:42 +02:00
|
|
|
slice->data = unit->data + pos / 8;
|
2017-05-05 00:03:03 +02:00
|
|
|
|
|
|
|
slice->data_bit_start = pos % 8;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
switch (unit->type) {
|
2018-02-11 02:51:33 +02:00
|
|
|
#define START(start_code, type, read_func, free_func) \
|
2017-05-05 00:03:03 +02:00
|
|
|
case start_code: \
|
|
|
|
{ \
|
|
|
|
type *header; \
|
2018-02-11 02:51:33 +02:00
|
|
|
err = ff_cbs_alloc_unit_content(ctx, unit, \
|
|
|
|
sizeof(*header), free_func); \
|
|
|
|
if (err < 0) \
|
|
|
|
return err; \
|
|
|
|
header = unit->content; \
|
2018-02-22 00:22:54 +02:00
|
|
|
err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
|
2018-02-11 02:51:33 +02:00
|
|
|
if (err < 0) \
|
2017-05-05 00:03:03 +02:00
|
|
|
return err; \
|
|
|
|
} \
|
|
|
|
break;
|
2019-05-22 03:04:31 +02:00
|
|
|
START(MPEG2_START_PICTURE, MPEG2RawPictureHeader,
|
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>
2019-06-20 01:45:12 +02:00
|
|
|
picture_header, &cbs_mpeg2_free_picture_header);
|
2019-05-22 03:04:31 +02:00
|
|
|
START(MPEG2_START_USER_DATA, MPEG2RawUserData,
|
|
|
|
user_data, &cbs_mpeg2_free_user_data);
|
|
|
|
START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader,
|
|
|
|
sequence_header, NULL);
|
|
|
|
START(MPEG2_START_EXTENSION, MPEG2RawExtensionData,
|
|
|
|
extension_data, NULL);
|
|
|
|
START(MPEG2_START_GROUP, MPEG2RawGroupOfPicturesHeader,
|
|
|
|
group_of_pictures_header, NULL);
|
2019-07-29 21:56:54 +02:00
|
|
|
START(MPEG2_START_SEQUENCE_END, MPEG2RawSequenceEnd,
|
|
|
|
sequence_end, NULL);
|
2017-05-05 00:03:03 +02:00
|
|
|
#undef START
|
|
|
|
default:
|
2019-05-22 03:04:35 +02:00
|
|
|
return AVERROR(ENOSYS);
|
2017-05-05 00:03:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
|
|
|
|
CodedBitstreamUnit *unit,
|
|
|
|
PutBitContext *pbc)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
switch (unit->type) {
|
|
|
|
#define START(start_code, type, func) \
|
|
|
|
case start_code: \
|
|
|
|
err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
|
|
|
|
break;
|
2019-05-22 03:04:31 +02:00
|
|
|
START(MPEG2_START_PICTURE, MPEG2RawPictureHeader, picture_header);
|
|
|
|
START(MPEG2_START_USER_DATA, MPEG2RawUserData, user_data);
|
|
|
|
START(MPEG2_START_SEQUENCE_HEADER, MPEG2RawSequenceHeader, sequence_header);
|
|
|
|
START(MPEG2_START_EXTENSION, MPEG2RawExtensionData, extension_data);
|
|
|
|
START(MPEG2_START_GROUP, MPEG2RawGroupOfPicturesHeader,
|
|
|
|
group_of_pictures_header);
|
2019-07-29 21:56:54 +02:00
|
|
|
START(MPEG2_START_SEQUENCE_END, MPEG2RawSequenceEnd, sequence_end);
|
2017-05-05 00:03:03 +02:00
|
|
|
#undef START
|
|
|
|
default:
|
|
|
|
av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
|
2017-11-09 03:04:02 +02:00
|
|
|
"code %02"PRIx32".\n", unit->type);
|
2017-05-05 00:03:03 +02:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
|
|
|
|
CodedBitstreamUnit *unit,
|
|
|
|
PutBitContext *pbc)
|
|
|
|
{
|
|
|
|
MPEG2RawSlice *slice = unit->content;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (slice->data) {
|
2018-11-04 06:48:40 +02:00
|
|
|
size_t rest = slice->data_size - (slice->data_bit_start + 7) / 8;
|
|
|
|
uint8_t *pos = slice->data + slice->data_bit_start / 8;
|
|
|
|
|
|
|
|
av_assert0(slice->data_bit_start >= 0 &&
|
2019-11-17 09:34:36 +02:00
|
|
|
slice->data_size > slice->data_bit_start / 8);
|
2018-11-04 06:48:40 +02:00
|
|
|
|
2017-05-05 00:03:03 +02:00
|
|
|
if (slice->data_size * 8 + 8 > put_bits_left(pbc))
|
|
|
|
return AVERROR(ENOSPC);
|
|
|
|
|
2018-11-04 06:48:40 +02:00
|
|
|
// First copy the remaining bits of the first byte
|
|
|
|
if (slice->data_bit_start % 8)
|
|
|
|
put_bits(pbc, 8 - slice->data_bit_start % 8,
|
|
|
|
*pos++ & MAX_UINT_BITS(8 - slice->data_bit_start % 8));
|
|
|
|
|
|
|
|
if (put_bits_count(pbc) % 8 == 0) {
|
|
|
|
// If the writer is aligned at this point,
|
|
|
|
// memcpy can be used to improve performance.
|
|
|
|
// This is the normal case.
|
|
|
|
flush_put_bits(pbc);
|
|
|
|
memcpy(put_bits_ptr(pbc), pos, rest);
|
|
|
|
skip_put_bytes(pbc, rest);
|
|
|
|
} else {
|
|
|
|
// If not, we have to copy manually:
|
|
|
|
for (; rest > 3; rest -= 4, pos += 4)
|
|
|
|
put_bits32(pbc, AV_RB32(pos));
|
2017-05-05 00:03:03 +02:00
|
|
|
|
2018-11-04 06:48:40 +02:00
|
|
|
for (; rest; rest--, pos++)
|
|
|
|
put_bits(pbc, 8, *pos);
|
2017-05-05 00:03:03 +02:00
|
|
|
|
2018-11-04 06:48:40 +02:00
|
|
|
// Align with zeros
|
|
|
|
put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
|
|
|
|
}
|
2017-05-05 00:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
|
2019-11-17 09:34:35 +02:00
|
|
|
CodedBitstreamUnit *unit,
|
|
|
|
PutBitContext *pbc)
|
2017-05-05 00:03:03 +02:00
|
|
|
{
|
2019-05-22 03:04:31 +02:00
|
|
|
if (MPEG2_START_IS_SLICE(unit->type))
|
2019-11-17 09:34:35 +02:00
|
|
|
return cbs_mpeg2_write_slice (ctx, unit, pbc);
|
2017-05-05 00:03:03 +02:00
|
|
|
else
|
2019-11-17 09:34:35 +02:00
|
|
|
return cbs_mpeg2_write_header(ctx, unit, pbc);
|
2017-05-05 00:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
|
|
|
|
CodedBitstreamFragment *frag)
|
|
|
|
{
|
|
|
|
uint8_t *data;
|
2018-04-24 04:39:54 +02:00
|
|
|
size_t size, dp;
|
2017-05-05 00:03:03 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
for (i = 0; i < frag->nb_units; i++)
|
|
|
|
size += 3 + frag->units[i].data_size;
|
|
|
|
|
2018-03-05 16:04:59 +02:00
|
|
|
frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
|
2018-02-11 02:51:33 +02:00
|
|
|
if (!frag->data_ref)
|
2017-05-05 00:03:03 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2018-02-11 02:51:33 +02:00
|
|
|
data = frag->data_ref->data;
|
2017-05-05 00:03:03 +02:00
|
|
|
|
|
|
|
dp = 0;
|
|
|
|
for (i = 0; i < frag->nb_units; i++) {
|
|
|
|
CodedBitstreamUnit *unit = &frag->units[i];
|
|
|
|
|
|
|
|
data[dp++] = 0;
|
|
|
|
data[dp++] = 0;
|
|
|
|
data[dp++] = 1;
|
|
|
|
|
2018-04-24 04:39:54 +02:00
|
|
|
memcpy(data + dp, unit->data, unit->data_size);
|
|
|
|
dp += unit->data_size;
|
2017-05-05 00:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
av_assert0(dp == size);
|
|
|
|
|
2018-03-05 16:04:59 +02:00
|
|
|
memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2017-05-05 00:03:03 +02:00
|
|
|
frag->data = data;
|
|
|
|
frag->data_size = size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CodedBitstreamType ff_cbs_type_mpeg2 = {
|
|
|
|
.codec_id = AV_CODEC_ID_MPEG2VIDEO,
|
|
|
|
|
|
|
|
.priv_data_size = sizeof(CodedBitstreamMPEG2Context),
|
|
|
|
|
|
|
|
.split_fragment = &cbs_mpeg2_split_fragment,
|
|
|
|
.read_unit = &cbs_mpeg2_read_unit,
|
|
|
|
.write_unit = &cbs_mpeg2_write_unit,
|
|
|
|
.assemble_fragment = &cbs_mpeg2_assemble_fragment,
|
|
|
|
};
|