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