FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vlc.h
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 2 2 100.0%
Functions: 1 1 100.0%
Branches: 0 0 -%

Line Branch Exec Source
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_VLC_H
20 #define AVCODEC_VLC_H
21
22 #include <stddef.h>
23 #include <stdint.h>
24
25 #include "libavutil/macros.h"
26
27 #define VLC_MULTI_MAX_SYMBOLS 6
28
29 // When changing this, be sure to also update tableprint_vlc.h accordingly.
30 typedef int16_t VLCBaseType;
31
32 typedef struct VLCElem {
33 union {
34 /// The struct is for use as ordinary VLC (with get_vlc2())
35 struct {
36 VLCBaseType sym;
37 VLCBaseType len;
38 };
39 /// This struct is for use as run-length VLC (with GET_RL_VLC)
40 struct {
41 int16_t level;
42 int8_t len8;
43 uint8_t run;
44 };
45 };
46 } VLCElem;
47
48 typedef VLCElem RL_VLC_ELEM;
49
50 typedef struct VLC {
51 int bits;
52 VLCElem *table;
53 int table_size, table_allocated;
54 } VLC;
55
56 typedef struct VLC_MULTI_ELEM {
57 union {
58 uint8_t val8[VLC_MULTI_MAX_SYMBOLS];
59 uint16_t val16[VLC_MULTI_MAX_SYMBOLS / 2];
60 };
61 int8_t len; // -31,32
62 uint8_t num;
63 } VLC_MULTI_ELEM;
64
65 typedef struct VLC_MULTI {
66 VLC_MULTI_ELEM *table;
67 int table_size, table_allocated;
68 } VLC_MULTI;
69
70 #define vlc_init(vlc, nb_bits, nb_codes, \
71 bits, bits_wrap, bits_size, \
72 codes, codes_wrap, codes_size, \
73 flags) \
74 ff_vlc_init_sparse(vlc, nb_bits, nb_codes, \
75 bits, bits_wrap, bits_size, \
76 codes, codes_wrap, codes_size, \
77 NULL, 0, 0, flags)
78
79 /**
80 * Build VLC decoding tables suitable for use with get_vlc2().
81 *
82 * @param[in,out] vlc The VLC to be initialized; table and table_allocated
83 * must have been set when initializing a static VLC,
84 * otherwise this will be treated as uninitialized.
85 * @param[in] nb_bits The number of bits to use for the VLC table;
86 * higher values take up more memory and cache, but
87 * allow to read codes with fewer reads.
88 * Corresponds to the `bits` parameter of get_vlc2().
89 * @param[in] nb_codes The number of provided bits, codes and (if supplied)
90 * symbol entries.
91 * @param[in] bits The lengths (in bits) of the codes. Entries > 0
92 * correspond to valid codes; entries == 0 will be skipped.
93 * @param[in] bits_wrap Stride (in bytes) of the bits table.
94 * @param[in] codes_size Size of the bits. 1, 2 and 4 are supported.
95 * @param[in] codes Table which gives the bit pattern of of each vlc code.
96 * @param[in] codes_wrap Stride (in bytes) of the codes table.
97 * @param[in] codes_size Size of the codes. 1, 2 and 4 are supported.
98 * @param[in] symbols The symbols, i.e. what is returned from get_vlc2()
99 * when the corresponding code is encountered.
100 * May be NULL, then 0, 1, 2, 3, 4,... will be used.
101 * @param[in] symbols_wrap Stride (in bytes) of the symbols table.
102 * @param[in] symbols_size Size of the symbols. 1 and 2 are supported.
103 * @param[in] flags A combination of the VLC_INIT_* flags.
104 *
105 * 'wrap' and 'size' make it possible to use any memory configuration and types
106 * (byte/word/int) to store the 'bits', 'codes', and 'symbols' tables.
107 */
108 int ff_vlc_init_sparse(VLC *vlc, int nb_bits, int nb_codes,
109 const void *bits, int bits_wrap, int bits_size,
110 const void *codes, int codes_wrap, int codes_size,
111 const void *symbols, int symbols_wrap, int symbols_size,
112 int flags);
113
114 /**
115 * Build VLC decoding tables suitable for use with get_vlc2()
116 *
117 * This function takes lengths and symbols and calculates the codes from them.
118 * For this the input lengths and symbols have to be sorted according to "left
119 * nodes in the corresponding tree first".
120 *
121 * @param[in,out] vlc The VLC to be initialized; table and table_allocated
122 * must have been set when initializing a static VLC,
123 * otherwise this will be treated as uninitialized.
124 * @param[in] nb_bits The number of bits to use for the VLC table;
125 * higher values take up more memory and cache, but
126 * allow to read codes with fewer reads.
127 * @param[in] nb_codes The number of provided length and (if supplied) symbol
128 * entries.
129 * @param[in] lens The lengths of the codes. Entries > 0 correspond to
130 * valid codes; entries == 0 will be skipped and entries
131 * with len < 0 indicate that the tree is incomplete and
132 * has an open end of length -len at this position.
133 * @param[in] lens_wrap Stride (in bytes) of the lengths.
134 * @param[in] symbols The symbols, i.e. what is returned from get_vlc2()
135 * when the corresponding code is encountered.
136 * May be NULL, then 0, 1, 2, 3, 4,... will be used.
137 * @param[in] symbols_wrap Stride (in bytes) of the symbols.
138 * @param[in] symbols_size Size of the symbols. 1 and 2 are supported.
139 * @param[in] offset An offset to apply to all the valid symbols.
140 * @param[in] flags A combination of the VLC_INIT_* flags; notice that
141 * VLC_INIT_INPUT_LE is pointless and ignored.
142 */
143 int ff_vlc_init_from_lengths(VLC *vlc, int nb_bits, int nb_codes,
144 const int8_t *lens, int lens_wrap,
145 const void *symbols, int symbols_wrap, int symbols_size,
146 int offset, int flags, void *logctx);
147
148 /**
149 * Build VLC decoding tables suitable for use with get_vlc_multi()
150 *
151 * This function takes lengths and symbols and calculates the codes from them.
152 * For this the input lengths and symbols have to be sorted according to "left
153 * nodes in the corresponding tree first".
154 *
155 * @param[in,out] vlc The VLC to be initialized; table and table_allocated
156 * must have been set when initializing a static VLC,
157 * otherwise this will be treated as uninitialized.
158 * @param[in,out] multi The VLC_MULTI to be initialized; table and table_allocated
159 * must have been set when initializing a static VLC,
160 * otherwise this will be treated as uninitialized.
161 * @param[in] nb_bits The number of bits to use for the VLC table;
162 * higher values take up more memory and cache, but
163 * allow to read codes with fewer reads.
164 * @param[in] nb_elems The max possible number of elements.
165 * @param[in] nb_codes The number of provided length and (if supplied) symbol
166 * entries.
167 * @param[in] lens The lengths of the codes. Entries > 0 correspond to
168 * valid codes; entries == 0 will be skipped and entries
169 * with len < 0 indicate that the tree is incomplete and
170 * has an open end of length -len at this position.
171 * @param[in] lens_wrap Stride (in bytes) of the lengths.
172 * @param[in] symbols The symbols, i.e. what is returned from get_vlc2()
173 * when the corresponding code is encountered.
174 * May be NULL, then 0, 1, 2, 3, 4,... will be used.
175 * @param[in] symbols_wrap Stride (in bytes) of the symbols.
176 * @param[in] symbols_size Size of the symbols. 1 and 2 are supported.
177 * @param[in] offset An offset to apply to all the valid symbols.
178 * @param[in] flags A combination of the VLC_INIT_* flags; notice that
179 * VLC_INIT_INPUT_LE is pointless and ignored.
180 */
181 int ff_vlc_init_multi_from_lengths(VLC *vlc, VLC_MULTI *multi, int nb_bits, int nb_elems,
182 int nb_codes, const int8_t *lens, int lens_wrap,
183 const void *symbols, int symbols_wrap, int symbols_size,
184 int offset, int flags, void *logctx);
185
186
187 void ff_vlc_free_multi(VLC_MULTI *vlc);
188 void ff_vlc_free(VLC *vlc);
189
190 #define VLC_INIT_USE_STATIC 1
191 #define VLC_INIT_STATIC_OVERLONG (2 | VLC_INIT_USE_STATIC)
192 /* If VLC_INIT_INPUT_LE is set, the LSB bit of the codes used to
193 * initialize the VLC table is the first bit to be read. */
194 #define VLC_INIT_INPUT_LE 4
195 /* If set the VLC is intended for a little endian bitstream reader. */
196 #define VLC_INIT_OUTPUT_LE 8
197 #define VLC_INIT_LE (VLC_INIT_INPUT_LE | VLC_INIT_OUTPUT_LE)
198
199 /**
200 * For static VLCs, the number of bits can often be hardcoded
201 * at each get_vlc2() callsite. Then using a full VLC would be uneconomical,
202 * because only VLC.table would ever be accessed after initialization.
203 * The following functions provide wrappers around the relevant ff_vlc_init_*
204 * functions suitable for said task.
205 *
206 * The ff_vlc_init_tables_* functions are intended to be used for initializing
207 * a series of VLCs. The user initializes a VLCInitState with the details
208 * about the underlying array of VLCElem; it is automatically updated by
209 * the ff_vlc_init_tables_* functions (i.e. table is incremented and size
210 * decremented by the number of elements of the current table).
211 * The VLC_INIT_STATIC_OVERLONG flag is also automatically added.
212 * These functions return a pointer to the table just initialized,
213 * potentially to be used in arrays of pointer to VLC tables.
214 *
215 * The ff_vlc_init_table_* functions are intended to be used for initializing
216 * a single VLC table, given by table and table_size. The VLC_INIT_USE_STATIC
217 * flag is automatically added.
218 */
219
220 typedef struct VLCInitState {
221 VLCElem *table; ///< points to where the next VLC table will be placed
222 unsigned size; ///< remaining number of elements in table
223 } VLCInitState;
224
225 #define VLC_INIT_STATE(_table) { .table = (_table), .size = FF_ARRAY_ELEMS(_table) }
226
227 void ff_vlc_init_table_from_lengths(VLCElem table[], int table_size,
228 int nb_bits, int nb_codes,
229 const int8_t *lens, int lens_wrap,
230 const void *symbols, int symbols_wrap, int symbols_size,
231 int offset, int flags);
232
233 const VLCElem *ff_vlc_init_tables_from_lengths(VLCInitState *state,
234 int nb_bits, int nb_codes,
235 const int8_t *lens, int lens_wrap,
236 const void *symbols, int symbols_wrap, int symbols_size,
237 int offset, int flags);
238
239 void ff_vlc_init_table_sparse(VLCElem table[], int table_size,
240 int nb_bits, int nb_codes,
241 const void *bits, int bits_wrap, int bits_size,
242 const void *codes, int codes_wrap, int codes_size,
243 const void *symbols, int symbols_wrap, int symbols_size,
244 int flags);
245
246 const VLCElem *ff_vlc_init_tables_sparse(VLCInitState *state,
247 int nb_bits, int nb_codes,
248 const void *bits, int bits_wrap, int bits_size,
249 const void *codes, int codes_wrap, int codes_size,
250 const void *symbols, int symbols_wrap, int symbols_size,
251 int flags);
252
253 static inline
254 13943 const VLCElem *ff_vlc_init_tables(VLCInitState *state,
255 int nb_bits, int nb_codes,
256 const void *bits, int bits_wrap, int bits_size,
257 const void *codes, int codes_wrap, int codes_size,
258 int flags)
259 {
260 13943 return ff_vlc_init_tables_sparse(state, nb_bits, nb_codes,
261 bits, bits_wrap, bits_size,
262 codes, codes_wrap, codes_size,
263 NULL, 0, 0, flags);
264 }
265
266 #define VLC_INIT_STATIC_SPARSE_TABLE(vlc_table, nb_bits, nb_codes, \
267 bits, bits_wrap, bits_size, \
268 codes, codes_wrap, codes_size, \
269 symbols, symbols_wrap, symbols_size, \
270 flags) \
271 ff_vlc_init_table_sparse(vlc_table, FF_ARRAY_ELEMS(vlc_table), \
272 (nb_bits), (nb_codes), \
273 (bits), (bits_wrap), (bits_size), \
274 (codes), (codes_wrap), (codes_size), \
275 (symbols), (symbols_wrap), (symbols_size), \
276 (flags))
277
278 #define VLC_INIT_STATIC_TABLE(vlc_table, nb_bits, nb_codes, \
279 bits, bits_wrap, bits_size, \
280 codes, codes_wrap, codes_size, \
281 flags) \
282 ff_vlc_init_table_sparse(vlc_table, FF_ARRAY_ELEMS(vlc_table), \
283 (nb_bits), (nb_codes), \
284 (bits), (bits_wrap), (bits_size), \
285 (codes), (codes_wrap), (codes_size), \
286 NULL, 0, 0, (flags))
287
288 #define VLC_INIT_STATIC_TABLE_FROM_LENGTHS(vlc_table, nb_bits, nb_codes, \
289 lens, lens_wrap, \
290 syms, syms_wrap, syms_size, \
291 offset, flags) \
292 ff_vlc_init_table_from_lengths(vlc_table, FF_ARRAY_ELEMS(vlc_table), \
293 (nb_bits), (nb_codes), \
294 (lens), (lens_wrap), \
295 (syms), (syms_wrap), (syms_size), \
296 (offset), (flags))
297
298 #endif /* AVCODEC_VLC_H */
299