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 "avcodec.h"
26 
27 
28 /*
29  * This defines a framework for converting between a coded bitstream
30  * and structures defining all individual syntax elements found in
31  * such a stream.
32  *
33  * Conversion in both directions is possible. Given a coded bitstream
34  * (any meaningful fragment), it can be parsed and decomposed into
35  * syntax elements stored in a set of codec-specific structures.
36  * Similarly, given a set of those same codec-specific structures the
37  * syntax elements can be serialised and combined to create a coded
38  * bitstream.
39  */
40 
42 
43 /**
44  * The codec-specific type of a bitstream unit.
45  *
46  * H.264 / AVC: nal_unit_type
47  * H.265 / HEVC: nal_unit_type
48  * MPEG-2: start code value (without prefix)
49  */
50 typedef uint32_t CodedBitstreamUnitType;
51 
52 /**
53  * Coded bitstream unit structure.
54  *
55  * A bitstream unit the smallest element of a bitstream which
56  * is meaningful on its own. For example, an H.264 NAL unit.
57  *
58  * See the codec-specific header for the meaning of this for any
59  * particular codec.
60  */
61 typedef struct CodedBitstreamUnit {
62  /**
63  * Codec-specific type of this unit.
64  */
66 
67  /**
68  * Pointer to the directly-parsable bitstream form of this unit.
69  *
70  * May be NULL if the unit currently only exists in decomposed form.
71  */
73  /**
74  * The number of bytes in the bitstream (including any padding bits
75  * in the final byte).
76  */
77  size_t data_size;
78  /**
79  * The number of bits which should be ignored in the final byte.
80  *
81  * This supports non-byte-aligned bitstreams.
82  */
84 
85  /**
86  * Pointer to the decomposed form of this unit.
87  *
88  * The type of this structure depends on both the codec and the
89  * type of this unit. May be NULL if the unit only exists in
90  * bitstream form.
91  */
92  void *content;
93  /**
94  * Whether the content was supplied externally.
95  *
96  * If so, it should not be freed when freeing the unit.
97  */
100 
101 /**
102  * Coded bitstream fragment structure, combining one or more units.
103  *
104  * This is any sequence of units. It need not form some greater whole,
105  * though in many cases it will. For example, an H.264 access unit,
106  * which is composed of a sequence of H.264 NAL units.
107  */
108 typedef struct CodedBitstreamFragment {
109  /**
110  * Pointer to the bitstream form of this fragment.
111  *
112  * May be NULL if the fragment only exists as component units.
113  */
115  /**
116  * The number of bytes in the bitstream.
117  *
118  * The number of bytes in the bitstream (including any padding bits
119  * in the final byte).
120  */
121  size_t data_size;
122  /**
123  * The number of bits which should be ignored in the final byte.
124  */
126 
127  /**
128  * Number of units in this fragment.
129  *
130  * This may be zero if the fragment only exists in bitstream form
131  * and has not been decomposed.
132  */
133  int nb_units;
134  /**
135  * Pointer to an array of units of length nb_units.
136  *
137  * Must be NULL if nb_units is zero.
138  */
141 
142 /**
143  * Context structure for coded bitstream operations.
144  */
145 typedef struct CodedBitstreamContext {
146  /**
147  * Logging context to be passed to all av_log() calls associated
148  * with this context.
149  */
150  void *log_ctx;
151 
152  /**
153  * Internal codec-specific hooks.
154  */
155  const struct CodedBitstreamType *codec;
156 
157  /**
158  * Internal codec-specific data.
159  *
160  * This contains any information needed when reading/writing
161  * bitsteams which will not necessarily be present in a fragment.
162  * For example, for H.264 it contains all currently visible
163  * parameter sets - they are required to determine the bitstream
164  * syntax but need not be present in every access unit.
165  */
166  void *priv_data;
167 
168  /**
169  * Array of unit types which should be decomposed when reading.
170  *
171  * Types not in this list will be available in bitstream form only.
172  * If NULL, all supported types will be decomposed.
173  */
175  /**
176  * Length of the decompose_unit_types array.
177  */
179 
180  /**
181  * Enable trace output during read/write operations.
182  */
184  /**
185  * Log level to use for trace output.
186  *
187  * From AV_LOG_*; defaults to AV_LOG_TRACE.
188  */
191 
192 
193 /**
194  * Create and initialise a new context for the given codec.
195  */
197  enum AVCodecID codec_id, void *log_ctx);
198 
199 /**
200  * Close a context and free all internal state.
201  */
203 
204 
205 /**
206  * Read the extradata bitstream found in codec parameters into a
207  * fragment, then split into units and decompose.
208  *
209  * This also updates the internal state, so will need to be called for
210  * codecs with extradata to read parameter sets necessary for further
211  * parsing even if the fragment itself is not desired.
212  */
215  const AVCodecParameters *par);
216 
217 /**
218  * Read the data bitstream from a packet into a fragment, then
219  * split into units and decompose.
220  *
221  * This also updates the internal state of the coded bitstream context
222  * with any persistent data from the fragment which may be required to
223  * read following fragments (e.g. parameter sets).
224  */
227  const AVPacket *pkt);
228 
229 /**
230  * Read a bitstream from a memory region into a fragment, then
231  * split into units and decompose.
232  *
233  * This also updates the internal state of the coded bitstream context
234  * with any persistent data from the fragment which may be required to
235  * read following fragments (e.g. parameter sets).
236  */
239  const uint8_t *data, size_t size);
240 
241 
242 /**
243  * Write the content of the fragment to its own internal buffer.
244  *
245  * Writes the content of all units and then assembles them into a new
246  * data buffer. When modifying the content of decomposed units, this
247  * can be used to regenerate the bitstream form of units or the whole
248  * fragment so that it can be extracted for other use.
249  *
250  * This also updates the internal state of the coded bitstream context
251  * with any persistent data from the fragment which may be required to
252  * write following fragments (e.g. parameter sets).
253  */
255  CodedBitstreamFragment *frag);
256 
257 /**
258  * Write the bitstream of a fragment to the extradata in codec parameters.
259  *
260  * This replaces any existing extradata in the structure.
261  */
263  AVCodecParameters *par,
264  CodedBitstreamFragment *frag);
265 
266 /**
267  * Write the bitstream of a fragment to a packet.
268  */
270  AVPacket *pkt,
271  CodedBitstreamFragment *frag);
272 
273 
274 /**
275  * Free all allocated memory in a fragment.
276  */
278  CodedBitstreamFragment *frag);
279 
280 
281 /**
282  * Insert a new unit into a fragment with the given content.
283  *
284  * The content structure continues to be owned by the caller, and
285  * will not be freed when the unit is.
286  */
289  int position,
291  void *content);
292 
293 /**
294  * Insert a new unit into a fragment with the given data bitstream.
295  *
296  * The data buffer will be owned by the unit after this operation.
297  */
300  int position,
302  uint8_t *data, size_t data_size);
303 
304 /**
305  * Delete a unit from a fragment and free all memory it uses.
306  */
309  int position);
310 
311 
312 #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:216
int nb_units
Number of units in this fragment.
Definition: cbs.h:133
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:65
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:422
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:151
static AVPacket pkt
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3830
int trace_enable
Enable trace output during read/write operations.
Definition: cbs.h:183
uint8_t
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:41
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:83
Coded bitstream unit structure.
Definition: cbs.h:61
ptrdiff_t size
Definition: opengl_enc.c:101
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:92
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units.
Definition: cbs.h:139
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:72
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:121
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition: cbs.h:125
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:150
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:267
int content_external
Whether the content was supplied externally.
Definition: cbs.h:98
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:193
enum AVCodecID codec_id
Definition: vaapi_decode.c:236
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:42
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:114
void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free all allocated memory in a fragment.
Definition: cbs.c:106
Libavcodec external API header.
int nb_decompose_unit_types
Length of the decompose_unit_types array.
Definition: cbs.h:178
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:108
int trace_level
Log level to use for trace output.
Definition: cbs.h:189
GLint GLenum type
Definition: opengl_enc.c:105
Context structure for coded bitstream operations.
Definition: cbs.h:145
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:172
void * priv_data
Internal codec-specific data.
Definition: cbs.h:166
CodedBitstreamUnitType * decompose_unit_types
Array of unit types which should be decomposed when reading.
Definition: cbs.h:174
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:242
const struct CodedBitstreamType * codec
Internal codec-specific hooks.
Definition: cbs.h:155
This structure stores compressed data.
Definition: avcodec.h:1391
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:468
int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, uint8_t *data, size_t data_size)
Insert a new unit into a fragment with the given data bitstream.
Definition: cbs.c:445
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:77
void ff_cbs_close(CodedBitstreamContext **ctx)
Close a context and free all internal state.
Definition: cbs.c:81