floor0.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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.11.2.1.2.6 2000/05/08 08:25:43 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. typedef struct {
  29. long n;
  30. int ln;
  31. int m;
  32. int *linearmap;
  33. vorbis_info_floor0 *vi;
  34. lpc_lookup lpclook;
  35. } vorbis_look_floor0;
  36. static void free_info(vorbis_info_floor *i){
  37. if(i){
  38. memset(i,0,sizeof(vorbis_info_floor0));
  39. free(i);
  40. }
  41. }
  42. static void free_look(vorbis_look_floor *i){
  43. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  44. if(i){
  45. if(look->linearmap)free(look->linearmap);
  46. lpc_clear(&look->lpclook);
  47. memset(look,0,sizeof(vorbis_look_floor0));
  48. free(look);
  49. }
  50. }
  51. static void pack (vorbis_info_floor *i,oggpack_buffer *opb){
  52. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  53. int j;
  54. _oggpack_write(opb,info->order,8);
  55. _oggpack_write(opb,info->rate,16);
  56. _oggpack_write(opb,info->barkmap,16);
  57. _oggpack_write(opb,info->ampbits,6);
  58. _oggpack_write(opb,info->ampdB,8);
  59. _oggpack_write(opb,info->numbooks-1,4);
  60. for(j=0;j<info->numbooks;j++)
  61. _oggpack_write(opb,info->books[j],8);
  62. }
  63. static vorbis_info_floor *unpack (vorbis_info *vi,oggpack_buffer *opb){
  64. int j;
  65. vorbis_info_floor0 *info=malloc(sizeof(vorbis_info_floor0));
  66. info->order=_oggpack_read(opb,8);
  67. info->rate=_oggpack_read(opb,16);
  68. info->barkmap=_oggpack_read(opb,16);
  69. info->ampbits=_oggpack_read(opb,6);
  70. info->ampdB=_oggpack_read(opb,8);
  71. info->numbooks=_oggpack_read(opb,4)+1;
  72. if(info->order<1)goto err_out;
  73. if(info->rate<1)goto err_out;
  74. if(info->barkmap<1)goto err_out;
  75. if(info->numbooks<1)goto err_out;
  76. for(j=0;j<info->numbooks;j++){
  77. info->books[j]=_oggpack_read(opb,8);
  78. if(info->books[j]<0 || info->books[j]>=vi->books)goto err_out;
  79. }
  80. return(info);
  81. err_out:
  82. free_info(info);
  83. return(NULL);
  84. }
  85. /* initialize Bark scale and normalization lookups. We could do this
  86. with static tables, but Vorbis allows a number of possible
  87. combinations, so it's best to do it computationally.
  88. The below is authoritative in terms of defining scale mapping.
  89. Note that the scale depends on the sampling rate as well as the
  90. linear block and mapping sizes */
  91. static vorbis_look_floor *look (vorbis_dsp_state *vd,vorbis_info_mode *mi,
  92. vorbis_info_floor *i){
  93. int j;
  94. double scale;
  95. vorbis_info *vi=vd->vi;
  96. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  97. vorbis_look_floor0 *look=malloc(sizeof(vorbis_look_floor0));
  98. look->m=info->order;
  99. look->n=vi->blocksizes[mi->blockflag]/2;
  100. look->ln=info->barkmap;
  101. look->vi=info;
  102. lpc_init(&look->lpclook,look->ln,look->m);
  103. /* we choose a scaling constant so that:
  104. floor(bark(rate/2-1)*C)=mapped-1
  105. floor(bark(rate/2)*C)=mapped */
  106. scale=look->ln/toBARK(info->rate/2.);
  107. /* the mapping from a linear scale to a smaller bark scale is
  108. straightforward. We do *not* make sure that the linear mapping
  109. does not skip bark-scale bins; the decoder simply skips them and
  110. the encoder may do what it wishes in filling them. They're
  111. necessary in some mapping combinations to keep the scale spacing
  112. accurate */
  113. look->linearmap=malloc(look->n*sizeof(int));
  114. for(j=0;j<look->n;j++){
  115. int val=floor( toBARK((info->rate/2.)/look->n*j)
  116. *scale); /* bark numbers represent band edges */
  117. if(val>look->ln)val=look->ln; /* guard against the approximation */
  118. look->linearmap[j]=val;
  119. }
  120. return look;
  121. }
  122. #include <stdio.h>
  123. /* less efficient than the decode side (written for clarity). We're
  124. not bottlenecked here anyway */
  125. double _curve_to_lpc(double *curve,double *lpc,vorbis_look_floor0 *l,
  126. long frameno){
  127. /* map the input curve to a bark-scale curve for encoding */
  128. int mapped=l->ln;
  129. double *work=alloca(sizeof(double)*mapped);
  130. int i,j,last=0;
  131. memset(work,0,sizeof(double)*mapped);
  132. /* Only the decode side is behavior-specced; for now in the encoder,
  133. we select the maximum value of each band as representative (this
  134. helps make sure peaks don't go out of range. In error terms,
  135. selecting min would make more sense, but the codebook is trained
  136. numerically, so we don't actually lose. We'd still want to
  137. use the original curve for error and noise estimation */
  138. for(i=0;i<l->n;i++){
  139. int bark=l->linearmap[i];
  140. if(work[bark]<curve[i])work[bark]=curve[i];
  141. if(bark>last+1){
  142. /* If the bark scale is climbing rapidly, some bins may end up
  143. going unused. This isn't a waste actually; it keeps the
  144. scale resolution even so that the LPC generator has an easy
  145. time. However, if we leave the bins empty we lose energy.
  146. So, fill 'em in. The decoder does not do anything with he
  147. unused bins, so we can fill them anyway we like to end up
  148. with a better spectral curve */
  149. /* we'll always have a bin zero, so we don't need to guard init */
  150. long span=bark-last;
  151. for(j=1;j<span;j++){
  152. double del=(double)j/span;
  153. work[j+last]=work[bark]*del+work[last]*(1.-del);
  154. }
  155. }
  156. last=bark;
  157. }
  158. #if 0
  159. { /******************/
  160. FILE *of;
  161. char buffer[80];
  162. int i;
  163. sprintf(buffer,"Fmask_%d.m",frameno);
  164. of=fopen(buffer,"w");
  165. for(i=0;i<mapped;i++)
  166. fprintf(of,"%g\n",work[i]);
  167. fclose(of);
  168. }
  169. #endif
  170. return vorbis_lpc_from_curve(work,lpc,&(l->lpclook));
  171. }
  172. /* generate the whole freq response curve of an LPC IIR filter */
  173. void _lpc_to_curve(double *curve,double *lpc,double amp,
  174. vorbis_look_floor0 *l,char *name,long frameno){
  175. double *lcurve=alloca(sizeof(double)*(l->ln*2));
  176. int i;
  177. if(amp==0){
  178. memset(curve,0,sizeof(double)*l->n);
  179. return;
  180. }
  181. vorbis_lpc_to_curve(lcurve,lpc,amp,&(l->lpclook));
  182. #if 0
  183. { /******************/
  184. FILE *of;
  185. char buffer[80];
  186. int i;
  187. sprintf(buffer,"%s_%d.m",name,frameno);
  188. of=fopen(buffer,"w");
  189. for(i=0;i<l->ln;i++)
  190. fprintf(of,"%g\n",lcurve[i]);
  191. fclose(of);
  192. }
  193. #endif
  194. for(i=0;i<l->n;i++)curve[i]=lcurve[l->linearmap[i]];
  195. }
  196. static long seq=0;
  197. static int forward(vorbis_block *vb,vorbis_look_floor *i,
  198. double *in,double *out){
  199. long j,k;
  200. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  201. vorbis_info_floor0 *info=look->vi;
  202. double *work=alloca(look->n*sizeof(double));
  203. double amp;
  204. long bits=0;
  205. /* our floor comes in on a linear scale; go to a [-Inf...0] dB
  206. scale. The curve has to be positive, so we offset it. */
  207. for(j=0;j<look->n;j++)work[j]=todB(in[j])+info->ampdB;
  208. /* use 'out' as temp storage */
  209. /* Convert our floor to a set of lpc coefficients */
  210. amp=sqrt(_curve_to_lpc(work,out,look,vb->sequence));
  211. /* amp is in the range (0. to ampdB]. Encode that range using
  212. ampbits bits */
  213. {
  214. long maxval=(1<<info->ampbits)-1;
  215. long val=rint(amp/info->ampdB*maxval);
  216. if(val<0)val=0; /* likely */
  217. if(val>maxval)val=maxval; /* not bloody likely */
  218. _oggpack_write(&vb->opb,val,info->ampbits);
  219. if(val>0)
  220. amp=(float)val/maxval*info->ampdB;
  221. else
  222. amp=0;
  223. }
  224. if(amp>0){
  225. /* LSP <-> LPC is orthogonal and LSP quantizes more stably */
  226. vorbis_lpc_to_lsp(out,out,look->m);
  227. #ifdef ANALYSIS
  228. if(vb->mode==0)_analysis_output("lsp",seq++,out,look->m,0,0);
  229. #endif
  230. #ifdef TRAIN
  231. {
  232. int j;
  233. FILE *of;
  234. char buffer[80];
  235. sprintf(buffer,"lsp0coeff_%d.vqd",vb->mode);
  236. of=fopen(buffer,"a");
  237. for(j=0;j<look->m;j++)
  238. fprintf(of,"%g, ",out[j]);
  239. fprintf(of,"\n");
  240. fclose(of);
  241. }
  242. #endif
  243. #if 0
  244. { /******************/
  245. vorbis_lsp_to_lpc(out,work,look->m);
  246. _lpc_to_curve(work,work,amp,look,"Fprefloor",vb->sequence);
  247. }
  248. #endif
  249. /* code the spectral envelope, and keep track of the actual
  250. quantized values; we don't want creeping error as each block is
  251. nailed to the last quantized value of the previous block. */
  252. /* the spec supports using one of a number of codebooks. Right
  253. now, encode using this lib supports only one */
  254. _oggpack_write(&vb->opb,0,_ilog(info->numbooks));
  255. {
  256. codebook *b=vb->vd->fullbooks+info->books[0];
  257. double last=0.;
  258. for(j=0;j<look->m;){
  259. for(k=0;k<b->dim;k++)out[j+k]-=last;
  260. bits+=vorbis_book_encodev(b,out+j,&vb->opb);
  261. for(k=0;k<b->dim;k++,j++)out[j]+=last;
  262. last=out[j-1];
  263. }
  264. }
  265. /* take the coefficients back to a spectral envelope curve */
  266. vorbis_lsp_to_lpc(out,out,look->m);
  267. _lpc_to_curve(out,out,amp,look,"Ffloor",vb->sequence);
  268. for(j=0;j<look->n;j++)out[j]= fromdB(out[j]-info->ampdB);
  269. return(1);
  270. }
  271. memset(out,0,sizeof(double)*look->n);
  272. return(0);
  273. }
  274. static int inverse(vorbis_block *vb,vorbis_look_floor *i,double *out){
  275. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  276. vorbis_info_floor0 *info=look->vi;
  277. int j,k;
  278. int ampraw=_oggpack_read(&vb->opb,info->ampbits);
  279. if(ampraw>0){
  280. long maxval=(1<<info->ampbits)-1;
  281. double amp=(float)ampraw/maxval*info->ampdB;
  282. int booknum=_oggpack_read(&vb->opb,_ilog(info->numbooks));
  283. codebook *b=vb->vd->fullbooks+info->books[booknum];
  284. double last=0.;
  285. memset(out,0,sizeof(double)*look->m);
  286. for(j=0;j<look->m;j+=b->dim)
  287. vorbis_book_decodevs(b,out+j,&vb->opb,1,-1);
  288. for(j=0;j<look->m;){
  289. for(k=0;k<b->dim;k++,j++)out[j]+=last;
  290. last=out[j-1];
  291. }
  292. /* take the coefficients back to a spectral envelope curve */
  293. vorbis_lsp_to_lpc(out,out,look->m);
  294. _lpc_to_curve(out,out,amp,look,"",0);
  295. for(j=0;j<look->n;j++)out[j]= fromdB(out[j]-info->ampdB);
  296. return(1);
  297. }else
  298. memset(out,0,sizeof(double)*look->n);
  299. return(0);
  300. }
  301. /* export hooks */
  302. vorbis_func_floor floor0_exportbundle={
  303. &pack,&unpack,&look,&free_info,&free_look,&forward,&inverse
  304. };