FFmpeg
Loading...
Searching...
No Matches
cbs.h
Go to the documentation of this file.
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 "defs.h"
30#include "packet.h"
31
32#ifndef CBS_PREFIX
33#define CBS_PREFIX cbs
34#endif
35
36#define CBS_FUNC_PREFIX_NAME(prefix, name) ff_ ## prefix ## _ ## name
37#define CBS_FUNC_NAME(prefix, name) CBS_FUNC_PREFIX_NAME(prefix, name)
38#define CBS_FUNC(name) CBS_FUNC_NAME(CBS_PREFIX, name)
39
40/*
41 * This defines a framework for converting between a coded bitstream
42 * and structures defining all individual syntax elements found in
43 * such a stream.
44 *
45 * Conversion in both directions is possible. Given a coded bitstream
46 * (any meaningful fragment), it can be parsed and decomposed into
47 * syntax elements stored in a set of codec-specific structures.
48 * Similarly, given a set of those same codec-specific structures the
49 * syntax elements can be serialised and combined to create a coded
50 * bitstream.
51 */
52
53struct AVCodecContext;
55
56/**
57 * The codec-specific type of a bitstream unit.
58 *
59 * AV1: obu_type
60 * H.264 / AVC: nal_unit_type
61 * H.265 / HEVC: nal_unit_type
62 * JPEG: marker value (without 0xff prefix)
63 * MPEG-2: start code value (without prefix)
64 * VP9: unused, set to zero (every unit is a frame)
65 */
66typedef uint32_t CodedBitstreamUnitType;
67
68/**
69 * Coded bitstream unit structure.
70 *
71 * A bitstream unit the smallest element of a bitstream which
72 * is meaningful on its own. For example, an H.264 NAL unit.
73 *
74 * See the codec-specific header for the meaning of this for any
75 * particular codec.
76 */
77typedef struct CodedBitstreamUnit {
78 /**
79 * Codec-specific type of this unit.
80 */
82
83 /**
84 * Pointer to the directly-parsable bitstream form of this unit.
85 *
86 * May be NULL if the unit currently only exists in decomposed form.
87 */
88 uint8_t *data;
89 /**
90 * The number of bytes in the bitstream (including any padding bits
91 * in the final byte).
92 */
93 size_t data_size;
94 /**
95 * The number of bits which should be ignored in the final byte.
96 *
97 * This supports non-byte-aligned bitstreams.
98 */
100 /**
101 * A reference to the buffer containing data.
102 *
103 * Must be set if data is not NULL.
104 */
106
107 /**
108 * Pointer to the decomposed form of this unit.
109 *
110 * The type of this structure depends on both the codec and the
111 * type of this unit. May be NULL if the unit only exists in
112 * bitstream form.
113 */
114 void *content;
115 /**
116 * If content is reference counted, a RefStruct reference backing content.
117 * NULL if content is not reference counted.
118 */
121
122/**
123 * Coded bitstream fragment structure, combining one or more units.
124 *
125 * This is any sequence of units. It need not form some greater whole,
126 * though in many cases it will. For example, an H.264 access unit,
127 * which is composed of a sequence of H.264 NAL units.
128 */
130 /**
131 * Pointer to the bitstream form of this fragment.
132 *
133 * May be NULL if the fragment only exists as component units.
134 */
135 uint8_t *data;
136 /**
137 * The number of bytes in the bitstream.
138 *
139 * The number of bytes in the bitstream (including any padding bits
140 * in the final byte).
141 */
142 size_t data_size;
143 /**
144 * The number of bits which should be ignored in the final byte.
145 */
147 /**
148 * A reference to the buffer containing data.
149 *
150 * Must be set if data is not NULL.
151 */
153
154 /**
155 * Number of units in this fragment.
156 *
157 * This may be zero if the fragment only exists in bitstream form
158 * and has not been decomposed.
159 */
161
162 /**
163 * Number of allocated units.
164 *
165 * Must always be >= nb_units; designed for internal use by cbs.
166 */
168
169 /**
170 * Pointer to an array of units of length nb_units_allocated.
171 * Only the first nb_units are valid.
172 *
173 * Must be NULL if nb_units_allocated is zero.
174 */
177
178
180struct GetBitContext;
181struct PutBitContext;
182
183/**
184 * Callback type for read tracing.
185 *
186 * @param ctx User-set trace context.
187 * @param gbc A GetBitContext set at the start of the syntax
188 * element. This is a copy, the callee does not
189 * need to preserve it.
190 * @param length Length in bits of the syntax element.
191 * @param name String name of the syntax elements.
192 * @param subscripts If the syntax element is an array, a pointer to
193 * an array of subscripts into the array.
194 * @param value Parsed value of the syntax element.
195 */
196typedef void (*CBSTraceReadCallback)(void *trace_context,
197 struct GetBitContext *gbc,
198 int start_position,
199 const char *name,
200 const int *subscripts,
201 int64_t value);
202
203/**
204 * Callback type for write tracing.
205 *
206 * @param ctx User-set trace context.
207 * @param pbc A PutBitContext set at the end of the syntax
208 * element. The user must not modify this, but may
209 * inspect it to determine state.
210 * @param length Length in bits of the syntax element.
211 * @param name String name of the syntax elements.
212 * @param subscripts If the syntax element is an array, a pointer to
213 * an array of subscripts into the array.
214 * @param value Written value of the syntax element.
215 */
216typedef void (*CBSTraceWriteCallback)(void *trace_context,
217 struct PutBitContext *pbc,
218 int start_position,
219 const char *name,
220 const int *subscripts,
221 int64_t value);
222
223/**
224 * Context structure for coded bitstream operations.
225 */
226typedef struct CodedBitstreamContext {
227 /**
228 * Logging context to be passed to all av_log() calls associated
229 * with this context.
230 */
231 void *log_ctx;
232
233 /**
234 * Internal codec-specific hooks.
235 */
237
238 /**
239 * Internal codec-specific data.
240 *
241 * This contains any information needed when reading/writing
242 * bitsteams which will not necessarily be present in a fragment.
243 * For example, for H.264 it contains all currently visible
244 * parameter sets - they are required to determine the bitstream
245 * syntax but need not be present in every access unit.
246 */
248
249 /**
250 * Array of unit types which should be decomposed when reading.
251 *
252 * Types not in this list will be available in bitstream form only.
253 * If NULL, all supported types will be decomposed.
254 */
256 /**
257 * Length of the decompose_unit_types array.
258 */
260
261 /**
262 * Enable trace output during read/write operations.
263 */
265 /**
266 * Log level to use for default trace output.
267 *
268 * From AV_LOG_*; defaults to AV_LOG_TRACE.
269 */
271 /**
272 * User context pointer to pass to trace callbacks.
273 */
275 /**
276 * Callback for read tracing.
277 *
278 * If tracing is enabled then this is called once for each syntax
279 * element parsed.
280 */
282 /**
283 * Callback for write tracing.
284 *
285 * If tracing is enabled then this is called once for each syntax
286 * element written.
287 */
289
290 /**
291 * Write buffer. Used as intermediate buffer when writing units.
292 * For internal use of cbs only.
293 */
294 uint8_t *write_buffer;
297
298
299/**
300 * Create and initialise a new context for the given codec.
301 */
303 enum AVCodecID codec_id, void *log_ctx);
304
305/**
306 * Reset all internal state in a context.
307 */
309
310/**
311 * Close a context and free all internal state.
312 */
314
315
316/**
317 * Read the extradata bitstream found in codec parameters into a
318 * fragment, then split into units and decompose.
319 *
320 * This also updates the internal state, so will need to be called for
321 * codecs with extradata to read parameter sets necessary for further
322 * parsing even if the fragment itself is not desired.
323 *
324 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
325 * before use.
326 */
329 const AVCodecParameters *par);
330
331/**
332 * Read the extradata bitstream found in a codec context into a
333 * fragment, then split into units and decompose.
334 *
335 * This acts identical to ff_cbs_read_extradata() for the case where
336 * you already have a codec context.
337 */
340 const struct AVCodecContext *avctx);
341
344 const AVPacket *pkt);
345
346/**
347 * Read the data bitstream from a packet into a fragment, then
348 * split into units and decompose.
349 *
350 * This also updates the internal state of the coded bitstream context
351 * with any persistent data from the fragment which may be required to
352 * read following fragments (e.g. parameter sets).
353 *
354 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
355 * before use.
356 */
359 const AVPacket *pkt);
360
361/**
362 * Read a bitstream from a memory region into a fragment, then
363 * split into units and decompose.
364 *
365 * This also updates the internal state of the coded bitstream context
366 * with any persistent data from the fragment which may be required to
367 * read following fragments (e.g. parameter sets).
368 *
369 * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
370 * before use.
371 */
374 const AVBufferRef *buf,
375 const uint8_t *data, size_t size);
376
377
378/**
379 * Write the content of the fragment to its own internal buffer.
380 *
381 * Writes the content of all units and then assembles them into a new
382 * data buffer. When modifying the content of decomposed units, this
383 * can be used to regenerate the bitstream form of units or the whole
384 * fragment so that it can be extracted for other use.
385 *
386 * This also updates the internal state of the coded bitstream context
387 * with any persistent data from the fragment which may be required to
388 * write following fragments (e.g. parameter sets).
389 */
392
393/**
394 * Write the bitstream of a fragment to the extradata in codec parameters.
395 *
396 * Modifies context and fragment as ff_cbs_write_fragment_data does and
397 * replaces any existing extradata in the structure.
398 */
402
403/**
404 * Write the bitstream of a fragment to a packet.
405 *
406 * Modifies context and fragment as ff_cbs_write_fragment_data does.
407 *
408 * On success, the packet's buf is unreferenced and its buf, data and
409 * size fields are set to the corresponding values from the newly updated
410 * fragment; other fields are not touched. On failure, the packet is not
411 * touched at all.
412 */
414 AVPacket *pkt,
416
417
418/**
419 * Free the units contained in a fragment as well as the fragment's
420 * own data buffer, but not the units array itself.
421 */
423
424/**
425 * Free the units array of a fragment in addition to what
426 * ff_cbs_fragment_reset does.
427 */
429
430/**
431 * Allocate a new internal content buffer matching the type of the unit.
432 *
433 * The content will be zeroed.
434 */
436 CodedBitstreamUnit *unit);
437
438/**
439 * Insert a new unit into a fragment with the given content.
440 *
441 * If content_ref is supplied, it has to be a RefStruct reference
442 * backing content; the user keeps ownership of the supplied reference.
443 * The content structure continues to be owned by the caller if
444 * content_ref is not supplied.
445 */
447 int position,
449 void *content,
450 void *content_ref);
451
452/**
453 * Add a new unit to a fragment with the given data bitstream.
454 *
455 * If data_buf is not supplied then data must have been allocated with
456 * av_malloc() and will on success become owned by the unit after this
457 * call or freed on error.
458 */
461 uint8_t *data, size_t data_size,
462 AVBufferRef *data_buf);
463
464/**
465 * Delete a unit from a fragment and free all memory it uses.
466 *
467 * Requires position to be >= 0 and < frag->nb_units.
468 */
470 int position);
471
472
473/**
474 * Make the content of a unit refcounted.
475 *
476 * If the unit is not refcounted, this will do a deep copy of the unit
477 * content to new refcounted buffers.
478 *
479 * It is not valid to call this function on a unit which does not have
480 * decomposed content.
481 */
483 CodedBitstreamUnit *unit);
484
485/**
486 * Make the content of a unit writable so that internal fields can be
487 * modified.
488 *
489 * If it is known that there are no other references to the content of
490 * the unit, does nothing and returns success. Otherwise (including the
491 * case where the unit content is not refcounted), it does a full clone
492 * of the content (including any internal buffers) to make a new copy,
493 * and replaces the existing references inside the unit with that.
494 *
495 * It is not valid to call this function on a unit which does not have
496 * decomposed content.
497 */
499 CodedBitstreamUnit *unit);
500
503
504 /**
505 * keep non-vcl units even if the picture has been dropped.
506 */
508};
509
510/**
511 * Discard units according to 'skip'.
512 */
515 enum AVDiscard skip,
516 int flags);
517
518
519/**
520 * Helper function for read tracing which formats the syntax element
521 * and logs the result.
522 *
523 * Trace context should be set to the CodedBitstreamContext.
524 */
525void CBS_FUNC(trace_read_log)(void *trace_context,
526 struct GetBitContext *gbc, int length,
527 const char *str, const int *subscripts,
528 int64_t value);
529
530/**
531 * Helper function for write tracing which formats the syntax element
532 * and logs the result.
533 *
534 * Trace context should be set to the CodedBitstreamContext.
535 */
536void CBS_FUNC(trace_write_log)(void *trace_context,
537 struct PutBitContext *pbc, int length,
538 const char *str, const int *subscripts,
539 int64_t value);
540
541#endif /* AVCODEC_CBS_H */
static AVFormatContext * ctx
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
refcounted data buffer API
#define flags(name, subs,...)
Definition cbs_h264.c:74
long long int64_t
Definition coverity.c:34
Misc types and constants that do not belong anywhere else.
static AVPacket * pkt
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:610
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
double value
Definition eval.c:102
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition ffmpeg_mux.c:204
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
AVDiscard
Definition defs.h:223
cl_device_type type
int CBS_FUNC insert_unit_content(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, void *content_ref)
Insert a new unit into a fragment with the given content.
Definition cbs.c:771
void CBS_FUNC trace_read_log(void *trace_context, GetBitContext *gbc, int length, const char *str, const int *subscripts, int64_t value)
Helper function for read tracing which formats the syntax element and logs the result.
Definition cbs.c:492
void CBS_FUNC discard_units(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, enum AVDiscard skip, int flags)
Discard units according to 'skip'.
Definition cbs.c:1049
av_cold void CBS_FUNC fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition cbs.c:162
void CBS_FUNC delete_unit(CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition cbs.c:848
int CBS_FUNC alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
Definition cbs.c:911
int CBS_FUNC make_unit_writable(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit writable so that internal fields can be modified.
Definition cbs.c:1032
void CBS_FUNC trace_write_log(void *trace_context, PutBitContext *pbc, int length, const char *str, const int *subscripts, int64_t value)
Helper function for write tracing which formats the syntax element and logs the result.
Definition cbs.c:552
int CBS_FUNC append_unit_data(CodedBitstreamFragment *frag, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Add a new unit to a fragment with the given data bitstream.
Definition cbs.c:838
void CBS_FUNC fragment_reset(CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment's own data buffer, but not the units a...
Definition cbs.c:148
int CBS_FUNC make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
Definition cbs.c:1023
int CBS_FUNC read_extradata_from_codec(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const struct AVCodecContext *avctx)
Read the extradata bitstream found in a codec context into a fragment, then split into units and deco...
int CBS_FUNC write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
int CBS_FUNC read_packet_side_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
CbsDiscardFlags
Definition cbs.h:501
@ DISCARD_FLAG_KEEP_NON_VCL
keep non-vcl units even if the picture has been dropped.
Definition cbs.h:507
@ DISCARD_FLAG_NONE
Definition cbs.h:502
int CBS_FUNC write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
void(* CBSTraceReadCallback)(void *trace_context, struct GetBitContext *gbc, int start_position, const char *name, const int *subscripts, int64_t value)
Callback type for read tracing.
Definition cbs.h:196
int CBS_FUNC read_extradata(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecParameters *par)
Read the extradata bitstream found in codec parameters into a fragment, then split into units and dec...
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition cbs.h:66
#define CBS_FUNC(name)
Definition cbs.h:38
void(* CBSTraceWriteCallback)(void *trace_context, struct PutBitContext *pbc, int start_position, const char *name, const int *subscripts, int64_t value)
Callback type for write tracing.
Definition cbs.h:216
const char data[16]
Definition mxf.c:149
const char * name
Definition qsvenc.c:142
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
This structure stores compressed data.
Definition packet.h:580
Context structure for coded bitstream operations.
Definition cbs.h:226
int trace_enable
Enable trace output during read/write operations.
Definition cbs.h:264
CBSTraceReadCallback trace_read_callback
Callback for read tracing.
Definition cbs.h:281
const CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
Definition cbs.h:255
uint8_t * write_buffer
Write buffer.
Definition cbs.h:294
void * trace_context
User context pointer to pass to trace callbacks.
Definition cbs.h:274
int trace_level
Log level to use for default trace output.
Definition cbs.h:270
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition cbs.h:236
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition cbs.h:231
void * priv_data
Internal codec-specific data.
Definition cbs.h:247
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition cbs.h:259
CBSTraceWriteCallback trace_write_callback
Callback for write tracing.
Definition cbs.h:288
size_t write_buffer_size
Definition cbs.h:295
Coded bitstream fragment structure, combining one or more units.
Definition cbs.h:129
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition cbs.h:175
int nb_units
Number of units in this fragment.
Definition cbs.h:160
int nb_units_allocated
Number of allocated units.
Definition cbs.h:167
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition cbs.h:146
size_t data_size
The number of bytes in the bitstream.
Definition cbs.h:142
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition cbs.h:135
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition cbs.h:152
Coded bitstream unit structure.
Definition cbs.h:77
void * content
Pointer to the decomposed form of this unit.
Definition cbs.h:114
void * content_ref
If content is reference counted, a RefStruct reference backing content.
Definition cbs.h:119
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition cbs.h:88
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition cbs.h:99
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition cbs.h:81
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition cbs.h:93
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition cbs.h:105
int size
enum AVCodecID codec_id