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  * H.264 / AVC: nal_unit_type
49  * H.265 / HEVC: nal_unit_type
50  * MPEG-2: start code value (without prefix)
51  * VP9: unused, set to zero (every unit is a frame)
52  */
53 typedef uint32_t CodedBitstreamUnitType;
54 
55 /**
56  * Coded bitstream unit structure.
57  *
58  * A bitstream unit the smallest element of a bitstream which
59  * is meaningful on its own. For example, an H.264 NAL unit.
60  *
61  * See the codec-specific header for the meaning of this for any
62  * particular codec.
63  */
64 typedef struct CodedBitstreamUnit {
65  /**
66  * Codec-specific type of this unit.
67  */
69 
70  /**
71  * Pointer to the directly-parsable bitstream form of this unit.
72  *
73  * May be NULL if the unit currently only exists in decomposed form.
74  */
76  /**
77  * The number of bytes in the bitstream (including any padding bits
78  * in the final byte).
79  */
80  size_t data_size;
81  /**
82  * The number of bits which should be ignored in the final byte.
83  *
84  * This supports non-byte-aligned bitstreams.
85  */
87  /**
88  * A reference to the buffer containing data.
89  *
90  * Must be set if data is not NULL.
91  */
93 
94  /**
95  * Pointer to the decomposed form of this unit.
96  *
97  * The type of this structure depends on both the codec and the
98  * type of this unit. May be NULL if the unit only exists in
99  * bitstream form.
100  */
101  void *content;
102  /**
103  * If content is reference counted, a reference to the buffer containing
104  * content. Null if content is not reference counted.
105  */
108 
109 /**
110  * Coded bitstream fragment structure, combining one or more units.
111  *
112  * This is any sequence of units. It need not form some greater whole,
113  * though in many cases it will. For example, an H.264 access unit,
114  * which is composed of a sequence of H.264 NAL units.
115  */
116 typedef struct CodedBitstreamFragment {
117  /**
118  * Pointer to the bitstream form of this fragment.
119  *
120  * May be NULL if the fragment only exists as component units.
121  */
123  /**
124  * The number of bytes in the bitstream.
125  *
126  * The number of bytes in the bitstream (including any padding bits
127  * in the final byte).
128  */
129  size_t data_size;
130  /**
131  * The number of bits which should be ignored in the final byte.
132  */
134  /**
135  * A reference to the buffer containing data.
136  *
137  * Must be set if data is not NULL.
138  */
140 
141  /**
142  * Number of units in this fragment.
143  *
144  * This may be zero if the fragment only exists in bitstream form
145  * and has not been decomposed.
146  */
147  int nb_units;
148 
149  /**
150  * Number of allocated units.
151  *
152  * Must always be >= nb_units; designed for internal use by cbs.
153  */
155 
156  /**
157  * Pointer to an array of units of length nb_units_allocated.
158  * Only the first nb_units are valid.
159  *
160  * Must be NULL if nb_units_allocated is zero.
161  */
164 
165 /**
166  * Context structure for coded bitstream operations.
167  */
168 typedef struct CodedBitstreamContext {
169  /**
170  * Logging context to be passed to all av_log() calls associated
171  * with this context.
172  */
173  void *log_ctx;
174 
175  /**
176  * Internal codec-specific hooks.
177  */
178  const struct CodedBitstreamType *codec;
179 
180  /**
181  * Internal codec-specific data.
182  *
183  * This contains any information needed when reading/writing
184  * bitsteams which will not necessarily be present in a fragment.
185  * For example, for H.264 it contains all currently visible
186  * parameter sets - they are required to determine the bitstream
187  * syntax but need not be present in every access unit.
188  */
189  void *priv_data;
190 
191  /**
192  * Array of unit types which should be decomposed when reading.
193  *
194  * Types not in this list will be available in bitstream form only.
195  * If NULL, all supported types will be decomposed.
196  */
198  /**
199  * Length of the decompose_unit_types array.
200  */
202 
203  /**
204  * Enable trace output during read/write operations.
205  */
207  /**
208  * Log level to use for trace output.
209  *
210  * From AV_LOG_*; defaults to AV_LOG_TRACE.
211  */
213 
214  /**
215  * Write buffer. Used as intermediate buffer when writing units.
216  * For internal use of cbs only.
217  */
221 
222 
223 /**
224  * Table of all supported codec IDs.
225  *
226  * Terminated by AV_CODEC_ID_NONE.
227  */
228 extern const enum AVCodecID ff_cbs_all_codec_ids[];
229 
230 
231 /**
232  * Create and initialise a new context for the given codec.
233  */
235  enum AVCodecID codec_id, void *log_ctx);
236 
237 /**
238  * Close a context and free all internal state.
239  */
241 
242 
243 /**
244  * Read the extradata bitstream found in codec parameters into a
245  * fragment, then split into units and decompose.
246  *
247  * This also updates the internal state, so will need to be called for
248  * codecs with extradata to read parameter sets necessary for further
249  * parsing even if the fragment itself is not desired.
250  *
251  * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
252  * before use.
253  */
256  const AVCodecParameters *par);
257 
258 /**
259  * Read the data bitstream from a packet into a fragment, then
260  * split into units and decompose.
261  *
262  * This also updates the internal state of the coded bitstream context
263  * with any persistent data from the fragment which may be required to
264  * read following fragments (e.g. parameter sets).
265  *
266  * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
267  * before use.
268  */
271  const AVPacket *pkt);
272 
273 /**
274  * Read a bitstream from a memory region into a fragment, then
275  * split into units and decompose.
276  *
277  * This also updates the internal state of the coded bitstream context
278  * with any persistent data from the fragment which may be required to
279  * read following fragments (e.g. parameter sets).
280  *
281  * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
282  * before use.
283  */
286  const uint8_t *data, size_t size);
287 
288 
289 /**
290  * Write the content of the fragment to its own internal buffer.
291  *
292  * Writes the content of all units and then assembles them into a new
293  * data buffer. When modifying the content of decomposed units, this
294  * can be used to regenerate the bitstream form of units or the whole
295  * fragment so that it can be extracted for other use.
296  *
297  * This also updates the internal state of the coded bitstream context
298  * with any persistent data from the fragment which may be required to
299  * write following fragments (e.g. parameter sets).
300  */
302  CodedBitstreamFragment *frag);
303 
304 /**
305  * Write the bitstream of a fragment to the extradata in codec parameters.
306  *
307  * Modifies context and fragment as ff_cbs_write_fragment_data does and
308  * replaces any existing extradata in the structure.
309  */
311  AVCodecParameters *par,
312  CodedBitstreamFragment *frag);
313 
314 /**
315  * Write the bitstream of a fragment to a packet.
316  *
317  * Modifies context and fragment as ff_cbs_write_fragment_data does.
318  *
319  * On success, the packet's buf is unreferenced and its buf, data and
320  * size fields are set to the corresponding values from the newly updated
321  * fragment; other fields are not touched. On failure, the packet is not
322  * touched at all.
323  */
325  AVPacket *pkt,
326  CodedBitstreamFragment *frag);
327 
328 
329 /**
330  * Free the units contained in a fragment as well as the fragment's
331  * own data buffer, but not the units array itself.
332  */
334  CodedBitstreamFragment *frag);
335 
336 /**
337  * Free the units array of a fragment in addition to what
338  * ff_cbs_fragment_reset does.
339  */
341  CodedBitstreamFragment *frag);
342 
343 /**
344  * Allocate a new internal content buffer of the given size in the unit.
345  *
346  * The content will be zeroed.
347  */
349  CodedBitstreamUnit *unit,
350  size_t size,
351  void (*free)(void *unit, uint8_t *content));
352 
353 /**
354  * Allocate a new internal data buffer of the given size in the unit.
355  *
356  * The data buffer will have input padding.
357  */
359  CodedBitstreamUnit *unit,
360  size_t size);
361 
362 /**
363  * Insert a new unit into a fragment with the given content.
364  *
365  * The content structure continues to be owned by the caller if
366  * content_buf is not supplied.
367  */
370  int position,
372  void *content,
373  AVBufferRef *content_buf);
374 
375 /**
376  * Insert a new unit into a fragment with the given data bitstream.
377  *
378  * If data_buf is not supplied then data must have been allocated with
379  * av_malloc() and will become owned by the unit after this call.
380  */
383  int position,
385  uint8_t *data, size_t data_size,
386  AVBufferRef *data_buf);
387 
388 /**
389  * Delete a unit from a fragment and free all memory it uses.
390  *
391  * Requires position to be >= 0 and < frag->nb_units.
392  */
395  int position);
396 
397 
398 #endif /* AVCODEC_CBS_H */
CodedBitstreamContext::priv_data
void * priv_data
Internal codec-specific data.
Definition: cbs.h:189
codec_id
enum AVCodecID codec_id
Definition: qsv.c:72
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3949
CodedBitstreamContext::codec
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:178
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
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:74
CodedBitstreamContext::write_buffer
uint8_t * write_buffer
Write buffer.
Definition: cbs.h:218
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:168
data
const char data[16]
Definition: mxf.c:91
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
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:242
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:64
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:401
CodedBitstreamContext::log_ctx
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:173
ff_cbs_fragment_reset
void ff_cbs_fragment_reset(CodedBitstreamContext *ctx, 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:142
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:75
ff_cbs_fragment_free
void ff_cbs_fragment_free(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:157
CodedBitstreamContext::trace_level
int trace_level
Log level to use for trace output.
Definition: cbs.h:212
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
CodedBitstreamFragment::data_size
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:129
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:269
CodedBitstreamFragment::data_bit_padding
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:133
ff_cbs_delete_unit
void ff_cbs_delete_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
Definition: cbs.c:796
ff_cbs_alloc_unit_data
int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size)
Allocate a new internal data buffer of the given size in the unit.
Definition: cbs.c:665
ff_cbs_insert_unit_content
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, 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:722
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:80
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
CodedBitstreamContext::write_buffer_size
size_t write_buffer_size
Definition: cbs.h:219
size
int size
Definition: twinvq_data.h:11134
CodedBitstreamFragment::data
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
CodedBitstreamUnit::data_bit_padding
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:86
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:376
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:224
buffer.h
CodedBitstreamType
Definition: cbs_internal.h:28
ff_cbs_close
void ff_cbs_close(CodedBitstreamContext **ctx)
Close a context and free all internal state.
Definition: cbs.c:115
CodedBitstreamUnit::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:92
ff_cbs_all_codec_ids
enum AVCodecID ff_cbs_all_codec_ids[]
Table of all supported codec IDs.
Definition: cbs.c:52
uint8_t
uint8_t
Definition: audio_convert.c:194
ff_cbs_alloc_unit_content
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size, void(*free)(void *unit, uint8_t *content))
Allocate a new internal content buffer of the given size in the unit.
CodedBitstreamFragment::nb_units_allocated
int nb_units_allocated
Number of allocated units.
Definition: cbs.h:154
avcodec.h
CodedBitstreamUnit::content_ref
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing content.
Definition: cbs.h:106
ff_cbs_insert_unit_data
int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx, 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:759
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:43
CodedBitstreamContext::trace_enable
int trace_enable
Enable trace output during read/write operations.
Definition: cbs.h:206
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:81
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
CodedBitstreamContext::nb_decompose_unit_types
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:201
CodedBitstreamFragment::data_ref
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:139
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:340
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
CodedBitstreamContext::decompose_unit_types
CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
Definition: cbs.h:197