block.c 23 KB

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