4 * @author Denes Balatoni ( dbalatoni programozo hu )
6 * This file is part of FFmpeg.
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.
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.
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
26 * @author Denes Balatoni ( dbalatoni programozo hu )
32 #define BITSTREAM_READER_LE
33 #include "libavutil/float_dsp.h"
34 #include "libavutil/avassert.h"
39 #include "fmtconvert.h"
47 #define V_MAX_VLCS (1 << 16)
48 #define V_MAX_PARTITIONS (1 << 20)
59 typedef union vorbis_floor_u vorbis_floor_data;
60 typedef struct vorbis_floor0_s vorbis_floor0;
61 typedef struct vorbis_floor1_s vorbis_floor1;
62 struct vorbis_context_s;
64 int (* vorbis_floor_decode_func)
65 (struct vorbis_context_s *, vorbis_floor_data *, float *);
68 vorbis_floor_decode_func decode;
69 union vorbis_floor_u {
70 struct vorbis_floor0_s {
73 uint16_t bark_map_size;
76 uint8_t amplitude_bits;
77 uint8_t amplitude_offset;
82 struct vorbis_floor1_s {
84 uint8_t partition_class[32];
85 uint8_t class_dimensions[16];
86 uint8_t class_subclasses[16];
87 uint8_t class_masterbook[16];
88 int16_t subclass_books[16][8];
91 vorbis_floor1_entry *list;
100 unsigned partition_size;
101 uint8_t classifications;
103 int16_t books[64][8];
105 uint16_t ptns_to_read;
111 uint16_t coupling_steps;
115 uint8_t submap_floor[16];
116 uint8_t submap_residue[16];
122 uint16_t transformtype;
126 typedef struct vorbis_context_s {
127 AVCodecContext *avccontext;
131 AVFloatDSPContext fdsp;
132 FmtConvertContext fmt_conv;
137 uint8_t audio_channels;
138 uint32_t audio_samplerate;
139 uint32_t bitrate_maximum;
140 uint32_t bitrate_nominal;
141 uint32_t bitrate_minimum;
142 uint32_t blocksize[2];
144 uint16_t codebook_count;
145 vorbis_codebook *codebooks;
147 vorbis_floor *floors;
148 uint8_t residue_count;
149 vorbis_residue *residues;
150 uint8_t mapping_count;
151 vorbis_mapping *mappings;
154 uint8_t mode_number; // mode number for the current packet
155 uint8_t previous_window;
156 float *channel_residues;
160 /* Helper functions */
163 (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x))
165 static const char idx_err_str[] = "Index value %d out of range (0 - %d) for %s at %s:%i\n";
166 #define VALIDATE_INDEX(idx, limit) \
168 av_log(vc->avccontext, AV_LOG_ERROR,\
170 (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\
171 return AVERROR_INVALIDDATA;\
173 #define GET_VALIDATED_INDEX(idx, bits, limit) \
175 idx = get_bits(gb, bits);\
176 VALIDATE_INDEX(idx, limit)\
179 static float vorbisfloat2float(unsigned val)
181 double mant = val & 0x1fffff;
182 long exp = (val & 0x7fe00000L) >> 21;
183 if (val & 0x80000000)
185 return ldexp(mant, exp - 20 - 768);
189 // Free all allocated memory -----------------------------------------
191 static void vorbis_free(vorbis_context *vc)
195 av_freep(&vc->channel_residues);
196 av_freep(&vc->saved);
199 for (i = 0; i < vc->residue_count; i++)
200 av_free(vc->residues[i].classifs);
201 av_freep(&vc->residues);
202 av_freep(&vc->modes);
204 ff_mdct_end(&vc->mdct[0]);
205 ff_mdct_end(&vc->mdct[1]);
208 for (i = 0; i < vc->codebook_count; ++i) {
209 av_free(vc->codebooks[i].codevectors);
210 ff_free_vlc(&vc->codebooks[i].vlc);
212 av_freep(&vc->codebooks);
215 for (i = 0; i < vc->floor_count; ++i) {
216 if (vc->floors[i].floor_type == 0) {
217 av_free(vc->floors[i].data.t0.map[0]);
218 av_free(vc->floors[i].data.t0.map[1]);
219 av_free(vc->floors[i].data.t0.book_list);
220 av_free(vc->floors[i].data.t0.lsp);
222 av_free(vc->floors[i].data.t1.list);
225 av_freep(&vc->floors);
228 for (i = 0; i < vc->mapping_count; ++i) {
229 av_free(vc->mappings[i].magnitude);
230 av_free(vc->mappings[i].angle);
231 av_free(vc->mappings[i].mux);
233 av_freep(&vc->mappings);
236 // Parse setup header -------------------------------------------------
238 // Process codebooks part
240 static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc)
243 uint8_t *tmp_vlc_bits;
244 uint32_t *tmp_vlc_codes;
245 GetBitContext *gb = &vc->gb;
246 uint16_t *codebook_multiplicands;
249 vc->codebook_count = get_bits(gb, 8) + 1;
251 av_dlog(NULL, " Codebooks: %d \n", vc->codebook_count);
253 vc->codebooks = av_mallocz(vc->codebook_count * sizeof(*vc->codebooks));
254 tmp_vlc_bits = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_bits));
255 tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_codes));
256 codebook_multiplicands = av_malloc(V_MAX_VLCS * sizeof(*codebook_multiplicands));
258 for (cb = 0; cb < vc->codebook_count; ++cb) {
259 vorbis_codebook *codebook_setup = &vc->codebooks[cb];
260 unsigned ordered, t, entries, used_entries = 0;
262 av_dlog(NULL, " %u. Codebook\n", cb);
264 if (get_bits(gb, 24) != 0x564342) {
265 av_log(vc->avccontext, AV_LOG_ERROR,
266 " %u. Codebook setup data corrupt.\n", cb);
267 ret = AVERROR_INVALIDDATA;
271 codebook_setup->dimensions=get_bits(gb, 16);
272 if (codebook_setup->dimensions > 16 || codebook_setup->dimensions == 0) {
273 av_log(vc->avccontext, AV_LOG_ERROR,
274 " %u. Codebook's dimension is invalid (%d).\n",
275 cb, codebook_setup->dimensions);
276 ret = AVERROR_INVALIDDATA;
279 entries = get_bits(gb, 24);
280 if (entries > V_MAX_VLCS) {
281 av_log(vc->avccontext, AV_LOG_ERROR,
282 " %u. Codebook has too many entries (%u).\n",
284 ret = AVERROR_INVALIDDATA;
288 ordered = get_bits1(gb);
290 av_dlog(NULL, " codebook_dimensions %d, codebook_entries %u\n",
291 codebook_setup->dimensions, entries);
295 unsigned sparse = get_bits1(gb);
297 av_dlog(NULL, " not ordered \n");
300 av_dlog(NULL, " sparse \n");
303 for (ce = 0; ce < entries; ++ce) {
304 flag = get_bits1(gb);
306 tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
309 tmp_vlc_bits[ce] = 0;
312 av_dlog(NULL, " not sparse \n");
314 used_entries = entries;
315 for (ce = 0; ce < entries; ++ce)
316 tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
319 unsigned current_entry = 0;
320 unsigned current_length = get_bits(gb, 5) + 1;
322 av_dlog(NULL, " ordered, current length: %u\n", current_length); //FIXME
324 used_entries = entries;
325 for (; current_entry < used_entries && current_length <= 32; ++current_length) {
328 av_dlog(NULL, " number bits: %u ", ilog(entries - current_entry));
330 number = get_bits(gb, ilog(entries - current_entry));
332 av_dlog(NULL, " number: %u\n", number);
334 for (i = current_entry; i < number+current_entry; ++i)
335 if (i < used_entries)
336 tmp_vlc_bits[i] = current_length;
338 current_entry+=number;
340 if (current_entry>used_entries) {
341 av_log(vc->avccontext, AV_LOG_ERROR, " More codelengths than codes in codebook. \n");
342 ret = AVERROR_INVALIDDATA;
347 codebook_setup->lookup_type = get_bits(gb, 4);
349 av_dlog(NULL, " lookup type: %d : %s \n", codebook_setup->lookup_type,
350 codebook_setup->lookup_type ? "vq" : "no lookup");
352 // If the codebook is used for (inverse) VQ, calculate codevectors.
354 if (codebook_setup->lookup_type == 1) {
356 unsigned codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions);
358 float codebook_minimum_value = vorbisfloat2float(get_bits_long(gb, 32));
359 float codebook_delta_value = vorbisfloat2float(get_bits_long(gb, 32));
360 unsigned codebook_value_bits = get_bits(gb, 4) + 1;
361 unsigned codebook_sequence_p = get_bits1(gb);
363 av_dlog(NULL, " We expect %d numbers for building the codevectors. \n",
364 codebook_lookup_values);
365 av_dlog(NULL, " delta %f minmum %f \n",
366 codebook_delta_value, codebook_minimum_value);
368 for (i = 0; i < codebook_lookup_values; ++i) {
369 codebook_multiplicands[i] = get_bits(gb, codebook_value_bits);
371 av_dlog(NULL, " multiplicands*delta+minmum : %e \n",
372 (float)codebook_multiplicands[i] * codebook_delta_value + codebook_minimum_value);
373 av_dlog(NULL, " multiplicand %u\n", codebook_multiplicands[i]);
376 // Weed out unused vlcs and build codevector vector
377 codebook_setup->codevectors = used_entries ? av_mallocz(used_entries *
378 codebook_setup->dimensions *
379 sizeof(*codebook_setup->codevectors))
381 for (j = 0, i = 0; i < entries; ++i) {
382 unsigned dim = codebook_setup->dimensions;
384 if (tmp_vlc_bits[i]) {
386 unsigned lookup_offset = i;
388 av_dlog(vc->avccontext, "Lookup offset %u ,", i);
390 for (k = 0; k < dim; ++k) {
391 unsigned multiplicand_offset = lookup_offset % codebook_lookup_values;
392 codebook_setup->codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;
393 if (codebook_sequence_p)
394 last = codebook_setup->codevectors[j * dim + k];
395 lookup_offset/=codebook_lookup_values;
397 tmp_vlc_bits[j] = tmp_vlc_bits[i];
399 av_dlog(vc->avccontext, "real lookup offset %u, vector: ", j);
400 for (k = 0; k < dim; ++k)
401 av_dlog(vc->avccontext, " %f ",
402 codebook_setup->codevectors[j * dim + k]);
403 av_dlog(vc->avccontext, "\n");
408 if (j != used_entries) {
409 av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \n");
410 ret = AVERROR_INVALIDDATA;
413 entries = used_entries;
414 } else if (codebook_setup->lookup_type >= 2) {
415 av_log(vc->avccontext, AV_LOG_ERROR, "Codebook lookup type not supported. \n");
416 ret = AVERROR_INVALIDDATA;
420 // Initialize VLC table
421 if (ff_vorbis_len2vlc(tmp_vlc_bits, tmp_vlc_codes, entries)) {
422 av_log(vc->avccontext, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n");
423 ret = AVERROR_INVALIDDATA;
426 codebook_setup->maxdepth = 0;
427 for (t = 0; t < entries; ++t)
428 if (tmp_vlc_bits[t] >= codebook_setup->maxdepth)
429 codebook_setup->maxdepth = tmp_vlc_bits[t];
431 if (codebook_setup->maxdepth > 3 * V_NB_BITS)
432 codebook_setup->nb_bits = V_NB_BITS2;
434 codebook_setup->nb_bits = V_NB_BITS;
436 codebook_setup->maxdepth = (codebook_setup->maxdepth+codebook_setup->nb_bits - 1) / codebook_setup->nb_bits;
438 if ((ret = init_vlc(&codebook_setup->vlc, codebook_setup->nb_bits,
439 entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits),
440 sizeof(*tmp_vlc_bits), tmp_vlc_codes,
441 sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes),
443 av_log(vc->avccontext, AV_LOG_ERROR, " Error generating vlc tables. \n");
448 av_free(tmp_vlc_bits);
449 av_free(tmp_vlc_codes);
450 av_free(codebook_multiplicands);
455 av_free(tmp_vlc_bits);
456 av_free(tmp_vlc_codes);
457 av_free(codebook_multiplicands);
461 // Process time domain transforms part (unused in Vorbis I)
463 static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc)
465 GetBitContext *gb = &vc->gb;
466 unsigned i, vorbis_time_count = get_bits(gb, 6) + 1;
468 for (i = 0; i < vorbis_time_count; ++i) {
469 unsigned vorbis_tdtransform = get_bits(gb, 16);
471 av_dlog(NULL, " Vorbis time domain transform %u: %u\n",
472 vorbis_time_count, vorbis_tdtransform);
474 if (vorbis_tdtransform) {
475 av_log(vc->avccontext, AV_LOG_ERROR, "Vorbis time domain transform data nonzero. \n");
476 return AVERROR_INVALIDDATA;
482 // Process floors part
484 static int vorbis_floor0_decode(vorbis_context *vc,
485 vorbis_floor_data *vfu, float *vec);
486 static void create_map(vorbis_context *vc, unsigned floor_number);
487 static int vorbis_floor1_decode(vorbis_context *vc,
488 vorbis_floor_data *vfu, float *vec);
489 static int vorbis_parse_setup_hdr_floors(vorbis_context *vc)
491 GetBitContext *gb = &vc->gb;
494 vc->floor_count = get_bits(gb, 6) + 1;
496 vc->floors = av_mallocz(vc->floor_count * sizeof(*vc->floors));
498 for (i = 0; i < vc->floor_count; ++i) {
499 vorbis_floor *floor_setup = &vc->floors[i];
501 floor_setup->floor_type = get_bits(gb, 16);
503 av_dlog(NULL, " %d. floor type %d \n", i, floor_setup->floor_type);
505 if (floor_setup->floor_type == 1) {
506 int maximum_class = -1;
507 unsigned rangebits, rangemax, floor1_values = 2;
509 floor_setup->decode = vorbis_floor1_decode;
511 floor_setup->data.t1.partitions = get_bits(gb, 5);
513 av_dlog(NULL, " %d.floor: %d partitions \n",
514 i, floor_setup->data.t1.partitions);
516 for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
517 floor_setup->data.t1.partition_class[j] = get_bits(gb, 4);
518 if (floor_setup->data.t1.partition_class[j] > maximum_class)
519 maximum_class = floor_setup->data.t1.partition_class[j];
521 av_dlog(NULL, " %d. floor %d partition class %d \n",
522 i, j, floor_setup->data.t1.partition_class[j]);
526 av_dlog(NULL, " maximum class %d \n", maximum_class);
528 for (j = 0; j <= maximum_class; ++j) {
529 floor_setup->data.t1.class_dimensions[j] = get_bits(gb, 3) + 1;
530 floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2);
532 av_dlog(NULL, " %d floor %d class dim: %d subclasses %d \n", i, j,
533 floor_setup->data.t1.class_dimensions[j],
534 floor_setup->data.t1.class_subclasses[j]);
536 if (floor_setup->data.t1.class_subclasses[j]) {
537 GET_VALIDATED_INDEX(floor_setup->data.t1.class_masterbook[j], 8, vc->codebook_count)
539 av_dlog(NULL, " masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]);
542 for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) {
543 int16_t bits = get_bits(gb, 8) - 1;
545 VALIDATE_INDEX(bits, vc->codebook_count)
546 floor_setup->data.t1.subclass_books[j][k] = bits;
548 av_dlog(NULL, " book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]);
552 floor_setup->data.t1.multiplier = get_bits(gb, 2) + 1;
553 floor_setup->data.t1.x_list_dim = 2;
555 for (j = 0; j < floor_setup->data.t1.partitions; ++j)
556 floor_setup->data.t1.x_list_dim+=floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]];
558 floor_setup->data.t1.list = av_mallocz(floor_setup->data.t1.x_list_dim *
559 sizeof(*floor_setup->data.t1.list));
562 rangebits = get_bits(gb, 4);
563 rangemax = (1 << rangebits);
564 if (rangemax > vc->blocksize[1] / 2) {
565 av_log(vc->avccontext, AV_LOG_ERROR,
566 "Floor value is too large for blocksize: %u (%"PRIu32")\n",
567 rangemax, vc->blocksize[1] / 2);
568 return AVERROR_INVALIDDATA;
570 floor_setup->data.t1.list[0].x = 0;
571 floor_setup->data.t1.list[1].x = rangemax;
573 for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
574 for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) {
575 floor_setup->data.t1.list[floor1_values].x = get_bits(gb, rangebits);
577 av_dlog(NULL, " %u. floor1 Y coord. %d\n", floor1_values,
578 floor_setup->data.t1.list[floor1_values].x);
582 // Precalculate order of x coordinates - needed for decode
583 if (ff_vorbis_ready_floor1_list(vc->avccontext,
584 floor_setup->data.t1.list,
585 floor_setup->data.t1.x_list_dim)) {
586 return AVERROR_INVALIDDATA;
588 } else if (floor_setup->floor_type == 0) {
589 unsigned max_codebook_dim = 0;
591 floor_setup->decode = vorbis_floor0_decode;
593 floor_setup->data.t0.order = get_bits(gb, 8);
594 floor_setup->data.t0.rate = get_bits(gb, 16);
595 floor_setup->data.t0.bark_map_size = get_bits(gb, 16);
596 floor_setup->data.t0.amplitude_bits = get_bits(gb, 6);
597 /* zero would result in a div by zero later *
599 if (floor_setup->data.t0.amplitude_bits == 0) {
600 av_log(vc->avccontext, AV_LOG_ERROR,
601 "Floor 0 amplitude bits is 0.\n");
602 return AVERROR_INVALIDDATA;
604 if (floor_setup->data.t0.bark_map_size == 0) {
605 av_log(vc->avccontext, AV_LOG_ERROR, "Floor 0 bark map size is 0.\n");
606 return AVERROR_INVALIDDATA;
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;
630 /* codebook dim is for padding if codebook dim doesn't *
631 * divide order+1 then we need to read more data */
632 floor_setup->data.t0.lsp =
633 av_malloc((floor_setup->data.t0.order + 1 + max_codebook_dim)
634 * sizeof(*floor_setup->data.t0.lsp));
635 if (!floor_setup->data.t0.lsp)
636 return AVERROR(ENOMEM);
638 /* debug output parsed headers */
639 av_dlog(NULL, "floor0 order: %u\n", floor_setup->data.t0.order);
640 av_dlog(NULL, "floor0 rate: %u\n", floor_setup->data.t0.rate);
641 av_dlog(NULL, "floor0 bark map size: %u\n",
642 floor_setup->data.t0.bark_map_size);
643 av_dlog(NULL, "floor0 amplitude bits: %u\n",
644 floor_setup->data.t0.amplitude_bits);
645 av_dlog(NULL, "floor0 amplitude offset: %u\n",
646 floor_setup->data.t0.amplitude_offset);
647 av_dlog(NULL, "floor0 number of books: %u\n",
648 floor_setup->data.t0.num_books);
649 av_dlog(NULL, "floor0 book list pointer: %p\n",
650 floor_setup->data.t0.book_list);
653 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
654 av_dlog(NULL, " Book %d: %u\n", idx + 1,
655 floor_setup->data.t0.book_list[idx]);
659 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid floor type!\n");
660 return AVERROR_INVALIDDATA;
666 // Process residues part
668 static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
670 GetBitContext *gb = &vc->gb;
673 vc->residue_count = get_bits(gb, 6)+1;
674 vc->residues = av_mallocz(vc->residue_count * sizeof(*vc->residues));
676 av_dlog(NULL, " There are %d residues. \n", vc->residue_count);
678 for (i = 0; i < vc->residue_count; ++i) {
679 vorbis_residue *res_setup = &vc->residues[i];
681 unsigned high_bits, low_bits;
683 res_setup->type = get_bits(gb, 16);
685 av_dlog(NULL, " %u. residue type %d\n", i, res_setup->type);
687 res_setup->begin = get_bits(gb, 24);
688 res_setup->end = get_bits(gb, 24);
689 res_setup->partition_size = get_bits(gb, 24) + 1;
690 /* Validations to prevent a buffer overflow later. */
691 if (res_setup->begin>res_setup->end ||
692 res_setup->end > (res_setup->type == 2 ? vc->audio_channels : 1) * vc->blocksize[1] / 2 ||
693 (res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
694 av_log(vc->avccontext, AV_LOG_ERROR,
695 "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n",
696 res_setup->type, res_setup->begin, res_setup->end,
697 res_setup->partition_size, vc->blocksize[1] / 2);
698 return AVERROR_INVALIDDATA;
701 res_setup->classifications = get_bits(gb, 6) + 1;
702 GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
704 res_setup->ptns_to_read =
705 (res_setup->end - res_setup->begin) / res_setup->partition_size;
706 res_setup->classifs = av_malloc(res_setup->ptns_to_read *
708 sizeof(*res_setup->classifs));
709 if (!res_setup->classifs)
710 return AVERROR(ENOMEM);
712 av_dlog(NULL, " begin %d end %d part.size %d classif.s %d classbook %d \n",
713 res_setup->begin, res_setup->end, res_setup->partition_size,
714 res_setup->classifications, res_setup->classbook);
716 for (j = 0; j < res_setup->classifications; ++j) {
718 low_bits = get_bits(gb, 3);
720 high_bits = get_bits(gb, 5);
721 cascade[j] = (high_bits << 3) + low_bits;
723 av_dlog(NULL, " %u class cascade depth: %d\n", j, ilog(cascade[j]));
726 res_setup->maxpass = 0;
727 for (j = 0; j < res_setup->classifications; ++j) {
728 for (k = 0; k < 8; ++k) {
729 if (cascade[j]&(1 << k)) {
730 GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
732 av_dlog(NULL, " %u class cascade depth %u book: %d\n",
733 j, k, res_setup->books[j][k]);
735 if (k>res_setup->maxpass)
736 res_setup->maxpass = k;
738 res_setup->books[j][k] = -1;
746 // Process mappings part
748 static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
750 GetBitContext *gb = &vc->gb;
753 vc->mapping_count = get_bits(gb, 6)+1;
754 vc->mappings = av_mallocz(vc->mapping_count * sizeof(*vc->mappings));
756 av_dlog(NULL, " There are %d mappings. \n", vc->mapping_count);
758 for (i = 0; i < vc->mapping_count; ++i) {
759 vorbis_mapping *mapping_setup = &vc->mappings[i];
761 if (get_bits(gb, 16)) {
762 av_log(vc->avccontext, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
763 return AVERROR_INVALIDDATA;
766 mapping_setup->submaps = get_bits(gb, 4) + 1;
768 mapping_setup->submaps = 1;
772 mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
773 mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps *
774 sizeof(*mapping_setup->magnitude));
775 mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps *
776 sizeof(*mapping_setup->angle));
777 for (j = 0; j < mapping_setup->coupling_steps; ++j) {
778 GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
779 GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels)
782 mapping_setup->coupling_steps = 0;
785 av_dlog(NULL, " %u mapping coupling steps: %d\n",
786 i, mapping_setup->coupling_steps);
788 if (get_bits(gb, 2)) {
789 av_log(vc->avccontext, AV_LOG_ERROR, "%u. mapping setup data invalid.\n", i);
790 return AVERROR_INVALIDDATA; // following spec.
793 if (mapping_setup->submaps>1) {
794 mapping_setup->mux = av_mallocz(vc->audio_channels *
795 sizeof(*mapping_setup->mux));
796 for (j = 0; j < vc->audio_channels; ++j)
797 mapping_setup->mux[j] = get_bits(gb, 4);
800 for (j = 0; j < mapping_setup->submaps; ++j) {
801 skip_bits(gb, 8); // FIXME check?
802 GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count)
803 GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
805 av_dlog(NULL, " %u mapping %u submap : floor %d, residue %d\n", i, j,
806 mapping_setup->submap_floor[j],
807 mapping_setup->submap_residue[j]);
813 // Process modes part
815 static void create_map(vorbis_context *vc, unsigned floor_number)
817 vorbis_floor *floors = vc->floors;
823 for (blockflag = 0; blockflag < 2; ++blockflag) {
824 n = vc->blocksize[blockflag] / 2;
825 floors[floor_number].data.t0.map[blockflag] =
826 av_malloc((n + 1) * sizeof(int32_t)); // n + sentinel
828 map = floors[floor_number].data.t0.map[blockflag];
829 vf = &floors[floor_number].data.t0;
831 for (idx = 0; idx < n; ++idx) {
832 map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
833 (vf->bark_map_size / BARK(vf->rate / 2.0f)));
834 if (vf->bark_map_size-1 < map[idx])
835 map[idx] = vf->bark_map_size - 1;
838 vf->map_size[blockflag] = n;
841 for (idx = 0; idx <= n; ++idx) {
842 av_dlog(NULL, "floor0 map: map at pos %d is %d\n", idx, map[idx]);
846 static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
848 GetBitContext *gb = &vc->gb;
851 vc->mode_count = get_bits(gb, 6) + 1;
852 vc->modes = av_mallocz(vc->mode_count * sizeof(*vc->modes));
854 av_dlog(NULL, " There are %d modes.\n", vc->mode_count);
856 for (i = 0; i < vc->mode_count; ++i) {
857 vorbis_mode *mode_setup = &vc->modes[i];
859 mode_setup->blockflag = get_bits1(gb);
860 mode_setup->windowtype = get_bits(gb, 16); //FIXME check
861 mode_setup->transformtype = get_bits(gb, 16); //FIXME check
862 GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
864 av_dlog(NULL, " %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
865 i, mode_setup->blockflag, mode_setup->windowtype,
866 mode_setup->transformtype, mode_setup->mapping);
871 // Process the whole setup header using the functions above
873 static int vorbis_parse_setup_hdr(vorbis_context *vc)
875 GetBitContext *gb = &vc->gb;
878 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
879 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
880 (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
881 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
882 return AVERROR_INVALIDDATA;
885 if ((ret = vorbis_parse_setup_hdr_codebooks(vc))) {
886 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
889 if ((ret = vorbis_parse_setup_hdr_tdtransforms(vc))) {
890 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
893 if ((ret = vorbis_parse_setup_hdr_floors(vc))) {
894 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
897 if ((ret = vorbis_parse_setup_hdr_residues(vc))) {
898 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
901 if ((ret = vorbis_parse_setup_hdr_mappings(vc))) {
902 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
905 if ((ret = vorbis_parse_setup_hdr_modes(vc))) {
906 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
909 if (!get_bits1(gb)) {
910 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
911 return AVERROR_INVALIDDATA; // framing flag bit unset error
917 // Process the identification header
919 static int vorbis_parse_id_hdr(vorbis_context *vc)
921 GetBitContext *gb = &vc->gb;
924 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
925 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
926 (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
927 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
928 return AVERROR_INVALIDDATA;
931 vc->version = get_bits_long(gb, 32); //FIXME check 0
932 vc->audio_channels = get_bits(gb, 8);
933 if (vc->audio_channels <= 0) {
934 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid number of channels\n");
935 return AVERROR_INVALIDDATA;
937 vc->audio_samplerate = get_bits_long(gb, 32);
938 if (vc->audio_samplerate <= 0) {
939 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid samplerate\n");
940 return AVERROR_INVALIDDATA;
942 vc->bitrate_maximum = get_bits_long(gb, 32);
943 vc->bitrate_nominal = get_bits_long(gb, 32);
944 vc->bitrate_minimum = get_bits_long(gb, 32);
945 bl0 = get_bits(gb, 4);
946 bl1 = get_bits(gb, 4);
947 if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
948 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
949 return AVERROR_INVALIDDATA;
951 vc->blocksize[0] = (1 << bl0);
952 vc->blocksize[1] = (1 << bl1);
953 vc->win[0] = ff_vorbis_vwin[bl0 - 6];
954 vc->win[1] = ff_vorbis_vwin[bl1 - 6];
956 if ((get_bits1(gb)) == 0) {
957 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
958 return AVERROR_INVALIDDATA;
961 vc->channel_residues = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(*vc->channel_residues));
962 vc->saved = av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(*vc->saved));
963 vc->previous_window = 0;
965 ff_mdct_init(&vc->mdct[0], bl0, 1, -1.0);
966 ff_mdct_init(&vc->mdct[1], bl1, 1, -1.0);
968 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 ",
969 vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
972 BLK = vc->blocksize[0];
973 for (i = 0; i < BLK / 2; ++i) {
974 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)));
981 // Process the extradata using the functions above (identification header, setup header)
983 static av_cold int vorbis_decode_init(AVCodecContext *avccontext)
985 vorbis_context *vc = avccontext->priv_data;
986 uint8_t *headers = avccontext->extradata;
987 int headers_len = avccontext->extradata_size;
988 uint8_t *header_start[3];
990 GetBitContext *gb = &vc->gb;
993 vc->avccontext = avccontext;
994 ff_dsputil_init(&vc->dsp, avccontext);
995 avpriv_float_dsp_init(&vc->fdsp, avccontext->flags & CODEC_FLAG_BITEXACT);
996 ff_fmt_convert_init(&vc->fmt_conv, avccontext);
998 avccontext->sample_fmt = AV_SAMPLE_FMT_FLTP;
1001 av_log(avccontext, AV_LOG_ERROR, "Extradata missing.\n");
1002 return AVERROR_INVALIDDATA;
1005 if ((ret = avpriv_split_xiph_headers(headers, headers_len, 30, header_start, header_len)) < 0) {
1006 av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
1010 init_get_bits(gb, header_start[0], header_len[0]*8);
1011 hdr_type = get_bits(gb, 8);
1012 if (hdr_type != 1) {
1013 av_log(avccontext, AV_LOG_ERROR, "First header is not the id header.\n");
1014 return AVERROR_INVALIDDATA;
1016 if ((ret = vorbis_parse_id_hdr(vc))) {
1017 av_log(avccontext, AV_LOG_ERROR, "Id header corrupt.\n");
1022 init_get_bits(gb, header_start[2], header_len[2]*8);
1023 hdr_type = get_bits(gb, 8);
1024 if (hdr_type != 5) {
1025 av_log(avccontext, AV_LOG_ERROR, "Third header is not the setup header.\n");
1027 return AVERROR_INVALIDDATA;
1029 if ((ret = vorbis_parse_setup_hdr(vc))) {
1030 av_log(avccontext, AV_LOG_ERROR, "Setup header corrupt.\n");
1035 if (vc->audio_channels > 8)
1036 avccontext->channel_layout = 0;
1038 avccontext->channel_layout = ff_vorbis_channel_layouts[vc->audio_channels - 1];
1040 avccontext->channels = vc->audio_channels;
1041 avccontext->sample_rate = vc->audio_samplerate;
1043 avcodec_get_frame_defaults(&vc->frame);
1044 avccontext->coded_frame = &vc->frame;
1049 // Decode audiopackets -------------------------------------------------
1051 // Read and decode floor
1053 static int vorbis_floor0_decode(vorbis_context *vc,
1054 vorbis_floor_data *vfu, float *vec)
1056 vorbis_floor0 *vf = &vfu->t0;
1057 float *lsp = vf->lsp;
1058 unsigned amplitude, book_idx;
1059 unsigned blockflag = vc->modes[vc->mode_number].blockflag;
1061 amplitude = get_bits(&vc->gb, vf->amplitude_bits);
1062 if (amplitude > 0) {
1064 unsigned idx, lsp_len = 0;
1065 vorbis_codebook codebook;
1067 book_idx = get_bits(&vc->gb, ilog(vf->num_books));
1068 if (book_idx >= vf->num_books) {
1069 av_log(vc->avccontext, AV_LOG_ERROR,
1070 "floor0 dec: booknumber too high!\n");
1073 av_dlog(NULL, "floor0 dec: booknumber: %u\n", book_idx);
1074 codebook = vc->codebooks[vf->book_list[book_idx]];
1075 /* Invalid codebook! */
1076 if (!codebook.codevectors)
1077 return AVERROR_INVALIDDATA;
1079 while (lsp_len<vf->order) {
1082 av_dlog(NULL, "floor0 dec: book dimension: %d\n", codebook.dimensions);
1083 av_dlog(NULL, "floor0 dec: maximum depth: %d\n", codebook.maxdepth);
1084 /* read temp vector */
1085 vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
1086 codebook.nb_bits, codebook.maxdepth)
1087 * codebook.dimensions;
1088 av_dlog(NULL, "floor0 dec: vector offset: %d\n", vec_off);
1089 /* copy each vector component and add last to it */
1090 for (idx = 0; idx < codebook.dimensions; ++idx)
1091 lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
1092 last = lsp[lsp_len+idx-1]; /* set last to last vector component */
1094 lsp_len += codebook.dimensions;
1096 /* DEBUG: output lsp coeffs */
1099 for (idx = 0; idx < lsp_len; ++idx)
1100 av_dlog(NULL, "floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
1103 /* synthesize floor output vector */
1106 int order = vf->order;
1107 float wstep = M_PI / vf->bark_map_size;
1109 for (i = 0; i < order; i++)
1110 lsp[i] = 2.0f * cos(lsp[i]);
1112 av_dlog(NULL, "floor0 synth: map_size = %"PRIu32"; m = %d; wstep = %f\n",
1113 vf->map_size[blockflag], order, wstep);
1116 while (i < vf->map_size[blockflag]) {
1117 int j, iter_cond = vf->map[blockflag][i];
1120 float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times
1122 /* similar part for the q and p products */
1123 for (j = 0; j + 1 < order; j += 2) {
1124 q *= lsp[j] - two_cos_w;
1125 p *= lsp[j + 1] - two_cos_w;
1127 if (j == order) { // even order
1128 p *= p * (2.0f - two_cos_w);
1129 q *= q * (2.0f + two_cos_w);
1130 } else { // odd order
1131 q *= two_cos_w-lsp[j]; // one more time for q
1133 /* final step and square */
1134 p *= p * (4.f - two_cos_w * two_cos_w);
1138 /* calculate linear floor value */
1139 q = exp((((amplitude*vf->amplitude_offset) /
1140 (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
1141 - vf->amplitude_offset) * .11512925f);
1146 } while (vf->map[blockflag][i] == iter_cond);
1150 /* this channel is unused */
1154 av_dlog(NULL, " Floor0 decoded\n");
1159 static int vorbis_floor1_decode(vorbis_context *vc,
1160 vorbis_floor_data *vfu, float *vec)
1162 vorbis_floor1 *vf = &vfu->t1;
1163 GetBitContext *gb = &vc->gb;
1164 uint16_t range_v[4] = { 256, 128, 86, 64 };
1165 unsigned range = range_v[vf->multiplier - 1];
1166 uint16_t floor1_Y[258];
1167 uint16_t floor1_Y_final[258];
1168 int floor1_flag[258];
1169 unsigned partition_class, cdim, cbits, csub, cval, offset, i, j;
1170 int book, adx, ady, dy, off, predicted, err;
1173 if (!get_bits1(gb)) // silence
1176 // Read values (or differences) for the floor's points
1178 floor1_Y[0] = get_bits(gb, ilog(range - 1));
1179 floor1_Y[1] = get_bits(gb, ilog(range - 1));
1181 av_dlog(NULL, "floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
1184 for (i = 0; i < vf->partitions; ++i) {
1185 partition_class = vf->partition_class[i];
1186 cdim = vf->class_dimensions[partition_class];
1187 cbits = vf->class_subclasses[partition_class];
1188 csub = (1 << cbits) - 1;
1191 av_dlog(NULL, "Cbits %u\n", cbits);
1193 if (cbits) // this reads all subclasses for this partition's class
1194 cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[partition_class]].vlc.table,
1195 vc->codebooks[vf->class_masterbook[partition_class]].nb_bits, 3);
1197 for (j = 0; j < cdim; ++j) {
1198 book = vf->subclass_books[partition_class][cval & csub];
1200 av_dlog(NULL, "book %d Cbits %u cval %u bits:%d\n",
1201 book, cbits, cval, get_bits_count(gb));
1203 cval = cval >> cbits;
1205 floor1_Y[offset+j] = get_vlc2(gb, vc->codebooks[book].vlc.table,
1206 vc->codebooks[book].nb_bits, 3);
1208 floor1_Y[offset+j] = 0;
1211 av_dlog(NULL, " floor(%d) = %d \n",
1212 vf->list[offset+j].x, floor1_Y[offset+j]);
1217 // Amplitude calculation from the differences
1221 floor1_Y_final[0] = floor1_Y[0];
1222 floor1_Y_final[1] = floor1_Y[1];
1224 for (i = 2; i < vf->x_list_dim; ++i) {
1225 unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
1227 low_neigh_offs = vf->list[i].low;
1228 high_neigh_offs = vf->list[i].high;
1229 dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs]; // render_point begin
1230 adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
1232 err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
1235 predicted = floor1_Y_final[low_neigh_offs] - off;
1237 predicted = floor1_Y_final[low_neigh_offs] + off;
1238 } // render_point end
1241 highroom = range-predicted;
1242 lowroom = predicted;
1243 if (highroom < lowroom) {
1244 room = highroom * 2;
1246 room = lowroom * 2; // SPEC misspelling
1249 floor1_flag[low_neigh_offs] = 1;
1250 floor1_flag[high_neigh_offs] = 1;
1253 if (highroom > lowroom) {
1254 floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
1256 floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
1260 floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
1262 floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
1267 floor1_Y_final[i] = av_clip_uint16(predicted);
1270 av_dlog(NULL, " Decoded floor(%d) = %u / val %u\n",
1271 vf->list[i].x, floor1_Y_final[i], val);
1274 // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ?
1276 ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x);
1278 av_dlog(NULL, " Floor decoded\n");
1283 // Read and decode residue
1285 static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc,
1288 uint8_t *do_not_decode,
1294 GetBitContext *gb = &vc->gb;
1295 unsigned c_p_c = vc->codebooks[vr->classbook].dimensions;
1296 unsigned ptns_to_read = vr->ptns_to_read;
1297 uint8_t *classifs = vr->classifs;
1298 unsigned pass, ch_used, i, j, k, l;
1299 unsigned max_output = (ch - 1) * vlen;
1302 for (j = 1; j < ch; ++j)
1303 do_not_decode[0] &= do_not_decode[j]; // FIXME - clobbering input
1304 if (do_not_decode[0])
1307 max_output += vr->end / ch;
1310 max_output += vr->end;
1313 if (max_output > ch_left * vlen) {
1314 av_log(vc->avccontext, AV_LOG_ERROR, "Insufficient output buffer\n");
1318 av_dlog(NULL, " residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
1320 for (pass = 0; pass <= vr->maxpass; ++pass) { // FIXME OPTIMIZE?
1321 uint16_t voffset, partition_count, j_times_ptns_to_read;
1323 voffset = vr->begin;
1324 for (partition_count = 0; partition_count < ptns_to_read;) { // SPEC error
1326 unsigned inverse_class = ff_inverse[vr->classifications];
1327 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1328 if (!do_not_decode[j]) {
1329 unsigned temp = get_vlc2(gb, vc->codebooks[vr->classbook].vlc.table,
1330 vc->codebooks[vr->classbook].nb_bits, 3);
1332 av_dlog(NULL, "Classword: %u\n", temp);
1334 av_assert0(vr->classifications > 1 && temp <= 65536); //needed for inverse[]
1335 for (i = 0; i < c_p_c; ++i) {
1338 temp2 = (((uint64_t)temp) * inverse_class) >> 32;
1339 if (partition_count + c_p_c - 1 - i < ptns_to_read)
1340 classifs[j_times_ptns_to_read + partition_count + c_p_c - 1 - i] = temp - temp2 * vr->classifications;
1344 j_times_ptns_to_read += ptns_to_read;
1347 for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
1348 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1351 if (!do_not_decode[j]) {
1352 unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
1353 int vqbook = vr->books[vqclass][pass];
1355 if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
1357 unsigned dim = vc->codebooks[vqbook].dimensions;
1358 unsigned step = FASTDIV(vr->partition_size << 1, dim << 1);
1359 vorbis_codebook codebook = vc->codebooks[vqbook];
1363 voffs = voffset+j*vlen;
1364 for (k = 0; k < step; ++k) {
1365 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
1366 for (l = 0; l < dim; ++l)
1367 vec[voffs + k + l * step] += codebook.codevectors[coffs + l];
1369 } else if (vr_type == 1) {
1370 voffs = voffset + j * vlen;
1371 for (k = 0; k < step; ++k) {
1372 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
1373 for (l = 0; l < dim; ++l, ++voffs) {
1374 vec[voffs]+=codebook.codevectors[coffs+l];
1376 av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
1377 pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
1380 } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) { // most frequent case optimized
1381 voffs = voffset >> 1;
1384 for (k = 0; k < step; ++k) {
1385 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 2;
1386 vec[voffs + k ] += codebook.codevectors[coffs ];
1387 vec[voffs + k + vlen] += codebook.codevectors[coffs + 1];
1389 } else if (dim == 4) {
1390 for (k = 0; k < step; ++k, voffs += 2) {
1391 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 4;
1392 vec[voffs ] += codebook.codevectors[coffs ];
1393 vec[voffs + 1 ] += codebook.codevectors[coffs + 2];
1394 vec[voffs + vlen ] += codebook.codevectors[coffs + 1];
1395 vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3];
1398 for (k = 0; k < step; ++k) {
1399 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
1400 for (l = 0; l < dim; l += 2, voffs++) {
1401 vec[voffs ] += codebook.codevectors[coffs + l ];
1402 vec[voffs + vlen] += codebook.codevectors[coffs + l + 1];
1404 av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1405 pass, voffset / ch + (voffs % ch) * vlen,
1406 vec[voffset / ch + (voffs % ch) * vlen],
1407 codebook.codevectors[coffs + l], coffs, l);
1411 } else if (vr_type == 2) {
1412 unsigned voffs_div = FASTDIV(voffset << 1, ch <<1);
1413 unsigned voffs_mod = voffset - voffs_div * ch;
1415 for (k = 0; k < step; ++k) {
1416 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
1417 for (l = 0; l < dim; ++l) {
1418 vec[voffs_div + voffs_mod * vlen] +=
1419 codebook.codevectors[coffs + l];
1421 av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1422 pass, voffs_div + voffs_mod * vlen,
1423 vec[voffs_div + voffs_mod * vlen],
1424 codebook.codevectors[coffs + l], coffs, l);
1426 if (++voffs_mod == ch) {
1435 j_times_ptns_to_read += ptns_to_read;
1438 voffset += vr->partition_size;
1445 static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr,
1447 uint8_t *do_not_decode,
1448 float *vec, unsigned vlen,
1452 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2);
1453 else if (vr->type == 1)
1454 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1);
1455 else if (vr->type == 0)
1456 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 0);
1458 av_log(vc->avccontext, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
1459 return AVERROR_INVALIDDATA;
1463 void ff_vorbis_inverse_coupling(float *mag, float *ang, int blocksize)
1466 for (i = 0; i < blocksize; i++) {
1469 ang[i] = mag[i] - ang[i];
1471 float temp = ang[i];
1479 float temp = ang[i];
1487 // Decode the audio packet using the functions above
1489 static int vorbis_parse_audio_packet(vorbis_context *vc, float **floor_ptr)
1491 GetBitContext *gb = &vc->gb;
1493 unsigned previous_window = vc->previous_window;
1494 unsigned mode_number, blockflag, blocksize;
1496 uint8_t no_residue[255];
1497 uint8_t do_not_decode[255];
1498 vorbis_mapping *mapping;
1499 float *ch_res_ptr = vc->channel_residues;
1500 uint8_t res_chan[255];
1501 unsigned res_num = 0;
1503 unsigned ch_left = vc->audio_channels;
1506 if (get_bits1(gb)) {
1507 av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
1508 return AVERROR_INVALIDDATA; // packet type not audio
1511 if (vc->mode_count == 1) {
1514 GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
1516 vc->mode_number = mode_number;
1517 mapping = &vc->mappings[vc->modes[mode_number].mapping];
1519 av_dlog(NULL, " Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
1520 vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
1522 blockflag = vc->modes[mode_number].blockflag;
1523 blocksize = vc->blocksize[blockflag];
1524 vlen = blocksize / 2;
1526 previous_window = get_bits(gb, 1);
1527 skip_bits1(gb); // next_window
1530 memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
1531 for (i = 0; i < vc->audio_channels; ++i)
1532 memset(floor_ptr[i], 0, vlen * sizeof(floor_ptr[0][0])); //FIXME can this be removed ?
1536 for (i = 0; i < vc->audio_channels; ++i) {
1537 vorbis_floor *floor;
1539 if (mapping->submaps > 1) {
1540 floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
1542 floor = &vc->floors[mapping->submap_floor[0]];
1545 ret = floor->decode(vc, &floor->data, floor_ptr[i]);
1548 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
1549 return AVERROR_INVALIDDATA;
1551 no_residue[i] = ret;
1554 // Nonzero vector propagate
1556 for (i = mapping->coupling_steps - 1; i >= 0; --i) {
1557 if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
1558 no_residue[mapping->magnitude[i]] = 0;
1559 no_residue[mapping->angle[i]] = 0;
1565 for (i = 0; i < mapping->submaps; ++i) {
1566 vorbis_residue *residue;
1570 for (j = 0; j < vc->audio_channels; ++j) {
1571 if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
1572 res_chan[j] = res_num;
1573 if (no_residue[j]) {
1574 do_not_decode[ch] = 1;
1576 do_not_decode[ch] = 0;
1582 residue = &vc->residues[mapping->submap_residue[i]];
1584 av_log(vc->avccontext, AV_LOG_ERROR, "Too many channels in vorbis_floor_decode.\n");
1588 ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left);
1593 ch_res_ptr += ch * vlen;
1598 return AVERROR_INVALIDDATA;
1602 for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed
1605 mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2;
1606 ang = vc->channel_residues+res_chan[mapping->angle[i]] * blocksize / 2;
1607 vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
1612 mdct = &vc->mdct[blockflag];
1614 for (j = vc->audio_channels-1;j >= 0; j--) {
1615 ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
1616 vc->fdsp.vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
1617 mdct->imdct_half(mdct, ch_res_ptr, floor_ptr[j]);
1620 // Overlap/add, save data for next overlapping
1622 retlen = (blocksize + vc->blocksize[previous_window]) / 4;
1623 for (j = 0; j < vc->audio_channels; j++) {
1624 unsigned bs0 = vc->blocksize[0];
1625 unsigned bs1 = vc->blocksize[1];
1626 float *residue = vc->channel_residues + res_chan[j] * blocksize / 2;
1627 float *saved = vc->saved + j * bs1 / 4;
1628 float *ret = floor_ptr[j];
1629 float *buf = residue;
1630 const float *win = vc->win[blockflag & previous_window];
1632 if (blockflag == previous_window) {
1633 vc->dsp.vector_fmul_window(ret, saved, buf, win, blocksize / 4);
1634 } else if (blockflag > previous_window) {
1635 vc->dsp.vector_fmul_window(ret, saved, buf, win, bs0 / 4);
1636 memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) * sizeof(float));
1638 memcpy(ret, saved, ((bs1 - bs0) / 4) * sizeof(float));
1639 vc->dsp.vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, bs0 / 4);
1641 memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
1644 vc->previous_window = blockflag;
1648 // Return the decoded audio packet through the standard api
1650 static int vorbis_decode_frame(AVCodecContext *avccontext, void *data,
1651 int *got_frame_ptr, AVPacket *avpkt)
1653 const uint8_t *buf = avpkt->data;
1654 int buf_size = avpkt->size;
1655 vorbis_context *vc = avccontext->priv_data;
1656 GetBitContext *gb = &vc->gb;
1657 float *channel_ptrs[255];
1660 av_dlog(NULL, "packet length %d \n", buf_size);
1662 /* get output buffer */
1663 vc->frame.nb_samples = vc->blocksize[1] / 2;
1664 if ((ret = ff_get_buffer(avccontext, &vc->frame)) < 0) {
1665 av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
1669 if (vc->audio_channels > 8) {
1670 for (i = 0; i < vc->audio_channels; i++)
1671 channel_ptrs[i] = (float *)vc->frame.extended_data[i];
1673 for (i = 0; i < vc->audio_channels; i++) {
1674 int ch = ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
1675 channel_ptrs[ch] = (float *)vc->frame.extended_data[i];
1679 init_get_bits(gb, buf, buf_size*8);
1681 if ((len = vorbis_parse_audio_packet(vc, channel_ptrs)) <= 0)
1684 if (!vc->first_frame) {
1685 vc->first_frame = 1;
1690 av_dlog(NULL, "parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
1691 get_bits_count(gb) / 8, get_bits_count(gb) % 8, len);
1693 vc->frame.nb_samples = len;
1695 *(AVFrame *)data = vc->frame;
1702 static av_cold int vorbis_decode_close(AVCodecContext *avccontext)
1704 vorbis_context *vc = avccontext->priv_data;
1711 static av_cold void vorbis_decode_flush(AVCodecContext *avccontext)
1713 vorbis_context *vc = avccontext->priv_data;
1716 memset(vc->saved, 0, (vc->blocksize[1] / 4) * vc->audio_channels *
1717 sizeof(*vc->saved));
1719 vc->previous_window = 0;
1722 AVCodec ff_vorbis_decoder = {
1724 .type = AVMEDIA_TYPE_AUDIO,
1725 .id = AV_CODEC_ID_VORBIS,
1726 .priv_data_size = sizeof(vorbis_context),
1727 .init = vorbis_decode_init,
1728 .close = vorbis_decode_close,
1729 .decode = vorbis_decode_frame,
1730 .flush = vorbis_decode_flush,
1731 .capabilities = CODEC_CAP_DR1,
1732 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
1733 .channel_layouts = ff_vorbis_channel_layouts,
1734 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1735 AV_SAMPLE_FMT_NONE },