FFmpeg
Loading...
Searching...
No Matches
vorbisdec.c
Go to the documentation of this file.
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
54typedef struct vorbis_codebook {
55 uint8_t dimensions;
56 uint8_t lookup_type;
57 uint8_t maxdepth;
60 unsigned int nb_bits;
62
63typedef union vorbis_floor_u vorbis_floor_data;
64typedef struct vorbis_floor0_s vorbis_floor0;
65typedef struct vorbis_floor1_s vorbis_floor1;
66struct vorbis_context_s;
67typedef
69 (struct vorbis_context_s *, vorbis_floor_data *, float *);
70typedef struct vorbis_floor {
71 uint8_t floor_type;
75 uint8_t order;
76 uint16_t rate;
77 uint16_t bark_map_size;
79 uint32_t map_size[2];
82 uint8_t num_books;
83 uint8_t *book_list;
84 float *lsp;
85 } t0;
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;
96 } t1;
99
100typedef struct vorbis_residue {
101 uint16_t type;
102 uint32_t begin;
103 uint32_t end;
106 uint8_t classbook;
107 int16_t books[64][8];
108 uint8_t maxpass;
109 uint16_t ptns_to_read;
110 uint8_t *classifs;
112
113typedef struct vorbis_mapping {
114 uint8_t submaps;
116 uint8_t *magnitude;
117 uint8_t *angle;
118 uint8_t *mux;
119 uint8_t submap_floor[16];
120 uint8_t submap_residue[16];
122
123typedef struct vorbis_mode {
124 uint8_t blockflag;
125 uint16_t windowtype;
127 uint8_t mapping;
129
130typedef struct vorbis_context_s {
135
138
139 uint8_t first_frame;
140 uint32_t version;
146 uint32_t blocksize[2];
147 const float *win[2];
150 uint8_t floor_count;
156 uint8_t mode_count;
158 uint8_t mode_number; // mode number for the current packet
161 float *saved;
162 /**
163 * Tags of a dropped priming frame, added to the first frame with samples.
164 */
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
173static 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
187static float vorbisfloat2float(unsigned val)
188{
189 float mant = val & 0x1fffff;
190 int exp = (val & 0x7fe00000) >> 21;
191 if (val & 0x80000000)
192 mant = -mant;
193 return ldexpf(mant, exp - 20 - 768);
194}
195
196
197// Free all allocated memory -----------------------------------------
198
200{
201 int i;
202
204 av_freep(&vc->saved);
205 av_freep(&vc->fdsp);
206
207 if (vc->residues)
208 for (i = 0; i < vc->residue_count; i++)
210 av_freep(&vc->residues);
211 av_freep(&vc->modes);
212
213 av_tx_uninit(&vc->mdct[0]);
214 av_tx_uninit(&vc->mdct[1]);
215
216 if (vc->codebooks)
217 for (i = 0; i < vc->codebook_count; ++i) {
219 ff_vlc_free(&vc->codebooks[i].vlc);
220 }
221 av_freep(&vc->codebooks);
222
223 if (vc->floors)
224 for (i = 0; i < vc->floor_count; ++i) {
225 if (vc->floors[i].floor_type == 0) {
226 av_freep(&vc->floors[i].data.t0.map[0]);
227 av_freep(&vc->floors[i].data.t0.map[1]);
229 av_freep(&vc->floors[i].data.t0.lsp);
230 } else {
231 av_freep(&vc->floors[i].data.t1.list);
232 }
233 }
234 av_freep(&vc->floors);
235
236 if (vc->mappings)
237 for (i = 0; i < vc->mapping_count; ++i) {
239 av_freep(&vc->mappings[i].angle);
240 av_freep(&vc->mappings[i].mux);
241 }
242 av_freep(&vc->mappings);
243}
244
245// Parse setup header -------------------------------------------------
246
247// Process codebooks part
248
250{
251 unsigned cb;
252 uint8_t *tmp_vlc_bits = NULL;
253 uint32_t *tmp_vlc_codes = NULL;
254 GetBitContext *gb = &vc->gb;
255 uint16_t *codebook_multiplicands = NULL;
256 int ret = 0;
257
258 vc->codebook_count = get_bits(gb, 8) + 1;
259
260 ff_dlog(NULL, " Codebooks: %d \n", vc->codebook_count);
261
262 vc->codebooks = av_mallocz(vc->codebook_count * sizeof(*vc->codebooks));
263 tmp_vlc_bits = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_bits));
264 tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_codes));
265 codebook_multiplicands = av_malloc(V_MAX_VLCS * sizeof(*codebook_multiplicands));
266 if (!vc->codebooks ||
267 !tmp_vlc_bits || !tmp_vlc_codes || !codebook_multiplicands) {
268 ret = AVERROR(ENOMEM);
269 goto error;
270 }
271
272 for (cb = 0; cb < vc->codebook_count; ++cb) {
273 vorbis_codebook *codebook_setup = &vc->codebooks[cb];
274 unsigned ordered, t, entries, used_entries = 0;
275
276 ff_dlog(NULL, " %u. Codebook\n", cb);
277
278 if (get_bits(gb, 24) != 0x564342) {
280 " %u. Codebook setup data corrupt.\n", cb);
282 goto error;
283 }
284
285 codebook_setup->dimensions=get_bits(gb, 16);
286 if (codebook_setup->dimensions > 16 || codebook_setup->dimensions == 0) {
288 " %u. Codebook's dimension is invalid (%d).\n",
289 cb, codebook_setup->dimensions);
291 goto error;
292 }
293 entries = get_bits(gb, 24);
294 if (entries > V_MAX_VLCS) {
296 " %u. Codebook has too many entries (%u).\n",
297 cb, entries);
299 goto error;
300 }
301
302 ordered = get_bits1(gb);
303
304 ff_dlog(NULL, " codebook_dimensions %d, codebook_entries %u\n",
305 codebook_setup->dimensions, entries);
306
307 if (!ordered) {
308 unsigned ce, flag;
309 unsigned sparse = get_bits1(gb);
310
311 ff_dlog(NULL, " not ordered \n");
312
313 if (sparse) {
314 ff_dlog(NULL, " sparse \n");
315
316 used_entries = 0;
317 for (ce = 0; ce < entries; ++ce) {
318 flag = get_bits1(gb);
319 if (flag) {
320 tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
321 ++used_entries;
322 } else
323 tmp_vlc_bits[ce] = 0;
324 }
325 } else {
326 ff_dlog(NULL, " not sparse \n");
327
328 used_entries = entries;
329 for (ce = 0; ce < entries; ++ce)
330 tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
331 }
332 } else {
333 unsigned current_entry = 0;
334 unsigned current_length = get_bits(gb, 5) + 1;
335
336 ff_dlog(NULL, " ordered, current length: %u\n", current_length); //FIXME
337
338 used_entries = entries;
339 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 number = get_bits(gb, ilog(entries - current_entry));
345
346 ff_dlog(NULL, " number: %u\n", number);
347
348 for (i = current_entry; i < number+current_entry; ++i)
349 if (i < used_entries)
350 tmp_vlc_bits[i] = current_length;
351
352 current_entry+=number;
353 }
354 if (current_entry>used_entries) {
355 av_log(vc->avctx, AV_LOG_ERROR, " More codelengths than codes in codebook. \n");
357 goto error;
358 }
359 }
360
361 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 if (codebook_setup->lookup_type == 1) {
369 unsigned i, j, k;
370 unsigned codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions);
371
372 float codebook_minimum_value = vorbisfloat2float(get_bits_long(gb, 32));
373 float codebook_delta_value = vorbisfloat2float(get_bits_long(gb, 32));
374 unsigned codebook_value_bits = get_bits(gb, 4) + 1;
375 unsigned codebook_sequence_p = get_bits1(gb);
376
377 if (!isfinite(codebook_minimum_value) || !isfinite(codebook_delta_value)) {
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 for (i = 0; i < codebook_lookup_values; ++i) {
387 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 if (used_entries) {
396 codebook_setup->codevectors =
397 av_calloc(used_entries, codebook_setup->dimensions *
398 sizeof(*codebook_setup->codevectors));
399 if (!codebook_setup->codevectors) {
400 ret = AVERROR(ENOMEM);
401 goto error;
402 }
403 } else
404 codebook_setup->codevectors = NULL;
405
406 for (j = 0, i = 0; i < entries; ++i) {
407 unsigned dim = codebook_setup->dimensions;
408
409 if (tmp_vlc_bits[i]) {
410 float last = 0.0;
411 unsigned lookup_offset = i;
412
413 ff_dlog(vc->avctx, "Lookup offset %u ,", i);
414
415 for (k = 0; k < dim; ++k) {
416 unsigned multiplicand_offset = lookup_offset % codebook_lookup_values;
417 codebook_setup->codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;
418 if (codebook_sequence_p)
419 last = codebook_setup->codevectors[j * dim + k];
420 lookup_offset/=codebook_lookup_values;
421 }
422 tmp_vlc_bits[j] = tmp_vlc_bits[i];
423
424 ff_dlog(vc->avctx, "real lookup offset %u, vector: ", j);
425 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 ++j;
431 }
432 }
433 if (j != used_entries) {
434 av_log(vc->avctx, AV_LOG_ERROR, "Bug in codevector vector building code. \n");
436 goto error;
437 }
438 entries = used_entries;
439 } else if (codebook_setup->lookup_type >= 2) {
440 av_log(vc->avctx, AV_LOG_ERROR, "Codebook lookup type not supported. \n");
442 goto error;
443 }
444
445// Initialize VLC table
446 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");
449 goto error;
450 }
451 codebook_setup->maxdepth = 0;
452 for (t = 0; t < entries; ++t)
453 if (tmp_vlc_bits[t] >= codebook_setup->maxdepth)
454 codebook_setup->maxdepth = tmp_vlc_bits[t];
455
456 if (codebook_setup->maxdepth > 3 * V_NB_BITS)
457 codebook_setup->nb_bits = V_NB_BITS2;
458 else
459 codebook_setup->nb_bits = V_NB_BITS;
460
461 codebook_setup->maxdepth = (codebook_setup->maxdepth+codebook_setup->nb_bits - 1) / codebook_setup->nb_bits;
462
463 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 av_free(tmp_vlc_bits);
474 av_free(tmp_vlc_codes);
475 av_free(codebook_multiplicands);
476 return 0;
477
478// Error:
479error:
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
489{
490 GetBitContext *gb = &vc->gb;
491 unsigned i, vorbis_time_count = get_bits(gb, 6) + 1;
492
493 for (i = 0; i < vorbis_time_count; ++i) {
494 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 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 return 0;
505}
506
507// Process floors part
508
510 vorbis_floor_data *vfu, float *vec);
511static int create_map(vorbis_context *vc, unsigned floor_number);
513 vorbis_floor_data *vfu, float *vec);
515{
516 GetBitContext *gb = &vc->gb;
517 int i, j, k, ret;
518
519 vc->floor_count = get_bits(gb, 6) + 1;
520
521 vc->floors = av_mallocz(vc->floor_count * sizeof(*vc->floors));
522 if (!vc->floors)
523 return AVERROR(ENOMEM);
524
525 for (i = 0; i < vc->floor_count; ++i) {
526 vorbis_floor *floor_setup = &vc->floors[i];
527
528 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 if (floor_setup->floor_type == 1) {
533 int maximum_class = -1;
534 unsigned rangebits, rangemax, floor1_values = 2;
535
536 floor_setup->decode = vorbis_floor1_decode;
537
538 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 for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
544 floor_setup->data.t1.partition_class[j] = get_bits(gb, 4);
545 if (floor_setup->data.t1.partition_class[j] > maximum_class)
546 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 for (j = 0; j <= maximum_class; ++j) {
556 floor_setup->data.t1.class_dimensions[j] = get_bits(gb, 3) + 1;
557 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 if (floor_setup->data.t1.class_subclasses[j]) {
565
566 ff_dlog(NULL, " masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]);
567 }
568
569 for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) {
570 int16_t bits = get_bits(gb, 8) - 1;
571 if (bits != -1)
573 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 floor_setup->data.t1.multiplier = get_bits(gb, 2) + 1;
580 floor_setup->data.t1.x_list_dim = 2;
581
582 for (j = 0; j < floor_setup->data.t1.partitions; ++j)
583 floor_setup->data.t1.x_list_dim+=floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]];
584
585 floor_setup->data.t1.list = av_calloc(floor_setup->data.t1.x_list_dim,
586 sizeof(*floor_setup->data.t1.list));
587 if (!floor_setup->data.t1.list)
588 return AVERROR(ENOMEM);
589
590 rangebits = get_bits(gb, 4);
591 if (!rangebits && floor_setup->data.t1.partitions) {
593 "A rangebits value of 0 is not compliant with the Vorbis I specification.\n");
594 return AVERROR_INVALIDDATA;
595 }
596 rangemax = (1 << rangebits);
597 if (rangemax > vc->blocksize[1] / 2) {
599 "Floor value is too large for blocksize: %u (%"PRIu32")\n",
600 rangemax, vc->blocksize[1] / 2);
601 return AVERROR_INVALIDDATA;
602 }
603 floor_setup->data.t1.list[0].x = 0;
604 floor_setup->data.t1.list[1].x = rangemax;
605
606 for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
607 for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) {
608 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
617 floor_setup->data.t1.list,
618 floor_setup->data.t1.x_list_dim)) {
619 return AVERROR_INVALIDDATA;
620 }
621 } else if (floor_setup->floor_type == 0) {
622 unsigned max_codebook_dim = 0;
623
624 floor_setup->decode = vorbis_floor0_decode;
625
626 floor_setup->data.t0.order = get_bits(gb, 8);
627 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 floor_setup->data.t0.rate = get_bits(gb, 16);
632 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 floor_setup->data.t0.bark_map_size = get_bits(gb, 16);
637 if (!floor_setup->data.t0.bark_map_size) {
639 "Floor 0 bark map size is 0.\n");
640 return AVERROR_INVALIDDATA;
641 }
642 floor_setup->data.t0.amplitude_bits = get_bits(gb, 6);
643 floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
644 floor_setup->data.t0.num_books = get_bits(gb, 4) + 1;
645
646 /* allocate mem for booklist */
647 floor_setup->data.t0.book_list =
648 av_malloc(floor_setup->data.t0.num_books);
649 if (!floor_setup->data.t0.book_list)
650 return AVERROR(ENOMEM);
651 /* read book indexes */
652 {
653 int idx;
654 unsigned book_idx;
655 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
656 GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count)
657 floor_setup->data.t0.book_list[idx] = book_idx;
658 if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
659 max_codebook_dim = vc->codebooks[book_idx].dimensions;
660 }
661 }
662
663 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 floor_setup->data.t0.lsp =
669 av_malloc_array((floor_setup->data.t0.order + 1 + max_codebook_dim),
670 sizeof(*floor_setup->data.t0.lsp));
671 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 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 return 0;
700}
701
702// Process residues part
703
705{
706 GetBitContext *gb = &vc->gb;
707 unsigned i, j, k;
708
709 vc->residue_count = get_bits(gb, 6)+1;
710 vc->residues = av_mallocz(vc->residue_count * sizeof(*vc->residues));
711 if (!vc->residues)
712 return AVERROR(ENOMEM);
713
714 ff_dlog(NULL, " There are %d residues. \n", vc->residue_count);
715
716 for (i = 0; i < vc->residue_count; ++i) {
717 vorbis_residue *res_setup = &vc->residues[i];
718 uint8_t cascade[64];
719 unsigned high_bits, low_bits;
720
721 res_setup->type = get_bits(gb, 16);
722
723 ff_dlog(NULL, " %u. residue type %d\n", i, res_setup->type);
724
725 res_setup->begin = get_bits(gb, 24);
726 res_setup->end = get_bits(gb, 24);
727 res_setup->partition_size = get_bits(gb, 24) + 1;
728 /* Validations to prevent a buffer overflow later. */
729 if (res_setup->begin>res_setup->end ||
730 (res_setup->end-res_setup->begin) / res_setup->partition_size > FFMIN(V_MAX_PARTITIONS, 65535)) {
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 res_setup->classifications = get_bits(gb, 6) + 1;
740
741 res_setup->ptns_to_read =
742 (res_setup->end - res_setup->begin) / res_setup->partition_size;
743 res_setup->classifs = av_malloc_array(res_setup->ptns_to_read,
744 vc->audio_channels *
745 sizeof(*res_setup->classifs));
746 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 for (j = 0; j < res_setup->classifications; ++j) {
754 high_bits = 0;
755 low_bits = get_bits(gb, 3);
756 if (get_bits1(gb))
757 high_bits = get_bits(gb, 5);
758 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 res_setup->maxpass = 0;
764 for (j = 0; j < res_setup->classifications; ++j) {
765 for (k = 0; k < 8; ++k) {
766 if (cascade[j]&(1 << k)) {
767 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 if (k>res_setup->maxpass)
773 res_setup->maxpass = k;
774 } else {
775 res_setup->books[j][k] = -1;
776 }
777 }
778 }
779 }
780 return 0;
781}
782
783// Process mappings part
784
786{
787 GetBitContext *gb = &vc->gb;
788 unsigned i, j;
789
790 vc->mapping_count = get_bits(gb, 6)+1;
791 vc->mappings = av_mallocz(vc->mapping_count * sizeof(*vc->mappings));
792 if (!vc->mappings)
793 return AVERROR(ENOMEM);
794
795 ff_dlog(NULL, " There are %d mappings. \n", vc->mapping_count);
796
797 for (i = 0; i < vc->mapping_count; ++i) {
798 vorbis_mapping *mapping_setup = &vc->mappings[i];
799
800 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 if (get_bits1(gb)) {
805 mapping_setup->submaps = get_bits(gb, 4) + 1;
806 } else {
807 mapping_setup->submaps = 1;
808 }
809
810 if (get_bits1(gb)) {
811 mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
812 if (vc->audio_channels < 2) {
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 mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps *
818 sizeof(*mapping_setup->magnitude));
819 mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps *
820 sizeof(*mapping_setup->angle));
821 if (!mapping_setup->angle || !mapping_setup->magnitude)
822 return AVERROR(ENOMEM);
823
824 for (j = 0; j < mapping_setup->coupling_steps; ++j) {
825 GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
826 GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels)
827 }
828 } else {
829 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 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 if (mapping_setup->submaps>1) {
841 mapping_setup->mux = av_calloc(vc->audio_channels,
842 sizeof(*mapping_setup->mux));
843 if (!mapping_setup->mux)
844 return AVERROR(ENOMEM);
845
846 for (j = 0; j < vc->audio_channels; ++j)
847 mapping_setup->mux[j] = get_bits(gb, 4);
848 }
849
850 for (j = 0; j < mapping_setup->submaps; ++j) {
851 skip_bits(gb, 8); // FIXME check?
852 GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count)
853 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 return 0;
861}
862
863// Process modes part
864
865static int create_map(vorbis_context *vc, unsigned floor_number)
866{
867 vorbis_floor *floors = vc->floors;
869 int idx;
870 int blockflag, n;
871 int32_t *map;
872
873 for (blockflag = 0; blockflag < 2; ++blockflag) {
874 n = vc->blocksize[blockflag] / 2;
875 floors[floor_number].data.t0.map[blockflag] =
876 av_malloc_array(n + 1, sizeof(int32_t)); // n + sentinel
877 if (!floors[floor_number].data.t0.map[blockflag])
878 return AVERROR(ENOMEM);
879
880 map = floors[floor_number].data.t0.map[blockflag];
881 vf = &floors[floor_number].data.t0;
882
883 for (idx = 0; idx < n; ++idx) {
884 map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
885 (vf->bark_map_size / BARK(vf->rate / 2.0f)));
886 if (vf->bark_map_size-1 < map[idx])
887 map[idx] = vf->bark_map_size - 1;
888 }
889 map[n] = -1;
890 vf->map_size[blockflag] = n;
891 }
892
893 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 return 0;
898}
899
901{
902 GetBitContext *gb = &vc->gb;
903 unsigned i;
904
905 vc->mode_count = get_bits(gb, 6) + 1;
906 vc->modes = av_mallocz(vc->mode_count * sizeof(*vc->modes));
907 if (!vc->modes)
908 return AVERROR(ENOMEM);
909
910 ff_dlog(NULL, " There are %d modes.\n", vc->mode_count);
911
912 for (i = 0; i < vc->mode_count; ++i) {
913 vorbis_mode *mode_setup = &vc->modes[i];
914
915 mode_setup->blockflag = get_bits1(gb);
916 mode_setup->windowtype = get_bits(gb, 16);
917 mode_setup->transformtype = get_bits(gb, 16);
918 if (mode_setup->transformtype != 0 || mode_setup->windowtype != 0) {
920 "Invalid mode: windowtype %u, transformtype %u (both must be 0)\n",
921 mode_setup->windowtype, mode_setup->transformtype);
922 }
923 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 return 0;
930}
931
932// Process the whole setup header using the functions above
933
935{
936 GetBitContext *gb = &vc->gb;
937 int ret;
938
939 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
940 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
941 (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
947 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
948 return ret;
949 }
951 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
952 return ret;
953 }
955 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
956 return ret;
957 }
959 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
960 return ret;
961 }
963 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
964 return ret;
965 }
966 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 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 return 0;
976}
977
978// Process the identification header
979
981{
982 GetBitContext *gb = &vc->gb;
983 unsigned bl0, bl1;
984 float scale = -1.0;
985 int ret;
986
987 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
988 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
989 (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 vc->version = get_bits_long(gb, 32); //FIXME check 0
995 vc->audio_channels = get_bits(gb, 8);
996 if (vc->audio_channels <= 0) {
997 av_log(vc->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
998 return AVERROR_INVALIDDATA;
999 }
1000 vc->audio_samplerate = get_bits_long(gb, 32);
1001 if (vc->audio_samplerate <= 0) {
1002 av_log(vc->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
1003 return AVERROR_INVALIDDATA;
1004 }
1005 vc->bitrate_maximum = get_bits_long(gb, 32);
1006 vc->bitrate_nominal = get_bits_long(gb, 32);
1007 vc->bitrate_minimum = get_bits_long(gb, 32);
1008 bl0 = get_bits(gb, 4);
1009 bl1 = get_bits(gb, 4);
1010 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 vc->blocksize[0] = (1 << bl0);
1015 vc->blocksize[1] = (1 << bl1);
1016 vc->win[0] = ff_vorbis_vwin[bl0 - 6];
1017 vc->win[1] = ff_vorbis_vwin[bl1 - 6];
1018
1019 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 vc->channel_residues = av_malloc_array(vc->blocksize[1] / 2, vc->audio_channels * sizeof(*vc->channel_residues));
1025 vc->saved = av_calloc(vc->blocksize[1] / 4, vc->audio_channels * sizeof(*vc->saved));
1026 if (!vc->channel_residues || !vc->saved)
1027 return AVERROR(ENOMEM);
1028
1029 vc->previous_window = -1;
1030
1031 ret = av_tx_init(&vc->mdct[0], &vc->mdct_fn[0], AV_TX_FLOAT_MDCT, 1,
1032 vc->blocksize[0] >> 1, &scale, 0);
1033 if (ret < 0)
1034 return ret;
1035
1036 ret = av_tx_init(&vc->mdct[1], &vc->mdct_fn[1], AV_TX_FLOAT_MDCT, 1,
1037 vc->blocksize[1] >> 1, &scale, 0);
1038 if (ret < 0)
1039 return ret;
1040
1042 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 ",
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 return 0;
1056}
1057
1058// Process the extradata using the functions above (identification header, setup header)
1059
1061 const uint8_t *headers, int headers_len)
1062{
1063 vorbis_context *vc = avctx->priv_data;
1064 const uint8_t *header_start[3];
1065 int header_len[3];
1066 GetBitContext *gb = &vc->gb;
1067 int hdr_type, ret;
1068
1069 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 init_get_bits(gb, header_start[0], header_len[0]*8);
1075 hdr_type = get_bits(gb, 8);
1076 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 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 init_get_bits(gb, header_start[2], header_len[2]*8);
1087 hdr_type = get_bits(gb, 8);
1088 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 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
1100 if (vc->audio_channels > 8) {
1103 } else {
1105 }
1106
1107 avctx->sample_rate = vc->audio_samplerate;
1108
1109 return 0;
1110}
1111
1113{
1114 vorbis_context *vc = avctx->priv_data;
1115
1116 vc->avctx = avctx;
1117 ff_vorbisdsp_init(&vc->dsp);
1118
1120
1121 if (!avctx->extradata_size) {
1122 av_log(avctx, AV_LOG_ERROR, "Extradata missing.\n");
1123 return AVERROR_INVALIDDATA;
1124 }
1125
1126 return vorbis_parse_headers(avctx, avctx->extradata, avctx->extradata_size);
1127}
1128
1129// Decode audiopackets -------------------------------------------------
1130
1131// Read and decode floor
1132
1134 vorbis_floor_data *vfu, float *vec)
1135{
1136 vorbis_floor0 *vf = &vfu->t0;
1137 float *lsp = vf->lsp;
1138 unsigned book_idx;
1139 uint64_t amplitude;
1140 unsigned blockflag = vc->modes[vc->mode_number].blockflag;
1141
1142 if (!vf->amplitude_bits)
1143 return 1;
1144
1145 amplitude = get_bits64(&vc->gb, vf->amplitude_bits);
1146 if (amplitude > 0) {
1147 float last = 0;
1148 unsigned idx, lsp_len = 0;
1150
1151 book_idx = get_bits(&vc->gb, ilog(vf->num_books));
1152 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 codebook = vc->codebooks[vf->book_list[book_idx]];
1158 /* Invalid codebook! */
1159 if (!codebook.codevectors)
1160 return AVERROR_INVALIDDATA;
1161
1162 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 vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
1169 codebook.nb_bits, 3);
1170 if (vec_off < 0)
1171 return AVERROR_INVALIDDATA;
1172 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 for (idx = 0; idx < codebook.dimensions; ++idx)
1176 lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
1177 last = lsp[lsp_len+idx-1]; /* set last to last vector component */
1178
1179 lsp_len += codebook.dimensions;
1180 }
1181 /* DEBUG: output lsp coeffs */
1182 {
1183 int idx;
1184 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 int order = vf->order;
1192 float wstep = M_PI / vf->bark_map_size;
1193
1194 for (i = 0; i < order; i++)
1195 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 i = 0;
1201 while (i < vf->map_size[blockflag]) {
1202 int j, iter_cond = vf->map[blockflag][i];
1203 float p = 0.5f;
1204 float q = 0.5f;
1205 float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times
1206
1207 /* similar part for the q and p products */
1208 for (j = 0; j + 1 < order; j += 2) {
1209 q *= lsp[j] - two_cos_w;
1210 p *= lsp[j + 1] - two_cos_w;
1211 }
1212 if (j == order) { // even order
1213 p *= p * (2.0f - two_cos_w);
1214 q *= q * (2.0f + two_cos_w);
1215 } else { // odd order
1216 q *= two_cos_w-lsp[j]; // one more time for q
1217
1218 /* final step and square */
1219 p *= p * (4.f - two_cos_w * two_cos_w);
1220 q *= q;
1221 }
1222
1223 if (p + q == 0.0)
1224 return AVERROR_INVALIDDATA;
1225
1226 /* calculate linear floor value */
1227 q = exp((((amplitude*vf->amplitude_offset) /
1228 (((1ULL << vf->amplitude_bits) - 1) * sqrt(p + q)))
1229 - vf->amplitude_offset) * .11512925f);
1230
1231 /* fill vector */
1232 do {
1233 vec[i] = q; ++i;
1234 } while (vf->map[blockflag][i] == iter_cond);
1235 }
1236 }
1237 } else {
1238 /* this channel is unused */
1239 return 1;
1240 }
1241
1242 ff_dlog(NULL, " Floor0 decoded\n");
1243
1244 return 0;
1245}
1246
1248 vorbis_floor_data *vfu, float *vec)
1249{
1250 vorbis_floor1 *vf = &vfu->t1;
1251 GetBitContext *gb = &vc->gb;
1252 uint16_t range_v[4] = { 256, 128, 86, 64 };
1253 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 if (!get_bits1(gb)) // silence
1262 return 1;
1263
1264// Read values (or differences) for the floor's points
1265
1266 floor1_Y[0] = get_bits(gb, ilog(range - 1));
1267 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 offset = 2;
1272 for (i = 0; i < vf->partitions; ++i) {
1273 partition_class = vf->partition_class[i];
1274 cdim = vf->class_dimensions[partition_class];
1275 cbits = vf->class_subclasses[partition_class];
1276 csub = (1 << cbits) - 1;
1277 cval = 0;
1278
1279 ff_dlog(NULL, "Cbits %u\n", cbits);
1280
1281 if (cbits) // this reads all subclasses for this partition's class
1282 cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[partition_class]].vlc.table,
1283 vc->codebooks[vf->class_masterbook[partition_class]].nb_bits, 3);
1284
1285 for (j = 0; j < cdim; ++j) {
1286 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 cval = cval >> cbits;
1292 if (book > -1) {
1293 int v = get_vlc2(gb, vc->codebooks[book].vlc.table,
1294 vc->codebooks[book].nb_bits, 3);
1295 if (v < 0)
1296 return AVERROR_INVALIDDATA;
1297 floor1_Y[offset+j] = v;
1298 } else {
1299 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 offset+=cdim;
1306 }
1307
1308// Amplitude calculation from the differences
1309
1310 floor1_flag[0] = 1;
1311 floor1_flag[1] = 1;
1312 floor1_Y_final[0] = floor1_Y[0];
1313 floor1_Y_final[1] = floor1_Y[1];
1314
1315 for (i = 2; i < vf->x_list_dim; ++i) {
1316 unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
1317
1318 low_neigh_offs = vf->list[i].low;
1319 high_neigh_offs = vf->list[i].high;
1320 dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs]; // render_point begin
1321 adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
1322 ady = FFABS(dy);
1323 err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
1324 off = err / adx;
1325 if (dy < 0) {
1326 predicted = floor1_Y_final[low_neigh_offs] - off;
1327 } else {
1328 predicted = floor1_Y_final[low_neigh_offs] + off;
1329 } // render_point end
1330
1331 val = floor1_Y[i];
1332 highroom = range-predicted;
1333 lowroom = predicted;
1334 if (highroom < lowroom) {
1335 room = highroom * 2;
1336 } else {
1337 room = lowroom * 2; // SPEC misspelling
1338 }
1339 if (val) {
1340 floor1_flag[low_neigh_offs] = 1;
1341 floor1_flag[high_neigh_offs] = 1;
1342 floor1_flag[i] = 1;
1343 if (val >= room) {
1344 if (highroom > lowroom) {
1345 floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
1346 } else {
1347 floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
1348 }
1349 } else {
1350 if (val & 1) {
1351 floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
1352 } else {
1353 floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
1354 }
1355 }
1356 } else {
1357 floor1_flag[i] = 0;
1358 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 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 return 0;
1372}
1373
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{
1383 int p, j, i;
1384 unsigned c_p_c = codebook->dimensions;
1385 unsigned inverse_class = ff_inverse[vr->classifications];
1386 int temp, temp2;
1387 for (p = 0, j = 0; j < ch_used; ++j) {
1388 if (!do_not_decode[j]) {
1389 temp = get_vlc2(&vc->gb, codebook->vlc.table,
1390 codebook->nb_bits, 3);
1391
1392 ff_dlog(NULL, "Classword: %u\n", temp);
1393
1394 av_assert0(temp < 65536);
1395
1396 if (temp < 0) {
1398 "Invalid vlc code decoding %d channel.", j);
1399 return AVERROR_INVALIDDATA;
1400 }
1401
1402 if (vr->classifications == 1) {
1403 for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1404 if (i < ptns_to_read)
1405 vr->classifs[p + i] = 0;
1406 }
1407 } else {
1408 for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1409 temp2 = (((uint64_t)temp) * inverse_class) >> 32;
1410
1411 if (i < ptns_to_read)
1412 vr->classifs[p + i] = temp - temp2 * vr->classifications;
1413 temp = temp2;
1414 }
1415 }
1416 }
1417 p += ptns_to_read;
1418 }
1419 return 0;
1420}
1421// Read and decode residue
1422
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 GetBitContext *gb = &vc->gb;
1433 unsigned c_p_c = vc->codebooks[vr->classbook].dimensions;
1434 uint8_t *classifs = vr->classifs;
1435 unsigned pass, ch_used, i, j, k, l;
1436 unsigned max_output = (ch - 1) * vlen;
1437 int ptns_to_read = vr->ptns_to_read;
1438 int libvorbis_bug = 0;
1439
1440 if (vr_type == 2) {
1441 for (j = 1; j < ch; ++j)
1442 do_not_decode[0] &= do_not_decode[j]; // FIXME - clobbering input
1443 if (do_not_decode[0])
1444 return 0;
1445 ch_used = 1;
1446 max_output += vr->end / ch;
1447 } else {
1448 ch_used = ch;
1449 max_output += vr->end;
1450 }
1451
1452 if (max_output > ch_left * vlen) {
1453 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 for (pass = 0; pass <= vr->maxpass; ++pass) { // FIXME OPTIMIZE?
1465 int voffset, partition_count, j_times_ptns_to_read;
1466
1467 voffset = vr->begin;
1468 for (partition_count = 0; partition_count < ptns_to_read;) { // SPEC error
1469 if (!pass) {
1470 int ret = setup_classifs(vc, vr, do_not_decode, ch_used, partition_count, ptns_to_read);
1471 if (ret < 0)
1472 return ret;
1473 }
1474 for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
1475 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1476 unsigned voffs;
1477
1478 if (!do_not_decode[j]) {
1479 unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
1480 int vqbook = vr->books[vqclass][pass];
1481
1482 if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
1483 int coffs;
1484 unsigned dim = vc->codebooks[vqbook].dimensions;
1485 unsigned step = FASTDIV(vr->partition_size << 1, dim << 1);
1486 vorbis_codebook codebook = vc->codebooks[vqbook];
1487
1488 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 if (vr_type == 0) {
1494
1495 voffs = voffset+j*vlen;
1496 for (k = 0; k < step; ++k) {
1497 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1498 if (coffs < 0)
1499 return coffs;
1500 coffs *= dim;
1501 for (l = 0; l < dim; ++l)
1502 vec[voffs + k + l * step] += codebook.codevectors[coffs + l];
1503 }
1504 } else if (vr_type == 1) {
1505 voffs = voffset + j * vlen;
1506 for (k = 0; k < step; ++k) {
1507 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1508 if (coffs < 0)
1509 return coffs;
1510 coffs *= dim;
1511 for (l = 0; l < dim; ++l, ++voffs) {
1512 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 } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) { // most frequent case optimized
1519 voffs = voffset >> 1;
1520
1521 if (dim == 2) {
1522 for (k = 0; k < step; ++k) {
1523 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1524 if (coffs < 0)
1525 return coffs;
1526 coffs *= 2;
1527 vec[voffs + k ] += codebook.codevectors[coffs ];
1528 vec[voffs + k + vlen] += codebook.codevectors[coffs + 1];
1529 }
1530 } else if (dim == 4) {
1531 for (k = 0; k < step; ++k, voffs += 2) {
1532 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1533 if (coffs < 0)
1534 return coffs;
1535 coffs *= 4;
1536 vec[voffs ] += codebook.codevectors[coffs ];
1537 vec[voffs + 1 ] += codebook.codevectors[coffs + 2];
1538 vec[voffs + vlen ] += codebook.codevectors[coffs + 1];
1539 vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3];
1540 }
1541 } else
1542 for (k = 0; k < step; ++k) {
1543 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1544 if (coffs < 0)
1545 return coffs;
1546 coffs *= dim;
1547 for (l = 0; l < dim; l += 2, voffs++) {
1548 vec[voffs ] += codebook.codevectors[coffs + l ];
1549 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 } else if (vr_type == 2) {
1559 unsigned voffs_div = ch == 1 ? voffset : FASTDIV(voffset, ch);
1560 unsigned voffs_mod = voffset - voffs_div * ch;
1561
1562 for (k = 0; k < step; ++k) {
1563 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3);
1564 if (coffs < 0)
1565 return coffs;
1566 coffs *= dim;
1567 for (l = 0; l < dim; ++l) {
1568 vec[voffs_div + voffs_mod * vlen] +=
1569 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 if (++voffs_mod == ch) {
1577 voffs_div++;
1578 voffs_mod = 0;
1579 }
1580 }
1581 }
1582 }
1583 }
1584 }
1585 j_times_ptns_to_read += ptns_to_read;
1586 }
1587 ++partition_count;
1588 voffset += vr->partition_size;
1589 }
1590 }
1591 if (libvorbis_bug && !pass) {
1592 for (j = 0; j < ch_used; ++j) {
1593 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 return 0;
1601}
1602
1604 unsigned ch,
1605 uint8_t *do_not_decode,
1606 float *vec, unsigned vlen,
1607 unsigned ch_left)
1608{
1609 if (vr->type == 2)
1610 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2);
1611 else if (vr->type == 1)
1612 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1);
1613 else if (vr->type == 0)
1614 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
1623static int vorbis_parse_audio_packet(vorbis_context *vc, float **floor_ptr)
1624{
1625 GetBitContext *gb = &vc->gb;
1626 AVTXContext *mdct;
1627 av_tx_fn mdct_fn;
1628 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 float *ch_res_ptr = vc->channel_residues;
1635 uint8_t res_chan[255];
1636 unsigned res_num = 0;
1637 int retlen = 0;
1638 unsigned ch_left = vc->audio_channels;
1639 unsigned vlen;
1640
1641 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 if (vc->mode_count == 1) {
1647 mode_number = 0;
1648 } else {
1649 GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
1650 }
1651 vc->mode_number = mode_number;
1652 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 blockflag = vc->modes[mode_number].blockflag;
1658 blocksize = vc->blocksize[blockflag];
1659 vlen = blocksize / 2;
1660 if (blockflag) {
1661 int code = get_bits(gb, 2);
1662 if (previous_window < 0)
1663 previous_window = code>>1;
1664 } else if (previous_window < 0)
1665 previous_window = 0;
1666
1667 memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
1668 for (i = 0; i < vc->audio_channels; ++i)
1669 memset(floor_ptr[i], 0, vlen * sizeof(floor_ptr[0][0])); //FIXME can this be removed ?
1670
1671// Decode floor
1672
1673 for (i = 0; i < vc->audio_channels; ++i) {
1675 int ret;
1676 if (mapping->submaps > 1) {
1677 floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
1678 } else {
1679 floor = &vc->floors[mapping->submap_floor[0]];
1680 }
1681
1682 ret = floor->decode(vc, &floor->data, floor_ptr[i]);
1683
1684 if (ret < 0) {
1685 av_log(vc->avctx, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
1686 return AVERROR_INVALIDDATA;
1687 }
1688 no_residue[i] = ret;
1689 }
1690
1691// Nonzero vector propagate
1692
1693 for (i = mapping->coupling_steps - 1; i >= 0; --i) {
1694 if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
1695 no_residue[mapping->magnitude[i]] = 0;
1696 no_residue[mapping->angle[i]] = 0;
1697 }
1698 }
1699
1700// Decode residue
1701
1702 for (i = 0; i < mapping->submaps; ++i) {
1703 vorbis_residue *residue;
1704 unsigned ch = 0;
1705 int ret;
1706
1707 for (j = 0; j < vc->audio_channels; ++j) {
1708 if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
1709 res_chan[j] = res_num;
1710 if (no_residue[j]) {
1711 do_not_decode[ch] = 1;
1712 } else {
1713 do_not_decode[ch] = 0;
1714 }
1715 ++ch;
1716 ++res_num;
1717 }
1718 }
1719 residue = &vc->residues[mapping->submap_residue[i]];
1720 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 if (ch) {
1725 ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left);
1726 if (ret < 0)
1727 return ret;
1728 }
1729
1730 ch_res_ptr += ch * vlen;
1731 ch_left -= ch;
1732 }
1733
1734 if (ch_left > 0)
1735 return AVERROR_INVALIDDATA;
1736
1737// Inverse coupling
1738
1739 for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed
1740 float *mag, *ang;
1741
1742 mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2;
1743 ang = vc->channel_residues+res_chan[mapping->angle[i]] * blocksize / 2;
1744 vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
1745 }
1746
1747// Dotproduct, MDCT
1748
1749 mdct = vc->mdct[blockflag];
1750 mdct_fn = vc->mdct_fn[blockflag];
1751
1752 for (j = vc->audio_channels-1;j >= 0; j--) {
1753 ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
1754 if (no_residue[j]) {
1755 memset(ch_res_ptr, 0, (blocksize / 2) * sizeof(float));
1756 continue;
1757 }
1758 vc->fdsp->vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
1759 mdct_fn(mdct, ch_res_ptr, floor_ptr[j], sizeof(float));
1760 }
1761
1762// Overlap/add, save data for next overlapping
1763
1764 retlen = (blocksize + vc->blocksize[previous_window]) / 4;
1765 for (j = 0; j < vc->audio_channels; j++) {
1766 unsigned bs0 = vc->blocksize[0];
1767 unsigned bs1 = vc->blocksize[1];
1768 float *residue = vc->channel_residues + res_chan[j] * blocksize / 2;
1769 float *saved = vc->saved + j * bs1 / 4;
1770 float *ret = floor_ptr[j];
1771 float *buf = residue;
1772 const float *win = vc->win[blockflag & previous_window];
1773
1774 if (blockflag == previous_window) {
1775 vc->fdsp->vector_fmul_window(ret, saved, buf, win, blocksize / 4);
1776 } else if (blockflag > previous_window) {
1777 vc->fdsp->vector_fmul_window(ret, saved, buf, win, bs0 / 4);
1778 memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) * sizeof(float));
1779 } else {
1780 memcpy(ret, saved, ((bs1 - bs0) / 4) * sizeof(float));
1781 vc->fdsp->vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, bs0 / 4);
1782 }
1783 memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
1784 }
1785
1786 vc->previous_window = blockflag;
1787 return retlen;
1788}
1789
1790// Return the decoded audio packet through the standard api
1791
1793 int *got_frame_ptr, AVPacket *avpkt)
1794{
1795 const uint8_t *buf = avpkt->data;
1796 int buf_size = avpkt->size;
1797 vorbis_context *vc = avctx->priv_data;
1798 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
1807 &new_headers_size);
1808 if (new_headers) {
1809 if (new_headers_size > INT_MAX)
1810 return AVERROR_INVALIDDATA;
1811 vorbis_free(vc);
1812 vc->first_frame = 0;
1813 if ((ret = vorbis_parse_headers(avctx, new_headers, new_headers_size)) < 0)
1814 return ret;
1815 }
1816
1817 if (*buf == 1 && buf_size > 7) {
1818 if ((ret = init_get_bits8(gb, buf + 1, buf_size - 1)) < 0)
1819 return ret;
1820
1821 vorbis_free(vc);
1822 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
1829 if (vc->audio_channels > 8) {
1832 } else {
1834 }
1835
1836 avctx->sample_rate = vc->audio_samplerate;
1837 return buf_size;
1838 }
1839
1840 if (*buf == 3 && buf_size > 7) {
1841 av_log(avctx, AV_LOG_DEBUG, "Ignoring comment header\n");
1842 return buf_size;
1843 }
1844
1845 if (*buf == 5 && buf_size > 7 && vc->channel_residues && !vc->modes) {
1846 if ((ret = init_get_bits8(gb, buf + 1, buf_size - 1)) < 0)
1847 return ret;
1848
1849 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 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 frame->nb_samples = vc->blocksize[1] / 2;
1864 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1865 return ret;
1866
1867 if (vc->audio_channels > 8) {
1868 for (i = 0; i < vc->audio_channels; i++)
1869 channel_ptrs[i] = (float *)frame->extended_data[i];
1870 } else {
1871 for (i = 0; i < vc->audio_channels; i++) {
1873 channel_ptrs[ch] = (float *)frame->extended_data[i];
1874 }
1875 }
1876
1877 if ((ret = init_get_bits8(gb, buf, buf_size)) < 0)
1878 return ret;
1879
1880 if ((len = vorbis_parse_audio_packet(vc, channel_ptrs)) <= 0)
1881 return len;
1882
1883 if (!vc->first_frame) {
1884 vc->first_frame = 1;
1885 avctx->internal->skip_samples = len;
1886 if (!(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
1888 vc->pending_metadata = frame->metadata;
1889 frame->metadata = NULL;
1890 }
1891 } else if (vc->pending_metadata) {
1892 if (!frame->metadata) {
1893 frame->metadata = vc->pending_metadata;
1894 vc->pending_metadata = NULL;
1895 } else {
1896 ret = av_dict_copy(&frame->metadata, vc->pending_metadata,
1899 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 frame->nb_samples = len;
1908 *got_frame_ptr = 1;
1909
1910 return buf_size;
1911}
1912
1913// Close decoder
1914
1916{
1917 vorbis_context *vc = avctx->priv_data;
1918
1919 vorbis_free(vc);
1921
1922 return 0;
1923}
1924
1926{
1927 vorbis_context *vc = avctx->priv_data;
1928
1930 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
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),
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};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int const int8_t const int8_t * vf
Definition dsp.h:272
static double val(void *priv, double ch)
Definition aeval.c:77
static float win(SuperEqualizerContext *s, float n, int N)
const FFCodec ff_vorbis_decoder
Definition vorbisdec.c:1938
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define flag(name)
Definition cbs_h264.c:60
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static const unsigned codebook[256][2]
Definition cfhdenc.c:41
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_clip_uint16
Definition common.h:112
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
static __device__ float floor(float a)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1814
static AVFrame * frame
Public dictionary API.
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int8_t exp
Definition eval.c:76
static const uint8_t bits[8]
Definition fastaudio.c:100
int ret
Definition ffplay.c:391
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
Definition get_bits.h:456
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
#define AV_CODEC_FLAG2_SKIP_MANUAL
Do not skip samples and export skip information as frame side data.
Definition avcodec.h:372
@ AV_CODEC_ID_VORBIS
Definition codec_id.h:464
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition dict.c:250
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition dict.h:81
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
const VDPAUPixFmtMap * map
unsigned offset
Definition libaomenc.c:812
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
common internal api header.
unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n)
Definition vorbis.c:41
void ff_vorbis_floor1_render_list(vorbis_floor1_entry *list, int values, uint16_t *y_list, int *flag, int multiplier, float *out, int samples)
Definition vorbis.c:199
int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, unsigned num)
Definition vorbis.c:59
int ff_vorbis_ready_floor1_list(void *logctx, vorbis_floor1_entry *list, int values)
Definition vorbis.c:109
av_cold void ff_vorbisdsp_init(VorbisDSPContext *dsp)
Definition vorbisdsp.c:46
#define av_always_inline
Definition attributes.h:72
#define av_cold
Definition attributes.h:117
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition float_dsp.c:135
#define FFMIN(a, b)
Definition macros.h:49
#define M_PI
Definition mathematics.h:67
#define FASTDIV(a, b)
Definition mathops.h:216
const uint32_t ff_inverse[257]
Definition mathtables.c:66
enum AVColorRange range
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
const char data[16]
Definition mxf.c:155
#define av_malloc(s)
Definition ops_static.c:52
const uint8_t * code
Definition spdifenc.c:433
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1059
int flags2
AV_CODEC_FLAG2_*.
Definition avcodec.h:507
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1051
int sample_rate
samples per second
Definition avcodec.h:1044
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition internal.h:125
void(* vector_fmul_window)(float *dst, const float *src0, const float *src1, const float *win, int len)
Overlap/add with window function.
Definition float_dsp.h:119
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats.
Definition float_dsp.h:38
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
Definition vlc.h:50
VLCElem * table
Definition vlc.h:52
void(* vorbis_inverse_coupling)(float *mag, float *ang, ptrdiff_t blocksize)
Definition vorbisdsp.h:26
float * codevectors
Definition vorbisdec.c:59
uint8_t lookup_type
Definition vorbisdec.c:56
unsigned int nb_bits
Definition vorbisdec.c:60
uint8_t dimensions
Definition vorbisdec.c:55
uint8_t maxdepth
Definition vorbisdec.c:57
uint32_t blocksize[2]
Definition vorbisdec.c:146
uint8_t mode_count
Definition vorbisdec.c:156
uint8_t audio_channels
Definition vorbisdec.c:141
vorbis_mode * modes
Definition vorbisdec.c:157
const float * win[2]
Definition vorbisdec.c:147
uint8_t mapping_count
Definition vorbisdec.c:154
uint8_t mode_number
Definition vorbisdec.c:158
uint32_t bitrate_minimum
Definition vorbisdec.c:145
uint32_t version
Definition vorbisdec.c:140
uint8_t first_frame
Definition vorbisdec.c:139
AVCodecContext * avctx
Definition vorbisdec.c:131
uint8_t residue_count
Definition vorbisdec.c:152
AVTXContext * mdct[2]
Definition vorbisdec.c:136
GetBitContext gb
Definition vorbisdec.c:132
vorbis_mapping * mappings
Definition vorbisdec.c:155
float * channel_residues
Definition vorbisdec.c:160
uint16_t codebook_count
Definition vorbisdec.c:148
vorbis_floor * floors
Definition vorbisdec.c:151
uint8_t floor_count
Definition vorbisdec.c:150
vorbis_residue * residues
Definition vorbisdec.c:153
uint32_t bitrate_maximum
Definition vorbisdec.c:143
VorbisDSPContext dsp
Definition vorbisdec.c:133
av_tx_fn mdct_fn[2]
Definition vorbisdec.c:137
AVDictionary * pending_metadata
Tags of a dropped priming frame, added to the first frame with samples.
Definition vorbisdec.c:165
int8_t previous_window
Definition vorbisdec.c:159
vorbis_codebook * codebooks
Definition vorbisdec.c:149
AVFloatDSPContext * fdsp
Definition vorbisdec.c:134
float * saved
Definition vorbisdec.c:161
uint32_t bitrate_nominal
Definition vorbisdec.c:144
uint32_t audio_samplerate
Definition vorbisdec.c:142
Definition vorbis.h:26
uint16_t x
Definition vorbis.h:27
vorbis_floor_decode_func decode
Definition vorbisdec.c:72
union vorbis_floor::vorbis_floor_u data
uint8_t floor_type
Definition vorbisdec.c:71
uint8_t submap_floor[16]
Definition vorbisdec.c:119
uint16_t coupling_steps
Definition vorbisdec.c:115
uint8_t * angle
Definition vorbisdec.c:117
uint8_t submaps
Definition vorbisdec.c:114
uint8_t * magnitude
Definition vorbisdec.c:116
uint8_t * mux
Definition vorbisdec.c:118
uint8_t submap_residue[16]
Definition vorbisdec.c:120
uint16_t windowtype
Definition vorbisdec.c:125
uint8_t mapping
Definition vorbisdec.c:127
uint8_t blockflag
Definition vorbisdec.c:124
uint16_t transformtype
Definition vorbisdec.c:126
uint16_t ptns_to_read
Definition vorbisdec.c:109
int16_t books[64][8]
Definition vorbisdec.c:107
uint16_t type
Definition vorbisdec.c:101
unsigned partition_size
Definition vorbisdec.c:104
uint32_t end
Definition vorbisdec.c:103
uint32_t begin
Definition vorbisdec.c:102
uint8_t classifications
Definition vorbisdec.c:105
uint8_t * classifs
Definition vorbisdec.c:110
uint8_t maxpass
Definition vorbisdec.c:108
uint8_t classbook
Definition vorbisdec.c:106
#define av_free(p)
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
Definition tx.h:68
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition tx.h:151
struct vorbis_floor::vorbis_floor_u::vorbis_floor1_s t1
struct vorbis_floor::vorbis_floor_u::vorbis_floor0_s t0
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
else temp
Definition vf_mcdeint.c:275
void ff_vlc_free(VLC *vlc)
Definition vlc.c:580
#define VLC_INIT_LE
Definition vlc.h:197
#define vlc_init(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition vlc.h:70
#define ilog(i)
Definition vorbis.h:41
const AVChannelLayout ff_vorbis_ch_layouts[9]
Definition vorbis_data.c:37
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition vorbis_data.c:26
const float *const ff_vorbis_vwin[8]
int dim
int len
#define V_NB_BITS
Definition vorbisdec.c:49
int(* vorbis_floor_decode_func)(struct vorbis_context_s *, vorbis_floor_data *, float *)
Definition vorbisdec.c:69
#define GET_VALIDATED_INDEX(idx, bits, limit)
Definition vorbisdec.c:181
#define V_MAX_VLCS
Definition vorbisdec.c:51
static av_cold int vorbis_decode_init(AVCodecContext *avctx)
Definition vorbisdec.c:1112
static float vorbisfloat2float(unsigned val)
Definition vorbisdec.c:187
struct vorbis_floor0_s vorbis_floor0
Definition vorbisdec.c:64
static av_cold void vorbis_decode_flush(AVCodecContext *avctx)
Definition vorbisdec.c:1925
#define BARK(x)
Definition vorbisdec.c:170
#define V_NB_BITS2
Definition vorbisdec.c:50
static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
Definition vorbisdec.c:704
static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc)
Definition vorbisdec.c:249
static int vorbis_parse_audio_packet(vorbis_context *vc, float **floor_ptr)
Definition vorbisdec.c:1623
union vorbis_floor_u vorbis_floor_data
Definition vorbisdec.c:63
static int vorbis_parse_headers(AVCodecContext *avctx, const uint8_t *headers, int headers_len)
Definition vorbisdec.c:1060
static int vorbis_parse_id_hdr(vorbis_context *vc)
Definition vorbisdec.c:980
static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
Definition vorbisdec.c:785
static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc, vorbis_residue *vr, unsigned ch, uint8_t *do_not_decode, float *vec, unsigned vlen, unsigned ch_left, int vr_type)
Definition vorbisdec.c:1423
static int vorbis_floor1_decode(vorbis_context *vc, vorbis_floor_data *vfu, float *vec)
Definition vorbisdec.c:1247
struct vorbis_floor1_s vorbis_floor1
Definition vorbisdec.c:65
#define VALIDATE_INDEX(idx, limit)
Definition vorbisdec.c:174
#define V_MAX_PARTITIONS
Definition vorbisdec.c:52
static int vorbis_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition vorbisdec.c:1792
static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc)
Definition vorbisdec.c:488
static int vorbis_floor0_decode(vorbis_context *vc, vorbis_floor_data *vfu, float *vec)
Definition vorbisdec.c:1133
static int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr, unsigned ch, uint8_t *do_not_decode, float *vec, unsigned vlen, unsigned ch_left)
Definition vorbisdec.c:1603
static void vorbis_free(vorbis_context *vc)
Definition vorbisdec.c:199
static const char idx_err_str[]
Definition vorbisdec.c:173
static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
Definition vorbisdec.c:900
static av_cold int vorbis_decode_close(AVCodecContext *avctx)
Definition vorbisdec.c:1915
static int vorbis_parse_setup_hdr_floors(vorbis_context *vc)
Definition vorbisdec.c:514
static av_always_inline int setup_classifs(vorbis_context *vc, vorbis_residue *vr, uint8_t *do_not_decode, unsigned ch_used, int partition_count, int ptns_to_read)
Definition vorbisdec.c:1374
static int vorbis_parse_setup_hdr(vorbis_context *vc)
Definition vorbisdec.c:934
static int create_map(vorbis_context *vc, unsigned floor_number)
Definition vorbisdec.c:865
int avpriv_split_xiph_headers(const uint8_t *extradata, int extradata_size, int first_header_size, const uint8_t *header_start[3], int header_len[3])
Split a single extradata buffer into the three headers that most Xiph codecs use.
Definition xiph.c:26