floor0.c 12 KB

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