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.26 2000/02/23 09:24:23 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);
120 void *ret=vb->localstore+vb->localtop;
126 /* reap the chain, pull the ripcord */
127 void _vorbis_block_ripcord(vorbis_block *vb){
129 struct alloc_chain *reap=vb->reap;
131 struct alloc_chain *next=reap->next;
133 memset(reap,0,sizeof(struct alloc_chain));
137 /* consolidate storage */
139 vb->localstore=realloc(vb->localstore,vb->totaluse+vb->localalloc);
140 vb->localalloc+=vb->totaluse;
144 /* pull the ripcord */
149 int vorbis_block_clear(vorbis_block *vb){
151 if(vb->vd->analysisp)
152 _oggpack_writeclear(&vb->opb);
153 _vorbis_block_ripcord(vb);
154 if(vb->localstore)free(vb->localstore);
156 memset(vb,0,sizeof(vorbis_block));
160 /* Analysis side code, but directly related to blocking. Thus it's
161 here and not in analysis.c (which is for analysis transforms only).
162 The init is here because some of it is shared */
164 static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){
166 memset(v,0,sizeof(vorbis_dsp_state));
169 v->modebits=ilog2(vi->modes);
171 v->transform[0]=calloc(VI_TRANSFORMB,sizeof(vorbis_look_transform *));
172 v->transform[1]=calloc(VI_TRANSFORMB,sizeof(vorbis_look_transform *));
174 /* MDCT is tranform 0 */
176 v->transform[0][0]=calloc(1,sizeof(mdct_lookup));
177 v->transform[1][0]=calloc(1,sizeof(mdct_lookup));
178 mdct_init(v->transform[0][0],vi->blocksizes[0]);
179 mdct_init(v->transform[1][0],vi->blocksizes[1]);
181 v->window[0][0][0]=calloc(VI_WINDOWB,sizeof(double *));
182 v->window[0][0][1]=v->window[0][0][0];
183 v->window[0][1][0]=v->window[0][0][0];
184 v->window[0][1][1]=v->window[0][0][0];
185 v->window[1][0][0]=calloc(VI_WINDOWB,sizeof(double *));
186 v->window[1][0][1]=calloc(VI_WINDOWB,sizeof(double *));
187 v->window[1][1][0]=calloc(VI_WINDOWB,sizeof(double *));
188 v->window[1][1][1]=calloc(VI_WINDOWB,sizeof(double *));
190 for(i=0;i<VI_WINDOWB;i++){
191 v->window[0][0][0][i]=
192 _vorbis_window(i,vi->blocksizes[0],vi->blocksizes[0]/2,vi->blocksizes[0]/2);
193 v->window[1][0][0][i]=
194 _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[0]/2,vi->blocksizes[0]/2);
195 v->window[1][0][1][i]=
196 _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[0]/2,vi->blocksizes[1]/2);
197 v->window[1][1][0][i]=
198 _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[1]/2,vi->blocksizes[0]/2);
199 v->window[1][1][1][i]=
200 _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[1]/2,vi->blocksizes[1]/2);
203 if(encp){ /* encode/decode differ here */
204 /* finish the codebooks */
205 v->fullbooks=calloc(vi->books,sizeof(codebook));
206 for(i=0;i<vi->books;i++)
207 vorbis_book_init_encode(v->fullbooks+i,vi->book_param[i]);
210 /* finish the codebooks */
211 v->fullbooks=calloc(vi->books,sizeof(codebook));
212 for(i=0;i<vi->books;i++)
213 vorbis_book_init_decode(v->fullbooks+i,vi->book_param[i]);
216 /* initialize the storage vectors to a decent size greater than the
219 v->pcm_storage=8192; /* we'll assume later that we have
220 a minimum of twice the blocksize of
221 accumulated samples in analysis */
222 v->pcm=malloc(vi->channels*sizeof(double *));
223 v->pcmret=malloc(vi->channels*sizeof(double *));
226 for(i=0;i<vi->channels;i++)
227 v->pcm[i]=calloc(v->pcm_storage,sizeof(double));
230 /* all 1 (large block) or 0 (small block) */
231 /* explicitly set for the sake of clarity */
232 v->lW=0; /* previous window size */
233 v->W=0; /* current window size */
235 /* all vector indexes; multiples of samples_per_envelope_step */
236 v->centerW=vi->blocksizes[1]/2;
238 v->pcm_current=v->centerW;
240 /* initialize all the mapping/backend lookups */
241 v->mode=calloc(vi->modes,sizeof(vorbis_look_mapping *));
242 for(i=0;i<vi->modes;i++){
243 int mapnum=vi->mode_param[i]->mapping;
244 int maptype=vi->map_type[mapnum];
245 v->mode[i]=_mapping_P[maptype]->look(v,vi->mode_param[i],
246 vi->map_param[mapnum]);
252 /* arbitrary settings and spec-mandated numbers get filled in here */
253 int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
254 _vds_shared_init(v,vi,1);
256 /* Initialize the envelope multiplier storage */
258 v->envelope_storage=v->pcm_storage/vi->envelopesa;
259 v->multipliers=calloc(v->envelope_storage,sizeof(double));
260 _ve_envelope_init(&v->ve,vi->envelopesa);
262 v->envelope_current=v->centerW/vi->envelopesa;
266 void vorbis_dsp_clear(vorbis_dsp_state *v){
269 vorbis_info *vi=v->vi;
271 for(i=0;i<VI_WINDOWB;i++){
272 if(v->window[0][0][0][i])free(v->window[0][0][0][i]);
275 if(v->window[1][j][k][i])free(v->window[1][j][k][i]);
278 for(i=0;i<vi->channels;i++)
279 if(v->pcm[i])free(v->pcm[i]);
281 if(v->pcmret)free(v->pcmret);
283 if(v->multipliers)free(v->multipliers);
285 _ve_envelope_clear(&v->ve);
286 mdct_clear(v->transform[0][0]);
287 mdct_clear(v->transform[1][0]);
288 free(v->transform[0][0]);
289 free(v->transform[1][0]);
291 free(v->transform[0]);
292 free(v->transform[1]);
294 /* free mode lookups; these are actually vorbis_look_mapping structs */
295 for(i=0;i<vi->modes;i++){
296 int mapnum=vi->mode_param[i]->mapping;
297 int maptype=vi->map_type[mapnum];
298 _mapping_P[maptype]->free_look(v->mode[i]);
300 if(v->mode)free(v->mode);
303 for(i=0;i<vi->books;i++)
304 vorbis_book_clear(v->fullbooks+i);
305 if(v->fullbooks)free(v->fullbooks);
307 /* free header, header1, header2 */
308 if(v->header)free(v->header);
309 if(v->header1)free(v->header1);
310 if(v->header2)free(v->header2);
312 memset(v,0,sizeof(vorbis_dsp_state));
316 double **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
318 vorbis_info *vi=v->vi;
320 /* free header, header1, header2 */
321 if(v->header)free(v->header);v->header=NULL;
322 if(v->header1)free(v->header1);v->header1=NULL;
323 if(v->header2)free(v->header2);v->header2=NULL;
325 /* Do we have enough storage space for the requested buffer? If not,
326 expand the PCM (and envelope) storage */
328 if(v->pcm_current+vals>=v->pcm_storage){
329 v->pcm_storage=v->pcm_current+vals*2;
330 v->envelope_storage=v->pcm_storage/v->vi->envelopesa;
332 for(i=0;i<vi->channels;i++){
333 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
335 v->multipliers=realloc(v->multipliers,v->envelope_storage*sizeof(double));
338 for(i=0;i<vi->channels;i++)
339 v->pcmret[i]=v->pcm[i]+v->pcm_current;
344 /* call with val<=0 to set eof */
346 int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
347 vorbis_info *vi=v->vi;
349 /* We're encoding the end of the stream. Just make sure we have
350 [at least] a full block of zeroes at the end. */
353 vorbis_analysis_buffer(v,v->vi->blocksizes[1]*2);
354 v->eofflag=v->pcm_current;
355 v->pcm_current+=v->vi->blocksizes[1]*2;
356 for(i=0;i<vi->channels;i++)
357 memset(v->pcm[i]+v->eofflag,0,
358 (v->pcm_current-v->eofflag)*sizeof(double));
361 if(v->pcm_current+vals>v->pcm_storage)
364 v->pcm_current+=vals;
369 /* do the deltas, envelope shaping, pre-echo and determine the size of
370 the next block on which to continue analysis */
371 int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
373 vorbis_info *vi=v->vi;
374 long beginW=v->centerW-vi->blocksizes[v->W]/2,centerNext;
376 /* check to see if we're done... */
377 if(v->eofflag==-1)return(0);
379 /* if we have any unfilled envelope blocks for which we have PCM
380 data, fill them up in before proceeding. */
382 if(v->pcm_current/vi->envelopesa>v->envelope_current){
383 /* This generates the multipliers, but does not sparsify the vector.
384 That's done by block before coding */
385 _ve_envelope_deltas(v);
388 /* By our invariant, we have lW, W and centerW set. Search for
389 the next boundary so we can determine nW (the next window size)
390 which lets us compute the shape of the current block's window */
392 if(vi->blocksizes[0]<vi->blocksizes[1]){
395 /* this is a long window; we start the search forward of centerW
396 because that's the fastest we could react anyway */
397 i=v->centerW+vi->blocksizes[1]/4-vi->blocksizes[0]/4;
399 /* short window. Search from centerW */
403 for(;i<v->envelope_current-1;i++){
404 /* Compare last with current; do we have an abrupt energy change? */
406 if(v->multipliers[i-1]*vi->preecho_thresh<
407 v->multipliers[i])break;
409 /* because the overlapping nature of the delta finding
410 'smears' the energy cliffs, also compare completely
411 unoverlapped areas just in case the plosive happened in an
414 if(v->multipliers[i-1]*vi->preecho_thresh<
415 v->multipliers[i+1])break;
419 if(i<v->envelope_current-1){
420 /* Ooo, we hit a multiplier. Is it beyond the boundary to make the
421 upcoming block large ? */
424 /* min boundary; nW large, next small */
425 largebound=v->centerW+vi->blocksizes[1]*3/4+vi->blocksizes[0]/4;
427 /* min boundary; nW large, next small */
428 largebound=v->centerW+vi->blocksizes[0]/2+vi->blocksizes[1]/2;
429 largebound/=vi->envelopesa;
437 /* Assume maximum; if the block is incomplete given current
438 buffered data, this will be detected below */
444 /* Do we actually have enough data *now* for the next block? The
445 reason to check is that if we had no multipliers, that could
446 simply been due to running out of data. In that case, we don't
447 know the size of the next block for sure and we need that now to
448 figure out the window shape of this block */
450 centerNext=v->centerW+vi->blocksizes[v->W]/4+vi->blocksizes[v->nW]/4;
453 /* center of next block + next block maximum right side. Note
454 that the next block needs an additional vi->envelopesa samples
455 to actually be written (for the last multiplier), but we didn't
456 need that to determine its size */
458 long blockbound=centerNext+vi->blocksizes[v->nW]/2;
459 if(v->pcm_current<blockbound)return(0); /* not enough data yet */
462 /* fill in the block. Note that for a short window, lW and nW are *short*
463 regardless of actual settings in the stream */
465 _vorbis_block_ripcord(vb);
476 vb->sequence=v->sequence;
477 vb->frameno=v->frameno;
478 vb->pcmend=vi->blocksizes[v->W];
480 /* copy the vectors; this uses the local storage in vb */
482 vb->pcm=_vorbis_block_alloc(vb,sizeof(double *)*vi->channels);
483 for(i=0;i<vi->channels;i++){
484 vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(double));
485 memcpy(vb->pcm[i],v->pcm[i]+beginW,vi->blocksizes[v->W]*sizeof(double));
489 /* handle eof detection: eof==0 means that we've not yet received EOF
490 eof>0 marks the last 'real' sample in pcm[]
491 eof<0 'no more to do'; doesn't get here */
494 if(v->centerW>=v->eofflag){
500 /* advance storage vectors and clean up */
502 int new_centerNext=vi->blocksizes[1]/2;
503 int movementW=centerNext-new_centerNext;
504 int movementM=movementW/vi->envelopesa;
506 /* the multipliers and pcm stay synced up because the blocksize
507 must be multiples of samples_per_envelope_step (minimum
510 v->pcm_current-=movementW;
511 v->envelope_current-=movementM;
513 for(i=0;i<vi->channels;i++)
514 memmove(v->pcm[i],v->pcm[i]+movementW,
515 v->pcm_current*sizeof(double));
517 memmove(v->multipliers,v->multipliers+movementM,
518 v->envelope_current*sizeof(double));
522 v->centerW=new_centerNext;
525 v->frameno+=movementW;
528 v->eofflag-=movementW;
535 int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
536 _vds_shared_init(v,vi,0);
538 /* Adjust centerW to allow an easier mechanism for determining output */
539 v->pcm_returned=v->centerW;
540 v->centerW-= vi->blocksizes[v->W]/4+vi->blocksizes[v->lW]/4;
544 /* Unike in analysis, the window is only partially applied for each
545 block. The time domain envelope is not yet handled at the point of
546 calling (as it relies on the previous block). */
548 int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
549 vorbis_info *vi=v->vi;
551 /* Shift out any PCM/multipliers that we returned previously */
552 /* centerW is currently the center of the last block added */
553 if(v->pcm_returned && v->centerW>vi->blocksizes[1]/2){
555 /* don't shift too much; we need to have a minimum PCM buffer of
558 int shiftPCM=v->centerW-vi->blocksizes[1]/2;
559 shiftPCM=(v->pcm_returned<shiftPCM?v->pcm_returned:shiftPCM);
561 v->pcm_current-=shiftPCM;
562 v->centerW-=shiftPCM;
563 v->pcm_returned-=shiftPCM;
567 for(i=0;i<vi->channels;i++)
568 memmove(v->pcm[i],v->pcm[i]+shiftPCM,
569 v->pcm_current*sizeof(double));
577 v->glue_bits+=vb->glue_bits;
578 v->time_bits+=vb->time_bits;
579 v->floor_bits+=vb->floor_bits;
580 v->res_bits+=vb->res_bits;
581 v->sequence=vb->sequence;
584 int sizeW=vi->blocksizes[v->W];
585 int centerW=v->centerW+vi->blocksizes[v->lW]/4+sizeW/4;
586 int beginW=centerW-sizeW/2;
587 int endW=beginW+sizeW;
592 /* Do we have enough PCM/mult storage for the block? */
593 if(endW>v->pcm_storage){
594 /* expand the storage */
595 v->pcm_storage=endW+vi->blocksizes[1];
597 for(i=0;i<vi->channels;i++)
598 v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
601 /* overlap/add PCM */
606 endSl=vi->blocksizes[0]/2;
609 beginSl=vi->blocksizes[1]/4-vi->blocksizes[v->lW]/4;
610 endSl=beginSl+vi->blocksizes[v->lW]/2;
614 for(j=0;j<vi->channels;j++){
615 double *pcm=v->pcm[j]+beginW;
617 /* the overlap/add section */
618 for(i=beginSl;i<endSl;i++)
619 pcm[i]+=vb->pcm[j][i];
620 /* the remaining section */
622 pcm[i]=vb->pcm[j][i];
625 /* Update, cleanup */
630 if(vb->eofflag)v->eofflag=1;
635 int vorbis_synthesis_pcmout(vorbis_dsp_state *v,double ***pcm){
636 vorbis_info *vi=v->vi;
637 if(v->pcm_returned<v->centerW){
639 for(i=0;i<vi->channels;i++)
640 v->pcmret[i]=v->pcm[i]+v->pcm_returned;
642 return(v->centerW-v->pcm_returned);
647 int vorbis_synthesis_read(vorbis_dsp_state *v,int bytes){
648 if(bytes && v->pcm_returned+bytes>v->centerW)return(-1);
649 v->pcm_returned+=bytes;