stereo.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* Copyright (C) 2002 Jean-Marc Valin
  2. File: stereo.c
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of the Xiph.org Foundation nor the names of its
  12. contributors may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include <speex/speex_stereo.h>
  30. #include <speex/speex_callbacks.h>
  31. #include "math_approx.h"
  32. #include "vq.h"
  33. #include <math.h>
  34. #include "os_support.h"
  35. typedef struct RealSpeexStereoState {
  36. spx_word32_t balance; /**< Left/right balance info */
  37. spx_word32_t e_ratio; /**< Ratio of energies: E(left+right)/[E(left)+E(right)] */
  38. spx_word32_t smooth_left; /**< Smoothed left channel gain */
  39. spx_word32_t smooth_right; /**< Smoothed right channel gain */
  40. spx_uint32_t reserved1; /**< Reserved for future use */
  41. spx_int32_t reserved2; /**< Reserved for future use */
  42. } RealSpeexStereoState;
  43. /*float e_ratio_quant[4] = {1, 1.26, 1.587, 2};*/
  44. #ifndef FIXED_POINT
  45. static const float e_ratio_quant[4] = {.25f, .315f, .397f, .5f};
  46. static const float e_ratio_quant_bounds[3] = {0.2825f, 0.356f, 0.4485f};
  47. #else
  48. static const spx_word16_t e_ratio_quant[4] = {8192, 10332, 13009, 16384};
  49. static const spx_word16_t e_ratio_quant_bounds[3] = {9257, 11665, 14696};
  50. static const spx_word16_t balance_bounds[31] = {18, 23, 30, 38, 49, 63, 81, 104,
  51. 134, 172, 221, 284, 364, 468, 600, 771,
  52. 990, 1271, 1632, 2096, 2691, 3455, 4436, 5696,
  53. 7314, 9392, 12059, 15484, 19882, 25529, 32766};
  54. #endif
  55. /* This is an ugly compatibility hack that properly resets the stereo state
  56. In case it it compiled in fixed-point, but initialised with the deprecated
  57. floating point static initialiser */
  58. #ifdef FIXED_POINT
  59. #define COMPATIBILITY_HACK(s) do {if ((s)->reserved1 != 0xdeadbeef) speex_stereo_state_reset((SpeexStereoState*)s); } while (0);
  60. #else
  61. #define COMPATIBILITY_HACK(s)
  62. #endif
  63. EXPORT SpeexStereoState *speex_stereo_state_init()
  64. {
  65. SpeexStereoState *stereo = speex_alloc(sizeof(SpeexStereoState));
  66. speex_stereo_state_reset(stereo);
  67. return stereo;
  68. }
  69. EXPORT void speex_stereo_state_reset(SpeexStereoState *_stereo)
  70. {
  71. RealSpeexStereoState *stereo = (RealSpeexStereoState*)_stereo;
  72. #ifdef FIXED_POINT
  73. stereo->balance = 65536;
  74. stereo->e_ratio = 16384;
  75. stereo->smooth_left = 16384;
  76. stereo->smooth_right = 16384;
  77. stereo->reserved1 = 0xdeadbeef;
  78. stereo->reserved2 = 0;
  79. #else
  80. stereo->balance = 1.0f;
  81. stereo->e_ratio = .5f;
  82. stereo->smooth_left = 1.f;
  83. stereo->smooth_right = 1.f;
  84. stereo->reserved1 = 0;
  85. stereo->reserved2 = 0;
  86. #endif
  87. }
  88. EXPORT void speex_stereo_state_destroy(SpeexStereoState *stereo)
  89. {
  90. speex_free(stereo);
  91. }
  92. #ifndef DISABLE_FLOAT_API
  93. EXPORT void speex_encode_stereo(float *data, int frame_size, SpeexBits *bits)
  94. {
  95. int i, tmp;
  96. float e_left=0, e_right=0, e_tot=0;
  97. float balance, e_ratio;
  98. for (i=0;i<frame_size;i++)
  99. {
  100. e_left += ((float)data[2*i])*data[2*i];
  101. e_right += ((float)data[2*i+1])*data[2*i+1];
  102. data[i] = .5*(((float)data[2*i])+data[2*i+1]);
  103. e_tot += ((float)data[i])*data[i];
  104. }
  105. balance=(e_left+1)/(e_right+1);
  106. e_ratio = e_tot/(1+e_left+e_right);
  107. /*Quantization*/
  108. speex_bits_pack(bits, 14, 5);
  109. speex_bits_pack(bits, SPEEX_INBAND_STEREO, 4);
  110. balance=4*log(balance);
  111. /*Pack sign*/
  112. if (balance>0)
  113. speex_bits_pack(bits, 0, 1);
  114. else
  115. speex_bits_pack(bits, 1, 1);
  116. balance=floor(.5+fabs(balance));
  117. if (balance>30)
  118. balance=31;
  119. speex_bits_pack(bits, (int)balance, 5);
  120. /* FIXME: this is a hack */
  121. tmp=scal_quant(e_ratio*Q15_ONE, e_ratio_quant_bounds, 4);
  122. speex_bits_pack(bits, tmp, 2);
  123. }
  124. #endif /* #ifndef DISABLE_FLOAT_API */
  125. EXPORT void speex_encode_stereo_int(spx_int16_t *data, int frame_size, SpeexBits *bits)
  126. {
  127. int i, tmp;
  128. spx_word32_t e_left=0, e_right=0, e_tot=0;
  129. spx_word32_t balance, e_ratio;
  130. spx_word32_t largest, smallest;
  131. int balance_id;
  132. #ifdef FIXED_POINT
  133. int shift;
  134. #endif
  135. /* In band marker */
  136. speex_bits_pack(bits, 14, 5);
  137. /* Stereo marker */
  138. speex_bits_pack(bits, SPEEX_INBAND_STEREO, 4);
  139. for (i=0;i<frame_size;i++)
  140. {
  141. e_left += SHR32(MULT16_16(data[2*i],data[2*i]),8);
  142. e_right += SHR32(MULT16_16(data[2*i+1],data[2*i+1]),8);
  143. #ifdef FIXED_POINT
  144. /* I think this is actually unbiased */
  145. data[i] = SHR16(data[2*i],1)+PSHR16(data[2*i+1],1);
  146. #else
  147. data[i] = .5*(((float)data[2*i])+data[2*i+1]);
  148. #endif
  149. e_tot += SHR32(MULT16_16(data[i],data[i]),8);
  150. }
  151. if (e_left > e_right)
  152. {
  153. speex_bits_pack(bits, 0, 1);
  154. largest = e_left;
  155. smallest = e_right;
  156. } else {
  157. speex_bits_pack(bits, 1, 1);
  158. largest = e_right;
  159. smallest = e_left;
  160. }
  161. /* Balance quantization */
  162. #ifdef FIXED_POINT
  163. shift = spx_ilog2(largest)-15;
  164. largest = VSHR32(largest, shift-4);
  165. smallest = VSHR32(smallest, shift);
  166. balance = DIV32(largest, ADD32(smallest, 1));
  167. if (balance > 32767)
  168. balance = 32767;
  169. balance_id = scal_quant(EXTRACT16(balance), balance_bounds, 32);
  170. #else
  171. balance=(largest+1.)/(smallest+1.);
  172. balance=4*log(balance);
  173. balance_id=floor(.5+fabs(balance));
  174. if (balance_id>30)
  175. balance_id=31;
  176. #endif
  177. speex_bits_pack(bits, balance_id, 5);
  178. /* "coherence" quantisation */
  179. #ifdef FIXED_POINT
  180. shift = spx_ilog2(e_tot);
  181. e_tot = VSHR32(e_tot, shift-25);
  182. e_left = VSHR32(e_left, shift-10);
  183. e_right = VSHR32(e_right, shift-10);
  184. e_ratio = DIV32(e_tot, e_left+e_right+1);
  185. #else
  186. e_ratio = e_tot/(1.+e_left+e_right);
  187. #endif
  188. tmp=scal_quant(EXTRACT16(e_ratio), e_ratio_quant_bounds, 4);
  189. /*fprintf (stderr, "%d %d %d %d\n", largest, smallest, balance_id, e_ratio);*/
  190. speex_bits_pack(bits, tmp, 2);
  191. }
  192. #ifndef DISABLE_FLOAT_API
  193. EXPORT void speex_decode_stereo(float *data, int frame_size, SpeexStereoState *_stereo)
  194. {
  195. int i;
  196. spx_word32_t balance;
  197. spx_word16_t e_left, e_right, e_ratio;
  198. RealSpeexStereoState *stereo = (RealSpeexStereoState*)_stereo;
  199. COMPATIBILITY_HACK(stereo);
  200. balance=stereo->balance;
  201. e_ratio=stereo->e_ratio;
  202. /* These two are Q14, with max value just below 2. */
  203. e_right = DIV32(QCONST32(1., 22), spx_sqrt(MULT16_32_Q15(e_ratio, ADD32(QCONST32(1., 16), balance))));
  204. e_left = SHR32(MULT16_16(spx_sqrt(balance), e_right), 8);
  205. for (i=frame_size-1;i>=0;i--)
  206. {
  207. spx_word16_t tmp=data[i];
  208. stereo->smooth_left = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_left, QCONST16(0.98, 15)), e_left, QCONST16(0.02, 15)), 15));
  209. stereo->smooth_right = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_right, QCONST16(0.98, 15)), e_right, QCONST16(0.02, 15)), 15));
  210. data[2*i] = (float)MULT16_16_P14(stereo->smooth_left, tmp);
  211. data[2*i+1] = (float)MULT16_16_P14(stereo->smooth_right, tmp);
  212. }
  213. }
  214. #endif /* #ifndef DISABLE_FLOAT_API */
  215. EXPORT void speex_decode_stereo_int(spx_int16_t *data, int frame_size, SpeexStereoState *_stereo)
  216. {
  217. int i;
  218. spx_word32_t balance;
  219. spx_word16_t e_left, e_right, e_ratio;
  220. RealSpeexStereoState *stereo = (RealSpeexStereoState*)_stereo;
  221. COMPATIBILITY_HACK(stereo);
  222. balance=stereo->balance;
  223. e_ratio=stereo->e_ratio;
  224. /* These two are Q14, with max value just below 2. */
  225. e_right = DIV32(QCONST32(1., 22), spx_sqrt(MULT16_32_Q15(e_ratio, ADD32(QCONST32(1., 16), balance))));
  226. e_left = SHR32(MULT16_16(spx_sqrt(balance), e_right), 8);
  227. for (i=frame_size-1;i>=0;i--)
  228. {
  229. spx_int16_t tmp=data[i];
  230. stereo->smooth_left = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_left, QCONST16(0.98, 15)), e_left, QCONST16(0.02, 15)), 15));
  231. stereo->smooth_right = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_right, QCONST16(0.98, 15)), e_right, QCONST16(0.02, 15)), 15));
  232. data[2*i] = (spx_int16_t)MULT16_16_P14(stereo->smooth_left, tmp);
  233. data[2*i+1] = (spx_int16_t)MULT16_16_P14(stereo->smooth_right, tmp);
  234. }
  235. }
  236. EXPORT int speex_std_stereo_request_handler(SpeexBits *bits, void *state, void *data)
  237. {
  238. RealSpeexStereoState *stereo;
  239. spx_word16_t sign=1, dexp;
  240. int tmp;
  241. stereo = (RealSpeexStereoState*)data;
  242. COMPATIBILITY_HACK(stereo);
  243. if (speex_bits_unpack_unsigned(bits, 1))
  244. sign=-1;
  245. dexp = speex_bits_unpack_unsigned(bits, 5);
  246. #ifndef FIXED_POINT
  247. stereo->balance = exp(sign*.25*dexp);
  248. #else
  249. stereo->balance = spx_exp(MULT16_16(sign, SHL16(dexp, 9)));
  250. #endif
  251. tmp = speex_bits_unpack_unsigned(bits, 2);
  252. stereo->e_ratio = e_ratio_quant[tmp];
  253. return 0;
  254. }