2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * @author Denes Balatoni ( dbalatoni programozo hu )
28 #define BITSTREAM_READER_LE
29 #include "libavutil/float_dsp.h"
33 #include "fmtconvert.h"
37 #include "vorbisdsp.h"
42 #define V_MAX_VLCS (1 << 16)
43 #define V_MAX_PARTITIONS (1 << 20)
54 typedef union vorbis_floor_u vorbis_floor_data;
55 typedef struct vorbis_floor0_s vorbis_floor0;
56 typedef struct vorbis_floor1_s vorbis_floor1;
57 struct vorbis_context_s;
59 int (* vorbis_floor_decode_func)
60 (struct vorbis_context_s *, vorbis_floor_data *, float *);
63 vorbis_floor_decode_func decode;
64 union vorbis_floor_u {
65 struct vorbis_floor0_s {
68 uint16_t bark_map_size;
71 uint8_t amplitude_bits;
72 uint8_t amplitude_offset;
77 struct vorbis_floor1_s {
79 uint8_t partition_class[32];
80 uint8_t class_dimensions[16];
81 uint8_t class_subclasses[16];
82 uint8_t class_masterbook[16];
83 int16_t subclass_books[16][8];
86 vorbis_floor1_entry *list;
95 unsigned partition_size;
96 uint8_t classifications;
100 uint16_t ptns_to_read;
106 uint16_t coupling_steps;
110 uint8_t submap_floor[16];
111 uint8_t submap_residue[16];
117 uint16_t transformtype;
121 typedef struct vorbis_context_s {
122 AVCodecContext *avctx;
124 VorbisDSPContext dsp;
125 AVFloatDSPContext fdsp;
126 FmtConvertContext fmt_conv;
131 uint8_t audio_channels;
132 uint32_t audio_samplerate;
133 uint32_t bitrate_maximum;
134 uint32_t bitrate_nominal;
135 uint32_t bitrate_minimum;
136 uint32_t blocksize[2];
138 uint16_t codebook_count;
139 vorbis_codebook *codebooks;
141 vorbis_floor *floors;
142 uint8_t residue_count;
143 vorbis_residue *residues;
144 uint8_t mapping_count;
145 vorbis_mapping *mappings;
148 uint8_t mode_number; // mode number for the current packet
149 uint8_t previous_window;
150 float *channel_residues;
154 /* Helper functions */
157 (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x))
159 static const char idx_err_str[] = "Index value %d out of range (0 - %d) for %s at %s:%i\n";
160 #define VALIDATE_INDEX(idx, limit) \
162 av_log(vc->avctx, AV_LOG_ERROR,\
164 (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\
165 return AVERROR_INVALIDDATA;\
167 #define GET_VALIDATED_INDEX(idx, bits, limit) \
169 idx = get_bits(gb, bits);\
170 VALIDATE_INDEX(idx, limit)\
173 static float vorbisfloat2float(unsigned val)
175 double mant = val & 0x1fffff;
176 long exp = (val & 0x7fe00000L) >> 21;
177 if (val & 0x80000000)
179 return ldexp(mant, exp - 20 - 768);
183 // Free all allocated memory -----------------------------------------
185 static void vorbis_free(vorbis_context *vc)
189 av_freep(&vc->channel_residues);
190 av_freep(&vc->saved);
192 for (i = 0; i < vc->residue_count; i++)
193 av_free(vc->residues[i].classifs);
194 av_freep(&vc->residues);
195 av_freep(&vc->modes);
197 ff_mdct_end(&vc->mdct[0]);
198 ff_mdct_end(&vc->mdct[1]);
200 for (i = 0; i < vc->codebook_count; ++i) {
201 av_free(vc->codebooks[i].codevectors);
202 ff_free_vlc(&vc->codebooks[i].vlc);
204 av_freep(&vc->codebooks);
206 for (i = 0; i < vc->floor_count; ++i) {
207 if (vc->floors[i].floor_type == 0) {
208 av_free(vc->floors[i].data.t0.map[0]);
209 av_free(vc->floors[i].data.t0.map[1]);
210 av_free(vc->floors[i].data.t0.book_list);
211 av_free(vc->floors[i].data.t0.lsp);
213 av_free(vc->floors[i].data.t1.list);
216 av_freep(&vc->floors);
218 for (i = 0; i < vc->mapping_count; ++i) {
219 av_free(vc->mappings[i].magnitude);
220 av_free(vc->mappings[i].angle);
221 av_free(vc->mappings[i].mux);
223 av_freep(&vc->mappings);
226 // Parse setup header -------------------------------------------------
228 // Process codebooks part
230 static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc)
233 uint8_t *tmp_vlc_bits = NULL;
234 uint32_t *tmp_vlc_codes = NULL;
235 GetBitContext *gb = &vc->gb;
236 uint16_t *codebook_multiplicands = NULL;
239 vc->codebook_count = get_bits(gb, 8) + 1;
241 av_dlog(NULL, " Codebooks: %d \n", vc->codebook_count);
243 vc->codebooks = av_mallocz(vc->codebook_count * sizeof(*vc->codebooks));
244 tmp_vlc_bits = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_bits));
245 tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_codes));
246 codebook_multiplicands = av_malloc(V_MAX_VLCS * sizeof(*codebook_multiplicands));
247 if (!vc->codebooks ||
248 !tmp_vlc_bits || !tmp_vlc_codes || !codebook_multiplicands) {
249 ret = AVERROR(ENOMEM);
253 for (cb = 0; cb < vc->codebook_count; ++cb) {
254 vorbis_codebook *codebook_setup = &vc->codebooks[cb];
255 unsigned ordered, t, entries, used_entries = 0;
257 av_dlog(NULL, " %u. Codebook\n", cb);
259 if (get_bits(gb, 24) != 0x564342) {
260 av_log(vc->avctx, AV_LOG_ERROR,
261 " %u. Codebook setup data corrupt.\n", cb);
262 ret = AVERROR_INVALIDDATA;
266 codebook_setup->dimensions=get_bits(gb, 16);
267 if (codebook_setup->dimensions > 16 || codebook_setup->dimensions == 0) {
268 av_log(vc->avctx, AV_LOG_ERROR,
269 " %u. Codebook's dimension is invalid (%d).\n",
270 cb, codebook_setup->dimensions);
271 ret = AVERROR_INVALIDDATA;
274 entries = get_bits(gb, 24);
275 if (entries > V_MAX_VLCS) {
276 av_log(vc->avctx, AV_LOG_ERROR,
277 " %u. Codebook has too many entries (%u).\n",
279 ret = AVERROR_INVALIDDATA;
283 ordered = get_bits1(gb);
285 av_dlog(NULL, " codebook_dimensions %d, codebook_entries %u\n",
286 codebook_setup->dimensions, entries);
290 unsigned sparse = get_bits1(gb);
292 av_dlog(NULL, " not ordered \n");
295 av_dlog(NULL, " sparse \n");
298 for (ce = 0; ce < entries; ++ce) {
299 flag = get_bits1(gb);
301 tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
304 tmp_vlc_bits[ce] = 0;
307 av_dlog(NULL, " not sparse \n");
309 used_entries = entries;
310 for (ce = 0; ce < entries; ++ce)
311 tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
314 unsigned current_entry = 0;
315 unsigned current_length = get_bits(gb, 5) + 1;
317 av_dlog(NULL, " ordered, current length: %u\n", current_length); //FIXME
319 used_entries = entries;
320 for (; current_entry < used_entries && current_length <= 32; ++current_length) {
323 av_dlog(NULL, " number bits: %u ", ilog(entries - current_entry));
325 number = get_bits(gb, ilog(entries - current_entry));
327 av_dlog(NULL, " number: %u\n", number);
329 for (i = current_entry; i < number+current_entry; ++i)
330 if (i < used_entries)
331 tmp_vlc_bits[i] = current_length;
333 current_entry+=number;
335 if (current_entry>used_entries) {
336 av_log(vc->avctx, AV_LOG_ERROR, " More codelengths than codes in codebook. \n");
337 ret = AVERROR_INVALIDDATA;
342 codebook_setup->lookup_type = get_bits(gb, 4);
344 av_dlog(NULL, " lookup type: %d : %s \n", codebook_setup->lookup_type,
345 codebook_setup->lookup_type ? "vq" : "no lookup");
347 // If the codebook is used for (inverse) VQ, calculate codevectors.
349 if (codebook_setup->lookup_type == 1) {
351 unsigned codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions);
353 float codebook_minimum_value = vorbisfloat2float(get_bits_long(gb, 32));
354 float codebook_delta_value = vorbisfloat2float(get_bits_long(gb, 32));
355 unsigned codebook_value_bits = get_bits(gb, 4) + 1;
356 unsigned codebook_sequence_p = get_bits1(gb);
358 av_dlog(NULL, " We expect %d numbers for building the codevectors. \n",
359 codebook_lookup_values);
360 av_dlog(NULL, " delta %f minmum %f \n",
361 codebook_delta_value, codebook_minimum_value);
363 for (i = 0; i < codebook_lookup_values; ++i) {
364 codebook_multiplicands[i] = get_bits(gb, codebook_value_bits);
366 av_dlog(NULL, " multiplicands*delta+minmum : %e \n",
367 (float)codebook_multiplicands[i] * codebook_delta_value + codebook_minimum_value);
368 av_dlog(NULL, " multiplicand %u\n", codebook_multiplicands[i]);
371 // Weed out unused vlcs and build codevector vector
372 codebook_setup->codevectors = used_entries ? av_mallocz(used_entries *
373 codebook_setup->dimensions *
374 sizeof(*codebook_setup->codevectors))
376 for (j = 0, i = 0; i < entries; ++i) {
377 unsigned dim = codebook_setup->dimensions;
379 if (tmp_vlc_bits[i]) {
381 unsigned lookup_offset = i;
383 av_dlog(vc->avctx, "Lookup offset %u ,", i);
385 for (k = 0; k < dim; ++k) {
386 unsigned multiplicand_offset = lookup_offset % codebook_lookup_values;
387 codebook_setup->codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;
388 if (codebook_sequence_p)
389 last = codebook_setup->codevectors[j * dim + k];
390 lookup_offset/=codebook_lookup_values;
392 tmp_vlc_bits[j] = tmp_vlc_bits[i];
394 av_dlog(vc->avctx, "real lookup offset %u, vector: ", j);
395 for (k = 0; k < dim; ++k)
396 av_dlog(vc->avctx, " %f ",
397 codebook_setup->codevectors[j * dim + k]);
398 av_dlog(vc->avctx, "\n");
403 if (j != used_entries) {
404 av_log(vc->avctx, AV_LOG_ERROR, "Bug in codevector vector building code. \n");
405 ret = AVERROR_INVALIDDATA;
408 entries = used_entries;
409 } else if (codebook_setup->lookup_type >= 2) {
410 av_log(vc->avctx, AV_LOG_ERROR, "Codebook lookup type not supported. \n");
411 ret = AVERROR_INVALIDDATA;
415 // Initialize VLC table
416 if (ff_vorbis_len2vlc(tmp_vlc_bits, tmp_vlc_codes, entries)) {
417 av_log(vc->avctx, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n");
418 ret = AVERROR_INVALIDDATA;
421 codebook_setup->maxdepth = 0;
422 for (t = 0; t < entries; ++t)
423 if (tmp_vlc_bits[t] >= codebook_setup->maxdepth)
424 codebook_setup->maxdepth = tmp_vlc_bits[t];
426 if (codebook_setup->maxdepth > 3 * V_NB_BITS)
427 codebook_setup->nb_bits = V_NB_BITS2;
429 codebook_setup->nb_bits = V_NB_BITS;
431 codebook_setup->maxdepth = (codebook_setup->maxdepth+codebook_setup->nb_bits - 1) / codebook_setup->nb_bits;
433 if ((ret = init_vlc(&codebook_setup->vlc, codebook_setup->nb_bits,
434 entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits),
435 sizeof(*tmp_vlc_bits), tmp_vlc_codes,
436 sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes),
438 av_log(vc->avctx, AV_LOG_ERROR, " Error generating vlc tables. \n");
443 av_free(tmp_vlc_bits);
444 av_free(tmp_vlc_codes);
445 av_free(codebook_multiplicands);
450 av_free(tmp_vlc_bits);
451 av_free(tmp_vlc_codes);
452 av_free(codebook_multiplicands);
456 // Process time domain transforms part (unused in Vorbis I)
458 static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc)
460 GetBitContext *gb = &vc->gb;
461 unsigned i, vorbis_time_count = get_bits(gb, 6) + 1;
463 for (i = 0; i < vorbis_time_count; ++i) {
464 unsigned vorbis_tdtransform = get_bits(gb, 16);
466 av_dlog(NULL, " Vorbis time domain transform %u: %u\n",
467 vorbis_time_count, vorbis_tdtransform);
469 if (vorbis_tdtransform) {
470 av_log(vc->avctx, AV_LOG_ERROR, "Vorbis time domain transform data nonzero. \n");
471 return AVERROR_INVALIDDATA;
477 // Process floors part
479 static int vorbis_floor0_decode(vorbis_context *vc,
480 vorbis_floor_data *vfu, float *vec);
481 static int create_map(vorbis_context *vc, unsigned floor_number);
482 static int vorbis_floor1_decode(vorbis_context *vc,
483 vorbis_floor_data *vfu, float *vec);
484 static int vorbis_parse_setup_hdr_floors(vorbis_context *vc)
486 GetBitContext *gb = &vc->gb;
489 vc->floor_count = get_bits(gb, 6) + 1;
491 vc->floors = av_mallocz(vc->floor_count * sizeof(*vc->floors));
493 return AVERROR(ENOMEM);
495 for (i = 0; i < vc->floor_count; ++i) {
496 vorbis_floor *floor_setup = &vc->floors[i];
498 floor_setup->floor_type = get_bits(gb, 16);
500 av_dlog(NULL, " %d. floor type %d \n", i, floor_setup->floor_type);
502 if (floor_setup->floor_type == 1) {
503 int maximum_class = -1;
504 unsigned rangebits, rangemax, floor1_values = 2;
506 floor_setup->decode = vorbis_floor1_decode;
508 floor_setup->data.t1.partitions = get_bits(gb, 5);
510 av_dlog(NULL, " %d.floor: %d partitions \n",
511 i, floor_setup->data.t1.partitions);
513 for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
514 floor_setup->data.t1.partition_class[j] = get_bits(gb, 4);
515 if (floor_setup->data.t1.partition_class[j] > maximum_class)
516 maximum_class = floor_setup->data.t1.partition_class[j];
518 av_dlog(NULL, " %d. floor %d partition class %d \n",
519 i, j, floor_setup->data.t1.partition_class[j]);
523 av_dlog(NULL, " maximum class %d \n", maximum_class);
525 for (j = 0; j <= maximum_class; ++j) {
526 floor_setup->data.t1.class_dimensions[j] = get_bits(gb, 3) + 1;
527 floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2);
529 av_dlog(NULL, " %d floor %d class dim: %d subclasses %d \n", i, j,
530 floor_setup->data.t1.class_dimensions[j],
531 floor_setup->data.t1.class_subclasses[j]);
533 if (floor_setup->data.t1.class_subclasses[j]) {
534 GET_VALIDATED_INDEX(floor_setup->data.t1.class_masterbook[j], 8, vc->codebook_count)
536 av_dlog(NULL, " masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]);
539 for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) {
540 int16_t bits = get_bits(gb, 8) - 1;
542 VALIDATE_INDEX(bits, vc->codebook_count)
543 floor_setup->data.t1.subclass_books[j][k] = bits;
545 av_dlog(NULL, " book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]);
549 floor_setup->data.t1.multiplier = get_bits(gb, 2) + 1;
550 floor_setup->data.t1.x_list_dim = 2;
552 for (j = 0; j < floor_setup->data.t1.partitions; ++j)
553 floor_setup->data.t1.x_list_dim+=floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]];
555 floor_setup->data.t1.list = av_mallocz(floor_setup->data.t1.x_list_dim *
556 sizeof(*floor_setup->data.t1.list));
557 if (!floor_setup->data.t1.list)
558 return AVERROR(ENOMEM);
560 rangebits = get_bits(gb, 4);
561 rangemax = (1 << rangebits);
562 if (rangemax > vc->blocksize[1] / 2) {
563 av_log(vc->avctx, AV_LOG_ERROR,
564 "Floor value is too large for blocksize: %u (%"PRIu32")\n",
565 rangemax, vc->blocksize[1] / 2);
566 return AVERROR_INVALIDDATA;
568 floor_setup->data.t1.list[0].x = 0;
569 floor_setup->data.t1.list[1].x = rangemax;
571 for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
572 for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) {
573 floor_setup->data.t1.list[floor1_values].x = get_bits(gb, rangebits);
575 av_dlog(NULL, " %u. floor1 Y coord. %d\n", floor1_values,
576 floor_setup->data.t1.list[floor1_values].x);
580 // Precalculate order of x coordinates - needed for decode
581 if (ff_vorbis_ready_floor1_list(vc->avctx,
582 floor_setup->data.t1.list,
583 floor_setup->data.t1.x_list_dim)) {
584 return AVERROR_INVALIDDATA;
586 } else if (floor_setup->floor_type == 0) {
587 unsigned max_codebook_dim = 0;
589 floor_setup->decode = vorbis_floor0_decode;
591 floor_setup->data.t0.order = get_bits(gb, 8);
592 if (!floor_setup->data.t0.order) {
593 av_log(vc->avctx, AV_LOG_ERROR, "Floor 0 order is 0.\n");
594 return AVERROR_INVALIDDATA;
596 floor_setup->data.t0.rate = get_bits(gb, 16);
597 if (!floor_setup->data.t0.rate) {
598 av_log(vc->avctx, AV_LOG_ERROR, "Floor 0 rate is 0.\n");
599 return AVERROR_INVALIDDATA;
601 floor_setup->data.t0.bark_map_size = get_bits(gb, 16);
602 if (!floor_setup->data.t0.bark_map_size) {
603 av_log(vc->avctx, AV_LOG_ERROR,
604 "Floor 0 bark map size is 0.\n");
605 return AVERROR_INVALIDDATA;
607 floor_setup->data.t0.amplitude_bits = get_bits(gb, 6);
608 floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
609 floor_setup->data.t0.num_books = get_bits(gb, 4) + 1;
611 /* allocate mem for booklist */
612 floor_setup->data.t0.book_list =
613 av_malloc(floor_setup->data.t0.num_books);
614 if (!floor_setup->data.t0.book_list)
615 return AVERROR(ENOMEM);
616 /* read book indexes */
620 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
621 GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count)
622 floor_setup->data.t0.book_list[idx] = book_idx;
623 if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
624 max_codebook_dim = vc->codebooks[book_idx].dimensions;
628 if ((ret = create_map(vc, i)) < 0)
631 /* codebook dim is for padding if codebook dim doesn't *
632 * divide order+1 then we need to read more data */
633 floor_setup->data.t0.lsp =
634 av_malloc((floor_setup->data.t0.order + 1 + max_codebook_dim)
635 * sizeof(*floor_setup->data.t0.lsp));
636 if (!floor_setup->data.t0.lsp)
637 return AVERROR(ENOMEM);
639 /* debug output parsed headers */
640 av_dlog(NULL, "floor0 order: %u\n", floor_setup->data.t0.order);
641 av_dlog(NULL, "floor0 rate: %u\n", floor_setup->data.t0.rate);
642 av_dlog(NULL, "floor0 bark map size: %u\n",
643 floor_setup->data.t0.bark_map_size);
644 av_dlog(NULL, "floor0 amplitude bits: %u\n",
645 floor_setup->data.t0.amplitude_bits);
646 av_dlog(NULL, "floor0 amplitude offset: %u\n",
647 floor_setup->data.t0.amplitude_offset);
648 av_dlog(NULL, "floor0 number of books: %u\n",
649 floor_setup->data.t0.num_books);
650 av_dlog(NULL, "floor0 book list pointer: %p\n",
651 floor_setup->data.t0.book_list);
654 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
655 av_dlog(NULL, " Book %d: %u\n", idx + 1,
656 floor_setup->data.t0.book_list[idx]);
660 av_log(vc->avctx, AV_LOG_ERROR, "Invalid floor type!\n");
661 return AVERROR_INVALIDDATA;
667 // Process residues part
669 static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
671 GetBitContext *gb = &vc->gb;
674 vc->residue_count = get_bits(gb, 6)+1;
675 vc->residues = av_mallocz(vc->residue_count * sizeof(*vc->residues));
677 return AVERROR(ENOMEM);
679 av_dlog(NULL, " There are %d residues. \n", vc->residue_count);
681 for (i = 0; i < vc->residue_count; ++i) {
682 vorbis_residue *res_setup = &vc->residues[i];
684 unsigned high_bits, low_bits;
686 res_setup->type = get_bits(gb, 16);
688 av_dlog(NULL, " %u. residue type %d\n", i, res_setup->type);
690 res_setup->begin = get_bits(gb, 24);
691 res_setup->end = get_bits(gb, 24);
692 res_setup->partition_size = get_bits(gb, 24) + 1;
693 /* Validations to prevent a buffer overflow later. */
694 if (res_setup->begin>res_setup->end ||
695 res_setup->end > (res_setup->type == 2 ? vc->avctx->channels : 1) * vc->blocksize[1] / 2 ||
696 (res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
697 av_log(vc->avctx, AV_LOG_ERROR,
698 "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n",
699 res_setup->type, res_setup->begin, res_setup->end,
700 res_setup->partition_size, vc->blocksize[1] / 2);
701 return AVERROR_INVALIDDATA;
704 res_setup->classifications = get_bits(gb, 6) + 1;
705 GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
707 res_setup->ptns_to_read =
708 (res_setup->end - res_setup->begin) / res_setup->partition_size;
709 res_setup->classifs = av_malloc(res_setup->ptns_to_read *
711 sizeof(*res_setup->classifs));
712 if (!res_setup->classifs)
713 return AVERROR(ENOMEM);
715 av_dlog(NULL, " begin %d end %d part.size %d classif.s %d classbook %d \n",
716 res_setup->begin, res_setup->end, res_setup->partition_size,
717 res_setup->classifications, res_setup->classbook);
719 for (j = 0; j < res_setup->classifications; ++j) {
721 low_bits = get_bits(gb, 3);
723 high_bits = get_bits(gb, 5);
724 cascade[j] = (high_bits << 3) + low_bits;
726 av_dlog(NULL, " %u class cascade depth: %d\n", j, ilog(cascade[j]));
729 res_setup->maxpass = 0;
730 for (j = 0; j < res_setup->classifications; ++j) {
731 for (k = 0; k < 8; ++k) {
732 if (cascade[j]&(1 << k)) {
733 GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
735 av_dlog(NULL, " %u class cascade depth %u book: %d\n",
736 j, k, res_setup->books[j][k]);
738 if (k>res_setup->maxpass)
739 res_setup->maxpass = k;
741 res_setup->books[j][k] = -1;
749 // Process mappings part
751 static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
753 GetBitContext *gb = &vc->gb;
756 vc->mapping_count = get_bits(gb, 6)+1;
757 vc->mappings = av_mallocz(vc->mapping_count * sizeof(*vc->mappings));
759 return AVERROR(ENOMEM);
761 av_dlog(NULL, " There are %d mappings. \n", vc->mapping_count);
763 for (i = 0; i < vc->mapping_count; ++i) {
764 vorbis_mapping *mapping_setup = &vc->mappings[i];
766 if (get_bits(gb, 16)) {
767 av_log(vc->avctx, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
768 return AVERROR_INVALIDDATA;
771 mapping_setup->submaps = get_bits(gb, 4) + 1;
773 mapping_setup->submaps = 1;
777 mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
778 mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps *
779 sizeof(*mapping_setup->magnitude));
780 mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps *
781 sizeof(*mapping_setup->angle));
782 if (!mapping_setup->angle || !mapping_setup->magnitude)
783 return AVERROR(ENOMEM);
785 for (j = 0; j < mapping_setup->coupling_steps; ++j) {
786 GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
787 GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels)
790 mapping_setup->coupling_steps = 0;
793 av_dlog(NULL, " %u mapping coupling steps: %d\n",
794 i, mapping_setup->coupling_steps);
796 if (get_bits(gb, 2)) {
797 av_log(vc->avctx, AV_LOG_ERROR, "%u. mapping setup data invalid.\n", i);
798 return AVERROR_INVALIDDATA; // following spec.
801 if (mapping_setup->submaps>1) {
802 mapping_setup->mux = av_mallocz(vc->audio_channels *
803 sizeof(*mapping_setup->mux));
804 if (!mapping_setup->mux)
805 return AVERROR(ENOMEM);
807 for (j = 0; j < vc->audio_channels; ++j)
808 mapping_setup->mux[j] = get_bits(gb, 4);
811 for (j = 0; j < mapping_setup->submaps; ++j) {
812 skip_bits(gb, 8); // FIXME check?
813 GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count)
814 GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
816 av_dlog(NULL, " %u mapping %u submap : floor %d, residue %d\n", i, j,
817 mapping_setup->submap_floor[j],
818 mapping_setup->submap_residue[j]);
824 // Process modes part
826 static int create_map(vorbis_context *vc, unsigned floor_number)
828 vorbis_floor *floors = vc->floors;
834 for (blockflag = 0; blockflag < 2; ++blockflag) {
835 n = vc->blocksize[blockflag] / 2;
836 floors[floor_number].data.t0.map[blockflag] =
837 av_malloc((n + 1) * sizeof(int32_t)); // n + sentinel
838 if (!floors[floor_number].data.t0.map[blockflag])
839 return AVERROR(ENOMEM);
841 map = floors[floor_number].data.t0.map[blockflag];
842 vf = &floors[floor_number].data.t0;
844 for (idx = 0; idx < n; ++idx) {
845 map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
846 (vf->bark_map_size / BARK(vf->rate / 2.0f)));
847 if (vf->bark_map_size-1 < map[idx])
848 map[idx] = vf->bark_map_size - 1;
851 vf->map_size[blockflag] = n;
854 for (idx = 0; idx <= n; ++idx) {
855 av_dlog(NULL, "floor0 map: map at pos %d is %d\n", idx, map[idx]);
861 static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
863 GetBitContext *gb = &vc->gb;
866 vc->mode_count = get_bits(gb, 6) + 1;
867 vc->modes = av_mallocz(vc->mode_count * sizeof(*vc->modes));
869 return AVERROR(ENOMEM);
871 av_dlog(NULL, " There are %d modes.\n", vc->mode_count);
873 for (i = 0; i < vc->mode_count; ++i) {
874 vorbis_mode *mode_setup = &vc->modes[i];
876 mode_setup->blockflag = get_bits1(gb);
877 mode_setup->windowtype = get_bits(gb, 16); //FIXME check
878 mode_setup->transformtype = get_bits(gb, 16); //FIXME check
879 GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
881 av_dlog(NULL, " %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
882 i, mode_setup->blockflag, mode_setup->windowtype,
883 mode_setup->transformtype, mode_setup->mapping);
888 // Process the whole setup header using the functions above
890 static int vorbis_parse_setup_hdr(vorbis_context *vc)
892 GetBitContext *gb = &vc->gb;
895 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
896 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
897 (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
898 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
899 return AVERROR_INVALIDDATA;
902 if ((ret = vorbis_parse_setup_hdr_codebooks(vc))) {
903 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
906 if ((ret = vorbis_parse_setup_hdr_tdtransforms(vc))) {
907 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
910 if ((ret = vorbis_parse_setup_hdr_floors(vc))) {
911 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
914 if ((ret = vorbis_parse_setup_hdr_residues(vc))) {
915 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
918 if ((ret = vorbis_parse_setup_hdr_mappings(vc))) {
919 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
922 if ((ret = vorbis_parse_setup_hdr_modes(vc))) {
923 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
926 if (!get_bits1(gb)) {
927 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
928 return AVERROR_INVALIDDATA; // framing flag bit unset error
934 // Process the identification header
936 static int vorbis_parse_id_hdr(vorbis_context *vc)
938 GetBitContext *gb = &vc->gb;
941 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
942 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
943 (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
944 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
945 return AVERROR_INVALIDDATA;
948 vc->version = get_bits_long(gb, 32); //FIXME check 0
949 vc->audio_channels = get_bits(gb, 8);
950 if (vc->audio_channels <= 0) {
951 av_log(vc->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
952 return AVERROR_INVALIDDATA;
954 vc->audio_samplerate = get_bits_long(gb, 32);
955 if (vc->audio_samplerate <= 0) {
956 av_log(vc->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
957 return AVERROR_INVALIDDATA;
959 vc->bitrate_maximum = get_bits_long(gb, 32);
960 vc->bitrate_nominal = get_bits_long(gb, 32);
961 vc->bitrate_minimum = get_bits_long(gb, 32);
962 bl0 = get_bits(gb, 4);
963 bl1 = get_bits(gb, 4);
964 vc->blocksize[0] = (1 << bl0);
965 vc->blocksize[1] = (1 << bl1);
966 if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
967 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
968 return AVERROR_INVALIDDATA;
970 vc->win[0] = ff_vorbis_vwin[bl0 - 6];
971 vc->win[1] = ff_vorbis_vwin[bl1 - 6];
973 if ((get_bits1(gb)) == 0) {
974 av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
975 return AVERROR_INVALIDDATA;
978 vc->channel_residues = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(*vc->channel_residues));
979 vc->saved = av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(*vc->saved));
980 if (!vc->channel_residues || !vc->saved)
981 return AVERROR(ENOMEM);
983 vc->previous_window = 0;
985 ff_mdct_init(&vc->mdct[0], bl0, 1, -1.0);
986 ff_mdct_init(&vc->mdct[1], bl1, 1, -1.0);
988 av_dlog(NULL, " vorbis version %d \n audio_channels %d \n audio_samplerate %d \n bitrate_max %d \n bitrate_nom %d \n bitrate_min %d \n blk_0 %d blk_1 %d \n ",
989 vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
992 BLK = vc->blocksize[0];
993 for (i = 0; i < BLK / 2; ++i) {
994 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)));
1001 // Process the extradata using the functions above (identification header, setup header)
1003 static av_cold int vorbis_decode_init(AVCodecContext *avctx)
1005 vorbis_context *vc = avctx->priv_data;
1006 uint8_t *headers = avctx->extradata;
1007 int headers_len = avctx->extradata_size;
1008 uint8_t *header_start[3];
1010 GetBitContext *gb = &vc->gb;
1014 ff_vorbisdsp_init(&vc->dsp);
1015 avpriv_float_dsp_init(&vc->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
1016 ff_fmt_convert_init(&vc->fmt_conv, avctx);
1018 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1021 av_log(avctx, AV_LOG_ERROR, "Extradata missing.\n");
1022 return AVERROR_INVALIDDATA;
1025 if ((ret = avpriv_split_xiph_headers(headers, headers_len, 30, header_start, header_len)) < 0) {
1026 av_log(avctx, AV_LOG_ERROR, "Extradata corrupt.\n");
1030 init_get_bits(gb, header_start[0], header_len[0]*8);
1031 hdr_type = get_bits(gb, 8);
1032 if (hdr_type != 1) {
1033 av_log(avctx, AV_LOG_ERROR, "First header is not the id header.\n");
1034 return AVERROR_INVALIDDATA;
1036 if ((ret = vorbis_parse_id_hdr(vc))) {
1037 av_log(avctx, AV_LOG_ERROR, "Id header corrupt.\n");
1042 init_get_bits(gb, header_start[2], header_len[2]*8);
1043 hdr_type = get_bits(gb, 8);
1044 if (hdr_type != 5) {
1045 av_log(avctx, AV_LOG_ERROR, "Third header is not the setup header.\n");
1047 return AVERROR_INVALIDDATA;
1049 if ((ret = vorbis_parse_setup_hdr(vc))) {
1050 av_log(avctx, AV_LOG_ERROR, "Setup header corrupt.\n");
1055 if (vc->audio_channels > 8)
1056 avctx->channel_layout = 0;
1058 avctx->channel_layout = ff_vorbis_channel_layouts[vc->audio_channels - 1];
1060 avctx->channels = vc->audio_channels;
1061 avctx->sample_rate = vc->audio_samplerate;
1066 // Decode audiopackets -------------------------------------------------
1068 // Read and decode floor
1070 static int vorbis_floor0_decode(vorbis_context *vc,
1071 vorbis_floor_data *vfu, float *vec)
1073 vorbis_floor0 *vf = &vfu->t0;
1074 float *lsp = vf->lsp;
1075 unsigned amplitude, book_idx;
1076 unsigned blockflag = vc->modes[vc->mode_number].blockflag;
1078 if (!vf->amplitude_bits)
1081 amplitude = get_bits(&vc->gb, vf->amplitude_bits);
1082 if (amplitude > 0) {
1084 unsigned idx, lsp_len = 0;
1085 vorbis_codebook codebook;
1087 book_idx = get_bits(&vc->gb, ilog(vf->num_books));
1088 if (book_idx >= vf->num_books) {
1089 av_log(vc->avctx, AV_LOG_ERROR, "floor0 dec: booknumber too high!\n");
1092 av_dlog(NULL, "floor0 dec: booknumber: %u\n", book_idx);
1093 codebook = vc->codebooks[vf->book_list[book_idx]];
1094 /* Invalid codebook! */
1095 if (!codebook.codevectors)
1096 return AVERROR_INVALIDDATA;
1098 while (lsp_len<vf->order) {
1101 av_dlog(NULL, "floor0 dec: book dimension: %d\n", codebook.dimensions);
1102 av_dlog(NULL, "floor0 dec: maximum depth: %d\n", codebook.maxdepth);
1103 /* read temp vector */
1104 vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
1105 codebook.nb_bits, codebook.maxdepth)
1106 * codebook.dimensions;
1107 av_dlog(NULL, "floor0 dec: vector offset: %d\n", vec_off);
1108 /* copy each vector component and add last to it */
1109 for (idx = 0; idx < codebook.dimensions; ++idx)
1110 lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
1111 last = lsp[lsp_len+idx-1]; /* set last to last vector component */
1113 lsp_len += codebook.dimensions;
1115 /* DEBUG: output lsp coeffs */
1118 for (idx = 0; idx < lsp_len; ++idx)
1119 av_dlog(NULL, "floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
1122 /* synthesize floor output vector */
1125 int order = vf->order;
1126 float wstep = M_PI / vf->bark_map_size;
1128 for (i = 0; i < order; i++)
1129 lsp[i] = 2.0f * cos(lsp[i]);
1131 av_dlog(NULL, "floor0 synth: map_size = %"PRIu32"; m = %d; wstep = %f\n",
1132 vf->map_size[blockflag], order, wstep);
1135 while (i < vf->map_size[blockflag]) {
1136 int j, iter_cond = vf->map[blockflag][i];
1139 float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times
1141 /* similar part for the q and p products */
1142 for (j = 0; j + 1 < order; j += 2) {
1143 q *= lsp[j] - two_cos_w;
1144 p *= lsp[j + 1] - two_cos_w;
1146 if (j == order) { // even order
1147 p *= p * (2.0f - two_cos_w);
1148 q *= q * (2.0f + two_cos_w);
1149 } else { // odd order
1150 q *= two_cos_w-lsp[j]; // one more time for q
1152 /* final step and square */
1153 p *= p * (4.f - two_cos_w * two_cos_w);
1157 /* calculate linear floor value */
1158 q = exp((((amplitude*vf->amplitude_offset) /
1159 (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
1160 - vf->amplitude_offset) * .11512925f);
1165 } while (vf->map[blockflag][i] == iter_cond);
1169 /* this channel is unused */
1173 av_dlog(NULL, " Floor0 decoded\n");
1178 static int vorbis_floor1_decode(vorbis_context *vc,
1179 vorbis_floor_data *vfu, float *vec)
1181 vorbis_floor1 *vf = &vfu->t1;
1182 GetBitContext *gb = &vc->gb;
1183 uint16_t range_v[4] = { 256, 128, 86, 64 };
1184 unsigned range = range_v[vf->multiplier - 1];
1185 uint16_t floor1_Y[258];
1186 uint16_t floor1_Y_final[258];
1187 int floor1_flag[258];
1188 unsigned class, cdim, cbits, csub, cval, offset, i, j;
1189 int book, adx, ady, dy, off, predicted, err;
1192 if (!get_bits1(gb)) // silence
1195 // Read values (or differences) for the floor's points
1197 floor1_Y[0] = get_bits(gb, ilog(range - 1));
1198 floor1_Y[1] = get_bits(gb, ilog(range - 1));
1200 av_dlog(NULL, "floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
1203 for (i = 0; i < vf->partitions; ++i) {
1204 class = vf->partition_class[i];
1205 cdim = vf->class_dimensions[class];
1206 cbits = vf->class_subclasses[class];
1207 csub = (1 << cbits) - 1;
1210 av_dlog(NULL, "Cbits %u\n", cbits);
1212 if (cbits) // this reads all subclasses for this partition's class
1213 cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[class]].vlc.table,
1214 vc->codebooks[vf->class_masterbook[class]].nb_bits, 3);
1216 for (j = 0; j < cdim; ++j) {
1217 book = vf->subclass_books[class][cval & csub];
1219 av_dlog(NULL, "book %d Cbits %u cval %u bits:%d\n",
1220 book, cbits, cval, get_bits_count(gb));
1222 cval = cval >> cbits;
1224 floor1_Y[offset+j] = get_vlc2(gb, vc->codebooks[book].vlc.table,
1225 vc->codebooks[book].nb_bits, 3);
1227 floor1_Y[offset+j] = 0;
1230 av_dlog(NULL, " floor(%d) = %d \n",
1231 vf->list[offset+j].x, floor1_Y[offset+j]);
1236 // Amplitude calculation from the differences
1240 floor1_Y_final[0] = floor1_Y[0];
1241 floor1_Y_final[1] = floor1_Y[1];
1243 for (i = 2; i < vf->x_list_dim; ++i) {
1244 unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
1246 low_neigh_offs = vf->list[i].low;
1247 high_neigh_offs = vf->list[i].high;
1248 dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs]; // render_point begin
1249 adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
1251 err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
1254 predicted = floor1_Y_final[low_neigh_offs] - off;
1256 predicted = floor1_Y_final[low_neigh_offs] + off;
1257 } // render_point end
1260 highroom = range-predicted;
1261 lowroom = predicted;
1262 if (highroom < lowroom) {
1263 room = highroom * 2;
1265 room = lowroom * 2; // SPEC misspelling
1268 floor1_flag[low_neigh_offs] = 1;
1269 floor1_flag[high_neigh_offs] = 1;
1272 if (highroom > lowroom) {
1273 floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
1275 floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
1279 floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
1281 floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
1286 floor1_Y_final[i] = av_clip_uint16(predicted);
1289 av_dlog(NULL, " Decoded floor(%d) = %u / val %u\n",
1290 vf->list[i].x, floor1_Y_final[i], val);
1293 // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ?
1295 ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x);
1297 av_dlog(NULL, " Floor decoded\n");
1302 static av_always_inline int setup_classifs(vorbis_context *vc,
1304 uint8_t *do_not_decode,
1306 int partition_count)
1309 unsigned c_p_c = vc->codebooks[vr->classbook].dimensions;
1310 unsigned inverse_class = ff_inverse[vr->classifications];
1311 unsigned temp, temp2;
1312 for (p = 0, j = 0; j < ch_used; ++j) {
1313 if (!do_not_decode[j]) {
1314 temp = get_vlc2(&vc->gb, vc->codebooks[vr->classbook].vlc.table,
1315 vc->codebooks[vr->classbook].nb_bits, 3);
1317 av_dlog(NULL, "Classword: %u\n", temp);
1319 if (temp <= 65536) {
1320 for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1321 temp2 = (((uint64_t)temp) * inverse_class) >> 32;
1323 if (i < vr->ptns_to_read)
1324 vr->classifs[p + i] = temp - temp2 * vr->classifications;
1328 for (i = partition_count + c_p_c - 1; i >= partition_count; i--) {
1329 temp2 = temp / vr->classifications;
1331 if (i < vr->ptns_to_read)
1332 vr->classifs[p + i] = temp - temp2 * vr->classifications;
1337 p += vr->ptns_to_read;
1341 // Read and decode residue
1343 static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc,
1346 uint8_t *do_not_decode,
1352 GetBitContext *gb = &vc->gb;
1353 unsigned c_p_c = vc->codebooks[vr->classbook].dimensions;
1354 uint8_t *classifs = vr->classifs;
1355 unsigned pass, ch_used, i, j, k, l;
1356 unsigned max_output = (ch - 1) * vlen;
1357 int ptns_to_read = vr->ptns_to_read;
1360 for (j = 1; j < ch; ++j)
1361 do_not_decode[0] &= do_not_decode[j]; // FIXME - clobbering input
1362 if (do_not_decode[0])
1365 max_output += vr->end / ch;
1368 max_output += vr->end;
1371 if (max_output > ch_left * vlen) {
1372 av_log(vc->avctx, AV_LOG_ERROR, "Insufficient output buffer\n");
1373 return AVERROR_INVALIDDATA;
1376 av_dlog(NULL, " residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
1378 for (pass = 0; pass <= vr->maxpass; ++pass) { // FIXME OPTIMIZE?
1379 int voffset, partition_count, j_times_ptns_to_read;
1381 voffset = vr->begin;
1382 for (partition_count = 0; partition_count < ptns_to_read;) { // SPEC error
1384 setup_classifs(vc, vr, do_not_decode, ch_used, partition_count);
1386 for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
1387 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1390 if (!do_not_decode[j]) {
1391 unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
1392 int vqbook = vr->books[vqclass][pass];
1394 if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
1396 unsigned dim = vc->codebooks[vqbook].dimensions;
1397 unsigned step = FASTDIV(vr->partition_size << 1, dim << 1);
1398 vorbis_codebook codebook = vc->codebooks[vqbook];
1402 voffs = voffset+j*vlen;
1403 for (k = 0; k < step; ++k) {
1404 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
1405 for (l = 0; l < dim; ++l)
1406 vec[voffs + k + l * step] += codebook.codevectors[coffs + l];
1408 } else if (vr_type == 1) {
1409 voffs = voffset + j * vlen;
1410 for (k = 0; k < step; ++k) {
1411 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
1412 for (l = 0; l < dim; ++l, ++voffs) {
1413 vec[voffs]+=codebook.codevectors[coffs+l];
1415 av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
1416 pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
1419 } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) { // most frequent case optimized
1420 voffs = voffset >> 1;
1423 for (k = 0; k < step; ++k) {
1424 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 2;
1425 vec[voffs + k ] += codebook.codevectors[coffs ];
1426 vec[voffs + k + vlen] += codebook.codevectors[coffs + 1];
1428 } else if (dim == 4) {
1429 for (k = 0; k < step; ++k, voffs += 2) {
1430 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 4;
1431 vec[voffs ] += codebook.codevectors[coffs ];
1432 vec[voffs + 1 ] += codebook.codevectors[coffs + 2];
1433 vec[voffs + vlen ] += codebook.codevectors[coffs + 1];
1434 vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3];
1437 for (k = 0; k < step; ++k) {
1438 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
1439 for (l = 0; l < dim; l += 2, voffs++) {
1440 vec[voffs ] += codebook.codevectors[coffs + l ];
1441 vec[voffs + vlen] += codebook.codevectors[coffs + l + 1];
1443 av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1444 pass, voffset / ch + (voffs % ch) * vlen,
1445 vec[voffset / ch + (voffs % ch) * vlen],
1446 codebook.codevectors[coffs + l], coffs, l);
1450 } else if (vr_type == 2) {
1451 unsigned voffs_div = FASTDIV(voffset << 1, ch <<1);
1452 unsigned voffs_mod = voffset - voffs_div * ch;
1454 for (k = 0; k < step; ++k) {
1455 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
1456 for (l = 0; l < dim; ++l) {
1457 vec[voffs_div + voffs_mod * vlen] +=
1458 codebook.codevectors[coffs + l];
1460 av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1461 pass, voffs_div + voffs_mod * vlen,
1462 vec[voffs_div + voffs_mod * vlen],
1463 codebook.codevectors[coffs + l], coffs, l);
1465 if (++voffs_mod == ch) {
1474 j_times_ptns_to_read += ptns_to_read;
1477 voffset += vr->partition_size;
1484 static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr,
1486 uint8_t *do_not_decode,
1487 float *vec, unsigned vlen,
1491 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2);
1492 else if (vr->type == 1)
1493 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1);
1494 else if (vr->type == 0)
1495 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 0);
1497 av_log(vc->avctx, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
1498 return AVERROR_INVALIDDATA;
1502 void ff_vorbis_inverse_coupling(float *mag, float *ang, intptr_t blocksize)
1505 for (i = 0; i < blocksize; i++) {
1508 ang[i] = mag[i] - ang[i];
1510 float temp = ang[i];
1518 float temp = ang[i];
1526 // Decode the audio packet using the functions above
1528 static int vorbis_parse_audio_packet(vorbis_context *vc, float **floor_ptr)
1530 GetBitContext *gb = &vc->gb;
1532 unsigned previous_window = vc->previous_window;
1533 unsigned mode_number, blockflag, blocksize;
1535 uint8_t no_residue[255];
1536 uint8_t do_not_decode[255];
1537 vorbis_mapping *mapping;
1538 float *ch_res_ptr = vc->channel_residues;
1539 uint8_t res_chan[255];
1540 unsigned res_num = 0;
1542 unsigned ch_left = vc->audio_channels;
1545 if (get_bits1(gb)) {
1546 av_log(vc->avctx, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
1547 return AVERROR_INVALIDDATA; // packet type not audio
1550 if (vc->mode_count == 1) {
1553 GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
1555 vc->mode_number = mode_number;
1556 mapping = &vc->mappings[vc->modes[mode_number].mapping];
1558 av_dlog(NULL, " Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
1559 vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
1561 blockflag = vc->modes[mode_number].blockflag;
1562 blocksize = vc->blocksize[blockflag];
1563 vlen = blocksize / 2;
1565 previous_window = get_bits(gb, 1);
1566 skip_bits1(gb); // next_window
1569 memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
1570 for (i = 0; i < vc->audio_channels; ++i)
1571 memset(floor_ptr[i], 0, vlen * sizeof(floor_ptr[0][0])); //FIXME can this be removed ?
1575 for (i = 0; i < vc->audio_channels; ++i) {
1576 vorbis_floor *floor;
1578 if (mapping->submaps > 1) {
1579 floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
1581 floor = &vc->floors[mapping->submap_floor[0]];
1584 ret = floor->decode(vc, &floor->data, floor_ptr[i]);
1587 av_log(vc->avctx, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
1588 return AVERROR_INVALIDDATA;
1590 no_residue[i] = ret;
1593 // Nonzero vector propagate
1595 for (i = mapping->coupling_steps - 1; i >= 0; --i) {
1596 if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
1597 no_residue[mapping->magnitude[i]] = 0;
1598 no_residue[mapping->angle[i]] = 0;
1604 for (i = 0; i < mapping->submaps; ++i) {
1605 vorbis_residue *residue;
1609 for (j = 0; j < vc->audio_channels; ++j) {
1610 if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
1611 res_chan[j] = res_num;
1612 if (no_residue[j]) {
1613 do_not_decode[ch] = 1;
1615 do_not_decode[ch] = 0;
1621 residue = &vc->residues[mapping->submap_residue[i]];
1623 av_log(vc->avctx, AV_LOG_ERROR, "Too many channels in vorbis_floor_decode.\n");
1624 return AVERROR_INVALIDDATA;
1627 ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left);
1632 ch_res_ptr += ch * vlen;
1637 return AVERROR_INVALIDDATA;
1641 for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed
1644 mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2;
1645 ang = vc->channel_residues+res_chan[mapping->angle[i]] * blocksize / 2;
1646 vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
1651 mdct = &vc->mdct[blockflag];
1653 for (j = vc->audio_channels-1;j >= 0; j--) {
1654 ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
1655 vc->fdsp.vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
1656 mdct->imdct_half(mdct, ch_res_ptr, floor_ptr[j]);
1659 // Overlap/add, save data for next overlapping
1661 retlen = (blocksize + vc->blocksize[previous_window]) / 4;
1662 for (j = 0; j < vc->audio_channels; j++) {
1663 unsigned bs0 = vc->blocksize[0];
1664 unsigned bs1 = vc->blocksize[1];
1665 float *residue = vc->channel_residues + res_chan[j] * blocksize / 2;
1666 float *saved = vc->saved + j * bs1 / 4;
1667 float *ret = floor_ptr[j];
1668 float *buf = residue;
1669 const float *win = vc->win[blockflag & previous_window];
1671 if (blockflag == previous_window) {
1672 vc->fdsp.vector_fmul_window(ret, saved, buf, win, blocksize / 4);
1673 } else if (blockflag > previous_window) {
1674 vc->fdsp.vector_fmul_window(ret, saved, buf, win, bs0 / 4);
1675 memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) * sizeof(float));
1677 memcpy(ret, saved, ((bs1 - bs0) / 4) * sizeof(float));
1678 vc->fdsp.vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, bs0 / 4);
1680 memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
1683 vc->previous_window = blockflag;
1687 // Return the decoded audio packet through the standard api
1689 static int vorbis_decode_frame(AVCodecContext *avctx, void *data,
1690 int *got_frame_ptr, AVPacket *avpkt)
1692 const uint8_t *buf = avpkt->data;
1693 int buf_size = avpkt->size;
1694 vorbis_context *vc = avctx->priv_data;
1695 AVFrame *frame = data;
1696 GetBitContext *gb = &vc->gb;
1697 float *channel_ptrs[255];
1700 av_dlog(NULL, "packet length %d \n", buf_size);
1702 /* get output buffer */
1703 frame->nb_samples = vc->blocksize[1] / 2;
1704 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1705 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1709 if (vc->audio_channels > 8) {
1710 for (i = 0; i < vc->audio_channels; i++)
1711 channel_ptrs[i] = (float *)frame->extended_data[i];
1713 for (i = 0; i < vc->audio_channels; i++) {
1714 int ch = ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
1715 channel_ptrs[ch] = (float *)frame->extended_data[i];
1719 init_get_bits(gb, buf, buf_size*8);
1721 if ((len = vorbis_parse_audio_packet(vc, channel_ptrs)) <= 0)
1724 if (!vc->first_frame) {
1725 vc->first_frame = 1;
1727 av_frame_unref(frame);
1731 av_dlog(NULL, "parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
1732 get_bits_count(gb) / 8, get_bits_count(gb) % 8, len);
1734 frame->nb_samples = len;
1742 static av_cold int vorbis_decode_close(AVCodecContext *avctx)
1744 vorbis_context *vc = avctx->priv_data;
1751 static av_cold void vorbis_decode_flush(AVCodecContext *avctx)
1753 vorbis_context *vc = avctx->priv_data;
1756 memset(vc->saved, 0, (vc->blocksize[1] / 4) * vc->audio_channels *
1757 sizeof(*vc->saved));
1759 vc->previous_window = 0;
1762 AVCodec ff_vorbis_decoder = {
1764 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
1765 .type = AVMEDIA_TYPE_AUDIO,
1766 .id = AV_CODEC_ID_VORBIS,
1767 .priv_data_size = sizeof(vorbis_context),
1768 .init = vorbis_decode_init,
1769 .close = vorbis_decode_close,
1770 .decode = vorbis_decode_frame,
1771 .flush = vorbis_decode_flush,
1772 .capabilities = CODEC_CAP_DR1,
1773 .channel_layouts = ff_vorbis_channel_layouts,
1774 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1775 AV_SAMPLE_FMT_NONE },