vbr.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.0f, -0.5f, -0.7f, -0.8f, -0.9f, -1.0f}, /* 2 kbps */
  39. {10.0f, 6.5f, 5.2f, 4.5f, 3.9f, 3.5f, 3.0f, 2.5f, 2.3f, 1.8f, 1.0f}, /* 6 kbps */
  40. {11.0f, 8.8f, 7.5f, 6.5f, 5.0f, 3.9f, 3.9f, 3.9f, 3.5f, 3.0f, 1.0f}, /* 8 kbps */
  41. {11.0f, 11.0f, 9.9f, 8.5f, 7.0f, 6.0f, 4.5f, 4.0f, 4.0f, 4.0f, 2.0f}, /* 11 kbps */
  42. {11.0f, 11.0f, 11.0f, 11.0f, 9.5f, 8.5f, 8.0f, 7.0f, 6.0f, 5.0f, 3.0f}, /* 15 kbps */
  43. {11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 9.5f, 8.5f, 7.0f, 6.0f, 5.0f}, /* 18 kbps */
  44. {11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 11.0f, 9.8f, 9.5f, 7.5f}, /* 24 kbps */
  45. { 7.0f, 4.5f, 3.7f, 3.0f, 2.5f, 2.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=0;
  62. vbr->last_energy=1;
  63. vbr->accum_sum=0;
  64. vbr->energy_alpha=.1;
  65. vbr->soft_pitch=0;
  66. vbr->last_pitch_coef=0;
  67. vbr->last_quality=0;
  68. vbr->noise_accum = .05*pow(MIN_ENERGY, NOISE_POW);
  69. vbr->noise_accum_count=.05;
  70. vbr->noise_level=vbr->noise_accum/vbr->noise_accum_count;
  71. vbr->consec_noise=0;
  72. for (i=0;i<VBR_MEMORY_SIZE;i++)
  73. vbr->last_log_energy[i] = log(MIN_ENERGY);
  74. }
  75. /*
  76. This function should analyse the signal and decide how critical the
  77. coding error will be perceptually. The following factors should be
  78. taken into account:
  79. -Attacks (positive energy derivative) should be coded with more bits
  80. -Stationary voiced segments should receive more bits
  81. -Segments with (very) low absolute energy should receive less bits (maybe
  82. only shaped noise?)
  83. -DTX for near-zero energy?
  84. -Stationary fricative segments should have less bits
  85. -Temporal masking: when energy slope is decreasing, decrease the bit-rate
  86. -Decrease bit-rate for males (low pitch)?
  87. -(wideband only) less bits in the high-band when signal is very
  88. non-stationary (harder to notice high-frequency noise)???
  89. */
  90. float vbr_analysis(VBRState *vbr, spx_word16_t *sig, int len, int pitch, float pitch_coef)
  91. {
  92. int i;
  93. float ener=0, ener1=0, ener2=0;
  94. float qual=7;
  95. int va;
  96. float log_energy;
  97. float non_st=0;
  98. float voicing;
  99. float pow_ener;
  100. for (i=0;i<len>>1;i++)
  101. ener1 += ((float)sig[i])*sig[i];
  102. for (i=len>>1;i<len;i++)
  103. ener2 += ((float)sig[i])*sig[i];
  104. ener=ener1+ener2;
  105. log_energy = log(ener+MIN_ENERGY);
  106. for (i=0;i<VBR_MEMORY_SIZE;i++)
  107. non_st += sqr(log_energy-vbr->last_log_energy[i]);
  108. non_st = non_st/(30*VBR_MEMORY_SIZE);
  109. if (non_st>1)
  110. non_st=1;
  111. voicing = 3*(pitch_coef-.4)*fabs(pitch_coef-.4);
  112. vbr->average_energy = (1-vbr->energy_alpha)*vbr->average_energy + vbr->energy_alpha*ener;
  113. vbr->noise_level=vbr->noise_accum/vbr->noise_accum_count;
  114. pow_ener = pow(ener,NOISE_POW);
  115. if (vbr->noise_accum_count<.06 && ener>MIN_ENERGY)
  116. vbr->noise_accum = .05*pow_ener;
  117. if ((voicing<.3 && non_st < .2 && pow_ener < 1.2*vbr->noise_level)
  118. || (voicing<.3 && non_st < .05 && pow_ener < 1.5*vbr->noise_level)
  119. || (voicing<.4 && non_st < .05 && pow_ener < 1.2*vbr->noise_level)
  120. || (voicing<0 && non_st < .05))
  121. {
  122. float tmp;
  123. va = 0;
  124. vbr->consec_noise++;
  125. if (pow_ener > 3*vbr->noise_level)
  126. tmp = 3*vbr->noise_level;
  127. else
  128. tmp = pow_ener;
  129. if (vbr->consec_noise>=4)
  130. {
  131. vbr->noise_accum = .95*vbr->noise_accum + .05*tmp;
  132. vbr->noise_accum_count = .95*vbr->noise_accum_count + .05;
  133. }
  134. } else {
  135. va = 1;
  136. vbr->consec_noise=0;
  137. }
  138. if (pow_ener < vbr->noise_level && ener>MIN_ENERGY)
  139. {
  140. vbr->noise_accum = .95*vbr->noise_accum + .05*pow_ener;
  141. vbr->noise_accum_count = .95*vbr->noise_accum_count + .05;
  142. }
  143. /* Checking for very low absolute energy */
  144. if (ener < 30000)
  145. {
  146. qual -= .7;
  147. if (ener < 10000)
  148. qual-=.7;
  149. if (ener < 3000)
  150. qual-=.7;
  151. } else {
  152. float short_diff, long_diff;
  153. short_diff = log((ener+1)/(1+vbr->last_energy));
  154. long_diff = log((ener+1)/(1+vbr->average_energy));
  155. /*fprintf (stderr, "%f %f\n", short_diff, long_diff);*/
  156. if (long_diff<-5)
  157. long_diff=-5;
  158. if (long_diff>2)
  159. long_diff=2;
  160. if (long_diff>0)
  161. qual += .6*long_diff;
  162. if (long_diff<0)
  163. qual += .5*long_diff;
  164. if (short_diff>0)
  165. {
  166. if (short_diff>5)
  167. short_diff=5;
  168. qual += .5*short_diff;
  169. }
  170. /* Checking for energy increases */
  171. if (ener2 > 1.6*ener1)
  172. qual += .5;
  173. }
  174. vbr->last_energy = ener;
  175. vbr->soft_pitch = .6*vbr->soft_pitch + .4*pitch_coef;
  176. qual += 2.2*((pitch_coef-.4) + (vbr->soft_pitch-.4));
  177. if (qual < vbr->last_quality)
  178. qual = .5*qual + .5*vbr->last_quality;
  179. if (qual<4)
  180. qual=4;
  181. if (qual>10)
  182. qual=10;
  183. /*
  184. if (vbr->consec_noise>=2)
  185. qual-=1.3;
  186. if (vbr->consec_noise>=5)
  187. qual-=1.3;
  188. if (vbr->consec_noise>=12)
  189. qual-=1.3;
  190. */
  191. if (vbr->consec_noise>=3)
  192. qual=4;
  193. if (vbr->consec_noise)
  194. qual -= 1.0 * (log(3.0 + vbr->consec_noise)-log(3));
  195. if (qual<0)
  196. qual=0;
  197. if (ener<60000)
  198. {
  199. if (vbr->consec_noise>2)
  200. qual-=0.5*(log(3.0 + vbr->consec_noise)-log(3));
  201. if (ener<10000&&vbr->consec_noise>2)
  202. qual-=0.5*(log(3.0 + vbr->consec_noise)-log(3));
  203. if (qual<0)
  204. qual=0;
  205. qual += .3*log(.0001+ener/60000.0);
  206. }
  207. if (qual<-1)
  208. qual=-1;
  209. /*printf ("%f %f %f %f %d\n", qual, voicing, non_st, pow_ener/(.01+vbr->noise_level), va);*/
  210. vbr->last_pitch_coef = pitch_coef;
  211. vbr->last_quality = qual;
  212. for (i=VBR_MEMORY_SIZE-1;i>0;i--)
  213. vbr->last_log_energy[i] = vbr->last_log_energy[i-1];
  214. vbr->last_log_energy[0] = log_energy;
  215. /*printf ("VBR: %f %f %f %d %f\n", (float)(log_energy-log(vbr->average_energy+MIN_ENERGY)), non_st, voicing, va, vbr->noise_level);*/
  216. return qual;
  217. }
  218. void vbr_destroy(VBRState *vbr)
  219. {
  220. }
  221. #endif /* #ifndef DISABLE_VBR */