nuclear@1: /******************************************************************** nuclear@1: * * nuclear@1: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * nuclear@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * nuclear@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * nuclear@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * nuclear@1: * * nuclear@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * nuclear@1: * by the Xiph.Org Foundation http://www.xiph.org/ * nuclear@1: * * nuclear@1: ******************************************************************** nuclear@1: nuclear@1: function: PCM data vector blocking, windowing and dis/reassembly nuclear@1: last mod: $Id: block.c 17561 2010-10-23 10:34:24Z xiphmont $ nuclear@1: nuclear@1: Handle windowing, overlap-add, etc of the PCM vectors. This is made nuclear@1: more amusing by Vorbis' current two allowed block sizes. nuclear@1: nuclear@1: ********************************************************************/ nuclear@1: nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include "vorbis/codec.h" nuclear@1: #include "codec_internal.h" nuclear@1: nuclear@1: #include "window.h" nuclear@1: #include "mdct.h" nuclear@1: #include "lpc.h" nuclear@1: #include "registry.h" nuclear@1: #include "misc.h" nuclear@1: nuclear@1: static int ilog2(unsigned int v){ nuclear@1: int ret=0; nuclear@1: if(v)--v; nuclear@1: while(v){ nuclear@1: ret++; nuclear@1: v>>=1; nuclear@1: } nuclear@1: return(ret); nuclear@1: } nuclear@1: nuclear@1: /* pcm accumulator examples (not exhaustive): nuclear@1: nuclear@1: <-------------- lW ----------------> nuclear@1: <--------------- W ----------------> nuclear@1: : .....|..... _______________ | nuclear@1: : .''' | '''_--- | |\ | nuclear@1: :.....''' |_____--- '''......| | \_______| nuclear@1: :.................|__________________|_______|__|______| nuclear@1: |<------ Sl ------>| > Sr < |endW nuclear@1: |beginSl |endSl | |endSr nuclear@1: |beginW |endlW |beginSr nuclear@1: nuclear@1: nuclear@1: |< lW >| nuclear@1: <--------------- W ----------------> nuclear@1: | | .. ______________ | nuclear@1: | | ' `/ | ---_ | nuclear@1: |___.'___/`. | ---_____| nuclear@1: |_______|__|_______|_________________| nuclear@1: | >|Sl|< |<------ Sr ----->|endW nuclear@1: | | |endSl |beginSr |endSr nuclear@1: |beginW | |endlW nuclear@1: mult[0] |beginSl mult[n] nuclear@1: nuclear@1: <-------------- lW -----------------> nuclear@1: |<--W-->| nuclear@1: : .............. ___ | | nuclear@1: : .''' |`/ \ | | nuclear@1: :.....''' |/`....\|...| nuclear@1: :.........................|___|___|___| nuclear@1: |Sl |Sr |endW nuclear@1: | | |endSr nuclear@1: | |beginSr nuclear@1: | |endSl nuclear@1: |beginSl nuclear@1: |beginW nuclear@1: */ nuclear@1: nuclear@1: /* block abstraction setup *********************************************/ nuclear@1: nuclear@1: #ifndef WORD_ALIGN nuclear@1: #define WORD_ALIGN 8 nuclear@1: #endif nuclear@1: nuclear@1: int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){ nuclear@1: int i; nuclear@1: memset(vb,0,sizeof(*vb)); nuclear@1: vb->vd=v; nuclear@1: vb->localalloc=0; nuclear@1: vb->localstore=NULL; nuclear@1: if(v->analysisp){ nuclear@1: vorbis_block_internal *vbi= nuclear@1: vb->internal=_ogg_calloc(1,sizeof(vorbis_block_internal)); nuclear@1: vbi->ampmax=-9999; nuclear@1: nuclear@1: for(i=0;ipacketblob[i]=&vb->opb; nuclear@1: }else{ nuclear@1: vbi->packetblob[i]= nuclear@1: _ogg_calloc(1,sizeof(oggpack_buffer)); nuclear@1: } nuclear@1: oggpack_writeinit(vbi->packetblob[i]); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: void *_vorbis_block_alloc(vorbis_block *vb,long bytes){ nuclear@1: bytes=(bytes+(WORD_ALIGN-1)) & ~(WORD_ALIGN-1); nuclear@1: if(bytes+vb->localtop>vb->localalloc){ nuclear@1: /* can't just _ogg_realloc... there are outstanding pointers */ nuclear@1: if(vb->localstore){ nuclear@1: struct alloc_chain *link=_ogg_malloc(sizeof(*link)); nuclear@1: vb->totaluse+=vb->localtop; nuclear@1: link->next=vb->reap; nuclear@1: link->ptr=vb->localstore; nuclear@1: vb->reap=link; nuclear@1: } nuclear@1: /* highly conservative */ nuclear@1: vb->localalloc=bytes; nuclear@1: vb->localstore=_ogg_malloc(vb->localalloc); nuclear@1: vb->localtop=0; nuclear@1: } nuclear@1: { nuclear@1: void *ret=(void *)(((char *)vb->localstore)+vb->localtop); nuclear@1: vb->localtop+=bytes; nuclear@1: return ret; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* reap the chain, pull the ripcord */ nuclear@1: void _vorbis_block_ripcord(vorbis_block *vb){ nuclear@1: /* reap the chain */ nuclear@1: struct alloc_chain *reap=vb->reap; nuclear@1: while(reap){ nuclear@1: struct alloc_chain *next=reap->next; nuclear@1: _ogg_free(reap->ptr); nuclear@1: memset(reap,0,sizeof(*reap)); nuclear@1: _ogg_free(reap); nuclear@1: reap=next; nuclear@1: } nuclear@1: /* consolidate storage */ nuclear@1: if(vb->totaluse){ nuclear@1: vb->localstore=_ogg_realloc(vb->localstore,vb->totaluse+vb->localalloc); nuclear@1: vb->localalloc+=vb->totaluse; nuclear@1: vb->totaluse=0; nuclear@1: } nuclear@1: nuclear@1: /* pull the ripcord */ nuclear@1: vb->localtop=0; nuclear@1: vb->reap=NULL; nuclear@1: } nuclear@1: nuclear@1: int vorbis_block_clear(vorbis_block *vb){ nuclear@1: int i; nuclear@1: vorbis_block_internal *vbi=vb->internal; nuclear@1: nuclear@1: _vorbis_block_ripcord(vb); nuclear@1: if(vb->localstore)_ogg_free(vb->localstore); nuclear@1: nuclear@1: if(vbi){ nuclear@1: for(i=0;ipacketblob[i]); nuclear@1: if(i!=PACKETBLOBS/2)_ogg_free(vbi->packetblob[i]); nuclear@1: } nuclear@1: _ogg_free(vbi); nuclear@1: } nuclear@1: memset(vb,0,sizeof(*vb)); nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: /* Analysis side code, but directly related to blocking. Thus it's nuclear@1: here and not in analysis.c (which is for analysis transforms only). nuclear@1: The init is here because some of it is shared */ nuclear@1: nuclear@1: static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){ nuclear@1: int i; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: private_state *b=NULL; nuclear@1: int hs; nuclear@1: nuclear@1: if(ci==NULL) return 1; nuclear@1: hs=ci->halfrate_flag; nuclear@1: nuclear@1: memset(v,0,sizeof(*v)); nuclear@1: b=v->backend_state=_ogg_calloc(1,sizeof(*b)); nuclear@1: nuclear@1: v->vi=vi; nuclear@1: b->modebits=ilog2(ci->modes); nuclear@1: nuclear@1: b->transform[0]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[0])); nuclear@1: b->transform[1]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[1])); nuclear@1: nuclear@1: /* MDCT is tranform 0 */ nuclear@1: nuclear@1: b->transform[0][0]=_ogg_calloc(1,sizeof(mdct_lookup)); nuclear@1: b->transform[1][0]=_ogg_calloc(1,sizeof(mdct_lookup)); nuclear@1: mdct_init(b->transform[0][0],ci->blocksizes[0]>>hs); nuclear@1: mdct_init(b->transform[1][0],ci->blocksizes[1]>>hs); nuclear@1: nuclear@1: /* Vorbis I uses only window type 0 */ nuclear@1: b->window[0]=ilog2(ci->blocksizes[0])-6; nuclear@1: b->window[1]=ilog2(ci->blocksizes[1])-6; nuclear@1: nuclear@1: if(encp){ /* encode/decode differ here */ nuclear@1: nuclear@1: /* analysis always needs an fft */ nuclear@1: drft_init(&b->fft_look[0],ci->blocksizes[0]); nuclear@1: drft_init(&b->fft_look[1],ci->blocksizes[1]); nuclear@1: nuclear@1: /* finish the codebooks */ nuclear@1: if(!ci->fullbooks){ nuclear@1: ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks)); nuclear@1: for(i=0;ibooks;i++) nuclear@1: vorbis_book_init_encode(ci->fullbooks+i,ci->book_param[i]); nuclear@1: } nuclear@1: nuclear@1: b->psy=_ogg_calloc(ci->psys,sizeof(*b->psy)); nuclear@1: for(i=0;ipsys;i++){ nuclear@1: _vp_psy_init(b->psy+i, nuclear@1: ci->psy_param[i], nuclear@1: &ci->psy_g_param, nuclear@1: ci->blocksizes[ci->psy_param[i]->blockflag]/2, nuclear@1: vi->rate); nuclear@1: } nuclear@1: nuclear@1: v->analysisp=1; nuclear@1: }else{ nuclear@1: /* finish the codebooks */ nuclear@1: if(!ci->fullbooks){ nuclear@1: ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks)); nuclear@1: for(i=0;ibooks;i++){ nuclear@1: if(ci->book_param[i]==NULL) nuclear@1: goto abort_books; nuclear@1: if(vorbis_book_init_decode(ci->fullbooks+i,ci->book_param[i])) nuclear@1: goto abort_books; nuclear@1: /* decode codebooks are now standalone after init */ nuclear@1: vorbis_staticbook_destroy(ci->book_param[i]); nuclear@1: ci->book_param[i]=NULL; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* initialize the storage vectors. blocksize[1] is small for encode, nuclear@1: but the correct size for decode */ nuclear@1: v->pcm_storage=ci->blocksizes[1]; nuclear@1: v->pcm=_ogg_malloc(vi->channels*sizeof(*v->pcm)); nuclear@1: v->pcmret=_ogg_malloc(vi->channels*sizeof(*v->pcmret)); nuclear@1: { nuclear@1: int i; nuclear@1: for(i=0;ichannels;i++) nuclear@1: v->pcm[i]=_ogg_calloc(v->pcm_storage,sizeof(*v->pcm[i])); nuclear@1: } nuclear@1: nuclear@1: /* all 1 (large block) or 0 (small block) */ nuclear@1: /* explicitly set for the sake of clarity */ nuclear@1: v->lW=0; /* previous window size */ nuclear@1: v->W=0; /* current window size */ nuclear@1: nuclear@1: /* all vector indexes */ nuclear@1: v->centerW=ci->blocksizes[1]/2; nuclear@1: nuclear@1: v->pcm_current=v->centerW; nuclear@1: nuclear@1: /* initialize all the backend lookups */ nuclear@1: b->flr=_ogg_calloc(ci->floors,sizeof(*b->flr)); nuclear@1: b->residue=_ogg_calloc(ci->residues,sizeof(*b->residue)); nuclear@1: nuclear@1: for(i=0;ifloors;i++) nuclear@1: b->flr[i]=_floor_P[ci->floor_type[i]]-> nuclear@1: look(v,ci->floor_param[i]); nuclear@1: nuclear@1: for(i=0;iresidues;i++) nuclear@1: b->residue[i]=_residue_P[ci->residue_type[i]]-> nuclear@1: look(v,ci->residue_param[i]); nuclear@1: nuclear@1: return 0; nuclear@1: abort_books: nuclear@1: for(i=0;ibooks;i++){ nuclear@1: if(ci->book_param[i]!=NULL){ nuclear@1: vorbis_staticbook_destroy(ci->book_param[i]); nuclear@1: ci->book_param[i]=NULL; nuclear@1: } nuclear@1: } nuclear@1: vorbis_dsp_clear(v); nuclear@1: return -1; nuclear@1: } nuclear@1: nuclear@1: /* arbitrary settings and spec-mandated numbers get filled in here */ nuclear@1: int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){ nuclear@1: private_state *b=NULL; nuclear@1: nuclear@1: if(_vds_shared_init(v,vi,1))return 1; nuclear@1: b=v->backend_state; nuclear@1: b->psy_g_look=_vp_global_look(vi); nuclear@1: nuclear@1: /* Initialize the envelope state storage */ nuclear@1: b->ve=_ogg_calloc(1,sizeof(*b->ve)); nuclear@1: _ve_envelope_init(b->ve,vi); nuclear@1: nuclear@1: vorbis_bitrate_init(vi,&b->bms); nuclear@1: nuclear@1: /* compressed audio packets start after the headers nuclear@1: with sequence number 3 */ nuclear@1: v->sequence=3; nuclear@1: nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: void vorbis_dsp_clear(vorbis_dsp_state *v){ nuclear@1: int i; nuclear@1: if(v){ nuclear@1: vorbis_info *vi=v->vi; nuclear@1: codec_setup_info *ci=(vi?vi->codec_setup:NULL); nuclear@1: private_state *b=v->backend_state; nuclear@1: nuclear@1: if(b){ nuclear@1: nuclear@1: if(b->ve){ nuclear@1: _ve_envelope_clear(b->ve); nuclear@1: _ogg_free(b->ve); nuclear@1: } nuclear@1: nuclear@1: if(b->transform[0]){ nuclear@1: mdct_clear(b->transform[0][0]); nuclear@1: _ogg_free(b->transform[0][0]); nuclear@1: _ogg_free(b->transform[0]); nuclear@1: } nuclear@1: if(b->transform[1]){ nuclear@1: mdct_clear(b->transform[1][0]); nuclear@1: _ogg_free(b->transform[1][0]); nuclear@1: _ogg_free(b->transform[1]); nuclear@1: } nuclear@1: nuclear@1: if(b->flr){ nuclear@1: if(ci) nuclear@1: for(i=0;ifloors;i++) nuclear@1: _floor_P[ci->floor_type[i]]-> nuclear@1: free_look(b->flr[i]); nuclear@1: _ogg_free(b->flr); nuclear@1: } nuclear@1: if(b->residue){ nuclear@1: if(ci) nuclear@1: for(i=0;iresidues;i++) nuclear@1: _residue_P[ci->residue_type[i]]-> nuclear@1: free_look(b->residue[i]); nuclear@1: _ogg_free(b->residue); nuclear@1: } nuclear@1: if(b->psy){ nuclear@1: if(ci) nuclear@1: for(i=0;ipsys;i++) nuclear@1: _vp_psy_clear(b->psy+i); nuclear@1: _ogg_free(b->psy); nuclear@1: } nuclear@1: nuclear@1: if(b->psy_g_look)_vp_global_free(b->psy_g_look); nuclear@1: vorbis_bitrate_clear(&b->bms); nuclear@1: nuclear@1: drft_clear(&b->fft_look[0]); nuclear@1: drft_clear(&b->fft_look[1]); nuclear@1: nuclear@1: } nuclear@1: nuclear@1: if(v->pcm){ nuclear@1: if(vi) nuclear@1: for(i=0;ichannels;i++) nuclear@1: if(v->pcm[i])_ogg_free(v->pcm[i]); nuclear@1: _ogg_free(v->pcm); nuclear@1: if(v->pcmret)_ogg_free(v->pcmret); nuclear@1: } nuclear@1: nuclear@1: if(b){ nuclear@1: /* free header, header1, header2 */ nuclear@1: if(b->header)_ogg_free(b->header); nuclear@1: if(b->header1)_ogg_free(b->header1); nuclear@1: if(b->header2)_ogg_free(b->header2); nuclear@1: _ogg_free(b); nuclear@1: } nuclear@1: nuclear@1: memset(v,0,sizeof(*v)); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: float **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){ nuclear@1: int i; nuclear@1: vorbis_info *vi=v->vi; nuclear@1: private_state *b=v->backend_state; nuclear@1: nuclear@1: /* free header, header1, header2 */ nuclear@1: if(b->header)_ogg_free(b->header);b->header=NULL; nuclear@1: if(b->header1)_ogg_free(b->header1);b->header1=NULL; nuclear@1: if(b->header2)_ogg_free(b->header2);b->header2=NULL; nuclear@1: nuclear@1: /* Do we have enough storage space for the requested buffer? If not, nuclear@1: expand the PCM (and envelope) storage */ nuclear@1: nuclear@1: if(v->pcm_current+vals>=v->pcm_storage){ nuclear@1: v->pcm_storage=v->pcm_current+vals*2; nuclear@1: nuclear@1: for(i=0;ichannels;i++){ nuclear@1: v->pcm[i]=_ogg_realloc(v->pcm[i],v->pcm_storage*sizeof(*v->pcm[i])); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: for(i=0;ichannels;i++) nuclear@1: v->pcmret[i]=v->pcm[i]+v->pcm_current; nuclear@1: nuclear@1: return(v->pcmret); nuclear@1: } nuclear@1: nuclear@1: static void _preextrapolate_helper(vorbis_dsp_state *v){ nuclear@1: int i; nuclear@1: int order=16; nuclear@1: float *lpc=alloca(order*sizeof(*lpc)); nuclear@1: float *work=alloca(v->pcm_current*sizeof(*work)); nuclear@1: long j; nuclear@1: v->preextrapolate=1; nuclear@1: nuclear@1: if(v->pcm_current-v->centerW>order*2){ /* safety */ nuclear@1: for(i=0;ivi->channels;i++){ nuclear@1: /* need to run the extrapolation in reverse! */ nuclear@1: for(j=0;jpcm_current;j++) nuclear@1: work[j]=v->pcm[i][v->pcm_current-j-1]; nuclear@1: nuclear@1: /* prime as above */ nuclear@1: vorbis_lpc_from_data(work,lpc,v->pcm_current-v->centerW,order); nuclear@1: nuclear@1: #if 0 nuclear@1: if(v->vi->channels==2){ nuclear@1: if(i==0) nuclear@1: _analysis_output("predataL",0,work,v->pcm_current-v->centerW,0,0,0); nuclear@1: else nuclear@1: _analysis_output("predataR",0,work,v->pcm_current-v->centerW,0,0,0); nuclear@1: }else{ nuclear@1: _analysis_output("predata",0,work,v->pcm_current-v->centerW,0,0,0); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: /* run the predictor filter */ nuclear@1: vorbis_lpc_predict(lpc,work+v->pcm_current-v->centerW-order, nuclear@1: order, nuclear@1: work+v->pcm_current-v->centerW, nuclear@1: v->centerW); nuclear@1: nuclear@1: for(j=0;jpcm_current;j++) nuclear@1: v->pcm[i][v->pcm_current-j-1]=work[j]; nuclear@1: nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* call with val<=0 to set eof */ nuclear@1: nuclear@1: int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){ nuclear@1: vorbis_info *vi=v->vi; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: nuclear@1: if(vals<=0){ nuclear@1: int order=32; nuclear@1: int i; nuclear@1: float *lpc=alloca(order*sizeof(*lpc)); nuclear@1: nuclear@1: /* if it wasn't done earlier (very short sample) */ nuclear@1: if(!v->preextrapolate) nuclear@1: _preextrapolate_helper(v); nuclear@1: nuclear@1: /* We're encoding the end of the stream. Just make sure we have nuclear@1: [at least] a few full blocks of zeroes at the end. */ nuclear@1: /* actually, we don't want zeroes; that could drop a large nuclear@1: amplitude off a cliff, creating spread spectrum noise that will nuclear@1: suck to encode. Extrapolate for the sake of cleanliness. */ nuclear@1: nuclear@1: vorbis_analysis_buffer(v,ci->blocksizes[1]*3); nuclear@1: v->eofflag=v->pcm_current; nuclear@1: v->pcm_current+=ci->blocksizes[1]*3; nuclear@1: nuclear@1: for(i=0;ichannels;i++){ nuclear@1: if(v->eofflag>order*2){ nuclear@1: /* extrapolate with LPC to fill in */ nuclear@1: long n; nuclear@1: nuclear@1: /* make a predictor filter */ nuclear@1: n=v->eofflag; nuclear@1: if(n>ci->blocksizes[1])n=ci->blocksizes[1]; nuclear@1: vorbis_lpc_from_data(v->pcm[i]+v->eofflag-n,lpc,n,order); nuclear@1: nuclear@1: /* run the predictor filter */ nuclear@1: vorbis_lpc_predict(lpc,v->pcm[i]+v->eofflag-order,order, nuclear@1: v->pcm[i]+v->eofflag,v->pcm_current-v->eofflag); nuclear@1: }else{ nuclear@1: /* not enough data to extrapolate (unlikely to happen due to nuclear@1: guarding the overlap, but bulletproof in case that nuclear@1: assumtion goes away). zeroes will do. */ nuclear@1: memset(v->pcm[i]+v->eofflag,0, nuclear@1: (v->pcm_current-v->eofflag)*sizeof(*v->pcm[i])); nuclear@1: nuclear@1: } nuclear@1: } nuclear@1: }else{ nuclear@1: nuclear@1: if(v->pcm_current+vals>v->pcm_storage) nuclear@1: return(OV_EINVAL); nuclear@1: nuclear@1: v->pcm_current+=vals; nuclear@1: nuclear@1: /* we may want to reverse extrapolate the beginning of a stream nuclear@1: too... in case we're beginning on a cliff! */ nuclear@1: /* clumsy, but simple. It only runs once, so simple is good. */ nuclear@1: if(!v->preextrapolate && v->pcm_current-v->centerW>ci->blocksizes[1]) nuclear@1: _preextrapolate_helper(v); nuclear@1: nuclear@1: } nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: /* do the deltas, envelope shaping, pre-echo and determine the size of nuclear@1: the next block on which to continue analysis */ nuclear@1: int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){ nuclear@1: int i; nuclear@1: vorbis_info *vi=v->vi; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: private_state *b=v->backend_state; nuclear@1: vorbis_look_psy_global *g=b->psy_g_look; nuclear@1: long beginW=v->centerW-ci->blocksizes[v->W]/2,centerNext; nuclear@1: vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal; nuclear@1: nuclear@1: /* check to see if we're started... */ nuclear@1: if(!v->preextrapolate)return(0); nuclear@1: nuclear@1: /* check to see if we're done... */ nuclear@1: if(v->eofflag==-1)return(0); nuclear@1: nuclear@1: /* By our invariant, we have lW, W and centerW set. Search for nuclear@1: the next boundary so we can determine nW (the next window size) nuclear@1: which lets us compute the shape of the current block's window */ nuclear@1: nuclear@1: /* we do an envelope search even on a single blocksize; we may still nuclear@1: be throwing more bits at impulses, and envelope search handles nuclear@1: marking impulses too. */ nuclear@1: { nuclear@1: long bp=_ve_envelope_search(v); nuclear@1: if(bp==-1){ nuclear@1: nuclear@1: if(v->eofflag==0)return(0); /* not enough data currently to search for a nuclear@1: full long block */ nuclear@1: v->nW=0; nuclear@1: }else{ nuclear@1: nuclear@1: if(ci->blocksizes[0]==ci->blocksizes[1]) nuclear@1: v->nW=0; nuclear@1: else nuclear@1: v->nW=bp; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: centerNext=v->centerW+ci->blocksizes[v->W]/4+ci->blocksizes[v->nW]/4; nuclear@1: nuclear@1: { nuclear@1: /* center of next block + next block maximum right side. */ nuclear@1: nuclear@1: long blockbound=centerNext+ci->blocksizes[v->nW]/2; nuclear@1: if(v->pcm_currentlW=v->lW; nuclear@1: vb->W=v->W; nuclear@1: vb->nW=v->nW; nuclear@1: nuclear@1: if(v->W){ nuclear@1: if(!v->lW || !v->nW){ nuclear@1: vbi->blocktype=BLOCKTYPE_TRANSITION; nuclear@1: /*fprintf(stderr,"-");*/ nuclear@1: }else{ nuclear@1: vbi->blocktype=BLOCKTYPE_LONG; nuclear@1: /*fprintf(stderr,"_");*/ nuclear@1: } nuclear@1: }else{ nuclear@1: if(_ve_envelope_mark(v)){ nuclear@1: vbi->blocktype=BLOCKTYPE_IMPULSE; nuclear@1: /*fprintf(stderr,"|");*/ nuclear@1: nuclear@1: }else{ nuclear@1: vbi->blocktype=BLOCKTYPE_PADDING; nuclear@1: /*fprintf(stderr,".");*/ nuclear@1: nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: vb->vd=v; nuclear@1: vb->sequence=v->sequence++; nuclear@1: vb->granulepos=v->granulepos; nuclear@1: vb->pcmend=ci->blocksizes[v->W]; nuclear@1: nuclear@1: /* copy the vectors; this uses the local storage in vb */ nuclear@1: nuclear@1: /* this tracks 'strongest peak' for later psychoacoustics */ nuclear@1: /* moved to the global psy state; clean this mess up */ nuclear@1: if(vbi->ampmax>g->ampmax)g->ampmax=vbi->ampmax; nuclear@1: g->ampmax=_vp_ampmax_decay(g->ampmax,v); nuclear@1: vbi->ampmax=g->ampmax; nuclear@1: nuclear@1: vb->pcm=_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels); nuclear@1: vbi->pcmdelay=_vorbis_block_alloc(vb,sizeof(*vbi->pcmdelay)*vi->channels); nuclear@1: for(i=0;ichannels;i++){ nuclear@1: vbi->pcmdelay[i]= nuclear@1: _vorbis_block_alloc(vb,(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i])); nuclear@1: memcpy(vbi->pcmdelay[i],v->pcm[i],(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i])); nuclear@1: vb->pcm[i]=vbi->pcmdelay[i]+beginW; nuclear@1: nuclear@1: /* before we added the delay nuclear@1: vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i])); nuclear@1: memcpy(vb->pcm[i],v->pcm[i]+beginW,ci->blocksizes[v->W]*sizeof(*vb->pcm[i])); nuclear@1: */ nuclear@1: nuclear@1: } nuclear@1: nuclear@1: /* handle eof detection: eof==0 means that we've not yet received EOF nuclear@1: eof>0 marks the last 'real' sample in pcm[] nuclear@1: eof<0 'no more to do'; doesn't get here */ nuclear@1: nuclear@1: if(v->eofflag){ nuclear@1: if(v->centerW>=v->eofflag){ nuclear@1: v->eofflag=-1; nuclear@1: vb->eofflag=1; nuclear@1: return(1); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* advance storage vectors and clean up */ nuclear@1: { nuclear@1: int new_centerNext=ci->blocksizes[1]/2; nuclear@1: int movementW=centerNext-new_centerNext; nuclear@1: nuclear@1: if(movementW>0){ nuclear@1: nuclear@1: _ve_envelope_shift(b->ve,movementW); nuclear@1: v->pcm_current-=movementW; nuclear@1: nuclear@1: for(i=0;ichannels;i++) nuclear@1: memmove(v->pcm[i],v->pcm[i]+movementW, nuclear@1: v->pcm_current*sizeof(*v->pcm[i])); nuclear@1: nuclear@1: nuclear@1: v->lW=v->W; nuclear@1: v->W=v->nW; nuclear@1: v->centerW=new_centerNext; nuclear@1: nuclear@1: if(v->eofflag){ nuclear@1: v->eofflag-=movementW; nuclear@1: if(v->eofflag<=0)v->eofflag=-1; nuclear@1: /* do not add padding to end of stream! */ nuclear@1: if(v->centerW>=v->eofflag){ nuclear@1: v->granulepos+=movementW-(v->centerW-v->eofflag); nuclear@1: }else{ nuclear@1: v->granulepos+=movementW; nuclear@1: } nuclear@1: }else{ nuclear@1: v->granulepos+=movementW; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* done */ nuclear@1: return(1); nuclear@1: } nuclear@1: nuclear@1: int vorbis_synthesis_restart(vorbis_dsp_state *v){ nuclear@1: vorbis_info *vi=v->vi; nuclear@1: codec_setup_info *ci; nuclear@1: int hs; nuclear@1: nuclear@1: if(!v->backend_state)return -1; nuclear@1: if(!vi)return -1; nuclear@1: ci=vi->codec_setup; nuclear@1: if(!ci)return -1; nuclear@1: hs=ci->halfrate_flag; nuclear@1: nuclear@1: v->centerW=ci->blocksizes[1]>>(hs+1); nuclear@1: v->pcm_current=v->centerW>>hs; nuclear@1: nuclear@1: v->pcm_returned=-1; nuclear@1: v->granulepos=-1; nuclear@1: v->sequence=-1; nuclear@1: v->eofflag=0; nuclear@1: ((private_state *)(v->backend_state))->sample_count=-1; nuclear@1: nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){ nuclear@1: if(_vds_shared_init(v,vi,0)){ nuclear@1: vorbis_dsp_clear(v); nuclear@1: return 1; nuclear@1: } nuclear@1: vorbis_synthesis_restart(v); nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: /* Unlike in analysis, the window is only partially applied for each nuclear@1: block. The time domain envelope is not yet handled at the point of nuclear@1: calling (as it relies on the previous block). */ nuclear@1: nuclear@1: int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){ nuclear@1: vorbis_info *vi=v->vi; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: private_state *b=v->backend_state; nuclear@1: int hs=ci->halfrate_flag; nuclear@1: int i,j; nuclear@1: nuclear@1: if(!vb)return(OV_EINVAL); nuclear@1: if(v->pcm_current>v->pcm_returned && v->pcm_returned!=-1)return(OV_EINVAL); nuclear@1: nuclear@1: v->lW=v->W; nuclear@1: v->W=vb->W; nuclear@1: v->nW=-1; nuclear@1: nuclear@1: if((v->sequence==-1)|| nuclear@1: (v->sequence+1 != vb->sequence)){ nuclear@1: v->granulepos=-1; /* out of sequence; lose count */ nuclear@1: b->sample_count=-1; nuclear@1: } nuclear@1: nuclear@1: v->sequence=vb->sequence; nuclear@1: nuclear@1: if(vb->pcm){ /* no pcm to process if vorbis_synthesis_trackonly nuclear@1: was called on block */ nuclear@1: int n=ci->blocksizes[v->W]>>(hs+1); nuclear@1: int n0=ci->blocksizes[0]>>(hs+1); nuclear@1: int n1=ci->blocksizes[1]>>(hs+1); nuclear@1: nuclear@1: int thisCenter; nuclear@1: int prevCenter; nuclear@1: nuclear@1: v->glue_bits+=vb->glue_bits; nuclear@1: v->time_bits+=vb->time_bits; nuclear@1: v->floor_bits+=vb->floor_bits; nuclear@1: v->res_bits+=vb->res_bits; nuclear@1: nuclear@1: if(v->centerW){ nuclear@1: thisCenter=n1; nuclear@1: prevCenter=0; nuclear@1: }else{ nuclear@1: thisCenter=0; nuclear@1: prevCenter=n1; nuclear@1: } nuclear@1: nuclear@1: /* v->pcm is now used like a two-stage double buffer. We don't want nuclear@1: to have to constantly shift *or* adjust memory usage. Don't nuclear@1: accept a new block until the old is shifted out */ nuclear@1: nuclear@1: for(j=0;jchannels;j++){ nuclear@1: /* the overlap/add section */ nuclear@1: if(v->lW){ nuclear@1: if(v->W){ nuclear@1: /* large/large */ nuclear@1: float *w=_vorbis_window_get(b->window[1]-hs); nuclear@1: float *pcm=v->pcm[j]+prevCenter; nuclear@1: float *p=vb->pcm[j]; nuclear@1: for(i=0;iwindow[0]-hs); nuclear@1: float *pcm=v->pcm[j]+prevCenter+n1/2-n0/2; nuclear@1: float *p=vb->pcm[j]; nuclear@1: for(i=0;iW){ nuclear@1: /* small/large */ nuclear@1: float *w=_vorbis_window_get(b->window[0]-hs); nuclear@1: float *pcm=v->pcm[j]+prevCenter; nuclear@1: float *p=vb->pcm[j]+n1/2-n0/2; nuclear@1: for(i=0;iwindow[0]-hs); nuclear@1: float *pcm=v->pcm[j]+prevCenter; nuclear@1: float *p=vb->pcm[j]; nuclear@1: for(i=0;ipcm[j]+thisCenter; nuclear@1: float *p=vb->pcm[j]+n; nuclear@1: for(i=0;icenterW) nuclear@1: v->centerW=0; nuclear@1: else nuclear@1: v->centerW=n1; nuclear@1: nuclear@1: /* deal with initial packet state; we do this using the explicit nuclear@1: pcm_returned==-1 flag otherwise we're sensitive to first block nuclear@1: being short or long */ nuclear@1: nuclear@1: if(v->pcm_returned==-1){ nuclear@1: v->pcm_returned=thisCenter; nuclear@1: v->pcm_current=thisCenter; nuclear@1: }else{ nuclear@1: v->pcm_returned=prevCenter; nuclear@1: v->pcm_current=prevCenter+ nuclear@1: ((ci->blocksizes[v->lW]/4+ nuclear@1: ci->blocksizes[v->W]/4)>>hs); nuclear@1: } nuclear@1: nuclear@1: } nuclear@1: nuclear@1: /* track the frame number... This is for convenience, but also nuclear@1: making sure our last packet doesn't end with added padding. If nuclear@1: the last packet is partial, the number of samples we'll have to nuclear@1: return will be past the vb->granulepos. nuclear@1: nuclear@1: This is not foolproof! It will be confused if we begin nuclear@1: decoding at the last page after a seek or hole. In that case, nuclear@1: we don't have a starting point to judge where the last frame nuclear@1: is. For this reason, vorbisfile will always try to make sure nuclear@1: it reads the last two marked pages in proper sequence */ nuclear@1: nuclear@1: if(b->sample_count==-1){ nuclear@1: b->sample_count=0; nuclear@1: }else{ nuclear@1: b->sample_count+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4; nuclear@1: } nuclear@1: nuclear@1: if(v->granulepos==-1){ nuclear@1: if(vb->granulepos!=-1){ /* only set if we have a position to set to */ nuclear@1: nuclear@1: v->granulepos=vb->granulepos; nuclear@1: nuclear@1: /* is this a short page? */ nuclear@1: if(b->sample_count>v->granulepos){ nuclear@1: /* corner case; if this is both the first and last audio page, nuclear@1: then spec says the end is cut, not beginning */ nuclear@1: long extra=b->sample_count-vb->granulepos; nuclear@1: nuclear@1: /* we use ogg_int64_t for granule positions because a nuclear@1: uint64 isn't universally available. Unfortunately, nuclear@1: that means granposes can be 'negative' and result in nuclear@1: extra being negative */ nuclear@1: if(extra<0) nuclear@1: extra=0; nuclear@1: nuclear@1: if(vb->eofflag){ nuclear@1: /* trim the end */ nuclear@1: /* no preceding granulepos; assume we started at zero (we'd nuclear@1: have to in a short single-page stream) */ nuclear@1: /* granulepos could be -1 due to a seek, but that would result nuclear@1: in a long count, not short count */ nuclear@1: nuclear@1: /* Guard against corrupt/malicious frames that set EOP and nuclear@1: a backdated granpos; don't rewind more samples than we nuclear@1: actually have */ nuclear@1: if(extra > (v->pcm_current - v->pcm_returned)<pcm_current - v->pcm_returned)<pcm_current-=extra>>hs; nuclear@1: }else{ nuclear@1: /* trim the beginning */ nuclear@1: v->pcm_returned+=extra>>hs; nuclear@1: if(v->pcm_returned>v->pcm_current) nuclear@1: v->pcm_returned=v->pcm_current; nuclear@1: } nuclear@1: nuclear@1: } nuclear@1: nuclear@1: } nuclear@1: }else{ nuclear@1: v->granulepos+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4; nuclear@1: if(vb->granulepos!=-1 && v->granulepos!=vb->granulepos){ nuclear@1: nuclear@1: if(v->granulepos>vb->granulepos){ nuclear@1: long extra=v->granulepos-vb->granulepos; nuclear@1: nuclear@1: if(extra) nuclear@1: if(vb->eofflag){ nuclear@1: /* partial last frame. Strip the extra samples off */ nuclear@1: nuclear@1: /* Guard against corrupt/malicious frames that set EOP and nuclear@1: a backdated granpos; don't rewind more samples than we nuclear@1: actually have */ nuclear@1: if(extra > (v->pcm_current - v->pcm_returned)<pcm_current - v->pcm_returned)<pcm_current-=extra>>hs; nuclear@1: } /* else {Shouldn't happen *unless* the bitstream is out of nuclear@1: spec. Either way, believe the bitstream } */ nuclear@1: } /* else {Shouldn't happen *unless* the bitstream is out of nuclear@1: spec. Either way, believe the bitstream } */ nuclear@1: v->granulepos=vb->granulepos; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* Update, cleanup */ nuclear@1: nuclear@1: if(vb->eofflag)v->eofflag=1; nuclear@1: return(0); nuclear@1: nuclear@1: } nuclear@1: nuclear@1: /* pcm==NULL indicates we just want the pending samples, no more */ nuclear@1: int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm){ nuclear@1: vorbis_info *vi=v->vi; nuclear@1: nuclear@1: if(v->pcm_returned>-1 && v->pcm_returnedpcm_current){ nuclear@1: if(pcm){ nuclear@1: int i; nuclear@1: for(i=0;ichannels;i++) nuclear@1: v->pcmret[i]=v->pcm[i]+v->pcm_returned; nuclear@1: *pcm=v->pcmret; nuclear@1: } nuclear@1: return(v->pcm_current-v->pcm_returned); nuclear@1: } nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: int vorbis_synthesis_read(vorbis_dsp_state *v,int n){ nuclear@1: if(n && v->pcm_returned+n>v->pcm_current)return(OV_EINVAL); nuclear@1: v->pcm_returned+=n; nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: /* intended for use with a specific vorbisfile feature; we want access nuclear@1: to the [usually synthetic/postextrapolated] buffer and lapping at nuclear@1: the end of a decode cycle, specifically, a half-short-block worth. nuclear@1: This funtion works like pcmout above, except it will also expose nuclear@1: this implicit buffer data not normally decoded. */ nuclear@1: int vorbis_synthesis_lapout(vorbis_dsp_state *v,float ***pcm){ nuclear@1: vorbis_info *vi=v->vi; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: int hs=ci->halfrate_flag; nuclear@1: nuclear@1: int n=ci->blocksizes[v->W]>>(hs+1); nuclear@1: int n0=ci->blocksizes[0]>>(hs+1); nuclear@1: int n1=ci->blocksizes[1]>>(hs+1); nuclear@1: int i,j; nuclear@1: nuclear@1: if(v->pcm_returned<0)return 0; nuclear@1: nuclear@1: /* our returned data ends at pcm_returned; because the synthesis pcm nuclear@1: buffer is a two-fragment ring, that means our data block may be nuclear@1: fragmented by buffering, wrapping or a short block not filling nuclear@1: out a buffer. To simplify things, we unfragment if it's at all nuclear@1: possibly needed. Otherwise, we'd need to call lapout more than nuclear@1: once as well as hold additional dsp state. Opt for nuclear@1: simplicity. */ nuclear@1: nuclear@1: /* centerW was advanced by blockin; it would be the center of the nuclear@1: *next* block */ nuclear@1: if(v->centerW==n1){ nuclear@1: /* the data buffer wraps; swap the halves */ nuclear@1: /* slow, sure, small */ nuclear@1: for(j=0;jchannels;j++){ nuclear@1: float *p=v->pcm[j]; nuclear@1: for(i=0;ipcm_current-=n1; nuclear@1: v->pcm_returned-=n1; nuclear@1: v->centerW=0; nuclear@1: } nuclear@1: nuclear@1: /* solidify buffer into contiguous space */ nuclear@1: if((v->lW^v->W)==1){ nuclear@1: /* long/short or short/long */ nuclear@1: for(j=0;jchannels;j++){ nuclear@1: float *s=v->pcm[j]; nuclear@1: float *d=v->pcm[j]+(n1-n0)/2; nuclear@1: for(i=(n1+n0)/2-1;i>=0;--i) nuclear@1: d[i]=s[i]; nuclear@1: } nuclear@1: v->pcm_returned+=(n1-n0)/2; nuclear@1: v->pcm_current+=(n1-n0)/2; nuclear@1: }else{ nuclear@1: if(v->lW==0){ nuclear@1: /* short/short */ nuclear@1: for(j=0;jchannels;j++){ nuclear@1: float *s=v->pcm[j]; nuclear@1: float *d=v->pcm[j]+n1-n0; nuclear@1: for(i=n0-1;i>=0;--i) nuclear@1: d[i]=s[i]; nuclear@1: } nuclear@1: v->pcm_returned+=n1-n0; nuclear@1: v->pcm_current+=n1-n0; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: if(pcm){ nuclear@1: int i; nuclear@1: for(i=0;ichannels;i++) nuclear@1: v->pcmret[i]=v->pcm[i]+v->pcm_returned; nuclear@1: *pcm=v->pcmret; nuclear@1: } nuclear@1: nuclear@1: return(n1+n-v->pcm_returned); nuclear@1: nuclear@1: } nuclear@1: nuclear@1: float *vorbis_window(vorbis_dsp_state *v,int W){ nuclear@1: vorbis_info *vi=v->vi; nuclear@1: codec_setup_info *ci=vi->codec_setup; nuclear@1: int hs=ci->halfrate_flag; nuclear@1: private_state *b=v->backend_state; nuclear@1: nuclear@1: if(b->window[W]-1<0)return NULL; nuclear@1: return _vorbis_window_get(b->window[W]-hs); nuclear@1: }