tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

cbs.h (17308B)


      1 /*
      2 * This file is part of FFmpeg.
      3 *
      4 * FFmpeg is free software; you can redistribute it and/or
      5 * modify it under the terms of the GNU Lesser General Public
      6 * License as published by the Free Software Foundation; either
      7 * version 2.1 of the License, or (at your option) any later version.
      8 *
      9 * FFmpeg is distributed in the hope that it will be useful,
     10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12 * Lesser General Public License for more details.
     13 *
     14 * You should have received a copy of the GNU Lesser General Public
     15 * License along with FFmpeg; if not, write to the Free Software
     16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     17 */
     18 
     19 #ifndef AVCODEC_CBS_H
     20 #define AVCODEC_CBS_H
     21 
     22 #include <stddef.h>
     23 #include <stdint.h>
     24 
     25 #include "libavutil/buffer.h"
     26 
     27 #include "codec_id.h"
     28 #include "codec_par.h"
     29 #include "defs.h"
     30 #include "packet.h"
     31 
     32 
     33 /*
     34 * This defines a framework for converting between a coded bitstream
     35 * and structures defining all individual syntax elements found in
     36 * such a stream.
     37 *
     38 * Conversion in both directions is possible.  Given a coded bitstream
     39 * (any meaningful fragment), it can be parsed and decomposed into
     40 * syntax elements stored in a set of codec-specific structures.
     41 * Similarly, given a set of those same codec-specific structures the
     42 * syntax elements can be serialised and combined to create a coded
     43 * bitstream.
     44 */
     45 
     46 struct AVCodecContext;
     47 struct CodedBitstreamType;
     48 
     49 /**
     50 * The codec-specific type of a bitstream unit.
     51 *
     52 * AV1: obu_type
     53 * H.264 / AVC: nal_unit_type
     54 * H.265 / HEVC: nal_unit_type
     55 * JPEG: marker value (without 0xff prefix)
     56 * MPEG-2: start code value (without prefix)
     57 * VP9: unused, set to zero (every unit is a frame)
     58 */
     59 typedef uint32_t CodedBitstreamUnitType;
     60 
     61 /**
     62 * Coded bitstream unit structure.
     63 *
     64 * A bitstream unit the smallest element of a bitstream which
     65 * is meaningful on its own.  For example, an H.264 NAL unit.
     66 *
     67 * See the codec-specific header for the meaning of this for any
     68 * particular codec.
     69 */
     70 typedef struct CodedBitstreamUnit {
     71    /**
     72     * Codec-specific type of this unit.
     73     */
     74    CodedBitstreamUnitType type;
     75 
     76    /**
     77     * Pointer to the directly-parsable bitstream form of this unit.
     78     *
     79     * May be NULL if the unit currently only exists in decomposed form.
     80     */
     81    uint8_t *data;
     82    /**
     83     * The number of bytes in the bitstream (including any padding bits
     84     * in the final byte).
     85     */
     86    size_t   data_size;
     87    /**
     88     * The number of bits which should be ignored in the final byte.
     89     *
     90     * This supports non-byte-aligned bitstreams.
     91     */
     92    size_t   data_bit_padding;
     93    /**
     94     * A reference to the buffer containing data.
     95     *
     96     * Must be set if data is not NULL.
     97     */
     98    AVBufferRef *data_ref;
     99 
    100    /**
    101     * Pointer to the decomposed form of this unit.
    102     *
    103     * The type of this structure depends on both the codec and the
    104     * type of this unit.  May be NULL if the unit only exists in
    105     * bitstream form.
    106     */
    107    void *content;
    108    /**
    109     * If content is reference counted, a RefStruct reference backing content.
    110     * NULL if content is not reference counted.
    111     */
    112    void *content_ref;
    113 } CodedBitstreamUnit;
    114 
    115 /**
    116 * Coded bitstream fragment structure, combining one or more units.
    117 *
    118 * This is any sequence of units.  It need not form some greater whole,
    119 * though in many cases it will.  For example, an H.264 access unit,
    120 * which is composed of a sequence of H.264 NAL units.
    121 */
    122 typedef struct CodedBitstreamFragment {
    123    /**
    124     * Pointer to the bitstream form of this fragment.
    125     *
    126     * May be NULL if the fragment only exists as component units.
    127     */
    128    uint8_t *data;
    129    /**
    130     * The number of bytes in the bitstream.
    131     *
    132     * The number of bytes in the bitstream (including any padding bits
    133     * in the final byte).
    134     */
    135    size_t   data_size;
    136    /**
    137     * The number of bits which should be ignored in the final byte.
    138     */
    139    size_t data_bit_padding;
    140    /**
    141     * A reference to the buffer containing data.
    142     *
    143     * Must be set if data is not NULL.
    144     */
    145    AVBufferRef *data_ref;
    146 
    147    /**
    148     * Number of units in this fragment.
    149     *
    150     * This may be zero if the fragment only exists in bitstream form
    151     * and has not been decomposed.
    152     */
    153    int              nb_units;
    154 
    155    /**
    156     * Number of allocated units.
    157     *
    158     * Must always be >= nb_units; designed for internal use by cbs.
    159     */
    160     int             nb_units_allocated;
    161 
    162    /**
    163     * Pointer to an array of units of length nb_units_allocated.
    164     * Only the first nb_units are valid.
    165     *
    166     * Must be NULL if nb_units_allocated is zero.
    167     */
    168    CodedBitstreamUnit *units;
    169 } CodedBitstreamFragment;
    170 
    171 
    172 struct CodedBitstreamContext;
    173 struct GetBitContext;
    174 struct PutBitContext;
    175 
    176 /**
    177 * Callback type for read tracing.
    178 *
    179 * @param ctx         User-set trace context.
    180 * @param gbc         A GetBitContext set at the start of the syntax
    181 *                    element.  This is a copy, the callee does not
    182 *                    need to preserve it.
    183 * @param length      Length in bits of the syntax element.
    184 * @param name        String name of the syntax elements.
    185 * @param subscripts  If the syntax element is an array, a pointer to
    186 *                    an array of subscripts into the array.
    187 * @param value       Parsed value of the syntax element.
    188 */
    189 typedef void (*CBSTraceReadCallback)(void *trace_context,
    190                                     struct GetBitContext *gbc,
    191                                     int start_position,
    192                                     const char *name,
    193                                     const int *subscripts,
    194                                     int64_t value);
    195 
    196 /**
    197 * Callback type for write tracing.
    198 *
    199 * @param ctx         User-set trace context.
    200 * @param pbc         A PutBitContext set at the end of the syntax
    201 *                    element.  The user must not modify this, but may
    202 *                    inspect it to determine state.
    203 * @param length      Length in bits of the syntax element.
    204 * @param name        String name of the syntax elements.
    205 * @param subscripts  If the syntax element is an array, a pointer to
    206 *                    an array of subscripts into the array.
    207 * @param value       Written value of the syntax element.
    208 */
    209 typedef void (*CBSTraceWriteCallback)(void *trace_context,
    210                                      struct PutBitContext *pbc,
    211                                      int start_position,
    212                                      const char *name,
    213                                      const int *subscripts,
    214                                      int64_t value);
    215 
    216 /**
    217 * Context structure for coded bitstream operations.
    218 */
    219 typedef struct CodedBitstreamContext {
    220    /**
    221     * Logging context to be passed to all av_log() calls associated
    222     * with this context.
    223     */
    224    void *log_ctx;
    225 
    226    /**
    227     * Internal codec-specific hooks.
    228     */
    229    const struct CodedBitstreamType *codec;
    230 
    231    /**
    232     * Internal codec-specific data.
    233     *
    234     * This contains any information needed when reading/writing
    235     * bitsteams which will not necessarily be present in a fragment.
    236     * For example, for H.264 it contains all currently visible
    237     * parameter sets - they are required to determine the bitstream
    238     * syntax but need not be present in every access unit.
    239     */
    240    void *priv_data;
    241 
    242    /**
    243     * Array of unit types which should be decomposed when reading.
    244     *
    245     * Types not in this list will be available in bitstream form only.
    246     * If NULL, all supported types will be decomposed.
    247     */
    248    const CodedBitstreamUnitType *decompose_unit_types;
    249    /**
    250     * Length of the decompose_unit_types array.
    251     */
    252    int nb_decompose_unit_types;
    253 
    254    /**
    255     * Enable trace output during read/write operations.
    256     */
    257    int trace_enable;
    258    /**
    259     * Log level to use for default trace output.
    260     *
    261     * From AV_LOG_*; defaults to AV_LOG_TRACE.
    262     */
    263    int trace_level;
    264    /**
    265     * User context pointer to pass to trace callbacks.
    266     */
    267    void *trace_context;
    268    /**
    269     * Callback for read tracing.
    270     *
    271     * If tracing is enabled then this is called once for each syntax
    272     * element parsed.
    273     */
    274    CBSTraceReadCallback  trace_read_callback;
    275    /**
    276     * Callback for write tracing.
    277     *
    278     * If tracing is enabled then this is called once for each syntax
    279     * element written.
    280     */
    281    CBSTraceWriteCallback trace_write_callback;
    282 
    283    /**
    284     * Write buffer. Used as intermediate buffer when writing units.
    285     * For internal use of cbs only.
    286     */
    287    uint8_t *write_buffer;
    288    size_t   write_buffer_size;
    289 } CodedBitstreamContext;
    290 
    291 
    292 /**
    293 * Table of all supported codec IDs.
    294 *
    295 * Terminated by AV_CODEC_ID_NONE.
    296 */
    297 extern const enum AVCodecID ff_cbs_all_codec_ids[];
    298 
    299 
    300 /**
    301 * Create and initialise a new context for the given codec.
    302 */
    303 int ff_cbs_init(CodedBitstreamContext **ctx,
    304                enum AVCodecID codec_id, void *log_ctx);
    305 
    306 /**
    307 * Reset all internal state in a context.
    308 */
    309 void ff_cbs_flush(CodedBitstreamContext *ctx);
    310 
    311 /**
    312 * Close a context and free all internal state.
    313 */
    314 void ff_cbs_close(CodedBitstreamContext **ctx);
    315 
    316 
    317 /**
    318 * Read the extradata bitstream found in codec parameters into a
    319 * fragment, then split into units and decompose.
    320 *
    321 * This also updates the internal state, so will need to be called for
    322 * codecs with extradata to read parameter sets necessary for further
    323 * parsing even if the fragment itself is not desired.
    324 *
    325 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
    326 * before use.
    327 */
    328 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
    329                          CodedBitstreamFragment *frag,
    330                          const AVCodecParameters *par);
    331 
    332 /**
    333 * Read the extradata bitstream found in a codec context into a
    334 * fragment, then split into units and decompose.
    335 *
    336 * This acts identical to ff_cbs_read_extradata() for the case where
    337 * you already have a codec context.
    338 */
    339 int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx,
    340                                     CodedBitstreamFragment *frag,
    341                                     const struct AVCodecContext *avctx);
    342 
    343 int ff_cbs_read_packet_side_data(CodedBitstreamContext *ctx,
    344                                 CodedBitstreamFragment *frag,
    345                                 const AVPacket *pkt);
    346 
    347 /**
    348 * Read the data bitstream from a packet into a fragment, then
    349 * split into units and decompose.
    350 *
    351 * This also updates the internal state of the coded bitstream context
    352 * with any persistent data from the fragment which may be required to
    353 * read following fragments (e.g. parameter sets).
    354 *
    355 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
    356 * before use.
    357 */
    358 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
    359                       CodedBitstreamFragment *frag,
    360                       const AVPacket *pkt);
    361 
    362 /**
    363 * Read a bitstream from a memory region into a fragment, then
    364 * split into units and decompose.
    365 *
    366 * This also updates the internal state of the coded bitstream context
    367 * with any persistent data from the fragment which may be required to
    368 * read following fragments (e.g. parameter sets).
    369 *
    370 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
    371 * before use.
    372 */
    373 int ff_cbs_read(CodedBitstreamContext *ctx,
    374                CodedBitstreamFragment *frag,
    375                const uint8_t *data, size_t size);
    376 
    377 
    378 /**
    379 * Write the content of the fragment to its own internal buffer.
    380 *
    381 * Writes the content of all units and then assembles them into a new
    382 * data buffer.  When modifying the content of decomposed units, this
    383 * can be used to regenerate the bitstream form of units or the whole
    384 * fragment so that it can be extracted for other use.
    385 *
    386 * This also updates the internal state of the coded bitstream context
    387 * with any persistent data from the fragment which may be required to
    388 * write following fragments (e.g. parameter sets).
    389 */
    390 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
    391                               CodedBitstreamFragment *frag);
    392 
    393 /**
    394 * Write the bitstream of a fragment to the extradata in codec parameters.
    395 *
    396 * Modifies context and fragment as ff_cbs_write_fragment_data does and
    397 * replaces any existing extradata in the structure.
    398 */
    399 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
    400                           AVCodecParameters *par,
    401                           CodedBitstreamFragment *frag);
    402 
    403 /**
    404 * Write the bitstream of a fragment to a packet.
    405 *
    406 * Modifies context and fragment as ff_cbs_write_fragment_data does.
    407 *
    408 * On success, the packet's buf is unreferenced and its buf, data and
    409 * size fields are set to the corresponding values from the newly updated
    410 * fragment; other fields are not touched.  On failure, the packet is not
    411 * touched at all.
    412 */
    413 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
    414                        AVPacket *pkt,
    415                        CodedBitstreamFragment *frag);
    416 
    417 
    418 /**
    419 * Free the units contained in a fragment as well as the fragment's
    420 * own data buffer, but not the units array itself.
    421 */
    422 void ff_cbs_fragment_reset(CodedBitstreamFragment *frag);
    423 
    424 /**
    425 * Free the units array of a fragment in addition to what
    426 * ff_cbs_fragment_reset does.
    427 */
    428 void ff_cbs_fragment_free(CodedBitstreamFragment *frag);
    429 
    430 /**
    431 * Allocate a new internal content buffer matching the type of the unit.
    432 *
    433 * The content will be zeroed.
    434 */
    435 int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
    436                              CodedBitstreamUnit *unit);
    437 
    438 /**
    439 * Insert a new unit into a fragment with the given content.
    440 *
    441 * If content_ref is supplied, it has to be a RefStruct reference
    442 * backing content; the user keeps ownership of the supplied reference.
    443 * The content structure continues to be owned by the caller if
    444 * content_ref is not supplied.
    445 */
    446 int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag,
    447                               int position,
    448                               CodedBitstreamUnitType type,
    449                               void *content,
    450                               void *content_ref);
    451 
    452 /**
    453 * Add a new unit to a fragment with the given data bitstream.
    454 *
    455 * If data_buf is not supplied then data must have been allocated with
    456 * av_malloc() and will on success become owned by the unit after this
    457 * call or freed on error.
    458 */
    459 int ff_cbs_append_unit_data(CodedBitstreamFragment *frag,
    460                            CodedBitstreamUnitType type,
    461                            uint8_t *data, size_t data_size,
    462                            AVBufferRef *data_buf);
    463 
    464 /**
    465 * Delete a unit from a fragment and free all memory it uses.
    466 *
    467 * Requires position to be >= 0 and < frag->nb_units.
    468 */
    469 void ff_cbs_delete_unit(CodedBitstreamFragment *frag,
    470                        int position);
    471 
    472 
    473 /**
    474 * Make the content of a unit refcounted.
    475 *
    476 * If the unit is not refcounted, this will do a deep copy of the unit
    477 * content to new refcounted buffers.
    478 *
    479 * It is not valid to call this function on a unit which does not have
    480 * decomposed content.
    481 */
    482 int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx,
    483                                CodedBitstreamUnit *unit);
    484 
    485 /**
    486 * Make the content of a unit writable so that internal fields can be
    487 * modified.
    488 *
    489 * If it is known that there are no other references to the content of
    490 * the unit, does nothing and returns success.  Otherwise (including the
    491 * case where the unit content is not refcounted), it does a full clone
    492 * of the content (including any internal buffers) to make a new copy,
    493 * and replaces the existing references inside the unit with that.
    494 *
    495 * It is not valid to call this function on a unit which does not have
    496 * decomposed content.
    497 */
    498 int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx,
    499                              CodedBitstreamUnit *unit);
    500 
    501 enum CbsDiscardFlags {
    502    DISCARD_FLAG_NONE = 0,
    503 
    504    /**
    505     * keep non-vcl units even if the picture has been dropped.
    506     */
    507    DISCARD_FLAG_KEEP_NON_VCL = 0x01,
    508 };
    509 
    510 /**
    511 * Discard units accroding to 'skip'.
    512 */
    513 void ff_cbs_discard_units(CodedBitstreamContext *ctx,
    514                          CodedBitstreamFragment *frag,
    515                          enum AVDiscard skip,
    516                          int flags);
    517 
    518 
    519 /**
    520 * Helper function for read tracing which formats the syntax element
    521 * and logs the result.
    522 *
    523 * Trace context should be set to the CodedBitstreamContext.
    524 */
    525 void ff_cbs_trace_read_log(void *trace_context,
    526                           struct GetBitContext *gbc, int length,
    527                           const char *str, const int *subscripts,
    528                           int64_t value);
    529 
    530 /**
    531 * Helper function for write tracing which formats the syntax element
    532 * and logs the result.
    533 *
    534 * Trace context should be set to the CodedBitstreamContext.
    535 */
    536 void ff_cbs_trace_write_log(void *trace_context,
    537                            struct PutBitContext *pbc, int length,
    538                            const char *str, const int *subscripts,
    539                            int64_t value);
    540 
    541 #endif /* AVCODEC_CBS_H */