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