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