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 * This defines a framework for converting between a coded bitstream
34 * and structures defining all individual syntax elements found in
37 * Conversion in both directions is possible. Given a coded bitstream
38 * (any meaningful fragment), it can be parsed and decomposed into
39 * syntax elements stored in a set of codec-specific structures.
40 * Similarly, given a set of those same codec-specific structures the
41 * syntax elements can be serialised and combined to create a coded
45 struct AVCodecContext
;
46 struct CodedBitstreamType
;
49 * The codec-specific type of a bitstream unit.
52 * H.264 / AVC: nal_unit_type
53 * H.265 / HEVC: nal_unit_type
54 * JPEG: marker value (without 0xff prefix)
55 * MPEG-2: start code value (without prefix)
56 * VP9: unused, set to zero (every unit is a frame)
58 typedef uint32_t CodedBitstreamUnitType
;
61 * Coded bitstream unit structure.
63 * A bitstream unit the smallest element of a bitstream which
64 * is meaningful on its own. For example, an H.264 NAL unit.
66 * See the codec-specific header for the meaning of this for any
69 typedef struct CodedBitstreamUnit
{
71 * Codec-specific type of this unit.
73 CodedBitstreamUnitType type
;
76 * Pointer to the directly-parsable bitstream form of this unit.
78 * May be NULL if the unit currently only exists in decomposed form.
82 * The number of bytes in the bitstream (including any padding bits
87 * The number of bits which should be ignored in the final byte.
89 * This supports non-byte-aligned bitstreams.
91 size_t data_bit_padding
;
93 * A reference to the buffer containing data.
95 * Must be set if data is not NULL.
97 AVBufferRef
*data_ref
;
100 * Pointer to the decomposed form of this unit.
102 * The type of this structure depends on both the codec and the
103 * type of this unit. May be NULL if the unit only exists in
108 * If content is reference counted, a reference to the buffer containing
109 * content. Null if content is not reference counted.
111 AVBufferRef
*content_ref
;
112 } CodedBitstreamUnit
;
115 * Coded bitstream fragment structure, combining one or more units.
117 * This is any sequence of units. It need not form some greater whole,
118 * though in many cases it will. For example, an H.264 access unit,
119 * which is composed of a sequence of H.264 NAL units.
121 typedef struct CodedBitstreamFragment
{
123 * Pointer to the bitstream form of this fragment.
125 * May be NULL if the fragment only exists as component units.
129 * The number of bytes in the bitstream.
131 * The number of bytes in the bitstream (including any padding bits
132 * in the final byte).
136 * The number of bits which should be ignored in the final byte.
138 size_t data_bit_padding
;
140 * A reference to the buffer containing data.
142 * Must be set if data is not NULL.
144 AVBufferRef
*data_ref
;
147 * Number of units in this fragment.
149 * This may be zero if the fragment only exists in bitstream form
150 * and has not been decomposed.
155 * Number of allocated units.
157 * Must always be >= nb_units; designed for internal use by cbs.
159 int nb_units_allocated
;
162 * Pointer to an array of units of length nb_units_allocated.
163 * Only the first nb_units are valid.
165 * Must be NULL if nb_units_allocated is zero.
167 CodedBitstreamUnit
*units
;
168 } CodedBitstreamFragment
;
171 * Context structure for coded bitstream operations.
173 typedef struct CodedBitstreamContext
{
175 * Logging context to be passed to all av_log() calls associated
181 * Internal codec-specific hooks.
183 const struct CodedBitstreamType
*codec
;
186 * Internal codec-specific data.
188 * This contains any information needed when reading/writing
189 * bitsteams which will not necessarily be present in a fragment.
190 * For example, for H.264 it contains all currently visible
191 * parameter sets - they are required to determine the bitstream
192 * syntax but need not be present in every access unit.
197 * Array of unit types which should be decomposed when reading.
199 * Types not in this list will be available in bitstream form only.
200 * If NULL, all supported types will be decomposed.
202 const CodedBitstreamUnitType
*decompose_unit_types
;
204 * Length of the decompose_unit_types array.
206 int nb_decompose_unit_types
;
209 * Enable trace output during read/write operations.
213 * Log level to use for trace output.
215 * From AV_LOG_*; defaults to AV_LOG_TRACE.
220 * Write buffer. Used as intermediate buffer when writing units.
221 * For internal use of cbs only.
223 uint8_t *write_buffer
;
224 size_t write_buffer_size
;
225 } CodedBitstreamContext
;
229 * Table of all supported codec IDs.
231 * Terminated by AV_CODEC_ID_NONE.
233 extern const enum AVCodecID ff_cbs_all_codec_ids
[];
237 * Create and initialise a new context for the given codec.
239 int ff_cbs_init(CodedBitstreamContext
**ctx
,
240 enum AVCodecID codec_id
, void *log_ctx
);
243 * Reset all internal state in a context.
245 void ff_cbs_flush(CodedBitstreamContext
*ctx
);
248 * Close a context and free all internal state.
250 void ff_cbs_close(CodedBitstreamContext
**ctx
);
254 * Read the extradata bitstream found in codec parameters into a
255 * fragment, then split into units and decompose.
257 * This also updates the internal state, so will need to be called for
258 * codecs with extradata to read parameter sets necessary for further
259 * parsing even if the fragment itself is not desired.
261 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
264 int ff_cbs_read_extradata(CodedBitstreamContext
*ctx
,
265 CodedBitstreamFragment
*frag
,
266 const AVCodecParameters
*par
);
269 * Read the extradata bitstream found in a codec context into a
270 * fragment, then split into units and decompose.
272 * This acts identical to ff_cbs_read_extradata() for the case where
273 * you already have a codec context.
275 int ff_cbs_read_extradata_from_codec(CodedBitstreamContext
*ctx
,
276 CodedBitstreamFragment
*frag
,
277 const struct AVCodecContext
*avctx
);
279 int ff_cbs_read_packet_side_data(CodedBitstreamContext
*ctx
,
280 CodedBitstreamFragment
*frag
,
281 const AVPacket
*pkt
);
284 * Read the data bitstream from a packet into a fragment, then
285 * split into units and decompose.
287 * This also updates the internal state of the coded bitstream context
288 * with any persistent data from the fragment which may be required to
289 * read following fragments (e.g. parameter sets).
291 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
294 int ff_cbs_read_packet(CodedBitstreamContext
*ctx
,
295 CodedBitstreamFragment
*frag
,
296 const AVPacket
*pkt
);
299 * Read a bitstream from a memory region into a fragment, then
300 * split into units and decompose.
302 * This also updates the internal state of the coded bitstream context
303 * with any persistent data from the fragment which may be required to
304 * read following fragments (e.g. parameter sets).
306 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
309 int ff_cbs_read(CodedBitstreamContext
*ctx
,
310 CodedBitstreamFragment
*frag
,
311 const uint8_t *data
, size_t size
);
315 * Write the content of the fragment to its own internal buffer.
317 * Writes the content of all units and then assembles them into a new
318 * data buffer. When modifying the content of decomposed units, this
319 * can be used to regenerate the bitstream form of units or the whole
320 * fragment so that it can be extracted for other use.
322 * This also updates the internal state of the coded bitstream context
323 * with any persistent data from the fragment which may be required to
324 * write following fragments (e.g. parameter sets).
326 int ff_cbs_write_fragment_data(CodedBitstreamContext
*ctx
,
327 CodedBitstreamFragment
*frag
);
330 * Write the bitstream of a fragment to the extradata in codec parameters.
332 * Modifies context and fragment as ff_cbs_write_fragment_data does and
333 * replaces any existing extradata in the structure.
335 int ff_cbs_write_extradata(CodedBitstreamContext
*ctx
,
336 AVCodecParameters
*par
,
337 CodedBitstreamFragment
*frag
);
340 * Write the bitstream of a fragment to a packet.
342 * Modifies context and fragment as ff_cbs_write_fragment_data does.
344 * On success, the packet's buf is unreferenced and its buf, data and
345 * size fields are set to the corresponding values from the newly updated
346 * fragment; other fields are not touched. On failure, the packet is not
349 int ff_cbs_write_packet(CodedBitstreamContext
*ctx
,
351 CodedBitstreamFragment
*frag
);
355 * Free the units contained in a fragment as well as the fragment's
356 * own data buffer, but not the units array itself.
358 void ff_cbs_fragment_reset(CodedBitstreamFragment
*frag
);
361 * Free the units array of a fragment in addition to what
362 * ff_cbs_fragment_reset does.
364 void ff_cbs_fragment_free(CodedBitstreamFragment
*frag
);
367 * Allocate a new internal content buffer of the given size in the unit.
369 * The content will be zeroed.
371 int ff_cbs_alloc_unit_content(CodedBitstreamUnit
*unit
,
373 void (*free
)(void *opaque
, uint8_t *content
));
376 * Allocate a new internal content buffer matching the type of the unit.
378 * The content will be zeroed.
380 int ff_cbs_alloc_unit_content2(CodedBitstreamContext
*ctx
,
381 CodedBitstreamUnit
*unit
);
384 * Insert a new unit into a fragment with the given content.
386 * The content structure continues to be owned by the caller if
387 * content_buf is not supplied.
389 int ff_cbs_insert_unit_content(CodedBitstreamFragment
*frag
,
391 CodedBitstreamUnitType type
,
393 AVBufferRef
*content_buf
);
396 * Add a new unit to a fragment with the given data bitstream.
398 * If data_buf is not supplied then data must have been allocated with
399 * av_malloc() and will on success become owned by the unit after this
400 * call or freed on error.
402 int ff_cbs_append_unit_data(CodedBitstreamFragment
*frag
,
403 CodedBitstreamUnitType type
,
404 uint8_t *data
, size_t data_size
,
405 AVBufferRef
*data_buf
);
408 * Delete a unit from a fragment and free all memory it uses.
410 * Requires position to be >= 0 and < frag->nb_units.
412 void ff_cbs_delete_unit(CodedBitstreamFragment
*frag
,
417 * Make the content of a unit refcounted.
419 * If the unit is not refcounted, this will do a deep copy of the unit
420 * content to new refcounted buffers.
422 * It is not valid to call this function on a unit which does not have
423 * decomposed content.
425 int ff_cbs_make_unit_refcounted(CodedBitstreamContext
*ctx
,
426 CodedBitstreamUnit
*unit
);
429 * Make the content of a unit writable so that internal fields can be
432 * If it is known that there are no other references to the content of
433 * the unit, does nothing and returns success. Otherwise (including the
434 * case where the unit content is not refcounted), it does a full clone
435 * of the content (including any internal buffers) to make a new copy,
436 * and replaces the existing references inside the unit with that.
438 * It is not valid to call this function on a unit which does not have
439 * decomposed content.
441 int ff_cbs_make_unit_writable(CodedBitstreamContext
*ctx
,
442 CodedBitstreamUnit
*unit
);
445 #endif /* AVCODEC_CBS_H */