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