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