Commit Graph

58 Commits

Author SHA1 Message Date
Andriy Gelman
02a83e26de lavc/cbs_h2645: Fix incorrect max size of nalu unit
In the worst case the startcode prefix has 4 bytes.

This fixes a trigerred assertion:
Assertion dp <= max_size failed at libavcodec/cbs_h2645.c:1451

Found-by:libFuzzer
Reviewed-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Signed-off-by: Andriy Gelman <andriy.gelman@gmail.com>
2019-12-06 15:05:56 -03:00
Andreas Rheinhardt
cda3e8ca04 avcodec/cbs: Fix potential overflow
The number of bits in a PutBitContext must fit into an int, yet nothing
guaranteed the size argument cbs_write_unit_data() uses in init_put_bits()
to be in the range 0..INT_MAX / 8. This has been changed.

Furthermore, the check 8 * data_size > data_bit_start that there is
data beyond the initial padding when writing mpeg2 or H.264/5 slices
could also overflow, so divide it by 8 to get an equivalent check
without this problem.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-11-17 23:31:45 +00:00
Andreas Rheinhardt
7c92eaace2 avcodec/cbs: Factor out common code for writing units
All cbs-functions to write units share a common pattern:
1. They check whether they have a write buffer (that is used to store
the unit's data until the needed size becomes known after writing the
unit when a dedicated buffer will be allocated).
2. They use this buffer for a PutBitContext.
3. The (codec-specific) writing takes place through the PutBitContext.
4. The return value is checked. AVERROR(ENOSPC) here always indicates
that the buffer was too small and leads to a reallocation of said
buffer.
5. The final buffer will be allocated and the data copied.

This commit factors this common code out in a single function in cbs.c.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-11-17 23:31:44 +00:00
Andreas Rheinhardt
f3333c3c67 avcodec/cbs_h2645: Fix potential out-of-bounds array access
The maximum allowed index for an array access is FF_ARRAY_ELEMS - 1; yet
the current code allowed FF_ARRAY_ELEMS. This wasn't dangerous in practice,
as parameter sets with invalid ids were already filtered out during
reading.

Found via PVS-Studio (see ticket #8156).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-09-29 17:48:38 +01:00
Andreas Rheinhardt
f83ac5fd79 avcodec/cbs_h264: Automatically free SEI payload on error
If adding an SEI message to an access unit fails, said SEI message was
not touched, so that the caller had to free any data associated with it
that might need to be freed. But given that ff_cbs_h264_add_sei_message
can simply call cbs_h264_free_sei_payload, one can easily free
the content of the SEI payload.

This fixes a memleak when inserting a user data unregistered string for
h264_metadata fails.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-09-24 15:54:31 +01:00
Thierry Foucu
a80fdbcf13 lavc/cbs_h2645: Use av_realloc instead of av_malloc
Follow the description of av_realloc, the memory needs to be allocated
by av_realloc.
2019-08-28 21:57:48 +02:00
Andreas Rheinhardt
ae49993ce6 cbs_h264: Improve adding SEI messages
Up until now, if an SEI messages was to be added to a fragment, it was
tried to add said SEI message to the first SEI NAL unit of the fragment
and if this SEI NAL unit already contained H264_NAL_SEI SEI messages (an
arbitrary limit imposed by cbs_h264), adding failed; if there was no SEI
NAL unit, a new one has been added.
With this commit, the fragment is searched for further NAL units to add
the SEI messages to. If all of them are full, a new SEI NAL unit is added.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-07-29 22:25:10 +01:00
Andreas Rheinhardt
4e7e30bbe0 cbs: Don't set AVBuffer's opaque
cbs is currently inconsistent regarding the opaque field that can be
used as a special argument to av_buffer_create in order to be used
during freeing the buffer: ff_cbs_alloc_unit_content and all the free
functions used name this parameter as if it should contain a pointer to
the unit whose content is about to be created; but both
ff_cbs_alloc_unit_content as well as ff_cbs_h264_add_sei_message
actually use a pointer to the CodedBitstreamContext as opaque. It should
actually be neither, because it is unneeded (as is evidenced by the fact
that none of the free functions use this pointer at all) and because it
ties the unit's content to the lifetime of other objects, although a
refcounted buffer is supposed to have its own lifetime that only ends
when its reference count reaches zero. This problem manifests itself in
the pointer becoming dangling.
The pointer to the unit can become dangling if another unit is added to
the fragment later as happens in the bitstream filters; in this case,
the pointer can point to the wrong unit (if the fragment's unit array
needn't be relocated) or it can point to where the array was earlier.
It can also become dangling if the unit's content is meant to survive
the resetting of the fragment it was originally read with. This applies
to the extradata of H.264 and HEVC.
The pointer to the context can become dangling if the context is closed
before the content is freed. Although this doesn't seem to happen right
now, it could happen, in particular if one uses different
CodedBitstreamContexts for in- and output.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-07-29 22:25:10 +01:00
Andreas Rheinhardt
b71a0367a6 cbs: Remove useless initializations
Up until now, a temporary variable was used and initialized every time a
value was read in CBS; if reading turned out to be successfull, this
value was overwritten (without having ever been looked at) with the
value read if reading was successfull; on failure the variable wasn't
touched either. Therefore these initializations can be and have been
removed.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-07-27 22:00:43 +01:00
James Almer
e460dcc832 avcodec/cbs_h265: add support for Alpha Channel Info SEI messages
As defined in sections F.14.2.8 and F.14.3.8

Reviewed-by: Mark Thompson <sw@jkqxz.net>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-07-20 11:28:09 -03:00
Andreas Rheinhardt
d4035ca849 cbs_h2645: Fix infinite loop in more_rbsp_data
cbs_h2645_read_more_rbsp_data does not handle malformed input very well:
1. If there were <= 8 bits left in the bitreader, these bits were read
via show_bits. But show_bits requires the number of bits to be read to
be > 0 (internally it shifts by 32 - number of bits to be read which is
undefined behaviour if said number is zero; there is also an assert for
this, but it is only an av_assert2). Furthermore, in this case a shift
by -1 was performed which is of course undefined behaviour, too.
2. If there were > 0 and <= 8 bits left and all of them were zero
(this can only happen for defective input), it was reported that there
was further RBSP data.

This can lead to an infinite loop in H.265's cbs_h265_read_extension_data
corresponding to the [vsp]ps_extension_data_flag syntax elements. If the
relevant flag indicates the (potential) occurence of these syntax elements,
while all bits after this flag are zero, cbs_h2645_read_more_rbsp_data
always returns 1 on x86. Given that a checked bitstream reader is used,
we are also not "saved" by an overflow in the bitstream reader's index.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-07-20 12:48:18 +01:00
Andreas Rheinhardt
f83b46e218 configure, cbs_h2645: Remove unneeded golomb dependency
This has been forgotten in 44cde38c.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-07-08 22:59:41 +01:00
Andreas Rheinhardt
d9418aba66 cbs_h264, h264_metadata: Deleting SEI messages never fails
Given the recent changes to ff_cbs_delete_unit, it is no longer sensible
to use a return value for ff_cbs_h264_delete_sei_message; instead, use
asserts to ensure that the required conditions are met and remove the
callers' checks for the return value. Also, document said conditions.

An assert that is essentially equivalent to the one used in
ff_cbs_delete_unit has been removed, too.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-07-08 22:59:41 +01:00
Andreas Rheinhardt
730e5be3aa cbs: ff_cbs_delete_unit: Replace return value with assert
ff_cbs_delete_unit never fails if the index of the unit to delete is
valid, as it is with all current callers of the function. So just assert
in ff_cbs_delete_unit that the index is valid and change the return
value to void in order to remove the callers' checks for whether
ff_cbs_delete_unit failed.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
2019-07-08 22:59:41 +01:00
James Almer
ff2a638c83 avcodec/cbs_h264: add support for Alternative Transfer Characteristics SEI message
Reviewed-by: Mark Thompson <sw@jkqxz.net>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-06-03 19:21:38 -03:00
James Almer
16c50abb50 avcodec/cbs_h2645: add macros to read and write fields with no custom range of values
Reviewed-by: Mark Thompson <sw@jkqxz.net>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-04-28 20:28:42 -03:00
James Almer
3dc6adf326 avcodec/cbs_h2645: add helper macros for signed values
Reviewed-by: Mark Thompson <sw@jkqxz.net>
Signed-off-by: James Almer <jamrial@gmail.com>
2019-04-16 20:32:12 -03:00
Andreas Rheinhardt
8ca55a2b9e cbs_h2645: Avoid memcpy when splitting fragment #2
Now memcpy can be avoided for NAL units containing escapes, too.

Particularly improves performance for files with hardcoded black bars.
For such a file, time spent in cbs_h2645_split_fragment went down from
369410 decicycles to 327677 decicycles. (It were 379114 decicycles when
every NAL unit was copied.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@googlemail.com>
2019-01-23 22:33:45 +00:00
Andreas Rheinhardt
992532ee31 h2645_parse: Make ff_h2645_packet_split reference-compatible
This is in preparation for a patch for cbs_h2645. Now the packet's
rbsp_buffer can be owned by an AVBuffer.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@googlemail.com>
2019-01-23 22:33:43 +00:00
Andreas Rheinhardt
e895b800fe cbs_h2645: Avoid memcpy when splitting fragment
Now memcpy is avoided for NAL units that don't contain 0x03 escape
characters.

Improves performance of cbs_h2645_fragment_add_nals from 36940
decicycles to 6364 decicycles based on 8 runs with a 5.1 Mb/s H.264
sample (262144 runs each).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@googlemail.com>
Signed-off-by: Mark Thompson <sw@jkqxz.net>
2018-11-22 23:19:17 +00:00
Mark Thompson
7a20656474 cbs_h265: Add a lot more SEI parsing support
Supports both prefix and suffix SEI, decoding all of the common SEI
types and some more obscure ones.  Most of this is tested by the
existing tests in fate.
2018-11-18 17:33:26 +00:00
Andreas Rheinhardt
ee47ac97d7 cbs_h2645: Improve performance of writing slices
Instead of using a combination of bitreader and -writer for copying data,
one can byte-align the (obsolete and removed) bitreader to improve performance.
With the right alignment one can even use memcpy. The right alignment
normally exists for CABAC and hence for H.265 in general.
For aligned data this reduced the time to copy the slicedata from
776520 decicycles to 33889 with 262144 runs and a 6.5mb/s H.264 video.
For unaligned data the number went down from 279196 to 97739 decicycles.

Signed-off-by: Mark Thompson <sw@jkqxz.net>
2018-11-12 00:12:06 +00:00
Mark Thompson
da6db843b8 cbs_h2645: Allocate all internal buffers with padding
Any of these buffers (for both H.264 and H.265) might reasonably be
parsed using the bitstream reader, so include padding on all of them.
2018-10-22 23:01:32 +01:00
Mark Thompson
3143fe34f6 cbs_h264: Actually decompose end-of-sequence NAL units
64c50c0e97 declared support for decomposing
them but omitted to implement it; this adds an implementation.

Also do the same for end-of-stream NAL units, since they are equivalent.
2018-10-22 23:01:29 +01:00
Aman Gupta
64c50c0e97 avcodec/cbs_h264: silence errors about end_of_seq nalus
[ffmpeg] AVBSFContext: Decomposition unimplemented for unit 4 (type 10).

Signed-off-by: Aman Gupta <aman@tmm1.net>
2018-10-15 11:52:49 -07:00
Mark Thompson
ac687add84 cbs_h264: Add support for mastering display SEI messages 2018-05-10 23:54:01 +01:00
Mark Thompson
d94dda742c cbs_h264: Add support for pan-scan rectangle SEI messages 2018-05-10 23:54:01 +01:00
Haihao Xiang
2943dd35b7 cbs_h265: read/write content light level information SEI message
Signed-off-by: Haihao Xiang <haihao.xiang@intel.com>
2018-05-10 20:55:42 +01:00
Haihao Xiang
56ed011692 cbs_h265: read/write HEVC PREFIX SEI
Similar to H264, cbs_h265_{read, write}_nal_unit() can handle HEVC
prefix SEI NAL units. Currently mastering display colour volume SEI
message is added only, we may add more SEI message if needed later

Signed-off-by: Haihao Xiang <haihao.xiang@intel.com>
2018-05-10 20:36:45 +01:00
James Almer
c6a63e1109 avcodec/cbs_h2645: use AVBufferRef to store list of active parameter sets
Removes unnecessary data copies, and partially fixes potential issues
with dangling references held in said lists.

Reviewed-by: Mark Thompson <sw@jkqxz.net>
Signed-off-by: James Almer <jamrial@gmail.com>
2018-05-09 11:14:14 -03:00
Mark Thompson
300ef25314 cbs: Add support for array subscripts in trace output
This makes the trace output for arrays significantly nicer.
2018-05-02 01:20:45 +01:00
Mark Thompson
315cc8c098 cbs_h2645: Simplify representation of fixed values 2018-05-01 23:31:41 +01:00
James Almer
0807a77160 avcodec/cbs_h2645: create a reference to the existing buffer when decomposing slice units
Reviewed-by: Mark Thompson <sw@jkqxz.net>
Signed-off-by: James Almer <jamrial@gmail.com>
2018-04-30 20:30:02 -03:00
James Almer
df3a2ff767 avcodec/cbs: use a reference to the assembled CodedBitstreamFragment buffer when writing packets
This saves one malloc + memcpy per packet

The CodedBitstreamFragment buffer is padded to follow the requirements
of AVPacket.

Reviewed-by: jkqxz
Signed-off-by: James Almer <jamrial@gmail.com>
2018-03-05 11:44:51 -03:00
Mark Thompson
fbeac5356c Merge commit 'eccc03c8fbc603a0a3257df66f0705f74fe2581a'
* commit 'eccc03c8fbc603a0a3257df66f0705f74fe2581a':
  cbs_h264: Add support for filler NAL units

Some bitstream -> get_bits.

Merged-by: Mark Thompson <sw@jkqxz.net>
2018-02-21 22:51:46 +00:00
Mark Thompson
ab6edb173b Merge commit '7157d959264f3729da463725c6faa580d9394d19'
* commit '7157d959264f3729da463725c6faa580d9394d19':
  cbs_h264: Move slice_group_id array out of PPS structure

Merged-by: Mark Thompson <sw@jkqxz.net>
2018-02-21 22:41:00 +00:00
Mark Thompson
77eba7bd99 Merge commit 'a2ca8ed903b435446031a8a0792ca535e6ee2913'
* commit 'a2ca8ed903b435446031a8a0792ca535e6ee2913':
  cbs_h264: Add utility functions to insert/delete SEI messages

Merged-by: Mark Thompson <sw@jkqxz.net>
2018-02-21 22:27:07 +00:00
Mark Thompson
0cc8e34a94 Merge commit 'ce5870a3a8f2b10668ee4f04c2ae0287f66f31b2'
* commit 'ce5870a3a8f2b10668ee4f04c2ae0287f66f31b2':
  cbs: Refcount all the things!

Some changes for bitstream API.

Merged-by: Mark Thompson <sw@jkqxz.net>
2018-02-21 22:22:54 +00:00
Mark Thompson
af3727e239 Merge commit '0e4c166cdd6446522a085dd9731967d09ac71f72'
* commit '0e4c166cdd6446522a085dd9731967d09ac71f72':
  cbs_h2645: Remove active ps references when it is replaced

Merged-by: Mark Thompson <sw@jkqxz.net>
2018-02-21 22:03:15 +00:00
Mark Thompson
eccc03c8fb cbs_h264: Add support for filler NAL units 2018-02-20 22:04:12 +00:00
Mark Thompson
7157d95926 cbs_h264: Move slice_group_id array out of PPS structure
It's very large, and is only used in some FMO streams.
2018-02-20 22:04:12 +00:00
Mark Thompson
a2ca8ed903 cbs_h264: Add utility functions to insert/delete SEI messages 2018-02-20 22:04:12 +00:00
Mark Thompson
ce5870a3a8 cbs: Refcount all the things!
This makes it easier for users of the CBS API to get alloc/free right -
all subelements use the buffer API so that it's clear how to free them.
It also allows eliding some redundant copies: the packet -> fragment copy
disappears after this change if the input packet is refcounted, and more
codec-specific cases are now possible (but not included in this patch).
2018-02-20 22:04:12 +00:00
Mark Thompson
0e4c166cdd cbs_h2645: Remove active ps references when it is replaced 2018-02-20 22:04:12 +00:00
Mark Thompson
1d12a545ce cbs: Add an explicit type for coded bitstream unit types
Also fix conversion specifiers used for the unit type.
2018-02-20 22:04:12 +00:00
Carl Eugen Hoyos
9b79c65ec0 lavu/lavc/lavf/lavfi: Do not use type modifier %zu on Windows MSVCRT. 2017-12-17 18:05:42 +01:00
Mark Thompson
7bf3f38046 cbs: Add padding to slice data allocations
These may be read by the bitstream reader, so they should include the
necessary padding for overreads.
2017-12-02 15:21:30 +00:00
Mark Thompson
22aed77687 cbs_h2645: Fix memory leak on when reading SEI fails 2017-11-12 16:00:41 +00:00
Mark Thompson
41272e112b cbs_h264: Fix memory leak in error case
Fixes CID 1419834.
2017-10-18 20:05:11 +01:00
Mark Thompson
624d4739db cbs_h264: Fix writing streams with auxiliary pictures
Tested with the alphaconformanceG sample.

Fixes CID 1419836.

(cherry picked from commit 9ed18f302b)
2017-10-18 20:05:11 +01:00