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