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