1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
13 function: PCM data vector blocking, windowing and dis/reassembly
16 Handle windowing, overlap-add, etc of the PCM vectors. This is made
17 more amusing by Vorbis' current two allowed block sizes.
19 ********************************************************************/
25 #include "vorbis/codec.h"
26 #include "codec_internal.h"
34 /* pcm accumulator examples (not exhaustive):
36 <-------------- lW ---------------->
37 <--------------- W ---------------->
38 : .....|..... _______________ |
39 : .''' | '''_--- | |\ |
40 :.....''' |_____--- '''......| | \_______|
41 :.................|__________________|_______|__|______|
42 |<------ Sl ------>| > Sr < |endW
43 |beginSl |endSl | |endSr
44 |beginW |endlW |beginSr
48 <--------------- W ---------------->
49 | | .. ______________ |
51 |___.'___/`. | ---_____|
52 |_______|__|_______|_________________|
53 | >|Sl|< |<------ Sr ----->|endW
54 | | |endSl |beginSr |endSr
56 mult[0] |beginSl mult[n]
58 <-------------- lW ----------------->
60 : .............. ___ | |
62 :.....''' |/`....\|...|
63 :.........................|___|___|___|
72 /* block abstraction setup *********************************************/
78 int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
80 memset(vb,0,sizeof(*vb));
85 vorbis_block_internal *vbi=
86 vb->internal=_ogg_calloc(1,sizeof(vorbis_block_internal));
89 for(i=0;i<PACKETBLOBS;i++){
91 vbi->packetblob[i]=&vb->opb;
94 _ogg_calloc(1,sizeof(oggpack_buffer));
96 oggpack_writeinit(vbi->packetblob[i]);
103 void *_vorbis_block_alloc(vorbis_block *vb,long bytes){
104 bytes=(bytes+(WORD_ALIGN-1)) & ~(WORD_ALIGN-1);
105 if(bytes+vb->localtop>vb->localalloc){
106 /* can't just _ogg_realloc... there are outstanding pointers */
108 struct alloc_chain *link=_ogg_malloc(sizeof(*link));
109 vb->totaluse+=vb->localtop;
111 link->ptr=vb->localstore;
114 /* highly conservative */
115 vb->localalloc=bytes;
116 vb->localstore=_ogg_malloc(vb->localalloc);
120 void *ret=(void *)(((char *)vb->localstore)+vb->localtop);
126 /* reap the chain, pull the ripcord */
127 void _vorbis_block_ripcord(vorbis_block *vb){
129 struct alloc_chain *reap=vb->reap;
131 struct alloc_chain *next=reap->next;
132 _ogg_free(reap->ptr);
133 memset(reap,0,sizeof(*reap));
137 /* consolidate storage */
139 vb->localstore=_ogg_realloc(vb->localstore,vb->totaluse+vb->localalloc);
140 vb->localalloc+=vb->totaluse;
144 /* pull the ripcord */
149 int vorbis_block_clear(vorbis_block *vb){
151 vorbis_block_internal *vbi=vb->internal;
153 _vorbis_block_ripcord(vb);
154 if(vb->localstore)_ogg_free(vb->localstore);
157 for(i=0;i<PACKETBLOBS;i++){
158 oggpack_writeclear(vbi->packetblob[i]);
159 if(i!=PACKETBLOBS/2)_ogg_free(vbi->packetblob[i]);
163 memset(vb,0,sizeof(*vb));
167 /* Analysis side code, but directly related to blocking. Thus it's
168 here and not in analysis.c (which is for analysis transforms only).
169 The init is here because some of it is shared */
171 static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){
173 codec_setup_info *ci=vi->codec_setup;
174 private_state *b=NULL;
179 ci->blocksizes[0]<64||
180 ci->blocksizes[1]<ci->blocksizes[0]){
183 hs=ci->halfrate_flag;
185 memset(v,0,sizeof(*v));
186 b=v->backend_state=_ogg_calloc(1,sizeof(*b));
189 b->modebits=ov_ilog(ci->modes-1);
191 b->transform[0]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[0]));
192 b->transform[1]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[1]));
194 /* MDCT is tranform 0 */
196 b->transform[0][0]=_ogg_calloc(1,sizeof(mdct_lookup));
197 b->transform[1][0]=_ogg_calloc(1,sizeof(mdct_lookup));
198 mdct_init(b->transform[0][0],ci->blocksizes[0]>>hs);
199 mdct_init(b->transform[1][0],ci->blocksizes[1]>>hs);
201 /* Vorbis I uses only window type 0 */
202 /* note that the correct computation below is technically:
203 b->window[0]=ov_ilog(ci->blocksizes[0]-1)-6;
204 b->window[1]=ov_ilog(ci->blocksizes[1]-1)-6;
205 but since blocksizes are always powers of two,
206 the below is equivalent.
208 b->window[0]=ov_ilog(ci->blocksizes[0])-7;
209 b->window[1]=ov_ilog(ci->blocksizes[1])-7;
211 if(encp){ /* encode/decode differ here */
213 /* analysis always needs an fft */
214 drft_init(&b->fft_look[0],ci->blocksizes[0]);
215 drft_init(&b->fft_look[1],ci->blocksizes[1]);
217 /* finish the codebooks */
219 ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks));
220 for(i=0;i<ci->books;i++)
221 vorbis_book_init_encode(ci->fullbooks+i,ci->book_param[i]);
224 b->psy=_ogg_calloc(ci->psys,sizeof(*b->psy));
225 for(i=0;i<ci->psys;i++){
226 _vp_psy_init(b->psy+i,
229 ci->blocksizes[ci->psy_param[i]->blockflag]/2,
235 /* finish the codebooks */
237 ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks));
238 for(i=0;i<ci->books;i++){
239 if(ci->book_param[i]==NULL)
241 if(vorbis_book_init_decode(ci->fullbooks+i,ci->book_param[i]))
243 /* decode codebooks are now standalone after init */
244 vorbis_staticbook_destroy(ci->book_param[i]);
245 ci->book_param[i]=NULL;
250 /* initialize the storage vectors. blocksize[1] is small for encode,
251 but the correct size for decode */
252 v->pcm_storage=ci->blocksizes[1];
253 v->pcm=_ogg_malloc(vi->channels*sizeof(*v->pcm));
254 v->pcmret=_ogg_malloc(vi->channels*sizeof(*v->pcmret));
257 for(i=0;i<vi->channels;i++)
258 v->pcm[i]=_ogg_calloc(v->pcm_storage,sizeof(*v->pcm[i]));
261 /* all 1 (large block) or 0 (small block) */
262 /* explicitly set for the sake of clarity */
263 v->lW=0; /* previous window size */
264 v->W=0; /* current window size */
266 /* all vector indexes */
267 v->centerW=ci->blocksizes[1]/2;
269 v->pcm_current=v->centerW;
271 /* initialize all the backend lookups */
272 b->flr=_ogg_calloc(ci->floors,sizeof(*b->flr));
273 b->residue=_ogg_calloc(ci->residues,sizeof(*b->residue));
275 for(i=0;i<ci->floors;i++)
276 b->flr[i]=_floor_P[ci->floor_type[i]]->
277 look(v,ci->floor_param[i]);
279 for(i=0;i<ci->residues;i++)
280 b->residue[i]=_residue_P[ci->residue_type[i]]->
281 look(v,ci->residue_param[i]);
285 for(i=0;i<ci->books;i++){
286 if(ci->book_param[i]!=NULL){
287 vorbis_staticbook_destroy(ci->book_param[i]);
288 ci->book_param[i]=NULL;
295 /* arbitrary settings and spec-mandated numbers get filled in here */
296 int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
297 private_state *b=NULL;
299 if(_vds_shared_init(v,vi,1))return 1;
301 b->psy_g_look=_vp_global_look(vi);
303 /* Initialize the envelope state storage */
304 b->ve=_ogg_calloc(1,sizeof(*b->ve));
305 _ve_envelope_init(b->ve,vi);
307 vorbis_bitrate_init(vi,&b->bms);
309 /* compressed audio packets start after the headers
310 with sequence number 3 */
316 void vorbis_dsp_clear(vorbis_dsp_state *v){
319 vorbis_info *vi=v->vi;
320 codec_setup_info *ci=(vi?vi->codec_setup:NULL);
321 private_state *b=v->backend_state;
326 _ve_envelope_clear(b->ve);
331 mdct_clear(b->transform[0][0]);
332 _ogg_free(b->transform[0][0]);
333 _ogg_free(b->transform[0]);
336 mdct_clear(b->transform[1][0]);
337 _ogg_free(b->transform[1][0]);
338 _ogg_free(b->transform[1]);
343 for(i=0;i<ci->floors;i++)
344 _floor_P[ci->floor_type[i]]->
345 free_look(b->flr[i]);
350 for(i=0;i<ci->residues;i++)
351 _residue_P[ci->residue_type[i]]->
352 free_look(b->residue[i]);
353 _ogg_free(b->residue);
357 for(i=0;i<ci->psys;i++)
358 _vp_psy_clear(b->psy+i);
362 if(b->psy_g_look)_vp_global_free(b->psy_g_look);
363 vorbis_bitrate_clear(&b->bms);
365 drft_clear(&b->fft_look[0]);
366 drft_clear(&b->fft_look[1]);
372 for(i=0;i<vi->channels;i++)
373 if(v->pcm[i])_ogg_free(v->pcm[i]);
375 if(v->pcmret)_ogg_free(v->pcmret);
379 /* free header, header1, header2 */
380 if(b->header)_ogg_free(b->header);
381 if(b->header1)_ogg_free(b->header1);
382 if(b->header2)_ogg_free(b->header2);
386 memset(v,0,sizeof(*v));
390 float **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
392 vorbis_info *vi=v->vi;
393 private_state *b=v->backend_state;
395 /* free header, header1, header2 */
396 if(b->header)_ogg_free(b->header);b->header=NULL;
397 if(b->header1)_ogg_free(b->header1);b->header1=NULL;
398 if(b->header2)_ogg_free(b->header2);b->header2=NULL;
400 /* Do we have enough storage space for the requested buffer? If not,
401 expand the PCM (and envelope) storage */
403 if(v->pcm_current+vals>=v->pcm_storage){
404 v->pcm_storage=v->pcm_current+vals*2;
406 for(i=0;i<vi->channels;i++){
407 v->pcm[i]=_ogg_realloc(v->pcm[i],v->pcm_storage*sizeof(*v->pcm[i]));
411 for(i=0;i<vi->channels;i++)
412 v->pcmret[i]=v->pcm[i]+v->pcm_current;
417 static void _preextrapolate_helper(vorbis_dsp_state *v){
420 float *lpc=alloca(order*sizeof(*lpc));
421 float *work=alloca(v->pcm_current*sizeof(*work));
425 if(v->pcm_current-v->centerW>order*2){ /* safety */
426 for(i=0;i<v->vi->channels;i++){
427 /* need to run the extrapolation in reverse! */
428 for(j=0;j<v->pcm_current;j++)
429 work[j]=v->pcm[i][v->pcm_current-j-1];
432 vorbis_lpc_from_data(work,lpc,v->pcm_current-v->centerW,order);
435 if(v->vi->channels==2){
437 _analysis_output("predataL",0,work,v->pcm_current-v->centerW,0,0,0);
439 _analysis_output("predataR",0,work,v->pcm_current-v->centerW,0,0,0);
441 _analysis_output("predata",0,work,v->pcm_current-v->centerW,0,0,0);
445 /* run the predictor filter */
446 vorbis_lpc_predict(lpc,work+v->pcm_current-v->centerW-order,
448 work+v->pcm_current-v->centerW,
451 for(j=0;j<v->pcm_current;j++)
452 v->pcm[i][v->pcm_current-j-1]=work[j];
459 /* call with val<=0 to set eof */
461 int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
462 vorbis_info *vi=v->vi;
463 codec_setup_info *ci=vi->codec_setup;
468 float *lpc=alloca(order*sizeof(*lpc));
470 /* if it wasn't done earlier (very short sample) */
471 if(!v->preextrapolate)
472 _preextrapolate_helper(v);
474 /* We're encoding the end of the stream. Just make sure we have
475 [at least] a few full blocks of zeroes at the end. */
476 /* actually, we don't want zeroes; that could drop a large
477 amplitude off a cliff, creating spread spectrum noise that will
478 suck to encode. Extrapolate for the sake of cleanliness. */
480 vorbis_analysis_buffer(v,ci->blocksizes[1]*3);
481 v->eofflag=v->pcm_current;
482 v->pcm_current+=ci->blocksizes[1]*3;
484 for(i=0;i<vi->channels;i++){
485 if(v->eofflag>order*2){
486 /* extrapolate with LPC to fill in */
489 /* make a predictor filter */
491 if(n>ci->blocksizes[1])n=ci->blocksizes[1];
492 vorbis_lpc_from_data(v->pcm[i]+v->eofflag-n,lpc,n,order);
494 /* run the predictor filter */
495 vorbis_lpc_predict(lpc,v->pcm[i]+v->eofflag-order,order,
496 v->pcm[i]+v->eofflag,v->pcm_current-v->eofflag);
498 /* not enough data to extrapolate (unlikely to happen due to
499 guarding the overlap, but bulletproof in case that
500 assumtion goes away). zeroes will do. */
501 memset(v->pcm[i]+v->eofflag,0,
502 (v->pcm_current-v->eofflag)*sizeof(*v->pcm[i]));
508 if(v->pcm_current+vals>v->pcm_storage)
511 v->pcm_current+=vals;
513 /* we may want to reverse extrapolate the beginning of a stream
514 too... in case we're beginning on a cliff! */
515 /* clumsy, but simple. It only runs once, so simple is good. */
516 if(!v->preextrapolate && v->pcm_current-v->centerW>ci->blocksizes[1])
517 _preextrapolate_helper(v);
523 /* do the deltas, envelope shaping, pre-echo and determine the size of
524 the next block on which to continue analysis */
525 int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
527 vorbis_info *vi=v->vi;
528 codec_setup_info *ci=vi->codec_setup;
529 private_state *b=v->backend_state;
530 vorbis_look_psy_global *g=b->psy_g_look;
531 long beginW=v->centerW-ci->blocksizes[v->W]/2,centerNext;
532 vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
534 /* check to see if we're started... */
535 if(!v->preextrapolate)return(0);
537 /* check to see if we're done... */
538 if(v->eofflag==-1)return(0);
540 /* By our invariant, we have lW, W and centerW set. Search for
541 the next boundary so we can determine nW (the next window size)
542 which lets us compute the shape of the current block's window */
544 /* we do an envelope search even on a single blocksize; we may still
545 be throwing more bits at impulses, and envelope search handles
546 marking impulses too. */
548 long bp=_ve_envelope_search(v);
551 if(v->eofflag==0)return(0); /* not enough data currently to search for a
556 if(ci->blocksizes[0]==ci->blocksizes[1])
563 centerNext=v->centerW+ci->blocksizes[v->W]/4+ci->blocksizes[v->nW]/4;
566 /* center of next block + next block maximum right side. */
568 long blockbound=centerNext+ci->blocksizes[v->nW]/2;
569 if(v->pcm_current<blockbound)return(0); /* not enough data yet;
570 although this check is
573 the search is not run
580 /* fill in the block. Note that for a short window, lW and nW are *short*
581 regardless of actual settings in the stream */
583 _vorbis_block_ripcord(vb);
589 if(!v->lW || !v->nW){
590 vbi->blocktype=BLOCKTYPE_TRANSITION;
591 /*fprintf(stderr,"-");*/
593 vbi->blocktype=BLOCKTYPE_LONG;
594 /*fprintf(stderr,"_");*/
597 if(_ve_envelope_mark(v)){
598 vbi->blocktype=BLOCKTYPE_IMPULSE;
599 /*fprintf(stderr,"|");*/
602 vbi->blocktype=BLOCKTYPE_PADDING;
603 /*fprintf(stderr,".");*/
609 vb->sequence=v->sequence++;
610 vb->granulepos=v->granulepos;
611 vb->pcmend=ci->blocksizes[v->W];
613 /* copy the vectors; this uses the local storage in vb */
615 /* this tracks 'strongest peak' for later psychoacoustics */
616 /* moved to the global psy state; clean this mess up */
617 if(vbi->ampmax>g->ampmax)g->ampmax=vbi->ampmax;
618 g->ampmax=_vp_ampmax_decay(g->ampmax,v);
619 vbi->ampmax=g->ampmax;
621 vb->pcm=_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels);
622 vbi->pcmdelay=_vorbis_block_alloc(vb,sizeof(*vbi->pcmdelay)*vi->channels);
623 for(i=0;i<vi->channels;i++){
625 _vorbis_block_alloc(vb,(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
626 memcpy(vbi->pcmdelay[i],v->pcm[i],(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
627 vb->pcm[i]=vbi->pcmdelay[i]+beginW;
629 /* before we added the delay
630 vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i]));
631 memcpy(vb->pcm[i],v->pcm[i]+beginW,ci->blocksizes[v->W]*sizeof(*vb->pcm[i]));
636 /* handle eof detection: eof==0 means that we've not yet received EOF
637 eof>0 marks the last 'real' sample in pcm[]
638 eof<0 'no more to do'; doesn't get here */
641 if(v->centerW>=v->eofflag){
648 /* advance storage vectors and clean up */
650 int new_centerNext=ci->blocksizes[1]/2;
651 int movementW=centerNext-new_centerNext;
655 _ve_envelope_shift(b->ve,movementW);
656 v->pcm_current-=movementW;
658 for(i=0;i<vi->channels;i++)
659 memmove(v->pcm[i],v->pcm[i]+movementW,
660 v->pcm_current*sizeof(*v->pcm[i]));
665 v->centerW=new_centerNext;
668 v->eofflag-=movementW;
669 if(v->eofflag<=0)v->eofflag=-1;
670 /* do not add padding to end of stream! */
671 if(v->centerW>=v->eofflag){
672 v->granulepos+=movementW-(v->centerW-v->eofflag);
674 v->granulepos+=movementW;
677 v->granulepos+=movementW;
686 int vorbis_synthesis_restart(vorbis_dsp_state *v){
687 vorbis_info *vi=v->vi;
688 codec_setup_info *ci;
691 if(!v->backend_state)return -1;
695 hs=ci->halfrate_flag;
697 v->centerW=ci->blocksizes[1]>>(hs+1);
698 v->pcm_current=v->centerW>>hs;
704 ((private_state *)(v->backend_state))->sample_count=-1;
709 int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
710 if(_vds_shared_init(v,vi,0)){
714 vorbis_synthesis_restart(v);
718 /* Unlike in analysis, the window is only partially applied for each
719 block. The time domain envelope is not yet handled at the point of
720 calling (as it relies on the previous block). */
722 int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
723 vorbis_info *vi=v->vi;
724 codec_setup_info *ci=vi->codec_setup;
725 private_state *b=v->backend_state;
726 int hs=ci->halfrate_flag;
729 if(!vb)return(OV_EINVAL);
730 if(v->pcm_current>v->pcm_returned && v->pcm_returned!=-1)return(OV_EINVAL);
736 if((v->sequence==-1)||
737 (v->sequence+1 != vb->sequence)){
738 v->granulepos=-1; /* out of sequence; lose count */
742 v->sequence=vb->sequence;
744 if(vb->pcm){ /* no pcm to process if vorbis_synthesis_trackonly
745 was called on block */
746 int n=ci->blocksizes[v->W]>>(hs+1);
747 int n0=ci->blocksizes[0]>>(hs+1);
748 int n1=ci->blocksizes[1]>>(hs+1);
753 v->glue_bits+=vb->glue_bits;
754 v->time_bits+=vb->time_bits;
755 v->floor_bits+=vb->floor_bits;
756 v->res_bits+=vb->res_bits;
766 /* v->pcm is now used like a two-stage double buffer. We don't want
767 to have to constantly shift *or* adjust memory usage. Don't
768 accept a new block until the old is shifted out */
770 for(j=0;j<vi->channels;j++){
771 /* the overlap/add section */
775 const float *w=_vorbis_window_get(b->window[1]-hs);
776 float *pcm=v->pcm[j]+prevCenter;
779 pcm[i]=pcm[i]*w[n1-i-1] + p[i]*w[i];
782 const float *w=_vorbis_window_get(b->window[0]-hs);
783 float *pcm=v->pcm[j]+prevCenter+n1/2-n0/2;
786 pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
791 const float *w=_vorbis_window_get(b->window[0]-hs);
792 float *pcm=v->pcm[j]+prevCenter;
793 float *p=vb->pcm[j]+n1/2-n0/2;
795 pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
796 for(;i<n1/2+n0/2;i++)
800 const float *w=_vorbis_window_get(b->window[0]-hs);
801 float *pcm=v->pcm[j]+prevCenter;
804 pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
808 /* the copy section */
810 float *pcm=v->pcm[j]+thisCenter;
811 float *p=vb->pcm[j]+n;
822 /* deal with initial packet state; we do this using the explicit
823 pcm_returned==-1 flag otherwise we're sensitive to first block
824 being short or long */
826 if(v->pcm_returned==-1){
827 v->pcm_returned=thisCenter;
828 v->pcm_current=thisCenter;
830 v->pcm_returned=prevCenter;
831 v->pcm_current=prevCenter+
832 ((ci->blocksizes[v->lW]/4+
833 ci->blocksizes[v->W]/4)>>hs);
838 /* track the frame number... This is for convenience, but also
839 making sure our last packet doesn't end with added padding. If
840 the last packet is partial, the number of samples we'll have to
841 return will be past the vb->granulepos.
843 This is not foolproof! It will be confused if we begin
844 decoding at the last page after a seek or hole. In that case,
845 we don't have a starting point to judge where the last frame
846 is. For this reason, vorbisfile will always try to make sure
847 it reads the last two marked pages in proper sequence */
849 if(b->sample_count==-1){
852 b->sample_count+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4;
855 if(v->granulepos==-1){
856 if(vb->granulepos!=-1){ /* only set if we have a position to set to */
858 v->granulepos=vb->granulepos;
860 /* is this a short page? */
861 if(b->sample_count>v->granulepos){
862 /* corner case; if this is both the first and last audio page,
863 then spec says the end is cut, not beginning */
864 long extra=b->sample_count-vb->granulepos;
866 /* we use ogg_int64_t for granule positions because a
867 uint64 isn't universally available. Unfortunately,
868 that means granposes can be 'negative' and result in
869 extra being negative */
875 /* no preceding granulepos; assume we started at zero (we'd
876 have to in a short single-page stream) */
877 /* granulepos could be -1 due to a seek, but that would result
878 in a long count, not short count */
880 /* Guard against corrupt/malicious frames that set EOP and
881 a backdated granpos; don't rewind more samples than we
883 if(extra > (v->pcm_current - v->pcm_returned)<<hs)
884 extra = (v->pcm_current - v->pcm_returned)<<hs;
886 v->pcm_current-=extra>>hs;
888 /* trim the beginning */
889 v->pcm_returned+=extra>>hs;
890 if(v->pcm_returned>v->pcm_current)
891 v->pcm_returned=v->pcm_current;
898 v->granulepos+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4;
899 if(vb->granulepos!=-1 && v->granulepos!=vb->granulepos){
901 if(v->granulepos>vb->granulepos){
902 long extra=v->granulepos-vb->granulepos;
906 /* partial last frame. Strip the extra samples off */
908 /* Guard against corrupt/malicious frames that set EOP and
909 a backdated granpos; don't rewind more samples than we
911 if(extra > (v->pcm_current - v->pcm_returned)<<hs)
912 extra = (v->pcm_current - v->pcm_returned)<<hs;
914 /* we use ogg_int64_t for granule positions because a
915 uint64 isn't universally available. Unfortunately,
916 that means granposes can be 'negative' and result in
917 extra being negative */
921 v->pcm_current-=extra>>hs;
922 } /* else {Shouldn't happen *unless* the bitstream is out of
923 spec. Either way, believe the bitstream } */
924 } /* else {Shouldn't happen *unless* the bitstream is out of
925 spec. Either way, believe the bitstream } */
926 v->granulepos=vb->granulepos;
930 /* Update, cleanup */
932 if(vb->eofflag)v->eofflag=1;
937 /* pcm==NULL indicates we just want the pending samples, no more */
938 int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm){
939 vorbis_info *vi=v->vi;
941 if(v->pcm_returned>-1 && v->pcm_returned<v->pcm_current){
944 for(i=0;i<vi->channels;i++)
945 v->pcmret[i]=v->pcm[i]+v->pcm_returned;
948 return(v->pcm_current-v->pcm_returned);
953 int vorbis_synthesis_read(vorbis_dsp_state *v,int n){
954 if(n && v->pcm_returned+n>v->pcm_current)return(OV_EINVAL);
959 /* intended for use with a specific vorbisfile feature; we want access
960 to the [usually synthetic/postextrapolated] buffer and lapping at
961 the end of a decode cycle, specifically, a half-short-block worth.
962 This funtion works like pcmout above, except it will also expose
963 this implicit buffer data not normally decoded. */
964 int vorbis_synthesis_lapout(vorbis_dsp_state *v,float ***pcm){
965 vorbis_info *vi=v->vi;
966 codec_setup_info *ci=vi->codec_setup;
967 int hs=ci->halfrate_flag;
969 int n=ci->blocksizes[v->W]>>(hs+1);
970 int n0=ci->blocksizes[0]>>(hs+1);
971 int n1=ci->blocksizes[1]>>(hs+1);
974 if(v->pcm_returned<0)return 0;
976 /* our returned data ends at pcm_returned; because the synthesis pcm
977 buffer is a two-fragment ring, that means our data block may be
978 fragmented by buffering, wrapping or a short block not filling
979 out a buffer. To simplify things, we unfragment if it's at all
980 possibly needed. Otherwise, we'd need to call lapout more than
981 once as well as hold additional dsp state. Opt for
984 /* centerW was advanced by blockin; it would be the center of the
987 /* the data buffer wraps; swap the halves */
988 /* slow, sure, small */
989 for(j=0;j<vi->channels;j++){
1003 /* solidify buffer into contiguous space */
1004 if((v->lW^v->W)==1){
1005 /* long/short or short/long */
1006 for(j=0;j<vi->channels;j++){
1008 float *d=v->pcm[j]+(n1-n0)/2;
1009 for(i=(n1+n0)/2-1;i>=0;--i)
1012 v->pcm_returned+=(n1-n0)/2;
1013 v->pcm_current+=(n1-n0)/2;
1017 for(j=0;j<vi->channels;j++){
1019 float *d=v->pcm[j]+n1-n0;
1020 for(i=n0-1;i>=0;--i)
1023 v->pcm_returned+=n1-n0;
1024 v->pcm_current+=n1-n0;
1030 for(i=0;i<vi->channels;i++)
1031 v->pcmret[i]=v->pcm[i]+v->pcm_returned;
1035 return(n1+n-v->pcm_returned);
1039 const float *vorbis_window(vorbis_dsp_state *v,int W){
1040 vorbis_info *vi=v->vi;
1041 codec_setup_info *ci=vi->codec_setup;
1042 int hs=ci->halfrate_flag;
1043 private_state *b=v->backend_state;
1045 if(b->window[W]-1<0)return NULL;
1046 return _vorbis_window_get(b->window[W]-hs);