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  // Read the data from all of frag->units and assemble it into
137  // a bitstream for the whole fragment.
139  CodedBitstreamFragment *frag);
140 
141  // Reset the codec internal state.
143 
144  // Free the codec internal state.
147 
148 
149 // Helper functions for trace output.
150 
152  const char *name);
153 
155  const char *name, const int *subscripts,
156  const char *bitstring, int64_t value);
157 
158 
159 // Helper functions for read/write of common bitstream elements, including
160 // generation of trace output.
161 
163  int width, const char *name,
164  const int *subscripts, uint32_t *write_to,
165  uint32_t range_min, uint32_t range_max);
166 
168  int width, const char *name,
169  const int *subscripts, uint32_t value,
170  uint32_t range_min, uint32_t range_max);
171 
173  int width, const char *name,
174  const int *subscripts, int32_t *write_to,
175  int32_t range_min, int32_t range_max);
176 
178  int width, const char *name,
179  const int *subscripts, int32_t value,
180  int32_t range_min, int32_t range_max);
181 
182 // The largest unsigned value representable in N bits, suitable for use as
183 // range_max in the above functions.
184 #define MAX_UINT_BITS(length) ((UINT64_C(1) << (length)) - 1)
185 
186 // The largest signed value representable in N bits, suitable for use as
187 // range_max in the above functions.
188 #define MAX_INT_BITS(length) ((INT64_C(1) << ((length) - 1)) - 1)
189 
190 // The smallest signed value representable in N bits, suitable for use as
191 // range_min in the above functions.
192 #define MIN_INT_BITS(length) (-(INT64_C(1) << ((length) - 1)))
193 
194 #define TYPE_LIST(...) { __VA_ARGS__ }
195 #define CBS_UNIT_TYPE_POD(type_, structure) { \
196  .nb_unit_types = 1, \
197  .unit_type.list = { type_ }, \
198  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
199  .content_size = sizeof(structure), \
200  .type.ref = { .nb_offsets = 0 }, \
201  }
202 #define CBS_UNIT_RANGE_POD(range_start, range_end, structure) { \
203  .nb_unit_types = CBS_UNIT_TYPE_RANGE, \
204  .unit_type.range.start = range_start, \
205  .unit_type.range.end = range_end, \
206  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
207  .content_size = sizeof(structure), \
208  .type.ref = { .nb_offsets = 0 }, \
209  }
210 
211 #define CBS_UNIT_TYPES_INTERNAL_REF(types, structure, ref_field) { \
212  .nb_unit_types = FF_ARRAY_ELEMS((CodedBitstreamUnitType[])TYPE_LIST types), \
213  .unit_type.list = TYPE_LIST types, \
214  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
215  .content_size = sizeof(structure), \
216  .type.ref = { .nb_offsets = 1, \
217  .offsets = { offsetof(structure, ref_field) } }, \
218  }
219 #define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field) \
220  CBS_UNIT_TYPES_INTERNAL_REF((type), structure, ref_field)
221 
222 #define CBS_UNIT_RANGE_INTERNAL_REF(range_start, range_end, structure, ref_field) { \
223  .nb_unit_types = CBS_UNIT_TYPE_RANGE, \
224  .unit_type.range.start = range_start, \
225  .unit_type.range.end = range_end, \
226  .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
227  .content_size = sizeof(structure), \
228  .type.ref = { .nb_offsets = 1, \
229  .offsets = { offsetof(structure, ref_field) } }, \
230  }
231 
232 #define CBS_UNIT_TYPES_COMPLEX(types, structure, free_func) { \
233  .nb_unit_types = FF_ARRAY_ELEMS((CodedBitstreamUnitType[])TYPE_LIST types), \
234  .unit_type.list = TYPE_LIST types, \
235  .content_type = CBS_CONTENT_TYPE_COMPLEX, \
236  .content_size = sizeof(structure), \
237  .type.complex = { .content_free = free_func }, \
238  }
239 #define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func) \
240  CBS_UNIT_TYPES_COMPLEX((type), structure, free_func)
241 
242 #define CBS_UNIT_TYPE_END_OF_LIST { .nb_unit_types = 0 }
243 
244 
251 
252 
253 #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:145
CodedBitstreamType::priv_class
const AVClass * priv_class
Definition: cbs_internal.h:108
CBS_MAX_REF_OFFSETS
@ CBS_MAX_REF_OFFSETS
Definition: cbs_internal.h:48
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: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: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:622
CodedBitstreamUnitTypeDescriptor::end
CodedBitstreamUnitType end
Definition: cbs_internal.h:68
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:586
GetBitContext
Definition: get_bits.h:61
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:493
CodedBitstreamUnitTypeDescriptor::list
CodedBitstreamUnitType list[CBS_MAX_LIST_UNIT_TYPES]
Definition: cbs_internal.h:63
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: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: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:543
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
CBS_UNIT_TYPE_RANGE
@ CBS_UNIT_TYPE_RANGE
Definition: cbs_internal.h:52
CodedBitstreamType::read_unit
int(* read_unit)(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_internal.h:127
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: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:484
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
CodedBitstreamUnitTypeDescriptor::range
struct CodedBitstreamUnitTypeDescriptor::@33::@35 range
ff_cbs_type_av1
const CodedBitstreamType ff_cbs_type_av1
Definition: cbs_av1.c:1351
CodedBitstreamUnitTypeDescriptor::unit_type
union CodedBitstreamUnitTypeDescriptor::@33 unit_type
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:665
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:142
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:138
CodedBitstreamType::split_fragment
int(* split_fragment)(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_internal.h:121
int
int
Definition: ffmpeg_filter.c:156
put_bits.h
ff_cbs_type_h264
const CodedBitstreamType ff_cbs_type_h264
Definition: cbs_h2645.c:1434