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"
31 * This defines a framework for converting between a coded bitstream
32 * and structures defining all individual syntax elements found in
35 * Conversion in both directions is possible. Given a coded bitstream
36 * (any meaningful fragment), it can be parsed and decomposed into
37 * syntax elements stored in a set of codec-specific structures.
38 * Similarly, given a set of those same codec-specific structures the
39 * syntax elements can be serialised and combined to create a coded
43 struct CodedBitstreamType;
46 * The codec-specific type of a bitstream unit.
49 * H.264 / AVC: nal_unit_type
50 * H.265 / HEVC: nal_unit_type
51 * JPEG: marker value (without 0xff prefix)
52 * MPEG-2: start code value (without prefix)
53 * VP9: unused, set to zero (every unit is a frame)
55 typedef uint32_t CodedBitstreamUnitType;
58 * Coded bitstream unit structure.
60 * A bitstream unit the smallest element of a bitstream which
61 * is meaningful on its own. For example, an H.264 NAL unit.
63 * See the codec-specific header for the meaning of this for any
66 typedef struct CodedBitstreamUnit {
68 * Codec-specific type of this unit.
70 CodedBitstreamUnitType type;
73 * Pointer to the directly-parsable bitstream form of this unit.
75 * May be NULL if the unit currently only exists in decomposed form.
79 * The number of bytes in the bitstream (including any padding bits
84 * The number of bits which should be ignored in the final byte.
86 * This supports non-byte-aligned bitstreams.
88 size_t data_bit_padding;
90 * A reference to the buffer containing data.
92 * Must be set if data is not NULL.
94 AVBufferRef *data_ref;
97 * Pointer to the decomposed form of this unit.
99 * The type of this structure depends on both the codec and the
100 * type of this unit. May be NULL if the unit only exists in
105 * If content is reference counted, a reference to the buffer containing
106 * content. Null if content is not reference counted.
108 AVBufferRef *content_ref;
109 } CodedBitstreamUnit;
112 * Coded bitstream fragment structure, combining one or more units.
114 * This is any sequence of units. It need not form some greater whole,
115 * though in many cases it will. For example, an H.264 access unit,
116 * which is composed of a sequence of H.264 NAL units.
118 typedef struct CodedBitstreamFragment {
120 * Pointer to the bitstream form of this fragment.
122 * May be NULL if the fragment only exists as component units.
126 * The number of bytes in the bitstream.
128 * The number of bytes in the bitstream (including any padding bits
129 * in the final byte).
133 * The number of bits which should be ignored in the final byte.
135 size_t data_bit_padding;
137 * A reference to the buffer containing data.
139 * Must be set if data is not NULL.
141 AVBufferRef *data_ref;
144 * Number of units in this fragment.
146 * This may be zero if the fragment only exists in bitstream form
147 * and has not been decomposed.
152 * Number of allocated units.
154 * Must always be >= nb_units; designed for internal use by cbs.
156 int nb_units_allocated;
159 * Pointer to an array of units of length nb_units_allocated.
160 * Only the first nb_units are valid.
162 * Must be NULL if nb_units_allocated is zero.
164 CodedBitstreamUnit *units;
165 } CodedBitstreamFragment;
168 * Context structure for coded bitstream operations.
170 typedef struct CodedBitstreamContext {
172 * Logging context to be passed to all av_log() calls associated
178 * Internal codec-specific hooks.
180 const struct CodedBitstreamType *codec;
183 * Internal codec-specific data.
185 * This contains any information needed when reading/writing
186 * bitsteams which will not necessarily be present in a fragment.
187 * For example, for H.264 it contains all currently visible
188 * parameter sets - they are required to determine the bitstream
189 * syntax but need not be present in every access unit.
194 * Array of unit types which should be decomposed when reading.
196 * Types not in this list will be available in bitstream form only.
197 * If NULL, all supported types will be decomposed.
199 CodedBitstreamUnitType *decompose_unit_types;
201 * Length of the decompose_unit_types array.
203 int nb_decompose_unit_types;
206 * Enable trace output during read/write operations.
210 * Log level to use for trace output.
212 * From AV_LOG_*; defaults to AV_LOG_TRACE.
217 * Write buffer. Used as intermediate buffer when writing units.
218 * For internal use of cbs only.
220 uint8_t *write_buffer;
221 size_t write_buffer_size;
222 } CodedBitstreamContext;
226 * Table of all supported codec IDs.
228 * Terminated by AV_CODEC_ID_NONE.
230 extern const enum AVCodecID ff_cbs_all_codec_ids[];
234 * Create and initialise a new context for the given codec.
236 int ff_cbs_init(CodedBitstreamContext **ctx,
237 enum AVCodecID codec_id, void *log_ctx);
240 * Reset all internal state in a context.
242 void ff_cbs_flush(CodedBitstreamContext *ctx);
245 * Close a context and free all internal state.
247 void ff_cbs_close(CodedBitstreamContext **ctx);
251 * Read the extradata bitstream found in codec parameters into a
252 * fragment, then split into units and decompose.
254 * This also updates the internal state, so will need to be called for
255 * codecs with extradata to read parameter sets necessary for further
256 * parsing even if the fragment itself is not desired.
258 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
261 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
262 CodedBitstreamFragment *frag,
263 const AVCodecParameters *par);
266 * Read the extradata bitstream found in a codec context into a
267 * fragment, then split into units and decompose.
269 * This acts identical to ff_cbs_read_extradata() for the case where
270 * you already have a codec context.
272 int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx,
273 CodedBitstreamFragment *frag,
274 const AVCodecContext *avctx);
277 * Read the data bitstream from a packet into a fragment, then
278 * split into units and decompose.
280 * This also updates the internal state of the coded bitstream context
281 * with any persistent data from the fragment which may be required to
282 * read following fragments (e.g. parameter sets).
284 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
287 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
288 CodedBitstreamFragment *frag,
289 const AVPacket *pkt);
292 * Read a bitstream from a memory region into a fragment, then
293 * split into units and decompose.
295 * This also updates the internal state of the coded bitstream context
296 * with any persistent data from the fragment which may be required to
297 * read following fragments (e.g. parameter sets).
299 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
302 int ff_cbs_read(CodedBitstreamContext *ctx,
303 CodedBitstreamFragment *frag,
304 const uint8_t *data, size_t size);
308 * Write the content of the fragment to its own internal buffer.
310 * Writes the content of all units and then assembles them into a new
311 * data buffer. When modifying the content of decomposed units, this
312 * can be used to regenerate the bitstream form of units or the whole
313 * fragment so that it can be extracted for other use.
315 * This also updates the internal state of the coded bitstream context
316 * with any persistent data from the fragment which may be required to
317 * write following fragments (e.g. parameter sets).
319 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
320 CodedBitstreamFragment *frag);
323 * Write the bitstream of a fragment to the extradata in codec parameters.
325 * Modifies context and fragment as ff_cbs_write_fragment_data does and
326 * replaces any existing extradata in the structure.
328 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
329 AVCodecParameters *par,
330 CodedBitstreamFragment *frag);
333 * Write the bitstream of a fragment to a packet.
335 * Modifies context and fragment as ff_cbs_write_fragment_data does.
337 * On success, the packet's buf is unreferenced and its buf, data and
338 * size fields are set to the corresponding values from the newly updated
339 * fragment; other fields are not touched. On failure, the packet is not
342 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
344 CodedBitstreamFragment *frag);
348 * Free the units contained in a fragment as well as the fragment's
349 * own data buffer, but not the units array itself.
351 void ff_cbs_fragment_reset(CodedBitstreamFragment *frag);
354 * Free the units array of a fragment in addition to what
355 * ff_cbs_fragment_reset does.
357 void ff_cbs_fragment_free(CodedBitstreamFragment *frag);
360 * Allocate a new internal content buffer of the given size in the unit.
362 * The content will be zeroed.
364 int ff_cbs_alloc_unit_content(CodedBitstreamUnit *unit,
366 void (*free)(void *opaque, uint8_t *content));
369 * Allocate a new internal content buffer matching the type of the unit.
371 * The content will be zeroed.
373 int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx,
374 CodedBitstreamUnit *unit);
378 * Allocate a new internal data buffer of the given size in the unit.
380 * The data buffer will have input padding.
382 int ff_cbs_alloc_unit_data(CodedBitstreamUnit *unit,
386 * Insert a new unit into a fragment with the given content.
388 * The content structure continues to be owned by the caller if
389 * content_buf is not supplied.
391 int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag,
393 CodedBitstreamUnitType type,
395 AVBufferRef *content_buf);
398 * Insert a new unit into a fragment with the given data bitstream.
400 * If data_buf is not supplied then data must have been allocated with
401 * av_malloc() and will on success become owned by the unit after this
402 * call or freed on error.
404 int ff_cbs_insert_unit_data(CodedBitstreamFragment *frag,
406 CodedBitstreamUnitType type,
407 uint8_t *data, size_t data_size,
408 AVBufferRef *data_buf);
411 * Delete a unit from a fragment and free all memory it uses.
413 * Requires position to be >= 0 and < frag->nb_units.
415 void ff_cbs_delete_unit(CodedBitstreamFragment *frag,
420 * Make the content of a unit refcounted.
422 * If the unit is not refcounted, this will do a deep copy of the unit
423 * content to new refcounted buffers.
425 * It is not valid to call this function on a unit which does not have
426 * decomposed content.
428 int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx,
429 CodedBitstreamUnit *unit);
432 * Make the content of a unit writable so that internal fields can be
435 * If it is known that there are no other references to the content of
436 * the unit, does nothing and returns success. Otherwise (including the
437 * case where the unit content is not refcounted), it does a full clone
438 * of the content (including any internal buffers) to make a new copy,
439 * and replaces the existing references inside the unit with that.
441 * It is not valid to call this function on a unit which does not have
442 * decomposed content.
444 int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx,
445 CodedBitstreamUnit *unit);
448 #endif /* AVCODEC_CBS_H */