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