vbr.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* Copyright (C) 2002 Jean-Marc Valin
  2. File: vbr.c
  3. VBR-related routines
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "vbr.h"
  31. #include <math.h>
  32. #define sqr(x) ((x)*(x))
  33. #define MIN_ENERGY 6000
  34. #define NOISE_POW .3
  35. #ifndef DISABLE_VBR
  36. const float vbr_nb_thresh[9][11]={
  37. {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f}, /* CNG */
  38. { 4.0f, 2.5f, 2.0f, 1.2f, 0.5f,-0.25f, -0.5f, -0.7f, -0.8f, -0.9f, -1.0f}, /* 2 kbps */
  39. {10.0f, 6.5f, 5.2f, 4.5f, 3.9f, 3.7f, 3.0f, 2.5f, 2.3f, 1.8f, 1.0f}, /* 6 kbps */
  40. {11.0f, 8.8f, 7.5f, 6.5f, 5.0f, 4.2f, 3.9f, 3.9f, 3.5f, 3.0f, 1.0f}, /* 8 kbps */
  41. {11.0f, 11.0f, 9.9f, 8.5f, 7.0f, 5.25f, 4.5f, 4.0f, 4.0f, 4.0f, 2.0f}, /* 11 kbps */
  42. {11.0f, 11.0f, 11.0f, 11.0f, 9.5f, 9.25f, 8.0f, 7.0f, 5.0f, 4.0f, 3.0f}, /* 15 kbps */
  43. {11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 9.5f, 8.5f, 6.2f, 5.2f, 5.0f}, /* 18 kbps */
  44. {11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 10.0f, 9.8f, 7.5f}, /* 24 kbps */
  45. { 7.0f, 4.5f, 3.7f, 3.0f, 2.5f, 1.0f, 1.8f, 1.5f, 1.0f, 0.0f, 0.0f} /* 4 kbps */
  46. };
  47. const float vbr_hb_thresh[5][11]={
  48. {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f}, /* silence */
  49. {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f}, /* 2 kbps */
  50. {11.0f, 11.0f, 9.5f, 8.5f, 7.5f, 6.0f, 5.0f, 3.9f, 3.0f, 2.0f, 1.0f}, /* 6 kbps */
  51. {11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 9.5f, 8.7f, 7.8f, 7.0f, 6.5f, 4.0f}, /* 10 kbps */
  52. {11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 9.8f, 7.5f, 5.5f} /* 18 kbps */
  53. };
  54. const float vbr_uhb_thresh[2][11]={
  55. {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f}, /* silence */
  56. { 3.9f, 2.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f} /* 2 kbps */
  57. };
  58. void vbr_init(VBRState *vbr)
  59. {
  60. int i;
  61. vbr->average_energy=1600000;
  62. vbr->last_energy=1;
  63. vbr->accum_sum=0;
  64. vbr->soft_pitch=0;
  65. vbr->last_pitch_coef=0;
  66. vbr->last_quality=0;
  67. vbr->noise_accum = .05*pow(MIN_ENERGY, NOISE_POW);
  68. vbr->noise_accum_count=.05;
  69. vbr->noise_level=vbr->noise_accum/vbr->noise_accum_count;
  70. vbr->consec_noise=0;
  71. for (i=0;i<VBR_MEMORY_SIZE;i++)
  72. vbr->last_log_energy[i] = log(MIN_ENERGY);
  73. }
  74. /*
  75. This function should analyse the signal and decide how critical the
  76. coding error will be perceptually. The following factors should be
  77. taken into account:
  78. -Attacks (positive energy derivative) should be coded with more bits
  79. -Stationary voiced segments should receive more bits
  80. -Segments with (very) low absolute energy should receive less bits (maybe
  81. only shaped noise?)
  82. -DTX for near-zero energy?
  83. -Stationary fricative segments should have less bits
  84. -Temporal masking: when energy slope is decreasing, decrease the bit-rate
  85. -Decrease bit-rate for males (low pitch)?
  86. -(wideband only) less bits in the high-band when signal is very
  87. non-stationary (harder to notice high-frequency noise)???
  88. */
  89. float vbr_analysis(VBRState *vbr, spx_word16_t *sig, int len, int pitch, float pitch_coef)
  90. {
  91. int i;
  92. float ener=0, ener1=0, ener2=0;
  93. float qual=7;
  94. float log_energy;
  95. float non_st=0;
  96. float voicing;
  97. float pow_ener;
  98. for (i=0;i<len>>1;i++)
  99. ener1 += ((float)sig[i])*sig[i];
  100. for (i=len>>1;i<len;i++)
  101. ener2 += ((float)sig[i])*sig[i];
  102. ener=ener1+ener2;
  103. log_energy = log(ener+MIN_ENERGY);
  104. for (i=0;i<VBR_MEMORY_SIZE;i++)
  105. non_st += sqr(log_energy-vbr->last_log_energy[i]);
  106. non_st = non_st/(30*VBR_MEMORY_SIZE);
  107. if (non_st>1)
  108. non_st=1;
  109. voicing = 3*(pitch_coef-.4)*fabs(pitch_coef-.4);
  110. vbr->average_energy = 0.9*vbr->average_energy + .1*ener;
  111. vbr->noise_level=vbr->noise_accum/vbr->noise_accum_count;
  112. pow_ener = pow(ener,NOISE_POW);
  113. if (vbr->noise_accum_count<.06 && ener>MIN_ENERGY)
  114. vbr->noise_accum = .05*pow_ener;
  115. if ((voicing<.3 && non_st < .2 && pow_ener < 1.2*vbr->noise_level)
  116. || (voicing<.3 && non_st < .05 && pow_ener < 1.5*vbr->noise_level)
  117. || (voicing<.4 && non_st < .05 && pow_ener < 1.2*vbr->noise_level)
  118. || (voicing<0 && non_st < .05))
  119. {
  120. float tmp;
  121. vbr->consec_noise++;
  122. if (pow_ener > 3*vbr->noise_level)
  123. tmp = 3*vbr->noise_level;
  124. else
  125. tmp = pow_ener;
  126. if (vbr->consec_noise>=4)
  127. {
  128. vbr->noise_accum = .95*vbr->noise_accum + .05*tmp;
  129. vbr->noise_accum_count = .95*vbr->noise_accum_count + .05;
  130. }
  131. } else {
  132. vbr->consec_noise=0;
  133. }
  134. if (pow_ener < vbr->noise_level && ener>MIN_ENERGY)
  135. {
  136. vbr->noise_accum = .95*vbr->noise_accum + .05*pow_ener;
  137. vbr->noise_accum_count = .95*vbr->noise_accum_count + .05;
  138. }
  139. /* Checking for very low absolute energy */
  140. if (ener < 30000)
  141. {
  142. qual -= .7;
  143. if (ener < 10000)
  144. qual-=.7;
  145. if (ener < 3000)
  146. qual-=.7;
  147. } else {
  148. float short_diff, long_diff;
  149. short_diff = log((ener+1)/(1+vbr->last_energy));
  150. long_diff = log((ener+1)/(1+vbr->average_energy));
  151. /*fprintf (stderr, "%f %f\n", short_diff, long_diff);*/
  152. if (long_diff<-5)
  153. long_diff=-5;
  154. if (long_diff>2)
  155. long_diff=2;
  156. if (long_diff>0)
  157. qual += .6*long_diff;
  158. if (long_diff<0)
  159. qual += .5*long_diff;
  160. if (short_diff>0)
  161. {
  162. if (short_diff>5)
  163. short_diff=5;
  164. qual += 1*short_diff;
  165. }
  166. /* Checking for energy increases */
  167. if (ener2 > 1.6*ener1)
  168. qual += .5;
  169. }
  170. vbr->last_energy = ener;
  171. vbr->soft_pitch = .8*vbr->soft_pitch + .2*pitch_coef;
  172. qual += 2.2*((pitch_coef-.4) + (vbr->soft_pitch-.4));
  173. if (qual < vbr->last_quality)
  174. qual = .5*qual + .5*vbr->last_quality;
  175. if (qual<4)
  176. qual=4;
  177. if (qual>10)
  178. qual=10;
  179. /*
  180. if (vbr->consec_noise>=2)
  181. qual-=1.3;
  182. if (vbr->consec_noise>=5)
  183. qual-=1.3;
  184. if (vbr->consec_noise>=12)
  185. qual-=1.3;
  186. */
  187. if (vbr->consec_noise>=3)
  188. qual=4;
  189. if (vbr->consec_noise)
  190. qual -= 1.0 * (log(3.0 + vbr->consec_noise)-log(3));
  191. if (qual<0)
  192. qual=0;
  193. if (ener<1600000)
  194. {
  195. if (vbr->consec_noise>2)
  196. qual-=0.5*(log(3.0 + vbr->consec_noise)-log(3));
  197. if (ener<10000&&vbr->consec_noise>2)
  198. qual-=0.5*(log(3.0 + vbr->consec_noise)-log(3));
  199. if (qual<0)
  200. qual=0;
  201. qual += .3*log(.0001+ener/1600000.0);
  202. }
  203. if (qual<-1)
  204. qual=-1;
  205. /*printf ("%f %f %f %f\n", qual, voicing, non_st, pow_ener/(.01+vbr->noise_level));*/
  206. vbr->last_pitch_coef = pitch_coef;
  207. vbr->last_quality = qual;
  208. for (i=VBR_MEMORY_SIZE-1;i>0;i--)
  209. vbr->last_log_energy[i] = vbr->last_log_energy[i-1];
  210. vbr->last_log_energy[0] = log_energy;
  211. /*printf ("VBR: %f %f %f %f\n", (float)(log_energy-log(vbr->average_energy+MIN_ENERGY)), non_st, voicing, vbr->noise_level);*/
  212. return qual;
  213. }
  214. void vbr_destroy(VBRState *vbr)
  215. {
  216. }
  217. #endif /* #ifndef DISABLE_VBR */