FFmpeg
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 <stdint.h>
23 
24 #include "libavutil/buffer.h"
25 #include "libavutil/log.h"
26 
27 #include "cbs.h"
28 #include "codec_id.h"
29 #include "get_bits.h"
30 #include "put_bits.h"
31 
32 
34  // Unit content may contain some references to other structures, but all
35  // managed via buffer reference counting. The descriptor defines the
36  // structure offsets of every buffer reference.
38  // Unit content is something more complex. The descriptor defines
39  // special functions to manage the content.
41 };
42 
43 enum {
44  // Maximum number of unit types described by the same non-range
45  // unit type descriptor.
47  // Maximum number of reference buffer offsets in any one unit.
49  // Special value used in a unit type descriptor to indicate that it
50  // applies to a large range of types rather than a set of discrete
51  // values.
53 };
54 
55 typedef const struct CodedBitstreamUnitTypeDescriptor {
56  // Number of entries in the unit_types array, or the special value
57  // CBS_UNIT_TYPE_RANGE to indicate that the range fields should be
58  // used instead.
60 
61  union {
62  // Array of unit types that this entry describes.
64  // Start and end of unit type range, used if nb_unit_types is
65  // CBS_UNIT_TYPE_RANGE.
66  struct {
69  } range;
70  } unit_type;
71 
72  // The type of content described.
74  // The size of the structure which should be allocated to contain
75  // the decomposed content of this type of unit.
76  size_t content_size;
77 
78  union {
79  // This union's state is determined by content_type:
80  // ref for CBS_CONTENT_TYPE_INTERNAL_REFS,
81  // complex for CBS_CONTENT_TYPE_COMPLEX.
82  struct {
83  // Number of entries in the ref_offsets array.
84  // May be zero, then the structure is POD-like.
86  // The structure must contain two adjacent elements:
87  // type *field;
88  // AVBufferRef *field_ref;
89  // where field points to something in the buffer referred to by
90  // field_ref. This offset is then set to offsetof(struct, field).
92  } ref;
93 
94  struct {
95  void (*content_free)(void *opaque, uint8_t *data);
97  } complex;
98  } type;
100 
101 typedef struct CodedBitstreamType {
103 
104  // A class for the private data, used to declare private AVOptions.
105  // This field is NULL for types that do not declare any options.
106  // If this field is non-NULL, the first member of the filter private data
107  // must be a pointer to AVClass.
109 
111 
112  // List of unit type descriptors for this codec.
113  // Terminated by a descriptor with nb_unit_types equal to zero.
115 
116  // Split frag->data into coded bitstream units, creating the
117  // frag->units array. Fill data but not content on each unit.
118  // The header argument should be set if the fragment came from
119  // a header block, which may require different parsing for some
120  // codecs (e.g. the AVCC header in H.264).
123  int header);
124 
125  // Read the unit->data bitstream and decompose it, creating
126  // unit->content.
128  CodedBitstreamUnit *unit);
129 
130  // Write the data bitstream from unit->content into pbc.
131  // Return value AVERROR(ENOSPC) indicates that pbc was too small.
133  CodedBitstreamUnit *unit,
134  PutBitContext *pbc);
135 
136  // Return 1 when the unit should be dropped according to 'skip',
137  // 0 otherwise.
139  const CodedBitstreamUnit *unit,
140  enum AVDiscard skip);
141 
142  // Read the data from all of frag->units and assemble it into
143  // a bitstream for the whole fragment.
145  CodedBitstreamFragment *frag);
146 
147  // Reset the codec internal state.
149 
150  // Free the codec internal state.
153 
154 
155 // Helper functions for trace output.
156 
158  const char *name);
159 
161  const char *name, const int *subscripts,
162  const char *bitstring, int64_t value);
163 
164 
165 // Helper functions for read/write of common bitstream elements, including
166 // generation of trace output. The simple functions are equivalent to
167 // their non-simple counterparts except that their range is unrestricted
168 // (i.e. only limited by the amount of bits used) and they lack
169 // the ability to use subscripts.
170 
172  int width, const char *name,
173  const int *subscripts, uint32_t *write_to,
174  uint32_t range_min, uint32_t range_max);
175 
177  int width, const char *name, uint32_t *write_to);
178 
180  int width, const char *name,
181  const int *subscripts, uint32_t value,
182  uint32_t range_min, uint32_t range_max);
183 
185  int width, const char *name, uint32_t value);
186 
188  int width, const char *name,
189  const int *subscripts, int32_t *write_to,
190  int32_t range_min, int32_t range_max);
191 
193  int width, const char *name,
194  const int *subscripts, int32_t value,
195  int32_t range_min, int32_t range_max);
196 
197 // The largest unsigned value representable in N bits, suitable for use as
198 // range_max in the above functions.
199 #define MAX_UINT_BITS(length) ((UINT64_C(1) << (length)) - 1)
200 
201 // The largest signed value representable in N bits, suitable for use as
202 // range_max in the above functions.
203 #define MAX_INT_BITS(length) ((INT64_C(1) << ((length) - 1)) - 1)
204 
205 // The smallest signed value representable in N bits, suitable for use as
206 // range_min in the above functions.
207 #define MIN_INT_BITS(length) (-(INT64_C(1) << ((length) - 1)))
208 
209 #define TYPE_LIST(...) { __VA_ARGS__ }
210 #define CBS_UNIT_TYPE_POD(type_, structure) { \
211  .nb_unit_types = 1, \
212  .unit_type.list = { type_ }, \
213  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
214  .content_size = sizeof(structure), \
215  .type.ref = { .nb_offsets = 0 }, \
216  }
217 #define CBS_UNIT_RANGE_POD(range_start, range_end, structure) { \
218  .nb_unit_types = CBS_UNIT_TYPE_RANGE, \
219  .unit_type.range.start = range_start, \
220  .unit_type.range.end = range_end, \
221  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
222  .content_size = sizeof(structure), \
223  .type.ref = { .nb_offsets = 0 }, \
224  }
225 
226 #define CBS_UNIT_TYPES_INTERNAL_REF(types, structure, ref_field) { \
227  .nb_unit_types = FF_ARRAY_ELEMS((CodedBitstreamUnitType[])TYPE_LIST types), \
228  .unit_type.list = TYPE_LIST types, \
229  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
230  .content_size = sizeof(structure), \
231  .type.ref = { .nb_offsets = 1, \
232  .offsets = { offsetof(structure, ref_field) } }, \
233  }
234 #define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field) \
235  CBS_UNIT_TYPES_INTERNAL_REF((type), structure, ref_field)
236 
237 #define CBS_UNIT_RANGE_INTERNAL_REF(range_start, range_end, structure, ref_field) { \
238  .nb_unit_types = CBS_UNIT_TYPE_RANGE, \
239  .unit_type.range.start = range_start, \
240  .unit_type.range.end = range_end, \
241  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
242  .content_size = sizeof(structure), \
243  .type.ref = { .nb_offsets = 1, \
244  .offsets = { offsetof(structure, ref_field) } }, \
245  }
246 
247 #define CBS_UNIT_TYPES_COMPLEX(types, structure, free_func) { \
248  .nb_unit_types = FF_ARRAY_ELEMS((CodedBitstreamUnitType[])TYPE_LIST types), \
249  .unit_type.list = TYPE_LIST types, \
250  .content_type = CBS_CONTENT_TYPE_COMPLEX, \
251  .content_size = sizeof(structure), \
252  .type.complex = { .content_free = free_func }, \
253  }
254 #define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func) \
255  CBS_UNIT_TYPES_COMPLEX((type), structure, free_func)
256 
257 #define CBS_UNIT_TYPE_END_OF_LIST { .nb_unit_types = 0 }
258 
259 
267 
268 
269 #endif /* AVCODEC_CBS_INTERNAL_H */
CodedBitstreamUnitTypeDescriptor::start
CodedBitstreamUnitType start
Definition: cbs_internal.h:67
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:151
CodedBitstreamType::priv_class
const AVClass * priv_class
Definition: cbs_internal.h:108
ff_cbs_write_simple_unsigned
int ff_cbs_write_simple_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, int width, const char *name, uint32_t value)
Definition: cbs.c:647
CodedBitstreamUnitTypeDescriptor::content_type
enum CBSContentType content_type
Definition: cbs_internal.h:73
ff_cbs_type_vp9
const CodedBitstreamType ff_cbs_type_vp9
Definition: cbs_vp9.c:658
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:174
data
const char data[16]
Definition: mxf.c:148
CBS_UNIT_TYPE_RANGE
@ CBS_UNIT_TYPE_RANGE
Definition: cbs_internal.h:52
cbs.h
CodedBitstreamUnitTypeDescriptor::content_free
void(* content_free)(void *opaque, uint8_t *data)
Definition: cbs_internal.h:95
ff_cbs_read_signed
int ff_cbs_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:654
CodedBitstreamUnitTypeDescriptor::end
CodedBitstreamUnitType end
Definition: cbs_internal.h:68
CodedBitstreamUnitTypeDescriptor::ref
struct CodedBitstreamUnitTypeDescriptor::@36::@38 ref
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:70
ff_cbs_write_unsigned
int ff_cbs_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:611
GetBitContext
Definition: get_bits.h:108
CodedBitstreamType::discarded_unit
int(* discarded_unit)(CodedBitstreamContext *ctx, const CodedBitstreamUnit *unit, enum AVDiscard skip)
Definition: cbs_internal.h:138
CBS_CONTENT_TYPE_INTERNAL_REFS
@ CBS_CONTENT_TYPE_INTERNAL_REFS
Definition: cbs_internal.h:37
ff_cbs_trace_syntax_element
void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position, const char *name, const int *subscripts, const char *bitstring, int64_t value)
Definition: cbs.c:499
CodedBitstreamUnitTypeDescriptor::list
CodedBitstreamUnitType list[CBS_MAX_LIST_UNIT_TYPES]
Definition: cbs_internal.h:63
CodedBitstreamUnitTypeDescriptor::unit_type
union CodedBitstreamUnitTypeDescriptor::@35 unit_type
CBS_MAX_LIST_UNIT_TYPES
@ CBS_MAX_LIST_UNIT_TYPES
Definition: cbs_internal.h:46
CodedBitstreamUnitTypeDescriptor
Definition: cbs_internal.h:55
ff_cbs_type_mpeg2
const CodedBitstreamType ff_cbs_type_mpeg2
Definition: cbs_mpeg2.c:427
codec_id.h
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:122
width
#define width
CodedBitstreamUnitTypeDescriptor::nb_unit_types
int nb_unit_types
Definition: cbs_internal.h:59
ff_cbs_read_unsigned
int ff_cbs_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:595
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
CodedBitstreamType::codec_id
enum AVCodecID codec_id
Definition: cbs_internal.h:102
PutBitContext
Definition: put_bits.h:50
CodedBitstreamType::read_unit
int(* read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:127
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
CBS_MAX_REF_OFFSETS
@ CBS_MAX_REF_OFFSETS
Definition: cbs_internal.h:48
CodedBitstreamUnitTypeDescriptor::content_clone
int(* content_clone)(AVBufferRef **ref, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:96
CodedBitstreamUnitTypeDescriptor::offsets
size_t offsets[CBS_MAX_REF_OFFSETS]
Definition: cbs_internal.h:91
ff_cbs_trace_header
void ff_cbs_trace_header(CodedBitstreamContext *ctx, const char *name)
Definition: cbs.c:490
CBSContentType
CBSContentType
Definition: cbs_internal.h:33
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
ff_cbs_type_jpeg
const CodedBitstreamType ff_cbs_type_jpeg
Definition: cbs_jpeg.c:435
CodedBitstreamType::unit_types
const CodedBitstreamUnitTypeDescriptor * unit_types
Definition: cbs_internal.h:114
CBS_CONTENT_TYPE_COMPLEX
@ CBS_CONTENT_TYPE_COMPLEX
Definition: cbs_internal.h:40
header
static const uint8_t header[24]
Definition: sdr2.c:67
buffer.h
CodedBitstreamType
Definition: cbs_internal.h:101
ff_cbs_type_av1
const CodedBitstreamType ff_cbs_type_av1
Definition: cbs_av1.c:1368
ff_cbs_type_h266
const CodedBitstreamType ff_cbs_type_h266
Definition: cbs_h2645.c:2089
CodedBitstreamType::priv_data_size
size_t priv_data_size
Definition: cbs_internal.h:110
CodedBitstreamUnitTypeDescriptor::nb_offsets
int nb_offsets
Definition: cbs_internal.h:85
log.h
CodedBitstreamType::write_unit
int(* write_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_internal.h:132
ff_cbs_write_signed
int ff_cbs_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:697
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
ff_cbs_read_simple_unsigned
int ff_cbs_read_simple_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc, int width, const char *name, uint32_t *write_to)
Definition: cbs.c:604
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:47
ff_cbs_type_h265
const CodedBitstreamType ff_cbs_type_h265
Definition: cbs_h2645.c:2072
CodedBitstreamType::flush
void(* flush)(CodedBitstreamContext *ctx)
Definition: cbs_internal.h:148
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
CodedBitstreamUnitTypeDescriptor::content_size
size_t content_size
Definition: cbs_internal.h:76
int32_t
int32_t
Definition: audioconvert.c:56
CodedBitstreamType::assemble_fragment
int(* assemble_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_internal.h:144
CodedBitstreamUnitTypeDescriptor::complex
struct CodedBitstreamUnitTypeDescriptor::@36::@39 complex
CodedBitstreamType::split_fragment
int(* split_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_internal.h:121
AVDiscard
AVDiscard
Definition: defs.h:202
CodedBitstreamUnitTypeDescriptor::range
struct CodedBitstreamUnitTypeDescriptor::@35::@37 range
int
int
Definition: ffmpeg_filter.c:331
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
ff_cbs_type_h264
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:2055
CodedBitstreamUnitTypeDescriptor::type
union CodedBitstreamUnitTypeDescriptor::@36 type