floor0.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. ********************************************************************
  11. function: floor backend 0 implementation
  12. last mod: $Id: floor0.c,v 1.43.2.2 2001/08/02 06:14:43 xiphmont Exp $
  13. ********************************************************************/
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <math.h>
  17. #include <ogg/ogg.h>
  18. #include "vorbis/codec.h"
  19. #include "codec_internal.h"
  20. #include "registry.h"
  21. #include "lpc.h"
  22. #include "lsp.h"
  23. #include "codebook.h"
  24. #include "scales.h"
  25. #include "misc.h"
  26. #include "os.h"
  27. #include "misc.h"
  28. #include <stdio.h>
  29. typedef struct {
  30. long n;
  31. int ln;
  32. int m;
  33. int *linearmap;
  34. vorbis_info_floor0 *vi;
  35. lpc_lookup lpclook;
  36. float *lsp_look;
  37. long bits;
  38. long frames;
  39. } vorbis_look_floor0;
  40. /* infrastructure for finding fit */
  41. static long _f0_fit(codebook *book,
  42. float *orig,
  43. float *workfit,
  44. int cursor){
  45. int dim=book->dim;
  46. float norm,base=0.f;
  47. int i,best=0;
  48. float *lsp=workfit+cursor;
  49. if(cursor)base=workfit[cursor-1];
  50. norm=orig[cursor+dim-1]-base;
  51. for(i=0;i<dim;i++)
  52. lsp[i]=(orig[i+cursor]-base);
  53. best=_best(book,lsp,1);
  54. memcpy(lsp,book->valuelist+best*dim,dim*sizeof(float));
  55. for(i=0;i<dim;i++)
  56. lsp[i]+=base;
  57. return(best);
  58. }
  59. /***********************************************/
  60. static vorbis_info_floor *floor0_copy_info (vorbis_info_floor *i){
  61. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  62. vorbis_info_floor0 *ret=_ogg_malloc(sizeof(vorbis_info_floor0));
  63. memcpy(ret,info,sizeof(vorbis_info_floor0));
  64. return(ret);
  65. }
  66. static void floor0_free_info(vorbis_info_floor *i){
  67. if(i){
  68. memset(i,0,sizeof(vorbis_info_floor0));
  69. _ogg_free(i);
  70. }
  71. }
  72. static void floor0_free_look(vorbis_look_floor *i){
  73. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  74. if(i){
  75. fprintf(stderr,"floor 0 bit usage %f\n",
  76. (float)look->bits/look->frames);
  77. if(look->linearmap)_ogg_free(look->linearmap);
  78. if(look->lsp_look)_ogg_free(look->lsp_look);
  79. lpc_clear(&look->lpclook);
  80. memset(look,0,sizeof(vorbis_look_floor0));
  81. _ogg_free(look);
  82. }
  83. }
  84. static void floor0_pack (vorbis_info_floor *i,oggpack_buffer *opb){
  85. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  86. int j;
  87. oggpack_write(opb,info->order,8);
  88. oggpack_write(opb,info->rate,16);
  89. oggpack_write(opb,info->barkmap,16);
  90. oggpack_write(opb,info->ampbits,6);
  91. oggpack_write(opb,info->ampdB,8);
  92. oggpack_write(opb,info->numbooks-1,4);
  93. for(j=0;j<info->numbooks;j++)
  94. oggpack_write(opb,info->books[j],8);
  95. }
  96. static vorbis_info_floor *floor0_unpack (vorbis_info *vi,oggpack_buffer *opb){
  97. codec_setup_info *ci=vi->codec_setup;
  98. int j;
  99. vorbis_info_floor0 *info=_ogg_malloc(sizeof(vorbis_info_floor0));
  100. info->order=oggpack_read(opb,8);
  101. info->rate=oggpack_read(opb,16);
  102. info->barkmap=oggpack_read(opb,16);
  103. info->ampbits=oggpack_read(opb,6);
  104. info->ampdB=oggpack_read(opb,8);
  105. info->numbooks=oggpack_read(opb,4)+1;
  106. if(info->order<1)goto err_out;
  107. if(info->rate<1)goto err_out;
  108. if(info->barkmap<1)goto err_out;
  109. if(info->numbooks<1)goto err_out;
  110. for(j=0;j<info->numbooks;j++){
  111. info->books[j]=oggpack_read(opb,8);
  112. if(info->books[j]<0 || info->books[j]>=ci->books)goto err_out;
  113. }
  114. return(info);
  115. err_out:
  116. floor0_free_info(info);
  117. return(NULL);
  118. }
  119. /* initialize Bark scale and normalization lookups. We could do this
  120. with static tables, but Vorbis allows a number of possible
  121. combinations, so it's best to do it computationally.
  122. The below is authoritative in terms of defining scale mapping.
  123. Note that the scale depends on the sampling rate as well as the
  124. linear block and mapping sizes */
  125. static vorbis_look_floor *floor0_look (vorbis_dsp_state *vd,vorbis_info_mode *mi,
  126. vorbis_info_floor *i){
  127. int j;
  128. float scale;
  129. vorbis_info *vi=vd->vi;
  130. codec_setup_info *ci=vi->codec_setup;
  131. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  132. vorbis_look_floor0 *look=_ogg_calloc(1,sizeof(vorbis_look_floor0));
  133. look->m=info->order;
  134. look->n=ci->blocksizes[mi->blockflag]/2;
  135. look->ln=info->barkmap;
  136. look->vi=info;
  137. if(vd->analysisp)
  138. lpc_init(&look->lpclook,look->ln,look->m);
  139. /* we choose a scaling constant so that:
  140. floor(bark(rate/2-1)*C)=mapped-1
  141. floor(bark(rate/2)*C)=mapped */
  142. scale=look->ln/toBARK(info->rate/2.f);
  143. /* the mapping from a linear scale to a smaller bark scale is
  144. straightforward. We do *not* make sure that the linear mapping
  145. does not skip bark-scale bins; the decoder simply skips them and
  146. the encoder may do what it wishes in filling them. They're
  147. necessary in some mapping combinations to keep the scale spacing
  148. accurate */
  149. look->linearmap=_ogg_malloc((look->n+1)*sizeof(int));
  150. for(j=0;j<look->n;j++){
  151. int val=floor( toBARK((info->rate/2.f)/look->n*j)
  152. *scale); /* bark numbers represent band edges */
  153. if(val>=look->ln)val=look->ln; /* guard against the approximation */
  154. look->linearmap[j]=val;
  155. }
  156. look->linearmap[j]=-1;
  157. look->lsp_look=_ogg_malloc(look->ln*sizeof(float));
  158. for(j=0;j<look->ln;j++)
  159. look->lsp_look[j]=2*cos(M_PI/look->ln*j);
  160. return look;
  161. }
  162. /* less efficient than the decode side (written for clarity). We're
  163. not bottlenecked here anyway */
  164. float _curve_to_lpc(float *curve,float *lpc,
  165. vorbis_look_floor0 *l){
  166. /* map the input curve to a bark-scale curve for encoding */
  167. int mapped=l->ln;
  168. float *work=alloca(sizeof(float)*mapped);
  169. int i,j,last=0;
  170. int bark=0;
  171. static int seq=0;
  172. memset(work,0,sizeof(float)*mapped);
  173. /* Only the decode side is behavior-specced; for now in the encoder,
  174. we select the maximum value of each band as representative (this
  175. helps make sure peaks don't go out of range. In error terms,
  176. selecting min would make more sense, but the codebook is trained
  177. numerically, so we don't actually lose. We'd still want to
  178. use the original curve for error and noise estimation */
  179. for(i=0;i<l->n;i++){
  180. bark=l->linearmap[i];
  181. if(work[bark]<curve[i])work[bark]=curve[i];
  182. if(bark>last+1){
  183. /* If the bark scale is climbing rapidly, some bins may end up
  184. going unused. This isn't a waste actually; it keeps the
  185. scale resolution even so that the LPC generator has an easy
  186. time. However, if we leave the bins empty we lose energy.
  187. So, fill 'em in. The decoder does not do anything with he
  188. unused bins, so we can fill them anyway we like to end up
  189. with a better spectral curve */
  190. /* we'll always have a bin zero, so we don't need to guard init */
  191. long span=bark-last;
  192. for(j=1;j<span;j++){
  193. float del=(float)j/span;
  194. work[j+last]=work[bark]*del+work[last]*(1.f-del);
  195. }
  196. }
  197. last=bark;
  198. }
  199. /* If we're over-ranged to avoid edge effects, fill in the end of spectrum gap */
  200. for(i=bark+1;i<mapped;i++)
  201. work[i]=work[i-1];
  202. /**********************/
  203. for(i=0;i<l->n;i++)
  204. curve[i]-=150;
  205. _analysis_output("barkfloor",seq,work,bark,0,0);
  206. _analysis_output("barkcurve",seq++,curve,l->n,1,0);
  207. for(i=0;i<l->n;i++)
  208. curve[i]+=150;
  209. /**********************/
  210. return vorbis_lpc_from_curve(work,lpc,&(l->lpclook));
  211. }
  212. static int floor0_forward(vorbis_block *vb,vorbis_look_floor *in,
  213. float *mdct, const float *logmdct, /* in */
  214. const float *logmask, const float *logmax, /* in */
  215. float *codedflr){ /* out */
  216. long j;
  217. vorbis_look_floor0 *look=(vorbis_look_floor0 *)in;
  218. vorbis_info_floor0 *info=look->vi;
  219. float amp;
  220. long bits=0;
  221. long val=0;
  222. static int seq=0;
  223. #ifdef TRAIN_LSP
  224. FILE *of;
  225. FILE *ef;
  226. char buffer[80];
  227. #if 1
  228. sprintf(buffer,"lsp0coeff_%d.vqd",vb->mode);
  229. of=fopen(buffer,"a");
  230. #endif
  231. #endif
  232. seq++;
  233. /* our floor comes in on a [-Inf...0] dB scale. The curve has to be
  234. positive, so we offset it. */
  235. for(j=0;j<look->n;j++)
  236. codedflr[j]=logmask[j]+info->ampdB;
  237. /* use 'out' as temp storage */
  238. /* Convert our floor to a set of lpc coefficients */
  239. amp=sqrt(_curve_to_lpc(codedflr,codedflr,look));
  240. /* amp is in the range (0. to ampdB]. Encode that range using
  241. ampbits bits */
  242. {
  243. long maxval=(1L<<info->ampbits)-1;
  244. val=rint(amp/info->ampdB*maxval);
  245. if(val<0)val=0; /* likely */
  246. if(val>maxval)val=maxval; /* not bloody likely */
  247. if(val>0)
  248. amp=(float)val/maxval*info->ampdB;
  249. else
  250. amp=0;
  251. }
  252. if(val){
  253. /* LSP <-> LPC is orthogonal and LSP quantizes more stably */
  254. _analysis_output("lpc",seq-1,codedflr,look->m,0,0);
  255. if(vorbis_lpc_to_lsp(codedflr,codedflr,look->m))
  256. val=0;
  257. }
  258. oggpack_write(&vb->opb,val,info->ampbits);
  259. look->bits+=info->ampbits+1;
  260. look->frames++;
  261. if(val){
  262. float *lspwork=alloca(look->m*sizeof(float));
  263. /* the spec supports using one of a number of codebooks. Right
  264. now, encode using this lib supports only one */
  265. backend_lookup_state *be=vb->vd->backend_state;
  266. codebook *b;
  267. int booknum;
  268. _analysis_output("lsp",seq-1,codedflr,look->m,0,0);
  269. /* which codebook to use? We do it only by range right now. */
  270. if(info->numbooks>1){
  271. float last=0.;
  272. for(j=0;j<look->m;j++){
  273. float val=codedflr[j]-last;
  274. if(val<info->lessthan || val>info->greaterthan)break;
  275. last=codedflr[j];
  276. }
  277. if(j<look->m)
  278. booknum=0;
  279. else
  280. booknum=1;
  281. }else
  282. booknum=0;
  283. b=be->fullbooks+info->books[booknum];
  284. oggpack_write(&vb->opb,booknum,_ilog(info->numbooks));
  285. look->bits+=_ilog(info->numbooks);
  286. #ifdef TRAIN_LSP
  287. {
  288. float last=0.f;
  289. for(j=0;j<look->m;j++){
  290. fprintf(of,"%.12g, ",codedflr[j]-last);
  291. last=codedflr[j];
  292. }
  293. }
  294. fprintf(of,"\n");
  295. fclose(of);
  296. sprintf(buffer,"lsp0ent_m%d_b%d.vqd",vb->mode,booknum);
  297. ef=fopen(buffer,"a");
  298. #endif
  299. /* code the spectral envelope, and keep track of the actual
  300. quantized values; we don't want creeping error as each block is
  301. nailed to the last quantized value of the previous block. */
  302. for(j=0;j<look->m;j+=b->dim){
  303. int entry=_f0_fit(b,codedflr,lspwork,j);
  304. look->bits+=vorbis_book_encode(b,entry,&vb->opb);
  305. #ifdef TRAIN_LSP
  306. fprintf(ef,"%d,\n",entry);
  307. #endif
  308. }
  309. #ifdef TRAIN_LSP
  310. fclose(ef);
  311. #endif
  312. _analysis_output("lsp2",seq-1,lspwork,look->m,0,0);
  313. /* take the coefficients back to a spectral envelope curve */
  314. for(j=0;j<look->n;j++)
  315. codedflr[j]=1.f;
  316. vorbis_lsp_to_curve(codedflr,look->linearmap,look->n,look->ln,
  317. lspwork,look->m,amp,info->ampdB);
  318. _analysis_output("barklsp",seq-1,codedflr,look->n,1,1);
  319. _analysis_output("lsp3",seq-1,codedflr,look->n,0,1);
  320. return(val);
  321. }
  322. #ifdef TRAIN_LSP
  323. fclose(of);
  324. #endif
  325. memset(codedflr,0,sizeof(float)*look->n);
  326. memset(mdct,0,sizeof(float)*look->n);
  327. return(val);
  328. }
  329. static void *floor0_inverse1(vorbis_block *vb,vorbis_look_floor *i){
  330. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  331. vorbis_info_floor0 *info=look->vi;
  332. int j,k;
  333. int ampraw=oggpack_read(&vb->opb,info->ampbits);
  334. if(ampraw>0){ /* also handles the -1 out of data case */
  335. long maxval=(1<<info->ampbits)-1;
  336. float amp=(float)ampraw/maxval*info->ampdB;
  337. int booknum=oggpack_read(&vb->opb,_ilog(info->numbooks));
  338. if(booknum!=-1 && booknum<info->numbooks){ /* be paranoid */
  339. backend_lookup_state *be=vb->vd->backend_state;
  340. codebook *b=be->fullbooks+info->books[booknum];
  341. float last=0.f;
  342. float *lsp=_vorbis_block_alloc(vb,sizeof(float)*(look->m+1));
  343. for(j=0;j<look->m;j+=b->dim)
  344. if(vorbis_book_decodev_set(b,lsp+j,&vb->opb,b->dim)==-1)goto eop;
  345. for(j=0;j<look->m;){
  346. for(k=0;k<b->dim;k++,j++)lsp[j]+=last;
  347. last=lsp[j-1];
  348. }
  349. lsp[look->m]=amp;
  350. return(lsp);
  351. }
  352. }
  353. eop:
  354. return(NULL);
  355. }
  356. static int floor0_inverse2(vorbis_block *vb,vorbis_look_floor *i,
  357. void *memo,float *out){
  358. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  359. vorbis_info_floor0 *info=look->vi;
  360. if(memo){
  361. float *lsp=(float *)memo;
  362. float amp=lsp[look->m];
  363. /* take the coefficients back to a spectral envelope curve */
  364. vorbis_lsp_to_curve(out,look->linearmap,look->n,look->ln,
  365. lsp,look->m,amp,info->ampdB);
  366. return(1);
  367. }
  368. memset(out,0,sizeof(float)*look->n);
  369. return(0);
  370. }
  371. /* export hooks */
  372. vorbis_func_floor floor0_exportbundle={
  373. &floor0_pack,&floor0_unpack,&floor0_look,&floor0_copy_info,&floor0_free_info,
  374. &floor0_free_look,&floor0_forward,&floor0_inverse1,&floor0_inverse2
  375. };