block.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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.38.2.2.2.2 2000/09/26 18:45:33 jack 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 <ogg/ogg.h>
  25. #include "vorbis/codec.h"
  26. #include "window.h"
  27. #include "envelope.h"
  28. #include "mdct.h"
  29. #include "lpc.h"
  30. #include "registry.h"
  31. #include "sharedbook.h"
  32. #include "bookinternal.h"
  33. #include "misc.h"
  34. #include "os.h"
  35. static int ilog2(unsigned int v){
  36. int ret=0;
  37. while(v>1){
  38. ret++;
  39. v>>=1;
  40. }
  41. return(ret);
  42. }
  43. /* pcm accumulator examples (not exhaustive):
  44. <-------------- lW ---------------->
  45. <--------------- W ---------------->
  46. : .....|..... _______________ |
  47. : .''' | '''_--- | |\ |
  48. :.....''' |_____--- '''......| | \_______|
  49. :.................|__________________|_______|__|______|
  50. |<------ Sl ------>| > Sr < |endW
  51. |beginSl |endSl | |endSr
  52. |beginW |endlW |beginSr
  53. |< lW >|
  54. <--------------- W ---------------->
  55. | | .. ______________ |
  56. | | ' `/ | ---_ |
  57. |___.'___/`. | ---_____|
  58. |_______|__|_______|_________________|
  59. | >|Sl|< |<------ Sr ----->|endW
  60. | | |endSl |beginSr |endSr
  61. |beginW | |endlW
  62. mult[0] |beginSl mult[n]
  63. <-------------- lW ----------------->
  64. |<--W-->|
  65. : .............. ___ | |
  66. : .''' |`/ \ | |
  67. :.....''' |/`....\|...|
  68. :.........................|___|___|___|
  69. |Sl |Sr |endW
  70. | | |endSr
  71. | |beginSr
  72. | |endSl
  73. |beginSl
  74. |beginW
  75. */
  76. /* block abstraction setup *********************************************/
  77. #ifndef WORD_ALIGN
  78. #define WORD_ALIGN 8
  79. #endif
  80. int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
  81. memset(vb,0,sizeof(vorbis_block));
  82. vb->vd=v;
  83. vb->localalloc=0;
  84. vb->localstore=NULL;
  85. if(v->analysisp)
  86. oggpack_writeinit(&vb->opb);
  87. return(0);
  88. }
  89. void *_vorbis_block_alloc(vorbis_block *vb,long bytes){
  90. bytes=(bytes+(WORD_ALIGN-1)) & ~(WORD_ALIGN-1);
  91. if(bytes+vb->localtop>vb->localalloc){
  92. /* can't just realloc... there are outstanding pointers */
  93. if(vb->localstore){
  94. struct alloc_chain *link=malloc(sizeof(struct alloc_chain));
  95. vb->totaluse+=vb->localtop;
  96. link->next=vb->reap;
  97. link->ptr=vb->localstore;
  98. vb->reap=link;
  99. }
  100. /* highly conservative */
  101. vb->localalloc=bytes;
  102. vb->localstore=malloc(vb->localalloc);
  103. vb->localtop=0;
  104. }
  105. {
  106. void *ret=(void *)(((char *)vb->localstore)+vb->localtop);
  107. vb->localtop+=bytes;
  108. return ret;
  109. }
  110. }
  111. /* reap the chain, pull the ripcord */
  112. void _vorbis_block_ripcord(vorbis_block *vb){
  113. /* reap the chain */
  114. struct alloc_chain *reap=vb->reap;
  115. while(reap){
  116. struct alloc_chain *next=reap->next;
  117. free(reap->ptr);
  118. memset(reap,0,sizeof(struct alloc_chain));
  119. free(reap);
  120. reap=next;
  121. }
  122. /* consolidate storage */
  123. if(vb->totaluse){
  124. vb->localstore=realloc(vb->localstore,vb->totaluse+vb->localalloc);
  125. vb->localalloc+=vb->totaluse;
  126. vb->totaluse=0;
  127. }
  128. /* pull the ripcord */
  129. vb->localtop=0;
  130. vb->reap=NULL;
  131. }
  132. int vorbis_block_clear(vorbis_block *vb){
  133. if(vb->vd)
  134. if(vb->vd->analysisp)
  135. oggpack_writeclear(&vb->opb);
  136. _vorbis_block_ripcord(vb);
  137. if(vb->localstore)free(vb->localstore);
  138. memset(vb,0,sizeof(vorbis_block));
  139. return(0);
  140. }
  141. /* Analysis side code, but directly related to blocking. Thus it's
  142. here and not in analysis.c (which is for analysis transforms only).
  143. The init is here because some of it is shared */
  144. static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){
  145. int i;
  146. memset(v,0,sizeof(vorbis_dsp_state));
  147. v->vi=vi;
  148. v->modebits=ilog2(vi->modes);
  149. v->transform[0]=calloc(VI_TRANSFORMB,sizeof(vorbis_look_transform *));
  150. v->transform[1]=calloc(VI_TRANSFORMB,sizeof(vorbis_look_transform *));
  151. /* MDCT is tranform 0 */
  152. v->transform[0][0]=calloc(1,sizeof(mdct_lookup));
  153. v->transform[1][0]=calloc(1,sizeof(mdct_lookup));
  154. mdct_init(v->transform[0][0],vi->blocksizes[0]);
  155. mdct_init(v->transform[1][0],vi->blocksizes[1]);
  156. v->window[0][0][0]=calloc(VI_WINDOWB,sizeof(float *));
  157. v->window[0][0][1]=v->window[0][0][0];
  158. v->window[0][1][0]=v->window[0][0][0];
  159. v->window[0][1][1]=v->window[0][0][0];
  160. v->window[1][0][0]=calloc(VI_WINDOWB,sizeof(float *));
  161. v->window[1][0][1]=calloc(VI_WINDOWB,sizeof(float *));
  162. v->window[1][1][0]=calloc(VI_WINDOWB,sizeof(float *));
  163. v->window[1][1][1]=calloc(VI_WINDOWB,sizeof(float *));
  164. for(i=0;i<VI_WINDOWB;i++){
  165. v->window[0][0][0][i]=
  166. _vorbis_window(i,vi->blocksizes[0],vi->blocksizes[0]/2,vi->blocksizes[0]/2);
  167. v->window[1][0][0][i]=
  168. _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[0]/2,vi->blocksizes[0]/2);
  169. v->window[1][0][1][i]=
  170. _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[0]/2,vi->blocksizes[1]/2);
  171. v->window[1][1][0][i]=
  172. _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[1]/2,vi->blocksizes[0]/2);
  173. v->window[1][1][1][i]=
  174. _vorbis_window(i,vi->blocksizes[1],vi->blocksizes[1]/2,vi->blocksizes[1]/2);
  175. }
  176. if(encp){ /* encode/decode differ here */
  177. /* finish the codebooks */
  178. v->fullbooks=calloc(vi->books,sizeof(codebook));
  179. for(i=0;i<vi->books;i++)
  180. vorbis_book_init_encode(v->fullbooks+i,vi->book_param[i]);
  181. v->analysisp=1;
  182. }else{
  183. /* finish the codebooks */
  184. v->fullbooks=calloc(vi->books,sizeof(codebook));
  185. for(i=0;i<vi->books;i++)
  186. vorbis_book_init_decode(v->fullbooks+i,vi->book_param[i]);
  187. }
  188. /* initialize the storage vectors to a decent size greater than the
  189. minimum */
  190. v->pcm_storage=8192; /* we'll assume later that we have
  191. a minimum of twice the blocksize of
  192. accumulated samples in analysis */
  193. v->pcm=malloc(vi->channels*sizeof(float *));
  194. v->pcmret=malloc(vi->channels*sizeof(float *));
  195. {
  196. int i;
  197. for(i=0;i<vi->channels;i++)
  198. v->pcm[i]=calloc(v->pcm_storage,sizeof(float));
  199. }
  200. /* all 1 (large block) or 0 (small block) */
  201. /* explicitly set for the sake of clarity */
  202. v->lW=0; /* previous window size */
  203. v->W=0; /* current window size */
  204. /* all vector indexes */
  205. v->centerW=vi->blocksizes[1]/2;
  206. v->pcm_current=v->centerW;
  207. /* initialize all the mapping/backend lookups */
  208. v->mode=calloc(vi->modes,sizeof(vorbis_look_mapping *));
  209. for(i=0;i<vi->modes;i++){
  210. int mapnum=vi->mode_param[i]->mapping;
  211. int maptype=vi->map_type[mapnum];
  212. v->mode[i]=_mapping_P[maptype]->look(v,vi->mode_param[i],
  213. vi->map_param[mapnum]);
  214. }
  215. return(0);
  216. }
  217. /* arbitrary settings and spec-mandated numbers get filled in here */
  218. int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
  219. _vds_shared_init(v,vi,1);
  220. /* Initialize the envelope state storage */
  221. v->ve=calloc(1,sizeof(envelope_lookup));
  222. _ve_envelope_init(v->ve,vi);
  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. free(v->window[0][0][0]);
  233. for(j=0;j<2;j++)
  234. for(k=0;k<2;k++){
  235. for(i=0;i<VI_WINDOWB;i++)
  236. if(v->window[1][j][k][i])free(v->window[1][j][k][i]);
  237. free(v->window[1][j][k]);
  238. }
  239. }
  240. if(v->pcm){
  241. for(i=0;i<vi->channels;i++)
  242. if(v->pcm[i])free(v->pcm[i]);
  243. free(v->pcm);
  244. if(v->pcmret)free(v->pcmret);
  245. }
  246. if(v->ve){
  247. _ve_envelope_clear(v->ve);
  248. free(v->ve);
  249. }
  250. if(v->transform[0]){
  251. mdct_clear(v->transform[0][0]);
  252. free(v->transform[0][0]);
  253. free(v->transform[0]);
  254. }
  255. if(v->transform[1]){
  256. mdct_clear(v->transform[1][0]);
  257. free(v->transform[1][0]);
  258. free(v->transform[1]);
  259. }
  260. /* free mode lookups; these are actually vorbis_look_mapping structs */
  261. if(vi){
  262. for(i=0;i<vi->modes;i++){
  263. int mapnum=vi->mode_param[i]->mapping;
  264. int maptype=vi->map_type[mapnum];
  265. _mapping_P[maptype]->free_look(v->mode[i]);
  266. }
  267. /* free codebooks */
  268. for(i=0;i<vi->books;i++)
  269. vorbis_book_clear(v->fullbooks+i);
  270. }
  271. if(v->mode)free(v->mode);
  272. if(v->fullbooks)free(v->fullbooks);
  273. /* free header, header1, header2 */
  274. if(v->header)free(v->header);
  275. if(v->header1)free(v->header1);
  276. if(v->header2)free(v->header2);
  277. memset(v,0,sizeof(vorbis_dsp_state));
  278. }
  279. }
  280. float **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
  281. int i;
  282. vorbis_info *vi=v->vi;
  283. /* free header, header1, header2 */
  284. if(v->header)free(v->header);v->header=NULL;
  285. if(v->header1)free(v->header1);v->header1=NULL;
  286. if(v->header2)free(v->header2);v->header2=NULL;
  287. /* Do we have enough storage space for the requested buffer? If not,
  288. expand the PCM (and envelope) storage */
  289. if(v->pcm_current+vals>=v->pcm_storage){
  290. v->pcm_storage=v->pcm_current+vals*2;
  291. for(i=0;i<vi->channels;i++){
  292. v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(float));
  293. }
  294. }
  295. for(i=0;i<vi->channels;i++)
  296. v->pcmret[i]=v->pcm[i]+v->pcm_current;
  297. return(v->pcmret);
  298. }
  299. static void _preextrapolate_helper(vorbis_dsp_state *v){
  300. int i;
  301. int order=32;
  302. float *lpc=alloca(order*sizeof(float));
  303. float *work=alloca(v->pcm_current*sizeof(float));
  304. long j;
  305. v->preextrapolate=1;
  306. if(v->pcm_current-v->centerW>order*2){ /* safety */
  307. for(i=0;i<v->vi->channels;i++){
  308. /* need to run the extrapolation in reverse! */
  309. for(j=0;j<v->pcm_current;j++)
  310. work[j]=v->pcm[i][v->pcm_current-j-1];
  311. /* prime as above */
  312. vorbis_lpc_from_data(work,lpc,v->pcm_current-v->centerW,order);
  313. /* run the predictor filter */
  314. vorbis_lpc_predict(lpc,work+v->pcm_current-v->centerW-order,
  315. order,
  316. work+v->pcm_current-v->centerW,
  317. v->centerW);
  318. for(j=0;j<v->pcm_current;j++)
  319. v->pcm[i][v->pcm_current-j-1]=work[j];
  320. }
  321. }
  322. }
  323. /* call with val<=0 to set eof */
  324. int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
  325. vorbis_info *vi=v->vi;
  326. if(vals<=0){
  327. int order=32;
  328. int i;
  329. float *lpc=alloca(order*sizeof(float));
  330. /* if it wasn't done earlier (very short sample) */
  331. if(!v->preextrapolate)
  332. _preextrapolate_helper(v);
  333. /* We're encoding the end of the stream. Just make sure we have
  334. [at least] a full block of zeroes at the end. */
  335. /* actually, we don't want zeroes; that could drop a large
  336. amplitude off a cliff, creating spread spectrum noise that will
  337. suck to encode. Extrapolate for the sake of cleanliness. */
  338. vorbis_analysis_buffer(v,v->vi->blocksizes[1]*2);
  339. v->eofflag=v->pcm_current;
  340. v->pcm_current+=v->vi->blocksizes[1]*2;
  341. for(i=0;i<vi->channels;i++){
  342. if(v->eofflag>order*2){
  343. /* extrapolate with LPC to fill in */
  344. long n;
  345. /* make a predictor filter */
  346. n=v->eofflag;
  347. if(n>v->vi->blocksizes[1])n=v->vi->blocksizes[1];
  348. vorbis_lpc_from_data(v->pcm[i]+v->eofflag-n,lpc,n,order);
  349. /* run the predictor filter */
  350. vorbis_lpc_predict(lpc,v->pcm[i]+v->eofflag-order,order,
  351. v->pcm[i]+v->eofflag,v->pcm_current-v->eofflag);
  352. }else{
  353. /* not enough data to extrapolate (unlikely to happen due to
  354. guarding the overlap, but bulletproof in case that
  355. assumtion goes away). zeroes will do. */
  356. memset(v->pcm[i]+v->eofflag,0,
  357. (v->pcm_current-v->eofflag)*sizeof(float));
  358. }
  359. }
  360. }else{
  361. if(v->pcm_current+vals>v->pcm_storage)
  362. return(-1);
  363. v->pcm_current+=vals;
  364. /* we may want to reverse extrapolate the beginning of a stream
  365. too... in case we're beginning on a cliff! */
  366. /* clumsy, but simple. It only runs once, so simple is good. */
  367. if(!v->preextrapolate && v->pcm_current-v->centerW>v->vi->blocksizes[1])
  368. _preextrapolate_helper(v);
  369. }
  370. return(0);
  371. }
  372. /* do the deltas, envelope shaping, pre-echo and determine the size of
  373. the next block on which to continue analysis */
  374. int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
  375. int i;
  376. vorbis_info *vi=v->vi;
  377. long beginW=v->centerW-vi->blocksizes[v->W]/2,centerNext;
  378. /* check to see if we're started... */
  379. if(!v->preextrapolate)return(0);
  380. /* check to see if we're done... */
  381. if(v->eofflag==-1)return(0);
  382. /* By our invariant, we have lW, W and centerW set. Search for
  383. the next boundary so we can determine nW (the next window size)
  384. which lets us compute the shape of the current block's window */
  385. if(vi->blocksizes[0]<vi->blocksizes[1]){
  386. long largebound;
  387. long bp;
  388. if(v->W)
  389. /* min boundary; nW large, next small */
  390. largebound=v->centerW+vi->blocksizes[1]*3/4+vi->blocksizes[0]/4;
  391. else
  392. /* min boundary; nW large, next small */
  393. largebound=v->centerW+vi->blocksizes[1]*3/4+vi->blocksizes[0]*3/4;
  394. bp=_ve_envelope_search(v,largebound);
  395. if(bp==-1)return(0); /* not enough data currently to search for a
  396. full long block */
  397. v->nW=bp;
  398. }else
  399. v->nW=0;
  400. centerNext=v->centerW+vi->blocksizes[v->W]/4+vi->blocksizes[v->nW]/4;
  401. {
  402. /* center of next block + next block maximum right side. */
  403. long blockbound=centerNext+vi->blocksizes[v->nW]/2;
  404. if(v->pcm_current<blockbound)return(0); /* not enough data yet;
  405. although this check is
  406. less strict that the
  407. _ve_envelope_search,
  408. the search is not run
  409. if we only use one
  410. block size */
  411. }
  412. /* fill in the block. Note that for a short window, lW and nW are *short*
  413. regardless of actual settings in the stream */
  414. _vorbis_block_ripcord(vb);
  415. if(v->W){
  416. vb->lW=v->lW;
  417. vb->W=v->W;
  418. vb->nW=v->nW;
  419. }else{
  420. vb->lW=0;
  421. vb->W=v->W;
  422. vb->nW=0;
  423. }
  424. vb->vd=v;
  425. vb->sequence=v->sequence;
  426. vb->granulepos=v->granulepos;
  427. vb->pcmend=vi->blocksizes[v->W];
  428. /* copy the vectors; this uses the local storage in vb */
  429. {
  430. vb->pcm=_vorbis_block_alloc(vb,sizeof(float *)*vi->channels);
  431. for(i=0;i<vi->channels;i++){
  432. vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(float));
  433. memcpy(vb->pcm[i],v->pcm[i]+beginW,vi->blocksizes[v->W]*sizeof(float));
  434. }
  435. }
  436. /* handle eof detection: eof==0 means that we've not yet received EOF
  437. eof>0 marks the last 'real' sample in pcm[]
  438. eof<0 'no more to do'; doesn't get here */
  439. if(v->eofflag){
  440. if(v->centerW>=v->eofflag){
  441. v->eofflag=-1;
  442. vb->eofflag=1;
  443. return(1);
  444. }
  445. }
  446. /* advance storage vectors and clean up */
  447. {
  448. int new_centerNext=vi->blocksizes[1]/2;
  449. int movementW=centerNext-new_centerNext;
  450. _ve_envelope_shift(v->ve,movementW);
  451. v->pcm_current-=movementW;
  452. for(i=0;i<vi->channels;i++)
  453. memmove(v->pcm[i],v->pcm[i]+movementW,
  454. v->pcm_current*sizeof(float));
  455. v->lW=v->W;
  456. v->W=v->nW;
  457. v->centerW=new_centerNext;
  458. v->sequence++;
  459. if(v->eofflag){
  460. v->eofflag-=movementW;
  461. /* do not add padding to end of stream! */
  462. if(v->centerW>=v->eofflag){
  463. v->granulepos+=movementW-(v->centerW-v->eofflag);
  464. }else{
  465. v->granulepos+=movementW;
  466. }
  467. }else{
  468. v->granulepos+=movementW;
  469. }
  470. }
  471. /* done */
  472. return(1);
  473. }
  474. int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
  475. _vds_shared_init(v,vi,0);
  476. /* Adjust centerW to allow an easier mechanism for determining output */
  477. v->pcm_returned=v->centerW;
  478. v->centerW-= vi->blocksizes[v->W]/4+vi->blocksizes[v->lW]/4;
  479. v->granulepos=-1;
  480. v->sequence=-1;
  481. return(0);
  482. }
  483. /* Unlike in analysis, the window is only partially applied for each
  484. block. The time domain envelope is not yet handled at the point of
  485. calling (as it relies on the previous block). */
  486. int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
  487. vorbis_info *vi=v->vi;
  488. /* Shift out any PCM that we returned previously */
  489. /* centerW is currently the center of the last block added */
  490. if(v->pcm_returned && v->centerW>vi->blocksizes[1]/2){
  491. /* don't shift too much; we need to have a minimum PCM buffer of
  492. 1/2 long block */
  493. int shiftPCM=v->centerW-vi->blocksizes[1]/2;
  494. shiftPCM=(v->pcm_returned<shiftPCM?v->pcm_returned:shiftPCM);
  495. v->pcm_current-=shiftPCM;
  496. v->centerW-=shiftPCM;
  497. v->pcm_returned-=shiftPCM;
  498. if(shiftPCM){
  499. int i;
  500. for(i=0;i<vi->channels;i++)
  501. memmove(v->pcm[i],v->pcm[i]+shiftPCM,
  502. v->pcm_current*sizeof(float));
  503. }
  504. }
  505. v->lW=v->W;
  506. v->W=vb->W;
  507. v->nW=-1;
  508. v->glue_bits+=vb->glue_bits;
  509. v->time_bits+=vb->time_bits;
  510. v->floor_bits+=vb->floor_bits;
  511. v->res_bits+=vb->res_bits;
  512. if(v->sequence+1 != vb->sequence)v->granulepos=-1; /* out of sequence;
  513. lose count */
  514. v->sequence=vb->sequence;
  515. {
  516. int sizeW=vi->blocksizes[v->W];
  517. int centerW=v->centerW+vi->blocksizes[v->lW]/4+sizeW/4;
  518. int beginW=centerW-sizeW/2;
  519. int endW=beginW+sizeW;
  520. int beginSl;
  521. int endSl;
  522. int i,j;
  523. /* Do we have enough PCM/mult storage for the block? */
  524. if(endW>v->pcm_storage){
  525. /* expand the storage */
  526. v->pcm_storage=endW+vi->blocksizes[1];
  527. for(i=0;i<vi->channels;i++)
  528. v->pcm[i]=realloc(v->pcm[i],v->pcm_storage*sizeof(float));
  529. }
  530. /* overlap/add PCM */
  531. switch(v->W){
  532. case 0:
  533. beginSl=0;
  534. endSl=vi->blocksizes[0]/2;
  535. break;
  536. case 1:
  537. beginSl=vi->blocksizes[1]/4-vi->blocksizes[v->lW]/4;
  538. endSl=beginSl+vi->blocksizes[v->lW]/2;
  539. break;
  540. }
  541. for(j=0;j<vi->channels;j++){
  542. float *pcm=v->pcm[j]+beginW;
  543. float *p=vb->pcm[j];
  544. /* the overlap/add section */
  545. for(i=beginSl;i<endSl;i++)
  546. pcm[i]+=p[i];
  547. /* the remaining section */
  548. for(;i<sizeW;i++)
  549. pcm[i]=p[i];
  550. }
  551. /* track the frame number... This is for convenience, but also
  552. making sure our last packet doesn't end with added padding. If
  553. the last packet is partial, the number of samples we'll have to
  554. return will be past the vb->granulepos.
  555. This is not foolproof! It will be confused if we begin
  556. decoding at the last page after a seek or hole. In that case,
  557. we don't have a starting point to judge where the last frame
  558. is. For this reason, vorbisfile will always try to make sure
  559. it reads the last two marked pages in proper sequence */
  560. if(v->granulepos==-1)
  561. v->granulepos=vb->granulepos;
  562. else{
  563. v->granulepos+=(centerW-v->centerW);
  564. if(vb->granulepos!=-1 && v->granulepos!=vb->granulepos){
  565. if(v->granulepos>vb->granulepos && vb->eofflag){
  566. /* partial last frame. Strip the padding off */
  567. centerW-=(v->granulepos-vb->granulepos);
  568. }/* else{ Shouldn't happen *unless* the bitstream is out of
  569. spec. Either way, believe the bitstream } */
  570. v->granulepos=vb->granulepos;
  571. }
  572. }
  573. /* Update, cleanup */
  574. v->centerW=centerW;
  575. v->pcm_current=endW;
  576. if(vb->eofflag)v->eofflag=1;
  577. }
  578. return(0);
  579. }
  580. /* pcm==NULL indicates we just want the pending samples, no more */
  581. int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm){
  582. vorbis_info *vi=v->vi;
  583. if(v->pcm_returned<v->centerW){
  584. if(pcm){
  585. int i;
  586. for(i=0;i<vi->channels;i++)
  587. v->pcmret[i]=v->pcm[i]+v->pcm_returned;
  588. *pcm=v->pcmret;
  589. }
  590. return(v->centerW-v->pcm_returned);
  591. }
  592. return(0);
  593. }
  594. int vorbis_synthesis_read(vorbis_dsp_state *v,int bytes){
  595. if(bytes && v->pcm_returned+bytes>v->centerW)return(-1);
  596. v->pcm_returned+=bytes;
  597. return(0);
  598. }