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: Aug 05 1999
19 Handle windowing, overlap-add, etc of the PCM vectors. This is made
20 more amusing by Vorbis' current two allowed block sizes.
22 Vorbis manipulates the dynamic range of the incoming PCM data
23 envelope to minimise time-domain energy leakage from percussive and
24 plosive waveforms being quantized in the MDCT domain.
26 ********************************************************************/
35 /* pcm accumulator examples (not exhaustive):
37 <-------------- lW ---------------->
38 <--------------- W ---------------->
39 : .....|..... _______________ |
40 : .''' | '''_--- | |\ |
41 :.....''' |_____--- '''......| | \_______|
42 :.................|__________________|_______|__|______|
43 |<------ Sl ------>| > Sr < |endW
44 |beginSl |endSl | |endSr
45 |beginW |endlW |beginSr
49 <--------------- W ---------------->
50 | | .. ______________ |
52 |___.'___/`. | ---_____|
53 |_______|__|_______|_________________|
54 | >|Sl|< |<------ Sr ----->|endW
55 | | |endSl |beginSr |endSr
57 mult[0] |beginSl mult[n]
59 <-------------- lW ----------------->
61 : .............. ___ | |
63 :.....''' |/`....\|...|
64 :.........................|___|___|___|
73 static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi){
74 memset(v,0,sizeof(vorbis_dsp_state));
76 memcpy(&v->vi,vi,sizeof(vorbis_info));
77 _ve_envelope_init(&v->ve,vi->envelopesa);
78 mdct_init(&v->vm[0],vi->smallblock);
79 mdct_init(&v->vm[1],vi->largeblock);
81 v->samples_per_envelope_step=vi->envelopesa;
82 v->block_size[0]=vi->smallblock;
83 v->block_size[1]=vi->largeblock;
85 v->window[0][0][0]=_vorbis_window(v->block_size[0],
86 v->block_size[0]/2,v->block_size[0]/2);
87 v->window[0][0][1]=v->window[0][0][0];
88 v->window[0][1][0]=v->window[0][0][0];
89 v->window[0][1][1]=v->window[0][0][0];
91 v->window[1][0][0]=_vorbis_window(v->block_size[1],
92 v->block_size[0]/2,v->block_size[0]/2);
93 v->window[1][0][1]=_vorbis_window(v->block_size[1],
94 v->block_size[0]/2,v->block_size[1]/2);
95 v->window[1][1][0]=_vorbis_window(v->block_size[1],
96 v->block_size[1]/2,v->block_size[0]/2);
97 v->window[1][1][1]=_vorbis_window(v->block_size[1],
98 v->block_size[1]/2,v->block_size[1]/2);
100 /* initialize the storage vectors to a decent size greater than the
103 v->pcm_storage=8192; /* we'll assume later that we have
104 a minimum of twice the blocksize of
105 accumulated samples in analysis */
106 v->pcm_channels=v->vi.channels=vi->channels;
107 v->pcm=malloc(vi->channels*sizeof(double *));
108 v->pcmret=malloc(vi->channels*sizeof(double *));
111 for(i=0;i<vi->channels;i++)
112 v->pcm[i]=calloc(v->pcm_storage,sizeof(double));
115 /* Initialize the envelope multiplier storage */
118 v->envelope_storage=v->pcm_storage/v->samples_per_envelope_step;
119 v->envelope_channels=vi->envelopech;
120 v->multipliers=calloc(v->envelope_channels,sizeof(double *));
123 for(i=0;i<v->envelope_channels;i++){
124 v->multipliers[i]=calloc(v->envelope_storage,sizeof(double));
129 /* all 1 (large block) or 0 (small block) */
130 /* explicitly set for the sake of clarity */
131 v->lW=0; /* previous window size */
132 v->W=0; /* current window size */
134 /* all vector indexes; multiples of samples_per_envelope_step */
135 v->centerW=v->block_size[1]/2;
137 v->pcm_current=v->centerW;
138 v->envelope_current=v->centerW/v->samples_per_envelope_step;
142 /* arbitrary settings and spec-mandated numbers get filled in here */
143 int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
144 _vds_shared_init(v,vi);
145 drft_init(&v->vf[0],vi->smallblock); /* only used in analysis */
146 drft_init(&v->vf[1],vi->largeblock); /* only used in analysis */
151 void vorbis_analysis_clear(vorbis_dsp_state *v){
155 if(v->window[0][0][0])free(v->window[0][0][0]);
158 if(v->window[1][j][k])free(v->window[1][j][k]);
160 for(i=0;i<v->pcm_channels;i++)
161 if(v->pcm[i])free(v->pcm[i]);
166 for(i=0;i<v->envelope_channels;i++)
167 if(v->multipliers[i])free(v->multipliers[i]);
168 free(v->multipliers);
170 _ve_envelope_clear(&v->ve);
171 drft_clear(&v->vf[0]);
172 drft_clear(&v->vf[1]);
173 mdct_clear(&v->vm[0]);
174 mdct_clear(&v->vm[1]);
175 memset(v,0,sizeof(vorbis_dsp_state));
179 double **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
182 /* Do we have enough storage space for the requested buffer? If not,
183 expand the PCM (and envelope) storage */
185 if(v->pcm_current+vals>=v->pcm_storage){
186 v->pcm_storage=v->pcm_current+vals*2;
187 v->envelope_storage=v->pcm_storage/v->samples_per_envelope_step;
189 for(i=0;i<v->pcm_channels;i++){
190 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
192 for(i=0;i<v->envelope_channels;i++){
193 v->multipliers[i]=realloc(v->multipliers[i],
194 v->envelope_storage*sizeof(double));
198 for(i=0;i<v->pcm_channels;i++)
199 v->pcmret[i]=v->pcm[i]+v->pcm_current;
204 /* call with val<=0 to set eof */
206 int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
208 /* We're encoding the end of the stream. Just make sure we have
209 [at least] a full block of zeroes at the end. */
212 vorbis_analysis_buffer(v,v->block_size[1]*2);
213 v->eofflag=v->pcm_current;
214 v->pcm_current+=v->block_size[1]*2;
215 for(i=0;i<v->pcm_channels;i++)
216 memset(v->pcm[i]+v->eofflag,0,
217 (v->pcm_current-v->eofflag)*sizeof(double));
220 if(v->pcm_current+vals>v->pcm_storage)
223 v->pcm_current+=vals;
228 int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
230 vb->pcm_storage=v->block_size[1];
231 vb->pcm_channels=v->pcm_channels;
232 vb->mult_storage=v->block_size[1]/v->samples_per_envelope_step;
233 vb->mult_channels=v->envelope_channels;
235 vb->pcm=malloc(vb->pcm_channels*sizeof(double *));
236 for(i=0;i<vb->pcm_channels;i++)
237 vb->pcm[i]=malloc(vb->pcm_storage*sizeof(double));
239 vb->mult=malloc(vb->mult_channels*sizeof(double *));
240 for(i=0;i<vb->mult_channels;i++)
241 vb->mult[i]=malloc(vb->mult_storage*sizeof(double));
245 int vorbis_block_clear(vorbis_block *vb){
248 for(i=0;i<vb->pcm_channels;i++)
253 for(i=0;i<vb->mult_channels;i++)
257 memset(vb,0,sizeof(vorbis_block));
261 /* do the deltas, envelope shaping, pre-echo and determine the size of
262 the next block on which to continue analysis */
263 int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
265 long beginW=v->centerW-v->block_size[v->W]/2,centerNext;
266 long beginM=beginW/v->samples_per_envelope_step;
268 /* check to see if we're done... */
269 if(v->eofflag==-1)return(0);
271 /* if we have any unfilled envelope blocks for which we have PCM
272 data, fill them up in before proceeding. */
274 if(v->pcm_current/v->samples_per_envelope_step>v->envelope_current){
275 /* This generates the multipliers, but does not sparsify the vector.
276 That's done by block before coding */
277 _ve_envelope_multipliers(v);
280 /* By our invariant, we have lW, W and centerW set. Search for
281 the next boundary so we can determine nW (the next window size)
282 which lets us compute the shape of the current block's window */
284 /* overconserve for now; any block with a non-placeholder multiplier
285 should be minimal size. We can be greedy and only look at nW size */
288 /* this is a long window; we start the search forward of centerW
289 because that's the fastest we could react anyway */
290 i=v->centerW+v->block_size[1]/4-v->block_size[0]/4;
292 /* short window. Search from centerW */
294 i/=v->samples_per_envelope_step;
296 for(;i<v->envelope_current;i++){
297 for(j=0;j<v->envelope_channels;j++)
298 if(v->multipliers[j][i-1]*v->vi.preecho_thresh<
299 v->multipliers[j][i])break;
300 if(j<v->envelope_channels)break;
303 if(i<v->envelope_current){
304 /* Ooo, we hit a multiplier. Is it beyond the boundary to make the
305 upcoming block large ? */
308 largebound=v->centerW+v->block_size[1];
310 largebound=v->centerW+v->block_size[0]/4+v->block_size[1]*3/4;
311 largebound/=v->samples_per_envelope_step;
319 /* Assume maximum; if the block is incomplete given current
320 buffered data, this will be detected below */
324 /* Do we actually have enough data *now* for the next block? The
325 reason to check is that if we had no multipliers, that could
326 simply been due to running out of data. In that case, we don;t
327 know the size of the next block for sure and we need that now to
328 figure out the window shape of this block */
330 centerNext=v->centerW+v->block_size[v->W]/4+v->block_size[v->nW]/4;
333 long blockbound=centerNext+v->block_size[v->nW]/2;
334 if(v->pcm_current<blockbound)return(0); /* not enough data yet */
337 /* fill in the block */
343 vb->pcmend=v->block_size[v->W];
344 vb->multend=vb->pcmend / v->samples_per_envelope_step;
346 if(v->pcm_channels!=vb->pcm_channels ||
347 v->block_size[1]!=vb->pcm_storage ||
348 v->envelope_channels!=vb->mult_channels){
350 /* Storage not initialized or initilized for some other codec
351 instance with different settings */
353 vorbis_block_clear(vb);
354 vorbis_block_init(v,vb);
357 /* copy the vectors */
358 for(i=0;i<v->pcm_channels;i++)
359 memcpy(vb->pcm[i],v->pcm[i]+beginW,v->block_size[v->W]*sizeof(double));
360 for(i=0;i<v->envelope_channels;i++)
361 memcpy(vb->mult[i],v->multipliers[i]+beginM,v->block_size[v->W]/
362 v->samples_per_envelope_step*sizeof(double));
364 vb->frameno=v->frame;
366 /* handle eof detection: eof==0 means that we've not yet received EOF
367 eof>0 marks the last 'real' sample in pcm[]
368 eof<0 'no more to do'; doesn't get here */
371 if(v->centerW>=v->eofflag){
377 /* advance storage vectors and clean up */
379 int new_centerNext=v->block_size[1]/2;
380 int movementW=centerNext-new_centerNext;
381 int movementM=movementW/v->samples_per_envelope_step;
383 /* the multipliers and pcm stay synced up because the blocksizes
384 must be multiples of samples_per_envelope_step (minimum
387 for(i=0;i<v->pcm_channels;i++)
388 memmove(v->pcm[i],v->pcm[i]+movementW,
389 (v->pcm_current-movementW)*sizeof(double));
391 for(i=0;i<v->envelope_channels;i++){
392 memmove(v->multipliers[i],v->multipliers[i]+movementM,
393 (v->envelope_current-movementM)*sizeof(double));
396 v->pcm_current-=movementW;
397 v->envelope_current-=movementM;
401 v->centerW=new_centerNext;
404 v->samples+=movementW;
407 v->eofflag-=movementW;
414 int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
415 int temp=vi->envelopech;
416 vi->envelopech=0; /* we don't need multiplier buffering in syn */
417 _vds_shared_init(v,vi);
420 /* Adjust centerW to allow an easier mechanism for determining output */
421 v->pcm_returned=v->centerW;
422 v->centerW-= v->block_size[v->W]/4+v->block_size[v->lW]/4;
426 /* Unike in analysis, the window is only partially applied. Envelope
427 is previously applied (the whole envelope, if any, is shipped in
430 int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
432 /* Shift out any PCM that we returned previously */
434 if(v->pcm_returned && v->centerW>v->block_size[1]/2){
436 /* don't shift too much; we need to have a minimum PCM buffer of
439 int shift=v->centerW-v->block_size[1]/2;
440 shift=(v->pcm_returned<shift?v->pcm_returned:shift);
442 v->pcm_current-=shift;
444 v->pcm_returned-=shift;
448 for(i=0;i<v->pcm_channels;i++)
449 memmove(v->pcm[i],v->pcm[i]+shift,
450 v->pcm_current*sizeof(double));
455 int sizeW=v->block_size[vb->W];
456 int centerW=v->centerW+v->block_size[vb->lW]/4+sizeW/4;
457 int beginW=centerW-sizeW/2;
458 int endW=beginW+sizeW;
464 /* Do we have enough PCM storage for the block? */
465 if(endW>v->pcm_storage){
466 /* expand the PCM storage */
468 v->pcm_storage=endW+v->block_size[1];
470 for(i=0;i<v->pcm_channels;i++)
471 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
478 endSl=v->block_size[0]/2;
481 beginSl=v->block_size[1]/4-v->block_size[vb->lW]/4;
482 endSl=beginSl+v->block_size[vb->lW]/2;
486 window=v->window[vb->W][0][vb->lW]+v->block_size[vb->W]/2;
488 for(j=0;j<v->pcm_channels;j++){
489 double *pcm=v->pcm[j]+beginW;
491 /* the add section */
492 for(i=beginSl;i<endSl;i++)
493 pcm[i]=pcm[i]*window[i]+vb->pcm[j][i];
494 /* the remaining section */
496 pcm[i]=vb->pcm[j][i];
499 /* Update, cleanup */
504 if(vb->eofflag)v->eofflag=1;
509 int vorbis_synthesis_pcmout(vorbis_dsp_state *v,double ***pcm){
510 if(v->pcm_returned<v->centerW){
512 for(i=0;i<v->pcm_channels;i++)
513 v->pcmret[i]=v->pcm[i]+v->pcm_returned;
515 return(v->centerW-v->pcm_returned);
520 int vorbis_synthesis_read(vorbis_dsp_state *v,int bytes){
521 if(bytes && v->pcm_returned+bytes>v->centerW)return(-1);
522 v->pcm_returned+=bytes;