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