floor0.c 13 KB

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