ffeca057abb897208da7c268c99792986b81a449
[libav.git] / libavcodec / cbs.h
1 /*
2 * This file is part of Libav.
3 *
4 * Libav 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 * Libav 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 Libav; 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
41 struct CodedBitstreamType;
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 */
65 CodedBitstreamUnitType type;
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 */
72 uint8_t *data;
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 */
83 size_t data_bit_padding;
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 */
98 int content_external;
99 } CodedBitstreamUnit;
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 */
114 uint8_t *data;
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 */
125 size_t data_bit_padding;
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 */
139 CodedBitstreamUnit *units;
140 } CodedBitstreamFragment;
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 */
174 CodedBitstreamUnitType *decompose_unit_types;
175 /**
176 * Length of the decompose_unit_types array.
177 */
178 int nb_decompose_unit_types;
179
180 /**
181 * Enable trace output during read/write operations.
182 */
183 int trace_enable;
184 /**
185 * Log level to use for trace output.
186 *
187 * From AV_LOG_*; defaults to AV_LOG_TRACE.
188 */
189 int trace_level;
190 } CodedBitstreamContext;
191
192
193 /**
194 * Create and initialise a new context for the given codec.
195 */
196 int ff_cbs_init(CodedBitstreamContext **ctx,
197 enum AVCodecID codec_id, void *log_ctx);
198
199 /**
200 * Close a context and free all internal state.
201 */
202 void ff_cbs_close(CodedBitstreamContext **ctx);
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 */
213 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
214 CodedBitstreamFragment *frag,
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 */
225 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
226 CodedBitstreamFragment *frag,
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 */
237 int ff_cbs_read(CodedBitstreamContext *ctx,
238 CodedBitstreamFragment *frag,
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 */
254 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
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 */
262 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
263 AVCodecParameters *par,
264 CodedBitstreamFragment *frag);
265
266 /**
267 * Write the bitstream of a fragment to a packet.
268 */
269 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
270 AVPacket *pkt,
271 CodedBitstreamFragment *frag);
272
273
274 /**
275 * Free all allocated memory in a fragment.
276 */
277 void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
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 */
287 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
288 CodedBitstreamFragment *frag,
289 int position,
290 CodedBitstreamUnitType type,
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 */
298 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
299 CodedBitstreamFragment *frag,
300 int position,
301 CodedBitstreamUnitType type,
302 uint8_t *data, size_t data_size);
303
304 /**
305 * Delete a unit from a fragment and free all memory it uses.
306 */
307 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
308 CodedBitstreamFragment *frag,
309 int position);
310
311
312 #endif /* AVCODEC_CBS_H */