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 * H.264 / AVC: nal_unit_type
47 * H.265 / HEVC: nal_unit_type
48 * MPEG-2: start code value (without prefix)
50 typedef uint32_t CodedBitstreamUnitType;
53 * Coded bitstream unit structure.
55 * A bitstream unit the smallest element of a bitstream which
56 * is meaningful on its own. For example, an H.264 NAL unit.
58 * See the codec-specific header for the meaning of this for any
61 typedef struct CodedBitstreamUnit {
63 * Codec-specific type of this unit.
65 CodedBitstreamUnitType type;
68 * Pointer to the directly-parsable bitstream form of this unit.
70 * May be NULL if the unit currently only exists in decomposed form.
74 * The number of bytes in the bitstream (including any padding bits
79 * The number of bits which should be ignored in the final byte.
81 * This supports non-byte-aligned bitstreams.
83 size_t data_bit_padding;
86 * Pointer to the decomposed form of this unit.
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
94 * Whether the content was supplied externally.
96 * If so, it should not be freed when freeing the unit.
102 * Coded bitstream fragment structure, combining one or more units.
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.
108 typedef struct CodedBitstreamFragment {
110 * Pointer to the bitstream form of this fragment.
112 * May be NULL if the fragment only exists as component units.
116 * The number of bytes in the bitstream.
118 * The number of bytes in the bitstream (including any padding bits
119 * in the final byte).
123 * The number of bits which should be ignored in the final byte.
125 size_t data_bit_padding;
128 * Number of units in this fragment.
130 * This may be zero if the fragment only exists in bitstream form
131 * and has not been decomposed.
135 * Pointer to an array of units of length nb_units.
137 * Must be NULL if nb_units is zero.
139 CodedBitstreamUnit *units;
140 } CodedBitstreamFragment;
143 * Context structure for coded bitstream operations.
145 typedef struct CodedBitstreamContext {
147 * Logging context to be passed to all av_log() calls associated
153 * Internal codec-specific hooks.
155 const struct CodedBitstreamType *codec;
158 * Internal codec-specific data.
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.
169 * Array of unit types which should be decomposed when reading.
171 * Types not in this list will be available in bitstream form only.
172 * If NULL, all supported types will be decomposed.
174 CodedBitstreamUnitType *decompose_unit_types;
176 * Length of the decompose_unit_types array.
178 int nb_decompose_unit_types;
181 * Enable trace output during read/write operations.
185 * Log level to use for trace output.
187 * From AV_LOG_*; defaults to AV_LOG_TRACE.
190 } CodedBitstreamContext;
194 * Create and initialise a new context for the given codec.
196 int ff_cbs_init(CodedBitstreamContext **ctx,
197 enum AVCodecID codec_id, void *log_ctx);
200 * Close a context and free all internal state.
202 void ff_cbs_close(CodedBitstreamContext **ctx);
206 * Read the extradata bitstream found in codec parameters into a
207 * fragment, then split into units and decompose.
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.
213 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
214 CodedBitstreamFragment *frag,
215 const AVCodecParameters *par);
218 * Read the data bitstream from a packet into a fragment, then
219 * split into units and decompose.
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).
225 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
226 CodedBitstreamFragment *frag,
227 const AVPacket *pkt);
230 * Read a bitstream from a memory region into a fragment, then
231 * split into units and decompose.
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).
237 int ff_cbs_read(CodedBitstreamContext *ctx,
238 CodedBitstreamFragment *frag,
239 const uint8_t *data, size_t size);
243 * Write the content of the fragment to its own internal buffer.
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.
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).
254 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
255 CodedBitstreamFragment *frag);
258 * Write the bitstream of a fragment to the extradata in codec parameters.
260 * This replaces any existing extradata in the structure.
262 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
263 AVCodecParameters *par,
264 CodedBitstreamFragment *frag);
267 * Write the bitstream of a fragment to a packet.
269 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
271 CodedBitstreamFragment *frag);
275 * Free all allocated memory in a fragment.
277 void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
278 CodedBitstreamFragment *frag);
282 * Insert a new unit into a fragment with the given content.
284 * The content structure continues to be owned by the caller, and
285 * will not be freed when the unit is.
287 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
288 CodedBitstreamFragment *frag,
290 CodedBitstreamUnitType type,
294 * Insert a new unit into a fragment with the given data bitstream.
296 * The data buffer will be owned by the unit after this operation.
298 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
299 CodedBitstreamFragment *frag,
301 CodedBitstreamUnitType type,
302 uint8_t *data, size_t data_size);
305 * Delete a unit from a fragment and free all memory it uses.
307 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
308 CodedBitstreamFragment *frag,
312 #endif /* AVCODEC_CBS_H */