block.c 24 KB

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