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