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 vorbis_info *vi=v->vi;
79 memset(vb,0,sizeof(vorbis_block));
82 vb->pcm_storage=vi->blocksize[1];
83 vb->pcm_channels=vi->channels;
84 vb->mult_storage=vi->blocksize[1]/vi->envelopesa;
85 vb->mult_channels=vi->envelopech;
86 vb->floor_channels=vi->floorch;
87 vb->floor_storage=max(vi->floororder[0],vi->floororder[1]);
89 vb->pcm=malloc(vb->pcm_channels*sizeof(double *));
90 for(i=0;i<vb->pcm_channels;i++)
91 vb->pcm[i]=malloc(vb->pcm_storage*sizeof(double));
93 vb->mult=malloc(vb->mult_channels*sizeof(double *));
94 for(i=0;i<vb->mult_channels;i++)
95 vb->mult[i]=malloc(vb->mult_storage*sizeof(double));
97 vb->lsp=malloc(vb->floor_channels*sizeof(double *));
98 vb->lpc=malloc(vb->floor_channels*sizeof(double *));
99 vb->amp=malloc(vb->floor_channels*sizeof(double));
100 for(i=0;i<vb->floor_channels;i++){
101 vb->lsp[i]=malloc(vb->floor_storage*sizeof(double));
102 vb->lpc[i]=malloc(vb->floor_storage*sizeof(double));
105 _oggpack_writeinit(&vb->opb);
110 int vorbis_block_clear(vorbis_block *vb){
113 for(i=0;i<vb->pcm_channels;i++)
118 for(i=0;i<vb->mult_channels;i++)
122 if(vb->vd->analysisp)
123 _oggpack_writeclear(&vb->opb);
125 memset(vb,0,sizeof(vorbis_block));
129 /* Analysis side code, but directly related to blocking. Thus it's
130 here and not in analysis.c (which is for analysis transforms only).
131 The init is here because some of it is shared */
133 static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi){
134 memset(v,0,sizeof(vorbis_dsp_state));
137 _ve_envelope_init(&v->ve,vi->envelopesa);
138 mdct_init(&v->vm[0],vi->blocksize[0]);
139 mdct_init(&v->vm[1],vi->blocksize[1]);
141 v->window[0][0][0]=_vorbis_window(vi->blocksize[0],
142 vi->blocksize[0]/2,vi->blocksize[0]/2);
143 v->window[0][0][1]=v->window[0][0][0];
144 v->window[0][1][0]=v->window[0][0][0];
145 v->window[0][1][1]=v->window[0][0][0];
147 v->window[1][0][0]=_vorbis_window(vi->blocksize[1],
148 vi->blocksize[0]/2,vi->blocksize[0]/2);
149 v->window[1][0][1]=_vorbis_window(vi->blocksize[1],
150 vi->blocksize[0]/2,vi->blocksize[1]/2);
151 v->window[1][1][0]=_vorbis_window(vi->blocksize[1],
152 vi->blocksize[1]/2,vi->blocksize[0]/2);
153 v->window[1][1][1]=_vorbis_window(vi->blocksize[1],
154 vi->blocksize[1]/2,vi->blocksize[1]/2);
156 /* initialize the storage vectors to a decent size greater than the
159 v->pcm_storage=8192; /* we'll assume later that we have
160 a minimum of twice the blocksize of
161 accumulated samples in analysis */
162 v->pcm=malloc(vi->channels*sizeof(double *));
163 v->pcmret=malloc(vi->channels*sizeof(double *));
166 for(i=0;i<vi->channels;i++)
167 v->pcm[i]=calloc(v->pcm_storage,sizeof(double));
170 /* Initialize the envelope multiplier storage */
173 v->envelope_storage=v->pcm_storage/vi->envelopesa;
174 v->multipliers=calloc(vi->envelopech,sizeof(double *));
177 for(i=0;i<vi->envelopech;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=vi->blocksize[1]/2;
191 v->pcm_current=v->centerW;
192 v->envelope_current=v->centerW/vi->envelopesa;
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 /* the coder init is different for read/write */
203 /* Yes, wasteful to have four lookups. This will get collapsed once
204 things crystallize */
205 lpc_init(&v->vl[0],vi->blocksize[0]/2,vi->blocksize[0]/2,
206 vi->floororder[0],vi->flooroctaves[0],1);
207 lpc_init(&v->vl[1],vi->blocksize[1]/2,vi->blocksize[1]/2,
208 vi->floororder[0],vi->flooroctaves[0],1);
210 /*lpc_init(&v->vbal[0],vi->blocksize[0]/2,256,
211 vi->balanceorder,vi->balanceoctaves,1);
212 lpc_init(&v->vbal[1],vi->blocksize[1]/2,256,
213 vi->balanceorder,vi->balanceoctaves,1);*/
218 void vorbis_dsp_clear(vorbis_dsp_state *v){
221 vorbis_info *vi=v->vi;
223 if(v->window[0][0][0])free(v->window[0][0][0]);
226 if(v->window[1][j][k])free(v->window[1][j][k]);
228 for(i=0;i<vi->channels;i++)
229 if(v->pcm[i])free(v->pcm[i]);
231 if(v->pcmret)free(v->pcmret);
234 for(i=0;i<vi->envelopech;i++)
235 if(v->multipliers[i])free(v->multipliers[i]);
236 free(v->multipliers);
238 _ve_envelope_clear(&v->ve);
239 mdct_clear(&v->vm[0]);
240 mdct_clear(&v->vm[1]);
241 lpc_clear(&v->vl[0]);
242 lpc_clear(&v->vl[1]);
243 /*lpc_clear(&v->vbal[0]);
244 lpc_clear(&v->vbal[1]);*/
245 memset(v,0,sizeof(vorbis_dsp_state));
249 double **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
251 vorbis_info *vi=v->vi;
253 /* Do we have enough storage space for the requested buffer? If not,
254 expand the PCM (and envelope) storage */
256 if(v->pcm_current+vals>=v->pcm_storage){
257 v->pcm_storage=v->pcm_current+vals*2;
258 v->envelope_storage=v->pcm_storage/v->vi->envelopesa;
260 for(i=0;i<vi->channels;i++){
261 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
263 for(i=0;i<vi->envelopech;i++){
264 v->multipliers[i]=realloc(v->multipliers[i],
265 v->envelope_storage*sizeof(double));
269 for(i=0;i<vi->channels;i++)
270 v->pcmret[i]=v->pcm[i]+v->pcm_current;
275 /* call with val<=0 to set eof */
277 int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
278 vorbis_info *vi=v->vi;
280 /* We're encoding the end of the stream. Just make sure we have
281 [at least] a full block of zeroes at the end. */
284 vorbis_analysis_buffer(v,v->vi->blocksize[1]*2);
285 v->eofflag=v->pcm_current;
286 v->pcm_current+=v->vi->blocksize[1]*2;
287 for(i=0;i<vi->channels;i++)
288 memset(v->pcm[i]+v->eofflag,0,
289 (v->pcm_current-v->eofflag)*sizeof(double));
292 if(v->pcm_current+vals>v->pcm_storage)
295 v->pcm_current+=vals;
300 /* do the deltas, envelope shaping, pre-echo and determine the size of
301 the next block on which to continue analysis */
302 int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
304 vorbis_info *vi=v->vi;
305 long beginW=v->centerW-vi->blocksize[v->W]/2,centerNext;
306 long beginM=beginW/vi->envelopesa;
308 /* check to see if we're done... */
309 if(v->eofflag==-1)return(0);
311 /* if we have any unfilled envelope blocks for which we have PCM
312 data, fill them up in before proceeding. */
314 if(v->pcm_current/vi->envelopesa>v->envelope_current){
315 /* This generates the multipliers, but does not sparsify the vector.
316 That's done by block before coding */
317 _ve_envelope_multipliers(v);
320 /* By our invariant, we have lW, W and centerW set. Search for
321 the next boundary so we can determine nW (the next window size)
322 which lets us compute the shape of the current block's window */
324 /* overconserve for now; any block with a non-placeholder multiplier
325 should be minimal size. We can be greedy and only look at nW size */
327 if(vi->blocksize[0]<vi->blocksize[1]){
330 /* this is a long window; we start the search forward of centerW
331 because that's the fastest we could react anyway */
332 i=v->centerW+vi->blocksize[1]/4-vi->blocksize[0]/4;
334 /* short window. Search from centerW */
338 for(;i<v->envelope_current;i++){
339 for(j=0;j<vi->envelopech;j++)
340 if(v->multipliers[j][i-1]*vi->preecho_thresh<
341 v->multipliers[j][i])break;
342 if(j<vi->envelopech)break;
345 if(i<v->envelope_current){
346 /* Ooo, we hit a multiplier. Is it beyond the boundary to make the
347 upcoming block large ? */
350 largebound=v->centerW+vi->blocksize[1];
352 largebound=v->centerW+vi->blocksize[0]/4+vi->blocksize[1]*3/4;
353 largebound/=vi->envelopesa;
361 /* Assume maximum; if the block is incomplete given current
362 buffered data, this will be detected below */
369 /* Do we actually have enough data *now* for the next block? The
370 reason to check is that if we had no multipliers, that could
371 simply been due to running out of data. In that case, we don;t
372 know the size of the next block for sure and we need that now to
373 figure out the window shape of this block */
375 centerNext=v->centerW+vi->blocksize[v->W]/4+vi->blocksize[v->nW]/4;
378 long blockbound=centerNext+vi->blocksize[v->nW]/2;
379 if(v->pcm_current<blockbound)return(0); /* not enough data yet */
382 /* fill in the block */
388 vb->pcmend=vi->blocksize[v->W];
389 vb->multend=vb->pcmend / vi->envelopesa;
391 /* copy the vectors */
392 for(i=0;i<vi->channels;i++)
393 memcpy(vb->pcm[i],v->pcm[i]+beginW,vi->blocksize[v->W]*sizeof(double));
394 for(i=0;i<vi->envelopech;i++)
395 memcpy(vb->mult[i],v->multipliers[i]+beginM,vi->blocksize[v->W]/
396 vi->envelopesa*sizeof(double));
398 vb->frameno=v->frame;
400 /* handle eof detection: eof==0 means that we've not yet received EOF
401 eof>0 marks the last 'real' sample in pcm[]
402 eof<0 'no more to do'; doesn't get here */
405 if(v->centerW>=v->eofflag){
411 /* advance storage vectors and clean up */
413 int new_centerNext=vi->blocksize[1]/2;
414 int movementW=centerNext-new_centerNext;
415 int movementM=movementW/vi->envelopesa;
417 /* the multipliers and pcm stay synced up because the blocksizes
418 must be multiples of samples_per_envelope_step (minimum
421 for(i=0;i<vi->channels;i++)
422 memmove(v->pcm[i],v->pcm[i]+movementW,
423 (v->pcm_current-movementW)*sizeof(double));
425 for(i=0;i<vi->envelopech;i++){
426 memmove(v->multipliers[i],v->multipliers[i]+movementM,
427 (v->envelope_current-movementM)*sizeof(double));
430 v->pcm_current-=movementW;
431 v->envelope_current-=movementM;
435 v->centerW=new_centerNext;
438 v->samples+=movementW;
441 v->eofflag-=movementW;
448 int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
449 int temp=vi->envelopech;
450 vi->envelopech=0; /* we don't need multiplier buffering in syn */
451 _vds_shared_init(v,vi);
454 /* Yes, wasteful to have four lookups. This will get collapsed once
455 things crystallize */
456 lpc_init(&v->vl[0],vi->blocksize[0]/2,vi->blocksize[0]/2,
457 vi->floororder[0],vi->flooroctaves[0],0);
458 lpc_init(&v->vl[1],vi->blocksize[1]/2,vi->blocksize[1]/2,
459 vi->floororder[1],vi->flooroctaves[1],0);
460 /*lpc_init(&v->vbal[0],vi->blocksize[0]/2,256,
461 vi->balanceorder,vi->balanceoctaves,0);
462 lpc_init(&v->vbal[1],vi->blocksize[1]/2,256,
463 vi->balanceorder,vi->balanceoctaves,0);*/
466 /* Adjust centerW to allow an easier mechanism for determining output */
467 v->pcm_returned=v->centerW;
468 v->centerW-= vi->blocksize[v->W]/4+vi->blocksize[v->lW]/4;
472 /* Unike in analysis, the window is only partially applied. Envelope
473 is previously applied (the whole envelope, if any, is shipped in
476 int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
477 vorbis_info *vi=v->vi;
479 /* Shift out any PCM that we returned previously */
481 if(v->pcm_returned && v->centerW>vi->blocksize[1]/2){
483 /* don't shift too much; we need to have a minimum PCM buffer of
486 int shift=v->centerW-vi->blocksize[1]/2;
487 shift=(v->pcm_returned<shift?v->pcm_returned:shift);
489 v->pcm_current-=shift;
491 v->pcm_returned-=shift;
495 for(i=0;i<vi->channels;i++)
496 memmove(v->pcm[i],v->pcm[i]+shift,
497 v->pcm_current*sizeof(double));
505 int sizeW=vi->blocksize[v->W];
506 int centerW=v->centerW+vi->blocksize[v->lW]/4+sizeW/4;
507 int beginW=centerW-sizeW/2;
508 int endW=beginW+sizeW;
514 /* Do we have enough PCM storage for the block? */
515 if(endW>v->pcm_storage){
516 /* expand the PCM storage */
518 v->pcm_storage=endW+vi->blocksize[1];
520 for(i=0;i<vi->channels;i++)
521 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
528 endSl=vi->blocksize[0]/2;
531 beginSl=vi->blocksize[1]/4-vi->blocksize[v->lW]/4;
532 endSl=beginSl+vi->blocksize[v->lW]/2;
536 window=v->window[v->W][0][v->lW]+vi->blocksize[v->W]/2;
538 for(j=0;j<vi->channels;j++){
539 double *pcm=v->pcm[j]+beginW;
541 /* the add section */
542 for(i=beginSl;i<endSl;i++)
543 pcm[i]=pcm[i]*window[i]+vb->pcm[j][i];
544 /* the remaining section */
546 pcm[i]=vb->pcm[j][i];
549 /* Update, cleanup */
554 if(vb->eofflag)v->eofflag=1;
559 int vorbis_synthesis_pcmout(vorbis_dsp_state *v,double ***pcm){
560 vorbis_info *vi=v->vi;
561 if(v->pcm_returned<v->centerW){
563 for(i=0;i<vi->channels;i++)
564 v->pcmret[i]=v->pcm[i]+v->pcm_returned;
566 return(v->centerW-v->pcm_returned);
571 int vorbis_synthesis_read(vorbis_dsp_state *v,int bytes){
572 if(bytes && v->pcm_returned+bytes>v->centerW)return(-1);
573 v->pcm_returned+=bytes;