block.c 30 KB

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