block.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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-2007 *
  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$
  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. vorbis_book_init_decode(ci->fullbooks+i,ci->book_param[i]);
  203. /* decode codebooks are now standalone after init */
  204. vorbis_staticbook_destroy(ci->book_param[i]);
  205. ci->book_param[i]=NULL;
  206. }
  207. }
  208. }
  209. /* initialize the storage vectors. blocksize[1] is small for encode,
  210. but the correct size for decode */
  211. v->pcm_storage=ci->blocksizes[1];
  212. v->pcm=_ogg_malloc(vi->channels*sizeof(*v->pcm));
  213. v->pcmret=_ogg_malloc(vi->channels*sizeof(*v->pcmret));
  214. {
  215. int i;
  216. for(i=0;i<vi->channels;i++)
  217. v->pcm[i]=_ogg_calloc(v->pcm_storage,sizeof(*v->pcm[i]));
  218. }
  219. /* all 1 (large block) or 0 (small block) */
  220. /* explicitly set for the sake of clarity */
  221. v->lW=0; /* previous window size */
  222. v->W=0; /* current window size */
  223. /* all vector indexes */
  224. v->centerW=ci->blocksizes[1]/2;
  225. v->pcm_current=v->centerW;
  226. /* initialize all the backend lookups */
  227. b->flr=_ogg_calloc(ci->floors,sizeof(*b->flr));
  228. b->residue=_ogg_calloc(ci->residues,sizeof(*b->residue));
  229. for(i=0;i<ci->floors;i++)
  230. b->flr[i]=_floor_P[ci->floor_type[i]]->
  231. look(v,ci->floor_param[i]);
  232. for(i=0;i<ci->residues;i++)
  233. b->residue[i]=_residue_P[ci->residue_type[i]]->
  234. look(v,ci->residue_param[i]);
  235. return 0;
  236. }
  237. /* arbitrary settings and spec-mandated numbers get filled in here */
  238. int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
  239. private_state *b=NULL;
  240. if(_vds_shared_init(v,vi,1))return 1;
  241. b=v->backend_state;
  242. b->psy_g_look=_vp_global_look(vi);
  243. /* Init (added by aoyumi) */
  244. b->nblock = _ogg_calloc((256*vi->channels), sizeof(*b->nblock));
  245. b->tblock = _ogg_calloc((256*vi->channels), sizeof(*b->tblock));
  246. b->lownoise_compand_level = _ogg_calloc(vi->channels, sizeof(*b->lownoise_compand_level));
  247. b->padnum = 0;
  248. /* Initialize the envelope state storage */
  249. b->ve=_ogg_calloc(1,sizeof(*b->ve));
  250. _ve_envelope_init(b->ve,vi);
  251. vorbis_bitrate_init(vi,&b->bms);
  252. /* compressed audio packets start after the headers
  253. with sequence number 3 */
  254. v->sequence=3;
  255. return(0);
  256. }
  257. void vorbis_dsp_clear(vorbis_dsp_state *v){
  258. int i;
  259. if(v){
  260. vorbis_info *vi=v->vi;
  261. codec_setup_info *ci=(vi?vi->codec_setup:NULL);
  262. private_state *b=v->backend_state;
  263. if(b){
  264. if(b->ve){
  265. _ve_envelope_clear(b->ve);
  266. _ogg_free(b->ve);
  267. }
  268. if(b->transform[0]){
  269. mdct_clear(b->transform[0][0]);
  270. _ogg_free(b->transform[0][0]);
  271. _ogg_free(b->transform[0]);
  272. }
  273. if(b->transform[1]){
  274. mdct_clear(b->transform[1][0]);
  275. _ogg_free(b->transform[1][0]);
  276. _ogg_free(b->transform[1]);
  277. }
  278. if(b->flr){
  279. if(ci)
  280. for(i=0;i<ci->floors;i++)
  281. _floor_P[ci->floor_type[i]]->
  282. free_look(b->flr[i]);
  283. _ogg_free(b->flr);
  284. }
  285. if(b->residue){
  286. if(ci)
  287. for(i=0;i<ci->residues;i++)
  288. _residue_P[ci->residue_type[i]]->
  289. free_look(b->residue[i]);
  290. _ogg_free(b->residue);
  291. }
  292. if(b->psy){
  293. if(ci)
  294. for(i=0;i<ci->psys;i++)
  295. _vp_psy_clear(b->psy+i);
  296. _ogg_free(b->psy);
  297. }
  298. if(b->psy_g_look)_vp_global_free(b->psy_g_look);
  299. vorbis_bitrate_clear(&b->bms);
  300. drft_clear(&b->fft_look[0]);
  301. drft_clear(&b->fft_look[1]);
  302. /* added by aoyumi */
  303. if(b->nblock) _ogg_free(b->nblock);
  304. if(b->tblock) _ogg_free(b->tblock);
  305. if(b->lownoise_compand_level) _ogg_free(b->lownoise_compand_level);
  306. }
  307. if(v->pcm){
  308. if(vi)
  309. for(i=0;i<vi->channels;i++)
  310. if(v->pcm[i])_ogg_free(v->pcm[i]);
  311. _ogg_free(v->pcm);
  312. if(v->pcmret)_ogg_free(v->pcmret);
  313. }
  314. if(b){
  315. /* free header, header1, header2 */
  316. if(b->header)_ogg_free(b->header);
  317. if(b->header1)_ogg_free(b->header1);
  318. if(b->header2)_ogg_free(b->header2);
  319. _ogg_free(b);
  320. }
  321. memset(v,0,sizeof(*v));
  322. }
  323. }
  324. float **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
  325. int i;
  326. vorbis_info *vi=v->vi;
  327. private_state *b=v->backend_state;
  328. /* free header, header1, header2 */
  329. if(b->header)_ogg_free(b->header);b->header=NULL;
  330. if(b->header1)_ogg_free(b->header1);b->header1=NULL;
  331. if(b->header2)_ogg_free(b->header2);b->header2=NULL;
  332. /* Do we have enough storage space for the requested buffer? If not,
  333. expand the PCM (and envelope) storage */
  334. if(v->pcm_current+vals>=v->pcm_storage){
  335. v->pcm_storage=v->pcm_current+vals*2;
  336. for(i=0;i<vi->channels;i++){
  337. v->pcm[i]=_ogg_realloc(v->pcm[i],v->pcm_storage*sizeof(*v->pcm[i]));
  338. }
  339. }
  340. for(i=0;i<vi->channels;i++)
  341. v->pcmret[i]=v->pcm[i]+v->pcm_current;
  342. return(v->pcmret);
  343. }
  344. static void _preextrapolate_helper(vorbis_dsp_state *v){
  345. int i;
  346. int order=16;
  347. float *lpc=alloca(order*sizeof(*lpc));
  348. float *work=alloca(v->pcm_current*sizeof(*work));
  349. long j;
  350. v->preextrapolate=1;
  351. if(v->pcm_current-v->centerW>order*2){ /* safety */
  352. for(i=0;i<v->vi->channels;i++){
  353. /* need to run the extrapolation in reverse! */
  354. for(j=0;j<v->pcm_current;j++)
  355. work[j]=v->pcm[i][v->pcm_current-j-1];
  356. /* prime as above */
  357. vorbis_lpc_from_data(work,lpc,v->pcm_current-v->centerW,order);
  358. #if 0
  359. if(v->vi->channels==2){
  360. if(i==0)
  361. _analysis_output("predataL",0,work,v->pcm_current-v->centerW,0,0,0);
  362. else
  363. _analysis_output("predataR",0,work,v->pcm_current-v->centerW,0,0,0);
  364. }else{
  365. _analysis_output("predata",0,work,v->pcm_current-v->centerW,0,0,0);
  366. }
  367. #endif
  368. /* run the predictor filter */
  369. vorbis_lpc_predict(lpc,work+v->pcm_current-v->centerW-order,
  370. order,
  371. work+v->pcm_current-v->centerW,
  372. v->centerW);
  373. for(j=0;j<v->pcm_current;j++)
  374. v->pcm[i][v->pcm_current-j-1]=work[j];
  375. }
  376. }
  377. }
  378. /* call with val<=0 to set eof */
  379. int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
  380. vorbis_info *vi=v->vi;
  381. codec_setup_info *ci=vi->codec_setup;
  382. if(vals<=0){
  383. int order=32;
  384. int i;
  385. float *lpc=alloca(order*sizeof(*lpc));
  386. /* if it wasn't done earlier (very short sample) */
  387. if(!v->preextrapolate)
  388. _preextrapolate_helper(v);
  389. /* We're encoding the end of the stream. Just make sure we have
  390. [at least] a few full blocks of zeroes at the end. */
  391. /* actually, we don't want zeroes; that could drop a large
  392. amplitude off a cliff, creating spread spectrum noise that will
  393. suck to encode. Extrapolate for the sake of cleanliness. */
  394. vorbis_analysis_buffer(v,ci->blocksizes[1]*3);
  395. v->eofflag=v->pcm_current;
  396. v->pcm_current+=ci->blocksizes[1]*3;
  397. for(i=0;i<vi->channels;i++){
  398. if(v->eofflag>order*2){
  399. /* extrapolate with LPC to fill in */
  400. long n;
  401. /* make a predictor filter */
  402. n=v->eofflag;
  403. if(n>ci->blocksizes[1])n=ci->blocksizes[1];
  404. vorbis_lpc_from_data(v->pcm[i]+v->eofflag-n,lpc,n,order);
  405. /* run the predictor filter */
  406. vorbis_lpc_predict(lpc,v->pcm[i]+v->eofflag-order,order,
  407. v->pcm[i]+v->eofflag,v->pcm_current-v->eofflag);
  408. }else{
  409. /* not enough data to extrapolate (unlikely to happen due to
  410. guarding the overlap, but bulletproof in case that
  411. assumtion goes away). zeroes will do. */
  412. memset(v->pcm[i]+v->eofflag,0,
  413. (v->pcm_current-v->eofflag)*sizeof(*v->pcm[i]));
  414. }
  415. }
  416. }else{
  417. if(v->pcm_current+vals>v->pcm_storage)
  418. return(OV_EINVAL);
  419. v->pcm_current+=vals;
  420. /* we may want to reverse extrapolate the beginning of a stream
  421. too... in case we're beginning on a cliff! */
  422. /* clumsy, but simple. It only runs once, so simple is good. */
  423. if(!v->preextrapolate && v->pcm_current-v->centerW>ci->blocksizes[1])
  424. _preextrapolate_helper(v);
  425. }
  426. return(0);
  427. }
  428. /* do the deltas, envelope shaping, pre-echo and determine the size of
  429. the next block on which to continue analysis */
  430. int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
  431. int i;
  432. vorbis_info *vi=v->vi;
  433. codec_setup_info *ci=vi->codec_setup;
  434. private_state *b=v->backend_state;
  435. vorbis_look_psy_global *g=b->psy_g_look;
  436. long beginW=v->centerW-ci->blocksizes[v->W]/2,centerNext;
  437. vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
  438. /* check to see if we're started... */
  439. if(!v->preextrapolate)return(0);
  440. /* check to see if we're done... */
  441. if(v->eofflag==-1)return(0);
  442. /* By our invariant, we have lW, W and centerW set. Search for
  443. the next boundary so we can determine nW (the next window size)
  444. which lets us compute the shape of the current block's window */
  445. /* we do an envelope search even on a single blocksize; we may still
  446. be throwing more bits at impulses, and envelope search handles
  447. marking impulses too. */
  448. {
  449. long bp=_ve_envelope_search(v);
  450. if(bp==-1){
  451. if(v->eofflag==0)return(0); /* not enough data currently to search for a
  452. full long block */
  453. v->nW=0;
  454. }else{
  455. if(ci->blocksizes[0]==ci->blocksizes[1])
  456. v->nW=0;
  457. else
  458. v->nW=bp;
  459. }
  460. }
  461. centerNext=v->centerW+ci->blocksizes[v->W]/4+ci->blocksizes[v->nW]/4;
  462. {
  463. /* center of next block + next block maximum right side. */
  464. long blockbound=centerNext+ci->blocksizes[v->nW]/2;
  465. if(v->pcm_current<blockbound)return(0); /* not enough data yet;
  466. although this check is
  467. less strict that the
  468. _ve_envelope_search,
  469. the search is not run
  470. if we only use one
  471. block size */
  472. }
  473. /* fill in the block. Note that for a short window, lW and nW are *short*
  474. regardless of actual settings in the stream */
  475. _vorbis_block_ripcord(vb);
  476. vb->lW=v->lW;
  477. vb->W=v->W;
  478. vb->nW=v->nW;
  479. if(v->W){
  480. if(!v->lW || !v->nW){
  481. vbi->blocktype=BLOCKTYPE_TRANSITION;
  482. /*fprintf(stderr,"-");*/
  483. }else{
  484. vbi->blocktype=BLOCKTYPE_LONG;
  485. /*fprintf(stderr,"_");*/
  486. }
  487. }else{
  488. if(_ve_envelope_mark(v)){
  489. vbi->blocktype=BLOCKTYPE_IMPULSE;
  490. /*fprintf(stderr,"|");*/
  491. }else{
  492. vbi->blocktype=BLOCKTYPE_PADDING;
  493. /*fprintf(stderr,".");*/
  494. }
  495. }
  496. vb->vd=v;
  497. vb->sequence=v->sequence++;
  498. vb->granulepos=v->granulepos;
  499. vb->pcmend=ci->blocksizes[v->W];
  500. /* copy the vectors; this uses the local storage in vb */
  501. /* this tracks 'strongest peak' for later psychoacoustics */
  502. /* moved to the global psy state; clean this mess up */
  503. if(vbi->ampmax>g->ampmax)g->ampmax=vbi->ampmax;
  504. g->ampmax=_vp_ampmax_decay(g->ampmax,v);
  505. vbi->ampmax=g->ampmax;
  506. vb->pcm=_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels);
  507. vbi->pcmdelay=_vorbis_block_alloc(vb,sizeof(*vbi->pcmdelay)*vi->channels);
  508. for(i=0;i<vi->channels;i++){
  509. vbi->pcmdelay[i]=
  510. _vorbis_block_alloc(vb,(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
  511. memcpy(vbi->pcmdelay[i],v->pcm[i],(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
  512. vb->pcm[i]=vbi->pcmdelay[i]+beginW;
  513. /* before we added the delay
  514. vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i]));
  515. memcpy(vb->pcm[i],v->pcm[i]+beginW,ci->blocksizes[v->W]*sizeof(*vb->pcm[i]));
  516. */
  517. }
  518. /* handle eof detection: eof==0 means that we've not yet received EOF
  519. eof>0 marks the last 'real' sample in pcm[]
  520. eof<0 'no more to do'; doesn't get here */
  521. if(v->eofflag){
  522. if(v->centerW>=v->eofflag){
  523. v->eofflag=-1;
  524. vb->eofflag=1;
  525. return(1);
  526. }
  527. }
  528. /* advance storage vectors and clean up */
  529. {
  530. int new_centerNext=ci->blocksizes[1]/2;
  531. int movementW=centerNext-new_centerNext;
  532. if(movementW>0){
  533. _ve_envelope_shift(b->ve,movementW);
  534. v->pcm_current-=movementW;
  535. for(i=0;i<vi->channels;i++)
  536. memmove(v->pcm[i],v->pcm[i]+movementW,
  537. v->pcm_current*sizeof(*v->pcm[i]));
  538. v->lW=v->W;
  539. v->W=v->nW;
  540. v->centerW=new_centerNext;
  541. if(v->eofflag){
  542. v->eofflag-=movementW;
  543. if(v->eofflag<=0)v->eofflag=-1;
  544. /* do not add padding to end of stream! */
  545. if(v->centerW>=v->eofflag){
  546. v->granulepos+=movementW-(v->centerW-v->eofflag);
  547. }else{
  548. v->granulepos+=movementW;
  549. }
  550. }else{
  551. v->granulepos+=movementW;
  552. }
  553. }
  554. }
  555. /* done */
  556. return(1);
  557. }
  558. int vorbis_synthesis_restart(vorbis_dsp_state *v){
  559. vorbis_info *vi=v->vi;
  560. codec_setup_info *ci;
  561. int hs;
  562. if(!v->backend_state)return -1;
  563. if(!vi)return -1;
  564. ci=vi->codec_setup;
  565. if(!ci)return -1;
  566. hs=ci->halfrate_flag;
  567. v->centerW=ci->blocksizes[1]>>(hs+1);
  568. v->pcm_current=v->centerW>>hs;
  569. v->pcm_returned=-1;
  570. v->granulepos=-1;
  571. v->sequence=-1;
  572. v->eofflag=0;
  573. ((private_state *)(v->backend_state))->sample_count=-1;
  574. return(0);
  575. }
  576. int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
  577. if(_vds_shared_init(v,vi,0)) return 1;
  578. vorbis_synthesis_restart(v);
  579. return 0;
  580. }
  581. /* Unlike in analysis, the window is only partially applied for each
  582. block. The time domain envelope is not yet handled at the point of
  583. calling (as it relies on the previous block). */
  584. int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
  585. vorbis_info *vi=v->vi;
  586. codec_setup_info *ci=vi->codec_setup;
  587. private_state *b=v->backend_state;
  588. int hs=ci->halfrate_flag;
  589. int i,j;
  590. if(!vb)return(OV_EINVAL);
  591. if(v->pcm_current>v->pcm_returned && v->pcm_returned!=-1)return(OV_EINVAL);
  592. v->lW=v->W;
  593. v->W=vb->W;
  594. v->nW=-1;
  595. if((v->sequence==-1)||
  596. (v->sequence+1 != vb->sequence)){
  597. v->granulepos=-1; /* out of sequence; lose count */
  598. b->sample_count=-1;
  599. }
  600. v->sequence=vb->sequence;
  601. if(vb->pcm){ /* no pcm to process if vorbis_synthesis_trackonly
  602. was called on block */
  603. int n=ci->blocksizes[v->W]>>(hs+1);
  604. int n0=ci->blocksizes[0]>>(hs+1);
  605. int n1=ci->blocksizes[1]>>(hs+1);
  606. int thisCenter;
  607. int prevCenter;
  608. v->glue_bits+=vb->glue_bits;
  609. v->time_bits+=vb->time_bits;
  610. v->floor_bits+=vb->floor_bits;
  611. v->res_bits+=vb->res_bits;
  612. if(v->centerW){
  613. thisCenter=n1;
  614. prevCenter=0;
  615. }else{
  616. thisCenter=0;
  617. prevCenter=n1;
  618. }
  619. /* v->pcm is now used like a two-stage double buffer. We don't want
  620. to have to constantly shift *or* adjust memory usage. Don't
  621. accept a new block until the old is shifted out */
  622. for(j=0;j<vi->channels;j++){
  623. /* the overlap/add section */
  624. if(v->lW){
  625. if(v->W){
  626. /* large/large */
  627. float *w=_vorbis_window_get(b->window[1]-hs);
  628. float *pcm=v->pcm[j]+prevCenter;
  629. float *p=vb->pcm[j];
  630. for(i=0;i<n1;i++)
  631. pcm[i]=pcm[i]*w[n1-i-1] + p[i]*w[i];
  632. }else{
  633. /* large/small */
  634. float *w=_vorbis_window_get(b->window[0]-hs);
  635. float *pcm=v->pcm[j]+prevCenter+n1/2-n0/2;
  636. float *p=vb->pcm[j];
  637. for(i=0;i<n0;i++)
  638. pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
  639. }
  640. }else{
  641. if(v->W){
  642. /* small/large */
  643. float *w=_vorbis_window_get(b->window[0]-hs);
  644. float *pcm=v->pcm[j]+prevCenter;
  645. float *p=vb->pcm[j]+n1/2-n0/2;
  646. for(i=0;i<n0;i++)
  647. pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
  648. for(;i<n1/2+n0/2;i++)
  649. pcm[i]=p[i];
  650. }else{
  651. /* small/small */
  652. float *w=_vorbis_window_get(b->window[0]-hs);
  653. float *pcm=v->pcm[j]+prevCenter;
  654. float *p=vb->pcm[j];
  655. for(i=0;i<n0;i++)
  656. pcm[i]=pcm[i]*w[n0-i-1] +p[i]*w[i];
  657. }
  658. }
  659. /* the copy section */
  660. {
  661. float *pcm=v->pcm[j]+thisCenter;
  662. float *p=vb->pcm[j]+n;
  663. for(i=0;i<n;i++)
  664. pcm[i]=p[i];
  665. }
  666. }
  667. if(v->centerW)
  668. v->centerW=0;
  669. else
  670. v->centerW=n1;
  671. /* deal with initial packet state; we do this using the explicit
  672. pcm_returned==-1 flag otherwise we're sensitive to first block
  673. being short or long */
  674. if(v->pcm_returned==-1){
  675. v->pcm_returned=thisCenter;
  676. v->pcm_current=thisCenter;
  677. }else{
  678. v->pcm_returned=prevCenter;
  679. v->pcm_current=prevCenter+
  680. ((ci->blocksizes[v->lW]/4+
  681. ci->blocksizes[v->W]/4)>>hs);
  682. }
  683. }
  684. /* track the frame number... This is for convenience, but also
  685. making sure our last packet doesn't end with added padding. If
  686. the last packet is partial, the number of samples we'll have to
  687. return will be past the vb->granulepos.
  688. This is not foolproof! It will be confused if we begin
  689. decoding at the last page after a seek or hole. In that case,
  690. we don't have a starting point to judge where the last frame
  691. is. For this reason, vorbisfile will always try to make sure
  692. it reads the last two marked pages in proper sequence */
  693. if(b->sample_count==-1){
  694. b->sample_count=0;
  695. }else{
  696. b->sample_count+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4;
  697. }
  698. if(v->granulepos==-1){
  699. if(vb->granulepos!=-1){ /* only set if we have a position to set to */
  700. v->granulepos=vb->granulepos;
  701. /* is this a short page? */
  702. if(b->sample_count>v->granulepos){
  703. /* corner case; if this is both the first and last audio page,
  704. then spec says the end is cut, not beginning */
  705. if(vb->eofflag){
  706. /* trim the end */
  707. /* no preceeding granulepos; assume we started at zero (we'd
  708. have to in a short single-page stream) */
  709. /* granulepos could be -1 due to a seek, but that would result
  710. in a long count, not short count */
  711. v->pcm_current-=(b->sample_count-v->granulepos)>>hs;
  712. }else{
  713. /* trim the beginning */
  714. v->pcm_returned+=(b->sample_count-v->granulepos)>>hs;
  715. if(v->pcm_returned>v->pcm_current)
  716. v->pcm_returned=v->pcm_current;
  717. }
  718. }
  719. }
  720. }else{
  721. v->granulepos+=ci->blocksizes[v->lW]/4+ci->blocksizes[v->W]/4;
  722. if(vb->granulepos!=-1 && v->granulepos!=vb->granulepos){
  723. if(v->granulepos>vb->granulepos){
  724. long extra=v->granulepos-vb->granulepos;
  725. if(extra)
  726. if(vb->eofflag){
  727. /* partial last frame. Strip the extra samples off */
  728. v->pcm_current-=extra>>hs;
  729. } /* else {Shouldn't happen *unless* the bitstream is out of
  730. spec. Either way, believe the bitstream } */
  731. } /* else {Shouldn't happen *unless* the bitstream is out of
  732. spec. Either way, believe the bitstream } */
  733. v->granulepos=vb->granulepos;
  734. }
  735. }
  736. /* Update, cleanup */
  737. if(vb->eofflag)v->eofflag=1;
  738. return(0);
  739. }
  740. /* pcm==NULL indicates we just want the pending samples, no more */
  741. int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm){
  742. vorbis_info *vi=v->vi;
  743. if(v->pcm_returned>-1 && v->pcm_returned<v->pcm_current){
  744. if(pcm){
  745. int i;
  746. for(i=0;i<vi->channels;i++)
  747. v->pcmret[i]=v->pcm[i]+v->pcm_returned;
  748. *pcm=v->pcmret;
  749. }
  750. return(v->pcm_current-v->pcm_returned);
  751. }
  752. return(0);
  753. }
  754. int vorbis_synthesis_read(vorbis_dsp_state *v,int n){
  755. if(n && v->pcm_returned+n>v->pcm_current)return(OV_EINVAL);
  756. v->pcm_returned+=n;
  757. return(0);
  758. }
  759. /* intended for use with a specific vorbisfile feature; we want access
  760. to the [usually synthetic/postextrapolated] buffer and lapping at
  761. the end of a decode cycle, specifically, a half-short-block worth.
  762. This funtion works like pcmout above, except it will also expose
  763. this implicit buffer data not normally decoded. */
  764. int vorbis_synthesis_lapout(vorbis_dsp_state *v,float ***pcm){
  765. vorbis_info *vi=v->vi;
  766. codec_setup_info *ci=vi->codec_setup;
  767. int hs=ci->halfrate_flag;
  768. int n=ci->blocksizes[v->W]>>(hs+1);
  769. int n0=ci->blocksizes[0]>>(hs+1);
  770. int n1=ci->blocksizes[1]>>(hs+1);
  771. int i,j;
  772. if(v->pcm_returned<0)return 0;
  773. /* our returned data ends at pcm_returned; because the synthesis pcm
  774. buffer is a two-fragment ring, that means our data block may be
  775. fragmented by buffering, wrapping or a short block not filling
  776. out a buffer. To simplify things, we unfragment if it's at all
  777. possibly needed. Otherwise, we'd need to call lapout more than
  778. once as well as hold additional dsp state. Opt for
  779. simplicity. */
  780. /* centerW was advanced by blockin; it would be the center of the
  781. *next* block */
  782. if(v->centerW==n1){
  783. /* the data buffer wraps; swap the halves */
  784. /* slow, sure, small */
  785. for(j=0;j<vi->channels;j++){
  786. float *p=v->pcm[j];
  787. for(i=0;i<n1;i++){
  788. float temp=p[i];
  789. p[i]=p[i+n1];
  790. p[i+n1]=temp;
  791. }
  792. }
  793. v->pcm_current-=n1;
  794. v->pcm_returned-=n1;
  795. v->centerW=0;
  796. }
  797. /* solidify buffer into contiguous space */
  798. if((v->lW^v->W)==1){
  799. /* long/short or short/long */
  800. for(j=0;j<vi->channels;j++){
  801. float *s=v->pcm[j];
  802. float *d=v->pcm[j]+(n1-n0)/2;
  803. for(i=(n1+n0)/2-1;i>=0;--i)
  804. d[i]=s[i];
  805. }
  806. v->pcm_returned+=(n1-n0)/2;
  807. v->pcm_current+=(n1-n0)/2;
  808. }else{
  809. if(v->lW==0){
  810. /* short/short */
  811. for(j=0;j<vi->channels;j++){
  812. float *s=v->pcm[j];
  813. float *d=v->pcm[j]+n1-n0;
  814. for(i=n0-1;i>=0;--i)
  815. d[i]=s[i];
  816. }
  817. v->pcm_returned+=n1-n0;
  818. v->pcm_current+=n1-n0;
  819. }
  820. }
  821. if(pcm){
  822. int i;
  823. for(i=0;i<vi->channels;i++)
  824. v->pcmret[i]=v->pcm[i]+v->pcm_returned;
  825. *pcm=v->pcmret;
  826. }
  827. return(n1+n-v->pcm_returned);
  828. }
  829. float *vorbis_window(vorbis_dsp_state *v,int W){
  830. vorbis_info *vi=v->vi;
  831. codec_setup_info *ci=vi->codec_setup;
  832. int hs=ci->halfrate_flag;
  833. private_state *b=v->backend_state;
  834. if(b->window[W]-1<0)return NULL;
  835. return _vorbis_window_get(b->window[W]-hs);
  836. }