00001
00023 #undef V_DEBUG
00024
00025
00026
00027 #include <math.h>
00028
00029 #define ALT_BITSTREAM_READER_LE
00030 #include "avcodec.h"
00031 #include "get_bits.h"
00032 #include "dsputil.h"
00033 #include "fft.h"
00034
00035 #include "vorbis.h"
00036 #include "xiph.h"
00037
00038 #define V_NB_BITS 8
00039 #define V_NB_BITS2 11
00040 #define V_MAX_VLCS (1 << 16)
00041 #define V_MAX_PARTITIONS (1 << 20)
00042
00043 #ifndef V_DEBUG
00044 #define AV_DEBUG(...)
00045 #endif
00046
00047 #undef NDEBUG
00048 #include <assert.h>
00049
00050 typedef struct {
00051 uint_fast8_t dimensions;
00052 uint_fast8_t lookup_type;
00053 uint_fast8_t maxdepth;
00054 VLC vlc;
00055 float *codevectors;
00056 unsigned int nb_bits;
00057 } vorbis_codebook;
00058
00059 typedef union vorbis_floor_u vorbis_floor_data;
00060 typedef struct vorbis_floor0_s vorbis_floor0;
00061 typedef struct vorbis_floor1_s vorbis_floor1;
00062 struct vorbis_context_s;
00063 typedef
00064 int (* vorbis_floor_decode_func)
00065 (struct vorbis_context_s *, vorbis_floor_data *, float *);
00066 typedef struct {
00067 uint_fast8_t floor_type;
00068 vorbis_floor_decode_func decode;
00069 union vorbis_floor_u {
00070 struct vorbis_floor0_s {
00071 uint_fast8_t order;
00072 uint_fast16_t rate;
00073 uint_fast16_t bark_map_size;
00074 int_fast32_t *map[2];
00075 uint_fast32_t map_size[2];
00076 uint_fast8_t amplitude_bits;
00077 uint_fast8_t amplitude_offset;
00078 uint_fast8_t num_books;
00079 uint_fast8_t *book_list;
00080 float *lsp;
00081 } t0;
00082 struct vorbis_floor1_s {
00083 uint_fast8_t partitions;
00084 uint_fast8_t maximum_class;
00085 uint_fast8_t partition_class[32];
00086 uint_fast8_t class_dimensions[16];
00087 uint_fast8_t class_subclasses[16];
00088 uint_fast8_t class_masterbook[16];
00089 int_fast16_t subclass_books[16][8];
00090 uint_fast8_t multiplier;
00091 uint_fast16_t x_list_dim;
00092 vorbis_floor1_entry *list;
00093 } t1;
00094 } data;
00095 } vorbis_floor;
00096
00097 typedef struct {
00098 uint_fast16_t type;
00099 uint_fast32_t begin;
00100 uint_fast32_t end;
00101 uint_fast32_t partition_size;
00102 uint_fast8_t classifications;
00103 uint_fast8_t classbook;
00104 int_fast16_t books[64][8];
00105 uint_fast8_t maxpass;
00106 } vorbis_residue;
00107
00108 typedef struct {
00109 uint_fast8_t submaps;
00110 uint_fast16_t coupling_steps;
00111 uint_fast8_t *magnitude;
00112 uint_fast8_t *angle;
00113 uint_fast8_t *mux;
00114 uint_fast8_t submap_floor[16];
00115 uint_fast8_t submap_residue[16];
00116 } vorbis_mapping;
00117
00118 typedef struct {
00119 uint_fast8_t blockflag;
00120 uint_fast16_t windowtype;
00121 uint_fast16_t transformtype;
00122 uint_fast8_t mapping;
00123 } vorbis_mode;
00124
00125 typedef struct vorbis_context_s {
00126 AVCodecContext *avccontext;
00127 GetBitContext gb;
00128 DSPContext dsp;
00129
00130 FFTContext mdct[2];
00131 uint_fast8_t first_frame;
00132 uint_fast32_t version;
00133 uint_fast8_t audio_channels;
00134 uint_fast32_t audio_samplerate;
00135 uint_fast32_t bitrate_maximum;
00136 uint_fast32_t bitrate_nominal;
00137 uint_fast32_t bitrate_minimum;
00138 uint_fast32_t blocksize[2];
00139 const float *win[2];
00140 uint_fast16_t codebook_count;
00141 vorbis_codebook *codebooks;
00142 uint_fast8_t floor_count;
00143 vorbis_floor *floors;
00144 uint_fast8_t residue_count;
00145 vorbis_residue *residues;
00146 uint_fast8_t mapping_count;
00147 vorbis_mapping *mappings;
00148 uint_fast8_t mode_count;
00149 vorbis_mode *modes;
00150 uint_fast8_t mode_number;
00151 uint_fast8_t previous_window;
00152 float *channel_residues;
00153 float *channel_floors;
00154 float *saved;
00155 uint_fast32_t add_bias;
00156 uint_fast32_t exp_bias;
00157 } vorbis_context;
00158
00159
00160
00161 #define BARK(x) \
00162 (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x))
00163
00164 static const char idx_err_str[] = "Index value %d out of range (0 - %d) for %s at %s:%i\n";
00165 #define VALIDATE_INDEX(idx, limit) \
00166 if (idx >= limit) {\
00167 av_log(vc->avccontext, AV_LOG_ERROR,\
00168 idx_err_str,\
00169 (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\
00170 return -1;\
00171 }
00172 #define GET_VALIDATED_INDEX(idx, bits, limit) \
00173 {\
00174 idx = get_bits(gb, bits);\
00175 VALIDATE_INDEX(idx, limit)\
00176 }
00177
00178 static float vorbisfloat2float(uint_fast32_t val)
00179 {
00180 double mant = val & 0x1fffff;
00181 long exp = (val & 0x7fe00000L) >> 21;
00182 if (val & 0x80000000)
00183 mant = -mant;
00184 return ldexp(mant, exp - 20 - 768);
00185 }
00186
00187
00188
00189
00190 static void vorbis_free(vorbis_context *vc)
00191 {
00192 int_fast16_t i;
00193
00194 av_freep(&vc->channel_residues);
00195 av_freep(&vc->channel_floors);
00196 av_freep(&vc->saved);
00197
00198 av_freep(&vc->residues);
00199 av_freep(&vc->modes);
00200
00201 ff_mdct_end(&vc->mdct[0]);
00202 ff_mdct_end(&vc->mdct[1]);
00203
00204 for (i = 0; i < vc->codebook_count; ++i) {
00205 av_free(vc->codebooks[i].codevectors);
00206 free_vlc(&vc->codebooks[i].vlc);
00207 }
00208 av_freep(&vc->codebooks);
00209
00210 for (i = 0; i < vc->floor_count; ++i) {
00211 if (vc->floors[i].floor_type == 0) {
00212 av_free(vc->floors[i].data.t0.map[0]);
00213 av_free(vc->floors[i].data.t0.map[1]);
00214 av_free(vc->floors[i].data.t0.book_list);
00215 av_free(vc->floors[i].data.t0.lsp);
00216 } else {
00217 av_free(vc->floors[i].data.t1.list);
00218 }
00219 }
00220 av_freep(&vc->floors);
00221
00222 for (i = 0; i < vc->mapping_count; ++i) {
00223 av_free(vc->mappings[i].magnitude);
00224 av_free(vc->mappings[i].angle);
00225 av_free(vc->mappings[i].mux);
00226 }
00227 av_freep(&vc->mappings);
00228 }
00229
00230
00231
00232
00233
00234 static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc)
00235 {
00236 uint_fast16_t cb;
00237 uint8_t *tmp_vlc_bits;
00238 uint32_t *tmp_vlc_codes;
00239 GetBitContext *gb = &vc->gb;
00240
00241 vc->codebook_count = get_bits(gb, 8) + 1;
00242
00243 AV_DEBUG(" Codebooks: %d \n", vc->codebook_count);
00244
00245 vc->codebooks = av_mallocz(vc->codebook_count * sizeof(vorbis_codebook));
00246 tmp_vlc_bits = av_mallocz(V_MAX_VLCS * sizeof(uint8_t));
00247 tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(uint32_t));
00248
00249 for (cb = 0; cb < vc->codebook_count; ++cb) {
00250 vorbis_codebook *codebook_setup = &vc->codebooks[cb];
00251 uint_fast8_t ordered;
00252 uint_fast32_t t, used_entries = 0;
00253 uint_fast32_t entries;
00254
00255 AV_DEBUG(" %d. Codebook \n", cb);
00256
00257 if (get_bits(gb, 24) != 0x564342) {
00258 av_log(vc->avccontext, AV_LOG_ERROR, " %"PRIdFAST16". Codebook setup data corrupt. \n", cb);
00259 goto error;
00260 }
00261
00262 codebook_setup->dimensions=get_bits(gb, 16);
00263 if (codebook_setup->dimensions > 16 || codebook_setup->dimensions == 0) {
00264 av_log(vc->avccontext, AV_LOG_ERROR, " %"PRIdFAST16". Codebook's dimension is invalid (%d). \n", cb, codebook_setup->dimensions);
00265 goto error;
00266 }
00267 entries = get_bits(gb, 24);
00268 if (entries > V_MAX_VLCS) {
00269 av_log(vc->avccontext, AV_LOG_ERROR, " %"PRIdFAST16". Codebook has too many entries (%"PRIdFAST32"). \n", cb, entries);
00270 goto error;
00271 }
00272
00273 ordered = get_bits1(gb);
00274
00275 AV_DEBUG(" codebook_dimensions %d, codebook_entries %d \n", codebook_setup->dimensions, entries);
00276
00277 if (!ordered) {
00278 uint_fast16_t ce;
00279 uint_fast8_t flag;
00280 uint_fast8_t sparse = get_bits1(gb);
00281
00282 AV_DEBUG(" not ordered \n");
00283
00284 if (sparse) {
00285 AV_DEBUG(" sparse \n");
00286
00287 used_entries = 0;
00288 for (ce = 0; ce < entries; ++ce) {
00289 flag = get_bits1(gb);
00290 if (flag) {
00291 tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
00292 ++used_entries;
00293 } else
00294 tmp_vlc_bits[ce] = 0;
00295 }
00296 } else {
00297 AV_DEBUG(" not sparse \n");
00298
00299 used_entries = entries;
00300 for (ce = 0; ce < entries; ++ce)
00301 tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
00302 }
00303 } else {
00304 uint_fast16_t current_entry = 0;
00305 uint_fast8_t current_length = get_bits(gb, 5)+1;
00306
00307 AV_DEBUG(" ordered, current length: %d \n", current_length);
00308
00309 used_entries = entries;
00310 for (; current_entry < used_entries && current_length <= 32; ++current_length) {
00311 uint_fast16_t i, number;
00312
00313 AV_DEBUG(" number bits: %d ", ilog(entries - current_entry));
00314
00315 number = get_bits(gb, ilog(entries - current_entry));
00316
00317 AV_DEBUG(" number: %d \n", number);
00318
00319 for (i = current_entry; i < number+current_entry; ++i)
00320 if (i < used_entries)
00321 tmp_vlc_bits[i] = current_length;
00322
00323 current_entry+=number;
00324 }
00325 if (current_entry>used_entries) {
00326 av_log(vc->avccontext, AV_LOG_ERROR, " More codelengths than codes in codebook. \n");
00327 goto error;
00328 }
00329 }
00330
00331 codebook_setup->lookup_type = get_bits(gb, 4);
00332
00333 AV_DEBUG(" lookup type: %d : %s \n", codebook_setup->lookup_type, codebook_setup->lookup_type ? "vq" : "no lookup");
00334
00335
00336
00337 if (codebook_setup->lookup_type == 1) {
00338 uint_fast16_t i, j, k;
00339 uint_fast16_t codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions);
00340 uint_fast16_t codebook_multiplicands[codebook_lookup_values];
00341
00342 float codebook_minimum_value = vorbisfloat2float(get_bits_long(gb, 32));
00343 float codebook_delta_value = vorbisfloat2float(get_bits_long(gb, 32));
00344 uint_fast8_t codebook_value_bits = get_bits(gb, 4)+1;
00345 uint_fast8_t codebook_sequence_p = get_bits1(gb);
00346
00347 AV_DEBUG(" We expect %d numbers for building the codevectors. \n", codebook_lookup_values);
00348 AV_DEBUG(" delta %f minmum %f \n", codebook_delta_value, codebook_minimum_value);
00349
00350 for (i = 0; i < codebook_lookup_values; ++i) {
00351 codebook_multiplicands[i] = get_bits(gb, codebook_value_bits);
00352
00353 AV_DEBUG(" multiplicands*delta+minmum : %e \n", (float)codebook_multiplicands[i]*codebook_delta_value+codebook_minimum_value);
00354 AV_DEBUG(" multiplicand %d \n", codebook_multiplicands[i]);
00355 }
00356
00357
00358 codebook_setup->codevectors = used_entries ? av_mallocz(used_entries*codebook_setup->dimensions * sizeof(float)) : NULL;
00359 for (j = 0, i = 0; i < entries; ++i) {
00360 uint_fast8_t dim = codebook_setup->dimensions;
00361
00362 if (tmp_vlc_bits[i]) {
00363 float last = 0.0;
00364 uint_fast32_t lookup_offset = i;
00365
00366 #ifdef V_DEBUG
00367 av_log(vc->avccontext, AV_LOG_INFO, "Lookup offset %d ,", i);
00368 #endif
00369
00370 for (k = 0; k < dim; ++k) {
00371 uint_fast32_t multiplicand_offset = lookup_offset % codebook_lookup_values;
00372 codebook_setup->codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;
00373 if (codebook_sequence_p)
00374 last = codebook_setup->codevectors[j * dim + k];
00375 lookup_offset/=codebook_lookup_values;
00376 }
00377 tmp_vlc_bits[j] = tmp_vlc_bits[i];
00378
00379 #ifdef V_DEBUG
00380 av_log(vc->avccontext, AV_LOG_INFO, "real lookup offset %d, vector: ", j);
00381 for (k = 0; k < dim; ++k)
00382 av_log(vc->avccontext, AV_LOG_INFO, " %f ", codebook_setup->codevectors[j * dim + k]);
00383 av_log(vc->avccontext, AV_LOG_INFO, "\n");
00384 #endif
00385
00386 ++j;
00387 }
00388 }
00389 if (j != used_entries) {
00390 av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \n");
00391 goto error;
00392 }
00393 entries = used_entries;
00394 } else if (codebook_setup->lookup_type >= 2) {
00395 av_log(vc->avccontext, AV_LOG_ERROR, "Codebook lookup type not supported. \n");
00396 goto error;
00397 }
00398
00399
00400 if (ff_vorbis_len2vlc(tmp_vlc_bits, tmp_vlc_codes, entries)) {
00401 av_log(vc->avccontext, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n");
00402 goto error;
00403 }
00404 codebook_setup->maxdepth = 0;
00405 for (t = 0; t < entries; ++t)
00406 if (tmp_vlc_bits[t] >= codebook_setup->maxdepth)
00407 codebook_setup->maxdepth = tmp_vlc_bits[t];
00408
00409 if (codebook_setup->maxdepth > 3 * V_NB_BITS)
00410 codebook_setup->nb_bits = V_NB_BITS2;
00411 else
00412 codebook_setup->nb_bits = V_NB_BITS;
00413
00414 codebook_setup->maxdepth = (codebook_setup->maxdepth+codebook_setup->nb_bits - 1) / codebook_setup->nb_bits;
00415
00416 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)) {
00417 av_log(vc->avccontext, AV_LOG_ERROR, " Error generating vlc tables. \n");
00418 goto error;
00419 }
00420 }
00421
00422 av_free(tmp_vlc_bits);
00423 av_free(tmp_vlc_codes);
00424 return 0;
00425
00426
00427 error:
00428 av_free(tmp_vlc_bits);
00429 av_free(tmp_vlc_codes);
00430 return -1;
00431 }
00432
00433
00434
00435 static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc)
00436 {
00437 GetBitContext *gb = &vc->gb;
00438 uint_fast8_t i;
00439 uint_fast8_t vorbis_time_count = get_bits(gb, 6) + 1;
00440
00441 for (i = 0; i < vorbis_time_count; ++i) {
00442 uint_fast16_t vorbis_tdtransform = get_bits(gb, 16);
00443
00444 AV_DEBUG(" Vorbis time domain transform %d: %d \n", vorbis_time_count, vorbis_tdtransform);
00445
00446 if (vorbis_tdtransform) {
00447 av_log(vc->avccontext, AV_LOG_ERROR, "Vorbis time domain transform data nonzero. \n");
00448 return -1;
00449 }
00450 }
00451 return 0;
00452 }
00453
00454
00455
00456 static int vorbis_floor0_decode(vorbis_context *vc,
00457 vorbis_floor_data *vfu, float *vec);
00458 static void create_map(vorbis_context *vc, uint_fast8_t floor_number);
00459 static int vorbis_floor1_decode(vorbis_context *vc,
00460 vorbis_floor_data *vfu, float *vec);
00461 static int vorbis_parse_setup_hdr_floors(vorbis_context *vc)
00462 {
00463 GetBitContext *gb = &vc->gb;
00464 uint_fast16_t i,j,k;
00465
00466 vc->floor_count = get_bits(gb, 6) + 1;
00467
00468 vc->floors = av_mallocz(vc->floor_count * sizeof(vorbis_floor));
00469
00470 for (i = 0; i < vc->floor_count; ++i) {
00471 vorbis_floor *floor_setup = &vc->floors[i];
00472
00473 floor_setup->floor_type = get_bits(gb, 16);
00474
00475 AV_DEBUG(" %d. floor type %d \n", i, floor_setup->floor_type);
00476
00477 if (floor_setup->floor_type == 1) {
00478 uint_fast8_t maximum_class = 0;
00479 uint_fast8_t rangebits;
00480 uint_fast32_t rangemax;
00481 uint_fast16_t floor1_values = 2;
00482
00483 floor_setup->decode = vorbis_floor1_decode;
00484
00485 floor_setup->data.t1.partitions = get_bits(gb, 5);
00486
00487 AV_DEBUG(" %d.floor: %d partitions \n", i, floor_setup->data.t1.partitions);
00488
00489 for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
00490 floor_setup->data.t1.partition_class[j] = get_bits(gb, 4);
00491 if (floor_setup->data.t1.partition_class[j] > maximum_class)
00492 maximum_class = floor_setup->data.t1.partition_class[j];
00493
00494 AV_DEBUG(" %d. floor %d partition class %d \n", i, j, floor_setup->data.t1.partition_class[j]);
00495
00496 }
00497
00498 AV_DEBUG(" maximum class %d \n", maximum_class);
00499
00500 floor_setup->data.t1.maximum_class = maximum_class;
00501
00502 for (j = 0; j <= maximum_class; ++j) {
00503 floor_setup->data.t1.class_dimensions[j] = get_bits(gb, 3) + 1;
00504 floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2);
00505
00506 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]);
00507
00508 if (floor_setup->data.t1.class_subclasses[j]) {
00509 GET_VALIDATED_INDEX(floor_setup->data.t1.class_masterbook[j], 8, vc->codebook_count)
00510
00511 AV_DEBUG(" masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]);
00512 }
00513
00514 for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) {
00515 int16_t bits = get_bits(gb, 8) - 1;
00516 if (bits != -1)
00517 VALIDATE_INDEX(bits, vc->codebook_count)
00518 floor_setup->data.t1.subclass_books[j][k] = bits;
00519
00520 AV_DEBUG(" book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]);
00521 }
00522 }
00523
00524 floor_setup->data.t1.multiplier = get_bits(gb, 2) + 1;
00525 floor_setup->data.t1.x_list_dim = 2;
00526
00527 for (j = 0; j < floor_setup->data.t1.partitions; ++j)
00528 floor_setup->data.t1.x_list_dim+=floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]];
00529
00530 floor_setup->data.t1.list = av_mallocz(floor_setup->data.t1.x_list_dim * sizeof(vorbis_floor1_entry));
00531
00532
00533 rangebits = get_bits(gb, 4);
00534 rangemax = (1 << rangebits);
00535 if (rangemax > vc->blocksize[1] / 2) {
00536 av_log(vc->avccontext, AV_LOG_ERROR,
00537 "Floor value is too large for blocksize: %d (%d)\n",
00538 rangemax, vc->blocksize[1] / 2);
00539 return -1;
00540 }
00541 floor_setup->data.t1.list[0].x = 0;
00542 floor_setup->data.t1.list[1].x = rangemax;
00543
00544 for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
00545 for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) {
00546 floor_setup->data.t1.list[floor1_values].x = get_bits(gb, rangebits);
00547
00548 AV_DEBUG(" %d. floor1 Y coord. %d \n", floor1_values, floor_setup->data.t1.list[floor1_values].x);
00549 }
00550 }
00551
00552
00553 ff_vorbis_ready_floor1_list(floor_setup->data.t1.list, floor_setup->data.t1.x_list_dim);
00554 } else if (floor_setup->floor_type == 0) {
00555 uint_fast8_t max_codebook_dim = 0;
00556
00557 floor_setup->decode = vorbis_floor0_decode;
00558
00559 floor_setup->data.t0.order = get_bits(gb, 8);
00560 floor_setup->data.t0.rate = get_bits(gb, 16);
00561 floor_setup->data.t0.bark_map_size = get_bits(gb, 16);
00562 floor_setup->data.t0.amplitude_bits = get_bits(gb, 6);
00563
00564
00565 if (floor_setup->data.t0.amplitude_bits == 0) {
00566 av_log(vc->avccontext, AV_LOG_ERROR,
00567 "Floor 0 amplitude bits is 0.\n");
00568 return -1;
00569 }
00570 floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
00571 floor_setup->data.t0.num_books = get_bits(gb, 4) + 1;
00572
00573
00574 floor_setup->data.t0.book_list =
00575 av_malloc(floor_setup->data.t0.num_books);
00576 if (!floor_setup->data.t0.book_list)
00577 return -1;
00578
00579 {
00580 int idx;
00581 uint_fast8_t book_idx;
00582 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
00583 GET_VALIDATED_INDEX(floor_setup->data.t0.book_list[idx], 8, vc->codebook_count)
00584 if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
00585 max_codebook_dim = vc->codebooks[book_idx].dimensions;
00586 }
00587 }
00588
00589 create_map(vc, i);
00590
00591
00592 {
00593
00594
00595 floor_setup->data.t0.lsp =
00596 av_malloc((floor_setup->data.t0.order+1 + max_codebook_dim)
00597 * sizeof(float));
00598 if (!floor_setup->data.t0.lsp)
00599 return -1;
00600 }
00601
00602 #ifdef V_DEBUG
00603 AV_DEBUG("floor0 order: %u\n", floor_setup->data.t0.order);
00604 AV_DEBUG("floor0 rate: %u\n", floor_setup->data.t0.rate);
00605 AV_DEBUG("floor0 bark map size: %u\n",
00606 floor_setup->data.t0.bark_map_size);
00607 AV_DEBUG("floor0 amplitude bits: %u\n",
00608 floor_setup->data.t0.amplitude_bits);
00609 AV_DEBUG("floor0 amplitude offset: %u\n",
00610 floor_setup->data.t0.amplitude_offset);
00611 AV_DEBUG("floor0 number of books: %u\n",
00612 floor_setup->data.t0.num_books);
00613 AV_DEBUG("floor0 book list pointer: %p\n",
00614 floor_setup->data.t0.book_list);
00615 {
00616 int idx;
00617 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
00618 AV_DEBUG(" Book %d: %u\n",
00619 idx+1,
00620 floor_setup->data.t0.book_list[idx]);
00621 }
00622 }
00623 #endif
00624 } else {
00625 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid floor type!\n");
00626 return -1;
00627 }
00628 }
00629 return 0;
00630 }
00631
00632
00633
00634 static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
00635 {
00636 GetBitContext *gb = &vc->gb;
00637 uint_fast8_t i, j, k;
00638
00639 vc->residue_count = get_bits(gb, 6)+1;
00640 vc->residues = av_mallocz(vc->residue_count * sizeof(vorbis_residue));
00641
00642 AV_DEBUG(" There are %d residues. \n", vc->residue_count);
00643
00644 for (i = 0; i < vc->residue_count; ++i) {
00645 vorbis_residue *res_setup = &vc->residues[i];
00646 uint_fast8_t cascade[64];
00647 uint_fast8_t high_bits;
00648 uint_fast8_t low_bits;
00649
00650 res_setup->type = get_bits(gb, 16);
00651
00652 AV_DEBUG(" %d. residue type %d \n", i, res_setup->type);
00653
00654 res_setup->begin = get_bits(gb, 24);
00655 res_setup->end = get_bits(gb, 24);
00656 res_setup->partition_size = get_bits(gb, 24) + 1;
00657
00658 if (res_setup->begin>res_setup->end ||
00659 res_setup->end>vc->blocksize[1] / (res_setup->type == 2 ? 1 : 2) ||
00660 (res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
00661 av_log(vc->avccontext, AV_LOG_ERROR, "partition out of bounds: type, begin, end, size, blocksize: %"PRIdFAST16", %"PRIdFAST32", %"PRIdFAST32", %"PRIdFAST32", %"PRIdFAST32"\n", res_setup->type, res_setup->begin, res_setup->end, res_setup->partition_size, vc->blocksize[1] / 2);
00662 return -1;
00663 }
00664
00665 res_setup->classifications = get_bits(gb, 6) + 1;
00666 GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
00667
00668 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,
00669 res_setup->classifications, res_setup->classbook);
00670
00671 for (j = 0; j < res_setup->classifications; ++j) {
00672 high_bits = 0;
00673 low_bits = get_bits(gb, 3);
00674 if (get_bits1(gb))
00675 high_bits = get_bits(gb, 5);
00676 cascade[j] = (high_bits << 3) + low_bits;
00677
00678 AV_DEBUG(" %d class casscade depth: %d \n", j, ilog(cascade[j]));
00679 }
00680
00681 res_setup->maxpass = 0;
00682 for (j = 0; j < res_setup->classifications; ++j) {
00683 for (k = 0; k < 8; ++k) {
00684 if (cascade[j]&(1 << k)) {
00685 GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
00686
00687 AV_DEBUG(" %d class casscade depth %d book: %d \n", j, k, res_setup->books[j][k]);
00688
00689 if (k>res_setup->maxpass)
00690 res_setup->maxpass = k;
00691 } else {
00692 res_setup->books[j][k] = -1;
00693 }
00694 }
00695 }
00696 }
00697 return 0;
00698 }
00699
00700
00701
00702 static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
00703 {
00704 GetBitContext *gb = &vc->gb;
00705 uint_fast8_t i, j;
00706
00707 vc->mapping_count = get_bits(gb, 6)+1;
00708 vc->mappings = av_mallocz(vc->mapping_count * sizeof(vorbis_mapping));
00709
00710 AV_DEBUG(" There are %d mappings. \n", vc->mapping_count);
00711
00712 for (i = 0; i < vc->mapping_count; ++i) {
00713 vorbis_mapping *mapping_setup = &vc->mappings[i];
00714
00715 if (get_bits(gb, 16)) {
00716 av_log(vc->avccontext, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
00717 return -1;
00718 }
00719 if (get_bits1(gb)) {
00720 mapping_setup->submaps = get_bits(gb, 4) + 1;
00721 } else {
00722 mapping_setup->submaps = 1;
00723 }
00724
00725 if (get_bits1(gb)) {
00726 mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
00727 mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t));
00728 mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t));
00729 for (j = 0; j < mapping_setup->coupling_steps; ++j) {
00730 GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
00731 GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels)
00732 }
00733 } else {
00734 mapping_setup->coupling_steps = 0;
00735 }
00736
00737 AV_DEBUG(" %d mapping coupling steps: %d \n", i, mapping_setup->coupling_steps);
00738
00739 if (get_bits(gb, 2)) {
00740 av_log(vc->avccontext, AV_LOG_ERROR, "%d. mapping setup data invalid. \n", i);
00741 return -1;
00742 }
00743
00744 if (mapping_setup->submaps>1) {
00745 mapping_setup->mux = av_mallocz(vc->audio_channels * sizeof(uint_fast8_t));
00746 for (j = 0; j < vc->audio_channels; ++j)
00747 mapping_setup->mux[j] = get_bits(gb, 4);
00748 }
00749
00750 for (j = 0; j < mapping_setup->submaps; ++j) {
00751 skip_bits(gb, 8);
00752 GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count)
00753 GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
00754
00755 AV_DEBUG(" %d mapping %d submap : floor %d, residue %d \n", i, j, mapping_setup->submap_floor[j], mapping_setup->submap_residue[j]);
00756 }
00757 }
00758 return 0;
00759 }
00760
00761
00762
00763 static void create_map(vorbis_context *vc, uint_fast8_t floor_number)
00764 {
00765 vorbis_floor *floors = vc->floors;
00766 vorbis_floor0 *vf;
00767 int idx;
00768 int_fast8_t blockflag;
00769 int_fast32_t *map;
00770 int_fast32_t n;
00771
00772 for (blockflag = 0; blockflag < 2; ++blockflag) {
00773 n = vc->blocksize[blockflag] / 2;
00774 floors[floor_number].data.t0.map[blockflag] =
00775 av_malloc((n+1) * sizeof(int_fast32_t));
00776
00777 map = floors[floor_number].data.t0.map[blockflag];
00778 vf = &floors[floor_number].data.t0;
00779
00780 for (idx = 0; idx < n; ++idx) {
00781 map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
00782 ((vf->bark_map_size) /
00783 BARK(vf->rate / 2.0f)));
00784 if (vf->bark_map_size-1 < map[idx])
00785 map[idx] = vf->bark_map_size - 1;
00786 }
00787 map[n] = -1;
00788 vf->map_size[blockflag] = n;
00789 }
00790
00791 # ifdef V_DEBUG
00792 for (idx = 0; idx <= n; ++idx) {
00793 AV_DEBUG("floor0 map: map at pos %d is %d\n",
00794 idx, map[idx]);
00795 }
00796 # endif
00797 }
00798
00799 static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
00800 {
00801 GetBitContext *gb = &vc->gb;
00802 uint_fast8_t i;
00803
00804 vc->mode_count = get_bits(gb, 6) + 1;
00805 vc->modes = av_mallocz(vc->mode_count * sizeof(vorbis_mode));
00806
00807 AV_DEBUG(" There are %d modes.\n", vc->mode_count);
00808
00809 for (i = 0; i < vc->mode_count; ++i) {
00810 vorbis_mode *mode_setup = &vc->modes[i];
00811
00812 mode_setup->blockflag = get_bits1(gb);
00813 mode_setup->windowtype = get_bits(gb, 16);
00814 mode_setup->transformtype = get_bits(gb, 16);
00815 GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
00816
00817 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);
00818 }
00819 return 0;
00820 }
00821
00822
00823
00824 static int vorbis_parse_setup_hdr(vorbis_context *vc)
00825 {
00826 GetBitContext *gb = &vc->gb;
00827
00828 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
00829 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
00830 (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
00831 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
00832 return -1;
00833 }
00834
00835 if (vorbis_parse_setup_hdr_codebooks(vc)) {
00836 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
00837 return -2;
00838 }
00839 if (vorbis_parse_setup_hdr_tdtransforms(vc)) {
00840 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
00841 return -3;
00842 }
00843 if (vorbis_parse_setup_hdr_floors(vc)) {
00844 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
00845 return -4;
00846 }
00847 if (vorbis_parse_setup_hdr_residues(vc)) {
00848 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
00849 return -5;
00850 }
00851 if (vorbis_parse_setup_hdr_mappings(vc)) {
00852 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
00853 return -6;
00854 }
00855 if (vorbis_parse_setup_hdr_modes(vc)) {
00856 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
00857 return -7;
00858 }
00859 if (!get_bits1(gb)) {
00860 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
00861 return -8;
00862 }
00863
00864 return 0;
00865 }
00866
00867
00868
00869 static int vorbis_parse_id_hdr(vorbis_context *vc)
00870 {
00871 GetBitContext *gb = &vc->gb;
00872 uint_fast8_t bl0, bl1;
00873
00874 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
00875 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
00876 (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
00877 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
00878 return -1;
00879 }
00880
00881 vc->version = get_bits_long(gb, 32);
00882 vc->audio_channels = get_bits(gb, 8);
00883 if (vc->audio_channels <= 0) {
00884 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid number of channels\n");
00885 return -1;
00886 }
00887 vc->audio_samplerate = get_bits_long(gb, 32);
00888 if (vc->audio_samplerate <= 0) {
00889 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid samplerate\n");
00890 return -1;
00891 }
00892 vc->bitrate_maximum = get_bits_long(gb, 32);
00893 vc->bitrate_nominal = get_bits_long(gb, 32);
00894 vc->bitrate_minimum = get_bits_long(gb, 32);
00895 bl0 = get_bits(gb, 4);
00896 bl1 = get_bits(gb, 4);
00897 vc->blocksize[0] = (1 << bl0);
00898 vc->blocksize[1] = (1 << bl1);
00899 if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
00900 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
00901 return -3;
00902 }
00903
00904 if (vc->blocksize[1] / 2 * vc->audio_channels * 2 > AVCODEC_MAX_AUDIO_FRAME_SIZE) {
00905 av_log(vc->avccontext, AV_LOG_ERROR, "Vorbis channel count makes "
00906 "output packets too large.\n");
00907 return -4;
00908 }
00909 vc->win[0] = ff_vorbis_vwin[bl0 - 6];
00910 vc->win[1] = ff_vorbis_vwin[bl1 - 6];
00911
00912 if ((get_bits1(gb)) == 0) {
00913 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
00914 return -2;
00915 }
00916
00917 vc->channel_residues = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(float));
00918 vc->channel_floors = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(float));
00919 vc->saved = av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(float));
00920 vc->previous_window = 0;
00921
00922 ff_mdct_init(&vc->mdct[0], bl0, 1, vc->exp_bias ? -(1 << 15) : -1.0);
00923 ff_mdct_init(&vc->mdct[1], bl1, 1, vc->exp_bias ? -(1 << 15) : -1.0);
00924
00925 AV_DEBUG(" vorbis version %d \n audio_channels %d \n audio_samplerate %d \n bitrate_max %d \n bitrate_nom %d \n bitrate_min %d \n blk_0 %d blk_1 %d \n ",
00926 vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
00927
00928
00929
00930
00931
00932
00933
00934
00935 return 0;
00936 }
00937
00938
00939
00940 static av_cold int vorbis_decode_init(AVCodecContext *avccontext)
00941 {
00942 vorbis_context *vc = avccontext->priv_data ;
00943 uint8_t *headers = avccontext->extradata;
00944 int headers_len = avccontext->extradata_size;
00945 uint8_t *header_start[3];
00946 int header_len[3];
00947 GetBitContext *gb = &(vc->gb);
00948 int hdr_type;
00949
00950 vc->avccontext = avccontext;
00951 dsputil_init(&vc->dsp, avccontext);
00952
00953 if (vc->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
00954 vc->add_bias = 385;
00955 vc->exp_bias = 0;
00956 } else {
00957 vc->add_bias = 0;
00958 vc->exp_bias = 15 << 23;
00959 }
00960
00961 if (!headers_len) {
00962 av_log(avccontext, AV_LOG_ERROR, "Extradata missing.\n");
00963 return -1;
00964 }
00965
00966 if (ff_split_xiph_headers(headers, headers_len, 30, header_start, header_len) < 0) {
00967 av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
00968 return -1;
00969 }
00970
00971 init_get_bits(gb, header_start[0], header_len[0]*8);
00972 hdr_type = get_bits(gb, 8);
00973 if (hdr_type != 1) {
00974 av_log(avccontext, AV_LOG_ERROR, "First header is not the id header.\n");
00975 return -1;
00976 }
00977 if (vorbis_parse_id_hdr(vc)) {
00978 av_log(avccontext, AV_LOG_ERROR, "Id header corrupt.\n");
00979 vorbis_free(vc);
00980 return -1;
00981 }
00982
00983 init_get_bits(gb, header_start[2], header_len[2]*8);
00984 hdr_type = get_bits(gb, 8);
00985 if (hdr_type != 5) {
00986 av_log(avccontext, AV_LOG_ERROR, "Third header is not the setup header.\n");
00987 vorbis_free(vc);
00988 return -1;
00989 }
00990 if (vorbis_parse_setup_hdr(vc)) {
00991 av_log(avccontext, AV_LOG_ERROR, "Setup header corrupt.\n");
00992 vorbis_free(vc);
00993 return -1;
00994 }
00995
00996 if (vc->audio_channels > 8)
00997 avccontext->channel_layout = 0;
00998 else
00999 avccontext->channel_layout = ff_vorbis_channel_layouts[vc->audio_channels - 1];
01000
01001 avccontext->channels = vc->audio_channels;
01002 avccontext->sample_rate = vc->audio_samplerate;
01003 avccontext->frame_size = FFMIN(vc->blocksize[0], vc->blocksize[1]) >> 2;
01004 avccontext->sample_fmt = SAMPLE_FMT_S16;
01005
01006 return 0 ;
01007 }
01008
01009
01010
01011
01012
01013 static int vorbis_floor0_decode(vorbis_context *vc,
01014 vorbis_floor_data *vfu, float *vec)
01015 {
01016 vorbis_floor0 *vf = &vfu->t0;
01017 float *lsp = vf->lsp;
01018 uint_fast32_t amplitude;
01019 uint_fast32_t book_idx;
01020 uint_fast8_t blockflag = vc->modes[vc->mode_number].blockflag;
01021
01022 amplitude = get_bits(&vc->gb, vf->amplitude_bits);
01023 if (amplitude > 0) {
01024 float last = 0;
01025 uint_fast16_t lsp_len = 0;
01026 uint_fast16_t idx;
01027 vorbis_codebook codebook;
01028
01029 book_idx = get_bits(&vc->gb, ilog(vf->num_books));
01030 if (book_idx >= vf->num_books) {
01031 av_log(vc->avccontext, AV_LOG_ERROR,
01032 "floor0 dec: booknumber too high!\n");
01033 book_idx = 0;
01034
01035 }
01036 AV_DEBUG("floor0 dec: booknumber: %u\n", book_idx);
01037 codebook = vc->codebooks[vf->book_list[book_idx]];
01038
01039 if (!codebook.codevectors)
01040 return -1;
01041
01042 while (lsp_len<vf->order) {
01043 int vec_off;
01044
01045 AV_DEBUG("floor0 dec: book dimension: %d\n", codebook.dimensions);
01046 AV_DEBUG("floor0 dec: maximum depth: %d\n", codebook.maxdepth);
01047
01048 vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
01049 codebook.nb_bits, codebook.maxdepth)
01050 * codebook.dimensions;
01051 AV_DEBUG("floor0 dec: vector offset: %d\n", vec_off);
01052
01053 for (idx = 0; idx < codebook.dimensions; ++idx)
01054 lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
01055 last = lsp[lsp_len+idx-1];
01056
01057 lsp_len += codebook.dimensions;
01058 }
01059 #ifdef V_DEBUG
01060
01061 {
01062 int idx;
01063 for (idx = 0; idx < lsp_len; ++idx)
01064 AV_DEBUG("floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
01065 }
01066 #endif
01067
01068
01069 {
01070 int i;
01071 int order = vf->order;
01072 float wstep = M_PI / vf->bark_map_size;
01073
01074 for (i = 0; i < order; i++)
01075 lsp[i] = 2.0f * cos(lsp[i]);
01076
01077 AV_DEBUG("floor0 synth: map_size = %d; m = %d; wstep = %f\n",
01078 vf->map_size, order, wstep);
01079
01080 i = 0;
01081 while (i < vf->map_size[blockflag]) {
01082 int j, iter_cond = vf->map[blockflag][i];
01083 float p = 0.5f;
01084 float q = 0.5f;
01085 float two_cos_w = 2.0f * cos(wstep * iter_cond);
01086
01087
01088 for (j = 0; j + 1 < order; j += 2) {
01089 q *= lsp[j] - two_cos_w;
01090 p *= lsp[j + 1] - two_cos_w;
01091 }
01092 if (j == order) {
01093 p *= p * (2.0f - two_cos_w);
01094 q *= q * (2.0f + two_cos_w);
01095 } else {
01096 q *= two_cos_w-lsp[j];
01097
01098
01099 p *= p * (4.f - two_cos_w * two_cos_w);
01100 q *= q;
01101 }
01102
01103
01104 {
01105 q = exp((((amplitude*vf->amplitude_offset) /
01106 (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
01107 - vf->amplitude_offset) * .11512925f);
01108 }
01109
01110
01111 do {
01112 vec[i] = q; ++i;
01113 } while (vf->map[blockflag][i] == iter_cond);
01114 }
01115 }
01116 } else {
01117
01118 return 1;
01119 }
01120
01121 AV_DEBUG(" Floor0 decoded\n");
01122
01123 return 0;
01124 }
01125
01126 static int vorbis_floor1_decode(vorbis_context *vc,
01127 vorbis_floor_data *vfu, float *vec)
01128 {
01129 vorbis_floor1 *vf = &vfu->t1;
01130 GetBitContext *gb = &vc->gb;
01131 uint_fast16_t range_v[4] = { 256, 128, 86, 64 };
01132 uint_fast16_t range = range_v[vf->multiplier-1];
01133 uint_fast16_t floor1_Y[vf->x_list_dim];
01134 uint_fast16_t floor1_Y_final[vf->x_list_dim];
01135 int floor1_flag[vf->x_list_dim];
01136 uint_fast8_t class_;
01137 uint_fast8_t cdim;
01138 uint_fast8_t cbits;
01139 uint_fast8_t csub;
01140 uint_fast8_t cval;
01141 int_fast16_t book;
01142 uint_fast16_t offset;
01143 uint_fast16_t i,j;
01144 int_fast16_t adx, ady, off, predicted;
01145 int_fast16_t dy, err;
01146
01147
01148 if (!get_bits1(gb))
01149 return 1;
01150
01151
01152
01153 floor1_Y[0] = get_bits(gb, ilog(range - 1));
01154 floor1_Y[1] = get_bits(gb, ilog(range - 1));
01155
01156 AV_DEBUG("floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
01157
01158 offset = 2;
01159 for (i = 0; i < vf->partitions; ++i) {
01160 class_ = vf->partition_class[i];
01161 cdim = vf->class_dimensions[class_];
01162 cbits = vf->class_subclasses[class_];
01163 csub = (1 << cbits) - 1;
01164 cval = 0;
01165
01166 AV_DEBUG("Cbits %d \n", cbits);
01167
01168 if (cbits)
01169 cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[class_]].vlc.table,
01170 vc->codebooks[vf->class_masterbook[class_]].nb_bits, 3);
01171
01172 for (j = 0; j < cdim; ++j) {
01173 book = vf->subclass_books[class_][cval & csub];
01174
01175 AV_DEBUG("book %d Cbits %d cval %d bits:%d \n", book, cbits, cval, get_bits_count(gb));
01176
01177 cval = cval >> cbits;
01178 if (book > -1) {
01179 floor1_Y[offset+j] = get_vlc2(gb, vc->codebooks[book].vlc.table,
01180 vc->codebooks[book].nb_bits, 3);
01181 } else {
01182 floor1_Y[offset+j] = 0;
01183 }
01184
01185 AV_DEBUG(" floor(%d) = %d \n", vf->list[offset+j].x, floor1_Y[offset+j]);
01186 }
01187 offset+=cdim;
01188 }
01189
01190
01191
01192 floor1_flag[0] = 1;
01193 floor1_flag[1] = 1;
01194 floor1_Y_final[0] = floor1_Y[0];
01195 floor1_Y_final[1] = floor1_Y[1];
01196
01197 for (i = 2; i < vf->x_list_dim; ++i) {
01198 uint_fast16_t val, highroom, lowroom, room;
01199 uint_fast16_t high_neigh_offs;
01200 uint_fast16_t low_neigh_offs;
01201
01202 low_neigh_offs = vf->list[i].low;
01203 high_neigh_offs = vf->list[i].high;
01204 dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs];
01205 adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
01206 ady = FFABS(dy);
01207 err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
01208 off = (int16_t)err / (int16_t)adx;
01209 if (dy < 0) {
01210 predicted = floor1_Y_final[low_neigh_offs] - off;
01211 } else {
01212 predicted = floor1_Y_final[low_neigh_offs] + off;
01213 }
01214
01215 val = floor1_Y[i];
01216 highroom = range-predicted;
01217 lowroom = predicted;
01218 if (highroom < lowroom) {
01219 room = highroom * 2;
01220 } else {
01221 room = lowroom * 2;
01222 }
01223 if (val) {
01224 floor1_flag[low_neigh_offs] = 1;
01225 floor1_flag[high_neigh_offs] = 1;
01226 floor1_flag[i] = 1;
01227 if (val >= room) {
01228 if (highroom > lowroom) {
01229 floor1_Y_final[i] = val - lowroom + predicted;
01230 } else {
01231 floor1_Y_final[i] = predicted - val + highroom - 1;
01232 }
01233 } else {
01234 if (val & 1) {
01235 floor1_Y_final[i] = predicted - (val + 1) / 2;
01236 } else {
01237 floor1_Y_final[i] = predicted + val / 2;
01238 }
01239 }
01240 } else {
01241 floor1_flag[i] = 0;
01242 floor1_Y_final[i] = predicted;
01243 }
01244
01245 AV_DEBUG(" Decoded floor(%d) = %d / val %d \n", vf->list[i].x, floor1_Y_final[i], val);
01246 }
01247
01248
01249
01250 ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x);
01251
01252 AV_DEBUG(" Floor decoded\n");
01253
01254 return 0;
01255 }
01256
01257
01258
01259 static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc,
01260 vorbis_residue *vr,
01261 uint_fast8_t ch,
01262 uint_fast8_t *do_not_decode,
01263 float *vec,
01264 uint_fast16_t vlen,
01265 int vr_type)
01266 {
01267 GetBitContext *gb = &vc->gb;
01268 uint_fast8_t c_p_c = vc->codebooks[vr->classbook].dimensions;
01269 uint_fast16_t n_to_read = vr->end-vr->begin;
01270 uint_fast16_t ptns_to_read = n_to_read/vr->partition_size;
01271 uint_fast8_t classifs[ptns_to_read*vc->audio_channels];
01272 uint_fast8_t pass;
01273 uint_fast8_t ch_used;
01274 uint_fast8_t i,j,l;
01275 uint_fast16_t k;
01276
01277 if (vr_type == 2) {
01278 for (j = 1; j < ch; ++j)
01279 do_not_decode[0] &= do_not_decode[j];
01280 if (do_not_decode[0])
01281 return 0;
01282 ch_used = 1;
01283 } else {
01284 ch_used = ch;
01285 }
01286
01287 AV_DEBUG(" residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
01288
01289 for (pass = 0; pass <= vr->maxpass; ++pass) {
01290 uint_fast16_t voffset;
01291 uint_fast16_t partition_count;
01292 uint_fast16_t j_times_ptns_to_read;
01293
01294 voffset = vr->begin;
01295 for (partition_count = 0; partition_count < ptns_to_read;) {
01296 if (!pass) {
01297 uint_fast32_t inverse_class = ff_inverse[vr->classifications];
01298 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
01299 if (!do_not_decode[j]) {
01300 uint_fast32_t temp = get_vlc2(gb, vc->codebooks[vr->classbook].vlc.table,
01301 vc->codebooks[vr->classbook].nb_bits, 3);
01302
01303 AV_DEBUG("Classword: %d \n", temp);
01304
01305 assert(vr->classifications > 1 && temp <= 65536);
01306 for (i = 0; i < c_p_c; ++i) {
01307 uint_fast32_t temp2;
01308
01309 temp2 = (((uint_fast64_t)temp) * inverse_class) >> 32;
01310 if (partition_count + c_p_c - 1 - i < ptns_to_read)
01311 classifs[j_times_ptns_to_read + partition_count + c_p_c - 1 - i] = temp - temp2 * vr->classifications;
01312 temp = temp2;
01313 }
01314 }
01315 j_times_ptns_to_read += ptns_to_read;
01316 }
01317 }
01318 for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
01319 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
01320 uint_fast16_t voffs;
01321
01322 if (!do_not_decode[j]) {
01323 uint_fast8_t vqclass = classifs[j_times_ptns_to_read+partition_count];
01324 int_fast16_t vqbook = vr->books[vqclass][pass];
01325
01326 if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
01327 uint_fast16_t coffs;
01328 unsigned dim = vc->codebooks[vqbook].dimensions;
01329 uint_fast16_t step = dim == 1 ? vr->partition_size
01330 : FASTDIV(vr->partition_size, dim);
01331 vorbis_codebook codebook = vc->codebooks[vqbook];
01332
01333 if (vr_type == 0) {
01334
01335 voffs = voffset+j*vlen;
01336 for (k = 0; k < step; ++k) {
01337 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01338 for (l = 0; l < dim; ++l)
01339 vec[voffs + k + l * step] += codebook.codevectors[coffs + l];
01340 }
01341 } else if (vr_type == 1) {
01342 voffs = voffset + j * vlen;
01343 for (k = 0; k < step; ++k) {
01344 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01345 for (l = 0; l < dim; ++l, ++voffs) {
01346 vec[voffs]+=codebook.codevectors[coffs+l];
01347
01348 AV_DEBUG(" pass %d offs: %d curr: %f change: %f cv offs.: %d \n", pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
01349 }
01350 }
01351 } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) {
01352 voffs = voffset >> 1;
01353
01354 if (dim == 2) {
01355 for (k = 0; k < step; ++k) {
01356 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 2;
01357 vec[voffs + k ] += codebook.codevectors[coffs ];
01358 vec[voffs + k + vlen] += codebook.codevectors[coffs + 1];
01359 }
01360 } else if (dim == 4) {
01361 for (k = 0; k < step; ++k, voffs += 2) {
01362 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 4;
01363 vec[voffs ] += codebook.codevectors[coffs ];
01364 vec[voffs + 1 ] += codebook.codevectors[coffs + 2];
01365 vec[voffs + vlen ] += codebook.codevectors[coffs + 1];
01366 vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3];
01367 }
01368 } else
01369 for (k = 0; k < step; ++k) {
01370 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01371 for (l = 0; l < dim; l += 2, voffs++) {
01372 vec[voffs ] += codebook.codevectors[coffs + l ];
01373 vec[voffs + vlen] += codebook.codevectors[coffs + l + 1];
01374
01375 AV_DEBUG(" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n", pass, voffset / ch + (voffs % ch) * vlen, vec[voffset / ch + (voffs % ch) * vlen], codebook.codevectors[coffs + l], coffs, l);
01376 }
01377 }
01378
01379 } else if (vr_type == 2) {
01380 voffs = voffset;
01381
01382 for (k = 0; k < step; ++k) {
01383 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01384 for (l = 0; l < dim; ++l, ++voffs) {
01385 vec[voffs / ch + (voffs % ch) * vlen] += codebook.codevectors[coffs + l];
01386
01387 AV_DEBUG(" pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n", pass, voffset / ch + (voffs % ch) * vlen, vec[voffset / ch + (voffs % ch) * vlen], codebook.codevectors[coffs + l], coffs, l);
01388 }
01389 }
01390 }
01391 }
01392 }
01393 j_times_ptns_to_read += ptns_to_read;
01394 }
01395 ++partition_count;
01396 voffset += vr->partition_size;
01397 }
01398 }
01399 }
01400 return 0;
01401 }
01402
01403 static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr,
01404 uint_fast8_t ch,
01405 uint_fast8_t *do_not_decode,
01406 float *vec, uint_fast16_t vlen)
01407 {
01408 if (vr->type == 2)
01409 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 2);
01410 else if (vr->type == 1)
01411 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 1);
01412 else if (vr->type == 0)
01413 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, 0);
01414 else {
01415 av_log(vc->avccontext, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
01416 return -1;
01417 }
01418 }
01419
01420 void vorbis_inverse_coupling(float *mag, float *ang, int blocksize)
01421 {
01422 int i;
01423 for (i = 0; i < blocksize; i++) {
01424 if (mag[i] > 0.0) {
01425 if (ang[i] > 0.0) {
01426 ang[i] = mag[i] - ang[i];
01427 } else {
01428 float temp = ang[i];
01429 ang[i] = mag[i];
01430 mag[i] += temp;
01431 }
01432 } else {
01433 if (ang[i] > 0.0) {
01434 ang[i] += mag[i];
01435 } else {
01436 float temp = ang[i];
01437 ang[i] = mag[i];
01438 mag[i] -= temp;
01439 }
01440 }
01441 }
01442 }
01443
01444 static void copy_normalize(float *dst, float *src, int len, int exp_bias,
01445 float add_bias)
01446 {
01447 int i;
01448 if (exp_bias) {
01449 memcpy(dst, src, len * sizeof(float));
01450 } else {
01451 for (i = 0; i < len; i++)
01452 dst[i] = src[i] + add_bias;
01453 }
01454 }
01455
01456
01457
01458 static int vorbis_parse_audio_packet(vorbis_context *vc)
01459 {
01460 GetBitContext *gb = &vc->gb;
01461
01462 uint_fast8_t previous_window = vc->previous_window;
01463 uint_fast8_t mode_number;
01464 uint_fast8_t blockflag;
01465 uint_fast16_t blocksize;
01466 int_fast32_t i,j;
01467 uint_fast8_t no_residue[vc->audio_channels];
01468 uint_fast8_t do_not_decode[vc->audio_channels];
01469 vorbis_mapping *mapping;
01470 float *ch_res_ptr = vc->channel_residues;
01471 float *ch_floor_ptr = vc->channel_floors;
01472 uint_fast8_t res_chan[vc->audio_channels];
01473 uint_fast8_t res_num = 0;
01474 int_fast16_t retlen = 0;
01475 float fadd_bias = vc->add_bias;
01476
01477 if (get_bits1(gb)) {
01478 av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
01479 return -1;
01480 }
01481
01482 if (vc->mode_count == 1) {
01483 mode_number = 0;
01484 } else {
01485 GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
01486 }
01487 vc->mode_number = mode_number;
01488 mapping = &vc->mappings[vc->modes[mode_number].mapping];
01489
01490 AV_DEBUG(" Mode number: %d , mapping: %d , blocktype %d \n", mode_number, vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
01491
01492 blockflag = vc->modes[mode_number].blockflag;
01493 blocksize = vc->blocksize[blockflag];
01494 if (blockflag)
01495 skip_bits(gb, 2);
01496
01497 memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * blocksize / 2);
01498 memset(ch_floor_ptr, 0, sizeof(float) * vc->audio_channels * blocksize / 2);
01499
01500
01501
01502 for (i = 0; i < vc->audio_channels; ++i) {
01503 vorbis_floor *floor;
01504 int ret;
01505 if (mapping->submaps > 1) {
01506 floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
01507 } else {
01508 floor = &vc->floors[mapping->submap_floor[0]];
01509 }
01510
01511 ret = floor->decode(vc, &floor->data, ch_floor_ptr);
01512
01513 if (ret < 0) {
01514 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
01515 return -1;
01516 }
01517 no_residue[i] = ret;
01518 ch_floor_ptr += blocksize / 2;
01519 }
01520
01521
01522
01523 for (i = mapping->coupling_steps - 1; i >= 0; --i) {
01524 if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
01525 no_residue[mapping->magnitude[i]] = 0;
01526 no_residue[mapping->angle[i]] = 0;
01527 }
01528 }
01529
01530
01531
01532 for (i = 0; i < mapping->submaps; ++i) {
01533 vorbis_residue *residue;
01534 uint_fast8_t ch = 0;
01535
01536 for (j = 0; j < vc->audio_channels; ++j) {
01537 if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
01538 res_chan[j] = res_num;
01539 if (no_residue[j]) {
01540 do_not_decode[ch] = 1;
01541 } else {
01542 do_not_decode[ch] = 0;
01543 }
01544 ++ch;
01545 ++res_num;
01546 }
01547 }
01548 residue = &vc->residues[mapping->submap_residue[i]];
01549 vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, blocksize/2);
01550
01551 ch_res_ptr += ch * blocksize / 2;
01552 }
01553
01554
01555
01556 for (i = mapping->coupling_steps - 1; i >= 0; --i) {
01557 float *mag, *ang;
01558
01559 mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2;
01560 ang = vc->channel_residues+res_chan[mapping->angle[i]] * blocksize / 2;
01561 vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
01562 }
01563
01564
01565
01566 for (j = vc->audio_channels-1;j >= 0; j--) {
01567 ch_floor_ptr = vc->channel_floors + j * blocksize / 2;
01568 ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2;
01569 vc->dsp.vector_fmul(ch_floor_ptr, ch_res_ptr, blocksize / 2);
01570 ff_imdct_half(&vc->mdct[blockflag], ch_res_ptr, ch_floor_ptr);
01571 }
01572
01573
01574
01575 retlen = (blocksize + vc->blocksize[previous_window]) / 4;
01576 for (j = 0; j < vc->audio_channels; j++) {
01577 uint_fast16_t bs0 = vc->blocksize[0];
01578 uint_fast16_t bs1 = vc->blocksize[1];
01579 float *residue = vc->channel_residues + res_chan[j] * blocksize / 2;
01580 float *saved = vc->saved + j * bs1 / 4;
01581 float *ret = vc->channel_floors + j * retlen;
01582 float *buf = residue;
01583 const float *win = vc->win[blockflag & previous_window];
01584
01585 if (blockflag == previous_window) {
01586 vc->dsp.vector_fmul_window(ret, saved, buf, win, fadd_bias, blocksize / 4);
01587 } else if (blockflag > previous_window) {
01588 vc->dsp.vector_fmul_window(ret, saved, buf, win, fadd_bias, bs0 / 4);
01589 copy_normalize(ret+bs0/2, buf+bs0/4, (bs1-bs0)/4, vc->exp_bias, fadd_bias);
01590 } else {
01591 copy_normalize(ret, saved, (bs1 - bs0) / 4, vc->exp_bias, fadd_bias);
01592 vc->dsp.vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, fadd_bias, bs0 / 4);
01593 }
01594 memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
01595 }
01596
01597 vc->previous_window = blockflag;
01598 return retlen;
01599 }
01600
01601
01602
01603 static int vorbis_decode_frame(AVCodecContext *avccontext,
01604 void *data, int *data_size,
01605 AVPacket *avpkt)
01606 {
01607 const uint8_t *buf = avpkt->data;
01608 int buf_size = avpkt->size;
01609 vorbis_context *vc = avccontext->priv_data ;
01610 GetBitContext *gb = &(vc->gb);
01611 const float *channel_ptrs[vc->audio_channels];
01612 int i;
01613
01614 int_fast16_t len;
01615
01616 if (!buf_size)
01617 return 0;
01618
01619 AV_DEBUG("packet length %d \n", buf_size);
01620
01621 init_get_bits(gb, buf, buf_size*8);
01622
01623 len = vorbis_parse_audio_packet(vc);
01624
01625 if (len <= 0) {
01626 *data_size = 0;
01627 return buf_size;
01628 }
01629
01630 if (!vc->first_frame) {
01631 vc->first_frame = 1;
01632 *data_size = 0;
01633 return buf_size ;
01634 }
01635
01636 AV_DEBUG("parsed %d bytes %d bits, returned %d samples (*ch*bits) \n", get_bits_count(gb)/8, get_bits_count(gb)%8, len);
01637
01638 if (vc->audio_channels > 8) {
01639 for (i = 0; i < vc->audio_channels; i++)
01640 channel_ptrs[i] = vc->channel_floors + i * len;
01641 } else {
01642 for (i = 0; i < vc->audio_channels; i++)
01643 channel_ptrs[i] = vc->channel_floors +
01644 len * ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
01645 }
01646
01647 vc->dsp.float_to_int16_interleave(data, channel_ptrs, len, vc->audio_channels);
01648 *data_size = len * 2 * vc->audio_channels;
01649
01650 return buf_size ;
01651 }
01652
01653
01654
01655 static av_cold int vorbis_decode_close(AVCodecContext *avccontext)
01656 {
01657 vorbis_context *vc = avccontext->priv_data;
01658
01659 vorbis_free(vc);
01660
01661 return 0 ;
01662 }
01663
01664 AVCodec vorbis_decoder = {
01665 "vorbis",
01666 AVMEDIA_TYPE_AUDIO,
01667 CODEC_ID_VORBIS,
01668 sizeof(vorbis_context),
01669 vorbis_decode_init,
01670 NULL,
01671 vorbis_decode_close,
01672 vorbis_decode_frame,
01673 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
01674 .channel_layouts = ff_vorbis_channel_layouts,
01675 };
01676