Update for 5.1.8
[ffmpeg.git] / libavcodec / cbs.h
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
25 #include "libavutil/buffer.h"
26
27 #include "codec_id.h"
28 #include "codec_par.h"
29 #include "packet.h"
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
45 struct AVCodecContext;
46 struct CodedBitstreamType;
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 */
58 typedef uint32_t CodedBitstreamUnitType;
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 */
69 typedef struct CodedBitstreamUnit {
70 /**
71 * Codec-specific type of this unit.
72 */
73 CodedBitstreamUnitType type;
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 */
80 uint8_t *data;
81 /**
82 * The number of bytes in the bitstream (including any padding bits
83 * in the final byte).
84 */
85 size_t data_size;
86 /**
87 * The number of bits which should be ignored in the final byte.
88 *
89 * This supports non-byte-aligned bitstreams.
90 */
91 size_t data_bit_padding;
92 /**
93 * A reference to the buffer containing data.
94 *
95 * Must be set if data is not NULL.
96 */
97 AVBufferRef *data_ref;
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 */
106 void *content;
107 /**
108 * If content is reference counted, a reference to the buffer containing
109 * content. Null if content is not reference counted.
110 */
111 AVBufferRef *content_ref;
112 } CodedBitstreamUnit;
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 */
121 typedef struct CodedBitstreamFragment {
122 /**
123 * Pointer to the bitstream form of this fragment.
124 *
125 * May be NULL if the fragment only exists as component units.
126 */
127 uint8_t *data;
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 */
134 size_t data_size;
135 /**
136 * The number of bits which should be ignored in the final byte.
137 */
138 size_t data_bit_padding;
139 /**
140 * A reference to the buffer containing data.
141 *
142 * Must be set if data is not NULL.
143 */
144 AVBufferRef *data_ref;
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 */
152 int nb_units;
153
154 /**
155 * Number of allocated units.
156 *
157 * Must always be >= nb_units; designed for internal use by cbs.
158 */
159 int nb_units_allocated;
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 */
167 CodedBitstreamUnit *units;
168 } CodedBitstreamFragment;
169
170 /**
171 * Context structure for coded bitstream operations.
172 */
173 typedef struct CodedBitstreamContext {
174 /**
175 * Logging context to be passed to all av_log() calls associated
176 * with this context.
177 */
178 void *log_ctx;
179
180 /**
181 * Internal codec-specific hooks.
182 */
183 const struct CodedBitstreamType *codec;
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 */
194 void *priv_data;
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 */
202 const CodedBitstreamUnitType *decompose_unit_types;
203 /**
204 * Length of the decompose_unit_types array.
205 */
206 int nb_decompose_unit_types;
207
208 /**
209 * Enable trace output during read/write operations.
210 */
211 int trace_enable;
212 /**
213 * Log level to use for trace output.
214 *
215 * From AV_LOG_*; defaults to AV_LOG_TRACE.
216 */
217 int trace_level;
218
219 /**
220 * Write buffer. Used as intermediate buffer when writing units.
221 * For internal use of cbs only.
222 */
223 uint8_t *write_buffer;
224 size_t write_buffer_size;
225 } CodedBitstreamContext;
226
227
228 /**
229 * Table of all supported codec IDs.
230 *
231 * Terminated by AV_CODEC_ID_NONE.
232 */
233 extern const enum AVCodecID ff_cbs_all_codec_ids[];
234
235
236 /**
237 * Create and initialise a new context for the given codec.
238 */
239 int ff_cbs_init(CodedBitstreamContext **ctx,
240 enum AVCodecID codec_id, void *log_ctx);
241
242 /**
243 * Reset all internal state in a context.
244 */
245 void ff_cbs_flush(CodedBitstreamContext *ctx);
246
247 /**
248 * Close a context and free all internal state.
249 */
250 void ff_cbs_close(CodedBitstreamContext **ctx);
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 */
264 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
265 CodedBitstreamFragment *frag,
266 const AVCodecParameters *par);
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 */
275 int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx,
276 CodedBitstreamFragment *frag,
277 const struct AVCodecContext *avctx);
278
279 int ff_cbs_read_packet_side_data(CodedBitstreamContext *ctx,
280 CodedBitstreamFragment *frag,
281 const AVPacket *pkt);
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 */
294 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
295 CodedBitstreamFragment *frag,
296 const AVPacket *pkt);
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 */
309 int ff_cbs_read(CodedBitstreamContext *ctx,
310 CodedBitstreamFragment *frag,
311 const uint8_t *data, size_t size);
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 */
326 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
327 CodedBitstreamFragment *frag);
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 */
335 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
336 AVCodecParameters *par,
337 CodedBitstreamFragment *frag);
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 */
349 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
350 AVPacket *pkt,
351 CodedBitstreamFragment *frag);
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 */
358 void ff_cbs_fragment_reset(CodedBitstreamFragment *frag);
359
360 /**
361 * Free the units array of a fragment in addition to what
362 * ff_cbs_fragment_reset does.
363 */
364 void ff_cbs_fragment_free(CodedBitstreamFragment *frag);
365
366 /**
367 * Allocate a new internal content buffer of the given size in the unit.
368 *
369 * The content will be zeroed.
370 */
371 int ff_cbs_alloc_unit_content(CodedBitstreamUnit *unit,
372 size_t size,
373 void (*free)(void *opaque, uint8_t *content));
374
375 /**
376 * Allocate a new internal content buffer matching the type of the unit.
377 *
378 * The content will be zeroed.
379 */
380 int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx,
381 CodedBitstreamUnit *unit);
382
383 /**
384 * Insert a new unit into a fragment with the given content.
385 *
386 * The content structure continues to be owned by the caller if
387 * content_buf is not supplied.
388 */
389 int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag,
390 int position,
391 CodedBitstreamUnitType type,
392 void *content,
393 AVBufferRef *content_buf);
394
395 /**
396 * Add a new unit to a fragment with the given data bitstream.
397 *
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.
401 */
402 int ff_cbs_append_unit_data(CodedBitstreamFragment *frag,
403 CodedBitstreamUnitType type,
404 uint8_t *data, size_t data_size,
405 AVBufferRef *data_buf);
406
407 /**
408 * Delete a unit from a fragment and free all memory it uses.
409 *
410 * Requires position to be >= 0 and < frag->nb_units.
411 */
412 void ff_cbs_delete_unit(CodedBitstreamFragment *frag,
413 int position);
414
415
416 /**
417 * Make the content of a unit refcounted.
418 *
419 * If the unit is not refcounted, this will do a deep copy of the unit
420 * content to new refcounted buffers.
421 *
422 * It is not valid to call this function on a unit which does not have
423 * decomposed content.
424 */
425 int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx,
426 CodedBitstreamUnit *unit);
427
428 /**
429 * Make the content of a unit writable so that internal fields can be
430 * modified.
431 *
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.
437 *
438 * It is not valid to call this function on a unit which does not have
439 * decomposed content.
440 */
441 int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx,
442 CodedBitstreamUnit *unit);
443
444
445 #endif /* AVCODEC_CBS_H */