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
26
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 */
53
54 /**
55 * Coded bitstream unit structure.
56 *
57 * A bitstream unit the smallest element of a bitstream which
58 * is meaningful on its own. For example, an H.264 NAL unit.
59 *
60 * See the codec-specific header for the meaning of this for any
61 * particular codec.
62 */
64 /**
65 * Codec-specific type of this unit.
66 */
68
69 /**
70 * Pointer to the directly-parsable bitstream form of this unit.
71 *
72 * May be NULL if the unit currently only exists in decomposed form.
73 */
75 /**
76 * The number of bytes in the bitstream (including any padding bits
77 * in the final byte).
78 */
80 /**
81 * The number of bits which should be ignored in the final byte.
82 *
83 * This supports non-byte-aligned bitstreams.
84 */
86 /**
87 * If data is reference counted, a reference to the buffer containing
88 * data. Null if data is not reference counted.
89 */
91
92 /**
93 * Pointer to the decomposed form of this unit.
94 *
95 * The type of this structure depends on both the codec and the
96 * type of this unit. May be NULL if the unit only exists in
97 * bitstream form.
98 */
100 /**
101 * If content is reference counted, a reference to the buffer containing
102 * content. Null if content is not reference counted.
103 */
106
107 /**
108 * Coded bitstream fragment structure, combining one or more units.
109 *
110 * This is any sequence of units. It need not form some greater whole,
111 * though in many cases it will. For example, an H.264 access unit,
112 * which is composed of a sequence of H.264 NAL units.
113 */
115 /**
116 * Pointer to the bitstream form of this fragment.
117 *
118 * May be NULL if the fragment only exists as component units.
119 */
121 /**
122 * The number of bytes in the bitstream.
123 *
124 * The number of bytes in the bitstream (including any padding bits
125 * in the final byte).
126 */
128 /**
129 * The number of bits which should be ignored in the final byte.
130 */
132 /**
133 * If data is reference counted, a reference to the buffer containing
134 * data. Null if data is not reference counted.
135 */
137
138 /**
139 * Number of units in this fragment.
140 *
141 * This may be zero if the fragment only exists in bitstream form
142 * and has not been decomposed.
143 */
145 /**
146 * Pointer to an array of units of length nb_units.
147 *
148 * Must be NULL if nb_units is zero.
149 */
152
153 /**
154 * Context structure for coded bitstream operations.
155 */
157 /**
158 * Logging context to be passed to all av_log() calls associated
159 * with this context.
160 */
162
163 /**
164 * Internal codec-specific hooks.
165 */
167
168 /**
169 * Internal codec-specific data.
170 *
171 * This contains any information needed when reading/writing
172 * bitsteams which will not necessarily be present in a fragment.
173 * For example, for H.264 it contains all currently visible
174 * parameter sets - they are required to determine the bitstream
175 * syntax but need not be present in every access unit.
176 */
178
179 /**
180 * Array of unit types which should be decomposed when reading.
181 *
182 * Types not in this list will be available in bitstream form only.
183 * If NULL, all supported types will be decomposed.
184 */
186 /**
187 * Length of the decompose_unit_types array.
188 */
190
191 /**
192 * Enable trace output during read/write operations.
193 */
195 /**
196 * Log level to use for trace output.
197 *
198 * From AV_LOG_*; defaults to AV_LOG_TRACE.
199 */
202
203
204 /**
205 * Table of all supported codec IDs.
206 *
207 * Terminated by AV_CODEC_ID_NONE.
208 */
210
211
212 /**
213 * Create and initialise a new context for the given codec.
214 */
217
218 /**
219 * Close a context and free all internal state.
220 */
222
223
224 /**
225 * Read the extradata bitstream found in codec parameters into a
226 * fragment, then split into units and decompose.
227 *
228 * This also updates the internal state, so will need to be called for
229 * codecs with extradata to read parameter sets necessary for further
230 * parsing even if the fragment itself is not desired.
231 */
235
236 /**
237 * Read the data bitstream from a packet into a fragment, then
238 * split into units and decompose.
239 *
240 * This also updates the internal state of the coded bitstream context
241 * with any persistent data from the fragment which may be required to
242 * read following fragments (e.g. parameter sets).
243 */
247
248 /**
249 * Read a bitstream from a memory region into a fragment, then
250 * split into units and decompose.
251 *
252 * This also updates the internal state of the coded bitstream context
253 * with any persistent data from the fragment which may be required to
254 * read following fragments (e.g. parameter sets).
255 */
259
260
261 /**
262 * Write the content of the fragment to its own internal buffer.
263 *
264 * Writes the content of all units and then assembles them into a new
265 * data buffer. When modifying the content of decomposed units, this
266 * can be used to regenerate the bitstream form of units or the whole
267 * fragment so that it can be extracted for other use.
268 *
269 * This also updates the internal state of the coded bitstream context
270 * with any persistent data from the fragment which may be required to
271 * write following fragments (e.g. parameter sets).
272 */
275
276 /**
277 * Write the bitstream of a fragment to the extradata in codec parameters.
278 *
279 * This replaces any existing extradata in the structure.
280 */
284
285 /**
286 * Write the bitstream of a fragment to a packet.
287 */
291
292
293 /**
294 * Free all allocated memory in a fragment.
295 */
298
299
300 /**
301 * Allocate a new internal content buffer of the given size in the unit.
302 *
303 * The content will be zeroed.
304 */
308 void (*free)(
void *unit,
uint8_t *content));
309
310 /**
311 * Allocate a new internal data buffer of the given size in the unit.
312 *
313 * The data buffer will have input padding.
314 */
317 size_t size);
318
319 /**
320 * Insert a new unit into a fragment with the given content.
321 *
322 * The content structure continues to be owned by the caller if
323 * content_buf is not supplied.
324 */
327 int position,
329 void *content,
331
332 /**
333 * Insert a new unit into a fragment with the given data bitstream.
334 *
335 * If data_buf is not supplied then data must have been allocated with
336 * av_malloc() and will become owned by the unit after this call.
337 */
340 int position,
344
345 /**
346 * Delete a unit from a fragment and free all memory it uses.
347 */
350 int position);
351
352
353 #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.
int nb_units
Number of units in this fragment.
ptrdiff_t const GLvoid * data
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.
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...
This struct describes the properties of an encoded stream.
int trace_enable
Enable trace output during read/write operations.
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Coded bitstream unit structure.
void * content
Pointer to the decomposed form of this unit.
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units.
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
size_t data_size
The number of bytes in the bitstream.
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.
AVCodecID
Identify the syntax and semantics of the bitstream.
enum AVCodecID ff_cbs_all_codec_ids[]
Table of all supported codec IDs.
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
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.
int ff_cbs_init(CodedBitstreamContext **ctx, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
uint8_t * data
Pointer to the bitstream form of this fragment.
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
Libavcodec external API header.
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Coded bitstream fragment structure, combining one or more units.
int trace_level
Log level to use for trace output.
Context structure for coded bitstream operations.
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.
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing content.
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.
void * priv_data
Internal codec-specific data.
A reference to a data buffer.
AVBufferRef * data_ref
If data is reference counted, a reference to the buffer containing data.
CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
int ff_cbs_write_extradata(CodedBitstreamContext *ctx, AVCodecParameters *par, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to the extradata in codec parameters.
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.
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
AVBufferRef * data_ref
If data is reference counted, a reference to the buffer containing data.
This structure stores compressed data.
int ff_cbs_delete_unit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position)
Delete a unit from a fragment and free all memory it uses.
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
void ff_cbs_close(CodedBitstreamContext **ctx)
Close a context and free all internal state.