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