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: Oct 2 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 ********************************************************************/
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 /* block abstraction setup *********************************************/
76 int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
78 memset(vb,0,sizeof(vorbis_block));
81 vb->pcm_storage=v->block_size[1];
82 vb->pcm_channels=v->pcm_channels;
83 vb->mult_storage=v->block_size[1]/v->samples_per_envelope_step;
84 vb->mult_channels=v->envelope_channels;
85 vb->floor_channels=v->vi.floorch;
86 vb->floor_storage=v->vi.floororder;
88 vb->pcm=malloc(vb->pcm_channels*sizeof(double *));
89 for(i=0;i<vb->pcm_channels;i++)
90 vb->pcm[i]=malloc(vb->pcm_storage*sizeof(double));
92 vb->mult=malloc(vb->mult_channels*sizeof(double *));
93 for(i=0;i<vb->mult_channels;i++)
94 vb->mult[i]=malloc(vb->mult_storage*sizeof(double));
96 vb->lsp=malloc(vb->floor_channels*sizeof(double *));
97 vb->lpc=malloc(vb->floor_channels*sizeof(double *));
98 vb->amp=malloc(vb->floor_channels*sizeof(double));
99 for(i=0;i<vb->floor_channels;i++){
100 vb->lsp[i]=malloc(vb->floor_storage*sizeof(double));
101 vb->lpc[i]=malloc(vb->floor_storage*sizeof(double));
107 int vorbis_block_clear(vorbis_block *vb){
110 for(i=0;i<vb->pcm_channels;i++)
115 for(i=0;i<vb->mult_channels;i++)
119 memset(vb,0,sizeof(vorbis_block));
123 /* Analysis side code, but directly related to blocking. Thus it's
124 here and not in analysis.c (which is for analysis transforms only).
125 The init is here because some of it is shared */
127 static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi){
128 memset(v,0,sizeof(vorbis_dsp_state));
130 memcpy(&v->vi,vi,sizeof(vorbis_info));
131 _ve_envelope_init(&v->ve,vi->envelopesa);
132 mdct_init(&v->vm[0],vi->smallblock);
133 mdct_init(&v->vm[1],vi->largeblock);
135 v->samples_per_envelope_step=vi->envelopesa;
136 v->block_size[0]=vi->smallblock;
137 v->block_size[1]=vi->largeblock;
139 v->window[0][0][0]=_vorbis_window(v->block_size[0],
140 v->block_size[0]/2,v->block_size[0]/2);
141 v->window[0][0][1]=v->window[0][0][0];
142 v->window[0][1][0]=v->window[0][0][0];
143 v->window[0][1][1]=v->window[0][0][0];
145 v->window[1][0][0]=_vorbis_window(v->block_size[1],
146 v->block_size[0]/2,v->block_size[0]/2);
147 v->window[1][0][1]=_vorbis_window(v->block_size[1],
148 v->block_size[0]/2,v->block_size[1]/2);
149 v->window[1][1][0]=_vorbis_window(v->block_size[1],
150 v->block_size[1]/2,v->block_size[0]/2);
151 v->window[1][1][1]=_vorbis_window(v->block_size[1],
152 v->block_size[1]/2,v->block_size[1]/2);
154 /* initialize the storage vectors to a decent size greater than the
157 v->pcm_storage=8192; /* we'll assume later that we have
158 a minimum of twice the blocksize of
159 accumulated samples in analysis */
160 v->pcm_channels=v->vi.channels=vi->channels;
161 v->pcm=malloc(vi->channels*sizeof(double *));
162 v->pcmret=malloc(vi->channels*sizeof(double *));
165 for(i=0;i<vi->channels;i++)
166 v->pcm[i]=calloc(v->pcm_storage,sizeof(double));
169 /* Initialize the envelope multiplier storage */
172 v->envelope_storage=v->pcm_storage/v->samples_per_envelope_step;
173 v->envelope_channels=vi->envelopech;
174 v->multipliers=calloc(v->envelope_channels,sizeof(double *));
177 for(i=0;i<v->envelope_channels;i++){
178 v->multipliers[i]=calloc(v->envelope_storage,sizeof(double));
183 /* all 1 (large block) or 0 (small block) */
184 /* explicitly set for the sake of clarity */
185 v->lW=0; /* previous window size */
186 v->W=0; /* current window size */
188 /* all vector indexes; multiples of samples_per_envelope_step */
189 v->centerW=v->block_size[1]/2;
191 v->pcm_current=v->centerW;
192 v->envelope_current=v->centerW/v->samples_per_envelope_step;
196 /* arbitrary settings and spec-mandated numbers get filled in here */
197 int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
198 _vds_shared_init(v,vi);
200 /* Yes, wasteful to have four lookups. This will get collapsed once
201 things crystallize */
202 lpc_init(&v->vl[0],vi->smallblock/2,vi->smallblock/2,
203 vi->floororder,vi->flooroctaves,1);
204 lpc_init(&v->vl[1],vi->largeblock/2,vi->largeblock/2,
205 vi->floororder,vi->flooroctaves,1);
207 lpc_init(&v->vbal[0],vi->smallblock/2,256,
208 vi->balanceorder,vi->balanceoctaves,1);
209 lpc_init(&v->vbal[1],vi->largeblock/2,256,
210 vi->balanceorder,vi->balanceoctaves,1);
215 void vorbis_dsp_clear(vorbis_dsp_state *v){
219 if(v->window[0][0][0])free(v->window[0][0][0]);
222 if(v->window[1][j][k])free(v->window[1][j][k]);
224 for(i=0;i<v->pcm_channels;i++)
225 if(v->pcm[i])free(v->pcm[i]);
227 if(v->pcmret)free(v->pcmret);
230 for(i=0;i<v->envelope_channels;i++)
231 if(v->multipliers[i])free(v->multipliers[i]);
232 free(v->multipliers);
234 _ve_envelope_clear(&v->ve);
235 mdct_clear(&v->vm[0]);
236 mdct_clear(&v->vm[1]);
237 lpc_clear(&v->vl[0]);
238 lpc_clear(&v->vl[1]);
239 lpc_clear(&v->vbal[0]);
240 lpc_clear(&v->vbal[1]);
241 memset(v,0,sizeof(vorbis_dsp_state));
245 double **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
248 /* Do we have enough storage space for the requested buffer? If not,
249 expand the PCM (and envelope) storage */
251 if(v->pcm_current+vals>=v->pcm_storage){
252 v->pcm_storage=v->pcm_current+vals*2;
253 v->envelope_storage=v->pcm_storage/v->samples_per_envelope_step;
255 for(i=0;i<v->pcm_channels;i++){
256 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
258 for(i=0;i<v->envelope_channels;i++){
259 v->multipliers[i]=realloc(v->multipliers[i],
260 v->envelope_storage*sizeof(double));
264 for(i=0;i<v->pcm_channels;i++)
265 v->pcmret[i]=v->pcm[i]+v->pcm_current;
270 /* call with val<=0 to set eof */
272 int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
274 /* We're encoding the end of the stream. Just make sure we have
275 [at least] a full block of zeroes at the end. */
278 vorbis_analysis_buffer(v,v->block_size[1]*2);
279 v->eofflag=v->pcm_current;
280 v->pcm_current+=v->block_size[1]*2;
281 for(i=0;i<v->pcm_channels;i++)
282 memset(v->pcm[i]+v->eofflag,0,
283 (v->pcm_current-v->eofflag)*sizeof(double));
286 if(v->pcm_current+vals>v->pcm_storage)
289 v->pcm_current+=vals;
294 /* do the deltas, envelope shaping, pre-echo and determine the size of
295 the next block on which to continue analysis */
296 int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
298 long beginW=v->centerW-v->block_size[v->W]/2,centerNext;
299 long beginM=beginW/v->samples_per_envelope_step;
301 /* check to see if we're done... */
302 if(v->eofflag==-1)return(0);
304 /* if we have any unfilled envelope blocks for which we have PCM
305 data, fill them up in before proceeding. */
307 if(v->pcm_current/v->samples_per_envelope_step>v->envelope_current){
308 /* This generates the multipliers, but does not sparsify the vector.
309 That's done by block before coding */
310 _ve_envelope_multipliers(v);
313 /* By our invariant, we have lW, W and centerW set. Search for
314 the next boundary so we can determine nW (the next window size)
315 which lets us compute the shape of the current block's window */
317 /* overconserve for now; any block with a non-placeholder multiplier
318 should be minimal size. We can be greedy and only look at nW size */
320 if(v->vi.smallblock<v->vi.largeblock){
323 /* this is a long window; we start the search forward of centerW
324 because that's the fastest we could react anyway */
325 i=v->centerW+v->block_size[1]/4-v->block_size[0]/4;
327 /* short window. Search from centerW */
329 i/=v->samples_per_envelope_step;
331 for(;i<v->envelope_current;i++){
332 for(j=0;j<v->envelope_channels;j++)
333 if(v->multipliers[j][i-1]*v->vi.preecho_thresh<
334 v->multipliers[j][i])break;
335 if(j<v->envelope_channels)break;
338 if(i<v->envelope_current){
339 /* Ooo, we hit a multiplier. Is it beyond the boundary to make the
340 upcoming block large ? */
343 largebound=v->centerW+v->block_size[1];
345 largebound=v->centerW+v->block_size[0]/4+v->block_size[1]*3/4;
346 largebound/=v->samples_per_envelope_step;
354 /* Assume maximum; if the block is incomplete given current
355 buffered data, this will be detected below */
362 /* Do we actually have enough data *now* for the next block? The
363 reason to check is that if we had no multipliers, that could
364 simply been due to running out of data. In that case, we don;t
365 know the size of the next block for sure and we need that now to
366 figure out the window shape of this block */
368 centerNext=v->centerW+v->block_size[v->W]/4+v->block_size[v->nW]/4;
371 long blockbound=centerNext+v->block_size[v->nW]/2;
372 if(v->pcm_current<blockbound)return(0); /* not enough data yet */
375 /* fill in the block */
381 vb->pcmend=v->block_size[v->W];
382 vb->multend=vb->pcmend / v->samples_per_envelope_step;
384 if(vb->floor_channels!=v->vi.floorch ||
385 vb->floor_storage!=v->vi.floororder ||
386 v->pcm_channels!=vb->pcm_channels ||
387 v->block_size[1]!=vb->pcm_storage ||
388 v->envelope_channels!=vb->mult_channels){
390 /* Storage not initialized or initilized for some other codec
391 instance with different settings */
393 vorbis_block_clear(vb);
394 vorbis_block_init(v,vb);
397 /* copy the vectors */
398 for(i=0;i<v->pcm_channels;i++)
399 memcpy(vb->pcm[i],v->pcm[i]+beginW,v->block_size[v->W]*sizeof(double));
400 for(i=0;i<v->envelope_channels;i++)
401 memcpy(vb->mult[i],v->multipliers[i]+beginM,v->block_size[v->W]/
402 v->samples_per_envelope_step*sizeof(double));
404 vb->frameno=v->frame;
406 /* handle eof detection: eof==0 means that we've not yet received EOF
407 eof>0 marks the last 'real' sample in pcm[]
408 eof<0 'no more to do'; doesn't get here */
411 if(v->centerW>=v->eofflag){
417 /* advance storage vectors and clean up */
419 int new_centerNext=v->block_size[1]/2;
420 int movementW=centerNext-new_centerNext;
421 int movementM=movementW/v->samples_per_envelope_step;
423 /* the multipliers and pcm stay synced up because the blocksizes
424 must be multiples of samples_per_envelope_step (minimum
427 for(i=0;i<v->pcm_channels;i++)
428 memmove(v->pcm[i],v->pcm[i]+movementW,
429 (v->pcm_current-movementW)*sizeof(double));
431 for(i=0;i<v->envelope_channels;i++){
432 memmove(v->multipliers[i],v->multipliers[i]+movementM,
433 (v->envelope_current-movementM)*sizeof(double));
436 v->pcm_current-=movementW;
437 v->envelope_current-=movementM;
441 v->centerW=new_centerNext;
444 v->samples+=movementW;
447 v->eofflag-=movementW;
454 int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
455 int temp=vi->envelopech;
456 vi->envelopech=0; /* we don't need multiplier buffering in syn */
457 _vds_shared_init(v,vi);
460 /* Yes, wasteful to have four lookups. This will get collapsed once
461 things crystallize */
462 lpc_init(&v->vl[0],vi->smallblock/2,vi->smallblock/2,
463 vi->floororder,vi->flooroctaves,0);
464 lpc_init(&v->vl[1],vi->largeblock/2,vi->largeblock/2,
465 vi->floororder,vi->flooroctaves,0);
466 lpc_init(&v->vbal[0],vi->smallblock/2,256,
467 vi->balanceorder,vi->balanceoctaves,0);
468 lpc_init(&v->vbal[1],vi->largeblock/2,256,
469 vi->balanceorder,vi->balanceoctaves,0);
472 /* Adjust centerW to allow an easier mechanism for determining output */
473 v->pcm_returned=v->centerW;
474 v->centerW-= v->block_size[v->W]/4+v->block_size[v->lW]/4;
478 /* Unike in analysis, the window is only partially applied. Envelope
479 is previously applied (the whole envelope, if any, is shipped in
482 int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
484 /* Shift out any PCM that we returned previously */
486 if(v->pcm_returned && v->centerW>v->block_size[1]/2){
488 /* don't shift too much; we need to have a minimum PCM buffer of
491 int shift=v->centerW-v->block_size[1]/2;
492 shift=(v->pcm_returned<shift?v->pcm_returned:shift);
494 v->pcm_current-=shift;
496 v->pcm_returned-=shift;
500 for(i=0;i<v->pcm_channels;i++)
501 memmove(v->pcm[i],v->pcm[i]+shift,
502 v->pcm_current*sizeof(double));
507 int sizeW=v->block_size[vb->W];
508 int centerW=v->centerW+v->block_size[vb->lW]/4+sizeW/4;
509 int beginW=centerW-sizeW/2;
510 int endW=beginW+sizeW;
516 /* Do we have enough PCM storage for the block? */
517 if(endW>v->pcm_storage){
518 /* expand the PCM storage */
520 v->pcm_storage=endW+v->block_size[1];
522 for(i=0;i<v->pcm_channels;i++)
523 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
530 endSl=v->block_size[0]/2;
533 beginSl=v->block_size[1]/4-v->block_size[vb->lW]/4;
534 endSl=beginSl+v->block_size[vb->lW]/2;
538 window=v->window[vb->W][0][vb->lW]+v->block_size[vb->W]/2;
540 for(j=0;j<v->pcm_channels;j++){
541 double *pcm=v->pcm[j]+beginW;
543 /* the add section */
544 for(i=beginSl;i<endSl;i++)
545 pcm[i]=pcm[i]*window[i]+vb->pcm[j][i];
546 /* the remaining section */
548 pcm[i]=vb->pcm[j][i];
551 /* Update, cleanup */
556 if(vb->eofflag)v->eofflag=1;
561 int vorbis_synthesis_pcmout(vorbis_dsp_state *v,double ***pcm){
562 if(v->pcm_returned<v->centerW){
564 for(i=0;i<v->pcm_channels;i++)
565 v->pcmret[i]=v->pcm[i]+v->pcm_returned;
567 return(v->centerW-v->pcm_returned);
572 int vorbis_synthesis_read(vorbis_dsp_state *v,int bytes){
573 if(bytes && v->pcm_returned+bytes>v->centerW)return(-1);
574 v->pcm_returned+=bytes;