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