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
40
41 #define GET_DATA(v, table, i, wrap, size) \
42 { \
43 const uint8_t *ptr = (const uint8_t *)table + i * wrap; \
44 switch(size) { \
45 case 1: \
46 v = *(const uint8_t *)ptr; \
47 break; \
48 case 2: \
49 v = *(const uint16_t *)ptr; \
50 break; \
51 case 4: \
52 default: \
53 av_assert1(size == 4); \
54 v = *(const uint32_t *)ptr; \
55 break; \
56 } \
57 }
58
59
61 {
63
66 if (use_static)
67 abort(); // cannot do anything, vlc_init() is used with too little memory
74 }
76 }
78 }
79
80 #define LOCALBUF_ELEMS 1500 // the maximum currently needed is 1296 by rv34
81
83 {
88 }
89
93 /** codeword, with the first bit-to-be-read in the msb
94 * (even if intended for a little-endian bitstream reader) */
97
100 {
105 } else {
108 }
111 if (!*buf)
113 }
114
115 return 0;
116 }
117
119 {
121 return (sa->
code >> 1) - (sb->code >> 1);
122 }
123
124 /**
125 * Build VLC decoding tables suitable for use with get_vlc().
126 *
127 * @param vlc the context to be initialized
128 *
129 * @param table_nb_bits max length of vlc codes to store directly in this table
130 * (Longer codes are delegated to subtables.)
131 *
132 * @param nb_codes number of elements in codes[]
133 *
134 * @param codes descriptions of the vlc codes
135 * These must be ordered such that codes going into the same subtable are contiguous.
136 * Sorting by VLCcode.code is sufficient, though not necessary.
137 */
140 {
141 int table_size, table_index;
143
144 if (table_nb_bits > 30)
146 table_size = 1 << table_nb_bits;
148 ff_dlog(
NULL,
"new table index=%d size=%d\n", table_index, table_size);
149 if (table_index < 0)
150 return table_index;
152
153 /* first pass: map codes and compute auxiliary table sizes */
154 for (
int i = 0;
i < nb_codes;
i++) {
155 int n = codes[
i].
bits;
159 if (n <= table_nb_bits) {
160 /* no need to add another table */
161 int j =
code >> (32 - table_nb_bits);
162 int nb = 1 << (table_nb_bits - n);
163 int inc = 1;
164
167 inc = 1 << n;
168 }
169 for (int k = 0; k < nb; k++) {
171 int oldsym =
table[j].sym;
173 if ((
bits || oldsym) && (
bits != n || oldsym != symbol)) {
176 }
178 table[j].sym = symbol;
179 j += inc;
180 }
181 } else {
182 /* fill auxiliary table recursively */
184 int index, subtable_bits, j, k;
185
186 n -= table_nb_bits;
188 subtable_bits = n;
191 for (k =
i + 1; k < nb_codes; k++) {
192 n = codes[k].
bits - table_nb_bits;
193 if (n <= 0)
194 break;
197 break;
199 codes[k].
code =
code << table_nb_bits;
200 subtable_bits =
FFMAX(subtable_bits, n);
201 }
202 subtable_bits =
FFMIN(subtable_bits, table_nb_bits);
204 table[j].len = -subtable_bits;
206 j, codes[
i].
bits + table_nb_bits);
210 /* note: realloc has been done, so reload tables */
216 }
218 }
219 }
220
221 for (
int i = 0;
i < table_size;
i++) {
224 }
225
226 return table_index;
227 }
228
231 {
233
239 } else {
240 if (codes != localbuf)
245 }
246 }
247 return 0;
248 }
249
252 const void *codes, int codes_wrap, int codes_size,
253 const void *symbols, int symbols_wrap, int symbols_size,
255 {
258
262
264 j = 0;
265 #define COPY(condition)\
266 for (int i = 0; i < nb_codes; i++) { \
267 unsigned len; \
268 GET_DATA(len, bits, i, bits_wrap, bits_size); \
269 if (!(condition)) \
270 continue; \
271 if (len > 3*nb_bits || len > 32) { \
272 av_log(NULL, AV_LOG_ERROR, "Too long VLC (%u) in vlc_init\n", len);\
273 if (buf != localbuf) \
274 av_free(buf); \
275 return AVERROR(EINVAL); \
276 } \
277 buf[j].bits = len; \
278 GET_DATA(buf[j].code, codes, i, codes_wrap, codes_size); \
279 if (buf[j].code >= (1LL<<buf[j].bits)) { \
280 av_log(NULL, AV_LOG_ERROR, "Invalid code %"PRIx32" for %d in " \
281 "vlc_init\n", buf[j].code, i); \
282 if (buf != localbuf) \
283 av_free(buf); \
284 return AVERROR(EINVAL); \
285 } \
286 if (flags & VLC_INIT_INPUT_LE) \
287 buf[j].code = bitswap_32(buf[j].code); \
288 else \
289 buf[j].code <<= 32 - buf[j].bits; \
290 if (symbols) \
291 GET_DATA(buf[j].symbol, symbols, i, symbols_wrap, symbols_size) \
292 else \
293 buf[j].symbol = i; \
294 j++; \
295 }
297 // qsort is the slowest part of vlc_init, and could probably be improved or avoided
300 nb_codes = j;
301
304 }
305
307 const int8_t *lens, int lens_wrap,
308 const void *symbols, int symbols_wrap, int symbols_size,
310 {
313 int ret, j, len_max =
FFMIN(32, 3 * nb_bits);
314
318
320 for (
int i = 0;
i < nb_codes;
i++, lens += lens_wrap) {
323 unsigned sym;
324
326 if (symbols)
327 GET_DATA(sym, symbols,
i, symbols_wrap, symbols_size)
328 else
332 }
else if (
len < 0) {
334 } else
335 continue;
336 if (
len > len_max ||
code & ((1
U << (32 -
len)) - 1)) {
339 }
341 if (
code > UINT32_MAX + 1ULL) {
344 }
345 }
348 if (buf != localbuf)
351 }
352
354 int nb_bits, int nb_codes,
355 const int8_t *lens, int lens_wrap,
356 const void *symbols, int symbols_wrap, int symbols_size,
358 {
360
362 symbols, symbols_wrap, symbols_size,
364 }
365
367 int nb_bits, int nb_codes,
368 const int8_t *lens, int lens_wrap,
369 const void *symbols, int symbols_wrap, int symbols_size,
371 {
373
375 symbols, symbols_wrap, symbols_size,
377
380
382 }
383
385 int nb_bits, int nb_codes,
387 const void *codes, int codes_wrap, int codes_size,
388 const void *symbols, int symbols_wrap, int symbols_size,
390 {
392
395 codes, codes_wrap, codes_size,
396 symbols, symbols_wrap, symbols_size,
398 }
399
401 int nb_bits, int nb_codes,
403 const void *codes, int codes_wrap, int codes_size,
404 const void *symbols, int symbols_wrap, int symbols_size,
406 {
408
411 codes, codes_wrap, codes_size,
412 symbols, symbols_wrap, symbols_size,
414
417
419 }
420
422 const int num, const int numbits,
424 uint32_t curcode, int curlen,
425 int curlimit, int curlevel,
426 const int minlen,
const int max,
428 {
430 for (
int i = num-1;
i >=
max;
i--) {
431 for (int j = 0; j < 2; j++) {
432 int newlimit, sym;
436
438 if (l >= curlimit)
439 return;
440 code = curcode + (buf[t].
code >> curlen);
441 newlimit = curlimit - l;
442 l += curlen;
444 else info.val[curlevel] = sym&0xFF;
445
446 if (curlevel) { // let's not add single entries
447 uint32_t
val =
code >> (32 - numbits);
448 uint32_t nb =
val + (1
U << (numbits - l));
450 info.num = curlevel+1;
453 levelcnt[curlevel-1]++;
454 }
455
456 if (curlevel+1 < max_symbols && newlimit >= minlen) {
458 code, l, newlimit, curlevel+1,
460 }
461 }
462 }
463 }
464
466 const int is16bit, const int nb_codes, const int numbits,
468 {
469 int minbits, maxbits,
max;
472 int count0 = 0;
473
474 for (int j = 0; j < 1<<numbits; j++) {
476 count0 ++;
477 j += (1 << (numbits - single->
table[j].
len)) - 1;
478 }
479 }
480
481 minbits = 32;
482 maxbits = 0;
483
484 for (int n = nb_codes - count0; n < nb_codes; n++) {
487 }
489
490 for (
max = nb_codes;
max > nb_codes - count0;
max--) {
491 // We can only add a code that fits with the shortest other code into the table
492 // We assume the table is sorted by bits and we skip subtables which from our
493 // point of view are basically random corrupted entries
494 // If we have not a single useable vlc we end with max = nb_codes
495 if (buf[
max - 1].
bits+minbits > numbits)
496 break;
497 }
498
499 for (int j = 0; j < 1<<numbits; j++) {
502 if (is16bit)
504 else
506 }
507
509 0, 0,
FFMIN(maxbits, numbits), 0, minbits,
max, count,
info);
510
512 count[0], count[1], count[2], count[3], count[4], minbits,
max);
513
514 return 0;
515 }
516
518 int nb_codes, const int8_t *lens, int lens_wrap,
519 const void *symbols, int symbols_wrap, int symbols_size,
521 {
524 int ret, j, len_max =
FFMIN(32, 3 * nb_bits);
525
529
533
535 for (
int i = 0;
i < nb_codes;
i++, lens += lens_wrap) {
538 unsigned sym;
539
541 if (symbols)
542 GET_DATA(sym, symbols,
i, symbols_wrap, symbols_size)
543 else
547 }
else if (
len < 0) {
549 } else
550 continue;
551 if (
len > len_max ||
code & ((1
U << (32 -
len)) - 1)) {
554 }
556 if (
code > UINT32_MAX + 1ULL) {
559 }
560 }
565 if (buf != localbuf)
569 if (buf != localbuf)
573 }
574
576 {
578 }
579
581 {
583 }