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