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