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