1 /********************************************************************
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. *
8 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999 *
9 * by 1999 Monty <monty@xiph.org> and The XIPHOPHORUS Company *
10 * http://www.xiph.org/ *
12 ********************************************************************
14 function: PCM data vector blocking, windowing and dis/reassembly
15 author: Monty <xiphmont@mit.edu>
16 modifications by: Monty
17 last modification date: Jul 29 1999
19 Handle windowing, overlap-add, etc of the PCM vectors. This is made
20 more amusing by Vorbis' current two allowed block sizes (512 and 2048
23 Vorbis manipulates the dynamic range of the incoming PCM data
24 envelope to minimise time-domain energy leakage from percussive and
25 plosive waveforms being quantized in the MDCT domain.
27 ********************************************************************/
36 /* pcm accumulator examples (not exhaustive):
38 <-------------- lW ---------------->
39 <--------------- W ---------------->
40 : .....|..... _______________ |
41 : .''' | '''_--- | |\ |
42 :.....''' |_____--- '''......| | \_______|
43 :.................|__________________|_______|__|______|
44 |<------ Sl ------>| > Sr < |endW
45 |beginSl |endSl | |endSr
46 |beginW |endlW |beginSr
50 <--------------- W ---------------->
51 | | .. ______________ |
53 |___.'___/`. | ---_____|
54 |_______|__|_______|_________________|
55 | >|Sl|< |<------ Sr ----->|endW
56 | | |endSl |beginSr |endSr
58 mult[0] |beginSl mult[n]
60 <-------------- lW ----------------->
62 : .............. ___ | |
64 :.....''' |/`....\|...|
65 :.........................|___|___|___|
74 static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi){
75 memset(v,0,sizeof(vorbis_dsp_state));
76 v->samples_per_envelope_step=vi->envelopesa;
77 v->block_size[0]=vi->smallblock;
78 v->block_size[1]=vi->largeblock;
80 v->window[0][0][0]=_vorbis_window(v->block_size[0],
81 v->block_size[0]/2,v->block_size[0]/2);
82 v->window[0][0][1]=v->window[0][0][0];
83 v->window[0][1][0]=v->window[0][0][0];
84 v->window[0][1][1]=v->window[0][0][0];
86 v->window[1][0][0]=_vorbis_window(v->block_size[1],
87 v->block_size[0]/2,v->block_size[0]/2);
88 v->window[1][0][1]=_vorbis_window(v->block_size[1],
89 v->block_size[0]/2,v->block_size[1]/2);
90 v->window[1][1][0]=_vorbis_window(v->block_size[1],
91 v->block_size[1]/2,v->block_size[0]/2);
92 v->window[1][1][1]=_vorbis_window(v->block_size[1],
93 v->block_size[1]/2,v->block_size[1]/2);
95 /* initialize the storage vectors to a decent size greater than the
98 v->pcm_storage=8192; /* we'll assume later that we have
99 a minimum of twice the blocksize of
100 accumulated samples in analysis */
101 v->pcm_channels=v->vi.channels=vi->channels;
102 v->pcm=malloc(vi->channels*sizeof(double *));
103 v->pcmret=malloc(vi->channels*sizeof(double *));
106 for(i=0;i<vi->channels;i++)
107 v->pcm[i]=calloc(v->pcm_storage,sizeof(double));
110 /* Initialize the envelope multiplier storage */
113 v->envelope_storage=v->pcm_storage/v->samples_per_envelope_step;
114 v->envelope_channels=vi->envelopech;
115 v->multipliers=calloc(v->envelope_channels,sizeof(int *));
118 for(i=0;i<v->envelope_channels;i++){
119 v->multipliers[i]=calloc(v->envelope_storage,sizeof(int));
124 /* all 1 (large block) or 0 (small block) */
125 /* explicitly set for the sake of clarity */
126 v->lW=0; /* previous window size */
127 v->W=0; /* current window size */
129 /* all vector indexes; multiples of samples_per_envelope_step */
130 v->centerW=v->block_size[1]/2;
132 v->pcm_current=v->centerW;
133 v->envelope_current=v->centerW/v->samples_per_envelope_step;
137 /* arbitrary settings and spec-mandated numbers get filled in here */
138 int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
142 vi->envelopech=vi->channels;
144 _vds_shared_init(v,vi);
149 void vorbis_analysis_clear(vorbis_dsp_state *v){
153 if(v->window[0][0][0])free(v->window[0][0][0]);
156 if(v->window[1][j][k])free(v->window[1][j][k]);
158 for(i=0;i<v->pcm_channels;i++)
159 if(v->pcm[i])free(v->pcm[i]);
164 for(i=0;i<v->envelope_channels;i++)
165 if(v->multipliers[i])free(v->multipliers[i]);
166 free(v->multipliers);
168 memset(v,0,sizeof(vorbis_dsp_state));
172 double **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
175 /* Do we have enough storage space for the requested buffer? If not,
176 expand the PCM (and envelope) storage */
178 if(v->pcm_current+vals>=v->pcm_storage){
179 v->pcm_storage=v->pcm_current+vals*2;
180 v->envelope_storage=v->pcm_storage/v->samples_per_envelope_step;
182 for(i=0;i<v->pcm_channels;i++){
183 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
185 for(i=0;i<v->envelope_channels;i++){
186 v->multipliers[i]=realloc(v->multipliers[i],
187 v->envelope_storage*sizeof(double));
191 for(i=0;i<v->pcm_channels;i++)
192 v->pcmret[i]=v->pcm[i]+v->pcm_current;
197 /* call with val<=0 to set eof */
199 int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
201 /* We're encoding the end of the stream. Just make sure we have
202 [at least] a full block of zeroes at the end. */
205 vorbis_analysis_buffer(v,v->block_size[1]*2);
206 v->eofflag=v->pcm_current;
207 v->pcm_current+=v->block_size[1]*2;
208 for(i=0;i<v->pcm_channels;i++)
209 memset(v->pcm[i]+v->eofflag,0,
210 (v->pcm_current-v->eofflag)*sizeof(double));
213 if(v->pcm_current+vals>v->pcm_storage)
216 v->pcm_current+=vals;
221 int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
223 vb->pcm_storage=v->block_size[1];
224 vb->pcm_channels=v->pcm_channels;
225 vb->mult_storage=v->block_size[1]/v->samples_per_envelope_step;
226 vb->mult_channels=v->envelope_channels;
228 vb->pcm=malloc(vb->pcm_channels*sizeof(double *));
229 for(i=0;i<vb->pcm_channels;i++)
230 vb->pcm[i]=malloc(vb->pcm_storage*sizeof(double));
232 vb->mult=malloc(vb->mult_channels*sizeof(int *));
233 for(i=0;i<vb->mult_channels;i++)
234 vb->mult[i]=malloc(vb->mult_storage*sizeof(int));
238 int vorbis_block_clear(vorbis_block *vb){
241 for(i=0;i<vb->pcm_channels;i++)
246 for(i=0;i<vb->mult_channels;i++)
250 memset(vb,0,sizeof(vorbis_block));
254 /* do the deltas, envelope shaping, pre-echo and determine the size of
255 the next block on which to continue analysis */
256 int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
258 long beginW=v->centerW-v->block_size[v->W]/2,centerNext;
259 long beginM=beginW/v->samples_per_envelope_step;
261 /* check to see if we're done... */
262 if(v->eofflag==-1)return(0);
264 /* if we have any unfilled envelope blocks for which we have PCM
265 data, fill them up in before proceeding. */
267 if(v->pcm_current/v->samples_per_envelope_step>v->envelope_current){
268 _ve_envelope_multipliers(v);
271 /* By our invariant, we have lW, W and centerW set. Search for
272 the next boundary so we can determine nW (the next window size)
273 which lets us compute the shape of the current block's window */
275 /* overconserve for now; any block with a non-placeholder multiplier
276 should be minimal size. We can be greedy and only look at nW size */
279 /* this is a long window; we start the search forward of centerW
280 because that's the fastest we could react anyway */
281 i=v->centerW+v->block_size[1]/4-v->block_size[0]/4;
283 /* short window. Search from centerW */
285 i/=v->samples_per_envelope_step;
287 for(;i<v->envelope_current;i++){
288 for(j=0;j<v->envelope_channels;j++)
289 if(v->multipliers[j][i])break;
290 if(j<v->envelope_channels)break;
293 if(i<v->envelope_current){
294 /* Ooo, we hit a multiplier. Is it beyond the boundary to make the
295 upcoming block large ? */
298 largebound=v->centerW+v->block_size[1];
300 largebound=v->centerW+v->block_size[0]/4+v->block_size[1]*3/4;
301 largebound/=v->samples_per_envelope_step;
309 /* Assume maximum; if the block is incomplete given current
310 buffered data, this will be detected below */
314 /* Do we actually have enough data *now* for the next block? The
315 reason to check is that if we had no multipliers, that could
316 simply been due to running out of data. In that case, we don;t
317 know the size of the next block for sure and we need that now to
318 figure out the window shape of this block */
320 centerNext=v->centerW+v->block_size[v->W]/4+v->block_size[v->nW]/4;
323 long blockbound=centerNext+v->block_size[v->nW]/2;
324 if(v->pcm_current<blockbound)return(0); /* not enough data yet */
327 /* fill in the block */
333 vb->pcmend=v->block_size[v->W];
334 vb->multend=vb->pcmend / v->samples_per_envelope_step;
336 if(v->pcm_channels!=vb->pcm_channels ||
337 v->block_size[1]!=vb->pcm_storage ||
338 v->envelope_channels!=vb->mult_channels){
340 /* Storage not initialized or initilized for some other codec
341 instance with different settings */
343 vorbis_block_clear(vb);
344 vorbis_block_init(v,vb);
347 /* copy the vectors */
348 for(i=0;i<v->pcm_channels;i++)
349 memcpy(vb->pcm[i],v->pcm[i]+beginW,v->block_size[v->W]*sizeof(double));
350 for(i=0;i<v->envelope_channels;i++)
351 memcpy(vb->mult[i],v->multipliers[i]+beginM,v->block_size[v->W]/
352 v->samples_per_envelope_step*sizeof(int));
354 vb->frameno=v->frame;
356 /* handle eof detection: eof==0 means that we've not yet received EOF
357 eof>0 marks the last 'real' sample in pcm[]
358 eof<0 'no more to do'; doesn't get here */
361 if(v->centerW>=v->eofflag){
367 /* advance storage vectors and clean up */
369 int new_centerNext=v->block_size[1]/2;
370 int movementW=centerNext-new_centerNext;
371 int movementM=movementW/v->samples_per_envelope_step;
373 for(i=0;i<v->pcm_channels;i++)
374 memmove(v->pcm[i],v->pcm[i]+movementW,
375 (v->pcm_current-movementW)*sizeof(double));
377 for(i=0;i<v->envelope_channels;i++){
378 memmove(v->multipliers[i],v->multipliers[i]+movementM,
379 (v->envelope_current-movementM)*sizeof(int));
382 v->pcm_current-=movementW;
383 v->envelope_current-=movementM;
387 v->centerW=new_centerNext;
390 v->samples+=movementW;
393 v->eofflag-=movementW;
400 int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
401 _vds_shared_init(v,vi);
402 /* Adjust centerW to allow an easier mechanism for determining output */
403 v->pcm_returned=v->centerW;
404 v->centerW-= v->block_size[v->W]/4+v->block_size[v->lW]/4;
408 /* Unike in analysis, the window is only partially applied, and
409 envelope *not* applied. */
411 int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
413 /* Shift out any PCM that we returned previously */
415 if(v->pcm_returned && v->centerW>v->block_size[1]/2){
417 /* don't shift too much; we need to have a minimum PCM buffer of
420 int shift=v->centerW-v->block_size[1]/2;
421 shift=(v->pcm_returned<shift?v->pcm_returned:shift);
423 v->pcm_current-=shift;
425 v->pcm_returned-=shift;
429 for(i=0;i<v->pcm_channels;i++)
430 memmove(v->pcm[i],v->pcm[i]+shift,
431 v->pcm_current*sizeof(double));
436 int envperlong=v->block_size[1]/v->samples_per_envelope_step;
437 int envperW=v->block_size[v->W]/v->samples_per_envelope_step;
438 int sizeW=v->block_size[vb->W];
439 int centerW=v->centerW+v->block_size[vb->lW]/4+sizeW/4;
440 int beginW=centerW-sizeW/2;
441 int endW=beginW+sizeW;
447 /* Do we have enough PCM storage for the block? */
448 if(endW>v->pcm_storage){
449 /* expand the PCM storage */
451 v->pcm_storage=endW+v->block_size[1];
453 for(i=0;i<v->pcm_channels;i++)
454 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
457 /* multiplier storage works differently in decode than it does in
458 encode; we only need to buffer the last 'half largeblock' and
459 we don't need to keep it aligned with the PCM. */
461 /* fill in the first half of the block's multipliers */
462 i=v->envelope_current-1;
465 for(k=0;k<v->envelope_channels;k++)
466 vb->mult[k][j]=v->multipliers[k][i];
468 /* shift out unneeded buffered multipliers */
470 int needed=(envperlong-envperW)/2;
472 /* We need to keep some of the buffered ones */
473 for(k=0;k<v->envelope_channels;k++)
474 memmove(v->multipliers[k],
475 v->multipliers[k]+v->envelope_current-needed,
478 v->envelope_current=needed;
481 /* add block's second half to the multiplier buffer */
482 /* init makes certain we have enough storage; we only buffer a
485 for(i=envperW/2;i<envperW;i++){
486 j=v->envelope_current;
487 for(k=0;k<v->envelope_channels;k++)
488 v->multipliers[k][j++]=vb->mult[k][i];
490 v->envelope_current+=envperW/2;
492 /* manufacture/apply multiplier vector */
494 _ve_envelope_apply(vb);
500 endSl=v->block_size[0]/2;
503 beginSl=v->block_size[1]/4-v->block_size[vb->lW]/4;
504 endSl=beginSl+v->block_size[vb->lW]/2;
508 window=v->window[vb->W][0][vb->lW]+v->block_size[vb->W]/2;
510 for(j=0;j<v->pcm_channels;j++){
511 double *pcm=v->pcm[j]+beginW;
513 /* the add section */
514 for(i=beginSl;i<endSl;i++)
515 pcm[i]=pcm[i]*window[i]+vb->pcm[j][i];
516 /* the remaining section */
518 pcm[i]=vb->pcm[j][i];
521 /* Update, cleanup */
526 if(vb->eofflag)v->eofflag=1;
531 int vorbis_synthesis_pcmout(vorbis_dsp_state *v,double ***pcm){
532 if(v->pcm_returned<v->centerW){
534 for(i=0;i<v->pcm_channels;i++)
535 v->pcmret[i]=v->pcm[i]+v->pcm_returned;
537 return(v->centerW-v->pcm_returned);
542 int vorbis_synthesis_read(vorbis_dsp_state *v,int bytes){
543 if(bytes && v->pcm_returned+bytes>v->centerW)return(-1);
544 v->pcm_returned+=bytes;
552 void _ve_envelope_multipliers(vorbis_dsp_state *v){
553 /* set 'random' deltas... */
554 int new_current=v->pcm_current/v->samples_per_envelope_step;
557 for(i=v->envelope_current;i<new_current;i++){
559 for(j=0;j<v->samples_per_envelope_step;j++)
560 if(v->pcm[0][j+i*v->samples_per_envelope_step]>5)flag=1;
562 for(j=0;j<v->envelope_channels;j++)
563 v->multipliers[j][i]=flag;
565 v->envelope_current=i;
568 void _ve_envelope_apply(vorbis_block *vb){
572 /* basic test of PCM blocking:
574 construct a PCM vector and block it using preset sizing in our fake
575 delta/multiplier generation. Immediately hand the block over to
576 'synthesis' and rebuild it. */
581 vorbis_dsp_state encode,decode;
588 char *temp[]={ "Test" ,"the Test band", "test records",NULL };
598 vi.user_comments=temp;
599 vi.vendor="Xiphophorus";
601 vorbis_analysis_init(&encode,&vi);
602 vorbis_synthesis_init(&decode,&vi);
604 memset(&vb,0,sizeof(vorbis_block));
605 vorbis_block_init(&encode,&vb);
607 ml[0]=MDCT_init(encode.block_size[0]);
608 ml[1]=MDCT_init(encode.block_size[1]);
610 /* Submit 100K samples of data reading out blocks... */
614 double **buf=vorbis_analysis_buffer(&encode,blocksize);
615 for(i=0;i<blocksize;i++){
616 buf[0][i]=sin((counterin+i)%500/500.*M_PI*2)+2;
619 if((counterin+i)%15000>13000)buf[0][i]+=10;
622 i=(counterin+blocksize>fini?fini-counterin:blocksize);
623 vorbis_analysis_wrote(&encode,i);
626 while(vorbis_analysis_blockout(&encode,&vb)){
632 double *window=encode.window[vb.W][vb.lW][vb.nW];
634 for(i=0;i<vb.pcm_channels;i++)
635 MDCT(vb.pcm[i],vb.pcm[i],ml[vb.W],window);
637 for(i=0;i<vb.pcm_channels;i++)
638 iMDCT(vb.pcm[i],vb.pcm[i],ml[vb.W],window);
646 int avail=encode.block_size[vb.W];
647 int beginW=countermid-avail/2;
649 sprintf(path,"ana%d",vb.frameno);
653 fprintf(out,"%ld %g\n",i+beginW,vb.pcm[0][i]);
656 fprintf(out,"%ld %g\n",i+beginW,window[i]);
659 countermid+=encode.block_size[vb.W]/4+encode.block_size[vb.nW]/4;
663 vorbis_synthesis_blockin(&decode,&vb);
666 while((avail=vorbis_synthesis_pcmout(&decode,&pcm))){
671 sprintf(path,"syn%d",frame);
675 fprintf(out,"%ld %g\n",i+counterout,pcm[0][i]);
678 fprintf(out,"%ld %g\n",i+counterout,pcm[1][i]);
682 vorbis_synthesis_read(&decode,avail);