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 
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  */
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  */
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  */
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  */
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  */
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  */
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  */
372  CodedBitstreamUnit *unit);
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,
384  AVBufferRef *content_buf);
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,
396  AVBufferRef *data_buf);
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  */
417  CodedBitstreamUnit *unit);
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  */
433  CodedBitstreamUnit *unit);
434 
435 
436 #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:740
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:54
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:1004
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_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
ff_cbs_read_packet_side_data
int ff_cbs_read_packet_side_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Definition: cbs.c:297
data
const char data[16]
Definition: mxf.c:146
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:459
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
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:310
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:388
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
ff_cbs_append_unit_data
int ff_cbs_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:813
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
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:430
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:101
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:823
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:426
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:46
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:1013
codec_par.h
AVPacket
This structure stores compressed data.
Definition: packet.h:351
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_alloc_unit_content
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Allocate a new internal content buffer matching the type of the unit.
Definition: cbs.c:878
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:394
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