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