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