block.c 30 KB

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