summaryrefslogtreecommitdiff
path: root/libavcodec/cbs.c
Commit message (Collapse)AuthorAgeFilesLines
* avcodec/cbs: Remove CBS_CONTENT_TYPE_PODAndreas Rheinhardt2022-10-221-14/+4
| | | | | | | It is equivalent to CBS_CONTENT_TYPE_INTERNAL_REFS with nb_offsets equal to zero. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/cbs: Only write extradata if there is something to writeAndreas Rheinhardt2022-09-251-0/+4
| | | | | | | | | It is e.g. legal for an ISOBMFF avcc to contain zero parameter sets. In this case the annex B that we produce would be empty and therefore useless. This happens e.g. with mov/frag_overlap.mp4 from the FATE-suite. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/cbs: Remove ff_cbs_alloc_unit_contentAndreas Rheinhardt2022-08-081-22/+2
| | | | | | | It is no longer used. Also rename ff_cbs_alloc_unit_content2 to ff_cbs_alloc_unit_content. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/cbs_internal: Use unions to shrink size of descriptorsAndreas Rheinhardt2022-08-081-13/+13
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/cbs: Use smaller scope for variables, add constAndreas Rheinhardt2022-08-081-10/+9
| | | | | | And also avoid an unnecessary indirection for src_buf. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/cbs: Remove redundant assignmentAndreas Rheinhardt2022-08-081-6/+0
| | | | | | | | | | | The code just creates new references without allocating new buffers for the subobjects; therefore the actual data pointer stays valid and need not be updated. Also remove an assert that ensured that the calculation for updating the pointer makes sense. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/cbs: Avoid code duplication for making unit refcounted/writableAndreas Rheinhardt2022-08-081-49/+25
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/cbs: Mark init and close functions as av_coldAndreas Rheinhardt2022-07-091-5/+5
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/cbs: Make ff_cbs_insert_unit_data() always append the new unitAndreas Rheinhardt2022-02-081-7/+15
| | | | | | | All split functions (the only users of this function) only append units. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/cbs: Make ff_cbs_alloc_unit_data() staticAndreas Rheinhardt2021-09-081-18/+23
| | | | | | Forgotten in 7c92eaace2b338e0b3acc18e1543b365610578fd. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/cbs: add a helper to read extradata within packet side dataJames Almer2021-08-141-0/+13
| | | | | | | | Using ff_cbs_read() on the raw buffer will not parse it as extradata, resulting in parsing errors for example when handling ISOBMFF avcC. This helper works around that. Signed-off-by: James Almer <jamrial@gmail.com>
* lavc/cbs: avoid avcodec.h dependencyAnton Khirnov2021-06-101-0/+1
| | | | Include only the headers that are actually needed.
* Avoid intermediate bitcount for number of bytes in PutBitContextAndreas Rheinhardt2021-03-301-1/+1
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avcodec/cbs: Remove redundant checks for CodedBitstreamContext.codecAndreas Rheinhardt2021-03-121-3/+3
| | | | | | | | | | | | | Setting this field happens immediately after the allocation in ff_cbs_init(), so the whole CBS code may presume that any CodedBitstreamContext has this set. Lots of code already presumed this, yet ff_cbs_close() did it inconsistently: It checked before checking whether the CodedBitstreamType has a close function; yet it simply unconditionally read ctx->codec->priv_class. Coverity complained about this in issue #1473564, which this commit fixes. Reviewed-by: Mark Thompson <sw@jkqxz.net> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
* avcodec/cbs: allow cbs_read_fragment_content() to skip decomposition of unitsJames Almer2021-01-041-0/+6
| | | | | | | | | | 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-041-0/+9
| | | | | | 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-011-1/+1
| | | | | 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-241-38/+39
| | | | | 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-291-0/+6
| | | | | | 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-021-0/+163
| | | | | | | | | | | | 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-021-0/+69
| | | | | | | | | | | | | | | | 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-071-29/+19
| | | | | | | | 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-131-2/+2
| | | | | | | | | | 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-091-1/+4
| | | | | | | | | | | | | | | | | | | | | | 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-171-1/+3
| | | | | | | | | | | | | 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-171-5/+59
| | | | | | | | | | | | | | | | | 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-291-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-081-7/+5
| | | | | | | | | | 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-071-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | 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-161-0/+79
| | | | | 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-251-26/+36
| | | | | | | | | | | | | | | | | 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-261-0/+6
|
* lavc/cbs: Add JPEG supportMark Thompson2018-09-231-0/+6
|
* lavc: Add coded bitstream read/write support for VP9Mark Thompson2018-05-021-0/+6
|
* cbs: Add support for array subscripts in trace outputMark Thompson2018-05-021-7/+37
| | | | This makes the trace output for arrays significantly nicer.
* cbs: Fragment/unit data is always reference countedMark Thompson2018-05-011-7/+12
| | | | | 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-261-21/+20
| | | | | | | | | 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-181-0/+13
| | | | 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-051-4/+8
| | | | | | | | | | | | 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-211-29/+136
|\ | | | | | | | | | | | | | | | | * 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-201-29/+136
| | | | | | | | | | | | | | | | 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-211-1/+1
|\ \ | |/ | | | | | | | | | | * commit 'a3daecd6375279d9fdb863ac9db3545a33e97651': cbs: Demote the "decomposition unimplemented" warning Merged-by: Mark Thompson <sw@jkqxz.net>
| * cbs: Demote the "decomposition unimplemented" warningMark Thompson2018-02-201-1/+1
| | | | | | | | | | | | 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-201-5/+7
| | | | | | | | Also fix conversion specifiers used for the unit type.
| * cbs: Allocate the context inside the init functionMark Thompson2018-02-201-3/+17
| | | | | | | | | | ... 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-121-1/+1
| |
| * cbs: Always check for bitstream end before readingMark Thompson2017-09-121-0/+6
| |
| * lavc: Add coded bitstream read/write support for MPEG-2Mark Thompson2017-08-201-0/+3
| | | | | | | | Also enable MPEG-2 support in the trace_headers filter.
| * lavc: Add coded bitstream read/write support for H.265Mark Thompson2017-08-121-0/+3
| |
| * lavc: Add coded bitstream read/write support for H.264Mark Thompson2017-08-121-0/+3
| |