2 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/buffer.h"
28 #include "codec_par.h"
33 #define CBS_PREFIX cbs
36 #define CBS_FUNC_PREFIX_NAME(prefix, name) ff_ ## prefix ## _ ## name
37 #define CBS_FUNC_NAME(prefix, name) CBS_FUNC_PREFIX_NAME(prefix, name)
38 #define CBS_FUNC(name) CBS_FUNC_NAME(CBS_PREFIX, name)
41 * This defines a framework for converting between a coded bitstream
42 * and structures defining all individual syntax elements found in
45 * Conversion in both directions is possible. Given a coded bitstream
46 * (any meaningful fragment), it can be parsed and decomposed into
47 * syntax elements stored in a set of codec-specific structures.
48 * Similarly, given a set of those same codec-specific structures the
49 * syntax elements can be serialised and combined to create a coded
53 struct AVCodecContext
;
54 struct CodedBitstreamType
;
57 * The codec-specific type of a bitstream unit.
60 * H.264 / AVC: nal_unit_type
61 * H.265 / HEVC: nal_unit_type
62 * JPEG: marker value (without 0xff prefix)
63 * MPEG-2: start code value (without prefix)
64 * VP9: unused, set to zero (every unit is a frame)
66 typedef uint32_t CodedBitstreamUnitType
;
69 * Coded bitstream unit structure.
71 * A bitstream unit the smallest element of a bitstream which
72 * is meaningful on its own. For example, an H.264 NAL unit.
74 * See the codec-specific header for the meaning of this for any
77 typedef struct CodedBitstreamUnit
{
79 * Codec-specific type of this unit.
81 CodedBitstreamUnitType type
;
84 * Pointer to the directly-parsable bitstream form of this unit.
86 * May be NULL if the unit currently only exists in decomposed form.
90 * The number of bytes in the bitstream (including any padding bits
95 * The number of bits which should be ignored in the final byte.
97 * This supports non-byte-aligned bitstreams.
99 size_t data_bit_padding
;
101 * A reference to the buffer containing data.
103 * Must be set if data is not NULL.
105 AVBufferRef
*data_ref
;
108 * Pointer to the decomposed form of this unit.
110 * The type of this structure depends on both the codec and the
111 * type of this unit. May be NULL if the unit only exists in
116 * If content is reference counted, a RefStruct reference backing content.
117 * NULL if content is not reference counted.
120 } CodedBitstreamUnit
;
123 * Coded bitstream fragment structure, combining one or more units.
125 * This is any sequence of units. It need not form some greater whole,
126 * though in many cases it will. For example, an H.264 access unit,
127 * which is composed of a sequence of H.264 NAL units.
129 typedef struct CodedBitstreamFragment
{
131 * Pointer to the bitstream form of this fragment.
133 * May be NULL if the fragment only exists as component units.
137 * The number of bytes in the bitstream.
139 * The number of bytes in the bitstream (including any padding bits
140 * in the final byte).
144 * The number of bits which should be ignored in the final byte.
146 size_t data_bit_padding
;
148 * A reference to the buffer containing data.
150 * Must be set if data is not NULL.
152 AVBufferRef
*data_ref
;
155 * Number of units in this fragment.
157 * This may be zero if the fragment only exists in bitstream form
158 * and has not been decomposed.
163 * Number of allocated units.
165 * Must always be >= nb_units; designed for internal use by cbs.
167 int nb_units_allocated
;
170 * Pointer to an array of units of length nb_units_allocated.
171 * Only the first nb_units are valid.
173 * Must be NULL if nb_units_allocated is zero.
175 CodedBitstreamUnit
*units
;
176 } CodedBitstreamFragment
;
179 struct CodedBitstreamContext
;
180 struct GetBitContext
;
181 struct PutBitContext
;
184 * Callback type for read tracing.
186 * @param ctx User-set trace context.
187 * @param gbc A GetBitContext set at the start of the syntax
188 * element. This is a copy, the callee does not
189 * need to preserve it.
190 * @param length Length in bits of the syntax element.
191 * @param name String name of the syntax elements.
192 * @param subscripts If the syntax element is an array, a pointer to
193 * an array of subscripts into the array.
194 * @param value Parsed value of the syntax element.
196 typedef void (*CBSTraceReadCallback
)(void *trace_context
,
197 struct GetBitContext
*gbc
,
200 const int *subscripts
,
204 * Callback type for write tracing.
206 * @param ctx User-set trace context.
207 * @param pbc A PutBitContext set at the end of the syntax
208 * element. The user must not modify this, but may
209 * inspect it to determine state.
210 * @param length Length in bits of the syntax element.
211 * @param name String name of the syntax elements.
212 * @param subscripts If the syntax element is an array, a pointer to
213 * an array of subscripts into the array.
214 * @param value Written value of the syntax element.
216 typedef void (*CBSTraceWriteCallback
)(void *trace_context
,
217 struct PutBitContext
*pbc
,
220 const int *subscripts
,
224 * Context structure for coded bitstream operations.
226 typedef struct CodedBitstreamContext
{
228 * Logging context to be passed to all av_log() calls associated
234 * Internal codec-specific hooks.
236 const struct CodedBitstreamType
*codec
;
239 * Internal codec-specific data.
241 * This contains any information needed when reading/writing
242 * bitsteams which will not necessarily be present in a fragment.
243 * For example, for H.264 it contains all currently visible
244 * parameter sets - they are required to determine the bitstream
245 * syntax but need not be present in every access unit.
250 * Array of unit types which should be decomposed when reading.
252 * Types not in this list will be available in bitstream form only.
253 * If NULL, all supported types will be decomposed.
255 const CodedBitstreamUnitType
*decompose_unit_types
;
257 * Length of the decompose_unit_types array.
259 int nb_decompose_unit_types
;
262 * Enable trace output during read/write operations.
266 * Log level to use for default trace output.
268 * From AV_LOG_*; defaults to AV_LOG_TRACE.
272 * User context pointer to pass to trace callbacks.
276 * Callback for read tracing.
278 * If tracing is enabled then this is called once for each syntax
281 CBSTraceReadCallback trace_read_callback
;
283 * Callback for write tracing.
285 * If tracing is enabled then this is called once for each syntax
288 CBSTraceWriteCallback trace_write_callback
;
291 * Write buffer. Used as intermediate buffer when writing units.
292 * For internal use of cbs only.
294 uint8_t *write_buffer
;
295 size_t write_buffer_size
;
296 } CodedBitstreamContext
;
300 * Table of all supported codec IDs.
302 * Terminated by AV_CODEC_ID_NONE.
304 extern const enum AVCodecID
CBS_FUNC(all_codec_ids
)[];
308 * Create and initialise a new context for the given codec.
310 int CBS_FUNC(init
)(CodedBitstreamContext
**ctx
,
311 enum AVCodecID codec_id
, void *log_ctx
);
314 * Reset all internal state in a context.
316 void CBS_FUNC(flush
)(CodedBitstreamContext
*ctx
);
319 * Close a context and free all internal state.
321 void CBS_FUNC(close
)(CodedBitstreamContext
**ctx
);
325 * Read the extradata bitstream found in codec parameters into a
326 * fragment, then split into units and decompose.
328 * This also updates the internal state, so will need to be called for
329 * codecs with extradata to read parameter sets necessary for further
330 * parsing even if the fragment itself is not desired.
332 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
335 int CBS_FUNC(read_extradata
)(CodedBitstreamContext
*ctx
,
336 CodedBitstreamFragment
*frag
,
337 const AVCodecParameters
*par
);
340 * Read the extradata bitstream found in a codec context into a
341 * fragment, then split into units and decompose.
343 * This acts identical to ff_cbs_read_extradata() for the case where
344 * you already have a codec context.
346 int CBS_FUNC(read_extradata_from_codec
)(CodedBitstreamContext
*ctx
,
347 CodedBitstreamFragment
*frag
,
348 const struct AVCodecContext
*avctx
);
350 int CBS_FUNC(read_packet_side_data
)(CodedBitstreamContext
*ctx
,
351 CodedBitstreamFragment
*frag
,
352 const AVPacket
*pkt
);
355 * Read the data bitstream from a packet into a fragment, then
356 * split into units and decompose.
358 * This also updates the internal state of the coded bitstream context
359 * with any persistent data from the fragment which may be required to
360 * read following fragments (e.g. parameter sets).
362 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
365 int CBS_FUNC(read_packet
)(CodedBitstreamContext
*ctx
,
366 CodedBitstreamFragment
*frag
,
367 const AVPacket
*pkt
);
370 * Read a bitstream from a memory region into a fragment, then
371 * split into units and decompose.
373 * This also updates the internal state of the coded bitstream context
374 * with any persistent data from the fragment which may be required to
375 * read following fragments (e.g. parameter sets).
377 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
380 int CBS_FUNC(read
)(CodedBitstreamContext
*ctx
,
381 CodedBitstreamFragment
*frag
,
382 const AVBufferRef
*buf
,
383 const uint8_t *data
, size_t size
);
387 * Write the content of the fragment to its own internal buffer.
389 * Writes the content of all units and then assembles them into a new
390 * data buffer. When modifying the content of decomposed units, this
391 * can be used to regenerate the bitstream form of units or the whole
392 * fragment so that it can be extracted for other use.
394 * This also updates the internal state of the coded bitstream context
395 * with any persistent data from the fragment which may be required to
396 * write following fragments (e.g. parameter sets).
398 int CBS_FUNC(write_fragment_data
)(CodedBitstreamContext
*ctx
,
399 CodedBitstreamFragment
*frag
);
402 * Write the bitstream of a fragment to the extradata in codec parameters.
404 * Modifies context and fragment as ff_cbs_write_fragment_data does and
405 * replaces any existing extradata in the structure.
407 int CBS_FUNC(write_extradata
)(CodedBitstreamContext
*ctx
,
408 AVCodecParameters
*par
,
409 CodedBitstreamFragment
*frag
);
412 * Write the bitstream of a fragment to a packet.
414 * Modifies context and fragment as ff_cbs_write_fragment_data does.
416 * On success, the packet's buf is unreferenced and its buf, data and
417 * size fields are set to the corresponding values from the newly updated
418 * fragment; other fields are not touched. On failure, the packet is not
421 int CBS_FUNC(write_packet
)(CodedBitstreamContext
*ctx
,
423 CodedBitstreamFragment
*frag
);
427 * Free the units contained in a fragment as well as the fragment's
428 * own data buffer, but not the units array itself.
430 void CBS_FUNC(fragment_reset
)(CodedBitstreamFragment
*frag
);
433 * Free the units array of a fragment in addition to what
434 * ff_cbs_fragment_reset does.
436 void CBS_FUNC(fragment_free
)(CodedBitstreamFragment
*frag
);
439 * Allocate a new internal content buffer matching the type of the unit.
441 * The content will be zeroed.
443 int CBS_FUNC(alloc_unit_content
)(CodedBitstreamContext
*ctx
,
444 CodedBitstreamUnit
*unit
);
447 * Insert a new unit into a fragment with the given content.
449 * If content_ref is supplied, it has to be a RefStruct reference
450 * backing content; the user keeps ownership of the supplied reference.
451 * The content structure continues to be owned by the caller if
452 * content_ref is not supplied.
454 int CBS_FUNC(insert_unit_content
)(CodedBitstreamFragment
*frag
,
456 CodedBitstreamUnitType type
,
461 * Add a new unit to a fragment with the given data bitstream.
463 * If data_buf is not supplied then data must have been allocated with
464 * av_malloc() and will on success become owned by the unit after this
465 * call or freed on error.
467 int CBS_FUNC(append_unit_data
)(CodedBitstreamFragment
*frag
,
468 CodedBitstreamUnitType type
,
469 uint8_t *data
, size_t data_size
,
470 AVBufferRef
*data_buf
);
473 * Delete a unit from a fragment and free all memory it uses.
475 * Requires position to be >= 0 and < frag->nb_units.
477 void CBS_FUNC(delete_unit
)(CodedBitstreamFragment
*frag
,
482 * Make the content of a unit refcounted.
484 * If the unit is not refcounted, this will do a deep copy of the unit
485 * content to new refcounted buffers.
487 * It is not valid to call this function on a unit which does not have
488 * decomposed content.
490 int CBS_FUNC(make_unit_refcounted
)(CodedBitstreamContext
*ctx
,
491 CodedBitstreamUnit
*unit
);
494 * Make the content of a unit writable so that internal fields can be
497 * If it is known that there are no other references to the content of
498 * the unit, does nothing and returns success. Otherwise (including the
499 * case where the unit content is not refcounted), it does a full clone
500 * of the content (including any internal buffers) to make a new copy,
501 * and replaces the existing references inside the unit with that.
503 * It is not valid to call this function on a unit which does not have
504 * decomposed content.
506 int CBS_FUNC(make_unit_writable
)(CodedBitstreamContext
*ctx
,
507 CodedBitstreamUnit
*unit
);
509 enum CbsDiscardFlags
{
510 DISCARD_FLAG_NONE
= 0,
513 * keep non-vcl units even if the picture has been dropped.
515 DISCARD_FLAG_KEEP_NON_VCL
= 0x01,
519 * Discard units according to 'skip'.
521 void CBS_FUNC(discard_units
)(CodedBitstreamContext
*ctx
,
522 CodedBitstreamFragment
*frag
,
528 * Helper function for read tracing which formats the syntax element
529 * and logs the result.
531 * Trace context should be set to the CodedBitstreamContext.
533 void CBS_FUNC(trace_read_log
)(void *trace_context
,
534 struct GetBitContext
*gbc
, int length
,
535 const char *str
, const int *subscripts
,
539 * Helper function for write tracing which formats the syntax element
540 * and logs the result.
542 * Trace context should be set to the CodedBitstreamContext.
544 void CBS_FUNC(trace_write_log
)(void *trace_context
,
545 struct PutBitContext
*pbc
, int length
,
546 const char *str
, const int *subscripts
,
549 #endif /* AVCODEC_CBS_H */