block.c 22 KB

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