📄 block.c
字号:
/* check to see if we're started... */ if(!v->preextrapolate)return(0); /* check to see if we're done... */ if(v->eofflag==-1)return(0); /* By our invariant, we have lW, W and centerW set. Search for the next boundary so we can determine nW (the next window size) which lets us compute the shape of the current block's window */ /* we do an envelope search even on a single blocksize; we may still be throwing more bits at impulses, and envelope search handles marking impulses too. */ { long bp=_ve_envelope_search(v); if(bp==-1){ if(v->eofflag==0)return(0); /* not enough data currently to search for a full long block */ v->nW=0; }else{ if(ci->blocksizes[0]==ci->blocksizes[1]) v->nW=0; else v->nW=bp; } } centerNext=v->centerW+ci->blocksizes[v->W]/4+ci->blocksizes[v->nW]/4; { /* center of next block + next block maximum right side. */ long blockbound=centerNext+ci->blocksizes[v->nW]/2; if(v->pcm_current<blockbound)return(0); /* not enough data yet; although this check is less strict that the _ve_envelope_search, the search is not run if we only use one block size */ } /* fill in the block. Note that for a short window, lW and nW are *short* regardless of actual settings in the stream */ _vorbis_block_ripcord(vb); vb->lW=v->lW; vb->W=v->W; vb->nW=v->nW; if(v->W){ if(!v->lW || !v->nW){ vbi->blocktype=BLOCKTYPE_TRANSITION; /*fprintf(stderr,"-");*/ }else{ vbi->blocktype=BLOCKTYPE_LONG; /*fprintf(stderr,"_");*/ } }else{ if(_ve_envelope_mark(v)){ vbi->blocktype=BLOCKTYPE_IMPULSE; /*fprintf(stderr,"|");*/ }else{ vbi->blocktype=BLOCKTYPE_PADDING; /*fprintf(stderr,".");*/ } } vb->vd=v; vb->sequence=v->sequence++; vb->granulepos=v->granulepos; vb->pcmend=ci->blocksizes[v->W]; /* copy the vectors; this uses the local storage in vb */ /* this tracks 'strongest peak' for later psychoacoustics */ /* moved to the global psy state; clean this mess up */ if(vbi->ampmax>g->ampmax)g->ampmax=vbi->ampmax; g->ampmax=_vp_ampmax_decay(g->ampmax,v); vbi->ampmax=g->ampmax; vb->pcm=_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels); vbi->pcmdelay=_vorbis_block_alloc(vb,sizeof(*vbi->pcmdelay)*vi->channels); for(i=0;i<vi->channels;i++){ vbi->pcmdelay[i]= _vorbis_block_alloc(vb,(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i])); memcpy(vbi->pcmdelay[i],v->pcm[i],(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i])); vb->pcm[i]=vbi->pcmdelay[i]+beginW; /* before we added the delay vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i])); memcpy(vb->pcm[i],v->pcm[i]+beginW,ci->blocksizes[v->W]*sizeof(*vb->pcm[i])); */ } /* handle eof detection: eof==0 means that we've not yet received EOF eof>0 marks the last 'real' sample in pcm[] eof<0 'no more to do'; doesn't get here */ if(v->eofflag){ if(v->centerW>=v->eofflag){ v->eofflag=-1; vb->eofflag=1; return(1); } } /* advance storage vectors and clean up */ { int new_centerNext=ci->blocksizes[1]/2; int movementW=centerNext-new_centerNext; if(movementW>0){ _ve_envelope_shift(b->ve,movementW); v->pcm_current-=movementW; for(i=0;i<vi->channels;i++) memmove(v->pcm[i],v->pcm[i]+movementW, v->pcm_current*sizeof(*v->pcm[i])); v->lW=v->W; v->W=v->nW; v->centerW=new_centerNext; if(v->eofflag){ v->eofflag-=movementW; if(v->eofflag<=0)v->eofflag=-1; /* do not add padding to end of stream! */ if(v->centerW>=v->eofflag){ v->granulepos+=movementW-(v->centerW-v->eofflag); }else{ v->granulepos+=movementW; } }else{ v->granulepos+=movementW; } } } /* done */ return(1);}int vorbis_synthesis_restart(vorbis_dsp_state *v){ vorbis_info *vi=v->vi; codec_setup_info *ci; int hs; if(!v->backend_state)return -1; if(!vi)return -1; ci=vi->codec_setup; if(!ci)return -1; hs=ci->halfrate_flag; v->centerW=ci->blocksizes[1]>>(hs+1); v->pcm_current=v->centerW>>hs; v->pcm_returned=-1; v->granulepos=-1; v->sequence=-1; v->eofflag=0; ((private_state *)(v->backend_state))->sample_count=-1; return(0);}int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){ if(_vds_shared_init(v,vi,0)) return 1; vorbis_synthesis_restart(v); return 0;}/* Unlike in analysis, the window is only partially applied for each block. The time domain envelope is not yet handled at the point of calling (as it relies on the previous block). */int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){ vorbis_info *vi=v->vi; codec_setup_info *ci=vi->codec_setup; private_state *b=v->backend_state; int hs=ci->halfrate_flag; int i,j; if(!vb)return(OV_EINVAL); if(v->pcm_current>v->pcm_returned && v->pcm_returned!=-1)return(OV_EINVAL); v->lW=v->W; v->W=vb->W; v->nW=-1; if((v->sequence==-1)|| (v->sequence+1 != vb->sequence)){ v->granulepos=-1; /* out of sequence; lose count */ b->sample_count=-1; } v->sequence=vb->sequence; if(vb->pcm){ /* no pcm to process if vorbis_synthesis_trackonly was called on block */ int n=ci->blocksizes[v->W]>>(hs+1); int n0=ci->blocksizes[0]>>(hs+1); int n1=ci->blocksizes[1]>>(hs+1); int thisCenter; int prevCenter; v->glue_bits+=vb->glue_bits; v->time_bits+=vb->time_bits; v->floor_bits+=vb->floor_bits; v->res_bits+=vb->res_bits; if(v->centerW){ thisCenter=n1; prevCenter=0; }else{ thisCenter=0; prevCenter=n1; } /* v->pcm is now used like a two-stage double buffer. We don't want to have to constantly shift *or* adjust memory usage. Don't accept a new block until the old is shifted out */ for(j=0;j<vi->channels;j++){ /* the overlap/add section */ if(v->lW){ if(v->W){ /* large/large */ float *w=_vorbis_window_get(b->window[1]-hs); float *pcm=v->pcm[j]+prevCenter; float *p=vb->pcm[j]; for(i=0;i<n1;i++) pcm[i]=pcm[i]*w[n1-i-1] + p[i]*w[i]; }else{ /* large/small */ float *w=_vorbis_window_get(b->window[0]-hs); float *pcm=v->pcm[j]+prevCenter+n1/2-n0/2; float *p=vb->pcm[j]; for(i=0;i<n0;i++) pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i]; } }else{ if(v->W){ /* small/large */ float *w=_vorbis_window_get(b->window[0]-hs); float *pcm=v->pcm[j]+prevCenter; float *p=vb->pcm[j]+n1/2-n0/2; for(i=0;i<n0;i++) pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i]; for(;i<n1/2+n0/2;i++) pcm[i]=p[i]; }else{ /* small/small */ float *w=_vorbis_window_get(b->window[0]-hs); float *pcm=v->pcm[j]+prevCenter; float *p=vb->pcm[j]; for(i=0;i<n0;i++) pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i]; } } /* the copy section */ { float *pcm=v->pcm[j]+thisCenter; float *p=vb->pcm[j]+n; for(i=0;i<n;i++) pcm[i]=p[i]; } } if(v->centerW) v->centerW=0; else v->centerW=n1; /* deal with initial packet state; we do this using the explicit pcm_returned==-1 flag otherwise we're sensitive to first block being short or long */ if(v->pcm_returned==-1){ v->pcm_returned=thisCenter; v->pcm_current=thisCenter; }else{ v->pcm_returned=prevCenter; v->pcm_current=prevCenter+ ((ci->blocksizes[v->lW]/4+ ci->blocksizes[v->W]/4)>>hs); } } /* track the frame number... This is for convenience, but also making sure our last packet doesn't end with added padding. If the last packet is partial, the number of samples we'll have to return will be past the vb->granulepos. This is not foolproof! It will be confused if we begin decoding at the last page after a seek or hole. In that case, we don't have a starting point to judge where the last frame is. For this reason, vorbisfile will always try to make sure it reads the last two marked pages in proper sequence */ if(b->sample_count==-1){ b->sample_count=0; }else{ b->sample_count+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4; } if(v->granulepos==-1){ if(vb->granulepos!=-1){ /* only set if we have a position to set to */ v->granulepos=vb->granulepos; /* is this a short page? */ if(b->sample_count>v->granulepos){ /* corner case; if this is both the first and last audio page, then spec says the end is cut, not beginning */ if(vb->eofflag){ /* trim the end */ /* no preceeding granulepos; assume we started at zero (we'd have to in a short single-page stream) */ /* granulepos could be -1 due to a seek, but that would result in a long count, not short count */ v->pcm_current-=(b->sample_count-v->granulepos)>>hs; }else{ /* trim the beginning */ v->pcm_returned+=(b->sample_count-v->granulepos)>>hs; if(v->pcm_returned>v->pcm_current) v->pcm_returned=v->pcm_current; } } } }else{ v->granulepos+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4; if(vb->granulepos!=-1 && v->granulepos!=vb->granulepos){ if(v->granulepos>vb->granulepos){ long extra=v->granulepos-vb->granulepos; if(extra) if(vb->eofflag){ /* partial last frame. Strip the extra samples off */ v->pcm_current-=extra>>hs; } /* else {Shouldn't happen *unless* the bitstream is out of spec. Either way, believe the bitstream } */ } /* else {Shouldn't happen *unless* the bitstream is out of spec. Either way, believe the bitstream } */ v->granulepos=vb->granulepos; } } /* Update, cleanup */ if(vb->eofflag)v->eofflag=1; return(0); }/* pcm==NULL indicates we just want the pending samples, no more */int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm){ vorbis_info *vi=v->vi; if(v->pcm_returned>-1 && v->pcm_returned<v->pcm_current){ if(pcm){ int i; for(i=0;i<vi->channels;i++) v->pcmret[i]=v->pcm[i]+v->pcm_returned; *pcm=v->pcmret; } return(v->pcm_current-v->pcm_returned); } return(0);}int vorbis_synthesis_read(vorbis_dsp_state *v,int n){ if(n && v->pcm_returned+n>v->pcm_current)return(OV_EINVAL); v->pcm_returned+=n; return(0);}/* intended for use with a specific vorbisfile feature; we want access to the [usually synthetic/postextrapolated] buffer and lapping at the end of a decode cycle, specifically, a half-short-block worth. This funtion works like pcmout above, except it will also expose this implicit buffer data not normally decoded. */int vorbis_synthesis_lapout(vorbis_dsp_state *v,float ***pcm){ vorbis_info *vi=v->vi; codec_setup_info *ci=vi->codec_setup; int hs=ci->halfrate_flag; int n=ci->blocksizes[v->W]>>(hs+1); int n0=ci->blocksizes[0]>>(hs+1); int n1=ci->blocksizes[1]>>(hs+1); int i,j; if(v->pcm_returned<0)return 0; /* our returned data ends at pcm_returned; because the synthesis pcm buffer is a two-fragment ring, that means our data block may be fragmented by buffering, wrapping or a short block not filling out a buffer. To simplify things, we unfragment if it's at all possibly needed. Otherwise, we'd need to call lapout more than once as well as hold additional dsp state. Opt for simplicity. */ /* centerW was advanced by blockin; it would be the center of the *next* block */ if(v->centerW==n1){ /* the data buffer wraps; swap the halves */ /* slow, sure, small */ for(j=0;j<vi->channels;j++){ float *p=v->pcm[j]; for(i=0;i<n1;i++){ float temp=p[i]; p[i]=p[i+n1]; p[i+n1]=temp; } } v->pcm_current-=n1; v->pcm_returned-=n1; v->centerW=0; } /* solidify buffer into contiguous space */ if((v->lW^v->W)==1){ /* long/short or short/long */ for(j=0;j<vi->channels;j++){ float *s=v->pcm[j]; float *d=v->pcm[j]+(n1-n0)/2; for(i=(n1+n0)/2-1;i>=0;--i) d[i]=s[i]; } v->pcm_returned+=(n1-n0)/2; v->pcm_current+=(n1-n0)/2; }else{ if(v->lW==0){ /* short/short */ for(j=0;j<vi->channels;j++){ float *s=v->pcm[j]; float *d=v->pcm[j]+n1-n0; for(i=n0-1;i>=0;--i) d[i]=s[i]; } v->pcm_returned+=n1-n0; v->pcm_current+=n1-n0; } } if(pcm){ int i; for(i=0;i<vi->channels;i++) v->pcmret[i]=v->pcm[i]+v->pcm_returned; *pcm=v->pcmret; } return(n1+n-v->pcm_returned);}float *vorbis_window(vorbis_dsp_state *v,int W){ vorbis_info *vi=v->vi; codec_setup_info *ci=vi->codec_setup; int hs=ci->halfrate_flag; private_state *b=v->backend_state; if(b->window[W]-1<0)return NULL; return _vorbis_window_get(b->window[W]-hs);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -