floor0.c 6.7 KB

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