- vorbis_floor * floors=vc->floors;
- vorbis_floor0 * vf;
- int idx;
- int_fast32_t * map;
- int_fast32_t n; //TODO: could theoretically be smaller?
-
- n=(vc->modes[mode_number].blockflag ?
- vc->blocksize_1 : vc->blocksize_0) / 2;
- floors[mode_number].data.t0.map=
- av_malloc((n+1) * sizeof(int_fast32_t)); // n+sentinel
-
- map=floors[mode_number].data.t0.map;
- vf=&floors[mode_number].data.t0;
-
- for (idx=0; idx<n;++idx) {
- map[idx]=floor( BARK((vf->rate*idx)/(2.0f*n)) *
- ((vf->bark_map_size)/
- BARK(vf->rate/2.0f )) );
- if (vf->bark_map_size-1 < map[idx]) {
- map[idx]=vf->bark_map_size-1;
- }
- }
- map[n]=-1;
- vf->map_size=n;
-
-# ifdef V_DEBUG
- for(idx=0;idx<=n;++idx) {
- AV_DEBUG("floor0 map: map at pos %d is %d\n",
- idx, map[idx]);
- }
-# endif
-}
-
-static int vorbis_parse_setup_hdr_modes(vorbis_context *vc) {
- GetBitContext *gb=&vc->gb;
- uint_fast8_t i;
-
- vc->mode_count=get_bits(gb, 6)+1;
- vc->modes=(vorbis_mode *)av_mallocz(vc->mode_count * sizeof(vorbis_mode));
-
- AV_DEBUG(" There are %d modes.\n", vc->mode_count);
-
- for(i=0;i<vc->mode_count;++i) {
- vorbis_mode *mode_setup=&vc->modes[i];
-
- mode_setup->blockflag=get_bits(gb, 1);
- mode_setup->windowtype=get_bits(gb, 16); //FIXME check
- mode_setup->transformtype=get_bits(gb, 16); //FIXME check
- mode_setup->mapping=get_bits(gb, 8); //FIXME check
-
- 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);
-
- if (vc->floors[i].floor_type == 0) { create_map( vc, i ); }
- }
- return 0;
-}
-
-// Process the whole setup header using the functions above
-
-static int vorbis_parse_setup_hdr(vorbis_context *vc) {
- GetBitContext *gb=&vc->gb;
-
- if ((get_bits(gb, 8)!='v') || (get_bits(gb, 8)!='o') ||
- (get_bits(gb, 8)!='r') || (get_bits(gb, 8)!='b') ||
- (get_bits(gb, 8)!='i') || (get_bits(gb, 8)!='s')) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
- return 1;
- }
-
- if (vorbis_parse_setup_hdr_codebooks(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
- return 2;
- }
- if (vorbis_parse_setup_hdr_tdtransforms(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
- return 3;
- }
- if (vorbis_parse_setup_hdr_floors(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
- return 4;
- }
- if (vorbis_parse_setup_hdr_residues(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
- return 5;
- }
- if (vorbis_parse_setup_hdr_mappings(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
- return 6;
- }
- if (vorbis_parse_setup_hdr_modes(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
- return 7;
- }
- if (!get_bits1(gb)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
- return 8; // framing flag bit unset error
- }
-
- return 0;
-}
-
-// Process the identification header
-
-static int vorbis_parse_id_hdr(vorbis_context *vc){
- GetBitContext *gb=&vc->gb;
- uint_fast8_t bl0, bl1;
- const float *vwin[8]={ vwin64, vwin128, vwin256, vwin512, vwin1024, vwin2048, vwin4096, vwin8192 };
-
- if ((get_bits(gb, 8)!='v') || (get_bits(gb, 8)!='o') ||
- (get_bits(gb, 8)!='r') || (get_bits(gb, 8)!='b') ||
- (get_bits(gb, 8)!='i') || (get_bits(gb, 8)!='s')) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
- return 1;
- }
-
- vc->version=get_bits_long_le(gb, 32); //FIXME check 0
- vc->audio_channels=get_bits(gb, 8); //FIXME check >0
- vc->audio_samplerate=get_bits_long_le(gb, 32); //FIXME check >0
- vc->bitrate_maximum=get_bits_long_le(gb, 32);
- vc->bitrate_nominal=get_bits_long_le(gb, 32);
- vc->bitrate_minimum=get_bits_long_le(gb, 32);
- bl0=get_bits(gb, 4);
- bl1=get_bits(gb, 4);
- vc->blocksize_0=(1<<bl0);
- vc->blocksize_1=(1<<bl1);
- if (bl0>13 || bl0<6 || bl1>13 || bl1<6) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
- return 3;
- }
- vc->swin=vwin[bl0-6];
- vc->lwin=vwin[bl1-6];
-
- if ((get_bits1(gb)) == 0) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
- return 2;
- }
-
- vc->channel_residues=(float *)av_malloc((vc->blocksize_1/2)*vc->audio_channels * sizeof(float));
- vc->channel_floors=(float *)av_malloc((vc->blocksize_1/2)*vc->audio_channels * sizeof(float));
- vc->saved=(float *)av_malloc((vc->blocksize_1/2)*vc->audio_channels * sizeof(float));
- vc->ret=(float *)av_malloc((vc->blocksize_1/2)*vc->audio_channels * sizeof(float));
- vc->buf=(float *)av_malloc(vc->blocksize_1 * sizeof(float));
- vc->buf_tmp=(float *)av_malloc(vc->blocksize_1 * sizeof(float));
- vc->saved_start=0;
-
- ff_mdct_init(&vc->mdct0, bl0, 1);
- ff_mdct_init(&vc->mdct1, bl1, 1);
-
- 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 ",
- vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize_0, vc->blocksize_1);
-
-/*
- BLK=vc->blocksize_0;
- for(i=0;i<BLK/2;++i) {
- vc->swin[i]=sin(0.5*3.14159265358*(sin(((float)i+0.5)/(float)BLK*3.14159265358))*(sin(((float)i+0.5)/(float)BLK*3.14159265358)));
- }
-*/
-
- return 0;
-}
-
-// Process the extradata using the functions above (identification header, setup header)
-
-static int vorbis_decode_init(AVCodecContext *avccontext) {
- vorbis_context *vc = avccontext->priv_data ;
- uint8_t *headers = avccontext->extradata;
- int headers_len=avccontext->extradata_size;
- uint8_t *header_start[3];
- int header_len[3];
- GetBitContext *gb = &(vc->gb);
- int i, j, hdr_type;
-
- vc->avccontext = avccontext;
-
- if (!headers_len) {
- av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
- return -1;
- }
-
- if(headers[0] == 0 && headers[1] == 30) {
- for(i = 0; i < 3; i++){
- header_len[i] = *headers++ << 8;
- header_len[i] += *headers++;
- header_start[i] = headers;
- headers += header_len[i];
- }
- } else if(headers[0] == 2) {
- for(j=1,i=0;i<2;++i, ++j) {
- header_len[i]=0;
- while(j<headers_len && headers[j]==0xff) {
- header_len[i]+=0xff;
- ++j;
- }
- if (j>=headers_len) {
- av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
- return -1;
- }
- header_len[i]+=headers[j];
- }
- header_len[2]=headers_len-header_len[0]-header_len[1]-j;
- headers+=j;
- header_start[0] = headers;
- header_start[1] = header_start[0] + header_len[0];
- header_start[2] = header_start[1] + header_len[1];
- } else {
- av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
- return -1;
- }
-
- init_get_bits(gb, header_start[0], header_len[0]*8);
- hdr_type=get_bits(gb, 8);
- if (hdr_type!=1) {
- av_log(avccontext, AV_LOG_ERROR, "First header is not the id header.\n");
- return -1;
- }
- if (vorbis_parse_id_hdr(vc)) {
- av_log(avccontext, AV_LOG_ERROR, "Id header corrupt.\n");
- vorbis_free(vc);
- return -1;
- }
-
- init_get_bits(gb, header_start[2], header_len[2]*8);
- hdr_type=get_bits(gb, 8);
- if (hdr_type!=5) {
- av_log(avccontext, AV_LOG_ERROR, "Third header is not the setup header.\n");
- return -1;
- }
- if (vorbis_parse_setup_hdr(vc)) {
- av_log(avccontext, AV_LOG_ERROR, "Setup header corrupt.\n");
- vorbis_free(vc);
- return -1;
- }
-
- avccontext->channels = vc->audio_channels;
- avccontext->sample_rate = vc->audio_samplerate;
-
- return 0 ;
-}
-
-// Decode audiopackets -------------------------------------------------
-
-// Read and decode floor
-
-static uint_fast8_t vorbis_floor0_decode(vorbis_context *vc,
- vorbis_floor_data *vfu, float *vec) {
- vorbis_floor0 * vf=&vfu->t0;
- float * lsp=vf->lsp;
- uint_fast32_t amplitude;
- uint_fast32_t book_idx;
-
- amplitude=get_bits(&vc->gb, vf->amplitude_bits);
- if (amplitude>0) {
- float last = 0;
- uint_fast16_t lsp_len = 0;
- uint_fast16_t idx;
- vorbis_codebook codebook;
-
- book_idx=get_bits(&vc->gb, ilog(vf->num_books));
- if ( book_idx >= vf->num_books ) {
- av_log( vc->avccontext, AV_LOG_ERROR,
- "floor0 dec: booknumber too high!\n" );
- //FIXME: look above
- }
- AV_DEBUG( "floor0 dec: booknumber: %u\n", book_idx );
- codebook=vc->codebooks[vf->book_list[book_idx]];
-
- while (lsp_len<vf->order) {
- int vec_off;
-
- AV_DEBUG( "floor0 dec: book dimension: %d\n", codebook.dimensions );
- AV_DEBUG( "floor0 dec: maximum depth: %d\n", codebook.maxdepth );
- /* read temp vector */
- vec_off=get_vlc2(&vc->gb,
- codebook.vlc.table,
- codebook.nb_bits,
- codebook.maxdepth ) *
- codebook.dimensions;
- AV_DEBUG( "floor0 dec: vector offset: %d\n", vec_off );
- /* copy each vector component and add last to it */
- for (idx=0; idx<codebook.dimensions; ++idx) {
- lsp[lsp_len+idx]=codebook.codevectors[vec_off+idx]+last;
- }
- last=lsp[lsp_len+idx-1]; /* set last to last vector component */
-
- lsp_len += codebook.dimensions;
- }
-#ifdef V_DEBUG
- /* DEBUG: output lsp coeffs */
- {
- int idx;
- for ( idx = 0; idx < lsp_len; ++idx )
- AV_DEBUG("floor0 dec: coeff at %d is %f\n", idx, lsp[idx] );
- }
-#endif
-
- /* synthesize floor output vector */
- {
- int i;
- int order=vf->order;
- float wstep=M_PI/vf->bark_map_size;
-
- for(i=0;i<order;i++) { lsp[i]=2.0f*cos(lsp[i]); }
-
- AV_DEBUG("floor0 synth: map_size=%d; m=%d; wstep=%f\n",
- vf->map_size, order, wstep);
-
- i=0;
- while(i<vf->map_size) {
- int j, iter_cond=vf->map[i];
- float p=0.5f;
- float q=0.5f;
- float two_cos_w=2.0f*cos(wstep*iter_cond); // needed all times
-
- /* similar part for the q and p products */
- for(j=0;j<order;j+=2) {
- q *= lsp[j] -two_cos_w;
- p *= lsp[j+1]-two_cos_w;
- }
- if(j==order) { // even order
- p *= p*(2.0f-two_cos_w);
- q *= q*(2.0f+two_cos_w);
- }
- else { // odd order
- q *= two_cos_w-lsp[j]; // one more time for q
-
- /* final step and square */
- p *= p*(4.f-two_cos_w*two_cos_w);
- q *= q;
- }
-
- /* calculate linear floor value */
- {
- q=exp( (
- ( (amplitude*vf->amplitude_offset)/
- (((1<<vf->amplitude_bits)-1) * sqrt(p+q)) )
- - vf->amplitude_offset ) * .11512925f
- );
- }
-
- /* fill vector */
- do { vec[i]=q; ++i; }while(vf->map[i]==iter_cond);
- }
- }
- }
- else {
- /* this channel is unused */
- return 1;
- }
-
- AV_DEBUG(" Floor0 decoded\n");
-
- return 0;
-}
-static uint_fast8_t vorbis_floor1_decode(vorbis_context *vc, vorbis_floor_data *vfu, float *vec) {
- vorbis_floor1 * vf=&vfu->t1;
- GetBitContext *gb=&vc->gb;
- uint_fast16_t range_v[4]={ 256, 128, 86, 64 };
- uint_fast16_t range=range_v[vf->multiplier-1];
- uint_fast16_t floor1_Y[vf->x_list_dim];
- uint_fast16_t floor1_Y_final[vf->x_list_dim];
- uint_fast8_t floor1_flag[vf->x_list_dim];
- uint_fast8_t class_;
- uint_fast8_t cdim;
- uint_fast8_t cbits;
- uint_fast8_t csub;
- uint_fast8_t cval;
- int_fast16_t book;
- uint_fast16_t offset;
- uint_fast16_t i,j;
- uint_fast16_t *floor_x_sort=vf->x_list_order;
- /*u*/int_fast16_t adx, ady, off, predicted; // WTF ? dy/adx= (unsigned)dy/adx ?
- int_fast16_t dy, err;
- uint_fast16_t lx,hx, ly, hy=0;
-
-
- if (!get_bits1(gb)) return 1; // silence
-
-// Read values (or differences) for the floor's points
-
- floor1_Y[0]=get_bits(gb, ilog(range-1));
- floor1_Y[1]=get_bits(gb, ilog(range-1));
-
- AV_DEBUG("floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
-
- offset=2;
- for(i=0;i<vf->partitions;++i) {
- class_=vf->partition_class[i];
- cdim=vf->class_dimensions[class_];
- cbits=vf->class_subclasses[class_];
- csub=(1<<cbits)-1;
- cval=0;
-
- AV_DEBUG("Cbits %d \n", cbits);
-
- if (cbits) { // this reads all subclasses for this partition's class
- cval=get_vlc2(gb, vc->codebooks[vf->class_masterbook[class_]].vlc.table,
- vc->codebooks[vf->class_masterbook[class_]].nb_bits, 3);
- }
-
- for(j=0;j<cdim;++j) {
- book=vf->subclass_books[class_][cval & csub];
-
- AV_DEBUG("book %d Cbits %d cval %d bits:%d \n", book, cbits, cval, get_bits_count(gb));
-
- cval=cval>>cbits;
- if (book>0) {
- floor1_Y[offset+j]=get_vlc2(gb, vc->codebooks[book].vlc.table,
- vc->codebooks[book].nb_bits, 3);