1 /*
2 * This file is part of FFmpeg.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #ifndef AVCODEC_CBS_H
20 #define AVCODEC_CBS_H
21
22 #include <stddef.h>
23 #include <stdint.h>
24
26
30
31
32 /*
33 * This defines a framework for converting between a coded bitstream
34 * and structures defining all individual syntax elements found in
35 * such a stream.
36 *
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
42 * bitstream.
43 */
44
47
48 /**
49 * The codec-specific type of a bitstream unit.
50 *
51 * AV1: obu_type
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)
57 */
59
60 /**
61 * Coded bitstream unit structure.
62 *
63 * A bitstream unit the smallest element of a bitstream which
64 * is meaningful on its own. For example, an H.264 NAL unit.
65 *
66 * See the codec-specific header for the meaning of this for any
67 * particular codec.
68 */
70 /**
71 * Codec-specific type of this unit.
72 */
74
75 /**
76 * Pointer to the directly-parsable bitstream form of this unit.
77 *
78 * May be NULL if the unit currently only exists in decomposed form.
79 */
81 /**
82 * The number of bytes in the bitstream (including any padding bits
83 * in the final byte).
84 */
86 /**
87 * The number of bits which should be ignored in the final byte.
88 *
89 * This supports non-byte-aligned bitstreams.
90 */
92 /**
93 * A reference to the buffer containing data.
94 *
95 * Must be set if data is not NULL.
96 */
98
99 /**
100 * Pointer to the decomposed form of this unit.
101 *
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
104 * bitstream form.
105 */
107 /**
108 * If content is reference counted, a reference to the buffer containing
109 * content. Null if content is not reference counted.
110 */
113
114 /**
115 * Coded bitstream fragment structure, combining one or more units.
116 *
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.
120 */
122 /**
123 * Pointer to the bitstream form of this fragment.
124 *
125 * May be NULL if the fragment only exists as component units.
126 */
128 /**
129 * The number of bytes in the bitstream.
130 *
131 * The number of bytes in the bitstream (including any padding bits
132 * in the final byte).
133 */
135 /**
136 * The number of bits which should be ignored in the final byte.
137 */
139 /**
140 * A reference to the buffer containing data.
141 *
142 * Must be set if data is not NULL.
143 */
145
146 /**
147 * Number of units in this fragment.
148 *
149 * This may be zero if the fragment only exists in bitstream form
150 * and has not been decomposed.
151 */
153
154 /**
155 * Number of allocated units.
156 *
157 * Must always be >= nb_units; designed for internal use by cbs.
158 */
160
161 /**
162 * Pointer to an array of units of length nb_units_allocated.
163 * Only the first nb_units are valid.
164 *
165 * Must be NULL if nb_units_allocated is zero.
166 */
169
170 /**
171 * Context structure for coded bitstream operations.
172 */
174 /**
175 * Logging context to be passed to all av_log() calls associated
176 * with this context.
177 */
179
180 /**
181 * Internal codec-specific hooks.
182 */
184
185 /**
186 * Internal codec-specific data.
187 *
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.
193 */
195
196 /**
197 * Array of unit types which should be decomposed when reading.
198 *
199 * Types not in this list will be available in bitstream form only.
200 * If NULL, all supported types will be decomposed.
201 */
203 /**
204 * Length of the decompose_unit_types array.
205 */
207
208 /**
209 * Enable trace output during read/write operations.
210 */
212 /**
213 * Log level to use for trace output.
214 *
215 * From AV_LOG_*; defaults to AV_LOG_TRACE.
216 */
218
219 /**
220 * Write buffer. Used as intermediate buffer when writing units.
221 * For internal use of cbs only.
222 */
226
227
228 /**
229 * Table of all supported codec IDs.
230 *
231 * Terminated by AV_CODEC_ID_NONE.
232 */
234
235
236 /**
237 * Create and initialise a new context for the given codec.
238 */
241
242 /**
243 * Reset all internal state in a context.
244 */
246
247 /**
248 * Close a context and free all internal state.
249 */
251
252
253 /**
254 * Read the extradata bitstream found in codec parameters into a
255 * fragment, then split into units and decompose.
256 *
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.
260 *
261 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
262 * before use.
263 */
267
268 /**
269 * Read the extradata bitstream found in a codec context into a
270 * fragment, then split into units and decompose.
271 *
272 * This acts identical to ff_cbs_read_extradata() for the case where
273 * you already have a codec context.
274 */
278
282
283 /**
284 * Read the data bitstream from a packet into a fragment, then
285 * split into units and decompose.
286 *
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).
290 *
291 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
292 * before use.
293 */
297
298 /**
299 * Read a bitstream from a memory region into a fragment, then
300 * split into units and decompose.
301 *
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).
305 *
306 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
307 * before use.
308 */
312
313
314 /**
315 * Write the content of the fragment to its own internal buffer.
316 *
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.
321 *
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).
325 */
328
329 /**
330 * Write the bitstream of a fragment to the extradata in codec parameters.
331 *
332 * Modifies context and fragment as ff_cbs_write_fragment_data does and
333 * replaces any existing extradata in the structure.
334 */
338
339 /**
340 * Write the bitstream of a fragment to a packet.
341 *
342 * Modifies context and fragment as ff_cbs_write_fragment_data does.
343 *
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
347 * touched at all.
348 */
352
353
354 /**
355 * Free the units contained in a fragment as well as the fragment's
356 * own data buffer, but not the units array itself.
357 */
359
360 /**
361 * Free the units array of a fragment in addition to what
362 * ff_cbs_fragment_reset does.
363 */
365
366 /**
367 * Allocate a new internal content buffer matching the type of the unit.
368 *
369 * The content will be zeroed.
370 */
373
374 /**
375 * Insert a new unit into a fragment with the given content.
376 *
377 * The content structure continues to be owned by the caller if
378 * content_buf is not supplied.
379 */
381 int position,
383 void *content,
385
386 /**
387 * Add a new unit to a fragment with the given data bitstream.
388 *
389 * If data_buf is not supplied then data must have been allocated with
390 * av_malloc() and will on success become owned by the unit after this
391 * call or freed on error.
392 */
395 uint8_t *
data,
size_t data_size,
397
398 /**
399 * Delete a unit from a fragment and free all memory it uses.
400 *
401 * Requires position to be >= 0 and < frag->nb_units.
402 */
404 int position);
405
406
407 /**
408 * Make the content of a unit refcounted.
409 *
410 * If the unit is not refcounted, this will do a deep copy of the unit
411 * content to new refcounted buffers.
412 *
413 * It is not valid to call this function on a unit which does not have
414 * decomposed content.
415 */
418
419 /**
420 * Make the content of a unit writable so that internal fields can be
421 * modified.
422 *
423 * If it is known that there are no other references to the content of
424 * the unit, does nothing and returns success. Otherwise (including the
425 * case where the unit content is not refcounted), it does a full clone
426 * of the content (including any internal buffers) to make a new copy,
427 * and replaces the existing references inside the unit with that.
428 *
429 * It is not valid to call this function on a unit which does not have
430 * decomposed content.
431 */
434
435
436 #endif /* AVCODEC_CBS_H */