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-2000 *
9 * by 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 last mod: $Id: block.c,v 1.23 2000/01/22 13:28:15 xiphmont Exp $
17 Handle windowing, overlap-add, etc of the PCM vectors. This is made
18 more amusing by Vorbis' current two allowed block sizes.
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.
24 ********************************************************************/
29 #include "vorbis/codec.h"
37 #include "bookinternal.h"
39 static int ilog2(unsigned int v){
48 /* pcm accumulator examples (not exhaustive):
50 <-------------- lW ---------------->
51 <--------------- W ---------------->
52 : .....|..... _______________ |
53 : .''' | '''_--- | |\ |
54 :.....''' |_____--- '''......| | \_______|
55 :.................|__________________|_______|__|______|
56 |<------ Sl ------>| > Sr < |endW
57 |beginSl |endSl | |endSr
58 |beginW |endlW |beginSr
62 <--------------- W ---------------->
63 | | .. ______________ |
65 |___.'___/`. | ---_____|
66 |_______|__|_______|_________________|
67 | >|Sl|< |<------ Sr ----->|endW
68 | | |endSl |beginSr |endSr
70 mult[0] |beginSl mult[n]
72 <-------------- lW ----------------->
74 : .............. ___ | |
76 :.....''' |/`....\|...|
77 :.........................|___|___|___|
86 /* block abstraction setup *********************************************/
92 int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
93 memset(vb,0,sizeof(vorbis_block));
98 _oggpack_writeinit(&vb->opb);
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 */
108 struct alloc_chain *link=malloc(sizeof(struct alloc_chain));
109 vb->totaluse+=vb->localtop;
111 link->ptr=vb->localstore;
114 /* highly conservative */
115 vb->localalloc=bytes;
116 vb->localstore=malloc(vb->localalloc);
119 return(vb->localstore+vb->localtop);
122 /* reap the chain, pull the ripcord */
123 void _vorbis_block_ripcord(vorbis_block *vb){
125 struct alloc_chain *reap=vb->reap;
127 struct alloc_chain *next=reap->next;
129 memset(reap,0,sizeof(struct alloc_chain));
133 /* consolidate storage */
135 vb->localstore=realloc(vb->localstore,vb->totaluse+vb->localalloc);
136 vb->localalloc+=vb->totaluse;
140 /* pull the ripcord */
145 int vorbis_block_clear(vorbis_block *vb){
147 if(vb->vd->analysisp)
148 _oggpack_writeclear(&vb->opb);
149 _vorbis_block_ripcord(vb);
150 if(vb->localstore)free(vb->localstore);
152 memset(vb,0,sizeof(vorbis_block));
156 /* Analysis side code, but directly related to blocking. Thus it's
157 here and not in analysis.c (which is for analysis transforms only).
158 The init is here because some of it is shared */
160 static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi){
162 memset(v,0,sizeof(vorbis_dsp_state));
165 v->modebits=ilog2(vi->modes);
167 v->transform[0]=calloc(VI_TRANSFORMB,sizeof(vorbis_look_transform *));
168 v->transform[1]=calloc(VI_TRANSFORMB,sizeof(vorbis_look_transform *));
170 /* MDCT is tranform 0 */
172 v->transform[0][0]=calloc(1,sizeof(mdct_lookup));
173 v->transform[1][0]=calloc(1,sizeof(mdct_lookup));
174 mdct_init(v->transform[0][0],vi->blocksizes[0]);
175 mdct_init(v->transform[1][0],vi->blocksizes[1]);
177 v->window[0][0][0]=calloc(VI_WINDOWB,sizeof(double *));
178 v->window[0][0][1]=v->window[0][0][0];
179 v->window[0][1][0]=v->window[0][0][0];
180 v->window[0][1][1]=v->window[0][0][0];
181 v->window[1][0][0]=calloc(VI_WINDOWB,sizeof(double *));
182 v->window[1][0][1]=calloc(VI_WINDOWB,sizeof(double *));
183 v->window[1][1][0]=calloc(VI_WINDOWB,sizeof(double *));
184 v->window[1][1][1]=calloc(VI_WINDOWB,sizeof(double *));
186 for(i=0;i<VI_WINDOWB;i++){
187 v->window[0][0][0][i]=
188 _vorbis_window(i,vi->blocksizes[0],vi->blocksizes[0]/2,vi->blocksizes[0]/2);
189 v->window[1][0][0][i]=
190 _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[0]/2,vi->blocksizes[0]/2);
191 v->window[1][0][1][i]=
192 _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[0]/2,vi->blocksizes[1]/2);
193 v->window[1][1][0][i]=
194 _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[1]/2,vi->blocksizes[0]/2);
195 v->window[1][1][1][i]=
196 _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[1]/2,vi->blocksizes[1]/2);
199 /* initialize all the mapping/backend lookups */
200 v->mode=calloc(vi->modes,sizeof(vorbis_look_mapping *));
201 for(i=0;i<vi->modes;i++){
202 int maptype=vi->mode_param[i]->mapping;
203 v->mode[i]=_mapping_P[maptype]->look(vi,vi->mode_param[i],
204 vi->map_param[maptype]);
207 /* finish the codebooks */
208 v->fullbooks=calloc(vi->books,sizeof(codebook));
209 for(i=0;i<vi->books;i++)
210 vorbis_book_finish(v->fullbooks+i,vi->book_param[i]);
212 /* initialize the storage vectors to a decent size greater than the
215 v->pcm_storage=8192; /* we'll assume later that we have
216 a minimum of twice the blocksize of
217 accumulated samples in analysis */
218 v->pcm=malloc(vi->channels*sizeof(double *));
219 v->pcmret=malloc(vi->channels*sizeof(double *));
222 for(i=0;i<vi->channels;i++)
223 v->pcm[i]=calloc(v->pcm_storage,sizeof(double));
226 /* all 1 (large block) or 0 (small block) */
227 /* explicitly set for the sake of clarity */
228 v->lW=0; /* previous window size */
229 v->W=0; /* current window size */
231 /* all vector indexes; multiples of samples_per_envelope_step */
232 v->centerW=vi->blocksizes[1]/2;
234 v->pcm_current=v->centerW;
238 /* arbitrary settings and spec-mandated numbers get filled in here */
239 int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
241 _vds_shared_init(v,vi);
243 /* Initialize the envelope multiplier storage */
245 v->envelope_storage=v->pcm_storage/vi->envelopesa;
246 v->multipliers=calloc(v->envelope_storage,sizeof(double));
247 _ve_envelope_init(&v->ve,vi->envelopesa);
249 /* the coder init is different for read/write */
252 v->envelope_current=v->centerW/vi->envelopesa;
256 void vorbis_dsp_clear(vorbis_dsp_state *v){
259 vorbis_info *vi=v->vi;
261 for(i=0;i<VI_WINDOWB;i++){
262 if(v->window[0][0][0][i])free(v->window[0][0][0][i]);
265 if(v->window[1][j][k][i])free(v->window[1][j][k][i]);
268 for(i=0;i<vi->channels;i++)
269 if(v->pcm[i])free(v->pcm[i]);
271 if(v->pcmret)free(v->pcmret);
273 if(v->multipliers)free(v->multipliers);
275 _ve_envelope_clear(&v->ve);
276 mdct_clear(v->transform[0][0]);
277 mdct_clear(v->transform[1][0]);
278 free(v->transform[0][0]);
279 free(v->transform[1][0]);
281 free(v->transform[0]);
282 free(v->transform[1]);
284 /* free mode lookups */
285 for(i=0;i<vi->modes;i++){
286 int maptype=vi->mode_param[i]->mapping;
287 _mapping_P[maptype]->free_look(v->mode[i]);
289 if(v->mode)free(v->mode);
292 for(i=0;i<vi->books;i++)
293 vorbis_book_clear(v->fullbooks+i);
294 if(v->fullbooks)free(v->fullbooks);
296 /* free header, header1, header2 */
297 if(v->header)free(v->header);
298 if(v->header1)free(v->header1);
299 if(v->header2)free(v->header2);
301 memset(v,0,sizeof(vorbis_dsp_state));
305 double **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
307 vorbis_info *vi=v->vi;
309 /* free header, header1, header2 */
310 if(v->header)free(v->header);v->header=NULL;
311 if(v->header1)free(v->header1);v->header1=NULL;
312 if(v->header2)free(v->header2);v->header2=NULL;
314 /* Do we have enough storage space for the requested buffer? If not,
315 expand the PCM (and envelope) storage */
317 if(v->pcm_current+vals>=v->pcm_storage){
318 v->pcm_storage=v->pcm_current+vals*2;
319 v->envelope_storage=v->pcm_storage/v->vi->envelopesa;
321 for(i=0;i<vi->channels;i++){
322 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
324 v->multipliers=realloc(v->multipliers,v->envelope_storage*sizeof(double));
327 for(i=0;i<vi->channels;i++)
328 v->pcmret[i]=v->pcm[i]+v->pcm_current;
333 /* call with val<=0 to set eof */
335 int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
336 vorbis_info *vi=v->vi;
338 /* We're encoding the end of the stream. Just make sure we have
339 [at least] a full block of zeroes at the end. */
342 vorbis_analysis_buffer(v,v->vi->blocksizes[1]*2);
343 v->eofflag=v->pcm_current;
344 v->pcm_current+=v->vi->blocksizes[1]*2;
345 for(i=0;i<vi->channels;i++)
346 memset(v->pcm[i]+v->eofflag,0,
347 (v->pcm_current-v->eofflag)*sizeof(double));
350 if(v->pcm_current+vals>v->pcm_storage)
353 v->pcm_current+=vals;
358 /* do the deltas, envelope shaping, pre-echo and determine the size of
359 the next block on which to continue analysis */
360 int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
362 vorbis_info *vi=v->vi;
363 long beginW=v->centerW-vi->blocksizes[v->W]/2,centerNext;
365 /* check to see if we're done... */
366 if(v->eofflag==-1)return(0);
368 /* if we have any unfilled envelope blocks for which we have PCM
369 data, fill them up in before proceeding. */
371 if(v->pcm_current/vi->envelopesa>v->envelope_current){
372 /* This generates the multipliers, but does not sparsify the vector.
373 That's done by block before coding */
374 _ve_envelope_deltas(v);
377 /* By our invariant, we have lW, W and centerW set. Search for
378 the next boundary so we can determine nW (the next window size)
379 which lets us compute the shape of the current block's window */
381 if(vi->blocksizes[0]<vi->blocksizes[1]){
384 /* this is a long window; we start the search forward of centerW
385 because that's the fastest we could react anyway */
386 i=v->centerW+vi->blocksizes[1]/4-vi->blocksizes[0]/4;
388 /* short window. Search from centerW */
392 for(;i<v->envelope_current-1;i++){
393 /* Compare last with current; do we have an abrupt energy change? */
395 if(v->multipliers[i-1]*vi->preecho_thresh<
396 v->multipliers[i])break;
398 /* because the overlapping nature of the delta finding
399 'smears' the energy cliffs, also compare completely
400 unoverlapped areas just in case the plosive happened in an
403 if(v->multipliers[i-1]*vi->preecho_thresh<
404 v->multipliers[i+1])break;
408 if(i<v->envelope_current-1){
409 /* Ooo, we hit a multiplier. Is it beyond the boundary to make the
410 upcoming block large ? */
413 /* min boundary; nW large, next small */
414 largebound=v->centerW+vi->blocksizes[1]*3/4+vi->blocksizes[0]/4;
416 /* min boundary; nW large, next small */
417 largebound=v->centerW+vi->blocksizes[0]/2+vi->blocksizes[1]/2;
418 largebound/=vi->envelopesa;
426 /* Assume maximum; if the block is incomplete given current
427 buffered data, this will be detected below */
433 /* Do we actually have enough data *now* for the next block? The
434 reason to check is that if we had no multipliers, that could
435 simply been due to running out of data. In that case, we don't
436 know the size of the next block for sure and we need that now to
437 figure out the window shape of this block */
439 centerNext=v->centerW+vi->blocksizes[v->W]/4+vi->blocksizes[v->nW]/4;
442 /* center of next block + next block maximum right side. Note
443 that the next block needs an additional vi->envelopesa samples
444 to actually be written (for the last multiplier), but we didn't
445 need that to determine its size */
447 long blockbound=centerNext+vi->blocksizes[v->nW]/2;
448 if(v->pcm_current<blockbound)return(0); /* not enough data yet */
451 /* fill in the block. Note that for a short window, lW and nW are *short*
452 regardless of actual settings in the stream */
454 _vorbis_block_ripcord(vb);
465 vb->sequence=v->sequence;
466 vb->frameno=v->frameno;
467 vb->pcmend=vi->blocksizes[v->W];
469 /* copy the vectors; this uses the local storage in vb */
471 vb->pcm=_vorbis_block_alloc(vb,sizeof(double *)*vi->channels);
472 for(i=0;i<vi->channels;i++){
473 vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(double));
474 memcpy(vb->pcm[i],v->pcm[i]+beginW,vi->blocksizes[v->W]*sizeof(double));
478 /* handle eof detection: eof==0 means that we've not yet received EOF
479 eof>0 marks the last 'real' sample in pcm[]
480 eof<0 'no more to do'; doesn't get here */
483 if(v->centerW>=v->eofflag){
489 /* advance storage vectors and clean up */
491 int new_centerNext=vi->blocksizes[1]/2;
492 int movementW=centerNext-new_centerNext;
493 int movementM=movementW/vi->envelopesa;
495 /* the multipliers and pcm stay synced up because the blocksize
496 must be multiples of samples_per_envelope_step (minimum
499 v->pcm_current-=movementW;
500 v->envelope_current-=movementM;
502 for(i=0;i<vi->channels;i++)
503 memmove(v->pcm[i],v->pcm[i]+movementW,
504 v->pcm_current*sizeof(double));
506 memmove(v->multipliers,v->multipliers+movementM,
507 v->envelope_current*sizeof(double));
511 v->centerW=new_centerNext;
514 v->frameno+=movementW;
517 v->eofflag-=movementW;
524 int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
525 _vds_shared_init(v,vi);
527 /* Adjust centerW to allow an easier mechanism for determining output */
528 v->pcm_returned=v->centerW;
529 v->centerW-= vi->blocksizes[v->W]/4+vi->blocksizes[v->lW]/4;
533 /* Unike in analysis, the window is only partially applied for each
534 block. The time domain envelope is not yet handled at the point of
535 calling (as it relies on the previous block). */
537 int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
538 vorbis_info *vi=v->vi;
540 /* Shift out any PCM/multipliers that we returned previously */
541 /* centerW is currently the center of the last block added */
542 if(v->pcm_returned && v->centerW>vi->blocksizes[1]/2){
544 /* don't shift too much; we need to have a minimum PCM buffer of
547 int shiftPCM=v->centerW-vi->blocksizes[1]/2;
548 shiftPCM=(v->pcm_returned<shiftPCM?v->pcm_returned:shiftPCM);
550 v->pcm_current-=shiftPCM;
551 v->centerW-=shiftPCM;
552 v->pcm_returned-=shiftPCM;
556 for(i=0;i<vi->channels;i++)
557 memmove(v->pcm[i],v->pcm[i]+shiftPCM,
558 v->pcm_current*sizeof(double));
566 v->glue_bits+=vb->glue_bits;
567 v->time_bits+=vb->time_bits;
568 v->floor_bits+=vb->floor_bits;
569 v->res_bits+=vb->res_bits;
570 v->sequence=vb->sequence;
573 int sizeW=vi->blocksizes[v->W];
574 int centerW=v->centerW+vi->blocksizes[v->lW]/4+sizeW/4;
575 int beginW=centerW-sizeW/2;
576 int endW=beginW+sizeW;
581 /* Do we have enough PCM/mult storage for the block? */
582 if(endW>v->pcm_storage){
583 /* expand the storage */
584 v->pcm_storage=endW+vi->blocksizes[1];
586 for(i=0;i<vi->channels;i++)
587 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
590 /* overlap/add PCM */
595 endSl=vi->blocksizes[0]/2;
598 beginSl=vi->blocksizes[1]/4-vi->blocksizes[v->lW]/4;
599 endSl=beginSl+vi->blocksizes[v->lW]/2;
603 for(j=0;j<vi->channels;j++){
604 double *pcm=v->pcm[j]+beginW;
606 /* the overlap/add section */
607 for(i=beginSl;i<endSl;i++)
608 pcm[i]+=vb->pcm[j][i];
609 /* the remaining section */
611 pcm[i]=vb->pcm[j][i];
614 /* Update, cleanup */
619 if(vb->eofflag)v->eofflag=1;
624 int vorbis_synthesis_pcmout(vorbis_dsp_state *v,double ***pcm){
625 vorbis_info *vi=v->vi;
626 if(v->pcm_returned<v->centerW){
628 for(i=0;i<vi->channels;i++)
629 v->pcmret[i]=v->pcm[i]+v->pcm_returned;
631 return(v->centerW-v->pcm_returned);
636 int vorbis_synthesis_read(vorbis_dsp_state *v,int bytes){
637 if(bytes && v->pcm_returned+bytes>v->centerW)return(-1);
638 v->pcm_returned+=bytes;