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