FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  * Pointer to an array of units of length nb_units.
150  *
151  * Must be NULL if nb_units is zero.
152  */
155 
156 /**
157  * Context structure for coded bitstream operations.
158  */
159 typedef struct CodedBitstreamContext {
160  /**
161  * Logging context to be passed to all av_log() calls associated
162  * with this context.
163  */
164  void *log_ctx;
165 
166  /**
167  * Internal codec-specific hooks.
168  */
169  const struct CodedBitstreamType *codec;
170 
171  /**
172  * Internal codec-specific data.
173  *
174  * This contains any information needed when reading/writing
175  * bitsteams which will not necessarily be present in a fragment.
176  * For example, for H.264 it contains all currently visible
177  * parameter sets - they are required to determine the bitstream
178  * syntax but need not be present in every access unit.
179  */
180  void *priv_data;
181 
182  /**
183  * Array of unit types which should be decomposed when reading.
184  *
185  * Types not in this list will be available in bitstream form only.
186  * If NULL, all supported types will be decomposed.
187  */
189  /**
190  * Length of the decompose_unit_types array.
191  */
193 
194  /**
195  * Enable trace output during read/write operations.
196  */
198  /**
199  * Log level to use for trace output.
200  *
201  * From AV_LOG_*; defaults to AV_LOG_TRACE.
202  */
205 
206 
207 /**
208  * Table of all supported codec IDs.
209  *
210  * Terminated by AV_CODEC_ID_NONE.
211  */
212 extern const enum AVCodecID ff_cbs_all_codec_ids[];
213 
214 
215 /**
216  * Create and initialise a new context for the given codec.
217  */
219  enum AVCodecID codec_id, void *log_ctx);
220 
221 /**
222  * Close a context and free all internal state.
223  */
225 
226 
227 /**
228  * Read the extradata bitstream found in codec parameters into a
229  * fragment, then split into units and decompose.
230  *
231  * This also updates the internal state, so will need to be called for
232  * codecs with extradata to read parameter sets necessary for further
233  * parsing even if the fragment itself is not desired.
234  */
237  const AVCodecParameters *par);
238 
239 /**
240  * Read the data bitstream from a packet into a fragment, then
241  * split into units and decompose.
242  *
243  * This also updates the internal state of the coded bitstream context
244  * with any persistent data from the fragment which may be required to
245  * read following fragments (e.g. parameter sets).
246  */
249  const AVPacket *pkt);
250 
251 /**
252  * Read a bitstream from a memory region into a fragment, then
253  * split into units and decompose.
254  *
255  * This also updates the internal state of the coded bitstream context
256  * with any persistent data from the fragment which may be required to
257  * read following fragments (e.g. parameter sets).
258  */
261  const uint8_t *data, size_t size);
262 
263 
264 /**
265  * Write the content of the fragment to its own internal buffer.
266  *
267  * Writes the content of all units and then assembles them into a new
268  * data buffer. When modifying the content of decomposed units, this
269  * can be used to regenerate the bitstream form of units or the whole
270  * fragment so that it can be extracted for other use.
271  *
272  * This also updates the internal state of the coded bitstream context
273  * with any persistent data from the fragment which may be required to
274  * write following fragments (e.g. parameter sets).
275  */
277  CodedBitstreamFragment *frag);
278 
279 /**
280  * Write the bitstream of a fragment to the extradata in codec parameters.
281  *
282  * This replaces any existing extradata in the structure.
283  */
285  AVCodecParameters *par,
286  CodedBitstreamFragment *frag);
287 
288 /**
289  * Write the bitstream of a fragment to a packet.
290  */
292  AVPacket *pkt,
293  CodedBitstreamFragment *frag);
294 
295 
296 /**
297  * Free all allocated memory in a fragment.
298  */
300  CodedBitstreamFragment *frag);
301 
302 
303 /**
304  * Allocate a new internal content buffer of the given size in the unit.
305  *
306  * The content will be zeroed.
307  */
309  CodedBitstreamUnit *unit,
310  size_t size,
311  void (*free)(void *unit, uint8_t *content));
312 
313 /**
314  * Allocate a new internal data buffer of the given size in the unit.
315  *
316  * The data buffer will have input padding.
317  */
319  CodedBitstreamUnit *unit,
320  size_t size);
321 
322 /**
323  * Insert a new unit into a fragment with the given content.
324  *
325  * The content structure continues to be owned by the caller if
326  * content_buf is not supplied.
327  */
330  int position,
332  void *content,
333  AVBufferRef *content_buf);
334 
335 /**
336  * Insert a new unit into a fragment with the given data bitstream.
337  *
338  * If data_buf is not supplied then data must have been allocated with
339  * av_malloc() and will become owned by the unit after this call.
340  */
343  int position,
345  uint8_t *data, size_t data_size,
346  AVBufferRef *data_buf);
347 
348 /**
349  * Delete a unit from a fragment and free all memory it uses.
350  */
353  int position);
354 
355 
356 #endif /* AVCODEC_CBS_H */
int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Write the content of the fragment to its own internal buffer.
Definition: cbs.c:282
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
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.
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
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:213
static AVPacket pkt
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3909
int trace_enable
Enable trace output during read/write operations.
Definition: cbs.h:197
uint8_t
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:43
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:86
Coded bitstream unit structure.
Definition: cbs.h:64
ptrdiff_t size
Definition: opengl_enc.c:101
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units.
Definition: cbs.h:153
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:75
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:129
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:570
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
enum AVCodecID ff_cbs_all_codec_ids[]
Table of all supported codec IDs.
Definition: cbs.c:52
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:133
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:164
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:343
AVFormatContext * ctx
Definition: movenc.c:48
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:262
enum AVCodecID codec_id
Definition: vaapi_decode.c:364
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
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
Definition: cbs.c:139
Libavcodec external API header.
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:192
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
int trace_level
Log level to use for trace output.
Definition: cbs.h:203
GLint GLenum type
Definition: opengl_enc.c:105
Context structure for coded bitstream operations.
Definition: cbs.h:159
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:233
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing content.
Definition: cbs.h:106
refcounted data buffer API
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:527
void * priv_data
Internal codec-specific data.
Definition: cbs.h:180
A reference to a data buffer.
Definition: buffer.h:81
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:92
CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
Definition: cbs.h:188
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:318
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:607
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:169
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:139
This structure stores compressed data.
Definition: avcodec.h:1429
int 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:644
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:80
void ff_cbs_close(CodedBitstreamContext **ctx)
Close a context and free all internal state.
Definition: cbs.c:113