Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/vlc.c |
Date: | 2022-05-23 03:24:52 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 127 | 152 | 83.6% |
Branches: | 145 | 189 | 76.7% |
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/log.h" | ||
34 | #include "libavutil/macros.h" | ||
35 | #include "libavutil/mem.h" | ||
36 | #include "libavutil/qsort.h" | ||
37 | #include "libavutil/reverse.h" | ||
38 | #include "vlc.h" | ||
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 | |||
59 | 440183 | static int alloc_table(VLC *vlc, int size, int use_static) | |
60 | { | ||
61 | 440183 | int index = vlc->table_size; | |
62 | |||
63 | 440183 | vlc->table_size += size; | |
64 |
2/2✓ Branch 0 taken 50232 times.
✓ Branch 1 taken 389951 times.
|
440183 | if (vlc->table_size > vlc->table_allocated) { |
65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50232 times.
|
50232 | if (use_static) |
66 | ✗ | abort(); // cannot do anything, init_vlc() is used with too little memory | |
67 | 50232 | vlc->table_allocated += (1 << vlc->bits); | |
68 | 50232 | vlc->table = av_realloc_f(vlc->table, vlc->table_allocated, sizeof(VLC_TYPE) * 2); | |
69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50232 times.
|
50232 | if (!vlc->table) { |
70 | ✗ | vlc->table_allocated = 0; | |
71 | ✗ | vlc->table_size = 0; | |
72 | ✗ | return AVERROR(ENOMEM); | |
73 | } | ||
74 | 50232 | memset(vlc->table + vlc->table_allocated - (1 << vlc->bits), 0, sizeof(VLC_TYPE) * 2 << vlc->bits); | |
75 | } | ||
76 | 440183 | return index; | |
77 | } | ||
78 | |||
79 | #define LOCALBUF_ELEMS 1500 // the maximum currently needed is 1296 by rv34 | ||
80 | |||
81 | 945739 | static av_always_inline uint32_t bitswap_32(uint32_t x) | |
82 | { | ||
83 | 945739 | return (uint32_t)ff_reverse[ x & 0xFF] << 24 | | |
84 | 945739 | (uint32_t)ff_reverse[(x >> 8) & 0xFF] << 16 | | |
85 | 1891478 | (uint32_t)ff_reverse[(x >> 16) & 0xFF] << 8 | | |
86 | 945739 | (uint32_t)ff_reverse[ x >> 24]; | |
87 | } | ||
88 | |||
89 | typedef struct VLCcode { | ||
90 | uint8_t bits; | ||
91 | VLC_TYPE symbol; | ||
92 | /** codeword, with the first bit-to-be-read in the msb | ||
93 | * (even if intended for a little-endian bitstream reader) */ | ||
94 | uint32_t code; | ||
95 | } VLCcode; | ||
96 | |||
97 | 59328 | static int vlc_common_init(VLC *vlc, int nb_bits, int nb_codes, | |
98 | VLCcode **buf, int flags) | ||
99 | { | ||
100 | 59328 | vlc->bits = nb_bits; | |
101 | 59328 | vlc->table_size = 0; | |
102 |
2/2✓ Branch 0 taken 32318 times.
✓ Branch 1 taken 27010 times.
|
59328 | if (flags & INIT_VLC_USE_NEW_STATIC) { |
103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32318 times.
|
32318 | av_assert0(nb_codes <= LOCALBUF_ELEMS); |
104 | } else { | ||
105 | 27010 | vlc->table = NULL; | |
106 | 27010 | vlc->table_allocated = 0; | |
107 | } | ||
108 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 59208 times.
|
59328 | if (nb_codes > LOCALBUF_ELEMS) { |
109 | 120 | *buf = av_malloc_array(nb_codes, sizeof(VLCcode)); | |
110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
|
120 | if (!*buf) |
111 | ✗ | return AVERROR(ENOMEM); | |
112 | } | ||
113 | |||
114 | 59328 | return 0; | |
115 | } | ||
116 | |||
117 | 20488544 | static int compare_vlcspec(const void *a, const void *b) | |
118 | { | ||
119 | 20488544 | const VLCcode *sa = a, *sb = b; | |
120 | 20488544 | 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 | */ | ||
137 | 440183 | static int build_table(VLC *vlc, int table_nb_bits, int nb_codes, | |
138 | VLCcode *codes, int flags) | ||
139 | { | ||
140 | int table_size, table_index; | ||
141 | VLC_TYPE (*table)[2]; | ||
142 | |||
143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 440183 times.
|
440183 | if (table_nb_bits > 30) |
144 | ✗ | return AVERROR(EINVAL); | |
145 | 440183 | table_size = 1 << table_nb_bits; | |
146 | 440183 | table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_NEW_STATIC); | |
147 | ff_dlog(NULL, "new table index=%d size=%d\n", table_index, table_size); | ||
148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 440183 times.
|
440183 | if (table_index < 0) |
149 | ✗ | return table_index; | |
150 | 440183 | table = &vlc->table[table_index]; | |
151 | |||
152 | /* first pass: map codes and compute auxiliary table sizes */ | ||
153 |
2/2✓ Branch 0 taken 5010225 times.
✓ Branch 1 taken 440183 times.
|
5450408 | for (int i = 0; i < nb_codes; i++) { |
154 | 5010225 | int n = codes[i].bits; | |
155 | 5010225 | uint32_t code = codes[i].code; | |
156 | 5010225 | int symbol = codes[i].symbol; | |
157 | ff_dlog(NULL, "i=%d n=%d code=0x%"PRIx32"\n", i, n, code); | ||
158 |
2/2✓ Branch 0 taken 4629370 times.
✓ Branch 1 taken 380855 times.
|
5010225 | if (n <= table_nb_bits) { |
159 | /* no need to add another table */ | ||
160 | 4629370 | int j = code >> (32 - table_nb_bits); | |
161 | 4629370 | int nb = 1 << (table_nb_bits - n); | |
162 | 4629370 | int inc = 1; | |
163 | |||
164 |
2/2✓ Branch 0 taken 472343 times.
✓ Branch 1 taken 4157027 times.
|
4629370 | if (flags & INIT_VLC_OUTPUT_LE) { |
165 | 472343 | j = bitswap_32(code); | |
166 | 472343 | inc = 1 << n; | |
167 | } | ||
168 |
2/2✓ Branch 0 taken 40893730 times.
✓ Branch 1 taken 4629370 times.
|
45523100 | for (int k = 0; k < nb; k++) { |
169 | 40893730 | int bits = table[j][1]; | |
170 | 40893730 | int oldsym = table[j][0]; | |
171 | ff_dlog(NULL, "%4x: code=%d n=%d\n", j, i, n); | ||
172 |
2/8✓ Branch 0 taken 40893730 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40893730 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
40893730 | if ((bits || oldsym) && (bits != n || oldsym != symbol)) { |
173 | ✗ | av_log(NULL, AV_LOG_ERROR, "incorrect codes\n"); | |
174 | ✗ | return AVERROR_INVALIDDATA; | |
175 | } | ||
176 | 40893730 | table[j][1] = n; //bits | |
177 | 40893730 | table[j][0] = symbol; | |
178 | 40893730 | j += inc; | |
179 | } | ||
180 | } else { | ||
181 | /* fill auxiliary table recursively */ | ||
182 | uint32_t code_prefix; | ||
183 | int index, subtable_bits, j, k; | ||
184 | |||
185 | 380855 | n -= table_nb_bits; | |
186 | 380855 | code_prefix = code >> (32 - table_nb_bits); | |
187 | 380855 | subtable_bits = n; | |
188 | 380855 | codes[i].bits = n; | |
189 | 380855 | codes[i].code = code << table_nb_bits; | |
190 |
2/2✓ Branch 0 taken 3466822 times.
✓ Branch 1 taken 12394 times.
|
3479216 | for (k = i + 1; k < nb_codes; k++) { |
191 | 3466822 | n = codes[k].bits - table_nb_bits; | |
192 |
2/2✓ Branch 0 taken 37894 times.
✓ Branch 1 taken 3428928 times.
|
3466822 | if (n <= 0) |
193 | 37894 | break; | |
194 | 3428928 | code = codes[k].code; | |
195 |
2/2✓ Branch 0 taken 330567 times.
✓ Branch 1 taken 3098361 times.
|
3428928 | if (code >> (32 - table_nb_bits) != code_prefix) |
196 | 330567 | break; | |
197 | 3098361 | codes[k].bits = n; | |
198 | 3098361 | codes[k].code = code << table_nb_bits; | |
199 | 3098361 | subtable_bits = FFMAX(subtable_bits, n); | |
200 | } | ||
201 | 380855 | subtable_bits = FFMIN(subtable_bits, table_nb_bits); | |
202 |
2/2✓ Branch 0 taken 51364 times.
✓ Branch 1 taken 329491 times.
|
380855 | j = (flags & INIT_VLC_OUTPUT_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) : code_prefix; |
203 | 380855 | table[j][1] = -subtable_bits; | |
204 | ff_dlog(NULL, "%4x: n=%d (subtable)\n", | ||
205 | j, codes[i].bits + table_nb_bits); | ||
206 | 380855 | index = build_table(vlc, subtable_bits, k-i, codes+i, flags); | |
207 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 380855 times.
|
380855 | if (index < 0) |
208 | ✗ | return index; | |
209 | /* note: realloc has been done, so reload tables */ | ||
210 | 380855 | table = &vlc->table[table_index]; | |
211 | 380855 | table[j][0] = index; //code | |
212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 380855 times.
|
380855 | if (table[j][0] != index) { |
213 | ✗ | avpriv_request_sample(NULL, "strange codes"); | |
214 | ✗ | return AVERROR_PATCHWELCOME; | |
215 | } | ||
216 | 380855 | i = k-1; | |
217 | } | ||
218 | } | ||
219 | |||
220 |
2/2✓ Branch 0 taken 41781298 times.
✓ Branch 1 taken 440183 times.
|
42221481 | for (int i = 0; i < table_size; i++) { |
221 |
2/2✓ Branch 0 taken 506713 times.
✓ Branch 1 taken 41274585 times.
|
41781298 | if (table[i][1] == 0) //bits |
222 | 506713 | table[i][0] = -1; //codes | |
223 | } | ||
224 | |||
225 | 440183 | return table_index; | |
226 | } | ||
227 | |||
228 | 59328 | static int vlc_common_end(VLC *vlc, int nb_bits, int nb_codes, VLCcode *codes, | |
229 | int flags, VLCcode localbuf[LOCALBUF_ELEMS]) | ||
230 | { | ||
231 | 59328 | int ret = build_table(vlc, nb_bits, nb_codes, codes, flags); | |
232 | |||
233 |
2/2✓ Branch 0 taken 32318 times.
✓ Branch 1 taken 27010 times.
|
59328 | if (flags & INIT_VLC_USE_NEW_STATIC) { |
234 |
2/2✓ Branch 0 taken 6483 times.
✓ Branch 1 taken 25835 times.
|
32318 | if (vlc->table_size != vlc->table_allocated && |
235 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6483 times.
|
6483 | !(flags & (INIT_VLC_STATIC_OVERLONG & ~INIT_VLC_USE_NEW_STATIC))) |
236 | ✗ | av_log(NULL, AV_LOG_ERROR, "needed %d had %d\n", vlc->table_size, vlc->table_allocated); | |
237 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32318 times.
|
32318 | av_assert0(ret >= 0); |
238 | } else { | ||
239 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 26890 times.
|
27010 | if (codes != localbuf) |
240 | 120 | av_free(codes); | |
241 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27010 times.
|
27010 | if (ret < 0) { |
242 | ✗ | av_freep(&vlc->table); | |
243 | ✗ | return ret; | |
244 | } | ||
245 | } | ||
246 | 59328 | 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 | */ | ||
272 | 37288 | int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, | |
273 | const void *bits, int bits_wrap, int bits_size, | ||
274 | const void *codes, int codes_wrap, int codes_size, | ||
275 | const void *symbols, int symbols_wrap, int symbols_size, | ||
276 | int flags) | ||
277 | { | ||
278 | 37288 | VLCcode localbuf[LOCALBUF_ELEMS], *buf = localbuf; | |
279 | int j, ret; | ||
280 | |||
281 | 37288 | ret = vlc_common_init(vlc, nb_bits, nb_codes, &buf, flags); | |
282 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37288 times.
|
37288 | if (ret < 0) |
283 | ✗ | return ret; | |
284 | |||
285 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 37288 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
37288 | av_assert0(symbols_size <= 2 || !symbols); |
286 | 37288 | 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 | } | ||
318 |
19/27✓ Branch 0 taken 2846941 times.
✓ Branch 1 taken 312276 times.
✓ Branch 2 taken 106765 times.
✓ Branch 3 taken 1119680 times.
✓ Branch 4 taken 2146302 times.
✓ Branch 5 taken 2146302 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 2146302 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 13 taken 46162 times.
✓ Branch 14 taken 981229 times.
✓ Branch 15 taken 1118911 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 2146302 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 22 taken 337333 times.
✓ Branch 23 taken 1808969 times.
✓ Branch 25 taken 158757 times.
✓ Branch 26 taken 1987545 times.
✓ Branch 27 taken 22247 times.
✓ Branch 28 taken 136510 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 3265982 times.
✓ Branch 31 taken 37288 times.
|
3303270 | COPY(len > nb_bits); |
319 | // qsort is the slowest part of init_vlc, and could probably be improved or avoided | ||
320 |
44/44✓ Branch 0 taken 780866 times.
✓ Branch 1 taken 251084 times.
✓ Branch 3 taken 360573 times.
✓ Branch 4 taken 420293 times.
✓ Branch 6 taken 120537 times.
✓ Branch 7 taken 240036 times.
✓ Branch 9 taken 126812 times.
✓ Branch 10 taken 293481 times.
✓ Branch 12 taken 419721 times.
✓ Branch 13 taken 361145 times.
✓ Branch 14 taken 142460 times.
✓ Branch 15 taken 638406 times.
✓ Branch 16 taken 8635638 times.
✓ Branch 17 taken 191074 times.
✓ Branch 19 taken 6198281 times.
✓ Branch 20 taken 2437357 times.
✓ Branch 21 taken 8594051 times.
✓ Branch 22 taken 533997 times.
✓ Branch 24 taken 6499617 times.
✓ Branch 25 taken 2094434 times.
✓ Branch 26 taken 2094434 times.
✓ Branch 27 taken 533997 times.
✓ Branch 28 taken 2628431 times.
✓ Branch 29 taken 638406 times.
✓ Branch 30 taken 126635 times.
✓ Branch 31 taken 511771 times.
✓ Branch 32 taken 100137 times.
✓ Branch 33 taken 26498 times.
✓ Branch 34 taken 50742 times.
✓ Branch 35 taken 49395 times.
✓ Branch 36 taken 665173 times.
✓ Branch 37 taken 23372 times.
✓ Branch 39 taken 611305 times.
✓ Branch 40 taken 53868 times.
✓ Branch 41 taken 23372 times.
✓ Branch 42 taken 53868 times.
✓ Branch 43 taken 247775 times.
✓ Branch 44 taken 367259 times.
✓ Branch 46 taken 141543 times.
✓ Branch 47 taken 109541 times.
✓ Branch 48 taken 1031950 times.
✓ Branch 49 taken 235406 times.
✓ Branch 50 taken 652322 times.
✓ Branch 51 taken 37288 times.
|
17242278 | AV_QSORT(buf, j, struct VLCcode, compare_vlcspec); |
321 |
21/29✓ Branch 0 taken 2846941 times.
✓ Branch 1 taken 312276 times.
✓ Branch 2 taken 106765 times.
✓ Branch 3 taken 3183728 times.
✓ Branch 4 taken 82254 times.
✓ Branch 5 taken 2146302 times.
✓ Branch 6 taken 1037426 times.
✓ Branch 7 taken 1037426 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1037426 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 15 taken 168397 times.
✓ Branch 16 taken 570681 times.
✓ Branch 17 taken 298348 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 1037426 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 24 taken 84699 times.
✓ Branch 25 taken 952727 times.
✓ Branch 27 taken 329648 times.
✓ Branch 28 taken 707778 times.
✓ Branch 29 taken 160761 times.
✓ Branch 30 taken 168887 times.
✗ Branch 31 not taken.
✓ Branch 32 taken 3265982 times.
✓ Branch 33 taken 37288 times.
|
3303270 | COPY(len && len <= nb_bits); |
322 | 37288 | nb_codes = j; | |
323 | |||
324 | 37288 | return vlc_common_end(vlc, nb_bits, nb_codes, buf, | |
325 | flags, localbuf); | ||
326 | } | ||
327 | |||
328 | 22040 | int ff_init_vlc_from_lengths(VLC *vlc, int nb_bits, int nb_codes, | |
329 | const int8_t *lens, int lens_wrap, | ||
330 | const void *symbols, int symbols_wrap, int symbols_size, | ||
331 | int offset, int flags, void *logctx) | ||
332 | { | ||
333 | 22040 | VLCcode localbuf[LOCALBUF_ELEMS], *buf = localbuf; | |
334 | uint64_t code; | ||
335 | 22040 | int ret, j, len_max = FFMIN(32, 3 * nb_bits); | |
336 | |||
337 | 22040 | ret = vlc_common_init(vlc, nb_bits, nb_codes, &buf, flags); | |
338 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22040 times.
|
22040 | if (ret < 0) |
339 | ✗ | return ret; | |
340 | |||
341 | 22040 | j = code = 0; | |
342 |
2/2✓ Branch 0 taken 1449328 times.
✓ Branch 1 taken 22040 times.
|
1471368 | for (int i = 0; i < nb_codes; i++, lens += lens_wrap) { |
343 | 1449328 | int len = *lens; | |
344 |
2/2✓ Branch 0 taken 1445642 times.
✓ Branch 1 taken 3686 times.
|
1449328 | if (len > 0) { |
345 | unsigned sym; | ||
346 | |||
347 | 1445642 | buf[j].bits = len; | |
348 |
2/2✓ Branch 0 taken 1441605 times.
✓ Branch 1 taken 4037 times.
|
1445642 | if (symbols) |
349 |
2/3✓ Branch 0 taken 239162 times.
✓ Branch 1 taken 1202443 times.
✗ Branch 2 not taken.
|
1441605 | GET_DATA(sym, symbols, i, symbols_wrap, symbols_size) |
350 | else | ||
351 | 4037 | sym = i; | |
352 | 1445642 | buf[j].symbol = sym + offset; | |
353 | 1445642 | buf[j++].code = code; | |
354 |
2/2✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 2134 times.
|
3686 | } else if (len < 0) { |
355 | 1552 | len = -len; | |
356 | } else | ||
357 | 2134 | continue; | |
358 |
2/4✓ Branch 0 taken 1447194 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1447194 times.
|
1447194 | if (len > len_max || code & ((1U << (32 - len)) - 1)) { |
359 | ✗ | av_log(logctx, AV_LOG_ERROR, "Invalid VLC (length %u)\n", len); | |
360 | ✗ | goto fail; | |
361 | } | ||
362 | 1447194 | code += 1U << (32 - len); | |
363 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1447194 times.
|
1447194 | if (code > UINT32_MAX + 1ULL) { |
364 | ✗ | av_log(logctx, AV_LOG_ERROR, "Overdetermined VLC tree\n"); | |
365 | ✗ | goto fail; | |
366 | } | ||
367 | } | ||
368 | 22040 | return vlc_common_end(vlc, nb_bits, j, buf, flags, localbuf); | |
369 | ✗ | fail: | |
370 | ✗ | if (buf != localbuf) | |
371 | ✗ | av_free(buf); | |
372 | ✗ | return AVERROR_INVALIDDATA; | |
373 | } | ||
374 | |||
375 | 31544 | void ff_free_vlc(VLC *vlc) | |
376 | { | ||
377 | 31544 | av_freep(&vlc->table); | |
378 | 31544 | } | |
379 |