floor0.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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.14.2.3 2000/06/12 00:31:15 xiphmont Exp $
  15. ********************************************************************/
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include "vorbis/codec.h"
  20. #include "bitwise.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. typedef struct {
  30. long n;
  31. int ln;
  32. int m;
  33. int *linearmap;
  34. vorbis_info_floor0 *vi;
  35. lpc_lookup lpclook;
  36. } vorbis_look_floor0;
  37. typedef struct {
  38. long *codewords;
  39. double *curve;
  40. long frameno;
  41. long codes;
  42. } vorbis_echstate_floor0;
  43. static void free_info(vorbis_info_floor *i){
  44. if(i){
  45. memset(i,0,sizeof(vorbis_info_floor0));
  46. free(i);
  47. }
  48. }
  49. static void free_look(vorbis_look_floor *i){
  50. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  51. if(i){
  52. if(look->linearmap)free(look->linearmap);
  53. lpc_clear(&look->lpclook);
  54. memset(look,0,sizeof(vorbis_look_floor0));
  55. free(look);
  56. }
  57. }
  58. static void pack (vorbis_info_floor *i,oggpack_buffer *opb){
  59. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  60. int j;
  61. _oggpack_write(opb,info->order,8);
  62. _oggpack_write(opb,info->rate,16);
  63. _oggpack_write(opb,info->barkmap,16);
  64. _oggpack_write(opb,info->ampbits,6);
  65. _oggpack_write(opb,info->ampdB,8);
  66. _oggpack_write(opb,info->numbooks-1,4);
  67. for(j=0;j<info->numbooks;j++)
  68. _oggpack_write(opb,info->books[j],8);
  69. }
  70. static vorbis_info_floor *unpack (vorbis_info *vi,oggpack_buffer *opb){
  71. int j;
  72. vorbis_info_floor0 *info=malloc(sizeof(vorbis_info_floor0));
  73. info->order=_oggpack_read(opb,8);
  74. info->rate=_oggpack_read(opb,16);
  75. info->barkmap=_oggpack_read(opb,16);
  76. info->ampbits=_oggpack_read(opb,6);
  77. info->ampdB=_oggpack_read(opb,8);
  78. info->numbooks=_oggpack_read(opb,4)+1;
  79. if(info->order<1)goto err_out;
  80. if(info->rate<1)goto err_out;
  81. if(info->barkmap<1)goto err_out;
  82. if(info->numbooks<1)goto err_out;
  83. for(j=0;j<info->numbooks;j++){
  84. info->books[j]=_oggpack_read(opb,8);
  85. if(info->books[j]<0 || info->books[j]>=vi->books)goto err_out;
  86. }
  87. return(info);
  88. err_out:
  89. free_info(info);
  90. return(NULL);
  91. }
  92. /* initialize Bark scale and normalization lookups. We could do this
  93. with static tables, but Vorbis allows a number of possible
  94. combinations, so it's best to do it computationally.
  95. The below is authoritative in terms of defining scale mapping.
  96. Note that the scale depends on the sampling rate as well as the
  97. linear block and mapping sizes */
  98. static vorbis_look_floor *look (vorbis_dsp_state *vd,vorbis_info_mode *mi,
  99. vorbis_info_floor *i){
  100. int j;
  101. double scale;
  102. vorbis_info *vi=vd->vi;
  103. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  104. vorbis_look_floor0 *look=malloc(sizeof(vorbis_look_floor0));
  105. look->m=info->order;
  106. look->n=vi->blocksizes[mi->blockflag]/2;
  107. look->ln=info->barkmap;
  108. look->vi=info;
  109. lpc_init(&look->lpclook,look->ln,look->m);
  110. /* we choose a scaling constant so that:
  111. floor(bark(rate/2-1)*C)=mapped-1
  112. floor(bark(rate/2)*C)=mapped */
  113. scale=look->ln/toBARK(info->rate/2.);
  114. /* the mapping from a linear scale to a smaller bark scale is
  115. straightforward. We do *not* make sure that the linear mapping
  116. does not skip bark-scale bins; the decoder simply skips them and
  117. the encoder may do what it wishes in filling them. They're
  118. necessary in some mapping combinations to keep the scale spacing
  119. accurate */
  120. look->linearmap=malloc(look->n*sizeof(int));
  121. for(j=0;j<look->n;j++){
  122. int val=floor( toBARK((info->rate/2.)/look->n*j)
  123. *scale); /* bark numbers represent band edges */
  124. if(val>look->ln)val=look->ln; /* guard against the approximation */
  125. look->linearmap[j]=val;
  126. }
  127. return look;
  128. }
  129. static vorbis_echstate_floor *state (vorbis_info_floor *i){
  130. vorbis_echstate_floor0 *state=calloc(1,sizeof(vorbis_echstate_floor0));
  131. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  132. /* a safe size if usually too big (dim==1) */
  133. state->codewords=malloc(info->order*sizeof(long));
  134. state->curve=malloc(info->barkmap*sizeof(double));
  135. state->frameno=-1;
  136. return(state);
  137. }
  138. static void free_state (vorbis_echstate_floor *vs){
  139. vorbis_echstate_floor0 *state=(vorbis_echstate_floor0 *)vs;
  140. if(state){
  141. free(state->codewords);
  142. free(state->curve);
  143. memset(state,0,sizeof(vorbis_echstate_floor0));
  144. free(state);
  145. }
  146. }
  147. #include <stdio.h>
  148. double _curve_error(double *curve1,double *curve2,long n){
  149. double acc=0.;
  150. long i;
  151. for(i=0;i<n;i++){
  152. double val=curve1[i]-curve2[i];
  153. acc+=val*val;
  154. }
  155. return(acc);
  156. }
  157. /* less efficient than the decode side (written for clarity). We're
  158. not bottlenecked here anyway */
  159. double _curve_to_lpc(double *curve,double *lpc,
  160. vorbis_look_floor0 *l,long frameno){
  161. /* map the input curve to a bark-scale curve for encoding */
  162. int mapped=l->ln;
  163. double *work=alloca(sizeof(double)*mapped);
  164. int i,j,last=0;
  165. int bark=0;
  166. memset(work,0,sizeof(double)*mapped);
  167. /* Only the decode side is behavior-specced; for now in the encoder,
  168. we select the maximum value of each band as representative (this
  169. helps make sure peaks don't go out of range. In error terms,
  170. selecting min would make more sense, but the codebook is trained
  171. numerically, so we don't actually lose. We'd still want to
  172. use the original curve for error and noise estimation */
  173. for(i=0;i<l->n;i++){
  174. bark=l->linearmap[i];
  175. if(work[bark]<curve[i])work[bark]=curve[i];
  176. if(bark>last+1){
  177. /* If the bark scale is climbing rapidly, some bins may end up
  178. going unused. This isn't a waste actually; it keeps the
  179. scale resolution even so that the LPC generator has an easy
  180. time. However, if we leave the bins empty we lose energy.
  181. So, fill 'em in. The decoder does not do anything with he
  182. unused bins, so we can fill them anyway we like to end up
  183. with a better spectral curve */
  184. /* we'll always have a bin zero, so we don't need to guard init */
  185. long span=bark-last;
  186. for(j=1;j<span;j++){
  187. double del=(double)j/span;
  188. work[j+last]=work[bark]*del+work[last]*(1.-del);
  189. }
  190. }
  191. last=bark;
  192. }
  193. /* If we're over-ranged to avoid edge effects, fill in the end of spectrum gap */
  194. for(i=bark+1;i<mapped;i++)
  195. work[i]=work[i-1];
  196. #if 0
  197. { /******************/
  198. FILE *of;
  199. char buffer[80];
  200. int i;
  201. sprintf(buffer,"Fmask_%d.m",frameno);
  202. of=fopen(buffer,"w");
  203. for(i=0;i<mapped;i++)
  204. fprintf(of,"%g\n",work[i]);
  205. fclose(of);
  206. }
  207. #endif
  208. return vorbis_lpc_from_curve(work,lpc,&(l->lpclook));
  209. }
  210. /* generate the whole freq response curve of an LPC IIR filter */
  211. void _lpc_to_curve(double *curve,double *lpc,double amp,
  212. vorbis_look_floor0 *l,char *name,long frameno){
  213. /* l->m+1 must be less than l->ln, but guard in case we get a bad stream */
  214. double *lcurve=alloca(sizeof(double)*max(l->ln*2,l->m*2+2));
  215. int i;
  216. if(amp==0){
  217. memset(curve,0,sizeof(double)*l->n);
  218. return;
  219. }
  220. vorbis_lpc_to_curve(lcurve,lpc,amp,&(l->lpclook));
  221. #if 0
  222. { /******************/
  223. FILE *of;
  224. char buffer[80];
  225. int i;
  226. sprintf(buffer,"%s_%d.m",name,frameno);
  227. of=fopen(buffer,"w");
  228. for(i=0;i<l->ln;i++)
  229. fprintf(of,"%g\n",lcurve[i]);
  230. fclose(of);
  231. }
  232. #endif
  233. for(i=0;i<l->n;i++)curve[i]=lcurve[l->linearmap[i]];
  234. }
  235. static long seq=0;
  236. static int forward(vorbis_block *vb,vorbis_look_floor *i,
  237. double *in,double *out,vorbis_echstate_floor *vs){
  238. long j,k,l;
  239. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  240. vorbis_info_floor0 *info=look->vi;
  241. vorbis_echstate_floor0 *state=(vorbis_echstate_floor0 *)vs;
  242. double *work=alloca(look->n*sizeof(double));
  243. double amp;
  244. long bits=0;
  245. /* our floor comes in on a linear scale; go to a [-Inf...0] dB
  246. scale. The curve has to be positive, so we offset it. */
  247. _analysis_output("Fpre",seq,in,look->n,0,1);
  248. for(j=0;j<look->n;j++)work[j]=todB(in[j])+info->ampdB;
  249. /* use 'out' as temp storage */
  250. /* Convert our floor to a set of lpc coefficients */
  251. amp=sqrt(_curve_to_lpc(work,out,look,seq));
  252. /* amp is in the range (0. to ampdB]. Encode that range using
  253. ampbits bits */
  254. {
  255. long maxval=(1L<<info->ampbits)-1;
  256. long val=rint(amp/info->ampdB*maxval);
  257. if(val<0)val=0; /* likely */
  258. if(val>maxval)val=maxval; /* not bloody likely */
  259. _oggpack_write(&vb->opb,val,info->ampbits);
  260. if(val>0)
  261. amp=(float)val/maxval*info->ampdB;
  262. else
  263. amp=0;
  264. }
  265. if(amp>0){
  266. double *refcurve=alloca(sizeof(double)*max(look->ln*2,look->m*2+2));
  267. double *newcurve=alloca(sizeof(double)*max(look->ln*2,look->m*2+2));
  268. long *codelist=alloca(sizeof(long)*look->m);
  269. int codes=0;
  270. if(state->frameno == vb->sequence){
  271. /* generate a reference curve for testing */
  272. vorbis_lpc_to_curve(refcurve,out,1,&(look->lpclook));
  273. }
  274. /* LSP <-> LPC is orthogonal and LSP quantizes more stably */
  275. vorbis_lpc_to_lsp(out,out,look->m);
  276. #ifdef TRAIN
  277. {
  278. int j;
  279. FILE *of;
  280. char buffer[80];
  281. sprintf(buffer,"lsp0coeff_%d.vqd",vb->mode);
  282. of=fopen(buffer,"a");
  283. for(j=0;j<look->m;j++)
  284. fprintf(of,"%.12g, ",out[j]);
  285. fprintf(of,"\n");
  286. fclose(of);
  287. }
  288. #endif
  289. /* code the spectral envelope, and keep track of the actual
  290. quantized values; we don't want creeping error as each block is
  291. nailed to the last quantized value of the previous block. */
  292. /* A new development: the code selection is based on error against
  293. the LSP coefficients and not the curve it produces. Because
  294. the coefficient error is not linearly related to the curve
  295. error, the fit we select is usually nonoptimal (but
  296. sufficient). This is fine, but flailing about causes a problem
  297. in generally consistent spectra... so we add hysterisis. */
  298. /* select a new fit, but don't code it. Just grab it for testing */
  299. {
  300. /* the spec supports using one of a number of codebooks. Right
  301. now, encode using this lib supports only one */
  302. codebook *b=vb->vd->fullbooks+info->books[0];
  303. double last=0.;
  304. for(j=0;j<look->m;){
  305. for(k=0;k<b->dim;k++)out[j+k]-=last;
  306. codelist[codes++]=vorbis_book_errorv(b,out+j);
  307. for(k=0;k<b->dim;k++,j++)out[j]+=last;
  308. last=out[j-1];
  309. }
  310. }
  311. vorbis_lsp_to_lpc(out,out,look->m);
  312. vorbis_lpc_to_curve(newcurve,out,1,&(look->lpclook));
  313. /* if we're out of sequence, no hysterisis this frame, else check it */
  314. if(state->frameno != vb->sequence ||
  315. _curve_error(refcurve,newcurve,look->ln)<
  316. _curve_error(refcurve,state->curve,look->ln)){
  317. /* new curve is the fit to use. replace the state */
  318. memcpy(state->curve,newcurve,sizeof(double)*look->ln);
  319. memcpy(state->codewords,codelist,sizeof(long)*codes);
  320. state->codes=codes;
  321. }else{
  322. /* use state */
  323. fprintf(stderr,"X");
  324. codelist=state->codewords;
  325. codes=state->codes;
  326. }
  327. state->frameno=vb->sequence+1;
  328. /* the spec supports using one of a number of codebooks. Right
  329. now, encode using this lib supports only one */
  330. _oggpack_write(&vb->opb,0,_ilog(info->numbooks));
  331. {
  332. codebook *b=vb->vd->fullbooks+info->books[0];
  333. double last=0.;
  334. for(l=0,j=0;l<codes;l++){
  335. bits+=vorbis_book_encodev(b,codelist[l],out+j,&vb->opb);
  336. for(k=0;k<b->dim;k++,j++)out[j]+=last;
  337. last=out[j-1];
  338. }
  339. }
  340. /* take the coefficients back to a spectral envelope curve */
  341. vorbis_lsp_to_lpc(out,out,look->m);
  342. _lpc_to_curve(out,out,amp,look,"Ffloor",seq);
  343. for(j=0;j<look->n;j++)out[j]= fromdB(out[j]-info->ampdB);
  344. _analysis_output("Fpost",seq++,out,look->n,0,1);
  345. return(1);
  346. }
  347. memset(out,0,sizeof(double)*look->n);
  348. seq++;
  349. return(0);
  350. }
  351. static int inverse(vorbis_block *vb,vorbis_look_floor *i,double *out){
  352. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  353. vorbis_info_floor0 *info=look->vi;
  354. int j,k;
  355. int ampraw=_oggpack_read(&vb->opb,info->ampbits);
  356. if(ampraw>0){
  357. long maxval=(1<<info->ampbits)-1;
  358. double amp=(float)ampraw/maxval*info->ampdB;
  359. int booknum=_oggpack_read(&vb->opb,_ilog(info->numbooks));
  360. codebook *b=vb->vd->fullbooks+info->books[booknum];
  361. double last=0.;
  362. memset(out,0,sizeof(double)*look->m);
  363. for(j=0;j<look->m;j+=b->dim)
  364. vorbis_book_decodevs(b,out+j,&vb->opb,1,-1);
  365. for(j=0;j<look->m;){
  366. for(k=0;k<b->dim;k++,j++)out[j]+=last;
  367. last=out[j-1];
  368. }
  369. /* take the coefficients back to a spectral envelope curve */
  370. vorbis_lsp_to_lpc(out,out,look->m);
  371. _lpc_to_curve(out,out,amp,look,"",0);
  372. for(j=0;j<look->n;j++)out[j]= fromdB(out[j]-info->ampdB);
  373. return(1);
  374. }else
  375. memset(out,0,sizeof(double)*look->n);
  376. return(0);
  377. }
  378. /* export hooks */
  379. vorbis_func_floor floor0_exportbundle={
  380. &pack,&unpack,&look,&state,&free_info,&free_look,&free_state,&forward,&inverse
  381. };