FFmpeg
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 "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;
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  */
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  */
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  */
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  */
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  */
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  */
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  */
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  */
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  */
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  */
223  uint8_t *write_buffer;
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  */
240  enum AVCodecID codec_id, void *log_ctx);
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  */
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  */
277  const struct AVCodecContext *avctx);
278 
279 /**
280  * Read the data bitstream from a packet into a fragment, then
281  * split into units and decompose.
282  *
283  * This also updates the internal state of the coded bitstream context
284  * with any persistent data from the fragment which may be required to
285  * read following fragments (e.g. parameter sets).
286  *
287  * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
288  * before use.
289  */
292  const AVPacket *pkt);
293 
294 /**
295  * Read a bitstream from a memory region into a fragment, then
296  * split into units and decompose.
297  *
298  * This also updates the internal state of the coded bitstream context
299  * with any persistent data from the fragment which may be required to
300  * read following fragments (e.g. parameter sets).
301  *
302  * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
303  * before use.
304  */
307  const uint8_t *data, size_t size);
308 
309 
310 /**
311  * Write the content of the fragment to its own internal buffer.
312  *
313  * Writes the content of all units and then assembles them into a new
314  * data buffer. When modifying the content of decomposed units, this
315  * can be used to regenerate the bitstream form of units or the whole
316  * fragment so that it can be extracted for other use.
317  *
318  * This also updates the internal state of the coded bitstream context
319  * with any persistent data from the fragment which may be required to
320  * write following fragments (e.g. parameter sets).
321  */
323  CodedBitstreamFragment *frag);
324 
325 /**
326  * Write the bitstream of a fragment to the extradata in codec parameters.
327  *
328  * Modifies context and fragment as ff_cbs_write_fragment_data does and
329  * replaces any existing extradata in the structure.
330  */
332  AVCodecParameters *par,
333  CodedBitstreamFragment *frag);
334 
335 /**
336  * Write the bitstream of a fragment to a packet.
337  *
338  * Modifies context and fragment as ff_cbs_write_fragment_data does.
339  *
340  * On success, the packet's buf is unreferenced and its buf, data and
341  * size fields are set to the corresponding values from the newly updated
342  * fragment; other fields are not touched. On failure, the packet is not
343  * touched at all.
344  */
346  AVPacket *pkt,
347  CodedBitstreamFragment *frag);
348 
349 
350 /**
351  * Free the units contained in a fragment as well as the fragment's
352  * own data buffer, but not the units array itself.
353  */
355 
356 /**
357  * Free the units array of a fragment in addition to what
358  * ff_cbs_fragment_reset does.
359  */
361 
362 /**
363  * Allocate a new internal content buffer of the given size in the unit.
364  *
365  * The content will be zeroed.
366  */
368  size_t size,
369  void (*free)(void *opaque, uint8_t *content));
370 
371 /**
372  * Allocate a new internal content buffer matching the type of the unit.
373  *
374  * The content will be zeroed.
375  */
377  CodedBitstreamUnit *unit);
378 
379 
380 /**
381  * Allocate a new internal data buffer of the given size in the unit.
382  *
383  * The data buffer will have input padding.
384  */
386  size_t size);
387 
388 /**
389  * Insert a new unit into a fragment with the given content.
390  *
391  * The content structure continues to be owned by the caller if
392  * content_buf is not supplied.
393  */
395  int position,
397  void *content,
398  AVBufferRef *content_buf);
399 
400 /**
401  * Insert a new unit into a fragment with the given data bitstream.
402  *
403  * If data_buf is not supplied then data must have been allocated with
404  * av_malloc() and will on success become owned by the unit after this
405  * call or freed on error.
406  */
408  int position,
410  uint8_t *data, size_t data_size,
411  AVBufferRef *data_buf);
412 
413 /**
414  * Delete a unit from a fragment and free all memory it uses.
415  *
416  * Requires position to be >= 0 and < frag->nb_units.
417  */
419  int position);
420 
421 
422 /**
423  * Make the content of a unit refcounted.
424  *
425  * If the unit is not refcounted, this will do a deep copy of the unit
426  * content to new refcounted buffers.
427  *
428  * It is not valid to call this function on a unit which does not have
429  * decomposed content.
430  */
432  CodedBitstreamUnit *unit);
433 
434 /**
435  * Make the content of a unit writable so that internal fields can be
436  * modified.
437  *
438  * If it is known that there are no other references to the content of
439  * the unit, does nothing and returns success. Otherwise (including the
440  * case where the unit content is not refcounted), it does a full clone
441  * of the content (including any internal buffers) to make a new copy,
442  * and replaces the existing references inside the unit with that.
443  *
444  * It is not valid to call this function on a unit which does not have
445  * decomposed content.
446  */
448  CodedBitstreamUnit *unit);
449 
450 
451 #endif /* AVCODEC_CBS_H */
CodedBitstreamContext::priv_data
void * priv_data
Internal codec-specific data.
Definition: cbs.h:194
ff_cbs_insert_unit_content
int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:738
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
ff_cbs_make_unit_refcounted
int ff_cbs_make_unit_refcounted(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit refcounted.
Definition: cbs.c:964
CodedBitstreamContext::codec
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:183
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:106
ff_cbs_alloc_unit_content2
int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
Definition: cbs.c:870
ff_cbs_init
int ff_cbs_init(CodedBitstreamContext **ctx, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:76
CodedBitstreamContext::write_buffer
uint8_t * write_buffer
Write buffer.
Definition: cbs.h:223
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:173
data
const char data[16]
Definition: mxf.c:143
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:73
ff_cbs_read_packet
int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Read the data bitstream from a packet into a fragment, then split into units and decompose.
Definition: cbs.c:289
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:69
ff_cbs_write_packet
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:420
CodedBitstreamContext::log_ctx
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:178
ff_cbs_fragment_free
void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:171
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
CodedBitstreamUnit::data
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:80
CodedBitstreamContext::trace_level
int trace_level
Log level to use for trace output.
Definition: cbs.h:217
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:167
pkt
AVPacket * pkt
Definition: movenc.c:59
codec_id.h
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:121
ff_cbs_alloc_unit_content
int ff_cbs_alloc_unit_content(CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *content))
Allocate a new internal content buffer of the given size in the unit.
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:134
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_cbs_read
int ff_cbs_read(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const uint8_t *data, size_t size)
Read a bitstream from a memory region into a fragment, then split into units and decompose.
Definition: cbs.c:297
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:369
CodedBitstreamFragment::data_bit_padding
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:138
ff_cbs_fragment_reset
void ff_cbs_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:157
CodedBitstreamUnit::data_size
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:85
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
CodedBitstreamContext::write_buffer_size
size_t write_buffer_size
Definition: cbs.h:224
size
int size
Definition: twinvq_data.h:10344
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:127
CodedBitstreamUnit::data_bit_padding
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:91
ff_cbs_write_extradata
int ff_cbs_write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
Definition: cbs.c:395
ff_cbs_read_extradata
int ff_cbs_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...
Definition: cbs.c:271
buffer.h
CodedBitstreamType
Definition: cbs_internal.h:91
ff_cbs_flush
void ff_cbs_flush(CodedBitstreamContext *ctx)
Reset all internal state in a context.
Definition: cbs.c:121
ff_cbs_delete_unit
void ff_cbs_delete_unit(CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:813
ff_cbs_close
void ff_cbs_close(CodedBitstreamContext **ctx)
Close a context and free all internal state.
Definition: cbs.c:127
CodedBitstreamUnit::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:97
packet.h
ff_cbs_all_codec_ids
enum AVCodecID ff_cbs_all_codec_ids[]
Table of all supported codec IDs.
Definition: cbs.c:54
CodedBitstreamFragment::nb_units_allocated
int nb_units_allocated
Number of allocated units.
Definition: cbs.h:159
CodedBitstreamUnit::content_ref
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing content.
Definition: cbs.h:111
AVCodecContext
main external API structure.
Definition: avcodec.h:379
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:46
ff_cbs_alloc_unit_data
int ff_cbs_alloc_unit_data(CodedBitstreamUnit *unit, size_t size)
Allocate a new internal data buffer of the given size in the unit.
Definition: cbs.c:683
CodedBitstreamContext::trace_enable
int trace_enable
Enable trace output during read/write operations.
Definition: cbs.h:211
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
ff_cbs_make_unit_writable
int ff_cbs_make_unit_writable(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Make the content of a unit writable so that internal fields can be modified.
Definition: cbs.c:1012
codec_par.h
AVPacket
This structure stores compressed data.
Definition: packet.h:350
CodedBitstreamContext::nb_decompose_unit_types
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:206
CodedBitstreamContext::decompose_unit_types
const CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
Definition: cbs.h:202
ff_cbs_insert_unit_data
int ff_cbs_insert_unit_data(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Insert a new unit into a fragment with the given data bitstream.
Definition: cbs.c:774
CodedBitstreamFragment::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:144
ff_cbs_write_fragment_data
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:359
ff_cbs_read_extradata_from_codec
int ff_cbs_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...
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:152