FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vlc.c
Date: 2022-10-02 18:56:10
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 444014 static int alloc_table(VLC *vlc, int size, int use_static)
60 {
61 444014 int index = vlc->table_size;
62
63 444014 vlc->table_size += size;
64
2/2
✓ Branch 0 taken 50239 times.
✓ Branch 1 taken 393775 times.
444014 if (vlc->table_size > vlc->table_allocated) {
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50239 times.
50239 if (use_static)
66 abort(); // cannot do anything, init_vlc() is used with too little memory
67 50239 vlc->table_allocated += (1 << vlc->bits);
68 50239 vlc->table = av_realloc_f(vlc->table, vlc->table_allocated, sizeof(*vlc->table));
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50239 times.
50239 if (!vlc->table) {
70 vlc->table_allocated = 0;
71 vlc->table_size = 0;
72 return AVERROR(ENOMEM);
73 }
74 50239 memset(vlc->table + vlc->table_allocated - (1 << vlc->bits), 0, sizeof(*vlc->table) << vlc->bits);
75 }
76 444014 return index;
77 }
78
79 #define LOCALBUF_ELEMS 1500 // the maximum currently needed is 1296 by rv34
80
81 932512 static av_always_inline uint32_t bitswap_32(uint32_t x)
82 {
83 932512 return (uint32_t)ff_reverse[ x & 0xFF] << 24 |
84 932512 (uint32_t)ff_reverse[(x >> 8) & 0xFF] << 16 |
85 1865024 (uint32_t)ff_reverse[(x >> 16) & 0xFF] << 8 |
86 932512 (uint32_t)ff_reverse[ x >> 24];
87 }
88
89 typedef struct VLCcode {
90 uint8_t bits;
91 VLCBaseType 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 60541 static int vlc_common_init(VLC *vlc, int nb_bits, int nb_codes,
98 VLCcode **buf, int flags)
99 {
100 60541 vlc->bits = nb_bits;
101 60541 vlc->table_size = 0;
102
2/2
✓ Branch 0 taken 33537 times.
✓ Branch 1 taken 27004 times.
60541 if (flags & INIT_VLC_USE_NEW_STATIC) {
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33537 times.
33537 av_assert0(nb_codes <= LOCALBUF_ELEMS);
104 } else {
105 27004 vlc->table = NULL;
106 27004 vlc->table_allocated = 0;
107 }
108
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 60421 times.
60541 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 60541 return 0;
115 }
116
117 20163017 static int compare_vlcspec(const void *a, const void *b)
118 {
119 20163017 const VLCcode *sa = a, *sb = b;
120 20163017 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 444014 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 VLCElem *table;
142
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 444014 times.
444014 if (table_nb_bits > 30)
144 return AVERROR(EINVAL);
145 444014 table_size = 1 << table_nb_bits;
146 444014 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 444014 times.
444014 if (table_index < 0)
149 return table_index;
150 444014 table = &vlc->table[table_index];
151
152 /* first pass: map codes and compute auxiliary table sizes */
153
2/2
✓ Branch 0 taken 5054213 times.
✓ Branch 1 taken 444014 times.
5498227 for (int i = 0; i < nb_codes; i++) {
154 5054213 int n = codes[i].bits;
155 5054213 uint32_t code = codes[i].code;
156 5054213 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 4670740 times.
✓ Branch 1 taken 383473 times.
5054213 if (n <= table_nb_bits) {
159 /* no need to add another table */
160 4670740 int j = code >> (32 - table_nb_bits);
161 4670740 int nb = 1 << (table_nb_bits - n);
162 4670740 int inc = 1;
163
164
2/2
✓ Branch 0 taken 474681 times.
✓ Branch 1 taken 4196059 times.
4670740 if (flags & INIT_VLC_OUTPUT_LE) {
165 474681 j = bitswap_32(code);
166 474681 inc = 1 << n;
167 }
168
2/2
✓ Branch 0 taken 41290171 times.
✓ Branch 1 taken 4670740 times.
45960911 for (int k = 0; k < nb; k++) {
169 41290171 int bits = table[j].len;
170 41290171 int oldsym = table[j].sym;
171 ff_dlog(NULL, "%4x: code=%d n=%d\n", j, i, n);
172
2/8
✓ Branch 0 taken 41290171 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 41290171 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
41290171 if ((bits || oldsym) && (bits != n || oldsym != symbol)) {
173 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
174 return AVERROR_INVALIDDATA;
175 }
176 41290171 table[j].len = n;
177 41290171 table[j].sym = symbol;
178 41290171 j += inc;
179 }
180 } else {
181 /* fill auxiliary table recursively */
182 uint32_t code_prefix;
183 int index, subtable_bits, j, k;
184
185 383473 n -= table_nb_bits;
186 383473 code_prefix = code >> (32 - table_nb_bits);
187 383473 subtable_bits = n;
188 383473 codes[i].bits = n;
189 383473 codes[i].code = code << table_nb_bits;
190
2/2
✓ Branch 0 taken 3488349 times.
✓ Branch 1 taken 12481 times.
3500830 for (k = i + 1; k < nb_codes; k++) {
191 3488349 n = codes[k].bits - table_nb_bits;
192
2/2
✓ Branch 0 taken 43051 times.
✓ Branch 1 taken 3445298 times.
3488349 if (n <= 0)
193 43051 break;
194 3445298 code = codes[k].code;
195
2/2
✓ Branch 0 taken 327941 times.
✓ Branch 1 taken 3117357 times.
3445298 if (code >> (32 - table_nb_bits) != code_prefix)
196 327941 break;
197 3117357 codes[k].bits = n;
198 3117357 codes[k].code = code << table_nb_bits;
199 3117357 subtable_bits = FFMAX(subtable_bits, n);
200 }
201 383473 subtable_bits = FFMIN(subtable_bits, table_nb_bits);
202
2/2
✓ Branch 0 taken 51497 times.
✓ Branch 1 taken 331976 times.
383473 j = (flags & INIT_VLC_OUTPUT_LE) ? bitswap_32(code_prefix) >> (32 - table_nb_bits) : code_prefix;
203 383473 table[j].len = -subtable_bits;
204 ff_dlog(NULL, "%4x: n=%d (subtable)\n",
205 j, codes[i].bits + table_nb_bits);
206 383473 index = build_table(vlc, subtable_bits, k-i, codes+i, flags);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383473 times.
383473 if (index < 0)
208 return index;
209 /* note: realloc has been done, so reload tables */
210 383473 table = &vlc->table[table_index];
211 383473 table[j].sym = index;
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383473 times.
383473 if (table[j].sym != index) {
213 avpriv_request_sample(NULL, "strange codes");
214 return AVERROR_PATCHWELCOME;
215 }
216 383473 i = k-1;
217 }
218 }
219
220
2/2
✓ Branch 0 taken 42181148 times.
✓ Branch 1 taken 444014 times.
42625162 for (int i = 0; i < table_size; i++) {
221
2/2
✓ Branch 0 taken 507504 times.
✓ Branch 1 taken 41673644 times.
42181148 if (table[i].len == 0)
222 507504 table[i].sym = -1;
223 }
224
225 444014 return table_index;
226 }
227
228 60541 static int vlc_common_end(VLC *vlc, int nb_bits, int nb_codes, VLCcode *codes,
229 int flags, VLCcode localbuf[LOCALBUF_ELEMS])
230 {
231 60541 int ret = build_table(vlc, nb_bits, nb_codes, codes, flags);
232
233
2/2
✓ Branch 0 taken 33537 times.
✓ Branch 1 taken 27004 times.
60541 if (flags & INIT_VLC_USE_NEW_STATIC) {
234
2/2
✓ Branch 0 taken 10766 times.
✓ Branch 1 taken 22771 times.
33537 if (vlc->table_size != vlc->table_allocated &&
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10766 times.
10766 !(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 33537 times.
33537 av_assert0(ret >= 0);
238 } else {
239
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 26884 times.
27004 if (codes != localbuf)
240 120 av_free(codes);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27004 times.
27004 if (ret < 0) {
242 av_freep(&vlc->table);
243 return ret;
244 }
245 }
246 60541 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 34074 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 34074 VLCcode localbuf[LOCALBUF_ELEMS], *buf = localbuf;
279 int j, ret;
280
281 34074 ret = vlc_common_init(vlc, nb_bits, nb_codes, &buf, flags);
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34074 times.
34074 if (ret < 0)
283 return ret;
284
285
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 34074 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
34074 av_assert0(symbols_size <= 2 || !symbols);
286 34074 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 2677526 times.
✓ Branch 1 taken 314281 times.
✓ Branch 2 taken 106765 times.
✓ Branch 3 taken 1025467 times.
✓ Branch 4 taken 2073105 times.
✓ Branch 5 taken 2073105 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 2073105 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 13 taken 47524 times.
✓ Branch 14 taken 914034 times.
✓ Branch 15 taken 1111547 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 2073105 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 22 taken 333714 times.
✓ Branch 23 taken 1739391 times.
✓ Branch 25 taken 154450 times.
✓ Branch 26 taken 1918655 times.
✓ Branch 27 taken 18628 times.
✓ Branch 28 taken 135822 times.
✗ Branch 29 not taken.
✓ Branch 30 taken 3098572 times.
✓ Branch 31 taken 34074 times.
3132646 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 760562 times.
✓ Branch 1 taken 243029 times.
✓ Branch 3 taken 352914 times.
✓ Branch 4 taken 407648 times.
✓ Branch 6 taken 117587 times.
✓ Branch 7 taken 235327 times.
✓ Branch 9 taken 122524 times.
✓ Branch 10 taken 285124 times.
✓ Branch 12 taken 409793 times.
✓ Branch 13 taken 350769 times.
✓ Branch 14 taken 138682 times.
✓ Branch 15 taken 621880 times.
✓ Branch 16 taken 8517689 times.
✓ Branch 17 taken 184962 times.
✓ Branch 19 taken 6124973 times.
✓ Branch 20 taken 2392716 times.
✓ Branch 21 taken 8480827 times.
✓ Branch 22 taken 519837 times.
✓ Branch 24 taken 6422986 times.
✓ Branch 25 taken 2057841 times.
✓ Branch 26 taken 2057841 times.
✓ Branch 27 taken 519837 times.
✓ Branch 28 taken 2577678 times.
✓ Branch 29 taken 621880 times.
✓ Branch 30 taken 124113 times.
✓ Branch 31 taken 497767 times.
✓ Branch 32 taken 98306 times.
✓ Branch 33 taken 25807 times.
✓ Branch 34 taken 50172 times.
✓ Branch 35 taken 48134 times.
✓ Branch 36 taken 639786 times.
✓ Branch 37 taken 23122 times.
✓ Branch 39 taken 586929 times.
✓ Branch 40 taken 52857 times.
✓ Branch 41 taken 23122 times.
✓ Branch 42 taken 52857 times.
✓ Branch 43 taken 239811 times.
✓ Branch 44 taken 358947 times.
✓ Branch 46 taken 136867 times.
✓ Branch 47 taken 106162 times.
✓ Branch 48 taken 1003591 times.
✓ Branch 49 taken 227999 times.
✓ Branch 50 taken 632832 times.
✓ Branch 51 taken 34074 times.
16978230 AV_QSORT(buf, j, struct VLCcode, compare_vlcspec);
321
21/29
✓ Branch 0 taken 2677526 times.
✓ Branch 1 taken 314281 times.
✓ Branch 2 taken 106765 times.
✓ Branch 3 taken 3014485 times.
✓ Branch 4 taken 84087 times.
✓ Branch 5 taken 2073105 times.
✓ Branch 6 taken 941380 times.
✓ Branch 7 taken 941380 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 941380 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 15 taken 173660 times.
✓ Branch 16 taken 473435 times.
✓ Branch 17 taken 294285 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 941380 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 24 taken 72620 times.
✓ Branch 25 taken 868760 times.
✓ Branch 27 taken 315563 times.
✓ Branch 28 taken 625817 times.
✓ Branch 29 taken 148682 times.
✓ Branch 30 taken 166881 times.
✗ Branch 31 not taken.
✓ Branch 32 taken 3098572 times.
✓ Branch 33 taken 34074 times.
3132646 COPY(len && len <= nb_bits);
322 34074 nb_codes = j;
323
324 34074 return vlc_common_end(vlc, nb_bits, nb_codes, buf,
325 flags, localbuf);
326 }
327
328 26467 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 26467 VLCcode localbuf[LOCALBUF_ELEMS], *buf = localbuf;
334 uint64_t code;
335 26467 int ret, j, len_max = FFMIN(32, 3 * nb_bits);
336
337 26467 ret = vlc_common_init(vlc, nb_bits, nb_codes, &buf, flags);
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26467 times.
26467 if (ret < 0)
339 return ret;
340
341 26467 j = code = 0;
342
2/2
✓ Branch 0 taken 1659979 times.
✓ Branch 1 taken 26467 times.
1686446 for (int i = 0; i < nb_codes; i++, lens += lens_wrap) {
343 1659979 int len = *lens;
344
2/2
✓ Branch 0 taken 1656255 times.
✓ Branch 1 taken 3724 times.
1659979 if (len > 0) {
345 unsigned sym;
346
347 1656255 buf[j].bits = len;
348
2/2
✓ Branch 0 taken 1622781 times.
✓ Branch 1 taken 33474 times.
1656255 if (symbols)
349
2/3
✓ Branch 0 taken 407387 times.
✓ Branch 1 taken 1215394 times.
✗ Branch 2 not taken.
1622781 GET_DATA(sym, symbols, i, symbols_wrap, symbols_size)
350 else
351 33474 sym = i;
352 1656255 buf[j].symbol = sym + offset;
353 1656255 buf[j++].code = code;
354
2/2
✓ Branch 0 taken 1568 times.
✓ Branch 1 taken 2156 times.
3724 } else if (len < 0) {
355 1568 len = -len;
356 } else
357 2156 continue;
358
2/4
✓ Branch 0 taken 1657823 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1657823 times.
1657823 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 1657823 code += 1U << (32 - len);
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1657823 times.
1657823 if (code > UINT32_MAX + 1ULL) {
364 av_log(logctx, AV_LOG_ERROR, "Overdetermined VLC tree\n");
365 goto fail;
366 }
367 }
368 26467 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 31610 void ff_free_vlc(VLC *vlc)
376 {
377 31610 av_freep(&vlc->table);
378 31610 }
379