FFmpeg
Loading...
Searching...
No Matches
cbs_internal.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_INTERNAL_H
20#define AVCODEC_CBS_INTERNAL_H
21
22#include <stddef.h>
23#include <stdint.h>
24
25#include "config.h"
26
27#include "libavutil/log.h"
28
29#include "cbs.h"
30#include "codec_id.h"
31#include "get_bits.h"
32#include "put_bits.h"
33#include "libavutil/refstruct.h"
34
35#ifndef CBS_READ
36#define CBS_READ 1
37#endif
38#ifndef CBS_WRITE
39#define CBS_WRITE 1
40#endif
41#ifndef CBS_TRACE
42#define CBS_TRACE 1
43#endif
44
45#ifndef CBS_APV
46#define CBS_APV CONFIG_CBS_APV
47#endif
48#ifndef CBS_AV1
49#define CBS_AV1 CONFIG_CBS_AV1
50#endif
51#ifndef CBS_H264
52#define CBS_H264 CONFIG_CBS_H264
53#endif
54#ifndef CBS_H265
55#define CBS_H265 CONFIG_CBS_H265
56#endif
57#ifndef CBS_H266
58#define CBS_H266 CONFIG_CBS_H266
59#endif
60#ifndef CBS_LCEVC
61#define CBS_LCEVC CONFIG_CBS_LCEVC
62#endif
63#ifndef CBS_JPEG
64#define CBS_JPEG CONFIG_CBS_JPEG
65#endif
66#ifndef CBS_MPEG2
67#define CBS_MPEG2 CONFIG_CBS_MPEG2
68#endif
69#ifndef CBS_VP8
70#define CBS_VP8 CONFIG_CBS_VP8
71#endif
72#ifndef CBS_VP9
73#define CBS_VP9 CONFIG_CBS_VP9
74#endif
75
77 // Unit content may contain some references to other structures, but all
78 // managed via buffer reference counting. The descriptor defines the
79 // structure offsets of every buffer reference.
81 // Unit content is something more complex. The descriptor defines
82 // special functions to manage the content.
84};
85
86enum {
87 // Maximum number of unit types described by the same non-range
88 // unit type descriptor.
90 // Maximum number of reference buffer offsets in any one unit.
92 // Special value used in a unit type descriptor to indicate that it
93 // applies to a large range of types rather than a set of discrete
94 // values.
96};
97
99 // Number of entries in the unit_types array, or the special value
100 // CBS_UNIT_TYPE_RANGE to indicate that the range fields should be
101 // used instead.
103
104 union {
105 // Array of unit types that this entry describes.
107 // Start and end of unit type range, used if nb_unit_types is
108 // CBS_UNIT_TYPE_RANGE.
109 struct {
114
115 // The type of content described.
117 // The size of the structure which should be allocated to contain
118 // the decomposed content of this type of unit.
120
121 union {
122 // This union's state is determined by content_type:
123 // ref for CBS_CONTENT_TYPE_INTERNAL_REFS,
124 // complex for CBS_CONTENT_TYPE_COMPLEX.
125 struct {
126 // Number of entries in the ref_offsets array.
127 // May be zero, then the structure is POD-like.
129 // The structure must contain two adjacent elements:
130 // type *field;
131 // AVBufferRef *field_ref;
132 // where field points to something in the buffer referred to by
133 // field_ref. This offset is then set to offsetof(struct, field).
136
137 struct {
138 void (*content_free)(AVRefStructOpaque opaque, void *content);
139 int (*content_clone)(void **new_content, CodedBitstreamUnit *unit);
143
144typedef struct CodedBitstreamType {
146
147 // A class for the private data, used to declare private AVOptions.
148 // This field is NULL for types that do not declare any options.
149 // If this field is non-NULL, the first member of the filter private data
150 // must be a pointer to AVClass.
152
154
155 // List of unit type descriptors for this codec.
156 // Terminated by a descriptor with nb_unit_types equal to zero.
158
159 // Split frag->data into coded bitstream units, creating the
160 // frag->units array. Fill data but not content on each unit.
161 // The header argument should be set if the fragment came from
162 // a header block, which may require different parsing for some
163 // codecs (e.g. the AVCC header in H.264).
166 int header);
167
168 // Read the unit->data bitstream and decompose it, creating
169 // unit->content.
171 CodedBitstreamUnit *unit);
172
173 // Write the data bitstream from unit->content into pbc.
174 // Return value AVERROR(ENOSPC) indicates that pbc was too small.
176 CodedBitstreamUnit *unit,
177 PutBitContext *pbc);
178
179 // Return 1 when the unit should be dropped according to 'skip',
180 // 0 otherwise.
182 const CodedBitstreamUnit *unit,
183 enum AVDiscard skip);
184
185 // Read the data from all of frag->units and assemble it into
186 // a bitstream for the whole fragment.
189
190 // Reset the codec internal state.
192
193 // Free the codec internal state.
196
197
198// Helper functions for trace output.
199
201 const char *name);
202
203
204// Helper functions for read/write of common bitstream elements, including
205// generation of trace output. The simple functions are equivalent to
206// their non-simple counterparts except that their range is unrestricted
207// (i.e. only limited by the amount of bits used) and they lack
208// the ability to use subscripts.
209
211 int width, const char *name,
212 const int *subscripts, uint32_t *write_to,
213 uint32_t range_min, uint32_t range_max);
214
216 int width, const char *name, uint32_t *write_to);
217
219 int width, const char *name,
220 const int *subscripts, uint32_t value,
221 uint32_t range_min, uint32_t range_max);
222
224 int width, const char *name, uint32_t value);
225
227 int width, const char *name,
228 const int *subscripts, int32_t *write_to,
229 int32_t range_min, int32_t range_max);
230
232 int width, const char *name,
233 const int *subscripts, int32_t value,
234 int32_t range_min, int32_t range_max);
235
236// The largest unsigned value representable in N bits, suitable for use as
237// range_max in the above functions.
238#define MAX_UINT_BITS(length) ((UINT64_C(1) << (length)) - 1)
239
240// The largest signed value representable in N bits, suitable for use as
241// range_max in the above functions.
242#define MAX_INT_BITS(length) ((INT64_C(1) << ((length) - 1)) - 1)
243
244// The smallest signed value representable in N bits, suitable for use as
245// range_min in the above functions.
246#define MIN_INT_BITS(length) (-(INT64_C(1) << ((length) - 1)))
247
248
249#if CBS_TRACE
250// Start of a syntax element during read tracing.
251#define CBS_TRACE_READ_START() \
252 GetBitContext trace_start; \
253 do { \
254 if (ctx->trace_enable) \
255 trace_start = *gbc; \
256 } while (0)
257
258// End of a syntax element for tracing, make callback.
259#define CBS_TRACE_READ_END() \
260 do { \
261 if (ctx->trace_enable) { \
262 int start_position = get_bits_count(&trace_start); \
263 int end_position = get_bits_count(gbc); \
264 av_assert0(start_position <= end_position); \
265 ctx->trace_read_callback(ctx->trace_context, &trace_start, \
266 end_position - start_position, \
267 name, subscripts, value); \
268 } \
269 } while (0)
270
271// End of a syntax element with no subscript entries.
272#define CBS_TRACE_READ_END_NO_SUBSCRIPTS() \
273 do { \
274 const int *subscripts = NULL; \
275 CBS_TRACE_READ_END(); \
276 } while (0)
277
278// End of a syntax element which is made up of subelements which
279// are already traced, so we are only showing the value.
280#define CBS_TRACE_READ_END_VALUE_ONLY() \
281 do { \
282 if (ctx->trace_enable) { \
283 ctx->trace_read_callback(ctx->trace_context, &trace_start, 0, \
284 name, subscripts, value); \
285 } \
286 } while (0)
287
288// Start of a syntax element during write tracing.
289#define CBS_TRACE_WRITE_START() \
290 int start_position; \
291 do { \
292 if (ctx->trace_enable) \
293 start_position = put_bits_count(pbc);; \
294 } while (0)
295
296// End of a syntax element for tracing, make callback.
297#define CBS_TRACE_WRITE_END() \
298 do { \
299 if (ctx->trace_enable) { \
300 int end_position = put_bits_count(pbc); \
301 av_assert0(start_position <= end_position); \
302 ctx->trace_write_callback(ctx->trace_context, pbc, \
303 end_position - start_position, \
304 name, subscripts, value); \
305 } \
306 } while (0)
307
308// End of a syntax element with no subscript entries.
309#define CBS_TRACE_WRITE_END_NO_SUBSCRIPTS() \
310 do { \
311 const int *subscripts = NULL; \
312 CBS_TRACE_WRITE_END(); \
313 } while (0)
314
315// End of a syntax element which is made up of subelements which are
316// already traced, so we are only showing the value. This forges a
317// PutBitContext to point to the position of the start of the syntax
318// element, but the other state doesn't matter because length is zero.
319#define CBS_TRACE_WRITE_END_VALUE_ONLY() \
320 do { \
321 if (ctx->trace_enable) { \
322 PutBitContext tmp; \
323 init_put_bits(&tmp, pbc->buf, start_position); \
324 skip_put_bits(&tmp, start_position); \
325 ctx->trace_write_callback(ctx->trace_context, &tmp, 0, \
326 name, subscripts, value); \
327 } \
328 } while (0)
329
330#else // CBS_TRACE
331#define CBS_TRACE_READ_START() do { } while (0)
332#define CBS_TRACE_READ_END() do { } while (0)
333#define CBS_TRACE_READ_END_NO_SUBSCRIPTS() do { } while (0)
334#define CBS_TRACE_READ_END_VALUE_ONLY() do { } while (0)
335#define CBS_TRACE_WRITE_START() do { } while (0)
336#define CBS_TRACE_WRITE_END() do { } while (0)
337#define CBS_TRACE_WRITE_END_NO_SUBSCRIPTS() do { } while (0)
338#define CBS_TRACE_WRITE_END_VALUE_ONLY() do { } while (0)
339#endif // CBS_TRACE
340
341#define TYPE_LIST(...) { __VA_ARGS__ }
342#define CBS_UNIT_TYPE_POD(type_, structure) { \
343 .nb_unit_types = 1, \
344 .unit_type.list = { type_ }, \
345 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
346 .content_size = sizeof(structure), \
347 .type.ref = { .nb_offsets = 0 }, \
348 }
349#define CBS_UNIT_RANGE_POD(range_start, range_end, structure) { \
350 .nb_unit_types = CBS_UNIT_TYPE_RANGE, \
351 .unit_type.range.start = range_start, \
352 .unit_type.range.end = range_end, \
353 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
354 .content_size = sizeof(structure), \
355 .type.ref = { .nb_offsets = 0 }, \
356 }
357
358#define CBS_UNIT_TYPES_INTERNAL_REF(types, structure, ref_field) { \
359 .nb_unit_types = FF_ARRAY_ELEMS((CodedBitstreamUnitType[])TYPE_LIST types), \
360 .unit_type.list = TYPE_LIST types, \
361 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
362 .content_size = sizeof(structure), \
363 .type.ref = { .nb_offsets = 1, \
364 .offsets = { offsetof(structure, ref_field) } }, \
365 }
366#define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field) \
367 CBS_UNIT_TYPES_INTERNAL_REF((type), structure, ref_field)
368
369#define CBS_UNIT_RANGE_INTERNAL_REF(range_start, range_end, structure, ref_field) { \
370 .nb_unit_types = CBS_UNIT_TYPE_RANGE, \
371 .unit_type.range.start = range_start, \
372 .unit_type.range.end = range_end, \
373 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
374 .content_size = sizeof(structure), \
375 .type.ref = { .nb_offsets = 1, \
376 .offsets = { offsetof(structure, ref_field) } }, \
377 }
378
379#define CBS_UNIT_TYPES_COMPLEX(types, structure, free_func) { \
380 .nb_unit_types = FF_ARRAY_ELEMS((CodedBitstreamUnitType[])TYPE_LIST types), \
381 .unit_type.list = TYPE_LIST types, \
382 .content_type = CBS_CONTENT_TYPE_COMPLEX, \
383 .content_size = sizeof(structure), \
384 .type.complex = { .content_free = free_func }, \
385 }
386#define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func) \
387 CBS_UNIT_TYPES_COMPLEX((type), structure, free_func)
388
389#define CBS_UNIT_TYPE_END_OF_LIST { .nb_unit_types = 0 }
390
391
392extern const CodedBitstreamType CBS_FUNC(type_apv);
393extern const CodedBitstreamType CBS_FUNC(type_av1);
394extern const CodedBitstreamType CBS_FUNC(type_h264);
395extern const CodedBitstreamType CBS_FUNC(type_h265);
396extern const CodedBitstreamType CBS_FUNC(type_h266);
397extern const CodedBitstreamType CBS_FUNC(type_lcevc);
398extern const CodedBitstreamType CBS_FUNC(type_jpeg);
399extern const CodedBitstreamType CBS_FUNC(type_mpeg2);
400extern const CodedBitstreamType CBS_FUNC(type_vp8);
401extern const CodedBitstreamType CBS_FUNC(type_vp9);
402
403
404#endif /* AVCODEC_CBS_INTERNAL_H */
static AVFormatContext * ctx
int32_t
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static int32_t BS_FUNC read_signed(BSCTX *bc, unsigned int n)
Return n bits from the buffer as a signed integer.
int CBS_FUNC read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, uint32_t *write_to)
@ CBS_MAX_LIST_UNIT_TYPES
@ CBS_UNIT_TYPE_RANGE
@ CBS_MAX_REF_OFFSETS
int CBS_FUNC write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, const int *subscripts, int32_t value, int32_t range_min, int32_t range_max)
CBSContentType
@ CBS_CONTENT_TYPE_INTERNAL_REFS
@ CBS_CONTENT_TYPE_COMPLEX
int CBS_FUNC write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, uint32_t value)
int CBS_FUNC read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, const int *subscripts, uint32_t *write_to, uint32_t range_min, uint32_t range_max)
int CBS_FUNC write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, const int *subscripts, uint32_t value, uint32_t range_min, uint32_t range_max)
double value
Definition eval.c:102
bitstream reader API header.
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
AVDiscard
Definition defs.h:223
cl_device_type type
void CBS_FUNC trace_header(CodedBitstreamContext *ctx, const char *name)
Definition cbs.c:481
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition cbs.h:66
#define CBS_FUNC(name)
Definition cbs.h:38
enum AVColorRange range
bitstream writer API
const char * name
Definition qsvenc.c:142
static const uint8_t header[24]
Definition sdr2.c:68
Describe the class of an AVClass context structure.
Definition log.h:76
Context structure for coded bitstream operations.
Definition cbs.h:226
Coded bitstream fragment structure, combining one or more units.
Definition cbs.h:129
int(* split_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
int(* read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
void(* flush)(CodedBitstreamContext *ctx)
enum AVCodecID codec_id
int(* write_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
void(* close)(CodedBitstreamContext *ctx)
int(* assemble_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
CodedBitstreamUnitTypeDescriptor * unit_types
const AVClass * priv_class
int(* discarded_unit)(CodedBitstreamContext *ctx, const CodedBitstreamUnit *unit, enum AVDiscard skip)
CodedBitstreamUnitType end
struct CodedBitstreamUnitTypeDescriptor::@336047356232222135040315337172251220176367310114::@154103274000131154014101000301131241246261022267 complex
size_t offsets[CBS_MAX_REF_OFFSETS]
CodedBitstreamUnitType start
int(* content_clone)(void **new_content, CodedBitstreamUnit *unit)
enum CBSContentType content_type
CodedBitstreamUnitType list[CBS_MAX_LIST_UNIT_TYPES]
union CodedBitstreamUnitTypeDescriptor::@025053026063241163272070015000137137020146347373 unit_type
void(* content_free)(AVRefStructOpaque opaque, void *content)
Coded bitstream unit structure.
Definition cbs.h:77
static int ref[MAX_W *MAX_W]
#define width
Definition dsp.h:89
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition refstruct.h:58