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