2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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)
52 typedef uint32_t CodedBitstreamUnitType;
55 * Coded bitstream unit structure.
57 * A bitstream unit the smallest element of a bitstream which
58 * is meaningful on its own. For example, an H.264 NAL unit.
60 * See the codec-specific header for the meaning of this for any
63 typedef struct CodedBitstreamUnit {
65 * Codec-specific type of this unit.
67 CodedBitstreamUnitType type;
70 * Pointer to the directly-parsable bitstream form of this unit.
72 * May be NULL if the unit currently only exists in decomposed form.
76 * The number of bytes in the bitstream (including any padding bits
81 * The number of bits which should be ignored in the final byte.
83 * This supports non-byte-aligned bitstreams.
85 size_t data_bit_padding;
87 * If data is reference counted, a reference to the buffer containing
88 * data. Null if data is not reference counted.
90 AVBufferRef *data_ref;
93 * Pointer to the decomposed form of this unit.
95 * The type of this structure depends on both the codec and the
96 * type of this unit. May be NULL if the unit only exists in
101 * If content is reference counted, a reference to the buffer containing
102 * content. Null if content is not reference counted.
104 AVBufferRef *content_ref;
105 } CodedBitstreamUnit;
108 * Coded bitstream fragment structure, combining one or more units.
110 * This is any sequence of units. It need not form some greater whole,
111 * though in many cases it will. For example, an H.264 access unit,
112 * which is composed of a sequence of H.264 NAL units.
114 typedef struct CodedBitstreamFragment {
116 * Pointer to the bitstream form of this fragment.
118 * May be NULL if the fragment only exists as component units.
122 * The number of bytes in the bitstream.
124 * The number of bytes in the bitstream (including any padding bits
125 * in the final byte).
129 * The number of bits which should be ignored in the final byte.
131 size_t data_bit_padding;
133 * If data is reference counted, a reference to the buffer containing
134 * data. Null if data is not reference counted.
136 AVBufferRef *data_ref;
139 * Number of units in this fragment.
141 * This may be zero if the fragment only exists in bitstream form
142 * and has not been decomposed.
146 * Pointer to an array of units of length nb_units.
148 * Must be NULL if nb_units is zero.
150 CodedBitstreamUnit *units;
151 } CodedBitstreamFragment;
154 * Context structure for coded bitstream operations.
156 typedef struct CodedBitstreamContext {
158 * Logging context to be passed to all av_log() calls associated
164 * Internal codec-specific hooks.
166 const struct CodedBitstreamType *codec;
169 * Internal codec-specific data.
171 * This contains any information needed when reading/writing
172 * bitsteams which will not necessarily be present in a fragment.
173 * For example, for H.264 it contains all currently visible
174 * parameter sets - they are required to determine the bitstream
175 * syntax but need not be present in every access unit.
180 * Array of unit types which should be decomposed when reading.
182 * Types not in this list will be available in bitstream form only.
183 * If NULL, all supported types will be decomposed.
185 CodedBitstreamUnitType *decompose_unit_types;
187 * Length of the decompose_unit_types array.
189 int nb_decompose_unit_types;
192 * Enable trace output during read/write operations.
196 * Log level to use for trace output.
198 * From AV_LOG_*; defaults to AV_LOG_TRACE.
201 } CodedBitstreamContext;
205 * Create and initialise a new context for the given codec.
207 int ff_cbs_init(CodedBitstreamContext **ctx,
208 enum AVCodecID codec_id, void *log_ctx);
211 * Close a context and free all internal state.
213 void ff_cbs_close(CodedBitstreamContext **ctx);
217 * Read the extradata bitstream found in codec parameters into a
218 * fragment, then split into units and decompose.
220 * This also updates the internal state, so will need to be called for
221 * codecs with extradata to read parameter sets necessary for further
222 * parsing even if the fragment itself is not desired.
224 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
225 CodedBitstreamFragment *frag,
226 const AVCodecParameters *par);
229 * Read the data bitstream from a packet into a fragment, then
230 * split into units and decompose.
232 * This also updates the internal state of the coded bitstream context
233 * with any persistent data from the fragment which may be required to
234 * read following fragments (e.g. parameter sets).
236 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
237 CodedBitstreamFragment *frag,
238 const AVPacket *pkt);
241 * Read a bitstream from a memory region into a fragment, then
242 * split into units and decompose.
244 * This also updates the internal state of the coded bitstream context
245 * with any persistent data from the fragment which may be required to
246 * read following fragments (e.g. parameter sets).
248 int ff_cbs_read(CodedBitstreamContext *ctx,
249 CodedBitstreamFragment *frag,
250 const uint8_t *data, size_t size);
254 * Write the content of the fragment to its own internal buffer.
256 * Writes the content of all units and then assembles them into a new
257 * data buffer. When modifying the content of decomposed units, this
258 * can be used to regenerate the bitstream form of units or the whole
259 * fragment so that it can be extracted for other use.
261 * This also updates the internal state of the coded bitstream context
262 * with any persistent data from the fragment which may be required to
263 * write following fragments (e.g. parameter sets).
265 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
266 CodedBitstreamFragment *frag);
269 * Write the bitstream of a fragment to the extradata in codec parameters.
271 * This replaces any existing extradata in the structure.
273 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
274 AVCodecParameters *par,
275 CodedBitstreamFragment *frag);
278 * Write the bitstream of a fragment to a packet.
280 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
282 CodedBitstreamFragment *frag);
286 * Free all allocated memory in a fragment.
288 void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
289 CodedBitstreamFragment *frag);
293 * Allocate a new internal content buffer of the given size in the unit.
295 * The content will be zeroed.
297 int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
298 CodedBitstreamUnit *unit,
300 void (*free)(void *unit, uint8_t *content));
303 * Allocate a new internal data buffer of the given size in the unit.
305 * The data buffer will have input padding.
307 int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
308 CodedBitstreamUnit *unit,
312 * Insert a new unit into a fragment with the given content.
314 * The content structure continues to be owned by the caller if
315 * content_buf is not supplied.
317 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
318 CodedBitstreamFragment *frag,
320 CodedBitstreamUnitType type,
322 AVBufferRef *content_buf);
325 * Insert a new unit into a fragment with the given data bitstream.
327 * If data_buf is not supplied then data must have been allocated with
328 * av_malloc() and will become owned by the unit after this call.
330 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
331 CodedBitstreamFragment *frag,
333 CodedBitstreamUnitType type,
334 uint8_t *data, size_t data_size,
335 AVBufferRef *data_buf);
338 * Delete a unit from a fragment and free all memory it uses.
340 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
341 CodedBitstreamFragment *frag,
345 #endif /* AVCODEC_CBS_H */