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