fixes to prevent segfaults
[platform/upstream/libvorbis.git] / lib / block.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE.  *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5  * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
6  * PLEASE READ THESE TERMS DISTRIBUTING.                            *
7  *                                                                  *
8  * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
9  * by Monty <monty@xiph.org> and The XIPHOPHORUS Company            *
10  * http://www.xiph.org/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: PCM data vector blocking, windowing and dis/reassembly
15  last mod: $Id: block.c,v 1.27 2000/02/23 11:22:43 xiphmont Exp $
16
17  Handle windowing, overlap-add, etc of the PCM vectors.  This is made
18  more amusing by Vorbis' current two allowed block sizes.
19  
20  Vorbis manipulates the dynamic range of the incoming PCM data
21  envelope to minimise time-domain energy leakage from percussive and
22  plosive waveforms being quantized in the MDCT domain.
23
24  ********************************************************************/
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "vorbis/codec.h"
30
31 #include "window.h"
32 #include "envelope.h"
33 #include "mdct.h"
34 #include "lpc.h"
35 #include "bitwise.h"
36 #include "registry.h"
37 #include "bookinternal.h"
38
39 static int ilog2(unsigned int v){
40   int ret=0;
41   while(v>1){
42     ret++;
43     v>>=1;
44   }
45   return(ret);
46 }
47
48 /* pcm accumulator examples (not exhaustive):
49
50  <-------------- lW ---------------->
51                    <--------------- W ---------------->
52 :            .....|.....       _______________         |
53 :        .'''     |     '''_---      |       |\        |
54 :.....'''         |_____--- '''......|       | \_______|
55 :.................|__________________|_______|__|______|
56                   |<------ Sl ------>|      > Sr <     |endW
57                   |beginSl           |endSl  |  |endSr   
58                   |beginW            |endlW  |beginSr
59
60
61                       |< lW >|       
62                    <--------------- W ---------------->
63                   |   |  ..  ______________            |
64                   |   | '  `/        |     ---_        |
65                   |___.'___/`.       |         ---_____| 
66                   |_______|__|_______|_________________|
67                   |      >|Sl|<      |<------ Sr ----->|endW
68                   |       |  |endSl  |beginSr          |endSr
69                   |beginW |  |endlW                     
70                   mult[0] |beginSl                     mult[n]
71
72  <-------------- lW ----------------->
73                           |<--W-->|                               
74 :            ..............  ___  |   |                    
75 :        .'''             |`/   \ |   |                       
76 :.....'''                 |/`....\|...|                    
77 :.........................|___|___|___|                  
78                           |Sl |Sr |endW    
79                           |   |   |endSr
80                           |   |beginSr
81                           |   |endSl
82                           |beginSl
83                           |beginW
84 */
85
86 /* block abstraction setup *********************************************/
87
88 #ifndef WORD_ALIGN
89 #define WORD_ALIGN 8
90 #endif
91
92 int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
93   memset(vb,0,sizeof(vorbis_block));
94   vb->vd=v;
95   vb->localalloc=0;
96   vb->localstore=NULL;
97   if(v->analysisp)
98     _oggpack_writeinit(&vb->opb);
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 realloc... there are outstanding pointers */
107     if(vb->localstore){
108       struct alloc_chain *link=malloc(sizeof(struct alloc_chain));
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=malloc(vb->localalloc);
117     vb->localtop=0;
118   }
119   {
120     void *ret=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     free(reap->ptr);
133     memset(reap,0,sizeof(struct alloc_chain));
134     free(reap);
135     reap=next;
136   }
137   /* consolidate storage */
138   if(vb->totaluse){
139     vb->localstore=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)free(vb->localstore);
155
156   memset(vb,0,sizeof(vorbis_block));
157   return(0);
158 }
159
160 /* Analysis side code, but directly related to blocking.  Thus it's
161    here and not in analysis.c (which is for analysis transforms only).
162    The init is here because some of it is shared */
163
164 static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){
165   int i;
166   memset(v,0,sizeof(vorbis_dsp_state));
167
168   v->vi=vi;
169   v->modebits=ilog2(vi->modes);
170
171   v->transform[0]=calloc(VI_TRANSFORMB,sizeof(vorbis_look_transform *));
172   v->transform[1]=calloc(VI_TRANSFORMB,sizeof(vorbis_look_transform *));
173
174   /* MDCT is tranform 0 */
175
176   v->transform[0][0]=calloc(1,sizeof(mdct_lookup));
177   v->transform[1][0]=calloc(1,sizeof(mdct_lookup));
178   mdct_init(v->transform[0][0],vi->blocksizes[0]);
179   mdct_init(v->transform[1][0],vi->blocksizes[1]);
180
181   v->window[0][0][0]=calloc(VI_WINDOWB,sizeof(double *));
182   v->window[0][0][1]=v->window[0][0][0];
183   v->window[0][1][0]=v->window[0][0][0];
184   v->window[0][1][1]=v->window[0][0][0];
185   v->window[1][0][0]=calloc(VI_WINDOWB,sizeof(double *));
186   v->window[1][0][1]=calloc(VI_WINDOWB,sizeof(double *));
187   v->window[1][1][0]=calloc(VI_WINDOWB,sizeof(double *));
188   v->window[1][1][1]=calloc(VI_WINDOWB,sizeof(double *));
189
190   for(i=0;i<VI_WINDOWB;i++){
191     v->window[0][0][0][i]=
192       _vorbis_window(i,vi->blocksizes[0],vi->blocksizes[0]/2,vi->blocksizes[0]/2);
193     v->window[1][0][0][i]=
194       _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[0]/2,vi->blocksizes[0]/2);
195     v->window[1][0][1][i]=
196       _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[0]/2,vi->blocksizes[1]/2);
197     v->window[1][1][0][i]=
198       _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[1]/2,vi->blocksizes[0]/2);
199     v->window[1][1][1][i]=
200       _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[1]/2,vi->blocksizes[1]/2);
201   }
202
203   if(encp){ /* encode/decode differ here */
204     /* finish the codebooks */
205     v->fullbooks=calloc(vi->books,sizeof(codebook));
206     for(i=0;i<vi->books;i++)
207       vorbis_book_init_encode(v->fullbooks+i,vi->book_param[i]);
208     v->analysisp=1;
209   }else{
210     /* finish the codebooks */
211     v->fullbooks=calloc(vi->books,sizeof(codebook));
212     for(i=0;i<vi->books;i++)
213       vorbis_book_init_decode(v->fullbooks+i,vi->book_param[i]);
214   }
215
216   /* initialize the storage vectors to a decent size greater than the
217      minimum */
218   
219   v->pcm_storage=8192; /* we'll assume later that we have
220                           a minimum of twice the blocksize of
221                           accumulated samples in analysis */
222   v->pcm=malloc(vi->channels*sizeof(double *));
223   v->pcmret=malloc(vi->channels*sizeof(double *));
224   {
225     int i;
226     for(i=0;i<vi->channels;i++)
227       v->pcm[i]=calloc(v->pcm_storage,sizeof(double));
228   }
229
230   /* all 1 (large block) or 0 (small block) */
231   /* explicitly set for the sake of clarity */
232   v->lW=0; /* previous window size */
233   v->W=0;  /* current window size */
234
235   /* all vector indexes; multiples of samples_per_envelope_step */
236   v->centerW=vi->blocksizes[1]/2;
237
238   v->pcm_current=v->centerW;
239
240   /* initialize all the mapping/backend lookups */
241   v->mode=calloc(vi->modes,sizeof(vorbis_look_mapping *));
242   for(i=0;i<vi->modes;i++){
243     int mapnum=vi->mode_param[i]->mapping;
244     int maptype=vi->map_type[mapnum];
245     v->mode[i]=_mapping_P[maptype]->look(v,vi->mode_param[i],
246                                          vi->map_param[mapnum]);
247   }
248
249   return(0);
250 }
251
252 /* arbitrary settings and spec-mandated numbers get filled in here */
253 int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
254   _vds_shared_init(v,vi,1);
255
256   /* Initialize the envelope multiplier storage */
257
258   v->envelope_storage=v->pcm_storage/vi->envelopesa;
259   v->multipliers=calloc(v->envelope_storage,sizeof(double));
260   _ve_envelope_init(&v->ve,vi->envelopesa);
261
262   v->envelope_current=v->centerW/vi->envelopesa;
263   return(0);
264 }
265
266 void vorbis_dsp_clear(vorbis_dsp_state *v){
267   int i,j,k;
268   if(v){
269     vorbis_info *vi=v->vi;
270
271     if(v->window[0][0][0])
272       for(i=0;i<VI_WINDOWB;i++){
273         if(v->window[0][0][0][i])free(v->window[0][0][0][i]);
274         for(j=0;j<2;j++)
275           for(k=0;k<2;k++)
276             if(v->window[1][j][k][i])free(v->window[1][j][k][i]);
277       }
278  
279     if(v->pcm){
280       for(i=0;i<vi->channels;i++)
281         if(v->pcm[i])free(v->pcm[i]);
282       free(v->pcm);
283       if(v->pcmret)free(v->pcmret);
284     }
285     if(v->multipliers)free(v->multipliers);
286
287     _ve_envelope_clear(&v->ve);
288     if(v->transform[0]){
289       mdct_clear(v->transform[0][0]);
290       free(v->transform[0][0]);
291       free(v->transform[0]);
292     }
293     if(v->transform[1]){
294       mdct_clear(v->transform[1][0]);
295       free(v->transform[1][0]);
296       free(v->transform[1]);
297     }
298
299     /* free mode lookups; these are actually vorbis_look_mapping structs */
300     if(vi){
301       for(i=0;i<vi->modes;i++){
302         int mapnum=vi->mode_param[i]->mapping;
303         int maptype=vi->map_type[mapnum];
304         _mapping_P[maptype]->free_look(v->mode[i]);
305       }
306       /* free codebooks */
307       for(i=0;i<vi->books;i++)
308         vorbis_book_clear(v->fullbooks+i);
309     }
310
311     if(v->mode)free(v->mode);    
312     if(v->fullbooks)free(v->fullbooks);
313
314     /* free header, header1, header2 */
315     if(v->header)free(v->header);
316     if(v->header1)free(v->header1);
317     if(v->header2)free(v->header2);
318
319     memset(v,0,sizeof(vorbis_dsp_state));
320   }
321 }
322
323 double **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
324   int i;
325   vorbis_info *vi=v->vi;
326
327   /* free header, header1, header2 */
328   if(v->header)free(v->header);v->header=NULL;
329   if(v->header1)free(v->header1);v->header1=NULL;
330   if(v->header2)free(v->header2);v->header2=NULL;
331
332   /* Do we have enough storage space for the requested buffer? If not,
333      expand the PCM (and envelope) storage */
334     
335   if(v->pcm_current+vals>=v->pcm_storage){
336     v->pcm_storage=v->pcm_current+vals*2;
337     v->envelope_storage=v->pcm_storage/v->vi->envelopesa;
338    
339     for(i=0;i<vi->channels;i++){
340       v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
341     }
342     v->multipliers=realloc(v->multipliers,v->envelope_storage*sizeof(double));
343   }
344
345   for(i=0;i<vi->channels;i++)
346     v->pcmret[i]=v->pcm[i]+v->pcm_current;
347     
348   return(v->pcmret);
349 }
350
351 /* call with val<=0 to set eof */
352
353 int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
354   vorbis_info *vi=v->vi;
355   if(vals<=0){
356     /* We're encoding the end of the stream.  Just make sure we have
357        [at least] a full block of zeroes at the end. */
358
359     int i;
360     vorbis_analysis_buffer(v,v->vi->blocksizes[1]*2);
361     v->eofflag=v->pcm_current;
362     v->pcm_current+=v->vi->blocksizes[1]*2;
363     for(i=0;i<vi->channels;i++)
364       memset(v->pcm[i]+v->eofflag,0,
365              (v->pcm_current-v->eofflag)*sizeof(double));
366   }else{
367     
368     if(v->pcm_current+vals>v->pcm_storage)
369       return(-1);
370
371     v->pcm_current+=vals;
372   }
373   return(0);
374 }
375
376 /* do the deltas, envelope shaping, pre-echo and determine the size of
377    the next block on which to continue analysis */
378 int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
379   int i;
380   vorbis_info *vi=v->vi;
381   long beginW=v->centerW-vi->blocksizes[v->W]/2,centerNext;
382
383   /* check to see if we're done... */
384   if(v->eofflag==-1)return(0);
385
386   /* if we have any unfilled envelope blocks for which we have PCM
387      data, fill them up in before proceeding. */
388
389   if(v->pcm_current/vi->envelopesa>v->envelope_current){
390     /* This generates the multipliers, but does not sparsify the vector.
391        That's done by block before coding */
392     _ve_envelope_deltas(v);
393   }
394
395   /* By our invariant, we have lW, W and centerW set.  Search for
396      the next boundary so we can determine nW (the next window size)
397      which lets us compute the shape of the current block's window */
398   
399   if(vi->blocksizes[0]<vi->blocksizes[1]){
400     
401     if(v->W)
402       /* this is a long window; we start the search forward of centerW
403          because that's the fastest we could react anyway */
404       i=v->centerW+vi->blocksizes[1]/4-vi->blocksizes[0]/4;
405     else
406       /* short window.  Search from centerW */
407       i=v->centerW;
408     i/=vi->envelopesa;
409     
410     for(;i<v->envelope_current-1;i++){
411       /* Compare last with current; do we have an abrupt energy change? */
412       
413       if(v->multipliers[i-1]*vi->preecho_thresh<  
414          v->multipliers[i])break;
415       
416       /* because the overlapping nature of the delta finding
417          'smears' the energy cliffs, also compare completely
418          unoverlapped areas just in case the plosive happened in an
419          unlucky place */
420       
421       if(v->multipliers[i-1]*vi->preecho_thresh<  
422          v->multipliers[i+1])break;
423         
424     }
425     
426     if(i<v->envelope_current-1){
427       /* Ooo, we hit a multiplier. Is it beyond the boundary to make the
428          upcoming block large ? */
429       long largebound;
430       if(v->W)
431         /* min boundary; nW large, next small */
432         largebound=v->centerW+vi->blocksizes[1]*3/4+vi->blocksizes[0]/4;
433       else
434         /* min boundary; nW large, next small */
435         largebound=v->centerW+vi->blocksizes[0]/2+vi->blocksizes[1]/2;
436       largebound/=vi->envelopesa;
437       
438       if(i>=largebound)
439         v->nW=1;
440       else
441         v->nW=0;
442       
443     }else{
444       /* Assume maximum; if the block is incomplete given current
445          buffered data, this will be detected below */
446       v->nW=1;
447     }
448   }else
449     v->nW=0;
450
451   /* Do we actually have enough data *now* for the next block? The
452      reason to check is that if we had no multipliers, that could
453      simply been due to running out of data.  In that case, we don't
454      know the size of the next block for sure and we need that now to
455      figure out the window shape of this block */
456   
457   centerNext=v->centerW+vi->blocksizes[v->W]/4+vi->blocksizes[v->nW]/4;
458
459   {
460     /* center of next block + next block maximum right side.  Note
461        that the next block needs an additional vi->envelopesa samples 
462        to actually be written (for the last multiplier), but we didn't
463        need that to determine its size */
464
465     long blockbound=centerNext+vi->blocksizes[v->nW]/2;
466     if(v->pcm_current<blockbound)return(0); /* not enough data yet */    
467   }
468   
469   /* fill in the block.  Note that for a short window, lW and nW are *short*
470      regardless of actual settings in the stream */
471
472   _vorbis_block_ripcord(vb);
473   if(v->W){
474     vb->lW=v->lW;
475     vb->W=v->W;
476     vb->nW=v->nW;
477   }else{
478     vb->lW=0;
479     vb->W=v->W;
480     vb->nW=0;
481   }
482   vb->vd=v;
483   vb->sequence=v->sequence;
484   vb->frameno=v->frameno;
485   vb->pcmend=vi->blocksizes[v->W];
486   
487   /* copy the vectors; this uses the local storage in vb */
488   {
489     vb->pcm=_vorbis_block_alloc(vb,sizeof(double *)*vi->channels);
490     for(i=0;i<vi->channels;i++){
491       vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(double));
492       memcpy(vb->pcm[i],v->pcm[i]+beginW,vi->blocksizes[v->W]*sizeof(double));
493     }
494   }
495   
496   /* handle eof detection: eof==0 means that we've not yet received EOF
497                            eof>0  marks the last 'real' sample in pcm[]
498                            eof<0  'no more to do'; doesn't get here */
499
500   if(v->eofflag){
501     if(v->centerW>=v->eofflag){
502       v->eofflag=-1;
503       vb->eofflag=1;
504     }
505   }
506
507   /* advance storage vectors and clean up */
508   {
509     int new_centerNext=vi->blocksizes[1]/2;
510     int movementW=centerNext-new_centerNext;
511     int movementM=movementW/vi->envelopesa;
512
513     /* the multipliers and pcm stay synced up because the blocksize
514        must be multiples of samples_per_envelope_step (minimum
515        multiple is 2) */
516
517     v->pcm_current-=movementW;
518     v->envelope_current-=movementM;
519
520     for(i=0;i<vi->channels;i++)
521       memmove(v->pcm[i],v->pcm[i]+movementW,
522               v->pcm_current*sizeof(double));
523     
524     memmove(v->multipliers,v->multipliers+movementM,
525             v->envelope_current*sizeof(double));
526
527     v->lW=v->W;
528     v->W=v->nW;
529     v->centerW=new_centerNext;
530
531     v->sequence++;
532     v->frameno+=movementW;
533
534     if(v->eofflag)
535       v->eofflag-=movementW;
536   }
537
538   /* done */
539   return(1);
540 }
541
542 int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
543   _vds_shared_init(v,vi,0);
544
545   /* Adjust centerW to allow an easier mechanism for determining output */
546   v->pcm_returned=v->centerW;
547   v->centerW-= vi->blocksizes[v->W]/4+vi->blocksizes[v->lW]/4;
548   return(0);
549 }
550
551 /* Unike in analysis, the window is only partially applied for each
552    block.  The time domain envelope is not yet handled at the point of
553    calling (as it relies on the previous block). */
554
555 int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
556   vorbis_info *vi=v->vi;
557
558   /* Shift out any PCM/multipliers that we returned previously */
559   /* centerW is currently the center of the last block added */
560   if(v->pcm_returned  && v->centerW>vi->blocksizes[1]/2){
561
562     /* don't shift too much; we need to have a minimum PCM buffer of
563        1/2 long block */
564
565     int shiftPCM=v->centerW-vi->blocksizes[1]/2;
566     shiftPCM=(v->pcm_returned<shiftPCM?v->pcm_returned:shiftPCM);
567
568     v->pcm_current-=shiftPCM;
569     v->centerW-=shiftPCM;
570     v->pcm_returned-=shiftPCM;
571     
572     if(shiftPCM){
573       int i;
574       for(i=0;i<vi->channels;i++)
575         memmove(v->pcm[i],v->pcm[i]+shiftPCM,
576                 v->pcm_current*sizeof(double));
577     }
578   }
579
580   v->lW=v->W;
581   v->W=vb->W;
582   v->nW=-1;
583
584   v->glue_bits+=vb->glue_bits;
585   v->time_bits+=vb->time_bits;
586   v->floor_bits+=vb->floor_bits;
587   v->res_bits+=vb->res_bits;
588   v->sequence=vb->sequence;
589
590   {
591     int sizeW=vi->blocksizes[v->W];
592     int centerW=v->centerW+vi->blocksizes[v->lW]/4+sizeW/4;
593     int beginW=centerW-sizeW/2;
594     int endW=beginW+sizeW;
595     int beginSl;
596     int endSl;
597     int i,j;
598
599     /* Do we have enough PCM/mult storage for the block? */
600     if(endW>v->pcm_storage){
601       /* expand the storage */
602       v->pcm_storage=endW+vi->blocksizes[1];
603    
604       for(i=0;i<vi->channels;i++)
605         v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double)); 
606     }
607
608     /* overlap/add PCM */
609
610     switch(v->W){
611     case 0:
612       beginSl=0;
613       endSl=vi->blocksizes[0]/2;
614       break;
615     case 1:
616       beginSl=vi->blocksizes[1]/4-vi->blocksizes[v->lW]/4;
617       endSl=beginSl+vi->blocksizes[v->lW]/2;
618       break;
619     }
620
621     for(j=0;j<vi->channels;j++){
622       double *pcm=v->pcm[j]+beginW;
623       
624       /* the overlap/add section */
625       for(i=beginSl;i<endSl;i++)
626         pcm[i]+=vb->pcm[j][i];
627       /* the remaining section */
628       for(;i<sizeW;i++)
629         pcm[i]=vb->pcm[j][i];
630     }
631
632     /* Update, cleanup */
633
634     v->centerW=centerW;
635     v->pcm_current=endW;
636
637     if(vb->eofflag)v->eofflag=1;
638   }
639   return(0);
640 }
641
642 int vorbis_synthesis_pcmout(vorbis_dsp_state *v,double ***pcm){
643   vorbis_info *vi=v->vi;
644   if(v->pcm_returned<v->centerW){
645     int i;
646     for(i=0;i<vi->channels;i++)
647       v->pcmret[i]=v->pcm[i]+v->pcm_returned;
648     *pcm=v->pcmret;
649     return(v->centerW-v->pcm_returned);
650   }
651   return(0);
652 }
653
654 int vorbis_synthesis_read(vorbis_dsp_state *v,int bytes){
655   if(bytes && v->pcm_returned+bytes>v->centerW)return(-1);
656   v->pcm_returned+=bytes;
657   return(0);
658 }
659