block.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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-2009 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: PCM data vector blocking, windowing and dis/reassembly
  13. last mod: $Id: block.c 19031 2013-12-03 19:20:50Z tterribe $
  14. Handle windowing, overlap-add, etc of the PCM vectors. This is made
  15. more amusing by Vorbis' current two allowed block sizes.
  16. ********************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ogg/ogg.h>
  21. #include "vorbis/codec.h"
  22. #include "codec_internal.h"
  23. #include "window.h"
  24. #include "mdct.h"
  25. #include "lpc.h"
  26. #include "registry.h"
  27. #include "misc.h"
  28. static int ilog2(unsigned int v){
  29. int ret=0;
  30. if(v)--v;
  31. while(v){
  32. ret++;
  33. v>>=1;
  34. }
  35. return(ret);
  36. }
  37. /* pcm accumulator examples (not exhaustive):
  38. <-------------- lW ---------------->
  39. <--------------- W ---------------->
  40. : .....|..... _______________ |
  41. : .''' | '''_--- | |\ |
  42. :.....''' |_____--- '''......| | \_______|
  43. :.................|__________________|_______|__|______|
  44. |<------ Sl ------>| > Sr < |endW
  45. |beginSl |endSl | |endSr
  46. |beginW |endlW |beginSr
  47. |< lW >|
  48. <--------------- W ---------------->
  49. | | .. ______________ |
  50. | | ' `/ | ---_ |
  51. |___.'___/`. | ---_____|
  52. |_______|__|_______|_________________|
  53. | >|Sl|< |<------ Sr ----->|endW
  54. | | |endSl |beginSr |endSr
  55. |beginW | |endlW
  56. mult[0] |beginSl mult[n]
  57. <-------------- lW ----------------->
  58. |<--W-->|
  59. : .............. ___ | |
  60. : .''' |`/ \ | |
  61. :.....''' |/`....\|...|
  62. :.........................|___|___|___|
  63. |Sl |Sr |endW
  64. | | |endSr
  65. | |beginSr
  66. | |endSl
  67. |beginSl
  68. |beginW
  69. */
  70. /* block abstraction setup *********************************************/
  71. #ifndef WORD_ALIGN
  72. #define WORD_ALIGN 8
  73. #endif
  74. int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
  75. int i;
  76. memset(vb,0,sizeof(*vb));
  77. vb->vd=v;
  78. vb->localalloc=0;
  79. vb->localstore=NULL;
  80. if(v->analysisp){
  81. vorbis_block_internal *vbi=
  82. vb->internal=_ogg_calloc(1,sizeof(vorbis_block_internal));
  83. vbi->ampmax=-9999;
  84. for(i=0;i<PACKETBLOBS;i++){
  85. if(i==PACKETBLOBS/2){
  86. vbi->packetblob[i]=&vb->opb;
  87. }else{
  88. vbi->packetblob[i]=
  89. _ogg_calloc(1,sizeof(oggpack_buffer));
  90. }
  91. oggpack_writeinit(vbi->packetblob[i]);
  92. }
  93. }
  94. return(0);
  95. }
  96. void *_vorbis_block_alloc(vorbis_block *vb,long bytes){
  97. bytes=(bytes+(WORD_ALIGN-1)) & ~(WORD_ALIGN-1);
  98. if(bytes+vb->localtop>vb->localalloc){
  99. /* can't just _ogg_realloc... there are outstanding pointers */
  100. if(vb->localstore){
  101. struct alloc_chain *link=_ogg_malloc(sizeof(*link));
  102. vb->totaluse+=vb->localtop;
  103. link->next=vb->reap;
  104. link->ptr=vb->localstore;
  105. vb->reap=link;
  106. }
  107. /* highly conservative */
  108. vb->localalloc=bytes;
  109. vb->localstore=_ogg_malloc(vb->localalloc);
  110. vb->localtop=0;
  111. }
  112. {
  113. void *ret=(void *)(((char *)vb->localstore)+vb->localtop);
  114. vb->localtop+=bytes;
  115. return ret;
  116. }
  117. }
  118. /* reap the chain, pull the ripcord */
  119. void _vorbis_block_ripcord(vorbis_block *vb){
  120. /* reap the chain */
  121. struct alloc_chain *reap=vb->reap;
  122. while(reap){
  123. struct alloc_chain *next=reap->next;
  124. _ogg_free(reap->ptr);
  125. memset(reap,0,sizeof(*reap));
  126. _ogg_free(reap);
  127. reap=next;
  128. }
  129. /* consolidate storage */
  130. if(vb->totaluse){
  131. vb->localstore=_ogg_realloc(vb->localstore,vb->totaluse+vb->localalloc);
  132. vb->localalloc+=vb->totaluse;
  133. vb->totaluse=0;
  134. }
  135. /* pull the ripcord */
  136. vb->localtop=0;
  137. vb->reap=NULL;
  138. }
  139. int vorbis_block_clear(vorbis_block *vb){
  140. int i;
  141. vorbis_block_internal *vbi=vb->internal;
  142. _vorbis_block_ripcord(vb);
  143. if(vb->localstore)_ogg_free(vb->localstore);
  144. if(vbi){
  145. for(i=0;i<PACKETBLOBS;i++){
  146. oggpack_writeclear(vbi->packetblob[i]);
  147. if(i!=PACKETBLOBS/2)_ogg_free(vbi->packetblob[i]);
  148. }
  149. _ogg_free(vbi);
  150. }
  151. memset(vb,0,sizeof(*vb));
  152. return(0);
  153. }
  154. /* Analysis side code, but directly related to blocking. Thus it's
  155. here and not in analysis.c (which is for analysis transforms only).
  156. The init is here because some of it is shared */
  157. static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){
  158. int i;
  159. codec_setup_info *ci=vi->codec_setup;
  160. private_state *b=NULL;
  161. int hs;
  162. if(ci==NULL) return 1;
  163. hs=ci->halfrate_flag;
  164. memset(v,0,sizeof(*v));
  165. b=v->backend_state=_ogg_calloc(1,sizeof(*b));
  166. v->vi=vi;
  167. b->modebits=ilog2(ci->modes);
  168. b->transform[0]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[0]));
  169. b->transform[1]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[1]));
  170. /* MDCT is tranform 0 */
  171. b->transform[0][0]=_ogg_calloc(1,sizeof(mdct_lookup));
  172. b->transform[1][0]=_ogg_calloc(1,sizeof(mdct_lookup));
  173. mdct_init(b->transform[0][0],ci->blocksizes[0]>>hs);
  174. mdct_init(b->transform[1][0],ci->blocksizes[1]>>hs);
  175. /* Vorbis I uses only window type 0 */
  176. b->window[0]=ilog2(ci->blocksizes[0])-6;
  177. b->window[1]=ilog2(ci->blocksizes[1])-6;
  178. if(encp){ /* encode/decode differ here */
  179. /* analysis always needs an fft */
  180. drft_init(&b->fft_look[0],ci->blocksizes[0]);
  181. drft_init(&b->fft_look[1],ci->blocksizes[1]);
  182. /* finish the codebooks */
  183. if(!ci->fullbooks){
  184. ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks));
  185. for(i=0;i<ci->books;i++)
  186. vorbis_book_init_encode(ci->fullbooks+i,ci->book_param[i]);
  187. }
  188. b->psy=_ogg_calloc(ci->psys,sizeof(*b->psy));
  189. for(i=0;i<ci->psys;i++){
  190. _vp_psy_init(b->psy+i,
  191. ci->psy_param[i],
  192. &ci->psy_g_param,
  193. ci->blocksizes[ci->psy_param[i]->blockflag]/2,
  194. vi->rate);
  195. }
  196. v->analysisp=1;
  197. }else{
  198. /* finish the codebooks */
  199. if(!ci->fullbooks){
  200. ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks));
  201. for(i=0;i<ci->books;i++){
  202. if(ci->book_param[i]==NULL)
  203. goto abort_books;
  204. if(vorbis_book_init_decode(ci->fullbooks+i,ci->book_param[i]))
  205. goto abort_books;
  206. /* decode codebooks are now standalone after init */
  207. vorbis_staticbook_destroy(ci->book_param[i]);
  208. ci->book_param[i]=NULL;
  209. }
  210. }
  211. }
  212. /* initialize the storage vectors. blocksize[1] is small for encode,
  213. but the correct size for decode */
  214. v->pcm_storage=ci->blocksizes[1];
  215. v->pcm=_ogg_malloc(vi->channels*sizeof(*v->pcm));
  216. v->pcmret=_ogg_malloc(vi->channels*sizeof(*v->pcmret));
  217. {
  218. int i;
  219. for(i=0;i<vi->channels;i++)
  220. v->pcm[i]=_ogg_calloc(v->pcm_storage,sizeof(*v->pcm[i]));
  221. }
  222. /* all 1 (large block) or 0 (small block) */
  223. /* explicitly set for the sake of clarity */
  224. v->lW=0; /* previous window size */
  225. v->W=0; /* current window size */
  226. /* all vector indexes */
  227. v->centerW=ci->blocksizes[1]/2;
  228. v->pcm_current=v->centerW;
  229. /* initialize all the backend lookups */
  230. b->flr=_ogg_calloc(ci->floors,sizeof(*b->flr));
  231. b->residue=_ogg_calloc(ci->residues,sizeof(*b->residue));
  232. for(i=0;i<ci->floors;i++)
  233. b->flr[i]=_floor_P[ci->floor_type[i]]->
  234. look(v,ci->floor_param[i]);
  235. for(i=0;i<ci->residues;i++)
  236. b->residue[i]=_residue_P[ci->residue_type[i]]->
  237. look(v,ci->residue_param[i]);
  238. return 0;
  239. abort_books:
  240. for(i=0;i<ci->books;i++){
  241. if(ci->book_param[i]!=NULL){
  242. vorbis_staticbook_destroy(ci->book_param[i]);
  243. ci->book_param[i]=NULL;
  244. }
  245. }
  246. vorbis_dsp_clear(v);
  247. return -1;
  248. }
  249. /* arbitrary settings and spec-mandated numbers get filled in here */
  250. int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
  251. private_state *b=NULL;
  252. if(_vds_shared_init(v,vi,1))return 1;
  253. b=v->backend_state;
  254. b->psy_g_look=_vp_global_look(vi);
  255. /* Initialize the envelope state storage */
  256. b->ve=_ogg_calloc(1,sizeof(*b->ve));
  257. _ve_envelope_init(b->ve,vi);
  258. vorbis_bitrate_init(vi,&b->bms);
  259. /* compressed audio packets start after the headers
  260. with sequence number 3 */
  261. v->sequence=3;
  262. return(0);
  263. }
  264. void vorbis_dsp_clear(vorbis_dsp_state *v){
  265. int i;
  266. if(v){
  267. vorbis_info *vi=v->vi;
  268. codec_setup_info *ci=(vi?vi->codec_setup:NULL);
  269. private_state *b=v->backend_state;
  270. if(b){
  271. if(b->ve){
  272. _ve_envelope_clear(b->ve);
  273. _ogg_free(b->ve);
  274. }
  275. if(b->transform[0]){
  276. mdct_clear(b->transform[0][0]);
  277. _ogg_free(b->transform[0][0]);
  278. _ogg_free(b->transform[0]);
  279. }
  280. if(b->transform[1]){
  281. mdct_clear(b->transform[1][0]);
  282. _ogg_free(b->transform[1][0]);
  283. _ogg_free(b->transform[1]);
  284. }
  285. if(b->flr){
  286. if(ci)
  287. for(i=0;i<ci->floors;i++)
  288. _floor_P[ci->floor_type[i]]->
  289. free_look(b->flr[i]);
  290. _ogg_free(b->flr);
  291. }
  292. if(b->residue){
  293. if(ci)
  294. for(i=0;i<ci->residues;i++)
  295. _residue_P[ci->residue_type[i]]->
  296. free_look(b->residue[i]);
  297. _ogg_free(b->residue);
  298. }
  299. if(b->psy){
  300. if(ci)
  301. for(i=0;i<ci->psys;i++)
  302. _vp_psy_clear(b->psy+i);
  303. _ogg_free(b->psy);
  304. }
  305. if(b->psy_g_look)_vp_global_free(b->psy_g_look);
  306. vorbis_bitrate_clear(&b->bms);
  307. drft_clear(&b->fft_look[0]);
  308. drft_clear(&b->fft_look[1]);
  309. }
  310. if(v->pcm){
  311. if(vi)
  312. for(i=0;i<vi->channels;i++)
  313. if(v->pcm[i])_ogg_free(v->pcm[i]);
  314. _ogg_free(v->pcm);
  315. if(v->pcmret)_ogg_free(v->pcmret);
  316. }
  317. if(b){
  318. /* free header, header1, header2 */
  319. if(b->header)_ogg_free(b->header);
  320. if(b->header1)_ogg_free(b->header1);
  321. if(b->header2)_ogg_free(b->header2);
  322. _ogg_free(b);
  323. }
  324. memset(v,0,sizeof(*v));
  325. }
  326. }
  327. float **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
  328. int i;
  329. vorbis_info *vi=v->vi;
  330. private_state *b=v->backend_state;
  331. /* free header, header1, header2 */
  332. if(b->header)_ogg_free(b->header);b->header=NULL;
  333. if(b->header1)_ogg_free(b->header1);b->header1=NULL;
  334. if(b->header2)_ogg_free(b->header2);b->header2=NULL;
  335. /* Do we have enough storage space for the requested buffer? If not,
  336. expand the PCM (and envelope) storage */
  337. if(v->pcm_current+vals>=v->pcm_storage){
  338. v->pcm_storage=v->pcm_current+vals*2;
  339. for(i=0;i<vi->channels;i++){
  340. v->pcm[i]=_ogg_realloc(v->pcm[i],v->pcm_storage*sizeof(*v->pcm[i]));
  341. }
  342. }
  343. for(i=0;i<vi->channels;i++)
  344. v->pcmret[i]=v->pcm[i]+v->pcm_current;
  345. return(v->pcmret);
  346. }
  347. static void _preextrapolate_helper(vorbis_dsp_state *v){
  348. int i;
  349. int order=16;
  350. float *lpc=alloca(order*sizeof(*lpc));
  351. float *work=alloca(v->pcm_current*sizeof(*work));
  352. long j;
  353. v->preextrapolate=1;
  354. if(v->pcm_current-v->centerW>order*2){ /* safety */
  355. for(i=0;i<v->vi->channels;i++){
  356. /* need to run the extrapolation in reverse! */
  357. for(j=0;j<v->pcm_current;j++)
  358. work[j]=v->pcm[i][v->pcm_current-j-1];
  359. /* prime as above */
  360. vorbis_lpc_from_data(work,lpc,v->pcm_current-v->centerW,order);
  361. #if 0
  362. if(v->vi->channels==2){
  363. if(i==0)
  364. _analysis_output("predataL",0,work,v->pcm_current-v->centerW,0,0,0);
  365. else
  366. _analysis_output("predataR",0,work,v->pcm_current-v->centerW,0,0,0);
  367. }else{
  368. _analysis_output("predata",0,work,v->pcm_current-v->centerW,0,0,0);
  369. }
  370. #endif
  371. /* run the predictor filter */
  372. vorbis_lpc_predict(lpc,work+v->pcm_current-v->centerW-order,
  373. order,
  374. work+v->pcm_current-v->centerW,
  375. v->centerW);
  376. for(j=0;j<v->pcm_current;j++)
  377. v->pcm[i][v->pcm_current-j-1]=work[j];
  378. }
  379. }
  380. }
  381. /* call with val<=0 to set eof */
  382. int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
  383. vorbis_info *vi=v->vi;
  384. codec_setup_info *ci=vi->codec_setup;
  385. if(vals<=0){
  386. int order=32;
  387. int i;
  388. float *lpc=alloca(order*sizeof(*lpc));
  389. /* if it wasn't done earlier (very short sample) */
  390. if(!v->preextrapolate)
  391. _preextrapolate_helper(v);
  392. /* We're encoding the end of the stream. Just make sure we have
  393. [at least] a few full blocks of zeroes at the end. */
  394. /* actually, we don't want zeroes; that could drop a large
  395. amplitude off a cliff, creating spread spectrum noise that will
  396. suck to encode. Extrapolate for the sake of cleanliness. */
  397. vorbis_analysis_buffer(v,ci->blocksizes[1]*3);
  398. v->eofflag=v->pcm_current;
  399. v->pcm_current+=ci->blocksizes[1]*3;
  400. for(i=0;i<vi->channels;i++){
  401. if(v->eofflag>order*2){
  402. /* extrapolate with LPC to fill in */
  403. long n;
  404. /* make a predictor filter */
  405. n=v->eofflag;
  406. if(n>ci->blocksizes[1])n=ci->blocksizes[1];
  407. vorbis_lpc_from_data(v->pcm[i]+v->eofflag-n,lpc,n,order);
  408. /* run the predictor filter */
  409. vorbis_lpc_predict(lpc,v->pcm[i]+v->eofflag-order,order,
  410. v->pcm[i]+v->eofflag,v->pcm_current-v->eofflag);
  411. }else{
  412. /* not enough data to extrapolate (unlikely to happen due to
  413. guarding the overlap, but bulletproof in case that
  414. assumtion goes away). zeroes will do. */
  415. memset(v->pcm[i]+v->eofflag,0,
  416. (v->pcm_current-v->eofflag)*sizeof(*v->pcm[i]));
  417. }
  418. }
  419. }else{
  420. if(v->pcm_current+vals>v->pcm_storage)
  421. return(OV_EINVAL);
  422. v->pcm_current+=vals;
  423. /* we may want to reverse extrapolate the beginning of a stream
  424. too... in case we're beginning on a cliff! */
  425. /* clumsy, but simple. It only runs once, so simple is good. */
  426. if(!v->preextrapolate && v->pcm_current-v->centerW>ci->blocksizes[1])
  427. _preextrapolate_helper(v);
  428. }
  429. return(0);
  430. }
  431. /* do the deltas, envelope shaping, pre-echo and determine the size of
  432. the next block on which to continue analysis */
  433. int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
  434. int i;
  435. vorbis_info *vi=v->vi;
  436. codec_setup_info *ci=vi->codec_setup;
  437. private_state *b=v->backend_state;
  438. vorbis_look_psy_global *g=b->psy_g_look;
  439. long beginW=v->centerW-ci->blocksizes[v->W]/2,centerNext;
  440. vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
  441. /* check to see if we're started... */
  442. if(!v->preextrapolate)return(0);
  443. /* check to see if we're done... */
  444. if(v->eofflag==-1)return(0);
  445. /* By our invariant, we have lW, W and centerW set. Search for
  446. the next boundary so we can determine nW (the next window size)
  447. which lets us compute the shape of the current block's window */
  448. /* we do an envelope search even on a single blocksize; we may still
  449. be throwing more bits at impulses, and envelope search handles
  450. marking impulses too. */
  451. {
  452. long bp=_ve_envelope_search(v);
  453. if(bp==-1){
  454. if(v->eofflag==0)return(0); /* not enough data currently to search for a
  455. full long block */
  456. v->nW=0;
  457. }else{
  458. if(ci->blocksizes[0]==ci->blocksizes[1])
  459. v->nW=0;
  460. else
  461. v->nW=bp;
  462. }
  463. }
  464. centerNext=v->centerW+ci->blocksizes[v->W]/4+ci->blocksizes[v->nW]/4;
  465. {
  466. /* center of next block + next block maximum right side. */
  467. long blockbound=centerNext+ci->blocksizes[v->nW]/2;
  468. if(v->pcm_current<blockbound)return(0); /* not enough data yet;
  469. although this check is
  470. less strict that the
  471. _ve_envelope_search,
  472. the search is not run
  473. if we only use one
  474. block size */
  475. }
  476. /* fill in the block. Note that for a short window, lW and nW are *short*
  477. regardless of actual settings in the stream */
  478. _vorbis_block_ripcord(vb);
  479. vb->lW=v->lW;
  480. vb->W=v->W;
  481. vb->nW=v->nW;
  482. if(v->W){
  483. if(!v->lW || !v->nW){
  484. vbi->blocktype=BLOCKTYPE_TRANSITION;
  485. /*fprintf(stderr,"-");*/
  486. }else{
  487. vbi->blocktype=BLOCKTYPE_LONG;
  488. /*fprintf(stderr,"_");*/
  489. }
  490. }else{
  491. if(_ve_envelope_mark(v)){
  492. vbi->blocktype=BLOCKTYPE_IMPULSE;
  493. /*fprintf(stderr,"|");*/
  494. }else{
  495. vbi->blocktype=BLOCKTYPE_PADDING;
  496. /*fprintf(stderr,".");*/
  497. }
  498. }
  499. vb->vd=v;
  500. vb->sequence=v->sequence++;
  501. vb->granulepos=v->granulepos;
  502. vb->pcmend=ci->blocksizes[v->W];
  503. /* copy the vectors; this uses the local storage in vb */
  504. /* this tracks 'strongest peak' for later psychoacoustics */
  505. /* moved to the global psy state; clean this mess up */
  506. if(vbi->ampmax>g->ampmax)g->ampmax=vbi->ampmax;
  507. g->ampmax=_vp_ampmax_decay(g->ampmax,v);
  508. vbi->ampmax=g->ampmax;
  509. vb->pcm=_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels);
  510. vbi->pcmdelay=_vorbis_block_alloc(vb,sizeof(*vbi->pcmdelay)*vi->channels);
  511. for(i=0;i<vi->channels;i++){
  512. vbi->pcmdelay[i]=
  513. _vorbis_block_alloc(vb,(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
  514. memcpy(vbi->pcmdelay[i],v->pcm[i],(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
  515. vb->pcm[i]=vbi->pcmdelay[i]+beginW;
  516. /* before we added the delay
  517. vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i]));
  518. memcpy(vb->pcm[i],v->pcm[i]+beginW,ci->blocksizes[v->W]*sizeof(*vb->pcm[i]));
  519. */
  520. }
  521. /* handle eof detection: eof==0 means that we've not yet received EOF
  522. eof>0 marks the last 'real' sample in pcm[]
  523. eof<0 'no more to do'; doesn't get here */
  524. if(v->eofflag){
  525. if(v->centerW>=v->eofflag){
  526. v->eofflag=-1;
  527. vb->eofflag=1;
  528. return(1);
  529. }
  530. }
  531. /* advance storage vectors and clean up */
  532. {
  533. int new_centerNext=ci->blocksizes[1]/2;
  534. int movementW=centerNext-new_centerNext;
  535. if(movementW>0){
  536. _ve_envelope_shift(b->ve,movementW);
  537. v->pcm_current-=movementW;
  538. for(i=0;i<vi->channels;i++)
  539. memmove(v->pcm[i],v->pcm[i]+movementW,
  540. v->pcm_current*sizeof(*v->pcm[i]));
  541. v->lW=v->W;
  542. v->W=v->nW;
  543. v->centerW=new_centerNext;
  544. if(v->eofflag){
  545. v->eofflag-=movementW;
  546. if(v->eofflag<=0)v->eofflag=-1;
  547. /* do not add padding to end of stream! */
  548. if(v->centerW>=v->eofflag){
  549. v->granulepos+=movementW-(v->centerW-v->eofflag);
  550. }else{
  551. v->granulepos+=movementW;
  552. }
  553. }else{
  554. v->granulepos+=movementW;
  555. }
  556. }
  557. }
  558. /* done */
  559. return(1);
  560. }
  561. int vorbis_synthesis_restart(vorbis_dsp_state *v){
  562. vorbis_info *vi=v->vi;
  563. codec_setup_info *ci;
  564. int hs;
  565. if(!v->backend_state)return -1;
  566. if(!vi)return -1;
  567. ci=vi->codec_setup;
  568. if(!ci)return -1;
  569. hs=ci->halfrate_flag;
  570. v->centerW=ci->blocksizes[1]>>(hs+1);
  571. v->pcm_current=v->centerW>>hs;
  572. v->pcm_returned=-1;
  573. v->granulepos=-1;
  574. v->sequence=-1;
  575. v->eofflag=0;
  576. ((private_state *)(v->backend_state))->sample_count=-1;
  577. return(0);
  578. }
  579. int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
  580. if(_vds_shared_init(v,vi,0)){
  581. vorbis_dsp_clear(v);
  582. return 1;
  583. }
  584. vorbis_synthesis_restart(v);
  585. return 0;
  586. }
  587. int vorbis_synthesis_samplecount(vorbis_dsp_state *v){
  588. return ((private_state *)v->backend_state)->sample_count;
  589. }
  590. /* Unlike in analysis, the window is only partially applied for each
  591. block. The time domain envelope is not yet handled at the point of
  592. calling (as it relies on the previous block). */
  593. int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
  594. vorbis_info *vi=v->vi;
  595. codec_setup_info *ci=vi->codec_setup;
  596. private_state *b=v->backend_state;
  597. int hs=ci->halfrate_flag;
  598. int i,j;
  599. if(!vb)return(OV_EINVAL);
  600. if(v->pcm_current>v->pcm_returned && v->pcm_returned!=-1)return(OV_EINVAL);
  601. v->lW=v->W;
  602. v->W=vb->W;
  603. v->nW=-1;
  604. if((v->sequence==-1)||
  605. (v->sequence+1 != vb->sequence)){
  606. v->granulepos=-1; /* out of sequence; lose count */
  607. b->sample_count=-1;
  608. }
  609. v->sequence=vb->sequence;
  610. if(vb->pcm){ /* no pcm to process if vorbis_synthesis_trackonly
  611. was called on block */
  612. int n=ci->blocksizes[v->W]>>(hs+1);
  613. int n0=ci->blocksizes[0]>>(hs+1);
  614. int n1=ci->blocksizes[1]>>(hs+1);
  615. int thisCenter;
  616. int prevCenter;
  617. v->glue_bits+=vb->glue_bits;
  618. v->time_bits+=vb->time_bits;
  619. v->floor_bits+=vb->floor_bits;
  620. v->res_bits+=vb->res_bits;
  621. if(v->centerW){
  622. thisCenter=n1;
  623. prevCenter=0;
  624. }else{
  625. thisCenter=0;
  626. prevCenter=n1;
  627. }
  628. /* v->pcm is now used like a two-stage double buffer. We don't want
  629. to have to constantly shift *or* adjust memory usage. Don't
  630. accept a new block until the old is shifted out */
  631. for(j=0;j<vi->channels;j++){
  632. /* the overlap/add section */
  633. if(v->lW){
  634. if(v->W){
  635. /* large/large */
  636. const float *w=_vorbis_window_get(b->window[1]-hs);
  637. float *pcm=v->pcm[j]+prevCenter;
  638. float *p=vb->pcm[j];
  639. for(i=0;i<n1;i++)
  640. pcm[i]=pcm[i]*w[n1-i-1] + p[i]*w[i];
  641. }else{
  642. /* large/small */
  643. const float *w=_vorbis_window_get(b->window[0]-hs);
  644. float *pcm=v->pcm[j]+prevCenter+n1/2-n0/2;
  645. float *p=vb->pcm[j];
  646. for(i=0;i<n0;i++)
  647. pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
  648. }
  649. }else{
  650. if(v->W){
  651. /* small/large */
  652. const float *w=_vorbis_window_get(b->window[0]-hs);
  653. float *pcm=v->pcm[j]+prevCenter;
  654. float *p=vb->pcm[j]+n1/2-n0/2;
  655. for(i=0;i<n0;i++)
  656. pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
  657. for(;i<n1/2+n0/2;i++)
  658. pcm[i]=p[i];
  659. }else{
  660. /* small/small */
  661. const float *w=_vorbis_window_get(b->window[0]-hs);
  662. float *pcm=v->pcm[j]+prevCenter;
  663. float *p=vb->pcm[j];
  664. for(i=0;i<n0;i++)
  665. pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
  666. }
  667. }
  668. /* the copy section */
  669. {
  670. float *pcm=v->pcm[j]+thisCenter;
  671. float *p=vb->pcm[j]+n;
  672. for(i=0;i<n;i++)
  673. pcm[i]=p[i];
  674. }
  675. }
  676. if(v->centerW)
  677. v->centerW=0;
  678. else
  679. v->centerW=n1;
  680. /* deal with initial packet state; we do this using the explicit
  681. pcm_returned==-1 flag otherwise we're sensitive to first block
  682. being short or long */
  683. if(v->pcm_returned==-1){
  684. v->pcm_returned=thisCenter;
  685. v->pcm_current=thisCenter;
  686. }else{
  687. v->pcm_returned=prevCenter;
  688. v->pcm_current=prevCenter+
  689. ((ci->blocksizes[v->lW]/4+
  690. ci->blocksizes[v->W]/4)>>hs);
  691. }
  692. }
  693. /* track the frame number... This is for convenience, but also
  694. making sure our last packet doesn't end with added padding. If
  695. the last packet is partial, the number of samples we'll have to
  696. return will be past the vb->granulepos.
  697. This is not foolproof! It will be confused if we begin
  698. decoding at the last page after a seek or hole. In that case,
  699. we don't have a starting point to judge where the last frame
  700. is. For this reason, vorbisfile will always try to make sure
  701. it reads the last two marked pages in proper sequence */
  702. if(b->sample_count==-1){
  703. b->sample_count=0;
  704. }else{
  705. b->sample_count+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4;
  706. }
  707. if(v->granulepos==-1){
  708. if(vb->granulepos!=-1){ /* only set if we have a position to set to */
  709. v->granulepos=vb->granulepos;
  710. /* is this a short page? */
  711. if(b->sample_count>v->granulepos){
  712. /* corner case; if this is both the first and last audio page,
  713. then spec says the end is cut, not beginning */
  714. long extra=b->sample_count-vb->granulepos;
  715. /* we use ogg_int64_t for granule positions because a
  716. uint64 isn't universally available. Unfortunately,
  717. that means granposes can be 'negative' and result in
  718. extra being negative */
  719. if(extra<0)
  720. extra=0;
  721. if(vb->eofflag){
  722. /* trim the end */
  723. /* no preceding granulepos; assume we started at zero (we'd
  724. have to in a short single-page stream) */
  725. /* granulepos could be -1 due to a seek, but that would result
  726. in a long count, not short count */
  727. /* Guard against corrupt/malicious frames that set EOP and
  728. a backdated granpos; don't rewind more samples than we
  729. actually have */
  730. if(extra > (v->pcm_current - v->pcm_returned)<<hs)
  731. extra = (v->pcm_current - v->pcm_returned)<<hs;
  732. v->pcm_current-=extra>>hs;
  733. }else{
  734. /* trim the beginning */
  735. v->pcm_returned+=extra>>hs;
  736. if(v->pcm_returned>v->pcm_current)
  737. v->pcm_returned=v->pcm_current;
  738. }
  739. }
  740. }
  741. }else{
  742. v->granulepos+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4;
  743. if(vb->granulepos!=-1 && v->granulepos!=vb->granulepos){
  744. if(v->granulepos>vb->granulepos){
  745. long extra=v->granulepos-vb->granulepos;
  746. if(extra)
  747. if(vb->eofflag){
  748. /* partial last frame. Strip the extra samples off */
  749. /* Guard against corrupt/malicious frames that set EOP and
  750. a backdated granpos; don't rewind more samples than we
  751. actually have */
  752. if(extra > (v->pcm_current - v->pcm_returned)<<hs)
  753. extra = (v->pcm_current - v->pcm_returned)<<hs;
  754. /* we use ogg_int64_t for granule positions because a
  755. uint64 isn't universally available. Unfortunately,
  756. that means granposes can be 'negative' and result in
  757. extra being negative */
  758. if(extra<0)
  759. extra=0;
  760. v->pcm_current-=extra>>hs;
  761. } /* else {Shouldn't happen *unless* the bitstream is out of
  762. spec. Either way, believe the bitstream } */
  763. } /* else {Shouldn't happen *unless* the bitstream is out of
  764. spec. Either way, believe the bitstream } */
  765. v->granulepos=vb->granulepos;
  766. }
  767. }
  768. /* Update, cleanup */
  769. if(vb->eofflag)v->eofflag=1;
  770. return(0);
  771. }
  772. /* pcm==NULL indicates we just want the pending samples, no more */
  773. int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm){
  774. vorbis_info *vi=v->vi;
  775. if(v->pcm_returned>-1 && v->pcm_returned<v->pcm_current){
  776. if(pcm){
  777. int i;
  778. for(i=0;i<vi->channels;i++)
  779. v->pcmret[i]=v->pcm[i]+v->pcm_returned;
  780. *pcm=v->pcmret;
  781. }
  782. return(v->pcm_current-v->pcm_returned);
  783. }
  784. return(0);
  785. }
  786. int vorbis_synthesis_read(vorbis_dsp_state *v,int n){
  787. if(n && v->pcm_returned+n>v->pcm_current)return(OV_EINVAL);
  788. v->pcm_returned+=n;
  789. return(0);
  790. }
  791. /* intended for use with a specific vorbisfile feature; we want access
  792. to the [usually synthetic/postextrapolated] buffer and lapping at
  793. the end of a decode cycle, specifically, a half-short-block worth.
  794. This funtion works like pcmout above, except it will also expose
  795. this implicit buffer data not normally decoded. */
  796. int vorbis_synthesis_lapout(vorbis_dsp_state *v,float ***pcm){
  797. vorbis_info *vi=v->vi;
  798. codec_setup_info *ci=vi->codec_setup;
  799. int hs=ci->halfrate_flag;
  800. int n=ci->blocksizes[v->W]>>(hs+1);
  801. int n0=ci->blocksizes[0]>>(hs+1);
  802. int n1=ci->blocksizes[1]>>(hs+1);
  803. int i,j;
  804. if(v->pcm_returned<0)return 0;
  805. /* our returned data ends at pcm_returned; because the synthesis pcm
  806. buffer is a two-fragment ring, that means our data block may be
  807. fragmented by buffering, wrapping or a short block not filling
  808. out a buffer. To simplify things, we unfragment if it's at all
  809. possibly needed. Otherwise, we'd need to call lapout more than
  810. once as well as hold additional dsp state. Opt for
  811. simplicity. */
  812. /* centerW was advanced by blockin; it would be the center of the
  813. *next* block */
  814. if(v->centerW==n1){
  815. /* the data buffer wraps; swap the halves */
  816. /* slow, sure, small */
  817. for(j=0;j<vi->channels;j++){
  818. float *p=v->pcm[j];
  819. for(i=0;i<n1;i++){
  820. float temp=p[i];
  821. p[i]=p[i+n1];
  822. p[i+n1]=temp;
  823. }
  824. }
  825. v->pcm_current-=n1;
  826. v->pcm_returned-=n1;
  827. v->centerW=0;
  828. }
  829. /* solidify buffer into contiguous space */
  830. if((v->lW^v->W)==1){
  831. /* long/short or short/long */
  832. for(j=0;j<vi->channels;j++){
  833. float *s=v->pcm[j];
  834. float *d=v->pcm[j]+(n1-n0)/2;
  835. for(i=(n1+n0)/2-1;i>=0;--i)
  836. d[i]=s[i];
  837. }
  838. v->pcm_returned+=(n1-n0)/2;
  839. v->pcm_current+=(n1-n0)/2;
  840. }else{
  841. if(v->lW==0){
  842. /* short/short */
  843. for(j=0;j<vi->channels;j++){
  844. float *s=v->pcm[j];
  845. float *d=v->pcm[j]+n1-n0;
  846. for(i=n0-1;i>=0;--i)
  847. d[i]=s[i];
  848. }
  849. v->pcm_returned+=n1-n0;
  850. v->pcm_current+=n1-n0;
  851. }
  852. }
  853. if(pcm){
  854. int i;
  855. for(i=0;i<vi->channels;i++)
  856. v->pcmret[i]=v->pcm[i]+v->pcm_returned;
  857. *pcm=v->pcmret;
  858. }
  859. return(n1+n-v->pcm_returned);
  860. }
  861. const float *vorbis_window(vorbis_dsp_state *v,int W){
  862. vorbis_info *vi=v->vi;
  863. codec_setup_info *ci=vi->codec_setup;
  864. int hs=ci->halfrate_flag;
  865. private_state *b=v->backend_state;
  866. if(b->window[W]-1<0)return NULL;
  867. return _vorbis_window_get(b->window[W]-hs);
  868. }