floor0.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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-2015 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: floor backend 0 implementation
  13. last mod: $Id: floor0.c 19457 2015-03-03 00:15:29Z giles $
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include <ogg/ogg.h>
  19. #include "vorbis/codec.h"
  20. #include "codec_internal.h"
  21. #include "registry.h"
  22. #include "lpc.h"
  23. #include "lsp.h"
  24. #include "codebook.h"
  25. #include "scales.h"
  26. #include "misc.h"
  27. #include "os.h"
  28. #include "misc.h"
  29. #include <stdio.h>
  30. typedef struct {
  31. int ln;
  32. int m;
  33. int **linearmap;
  34. int n[2];
  35. vorbis_info_floor0 *vi;
  36. long bits;
  37. long frames;
  38. } vorbis_look_floor0;
  39. /***********************************************/
  40. static void floor0_free_info(vorbis_info_floor *i){
  41. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  42. if(info){
  43. memset(info,0,sizeof(*info));
  44. _ogg_free(info);
  45. }
  46. }
  47. static void floor0_free_look(vorbis_look_floor *i){
  48. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  49. if(look){
  50. if(look->linearmap){
  51. if(look->linearmap[0])_ogg_free(look->linearmap[0]);
  52. if(look->linearmap[1])_ogg_free(look->linearmap[1]);
  53. _ogg_free(look->linearmap);
  54. }
  55. memset(look,0,sizeof(*look));
  56. _ogg_free(look);
  57. }
  58. }
  59. static vorbis_info_floor *floor0_unpack (vorbis_info *vi,oggpack_buffer *opb){
  60. codec_setup_info *ci=vi->codec_setup;
  61. int j;
  62. vorbis_info_floor0 *info=_ogg_malloc(sizeof(*info));
  63. info->order=oggpack_read(opb,8);
  64. info->rate=oggpack_read(opb,16);
  65. info->barkmap=oggpack_read(opb,16);
  66. info->ampbits=oggpack_read(opb,6);
  67. info->ampdB=oggpack_read(opb,8);
  68. info->numbooks=oggpack_read(opb,4)+1;
  69. if(info->order<1)goto err_out;
  70. if(info->rate<1)goto err_out;
  71. if(info->barkmap<1)goto err_out;
  72. if(info->numbooks<1)goto err_out;
  73. for(j=0;j<info->numbooks;j++){
  74. info->books[j]=oggpack_read(opb,8);
  75. if(info->books[j]<0 || info->books[j]>=ci->books)goto err_out;
  76. if(ci->book_param[info->books[j]]->maptype==0)goto err_out;
  77. if(ci->book_param[info->books[j]]->dim<1)goto err_out;
  78. }
  79. return(info);
  80. err_out:
  81. floor0_free_info(info);
  82. return(NULL);
  83. }
  84. /* initialize Bark scale and normalization lookups. We could do this
  85. with static tables, but Vorbis allows a number of possible
  86. combinations, so it's best to do it computationally.
  87. The below is authoritative in terms of defining scale mapping.
  88. Note that the scale depends on the sampling rate as well as the
  89. linear block and mapping sizes */
  90. static void floor0_map_lazy_init(vorbis_block *vb,
  91. vorbis_info_floor *infoX,
  92. vorbis_look_floor0 *look){
  93. if(!look->linearmap[vb->W]){
  94. vorbis_dsp_state *vd=vb->vd;
  95. vorbis_info *vi=vd->vi;
  96. codec_setup_info *ci=vi->codec_setup;
  97. vorbis_info_floor0 *info=(vorbis_info_floor0 *)infoX;
  98. int W=vb->W;
  99. int n=ci->blocksizes[W]/2,j;
  100. /* we choose a scaling constant so that:
  101. floor(bark(rate/2-1)*C)=mapped-1
  102. floor(bark(rate/2)*C)=mapped */
  103. float scale=look->ln/toBARK(info->rate/2.f);
  104. /* the mapping from a linear scale to a smaller bark scale is
  105. straightforward. We do *not* make sure that the linear mapping
  106. does not skip bark-scale bins; the decoder simply skips them and
  107. the encoder may do what it wishes in filling them. They're
  108. necessary in some mapping combinations to keep the scale spacing
  109. accurate */
  110. look->linearmap[W]=_ogg_malloc((n+1)*sizeof(**look->linearmap));
  111. for(j=0;j<n;j++){
  112. int val=floor( toBARK((info->rate/2.f)/n*j)
  113. *scale); /* bark numbers represent band edges */
  114. if(val>=look->ln)val=look->ln-1; /* guard against the approximation */
  115. look->linearmap[W][j]=val;
  116. }
  117. look->linearmap[W][j]=-1;
  118. look->n[W]=n;
  119. }
  120. }
  121. static vorbis_look_floor *floor0_look(vorbis_dsp_state *vd,
  122. vorbis_info_floor *i){
  123. vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
  124. vorbis_look_floor0 *look=_ogg_calloc(1,sizeof(*look));
  125. (void)vd;
  126. look->m=info->order;
  127. look->ln=info->barkmap;
  128. look->vi=info;
  129. look->linearmap=_ogg_calloc(2,sizeof(*look->linearmap));
  130. return look;
  131. }
  132. static void *floor0_inverse1(vorbis_block *vb,vorbis_look_floor *i){
  133. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  134. vorbis_info_floor0 *info=look->vi;
  135. int j,k;
  136. int ampraw=oggpack_read(&vb->opb,info->ampbits);
  137. if(ampraw>0){ /* also handles the -1 out of data case */
  138. long maxval=(1<<info->ampbits)-1;
  139. float amp=(float)ampraw/maxval*info->ampdB;
  140. int booknum=oggpack_read(&vb->opb,ov_ilog(info->numbooks));
  141. if(booknum!=-1 && booknum<info->numbooks){ /* be paranoid */
  142. codec_setup_info *ci=vb->vd->vi->codec_setup;
  143. codebook *b=ci->fullbooks+info->books[booknum];
  144. float last=0.f;
  145. /* the additional b->dim is a guard against any possible stack
  146. smash; b->dim is provably more than we can overflow the
  147. vector */
  148. float *lsp=_vorbis_block_alloc(vb,sizeof(*lsp)*(look->m+b->dim+1));
  149. if(vorbis_book_decodev_set(b,lsp,&vb->opb,look->m)==-1)goto eop;
  150. for(j=0;j<look->m;){
  151. for(k=0;j<look->m && k<b->dim;k++,j++)lsp[j]+=last;
  152. last=lsp[j-1];
  153. }
  154. lsp[look->m]=amp;
  155. return(lsp);
  156. }
  157. }
  158. eop:
  159. return(NULL);
  160. }
  161. static int floor0_inverse2(vorbis_block *vb,vorbis_look_floor *i,
  162. void *memo,float *out){
  163. vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
  164. vorbis_info_floor0 *info=look->vi;
  165. floor0_map_lazy_init(vb,info,look);
  166. if(memo){
  167. float *lsp=(float *)memo;
  168. float amp=lsp[look->m];
  169. /* take the coefficients back to a spectral envelope curve */
  170. vorbis_lsp_to_curve(out,
  171. look->linearmap[vb->W],
  172. look->n[vb->W],
  173. look->ln,
  174. lsp,look->m,amp,(float)info->ampdB);
  175. return(1);
  176. }
  177. memset(out,0,sizeof(*out)*look->n[vb->W]);
  178. return(0);
  179. }
  180. /* export hooks */
  181. const vorbis_func_floor floor0_exportbundle={
  182. NULL,&floor0_unpack,&floor0_look,&floor0_free_info,
  183. &floor0_free_look,&floor0_inverse1,&floor0_inverse2
  184. };