📄 vorbis.c
字号:
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 (type 1 only)static uint_fast8_t vorbis_floor1_decode(vorbis_context *vc, vorbis_floor *vf, float *vec) { 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); } else { floor1_Y[offset+j]=0; } AV_DEBUG(" floor(%d) = %d \n", vf->x_list[offset+j], floor1_Y[offset+j]); } offset+=cdim; }// Amplitude calculation from the differences floor1_flag[0]=1; floor1_flag[1]=1; floor1_Y_final[0]=floor1_Y[0]; floor1_Y_final[1]=floor1_Y[1]; for(i=2;i<vf->x_list_dim;++i) { uint_fast16_t val, highroom, lowroom, room; uint_fast16_t high_neigh_offs; uint_fast16_t low_neigh_offs; low_neigh_offs=vf->low_neighbour[i]; high_neigh_offs=vf->high_neighbour[i]; dy=floor1_Y_final[high_neigh_offs]-floor1_Y_final[low_neigh_offs]; // render_point begin adx=vf->x_list[high_neigh_offs]-vf->x_list[low_neigh_offs]; ady= ABS(dy); err=ady*(vf->x_list[i]-vf->x_list[low_neigh_offs]); off=err/adx; if (dy<0) { predicted=floor1_Y_final[low_neigh_offs]-off; } else { predicted=floor1_Y_final[low_neigh_offs]+off; } // render_point end val=floor1_Y[i]; highroom=range-predicted; lowroom=predicted; if (highroom < lowroom) { room=highroom*2; } else { room=lowroom*2; // SPEC mispelling } if (val) { floor1_flag[low_neigh_offs]=1; floor1_flag[high_neigh_offs]=1; floor1_flag[i]=1; if (val>=room) { if (highroom > lowroom) { floor1_Y_final[i]=val-lowroom+predicted; } else { floor1_Y_final[i]=predicted-val+highroom-1; } } else { if (val & 1) { floor1_Y_final[i]=predicted-(val+1)/2; } else { floor1_Y_final[i]=predicted+val/2; } } } else { floor1_flag[i]=0; floor1_Y_final[i]=predicted; } AV_DEBUG(" Decoded floor(%d) = %d / val %d \n", vf->x_list[i], floor1_Y_final[i], val); }// Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ? hx=0; lx=0; ly=floor1_Y_final[0]*vf->multiplier; // conforms to SPEC vec[0]=floor1_inverse_db_table[ly]; for(i=1;i<vf->x_list_dim;++i) { AV_DEBUG(" Looking at post %d \n", i); if (floor1_flag[floor_x_sort[i]]) { // SPEC mispelled int_fast16_t x, y, dy, base, sy; // if uncommented: dy = -32 adx = 2 base = 2blablabla ????? hy=floor1_Y_final[floor_x_sort[i]]*vf->multiplier; hx=vf->x_list[floor_x_sort[i]]; dy=hy-ly; adx=hx-lx; ady= (dy<0) ? -dy:dy;//ABS(dy); base=dy/adx; AV_DEBUG(" dy %d adx %d base %d = %d \n", dy, adx, base, dy/adx); x=lx; y=ly; err=0; if (dy<0) { sy=base-1; } else { sy=base+1; } ady=ady-(base<0 ? -base : base)*adx; vec[x]=floor1_inverse_db_table[y]; AV_DEBUG(" vec[ %d ] = %d \n", x, y); for(x=lx+1;(x<hx) && (x<vf->x_list[1]);++x) { err+=ady; if (err>=adx) { err-=adx; y+=sy; } else { y+=base; } vec[x]=floor1_inverse_db_table[y]; AV_DEBUG(" vec[ %d ] = %d \n", x, y); }/* for(j=1;j<hx-lx+1;++j) { // iterating render_point dy=hy-ly; adx=hx-lx; ady= dy<0 ? -dy : dy; err=ady*j; off=err/adx; if (dy<0) { predicted=ly-off; } else { predicted=ly+off; } if (lx+j < vf->x_list[1]) { vec[lx+j]=floor1_inverse_db_table[predicted]; } }*/ lx=hx; ly=hy; } } if (hx<vf->x_list[1]) { for(i=hx;i<vf->x_list[1];++i) { vec[i]=floor1_inverse_db_table[hy]; } } AV_DEBUG(" Floor decoded\n"); return 0;}// Read and decode residuestatic 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) { GetBitContext *gb=&vc->gb; uint_fast8_t c_p_c=vc->codebooks[vr->classbook].dimensions; uint_fast16_t n_to_read=vr->end-vr->begin; uint_fast16_t ptns_to_read=n_to_read/vr->partition_size; uint_fast8_t classifs[ptns_to_read*vc->audio_channels]; uint_fast8_t pass; uint_fast8_t ch_used; uint_fast8_t i,j,l; uint_fast16_t k; if (vr->type==2) { for(j=1;j<ch;++j) { do_not_decode[0]&=do_not_decode[j]; // FIXME - clobbering input } if (do_not_decode[0]) return 0; ch_used=1; } else { ch_used=ch; } AV_DEBUG(" residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c); for(pass=0;pass<=vr->maxpass;++pass) { // FIXME OPTIMIZE? uint_fast16_t voffset; uint_fast16_t partition_count; uint_fast16_t j_times_ptns_to_read; voffset=vr->begin; for(partition_count=0;partition_count<ptns_to_read;) { // SPEC error if (!pass) { for(j_times_ptns_to_read=0, j=0;j<ch_used;++j) { if (!do_not_decode[j]) { uint_fast32_t temp=get_vlc2(gb, vc->codebooks[vr->classbook].vlc.table, vc->codebooks[vr->classbook].nb_bits, 3);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -