celt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2010 Xiph.Org Foundation
  3. Copyright (c) 2008 Gregory Maxwell
  4. Written by Jean-Marc Valin and Gregory Maxwell */
  5. /*
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. - Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. - Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  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 COPYRIGHT OWNER
  18. OR 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. #define CELT_C
  30. #include "os_support.h"
  31. #include "mdct.h"
  32. #include <math.h>
  33. #include "celt.h"
  34. #include "pitch.h"
  35. #include "bands.h"
  36. #include "modes.h"
  37. #include "entcode.h"
  38. #include "quant_bands.h"
  39. #include "rate.h"
  40. #include "stack_alloc.h"
  41. #include "mathops.h"
  42. #include "float_cast.h"
  43. #include <stdarg.h>
  44. #include "celt_lpc.h"
  45. #include "vq.h"
  46. #ifndef PACKAGE_VERSION
  47. #define PACKAGE_VERSION "unknown"
  48. #endif
  49. int resampling_factor(opus_int32 rate)
  50. {
  51. int ret;
  52. switch (rate)
  53. {
  54. case 48000:
  55. ret = 1;
  56. break;
  57. case 24000:
  58. ret = 2;
  59. break;
  60. case 16000:
  61. ret = 3;
  62. break;
  63. case 12000:
  64. ret = 4;
  65. break;
  66. case 8000:
  67. ret = 6;
  68. break;
  69. default:
  70. #ifndef CUSTOM_MODES
  71. celt_assert(0);
  72. #endif
  73. ret = 0;
  74. break;
  75. }
  76. return ret;
  77. }
  78. #ifndef OVERRIDE_COMB_FILTER_CONST
  79. static void comb_filter_const(opus_val32 *y, opus_val32 *x, int T, int N,
  80. opus_val16 g10, opus_val16 g11, opus_val16 g12)
  81. {
  82. opus_val32 x0, x1, x2, x3, x4;
  83. int i;
  84. x4 = x[-T-2];
  85. x3 = x[-T-1];
  86. x2 = x[-T];
  87. x1 = x[-T+1];
  88. for (i=0;i<N;i++)
  89. {
  90. x0=x[i-T+2];
  91. y[i] = x[i]
  92. + MULT16_32_Q15(g10,x2)
  93. + MULT16_32_Q15(g11,ADD32(x1,x3))
  94. + MULT16_32_Q15(g12,ADD32(x0,x4));
  95. x4=x3;
  96. x3=x2;
  97. x2=x1;
  98. x1=x0;
  99. }
  100. }
  101. #endif
  102. void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
  103. opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
  104. const opus_val16 *window, int overlap)
  105. {
  106. int i;
  107. /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
  108. opus_val16 g00, g01, g02, g10, g11, g12;
  109. opus_val32 x0, x1, x2, x3, x4;
  110. static const opus_val16 gains[3][3] = {
  111. {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
  112. {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
  113. {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
  114. if (g0==0 && g1==0)
  115. {
  116. /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
  117. if (x!=y)
  118. OPUS_MOVE(y, x, N);
  119. return;
  120. }
  121. g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
  122. g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
  123. g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
  124. g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
  125. g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
  126. g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
  127. x1 = x[-T1+1];
  128. x2 = x[-T1 ];
  129. x3 = x[-T1-1];
  130. x4 = x[-T1-2];
  131. for (i=0;i<overlap;i++)
  132. {
  133. opus_val16 f;
  134. x0=x[i-T1+2];
  135. f = MULT16_16_Q15(window[i],window[i]);
  136. y[i] = x[i]
  137. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
  138. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),ADD32(x[i-T0+1],x[i-T0-1]))
  139. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),ADD32(x[i-T0+2],x[i-T0-2]))
  140. + MULT16_32_Q15(MULT16_16_Q15(f,g10),x2)
  141. + MULT16_32_Q15(MULT16_16_Q15(f,g11),ADD32(x1,x3))
  142. + MULT16_32_Q15(MULT16_16_Q15(f,g12),ADD32(x0,x4));
  143. x4=x3;
  144. x3=x2;
  145. x2=x1;
  146. x1=x0;
  147. }
  148. if (g1==0)
  149. {
  150. /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
  151. if (x!=y)
  152. OPUS_MOVE(y+overlap, x+overlap, N-overlap);
  153. return;
  154. }
  155. /* Compute the part with the constant filter. */
  156. comb_filter_const(y+i, x+i, T1, N-i, g10, g11, g12);
  157. }
  158. const signed char tf_select_table[4][8] = {
  159. {0, -1, 0, -1, 0,-1, 0,-1},
  160. {0, -1, 0, -2, 1, 0, 1,-1},
  161. {0, -2, 0, -3, 2, 0, 1,-1},
  162. {0, -2, 0, -3, 3, 0, 1,-1},
  163. };
  164. void init_caps(const CELTMode *m,int *cap,int LM,int C)
  165. {
  166. int i;
  167. for (i=0;i<m->nbEBands;i++)
  168. {
  169. int N;
  170. N=(m->eBands[i+1]-m->eBands[i])<<LM;
  171. cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
  172. }
  173. }
  174. const char *opus_strerror(int error)
  175. {
  176. static const char * const error_strings[8] = {
  177. "success",
  178. "invalid argument",
  179. "buffer too small",
  180. "internal error",
  181. "corrupted stream",
  182. "request not implemented",
  183. "invalid state",
  184. "memory allocation failed"
  185. };
  186. if (error > 0 || error < -7)
  187. return "unknown error";
  188. else
  189. return error_strings[-error];
  190. }
  191. const char *opus_get_version_string(void)
  192. {
  193. return "libopus " PACKAGE_VERSION
  194. #ifdef FIXED_POINT
  195. "-fixed"
  196. #endif
  197. #ifdef FUZZING
  198. "-fuzzing"
  199. #endif
  200. ;
  201. }