summaryrefslogtreecommitdiff
path: root/libavcodec/cbs.c
Commit message (Collapse)AuthorAge
* avcodec/cbs: allow cbs_read_fragment_content() to skip decomposition of unitsJames Almer2021-01-04
| | | | | | | | | | The caller may not need all units in a fragment in reading only scenarios. They could in fact alter global state stored in the private CodedBitstreamType fields in an undesirable way. With this change, unit decomposition can be skipped based on parsed values within the unit. Signed-off-by: James Almer <jamrial@gmail.com>
* avcodec/cbs: add an AVClass to CodedBitstreamType for option handlingJames Almer2021-01-04
| | | | | | So unit parsing may be configured with caller set options. Signed-off-by: James Almer <jamrial@gmail.com>
* Mark some pointers as constAndreas Rheinhardt2021-01-01
| | | | | Reviewed-by: Lynne <dev@lynne.ee> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* cbs: Add function to read extradata from an AVCodecContextMark Thompson2020-11-24
| | | | | This is useful in decoders and parsers, matching the way that bitstream filters read extradata from AVCodecParameters.
* avcodec/cbs: add a flush callback to CodedBitstreamTypeJames Almer2020-09-29
| | | | | | Used to reset the codec's private internal state. Signed-off-by: James Almer <jamrial@gmail.com>
* cbs: Add support functions for handling unit content referencesMark Thompson2020-09-02
| | | | | | | | | | | | Use the unit type table to determine what we need to do to clone the internals of the unit content when making copies for refcounting or writeability. (This will still fail for units with complex content if they do not have a defined clone function.) Setup and naming from a patch by Andreas Rheinhardt <andreas.rheinhardt@gmail.com>, but with the implementation changed to use the unit type information if possible rather than requiring a codec-specific function.
* cbs: Describe allocate/free methods in tabular formMark Thompson2020-09-02
| | | | | | | | | | | | | | | | Unit types are split into three categories, depending on how their content is managed: * POD structure - these require no special treatment. * Structure containing references to refcounted buffers - these can use a common free function when the offsets of all the internal references are known. * More complex structures - these still require ad-hoc treatment. For each codec we can then maintain a table of descriptors for each set of equivalent unit types, defining the mechanism needed to allocate/free that unit content. This is not required to be used immediately - a new alloc function supports this, but does not replace the old one which works without referring to these tables.
* avcodec/cbs: Remove unused function parametersAndreas Rheinhardt2020-07-07
| | | | | | | | Several cbs-functions had an unused CodedBitstreamContext parameter. This commit removes these. Reviewed-by: Mark Thompson <sw@jkqxz.net> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avcodec/cbs: Allocate more CodedBitstreamUnit at once in cbs_insert_unit()Michael Niedermayer2020-06-13
| | | | | | | | | | Fixes: Timeout (85sec -> 0.5sec) Fixes: 20791/clusterfuzz-testcase-minimized-ffmpeg_BSF_AV1_FRAME_SPLIT_fuzzer-5659537719951360 Fixes: 21214/clusterfuzz-testcase-minimized-ffmpeg_BSF_MPEG2_METADATA_fuzzer-5165560875974656 Fixes: 21247/clusterfuzz-testcase-minimized-ffmpeg_BSF_H264_METADATA_fuzzer-5715175257931776 Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
* avcodec/cbs: Fix potential double-free when adding unit failsAndreas Rheinhardt2020-02-09
| | | | | | | | | | | | | | | | | | | | | | ff_cbs_insert_unit_data() has two modes of operation: It can insert a unit with a newly created reference to an already existing AVBuffer; or it can take a buffer and create an AVBuffer for it. Said buffer will then become owned by the unit lateron. A potential memleak/double-free exists in the second case, because if creating the AVBuffer fails, the function immediately returns, but when it fails lateron, the supplied buffer will be freed. The caller has no way to distinguish between these two outcomes. The only such caller (cbs_jpeg_split_fragment() in cbs_jpeg.c) opted for a potential double-free. This commit changes this by explicitly stating that a non-refcounted buffer will be freed on error. The aforementioned caller has been brought in line with this. Fixes CID 1452623. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avcodec/cbs: Fix potential overflowAndreas Rheinhardt2019-11-17
| | | | | | | | | | | | | 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>
* avcodec/cbs: Factor out common code for writing unitsAndreas Rheinhardt2019-11-17
| | | | | | | | | | | | | | | | | 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>
* cbs: Don't set AVBuffer's opaqueAndreas Rheinhardt2019-07-29
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* cbs: ff_cbs_delete_unit: Replace return value with assertAndreas Rheinhardt2019-07-08
| | | | | | | | | | 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>
* cbs: Allow non-blank packets in ff_cbs_write_packetAndreas Rheinhardt2019-07-07
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Up until now, ff_cbs_write_packet always initialized the packet structure it received without documenting this behaviour; furthermore, the packet's buffer would (on success) be overwritten with the new buffer without unreferencing the old. This meant that the input packet had to be either clean (otherwise there would be memleaks) in which case the initialization is redundant or uninitialized. ff_cbs_write_packet was never used with uninitialized packets, so the initialization was redundant. Worse yet, it forced callers to use more than one packet and made it difficult to add side-data to a packet designated for output, because said side-data could only be attached after the call to ff_cbs_write_packet. This has been changed. It is now allowed to use a non-blank packet. The currently existing buffer will be unreferenced and replaced by the new one, as will be the accompanying fields (i.e. data and size). The rest isn't touched at all. This change will enable us to use only one packet in the bitstream filters that rely on CBS. This commit also updates the documentation of ff_cbs_write_extradata and ff_cbs_write_packet (to better describe existing behaviour and in the latter case to also describe the new behaviour). Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avcodec/cbs: add helper functions and macros to read and write signed valuesJames Almer2019-04-16
| | | | | Reviewed-by: Mark Thompson <sw@jkqxz.net> Signed-off-by: James Almer <jamrial@gmail.com>
* libavcodec/cbs: Stop needlessly reallocating the units arrayAndreas Rheinhardt2019-02-25
| | | | | | | | | | | | | | | | | Currently, a fragment's unit array is constantly reallocated during splitting of a packet. This commit changes this: One can keep the units array by distinguishing between the number of allocated and the number of valid units in the units array. The more units a packet is split into, the bigger the benefit. So MPEG-2 benefits the most; for a video coming from an NTSC-DVD (usually 32 units per frame) the average cost of cbs_insert_unit (for a single unit) went down from 6717 decicycles to 450 decicycles (based upon 10 runs with 4194304 runs each); if each packet consists of only one unit, it went down from 2425 to 448; for a H.264 video where most packets contain nine units, it went from 4431 to 450. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@googlemail.com>
* lavc: Add coded bitstream read/write support for AV1Mark Thompson2018-09-26
|
* lavc/cbs: Add JPEG supportMark Thompson2018-09-23
|
* lavc: Add coded bitstream read/write support for VP9Mark Thompson2018-05-02
|
* cbs: Add support for array subscripts in trace outputMark Thompson2018-05-02
| | | | This makes the trace output for arrays significantly nicer.
* cbs: Fragment/unit data is always reference countedMark Thompson2018-05-01
| | | | | Make this clear in the documentation and add some asserts to ensure that it is always true.
* avcodec/cbs: create reference counted fragments in ff_cbs_read_extradata()James Almer2018-04-26
| | | | | | | | | This way, every CodedBitstreamType->split_fragment() function can safely assume the fragment passed to them will be reference counted, potentially simplifying code. Reviewed-by: Mark Thompson <sw@jkqxz.net> Signed-off-by: James Almer <jamrial@gmail.com>
* cbs: Add a table of all supported codec IDsMark Thompson2018-03-18
| | | | Use it as the set of codec IDs supported by the trace_headers BSF.
* avcodec/cbs: use a reference to the assembled CodedBitstreamFragment buffer ↵James Almer2018-03-05
| | | | | | | | | | | | 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>
* Merge commit 'ce5870a3a8f2b10668ee4f04c2ae0287f66f31b2'Mark Thompson2018-02-21
|\ | | | | | | | | | | | | | | | | * commit 'ce5870a3a8f2b10668ee4f04c2ae0287f66f31b2': cbs: Refcount all the things! Some changes for bitstream API. Merged-by: Mark Thompson <sw@jkqxz.net>
| * cbs: Refcount all the things!Mark Thompson2018-02-20
| | | | | | | | | | | | | | | | 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).
* | Merge commit 'a3daecd6375279d9fdb863ac9db3545a33e97651'Mark Thompson2018-02-21
|\| | | | | | | | | | | | | * commit 'a3daecd6375279d9fdb863ac9db3545a33e97651': cbs: Demote the "decomposition unimplemented" warning Merged-by: Mark Thompson <sw@jkqxz.net>
| * cbs: Demote the "decomposition unimplemented" warningMark Thompson2018-02-20
| | | | | | | | | | | | This is harmless and should not be a warning - unknown units are passed through to the write functions unchanged, and no other code will interact with them.
| * cbs: Add an explicit type for coded bitstream unit typesMark Thompson2018-02-20
| | | | | | | | Also fix conversion specifiers used for the unit type.
| * cbs: Allocate the context inside the init functionMark Thompson2018-02-20
| | | | | | | | | | ... instead of making callers allocate it themselves. This is more consistent with other APIs in libav.
| * cbs: Add a missing return value checkMark Thompson2017-11-12
| |
| * cbs: Always check for bitstream end before readingMark Thompson2017-09-12
| |
| * lavc: Add coded bitstream read/write support for MPEG-2Mark Thompson2017-08-20
| | | | | | | | Also enable MPEG-2 support in the trace_headers filter.
| * lavc: Add coded bitstream read/write support for H.265Mark Thompson2017-08-12
| |
| * lavc: Add coded bitstream read/write support for H.264Mark Thompson2017-08-12
| |
| * lavc: Add coded bitstream read/write APIMark Thompson2017-08-12
|
* lavc: Add coded bitstream read/write support for MPEG-2Mark Thompson2017-10-17
| | | | | (cherry picked from commit 2bc9ba8d3c41f3a8e56484bd67b05040c7909a01) (cherry picked from commit a41b69b5eb950c10d8ede472bcc4e88ce4246db9)
* lavc: Add coded bitstream read/write support for H.265Mark Thompson2017-10-17
| | | | | | (cherry picked from commit 867381b8b51fa21fa2b8f071f508f3d39cc9c1f0) (cherry picked from commit f763489364416bb6866adc4f4a96012dd2ca1bd0) (cherry picked from commit 067a9ddeb8feff1f724856f0054930c55219f76b)
* lavc: Add coded bitstream read/write support for H.264Mark Thompson2017-10-17
| | | | | | (cherry picked from commit acf06f45441be24c5cbae0920579cd69427326a1) (cherry picked from commit 768eb9182e94a94bc2ef46f565a0dac7afef3b57) (cherry picked from commit e7f64191b27bcf37cbf7006606f0f439c6cdc24f)
* lavc: Add coded bitstream read/write APIMark Thompson2017-10-17
(cherry picked from commit 18f1706f331bf5dd565774eae680508c8d3a97ad) (cherry picked from commit 44cde38c8acbef7d5250e6d1b52b1020871e093b)