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