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 <stdint.h>
23 
24 #define VLC_MULTI_MAX_SYMBOLS 6
25 
26 // When changing this, be sure to also update tableprint_vlc.h accordingly.
27 typedef int16_t VLCBaseType;
28 
29 typedef struct VLCElem {
31 } VLCElem;
32 
33 typedef struct VLC {
34  int bits;
37 } VLC;
38 
39 typedef struct VLC_MULTI_ELEM {
41  int8_t len; // -31,32
42  uint8_t num;
44 
45 typedef struct VLC_MULTI {
48 } VLC_MULTI;
49 
50 typedef struct RL_VLC_ELEM {
51  int16_t level;
52  int8_t len;
53  uint8_t run;
54 } RL_VLC_ELEM;
55 
56 #define vlc_init(vlc, nb_bits, nb_codes, \
57  bits, bits_wrap, bits_size, \
58  codes, codes_wrap, codes_size, \
59  flags) \
60  ff_vlc_init_sparse(vlc, nb_bits, nb_codes, \
61  bits, bits_wrap, bits_size, \
62  codes, codes_wrap, codes_size, \
63  NULL, 0, 0, flags)
64 
65 /**
66  * Build VLC decoding tables suitable for use with get_vlc2().
67  *
68  * @param[in,out] vlc The VLC to be initialized; table and table_allocated
69  * must have been set when initializing a static VLC,
70  * otherwise this will be treated as uninitialized.
71  * @param[in] nb_bits The number of bits to use for the VLC table;
72  * higher values take up more memory and cache, but
73  * allow to read codes with fewer reads.
74  * Corresponds to the `bits` parameter of get_vlc2().
75  * @param[in] nb_codes The number of provided bits, codes and (if supplied)
76  * symbol entries.
77  * @param[in] bits The lengths (in bits) of the codes. Entries > 0
78  * correspond to valid codes; entries == 0 will be skipped.
79  * @param[in] bits_wrap Stride (in bytes) of the bits table.
80  * @param[in] codes_size Size of the bits. 1, 2 and 4 are supported.
81  * @param[in] codes Table which gives the bit pattern of of each vlc code.
82  * @param[in] codes_wrap Stride (in bytes) of the codes table.
83  * @param[in] codes_size Size of the codes. 1, 2 and 4 are supported.
84  * @param[in] symbols The symbols, i.e. what is returned from get_vlc2()
85  * when the corresponding code is encountered.
86  * May be NULL, then 0, 1, 2, 3, 4,... will be used.
87  * @param[in] symbols_wrap Stride (in bytes) of the symbols table.
88  * @param[in] symbols_size Size of the symbols. 1 and 2 are supported.
89  * @param[in] flags A combination of the VLC_INIT_* flags.
90  *
91  * 'wrap' and 'size' make it possible to use any memory configuration and types
92  * (byte/word/int) to store the 'bits', 'codes', and 'symbols' tables.
93  */
94 int ff_vlc_init_sparse(VLC *vlc, int nb_bits, int nb_codes,
95  const void *bits, int bits_wrap, int bits_size,
96  const void *codes, int codes_wrap, int codes_size,
97  const void *symbols, int symbols_wrap, int symbols_size,
98  int flags);
99 
100 /**
101  * Build VLC decoding tables suitable for use with get_vlc2()
102  *
103  * This function takes lengths and symbols and calculates the codes from them.
104  * For this the input lengths and symbols have to be sorted according to "left
105  * nodes in the corresponding tree first".
106  *
107  * @param[in,out] vlc The VLC to be initialized; table and table_allocated
108  * must have been set when initializing a static VLC,
109  * otherwise this will be treated as uninitialized.
110  * @param[in] nb_bits The number of bits to use for the VLC table;
111  * higher values take up more memory and cache, but
112  * allow to read codes with fewer reads.
113  * @param[in] nb_codes The number of provided length and (if supplied) symbol
114  * entries.
115  * @param[in] lens The lengths of the codes. Entries > 0 correspond to
116  * valid codes; entries == 0 will be skipped and entries
117  * with len < 0 indicate that the tree is incomplete and
118  * has an open end of length -len at this position.
119  * @param[in] lens_wrap Stride (in bytes) of the lengths.
120  * @param[in] symbols The symbols, i.e. what is returned from get_vlc2()
121  * when the corresponding code is encountered.
122  * May be NULL, then 0, 1, 2, 3, 4,... will be used.
123  * @param[in] symbols_wrap Stride (in bytes) of the symbols.
124  * @param[in] symbols_size Size of the symbols. 1 and 2 are supported.
125  * @param[in] offset An offset to apply to all the valid symbols.
126  * @param[in] flags A combination of the VLC_INIT_* flags; notice that
127  * VLC_INIT_INPUT_LE is pointless and ignored.
128  */
129 int ff_vlc_init_from_lengths(VLC *vlc, int nb_bits, int nb_codes,
130  const int8_t *lens, int lens_wrap,
131  const void *symbols, int symbols_wrap, int symbols_size,
132  int offset, int flags, void *logctx);
133 
134 /**
135  * Build VLC decoding tables suitable for use with get_vlc_multi()
136  *
137  * This function takes lengths and symbols and calculates the codes from them.
138  * For this the input lengths and symbols have to be sorted according to "left
139  * nodes in the corresponding tree first".
140  *
141  * @param[in,out] vlc The VLC to be initialized; table and table_allocated
142  * must have been set when initializing a static VLC,
143  * otherwise this will be treated as uninitialized.
144  * @param[in,out] multi The VLC_MULTI 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] nb_bits The number of bits to use for the VLC table;
148  * higher values take up more memory and cache, but
149  * allow to read codes with fewer reads.
150  * @param[in] nb_elems The max possible number of elements.
151  * @param[in] nb_codes The number of provided length and (if supplied) symbol
152  * entries.
153  * @param[in] lens The lengths of the codes. Entries > 0 correspond to
154  * valid codes; entries == 0 will be skipped and entries
155  * with len < 0 indicate that the tree is incomplete and
156  * has an open end of length -len at this position.
157  * @param[in] lens_wrap Stride (in bytes) of the lengths.
158  * @param[in] symbols The symbols, i.e. what is returned from get_vlc2()
159  * when the corresponding code is encountered.
160  * May be NULL, then 0, 1, 2, 3, 4,... will be used.
161  * @param[in] symbols_wrap Stride (in bytes) of the symbols.
162  * @param[in] symbols_size Size of the symbols. 1 and 2 are supported.
163  * @param[in] offset An offset to apply to all the valid symbols.
164  * @param[in] flags A combination of the VLC_INIT_* flags; notice that
165  * VLC_INIT_INPUT_LE is pointless and ignored.
166  */
167 int ff_vlc_init_multi_from_lengths(VLC *vlc, VLC_MULTI *multi, int nb_bits, int nb_elems,
168  int nb_codes, const int8_t *lens, int lens_wrap,
169  const void *symbols, int symbols_wrap, int symbols_size,
170  int offset, int flags, void *logctx);
171 
172 
173 void ff_vlc_free_multi(VLC_MULTI *vlc);
174 void ff_vlc_free(VLC *vlc);
175 
176 #define VLC_INIT_USE_STATIC 1
177 #define VLC_INIT_STATIC_OVERLONG (2 | VLC_INIT_USE_STATIC)
178 /* If VLC_INIT_INPUT_LE is set, the LSB bit of the codes used to
179  * initialize the VLC table is the first bit to be read. */
180 #define VLC_INIT_INPUT_LE 4
181 /* If set the VLC is intended for a little endian bitstream reader. */
182 #define VLC_INIT_OUTPUT_LE 8
183 #define VLC_INIT_LE (VLC_INIT_INPUT_LE | VLC_INIT_OUTPUT_LE)
184 
185 #define VLC_INIT_CUSTOM_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \
186  h, i, j, flags, static_size) \
187  do { \
188  static VLCElem table[static_size]; \
189  (vlc)->table = table; \
190  (vlc)->table_allocated = static_size; \
191  ff_vlc_init_sparse(vlc, bits, a, b, c, d, e, f, g, h, i, j, \
192  flags | VLC_INIT_USE_STATIC); \
193  } while (0)
194 
195 #define VLC_INIT_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size) \
196  VLC_INIT_CUSTOM_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \
197  h, i, j, 0, static_size)
198 
199 #define VLC_INIT_LE_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, h, i, j, static_size) \
200  VLC_INIT_CUSTOM_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \
201  h, i, j, VLC_INIT_LE, static_size)
202 
203 #define VLC_INIT_CUSTOM_STATIC(vlc, bits, a, b, c, d, e, f, g, flags, static_size) \
204  VLC_INIT_CUSTOM_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, \
205  NULL, 0, 0, flags, static_size)
206 
207 #define VLC_INIT_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
208  VLC_INIT_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, NULL, 0, 0, static_size)
209 
210 #define VLC_INIT_LE_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size) \
211  VLC_INIT_LE_SPARSE_STATIC(vlc, bits, a, b, c, d, e, f, g, NULL, 0, 0, static_size)
212 
213 #define VLC_INIT_STATIC_FROM_LENGTHS(vlc, bits, nb_codes, lens, len_wrap, \
214  symbols, symbols_wrap, symbols_size, \
215  offset, flags, static_size) \
216  do { \
217  static VLCElem table[static_size]; \
218  (vlc)->table = table; \
219  (vlc)->table_allocated = static_size; \
220  ff_vlc_init_from_lengths(vlc, bits, nb_codes, lens, len_wrap, \
221  symbols, symbols_wrap, symbols_size, \
222  offset, flags | VLC_INIT_USE_STATIC, \
223  NULL); \
224  } while (0)
225 
226 #endif /* AVCODEC_VLC_H */
VLC_MULTI_ELEM
Definition: vlc.h:39
RL_VLC_ELEM::run
uint8_t run
Definition: vlc.h:53
VLCElem::len
VLCBaseType len
Definition: vlc.h:30
ff_vlc_free_multi
void ff_vlc_free_multi(VLC_MULTI *vlc)
Definition: vlc.c:504
VLCElem::sym
VLCBaseType sym
Definition: vlc.h:30
VLC_MULTI::table_size
int table_size
Definition: vlc.h:47
ff_vlc_free
void ff_vlc_free(VLC *vlc)
Definition: vlc.c:509
VLC_MULTI
Definition: vlc.h:45
bits
uint8_t bits
Definition: vp3data.h:128
VLC_MULTI_ELEM::val
uint8_t val[VLC_MULTI_MAX_SYMBOLS]
Definition: vlc.h:40
VLC_MULTI_ELEM::num
uint8_t num
Definition: vlc.h:42
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:446
VLC_MULTI_MAX_SYMBOLS
#define VLC_MULTI_MAX_SYMBOLS
Definition: vlc.h:24
RL_VLC_ELEM::len
int8_t len
Definition: vlc.h:52
VLC::table_allocated
int table_allocated
Definition: vlc.h:36
RL_VLC_ELEM
Definition: vlc.h:50
VLCElem
Definition: vlc.h:29
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
VLC::bits
int bits
Definition: vlc.h:34
VLC_MULTI_ELEM::len
int8_t len
Definition: vlc.h:41
VLC_MULTI::table
VLC_MULTI_ELEM * table
Definition: vlc.h:46
bits_size
#define bits_size
Definition: bitstream.h:113
VLC
Definition: vlc.h:33
VLC::table
VLCElem * table
Definition: vlc.h:35
VLC::table_size
int table_size
Definition: vlc.h:36
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
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
VLC_MULTI::table_allocated
int table_allocated
Definition: vlc.h:47
VLCBaseType
int16_t VLCBaseType
Definition: vlc.h:27
RL_VLC_ELEM::level
int16_t level
Definition: vlc.h:51