../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
732 / 942
77.7%
Branches:
414 / 604
68.5%
Functions:
21 / 22
95.5%

libavcodec/vorbisdec.c
Line Branch Exec Source
1 /**
2 * @file
3 * Vorbis I decoder
4 * @author Denes Balatoni ( dbalatoni programozo hu )
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Vorbis I decoder
26 * @author Denes Balatoni ( dbalatoni programozo hu )
27 */
28
29 #include <inttypes.h>
30 #include <math.h>
31
32 #include "libavutil/avassert.h"
33 #include "libavutil/dict.h"
34 #include "libavutil/float_dsp.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/tx.h"
37
38 #define BITSTREAM_READER_LE
39 #include "avcodec.h"
40 #include "codec_internal.h"
41 #include "decode.h"
42 #include "get_bits.h"
43 #include "internal.h"
44 #include "vorbis.h"
45 #include "vorbisdsp.h"
46 #include "vorbis_data.h"
47 #include "xiph.h"
48
49 #define V_NB_BITS 8
50 #define V_NB_BITS2 11
51 #define V_MAX_VLCS (1 << 16)
52 #define V_MAX_PARTITIONS (1 << 20)
53
54 typedef struct vorbis_codebook {
55 uint8_t dimensions;
56 uint8_t lookup_type;
57 uint8_t maxdepth;
58 VLC vlc;
59 float *codevectors;
60 unsigned int nb_bits;
61 } vorbis_codebook;
62
63 typedef union vorbis_floor_u vorbis_floor_data;
64 typedef struct vorbis_floor0_s vorbis_floor0;
65 typedef struct vorbis_floor1_s vorbis_floor1;
66 struct vorbis_context_s;
67 typedef
68 int (* vorbis_floor_decode_func)
69 (struct vorbis_context_s *, vorbis_floor_data *, float *);
70 typedef struct vorbis_floor {
71 uint8_t floor_type;
72 vorbis_floor_decode_func decode;
73 union vorbis_floor_u {
74 struct vorbis_floor0_s {
75 uint8_t order;
76 uint16_t rate;
77 uint16_t bark_map_size;
78 int32_t *map[2];
79 uint32_t map_size[2];
80 uint8_t amplitude_bits;
81 uint8_t amplitude_offset;
82 uint8_t num_books;
83 uint8_t *book_list;
84 float *lsp;
85 } t0;
86 struct vorbis_floor1_s {
87 uint8_t partitions;
88 uint8_t partition_class[32];
89 uint8_t class_dimensions[16];
90 uint8_t class_subclasses[16];
91 uint8_t class_masterbook[16];
92 int16_t subclass_books[16][8];
93 uint8_t multiplier;
94 uint16_t x_list_dim;
95 vorbis_floor1_entry *list;
96 } t1;
97 } data;
98 } vorbis_floor;
99
100 typedef struct vorbis_residue {
101 uint16_t type;
102 uint32_t begin;
103 uint32_t end;
104 unsigned partition_size;
105 uint8_t classifications;
106 uint8_t classbook;
107 int16_t books[64][8];
108 uint8_t maxpass;
109 uint16_t ptns_to_read;
110 uint8_t *classifs;
111 } vorbis_residue;
112
113 typedef struct vorbis_mapping {
114 uint8_t submaps;
115 uint16_t coupling_steps;
116 uint8_t *magnitude;
117 uint8_t *angle;
118 uint8_t *mux;
119 uint8_t submap_floor[16];
120 uint8_t submap_residue[16];
121 } vorbis_mapping;
122
123 typedef struct vorbis_mode {
124 uint8_t blockflag;
125 uint16_t windowtype;
126 uint16_t transformtype;
127 uint8_t mapping;
128 } vorbis_mode;
129
130 typedef struct vorbis_context_s {
131 AVCodecContext *avctx;
132 GetBitContext gb;
133 VorbisDSPContext dsp;
134 AVFloatDSPContext *fdsp;
135
136 AVTXContext *mdct[2];
137 av_tx_fn mdct_fn[2];
138
139 uint8_t first_frame;
140 uint32_t version;
141 uint8_t audio_channels;
142 uint32_t audio_samplerate;
143 uint32_t bitrate_maximum;
144 uint32_t bitrate_nominal;
145 uint32_t bitrate_minimum;
146 uint32_t blocksize[2];
147 const float *win[2];
148 uint16_t codebook_count;
149 vorbis_codebook *codebooks;
150 uint8_t floor_count;
151 vorbis_floor *floors;
152 uint8_t residue_count;
153 vorbis_residue *residues;
154 uint8_t mapping_count;
155 vorbis_mapping *mappings;
156 uint8_t mode_count;
157 vorbis_mode *modes;
158 uint8_t mode_number; // mode number for the current packet
159 int8_t previous_window;
160 float *channel_residues;
161 float *saved;
162 /**
163 * Tags of a dropped priming frame, added to the first frame with samples.
164 */
165 AVDictionary *pending_metadata;
166 } vorbis_context;
167
168 /* Helper functions */
169
170 #define BARK(x) \
171 (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x))
172
173 static const char idx_err_str[] = "Index value %d out of range (0 - %d) for %s at %s:%i\n";
174 #define VALIDATE_INDEX(idx, limit) \
175 if (idx >= limit) {\
176 av_log(vc->avctx, AV_LOG_ERROR,\
177 idx_err_str,\
178 (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\
179 return AVERROR_INVALIDDATA;\
180 }
181 #define GET_VALIDATED_INDEX(idx, bits, limit) \
182 {\
183 idx = get_bits(gb, bits);\
184 VALIDATE_INDEX(idx, limit)\
185 }
186
187 2384 static float vorbisfloat2float(unsigned val)
188 {
189 2384 float mant = val & 0x1fffff;
190 2384 int exp = (val & 0x7fe00000) >> 21;
191
2/2
✓ Branch 2 → 3 taken 1104 times.
✓ Branch 2 → 4 taken 1280 times.
2384 if (val & 0x80000000)
192 1104 mant = -mant;
193 2384 return ldexpf(mant, exp - 20 - 768);
194 }
195
196
197 // Free all allocated memory -----------------------------------------
198
199 89 static void vorbis_free(vorbis_context *vc)
200 {
201 int i;
202
203 89 av_freep(&vc->channel_residues);
204 89 av_freep(&vc->saved);
205 89 av_freep(&vc->fdsp);
206
207
1/2
✓ Branch 5 → 6 taken 89 times.
✗ Branch 5 → 10 not taken.
89 if (vc->residues)
208
2/2
✓ Branch 9 → 7 taken 178 times.
✓ Branch 9 → 10 taken 89 times.
267 for (i = 0; i < vc->residue_count; i++)
209 178 av_freep(&vc->residues[i].classifs);
210 89 av_freep(&vc->residues);
211 89 av_freep(&vc->modes);
212
213 89 av_tx_uninit(&vc->mdct[0]);
214 89 av_tx_uninit(&vc->mdct[1]);
215
216
1/2
✓ Branch 14 → 15 taken 89 times.
✗ Branch 14 → 20 not taken.
89 if (vc->codebooks)
217
2/2
✓ Branch 19 → 16 taken 2789 times.
✓ Branch 19 → 20 taken 89 times.
2878 for (i = 0; i < vc->codebook_count; ++i) {
218 2789 av_freep(&vc->codebooks[i].codevectors);
219 2789 ff_vlc_free(&vc->codebooks[i].vlc);
220 }
221 89 av_freep(&vc->codebooks);
222
223
1/2
✓ Branch 21 → 22 taken 89 times.
✗ Branch 21 → 31 not taken.
89 if (vc->floors)
224
2/2
✓ Branch 30 → 23 taken 178 times.
✓ Branch 30 → 31 taken 89 times.
267 for (i = 0; i < vc->floor_count; ++i) {
225
2/2
✓ Branch 23 → 24 taken 54 times.
✓ Branch 23 → 28 taken 124 times.
178 if (vc->floors[i].floor_type == 0) {
226 54 av_freep(&vc->floors[i].data.t0.map[0]);
227 54 av_freep(&vc->floors[i].data.t0.map[1]);
228 54 av_freep(&vc->floors[i].data.t0.book_list);
229 54 av_freep(&vc->floors[i].data.t0.lsp);
230 } else {
231 124 av_freep(&vc->floors[i].data.t1.list);
232 }
233 }
234 89 av_freep(&vc->floors);
235
236
1/2
✓ Branch 32 → 33 taken 89 times.
✗ Branch 32 → 39 not taken.
89 if (vc->mappings)
237
2/2
✓ Branch 38 → 34 taken 176 times.
✓ Branch 38 → 39 taken 89 times.
265 for (i = 0; i < vc->mapping_count; ++i) {
238 176 av_freep(&vc->mappings[i].magnitude);
239 176 av_freep(&vc->mappings[i].angle);
240 176 av_freep(&vc->mappings[i].mux);
241 }
242 89 av_freep(&vc->mappings);
243 89 }
244
245 // Parse setup header -------------------------------------------------
246
247 // Process codebooks part
248
249 89 static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc)
250 {
251 unsigned cb;
252 89 uint8_t *tmp_vlc_bits = NULL;
253 89 uint32_t *tmp_vlc_codes = NULL;
254 89 GetBitContext *gb = &vc->gb;
255 89 uint16_t *codebook_multiplicands = NULL;
256 89 int ret = 0;
257
258 89 vc->codebook_count = get_bits(gb, 8) + 1;
259
260 ff_dlog(NULL, " Codebooks: %d \n", vc->codebook_count);
261
262 89 vc->codebooks = av_mallocz(vc->codebook_count * sizeof(*vc->codebooks));
263 89 tmp_vlc_bits = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_bits));
264 89 tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_codes));
265 89 codebook_multiplicands = av_malloc(V_MAX_VLCS * sizeof(*codebook_multiplicands));
266
2/4
✓ Branch 7 → 8 taken 89 times.
✗ Branch 7 → 11 not taken.
✓ Branch 8 → 9 taken 89 times.
✗ Branch 8 → 11 not taken.
89 if (!vc->codebooks ||
267
2/4
✓ Branch 9 → 10 taken 89 times.
✗ Branch 9 → 11 not taken.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 89 times.
89 !tmp_vlc_bits || !tmp_vlc_codes || !codebook_multiplicands) {
268 ✗ ret = AVERROR(ENOMEM);
269 ✗ goto error;
270 }
271
272
2/2
✓ Branch 114 → 13 taken 2789 times.
✓ Branch 114 → 115 taken 89 times.
2878 for (cb = 0; cb < vc->codebook_count; ++cb) {
273 2789 vorbis_codebook *codebook_setup = &vc->codebooks[cb];
274 2789 unsigned ordered, t, entries, used_entries = 0;
275
276 ff_dlog(NULL, " %u. Codebook\n", cb);
277
278
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 17 taken 2789 times.
2789 if (get_bits(gb, 24) != 0x564342) {
279 ✗ av_log(vc->avctx, AV_LOG_ERROR,
280 " %u. Codebook setup data corrupt.\n", cb);
281 ✗ ret = AVERROR_INVALIDDATA;
282 ✗ goto error;
283 }
284
285 2789 codebook_setup->dimensions=get_bits(gb, 16);
286
2/4
✓ Branch 18 → 19 taken 2789 times.
✗ Branch 18 → 20 not taken.
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 22 taken 2789 times.
2789 if (codebook_setup->dimensions > 16 || codebook_setup->dimensions == 0) {
287 ✗ av_log(vc->avctx, AV_LOG_ERROR,
288 " %u. Codebook's dimension is invalid (%d).\n",
289 ✗ cb, codebook_setup->dimensions);
290 ✗ ret = AVERROR_INVALIDDATA;
291 ✗ goto error;
292 }
293 2789 entries = get_bits(gb, 24);
294
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 26 taken 2789 times.
2789 if (entries > V_MAX_VLCS) {
295 ✗ av_log(vc->avctx, AV_LOG_ERROR,
296 " %u. Codebook has too many entries (%u).\n",
297 cb, entries);
298 ✗ ret = AVERROR_INVALIDDATA;
299 ✗ goto error;
300 }
301
302 2789 ordered = get_bits1(gb);
303
304 ff_dlog(NULL, " codebook_dimensions %d, codebook_entries %u\n",
305 codebook_setup->dimensions, entries);
306
307
2/2
✓ Branch 27 → 28 taken 2762 times.
✓ Branch 27 → 42 taken 27 times.
2789 if (!ordered) {
308 unsigned ce, flag;
309 2762 unsigned sparse = get_bits1(gb);
310
311 ff_dlog(NULL, " not ordered \n");
312
313
2/2
✓ Branch 29 → 30 taken 1144 times.
✓ Branch 29 → 38 taken 1618 times.
2762 if (sparse) {
314 ff_dlog(NULL, " sparse \n");
315
316 1144 used_entries = 0;
317
2/2
✓ Branch 37 → 31 taken 377200 times.
✓ Branch 37 → 56 taken 1144 times.
378344 for (ce = 0; ce < entries; ++ce) {
318 377200 flag = get_bits1(gb);
319
2/2
✓ Branch 32 → 33 taken 125715 times.
✓ Branch 32 → 35 taken 251485 times.
377200 if (flag) {
320 125715 tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
321 125715 ++used_entries;
322 } else
323 251485 tmp_vlc_bits[ce] = 0;
324 }
325 } else {
326 ff_dlog(NULL, " not sparse \n");
327
328 1618 used_entries = entries;
329
2/2
✓ Branch 41 → 39 taken 427354 times.
✓ Branch 41 → 56 taken 1618 times.
428972 for (ce = 0; ce < entries; ++ce)
330 427354 tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
331 }
332 } else {
333 27 unsigned current_entry = 0;
334 27 unsigned current_length = get_bits(gb, 5) + 1;
335
336 ff_dlog(NULL, " ordered, current length: %u\n", current_length); //FIXME
337
338 27 used_entries = entries;
339
3/4
✓ Branch 51 → 52 taken 87 times.
✓ Branch 51 → 53 taken 27 times.
✓ Branch 52 → 44 taken 87 times.
✗ Branch 52 → 53 not taken.
114 for (; current_entry < used_entries && current_length <= 32; ++current_length) {
340 unsigned i, number;
341
342 ff_dlog(NULL, " number bits: %u ", ilog(entries - current_entry));
343
344 87 number = get_bits(gb, ilog(entries - current_entry));
345
346 ff_dlog(NULL, " number: %u\n", number);
347
348
2/2
✓ Branch 49 → 46 taken 914 times.
✓ Branch 49 → 50 taken 87 times.
1001 for (i = current_entry; i < number+current_entry; ++i)
349
1/2
✓ Branch 46 → 47 taken 914 times.
✗ Branch 46 → 48 not taken.
914 if (i < used_entries)
350 914 tmp_vlc_bits[i] = current_length;
351
352 87 current_entry+=number;
353 }
354
1/2
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 56 taken 27 times.
27 if (current_entry>used_entries) {
355 ✗ av_log(vc->avctx, AV_LOG_ERROR, " More codelengths than codes in codebook. \n");
356 ✗ ret = AVERROR_INVALIDDATA;
357 ✗ goto error;
358 }
359 }
360
361 2789 codebook_setup->lookup_type = get_bits(gb, 4);
362
363 ff_dlog(NULL, " lookup type: %d : %s \n", codebook_setup->lookup_type,
364 codebook_setup->lookup_type ? "vq" : "no lookup");
365
366 // If the codebook is used for (inverse) VQ, calculate codevectors.
367
368
2/2
✓ Branch 57 → 58 taken 1192 times.
✓ Branch 57 → 94 taken 1597 times.
2789 if (codebook_setup->lookup_type == 1) {
369 unsigned i, j, k;
370 1192 unsigned codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions);
371
372 1192 float codebook_minimum_value = vorbisfloat2float(get_bits_long(gb, 32));
373 1192 float codebook_delta_value = vorbisfloat2float(get_bits_long(gb, 32));
374 1192 unsigned codebook_value_bits = get_bits(gb, 4) + 1;
375 1192 unsigned codebook_sequence_p = get_bits1(gb);
376
377
2/4
✓ Branch 65 → 66 taken 1192 times.
✗ Branch 65 → 67 not taken.
✗ Branch 66 → 67 not taken.
✓ Branch 66 → 68 taken 1192 times.
1192 if (!isfinite(codebook_minimum_value) || !isfinite(codebook_delta_value)) {
378 ✗ ret = AVERROR_INVALIDDATA;
379 ✗ goto error;
380 }
381 ff_dlog(NULL, " We expect %d numbers for building the codevectors. \n",
382 codebook_lookup_values);
383 ff_dlog(NULL, " delta %f minimum %f \n",
384 codebook_delta_value, codebook_minimum_value);
385
386
2/2
✓ Branch 71 → 69 taken 23755 times.
✓ Branch 71 → 72 taken 1192 times.
24947 for (i = 0; i < codebook_lookup_values; ++i) {
387 23755 codebook_multiplicands[i] = get_bits(gb, codebook_value_bits);
388
389 ff_dlog(NULL, " multiplicands*delta+minimum : %e \n",
390 (float)codebook_multiplicands[i] * codebook_delta_value + codebook_minimum_value);
391 ff_dlog(NULL, " multiplicand %u\n", codebook_multiplicands[i]);
392 }
393
394 // Weed out unused vlcs and build codevector vector
395
1/2
✓ Branch 72 → 73 taken 1192 times.
✗ Branch 72 → 76 not taken.
1192 if (used_entries) {
396 1192 codebook_setup->codevectors =
397 1192 av_calloc(used_entries, codebook_setup->dimensions *
398 sizeof(*codebook_setup->codevectors));
399
1/2
✗ Branch 74 → 75 not taken.
✓ Branch 74 → 77 taken 1192 times.
1192 if (!codebook_setup->codevectors) {
400 ✗ ret = AVERROR(ENOMEM);
401 ✗ goto error;
402 }
403 } else
404 ✗ codebook_setup->codevectors = NULL;
405
406
2/2
✓ Branch 89 → 78 taken 675501 times.
✓ Branch 89 → 90 taken 1192 times.
676693 for (j = 0, i = 0; i < entries; ++i) {
407 675501 unsigned dim = codebook_setup->dimensions;
408
409
2/2
✓ Branch 78 → 79 taken 441141 times.
✓ Branch 78 → 88 taken 234360 times.
675501 if (tmp_vlc_bits[i]) {
410 441141 float last = 0.0;
411 441141 unsigned lookup_offset = i;
412
413 ff_dlog(vc->avctx, "Lookup offset %u ,", i);
414
415
2/2
✓ Branch 83 → 80 taken 1304376 times.
✓ Branch 83 → 84 taken 441141 times.
1745517 for (k = 0; k < dim; ++k) {
416 1304376 unsigned multiplicand_offset = lookup_offset % codebook_lookup_values;
417 1304376 codebook_setup->codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;
418
2/2
✓ Branch 80 → 81 taken 346825 times.
✓ Branch 80 → 82 taken 957551 times.
1304376 if (codebook_sequence_p)
419 346825 last = codebook_setup->codevectors[j * dim + k];
420 1304376 lookup_offset/=codebook_lookup_values;
421 }
422 441141 tmp_vlc_bits[j] = tmp_vlc_bits[i];
423
424 ff_dlog(vc->avctx, "real lookup offset %u, vector: ", j);
425
2/2
✓ Branch 86 → 85 taken 1304376 times.
✓ Branch 86 → 87 taken 441141 times.
1745517 for (k = 0; k < dim; ++k)
426 ff_dlog(vc->avctx, " %f ",
427 codebook_setup->codevectors[j * dim + k]);
428 ff_dlog(vc->avctx, "\n");
429
430 441141 ++j;
431 }
432 }
433
1/2
✗ Branch 90 → 91 not taken.
✓ Branch 90 → 93 taken 1192 times.
1192 if (j != used_entries) {
434 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Bug in codevector vector building code. \n");
435 ✗ ret = AVERROR_INVALIDDATA;
436 ✗ goto error;
437 }
438 1192 entries = used_entries;
439
1/2
✗ Branch 94 → 95 not taken.
✓ Branch 94 → 97 taken 1597 times.
1597 } else if (codebook_setup->lookup_type >= 2) {
440 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Codebook lookup type not supported. \n");
441 ✗ ret = AVERROR_INVALIDDATA;
442 ✗ goto error;
443 }
444
445 // Initialize VLC table
446
1/2
✗ Branch 98 → 99 not taken.
✓ Branch 98 → 101 taken 2789 times.
2789 if (ff_vorbis_len2vlc(tmp_vlc_bits, tmp_vlc_codes, entries)) {
447 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n");
448 ✗ ret = AVERROR_INVALIDDATA;
449 ✗ goto error;
450 }
451 2789 codebook_setup->maxdepth = 0;
452
2/2
✓ Branch 105 → 102 taken 571108 times.
✓ Branch 105 → 106 taken 2789 times.
573897 for (t = 0; t < entries; ++t)
453
2/2
✓ Branch 102 → 103 taken 153820 times.
✓ Branch 102 → 104 taken 417288 times.
571108 if (tmp_vlc_bits[t] >= codebook_setup->maxdepth)
454 153820 codebook_setup->maxdepth = tmp_vlc_bits[t];
455
456
1/2
✗ Branch 106 → 107 not taken.
✓ Branch 106 → 108 taken 2789 times.
2789 if (codebook_setup->maxdepth > 3 * V_NB_BITS)
457 ✗ codebook_setup->nb_bits = V_NB_BITS2;
458 else
459 2789 codebook_setup->nb_bits = V_NB_BITS;
460
461 2789 codebook_setup->maxdepth = (codebook_setup->maxdepth+codebook_setup->nb_bits - 1) / codebook_setup->nb_bits;
462
463
1/2
✗ Branch 110 → 111 not taken.
✓ Branch 110 → 113 taken 2789 times.
2789 if ((ret = vlc_init(&codebook_setup->vlc, codebook_setup->nb_bits,
464 entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits),
465 sizeof(*tmp_vlc_bits), tmp_vlc_codes,
466 sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes),
467 VLC_INIT_LE))) {
468 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Error generating vlc tables. \n");
469 ✗ goto error;
470 }
471 }
472
473 89 av_free(tmp_vlc_bits);
474 89 av_free(tmp_vlc_codes);
475 89 av_free(codebook_multiplicands);
476 89 return 0;
477
478 // Error:
479 ✗ error:
480 ✗ av_free(tmp_vlc_bits);
481 ✗ av_free(tmp_vlc_codes);
482 ✗ av_free(codebook_multiplicands);
483 ✗ return ret;
484 }
485
486 // Process time domain transforms part (unused in Vorbis I)
487
488 89 static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc)
489 {
490 89 GetBitContext *gb = &vc->gb;
491 89 unsigned i, vorbis_time_count = get_bits(gb, 6) + 1;
492
493
2/2
✓ Branch 9 → 4 taken 89 times.
✓ Branch 9 → 10 taken 89 times.
178 for (i = 0; i < vorbis_time_count; ++i) {
494 89 unsigned vorbis_tdtransform = get_bits(gb, 16);
495
496 ff_dlog(NULL, " Vorbis time domain transform %u: %u\n",
497 vorbis_time_count, vorbis_tdtransform);
498
499
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 89 times.
89 if (vorbis_tdtransform) {
500 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Vorbis time domain transform data nonzero. \n");
501 ✗ return AVERROR_INVALIDDATA;
502 }
503 }
504 89 return 0;
505 }
506
507 // Process floors part
508
509 static int vorbis_floor0_decode(vorbis_context *vc,
510 vorbis_floor_data *vfu, float *vec);
511 static int create_map(vorbis_context *vc, unsigned floor_number);
512 static int vorbis_floor1_decode(vorbis_context *vc,
513 vorbis_floor_data *vfu, float *vec);
514 89 static int vorbis_parse_setup_hdr_floors(vorbis_context *vc)
515 {
516 89 GetBitContext *gb = &vc->gb;
517 int i, j, k, ret;
518
519 89 vc->floor_count = get_bits(gb, 6) + 1;
520
521 89 vc->floors = av_mallocz(vc->floor_count * sizeof(*vc->floors));
522
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 89 times.
89 if (!vc->floors)
523 ✗ return AVERROR(ENOMEM);
524
525
2/2
✓ Branch 99 → 7 taken 178 times.
✓ Branch 99 → 100 taken 89 times.
267 for (i = 0; i < vc->floor_count; ++i) {
526 178 vorbis_floor *floor_setup = &vc->floors[i];
527
528 178 floor_setup->floor_type = get_bits(gb, 16);
529
530 ff_dlog(NULL, " %d. floor type %d \n", i, floor_setup->floor_type);
531
532
2/2
✓ Branch 8 → 9 taken 124 times.
✓ Branch 8 → 59 taken 54 times.
178 if (floor_setup->floor_type == 1) {
533 124 int maximum_class = -1;
534 124 unsigned rangebits, rangemax, floor1_values = 2;
535
536 124 floor_setup->decode = vorbis_floor1_decode;
537
538 124 floor_setup->data.t1.partitions = get_bits(gb, 5);
539
540 ff_dlog(NULL, " %d.floor: %d partitions \n",
541 i, floor_setup->data.t1.partitions);
542
543
2/2
✓ Branch 15 → 11 taken 679 times.
✓ Branch 15 → 16 taken 124 times.
803 for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
544 679 floor_setup->data.t1.partition_class[j] = get_bits(gb, 4);
545
2/2
✓ Branch 12 → 13 taken 441 times.
✓ Branch 12 → 14 taken 238 times.
679 if (floor_setup->data.t1.partition_class[j] > maximum_class)
546 441 maximum_class = floor_setup->data.t1.partition_class[j];
547
548 ff_dlog(NULL, " %d. floor %d partition class %d \n",
549 i, j, floor_setup->data.t1.partition_class[j]);
550
551 }
552
553 ff_dlog(NULL, " maximum class %d \n", maximum_class);
554
555
2/2
✓ Branch 33 → 17 taken 441 times.
✓ Branch 33 → 34 taken 124 times.
565 for (j = 0; j <= maximum_class; ++j) {
556 441 floor_setup->data.t1.class_dimensions[j] = get_bits(gb, 3) + 1;
557 441 floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2);
558
559 ff_dlog(NULL, " %d floor %d class dim: %d subclasses %d \n", i, j,
560 floor_setup->data.t1.class_dimensions[j],
561 floor_setup->data.t1.class_subclasses[j]);
562
563
2/2
✓ Branch 19 → 20 taken 367 times.
✓ Branch 19 → 24 taken 74 times.
441 if (floor_setup->data.t1.class_subclasses[j]) {
564
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 367 times.
367 GET_VALIDATED_INDEX(floor_setup->data.t1.class_masterbook[j], 8, vc->codebook_count)
565
566 ff_dlog(NULL, " masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]);
567 }
568
569
2/2
✓ Branch 31 → 25 taken 1270 times.
✓ Branch 31 → 32 taken 441 times.
1711 for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) {
570 1270 int16_t bits = get_bits(gb, 8) - 1;
571
2/2
✓ Branch 26 → 27 taken 1052 times.
✓ Branch 26 → 30 taken 218 times.
1270 if (bits != -1)
572
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 30 taken 1052 times.
1052 VALIDATE_INDEX(bits, vc->codebook_count)
573 1270 floor_setup->data.t1.subclass_books[j][k] = bits;
574
575 ff_dlog(NULL, " book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]);
576 }
577 }
578
579 124 floor_setup->data.t1.multiplier = get_bits(gb, 2) + 1;
580 124 floor_setup->data.t1.x_list_dim = 2;
581
582
2/2
✓ Branch 37 → 36 taken 679 times.
✓ Branch 37 → 38 taken 124 times.
803 for (j = 0; j < floor_setup->data.t1.partitions; ++j)
583 679 floor_setup->data.t1.x_list_dim+=floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]];
584
585 124 floor_setup->data.t1.list = av_calloc(floor_setup->data.t1.x_list_dim,
586 sizeof(*floor_setup->data.t1.list));
587
1/2
✗ Branch 39 → 40 not taken.
✓ Branch 39 → 41 taken 124 times.
124 if (!floor_setup->data.t1.list)
588 ✗ return AVERROR(ENOMEM);
589
590 124 rangebits = get_bits(gb, 4);
591
1/4
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 46 taken 124 times.
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 46 not taken.
124 if (!rangebits && floor_setup->data.t1.partitions) {
592 ✗ av_log(vc->avctx, AV_LOG_ERROR,
593 "A rangebits value of 0 is not compliant with the Vorbis I specification.\n");
594 ✗ return AVERROR_INVALIDDATA;
595 }
596 124 rangemax = (1 << rangebits);
597
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 49 taken 124 times.
124 if (rangemax > vc->blocksize[1] / 2) {
598 ✗ av_log(vc->avctx, AV_LOG_ERROR,
599 "Floor value is too large for blocksize: %u (%"PRIu32")\n",
600 ✗ rangemax, vc->blocksize[1] / 2);
601 ✗ return AVERROR_INVALIDDATA;
602 }
603 124 floor_setup->data.t1.list[0].x = 0;
604 124 floor_setup->data.t1.list[1].x = rangemax;
605
606
2/2
✓ Branch 55 → 50 taken 679 times.
✓ Branch 55 → 56 taken 124 times.
803 for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
607
2/2
✓ Branch 53 → 51 taken 2221 times.
✓ Branch 53 → 54 taken 679 times.
2900 for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) {
608 2221 floor_setup->data.t1.list[floor1_values].x = get_bits(gb, rangebits);
609
610 ff_dlog(NULL, " %u. floor1 Y coord. %d\n", floor1_values,
611 floor_setup->data.t1.list[floor1_values].x);
612 }
613 }
614
615 // Precalculate order of x coordinates - needed for decode
616
1/2
✗ Branch 57 → 58 not taken.
✓ Branch 57 → 98 taken 124 times.
124 if (ff_vorbis_ready_floor1_list(vc->avctx,
617 floor_setup->data.t1.list,
618 124 floor_setup->data.t1.x_list_dim)) {
619 ✗ return AVERROR_INVALIDDATA;
620 }
621
1/2
✓ Branch 59 → 60 taken 54 times.
✗ Branch 59 → 96 not taken.
54 } else if (floor_setup->floor_type == 0) {
622 54 unsigned max_codebook_dim = 0;
623
624 54 floor_setup->decode = vorbis_floor0_decode;
625
626 54 floor_setup->data.t0.order = get_bits(gb, 8);
627
1/2
✗ Branch 61 → 62 not taken.
✓ Branch 61 → 64 taken 54 times.
54 if (!floor_setup->data.t0.order) {
628 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Floor 0 order is 0.\n");
629 ✗ return AVERROR_INVALIDDATA;
630 }
631 54 floor_setup->data.t0.rate = get_bits(gb, 16);
632
1/2
✗ Branch 65 → 66 not taken.
✓ Branch 65 → 68 taken 54 times.
54 if (!floor_setup->data.t0.rate) {
633 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Floor 0 rate is 0.\n");
634 ✗ return AVERROR_INVALIDDATA;
635 }
636 54 floor_setup->data.t0.bark_map_size = get_bits(gb, 16);
637
1/2
✗ Branch 69 → 70 not taken.
✓ Branch 69 → 72 taken 54 times.
54 if (!floor_setup->data.t0.bark_map_size) {
638 ✗ av_log(vc->avctx, AV_LOG_ERROR,
639 "Floor 0 bark map size is 0.\n");
640 ✗ return AVERROR_INVALIDDATA;
641 }
642 54 floor_setup->data.t0.amplitude_bits = get_bits(gb, 6);
643 54 floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
644 54 floor_setup->data.t0.num_books = get_bits(gb, 4) + 1;
645
646 /* allocate mem for booklist */
647 54 floor_setup->data.t0.book_list =
648 54 av_malloc(floor_setup->data.t0.num_books);
649
1/2
✗ Branch 76 → 77 not taken.
✓ Branch 76 → 78 taken 54 times.
54 if (!floor_setup->data.t0.book_list)
650 ✗ return AVERROR(ENOMEM);
651 /* read book indexes */
652 {
653 int idx;
654 unsigned book_idx;
655
2/2
✓ Branch 86 → 79 taken 88 times.
✓ Branch 86 → 87 taken 54 times.
142 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
656
1/2
✗ Branch 80 → 81 not taken.
✓ Branch 80 → 83 taken 88 times.
88 GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count)
657 88 floor_setup->data.t0.book_list[idx] = book_idx;
658
1/2
✓ Branch 83 → 84 taken 88 times.
✗ Branch 83 → 85 not taken.
88 if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
659 88 max_codebook_dim = vc->codebooks[book_idx].dimensions;
660 }
661 }
662
663
1/2
✗ Branch 88 → 89 not taken.
✓ Branch 88 → 90 taken 54 times.
54 if ((ret = create_map(vc, i)) < 0)
664 ✗ return ret;
665
666 /* codebook dim is for padding if codebook dim doesn't *
667 * divide order+1 then we need to read more data */
668 54 floor_setup->data.t0.lsp =
669 54 av_malloc_array((floor_setup->data.t0.order + 1 + max_codebook_dim),
670 sizeof(*floor_setup->data.t0.lsp));
671
1/2
✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 54 times.
54 if (!floor_setup->data.t0.lsp)
672 ✗ return AVERROR(ENOMEM);
673
674 /* debug output parsed headers */
675 ff_dlog(NULL, "floor0 order: %u\n", floor_setup->data.t0.order);
676 ff_dlog(NULL, "floor0 rate: %u\n", floor_setup->data.t0.rate);
677 ff_dlog(NULL, "floor0 bark map size: %u\n",
678 floor_setup->data.t0.bark_map_size);
679 ff_dlog(NULL, "floor0 amplitude bits: %u\n",
680 floor_setup->data.t0.amplitude_bits);
681 ff_dlog(NULL, "floor0 amplitude offset: %u\n",
682 floor_setup->data.t0.amplitude_offset);
683 ff_dlog(NULL, "floor0 number of books: %u\n",
684 floor_setup->data.t0.num_books);
685 ff_dlog(NULL, "floor0 book list pointer: %p\n",
686 floor_setup->data.t0.book_list);
687 {
688 int idx;
689
2/2
✓ Branch 95 → 94 taken 88 times.
✓ Branch 95 → 98 taken 54 times.
142 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
690 ff_dlog(NULL, " Book %d: %u\n", idx + 1,
691 floor_setup->data.t0.book_list[idx]);
692 }
693 }
694 } else {
695 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Invalid floor type!\n");
696 ✗ return AVERROR_INVALIDDATA;
697 }
698 }
699 89 return 0;
700 }
701
702 // Process residues part
703
704 89 static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
705 {
706 89 GetBitContext *gb = &vc->gb;
707 unsigned i, j, k;
708
709 89 vc->residue_count = get_bits(gb, 6)+1;
710 89 vc->residues = av_mallocz(vc->residue_count * sizeof(*vc->residues));
711
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 89 times.
89 if (!vc->residues)
712 ✗ return AVERROR(ENOMEM);
713
714 ff_dlog(NULL, " There are %d residues. \n", vc->residue_count);
715
716
2/2
✓ Branch 46 → 7 taken 178 times.
✓ Branch 46 → 47 taken 89 times.
267 for (i = 0; i < vc->residue_count; ++i) {
717 178 vorbis_residue *res_setup = &vc->residues[i];
718 uint8_t cascade[64];
719 unsigned high_bits, low_bits;
720
721 178 res_setup->type = get_bits(gb, 16);
722
723 ff_dlog(NULL, " %u. residue type %d\n", i, res_setup->type);
724
725 178 res_setup->begin = get_bits(gb, 24);
726 178 res_setup->end = get_bits(gb, 24);
727 178 res_setup->partition_size = get_bits(gb, 24) + 1;
728 /* Validations to prevent a buffer overflow later. */
729
1/2
✓ Branch 11 → 12 taken 178 times.
✗ Branch 11 → 13 not taken.
178 if (res_setup->begin>res_setup->end ||
730
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 178 times.
178 (res_setup->end-res_setup->begin) / res_setup->partition_size > FFMIN(V_MAX_PARTITIONS, 65535)) {
731 ✗ av_log(vc->avctx, AV_LOG_ERROR,
732 "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n",
733 ✗ res_setup->type, res_setup->begin, res_setup->end,
734 ✗ res_setup->partition_size, vc->blocksize[1] / 2);
735 ✗ return AVERROR_INVALIDDATA;
736 }
737
738 178 res_setup->classifications = get_bits(gb, 6) + 1;
739
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 20 taken 178 times.
178 GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
740
741 178 res_setup->ptns_to_read =
742 178 (res_setup->end - res_setup->begin) / res_setup->partition_size;
743 356 res_setup->classifs = av_malloc_array(res_setup->ptns_to_read,
744 178 vc->audio_channels *
745 sizeof(*res_setup->classifs));
746
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 178 times.
178 if (!res_setup->classifs)
747 ✗ return AVERROR(ENOMEM);
748
749 ff_dlog(NULL, " begin %"PRIu32" end %"PRIu32" part.size %u classif.s %"PRIu8" classbook %"PRIu8"\n",
750 res_setup->begin, res_setup->end, res_setup->partition_size,
751 res_setup->classifications, res_setup->classbook);
752
753
2/2
✓ Branch 29 → 24 taken 1540 times.
✓ Branch 29 → 30 taken 178 times.
1718 for (j = 0; j < res_setup->classifications; ++j) {
754 1540 high_bits = 0;
755 1540 low_bits = get_bits(gb, 3);
756
2/2
✓ Branch 26 → 27 taken 4 times.
✓ Branch 26 → 28 taken 1536 times.
1540 if (get_bits1(gb))
757 4 high_bits = get_bits(gb, 5);
758 1540 cascade[j] = (high_bits << 3) + low_bits;
759
760 ff_dlog(NULL, " %u class cascade depth: %d\n", j, ilog(cascade[j]));
761 }
762
763 178 res_setup->maxpass = 0;
764
2/2
✓ Branch 43 → 31 taken 1540 times.
✓ Branch 43 → 44 taken 178 times.
1718 for (j = 0; j < res_setup->classifications; ++j) {
765
2/2
✓ Branch 41 → 32 taken 12320 times.
✓ Branch 41 → 42 taken 1540 times.
13860 for (k = 0; k < 8; ++k) {
766
2/2
✓ Branch 32 → 33 taken 1822 times.
✓ Branch 32 → 39 taken 10498 times.
12320 if (cascade[j]&(1 << k)) {
767
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 37 taken 1822 times.
1822 GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
768
769 ff_dlog(NULL, " %u class cascade depth %u book: %d\n",
770 j, k, res_setup->books[j][k]);
771
772
2/2
✓ Branch 37 → 38 taken 142 times.
✓ Branch 37 → 40 taken 1680 times.
1822 if (k>res_setup->maxpass)
773 142 res_setup->maxpass = k;
774 } else {
775 10498 res_setup->books[j][k] = -1;
776 }
777 }
778 }
779 }
780 89 return 0;
781 }
782
783 // Process mappings part
784
785 89 static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
786 {
787 89 GetBitContext *gb = &vc->gb;
788 unsigned i, j;
789
790 89 vc->mapping_count = get_bits(gb, 6)+1;
791 89 vc->mappings = av_mallocz(vc->mapping_count * sizeof(*vc->mappings));
792
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 89 times.
89 if (!vc->mappings)
793 ✗ return AVERROR(ENOMEM);
794
795 ff_dlog(NULL, " There are %d mappings. \n", vc->mapping_count);
796
797
2/2
✓ Branch 64 → 7 taken 176 times.
✓ Branch 64 → 65 taken 89 times.
265 for (i = 0; i < vc->mapping_count; ++i) {
798 176 vorbis_mapping *mapping_setup = &vc->mappings[i];
799
800
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 176 times.
176 if (get_bits(gb, 16)) {
801 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
802 ✗ return AVERROR_INVALIDDATA;
803 }
804
2/2
✓ Branch 12 → 13 taken 4 times.
✓ Branch 12 → 15 taken 172 times.
176 if (get_bits1(gb)) {
805 4 mapping_setup->submaps = get_bits(gb, 4) + 1;
806 } else {
807 172 mapping_setup->submaps = 1;
808 }
809
810
2/2
✓ Branch 17 → 18 taken 66 times.
✓ Branch 17 → 38 taken 110 times.
176 if (get_bits1(gb)) {
811 66 mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
812
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 22 taken 66 times.
66 if (vc->audio_channels < 2) {
813 ✗ av_log(vc->avctx, AV_LOG_ERROR,
814 "Square polar channel mapping with less than two channels is not compliant with the Vorbis I specification.\n");
815 ✗ return AVERROR_INVALIDDATA;
816 }
817 66 mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps *
818 sizeof(*mapping_setup->magnitude));
819 66 mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps *
820 sizeof(*mapping_setup->angle));
821
2/4
✓ Branch 24 → 25 taken 66 times.
✗ Branch 24 → 26 not taken.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 66 times.
66 if (!mapping_setup->angle || !mapping_setup->magnitude)
822 ✗ return AVERROR(ENOMEM);
823
824
2/2
✓ Branch 37 → 28 taken 78 times.
✓ Branch 37 → 39 taken 66 times.
144 for (j = 0; j < mapping_setup->coupling_steps; ++j) {
825
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 32 taken 78 times.
78 GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
826
1/2
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 36 taken 78 times.
78 GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels)
827 }
828 } else {
829 110 mapping_setup->coupling_steps = 0;
830 }
831
832 ff_dlog(NULL, " %u mapping coupling steps: %d\n",
833 i, mapping_setup->coupling_steps);
834
835
1/2
✗ Branch 40 → 41 not taken.
✓ Branch 40 → 43 taken 176 times.
176 if (get_bits(gb, 2)) {
836 ✗ av_log(vc->avctx, AV_LOG_ERROR, "%u. mapping setup data invalid.\n", i);
837 ✗ return AVERROR_INVALIDDATA; // following spec.
838 }
839
840
2/2
✓ Branch 43 → 44 taken 4 times.
✓ Branch 43 → 51 taken 172 times.
176 if (mapping_setup->submaps>1) {
841 4 mapping_setup->mux = av_calloc(vc->audio_channels,
842 sizeof(*mapping_setup->mux));
843
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 4 times.
4 if (!mapping_setup->mux)
844 ✗ return AVERROR(ENOMEM);
845
846
2/2
✓ Branch 50 → 48 taken 24 times.
✓ Branch 50 → 51 taken 4 times.
28 for (j = 0; j < vc->audio_channels; ++j)
847 24 mapping_setup->mux[j] = get_bits(gb, 4);
848 }
849
850
2/2
✓ Branch 62 → 52 taken 180 times.
✓ Branch 62 → 63 taken 176 times.
356 for (j = 0; j < mapping_setup->submaps; ++j) {
851 180 skip_bits(gb, 8); // FIXME check?
852
1/2
✗ Branch 54 → 55 not taken.
✓ Branch 54 → 57 taken 180 times.
180 GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count)
853
1/2
✗ Branch 58 → 59 not taken.
✓ Branch 58 → 61 taken 180 times.
180 GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
854
855 ff_dlog(NULL, " %u mapping %u submap : floor %d, residue %d\n", i, j,
856 mapping_setup->submap_floor[j],
857 mapping_setup->submap_residue[j]);
858 }
859 }
860 89 return 0;
861 }
862
863 // Process modes part
864
865 54 static int create_map(vorbis_context *vc, unsigned floor_number)
866 {
867 54 vorbis_floor *floors = vc->floors;
868 vorbis_floor0 *vf;
869 int idx;
870 int blockflag, n;
871 int32_t *map;
872
873
2/2
✓ Branch 12 → 3 taken 108 times.
✓ Branch 12 → 13 taken 54 times.
162 for (blockflag = 0; blockflag < 2; ++blockflag) {
874 108 n = vc->blocksize[blockflag] / 2;
875 216 floors[floor_number].data.t0.map[blockflag] =
876 108 av_malloc_array(n + 1, sizeof(int32_t)); // n + sentinel
877
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 108 times.
108 if (!floors[floor_number].data.t0.map[blockflag])
878 ✗ return AVERROR(ENOMEM);
879
880 108 map = floors[floor_number].data.t0.map[blockflag];
881 108 vf = &floors[floor_number].data.t0;
882
883
2/2
✓ Branch 10 → 7 taken 62720 times.
✓ Branch 10 → 11 taken 108 times.
62828 for (idx = 0; idx < n; ++idx) {
884 62720 map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
885 62720 (vf->bark_map_size / BARK(vf->rate / 2.0f)));
886
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 62720 times.
62720 if (vf->bark_map_size-1 < map[idx])
887 ✗ map[idx] = vf->bark_map_size - 1;
888 }
889 108 map[n] = -1;
890 108 vf->map_size[blockflag] = n;
891 }
892
893
2/2
✓ Branch 15 → 14 taken 55350 times.
✓ Branch 15 → 16 taken 54 times.
55404 for (idx = 0; idx <= n; ++idx) {
894 ff_dlog(NULL, "floor0 map: map at pos %d is %"PRId32"\n", idx, map[idx]);
895 }
896
897 54 return 0;
898 }
899
900 89 static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
901 {
902 89 GetBitContext *gb = &vc->gb;
903 unsigned i;
904
905 89 vc->mode_count = get_bits(gb, 6) + 1;
906 89 vc->modes = av_mallocz(vc->mode_count * sizeof(*vc->modes));
907
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 89 times.
89 if (!vc->modes)
908 ✗ return AVERROR(ENOMEM);
909
910 ff_dlog(NULL, " There are %d modes.\n", vc->mode_count);
911
912
2/2
✓ Branch 18 → 7 taken 178 times.
✓ Branch 18 → 19 taken 89 times.
267 for (i = 0; i < vc->mode_count; ++i) {
913 178 vorbis_mode *mode_setup = &vc->modes[i];
914
915 178 mode_setup->blockflag = get_bits1(gb);
916 178 mode_setup->windowtype = get_bits(gb, 16);
917 178 mode_setup->transformtype = get_bits(gb, 16);
918
2/4
✓ Branch 10 → 11 taken 178 times.
✗ Branch 10 → 12 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 178 times.
178 if (mode_setup->transformtype != 0 || mode_setup->windowtype != 0) {
919 ✗ av_log(vc->avctx, AV_LOG_WARNING,
920 "Invalid mode: windowtype %u, transformtype %u (both must be 0)\n",
921 ✗ mode_setup->windowtype, mode_setup->transformtype);
922 }
923
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 17 taken 178 times.
178 GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
924
925 ff_dlog(NULL, " %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
926 i, mode_setup->blockflag, mode_setup->windowtype,
927 mode_setup->transformtype, mode_setup->mapping);
928 }
929 89 return 0;
930 }
931
932 // Process the whole setup header using the functions above
933
934 89 static int vorbis_parse_setup_hdr(vorbis_context *vc)
935 {
936 89 GetBitContext *gb = &vc->gb;
937 int ret;
938
939
3/6
✓ Branch 3 → 4 taken 89 times.
✗ Branch 3 → 14 not taken.
✓ Branch 5 → 6 taken 89 times.
✗ Branch 5 → 14 not taken.
✓ Branch 7 → 8 taken 89 times.
✗ Branch 7 → 14 not taken.
178 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
940
2/4
✓ Branch 9 → 10 taken 89 times.
✗ Branch 9 → 14 not taken.
✓ Branch 11 → 12 taken 89 times.
✗ Branch 11 → 14 not taken.
267 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
941
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 89 times.
178 (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
942 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
943 ✗ return AVERROR_INVALIDDATA;
944 }
945
946
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 20 taken 89 times.
89 if ((ret = vorbis_parse_setup_hdr_codebooks(vc))) {
947 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
948 ✗ return ret;
949 }
950
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 89 times.
89 if ((ret = vorbis_parse_setup_hdr_tdtransforms(vc))) {
951 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
952 ✗ return ret;
953 }
954
1/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 28 taken 89 times.
89 if ((ret = vorbis_parse_setup_hdr_floors(vc))) {
955 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
956 ✗ return ret;
957 }
958
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 32 taken 89 times.
89 if ((ret = vorbis_parse_setup_hdr_residues(vc))) {
959 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
960 ✗ return ret;
961 }
962
1/2
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 36 taken 89 times.
89 if ((ret = vorbis_parse_setup_hdr_mappings(vc))) {
963 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
964 ✗ return ret;
965 }
966
1/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 40 taken 89 times.
89 if ((ret = vorbis_parse_setup_hdr_modes(vc))) {
967 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
968 ✗ return ret;
969 }
970
1/2
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 44 taken 89 times.
89 if (!get_bits1(gb)) {
971 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
972 ✗ return AVERROR_INVALIDDATA; // framing flag bit unset error
973 }
974
975 89 return 0;
976 }
977
978 // Process the identification header
979
980 89 static int vorbis_parse_id_hdr(vorbis_context *vc)
981 {
982 89 GetBitContext *gb = &vc->gb;
983 unsigned bl0, bl1;
984 89 float scale = -1.0;
985 int ret;
986
987
3/6
✓ Branch 3 → 4 taken 89 times.
✗ Branch 3 → 14 not taken.
✓ Branch 5 → 6 taken 89 times.
✗ Branch 5 → 14 not taken.
✓ Branch 7 → 8 taken 89 times.
✗ Branch 7 → 14 not taken.
178 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
988
2/4
✓ Branch 9 → 10 taken 89 times.
✗ Branch 9 → 14 not taken.
✓ Branch 11 → 12 taken 89 times.
✗ Branch 11 → 14 not taken.
267 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
989
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 89 times.
178 (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
990 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
991 ✗ return AVERROR_INVALIDDATA;
992 }
993
994 89 vc->version = get_bits_long(gb, 32); //FIXME check 0
995 89 vc->audio_channels = get_bits(gb, 8);
996
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 21 taken 89 times.
89 if (vc->audio_channels <= 0) {
997 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
998 ✗ return AVERROR_INVALIDDATA;
999 }
1000 89 vc->audio_samplerate = get_bits_long(gb, 32);
1001
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 25 taken 89 times.
89 if (vc->audio_samplerate <= 0) {
1002 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
1003 ✗ return AVERROR_INVALIDDATA;
1004 }
1005 89 vc->bitrate_maximum = get_bits_long(gb, 32);
1006 89 vc->bitrate_nominal = get_bits_long(gb, 32);
1007 89 vc->bitrate_minimum = get_bits_long(gb, 32);
1008 89 bl0 = get_bits(gb, 4);
1009 89 bl1 = get_bits(gb, 4);
1010
5/10
✓ Branch 30 → 31 taken 89 times.
✗ Branch 30 → 35 not taken.
✓ Branch 31 → 32 taken 89 times.
✗ Branch 31 → 35 not taken.
✓ Branch 32 → 33 taken 89 times.
✗ Branch 32 → 35 not taken.
✓ Branch 33 → 34 taken 89 times.
✗ Branch 33 → 35 not taken.
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 37 taken 89 times.
89 if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
1011 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
1012 ✗ return AVERROR_INVALIDDATA;
1013 }
1014 89 vc->blocksize[0] = (1 << bl0);
1015 89 vc->blocksize[1] = (1 << bl1);
1016 89 vc->win[0] = ff_vorbis_vwin[bl0 - 6];
1017 89 vc->win[1] = ff_vorbis_vwin[bl1 - 6];
1018
1019
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 41 taken 89 times.
89 if ((get_bits1(gb)) == 0) {
1020 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
1021 ✗ return AVERROR_INVALIDDATA;
1022 }
1023
1024 89 vc->channel_residues = av_malloc_array(vc->blocksize[1] / 2, vc->audio_channels * sizeof(*vc->channel_residues));
1025 89 vc->saved = av_calloc(vc->blocksize[1] / 4, vc->audio_channels * sizeof(*vc->saved));
1026
2/4
✓ Branch 43 → 44 taken 89 times.
✗ Branch 43 → 45 not taken.
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 89 times.
89 if (!vc->channel_residues || !vc->saved)
1027 ✗ return AVERROR(ENOMEM);
1028
1029 89 vc->previous_window = -1;
1030
1031 89 ret = av_tx_init(&vc->mdct[0], &vc->mdct_fn[0], AV_TX_FLOAT_MDCT, 1,
1032 89 vc->blocksize[0] >> 1, &scale, 0);
1033
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 89 times.
89 if (ret < 0)
1034 ✗ return ret;
1035
1036 89 ret = av_tx_init(&vc->mdct[1], &vc->mdct_fn[1], AV_TX_FLOAT_MDCT, 1,
1037 89 vc->blocksize[1] >> 1, &scale, 0);
1038
1/2
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 52 taken 89 times.
89 if (ret < 0)
1039 ✗ return ret;
1040
1041 89 vc->fdsp = avpriv_float_dsp_alloc(vc->avctx->flags & AV_CODEC_FLAG_BITEXACT);
1042
1/2
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 55 taken 89 times.
89 if (!vc->fdsp)
1043 ✗ return AVERROR(ENOMEM);
1044
1045 ff_dlog(NULL, " vorbis version %"PRIu32" \n audio_channels %"PRIu8" \n audio_samplerate %"PRIu32" \n bitrate_max %"PRIu32" \n bitrate_nom %"PRIu32" \n bitrate_min %"PRIu32" \n blk_0 %"PRIu32" blk_1 %"PRIu32" \n ",
1046 vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
1047
1048 /*
1049 BLK = vc->blocksize[0];
1050 for (i = 0; i < BLK / 2; ++i) {
1051 vc->win[0][i] = sin(0.5*3.14159265358*(sin(((float)i + 0.5) / (float)BLK*3.14159265358))*(sin(((float)i + 0.5) / (float)BLK*3.14159265358)));
1052 }
1053 */
1054
1055 89 return 0;
1056 }
1057
1058 // Process the extradata using the functions above (identification header, setup header)
1059
1060 89 static int vorbis_parse_headers(AVCodecContext *avctx,
1061 const uint8_t *headers, int headers_len)
1062 {
1063 89 vorbis_context *vc = avctx->priv_data;
1064 const uint8_t *header_start[3];
1065 int header_len[3];
1066 89 GetBitContext *gb = &vc->gb;
1067 int hdr_type, ret;
1068
1069
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 89 times.
89 if ((ret = avpriv_split_xiph_headers(headers, headers_len, 30, header_start, header_len)) < 0) {
1070 ✗ av_log(avctx, AV_LOG_ERROR, "Extradata corrupt.\n");
1071 ✗ return ret;
1072 }
1073
1074 89 init_get_bits(gb, header_start[0], header_len[0]*8);
1075 89 hdr_type = get_bits(gb, 8);
1076
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 89 times.
89 if (hdr_type != 1) {
1077 ✗ av_log(avctx, AV_LOG_ERROR, "First header is not the id header.\n");
1078 ✗ return AVERROR_INVALIDDATA;
1079 }
1080
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 16 taken 89 times.
89 if ((ret = vorbis_parse_id_hdr(vc))) {
1081 ✗ av_log(avctx, AV_LOG_ERROR, "Id header corrupt.\n");
1082 ✗ vorbis_free(vc);
1083 ✗ return ret;
1084 }
1085
1086 89 init_get_bits(gb, header_start[2], header_len[2]*8);
1087 89 hdr_type = get_bits(gb, 8);
1088
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 22 taken 89 times.
89 if (hdr_type != 5) {
1089 ✗ av_log(avctx, AV_LOG_ERROR, "Third header is not the setup header.\n");
1090 ✗ vorbis_free(vc);
1091 ✗ return AVERROR_INVALIDDATA;
1092 }
1093
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 27 taken 89 times.
89 if ((ret = vorbis_parse_setup_hdr(vc))) {
1094 ✗ av_log(avctx, AV_LOG_ERROR, "Setup header corrupt.\n");
1095 ✗ vorbis_free(vc);
1096 ✗ return ret;
1097 }
1098
1099 89 av_channel_layout_uninit(&avctx->ch_layout);
1100
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 89 times.
89 if (vc->audio_channels > 8) {
1101 ✗ avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
1102 ✗ avctx->ch_layout.nb_channels = vc->audio_channels;
1103 } else {
1104 89 av_channel_layout_copy(&avctx->ch_layout, &ff_vorbis_ch_layouts[vc->audio_channels - 1]);
1105 }
1106
1107 89 avctx->sample_rate = vc->audio_samplerate;
1108
1109 89 return 0;
1110 }
1111
1112 87 static av_cold int vorbis_decode_init(AVCodecContext *avctx)
1113 {
1114 87 vorbis_context *vc = avctx->priv_data;
1115
1116 87 vc->avctx = avctx;
1117 87 ff_vorbisdsp_init(&vc->dsp);
1118
1119 87 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1120
1121
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 87 times.
87 if (!avctx->extradata_size) {
1122 ✗ av_log(avctx, AV_LOG_ERROR, "Extradata missing.\n");
1123 ✗ return AVERROR_INVALIDDATA;
1124 }
1125
1126 87 return vorbis_parse_headers(avctx, avctx->extradata, avctx->extradata_size);
1127 }
1128
1129 // Decode audiopackets -------------------------------------------------
1130
1131 // Read and decode floor
1132
1133 6383 static int vorbis_floor0_decode(vorbis_context *vc,
1134 vorbis_floor_data *vfu, float *vec)
1135 {
1136 6383 vorbis_floor0 *vf = &vfu->t0;
1137 6383 float *lsp = vf->lsp;
1138 unsigned book_idx;
1139 uint64_t amplitude;
1140 6383 unsigned blockflag = vc->modes[vc->mode_number].blockflag;
1141
1142
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 6383 times.
6383 if (!vf->amplitude_bits)
1143 ✗ return 1;
1144
1145 6383 amplitude = get_bits64(&vc->gb, vf->amplitude_bits);
1146
2/2
✓ Branch 5 → 6 taken 6251 times.
✓ Branch 5 → 41 taken 132 times.
6383 if (amplitude > 0) {
1147 6251 float last = 0;
1148 6251 unsigned idx, lsp_len = 0;
1149 vorbis_codebook codebook;
1150
1151 6251 book_idx = get_bits(&vc->gb, ilog(vf->num_books));
1152
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 6251 times.
6251 if (book_idx >= vf->num_books) {
1153 ✗ av_log(vc->avctx, AV_LOG_ERROR, "floor0 dec: booknumber too high!\n");
1154 ✗ book_idx = 0;
1155 }
1156 ff_dlog(NULL, "floor0 dec: booknumber: %u\n", book_idx);
1157 6251 codebook = vc->codebooks[vf->book_list[book_idx]];
1158 /* Invalid codebook! */
1159
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 6251 times.
6251 if (!codebook.codevectors)
1160 ✗ return AVERROR_INVALIDDATA;
1161
1162
2/2
✓ Branch 20 → 13 taken 50348 times.
✓ Branch 20 → 21 taken 6251 times.
56599 while (lsp_len<vf->order) {
1163 int vec_off;
1164
1165 ff_dlog(NULL, "floor0 dec: book dimension: %d\n", codebook.dimensions);
1166 ff_dlog(NULL, "floor0 dec: maximum depth: %d\n", codebook.maxdepth);
1167 /* read temp vector */
1168 50348 vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
1169 50348 codebook.nb_bits, 3);
1170
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 50348 times.
50348 if (vec_off < 0)
1171 ✗ return AVERROR_INVALIDDATA;
1172 50348 vec_off *= codebook.dimensions;
1173 ff_dlog(NULL, "floor0 dec: vector offset: %d\n", vec_off);
1174 /* copy each vector component and add last to it */
1175
2/2
✓ Branch 18 → 17 taken 145572 times.
✓ Branch 18 → 19 taken 50348 times.
195920 for (idx = 0; idx < codebook.dimensions; ++idx)
1176 145572 lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
1177 50348 last = lsp[lsp_len+idx-1]; /* set last to last vector component */
1178
1179 50348 lsp_len += codebook.dimensions;
1180 }
1181 /* DEBUG: output lsp coeffs */
1182 {
1183 int idx;
1184
2/2
✓ Branch 23 → 22 taken 145572 times.
✓ Branch 23 → 24 taken 6251 times.
151823 for (idx = 0; idx < lsp_len; ++idx)
1185 ff_dlog(NULL, "floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
1186 }
1187
1188 /* synthesize floor output vector */
1189 {
1190 int i;
1191 6251 int order = vf->order;
1192 6251 float wstep = M_PI / vf->bark_map_size;
1193
1194
2/2
✓ Branch 26 → 25 taken 145572 times.
✓ Branch 26 → 27 taken 6251 times.
151823 for (i = 0; i < order; i++)
1195 145572 lsp[i] = 2.0f * cos(lsp[i]);
1196
1197 ff_dlog(NULL, "floor0 synth: map_size = %"PRIu32"; m = %d; wstep = %f\n",
1198 vf->map_size[blockflag], order, wstep);
1199
1200 6251 i = 0;
1201
2/2
✓ Branch 38 → 28 taken 939090 times.
✓ Branch 38 → 39 taken 6251 times.
945341 while (i < vf->map_size[blockflag]) {
1202 939090 int j, iter_cond = vf->map[blockflag][i];
1203 939090 float p = 0.5f;
1204 939090 float q = 0.5f;
1205 939090 float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times
1206
1207 /* similar part for the q and p products */
1208
2/2
✓ Branch 30 → 29 taken 13168596 times.
✓ Branch 30 → 31 taken 939090 times.
14107686 for (j = 0; j + 1 < order; j += 2) {
1209 13168596 q *= lsp[j] - two_cos_w;
1210 13168596 p *= lsp[j + 1] - two_cos_w;
1211 }
1212
2/2
✓ Branch 31 → 32 taken 925416 times.
✓ Branch 31 → 33 taken 13674 times.
939090 if (j == order) { // even order
1213 925416 p *= p * (2.0f - two_cos_w);
1214 925416 q *= q * (2.0f + two_cos_w);
1215 } else { // odd order
1216 13674 q *= two_cos_w-lsp[j]; // one more time for q
1217
1218 /* final step and square */
1219 13674 p *= p * (4.f - two_cos_w * two_cos_w);
1220 13674 q *= q;
1221 }
1222
1223
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 36 taken 939090 times.
939090 if (p + q == 0.0)
1224 ✗ return AVERROR_INVALIDDATA;
1225
1226 /* calculate linear floor value */
1227 939090 q = exp((((amplitude*vf->amplitude_offset) /
1228 939090 (((1ULL << vf->amplitude_bits) - 1) * sqrt(p + q)))
1229 939090 - vf->amplitude_offset) * .11512925f);
1230
1231 /* fill vector */
1232 do {
1233 4384000 vec[i] = q; ++i;
1234
2/2
✓ Branch 37 → 37 taken 3444910 times.
✓ Branch 37 → 38 taken 939090 times.
4384000 } while (vf->map[blockflag][i] == iter_cond);
1235 }
1236 }
1237 } else {
1238 /* this channel is unused */
1239 132 return 1;
1240 }
1241
1242 ff_dlog(NULL, " Floor0 decoded\n");
1243
1244 6251 return 0;
1245 }
1246
1247 11956 static int vorbis_floor1_decode(vorbis_context *vc,
1248 vorbis_floor_data *vfu, float *vec)
1249 {
1250 11956 vorbis_floor1 *vf = &vfu->t1;
1251 11956 GetBitContext *gb = &vc->gb;
1252 11956 uint16_t range_v[4] = { 256, 128, 86, 64 };
1253 11956 unsigned range = range_v[vf->multiplier - 1];
1254 uint16_t floor1_Y[258];
1255 uint16_t floor1_Y_final[258];
1256 int floor1_flag[258];
1257 unsigned partition_class, cdim, cbits, csub, cval, offset, i, j;
1258 int book, adx, ady, dy, off, predicted, err;
1259
1260
1261
2/2
✓ Branch 3 → 4 taken 2001 times.
✓ Branch 3 → 5 taken 9955 times.
11956 if (!get_bits1(gb)) // silence
1262 2001 return 1;
1263
1264 // Read values (or differences) for the floor's points
1265
1266 9955 floor1_Y[0] = get_bits(gb, ilog(range - 1));
1267 9955 floor1_Y[1] = get_bits(gb, ilog(range - 1));
1268
1269 ff_dlog(NULL, "floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
1270
1271 9955 offset = 2;
1272
2/2
✓ Branch 21 → 8 taken 71210 times.
✓ Branch 21 → 22 taken 9955 times.
81165 for (i = 0; i < vf->partitions; ++i) {
1273 71210 partition_class = vf->partition_class[i];
1274 71210 cdim = vf->class_dimensions[partition_class];
1275 71210 cbits = vf->class_subclasses[partition_class];
1276 71210 csub = (1 << cbits) - 1;
1277 71210 cval = 0;
1278
1279 ff_dlog(NULL, "Cbits %u\n", cbits);
1280
1281
2/2
✓ Branch 8 → 9 taken 65048 times.
✓ Branch 8 → 11 taken 6162 times.
71210 if (cbits) // this reads all subclasses for this partition's class
1282 65048 cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[partition_class]].vlc.table,
1283 65048 vc->codebooks[vf->class_masterbook[partition_class]].nb_bits, 3);
1284
1285
2/2
✓ Branch 19 → 12 taken 224672 times.
✓ Branch 19 → 20 taken 71210 times.
295882 for (j = 0; j < cdim; ++j) {
1286 224672 book = vf->subclass_books[partition_class][cval & csub];
1287
1288 ff_dlog(NULL, "book %d Cbits %u cval %u bits:%d\n",
1289 book, cbits, cval, get_bits_count(gb));
1290
1291 224672 cval = cval >> cbits;
1292
2/2
✓ Branch 12 → 13 taken 119098 times.
✓ Branch 12 → 17 taken 105574 times.
224672 if (book > -1) {
1293 119098 int v = get_vlc2(gb, vc->codebooks[book].vlc.table,
1294 119098 vc->codebooks[book].nb_bits, 3);
1295
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 119098 times.
119098 if (v < 0)
1296 ✗ return AVERROR_INVALIDDATA;
1297 119098 floor1_Y[offset+j] = v;
1298 } else {
1299 105574 floor1_Y[offset+j] = 0;
1300 }
1301
1302 ff_dlog(NULL, " floor(%d) = %d \n",
1303 vf->list[offset+j].x, floor1_Y[offset+j]);
1304 }
1305 71210 offset+=cdim;
1306 }
1307
1308 // Amplitude calculation from the differences
1309
1310 9955 floor1_flag[0] = 1;
1311 9955 floor1_flag[1] = 1;
1312 9955 floor1_Y_final[0] = floor1_Y[0];
1313 9955 floor1_Y_final[1] = floor1_Y[1];
1314
1315
2/2
✓ Branch 39 → 23 taken 224672 times.
✓ Branch 39 → 40 taken 9955 times.
234627 for (i = 2; i < vf->x_list_dim; ++i) {
1316 unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
1317
1318 224672 low_neigh_offs = vf->list[i].low;
1319 224672 high_neigh_offs = vf->list[i].high;
1320 224672 dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs]; // render_point begin
1321 224672 adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
1322 224672 ady = FFABS(dy);
1323 224672 err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
1324 224672 off = err / adx;
1325
2/2
✓ Branch 23 → 24 taken 123908 times.
✓ Branch 23 → 25 taken 100764 times.
224672 if (dy < 0) {
1326 123908 predicted = floor1_Y_final[low_neigh_offs] - off;
1327 } else {
1328 100764 predicted = floor1_Y_final[low_neigh_offs] + off;
1329 } // render_point end
1330
1331 224672 val = floor1_Y[i];
1332 224672 highroom = range-predicted;
1333 224672 lowroom = predicted;
1334
2/2
✓ Branch 26 → 27 taken 91577 times.
✓ Branch 26 → 28 taken 133095 times.
224672 if (highroom < lowroom) {
1335 91577 room = highroom * 2;
1336 } else {
1337 133095 room = lowroom * 2; // SPEC misspelling
1338 }
1339
2/2
✓ Branch 29 → 30 taken 102072 times.
✓ Branch 29 → 37 taken 122600 times.
224672 if (val) {
1340 102072 floor1_flag[low_neigh_offs] = 1;
1341 102072 floor1_flag[high_neigh_offs] = 1;
1342 102072 floor1_flag[i] = 1;
1343
2/2
✓ Branch 30 → 31 taken 222 times.
✓ Branch 30 → 34 taken 101850 times.
102072 if (val >= room) {
1344
2/2
✓ Branch 31 → 32 taken 88 times.
✓ Branch 31 → 33 taken 134 times.
222 if (highroom > lowroom) {
1345 88 floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
1346 } else {
1347 134 floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
1348 }
1349 } else {
1350
2/2
✓ Branch 34 → 35 taken 66406 times.
✓ Branch 34 → 36 taken 35444 times.
101850 if (val & 1) {
1351 66406 floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
1352 } else {
1353 35444 floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
1354 }
1355 }
1356 } else {
1357 122600 floor1_flag[i] = 0;
1358 122600 floor1_Y_final[i] = av_clip_uint16(predicted);
1359 }
1360
1361 ff_dlog(NULL, " Decoded floor(%d) = %u / val %u\n",
1362 vf->list[i].x, floor1_Y_final[i], val);
1363 }
1364
1365 // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ?
1366
1367 9955 ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x);
1368
1369 ff_dlog(NULL, " Floor decoded\n");
1370
1371 9955 return 0;
1372 }
1373
1374 138467 static av_always_inline int setup_classifs(vorbis_context *vc,
1375 vorbis_residue *vr,
1376 uint8_t *do_not_decode,
1377 unsigned ch_used,
1378 int partition_count,
1379 int ptns_to_read
1380 )
1381 {
1382 138467 vorbis_codebook *codebook = vc->codebooks + vr->classbook;
1383 int p, j, i;
1384 138467 unsigned c_p_c = codebook->dimensions;
1385 138467 unsigned inverse_class = ff_inverse[vr->classifications];
1386 int temp, temp2;
1387
2/2
✓ Branch 23 → 3 taken 167728 times.
✓ Branch 23 → 24 taken 138467 times.
306195 for (p = 0, j = 0; j < ch_used; ++j) {
1388
2/2
✓ Branch 3 → 4 taken 166391 times.
✓ Branch 3 → 22 taken 1337 times.
167728 if (!do_not_decode[j]) {
1389 166391 temp = get_vlc2(&vc->gb, codebook->vlc.table,
1390 166391 codebook->nb_bits, 3);
1391
1392 ff_dlog(NULL, "Classword: %u\n", temp);
1393
1394
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 166391 times.
166391 av_assert0(temp < 65536);
1395
1396
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 166391 times.
166391 if (temp < 0) {
1397 ✗ av_log(vc->avctx, AV_LOG_ERROR,
1398 "Invalid vlc code decoding %d channel.", j);
1399 ✗ return AVERROR_INVALIDDATA;
1400 }
1401
1402
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 17 taken 166391 times.
166391 if (vr->classifications == 1) {
1403
0/2
✗ Branch 16 → 13 not taken.
✗ Branch 16 → 22 not taken.
✗ for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1404
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ if (i < ptns_to_read)
1405 ✗ vr->classifs[p + i] = 0;
1406 }
1407 } else {
1408
2/2
✓ Branch 21 → 18 taken 351899 times.
✓ Branch 21 → 22 taken 166391 times.
518290 for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1409 351899 temp2 = (((uint64_t)temp) * inverse_class) >> 32;
1410
1411
2/2
✓ Branch 18 → 19 taken 351544 times.
✓ Branch 18 → 20 taken 355 times.
351899 if (i < ptns_to_read)
1412 351544 vr->classifs[p + i] = temp - temp2 * vr->classifications;
1413 351899 temp = temp2;
1414 }
1415 }
1416 }
1417 167728 p += ptns_to_read;
1418 }
1419 138467 return 0;
1420 }
1421 // Read and decode residue
1422
1423 9754 static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc,
1424 vorbis_residue *vr,
1425 unsigned ch,
1426 uint8_t *do_not_decode,
1427 float *vec,
1428 unsigned vlen,
1429 unsigned ch_left,
1430 int vr_type)
1431 {
1432 9754 GetBitContext *gb = &vc->gb;
1433 9754 unsigned c_p_c = vc->codebooks[vr->classbook].dimensions;
1434 9754 uint8_t *classifs = vr->classifs;
1435 unsigned pass, ch_used, i, j, k, l;
1436 9754 unsigned max_output = (ch - 1) * vlen;
1437 9754 int ptns_to_read = vr->ptns_to_read;
1438 9754 int libvorbis_bug = 0;
1439
1440
2/2
✓ Branch 2 → 3 taken 4513 times.
✓ Branch 2 → 9 taken 5241 times.
9754 if (vr_type == 2) {
1441
2/2
✓ Branch 5 → 4 taken 5626 times.
✓ Branch 5 → 6 taken 4513 times.
10139 for (j = 1; j < ch; ++j)
1442 5626 do_not_decode[0] &= do_not_decode[j]; // FIXME - clobbering input
1443
2/2
✓ Branch 6 → 7 taken 120 times.
✓ Branch 6 → 8 taken 4393 times.
4513 if (do_not_decode[0])
1444 120 return 0;
1445 4393 ch_used = 1;
1446 4393 max_output += vr->end / ch;
1447 } else {
1448 5241 ch_used = ch;
1449 5241 max_output += vr->end;
1450 }
1451
1452
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 15 taken 9634 times.
9634 if (max_output > ch_left * vlen) {
1453
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ if (max_output <= ch_left * vlen + vr->partition_size*ch_used/ch) {
1454 ✗ ptns_to_read--;
1455 ✗ libvorbis_bug = 1;
1456 } else {
1457 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Insufficient output buffer\n");
1458 ✗ return AVERROR_INVALIDDATA;
1459 }
1460 }
1461
1462 ff_dlog(NULL, " residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
1463
1464
2/2
✓ Branch 111 → 16 taken 22373 times.
✓ Branch 111 → 112 taken 9634 times.
32007 for (pass = 0; pass <= vr->maxpass; ++pass) { // FIXME OPTIMIZE?
1465 int voffset, partition_count, j_times_ptns_to_read;
1466
1467 22373 voffset = vr->begin;
1468
2/2
✓ Branch 102 → 17 taken 361374 times.
✓ Branch 102 → 103 taken 22373 times.
383747 for (partition_count = 0; partition_count < ptns_to_read;) { // SPEC error
1469
2/2
✓ Branch 17 → 18 taken 138467 times.
✓ Branch 17 → 21 taken 222907 times.
361374 if (!pass) {
1470 138467 int ret = setup_classifs(vc, vr, do_not_decode, ch_used, partition_count, ptns_to_read);
1471
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 138467 times.
138467 if (ret < 0)
1472 ✗ return ret;
1473 }
1474
4/4
✓ Branch 99 → 100 taken 758981 times.
✓ Branch 99 → 101 taken 360309 times.
✓ Branch 100 → 22 taken 757916 times.
✓ Branch 100 → 101 taken 1065 times.
1119290 for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
1475
2/2
✓ Branch 97 → 23 taken 820478 times.
✓ Branch 97 → 98 taken 757916 times.
1578394 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1476 unsigned voffs;
1477
1478
2/2
✓ Branch 23 → 24 taken 816414 times.
✓ Branch 23 → 96 taken 4064 times.
820478 if (!do_not_decode[j]) {
1479 816414 unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
1480 816414 int vqbook = vr->books[vqclass][pass];
1481
1482
3/4
✓ Branch 24 → 25 taken 328637 times.
✓ Branch 24 → 96 taken 487777 times.
✓ Branch 25 → 26 taken 328637 times.
✗ Branch 25 → 96 not taken.
816414 if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
1483 int coffs;
1484 328637 unsigned dim = vc->codebooks[vqbook].dimensions;
1485 328637 unsigned step = FASTDIV(vr->partition_size << 1, dim << 1);
1486 328637 vorbis_codebook codebook = vc->codebooks[vqbook];
1487
1488
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 31 taken 328637 times.
328637 if (get_bits_left(gb) < 0) {
1489 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Overread %d bits\n", -get_bits_left(gb));
1490 ✗ return 0;
1491 }
1492
1493
2/2
✓ Branch 31 → 32 taken 115082 times.
✓ Branch 31 → 41 taken 213555 times.
328637 if (vr_type == 0) {
1494
1495 115082 voffs = voffset+j*vlen;
1496
2/2
✓ Branch 40 → 33 taken 1242824 times.
✓ Branch 40 → 94 taken 115082 times.
1357906 for (k = 0; k < step; ++k) {
1497 1242824 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1498
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 36 taken 1242824 times.
1242824 if (coffs < 0)
1499 ✗ return coffs;
1500 1242824 coffs *= dim;
1501
2/2
✓ Branch 38 → 37 taken 3535904 times.
✓ Branch 38 → 39 taken 1242824 times.
4778728 for (l = 0; l < dim; ++l)
1502 3535904 vec[voffs + k + l * step] += codebook.codevectors[coffs + l];
1503 }
1504
2/2
✓ Branch 41 → 42 taken 27795 times.
✓ Branch 41 → 51 taken 185760 times.
213555 } else if (vr_type == 1) {
1505 27795 voffs = voffset + j * vlen;
1506
2/2
✓ Branch 50 → 43 taken 287230 times.
✓ Branch 50 → 94 taken 27795 times.
315025 for (k = 0; k < step; ++k) {
1507 287230 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1508
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 287230 times.
287230 if (coffs < 0)
1509 ✗ return coffs;
1510 287230 coffs *= dim;
1511
2/2
✓ Branch 48 → 47 taken 791716 times.
✓ Branch 48 → 49 taken 287230 times.
1078946 for (l = 0; l < dim; ++l, ++voffs) {
1512 791716 vec[voffs]+=codebook.codevectors[coffs+l];
1513
1514 ff_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
1515 pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
1516 }
1517 }
1518
5/8
✓ Branch 51 → 52 taken 185760 times.
✗ Branch 51 → 79 not taken.
✓ Branch 52 → 53 taken 158035 times.
✓ Branch 52 → 79 taken 27725 times.
✓ Branch 53 → 54 taken 158035 times.
✗ Branch 53 → 79 not taken.
✓ Branch 54 → 55 taken 158035 times.
✗ Branch 54 → 79 not taken.
185760 } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) { // most frequent case optimized
1519 158035 voffs = voffset >> 1;
1520
1521
2/2
✓ Branch 55 → 56 taken 67244 times.
✓ Branch 55 → 62 taken 90791 times.
158035 if (dim == 2) {
1522
2/2
✓ Branch 61 → 57 taken 1041380 times.
✓ Branch 61 → 78 taken 67244 times.
1108624 for (k = 0; k < step; ++k) {
1523 1041380 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1524
1/2
✗ Branch 58 → 59 not taken.
✓ Branch 58 → 60 taken 1041380 times.
1041380 if (coffs < 0)
1525 ✗ return coffs;
1526 1041380 coffs *= 2;
1527 1041380 vec[voffs + k ] += codebook.codevectors[coffs ];
1528 1041380 vec[voffs + k + vlen] += codebook.codevectors[coffs + 1];
1529 }
1530
2/2
✓ Branch 62 → 63 taken 63958 times.
✓ Branch 62 → 69 taken 26833 times.
90791 } else if (dim == 4) {
1531
2/2
✓ Branch 68 → 64 taken 453178 times.
✓ Branch 68 → 78 taken 63958 times.
517136 for (k = 0; k < step; ++k, voffs += 2) {
1532 453178 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1533
1/2
✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 453178 times.
453178 if (coffs < 0)
1534 ✗ return coffs;
1535 453178 coffs *= 4;
1536 453178 vec[voffs ] += codebook.codevectors[coffs ];
1537 453178 vec[voffs + 1 ] += codebook.codevectors[coffs + 2];
1538 453178 vec[voffs + vlen ] += codebook.codevectors[coffs + 1];
1539 453178 vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3];
1540 }
1541 } else
1542
2/2
✓ Branch 77 → 70 taken 106024 times.
✓ Branch 77 → 78 taken 26833 times.
132857 for (k = 0; k < step; ++k) {
1543 106024 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1544
1/2
✗ Branch 71 → 72 not taken.
✓ Branch 71 → 73 taken 106024 times.
106024 if (coffs < 0)
1545 ✗ return coffs;
1546 106024 coffs *= dim;
1547
2/2
✓ Branch 75 → 74 taken 424096 times.
✓ Branch 75 → 76 taken 106024 times.
530120 for (l = 0; l < dim; l += 2, voffs++) {
1548 424096 vec[voffs ] += codebook.codevectors[coffs + l ];
1549 424096 vec[voffs + vlen] += codebook.codevectors[coffs + l + 1];
1550
1551 ff_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1552 pass, voffset / ch + (voffs % ch) * vlen,
1553 vec[voffset / ch + (voffs % ch) * vlen],
1554 codebook.codevectors[coffs + l], coffs, l);
1555 }
1556 }
1557
1558
1/2
✓ Branch 79 → 80 taken 27725 times.
✗ Branch 79 → 94 not taken.
27725 } else if (vr_type == 2) {
1559
1/2
✓ Branch 80 → 81 taken 27725 times.
✗ Branch 80 → 82 not taken.
27725 unsigned voffs_div = ch == 1 ? voffset : FASTDIV(voffset, ch);
1560 27725 unsigned voffs_mod = voffset - voffs_div * ch;
1561
1562
2/2
✓ Branch 93 → 84 taken 164109 times.
✓ Branch 93 → 94 taken 27725 times.
191834 for (k = 0; k < step; ++k) {
1563 164109 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1564
1/2
✗ Branch 85 → 86 not taken.
✓ Branch 85 → 87 taken 164109 times.
164109 if (coffs < 0)
1565 ✗ return coffs;
1566 164109 coffs *= dim;
1567
2/2
✓ Branch 91 → 88 taken 748485 times.
✓ Branch 91 → 92 taken 164109 times.
912594 for (l = 0; l < dim; ++l) {
1568 748485 vec[voffs_div + voffs_mod * vlen] +=
1569 748485 codebook.codevectors[coffs + l];
1570
1571 ff_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1572 pass, voffs_div + voffs_mod * vlen,
1573 vec[voffs_div + voffs_mod * vlen],
1574 codebook.codevectors[coffs + l], coffs, l);
1575
1576
2/2
✓ Branch 88 → 89 taken 149697 times.
✓ Branch 88 → 90 taken 598788 times.
748485 if (++voffs_mod == ch) {
1577 149697 voffs_div++;
1578 149697 voffs_mod = 0;
1579 }
1580 }
1581 }
1582 }
1583 }
1584 }
1585 820478 j_times_ptns_to_read += ptns_to_read;
1586 }
1587 757916 ++partition_count;
1588 757916 voffset += vr->partition_size;
1589 }
1590 }
1591
1/4
✗ Branch 103 → 104 not taken.
✓ Branch 103 → 110 taken 22373 times.
✗ Branch 104 → 105 not taken.
✗ Branch 104 → 110 not taken.
22373 if (libvorbis_bug && !pass) {
1592
0/2
✗ Branch 109 → 106 not taken.
✗ Branch 109 → 110 not taken.
✗ for (j = 0; j < ch_used; ++j) {
1593
0/2
✗ Branch 106 → 107 not taken.
✗ Branch 106 → 108 not taken.
✗ if (!do_not_decode[j]) {
1594 ✗ get_vlc2(&vc->gb, vc->codebooks[vr->classbook].vlc.table,
1595 ✗ vc->codebooks[vr->classbook].nb_bits, 3);
1596 }
1597 }
1598 }
1599 }
1600 9634 return 0;
1601 }
1602
1603 9754 static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr,
1604 unsigned ch,
1605 uint8_t *do_not_decode,
1606 float *vec, unsigned vlen,
1607 unsigned ch_left)
1608 {
1609
2/2
✓ Branch 2 → 3 taken 4513 times.
✓ Branch 2 → 5 taken 5241 times.
9754 if (vr->type == 2)
1610 4513 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2);
1611
2/2
✓ Branch 5 → 6 taken 1817 times.
✓ Branch 5 → 8 taken 3424 times.
5241 else if (vr->type == 1)
1612 1817 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1);
1613
1/2
✓ Branch 8 → 9 taken 3424 times.
✗ Branch 8 → 11 not taken.
3424 else if (vr->type == 0)
1614 3424 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 0);
1615 else {
1616 ✗ av_log(vc->avctx, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
1617 ✗ return AVERROR_INVALIDDATA;
1618 }
1619 }
1620
1621 // Decode the audio packet using the functions above
1622
1623 9383 static int vorbis_parse_audio_packet(vorbis_context *vc, float **floor_ptr)
1624 {
1625 9383 GetBitContext *gb = &vc->gb;
1626 AVTXContext *mdct;
1627 av_tx_fn mdct_fn;
1628 9383 int previous_window = vc->previous_window;
1629 unsigned mode_number, blockflag, blocksize;
1630 int i, j;
1631 uint8_t no_residue[255];
1632 uint8_t do_not_decode[255];
1633 vorbis_mapping *mapping;
1634 9383 float *ch_res_ptr = vc->channel_residues;
1635 uint8_t res_chan[255];
1636 9383 unsigned res_num = 0;
1637 9383 int retlen = 0;
1638 9383 unsigned ch_left = vc->audio_channels;
1639 unsigned vlen;
1640
1641
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 9383 times.
9383 if (get_bits1(gb)) {
1642 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
1643 ✗ return AVERROR_INVALIDDATA; // packet type not audio
1644 }
1645
1646
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 9383 times.
9383 if (vc->mode_count == 1) {
1647 ✗ mode_number = 0;
1648 } else {
1649
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 9383 times.
9383 GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
1650 }
1651 9383 vc->mode_number = mode_number;
1652 9383 mapping = &vc->mappings[vc->modes[mode_number].mapping];
1653
1654 ff_dlog(NULL, " Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
1655 vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
1656
1657 9383 blockflag = vc->modes[mode_number].blockflag;
1658 9383 blocksize = vc->blocksize[blockflag];
1659 9383 vlen = blocksize / 2;
1660
2/2
✓ Branch 12 → 13 taken 6099 times.
✓ Branch 12 → 16 taken 3284 times.
9383 if (blockflag) {
1661 6099 int code = get_bits(gb, 2);
1662
2/2
✓ Branch 14 → 15 taken 3 times.
✓ Branch 14 → 18 taken 6096 times.
6099 if (previous_window < 0)
1663 3 previous_window = code>>1;
1664
2/2
✓ Branch 16 → 17 taken 71 times.
✓ Branch 16 → 18 taken 3213 times.
3284 } else if (previous_window < 0)
1665 71 previous_window = 0;
1666
1667 9383 memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
1668
2/2
✓ Branch 20 → 19 taken 18339 times.
✓ Branch 20 → 21 taken 9383 times.
27722 for (i = 0; i < vc->audio_channels; ++i)
1669 18339 memset(floor_ptr[i], 0, vlen * sizeof(floor_ptr[0][0])); //FIXME can this be removed ?
1670
1671 // Decode floor
1672
1673
2/2
✓ Branch 30 → 22 taken 18339 times.
✓ Branch 30 → 31 taken 9383 times.
27722 for (i = 0; i < vc->audio_channels; ++i) {
1674 vorbis_floor *floor;
1675 int ret;
1676
2/2
✓ Branch 22 → 23 taken 2226 times.
✓ Branch 22 → 24 taken 16113 times.
18339 if (mapping->submaps > 1) {
1677 2226 floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
1678 } else {
1679 16113 floor = &vc->floors[mapping->submap_floor[0]];
1680 }
1681
1682 18339 ret = floor->decode(vc, &floor->data, floor_ptr[i]);
1683
1684
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 29 taken 18339 times.
18339 if (ret < 0) {
1685 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
1686 ✗ return AVERROR_INVALIDDATA;
1687 }
1688 18339 no_residue[i] = ret;
1689 }
1690
1691 // Nonzero vector propagate
1692
1693
2/2
✓ Branch 35 → 32 taken 5626 times.
✓ Branch 35 → 36 taken 9383 times.
15009 for (i = mapping->coupling_steps - 1; i >= 0; --i) {
1694
2/2
✓ Branch 32 → 33 taken 4970 times.
✓ Branch 32 → 34 taken 656 times.
5626 if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
1695 4970 no_residue[mapping->magnitude[i]] = 0;
1696 4970 no_residue[mapping->angle[i]] = 0;
1697 }
1698 }
1699
1700 // Decode residue
1701
1702
2/2
✓ Branch 54 → 37 taken 9754 times.
✓ Branch 54 → 55 taken 9383 times.
19137 for (i = 0; i < mapping->submaps; ++i) {
1703 vorbis_residue *residue;
1704 9754 unsigned ch = 0;
1705 int ret;
1706
1707
2/2
✓ Branch 45 → 38 taken 20565 times.
✓ Branch 45 → 46 taken 9754 times.
30319 for (j = 0; j < vc->audio_channels; ++j) {
1708
4/4
✓ Branch 38 → 39 taken 4452 times.
✓ Branch 38 → 40 taken 16113 times.
✓ Branch 39 → 40 taken 2226 times.
✓ Branch 39 → 44 taken 2226 times.
20565 if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
1709 18339 res_chan[j] = res_num;
1710
2/2
✓ Branch 40 → 41 taken 1234 times.
✓ Branch 40 → 42 taken 17105 times.
18339 if (no_residue[j]) {
1711 1234 do_not_decode[ch] = 1;
1712 } else {
1713 17105 do_not_decode[ch] = 0;
1714 }
1715 18339 ++ch;
1716 18339 ++res_num;
1717 }
1718 }
1719 9754 residue = &vc->residues[mapping->submap_residue[i]];
1720
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 49 taken 9754 times.
9754 if (ch_left < ch) {
1721 ✗ av_log(vc->avctx, AV_LOG_ERROR, "Too many channels in vorbis_floor_decode.\n");
1722 ✗ return AVERROR_INVALIDDATA;
1723 }
1724
1/2
✓ Branch 49 → 50 taken 9754 times.
✗ Branch 49 → 53 not taken.
9754 if (ch) {
1725 9754 ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left);
1726
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 53 taken 9754 times.
9754 if (ret < 0)
1727 ✗ return ret;
1728 }
1729
1730 9754 ch_res_ptr += ch * vlen;
1731 9754 ch_left -= ch;
1732 }
1733
1734
1/2
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 9383 times.
9383 if (ch_left > 0)
1735 ✗ return AVERROR_INVALIDDATA;
1736
1737 // Inverse coupling
1738
1739
2/2
✓ Branch 60 → 58 taken 5626 times.
✓ Branch 60 → 61 taken 9383 times.
15009 for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed
1740 float *mag, *ang;
1741
1742 5626 mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2;
1743 5626 ang = vc->channel_residues+res_chan[mapping->angle[i]] * blocksize / 2;
1744 5626 vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
1745 }
1746
1747 // Dotproduct, MDCT
1748
1749 9383 mdct = vc->mdct[blockflag];
1750 9383 mdct_fn = vc->mdct_fn[blockflag];
1751
1752
2/2
✓ Branch 67 → 62 taken 18339 times.
✓ Branch 67 → 68 taken 9383 times.
27722 for (j = vc->audio_channels-1;j >= 0; j--) {
1753 18339 ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
1754
2/2
✓ Branch 62 → 63 taken 1234 times.
✓ Branch 62 → 64 taken 17105 times.
18339 if (no_residue[j]) {
1755 1234 memset(ch_res_ptr, 0, (blocksize / 2) * sizeof(float));
1756 1234 continue;
1757 }
1758 17105 vc->fdsp->vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
1759 17105 mdct_fn(mdct, ch_res_ptr, floor_ptr[j], sizeof(float));
1760 }
1761
1762 // Overlap/add, save data for next overlapping
1763
1764 9383 retlen = (blocksize + vc->blocksize[previous_window]) / 4;
1765
2/2
✓ Branch 76 → 69 taken 18339 times.
✓ Branch 76 → 77 taken 9383 times.
27722 for (j = 0; j < vc->audio_channels; j++) {
1766 18339 unsigned bs0 = vc->blocksize[0];
1767 18339 unsigned bs1 = vc->blocksize[1];
1768 18339 float *residue = vc->channel_residues + res_chan[j] * blocksize / 2;
1769 18339 float *saved = vc->saved + j * bs1 / 4;
1770 18339 float *ret = floor_ptr[j];
1771 18339 float *buf = residue;
1772 18339 const float *win = vc->win[blockflag & previous_window];
1773
1774
2/2
✓ Branch 69 → 70 taken 16856 times.
✓ Branch 69 → 71 taken 1483 times.
18339 if (blockflag == previous_window) {
1775 16856 vc->fdsp->vector_fmul_window(ret, saved, buf, win, blocksize / 4);
1776
2/2
✓ Branch 71 → 72 taken 763 times.
✓ Branch 71 → 74 taken 720 times.
1483 } else if (blockflag > previous_window) {
1777 763 vc->fdsp->vector_fmul_window(ret, saved, buf, win, bs0 / 4);
1778 763 memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) * sizeof(float));
1779 } else {
1780 720 memcpy(ret, saved, ((bs1 - bs0) / 4) * sizeof(float));
1781 720 vc->fdsp->vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, bs0 / 4);
1782 }
1783 18339 memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
1784 }
1785
1786 9383 vc->previous_window = blockflag;
1787 9383 return retlen;
1788 }
1789
1790 // Return the decoded audio packet through the standard api
1791
1792 9383 static int vorbis_decode_frame(AVCodecContext *avctx, AVFrame *frame,
1793 int *got_frame_ptr, AVPacket *avpkt)
1794 {
1795 9383 const uint8_t *buf = avpkt->data;
1796 9383 int buf_size = avpkt->size;
1797 9383 vorbis_context *vc = avctx->priv_data;
1798 9383 GetBitContext *gb = &vc->gb;
1799 float *channel_ptrs[255];
1800 const uint8_t *new_headers;
1801 size_t new_headers_size;
1802 int i, len, ret;
1803
1804 ff_dlog(NULL, "packet length %d \n", buf_size);
1805
1806 9383 new_headers = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
1807 &new_headers_size);
1808
2/2
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 10 taken 9381 times.
9383 if (new_headers) {
1809
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 2 times.
2 if (new_headers_size > INT_MAX)
1810 ✗ return AVERROR_INVALIDDATA;
1811 2 vorbis_free(vc);
1812 2 vc->first_frame = 0;
1813
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 2 times.
2 if ((ret = vorbis_parse_headers(avctx, new_headers, new_headers_size)) < 0)
1814 ✗ return ret;
1815 }
1816
1817
1/4
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 26 taken 9383 times.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 26 not taken.
9383 if (*buf == 1 && buf_size > 7) {
1818
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ if ((ret = init_get_bits8(gb, buf + 1, buf_size - 1)) < 0)
1819 ✗ return ret;
1820
1821 ✗ vorbis_free(vc);
1822
0/2
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 21 not taken.
✗ if ((ret = vorbis_parse_id_hdr(vc))) {
1823 ✗ av_log(avctx, AV_LOG_ERROR, "Id header corrupt.\n");
1824 ✗ vorbis_free(vc);
1825 ✗ return ret;
1826 }
1827
1828 ✗ av_channel_layout_uninit(&avctx->ch_layout);
1829
0/2
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ if (vc->audio_channels > 8) {
1830 ✗ avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
1831 ✗ avctx->ch_layout.nb_channels = vc->audio_channels;
1832 } else {
1833 ✗ av_channel_layout_copy(&avctx->ch_layout, &ff_vorbis_ch_layouts[vc->audio_channels - 1]);
1834 }
1835
1836 ✗ avctx->sample_rate = vc->audio_samplerate;
1837 ✗ return buf_size;
1838 }
1839
1840
1/4
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 30 taken 9383 times.
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 30 not taken.
9383 if (*buf == 3 && buf_size > 7) {
1841 ✗ av_log(avctx, AV_LOG_DEBUG, "Ignoring comment header\n");
1842 ✗ return buf_size;
1843 }
1844
1845
1/8
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 43 taken 9383 times.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 43 not taken.
✗ Branch 32 → 33 not taken.
✗ Branch 32 → 43 not taken.
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 43 not taken.
9383 if (*buf == 5 && buf_size > 7 && vc->channel_residues && !vc->modes) {
1846
0/2
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 37 not taken.
✗ if ((ret = init_get_bits8(gb, buf + 1, buf_size - 1)) < 0)
1847 ✗ return ret;
1848
1849
0/2
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 42 not taken.
✗ if ((ret = vorbis_parse_setup_hdr(vc))) {
1850 ✗ av_log(avctx, AV_LOG_ERROR, "Setup header corrupt.\n");
1851 ✗ vorbis_free(vc);
1852 ✗ return ret;
1853 }
1854 ✗ return buf_size;
1855 }
1856
1857
2/4
✓ Branch 43 → 44 taken 9383 times.
✗ Branch 43 → 45 not taken.
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 47 taken 9383 times.
9383 if (!vc->channel_residues || !vc->modes) {
1858 ✗ av_log(avctx, AV_LOG_ERROR, "Data packet before valid headers\n");
1859 ✗ return AVERROR_INVALIDDATA;
1860 }
1861
1862 /* get output buffer */
1863 9383 frame->nb_samples = vc->blocksize[1] / 2;
1864
1/2
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 9383 times.
9383 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1865 ✗ return ret;
1866
1867
1/2
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 54 taken 9383 times.
9383 if (vc->audio_channels > 8) {
1868
0/2
✗ Branch 53 → 52 not taken.
✗ Branch 53 → 57 not taken.
✗ for (i = 0; i < vc->audio_channels; i++)
1869 ✗ channel_ptrs[i] = (float *)frame->extended_data[i];
1870 } else {
1871
2/2
✓ Branch 56 → 55 taken 18339 times.
✓ Branch 56 → 57 taken 9383 times.
27722 for (i = 0; i < vc->audio_channels; i++) {
1872 18339 int ch = ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
1873 18339 channel_ptrs[ch] = (float *)frame->extended_data[i];
1874 }
1875 }
1876
1877
1/2
✗ Branch 58 → 59 not taken.
✓ Branch 58 → 60 taken 9383 times.
9383 if ((ret = init_get_bits8(gb, buf, buf_size)) < 0)
1878 ✗ return ret;
1879
1880
1/2
✗ Branch 61 → 62 not taken.
✓ Branch 61 → 63 taken 9383 times.
9383 if ((len = vorbis_parse_audio_packet(vc, channel_ptrs)) <= 0)
1881 ✗ return len;
1882
1883
2/2
✓ Branch 63 → 64 taken 74 times.
✓ Branch 63 → 67 taken 9309 times.
9383 if (!vc->first_frame) {
1884 74 vc->first_frame = 1;
1885 74 avctx->internal->skip_samples = len;
1886
1/2
✓ Branch 64 → 65 taken 74 times.
✗ Branch 64 → 74 not taken.
74 if (!(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
1887 74 av_dict_free(&vc->pending_metadata);
1888 74 vc->pending_metadata = frame->metadata;
1889 74 frame->metadata = NULL;
1890 }
1891
2/2
✓ Branch 67 → 68 taken 16 times.
✓ Branch 67 → 74 taken 9293 times.
9309 } else if (vc->pending_metadata) {
1892
1/2
✓ Branch 68 → 69 taken 16 times.
✗ Branch 68 → 70 not taken.
16 if (!frame->metadata) {
1893 16 frame->metadata = vc->pending_metadata;
1894 16 vc->pending_metadata = NULL;
1895 } else {
1896 ✗ ret = av_dict_copy(&frame->metadata, vc->pending_metadata,
1897 AV_DICT_DONT_OVERWRITE);
1898 ✗ av_dict_free(&vc->pending_metadata);
1899
0/2
✗ Branch 72 → 73 not taken.
✗ Branch 72 → 74 not taken.
✗ if (ret < 0)
1900 ✗ return ret;
1901 }
1902 }
1903
1904 ff_dlog(NULL, "parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
1905 get_bits_count(gb) / 8, get_bits_count(gb) % 8, len);
1906
1907 9383 frame->nb_samples = len;
1908 9383 *got_frame_ptr = 1;
1909
1910 9383 return buf_size;
1911 }
1912
1913 // Close decoder
1914
1915 87 static av_cold int vorbis_decode_close(AVCodecContext *avctx)
1916 {
1917 87 vorbis_context *vc = avctx->priv_data;
1918
1919 87 vorbis_free(vc);
1920 87 av_dict_free(&vc->pending_metadata);
1921
1922 87 return 0;
1923 }
1924
1925 ✗ static av_cold void vorbis_decode_flush(AVCodecContext *avctx)
1926 {
1927 ✗ vorbis_context *vc = avctx->priv_data;
1928
1929 ✗ av_dict_free(&vc->pending_metadata);
1930
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (vc->saved) {
1931 ✗ memset(vc->saved, 0, (vc->blocksize[1] / 4) * vc->audio_channels *
1932 sizeof(*vc->saved));
1933 }
1934 ✗ vc->previous_window = -1;
1935 ✗ vc->first_frame = 0;
1936 ✗ }
1937
1938 const FFCodec ff_vorbis_decoder = {
1939 .p.name = "vorbis",
1940 CODEC_LONG_NAME("Vorbis"),
1941 .p.type = AVMEDIA_TYPE_AUDIO,
1942 .p.id = AV_CODEC_ID_VORBIS,
1943 .priv_data_size = sizeof(vorbis_context),
1944 .init = vorbis_decode_init,
1945 .close = vorbis_decode_close,
1946 FF_CODEC_DECODE_CB(vorbis_decode_frame),
1947 .flush = vorbis_decode_flush,
1948 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1949 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1950 };
1951