FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vorbisdec.c
Go to the documentation of this file.
1 /**
2  * @file
3  * Vorbis I decoder
4  * @author Denes Balatoni ( dbalatoni programozo hu )
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * Vorbis I decoder
26  * @author Denes Balatoni ( dbalatoni programozo hu )
27  */
28 
29 #include <inttypes.h>
30 #include <math.h>
31 
32 #define BITSTREAM_READER_LE
33 #include "libavutil/float_dsp.h"
34 #include "libavutil/avassert.h"
35 #include "avcodec.h"
36 #include "get_bits.h"
37 #include "dsputil.h"
38 #include "fft.h"
39 #include "fmtconvert.h"
40 #include "internal.h"
41 
42 #include "vorbis.h"
43 #include "xiph.h"
44 
45 #define V_NB_BITS 8
46 #define V_NB_BITS2 11
47 #define V_MAX_VLCS (1 << 16)
48 #define V_MAX_PARTITIONS (1 << 20)
49 
50 typedef struct {
55  float *codevectors;
56  unsigned int nb_bits;
58 
59 typedef union vorbis_floor_u vorbis_floor_data;
60 typedef struct vorbis_floor0_s vorbis_floor0;
61 typedef struct vorbis_floor1_s vorbis_floor1;
62 struct vorbis_context_s;
63 typedef
65  (struct vorbis_context_s *, vorbis_floor_data *, float *);
66 typedef struct {
70  struct vorbis_floor0_s {
72  uint16_t rate;
73  uint16_t bark_map_size;
74  int32_t *map[2];
75  uint32_t map_size[2];
80  float *lsp;
81  } t0;
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];
90  uint16_t x_list_dim;
92  } t1;
93  } data;
94 } vorbis_floor;
95 
96 typedef struct {
97  uint16_t type;
98  uint32_t begin;
99  uint32_t end;
100  unsigned partition_size;
103  int16_t books[64][8];
105  uint16_t ptns_to_read;
108 
109 typedef struct {
111  uint16_t coupling_steps;
115  uint8_t submap_floor[16];
116  uint8_t submap_residue[16];
118 
119 typedef struct {
121  uint16_t windowtype;
122  uint16_t transformtype;
124 } vorbis_mode;
125 
126 typedef struct vorbis_context_s {
133 
134  FFTContext mdct[2];
136  uint32_t version;
139  uint32_t bitrate_maximum;
140  uint32_t bitrate_nominal;
141  uint32_t bitrate_minimum;
142  uint32_t blocksize[2];
143  const float *win[2];
144  uint16_t codebook_count;
154  uint8_t mode_number; // mode number for the current packet
157  float *saved;
159 
160 /* Helper functions */
161 
162 #define BARK(x) \
163  (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x))
164 
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) \
167  if (idx >= limit) {\
168  av_log(vc->avccontext, AV_LOG_ERROR,\
169  idx_err_str,\
170  (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\
171  return AVERROR_INVALIDDATA;\
172  }
173 #define GET_VALIDATED_INDEX(idx, bits, limit) \
174  {\
175  idx = get_bits(gb, bits);\
176  VALIDATE_INDEX(idx, limit)\
177  }
178 
179 static float vorbisfloat2float(unsigned val)
180 {
181  double mant = val & 0x1fffff;
182  long exp = (val & 0x7fe00000L) >> 21;
183  if (val & 0x80000000)
184  mant = -mant;
185  return ldexp(mant, exp - 20 - 768);
186 }
187 
188 
189 // Free all allocated memory -----------------------------------------
190 
191 static void vorbis_free(vorbis_context *vc)
192 {
193  int i;
194 
196  av_freep(&vc->saved);
197 
198  if (vc->residues)
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);
203 
204  ff_mdct_end(&vc->mdct[0]);
205  ff_mdct_end(&vc->mdct[1]);
206 
207  if (vc->codebooks)
208  for (i = 0; i < vc->codebook_count; ++i) {
209  av_free(vc->codebooks[i].codevectors);
210  ff_free_vlc(&vc->codebooks[i].vlc);
211  }
212  av_freep(&vc->codebooks);
213 
214  if (vc->floors)
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);
221  } else {
222  av_free(vc->floors[i].data.t1.list);
223  }
224  }
225  av_freep(&vc->floors);
226 
227  if (vc->mappings)
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);
232  }
233  av_freep(&vc->mappings);
234 }
235 
236 // Parse setup header -------------------------------------------------
237 
238 // Process codebooks part
239 
241 {
242  unsigned cb;
243  uint8_t *tmp_vlc_bits;
244  uint32_t *tmp_vlc_codes;
245  GetBitContext *gb = &vc->gb;
246  uint16_t *codebook_multiplicands;
247  int ret = 0;
248 
249  vc->codebook_count = get_bits(gb, 8) + 1;
250 
251  av_dlog(NULL, " Codebooks: %d \n", vc->codebook_count);
252 
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));
257 
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;
261 
262  av_dlog(NULL, " %u. Codebook\n", cb);
263 
264  if (get_bits(gb, 24) != 0x564342) {
266  " %u. Codebook setup data corrupt.\n", cb);
267  ret = AVERROR_INVALIDDATA;
268  goto error;
269  }
270 
271  codebook_setup->dimensions=get_bits(gb, 16);
272  if (codebook_setup->dimensions > 16 || codebook_setup->dimensions == 0) {
274  " %u. Codebook's dimension is invalid (%d).\n",
275  cb, codebook_setup->dimensions);
276  ret = AVERROR_INVALIDDATA;
277  goto error;
278  }
279  entries = get_bits(gb, 24);
280  if (entries > V_MAX_VLCS) {
282  " %u. Codebook has too many entries (%u).\n",
283  cb, entries);
284  ret = AVERROR_INVALIDDATA;
285  goto error;
286  }
287 
288  ordered = get_bits1(gb);
289 
290  av_dlog(NULL, " codebook_dimensions %d, codebook_entries %u\n",
291  codebook_setup->dimensions, entries);
292 
293  if (!ordered) {
294  unsigned ce, flag;
295  unsigned sparse = get_bits1(gb);
296 
297  av_dlog(NULL, " not ordered \n");
298 
299  if (sparse) {
300  av_dlog(NULL, " sparse \n");
301 
302  used_entries = 0;
303  for (ce = 0; ce < entries; ++ce) {
304  flag = get_bits1(gb);
305  if (flag) {
306  tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
307  ++used_entries;
308  } else
309  tmp_vlc_bits[ce] = 0;
310  }
311  } else {
312  av_dlog(NULL, " not sparse \n");
313 
314  used_entries = entries;
315  for (ce = 0; ce < entries; ++ce)
316  tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
317  }
318  } else {
319  unsigned current_entry = 0;
320  unsigned current_length = get_bits(gb, 5) + 1;
321 
322  av_dlog(NULL, " ordered, current length: %u\n", current_length); //FIXME
323 
324  used_entries = entries;
325  for (; current_entry < used_entries && current_length <= 32; ++current_length) {
326  unsigned i, number;
327 
328  av_dlog(NULL, " number bits: %u ", ilog(entries - current_entry));
329 
330  number = get_bits(gb, ilog(entries - current_entry));
331 
332  av_dlog(NULL, " number: %u\n", number);
333 
334  for (i = current_entry; i < number+current_entry; ++i)
335  if (i < used_entries)
336  tmp_vlc_bits[i] = current_length;
337 
338  current_entry+=number;
339  }
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;
343  goto error;
344  }
345  }
346 
347  codebook_setup->lookup_type = get_bits(gb, 4);
348 
349  av_dlog(NULL, " lookup type: %d : %s \n", codebook_setup->lookup_type,
350  codebook_setup->lookup_type ? "vq" : "no lookup");
351 
352 // If the codebook is used for (inverse) VQ, calculate codevectors.
353 
354  if (codebook_setup->lookup_type == 1) {
355  unsigned i, j, k;
356  unsigned codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions);
357 
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);
362 
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);
367 
368  for (i = 0; i < codebook_lookup_values; ++i) {
369  codebook_multiplicands[i] = get_bits(gb, codebook_value_bits);
370 
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]);
374  }
375 
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))
380  : NULL;
381  for (j = 0, i = 0; i < entries; ++i) {
382  unsigned dim = codebook_setup->dimensions;
383 
384  if (tmp_vlc_bits[i]) {
385  float last = 0.0;
386  unsigned lookup_offset = i;
387 
388  av_dlog(vc->avccontext, "Lookup offset %u ,", i);
389 
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;
396  }
397  tmp_vlc_bits[j] = tmp_vlc_bits[i];
398 
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");
404 
405  ++j;
406  }
407  }
408  if (j != used_entries) {
409  av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \n");
410  ret = AVERROR_INVALIDDATA;
411  goto error;
412  }
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;
417  goto error;
418  }
419 
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;
424  goto error;
425  }
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];
430 
431  if (codebook_setup->maxdepth > 3 * V_NB_BITS)
432  codebook_setup->nb_bits = V_NB_BITS2;
433  else
434  codebook_setup->nb_bits = V_NB_BITS;
435 
436  codebook_setup->maxdepth = (codebook_setup->maxdepth+codebook_setup->nb_bits - 1) / codebook_setup->nb_bits;
437 
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),
442  INIT_VLC_LE))) {
443  av_log(vc->avccontext, AV_LOG_ERROR, " Error generating vlc tables. \n");
444  goto error;
445  }
446  }
447 
448  av_free(tmp_vlc_bits);
449  av_free(tmp_vlc_codes);
450  av_free(codebook_multiplicands);
451  return 0;
452 
453 // Error:
454 error:
455  av_free(tmp_vlc_bits);
456  av_free(tmp_vlc_codes);
457  av_free(codebook_multiplicands);
458  return ret;
459 }
460 
461 // Process time domain transforms part (unused in Vorbis I)
462 
464 {
465  GetBitContext *gb = &vc->gb;
466  unsigned i, vorbis_time_count = get_bits(gb, 6) + 1;
467 
468  for (i = 0; i < vorbis_time_count; ++i) {
469  unsigned vorbis_tdtransform = get_bits(gb, 16);
470 
471  av_dlog(NULL, " Vorbis time domain transform %u: %u\n",
472  vorbis_time_count, vorbis_tdtransform);
473 
474  if (vorbis_tdtransform) {
475  av_log(vc->avccontext, AV_LOG_ERROR, "Vorbis time domain transform data nonzero. \n");
476  return AVERROR_INVALIDDATA;
477  }
478  }
479  return 0;
480 }
481 
482 // Process floors part
483 
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);
490 {
491  GetBitContext *gb = &vc->gb;
492  int i,j,k;
493 
494  vc->floor_count = get_bits(gb, 6) + 1;
495 
496  vc->floors = av_mallocz(vc->floor_count * sizeof(*vc->floors));
497 
498  for (i = 0; i < vc->floor_count; ++i) {
499  vorbis_floor *floor_setup = &vc->floors[i];
500 
501  floor_setup->floor_type = get_bits(gb, 16);
502 
503  av_dlog(NULL, " %d. floor type %d \n", i, floor_setup->floor_type);
504 
505  if (floor_setup->floor_type == 1) {
506  int maximum_class = -1;
507  unsigned rangebits, rangemax, floor1_values = 2;
508 
509  floor_setup->decode = vorbis_floor1_decode;
510 
511  floor_setup->data.t1.partitions = get_bits(gb, 5);
512 
513  av_dlog(NULL, " %d.floor: %d partitions \n",
514  i, floor_setup->data.t1.partitions);
515 
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];
520 
521  av_dlog(NULL, " %d. floor %d partition class %d \n",
522  i, j, floor_setup->data.t1.partition_class[j]);
523 
524  }
525 
526  av_dlog(NULL, " maximum class %d \n", maximum_class);
527 
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);
531 
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]);
535 
536  if (floor_setup->data.t1.class_subclasses[j]) {
538 
539  av_dlog(NULL, " masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]);
540  }
541 
542  for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) {
543  int16_t bits = get_bits(gb, 8) - 1;
544  if (bits != -1)
545  VALIDATE_INDEX(bits, vc->codebook_count)
546  floor_setup->data.t1.subclass_books[j][k] = bits;
547 
548  av_dlog(NULL, " book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]);
549  }
550  }
551 
552  floor_setup->data.t1.multiplier = get_bits(gb, 2) + 1;
553  floor_setup->data.t1.x_list_dim = 2;
554 
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]];
557 
558  floor_setup->data.t1.list = av_mallocz(floor_setup->data.t1.x_list_dim *
559  sizeof(*floor_setup->data.t1.list));
560 
561 
562  rangebits = get_bits(gb, 4);
563  rangemax = (1 << rangebits);
564  if (rangemax > vc->blocksize[1] / 2) {
566  "Floor value is too large for blocksize: %u (%"PRIu32")\n",
567  rangemax, vc->blocksize[1] / 2);
568  return AVERROR_INVALIDDATA;
569  }
570  floor_setup->data.t1.list[0].x = 0;
571  floor_setup->data.t1.list[1].x = rangemax;
572 
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);
576 
577  av_dlog(NULL, " %u. floor1 Y coord. %d\n", floor1_values,
578  floor_setup->data.t1.list[floor1_values].x);
579  }
580  }
581 
582 // Precalculate order of x coordinates - needed for decode
584  floor_setup->data.t1.list,
585  floor_setup->data.t1.x_list_dim)) {
586  return AVERROR_INVALIDDATA;
587  }
588  } else if (floor_setup->floor_type == 0) {
589  unsigned max_codebook_dim = 0;
590 
591  floor_setup->decode = vorbis_floor0_decode;
592 
593  floor_setup->data.t0.order = get_bits(gb, 8);
594  if (!floor_setup->data.t0.order) {
596  "Floor 0 order is 0.\n");
597  return AVERROR_INVALIDDATA;
598  }
599  floor_setup->data.t0.rate = get_bits(gb, 16);
600  if (!floor_setup->data.t0.rate) {
602  "Floor 0 rate is 0.\n");
603  return AVERROR_INVALIDDATA;
604  }
605  floor_setup->data.t0.bark_map_size = get_bits(gb, 16);
606  if (floor_setup->data.t0.bark_map_size == 0) {
608  "Floor 0 bark map size is 0.\n");
609  return AVERROR_INVALIDDATA;
610  }
611  floor_setup->data.t0.amplitude_bits = get_bits(gb, 6);
612  floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
613  floor_setup->data.t0.num_books = get_bits(gb, 4) + 1;
614 
615  /* allocate mem for booklist */
616  floor_setup->data.t0.book_list =
617  av_malloc(floor_setup->data.t0.num_books);
618  if (!floor_setup->data.t0.book_list)
619  return AVERROR(ENOMEM);
620  /* read book indexes */
621  {
622  int idx;
623  unsigned book_idx;
624  for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
625  GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count)
626  floor_setup->data.t0.book_list[idx] = book_idx;
627  if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
628  max_codebook_dim = vc->codebooks[book_idx].dimensions;
629  }
630  }
631 
632  create_map(vc, i);
633 
634  /* codebook dim is for padding if codebook dim doesn't *
635  * divide order+1 then we need to read more data */
636  floor_setup->data.t0.lsp =
637  av_malloc((floor_setup->data.t0.order + 1 + max_codebook_dim)
638  * sizeof(*floor_setup->data.t0.lsp));
639  if (!floor_setup->data.t0.lsp)
640  return AVERROR(ENOMEM);
641 
642  /* debug output parsed headers */
643  av_dlog(NULL, "floor0 order: %u\n", floor_setup->data.t0.order);
644  av_dlog(NULL, "floor0 rate: %u\n", floor_setup->data.t0.rate);
645  av_dlog(NULL, "floor0 bark map size: %u\n",
646  floor_setup->data.t0.bark_map_size);
647  av_dlog(NULL, "floor0 amplitude bits: %u\n",
648  floor_setup->data.t0.amplitude_bits);
649  av_dlog(NULL, "floor0 amplitude offset: %u\n",
650  floor_setup->data.t0.amplitude_offset);
651  av_dlog(NULL, "floor0 number of books: %u\n",
652  floor_setup->data.t0.num_books);
653  av_dlog(NULL, "floor0 book list pointer: %p\n",
654  floor_setup->data.t0.book_list);
655  {
656  int idx;
657  for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
658  av_dlog(NULL, " Book %d: %u\n", idx + 1,
659  floor_setup->data.t0.book_list[idx]);
660  }
661  }
662  } else {
663  av_log(vc->avccontext, AV_LOG_ERROR, "Invalid floor type!\n");
664  return AVERROR_INVALIDDATA;
665  }
666  }
667  return 0;
668 }
669 
670 // Process residues part
671 
673 {
674  GetBitContext *gb = &vc->gb;
675  unsigned i, j, k;
676 
677  vc->residue_count = get_bits(gb, 6)+1;
678  vc->residues = av_mallocz(vc->residue_count * sizeof(*vc->residues));
679 
680  av_dlog(NULL, " There are %d residues. \n", vc->residue_count);
681 
682  for (i = 0; i < vc->residue_count; ++i) {
683  vorbis_residue *res_setup = &vc->residues[i];
684  uint8_t cascade[64];
685  unsigned high_bits, low_bits;
686 
687  res_setup->type = get_bits(gb, 16);
688 
689  av_dlog(NULL, " %u. residue type %d\n", i, res_setup->type);
690 
691  res_setup->begin = get_bits(gb, 24);
692  res_setup->end = get_bits(gb, 24);
693  res_setup->partition_size = get_bits(gb, 24) + 1;
694  /* Validations to prevent a buffer overflow later. */
695  if (res_setup->begin>res_setup->end ||
696  res_setup->end > (res_setup->type == 2 ? vc->audio_channels : 1) * vc->blocksize[1] / 2 ||
697  (res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
699  "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n",
700  res_setup->type, res_setup->begin, res_setup->end,
701  res_setup->partition_size, vc->blocksize[1] / 2);
702  return AVERROR_INVALIDDATA;
703  }
704 
705  res_setup->classifications = get_bits(gb, 6) + 1;
706  GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
707 
708  res_setup->ptns_to_read =
709  (res_setup->end - res_setup->begin) / res_setup->partition_size;
710  res_setup->classifs = av_malloc(res_setup->ptns_to_read *
711  vc->audio_channels *
712  sizeof(*res_setup->classifs));
713  if (!res_setup->classifs)
714  return AVERROR(ENOMEM);
715 
716  av_dlog(NULL, " begin %d end %d part.size %d classif.s %d classbook %d \n",
717  res_setup->begin, res_setup->end, res_setup->partition_size,
718  res_setup->classifications, res_setup->classbook);
719 
720  for (j = 0; j < res_setup->classifications; ++j) {
721  high_bits = 0;
722  low_bits = get_bits(gb, 3);
723  if (get_bits1(gb))
724  high_bits = get_bits(gb, 5);
725  cascade[j] = (high_bits << 3) + low_bits;
726 
727  av_dlog(NULL, " %u class cascade depth: %d\n", j, ilog(cascade[j]));
728  }
729 
730  res_setup->maxpass = 0;
731  for (j = 0; j < res_setup->classifications; ++j) {
732  for (k = 0; k < 8; ++k) {
733  if (cascade[j]&(1 << k)) {
734  GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
735 
736  av_dlog(NULL, " %u class cascade depth %u book: %d\n",
737  j, k, res_setup->books[j][k]);
738 
739  if (k>res_setup->maxpass)
740  res_setup->maxpass = k;
741  } else {
742  res_setup->books[j][k] = -1;
743  }
744  }
745  }
746  }
747  return 0;
748 }
749 
750 // Process mappings part
751 
753 {
754  GetBitContext *gb = &vc->gb;
755  unsigned i, j;
756 
757  vc->mapping_count = get_bits(gb, 6)+1;
758  vc->mappings = av_mallocz(vc->mapping_count * sizeof(*vc->mappings));
759 
760  av_dlog(NULL, " There are %d mappings. \n", vc->mapping_count);
761 
762  for (i = 0; i < vc->mapping_count; ++i) {
763  vorbis_mapping *mapping_setup = &vc->mappings[i];
764 
765  if (get_bits(gb, 16)) {
766  av_log(vc->avccontext, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
767  return AVERROR_INVALIDDATA;
768  }
769  if (get_bits1(gb)) {
770  mapping_setup->submaps = get_bits(gb, 4) + 1;
771  } else {
772  mapping_setup->submaps = 1;
773  }
774 
775  if (get_bits1(gb)) {
776  mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
777  mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps *
778  sizeof(*mapping_setup->magnitude));
779  mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps *
780  sizeof(*mapping_setup->angle));
781  for (j = 0; j < mapping_setup->coupling_steps; ++j) {
782  GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
783  GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels)
784  }
785  } else {
786  mapping_setup->coupling_steps = 0;
787  }
788 
789  av_dlog(NULL, " %u mapping coupling steps: %d\n",
790  i, mapping_setup->coupling_steps);
791 
792  if (get_bits(gb, 2)) {
793  av_log(vc->avccontext, AV_LOG_ERROR, "%u. mapping setup data invalid.\n", i);
794  return AVERROR_INVALIDDATA; // following spec.
795  }
796 
797  if (mapping_setup->submaps>1) {
798  mapping_setup->mux = av_mallocz(vc->audio_channels *
799  sizeof(*mapping_setup->mux));
800  for (j = 0; j < vc->audio_channels; ++j)
801  mapping_setup->mux[j] = get_bits(gb, 4);
802  }
803 
804  for (j = 0; j < mapping_setup->submaps; ++j) {
805  skip_bits(gb, 8); // FIXME check?
806  GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count)
807  GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
808 
809  av_dlog(NULL, " %u mapping %u submap : floor %d, residue %d\n", i, j,
810  mapping_setup->submap_floor[j],
811  mapping_setup->submap_residue[j]);
812  }
813  }
814  return 0;
815 }
816 
817 // Process modes part
818 
819 static void create_map(vorbis_context *vc, unsigned floor_number)
820 {
821  vorbis_floor *floors = vc->floors;
822  vorbis_floor0 *vf;
823  int idx;
824  int blockflag, n;
825  int32_t *map;
826 
827  for (blockflag = 0; blockflag < 2; ++blockflag) {
828  n = vc->blocksize[blockflag] / 2;
829  floors[floor_number].data.t0.map[blockflag] =
830  av_malloc((n + 1) * sizeof(int32_t)); // n + sentinel
831 
832  map = floors[floor_number].data.t0.map[blockflag];
833  vf = &floors[floor_number].data.t0;
834 
835  for (idx = 0; idx < n; ++idx) {
836  map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
837  (vf->bark_map_size / BARK(vf->rate / 2.0f)));
838  if (vf->bark_map_size-1 < map[idx])
839  map[idx] = vf->bark_map_size - 1;
840  }
841  map[n] = -1;
842  vf->map_size[blockflag] = n;
843  }
844 
845  for (idx = 0; idx <= n; ++idx) {
846  av_dlog(NULL, "floor0 map: map at pos %d is %d\n", idx, map[idx]);
847  }
848 }
849 
851 {
852  GetBitContext *gb = &vc->gb;
853  unsigned i;
854 
855  vc->mode_count = get_bits(gb, 6) + 1;
856  vc->modes = av_mallocz(vc->mode_count * sizeof(*vc->modes));
857 
858  av_dlog(NULL, " There are %d modes.\n", vc->mode_count);
859 
860  for (i = 0; i < vc->mode_count; ++i) {
861  vorbis_mode *mode_setup = &vc->modes[i];
862 
863  mode_setup->blockflag = get_bits1(gb);
864  mode_setup->windowtype = get_bits(gb, 16); //FIXME check
865  mode_setup->transformtype = get_bits(gb, 16); //FIXME check
866  GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
867 
868  av_dlog(NULL, " %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
869  i, mode_setup->blockflag, mode_setup->windowtype,
870  mode_setup->transformtype, mode_setup->mapping);
871  }
872  return 0;
873 }
874 
875 // Process the whole setup header using the functions above
876 
878 {
879  GetBitContext *gb = &vc->gb;
880  int ret;
881 
882  if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
883  (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
884  (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
885  av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
886  return AVERROR_INVALIDDATA;
887  }
888 
889  if ((ret = vorbis_parse_setup_hdr_codebooks(vc))) {
890  av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
891  return ret;
892  }
893  if ((ret = vorbis_parse_setup_hdr_tdtransforms(vc))) {
894  av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
895  return ret;
896  }
897  if ((ret = vorbis_parse_setup_hdr_floors(vc))) {
898  av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
899  return ret;
900  }
901  if ((ret = vorbis_parse_setup_hdr_residues(vc))) {
902  av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
903  return ret;
904  }
905  if ((ret = vorbis_parse_setup_hdr_mappings(vc))) {
906  av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
907  return ret;
908  }
909  if ((ret = vorbis_parse_setup_hdr_modes(vc))) {
910  av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
911  return ret;
912  }
913  if (!get_bits1(gb)) {
914  av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
915  return AVERROR_INVALIDDATA; // framing flag bit unset error
916  }
917 
918  return 0;
919 }
920 
921 // Process the identification header
922 
924 {
925  GetBitContext *gb = &vc->gb;
926  unsigned bl0, bl1;
927 
928  if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
929  (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
930  (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
931  av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
932  return AVERROR_INVALIDDATA;
933  }
934 
935  vc->version = get_bits_long(gb, 32); //FIXME check 0
936  vc->audio_channels = get_bits(gb, 8);
937  if (vc->audio_channels <= 0) {
938  av_log(vc->avccontext, AV_LOG_ERROR, "Invalid number of channels\n");
939  return AVERROR_INVALIDDATA;
940  }
941  vc->audio_samplerate = get_bits_long(gb, 32);
942  if (vc->audio_samplerate <= 0) {
943  av_log(vc->avccontext, AV_LOG_ERROR, "Invalid samplerate\n");
944  return AVERROR_INVALIDDATA;
945  }
946  vc->bitrate_maximum = get_bits_long(gb, 32);
947  vc->bitrate_nominal = get_bits_long(gb, 32);
948  vc->bitrate_minimum = get_bits_long(gb, 32);
949  bl0 = get_bits(gb, 4);
950  bl1 = get_bits(gb, 4);
951  if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
952  av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
953  return AVERROR_INVALIDDATA;
954  }
955  vc->blocksize[0] = (1 << bl0);
956  vc->blocksize[1] = (1 << bl1);
957  vc->win[0] = ff_vorbis_vwin[bl0 - 6];
958  vc->win[1] = ff_vorbis_vwin[bl1 - 6];
959 
960  if ((get_bits1(gb)) == 0) {
961  av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
962  return AVERROR_INVALIDDATA;
963  }
964 
965  vc->channel_residues = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(*vc->channel_residues));
966  vc->saved = av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(*vc->saved));
967  vc->previous_window = 0;
968 
969  ff_mdct_init(&vc->mdct[0], bl0, 1, -1.0);
970  ff_mdct_init(&vc->mdct[1], bl1, 1, -1.0);
971 
972  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 ",
974 
975 /*
976  BLK = vc->blocksize[0];
977  for (i = 0; i < BLK / 2; ++i) {
978  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)));
979  }
980 */
981 
982  return 0;
983 }
984 
985 // Process the extradata using the functions above (identification header, setup header)
986 
988 {
989  vorbis_context *vc = avccontext->priv_data;
990  uint8_t *headers = avccontext->extradata;
991  int headers_len = avccontext->extradata_size;
992  uint8_t *header_start[3];
993  int header_len[3];
994  GetBitContext *gb = &vc->gb;
995  int hdr_type, ret;
996 
997  vc->avccontext = avccontext;
998  ff_dsputil_init(&vc->dsp, avccontext);
1000  ff_fmt_convert_init(&vc->fmt_conv, avccontext);
1001 
1002  avccontext->sample_fmt = AV_SAMPLE_FMT_FLTP;
1003 
1004  if (!headers_len) {
1005  av_log(avccontext, AV_LOG_ERROR, "Extradata missing.\n");
1006  return AVERROR_INVALIDDATA;
1007  }
1008 
1009  if ((ret = avpriv_split_xiph_headers(headers, headers_len, 30, header_start, header_len)) < 0) {
1010  av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
1011  return ret;
1012  }
1013 
1014  init_get_bits(gb, header_start[0], header_len[0]*8);
1015  hdr_type = get_bits(gb, 8);
1016  if (hdr_type != 1) {
1017  av_log(avccontext, AV_LOG_ERROR, "First header is not the id header.\n");
1018  return AVERROR_INVALIDDATA;
1019  }
1020  if ((ret = vorbis_parse_id_hdr(vc))) {
1021  av_log(avccontext, AV_LOG_ERROR, "Id header corrupt.\n");
1022  vorbis_free(vc);
1023  return ret;
1024  }
1025 
1026  init_get_bits(gb, header_start[2], header_len[2]*8);
1027  hdr_type = get_bits(gb, 8);
1028  if (hdr_type != 5) {
1029  av_log(avccontext, AV_LOG_ERROR, "Third header is not the setup header.\n");
1030  vorbis_free(vc);
1031  return AVERROR_INVALIDDATA;
1032  }
1033  if ((ret = vorbis_parse_setup_hdr(vc))) {
1034  av_log(avccontext, AV_LOG_ERROR, "Setup header corrupt.\n");
1035  vorbis_free(vc);
1036  return ret;
1037  }
1038 
1039  if (vc->audio_channels > 8)
1040  avccontext->channel_layout = 0;
1041  else
1043 
1044  avccontext->channels = vc->audio_channels;
1045  avccontext->sample_rate = vc->audio_samplerate;
1046 
1048  avccontext->coded_frame = &vc->frame;
1049 
1050  return 0;
1051 }
1052 
1053 // Decode audiopackets -------------------------------------------------
1054 
1055 // Read and decode floor
1056 
1058  vorbis_floor_data *vfu, float *vec)
1059 {
1060  vorbis_floor0 *vf = &vfu->t0;
1061  float *lsp = vf->lsp;
1062  unsigned amplitude, book_idx;
1063  unsigned blockflag = vc->modes[vc->mode_number].blockflag;
1064 
1065  if (!vf->amplitude_bits)
1066  return 1;
1067 
1068  amplitude = get_bits(&vc->gb, vf->amplitude_bits);
1069  if (amplitude > 0) {
1070  float last = 0;
1071  unsigned idx, lsp_len = 0;
1072  vorbis_codebook codebook;
1073 
1074  book_idx = get_bits(&vc->gb, ilog(vf->num_books));
1075  if (book_idx >= vf->num_books) {
1077  "floor0 dec: booknumber too high!\n");
1078  book_idx = 0;
1079  }
1080  av_dlog(NULL, "floor0 dec: booknumber: %u\n", book_idx);
1081  codebook = vc->codebooks[vf->book_list[book_idx]];
1082  /* Invalid codebook! */
1083  if (!codebook.codevectors)
1084  return AVERROR_INVALIDDATA;
1085 
1086  while (lsp_len<vf->order) {
1087  int vec_off;
1088 
1089  av_dlog(NULL, "floor0 dec: book dimension: %d\n", codebook.dimensions);
1090  av_dlog(NULL, "floor0 dec: maximum depth: %d\n", codebook.maxdepth);
1091  /* read temp vector */
1092  vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
1093  codebook.nb_bits, codebook.maxdepth)
1094  * codebook.dimensions;
1095  av_dlog(NULL, "floor0 dec: vector offset: %d\n", vec_off);
1096  /* copy each vector component and add last to it */
1097  for (idx = 0; idx < codebook.dimensions; ++idx)
1098  lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
1099  last = lsp[lsp_len+idx-1]; /* set last to last vector component */
1100 
1101  lsp_len += codebook.dimensions;
1102  }
1103  /* DEBUG: output lsp coeffs */
1104  {
1105  int idx;
1106  for (idx = 0; idx < lsp_len; ++idx)
1107  av_dlog(NULL, "floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
1108  }
1109 
1110  /* synthesize floor output vector */
1111  {
1112  int i;
1113  int order = vf->order;
1114  float wstep = M_PI / vf->bark_map_size;
1115 
1116  for (i = 0; i < order; i++)
1117  lsp[i] = 2.0f * cos(lsp[i]);
1118 
1119  av_dlog(NULL, "floor0 synth: map_size = %"PRIu32"; m = %d; wstep = %f\n",
1120  vf->map_size[blockflag], order, wstep);
1121 
1122  i = 0;
1123  while (i < vf->map_size[blockflag]) {
1124  int j, iter_cond = vf->map[blockflag][i];
1125  float p = 0.5f;
1126  float q = 0.5f;
1127  float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times
1128 
1129  /* similar part for the q and p products */
1130  for (j = 0; j + 1 < order; j += 2) {
1131  q *= lsp[j] - two_cos_w;
1132  p *= lsp[j + 1] - two_cos_w;
1133  }
1134  if (j == order) { // even order
1135  p *= p * (2.0f - two_cos_w);
1136  q *= q * (2.0f + two_cos_w);
1137  } else { // odd order
1138  q *= two_cos_w-lsp[j]; // one more time for q
1139 
1140  /* final step and square */
1141  p *= p * (4.f - two_cos_w * two_cos_w);
1142  q *= q;
1143  }
1144 
1145  /* calculate linear floor value */
1146  q = exp((((amplitude*vf->amplitude_offset) /
1147  (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
1148  - vf->amplitude_offset) * .11512925f);
1149 
1150  /* fill vector */
1151  do {
1152  vec[i] = q; ++i;
1153  } while (vf->map[blockflag][i] == iter_cond);
1154  }
1155  }
1156  } else {
1157  /* this channel is unused */
1158  return 1;
1159  }
1160 
1161  av_dlog(NULL, " Floor0 decoded\n");
1162 
1163  return 0;
1164 }
1165 
1167  vorbis_floor_data *vfu, float *vec)
1168 {
1169  vorbis_floor1 *vf = &vfu->t1;
1170  GetBitContext *gb = &vc->gb;
1171  uint16_t range_v[4] = { 256, 128, 86, 64 };
1172  unsigned range = range_v[vf->multiplier - 1];
1173  uint16_t floor1_Y[258];
1174  uint16_t floor1_Y_final[258];
1175  int floor1_flag[258];
1176  unsigned partition_class, cdim, cbits, csub, cval, offset, i, j;
1177  int book, adx, ady, dy, off, predicted, err;
1178 
1179 
1180  if (!get_bits1(gb)) // silence
1181  return 1;
1182 
1183 // Read values (or differences) for the floor's points
1184 
1185  floor1_Y[0] = get_bits(gb, ilog(range - 1));
1186  floor1_Y[1] = get_bits(gb, ilog(range - 1));
1187 
1188  av_dlog(NULL, "floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
1189 
1190  offset = 2;
1191  for (i = 0; i < vf->partitions; ++i) {
1192  partition_class = vf->partition_class[i];
1193  cdim = vf->class_dimensions[partition_class];
1194  cbits = vf->class_subclasses[partition_class];
1195  csub = (1 << cbits) - 1;
1196  cval = 0;
1197 
1198  av_dlog(NULL, "Cbits %u\n", cbits);
1199 
1200  if (cbits) // this reads all subclasses for this partition's class
1201  cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[partition_class]].vlc.table,
1202  vc->codebooks[vf->class_masterbook[partition_class]].nb_bits, 3);
1203 
1204  for (j = 0; j < cdim; ++j) {
1205  book = vf->subclass_books[partition_class][cval & csub];
1206 
1207  av_dlog(NULL, "book %d Cbits %u cval %u bits:%d\n",
1208  book, cbits, cval, get_bits_count(gb));
1209 
1210  cval = cval >> cbits;
1211  if (book > -1) {
1212  floor1_Y[offset+j] = get_vlc2(gb, vc->codebooks[book].vlc.table,
1213  vc->codebooks[book].nb_bits, 3);
1214  } else {
1215  floor1_Y[offset+j] = 0;
1216  }
1217 
1218  av_dlog(NULL, " floor(%d) = %d \n",
1219  vf->list[offset+j].x, floor1_Y[offset+j]);
1220  }
1221  offset+=cdim;
1222  }
1223 
1224 // Amplitude calculation from the differences
1225 
1226  floor1_flag[0] = 1;
1227  floor1_flag[1] = 1;
1228  floor1_Y_final[0] = floor1_Y[0];
1229  floor1_Y_final[1] = floor1_Y[1];
1230 
1231  for (i = 2; i < vf->x_list_dim; ++i) {
1232  unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
1233 
1234  low_neigh_offs = vf->list[i].low;
1235  high_neigh_offs = vf->list[i].high;
1236  dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs]; // render_point begin
1237  adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
1238  ady = FFABS(dy);
1239  err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
1240  off = err / adx;
1241  if (dy < 0) {
1242  predicted = floor1_Y_final[low_neigh_offs] - off;
1243  } else {
1244  predicted = floor1_Y_final[low_neigh_offs] + off;
1245  } // render_point end
1246 
1247  val = floor1_Y[i];
1248  highroom = range-predicted;
1249  lowroom = predicted;
1250  if (highroom < lowroom) {
1251  room = highroom * 2;
1252  } else {
1253  room = lowroom * 2; // SPEC misspelling
1254  }
1255  if (val) {
1256  floor1_flag[low_neigh_offs] = 1;
1257  floor1_flag[high_neigh_offs] = 1;
1258  floor1_flag[i] = 1;
1259  if (val >= room) {
1260  if (highroom > lowroom) {
1261  floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
1262  } else {
1263  floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
1264  }
1265  } else {
1266  if (val & 1) {
1267  floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
1268  } else {
1269  floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
1270  }
1271  }
1272  } else {
1273  floor1_flag[i] = 0;
1274  floor1_Y_final[i] = av_clip_uint16(predicted);
1275  }
1276 
1277  av_dlog(NULL, " Decoded floor(%d) = %u / val %u\n",
1278  vf->list[i].x, floor1_Y_final[i], val);
1279  }
1280 
1281 // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ?
1282 
1283  ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x);
1284 
1285  av_dlog(NULL, " Floor decoded\n");
1286 
1287  return 0;
1288 }
1289 
1290 // Read and decode residue
1291 
1293  vorbis_residue *vr,
1294  unsigned ch,
1295  uint8_t *do_not_decode,
1296  float *vec,
1297  unsigned vlen,
1298  unsigned ch_left,
1299  int vr_type)
1300 {
1301  GetBitContext *gb = &vc->gb;
1302  unsigned c_p_c = vc->codebooks[vr->classbook].dimensions;
1303  unsigned ptns_to_read = vr->ptns_to_read;
1304  uint8_t *classifs = vr->classifs;
1305  unsigned pass, ch_used, i, j, k, l;
1306  unsigned max_output = (ch - 1) * vlen;
1307 
1308  if (vr_type == 2) {
1309  for (j = 1; j < ch; ++j)
1310  do_not_decode[0] &= do_not_decode[j]; // FIXME - clobbering input
1311  if (do_not_decode[0])
1312  return 0;
1313  ch_used = 1;
1314  max_output += vr->end / ch;
1315  } else {
1316  ch_used = ch;
1317  max_output += vr->end;
1318  }
1319 
1320  if (max_output > ch_left * vlen) {
1321  av_log(vc->avccontext, AV_LOG_ERROR, "Insufficient output buffer\n");
1322  return -1;
1323  }
1324 
1325  av_dlog(NULL, " residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
1326 
1327  for (pass = 0; pass <= vr->maxpass; ++pass) { // FIXME OPTIMIZE?
1328  uint16_t voffset, partition_count, j_times_ptns_to_read;
1329 
1330  voffset = vr->begin;
1331  for (partition_count = 0; partition_count < ptns_to_read;) { // SPEC error
1332  if (!pass) {
1333  unsigned inverse_class = ff_inverse[vr->classifications];
1334  for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1335  if (!do_not_decode[j]) {
1336  unsigned temp = get_vlc2(gb, vc->codebooks[vr->classbook].vlc.table,
1337  vc->codebooks[vr->classbook].nb_bits, 3);
1338 
1339  av_dlog(NULL, "Classword: %u\n", temp);
1340 
1341  av_assert0(vr->classifications > 1 && temp <= 65536); //needed for inverse[]
1342  for (i = 0; i < c_p_c; ++i) {
1343  unsigned temp2;
1344 
1345  temp2 = (((uint64_t)temp) * inverse_class) >> 32;
1346  if (partition_count + c_p_c - 1 - i < ptns_to_read)
1347  classifs[j_times_ptns_to_read + partition_count + c_p_c - 1 - i] = temp - temp2 * vr->classifications;
1348  temp = temp2;
1349  }
1350  }
1351  j_times_ptns_to_read += ptns_to_read;
1352  }
1353  }
1354  for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
1355  for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
1356  unsigned voffs;
1357 
1358  if (!do_not_decode[j]) {
1359  unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
1360  int vqbook = vr->books[vqclass][pass];
1361 
1362  if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
1363  unsigned coffs;
1364  unsigned dim = vc->codebooks[vqbook].dimensions;
1365  unsigned step = FASTDIV(vr->partition_size << 1, dim << 1);
1366  vorbis_codebook codebook = vc->codebooks[vqbook];
1367 
1368  if (vr_type == 0) {
1369 
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)
1374  vec[voffs + k + l * step] += codebook.codevectors[coffs + l];
1375  }
1376  } else if (vr_type == 1) {
1377  voffs = voffset + j * vlen;
1378  for (k = 0; k < step; ++k) {
1379  coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
1380  for (l = 0; l < dim; ++l, ++voffs) {
1381  vec[voffs]+=codebook.codevectors[coffs+l];
1382 
1383  av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
1384  pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
1385  }
1386  }
1387  } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) { // most frequent case optimized
1388  voffs = voffset >> 1;
1389 
1390  if (dim == 2) {
1391  for (k = 0; k < step; ++k) {
1392  coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 2;
1393  vec[voffs + k ] += codebook.codevectors[coffs ];
1394  vec[voffs + k + vlen] += codebook.codevectors[coffs + 1];
1395  }
1396  } else if (dim == 4) {
1397  for (k = 0; k < step; ++k, voffs += 2) {
1398  coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 4;
1399  vec[voffs ] += codebook.codevectors[coffs ];
1400  vec[voffs + 1 ] += codebook.codevectors[coffs + 2];
1401  vec[voffs + vlen ] += codebook.codevectors[coffs + 1];
1402  vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3];
1403  }
1404  } else
1405  for (k = 0; k < step; ++k) {
1406  coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
1407  for (l = 0; l < dim; l += 2, voffs++) {
1408  vec[voffs ] += codebook.codevectors[coffs + l ];
1409  vec[voffs + vlen] += codebook.codevectors[coffs + l + 1];
1410 
1411  av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1412  pass, voffset / ch + (voffs % ch) * vlen,
1413  vec[voffset / ch + (voffs % ch) * vlen],
1414  codebook.codevectors[coffs + l], coffs, l);
1415  }
1416  }
1417 
1418  } else if (vr_type == 2) {
1419  unsigned voffs_div = FASTDIV(voffset << 1, ch <<1);
1420  unsigned voffs_mod = voffset - voffs_div * ch;
1421 
1422  for (k = 0; k < step; ++k) {
1423  coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
1424  for (l = 0; l < dim; ++l) {
1425  vec[voffs_div + voffs_mod * vlen] +=
1426  codebook.codevectors[coffs + l];
1427 
1428  av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
1429  pass, voffs_div + voffs_mod * vlen,
1430  vec[voffs_div + voffs_mod * vlen],
1431  codebook.codevectors[coffs + l], coffs, l);
1432 
1433  if (++voffs_mod == ch) {
1434  voffs_div++;
1435  voffs_mod = 0;
1436  }
1437  }
1438  }
1439  }
1440  }
1441  }
1442  j_times_ptns_to_read += ptns_to_read;
1443  }
1444  ++partition_count;
1445  voffset += vr->partition_size;
1446  }
1447  }
1448  }
1449  return 0;
1450 }
1451 
1453  unsigned ch,
1454  uint8_t *do_not_decode,
1455  float *vec, unsigned vlen,
1456  unsigned ch_left)
1457 {
1458  if (vr->type == 2)
1459  return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2);
1460  else if (vr->type == 1)
1461  return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1);
1462  else if (vr->type == 0)
1463  return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 0);
1464  else {
1465  av_log(vc->avccontext, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
1466  return AVERROR_INVALIDDATA;
1467  }
1468 }
1469 
1470 void ff_vorbis_inverse_coupling(float *mag, float *ang, int blocksize)
1471 {
1472  int i;
1473  for (i = 0; i < blocksize; i++) {
1474  if (mag[i] > 0.0) {
1475  if (ang[i] > 0.0) {
1476  ang[i] = mag[i] - ang[i];
1477  } else {
1478  float temp = ang[i];
1479  ang[i] = mag[i];
1480  mag[i] += temp;
1481  }
1482  } else {
1483  if (ang[i] > 0.0) {
1484  ang[i] += mag[i];
1485  } else {
1486  float temp = ang[i];
1487  ang[i] = mag[i];
1488  mag[i] -= temp;
1489  }
1490  }
1491  }
1492 }
1493 
1494 // Decode the audio packet using the functions above
1495 
1496 static int vorbis_parse_audio_packet(vorbis_context *vc, float **floor_ptr)
1497 {
1498  GetBitContext *gb = &vc->gb;
1499  FFTContext *mdct;
1500  unsigned previous_window = vc->previous_window;
1501  unsigned mode_number, blockflag, blocksize;
1502  int i, j;
1503  uint8_t no_residue[255];
1504  uint8_t do_not_decode[255];
1505  vorbis_mapping *mapping;
1506  float *ch_res_ptr = vc->channel_residues;
1507  uint8_t res_chan[255];
1508  unsigned res_num = 0;
1509  int retlen = 0;
1510  unsigned ch_left = vc->audio_channels;
1511  unsigned vlen;
1512 
1513  if (get_bits1(gb)) {
1514  av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
1515  return AVERROR_INVALIDDATA; // packet type not audio
1516  }
1517 
1518  if (vc->mode_count == 1) {
1519  mode_number = 0;
1520  } else {
1521  GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
1522  }
1523  vc->mode_number = mode_number;
1524  mapping = &vc->mappings[vc->modes[mode_number].mapping];
1525 
1526  av_dlog(NULL, " Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
1527  vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
1528 
1529  blockflag = vc->modes[mode_number].blockflag;
1530  blocksize = vc->blocksize[blockflag];
1531  vlen = blocksize / 2;
1532  if (blockflag) {
1533  previous_window = get_bits(gb, 1);
1534  skip_bits1(gb); // next_window
1535  }
1536 
1537  memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
1538  for (i = 0; i < vc->audio_channels; ++i)
1539  memset(floor_ptr[i], 0, vlen * sizeof(floor_ptr[0][0])); //FIXME can this be removed ?
1540 
1541 // Decode floor
1542 
1543  for (i = 0; i < vc->audio_channels; ++i) {
1544  vorbis_floor *floor;
1545  int ret;
1546  if (mapping->submaps > 1) {
1547  floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
1548  } else {
1549  floor = &vc->floors[mapping->submap_floor[0]];
1550  }
1551 
1552  ret = floor->decode(vc, &floor->data, floor_ptr[i]);
1553 
1554  if (ret < 0) {
1555  av_log(vc->avccontext, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
1556  return AVERROR_INVALIDDATA;
1557  }
1558  no_residue[i] = ret;
1559  }
1560 
1561 // Nonzero vector propagate
1562 
1563  for (i = mapping->coupling_steps - 1; i >= 0; --i) {
1564  if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
1565  no_residue[mapping->magnitude[i]] = 0;
1566  no_residue[mapping->angle[i]] = 0;
1567  }
1568  }
1569 
1570 // Decode residue
1571 
1572  for (i = 0; i < mapping->submaps; ++i) {
1573  vorbis_residue *residue;
1574  unsigned ch = 0;
1575  int ret;
1576 
1577  for (j = 0; j < vc->audio_channels; ++j) {
1578  if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
1579  res_chan[j] = res_num;
1580  if (no_residue[j]) {
1581  do_not_decode[ch] = 1;
1582  } else {
1583  do_not_decode[ch] = 0;
1584  }
1585  ++ch;
1586  ++res_num;
1587  }
1588  }
1589  residue = &vc->residues[mapping->submap_residue[i]];
1590  if (ch_left < ch) {
1591  av_log(vc->avccontext, AV_LOG_ERROR, "Too many channels in vorbis_floor_decode.\n");
1592  return -1;
1593  }
1594  if (ch) {
1595  ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left);
1596  if (ret < 0)
1597  return ret;
1598  }
1599 
1600  ch_res_ptr += ch * vlen;
1601  ch_left -= ch;
1602  }
1603 
1604  if (ch_left > 0)
1605  return AVERROR_INVALIDDATA;
1606 
1607 // Inverse coupling
1608 
1609  for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed
1610  float *mag, *ang;
1611 
1612  mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2;
1613  ang = vc->channel_residues+res_chan[mapping->angle[i]] * blocksize / 2;
1614  vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
1615  }
1616 
1617 // Dotproduct, MDCT
1618 
1619  mdct = &vc->mdct[blockflag];
1620 
1621  for (j = vc->audio_channels-1;j >= 0; j--) {
1622  ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
1623  vc->fdsp.vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2);
1624  mdct->imdct_half(mdct, ch_res_ptr, floor_ptr[j]);
1625  }
1626 
1627 // Overlap/add, save data for next overlapping
1628 
1629  retlen = (blocksize + vc->blocksize[previous_window]) / 4;
1630  for (j = 0; j < vc->audio_channels; j++) {
1631  unsigned bs0 = vc->blocksize[0];
1632  unsigned bs1 = vc->blocksize[1];
1633  float *residue = vc->channel_residues + res_chan[j] * blocksize / 2;
1634  float *saved = vc->saved + j * bs1 / 4;
1635  float *ret = floor_ptr[j];
1636  float *buf = residue;
1637  const float *win = vc->win[blockflag & previous_window];
1638 
1639  if (blockflag == previous_window) {
1640  vc->dsp.vector_fmul_window(ret, saved, buf, win, blocksize / 4);
1641  } else if (blockflag > previous_window) {
1642  vc->dsp.vector_fmul_window(ret, saved, buf, win, bs0 / 4);
1643  memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) * sizeof(float));
1644  } else {
1645  memcpy(ret, saved, ((bs1 - bs0) / 4) * sizeof(float));
1646  vc->dsp.vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, bs0 / 4);
1647  }
1648  memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
1649  }
1650 
1651  vc->previous_window = blockflag;
1652  return retlen;
1653 }
1654 
1655 // Return the decoded audio packet through the standard api
1656 
1657 static int vorbis_decode_frame(AVCodecContext *avccontext, void *data,
1658  int *got_frame_ptr, AVPacket *avpkt)
1659 {
1660  const uint8_t *buf = avpkt->data;
1661  int buf_size = avpkt->size;
1662  vorbis_context *vc = avccontext->priv_data;
1663  GetBitContext *gb = &vc->gb;
1664  float *channel_ptrs[255];
1665  int i, len, ret;
1666 
1667  av_dlog(NULL, "packet length %d \n", buf_size);
1668 
1669  if (*buf == 1 && buf_size > 7) {
1670  init_get_bits(gb, buf+1, buf_size*8 - 8);
1671  vorbis_free(vc);
1672  if ((ret = vorbis_parse_id_hdr(vc))) {
1673  av_log(avccontext, AV_LOG_ERROR, "Id header corrupt.\n");
1674  vorbis_free(vc);
1675  return ret;
1676  }
1677 
1678  if (vc->audio_channels > 8)
1679  avccontext->channel_layout = 0;
1680  else
1682 
1683  avccontext->channels = vc->audio_channels;
1684  avccontext->sample_rate = vc->audio_samplerate;
1685  return buf_size;
1686  }
1687 
1688  if (*buf == 3 && buf_size > 7) {
1689  av_log(avccontext, AV_LOG_DEBUG, "Ignoring comment header\n");
1690  return buf_size;
1691  }
1692 
1693  if (*buf == 5 && buf_size > 7 && vc->channel_residues && !vc->modes) {
1694  init_get_bits(gb, buf+1, buf_size*8 - 8);
1695  if ((ret = vorbis_parse_setup_hdr(vc))) {
1696  av_log(avccontext, AV_LOG_ERROR, "Setup header corrupt.\n");
1697  vorbis_free(vc);
1698  return ret;
1699  }
1700  return buf_size;
1701  }
1702 
1703  if (!vc->channel_residues || !vc->modes) {
1704  av_log(avccontext, AV_LOG_ERROR, "Data packet before valid headers\n");
1705  return AVERROR_INVALIDDATA;
1706  }
1707 
1708  /* get output buffer */
1709  vc->frame.nb_samples = vc->blocksize[1] / 2;
1710  if ((ret = ff_get_buffer(avccontext, &vc->frame)) < 0) {
1711  av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
1712  return ret;
1713  }
1714 
1715  if (vc->audio_channels > 8) {
1716  for (i = 0; i < vc->audio_channels; i++)
1717  channel_ptrs[i] = (float *)vc->frame.extended_data[i];
1718  } else {
1719  for (i = 0; i < vc->audio_channels; i++) {
1720  int ch = ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
1721  channel_ptrs[ch] = (float *)vc->frame.extended_data[i];
1722  }
1723  }
1724 
1725  init_get_bits(gb, buf, buf_size*8);
1726 
1727  if ((len = vorbis_parse_audio_packet(vc, channel_ptrs)) <= 0)
1728  return len;
1729 
1730  if (!vc->first_frame) {
1731  vc->first_frame = 1;
1732  *got_frame_ptr = 0;
1733  return buf_size;
1734  }
1735 
1736  av_dlog(NULL, "parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
1737  get_bits_count(gb) / 8, get_bits_count(gb) % 8, len);
1738 
1739  vc->frame.nb_samples = len;
1740  *got_frame_ptr = 1;
1741  *(AVFrame *)data = vc->frame;
1742 
1743  return buf_size;
1744 }
1745 
1746 // Close decoder
1747 
1749 {
1750  vorbis_context *vc = avccontext->priv_data;
1751 
1752  vorbis_free(vc);
1753 
1754  return 0;
1755 }
1756 
1758 {
1759  vorbis_context *vc = avccontext->priv_data;
1760 
1761  if (vc->saved) {
1762  memset(vc->saved, 0, (vc->blocksize[1] / 4) * vc->audio_channels *
1763  sizeof(*vc->saved));
1764  }
1765  vc->previous_window = 0;
1766 }
1767 
1769  .name = "vorbis",
1770  .type = AVMEDIA_TYPE_AUDIO,
1771  .id = AV_CODEC_ID_VORBIS,
1772  .priv_data_size = sizeof(vorbis_context),
1777  .capabilities = CODEC_CAP_DR1,
1778  .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
1779  .channel_layouts = ff_vorbis_channel_layouts,
1780  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1782 };