vorbis_dec.c 60.5 KB
Newer Older
1
/**
2
 * @file
3 4
 * Vorbis I decoder
 * @author Denes Balatoni  ( dbalatoni programozo hu )
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#undef V_DEBUG
//#define V_DEBUG
//#define AV_DEBUG(...) av_log(NULL, AV_LOG_INFO, __VA_ARGS__)

#include <math.h>

#define ALT_BITSTREAM_READER_LE
#include "avcodec.h"
31
#include "get_bits.h"
32
#include "dsputil.h"
33
#include "fft.h"
34
#include "fmtconvert.h"
35 36 37 38 39 40

#include "vorbis.h"
#include "xiph.h"

#define V_NB_BITS 8
#define V_NB_BITS2 11
41 42
#define V_MAX_VLCS (1 << 16)
#define V_MAX_PARTITIONS (1 << 20)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

#ifndef V_DEBUG
#define AV_DEBUG(...)
#endif

#undef NDEBUG
#include <assert.h>

typedef struct {
    uint_fast8_t dimensions;
    uint_fast8_t lookup_type;
    uint_fast8_t maxdepth;
    VLC vlc;
    float *codevectors;
    unsigned int nb_bits;
} vorbis_codebook;

60
typedef union  vorbis_floor_u  vorbis_floor_data;
61 62 63 64
typedef struct vorbis_floor0_s vorbis_floor0;
typedef struct vorbis_floor1_s vorbis_floor1;
struct vorbis_context_s;
typedef
65 66
int (* vorbis_floor_decode_func)
    (struct vorbis_context_s *, vorbis_floor_data *, float *);
67 68 69
typedef struct {
    uint_fast8_t floor_type;
    vorbis_floor_decode_func decode;
70 71
    union vorbis_floor_u {
        struct vorbis_floor0_s {
72
            uint_fast8_t  order;
73 74
            uint_fast16_t rate;
            uint_fast16_t bark_map_size;
75
            int_fast32_t *map[2];
76
            uint_fast32_t map_size[2];
77 78 79 80 81
            uint_fast8_t  amplitude_bits;
            uint_fast8_t  amplitude_offset;
            uint_fast8_t  num_books;
            uint_fast8_t *book_list;
            float        *lsp;
82
        } t0;
83
        struct vorbis_floor1_s {
84
            uint_fast8_t partitions;
85
            uint8_t      partition_class[32];
86 87 88 89 90 91
            uint_fast8_t class_dimensions[16];
            uint_fast8_t class_subclasses[16];
            uint_fast8_t class_masterbook[16];
            int_fast16_t subclass_books[16][8];
            uint_fast8_t multiplier;
            uint_fast16_t x_list_dim;
92
            vorbis_floor1_entry *list;
93 94 95 96 97 98 99 100
        } t1;
    } data;
} vorbis_floor;

typedef struct {
    uint_fast16_t type;
    uint_fast32_t begin;
    uint_fast32_t end;
101
    unsigned      partition_size;
102 103 104 105
    uint_fast8_t  classifications;
    uint_fast8_t  classbook;
    int_fast16_t  books[64][8];
    uint_fast8_t  maxpass;
106
    uint_fast16_t ptns_to_read;
107
    uint8_t *classifs;
108 109 110
} vorbis_residue;

typedef struct {
111
    uint_fast8_t  submaps;
112 113 114 115
    uint_fast16_t coupling_steps;
    uint_fast8_t *magnitude;
    uint_fast8_t *angle;
    uint_fast8_t *mux;
116 117
    uint_fast8_t  submap_floor[16];
    uint_fast8_t  submap_residue[16];
118 119 120
} vorbis_mapping;

typedef struct {
121
    uint_fast8_t  blockflag;
122 123
    uint_fast16_t windowtype;
    uint_fast16_t transformtype;
124
    uint_fast8_t  mapping;
125 126 127 128 129 130
} vorbis_mode;

typedef struct vorbis_context_s {
    AVCodecContext *avccontext;
    GetBitContext gb;
    DSPContext dsp;
131
    FmtConvertContext fmt_conv;
132

133
    FFTContext mdct[2];
134
    uint_fast8_t  first_frame;
135
    uint_fast32_t version;
136
    uint_fast8_t  audio_channels;
137 138 139 140 141
    uint_fast32_t audio_samplerate;
    uint_fast32_t bitrate_maximum;
    uint_fast32_t bitrate_nominal;
    uint_fast32_t bitrate_minimum;
    uint_fast32_t blocksize[2];
142
    const float  *win[2];
143 144
    uint_fast16_t codebook_count;
    vorbis_codebook *codebooks;
145
    uint_fast8_t  floor_count;
146
    vorbis_floor *floors;
147
    uint_fast8_t  residue_count;
148
    vorbis_residue *residues;
149
    uint_fast8_t  mapping_count;
150
    vorbis_mapping *mappings;
151 152 153 154 155 156 157
    uint_fast8_t  mode_count;
    vorbis_mode  *modes;
    uint_fast8_t  mode_number; // mode number for the current packet
    uint_fast8_t  previous_window;
    float        *channel_residues;
    float        *channel_floors;
    float        *saved;
158
    float         scale_bias; // for float->int conversion
159 160 161 162 163
} vorbis_context;

/* Helper functions */

#define BARK(x) \
164
    (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x))
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179
static const char idx_err_str[] = "Index value %d out of range (0 - %d) for %s at %s:%i\n";
#define VALIDATE_INDEX(idx, limit) \
    if (idx >= limit) {\
        av_log(vc->avccontext, AV_LOG_ERROR,\
               idx_err_str,\
               (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\
        return -1;\
    }
#define GET_VALIDATED_INDEX(idx, bits, limit) \
    {\
        idx = get_bits(gb, bits);\
        VALIDATE_INDEX(idx, limit)\
    }

180 181
static float vorbisfloat2float(uint_fast32_t val)
{
182 183 184 185
    double mant = val & 0x1fffff;
    long exp    = (val & 0x7fe00000L) >> 21;
    if (val & 0x80000000)
        mant = -mant;
186
    return ldexp(mant, exp - 20 - 768);
187 188 189 190 191
}


// Free all allocated memory -----------------------------------------

192 193
static void vorbis_free(vorbis_context *vc)
{
194 195 196 197 198 199
    int_fast16_t i;

    av_freep(&vc->channel_residues);
    av_freep(&vc->channel_floors);
    av_freep(&vc->saved);

200 201
    for (i = 0; i < vc->residue_count; i++)
        av_free(vc->residues[i].classifs);
202 203 204 205 206 207
    av_freep(&vc->residues);
    av_freep(&vc->modes);

    ff_mdct_end(&vc->mdct[0]);
    ff_mdct_end(&vc->mdct[1]);

208
    for (i = 0; i < vc->codebook_count; ++i) {
209 210 211 212 213
        av_free(vc->codebooks[i].codevectors);
        free_vlc(&vc->codebooks[i].vlc);
    }
    av_freep(&vc->codebooks);

214 215
    for (i = 0; i < vc->floor_count; ++i) {
        if (vc->floors[i].floor_type == 0) {
216 217 218 219
            av_free(vc->floors[i].data.t0.map[0]);
            av_free(vc->floors[i].data.t0.map[1]);
            av_free(vc->floors[i].data.t0.book_list);
            av_free(vc->floors[i].data.t0.lsp);
220
        } else {
221 222 223 224 225
            av_free(vc->floors[i].data.t1.list);
        }
    }
    av_freep(&vc->floors);

226
    for (i = 0; i < vc->mapping_count; ++i) {
227 228 229 230 231 232 233 234 235 236 237
        av_free(vc->mappings[i].magnitude);
        av_free(vc->mappings[i].angle);
        av_free(vc->mappings[i].mux);
    }
    av_freep(&vc->mappings);
}

// Parse setup header -------------------------------------------------

// Process codebooks part

238 239
static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc)
{
240
    uint_fast16_t cb;
241
    uint8_t  *tmp_vlc_bits;
242
    uint32_t *tmp_vlc_codes;
243
    GetBitContext *gb = &vc->gb;
244
    uint_fast16_t *codebook_multiplicands;
245

246
    vc->codebook_count = get_bits(gb, 8) + 1;
247 248 249

    AV_DEBUG(" Codebooks: %d \n", vc->codebook_count);

250 251 252
    vc->codebooks = av_mallocz(vc->codebook_count * sizeof(vorbis_codebook));
    tmp_vlc_bits  = av_mallocz(V_MAX_VLCS * sizeof(uint8_t));
    tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(uint32_t));
253
    codebook_multiplicands = av_malloc(V_MAX_VLCS * sizeof(*codebook_multiplicands));
254

255 256
    for (cb = 0; cb < vc->codebook_count; ++cb) {
        vorbis_codebook *codebook_setup = &vc->codebooks[cb];
257
        uint_fast8_t ordered;
258
        uint_fast32_t t, used_entries = 0;
259 260 261 262
        uint_fast32_t entries;

        AV_DEBUG(" %d. Codebook \n", cb);

263
        if (get_bits(gb, 24) != 0x564342) {
264 265 266 267 268
            av_log(vc->avccontext, AV_LOG_ERROR, " %"PRIdFAST16". Codebook setup data corrupt. \n", cb);
            goto error;
        }

        codebook_setup->dimensions=get_bits(gb, 16);
269
        if (codebook_setup->dimensions > 16 || codebook_setup->dimensions == 0) {
270
            av_log(vc->avccontext, AV_LOG_ERROR, " %"PRIdFAST16". Codebook's dimension is invalid (%d). \n", cb, codebook_setup->dimensions);
271 272
            goto error;
        }
273 274
        entries = get_bits(gb, 24);
        if (entries > V_MAX_VLCS) {
275 276 277 278
            av_log(vc->avccontext, AV_LOG_ERROR, " %"PRIdFAST16". Codebook has too many entries (%"PRIdFAST32"). \n", cb, entries);
            goto error;
        }

279
        ordered = get_bits1(gb);
280 281 282 283 284

        AV_DEBUG(" codebook_dimensions %d, codebook_entries %d \n", codebook_setup->dimensions, entries);

        if (!ordered) {
            uint_fast16_t ce;
285 286
            uint_fast8_t  flag;
            uint_fast8_t  sparse = get_bits1(gb);
287 288 289 290 291 292

            AV_DEBUG(" not ordered \n");

            if (sparse) {
                AV_DEBUG(" sparse \n");

293 294 295
                used_entries = 0;
                for (ce = 0; ce < entries; ++ce) {
                    flag = get_bits1(gb);
296
                    if (flag) {
297
                        tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
298
                        ++used_entries;
299
                    } else
300
                        tmp_vlc_bits[ce] = 0;
301 302 303 304
                }
            } else {
                AV_DEBUG(" not sparse \n");

305 306 307
                used_entries = entries;
                for (ce = 0; ce < entries; ++ce)
                    tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
308 309
            }
        } else {
310 311
            uint_fast16_t current_entry = 0;
            uint_fast8_t current_length = get_bits(gb, 5)+1;
312 313 314

            AV_DEBUG(" ordered, current length: %d \n", current_length);  //FIXME

315 316
            used_entries = entries;
            for (; current_entry < used_entries && current_length <= 32; ++current_length) {
317 318 319 320
                uint_fast16_t i, number;

                AV_DEBUG(" number bits: %d ", ilog(entries - current_entry));

321
                number = get_bits(gb, ilog(entries - current_entry));
322 323 324

                AV_DEBUG(" number: %d \n", number);

325 326 327
                for (i = current_entry; i < number+current_entry; ++i)
                    if (i < used_entries)
                        tmp_vlc_bits[i] = current_length;
328 329 330 331 332 333 334 335 336

                current_entry+=number;
            }
            if (current_entry>used_entries) {
                av_log(vc->avccontext, AV_LOG_ERROR, " More codelengths than codes in codebook. \n");
                goto error;
            }
        }

337
        codebook_setup->lookup_type = get_bits(gb, 4);
338

339
        AV_DEBUG(" lookup type: %d : %s \n", codebook_setup->lookup_type, codebook_setup->lookup_type ? "vq" : "no lookup");
340 341 342

// If the codebook is used for (inverse) VQ, calculate codevectors.

343
        if (codebook_setup->lookup_type == 1) {
344
            uint_fast16_t i, j, k;
345
            uint_fast16_t codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions);
346

347 348 349 350
            float codebook_minimum_value = vorbisfloat2float(get_bits_long(gb, 32));
            float codebook_delta_value   = vorbisfloat2float(get_bits_long(gb, 32));
            uint_fast8_t codebook_value_bits = get_bits(gb, 4)+1;
            uint_fast8_t codebook_sequence_p = get_bits1(gb);
351 352 353 354

            AV_DEBUG(" We expect %d numbers for building the codevectors. \n", codebook_lookup_values);
            AV_DEBUG("  delta %f minmum %f \n", codebook_delta_value, codebook_minimum_value);

355 356
            for (i = 0; i < codebook_lookup_values; ++i) {
                codebook_multiplicands[i] = get_bits(gb, codebook_value_bits);
357 358 359 360 361 362

                AV_DEBUG(" multiplicands*delta+minmum : %e \n", (float)codebook_multiplicands[i]*codebook_delta_value+codebook_minimum_value);
                AV_DEBUG(" multiplicand %d \n", codebook_multiplicands[i]);
            }

// Weed out unused vlcs and build codevector vector
363 364 365
            codebook_setup->codevectors = used_entries ? av_mallocz(used_entries*codebook_setup->dimensions * sizeof(float)) : NULL;
            for (j = 0, i = 0; i < entries; ++i) {
                uint_fast8_t dim = codebook_setup->dimensions;
366 367

                if (tmp_vlc_bits[i]) {
368 369
                    float last = 0.0;
                    uint_fast32_t lookup_offset = i;
370 371 372 373 374

#ifdef V_DEBUG
                    av_log(vc->avccontext, AV_LOG_INFO, "Lookup offset %d ,", i);
#endif

375
                    for (k = 0; k < dim; ++k) {
376
                        uint_fast32_t multiplicand_offset = lookup_offset % codebook_lookup_values;
377
                        codebook_setup->codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;
378
                        if (codebook_sequence_p)
379
                            last = codebook_setup->codevectors[j * dim + k];
380 381
                        lookup_offset/=codebook_lookup_values;
                    }
382
                    tmp_vlc_bits[j] = tmp_vlc_bits[i];
383 384 385

#ifdef V_DEBUG
                    av_log(vc->avccontext, AV_LOG_INFO, "real lookup offset %d, vector: ", j);
386 387
                    for (k = 0; k < dim; ++k)
                        av_log(vc->avccontext, AV_LOG_INFO, " %f ", codebook_setup->codevectors[j * dim + k]);
388 389 390 391 392 393
                    av_log(vc->avccontext, AV_LOG_INFO, "\n");
#endif

                    ++j;
                }
            }
394
            if (j != used_entries) {
395 396 397
                av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \n");
                goto error;
            }
398 399
            entries = used_entries;
        } else if (codebook_setup->lookup_type >= 2) {
400 401 402 403 404 405 406 407 408
            av_log(vc->avccontext, AV_LOG_ERROR, "Codebook lookup type not supported. \n");
            goto error;
        }

// Initialize VLC table
        if (ff_vorbis_len2vlc(tmp_vlc_bits, tmp_vlc_codes, entries)) {
            av_log(vc->avccontext, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n");
            goto error;
        }
409 410 411 412
        codebook_setup->maxdepth = 0;
        for (t = 0; t < entries; ++t)
            if (tmp_vlc_bits[t] >= codebook_setup->maxdepth)
                codebook_setup->maxdepth = tmp_vlc_bits[t];
413

414 415
        if (codebook_setup->maxdepth > 3 * V_NB_BITS)
            codebook_setup->nb_bits = V_NB_BITS2;
416
        else
417
            codebook_setup->nb_bits = V_NB_BITS;
418

419
        codebook_setup->maxdepth = (codebook_setup->maxdepth+codebook_setup->nb_bits - 1) / codebook_setup->nb_bits;
420 421 422 423 424 425 426 427 428

        if (init_vlc(&codebook_setup->vlc, codebook_setup->nb_bits, entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits), sizeof(*tmp_vlc_bits), tmp_vlc_codes, sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes), INIT_VLC_LE)) {
            av_log(vc->avccontext, AV_LOG_ERROR, " Error generating vlc tables. \n");
            goto error;
        }
    }

    av_free(tmp_vlc_bits);
    av_free(tmp_vlc_codes);
429
    av_free(codebook_multiplicands);
430 431 432 433 434 435
    return 0;

// Error:
error:
    av_free(tmp_vlc_bits);
    av_free(tmp_vlc_codes);
436
    av_free(codebook_multiplicands);
437
    return -1;
438 439 440 441
}

// Process time domain transforms part (unused in Vorbis I)

442 443
static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc)
{
444
    GetBitContext *gb = &vc->gb;
445
    uint_fast8_t i;
446
    uint_fast8_t vorbis_time_count = get_bits(gb, 6) + 1;
447

448 449
    for (i = 0; i < vorbis_time_count; ++i) {
        uint_fast16_t vorbis_tdtransform = get_bits(gb, 16);
450 451 452 453 454

        AV_DEBUG(" Vorbis time domain transform %d: %d \n", vorbis_time_count, vorbis_tdtransform);

        if (vorbis_tdtransform) {
            av_log(vc->avccontext, AV_LOG_ERROR, "Vorbis time domain transform data nonzero. \n");
455
            return -1;
456 457 458 459 460 461 462
        }
    }
    return 0;
}

// Process floors part

463 464
static int vorbis_floor0_decode(vorbis_context *vc,
                                vorbis_floor_data *vfu, float *vec);
465
static void create_map(vorbis_context *vc, uint_fast8_t floor_number);
466 467
static int vorbis_floor1_decode(vorbis_context *vc,
                                vorbis_floor_data *vfu, float *vec);
468 469
static int vorbis_parse_setup_hdr_floors(vorbis_context *vc)
{
470
    GetBitContext *gb = &vc->gb;
471
    int i,j,k;
472

473
    vc->floor_count = get_bits(gb, 6) + 1;
474

475
    vc->floors = av_mallocz(vc->floor_count * sizeof(vorbis_floor));
476

477 478
    for (i = 0; i < vc->floor_count; ++i) {
        vorbis_floor *floor_setup = &vc->floors[i];
479

480
        floor_setup->floor_type = get_bits(gb, 16);
481 482 483

        AV_DEBUG(" %d. floor type %d \n", i, floor_setup->floor_type);

484
        if (floor_setup->floor_type == 1) {
485
            int maximum_class = -1;
486
            uint_fast8_t  rangebits;
487
            uint_fast32_t rangemax;
488
            uint_fast16_t floor1_values = 2;
489

490
            floor_setup->decode = vorbis_floor1_decode;
491

492
            floor_setup->data.t1.partitions = get_bits(gb, 5);
493 494 495

            AV_DEBUG(" %d.floor: %d partitions \n", i, floor_setup->data.t1.partitions);

496 497 498 499
            for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
                floor_setup->data.t1.partition_class[j] = get_bits(gb, 4);
                if (floor_setup->data.t1.partition_class[j] > maximum_class)
                    maximum_class = floor_setup->data.t1.partition_class[j];
500 501 502 503 504 505 506

                AV_DEBUG(" %d. floor %d partition class %d \n", i, j, floor_setup->data.t1.partition_class[j]);

            }

            AV_DEBUG(" maximum class %d \n", maximum_class);

507 508 509
            for (j = 0; j <= maximum_class; ++j) {
                floor_setup->data.t1.class_dimensions[j] = get_bits(gb, 3) + 1;
                floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2);
510 511 512 513

                AV_DEBUG(" %d floor %d class dim: %d subclasses %d \n", i, j, floor_setup->data.t1.class_dimensions[j], floor_setup->data.t1.class_subclasses[j]);

                if (floor_setup->data.t1.class_subclasses[j]) {
514
                    GET_VALIDATED_INDEX(floor_setup->data.t1.class_masterbook[j], 8, vc->codebook_count)
515 516 517 518

                    AV_DEBUG("   masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]);
                }

519 520
                for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) {
                    int16_t bits = get_bits(gb, 8) - 1;
521 522
                    if (bits != -1)
                        VALIDATE_INDEX(bits, vc->codebook_count)
523
                    floor_setup->data.t1.subclass_books[j][k] = bits;
524 525 526 527 528

                    AV_DEBUG("    book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]);
                }
            }

529 530
            floor_setup->data.t1.multiplier = get_bits(gb, 2) + 1;
            floor_setup->data.t1.x_list_dim = 2;
531

532
            for (j = 0; j < floor_setup->data.t1.partitions; ++j)
533 534
                floor_setup->data.t1.x_list_dim+=floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]];

535
            floor_setup->data.t1.list = av_mallocz(floor_setup->data.t1.x_list_dim * sizeof(vorbis_floor1_entry));
536 537


538
            rangebits = get_bits(gb, 4);
539 540 541 542 543 544 545
            rangemax = (1 << rangebits);
            if (rangemax > vc->blocksize[1] / 2) {
                av_log(vc->avccontext, AV_LOG_ERROR,
                       "Floor value is too large for blocksize: %d (%d)\n",
                       rangemax, vc->blocksize[1] / 2);
                return -1;
            }
546
            floor_setup->data.t1.list[0].x = 0;
547
            floor_setup->data.t1.list[1].x = rangemax;
548

549 550 551
            for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
                for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) {
                    floor_setup->data.t1.list[floor1_values].x = get_bits(gb, rangebits);
552 553 554 555 556 557 558

                    AV_DEBUG(" %d. floor1 Y coord. %d \n", floor1_values, floor_setup->data.t1.list[floor1_values].x);
                }
            }

// Precalculate order of x coordinates - needed for decode
            ff_vorbis_ready_floor1_list(floor_setup->data.t1.list, floor_setup->data.t1.x_list_dim);
559 560
        } else if (floor_setup->floor_type == 0) {
            uint_fast8_t max_codebook_dim = 0;
561

562
            floor_setup->decode = vorbis_floor0_decode;
563

564 565 566 567
            floor_setup->data.t0.order          = get_bits(gb,  8);
            floor_setup->data.t0.rate           = get_bits(gb, 16);
            floor_setup->data.t0.bark_map_size  = get_bits(gb, 16);
            floor_setup->data.t0.amplitude_bits = get_bits(gb,  6);
568 569 570 571 572
            /* zero would result in a div by zero later *
             * 2^0 - 1 == 0                             */
            if (floor_setup->data.t0.amplitude_bits == 0) {
              av_log(vc->avccontext, AV_LOG_ERROR,
                     "Floor 0 amplitude bits is 0.\n");
573
              return -1;
574
            }
575 576
            floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
            floor_setup->data.t0.num_books        = get_bits(gb, 4) + 1;
577 578

            /* allocate mem for booklist */
579
            floor_setup->data.t0.book_list =
580
                av_malloc(floor_setup->data.t0.num_books);
581
            if (!floor_setup->data.t0.book_list)
582
                return -1;
583 584 585 586
            /* read book indexes */
            {
                int idx;
                uint_fast8_t book_idx;
587
                for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
588 589
                    GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count)
                    floor_setup->data.t0.book_list[idx] = book_idx;
590
                    if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
591
                        max_codebook_dim = vc->codebooks[book_idx].dimensions;
592 593 594
                }
            }

595
            create_map(vc, i);
596

597 598 599 600 601 602 603
            /* codebook dim is for padding if codebook dim doesn't *
             * divide order+1 then we need to read more data       */
            floor_setup->data.t0.lsp =
                av_malloc((floor_setup->data.t0.order+1 + max_codebook_dim)
                          * sizeof(float));
            if (!floor_setup->data.t0.lsp)
                return -1;
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

#ifdef V_DEBUG /* debug output parsed headers */
            AV_DEBUG("floor0 order: %u\n", floor_setup->data.t0.order);
            AV_DEBUG("floor0 rate: %u\n", floor_setup->data.t0.rate);
            AV_DEBUG("floor0 bark map size: %u\n",
              floor_setup->data.t0.bark_map_size);
            AV_DEBUG("floor0 amplitude bits: %u\n",
              floor_setup->data.t0.amplitude_bits);
            AV_DEBUG("floor0 amplitude offset: %u\n",
              floor_setup->data.t0.amplitude_offset);
            AV_DEBUG("floor0 number of books: %u\n",
              floor_setup->data.t0.num_books);
            AV_DEBUG("floor0 book list pointer: %p\n",
              floor_setup->data.t0.book_list);
            {
              int idx;
620 621 622 623
              for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
                  AV_DEBUG("  Book %d: %u\n",
                           idx+1,
                           floor_setup->data.t0.book_list[idx]);
624 625 626
              }
            }
#endif
627
        } else {
628
            av_log(vc->avccontext, AV_LOG_ERROR, "Invalid floor type!\n");
629
            return -1;
630 631 632 633 634 635 636
        }
    }
    return 0;
}

// Process residues part

637 638
static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
{
639
    GetBitContext *gb = &vc->gb;
640 641
    uint_fast8_t i, j, k;

642 643
    vc->residue_count = get_bits(gb, 6)+1;
    vc->residues      = av_mallocz(vc->residue_count * sizeof(vorbis_residue));
644 645 646

    AV_DEBUG(" There are %d residues. \n", vc->residue_count);

647 648
    for (i = 0; i < vc->residue_count; ++i) {
        vorbis_residue *res_setup = &vc->residues[i];
649 650 651 652
        uint_fast8_t cascade[64];
        uint_fast8_t high_bits;
        uint_fast8_t low_bits;

653
        res_setup->type = get_bits(gb, 16);
654 655 656

        AV_DEBUG(" %d. residue type %d \n", i, res_setup->type);

657 658 659
        res_setup->begin          = get_bits(gb, 24);
        res_setup->end            = get_bits(gb, 24);
        res_setup->partition_size = get_bits(gb, 24) + 1;
660
        /* Validations to prevent a buffer overflow later. */
661
        if (res_setup->begin>res_setup->end ||
662
            res_setup->end > vc->avccontext->channels * vc->blocksize[1] / 2 ||
663
            (res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
664
            av_log(vc->avccontext, AV_LOG_ERROR, "partition out of bounds: type, begin, end, size, blocksize: %"PRIdFAST16", %"PRIdFAST32", %"PRIdFAST32", %u, %"PRIdFAST32"\n", res_setup->type, res_setup->begin, res_setup->end, res_setup->partition_size, vc->blocksize[1] / 2);
665
            return -1;
666 667
        }

668
        res_setup->classifications = get_bits(gb, 6) + 1;
669
        GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
670

671 672 673 674 675
        res_setup->ptns_to_read =
            (res_setup->end - res_setup->begin) / res_setup->partition_size;
        res_setup->classifs = av_malloc(res_setup->ptns_to_read *
                                        vc->audio_channels *
                                        sizeof(*res_setup->classifs));
676 677
        if (!res_setup->classifs)
            return AVERROR(ENOMEM);
678

679 680 681
        AV_DEBUG("    begin %d end %d part.size %d classif.s %d classbook %d \n", res_setup->begin, res_setup->end, res_setup->partition_size,
          res_setup->classifications, res_setup->classbook);

682 683 684
        for (j = 0; j < res_setup->classifications; ++j) {
            high_bits = 0;
            low_bits  = get_bits(gb, 3);
685
            if (get_bits1(gb))
686 687
                high_bits = get_bits(gb, 5);
            cascade[j] = (high_bits << 3) + low_bits;
688 689 690 691

            AV_DEBUG("     %d class casscade depth: %d \n", j, ilog(cascade[j]));
        }

692 693 694 695
        res_setup->maxpass = 0;
        for (j = 0; j < res_setup->classifications; ++j) {
            for (k = 0; k < 8; ++k) {
                if (cascade[j]&(1 << k)) {
696
                    GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
697 698 699

                    AV_DEBUG("     %d class casscade depth %d book: %d \n", j, k, res_setup->books[j][k]);

700
                    if (k>res_setup->maxpass)
701
                        res_setup->maxpass = k;
702
                } else {
703
                    res_setup->books[j][k] = -1;
704 705 706 707 708 709 710 711 712
                }
            }
        }
    }
    return 0;
}

// Process mappings part

713 714
static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
{
715
    GetBitContext *gb = &vc->gb;
716 717
    uint_fast8_t i, j;

718 719
    vc->mapping_count = get_bits(gb, 6)+1;
    vc->mappings      = av_mallocz(vc->mapping_count * sizeof(vorbis_mapping));
720 721 722

    AV_DEBUG(" There are %d mappings. \n", vc->mapping_count);

723 724
    for (i = 0; i < vc->mapping_count; ++i) {
        vorbis_mapping *mapping_setup = &vc->mappings[i];
725 726 727

        if (get_bits(gb, 16)) {
            av_log(vc->avccontext, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
728
            return -1;
729 730
        }
        if (get_bits1(gb)) {
731
            mapping_setup->submaps = get_bits(gb, 4) + 1;
732
        } else {
733
            mapping_setup->submaps = 1;
734 735 736
        }

        if (get_bits1(gb)) {
737 738 739 740
            mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
            mapping_setup->magnitude      = av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t));
            mapping_setup->angle          = av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t));
            for (j = 0; j < mapping_setup->coupling_steps; ++j) {
741 742
                GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
                GET_VALIDATED_INDEX(mapping_setup->angle[j],     ilog(vc->audio_channels - 1), vc->audio_channels)
743 744
            }
        } else {
745
            mapping_setup->coupling_steps = 0;
746 747 748 749
        }

        AV_DEBUG("   %d mapping coupling steps: %d \n", i, mapping_setup->coupling_steps);

750
        if (get_bits(gb, 2)) {
751
            av_log(vc->avccontext, AV_LOG_ERROR, "%d. mapping setup data invalid. \n", i);
752
            return -1; // following spec.
753 754 755
        }

        if (mapping_setup->submaps>1) {
756 757 758
            mapping_setup->mux = av_mallocz(vc->audio_channels * sizeof(uint_fast8_t));
            for (j = 0; j < vc->audio_channels; ++j)
                mapping_setup->mux[j] = get_bits(gb, 4);
759 760
        }

761
        for (j = 0; j < mapping_setup->submaps; ++j) {
762
            skip_bits(gb, 8); // FIXME check?
763 764
            GET_VALIDATED_INDEX(mapping_setup->submap_floor[j],   8, vc->floor_count)
            GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
765 766 767 768 769 770 771 772 773

            AV_DEBUG("   %d mapping %d submap : floor %d, residue %d \n", i, j, mapping_setup->submap_floor[j], mapping_setup->submap_residue[j]);
        }
    }
    return 0;
}

// Process modes part

774
static void create_map(vorbis_context *vc, uint_fast8_t floor_number)
775
{
776 777
    vorbis_floor *floors = vc->floors;
    vorbis_floor0 *vf;
778 779
    int idx;
    int_fast8_t blockflag;
780
    int_fast32_t *map;
781 782
    int_fast32_t n; //TODO: could theoretically be smaller?

783 784 785 786
    for (blockflag = 0; blockflag < 2; ++blockflag) {
        n = vc->blocksize[blockflag] / 2;
        floors[floor_number].data.t0.map[blockflag] =
            av_malloc((n+1) * sizeof(int_fast32_t)); // n + sentinel
787

788 789
        map =  floors[floor_number].data.t0.map[blockflag];
        vf  = &floors[floor_number].data.t0;
790

791 792 793 794 795 796 797 798 799
        for (idx = 0; idx < n; ++idx) {
            map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
                             ((vf->bark_map_size) /
                              BARK(vf->rate / 2.0f)));
            if (vf->bark_map_size-1 < map[idx])
                map[idx] = vf->bark_map_size - 1;
        }
        map[n] = -1;
        vf->map_size[blockflag] = n;
800 801 802
    }

#   ifdef V_DEBUG
803
    for (idx = 0; idx <= n; ++idx) {
804 805 806 807 808 809
        AV_DEBUG("floor0 map: map at pos %d is %d\n",
                 idx, map[idx]);
    }
#   endif
}

810 811
static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
{
812
    GetBitContext *gb = &vc->gb;
813 814
    uint_fast8_t i;

815 816
    vc->mode_count = get_bits(gb, 6) + 1;
    vc->modes      = av_mallocz(vc->mode_count * sizeof(vorbis_mode));
817 818 819

    AV_DEBUG(" There are %d modes.\n", vc->mode_count);

820 821
    for (i = 0; i < vc->mode_count; ++i) {
        vorbis_mode *mode_setup = &vc->modes[i];
822

823 824 825
        mode_setup->blockflag     = get_bits1(gb);
        mode_setup->windowtype    = get_bits(gb, 16); //FIXME check
        mode_setup->transformtype = get_bits(gb, 16); //FIXME check
826
        GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
827 828 829 830 831 832 833 834

        AV_DEBUG(" %d mode: blockflag %d, windowtype %d, transformtype %d, mapping %d \n", i, mode_setup->blockflag, mode_setup->windowtype, mode_setup->transformtype, mode_setup->mapping);
    }
    return 0;
}

// Process the whole setup header using the functions above

835 836
static int vorbis_parse_setup_hdr(vorbis_context *vc)
{
837
    GetBitContext *gb = &vc->gb;
838

839 840 841
    if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
        (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
        (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
842
        av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
843
        return -1;
844 845 846 847
    }

    if (vorbis_parse_setup_hdr_codebooks(vc)) {
        av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
848
        return -2;
849 850 851
    }
    if (vorbis_parse_setup_hdr_tdtransforms(vc)) {
        av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
852
        return -3;
853 854 855
    }
    if (vorbis_parse_setup_hdr_floors(vc)) {
        av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
856
        return -4;
857 858 859
    }
    if (vorbis_parse_setup_hdr_residues(vc)) {
        av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");