FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vorbisdec.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 720 914 78.8%
Branches: 405 582 69.6%

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