New preecho detection/short block trigger code, replacing an IIR
[platform/upstream/libvorbis.git] / lib / block.c
1 /********************************************************************
2  *                                                                  *
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.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13  function: PCM data vector blocking, windowing and dis/reassembly
14  last mod: $Id: block.c,v 1.60 2002/03/17 19:50:47 xiphmont Exp $
15
16  Handle windowing, overlap-add, etc of the PCM vectors.  This is made
17  more amusing by Vorbis' current two allowed block sizes.
18  
19  ********************************************************************/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ogg/ogg.h>
25 #include "vorbis/codec.h"
26 #include "codec_internal.h"
27
28 #include "window.h"
29 #include "mdct.h"
30 #include "lpc.h"
31 #include "registry.h"
32 #include "misc.h"
33
34 static int ilog2(unsigned int v){
35   int ret=0;
36   while(v>1){
37     ret++;
38     v>>=1;
39   }
40   return(ret);
41 }
42
43 /* pcm accumulator examples (not exhaustive):
44
45  <-------------- lW ---------------->
46                    <--------------- W ---------------->
47 :            .....|.....       _______________         |
48 :        .'''     |     '''_---      |       |\        |
49 :.....'''         |_____--- '''......|       | \_______|
50 :.................|__________________|_______|__|______|
51                   |<------ Sl ------>|      > Sr <     |endW
52                   |beginSl           |endSl  |  |endSr   
53                   |beginW            |endlW  |beginSr
54
55
56                       |< lW >|       
57                    <--------------- W ---------------->
58                   |   |  ..  ______________            |
59                   |   | '  `/        |     ---_        |
60                   |___.'___/`.       |         ---_____| 
61                   |_______|__|_______|_________________|
62                   |      >|Sl|<      |<------ Sr ----->|endW
63                   |       |  |endSl  |beginSr          |endSr
64                   |beginW |  |endlW                     
65                   mult[0] |beginSl                     mult[n]
66
67  <-------------- lW ----------------->
68                           |<--W-->|                               
69 :            ..............  ___  |   |                    
70 :        .'''             |`/   \ |   |                       
71 :.....'''                 |/`....\|...|                    
72 :.........................|___|___|___|                  
73                           |Sl |Sr |endW    
74                           |   |   |endSr
75                           |   |beginSr
76                           |   |endSl
77                           |beginSl
78                           |beginW
79 */
80
81 /* block abstraction setup *********************************************/
82
83 #ifndef WORD_ALIGN
84 #define WORD_ALIGN 8
85 #endif
86
87 int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
88   memset(vb,0,sizeof(*vb));
89   vb->vd=v;
90   vb->localalloc=0;
91   vb->localstore=NULL;
92   if(v->analysisp){
93     vorbis_block_internal *vbi=
94       vb->internal=_ogg_calloc(1,sizeof(vorbis_block_internal));
95     oggpack_writeinit(&vb->opb);
96     vbi->ampmax=-9999;
97     vbi->packet_markers=_ogg_malloc(vorbis_bitrate_maxmarkers()*
98                                sizeof(*vbi->packet_markers));
99   }
100   
101   return(0);
102 }
103
104 void *_vorbis_block_alloc(vorbis_block *vb,long bytes){
105   bytes=(bytes+(WORD_ALIGN-1)) & ~(WORD_ALIGN-1);
106   if(bytes+vb->localtop>vb->localalloc){
107     /* can't just _ogg_realloc... there are outstanding pointers */
108     if(vb->localstore){
109       struct alloc_chain *link=_ogg_malloc(sizeof(*link));
110       vb->totaluse+=vb->localtop;
111       link->next=vb->reap;
112       link->ptr=vb->localstore;
113       vb->reap=link;
114     }
115     /* highly conservative */
116     vb->localalloc=bytes;
117     vb->localstore=_ogg_malloc(vb->localalloc);
118     vb->localtop=0;
119   }
120   {
121     void *ret=(void *)(((char *)vb->localstore)+vb->localtop);
122     vb->localtop+=bytes;
123     return ret;
124   }
125 }
126
127 /* reap the chain, pull the ripcord */
128 void _vorbis_block_ripcord(vorbis_block *vb){
129   /* reap the chain */
130   struct alloc_chain *reap=vb->reap;
131   while(reap){
132     struct alloc_chain *next=reap->next;
133     _ogg_free(reap->ptr);
134     memset(reap,0,sizeof(*reap));
135     _ogg_free(reap);
136     reap=next;
137   }
138   /* consolidate storage */
139   if(vb->totaluse){
140     vb->localstore=_ogg_realloc(vb->localstore,vb->totaluse+vb->localalloc);
141     vb->localalloc+=vb->totaluse;
142     vb->totaluse=0;
143   }
144
145   /* pull the ripcord */
146   vb->localtop=0;
147   vb->reap=NULL;
148 }
149
150 int vorbis_block_clear(vorbis_block *vb){
151   if(vb->vd)
152     if(vb->vd->analysisp)
153       oggpack_writeclear(&vb->opb);
154   _vorbis_block_ripcord(vb);
155   if(vb->localstore)_ogg_free(vb->localstore);
156
157   if(vb->internal){
158     vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
159     if(vbi->packet_markers)_ogg_free(vbi->packet_markers);
160
161     _ogg_free(vb->internal);
162   }
163
164   memset(vb,0,sizeof(*vb));
165   return(0);
166 }
167
168 /* Analysis side code, but directly related to blocking.  Thus it's
169    here and not in analysis.c (which is for analysis transforms only).
170    The init is here because some of it is shared */
171
172 static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){
173   int i;
174   codec_setup_info *ci=vi->codec_setup;
175   backend_lookup_state *b=NULL;
176
177   memset(v,0,sizeof(*v));
178   b=v->backend_state=_ogg_calloc(1,sizeof(*b));
179
180   v->vi=vi;
181   b->modebits=ilog2(ci->modes);
182
183   b->transform[0]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[0]));
184   b->transform[1]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[1]));
185
186   /* MDCT is tranform 0 */
187
188   b->transform[0][0]=_ogg_calloc(1,sizeof(mdct_lookup));
189   b->transform[1][0]=_ogg_calloc(1,sizeof(mdct_lookup));
190   mdct_init(b->transform[0][0],ci->blocksizes[0]);
191   mdct_init(b->transform[1][0],ci->blocksizes[1]);
192
193   /* Vorbis I uses only window type 0 */
194   b->window[0]=_vorbis_window(0,ci->blocksizes[0]/2);
195   b->window[1]=_vorbis_window(0,ci->blocksizes[1]/2);
196
197   if(encp){ /* encode/decode differ here */
198     /* finish the codebooks */
199     if(!ci->fullbooks){
200       ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks));
201       for(i=0;i<ci->books;i++)
202         vorbis_book_init_encode(ci->fullbooks+i,ci->book_param[i]);
203     }
204     v->analysisp=1;
205   }else{
206     /* finish the codebooks */
207     if(!ci->fullbooks){
208       ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks));
209       for(i=0;i<ci->books;i++){
210         vorbis_book_init_decode(ci->fullbooks+i,ci->book_param[i]);
211         /* decode codebooks are now standalone after init */
212         vorbis_staticbook_destroy(ci->book_param[i]);
213         ci->book_param[i]=NULL;
214       }
215     }
216   }
217
218   /* initialize the storage vectors. blocksize[1] is small for encode,
219      but the correct size for decode */
220   v->pcm_storage=ci->blocksizes[1];
221   v->pcm=_ogg_malloc(vi->channels*sizeof(*v->pcm));
222   v->pcmret=_ogg_malloc(vi->channels*sizeof(*v->pcmret));
223   {
224     int i;
225     for(i=0;i<vi->channels;i++)
226       v->pcm[i]=_ogg_calloc(v->pcm_storage,sizeof(*v->pcm[i]));
227   }
228
229   /* all 1 (large block) or 0 (small block) */
230   /* explicitly set for the sake of clarity */
231   v->lW=0; /* previous window size */
232   v->W=0;  /* current window size */
233
234   /* all vector indexes */
235   v->centerW=ci->blocksizes[1]/2;
236
237   v->pcm_current=v->centerW;
238
239   /* initialize all the mapping/backend lookups */
240   b->mode=_ogg_calloc(ci->modes,sizeof(*b->mode));
241   for(i=0;i<ci->modes;i++){
242     int mapnum=ci->mode_param[i]->mapping;
243     int maptype=ci->map_type[mapnum];
244     b->mode[i]=_mapping_P[maptype]->look(v,ci->mode_param[i],
245                                          ci->map_param[mapnum]);
246   }
247
248   return(0);
249 }
250
251 /* arbitrary settings and spec-mandated numbers get filled in here */
252 int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
253   backend_lookup_state *b=NULL;
254
255   _vds_shared_init(v,vi,1);
256   b=v->backend_state;
257   b->psy_g_look=_vp_global_look(vi);
258
259   /* Initialize the envelope state storage */
260   b->ve=_ogg_calloc(1,sizeof(*b->ve));
261   _ve_envelope_init(b->ve,vi);
262
263   vorbis_bitrate_init(vi,&b->bms);
264
265   return(0);
266 }
267
268 void vorbis_dsp_clear(vorbis_dsp_state *v){
269   int i;
270   if(v){
271     vorbis_info *vi=v->vi;
272     codec_setup_info *ci=(vi?vi->codec_setup:NULL);
273     backend_lookup_state *b=v->backend_state;
274
275     if(b){
276       if(b->window[0])
277         _ogg_free(b->window[0]);
278       if(b->window[1])
279         _ogg_free(b->window[1]);
280         
281       if(b->ve){
282         _ve_envelope_clear(b->ve);
283         _ogg_free(b->ve);
284       }
285
286       if(b->transform[0]){
287         mdct_clear(b->transform[0][0]);
288         _ogg_free(b->transform[0][0]);
289         _ogg_free(b->transform[0]);
290       }
291       if(b->transform[1]){
292         mdct_clear(b->transform[1][0]);
293         _ogg_free(b->transform[1][0]);
294         _ogg_free(b->transform[1]);
295       }
296       if(b->psy_g_look)_vp_global_free(b->psy_g_look);
297       vorbis_bitrate_clear(&b->bms);
298     }
299     
300     if(v->pcm){
301       for(i=0;i<vi->channels;i++)
302         if(v->pcm[i])_ogg_free(v->pcm[i]);
303       _ogg_free(v->pcm);
304       if(v->pcmret)_ogg_free(v->pcmret);
305     }
306
307     /* free mode lookups; these are actually vorbis_look_mapping structs */
308     if(ci){
309       for(i=0;i<ci->modes;i++){
310         int mapnum=ci->mode_param[i]->mapping;
311         int maptype=ci->map_type[mapnum];
312         if(b && b->mode)_mapping_P[maptype]->free_look(b->mode[i]);
313       }
314     }
315
316     if(b){
317       if(b->mode)_ogg_free(b->mode);    
318       
319       /* free header, header1, header2 */
320       if(b->header)_ogg_free(b->header);
321       if(b->header1)_ogg_free(b->header1);
322       if(b->header2)_ogg_free(b->header2);
323       _ogg_free(b);
324     }
325     
326     memset(v,0,sizeof(*v));
327   }
328 }
329
330 float **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
331   int i;
332   vorbis_info *vi=v->vi;
333   backend_lookup_state *b=v->backend_state;
334
335   /* free header, header1, header2 */
336   if(b->header)_ogg_free(b->header);b->header=NULL;
337   if(b->header1)_ogg_free(b->header1);b->header1=NULL;
338   if(b->header2)_ogg_free(b->header2);b->header2=NULL;
339
340   /* Do we have enough storage space for the requested buffer? If not,
341      expand the PCM (and envelope) storage */
342     
343   if(v->pcm_current+vals>=v->pcm_storage){
344     v->pcm_storage=v->pcm_current+vals*2;
345    
346     for(i=0;i<vi->channels;i++){
347       v->pcm[i]=_ogg_realloc(v->pcm[i],v->pcm_storage*sizeof(*v->pcm[i]));
348     }
349   }
350
351   for(i=0;i<vi->channels;i++)
352     v->pcmret[i]=v->pcm[i]+v->pcm_current;
353     
354   return(v->pcmret);
355 }
356
357 static void _preextrapolate_helper(vorbis_dsp_state *v){
358   int i;
359   int order=32;
360   float *lpc=alloca(order*sizeof(*lpc));
361   float *work=alloca(v->pcm_current*sizeof(*work));
362   long j;
363   v->preextrapolate=1;
364
365   if(v->pcm_current-v->centerW>order*2){ /* safety */
366     for(i=0;i<v->vi->channels;i++){
367       /* need to run the extrapolation in reverse! */
368       for(j=0;j<v->pcm_current;j++)
369         work[j]=v->pcm[i][v->pcm_current-j-1];
370       
371       /* prime as above */
372       vorbis_lpc_from_data(work,lpc,v->pcm_current-v->centerW,order);
373       
374       /* run the predictor filter */
375       vorbis_lpc_predict(lpc,work+v->pcm_current-v->centerW-order,
376                          order,
377                          work+v->pcm_current-v->centerW,
378                          v->centerW);
379
380       for(j=0;j<v->pcm_current;j++)
381         v->pcm[i][v->pcm_current-j-1]=work[j];
382
383     }
384   }
385 }
386
387
388 /* call with val<=0 to set eof */
389
390 int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
391   vorbis_info *vi=v->vi;
392   codec_setup_info *ci=vi->codec_setup;
393   /*backend_lookup_state *b=v->backend_state;*/
394
395   if(vals<=0){
396     int order=32;
397     int i;
398     float *lpc=alloca(order*sizeof(*lpc));
399
400     /* if it wasn't done earlier (very short sample) */
401     if(!v->preextrapolate)
402       _preextrapolate_helper(v);
403
404     /* We're encoding the end of the stream.  Just make sure we have
405        [at least] a full block of zeroes at the end. */
406     /* actually, we don't want zeroes; that could drop a large
407        amplitude off a cliff, creating spread spectrum noise that will
408        suck to encode.  Extrapolate for the sake of cleanliness. */
409
410     vorbis_analysis_buffer(v,ci->blocksizes[1]*2);
411     v->eofflag=v->pcm_current;
412     v->pcm_current+=ci->blocksizes[1]*2;
413
414     for(i=0;i<vi->channels;i++){
415       if(v->eofflag>order*2){
416         /* extrapolate with LPC to fill in */
417         long n;
418
419         /* make a predictor filter */
420         n=v->eofflag;
421         if(n>ci->blocksizes[1])n=ci->blocksizes[1];
422         vorbis_lpc_from_data(v->pcm[i]+v->eofflag-n,lpc,n,order);
423
424         /* run the predictor filter */
425         vorbis_lpc_predict(lpc,v->pcm[i]+v->eofflag-order,order,
426                            v->pcm[i]+v->eofflag,v->pcm_current-v->eofflag);
427       }else{
428         /* not enough data to extrapolate (unlikely to happen due to
429            guarding the overlap, but bulletproof in case that
430            assumtion goes away). zeroes will do. */
431         memset(v->pcm[i]+v->eofflag,0,
432                (v->pcm_current-v->eofflag)*sizeof(*v->pcm[i]));
433
434       }
435     }
436   }else{
437
438     if(v->pcm_current+vals>v->pcm_storage)
439       return(OV_EINVAL);
440
441     v->pcm_current+=vals;
442
443     /* we may want to reverse extrapolate the beginning of a stream
444        too... in case we're beginning on a cliff! */
445     /* clumsy, but simple.  It only runs once, so simple is good. */
446     if(!v->preextrapolate && v->pcm_current-v->centerW>ci->blocksizes[1])
447       _preextrapolate_helper(v);
448
449   }
450   return(0);
451 }
452
453 /* do the deltas, envelope shaping, pre-echo and determine the size of
454    the next block on which to continue analysis */
455 int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
456   int i;
457   vorbis_info *vi=v->vi;
458   codec_setup_info *ci=vi->codec_setup;
459   backend_lookup_state *b=v->backend_state;
460   vorbis_look_psy_global *g=b->psy_g_look;
461   vorbis_info_psy_global *gi=&ci->psy_g_param;
462   long beginW=v->centerW-ci->blocksizes[v->W]/2,centerNext;
463   vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
464
465   /* check to see if we're started... */
466   if(!v->preextrapolate)return(0);
467
468   /* check to see if we're done... */
469   if(v->eofflag==-1)return(0);
470
471   /* By our invariant, we have lW, W and centerW set.  Search for
472      the next boundary so we can determine nW (the next window size)
473      which lets us compute the shape of the current block's window */
474   
475   if(ci->blocksizes[0]<ci->blocksizes[1]){
476     long bp=_ve_envelope_search(v);
477     if(bp==-1)return(0); /* not enough data currently to search for a
478                             full long block */
479     v->nW=bp;
480
481   }else
482     v->nW=0;
483   
484   centerNext=v->centerW+ci->blocksizes[v->W]/4+ci->blocksizes[v->nW]/4;
485
486   {
487     /* center of next block + next block maximum right side. */
488
489     long blockbound=centerNext+ci->blocksizes[v->nW]/2;
490     if(v->pcm_current<blockbound)return(0); /* not enough data yet;
491                                                although this check is
492                                                less strict that the
493                                                _ve_envelope_search,
494                                                the search is not run
495                                                if we only use one
496                                                block size */
497
498
499   }
500   
501   /* fill in the block.  Note that for a short window, lW and nW are *short*
502      regardless of actual settings in the stream */
503
504   _vorbis_block_ripcord(vb);
505   if(v->W){
506     vb->lW=v->lW;
507     vb->W=v->W;
508     vb->nW=v->nW;
509   }else{
510     vb->lW=0;
511     vb->W=v->W;
512     vb->nW=0;
513   }
514
515   if(v->W){
516     if(!v->lW || !v->nW){
517       vbi->blocktype=BLOCKTYPE_TRANSITION;
518       /*fprintf(stderr,"-");*/
519     }else{
520       vbi->blocktype=BLOCKTYPE_LONG;
521       /*fprintf(stderr,"_");*/
522     }
523   }else{
524     if(_ve_envelope_mark(v)){
525       vbi->blocktype=BLOCKTYPE_IMPULSE;
526       /*fprintf(stderr,"|");*/
527
528     }else{
529       vbi->blocktype=BLOCKTYPE_PADDING;
530       /*fprintf(stderr,".");*/
531
532     }
533   }
534  
535   vb->vd=v;
536   vb->sequence=v->sequence++;
537   vb->granulepos=v->granulepos;
538   vb->pcmend=ci->blocksizes[v->W];
539   
540   /* copy the vectors; this uses the local storage in vb */
541
542   /* this tracks 'strongest peak' for later psychoacoustics */
543   /* moved to the global psy state; clean this mess up */
544   if(vbi->ampmax>g->ampmax)g->ampmax=vbi->ampmax;
545   g->ampmax=_vp_ampmax_decay(g->ampmax,v);
546   vbi->ampmax=g->ampmax;
547   
548   vb->pcm=_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels);
549   vbi->pcmdelay=_vorbis_block_alloc(vb,sizeof(*vbi->pcmdelay)*vi->channels);
550   for(i=0;i<vi->channels;i++){
551     vbi->pcmdelay[i]=
552       _vorbis_block_alloc(vb,(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
553     memcpy(vbi->pcmdelay[i],v->pcm[i],(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
554     vb->pcm[i]=vbi->pcmdelay[i]+beginW;
555     
556     /* before we added the delay 
557        vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i]));
558        memcpy(vb->pcm[i],v->pcm[i]+beginW,ci->blocksizes[v->W]*sizeof(*vb->pcm[i]));
559     */
560     
561   }
562   
563   /* handle eof detection: eof==0 means that we've not yet received EOF
564                            eof>0  marks the last 'real' sample in pcm[]
565                            eof<0  'no more to do'; doesn't get here */
566
567   if(v->eofflag){
568     if(v->centerW>=v->eofflag){
569       v->eofflag=-1;
570       vb->eofflag=1;
571       return(1);
572     }
573   }
574
575   /* advance storage vectors and clean up */
576   {
577     int new_centerNext=ci->blocksizes[1]/2+gi->delaycache;
578     int movementW=centerNext-new_centerNext;
579
580     if(movementW>0){
581
582       _ve_envelope_shift(b->ve,movementW);
583       v->pcm_current-=movementW;
584       
585       for(i=0;i<vi->channels;i++)
586         memmove(v->pcm[i],v->pcm[i]+movementW,
587                 v->pcm_current*sizeof(*v->pcm[i]));
588       
589       
590       v->lW=v->W;
591       v->W=v->nW;
592       v->centerW=new_centerNext;
593       
594       if(v->eofflag){
595         v->eofflag-=movementW;
596         /* do not add padding to end of stream! */
597         if(v->centerW>=v->eofflag){
598           v->granulepos+=movementW-(v->centerW-v->eofflag);
599         }else{
600           v->granulepos+=movementW;
601         }
602       }else{
603         v->granulepos+=movementW;
604       }
605     }
606   }
607
608   /* done */
609   return(1);
610 }
611
612 int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
613   _vds_shared_init(v,vi,0);
614
615   v->pcm_returned=-1;
616   v->granulepos=-1;
617   v->sequence=-1;
618
619   return(0);
620 }
621
622 /* Unlike in analysis, the window is only partially applied for each
623    block.  The time domain envelope is not yet handled at the point of
624    calling (as it relies on the previous block). */
625
626 int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
627   vorbis_info *vi=v->vi;
628   codec_setup_info *ci=vi->codec_setup;
629   int i,j;
630
631   if(!vb)return(OV_EINVAL);
632   if(v->pcm_current>v->pcm_returned  && v->pcm_returned!=-1)return(OV_EINVAL);
633     
634   v->lW=v->W;
635   v->W=vb->W;
636   v->nW=-1;
637   
638   if(v->sequence+1 != vb->sequence)v->granulepos=-1; /* out of sequence;
639                                                         lose count */
640   v->sequence=vb->sequence;
641   
642   if(vb->pcm){  /* not pcm to process if vorbis_synthesis_trackonly 
643                    was called on block */
644     int n=ci->blocksizes[v->W]/2;
645     int n0=ci->blocksizes[0]/2;
646     int n1=ci->blocksizes[1]/2;
647     
648     int thisCenter;
649     int prevCenter;
650     
651     v->glue_bits+=vb->glue_bits;
652     v->time_bits+=vb->time_bits;
653     v->floor_bits+=vb->floor_bits;
654     v->res_bits+=vb->res_bits;
655     
656     if(v->centerW){
657       thisCenter=n1;
658       prevCenter=0;
659     }else{
660         thisCenter=0;
661         prevCenter=n1;
662     }
663     
664     /* v->pcm is now used like a two-stage double buffer.  We don't want
665        to have to constantly shift *or* adjust memory usage.  Don't
666        accept a new block until the old is shifted out */
667     
668     /* overlap/add PCM */
669     
670     for(j=0;j<vi->channels;j++){
671       /* the overlap/add section */
672       if(v->lW){
673         if(v->W){
674           /* large/large */
675           float *pcm=v->pcm[j]+prevCenter;
676           float *p=vb->pcm[j];
677           for(i=0;i<n1;i++)
678             pcm[i]+=p[i];
679         }else{
680           /* large/small */
681           float *pcm=v->pcm[j]+prevCenter+n1/2-n0/2;
682           float *p=vb->pcm[j];
683           for(i=0;i<n0;i++)
684             pcm[i]+=p[i];
685         }
686       }else{
687         if(v->W){
688           /* small/large */
689           float *pcm=v->pcm[j]+prevCenter;
690           float *p=vb->pcm[j]+n1/2-n0/2;
691           for(i=0;i<n0;i++)
692             pcm[i]+=p[i];
693           for(;i<n1/2+n0/2;i++)
694             pcm[i]=p[i];
695         }else{
696           /* small/small */
697           float *pcm=v->pcm[j]+prevCenter;
698           float *p=vb->pcm[j];
699           for(i=0;i<n0;i++)
700             pcm[i]+=p[i];
701         }
702       }
703       
704       /* the copy section */
705       {
706         float *pcm=v->pcm[j]+thisCenter;
707         float *p=vb->pcm[j]+n;
708         for(i=0;i<n;i++)
709           pcm[i]=p[i];
710       }
711     }
712     
713     if(v->centerW)
714       v->centerW=0;
715     else
716       v->centerW=n1;
717     
718     /* deal with initial packet state; we do this using the explicit
719        pcm_returned==-1 flag otherwise we're sensitive to first block
720        being short or long */
721     
722     if(v->pcm_returned==-1){
723       v->pcm_returned=thisCenter;
724       v->pcm_current=thisCenter;
725     }else{
726       v->pcm_returned=prevCenter;
727       v->pcm_current=prevCenter+
728         ci->blocksizes[v->lW]/4+
729         ci->blocksizes[v->W]/4;
730     }
731     
732   }
733
734   /* track the frame number... This is for convenience, but also
735      making sure our last packet doesn't end with added padding.  If
736      the last packet is partial, the number of samples we'll have to
737      return will be past the vb->granulepos.
738      
739      This is not foolproof!  It will be confused if we begin
740      decoding at the last page after a seek or hole.  In that case,
741      we don't have a starting point to judge where the last frame
742      is.  For this reason, vorbisfile will always try to make sure
743      it reads the last two marked pages in proper sequence */
744   
745   if(v->granulepos==-1){
746     if(vb->granulepos!=-1){ /* only set if we have a position to set to */
747       v->granulepos=vb->granulepos;
748     }
749   }else{
750     v->granulepos+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4;
751     if(vb->granulepos!=-1 && v->granulepos!=vb->granulepos){
752       
753       if(v->granulepos>vb->granulepos){
754         long extra=v->granulepos-vb->granulepos;
755         
756         if(vb->eofflag){
757           /* partial last frame.  Strip the extra samples off */
758           v->pcm_current-=extra;
759         }else if(vb->sequence == 1){
760           /* ^^^ argh, this can be 1 from seeking! */
761           
762           
763           /* partial first frame.  Discard extra leading samples */
764           v->pcm_returned+=extra;
765           if(v->pcm_returned>v->pcm_current)
766             v->pcm_returned=v->pcm_current;
767           
768         } /* else {Shouldn't happen *unless* the bitstream is out of
769              spec.  Either way, believe the bitstream } */
770       } /* else {Shouldn't happen *unless* the bitstream is out of
771            spec.  Either way, believe the bitstream } */
772       v->granulepos=vb->granulepos;
773     }
774   }
775   
776   /* Update, cleanup */
777   
778   if(vb->eofflag)v->eofflag=1;
779   return(0);
780   
781 }
782
783 /* pcm==NULL indicates we just want the pending samples, no more */
784 int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm){
785   vorbis_info *vi=v->vi;
786   if(v->pcm_returned>-1 && v->pcm_returned<v->pcm_current){
787     if(pcm){
788       int i;
789       for(i=0;i<vi->channels;i++)
790         v->pcmret[i]=v->pcm[i]+v->pcm_returned;
791       *pcm=v->pcmret;
792     }
793     return(v->pcm_current-v->pcm_returned);
794   }
795   return(0);
796 }
797
798 int vorbis_synthesis_read(vorbis_dsp_state *v,int n){
799   if(n && v->pcm_returned+n>v->pcm_current)return(OV_EINVAL);
800   v->pcm_returned+=n;
801   return(0);
802 }
803