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