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 "avcodec.h"
28 
29 
30 /*
31  * This defines a framework for converting between a coded bitstream
32  * and structures defining all individual syntax elements found in
33  * such a stream.
34  *
35  * Conversion in both directions is possible. Given a coded bitstream
36  * (any meaningful fragment), it can be parsed and decomposed into
37  * syntax elements stored in a set of codec-specific structures.
38  * Similarly, given a set of those same codec-specific structures the
39  * syntax elements can be serialised and combined to create a coded
40  * bitstream.
41  */
42 
44 
45 /**
46  * The codec-specific type of a bitstream unit.
47  *
48  * AV1: obu_type
49  * H.264 / AVC: nal_unit_type
50  * H.265 / HEVC: nal_unit_type
51  * JPEG: marker value (without 0xff prefix)
52  * MPEG-2: start code value (without prefix)
53  * VP9: unused, set to zero (every unit is a frame)
54  */
55 typedef uint32_t CodedBitstreamUnitType;
56 
57 /**
58  * Coded bitstream unit structure.
59  *
60  * A bitstream unit the smallest element of a bitstream which
61  * is meaningful on its own. For example, an H.264 NAL unit.
62  *
63  * See the codec-specific header for the meaning of this for any
64  * particular codec.
65  */
66 typedef struct CodedBitstreamUnit {
67  /**
68  * Codec-specific type of this unit.
69  */
71 
72  /**
73  * Pointer to the directly-parsable bitstream form of this unit.
74  *
75  * May be NULL if the unit currently only exists in decomposed form.
76  */
78  /**
79  * The number of bytes in the bitstream (including any padding bits
80  * in the final byte).
81  */
82  size_t data_size;
83  /**
84  * The number of bits which should be ignored in the final byte.
85  *
86  * This supports non-byte-aligned bitstreams.
87  */
89  /**
90  * A reference to the buffer containing data.
91  *
92  * Must be set if data is not NULL.
93  */
95 
96  /**
97  * Pointer to the decomposed form of this unit.
98  *
99  * The type of this structure depends on both the codec and the
100  * type of this unit. May be NULL if the unit only exists in
101  * bitstream form.
102  */
103  void *content;
104  /**
105  * If content is reference counted, a reference to the buffer containing
106  * content. Null if content is not reference counted.
107  */
110 
111 /**
112  * Coded bitstream fragment structure, combining one or more units.
113  *
114  * This is any sequence of units. It need not form some greater whole,
115  * though in many cases it will. For example, an H.264 access unit,
116  * which is composed of a sequence of H.264 NAL units.
117  */
118 typedef struct CodedBitstreamFragment {
119  /**
120  * Pointer to the bitstream form of this fragment.
121  *
122  * May be NULL if the fragment only exists as component units.
123  */
125  /**
126  * The number of bytes in the bitstream.
127  *
128  * The number of bytes in the bitstream (including any padding bits
129  * in the final byte).
130  */
131  size_t data_size;
132  /**
133  * The number of bits which should be ignored in the final byte.
134  */
136  /**
137  * A reference to the buffer containing data.
138  *
139  * Must be set if data is not NULL.
140  */
142 
143  /**
144  * Number of units in this fragment.
145  *
146  * This may be zero if the fragment only exists in bitstream form
147  * and has not been decomposed.
148  */
149  int nb_units;
150 
151  /**
152  * Number of allocated units.
153  *
154  * Must always be >= nb_units; designed for internal use by cbs.
155  */
157 
158  /**
159  * Pointer to an array of units of length nb_units_allocated.
160  * Only the first nb_units are valid.
161  *
162  * Must be NULL if nb_units_allocated is zero.
163  */
166 
167 /**
168  * Context structure for coded bitstream operations.
169  */
170 typedef struct CodedBitstreamContext {
171  /**
172  * Logging context to be passed to all av_log() calls associated
173  * with this context.
174  */
175  void *log_ctx;
176 
177  /**
178  * Internal codec-specific hooks.
179  */
180  const struct CodedBitstreamType *codec;
181 
182  /**
183  * Internal codec-specific data.
184  *
185  * This contains any information needed when reading/writing
186  * bitsteams which will not necessarily be present in a fragment.
187  * For example, for H.264 it contains all currently visible
188  * parameter sets - they are required to determine the bitstream
189  * syntax but need not be present in every access unit.
190  */
191  void *priv_data;
192 
193  /**
194  * Array of unit types which should be decomposed when reading.
195  *
196  * Types not in this list will be available in bitstream form only.
197  * If NULL, all supported types will be decomposed.
198  */
200  /**
201  * Length of the decompose_unit_types array.
202  */
204 
205  /**
206  * Enable trace output during read/write operations.
207  */
209  /**
210  * Log level to use for trace output.
211  *
212  * From AV_LOG_*; defaults to AV_LOG_TRACE.
213  */
215 
216  /**
217  * Write buffer. Used as intermediate buffer when writing units.
218  * For internal use of cbs only.
219  */
223 
224 
225 /**
226  * Table of all supported codec IDs.
227  *
228  * Terminated by AV_CODEC_ID_NONE.
229  */
230 extern const enum AVCodecID ff_cbs_all_codec_ids[];
231 
232 
233 /**
234  * Create and initialise a new context for the given codec.
235  */
237  enum AVCodecID codec_id, void *log_ctx);
238 
239 /**
240  * Reset all internal state in a context.
241  */
243 
244 /**
245  * Close a context and free all internal state.
246  */
248 
249 
250 /**
251  * Read the extradata bitstream found in codec parameters into a
252  * fragment, then split into units and decompose.
253  *
254  * This also updates the internal state, so will need to be called for
255  * codecs with extradata to read parameter sets necessary for further
256  * parsing even if the fragment itself is not desired.
257  *
258  * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
259  * before use.
260  */
263  const AVCodecParameters *par);
264 
265 /**
266  * Read the extradata bitstream found in a codec context into a
267  * fragment, then split into units and decompose.
268  *
269  * This acts identical to ff_cbs_read_extradata() for the case where
270  * you already have a codec context.
271  */
274  const AVCodecContext *avctx);
275 
276 /**
277  * Read the data bitstream from a packet into a fragment, then
278  * split into units and decompose.
279  *
280  * This also updates the internal state of the coded bitstream context
281  * with any persistent data from the fragment which may be required to
282  * read following fragments (e.g. parameter sets).
283  *
284  * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
285  * before use.
286  */
289  const AVPacket *pkt);
290 
291 /**
292  * Read a bitstream from a memory region into a fragment, then
293  * split into units and decompose.
294  *
295  * This also updates the internal state of the coded bitstream context
296  * with any persistent data from the fragment which may be required to
297  * read following fragments (e.g. parameter sets).
298  *
299  * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
300  * before use.
301  */
304  const uint8_t *data, size_t size);
305 
306 
307 /**
308  * Write the content of the fragment to its own internal buffer.
309  *
310  * Writes the content of all units and then assembles them into a new
311  * data buffer. When modifying the content of decomposed units, this
312  * can be used to regenerate the bitstream form of units or the whole
313  * fragment so that it can be extracted for other use.
314  *
315  * This also updates the internal state of the coded bitstream context
316  * with any persistent data from the fragment which may be required to
317  * write following fragments (e.g. parameter sets).
318  */
320  CodedBitstreamFragment *frag);
321 
322 /**
323  * Write the bitstream of a fragment to the extradata in codec parameters.
324  *
325  * Modifies context and fragment as ff_cbs_write_fragment_data does and
326  * replaces any existing extradata in the structure.
327  */
329  AVCodecParameters *par,
330  CodedBitstreamFragment *frag);
331 
332 /**
333  * Write the bitstream of a fragment to a packet.
334  *
335  * Modifies context and fragment as ff_cbs_write_fragment_data does.
336  *
337  * On success, the packet's buf is unreferenced and its buf, data and
338  * size fields are set to the corresponding values from the newly updated
339  * fragment; other fields are not touched. On failure, the packet is not
340  * touched at all.
341  */
343  AVPacket *pkt,
344  CodedBitstreamFragment *frag);
345 
346 
347 /**
348  * Free the units contained in a fragment as well as the fragment's
349  * own data buffer, but not the units array itself.
350  */
352 
353 /**
354  * Free the units array of a fragment in addition to what
355  * ff_cbs_fragment_reset does.
356  */
358 
359 /**
360  * Allocate a new internal content buffer of the given size in the unit.
361  *
362  * The content will be zeroed.
363  */
365  size_t size,
366  void (*free)(void *opaque, uint8_t *content));
367 
368 /**
369  * Allocate a new internal content buffer matching the type of the unit.
370  *
371  * The content will be zeroed.
372  */
374  CodedBitstreamUnit *unit);
375 
376 
377 /**
378  * Allocate a new internal data buffer of the given size in the unit.
379  *
380  * The data buffer will have input padding.
381  */
383  size_t size);
384 
385 /**
386  * Insert a new unit into a fragment with the given content.
387  *
388  * The content structure continues to be owned by the caller if
389  * content_buf is not supplied.
390  */
392  int position,
394  void *content,
395  AVBufferRef *content_buf);
396 
397 /**
398  * Insert a new unit into a fragment with the given data bitstream.
399  *
400  * If data_buf is not supplied then data must have been allocated with
401  * av_malloc() and will on success become owned by the unit after this
402  * call or freed on error.
403  */
405  int position,
407  uint8_t *data, size_t data_size,
408  AVBufferRef *data_buf);
409 
410 /**
411  * Delete a unit from a fragment and free all memory it uses.
412  *
413  * Requires position to be >= 0 and < frag->nb_units.
414  */
416  int position);
417 
418 
419 /**
420  * Make the content of a unit refcounted.
421  *
422  * If the unit is not refcounted, this will do a deep copy of the unit
423  * content to new refcounted buffers.
424  *
425  * It is not valid to call this function on a unit which does not have
426  * decomposed content.
427  */
429  CodedBitstreamUnit *unit);
430 
431 /**
432  * Make the content of a unit writable so that internal fields can be
433  * modified.
434  *
435  * If it is known that there are no other references to the content of
436  * the unit, does nothing and returns success. Otherwise (including the
437  * case where the unit content is not refcounted), it does a full clone
438  * of the content (including any internal buffers) to make a new copy,
439  * and replaces the existing references inside the unit with that.
440  *
441  * It is not valid to call this function on a unit which does not have
442  * decomposed content.
443  */
445  CodedBitstreamUnit *unit);
446 
447 
448 #endif /* AVCODEC_CBS_H */
CodedBitstreamContext::priv_data
void * priv_data
Internal codec-specific data.
Definition: cbs.h:191
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:737
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:963
CodedBitstreamContext::codec
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:180
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:103
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:869
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:75
CodedBitstreamContext::write_buffer
uint8_t * write_buffer
Write buffer.
Definition: cbs.h:220
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:170
data
const char data[16]
Definition: mxf.c:142
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:70
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:288
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:66
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:419
CodedBitstreamContext::log_ctx
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:175
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:170
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:77
CodedBitstreamContext::trace_level
int trace_level
Log level to use for trace output.
Definition: cbs.h:214
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:164
pkt
AVPacket * pkt
Definition: movenc.c:59
ff_cbs_read_extradata_from_codec
int ff_cbs_read_extradata_from_codec(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecContext *avctx)
Read the extradata bitstream found in a codec context into a fragment, then split into units and deco...
Definition: cbs.c:279
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:118
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:131
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:296
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:135
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:156
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:82
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
CodedBitstreamContext::write_buffer_size
size_t write_buffer_size
Definition: cbs.h:221
size
int size
Definition: twinvq_data.h:10344
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:124
CodedBitstreamUnit::data_bit_padding
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:88
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:394
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:270
buffer.h
CodedBitstreamType
Definition: cbs_internal.h:86
ff_cbs_flush
void ff_cbs_flush(CodedBitstreamContext *ctx)
Reset all internal state in a context.
Definition: cbs.c:120
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:812
ff_cbs_close
void ff_cbs_close(CodedBitstreamContext **ctx)
Close a context and free all internal state.
Definition: cbs.c:126
CodedBitstreamUnit::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:94
ff_cbs_all_codec_ids
enum AVCodecID ff_cbs_all_codec_ids[]
Table of all supported codec IDs.
Definition: cbs.c:53
uint8_t
uint8_t
Definition: audio_convert.c:194
CodedBitstreamFragment::nb_units_allocated
int nb_units_allocated
Number of allocated units.
Definition: cbs.h:156
avcodec.h
CodedBitstreamUnit::content_ref
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing content.
Definition: cbs.h:108
AVCodecContext
main external API structure.
Definition: avcodec.h:536
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:43
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:682
CodedBitstreamContext::trace_enable
int trace_enable
Enable trace output during read/write operations.
Definition: cbs.h:208
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:84
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:1011
AVPacket
This structure stores compressed data.
Definition: packet.h:346
CodedBitstreamContext::nb_decompose_unit_types
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:203
CodedBitstreamContext::decompose_unit_types
const CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
Definition: cbs.h:199
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:773
CodedBitstreamFragment::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:141
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:358
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:149