Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
29 | #include "libavutil/attributes.h" | ||
30 | #include "libavutil/avassert.h" | ||
31 | #include "libavutil/error.h" | ||
32 | #include "libavutil/internal.h" | ||
33 | #include "libavutil/intreadwrite.h" | ||
34 | #include "libavutil/log.h" | ||
35 | #include "libavutil/macros.h" | ||
36 | #include "libavutil/mem.h" | ||
37 | #include "libavutil/qsort.h" | ||
38 | #include "libavutil/reverse.h" | ||
39 | #include "vlc.h" | ||
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 | |||
60 | 450184 | static int alloc_table(VLC *vlc, int size, int use_static) | |
61 | { | ||
62 | 450184 | int index = vlc->table_size; | |
63 | |||
64 | 450184 | vlc->table_size += size; | |
65 |
2/2✓ Branch 0 taken 53589 times.
✓ Branch 1 taken 396595 times.
|
450184 | if (vlc->table_size > vlc->table_allocated) { |
66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 53589 times.
|
53589 | if (use_static) |
67 | ✗ | abort(); // cannot do anything, vlc_init() is used with too little memory | |
68 | 53589 | vlc->table_allocated += (1 << vlc->bits); | |
69 | 53589 | vlc->table = av_realloc_f(vlc->table, vlc->table_allocated, sizeof(*vlc->table)); | |
70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 53589 times.
|
53589 | if (!vlc->table) { |
71 | ✗ | vlc->table_allocated = 0; | |
72 | ✗ | vlc->table_size = 0; | |
73 | ✗ | return AVERROR(ENOMEM); | |
74 | } | ||
75 | 53589 | memset(vlc->table + vlc->table_allocated - (1 << vlc->bits), 0, sizeof(*vlc->table) << vlc->bits); | |
76 | } | ||
77 | 450184 | return index; | |
78 | } | ||
79 | |||
80 | #define LOCALBUF_ELEMS 1500 // the maximum currently needed is 1296 by rv34 | ||
81 | |||
82 | 942834 | static av_always_inline uint32_t bitswap_32(uint32_t x) | |
83 | { | ||
84 | 942834 | return (uint32_t)ff_reverse[ x & 0xFF] << 24 | | |
85 | 942834 | (uint32_t)ff_reverse[(x >> 8) & 0xFF] << 16 | | |
86 | 1885668 | (uint32_t)ff_reverse[(x >> 16) & 0xFF] << 8 | | |
87 | 942834 | (uint32_t)ff_reverse[ x >> 24]; | |
88 | } | ||
89 | |||
90 | typedef struct VLCcode { | ||
91 | uint8_t bits; | ||
92 | VLCBaseType symbol; | ||
93 | /** codeword, with the first bit-to-be-read in the msb | ||
94 | * (even if intended for a little-endian bitstream reader) */ | ||
95 | uint32_t code; | ||
96 | } VLCcode; | ||
97 | |||
98 | 63819 | static int vlc_common_init(VLC *vlc, int nb_bits, int nb_codes, | |
99 | VLCcode **buf, int flags) | ||
100 | { | ||
101 | 63819 | vlc->bits = nb_bits; | |
102 | 63819 | vlc->table_size = 0; | |
103 |
2/2✓ Branch 0 taken 34882 times.
✓ Branch 1 taken 28937 times.
|
63819 | if (flags & VLC_INIT_USE_STATIC) { |
104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34882 times.
|
34882 | av_assert0(nb_codes <= LOCALBUF_ELEMS); |
105 | } else { | ||
106 | 28937 | vlc->table = NULL; | |
107 | 28937 | vlc->table_allocated = 0; | |
108 | } | ||
109 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 63699 times.
|
63819 | if (nb_codes > LOCALBUF_ELEMS) { |
110 | 120 | *buf = av_malloc_array(nb_codes, sizeof(VLCcode)); | |
111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
|
120 | if (!*buf) |
112 | ✗ | return AVERROR(ENOMEM); | |
113 | } | ||
114 | |||
115 | 63819 | return 0; | |
116 | } | ||
117 | |||
118 | 19433929 | static int compare_vlcspec(const void *a, const void *b) | |
119 | { | ||
120 | 19433929 | const VLCcode *sa = a, *sb = b; | |
121 | 19433929 | 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 | */ | ||
138 | 450184 | static int build_table(VLC *vlc, int table_nb_bits, int nb_codes, | |
139 | VLCcode *codes, int flags) | ||
140 | { | ||
141 | int table_size, table_index; | ||
142 | VLCElem *table; | ||
143 | |||
144 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 450184 times.
|
450184 | if (table_nb_bits > 30) |
145 | ✗ | return AVERROR(EINVAL); | |
146 | 450184 | table_size = 1 << table_nb_bits; | |
147 | 450184 | table_index = alloc_table(vlc, table_size, flags & VLC_INIT_USE_STATIC); | |
148 | ff_dlog(NULL, "new table index=%d size=%d\n", table_index, table_size); | ||
149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 450184 times.
|
450184 | if (table_index < 0) |
150 | ✗ | return table_index; | |
151 | 450184 | table = &vlc->table[table_index]; | |
152 | |||
153 | /* first pass: map codes and compute auxiliary table sizes */ | ||
154 |
2/2✓ Branch 0 taken 5144637 times.
✓ Branch 1 taken 450184 times.
|
5594821 | for (int i = 0; i < nb_codes; i++) { |
155 | 5144637 | int n = codes[i].bits; | |
156 | 5144637 | uint32_t code = codes[i].code; | |
157 | 5144637 | int symbol = codes[i].symbol; | |
158 | ff_dlog(NULL, "i=%d n=%d code=0x%"PRIx32"\n", i, n, code); | ||
159 |
2/2✓ Branch 0 taken 4758272 times.
✓ Branch 1 taken 386365 times.
|
5144637 | if (n <= table_nb_bits) { |
160 | /* no need to add another table */ | ||
161 | 4758272 | int j = code >> (32 - table_nb_bits); | |
162 | 4758272 | int nb = 1 << (table_nb_bits - n); | |
163 | 4758272 | int inc = 1; | |
164 | |||
165 |
2/2✓ Branch 0 taken 480134 times.
✓ Branch 1 taken 4278138 times.
|
4758272 | if (flags & VLC_INIT_OUTPUT_LE) { |
166 | 480134 | j = bitswap_32(code); | |
167 | 480134 | inc = 1 << n; | |
168 | } | ||
169 |
2/2✓ Branch 0 taken 45531403 times.
✓ Branch 1 taken 4758272 times.
|
50289675 | for (int k = 0; k < nb; k++) { |
170 | 45531403 | int bits = table[j].len; | |
171 | 45531403 | int oldsym = table[j].sym; | |
172 | ff_dlog(NULL, "%4x: code=%d n=%d\n", j, i, n); | ||
173 |
2/8✓ Branch 0 taken 45531403 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 45531403 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
45531403 | if ((bits || oldsym) && (bits != n || oldsym != symbol)) { |
174 | ✗ | av_log(NULL, AV_LOG_ERROR, "incorrect codes\n"); | |
175 | ✗ | return AVERROR_INVALIDDATA; | |
176 | } | ||
177 | 45531403 | table[j].len = n; | |
178 | 45531403 | table[j].sym = symbol; | |
179 | 45531403 | j += inc; | |
180 | } | ||
181 | } else { | ||
182 | /* fill auxiliary table recursively */ | ||
183 | uint32_t code_prefix; | ||
184 | int index, subtable_bits, j, k; | ||
185 | |||
186 | 386365 | n -= table_nb_bits; | |
187 | 386365 | code_prefix = code >> (32 - table_nb_bits); | |
188 | 386365 | subtable_bits = n; | |
189 | 386365 | codes[i].bits = n; | |
190 | 386365 | codes[i].code = code << table_nb_bits; | |
191 |
2/2✓ Branch 0 taken 3499247 times.
✓ Branch 1 taken 14843 times.
|
3514090 | for (k = i + 1; k < nb_codes; k++) { |
192 | 3499247 | n = codes[k].bits - table_nb_bits; | |
193 |
2/2✓ Branch 0 taken 42683 times.
✓ Branch 1 taken 3456564 times.
|
3499247 | if (n <= 0) |
194 | 42683 | break; | |
195 | 3456564 | code = codes[k].code; | |
196 |
2/2✓ Branch 0 taken 328839 times.
✓ Branch 1 taken 3127725 times.
|
3456564 | if (code >> (32 - table_nb_bits) != code_prefix) |
197 | 328839 | break; | |
198 | 3127725 | codes[k].bits = n; | |
199 | 3127725 | codes[k].code = code << table_nb_bits; | |
200 | 3127725 | subtable_bits = FFMAX(subtable_bits, n); | |
201 | } | ||
202 | 386365 | subtable_bits = FFMIN(subtable_bits, table_nb_bits); | |
203 |
2/2✓ Branch 0 taken 51659 times.
✓ Branch 1 taken 334706 times.
|
386365 | j = (flags & VLC_INIT_OUTPUT_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) : code_prefix; |
204 | 386365 | table[j].len = -subtable_bits; | |
205 | ff_dlog(NULL, "%4x: n=%d (subtable)\n", | ||
206 | j, codes[i].bits + table_nb_bits); | ||
207 | 386365 | index = build_table(vlc, subtable_bits, k-i, codes+i, flags); | |
208 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 386365 times.
|
386365 | if (index < 0) |
209 | ✗ | return index; | |
210 | /* note: realloc has been done, so reload tables */ | ||
211 | 386365 | table = &vlc->table[table_index]; | |
212 | 386365 | table[j].sym = index; | |
213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 386365 times.
|
386365 | if (table[j].sym != index) { |
214 | ✗ | avpriv_request_sample(NULL, "strange codes"); | |
215 | ✗ | return AVERROR_PATCHWELCOME; | |
216 | } | ||
217 | 386365 | i = k-1; | |
218 | } | ||
219 | } | ||
220 | |||
221 |
2/2✓ Branch 0 taken 46127602 times.
✓ Branch 1 taken 450184 times.
|
46577786 | for (int i = 0; i < table_size; i++) { |
222 |
2/2✓ Branch 0 taken 209834 times.
✓ Branch 1 taken 45917768 times.
|
46127602 | if (table[i].len == 0) |
223 | 209834 | table[i].sym = -1; | |
224 | } | ||
225 | |||
226 | 450184 | return table_index; | |
227 | } | ||
228 | |||
229 | 63819 | static int vlc_common_end(VLC *vlc, int nb_bits, int nb_codes, VLCcode *codes, | |
230 | int flags, VLCcode localbuf[LOCALBUF_ELEMS]) | ||
231 | { | ||
232 | 63819 | int ret = build_table(vlc, nb_bits, nb_codes, codes, flags); | |
233 | |||
234 |
2/2✓ Branch 0 taken 34882 times.
✓ Branch 1 taken 28937 times.
|
63819 | if (flags & VLC_INIT_USE_STATIC) { |
235 |
2/2✓ Branch 0 taken 27796 times.
✓ Branch 1 taken 7086 times.
|
34882 | if (vlc->table_size != vlc->table_allocated && |
236 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27796 times.
|
27796 | !(flags & (VLC_INIT_STATIC_OVERLONG & ~VLC_INIT_USE_STATIC))) |
237 | ✗ | av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated); | |
238 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34882 times.
|
34882 | av_assert0(ret >= 0); |
239 | } else { | ||
240 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 28817 times.
|
28937 | if (codes != localbuf) |
241 | 120 | av_free(codes); | |
242 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28937 times.
|
28937 | if (ret < 0) { |
243 | ✗ | av_freep(&vlc->table); | |
244 | ✗ | return ret; | |
245 | } | ||
246 | } | ||
247 | 63819 | return 0; | |
248 | } | ||
249 | |||
250 | 31607 | int ff_vlc_init_sparse(VLC *vlc, int nb_bits, int nb_codes, | |
251 | const void *bits, int bits_wrap, int bits_size, | ||
252 | const void *codes, int codes_wrap, int codes_size, | ||
253 | const void *symbols, int symbols_wrap, int symbols_size, | ||
254 | int flags) | ||
255 | { | ||
256 | 31607 | VLCcode localbuf[LOCALBUF_ELEMS], *buf = localbuf; | |
257 | int j, ret; | ||
258 | |||
259 | 31607 | ret = vlc_common_init(vlc, nb_bits, nb_codes, &buf, flags); | |
260 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31607 times.
|
31607 | if (ret < 0) |
261 | ✗ | return ret; | |
262 | |||
263 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 31607 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
31607 | av_assert0(symbols_size <= 2 || !symbols); |
264 | 31607 | 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 | } | ||
296 |
19/27✓ Branch 0 taken 2575609 times.
✓ Branch 1 taken 304649 times.
✓ Branch 2 taken 82314 times.
✓ Branch 3 taken 1008879 times.
✓ Branch 4 taken 1953693 times.
✓ Branch 5 taken 1953693 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 1953693 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 13 taken 49884 times.
✓ Branch 14 taken 845873 times.
✓ Branch 15 taken 1057936 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 1953693 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 22 taken 337908 times.
✓ Branch 23 taken 1615785 times.
✓ Branch 25 taken 161113 times.
✓ Branch 26 taken 1792580 times.
✓ Branch 27 taken 24460 times.
✓ Branch 28 taken 136653 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 2962572 times.
✓ Branch 31 taken 31607 times.
|
2994179 | COPY(len > nb_bits); |
297 | // qsort is the slowest part of vlc_init, and could probably be improved or avoided | ||
298 |
44/44✓ Branch 0 taken 718003 times.
✓ Branch 1 taken 228746 times.
✓ Branch 3 taken 336105 times.
✓ Branch 4 taken 381898 times.
✓ Branch 6 taken 110840 times.
✓ Branch 7 taken 225265 times.
✓ Branch 9 taken 112579 times.
✓ Branch 10 taken 269319 times.
✓ Branch 12 taken 391274 times.
✓ Branch 13 taken 326729 times.
✓ Branch 14 taken 131634 times.
✓ Branch 15 taken 586369 times.
✓ Branch 16 taken 8230908 times.
✓ Branch 17 taken 172155 times.
✓ Branch 19 taken 5928701 times.
✓ Branch 20 taken 2302207 times.
✓ Branch 21 taken 8213890 times.
✓ Branch 22 taken 488440 times.
✓ Branch 24 taken 6227968 times.
✓ Branch 25 taken 1985922 times.
✓ Branch 26 taken 488440 times.
✓ Branch 27 taken 1985922 times.
✓ Branch 28 taken 2474362 times.
✓ Branch 29 taken 586369 times.
✓ Branch 30 taken 115059 times.
✓ Branch 31 taken 471310 times.
✓ Branch 32 taken 89941 times.
✓ Branch 33 taken 25118 times.
✓ Branch 34 taken 45643 times.
✓ Branch 35 taken 44298 times.
✓ Branch 36 taken 606376 times.
✓ Branch 37 taken 21577 times.
✓ Branch 39 taken 557192 times.
✓ Branch 40 taken 49184 times.
✓ Branch 41 taken 21577 times.
✓ Branch 42 taken 49184 times.
✓ Branch 43 taken 226494 times.
✓ Branch 44 taken 338298 times.
✓ Branch 46 taken 129920 times.
✓ Branch 47 taken 98826 times.
✓ Branch 48 taken 946749 times.
✓ Branch 49 taken 214442 times.
✓ Branch 50 taken 596399 times.
✓ Branch 51 taken 31607 times.
|
16967390 | AV_QSORT(buf, j, struct VLCcode, compare_vlcspec); |
299 |
21/29✓ Branch 0 taken 2575609 times.
✓ Branch 1 taken 304649 times.
✓ Branch 2 taken 82314 times.
✓ Branch 3 taken 2874342 times.
✓ Branch 4 taken 88230 times.
✓ Branch 5 taken 1953693 times.
✓ Branch 6 taken 920649 times.
✓ Branch 7 taken 920649 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 920649 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 15 taken 173493 times.
✓ Branch 16 taken 457048 times.
✓ Branch 17 taken 290108 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 920649 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 24 taken 73133 times.
✓ Branch 25 taken 847516 times.
✓ Branch 27 taken 322655 times.
✓ Branch 28 taken 597994 times.
✓ Branch 29 taken 152503 times.
✓ Branch 30 taken 170152 times.
✗ Branch 31 not taken.
✓ Branch 32 taken 2962572 times.
✓ Branch 33 taken 31607 times.
|
2994179 | COPY(len && len <= nb_bits); |
300 | 31607 | nb_codes = j; | |
301 | |||
302 | 31607 | return vlc_common_end(vlc, nb_bits, nb_codes, buf, | |
303 | flags, localbuf); | ||
304 | } | ||
305 | |||
306 | 31991 | int ff_vlc_init_from_lengths(VLC *vlc, int nb_bits, int nb_codes, | |
307 | const int8_t *lens, int lens_wrap, | ||
308 | const void *symbols, int symbols_wrap, int symbols_size, | ||
309 | int offset, int flags, void *logctx) | ||
310 | { | ||
311 | 31991 | VLCcode localbuf[LOCALBUF_ELEMS], *buf = localbuf; | |
312 | uint64_t code; | ||
313 | 31991 | int ret, j, len_max = FFMIN(32, 3 * nb_bits); | |
314 | |||
315 | 31991 | ret = vlc_common_init(vlc, nb_bits, nb_codes, &buf, flags); | |
316 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31991 times.
|
31991 | if (ret < 0) |
317 | ✗ | return ret; | |
318 | |||
319 | 31991 | j = code = 0; | |
320 |
2/2✓ Branch 0 taken 1849369 times.
✓ Branch 1 taken 31991 times.
|
1881360 | for (int i = 0; i < nb_codes; i++, lens += lens_wrap) { |
321 | 1849369 | int len = *lens; | |
322 |
2/2✓ Branch 0 taken 1845425 times.
✓ Branch 1 taken 3944 times.
|
1849369 | if (len > 0) { |
323 | unsigned sym; | ||
324 | |||
325 | 1845425 | buf[j].bits = len; | |
326 |
2/2✓ Branch 0 taken 1811274 times.
✓ Branch 1 taken 34151 times.
|
1845425 | if (symbols) |
327 |
2/3✓ Branch 0 taken 610188 times.
✓ Branch 1 taken 1201086 times.
✗ Branch 2 not taken.
|
1811274 | GET_DATA(sym, symbols, i, symbols_wrap, symbols_size) |
328 | else | ||
329 | 34151 | sym = i; | |
330 | 1845425 | buf[j].symbol = sym + offset; | |
331 | 1845425 | buf[j++].code = code; | |
332 |
2/2✓ Branch 0 taken 1648 times.
✓ Branch 1 taken 2296 times.
|
3944 | } else if (len < 0) { |
333 | 1648 | len = -len; | |
334 | } else | ||
335 | 2296 | continue; | |
336 |
2/4✓ Branch 0 taken 1847073 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1847073 times.
|
1847073 | if (len > len_max || code & ((1U << (32 - len)) - 1)) { |
337 | ✗ | av_log(logctx, AV_LOG_ERROR, "Invalid VLC (length %u)\n", len); | |
338 | ✗ | goto fail; | |
339 | } | ||
340 | 1847073 | code += 1U << (32 - len); | |
341 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1847073 times.
|
1847073 | if (code > UINT32_MAX + 1ULL) { |
342 | ✗ | av_log(logctx, AV_LOG_ERROR, "Overdetermined VLC tree\n"); | |
343 | ✗ | goto fail; | |
344 | } | ||
345 | } | ||
346 | 31991 | return vlc_common_end(vlc, nb_bits, j, buf, flags, localbuf); | |
347 | ✗ | fail: | |
348 | ✗ | if (buf != localbuf) | |
349 | ✗ | av_free(buf); | |
350 | ✗ | return AVERROR_INVALIDDATA; | |
351 | } | ||
352 | |||
353 | 380 | av_cold void ff_vlc_init_table_from_lengths(VLCElem table[], int table_size, | |
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, | ||
357 | int offset, int flags) | ||
358 | { | ||
359 | 380 | VLC vlc = { .table = table, .table_allocated = table_size }; | |
360 | |||
361 | 380 | ff_vlc_init_from_lengths(&vlc, nb_bits, nb_codes, lens, lens_wrap, | |
362 | symbols, symbols_wrap, symbols_size, | ||
363 | offset, flags | VLC_INIT_USE_STATIC, NULL); | ||
364 | 380 | } | |
365 | |||
366 | 7770 | av_cold const VLCElem *ff_vlc_init_tables_from_lengths(VLCInitState *state, | |
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, | ||
370 | int offset, int flags) | ||
371 | { | ||
372 | 7770 | VLC vlc = { .table = state->table, .table_allocated = state->size }; | |
373 | |||
374 | 7770 | ff_vlc_init_from_lengths(&vlc, nb_bits, nb_codes, lens, lens_wrap, | |
375 | symbols, symbols_wrap, symbols_size, | ||
376 | offset, flags | VLC_INIT_STATIC_OVERLONG, NULL); | ||
377 | |||
378 | 7770 | state->table += vlc.table_size; | |
379 | 7770 | state->size -= vlc.table_size; | |
380 | |||
381 | 7770 | return vlc.table; | |
382 | } | ||
383 | |||
384 | 3517 | av_cold void ff_vlc_init_table_sparse(VLCElem table[], int table_size, | |
385 | int nb_bits, int nb_codes, | ||
386 | const void *bits, int bits_wrap, int bits_size, | ||
387 | const void *codes, int codes_wrap, int codes_size, | ||
388 | const void *symbols, int symbols_wrap, int symbols_size, | ||
389 | int flags) | ||
390 | { | ||
391 | 3517 | VLC vlc = { .table = table, .table_allocated = table_size }; | |
392 | |||
393 | 3517 | ff_vlc_init_sparse(&vlc, nb_bits, nb_codes, | |
394 | bits, bits_wrap, bits_size, | ||
395 | codes, codes_wrap, codes_size, | ||
396 | symbols, symbols_wrap, symbols_size, | ||
397 | flags | VLC_INIT_USE_STATIC); | ||
398 | 3517 | } | |
399 | |||
400 | 15841 | av_cold const VLCElem *ff_vlc_init_tables_sparse(VLCInitState *state, | |
401 | int nb_bits, int nb_codes, | ||
402 | const void *bits, int bits_wrap, int bits_size, | ||
403 | const void *codes, int codes_wrap, int codes_size, | ||
404 | const void *symbols, int symbols_wrap, int symbols_size, | ||
405 | int flags) | ||
406 | { | ||
407 | 15841 | VLC vlc = { .table = state->table, .table_allocated = state->size }; | |
408 | |||
409 | 15841 | ff_vlc_init_sparse(&vlc, nb_bits, nb_codes, | |
410 | bits, bits_wrap, bits_size, | ||
411 | codes, codes_wrap, codes_size, | ||
412 | symbols, symbols_wrap, symbols_size, | ||
413 | flags | VLC_INIT_STATIC_OVERLONG); | ||
414 | |||
415 | 15841 | state->table += vlc.table_size; | |
416 | 15841 | state->size -= vlc.table_size; | |
417 | |||
418 | 15841 | return vlc.table; | |
419 | } | ||
420 | |||
421 | 498122 | static void add_level(VLC_MULTI_ELEM *table, const int is16bit, | |
422 | const int num, const int numbits, | ||
423 | const VLCcode *buf, | ||
424 | uint32_t curcode, int curlen, | ||
425 | int curlimit, int curlevel, | ||
426 | const int minlen, const int max, | ||
427 | unsigned* levelcnt, VLC_MULTI_ELEM info) | ||
428 | { | ||
429 | 498122 | int max_symbols = VLC_MULTI_MAX_SYMBOLS >> is16bit; | |
430 |
2/2✓ Branch 0 taken 754290 times.
✓ Branch 1 taken 25 times.
|
754315 | for (int i = num-1; i >= max; i--) { |
431 |
2/2✓ Branch 0 taken 1271048 times.
✓ Branch 1 taken 256193 times.
|
1527241 | for (int j = 0; j < 2; j++) { |
432 | int newlimit, sym; | ||
433 |
2/2✓ Branch 0 taken 516758 times.
✓ Branch 1 taken 754290 times.
|
1271048 | int t = j ? i-1 : i; |
434 | 1271048 | int l = buf[t].bits; | |
435 | uint32_t code; | ||
436 | |||
437 | 1271048 | sym = buf[t].symbol; | |
438 |
2/2✓ Branch 0 taken 498097 times.
✓ Branch 1 taken 772951 times.
|
1271048 | if (l >= curlimit) |
439 | 498097 | return; | |
440 | 772951 | code = curcode + (buf[t].code >> curlen); | |
441 | 772951 | newlimit = curlimit - l; | |
442 | 772951 | l += curlen; | |
443 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 772951 times.
|
772951 | if (is16bit) info.val16[curlevel] = sym; |
444 | 772951 | else info.val8[curlevel] = sym&0xFF; | |
445 | |||
446 |
2/2✓ Branch 0 taken 757095 times.
✓ Branch 1 taken 15856 times.
|
772951 | if (curlevel) { // let's not add single entries |
447 | 757095 | uint32_t val = code >> (32 - numbits); | |
448 | 757095 | uint32_t nb = val + (1U << (numbits - l)); | |
449 | 757095 | info.len = l; | |
450 | 757095 | info.num = curlevel+1; | |
451 |
2/2✓ Branch 0 taken 4818408 times.
✓ Branch 1 taken 757095 times.
|
5575503 | for (; val < nb; val++) |
452 | 4818408 | AV_COPY64(table+val, &info); | |
453 | 757095 | levelcnt[curlevel-1]++; | |
454 | } | ||
455 | |||
456 |
4/4✓ Branch 0 taken 583080 times.
✓ Branch 1 taken 189871 times.
✓ Branch 2 taken 497901 times.
✓ Branch 3 taken 85179 times.
|
772951 | if (curlevel+1 < max_symbols && newlimit >= minlen) { |
457 | 497901 | add_level(table, is16bit, num, numbits, buf, | |
458 | code, l, newlimit, curlevel+1, | ||
459 | minlen, max, levelcnt, info); | ||
460 | } | ||
461 | } | ||
462 | } | ||
463 | } | ||
464 | |||
465 | 221 | static int vlc_multi_gen(VLC_MULTI_ELEM *table, const VLC *single, | |
466 | const int is16bit, const int nb_codes, const int numbits, | ||
467 | VLCcode *buf, void *logctx) | ||
468 | { | ||
469 | int minbits, maxbits, max; | ||
470 | 221 | unsigned count[VLC_MULTI_MAX_SYMBOLS-1] = { 0, }; | |
471 | 221 | VLC_MULTI_ELEM info = { 0 }; | |
472 | 221 | int count0 = 0; | |
473 | |||
474 |
2/2✓ Branch 0 taken 27792 times.
✓ Branch 1 taken 221 times.
|
28013 | for (int j = 0; j < 1<<numbits; j++) { |
475 |
2/2✓ Branch 0 taken 23326 times.
✓ Branch 1 taken 4466 times.
|
27792 | if (single->table[j].len > 0) { |
476 | 23326 | count0 ++; | |
477 | 23326 | j += (1 << (numbits - single->table[j].len)) - 1; | |
478 | } | ||
479 | } | ||
480 | |||
481 | 221 | minbits = 32; | |
482 | 221 | maxbits = 0; | |
483 | |||
484 |
2/2✓ Branch 0 taken 23326 times.
✓ Branch 1 taken 221 times.
|
23547 | for (int n = nb_codes - count0; n < nb_codes; n++) { |
485 | 23326 | minbits = FFMIN(minbits, buf[n].bits); | |
486 | 23326 | maxbits = FFMAX(maxbits, buf[n].bits); | |
487 | } | ||
488 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
|
221 | av_assert0(maxbits <= numbits); |
489 | |||
490 |
2/2✓ Branch 0 taken 8884 times.
✓ Branch 1 taken 26 times.
|
8910 | 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 |
2/2✓ Branch 0 taken 195 times.
✓ Branch 1 taken 8689 times.
|
8884 | if (buf[max - 1].bits+minbits > numbits) |
496 | 195 | break; | |
497 | } | ||
498 | |||
499 |
2/2✓ Branch 0 taken 538624 times.
✓ Branch 1 taken 221 times.
|
538845 | for (int j = 0; j < 1<<numbits; j++) { |
500 | 538624 | table[j].len = single->table[j].len; | |
501 | 538624 | table[j].num = single->table[j].len > 0 ? 1 : 0; | |
502 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 538624 times.
|
538624 | if (is16bit) |
503 | ✗ | table[j].val16[0] = single->table[j].sym; | |
504 | else | ||
505 | 538624 | table[j].val8[0] = single->table[j].sym; | |
506 | } | ||
507 | |||
508 | 221 | add_level(table, is16bit, nb_codes, numbits, buf, | |
509 | 0, 0, FFMIN(maxbits, numbits), 0, minbits, max, count, info); | ||
510 | |||
511 | 221 | av_log(logctx, AV_LOG_DEBUG, "Joint: %d/%d/%d/%d/%d codes min=%ubits max=%u\n", | |
512 | count[0], count[1], count[2], count[3], count[4], minbits, max); | ||
513 | |||
514 | 221 | return 0; | |
515 | } | ||
516 | |||
517 | 221 | int ff_vlc_init_multi_from_lengths(VLC *vlc, VLC_MULTI *multi, int nb_bits, int nb_elems, | |
518 | int nb_codes, const int8_t *lens, int lens_wrap, | ||
519 | const void *symbols, int symbols_wrap, int symbols_size, | ||
520 | int offset, int flags, void *logctx) | ||
521 | { | ||
522 | 221 | VLCcode localbuf[LOCALBUF_ELEMS], *buf = localbuf; | |
523 | uint64_t code; | ||
524 | 221 | int ret, j, len_max = FFMIN(32, 3 * nb_bits); | |
525 | |||
526 | 221 | ret = vlc_common_init(vlc, nb_bits, nb_codes, &buf, flags); | |
527 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
|
221 | if (ret < 0) |
528 | ✗ | return ret; | |
529 | |||
530 | 221 | multi->table = av_malloc(sizeof(*multi->table) << nb_bits); | |
531 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
|
221 | if (!multi->table) |
532 | ✗ | goto fail; | |
533 | |||
534 | 221 | j = code = 0; | |
535 |
2/2✓ Branch 0 taken 38505 times.
✓ Branch 1 taken 221 times.
|
38726 | for (int i = 0; i < nb_codes; i++, lens += lens_wrap) { |
536 | 38505 | int len = *lens; | |
537 |
1/2✓ Branch 0 taken 38505 times.
✗ Branch 1 not taken.
|
38505 | if (len > 0) { |
538 | unsigned sym; | ||
539 | |||
540 | 38505 | buf[j].bits = len; | |
541 |
1/2✓ Branch 0 taken 38505 times.
✗ Branch 1 not taken.
|
38505 | if (symbols) |
542 |
1/3✗ Branch 0 not taken.
✓ Branch 1 taken 38505 times.
✗ Branch 2 not taken.
|
38505 | GET_DATA(sym, symbols, i, symbols_wrap, symbols_size) |
543 | else | ||
544 | ✗ | sym = i; | |
545 | 38505 | buf[j].symbol = sym + offset; | |
546 | 38505 | buf[j++].code = code; | |
547 | ✗ | } else if (len < 0) { | |
548 | ✗ | len = -len; | |
549 | } else | ||
550 | ✗ | continue; | |
551 |
2/4✓ Branch 0 taken 38505 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38505 times.
|
38505 | if (len > len_max || code & ((1U << (32 - len)) - 1)) { |
552 | ✗ | av_log(logctx, AV_LOG_ERROR, "Invalid VLC (length %u)\n", len); | |
553 | ✗ | goto fail; | |
554 | } | ||
555 | 38505 | code += 1U << (32 - len); | |
556 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38505 times.
|
38505 | if (code > UINT32_MAX + 1ULL) { |
557 | ✗ | av_log(logctx, AV_LOG_ERROR, "Overdetermined VLC tree\n"); | |
558 | ✗ | goto fail; | |
559 | } | ||
560 | } | ||
561 | 221 | ret = vlc_common_end(vlc, nb_bits, j, buf, flags, buf); | |
562 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
|
221 | if (ret < 0) |
563 | ✗ | goto fail; | |
564 | 221 | ret = vlc_multi_gen(multi->table, vlc, nb_elems > 256, j, nb_bits, buf, logctx); | |
565 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
|
221 | if (buf != localbuf) |
566 | ✗ | av_free(buf); | |
567 | 221 | return ret; | |
568 | ✗ | fail: | |
569 | ✗ | if (buf != localbuf) | |
570 | ✗ | av_free(buf); | |
571 | ✗ | ff_vlc_free_multi(multi); | |
572 | ✗ | return AVERROR_INVALIDDATA; | |
573 | } | ||
574 | |||
575 | 277 | void ff_vlc_free_multi(VLC_MULTI *vlc) | |
576 | { | ||
577 | 277 | av_freep(&vlc->table); | |
578 | 277 | } | |
579 | |||
580 | 34067 | void ff_vlc_free(VLC *vlc) | |
581 | { | ||
582 | 34067 | av_freep(&vlc->table); | |
583 | 34067 | } | |
584 |