stereo.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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_ENCODER
  93. #ifndef DISABLE_FLOAT_API
  94. EXPORT void speex_encode_stereo(float *data, int frame_size, SpeexBits *bits)
  95. {
  96. int i, tmp;
  97. float e_left=0, e_right=0, e_tot=0;
  98. float balance, e_ratio;
  99. for (i=0;i<frame_size;i++)
  100. {
  101. e_left += ((float)data[2*i])*data[2*i];
  102. e_right += ((float)data[2*i+1])*data[2*i+1];
  103. data[i] = .5*(((float)data[2*i])+data[2*i+1]);
  104. e_tot += ((float)data[i])*data[i];
  105. }
  106. balance=(e_left+1)/(e_right+1);
  107. e_ratio = e_tot/(1+e_left+e_right);
  108. /*Quantization*/
  109. speex_bits_pack(bits, 14, 5);
  110. speex_bits_pack(bits, SPEEX_INBAND_STEREO, 4);
  111. balance=4*log(balance);
  112. /*Pack sign*/
  113. if (balance>0)
  114. speex_bits_pack(bits, 0, 1);
  115. else
  116. speex_bits_pack(bits, 1, 1);
  117. balance=floor(.5+fabs(balance));
  118. if (balance>30)
  119. balance=31;
  120. speex_bits_pack(bits, (int)balance, 5);
  121. /* FIXME: this is a hack */
  122. tmp=scal_quant(e_ratio*Q15_ONE, e_ratio_quant_bounds, 4);
  123. speex_bits_pack(bits, tmp, 2);
  124. }
  125. #endif /* #ifndef DISABLE_FLOAT_API */
  126. EXPORT void speex_encode_stereo_int(spx_int16_t *data, int frame_size, SpeexBits *bits)
  127. {
  128. int i, tmp;
  129. spx_word32_t e_left=0, e_right=0, e_tot=0;
  130. spx_word32_t balance, e_ratio;
  131. spx_word32_t largest, smallest;
  132. int balance_id;
  133. #ifdef FIXED_POINT
  134. int shift;
  135. #endif
  136. /* In band marker */
  137. speex_bits_pack(bits, 14, 5);
  138. /* Stereo marker */
  139. speex_bits_pack(bits, SPEEX_INBAND_STEREO, 4);
  140. for (i=0;i<frame_size;i++)
  141. {
  142. e_left += SHR32(MULT16_16(data[2*i],data[2*i]),8);
  143. e_right += SHR32(MULT16_16(data[2*i+1],data[2*i+1]),8);
  144. #ifdef FIXED_POINT
  145. /* I think this is actually unbiased */
  146. data[i] = SHR16(data[2*i],1)+PSHR16(data[2*i+1],1);
  147. #else
  148. data[i] = .5*(((float)data[2*i])+data[2*i+1]);
  149. #endif
  150. e_tot += SHR32(MULT16_16(data[i],data[i]),8);
  151. }
  152. if (e_left > e_right)
  153. {
  154. speex_bits_pack(bits, 0, 1);
  155. largest = e_left;
  156. smallest = e_right;
  157. } else {
  158. speex_bits_pack(bits, 1, 1);
  159. largest = e_right;
  160. smallest = e_left;
  161. }
  162. /* Balance quantization */
  163. #ifdef FIXED_POINT
  164. shift = spx_ilog2(largest)-15;
  165. largest = VSHR32(largest, shift-4);
  166. smallest = VSHR32(smallest, shift);
  167. balance = DIV32(largest, ADD32(smallest, 1));
  168. if (balance > 32767)
  169. balance = 32767;
  170. balance_id = scal_quant(EXTRACT16(balance), balance_bounds, 32);
  171. #else
  172. balance=(largest+1.)/(smallest+1.);
  173. balance=4*log(balance);
  174. balance_id=floor(.5+fabs(balance));
  175. if (balance_id>30)
  176. balance_id=31;
  177. #endif
  178. speex_bits_pack(bits, balance_id, 5);
  179. /* "coherence" quantisation */
  180. #ifdef FIXED_POINT
  181. shift = spx_ilog2(e_tot);
  182. e_tot = VSHR32(e_tot, shift-25);
  183. e_left = VSHR32(e_left, shift-10);
  184. e_right = VSHR32(e_right, shift-10);
  185. e_ratio = DIV32(e_tot, e_left+e_right+1);
  186. #else
  187. e_ratio = e_tot/(1.+e_left+e_right);
  188. #endif
  189. tmp=scal_quant(EXTRACT16(e_ratio), e_ratio_quant_bounds, 4);
  190. /*fprintf (stderr, "%d %d %d %d\n", largest, smallest, balance_id, e_ratio);*/
  191. speex_bits_pack(bits, tmp, 2);
  192. }
  193. #else /* DISABLE_ENCODER */
  194. EXPORT void speex_encode_stereo_int(spx_int16_t *data, int frame_size, SpeexBits *bits)
  195. {
  196. }
  197. #endif /* DISABLE_ENCODER */
  198. #ifndef DISABLE_FLOAT_API
  199. EXPORT void speex_decode_stereo(float *data, int frame_size, SpeexStereoState *_stereo)
  200. {
  201. int i;
  202. spx_word32_t balance;
  203. spx_word16_t e_left, e_right, e_ratio;
  204. RealSpeexStereoState *stereo = (RealSpeexStereoState*)_stereo;
  205. COMPATIBILITY_HACK(stereo);
  206. balance=stereo->balance;
  207. e_ratio=stereo->e_ratio;
  208. /* These two are Q14, with max value just below 2. */
  209. e_right = DIV32(QCONST32(1., 22), spx_sqrt(MULT16_32_Q15(e_ratio, ADD32(QCONST32(1., 16), balance))));
  210. e_left = SHR32(MULT16_16(spx_sqrt(balance), e_right), 8);
  211. for (i=frame_size-1;i>=0;i--)
  212. {
  213. spx_word16_t tmp=data[i];
  214. stereo->smooth_left = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_left, QCONST16(0.98, 15)), e_left, QCONST16(0.02, 15)), 15));
  215. stereo->smooth_right = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_right, QCONST16(0.98, 15)), e_right, QCONST16(0.02, 15)), 15));
  216. data[2*i] = (float)MULT16_16_P14(stereo->smooth_left, tmp);
  217. data[2*i+1] = (float)MULT16_16_P14(stereo->smooth_right, tmp);
  218. }
  219. }
  220. #endif /* #ifndef DISABLE_FLOAT_API */
  221. EXPORT void speex_decode_stereo_int(spx_int16_t *data, int frame_size, SpeexStereoState *_stereo)
  222. {
  223. int i;
  224. spx_word32_t balance;
  225. spx_word16_t e_left, e_right, e_ratio;
  226. RealSpeexStereoState *stereo = (RealSpeexStereoState*)_stereo;
  227. COMPATIBILITY_HACK(stereo);
  228. balance=stereo->balance;
  229. e_ratio=stereo->e_ratio;
  230. /* These two are Q14, with max value just below 2. */
  231. e_right = DIV32(QCONST32(1., 22), spx_sqrt(MULT16_32_Q15(e_ratio, ADD32(QCONST32(1., 16), balance))));
  232. e_left = SHR32(MULT16_16(spx_sqrt(balance), e_right), 8);
  233. for (i=frame_size-1;i>=0;i--)
  234. {
  235. spx_int16_t tmp=data[i];
  236. stereo->smooth_left = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_left, QCONST16(0.98, 15)), e_left, QCONST16(0.02, 15)), 15));
  237. stereo->smooth_right = EXTRACT16(PSHR32(MAC16_16(MULT16_16(stereo->smooth_right, QCONST16(0.98, 15)), e_right, QCONST16(0.02, 15)), 15));
  238. data[2*i] = (spx_int16_t)MULT16_16_P14(stereo->smooth_left, tmp);
  239. data[2*i+1] = (spx_int16_t)MULT16_16_P14(stereo->smooth_right, tmp);
  240. }
  241. }
  242. EXPORT int speex_std_stereo_request_handler(SpeexBits *bits, void *state, void *data)
  243. {
  244. RealSpeexStereoState *stereo;
  245. spx_word16_t sign=1, dexp;
  246. int tmp;
  247. stereo = (RealSpeexStereoState*)data;
  248. COMPATIBILITY_HACK(stereo);
  249. if (speex_bits_unpack_unsigned(bits, 1))
  250. sign=-1;
  251. dexp = speex_bits_unpack_unsigned(bits, 5);
  252. #ifndef FIXED_POINT
  253. stereo->balance = exp(sign*.25*dexp);
  254. #else
  255. stereo->balance = spx_exp(MULT16_16(sign, SHL16(dexp, 9)));
  256. #endif
  257. tmp = speex_bits_unpack_unsigned(bits, 2);
  258. stereo->e_ratio = e_ratio_quant[tmp];
  259. return 0;
  260. }