block.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /********************************************************************
  2. * *
  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. *
  7. * *
  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/ *
  11. * *
  12. ********************************************************************
  13. function: PCM data vector blocking, windowing and dis/reassembly
  14. last mod: $Id: block.c,v 1.27.4.2 2000/04/06 15:59:36 xiphmont Exp $
  15. Handle windowing, overlap-add, etc of the PCM vectors. This is made
  16. more amusing by Vorbis' current two allowed block sizes.
  17. Vorbis manipulates the dynamic range of the incoming PCM data
  18. envelope to minimise time-domain energy leakage from percussive and
  19. plosive waveforms being quantized in the MDCT domain.
  20. ********************************************************************/
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "vorbis/codec.h"
  25. #include "window.h"
  26. #include "envelope.h"
  27. #include "mdct.h"
  28. #include "lpc.h"
  29. #include "bitwise.h"
  30. #include "registry.h"
  31. #include "sharedbook.h"
  32. #include "bookinternal.h"
  33. static int ilog2(unsigned int v){
  34. int ret=0;
  35. while(v>1){
  36. ret++;
  37. v>>=1;
  38. }
  39. return(ret);
  40. }
  41. /* pcm accumulator examples (not exhaustive):
  42. <-------------- lW ---------------->
  43. <--------------- W ---------------->
  44. : .....|..... _______________ |
  45. : .''' | '''_--- | |\ |
  46. :.....''' |_____--- '''......| | \_______|
  47. :.................|__________________|_______|__|______|
  48. |<------ Sl ------>| > Sr < |endW
  49. |beginSl |endSl | |endSr
  50. |beginW |endlW |beginSr
  51. |< lW >|
  52. <--------------- W ---------------->
  53. | | .. ______________ |
  54. | | ' `/ | ---_ |
  55. |___.'___/`. | ---_____|
  56. |_______|__|_______|_________________|
  57. | >|Sl|< |<------ Sr ----->|endW
  58. | | |endSl |beginSr |endSr
  59. |beginW | |endlW
  60. mult[0] |beginSl mult[n]
  61. <-------------- lW ----------------->
  62. |<--W-->|
  63. : .............. ___ | |
  64. : .''' |`/ \ | |
  65. :.....''' |/`....\|...|
  66. :.........................|___|___|___|
  67. |Sl |Sr |endW
  68. | | |endSr
  69. | |beginSr
  70. | |endSl
  71. |beginSl
  72. |beginW
  73. */
  74. /* block abstraction setup *********************************************/
  75. #ifndef WORD_ALIGN
  76. #define WORD_ALIGN 8
  77. #endif
  78. int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
  79. memset(vb,0,sizeof(vorbis_block));
  80. vb->vd=v;
  81. vb->localalloc=0;
  82. vb->localstore=NULL;
  83. if(v->analysisp)
  84. _oggpack_writeinit(&vb->opb);
  85. return(0);
  86. }
  87. void *_vorbis_block_alloc(vorbis_block *vb,long bytes){
  88. bytes=(bytes+(WORD_ALIGN-1)) & ~(WORD_ALIGN-1);
  89. if(bytes+vb->localtop>vb->localalloc){
  90. /* can't just realloc... there are outstanding pointers */
  91. if(vb->localstore){
  92. struct alloc_chain *link=malloc(sizeof(struct alloc_chain));
  93. vb->totaluse+=vb->localtop;
  94. link->next=vb->reap;
  95. link->ptr=vb->localstore;
  96. vb->reap=link;
  97. }
  98. /* highly conservative */
  99. vb->localalloc=bytes;
  100. vb->localstore=malloc(vb->localalloc);
  101. vb->localtop=0;
  102. }
  103. {
  104. void *ret=(void *)(((char *)vb->localstore)+vb->localtop);
  105. vb->localtop+=bytes;
  106. return ret;
  107. }
  108. }
  109. /* reap the chain, pull the ripcord */
  110. void _vorbis_block_ripcord(vorbis_block *vb){
  111. /* reap the chain */
  112. struct alloc_chain *reap=vb->reap;
  113. while(reap){
  114. struct alloc_chain *next=reap->next;
  115. free(reap->ptr);
  116. memset(reap,0,sizeof(struct alloc_chain));
  117. free(reap);
  118. reap=next;
  119. }
  120. /* consolidate storage */
  121. if(vb->totaluse){
  122. vb->localstore=realloc(vb->localstore,vb->totaluse+vb->localalloc);
  123. vb->localalloc+=vb->totaluse;
  124. vb->totaluse=0;
  125. }
  126. /* pull the ripcord */
  127. vb->localtop=0;
  128. vb->reap=NULL;
  129. }
  130. int vorbis_block_clear(vorbis_block *vb){
  131. if(vb->vd)
  132. if(vb->vd->analysisp)
  133. _oggpack_writeclear(&vb->opb);
  134. _vorbis_block_ripcord(vb);
  135. if(vb->localstore)free(vb->localstore);
  136. memset(vb,0,sizeof(vorbis_block));
  137. return(0);
  138. }
  139. /* Analysis side code, but directly related to blocking. Thus it's
  140. here and not in analysis.c (which is for analysis transforms only).
  141. The init is here because some of it is shared */
  142. static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){
  143. int i;
  144. memset(v,0,sizeof(vorbis_dsp_state));
  145. v->vi=vi;
  146. v->modebits=ilog2(vi->modes);
  147. v->transform[0]=calloc(VI_TRANSFORMB,sizeof(vorbis_look_transform *));
  148. v->transform[1]=calloc(VI_TRANSFORMB,sizeof(vorbis_look_transform *));
  149. /* MDCT is tranform 0 */
  150. v->transform[0][0]=calloc(1,sizeof(mdct_lookup));
  151. v->transform[1][0]=calloc(1,sizeof(mdct_lookup));
  152. mdct_init(v->transform[0][0],vi->blocksizes[0]);
  153. mdct_init(v->transform[1][0],vi->blocksizes[1]);
  154. v->window[0][0][0]=calloc(VI_WINDOWB,sizeof(double *));
  155. v->window[0][0][1]=v->window[0][0][0];
  156. v->window[0][1][0]=v->window[0][0][0];
  157. v->window[0][1][1]=v->window[0][0][0];
  158. v->window[1][0][0]=calloc(VI_WINDOWB,sizeof(double *));
  159. v->window[1][0][1]=calloc(VI_WINDOWB,sizeof(double *));
  160. v->window[1][1][0]=calloc(VI_WINDOWB,sizeof(double *));
  161. v->window[1][1][1]=calloc(VI_WINDOWB,sizeof(double *));
  162. for(i=0;i<VI_WINDOWB;i++){
  163. v->window[0][0][0][i]=
  164. _vorbis_window(i,vi->blocksizes[0],vi->blocksizes[0]/2,vi->blocksizes[0]/2);
  165. v->window[1][0][0][i]=
  166. _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[0]/2,vi->blocksizes[0]/2);
  167. v->window[1][0][1][i]=
  168. _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[0]/2,vi->blocksizes[1]/2);
  169. v->window[1][1][0][i]=
  170. _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[1]/2,vi->blocksizes[0]/2);
  171. v->window[1][1][1][i]=
  172. _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[1]/2,vi->blocksizes[1]/2);
  173. }
  174. if(encp){ /* encode/decode differ here */
  175. /* finish the codebooks */
  176. v->fullbooks=calloc(vi->books,sizeof(codebook));
  177. for(i=0;i<vi->books;i++)
  178. vorbis_book_init_encode(v->fullbooks+i,vi->book_param[i]);
  179. v->analysisp=1;
  180. }else{
  181. /* finish the codebooks */
  182. v->fullbooks=calloc(vi->books,sizeof(codebook));
  183. for(i=0;i<vi->books;i++)
  184. vorbis_book_init_decode(v->fullbooks+i,vi->book_param[i]);
  185. }
  186. /* initialize the storage vectors to a decent size greater than the
  187. minimum */
  188. v->pcm_storage=8192; /* we'll assume later that we have
  189. a minimum of twice the blocksize of
  190. accumulated samples in analysis */
  191. v->pcm=malloc(vi->channels*sizeof(double *));
  192. v->pcmret=malloc(vi->channels*sizeof(double *));
  193. {
  194. int i;
  195. for(i=0;i<vi->channels;i++)
  196. v->pcm[i]=calloc(v->pcm_storage,sizeof(double));
  197. }
  198. /* all 1 (large block) or 0 (small block) */
  199. /* explicitly set for the sake of clarity */
  200. v->lW=0; /* previous window size */
  201. v->W=0; /* current window size */
  202. /* all vector indexes; multiples of samples_per_envelope_step */
  203. v->centerW=vi->blocksizes[1]/2;
  204. v->pcm_current=v->centerW;
  205. /* initialize all the mapping/backend lookups */
  206. v->mode=calloc(vi->modes,sizeof(vorbis_look_mapping *));
  207. for(i=0;i<vi->modes;i++){
  208. int mapnum=vi->mode_param[i]->mapping;
  209. int maptype=vi->map_type[mapnum];
  210. v->mode[i]=_mapping_P[maptype]->look(v,vi->mode_param[i],
  211. vi->map_param[mapnum]);
  212. }
  213. return(0);
  214. }
  215. /* arbitrary settings and spec-mandated numbers get filled in here */
  216. int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
  217. _vds_shared_init(v,vi,1);
  218. /* Initialize the envelope multiplier storage */
  219. v->envelope_storage=v->pcm_storage/vi->envelopesa;
  220. v->multipliers=calloc(v->envelope_storage,sizeof(double));
  221. _ve_envelope_init(&v->ve,vi->envelopesa);
  222. v->envelope_current=v->centerW/vi->envelopesa;
  223. return(0);
  224. }
  225. void vorbis_dsp_clear(vorbis_dsp_state *v){
  226. int i,j,k;
  227. if(v){
  228. vorbis_info *vi=v->vi;
  229. if(v->window[0][0][0])
  230. for(i=0;i<VI_WINDOWB;i++){
  231. if(v->window[0][0][0][i])free(v->window[0][0][0][i]);
  232. for(j=0;j<2;j++)
  233. for(k=0;k<2;k++)
  234. if(v->window[1][j][k][i])free(v->window[1][j][k][i]);
  235. }
  236. if(v->pcm){
  237. for(i=0;i<vi->channels;i++)
  238. if(v->pcm[i])free(v->pcm[i]);
  239. free(v->pcm);
  240. if(v->pcmret)free(v->pcmret);
  241. }
  242. if(v->multipliers)free(v->multipliers);
  243. _ve_envelope_clear(&v->ve);
  244. if(v->transform[0]){
  245. mdct_clear(v->transform[0][0]);
  246. free(v->transform[0][0]);
  247. free(v->transform[0]);
  248. }
  249. if(v->transform[1]){
  250. mdct_clear(v->transform[1][0]);
  251. free(v->transform[1][0]);
  252. free(v->transform[1]);
  253. }
  254. /* free mode lookups; these are actually vorbis_look_mapping structs */
  255. if(vi){
  256. for(i=0;i<vi->modes;i++){
  257. int mapnum=vi->mode_param[i]->mapping;
  258. int maptype=vi->map_type[mapnum];
  259. _mapping_P[maptype]->free_look(v->mode[i]);
  260. }
  261. /* free codebooks */
  262. for(i=0;i<vi->books;i++)
  263. vorbis_book_clear(v->fullbooks+i);
  264. }
  265. if(v->mode)free(v->mode);
  266. if(v->fullbooks)free(v->fullbooks);
  267. /* free header, header1, header2 */
  268. if(v->header)free(v->header);
  269. if(v->header1)free(v->header1);
  270. if(v->header2)free(v->header2);
  271. memset(v,0,sizeof(vorbis_dsp_state));
  272. }
  273. }
  274. double **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
  275. int i;
  276. vorbis_info *vi=v->vi;
  277. /* free header, header1, header2 */
  278. if(v->header)free(v->header);v->header=NULL;
  279. if(v->header1)free(v->header1);v->header1=NULL;
  280. if(v->header2)free(v->header2);v->header2=NULL;
  281. /* Do we have enough storage space for the requested buffer? If not,
  282. expand the PCM (and envelope) storage */
  283. if(v->pcm_current+vals>=v->pcm_storage){
  284. v->pcm_storage=v->pcm_current+vals*2;
  285. v->envelope_storage=v->pcm_storage/v->vi->envelopesa;
  286. for(i=0;i<vi->channels;i++){
  287. v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
  288. }
  289. v->multipliers=realloc(v->multipliers,v->envelope_storage*sizeof(double));
  290. }
  291. for(i=0;i<vi->channels;i++)
  292. v->pcmret[i]=v->pcm[i]+v->pcm_current;
  293. return(v->pcmret);
  294. }
  295. /* call with val<=0 to set eof */
  296. int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
  297. vorbis_info *vi=v->vi;
  298. if(vals<=0){
  299. /* We're encoding the end of the stream. Just make sure we have
  300. [at least] a full block of zeroes at the end. */
  301. int i;
  302. vorbis_analysis_buffer(v,v->vi->blocksizes[1]*2);
  303. v->eofflag=v->pcm_current;
  304. v->pcm_current+=v->vi->blocksizes[1]*2;
  305. for(i=0;i<vi->channels;i++)
  306. memset(v->pcm[i]+v->eofflag,0,
  307. (v->pcm_current-v->eofflag)*sizeof(double));
  308. }else{
  309. if(v->pcm_current+vals>v->pcm_storage)
  310. return(-1);
  311. v->pcm_current+=vals;
  312. }
  313. return(0);
  314. }
  315. /* do the deltas, envelope shaping, pre-echo and determine the size of
  316. the next block on which to continue analysis */
  317. int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
  318. int i;
  319. vorbis_info *vi=v->vi;
  320. long beginW=v->centerW-vi->blocksizes[v->W]/2,centerNext;
  321. /* check to see if we're done... */
  322. if(v->eofflag==-1)return(0);
  323. /* if we have any unfilled envelope blocks for which we have PCM
  324. data, fill them up in before proceeding. */
  325. if(v->pcm_current/vi->envelopesa>v->envelope_current){
  326. /* This generates the multipliers, but does not sparsify the vector.
  327. That's done by block before coding */
  328. _ve_envelope_deltas(v);
  329. }
  330. /* By our invariant, we have lW, W and centerW set. Search for
  331. the next boundary so we can determine nW (the next window size)
  332. which lets us compute the shape of the current block's window */
  333. if(vi->blocksizes[0]<vi->blocksizes[1]){
  334. if(v->W)
  335. /* this is a long window; we start the search forward of centerW
  336. because that's the fastest we could react anyway */
  337. i=v->centerW+vi->blocksizes[1]/4-vi->blocksizes[0]/4;
  338. else
  339. /* short window. Search from centerW */
  340. i=v->centerW;
  341. i/=vi->envelopesa;
  342. for(;i<v->envelope_current-1;i++){
  343. /* Compare last with current; do we have an abrupt energy change? */
  344. if(v->multipliers[i-1]*vi->preecho_thresh<
  345. v->multipliers[i])break;
  346. /* because the overlapping nature of the delta finding
  347. 'smears' the energy cliffs, also compare completely
  348. unoverlapped areas just in case the plosive happened in an
  349. unlucky place */
  350. if(v->multipliers[i-1]*vi->preecho_thresh<
  351. v->multipliers[i+1])break;
  352. }
  353. if(i<v->envelope_current-1){
  354. /* Ooo, we hit a multiplier. Is it beyond the boundary to make the
  355. upcoming block large ? */
  356. long largebound;
  357. if(v->W)
  358. /* min boundary; nW large, next small */
  359. largebound=v->centerW+vi->blocksizes[1]*3/4+vi->blocksizes[0]/4;
  360. else
  361. /* min boundary; nW large, next small */
  362. largebound=v->centerW+vi->blocksizes[0]/2+vi->blocksizes[1]/2;
  363. largebound/=vi->envelopesa;
  364. if(i>=largebound)
  365. v->nW=1;
  366. else
  367. v->nW=0;
  368. }else{
  369. /* Assume maximum; if the block is incomplete given current
  370. buffered data, this will be detected below */
  371. v->nW=1;
  372. }
  373. }else
  374. v->nW=0;
  375. /* Do we actually have enough data *now* for the next block? The
  376. reason to check is that if we had no multipliers, that could
  377. simply been due to running out of data. In that case, we don't
  378. know the size of the next block for sure and we need that now to
  379. figure out the window shape of this block */
  380. centerNext=v->centerW+vi->blocksizes[v->W]/4+vi->blocksizes[v->nW]/4;
  381. {
  382. /* center of next block + next block maximum right side. Note
  383. that the next block needs an additional vi->envelopesa samples
  384. to actually be written (for the last multiplier), but we didn't
  385. need that to determine its size */
  386. long blockbound=centerNext+vi->blocksizes[v->nW]/2;
  387. if(v->pcm_current<blockbound)return(0); /* not enough data yet */
  388. }
  389. /* fill in the block. Note that for a short window, lW and nW are *short*
  390. regardless of actual settings in the stream */
  391. _vorbis_block_ripcord(vb);
  392. if(v->W){
  393. vb->lW=v->lW;
  394. vb->W=v->W;
  395. vb->nW=v->nW;
  396. }else{
  397. vb->lW=0;
  398. vb->W=v->W;
  399. vb->nW=0;
  400. }
  401. vb->vd=v;
  402. vb->sequence=v->sequence;
  403. vb->frameno=v->frameno;
  404. vb->pcmend=vi->blocksizes[v->W];
  405. /* copy the vectors; this uses the local storage in vb */
  406. {
  407. vb->pcm=_vorbis_block_alloc(vb,sizeof(double *)*vi->channels);
  408. for(i=0;i<vi->channels;i++){
  409. vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(double));
  410. memcpy(vb->pcm[i],v->pcm[i]+beginW,vi->blocksizes[v->W]*sizeof(double));
  411. }
  412. }
  413. /* handle eof detection: eof==0 means that we've not yet received EOF
  414. eof>0 marks the last 'real' sample in pcm[]
  415. eof<0 'no more to do'; doesn't get here */
  416. if(v->eofflag){
  417. if(v->centerW>=v->eofflag){
  418. v->eofflag=-1;
  419. vb->eofflag=1;
  420. }
  421. }
  422. /* advance storage vectors and clean up */
  423. {
  424. int new_centerNext=vi->blocksizes[1]/2;
  425. int movementW=centerNext-new_centerNext;
  426. int movementM=movementW/vi->envelopesa;
  427. /* the multipliers and pcm stay synced up because the blocksize
  428. must be multiples of samples_per_envelope_step (minimum
  429. multiple is 2) */
  430. v->pcm_current-=movementW;
  431. v->envelope_current-=movementM;
  432. for(i=0;i<vi->channels;i++)
  433. memmove(v->pcm[i],v->pcm[i]+movementW,
  434. v->pcm_current*sizeof(double));
  435. memmove(v->multipliers,v->multipliers+movementM,
  436. v->envelope_current*sizeof(double));
  437. v->lW=v->W;
  438. v->W=v->nW;
  439. v->centerW=new_centerNext;
  440. v->sequence++;
  441. v->frameno+=movementW;
  442. if(v->eofflag)
  443. v->eofflag-=movementW;
  444. }
  445. /* done */
  446. return(1);
  447. }
  448. int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
  449. _vds_shared_init(v,vi,0);
  450. /* Adjust centerW to allow an easier mechanism for determining output */
  451. v->pcm_returned=v->centerW;
  452. v->centerW-= vi->blocksizes[v->W]/4+vi->blocksizes[v->lW]/4;
  453. return(0);
  454. }
  455. /* Unike in analysis, the window is only partially applied for each
  456. block. The time domain envelope is not yet handled at the point of
  457. calling (as it relies on the previous block). */
  458. int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
  459. vorbis_info *vi=v->vi;
  460. /* Shift out any PCM/multipliers that we returned previously */
  461. /* centerW is currently the center of the last block added */
  462. if(v->pcm_returned && v->centerW>vi->blocksizes[1]/2){
  463. /* don't shift too much; we need to have a minimum PCM buffer of
  464. 1/2 long block */
  465. int shiftPCM=v->centerW-vi->blocksizes[1]/2;
  466. shiftPCM=(v->pcm_returned<shiftPCM?v->pcm_returned:shiftPCM);
  467. v->pcm_current-=shiftPCM;
  468. v->centerW-=shiftPCM;
  469. v->pcm_returned-=shiftPCM;
  470. if(shiftPCM){
  471. int i;
  472. for(i=0;i<vi->channels;i++)
  473. memmove(v->pcm[i],v->pcm[i]+shiftPCM,
  474. v->pcm_current*sizeof(double));
  475. }
  476. }
  477. v->lW=v->W;
  478. v->W=vb->W;
  479. v->nW=-1;
  480. v->glue_bits+=vb->glue_bits;
  481. v->time_bits+=vb->time_bits;
  482. v->floor_bits+=vb->floor_bits;
  483. v->res_bits+=vb->res_bits;
  484. v->sequence=vb->sequence;
  485. {
  486. int sizeW=vi->blocksizes[v->W];
  487. int centerW=v->centerW+vi->blocksizes[v->lW]/4+sizeW/4;
  488. int beginW=centerW-sizeW/2;
  489. int endW=beginW+sizeW;
  490. int beginSl;
  491. int endSl;
  492. int i,j;
  493. /* Do we have enough PCM/mult storage for the block? */
  494. if(endW>v->pcm_storage){
  495. /* expand the storage */
  496. v->pcm_storage=endW+vi->blocksizes[1];
  497. for(i=0;i<vi->channels;i++)
  498. v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(double));
  499. }
  500. /* overlap/add PCM */
  501. switch(v->W){
  502. case 0:
  503. beginSl=0;
  504. endSl=vi->blocksizes[0]/2;
  505. break;
  506. case 1:
  507. beginSl=vi->blocksizes[1]/4-vi->blocksizes[v->lW]/4;
  508. endSl=beginSl+vi->blocksizes[v->lW]/2;
  509. break;
  510. }
  511. for(j=0;j<vi->channels;j++){
  512. double *pcm=v->pcm[j]+beginW;
  513. /* the overlap/add section */
  514. for(i=beginSl;i<endSl;i++)
  515. pcm[i]+=vb->pcm[j][i];
  516. /* the remaining section */
  517. for(;i<sizeW;i++)
  518. pcm[i]=vb->pcm[j][i];
  519. }
  520. /* Update, cleanup */
  521. v->centerW=centerW;
  522. v->pcm_current=endW;
  523. if(vb->eofflag)v->eofflag=1;
  524. }
  525. return(0);
  526. }
  527. int vorbis_synthesis_pcmout(vorbis_dsp_state *v,double ***pcm){
  528. vorbis_info *vi=v->vi;
  529. if(v->pcm_returned<v->centerW){
  530. int i;
  531. for(i=0;i<vi->channels;i++)
  532. v->pcmret[i]=v->pcm[i]+v->pcm_returned;
  533. *pcm=v->pcmret;
  534. return(v->centerW-v->pcm_returned);
  535. }
  536. return(0);
  537. }
  538. int vorbis_synthesis_read(vorbis_dsp_state *v,int bytes){
  539. if(bytes && v->pcm_returned+bytes>v->centerW)return(-1);
  540. v->pcm_returned+=bytes;
  541. return(0);
  542. }