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 if (ff_vorbis_ready_floor1_list(vc->avccontext,
00582 floor_setup->data.t1.list,
00583 floor_setup->data.t1.x_list_dim)) {
00584 return AVERROR_INVALIDDATA;
00585 }
00586
00587 for (j=1; j<floor_setup->data.t1.x_list_dim; j++) {
00588 if ( floor_setup->data.t1.list[ floor_setup->data.t1.list[j-1].sort ].x
00589 == floor_setup->data.t1.list[ floor_setup->data.t1.list[j ].sort ].x) {
00590 av_log(vc->avccontext, AV_LOG_ERROR, "Non unique x values in floor type 1\n");
00591 return AVERROR_INVALIDDATA;
00592 }
00593 }
00594 } else if (floor_setup->floor_type == 0) {
00595 unsigned max_codebook_dim = 0;
00596
00597 floor_setup->decode = vorbis_floor0_decode;
00598
00599 floor_setup->data.t0.order = get_bits(gb, 8);
00600 floor_setup->data.t0.rate = get_bits(gb, 16);
00601 floor_setup->data.t0.bark_map_size = get_bits(gb, 16);
00602 floor_setup->data.t0.amplitude_bits = get_bits(gb, 6);
00603
00604
00605 if (floor_setup->data.t0.amplitude_bits == 0) {
00606 av_log(vc->avccontext, AV_LOG_ERROR,
00607 "Floor 0 amplitude bits is 0.\n");
00608 return AVERROR_INVALIDDATA;
00609 }
00610 floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
00611 floor_setup->data.t0.num_books = get_bits(gb, 4) + 1;
00612
00613
00614 floor_setup->data.t0.book_list =
00615 av_malloc(floor_setup->data.t0.num_books);
00616 if (!floor_setup->data.t0.book_list)
00617 return AVERROR(ENOMEM);
00618
00619 {
00620 int idx;
00621 unsigned book_idx;
00622 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
00623 GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count)
00624 floor_setup->data.t0.book_list[idx] = book_idx;
00625 if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
00626 max_codebook_dim = vc->codebooks[book_idx].dimensions;
00627 }
00628 }
00629
00630 create_map(vc, i);
00631
00632
00633
00634 floor_setup->data.t0.lsp =
00635 av_malloc((floor_setup->data.t0.order + 1 + max_codebook_dim)
00636 * sizeof(*floor_setup->data.t0.lsp));
00637 if (!floor_setup->data.t0.lsp)
00638 return AVERROR(ENOMEM);
00639
00640
00641 av_dlog(NULL, "floor0 order: %u\n", floor_setup->data.t0.order);
00642 av_dlog(NULL, "floor0 rate: %u\n", floor_setup->data.t0.rate);
00643 av_dlog(NULL, "floor0 bark map size: %u\n",
00644 floor_setup->data.t0.bark_map_size);
00645 av_dlog(NULL, "floor0 amplitude bits: %u\n",
00646 floor_setup->data.t0.amplitude_bits);
00647 av_dlog(NULL, "floor0 amplitude offset: %u\n",
00648 floor_setup->data.t0.amplitude_offset);
00649 av_dlog(NULL, "floor0 number of books: %u\n",
00650 floor_setup->data.t0.num_books);
00651 av_dlog(NULL, "floor0 book list pointer: %p\n",
00652 floor_setup->data.t0.book_list);
00653 {
00654 int idx;
00655 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
00656 av_dlog(NULL, " Book %d: %u\n", idx + 1,
00657 floor_setup->data.t0.book_list[idx]);
00658 }
00659 }
00660 } else {
00661 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid floor type!\n");
00662 return AVERROR_INVALIDDATA;
00663 }
00664 }
00665 return 0;
00666 }
00667
00668
00669
00670 static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
00671 {
00672 GetBitContext *gb = &vc->gb;
00673 unsigned i, j, k;
00674
00675 vc->residue_count = get_bits(gb, 6)+1;
00676 vc->residues = av_mallocz(vc->residue_count * sizeof(*vc->residues));
00677
00678 av_dlog(NULL, " There are %d residues. \n", vc->residue_count);
00679
00680 for (i = 0; i < vc->residue_count; ++i) {
00681 vorbis_residue *res_setup = &vc->residues[i];
00682 uint8_t cascade[64];
00683 unsigned high_bits, low_bits;
00684
00685 res_setup->type = get_bits(gb, 16);
00686
00687 av_dlog(NULL, " %u. residue type %d\n", i, res_setup->type);
00688
00689 res_setup->begin = get_bits(gb, 24);
00690 res_setup->end = get_bits(gb, 24);
00691 res_setup->partition_size = get_bits(gb, 24) + 1;
00692
00693 if (res_setup->begin>res_setup->end ||
00694 res_setup->end > (res_setup->type == 2 ? vc->avccontext->channels : 1) * vc->blocksize[1] / 2 ||
00695 (res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
00696 av_log(vc->avccontext, AV_LOG_ERROR,
00697 "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n",
00698 res_setup->type, res_setup->begin, res_setup->end,
00699 res_setup->partition_size, vc->blocksize[1] / 2);
00700 return AVERROR_INVALIDDATA;
00701 }
00702
00703 res_setup->classifications = get_bits(gb, 6) + 1;
00704 GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
00705
00706 res_setup->ptns_to_read =
00707 (res_setup->end - res_setup->begin) / res_setup->partition_size;
00708 res_setup->classifs = av_malloc(res_setup->ptns_to_read *
00709 vc->audio_channels *
00710 sizeof(*res_setup->classifs));
00711 if (!res_setup->classifs)
00712 return AVERROR(ENOMEM);
00713
00714 av_dlog(NULL, " begin %d end %d part.size %d classif.s %d classbook %d \n",
00715 res_setup->begin, res_setup->end, res_setup->partition_size,
00716 res_setup->classifications, res_setup->classbook);
00717
00718 for (j = 0; j < res_setup->classifications; ++j) {
00719 high_bits = 0;
00720 low_bits = get_bits(gb, 3);
00721 if (get_bits1(gb))
00722 high_bits = get_bits(gb, 5);
00723 cascade[j] = (high_bits << 3) + low_bits;
00724
00725 av_dlog(NULL, " %u class cascade depth: %d\n", j, ilog(cascade[j]));
00726 }
00727
00728 res_setup->maxpass = 0;
00729 for (j = 0; j < res_setup->classifications; ++j) {
00730 for (k = 0; k < 8; ++k) {
00731 if (cascade[j]&(1 << k)) {
00732 GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
00733
00734 av_dlog(NULL, " %u class cascade depth %u book: %d\n",
00735 j, k, res_setup->books[j][k]);
00736
00737 if (k>res_setup->maxpass)
00738 res_setup->maxpass = k;
00739 } else {
00740 res_setup->books[j][k] = -1;
00741 }
00742 }
00743 }
00744 }
00745 return 0;
00746 }
00747
00748
00749
00750 static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
00751 {
00752 GetBitContext *gb = &vc->gb;
00753 unsigned i, j;
00754
00755 vc->mapping_count = get_bits(gb, 6)+1;
00756 vc->mappings = av_mallocz(vc->mapping_count * sizeof(*vc->mappings));
00757
00758 av_dlog(NULL, " There are %d mappings. \n", vc->mapping_count);
00759
00760 for (i = 0; i < vc->mapping_count; ++i) {
00761 vorbis_mapping *mapping_setup = &vc->mappings[i];
00762
00763 if (get_bits(gb, 16)) {
00764 av_log(vc->avccontext, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
00765 return AVERROR_INVALIDDATA;
00766 }
00767 if (get_bits1(gb)) {
00768 mapping_setup->submaps = get_bits(gb, 4) + 1;
00769 } else {
00770 mapping_setup->submaps = 1;
00771 }
00772
00773 if (get_bits1(gb)) {
00774 mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
00775 mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps *
00776 sizeof(*mapping_setup->magnitude));
00777 mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps *
00778 sizeof(*mapping_setup->angle));
00779 for (j = 0; j < mapping_setup->coupling_steps; ++j) {
00780 GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
00781 GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels)
00782 }
00783 } else {
00784 mapping_setup->coupling_steps = 0;
00785 }
00786
00787 av_dlog(NULL, " %u mapping coupling steps: %d\n",
00788 i, mapping_setup->coupling_steps);
00789
00790 if (get_bits(gb, 2)) {
00791 av_log(vc->avccontext, AV_LOG_ERROR, "%u. mapping setup data invalid.\n", i);
00792 return AVERROR_INVALIDDATA;
00793 }
00794
00795 if (mapping_setup->submaps>1) {
00796 mapping_setup->mux = av_mallocz(vc->audio_channels *
00797 sizeof(*mapping_setup->mux));
00798 for (j = 0; j < vc->audio_channels; ++j)
00799 mapping_setup->mux[j] = get_bits(gb, 4);
00800 }
00801
00802 for (j = 0; j < mapping_setup->submaps; ++j) {
00803 skip_bits(gb, 8);
00804 GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count)
00805 GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
00806
00807 av_dlog(NULL, " %u mapping %u submap : floor %d, residue %d\n", i, j,
00808 mapping_setup->submap_floor[j],
00809 mapping_setup->submap_residue[j]);
00810 }
00811 }
00812 return 0;
00813 }
00814
00815
00816
00817 static void create_map(vorbis_context *vc, unsigned floor_number)
00818 {
00819 vorbis_floor *floors = vc->floors;
00820 vorbis_floor0 *vf;
00821 int idx;
00822 int blockflag, n;
00823 int32_t *map;
00824
00825 for (blockflag = 0; blockflag < 2; ++blockflag) {
00826 n = vc->blocksize[blockflag] / 2;
00827 floors[floor_number].data.t0.map[blockflag] =
00828 av_malloc((n + 1) * sizeof(int32_t));
00829
00830 map = floors[floor_number].data.t0.map[blockflag];
00831 vf = &floors[floor_number].data.t0;
00832
00833 for (idx = 0; idx < n; ++idx) {
00834 map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
00835 (vf->bark_map_size / BARK(vf->rate / 2.0f)));
00836 if (vf->bark_map_size-1 < map[idx])
00837 map[idx] = vf->bark_map_size - 1;
00838 }
00839 map[n] = -1;
00840 vf->map_size[blockflag] = n;
00841 }
00842
00843 for (idx = 0; idx <= n; ++idx) {
00844 av_dlog(NULL, "floor0 map: map at pos %d is %d\n", idx, map[idx]);
00845 }
00846 }
00847
00848 static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
00849 {
00850 GetBitContext *gb = &vc->gb;
00851 unsigned i;
00852
00853 vc->mode_count = get_bits(gb, 6) + 1;
00854 vc->modes = av_mallocz(vc->mode_count * sizeof(*vc->modes));
00855
00856 av_dlog(NULL, " There are %d modes.\n", vc->mode_count);
00857
00858 for (i = 0; i < vc->mode_count; ++i) {
00859 vorbis_mode *mode_setup = &vc->modes[i];
00860
00861 mode_setup->blockflag = get_bits1(gb);
00862 mode_setup->windowtype = get_bits(gb, 16);
00863 mode_setup->transformtype = get_bits(gb, 16);
00864 GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
00865
00866 av_dlog(NULL, " %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
00867 i, mode_setup->blockflag, mode_setup->windowtype,
00868 mode_setup->transformtype, mode_setup->mapping);
00869 }
00870 return 0;
00871 }
00872
00873
00874
00875 static int vorbis_parse_setup_hdr(vorbis_context *vc)
00876 {
00877 GetBitContext *gb = &vc->gb;
00878 int ret;
00879
00880 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
00881 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
00882 (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
00883 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
00884 return AVERROR_INVALIDDATA;
00885 }
00886
00887 if ((ret = vorbis_parse_setup_hdr_codebooks(vc))) {
00888 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
00889 return ret;
00890 }
00891 if ((ret = vorbis_parse_setup_hdr_tdtransforms(vc))) {
00892 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
00893 return ret;
00894 }
00895 if ((ret = vorbis_parse_setup_hdr_floors(vc))) {
00896 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
00897 return ret;
00898 }
00899 if ((ret = vorbis_parse_setup_hdr_residues(vc))) {
00900 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
00901 return ret;
00902 }
00903 if ((ret = vorbis_parse_setup_hdr_mappings(vc))) {
00904 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
00905 return ret;
00906 }
00907 if ((ret = vorbis_parse_setup_hdr_modes(vc))) {
00908 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
00909 return ret;
00910 }
00911 if (!get_bits1(gb)) {
00912 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
00913 return AVERROR_INVALIDDATA;
00914 }
00915
00916 return 0;
00917 }
00918
00919
00920
00921 static int vorbis_parse_id_hdr(vorbis_context *vc)
00922 {
00923 GetBitContext *gb = &vc->gb;
00924 unsigned bl0, bl1;
00925
00926 if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
00927 (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
00928 (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
00929 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
00930 return AVERROR_INVALIDDATA;
00931 }
00932
00933 vc->version = get_bits_long(gb, 32);
00934 vc->audio_channels = get_bits(gb, 8);
00935 if (vc->audio_channels <= 0) {
00936 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid number of channels\n");
00937 return AVERROR_INVALIDDATA;
00938 }
00939 vc->audio_samplerate = get_bits_long(gb, 32);
00940 if (vc->audio_samplerate <= 0) {
00941 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid samplerate\n");
00942 return AVERROR_INVALIDDATA;
00943 }
00944 vc->bitrate_maximum = get_bits_long(gb, 32);
00945 vc->bitrate_nominal = get_bits_long(gb, 32);
00946 vc->bitrate_minimum = get_bits_long(gb, 32);
00947 bl0 = get_bits(gb, 4);
00948 bl1 = get_bits(gb, 4);
00949 if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
00950 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
00951 return AVERROR_INVALIDDATA;
00952 }
00953 vc->blocksize[0] = (1 << bl0);
00954 vc->blocksize[1] = (1 << bl1);
00955 vc->win[0] = ff_vorbis_vwin[bl0 - 6];
00956 vc->win[1] = ff_vorbis_vwin[bl1 - 6];
00957
00958 if ((get_bits1(gb)) == 0) {
00959 av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
00960 return AVERROR_INVALIDDATA;
00961 }
00962
00963 vc->channel_residues = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(*vc->channel_residues));
00964 vc->channel_floors = av_malloc((vc->blocksize[1] / 2) * vc->audio_channels * sizeof(*vc->channel_floors));
00965 vc->saved = av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(*vc->saved));
00966 vc->previous_window = 0;
00967
00968 ff_mdct_init(&vc->mdct[0], bl0, 1, -vc->scale_bias);
00969 ff_mdct_init(&vc->mdct[1], bl1, 1, -vc->scale_bias);
00970
00971 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 ",
00972 vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
00973
00974
00975
00976
00977
00978
00979
00980
00981 return 0;
00982 }
00983
00984
00985
00986 static av_cold int vorbis_decode_init(AVCodecContext *avccontext)
00987 {
00988 vorbis_context *vc = avccontext->priv_data;
00989 uint8_t *headers = avccontext->extradata;
00990 int headers_len = avccontext->extradata_size;
00991 uint8_t *header_start[3];
00992 int header_len[3];
00993 GetBitContext *gb = &vc->gb;
00994 int hdr_type, ret;
00995
00996 vc->avccontext = avccontext;
00997 dsputil_init(&vc->dsp, avccontext);
00998 ff_fmt_convert_init(&vc->fmt_conv, avccontext);
00999
01000 if (avccontext->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
01001 avccontext->sample_fmt = AV_SAMPLE_FMT_FLT;
01002 vc->scale_bias = 1.0f;
01003 } else {
01004 avccontext->sample_fmt = AV_SAMPLE_FMT_S16;
01005 vc->scale_bias = 32768.0f;
01006 }
01007
01008 if (!headers_len) {
01009 av_log(avccontext, AV_LOG_ERROR, "Extradata missing.\n");
01010 return AVERROR_INVALIDDATA;
01011 }
01012
01013 if ((ret = avpriv_split_xiph_headers(headers, headers_len, 30, header_start, header_len)) < 0) {
01014 av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
01015 return ret;
01016 }
01017
01018 init_get_bits(gb, header_start[0], header_len[0]*8);
01019 hdr_type = get_bits(gb, 8);
01020 if (hdr_type != 1) {
01021 av_log(avccontext, AV_LOG_ERROR, "First header is not the id header.\n");
01022 return AVERROR_INVALIDDATA;
01023 }
01024 if ((ret = vorbis_parse_id_hdr(vc))) {
01025 av_log(avccontext, AV_LOG_ERROR, "Id header corrupt.\n");
01026 vorbis_free(vc);
01027 return ret;
01028 }
01029
01030 init_get_bits(gb, header_start[2], header_len[2]*8);
01031 hdr_type = get_bits(gb, 8);
01032 if (hdr_type != 5) {
01033 av_log(avccontext, AV_LOG_ERROR, "Third header is not the setup header.\n");
01034 vorbis_free(vc);
01035 return AVERROR_INVALIDDATA;
01036 }
01037 if ((ret = vorbis_parse_setup_hdr(vc))) {
01038 av_log(avccontext, AV_LOG_ERROR, "Setup header corrupt.\n");
01039 vorbis_free(vc);
01040 return ret;
01041 }
01042
01043 if (vc->audio_channels > 8)
01044 avccontext->channel_layout = 0;
01045 else
01046 avccontext->channel_layout = ff_vorbis_channel_layouts[vc->audio_channels - 1];
01047
01048 avccontext->channels = vc->audio_channels;
01049 avccontext->sample_rate = vc->audio_samplerate;
01050 avccontext->frame_size = FFMIN(vc->blocksize[0], vc->blocksize[1]) >> 2;
01051
01052 avcodec_get_frame_defaults(&vc->frame);
01053 avccontext->coded_frame = &vc->frame;
01054
01055 return 0;
01056 }
01057
01058
01059
01060
01061
01062 static int vorbis_floor0_decode(vorbis_context *vc,
01063 vorbis_floor_data *vfu, float *vec)
01064 {
01065 vorbis_floor0 *vf = &vfu->t0;
01066 float *lsp = vf->lsp;
01067 unsigned amplitude, book_idx;
01068 unsigned blockflag = vc->modes[vc->mode_number].blockflag;
01069
01070 amplitude = get_bits(&vc->gb, vf->amplitude_bits);
01071 if (amplitude > 0) {
01072 float last = 0;
01073 unsigned idx, lsp_len = 0;
01074 vorbis_codebook codebook;
01075
01076 book_idx = get_bits(&vc->gb, ilog(vf->num_books));
01077 if (book_idx >= vf->num_books) {
01078 av_log(vc->avccontext, AV_LOG_ERROR,
01079 "floor0 dec: booknumber too high!\n");
01080 book_idx = 0;
01081 }
01082 av_dlog(NULL, "floor0 dec: booknumber: %u\n", book_idx);
01083 codebook = vc->codebooks[vf->book_list[book_idx]];
01084
01085 if (!codebook.codevectors)
01086 return AVERROR_INVALIDDATA;
01087
01088 while (lsp_len<vf->order) {
01089 int vec_off;
01090
01091 av_dlog(NULL, "floor0 dec: book dimension: %d\n", codebook.dimensions);
01092 av_dlog(NULL, "floor0 dec: maximum depth: %d\n", codebook.maxdepth);
01093
01094 vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
01095 codebook.nb_bits, codebook.maxdepth)
01096 * codebook.dimensions;
01097 av_dlog(NULL, "floor0 dec: vector offset: %d\n", vec_off);
01098
01099 for (idx = 0; idx < codebook.dimensions; ++idx)
01100 lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
01101 last = lsp[lsp_len+idx-1];
01102
01103 lsp_len += codebook.dimensions;
01104 }
01105
01106 {
01107 int idx;
01108 for (idx = 0; idx < lsp_len; ++idx)
01109 av_dlog(NULL, "floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
01110 }
01111
01112
01113 {
01114 int i;
01115 int order = vf->order;
01116 float wstep = M_PI / vf->bark_map_size;
01117
01118 for (i = 0; i < order; i++)
01119 lsp[i] = 2.0f * cos(lsp[i]);
01120
01121 av_dlog(NULL, "floor0 synth: map_size = %"PRIu32"; m = %d; wstep = %f\n",
01122 vf->map_size[blockflag], order, wstep);
01123
01124 i = 0;
01125 while (i < vf->map_size[blockflag]) {
01126 int j, iter_cond = vf->map[blockflag][i];
01127 float p = 0.5f;
01128 float q = 0.5f;
01129 float two_cos_w = 2.0f * cos(wstep * iter_cond);
01130
01131
01132 for (j = 0; j + 1 < order; j += 2) {
01133 q *= lsp[j] - two_cos_w;
01134 p *= lsp[j + 1] - two_cos_w;
01135 }
01136 if (j == order) {
01137 p *= p * (2.0f - two_cos_w);
01138 q *= q * (2.0f + two_cos_w);
01139 } else {
01140 q *= two_cos_w-lsp[j];
01141
01142
01143 p *= p * (4.f - two_cos_w * two_cos_w);
01144 q *= q;
01145 }
01146
01147
01148 q = exp((((amplitude*vf->amplitude_offset) /
01149 (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
01150 - vf->amplitude_offset) * .11512925f);
01151
01152
01153 do {
01154 vec[i] = q; ++i;
01155 } while (vf->map[blockflag][i] == iter_cond);
01156 }
01157 }
01158 } else {
01159
01160 return 1;
01161 }
01162
01163 av_dlog(NULL, " Floor0 decoded\n");
01164
01165 return 0;
01166 }
01167
01168 static int vorbis_floor1_decode(vorbis_context *vc,
01169 vorbis_floor_data *vfu, float *vec)
01170 {
01171 vorbis_floor1 *vf = &vfu->t1;
01172 GetBitContext *gb = &vc->gb;
01173 uint16_t range_v[4] = { 256, 128, 86, 64 };
01174 unsigned range = range_v[vf->multiplier - 1];
01175 uint16_t floor1_Y[258];
01176 uint16_t floor1_Y_final[258];
01177 int floor1_flag[258];
01178 unsigned partition_class, cdim, cbits, csub, cval, offset, i, j;
01179 int book, adx, ady, dy, off, predicted, err;
01180
01181
01182 if (!get_bits1(gb))
01183 return 1;
01184
01185
01186
01187 floor1_Y[0] = get_bits(gb, ilog(range - 1));
01188 floor1_Y[1] = get_bits(gb, ilog(range - 1));
01189
01190 av_dlog(NULL, "floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
01191
01192 offset = 2;
01193 for (i = 0; i < vf->partitions; ++i) {
01194 partition_class = vf->partition_class[i];
01195 cdim = vf->class_dimensions[partition_class];
01196 cbits = vf->class_subclasses[partition_class];
01197 csub = (1 << cbits) - 1;
01198 cval = 0;
01199
01200 av_dlog(NULL, "Cbits %u\n", cbits);
01201
01202 if (cbits)
01203 cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[partition_class]].vlc.table,
01204 vc->codebooks[vf->class_masterbook[partition_class]].nb_bits, 3);
01205
01206 for (j = 0; j < cdim; ++j) {
01207 book = vf->subclass_books[partition_class][cval & csub];
01208
01209 av_dlog(NULL, "book %d Cbits %u cval %u bits:%d\n",
01210 book, cbits, cval, get_bits_count(gb));
01211
01212 cval = cval >> cbits;
01213 if (book > -1) {
01214 floor1_Y[offset+j] = get_vlc2(gb, vc->codebooks[book].vlc.table,
01215 vc->codebooks[book].nb_bits, 3);
01216 } else {
01217 floor1_Y[offset+j] = 0;
01218 }
01219
01220 av_dlog(NULL, " floor(%d) = %d \n",
01221 vf->list[offset+j].x, floor1_Y[offset+j]);
01222 }
01223 offset+=cdim;
01224 }
01225
01226
01227
01228 floor1_flag[0] = 1;
01229 floor1_flag[1] = 1;
01230 floor1_Y_final[0] = floor1_Y[0];
01231 floor1_Y_final[1] = floor1_Y[1];
01232
01233 for (i = 2; i < vf->x_list_dim; ++i) {
01234 unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
01235
01236 low_neigh_offs = vf->list[i].low;
01237 high_neigh_offs = vf->list[i].high;
01238 dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs];
01239 adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
01240 ady = FFABS(dy);
01241 err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
01242 off = err / adx;
01243 if (dy < 0) {
01244 predicted = floor1_Y_final[low_neigh_offs] - off;
01245 } else {
01246 predicted = floor1_Y_final[low_neigh_offs] + off;
01247 }
01248
01249 val = floor1_Y[i];
01250 highroom = range-predicted;
01251 lowroom = predicted;
01252 if (highroom < lowroom) {
01253 room = highroom * 2;
01254 } else {
01255 room = lowroom * 2;
01256 }
01257 if (val) {
01258 floor1_flag[low_neigh_offs] = 1;
01259 floor1_flag[high_neigh_offs] = 1;
01260 floor1_flag[i] = 1;
01261 if (val >= room) {
01262 if (highroom > lowroom) {
01263 floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
01264 } else {
01265 floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
01266 }
01267 } else {
01268 if (val & 1) {
01269 floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
01270 } else {
01271 floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
01272 }
01273 }
01274 } else {
01275 floor1_flag[i] = 0;
01276 floor1_Y_final[i] = av_clip_uint16(predicted);
01277 }
01278
01279 av_dlog(NULL, " Decoded floor(%d) = %u / val %u\n",
01280 vf->list[i].x, floor1_Y_final[i], val);
01281 }
01282
01283
01284
01285 ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x);
01286
01287 av_dlog(NULL, " Floor decoded\n");
01288
01289 return 0;
01290 }
01291
01292
01293
01294 static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc,
01295 vorbis_residue *vr,
01296 unsigned ch,
01297 uint8_t *do_not_decode,
01298 float *vec,
01299 unsigned vlen,
01300 unsigned ch_left,
01301 int vr_type)
01302 {
01303 GetBitContext *gb = &vc->gb;
01304 unsigned c_p_c = vc->codebooks[vr->classbook].dimensions;
01305 unsigned ptns_to_read = vr->ptns_to_read;
01306 uint8_t *classifs = vr->classifs;
01307 unsigned pass, ch_used, i, j, k, l;
01308 unsigned max_output = (ch - 1) * vlen;
01309
01310 if (vr_type == 2) {
01311 for (j = 1; j < ch; ++j)
01312 do_not_decode[0] &= do_not_decode[j];
01313 if (do_not_decode[0])
01314 return 0;
01315 ch_used = 1;
01316 max_output += vr->end / ch;
01317 } else {
01318 ch_used = ch;
01319 max_output += vr->end;
01320 }
01321
01322 if (max_output > ch_left * vlen) {
01323 av_log(vc->avccontext, AV_LOG_ERROR, "Insufficient output buffer\n");
01324 return -1;
01325 }
01326
01327 av_dlog(NULL, " residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c);
01328
01329 for (pass = 0; pass <= vr->maxpass; ++pass) {
01330 uint16_t voffset, partition_count, j_times_ptns_to_read;
01331
01332 voffset = vr->begin;
01333 for (partition_count = 0; partition_count < ptns_to_read;) {
01334 if (!pass) {
01335 unsigned inverse_class = ff_inverse[vr->classifications];
01336 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
01337 if (!do_not_decode[j]) {
01338 unsigned temp = get_vlc2(gb, vc->codebooks[vr->classbook].vlc.table,
01339 vc->codebooks[vr->classbook].nb_bits, 3);
01340
01341 av_dlog(NULL, "Classword: %u\n", temp);
01342
01343 assert(vr->classifications > 1 && temp <= 65536);
01344 for (i = 0; i < c_p_c; ++i) {
01345 unsigned temp2;
01346
01347 temp2 = (((uint64_t)temp) * inverse_class) >> 32;
01348 if (partition_count + c_p_c - 1 - i < ptns_to_read)
01349 classifs[j_times_ptns_to_read + partition_count + c_p_c - 1 - i] = temp - temp2 * vr->classifications;
01350 temp = temp2;
01351 }
01352 }
01353 j_times_ptns_to_read += ptns_to_read;
01354 }
01355 }
01356 for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
01357 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
01358 unsigned voffs;
01359
01360 if (!do_not_decode[j]) {
01361 unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
01362 int vqbook = vr->books[vqclass][pass];
01363
01364 if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
01365 unsigned coffs;
01366 unsigned dim = vc->codebooks[vqbook].dimensions;
01367 unsigned step = dim == 1 ? vr->partition_size
01368 : FASTDIV(vr->partition_size, dim);
01369 vorbis_codebook codebook = vc->codebooks[vqbook];
01370
01371 if (vr_type == 0) {
01372
01373 voffs = voffset+j*vlen;
01374 for (k = 0; k < step; ++k) {
01375 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01376 for (l = 0; l < dim; ++l)
01377 vec[voffs + k + l * step] += codebook.codevectors[coffs + l];
01378 }
01379 } else if (vr_type == 1) {
01380 voffs = voffset + j * vlen;
01381 for (k = 0; k < step; ++k) {
01382 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01383 for (l = 0; l < dim; ++l, ++voffs) {
01384 vec[voffs]+=codebook.codevectors[coffs+l];
01385
01386 av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d \n",
01387 pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
01388 }
01389 }
01390 } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) {
01391 voffs = voffset >> 1;
01392
01393 if (dim == 2) {
01394 for (k = 0; k < step; ++k) {
01395 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 2;
01396 vec[voffs + k ] += codebook.codevectors[coffs ];
01397 vec[voffs + k + vlen] += codebook.codevectors[coffs + 1];
01398 }
01399 } else if (dim == 4) {
01400 for (k = 0; k < step; ++k, voffs += 2) {
01401 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 4;
01402 vec[voffs ] += codebook.codevectors[coffs ];
01403 vec[voffs + 1 ] += codebook.codevectors[coffs + 2];
01404 vec[voffs + vlen ] += codebook.codevectors[coffs + 1];
01405 vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3];
01406 }
01407 } else
01408 for (k = 0; k < step; ++k) {
01409 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01410 for (l = 0; l < dim; l += 2, voffs++) {
01411 vec[voffs ] += codebook.codevectors[coffs + l ];
01412 vec[voffs + vlen] += codebook.codevectors[coffs + l + 1];
01413
01414 av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
01415 pass, voffset / ch + (voffs % ch) * vlen,
01416 vec[voffset / ch + (voffs % ch) * vlen],
01417 codebook.codevectors[coffs + l], coffs, l);
01418 }
01419 }
01420
01421 } else if (vr_type == 2) {
01422 voffs = voffset;
01423
01424 for (k = 0; k < step; ++k) {
01425 coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01426 for (l = 0; l < dim; ++l, ++voffs) {
01427 vec[voffs / ch + (voffs % ch) * vlen] += codebook.codevectors[coffs + l];
01428
01429 av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n",
01430 pass, voffset / ch + (voffs % ch) * vlen,
01431 vec[voffset / ch + (voffs % ch) * vlen],
01432 codebook.codevectors[coffs + l], coffs, l);
01433 }
01434 }
01435 }
01436 }
01437 }
01438 j_times_ptns_to_read += ptns_to_read;
01439 }
01440 ++partition_count;
01441 voffset += vr->partition_size;
01442 }
01443 }
01444 }
01445 return 0;
01446 }
01447
01448 static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr,
01449 unsigned ch,
01450 uint8_t *do_not_decode,
01451 float *vec, unsigned vlen,
01452 unsigned ch_left)
01453 {
01454 if (vr->type == 2)
01455 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2);
01456 else if (vr->type == 1)
01457 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1);
01458 else if (vr->type == 0)
01459 return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 0);
01460 else {
01461 av_log(vc->avccontext, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
01462 return AVERROR_INVALIDDATA;
01463 }
01464 }
01465
01466 void vorbis_inverse_coupling(float *mag, float *ang, int blocksize)
01467 {
01468 int i;
01469 for (i = 0; i < blocksize; i++) {
01470 if (mag[i] > 0.0) {
01471 if (ang[i] > 0.0) {
01472 ang[i] = mag[i] - ang[i];
01473 } else {
01474 float temp = ang[i];
01475 ang[i] = mag[i];
01476 mag[i] += temp;
01477 }
01478 } else {
01479 if (ang[i] > 0.0) {
01480 ang[i] += mag[i];
01481 } else {
01482 float temp = ang[i];
01483 ang[i] = mag[i];
01484 mag[i] -= temp;
01485 }
01486 }
01487 }
01488 }
01489
01490
01491
01492 static int vorbis_parse_audio_packet(vorbis_context *vc)
01493 {
01494 GetBitContext *gb = &vc->gb;
01495 FFTContext *mdct;
01496 unsigned previous_window = vc->previous_window;
01497 unsigned mode_number, blockflag, blocksize;
01498 int i, j;
01499 uint8_t no_residue[255];
01500 uint8_t do_not_decode[255];
01501 vorbis_mapping *mapping;
01502 float *ch_res_ptr = vc->channel_residues;
01503 float *ch_floor_ptr = vc->channel_floors;
01504 uint8_t res_chan[255];
01505 unsigned res_num = 0;
01506 int retlen = 0;
01507 unsigned ch_left = vc->audio_channels;
01508 unsigned vlen;
01509
01510 if (get_bits1(gb)) {
01511 av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
01512 return AVERROR_INVALIDDATA;
01513 }
01514
01515 if (vc->mode_count == 1) {
01516 mode_number = 0;
01517 } else {
01518 GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
01519 }
01520 vc->mode_number = mode_number;
01521 mapping = &vc->mappings[vc->modes[mode_number].mapping];
01522
01523 av_dlog(NULL, " Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
01524 vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
01525
01526 blockflag = vc->modes[mode_number].blockflag;
01527 blocksize = vc->blocksize[blockflag];
01528 vlen = blocksize / 2;
01529 if (blockflag)
01530 skip_bits(gb, 2);
01531
01532 memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * vlen);
01533 memset(ch_floor_ptr, 0, sizeof(float) * vc->audio_channels * vlen);
01534
01535
01536
01537 for (i = 0; i < vc->audio_channels; ++i) {
01538 vorbis_floor *floor;
01539 int ret;
01540 if (mapping->submaps > 1) {
01541 floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
01542 } else {
01543 floor = &vc->floors[mapping->submap_floor[0]];
01544 }
01545
01546 ret = floor->decode(vc, &floor->data, ch_floor_ptr);
01547
01548 if (ret < 0) {
01549 av_log(vc->avccontext, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
01550 return AVERROR_INVALIDDATA;
01551 }
01552 no_residue[i] = ret;
01553 ch_floor_ptr += vlen;
01554 }
01555
01556
01557
01558 for (i = mapping->coupling_steps - 1; i >= 0; --i) {
01559 if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
01560 no_residue[mapping->magnitude[i]] = 0;
01561 no_residue[mapping->angle[i]] = 0;
01562 }
01563 }
01564
01565
01566
01567 for (i = 0; i < mapping->submaps; ++i) {
01568 vorbis_residue *residue;
01569 unsigned ch = 0;
01570 int ret;
01571
01572 for (j = 0; j < vc->audio_channels; ++j) {
01573 if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
01574 res_chan[j] = res_num;
01575 if (no_residue[j]) {
01576 do_not_decode[ch] = 1;
01577 } else {
01578 do_not_decode[ch] = 0;
01579 }
01580 ++ch;
01581 ++res_num;
01582 }
01583 }
01584 residue = &vc->residues[mapping->submap_residue[i]];
01585 if (ch_left < ch) {
01586 av_log(vc->avccontext, AV_LOG_ERROR, "Too many channels in vorbis_floor_decode.\n");
01587 return -1;
01588 }
01589 if (ch) {
01590 ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left);
01591 if (ret < 0)
01592 return ret;
01593 }
01594
01595 ch_res_ptr += ch * vlen;
01596 ch_left -= ch;
01597 }
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 AVCodec ff_vorbis_decoder = {
01718 .name = "vorbis",
01719 .type = AVMEDIA_TYPE_AUDIO,
01720 .id = CODEC_ID_VORBIS,
01721 .priv_data_size = sizeof(vorbis_context),
01722 .init = vorbis_decode_init,
01723 .close = vorbis_decode_close,
01724 .decode = vorbis_decode_frame,
01725 .capabilities = CODEC_CAP_DR1,
01726 .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
01727 .channel_layouts = ff_vorbis_channel_layouts,
01728 .sample_fmts = (const enum AVSampleFormat[]) {
01729 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
01730 },
01731 };
01732