1 /*
2 * API for creating VLC trees
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 * Copyright (c) 2010 Loren Merritt
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <inttypes.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28
39
40 #define GET_DATA(v, table, i, wrap, size) \
41 { \
42 const uint8_t *ptr = (const uint8_t *)table + i * wrap; \
43 switch(size) { \
44 case 1: \
45 v = *(const uint8_t *)ptr; \
46 break; \
47 case 2: \
48 v = *(const uint16_t *)ptr; \
49 break; \
50 case 4: \
51 default: \
52 av_assert1(size == 4); \
53 v = *(const uint32_t *)ptr; \
54 break; \
55 } \
56 }
57
58
60 {
62
65 if (use_static)
66 abort(); // cannot do anything, init_vlc() is used with too little memory
73 }
75 }
77 }
78
79 #define LOCALBUF_ELEMS 1500 // the maximum currently needed is 1296 by rv34
80
82 {
87 }
88
92 /** codeword, with the first bit-to-be-read in the msb
93 * (even if intended for a little-endian bitstream reader) */
96
99 {
104 } else {
107 }
110 if (!*buf)
112 }
113
114 return 0;
115 }
116
118 {
120 return (sa->
code >> 1) - (sb->code >> 1);
121 }
122
123 /**
124 * Build VLC decoding tables suitable for use with get_vlc().
125 *
126 * @param vlc the context to be initialized
127 *
128 * @param table_nb_bits max length of vlc codes to store directly in this table
129 * (Longer codes are delegated to subtables.)
130 *
131 * @param nb_codes number of elements in codes[]
132 *
133 * @param codes descriptions of the vlc codes
134 * These must be ordered such that codes going into the same subtable are contiguous.
135 * Sorting by VLCcode.code is sufficient, though not necessary.
136 */
139 {
140 int table_size, table_index;
142
143 if (table_nb_bits > 30)
145 table_size = 1 << table_nb_bits;
147 ff_dlog(
NULL,
"new table index=%d size=%d\n", table_index, table_size);
148 if (table_index < 0)
149 return table_index;
151
152 /* first pass: map codes and compute auxiliary table sizes */
153 for (
int i = 0;
i < nb_codes;
i++) {
154 int n = codes[
i].
bits;
158 if (n <= table_nb_bits) {
159 /* no need to add another table */
160 int j =
code >> (32 - table_nb_bits);
161 int nb = 1 << (table_nb_bits - n);
162 int inc = 1;
163
166 inc = 1 << n;
167 }
168 for (int k = 0; k < nb; k++) {
170 int oldsym =
table[j].sym;
172 if ((
bits || oldsym) && (
bits != n || oldsym != symbol)) {
175 }
177 table[j].sym = symbol;
178 j += inc;
179 }
180 } else {
181 /* fill auxiliary table recursively */
183 int index, subtable_bits, j, k;
184
185 n -= table_nb_bits;
187 subtable_bits = n;
190 for (k =
i + 1; k < nb_codes; k++) {
191 n = codes[k].
bits - table_nb_bits;
192 if (n <= 0)
193 break;
196 break;
198 codes[k].
code =
code << table_nb_bits;
199 subtable_bits =
FFMAX(subtable_bits, n);
200 }
201 subtable_bits =
FFMIN(subtable_bits, table_nb_bits);
203 table[j].len = -subtable_bits;
205 j, codes[
i].
bits + table_nb_bits);
209 /* note: realloc has been done, so reload tables */
215 }
217 }
218 }
219
220 for (
int i = 0;
i < table_size;
i++) {
223 }
224
225 return table_index;
226 }
227
230 {
232
238 } else {
239 if (codes != localbuf)
244 }
245 }
246 return 0;
247 }
248
249 /* Build VLC decoding tables suitable for use with get_vlc().
250
251 'nb_bits' sets the decoding table size (2^nb_bits) entries. The
252 bigger it is, the faster is the decoding. But it should not be too
253 big to save memory and L1 cache. '9' is a good compromise.
254
255 'nb_codes' : number of vlcs codes
256
257 'bits' : table which gives the size (in bits) of each vlc code.
258
259 'codes' : table which gives the bit pattern of of each vlc code.
260
261 'symbols' : table which gives the values to be returned from get_vlc().
262
263 'xxx_wrap' : give the number of bytes between each entry of the
264 'bits' or 'codes' tables.
265
266 'xxx_size' : gives the number of bytes of each entry of the 'bits'
267 or 'codes' tables. Currently 1,2 and 4 are supported.
268
269 'wrap' and 'size' make it possible to use any memory configuration and types
270 (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables.
271 */
274 const void *codes, int codes_wrap, int codes_size,
275 const void *symbols, int symbols_wrap, int symbols_size,
277 {
280
284
286 j = 0;
287 #define COPY(condition)\
288 for (int i = 0; i < nb_codes; i++) { \
289 unsigned len; \
290 GET_DATA(len, bits, i, bits_wrap, bits_size); \
291 if (!(condition)) \
292 continue; \
293 if (len > 3*nb_bits || len > 32) { \
294 av_log(NULL, AV_LOG_ERROR, "Too long VLC (%u) in init_vlc\n", len);\
295 if (buf != localbuf) \
296 av_free(buf); \
297 return AVERROR(EINVAL); \
298 } \
299 buf[j].bits = len; \
300 GET_DATA(buf[j].code, codes, i, codes_wrap, codes_size); \
301 if (buf[j].code >= (1LL<<buf[j].bits)) { \
302 av_log(NULL, AV_LOG_ERROR, "Invalid code %"PRIx32" for %d in " \
303 "init_vlc\n", buf[j].code, i); \
304 if (buf != localbuf) \
305 av_free(buf); \
306 return AVERROR(EINVAL); \
307 } \
308 if (flags & INIT_VLC_INPUT_LE) \
309 buf[j].code = bitswap_32(buf[j].code); \
310 else \
311 buf[j].code <<= 32 - buf[j].bits; \
312 if (symbols) \
313 GET_DATA(buf[j].symbol, symbols, i, symbols_wrap, symbols_size) \
314 else \
315 buf[j].symbol = i; \
316 j++; \
317 }
319 // qsort is the slowest part of init_vlc, and could probably be improved or avoided
322 nb_codes = j;
323
326 }
327
329 const int8_t *lens, int lens_wrap,
330 const void *symbols, int symbols_wrap, int symbols_size,
332 {
335 int ret, j, len_max =
FFMIN(32, 3 * nb_bits);
336
340
342 for (
int i = 0;
i < nb_codes;
i++, lens += lens_wrap) {
345 unsigned sym;
346
348 if (symbols)
349 GET_DATA(sym, symbols,
i, symbols_wrap, symbols_size)
350 else
354 }
else if (
len < 0) {
356 } else
357 continue;
358 if (
len > len_max ||
code & ((1
U << (32 -
len)) - 1)) {
361 }
363 if (
code > UINT32_MAX + 1ULL) {
366 }
367 }
370 if (buf != localbuf)
373 }
374
376 {
378 }