floor0.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
  6. * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis 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.25.2.3 2000/11/04 06:43:49 xiphmont 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 "codec_internal.h"
  22. #include "registry.h"
  23. #include "lpc.h"
  24. #include "lsp.h"
  25. #include "codebook.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 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. 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. if(look->linearmap)free(look->linearmap);
  76. if(look->lsp_look)free(look->lsp_look);
  77. lpc_clear(&look->lpclook);
  78. memset(look,0,sizeof(vorbis_look_floor0));
  79. free(look);
  80. }
  81. }
  82. static void floor0_pack (vorbis_info_floor *i,oggpack_buffer *opb){
  83. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  84. int j;
  85. oggpack_write(opb,info->order,8);
  86. oggpack_write(opb,info->rate,16);
  87. oggpack_write(opb,info->barkmap,16);
  88. oggpack_write(opb,info->ampbits,6);
  89. oggpack_write(opb,info->ampdB,8);
  90. oggpack_write(opb,info->numbooks-1,4);
  91. for(j=0;j<info->numbooks;j++)
  92. oggpack_write(opb,info->books[j],8);
  93. }
  94. static vorbis_info_floor *floor0_unpack (vorbis_info *vi,oggpack_buffer *opb){
  95. codec_setup_info *ci=vi->codec_setup;
  96. int j;
  97. vorbis_info_floor0 *info=_ogg_malloc(sizeof(vorbis_info_floor0));
  98. info->order=oggpack_read(opb,8);
  99. info->rate=oggpack_read(opb,16);
  100. info->barkmap=oggpack_read(opb,16);
  101. info->ampbits=oggpack_read(opb,6);
  102. info->ampdB=oggpack_read(opb,8);
  103. info->numbooks=oggpack_read(opb,4)+1;
  104. if(info->order<1)goto err_out;
  105. if(info->rate<1)goto err_out;
  106. if(info->barkmap<1)goto err_out;
  107. if(info->numbooks<1)goto err_out;
  108. for(j=0;j<info->numbooks;j++){
  109. info->books[j]=oggpack_read(opb,8);
  110. if(info->books[j]<0 || info->books[j]>=ci->books)goto err_out;
  111. }
  112. return(info);
  113. err_out:
  114. floor0_free_info(info);
  115. return(NULL);
  116. }
  117. /* initialize Bark scale and normalization lookups. We could do this
  118. with static tables, but Vorbis allows a number of possible
  119. combinations, so it's best to do it computationally.
  120. The below is authoritative in terms of defining scale mapping.
  121. Note that the scale depends on the sampling rate as well as the
  122. linear block and mapping sizes */
  123. static vorbis_look_floor *floor0_look (vorbis_dsp_state *vd,vorbis_info_mode *mi,
  124. vorbis_info_floor *i){
  125. int j;
  126. float scale;
  127. vorbis_info *vi=vd->vi;
  128. codec_setup_info *ci=vi->codec_setup;
  129. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  130. vorbis_look_floor0 *look=_ogg_calloc(1,sizeof(vorbis_look_floor0));
  131. look->m=info->order;
  132. look->n=ci->blocksizes[mi->blockflag]/2;
  133. look->ln=info->barkmap;
  134. look->vi=info;
  135. if(vd->analysisp)
  136. lpc_init(&look->lpclook,look->ln,look->m);
  137. /* we choose a scaling constant so that:
  138. floor(bark(rate/2-1)*C)=mapped-1
  139. floor(bark(rate/2)*C)=mapped */
  140. scale=look->ln/toBARK(info->rate/2.);
  141. /* the mapping from a linear scale to a smaller bark scale is
  142. straightforward. We do *not* make sure that the linear mapping
  143. does not skip bark-scale bins; the decoder simply skips them and
  144. the encoder may do what it wishes in filling them. They're
  145. necessary in some mapping combinations to keep the scale spacing
  146. accurate */
  147. look->linearmap=_ogg_malloc((look->n+1)*sizeof(int));
  148. for(j=0;j<look->n;j++){
  149. int val=floor( toBARK((info->rate/2.)/look->n*j)
  150. *scale); /* bark numbers represent band edges */
  151. if(val>look->ln)val=look->ln; /* guard against the approximation */
  152. look->linearmap[j]=val;
  153. }
  154. look->linearmap[j]=-1;
  155. look->lsp_look=_ogg_malloc(look->ln*sizeof(float));
  156. for(j=0;j<look->ln;j++)
  157. look->lsp_look[j]=2*cos(M_PI/look->ln*j);
  158. return look;
  159. }
  160. /* less efficient than the decode side (written for clarity). We're
  161. not bottlenecked here anyway */
  162. float _curve_to_lpc(float *curve,float *lpc,
  163. vorbis_look_floor0 *l){
  164. /* map the input curve to a bark-scale curve for encoding */
  165. int mapped=l->ln;
  166. float *work=alloca(sizeof(float)*mapped);
  167. int i,j,last=0;
  168. int bark=0;
  169. memset(work,0,sizeof(float)*mapped);
  170. /* Only the decode side is behavior-specced; for now in the encoder,
  171. we select the maximum value of each band as representative (this
  172. helps make sure peaks don't go out of range. In error terms,
  173. selecting min would make more sense, but the codebook is trained
  174. numerically, so we don't actually lose. We'd still want to
  175. use the original curve for error and noise estimation */
  176. for(i=0;i<l->n;i++){
  177. bark=l->linearmap[i];
  178. if(work[bark]<curve[i])work[bark]=curve[i];
  179. if(bark>last+1){
  180. /* If the bark scale is climbing rapidly, some bins may end up
  181. going unused. This isn't a waste actually; it keeps the
  182. scale resolution even so that the LPC generator has an easy
  183. time. However, if we leave the bins empty we lose energy.
  184. So, fill 'em in. The decoder does not do anything with he
  185. unused bins, so we can fill them anyway we like to end up
  186. with a better spectral curve */
  187. /* we'll always have a bin zero, so we don't need to guard init */
  188. long span=bark-last;
  189. for(j=1;j<span;j++){
  190. float del=(float)j/span;
  191. work[j+last]=work[bark]*del+work[last]*(1.-del);
  192. }
  193. }
  194. last=bark;
  195. }
  196. /* If we're over-ranged to avoid edge effects, fill in the end of spectrum gap */
  197. for(i=bark+1;i<mapped;i++)
  198. work[i]=work[i-1];
  199. return vorbis_lpc_from_curve(work,lpc,&(l->lpclook));
  200. }
  201. /* generate the whole freq response curve of an LSP IIR filter */
  202. static int floor0_forward(vorbis_block *vb,vorbis_look_floor *i,
  203. float *in,float *out){
  204. long j;
  205. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  206. vorbis_info_floor0 *info=look->vi;
  207. float *work=alloca((look->ln+look->n)*sizeof(float));
  208. float amp;
  209. long bits=0;
  210. #ifdef TRAIN_LSP
  211. FILE *of;
  212. FILE *ef;
  213. char buffer[80];
  214. #if 1
  215. sprintf(buffer,"lsp0coeff_%d.vqd",vb->mode);
  216. of=fopen(buffer,"a");
  217. #endif
  218. sprintf(buffer,"lsp0ent_%d.vqd",vb->mode);
  219. ef=fopen(buffer,"a");
  220. #endif
  221. /* our floor comes in on a linear scale; go to a [-Inf...0] dB
  222. scale. The curve has to be positive, so we offset it. */
  223. for(j=0;j<look->n;j++)
  224. work[j]=todB(in[j])+info->ampdB;
  225. /* use 'out' as temp storage */
  226. /* Convert our floor to a set of lpc coefficients */
  227. amp=sqrt(_curve_to_lpc(work,out,look));
  228. /* amp is in the range (0. to ampdB]. Encode that range using
  229. ampbits bits */
  230. {
  231. long maxval=(1L<<info->ampbits)-1;
  232. long val=rint(amp/info->ampdB*maxval);
  233. if(val<0)val=0; /* likely */
  234. if(val>maxval)val=maxval; /* not bloody likely */
  235. oggpack_write(&vb->opb,val,info->ampbits);
  236. if(val>0)
  237. amp=(float)val/maxval*info->ampdB;
  238. else
  239. amp=0;
  240. }
  241. if(amp>0){
  242. /* the spec supports using one of a number of codebooks. Right
  243. now, encode using this lib supports only one */
  244. backend_lookup_state *be=vb->vd->backend_state;
  245. codebook *b=be->fullbooks+info->books[0];
  246. oggpack_write(&vb->opb,0,_ilog(info->numbooks));
  247. /* LSP <-> LPC is orthogonal and LSP quantizes more stably */
  248. vorbis_lpc_to_lsp(out,out,look->m);
  249. #if 1
  250. #ifdef TRAIN_LSP
  251. {
  252. float last=0.;
  253. for(j=0;j<look->m;j++){
  254. fprintf(of,"%.12g, ",out[j]-last);
  255. last=out[j];
  256. }
  257. }
  258. fprintf(of,"\n");
  259. fclose(of);
  260. #endif
  261. #endif
  262. /* code the spectral envelope, and keep track of the actual
  263. quantized values; we don't want creeping error as each block is
  264. nailed to the last quantized value of the previous block. */
  265. for(j=0;j<look->m;j+=b->dim){
  266. int entry=_f0_fit(b,out,work,j);
  267. bits+=vorbis_book_encode(b,entry,&vb->opb);
  268. #ifdef TRAIN_LSP
  269. fprintf(ef,"%d,\n",entry);
  270. #endif
  271. }
  272. #ifdef ANALYSIS
  273. {
  274. float last=0;
  275. for(j=0;j<look->m;j++){
  276. out[j]=work[j]-last;
  277. last=work[j];
  278. }
  279. }
  280. #endif
  281. #ifdef TRAIN_LSP
  282. fclose(ef);
  283. #endif
  284. /* take the coefficients back to a spectral envelope curve */
  285. vorbis_lsp_to_curve(out,look->linearmap,look->n,look->ln,
  286. work,look->m,amp,info->ampdB);
  287. return(1);
  288. }
  289. memset(out,0,sizeof(float)*look->n);
  290. return(0);
  291. }
  292. static int floor0_inverse(vorbis_block *vb,vorbis_look_floor *i,float *out){
  293. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  294. vorbis_info_floor0 *info=look->vi;
  295. int j,k;
  296. int ampraw=oggpack_read(&vb->opb,info->ampbits);
  297. if(ampraw>0){ /* also handles the -1 out of data case */
  298. long maxval=(1<<info->ampbits)-1;
  299. float amp=(float)ampraw/maxval*info->ampdB;
  300. int booknum=oggpack_read(&vb->opb,_ilog(info->numbooks));
  301. float *lsp=alloca(sizeof(float)*look->m);
  302. if(booknum!=-1){
  303. backend_lookup_state *be=vb->vd->backend_state;
  304. codebook *b=be->fullbooks+info->books[booknum];
  305. float last=0.;
  306. memset(out,0,sizeof(double)*look->m);
  307. for(j=0;j<look->m;j+=b->dim)
  308. if(vorbis_book_decodevs(b,lsp+j,&vb->opb,1,-1)==-1)goto eop;
  309. for(j=0;j<look->m;){
  310. for(k=0;k<b->dim;k++,j++)lsp[j]+=last;
  311. last=lsp[j-1];
  312. }
  313. /* take the coefficients back to a spectral envelope curve */
  314. vorbis_lsp_to_curve(out,look->linearmap,look->n,look->ln,
  315. lsp,look->m,amp,info->ampdB);
  316. return(1);
  317. }
  318. }
  319. eop:
  320. memset(out,0,sizeof(float)*look->n);
  321. return(0);
  322. }
  323. /* export hooks */
  324. vorbis_func_floor floor0_exportbundle={
  325. &floor0_pack,&floor0_unpack,&floor0_look,&floor0_copy_info,&floor0_free_info,
  326. &floor0_free_look,&floor0_forward,&floor0_inverse
  327. };