]> git.sesse.net Git - ffmpeg/blob - libavcodec/cbs.h
cbs: Describe allocate/free methods in tabular form
[ffmpeg] / libavcodec / cbs.h
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
43 struct CodedBitstreamType;
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      */
70     CodedBitstreamUnitType type;
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      */
77     uint8_t *data;
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      */
88     size_t   data_bit_padding;
89     /**
90      * A reference to the buffer containing data.
91      *
92      * Must be set if data is not NULL.
93      */
94     AVBufferRef *data_ref;
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      */
108     AVBufferRef *content_ref;
109 } CodedBitstreamUnit;
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      */
124     uint8_t *data;
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      */
135     size_t data_bit_padding;
136     /**
137      * A reference to the buffer containing data.
138      *
139      * Must be set if data is not NULL.
140      */
141     AVBufferRef *data_ref;
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      */
156      int             nb_units_allocated;
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      */
164     CodedBitstreamUnit *units;
165 } CodedBitstreamFragment;
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      */
199     CodedBitstreamUnitType *decompose_unit_types;
200     /**
201      * Length of the decompose_unit_types array.
202      */
203     int nb_decompose_unit_types;
204
205     /**
206      * Enable trace output during read/write operations.
207      */
208     int trace_enable;
209     /**
210      * Log level to use for trace output.
211      *
212      * From AV_LOG_*; defaults to AV_LOG_TRACE.
213      */
214     int trace_level;
215
216     /**
217      * Write buffer. Used as intermediate buffer when writing units.
218      * For internal use of cbs only.
219      */
220     uint8_t *write_buffer;
221     size_t   write_buffer_size;
222 } CodedBitstreamContext;
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  */
236 int ff_cbs_init(CodedBitstreamContext **ctx,
237                 enum AVCodecID codec_id, void *log_ctx);
238
239 /**
240  * Close a context and free all internal state.
241  */
242 void ff_cbs_close(CodedBitstreamContext **ctx);
243
244
245 /**
246  * Read the extradata bitstream found in codec parameters into a
247  * fragment, then split into units and decompose.
248  *
249  * This also updates the internal state, so will need to be called for
250  * codecs with extradata to read parameter sets necessary for further
251  * parsing even if the fragment itself is not desired.
252  *
253  * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
254  * before use.
255  */
256 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
257                           CodedBitstreamFragment *frag,
258                           const AVCodecParameters *par);
259
260 /**
261  * Read the data bitstream from a packet into a fragment, then
262  * split into units and decompose.
263  *
264  * This also updates the internal state of the coded bitstream context
265  * with any persistent data from the fragment which may be required to
266  * read following fragments (e.g. parameter sets).
267  *
268  * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
269  * before use.
270  */
271 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
272                        CodedBitstreamFragment *frag,
273                        const AVPacket *pkt);
274
275 /**
276  * Read a bitstream from a memory region into a fragment, then
277  * split into units and decompose.
278  *
279  * This also updates the internal state of the coded bitstream context
280  * with any persistent data from the fragment which may be required to
281  * read following fragments (e.g. parameter sets).
282  *
283  * The fragment must have been zeroed or reset via ff_cbs_fragment_reset
284  * before use.
285  */
286 int ff_cbs_read(CodedBitstreamContext *ctx,
287                 CodedBitstreamFragment *frag,
288                 const uint8_t *data, size_t size);
289
290
291 /**
292  * Write the content of the fragment to its own internal buffer.
293  *
294  * Writes the content of all units and then assembles them into a new
295  * data buffer.  When modifying the content of decomposed units, this
296  * can be used to regenerate the bitstream form of units or the whole
297  * fragment so that it can be extracted for other use.
298  *
299  * This also updates the internal state of the coded bitstream context
300  * with any persistent data from the fragment which may be required to
301  * write following fragments (e.g. parameter sets).
302  */
303 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
304                                CodedBitstreamFragment *frag);
305
306 /**
307  * Write the bitstream of a fragment to the extradata in codec parameters.
308  *
309  * Modifies context and fragment as ff_cbs_write_fragment_data does and
310  * replaces any existing extradata in the structure.
311  */
312 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
313                            AVCodecParameters *par,
314                            CodedBitstreamFragment *frag);
315
316 /**
317  * Write the bitstream of a fragment to a packet.
318  *
319  * Modifies context and fragment as ff_cbs_write_fragment_data does.
320  *
321  * On success, the packet's buf is unreferenced and its buf, data and
322  * size fields are set to the corresponding values from the newly updated
323  * fragment; other fields are not touched.  On failure, the packet is not
324  * touched at all.
325  */
326 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
327                         AVPacket *pkt,
328                         CodedBitstreamFragment *frag);
329
330
331 /**
332  * Free the units contained in a fragment as well as the fragment's
333  * own data buffer, but not the units array itself.
334  */
335 void ff_cbs_fragment_reset(CodedBitstreamFragment *frag);
336
337 /**
338  * Free the units array of a fragment in addition to what
339  * ff_cbs_fragment_reset does.
340  */
341 void ff_cbs_fragment_free(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  */
348 int ff_cbs_alloc_unit_content(CodedBitstreamUnit *unit,
349                               size_t size,
350                               void (*free)(void *opaque, uint8_t *content));
351
352 /**
353  * Allocate a new internal content buffer matching the type of the unit.
354  *
355  * The content will be zeroed.
356  */
357 int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx,
358                                CodedBitstreamUnit *unit);
359
360
361 /**
362  * Allocate a new internal data buffer of the given size in the unit.
363  *
364  * The data buffer will have input padding.
365  */
366 int ff_cbs_alloc_unit_data(CodedBitstreamUnit *unit,
367                            size_t size);
368
369 /**
370  * Insert a new unit into a fragment with the given content.
371  *
372  * The content structure continues to be owned by the caller if
373  * content_buf is not supplied.
374  */
375 int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag,
376                                int position,
377                                CodedBitstreamUnitType type,
378                                void *content,
379                                AVBufferRef *content_buf);
380
381 /**
382  * Insert a new unit into a fragment with the given data bitstream.
383  *
384  * If data_buf is not supplied then data must have been allocated with
385  * av_malloc() and will on success become owned by the unit after this
386  * call or freed on error.
387  */
388 int ff_cbs_insert_unit_data(CodedBitstreamFragment *frag,
389                             int position,
390                             CodedBitstreamUnitType type,
391                             uint8_t *data, size_t data_size,
392                             AVBufferRef *data_buf);
393
394 /**
395  * Delete a unit from a fragment and free all memory it uses.
396  *
397  * Requires position to be >= 0 and < frag->nb_units.
398  */
399 void ff_cbs_delete_unit(CodedBitstreamFragment *frag,
400                         int position);
401
402
403 #endif /* AVCODEC_CBS_H */