Update for 5.1.8
[ffmpeg.git] / libavcodec / cbs_internal.h
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
33 enum CBSContentType {
34 // Unit content is a simple structure.
35 CBS_CONTENT_TYPE_POD,
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.
39 CBS_CONTENT_TYPE_INTERNAL_REFS,
40 // Unit content is something more complex. The descriptor defines
41 // special functions to manage the content.
42 CBS_CONTENT_TYPE_COMPLEX,
43 };
44
45 enum {
46 // Maximum number of unit types described by the same unit type
47 // descriptor.
48 CBS_MAX_UNIT_TYPES = 3,
49 // Maximum number of reference buffer offsets in any one unit.
50 CBS_MAX_REF_OFFSETS = 2,
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.
54 CBS_UNIT_TYPE_RANGE = -1,
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.
61 int nb_unit_types;
62
63 // Array of unit types that this entry describes.
64 const CodedBitstreamUnitType unit_types[CBS_MAX_UNIT_TYPES];
65
66 // Start and end of unit type range, used if nb_unit_types is
67 // CBS_UNIT_TYPE_RANGE.
68 const CodedBitstreamUnitType unit_type_range_start;
69 const CodedBitstreamUnitType unit_type_range_end;
70
71 // The type of content described.
72 enum CBSContentType content_type;
73 // The size of the structure which should be allocated to contain
74 // the decomposed content of this type of unit.
75 size_t content_size;
76
77 // Number of entries in the ref_offsets array. Only used if the
78 // content_type is CBS_CONTENT_TYPE_INTERNAL_REFS.
79 int nb_ref_offsets;
80 // The structure must contain two adjacent elements:
81 // type *field;
82 // AVBufferRef *field_ref;
83 // where field points to something in the buffer referred to by
84 // field_ref. This offset is then set to offsetof(struct, field).
85 size_t ref_offsets[CBS_MAX_REF_OFFSETS];
86
87 void (*content_free)(void *opaque, uint8_t *data);
88 int (*content_clone)(AVBufferRef **ref, CodedBitstreamUnit *unit);
89 } CodedBitstreamUnitTypeDescriptor;
90
91 typedef struct CodedBitstreamType {
92 enum AVCodecID codec_id;
93
94 // A class for the private data, used to declare private AVOptions.
95 // This field is NULL for types that do not declare any options.
96 // If this field is non-NULL, the first member of the filter private data
97 // must be a pointer to AVClass.
98 const AVClass *priv_class;
99
100 size_t priv_data_size;
101
102 // List of unit type descriptors for this codec.
103 // Terminated by a descriptor with nb_unit_types equal to zero.
104 const CodedBitstreamUnitTypeDescriptor *unit_types;
105
106 // Split frag->data into coded bitstream units, creating the
107 // frag->units array. Fill data but not content on each unit.
108 // The header argument should be set if the fragment came from
109 // a header block, which may require different parsing for some
110 // codecs (e.g. the AVCC header in H.264).
111 int (*split_fragment)(CodedBitstreamContext *ctx,
112 CodedBitstreamFragment *frag,
113 int header);
114
115 // Read the unit->data bitstream and decompose it, creating
116 // unit->content.
117 int (*read_unit)(CodedBitstreamContext *ctx,
118 CodedBitstreamUnit *unit);
119
120 // Write the data bitstream from unit->content into pbc.
121 // Return value AVERROR(ENOSPC) indicates that pbc was too small.
122 int (*write_unit)(CodedBitstreamContext *ctx,
123 CodedBitstreamUnit *unit,
124 PutBitContext *pbc);
125
126 // Read the data from all of frag->units and assemble it into
127 // a bitstream for the whole fragment.
128 int (*assemble_fragment)(CodedBitstreamContext *ctx,
129 CodedBitstreamFragment *frag);
130
131 // Reset the codec internal state.
132 void (*flush)(CodedBitstreamContext *ctx);
133
134 // Free the codec internal state.
135 void (*close)(CodedBitstreamContext *ctx);
136 } CodedBitstreamType;
137
138
139 // Helper functions for trace output.
140
141 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
142 const char *name);
143
144 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
145 const char *name, const int *subscripts,
146 const char *bitstring, int64_t value);
147
148
149 // Helper functions for read/write of common bitstream elements, including
150 // generation of trace output.
151
152 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
153 int width, const char *name,
154 const int *subscripts, uint32_t *write_to,
155 uint32_t range_min, uint32_t range_max);
156
157 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
158 int width, const char *name,
159 const int *subscripts, uint32_t value,
160 uint32_t range_min, uint32_t range_max);
161
162 int ff_cbs_read_signed(CodedBitstreamContext *ctx, GetBitContext *gbc,
163 int width, const char *name,
164 const int *subscripts, int32_t *write_to,
165 int32_t range_min, int32_t range_max);
166
167 int ff_cbs_write_signed(CodedBitstreamContext *ctx, PutBitContext *pbc,
168 int width, const char *name,
169 const int *subscripts, int32_t value,
170 int32_t range_min, int32_t range_max);
171
172 // The largest unsigned value representable in N bits, suitable for use as
173 // range_max in the above functions.
174 #define MAX_UINT_BITS(length) ((UINT64_C(1) << (length)) - 1)
175
176 // The largest signed value representable in N bits, suitable for use as
177 // range_max in the above functions.
178 #define MAX_INT_BITS(length) ((INT64_C(1) << ((length) - 1)) - 1)
179
180 // The smallest signed value representable in N bits, suitable for use as
181 // range_min in the above functions.
182 #define MIN_INT_BITS(length) (-(INT64_C(1) << ((length) - 1)))
183
184
185 #define CBS_UNIT_TYPE_POD(type, structure) { \
186 .nb_unit_types = 1, \
187 .unit_types = { type }, \
188 .content_type = CBS_CONTENT_TYPE_POD, \
189 .content_size = sizeof(structure), \
190 }
191 #define CBS_UNIT_TYPE_INTERNAL_REF(type, structure, ref_field) { \
192 .nb_unit_types = 1, \
193 .unit_types = { type }, \
194 .content_type = CBS_CONTENT_TYPE_INTERNAL_REFS, \
195 .content_size = sizeof(structure), \
196 .nb_ref_offsets = 1, \
197 .ref_offsets = { offsetof(structure, ref_field) }, \
198 }
199 #define CBS_UNIT_TYPE_COMPLEX(type, structure, free_func) { \
200 .nb_unit_types = 1, \
201 .unit_types = { type }, \
202 .content_type = CBS_CONTENT_TYPE_COMPLEX, \
203 .content_size = sizeof(structure), \
204 .content_free = free_func, \
205 }
206 #define CBS_UNIT_TYPE_END_OF_LIST { .nb_unit_types = 0 }
207
208
209 extern const CodedBitstreamType ff_cbs_type_av1;
210 extern const CodedBitstreamType ff_cbs_type_h264;
211 extern const CodedBitstreamType ff_cbs_type_h265;
212 extern const CodedBitstreamType ff_cbs_type_jpeg;
213 extern const CodedBitstreamType ff_cbs_type_mpeg2;
214 extern const CodedBitstreamType ff_cbs_type_vp9;
215
216
217 #endif /* AVCODEC_CBS_INTERNAL_H */