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.
48 * H.264 / AVC: nal_unit_type
49 * H.265 / HEVC: nal_unit_type
50 * MPEG-2: start code value (without prefix)
51 * VP9: unused, set to zero (every unit is a frame)
53 typedef uint32_t CodedBitstreamUnitType;
56 * Coded bitstream unit structure.
58 * A bitstream unit the smallest element of a bitstream which
59 * is meaningful on its own. For example, an H.264 NAL unit.
61 * See the codec-specific header for the meaning of this for any
64 typedef struct CodedBitstreamUnit {
66 * Codec-specific type of this unit.
68 CodedBitstreamUnitType type;
71 * Pointer to the directly-parsable bitstream form of this unit.
73 * May be NULL if the unit currently only exists in decomposed form.
77 * The number of bytes in the bitstream (including any padding bits
82 * The number of bits which should be ignored in the final byte.
84 * This supports non-byte-aligned bitstreams.
86 size_t data_bit_padding;
88 * A reference to the buffer containing data.
90 * Must be set if data is not NULL.
92 AVBufferRef *data_ref;
95 * Pointer to the decomposed form of this unit.
97 * The type of this structure depends on both the codec and the
98 * type of this unit. May be NULL if the unit only exists in
103 * If content is reference counted, a reference to the buffer containing
104 * content. Null if content is not reference counted.
106 AVBufferRef *content_ref;
107 } CodedBitstreamUnit;
110 * Coded bitstream fragment structure, combining one or more units.
112 * This is any sequence of units. It need not form some greater whole,
113 * though in many cases it will. For example, an H.264 access unit,
114 * which is composed of a sequence of H.264 NAL units.
116 typedef struct CodedBitstreamFragment {
118 * Pointer to the bitstream form of this fragment.
120 * May be NULL if the fragment only exists as component units.
124 * The number of bytes in the bitstream.
126 * The number of bytes in the bitstream (including any padding bits
127 * in the final byte).
131 * The number of bits which should be ignored in the final byte.
133 size_t data_bit_padding;
135 * A reference to the buffer containing data.
137 * Must be set if data is not NULL.
139 AVBufferRef *data_ref;
142 * Number of units in this fragment.
144 * This may be zero if the fragment only exists in bitstream form
145 * and has not been decomposed.
150 * Number of allocated units.
152 * Must always be >= nb_units; designed for internal use by cbs.
154 int nb_units_allocated;
157 * Pointer to an array of units of length nb_units_allocated.
158 * Only the first nb_units are valid.
160 * Must be NULL if nb_units_allocated is zero.
162 CodedBitstreamUnit *units;
163 } CodedBitstreamFragment;
166 * Context structure for coded bitstream operations.
168 typedef struct CodedBitstreamContext {
170 * Logging context to be passed to all av_log() calls associated
176 * Internal codec-specific hooks.
178 const struct CodedBitstreamType *codec;
181 * Internal codec-specific data.
183 * This contains any information needed when reading/writing
184 * bitsteams which will not necessarily be present in a fragment.
185 * For example, for H.264 it contains all currently visible
186 * parameter sets - they are required to determine the bitstream
187 * syntax but need not be present in every access unit.
192 * Array of unit types which should be decomposed when reading.
194 * Types not in this list will be available in bitstream form only.
195 * If NULL, all supported types will be decomposed.
197 CodedBitstreamUnitType *decompose_unit_types;
199 * Length of the decompose_unit_types array.
201 int nb_decompose_unit_types;
204 * Enable trace output during read/write operations.
208 * Log level to use for trace output.
210 * From AV_LOG_*; defaults to AV_LOG_TRACE.
213 } CodedBitstreamContext;
217 * Table of all supported codec IDs.
219 * Terminated by AV_CODEC_ID_NONE.
221 extern const enum AVCodecID ff_cbs_all_codec_ids[];
225 * Create and initialise a new context for the given codec.
227 int ff_cbs_init(CodedBitstreamContext **ctx,
228 enum AVCodecID codec_id, void *log_ctx);
231 * Close a context and free all internal state.
233 void ff_cbs_close(CodedBitstreamContext **ctx);
237 * Read the extradata bitstream found in codec parameters into a
238 * fragment, then split into units and decompose.
240 * This also updates the internal state, so will need to be called for
241 * codecs with extradata to read parameter sets necessary for further
242 * parsing even if the fragment itself is not desired.
244 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
247 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
248 CodedBitstreamFragment *frag,
249 const AVCodecParameters *par);
252 * Read the data bitstream from a packet into a fragment, then
253 * split into units and decompose.
255 * This also updates the internal state of the coded bitstream context
256 * with any persistent data from the fragment which may be required to
257 * read following fragments (e.g. parameter sets).
259 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
262 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
263 CodedBitstreamFragment *frag,
264 const AVPacket *pkt);
267 * Read a bitstream from a memory region into a fragment, then
268 * split into units and decompose.
270 * This also updates the internal state of the coded bitstream context
271 * with any persistent data from the fragment which may be required to
272 * read following fragments (e.g. parameter sets).
274 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
277 int ff_cbs_read(CodedBitstreamContext *ctx,
278 CodedBitstreamFragment *frag,
279 const uint8_t *data, size_t size);
283 * Write the content of the fragment to its own internal buffer.
285 * Writes the content of all units and then assembles them into a new
286 * data buffer. When modifying the content of decomposed units, this
287 * can be used to regenerate the bitstream form of units or the whole
288 * fragment so that it can be extracted for other use.
290 * This also updates the internal state of the coded bitstream context
291 * with any persistent data from the fragment which may be required to
292 * write following fragments (e.g. parameter sets).
294 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
295 CodedBitstreamFragment *frag);
298 * Write the bitstream of a fragment to the extradata in codec parameters.
300 * This replaces any existing extradata in the structure.
302 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
303 AVCodecParameters *par,
304 CodedBitstreamFragment *frag);
307 * Write the bitstream of a fragment to a packet.
309 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
311 CodedBitstreamFragment *frag);
315 * Free the units contained in a fragment as well as the fragment's
316 * own data buffer, but not the units array itself.
318 void ff_cbs_fragment_reset(CodedBitstreamContext *ctx,
319 CodedBitstreamFragment *frag);
322 * Free the units array of a fragment in addition to what
323 * ff_cbs_fragment_reset does.
325 void ff_cbs_fragment_free(CodedBitstreamContext *ctx,
326 CodedBitstreamFragment *frag);
329 * Allocate a new internal content buffer of the given size in the unit.
331 * The content will be zeroed.
333 int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
334 CodedBitstreamUnit *unit,
336 void (*free)(void *unit, uint8_t *content));
339 * Allocate a new internal data buffer of the given size in the unit.
341 * The data buffer will have input padding.
343 int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
344 CodedBitstreamUnit *unit,
348 * Insert a new unit into a fragment with the given content.
350 * The content structure continues to be owned by the caller if
351 * content_buf is not supplied.
353 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
354 CodedBitstreamFragment *frag,
356 CodedBitstreamUnitType type,
358 AVBufferRef *content_buf);
361 * Insert a new unit into a fragment with the given data bitstream.
363 * If data_buf is not supplied then data must have been allocated with
364 * av_malloc() and will become owned by the unit after this call.
366 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
367 CodedBitstreamFragment *frag,
369 CodedBitstreamUnitType type,
370 uint8_t *data, size_t data_size,
371 AVBufferRef *data_buf);
374 * Delete a unit from a fragment and free all memory it uses.
376 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
377 CodedBitstreamFragment *frag,
381 #endif /* AVCODEC_CBS_H */