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