2 * This file is part of FFmpeg.
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.
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.
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
29 * This defines a framework for converting between a coded bitstream
30 * and structures defining all individual syntax elements found in
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
41 struct CodedBitstreamType;
44 * The codec-specific type of a bitstream unit.
46 typedef uint32_t CodedBitstreamUnitType;
49 * Coded bitstream unit structure.
51 * A bitstream unit the smallest element of a bitstream which
52 * is meaningful on its own. For example, an H.264 NAL unit.
54 * See the codec-specific header for the meaning of this for any
57 typedef struct CodedBitstreamUnit {
59 * Codec-specific type of this unit.
61 CodedBitstreamUnitType type;
64 * Pointer to the directly-parsable bitstream form of this unit.
66 * May be NULL if the unit currently only exists in decomposed form.
70 * The number of bytes in the bitstream (including any padding bits
75 * The number of bits which should be ignored in the final byte.
77 * This supports non-byte-aligned bitstreams.
79 size_t data_bit_padding;
82 * Pointer to the decomposed form of this unit.
84 * The type of this structure depends on both the codec and the
85 * type of this unit. May be NULL if the unit only exists in
90 * Whether the content was supplied externally.
92 * If so, it should not be freed when freeing the unit.
98 * Coded bitstream fragment structure, combining one or more units.
100 * This is any sequence of units. It need not form some greater whole,
101 * though in many cases it will. For example, an H.264 access unit,
102 * which is composed of a sequence of H.264 NAL units.
104 typedef struct CodedBitstreamFragment {
106 * Pointer to the bitstream form of this fragment.
108 * May be NULL if the fragment only exists as component units.
112 * The number of bytes in the bitstream.
114 * The number of bytes in the bitstream (including any padding bits
115 * in the final byte).
119 * The number of bits which should be ignored in the final byte.
121 size_t data_bit_padding;
124 * Number of units in this fragment.
126 * This may be zero if the fragment only exists in bitstream form
127 * and has not been decomposed.
131 * Pointer to an array of units of length nb_units.
133 * Must be NULL if nb_units is zero.
135 CodedBitstreamUnit *units;
136 } CodedBitstreamFragment;
139 * Context structure for coded bitstream operations.
141 typedef struct CodedBitstreamContext {
143 * Logging context to be passed to all av_log() calls associated
149 * Internal codec-specific hooks.
151 const struct CodedBitstreamType *codec;
154 * Internal codec-specific data.
156 * This contains any information needed when reading/writing
157 * bitsteams which will not necessarily be present in a fragment.
158 * For example, for H.264 it contains all currently visible
159 * parameter sets - they are required to determine the bitstream
160 * syntax but need not be present in every access unit.
165 * Array of unit types which should be decomposed when reading.
167 * Types not in this list will be available in bitstream form only.
168 * If NULL, all supported types will be decomposed.
170 CodedBitstreamUnitType *decompose_unit_types;
172 * Length of the decompose_unit_types array.
174 int nb_decompose_unit_types;
177 * Enable trace output during read/write operations.
181 * Log level to use for trace output.
183 * From AV_LOG_*; defaults to AV_LOG_TRACE.
186 } CodedBitstreamContext;
190 * Create and initialise a new context for the given codec.
192 int ff_cbs_init(CodedBitstreamContext **ctx,
193 enum AVCodecID codec_id, void *log_ctx);
196 * Close a context and free all internal state.
198 void ff_cbs_close(CodedBitstreamContext **ctx);
202 * Read the extradata bitstream found in codec parameters into a
203 * fragment, then split into units and decompose.
205 * This also updates the internal state, so will need to be called for
206 * codecs with extradata to read parameter sets necessary for further
207 * parsing even if the fragment itself is not desired.
209 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
210 CodedBitstreamFragment *frag,
211 const AVCodecParameters *par);
214 * Read the data bitstream from a packet into a fragment, then
215 * split into units and decompose.
217 * This also updates the internal state of the coded bitstream context
218 * with any persistent data from the fragment which may be required to
219 * read following fragments (e.g. parameter sets).
221 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
222 CodedBitstreamFragment *frag,
223 const AVPacket *pkt);
226 * Read a bitstream from a memory region into a fragment, then
227 * split into units and decompose.
229 * This also updates the internal state of the coded bitstream context
230 * with any persistent data from the fragment which may be required to
231 * read following fragments (e.g. parameter sets).
233 int ff_cbs_read(CodedBitstreamContext *ctx,
234 CodedBitstreamFragment *frag,
235 const uint8_t *data, size_t size);
239 * Write the content of the fragment to its own internal buffer.
241 * Writes the content of all units and then assembles them into a new
242 * data buffer. When modifying the content of decomposed units, this
243 * can be used to regenerate the bitstream form of units or the whole
244 * fragment so that it can be extracted for other use.
246 * This also updates the internal state of the coded bitstream context
247 * with any persistent data from the fragment which may be required to
248 * write following fragments (e.g. parameter sets).
250 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
251 CodedBitstreamFragment *frag);
254 * Write the bitstream of a fragment to the extradata in codec parameters.
256 * This replaces any existing extradata in the structure.
258 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
259 AVCodecParameters *par,
260 CodedBitstreamFragment *frag);
263 * Write the bitstream of a fragment to a packet.
265 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
267 CodedBitstreamFragment *frag);
271 * Free all allocated memory in a fragment.
273 void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
274 CodedBitstreamFragment *frag);
278 * Insert a new unit into a fragment with the given content.
280 * The content structure continues to be owned by the caller, and
281 * will not be freed when the unit is.
283 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
284 CodedBitstreamFragment *frag,
286 CodedBitstreamUnitType type,
290 * Insert a new unit into a fragment with the given data bitstream.
292 * The data buffer will be owned by the unit after this operation.
294 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
295 CodedBitstreamFragment *frag,
297 CodedBitstreamUnitType type,
298 uint8_t *data, size_t data_size);
301 * Delete a unit from a fragment and free all memory it uses.
303 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
304 CodedBitstreamFragment *frag,
308 #endif /* AVCODEC_CBS_H */