celt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. int resampling_factor(opus_int32 rate)
  47. {
  48. int ret;
  49. switch (rate)
  50. {
  51. case 48000:
  52. ret = 1;
  53. break;
  54. case 24000:
  55. ret = 2;
  56. break;
  57. case 16000:
  58. ret = 3;
  59. break;
  60. case 12000:
  61. ret = 4;
  62. break;
  63. case 8000:
  64. ret = 6;
  65. break;
  66. default:
  67. #ifndef CUSTOM_MODES
  68. celt_assert(0);
  69. #endif
  70. ret = 0;
  71. break;
  72. }
  73. return ret;
  74. }
  75. void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
  76. opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
  77. const opus_val16 *window, int overlap)
  78. {
  79. int i;
  80. /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
  81. opus_val16 g00, g01, g02, g10, g11, g12;
  82. opus_val32 x0, x1, x2, x3, x4;
  83. static const opus_val16 gains[3][3] = {
  84. {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
  85. {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
  86. {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
  87. if (g0==0 && g1==0)
  88. {
  89. /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
  90. if (x!=y)
  91. OPUS_MOVE(y, x, N);
  92. return;
  93. }
  94. g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
  95. g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
  96. g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
  97. g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
  98. g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
  99. g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
  100. x1 = x[-T1+1];
  101. x2 = x[-T1 ];
  102. x3 = x[-T1-1];
  103. x4 = x[-T1-2];
  104. for (i=0;i<overlap;i++)
  105. {
  106. opus_val16 f;
  107. x0=x[i-T1+2];
  108. f = MULT16_16_Q15(window[i],window[i]);
  109. y[i] = x[i]
  110. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
  111. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),ADD32(x[i-T0+1],x[i-T0-1]))
  112. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),ADD32(x[i-T0+2],x[i-T0-2]))
  113. + MULT16_32_Q15(MULT16_16_Q15(f,g10),x2)
  114. + MULT16_32_Q15(MULT16_16_Q15(f,g11),ADD32(x1,x3))
  115. + MULT16_32_Q15(MULT16_16_Q15(f,g12),ADD32(x0,x4));
  116. x4=x3;
  117. x3=x2;
  118. x2=x1;
  119. x1=x0;
  120. }
  121. if (g1==0)
  122. {
  123. /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
  124. if (x!=y)
  125. OPUS_MOVE(y+overlap, x+overlap, N-overlap);
  126. return;
  127. }
  128. /* OPT: For machines where the movs are costly, unroll by 5 */
  129. for (;i<N;i++)
  130. {
  131. x0=x[i-T1+2];
  132. y[i] = x[i]
  133. + MULT16_32_Q15(g10,x2)
  134. + MULT16_32_Q15(g11,ADD32(x1,x3))
  135. + MULT16_32_Q15(g12,ADD32(x0,x4));
  136. x4=x3;
  137. x3=x2;
  138. x2=x1;
  139. x1=x0;
  140. }
  141. }
  142. const signed char tf_select_table[4][8] = {
  143. {0, -1, 0, -1, 0,-1, 0,-1},
  144. {0, -1, 0, -2, 1, 0, 1,-1},
  145. {0, -2, 0, -3, 2, 0, 1,-1},
  146. {0, -2, 0, -3, 3, 0, 1,-1},
  147. };
  148. void init_caps(const CELTMode *m,int *cap,int LM,int C)
  149. {
  150. int i;
  151. for (i=0;i<m->nbEBands;i++)
  152. {
  153. int N;
  154. N=(m->eBands[i+1]-m->eBands[i])<<LM;
  155. cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
  156. }
  157. }
  158. const char *opus_strerror(int error)
  159. {
  160. static const char * const error_strings[8] = {
  161. "success",
  162. "invalid argument",
  163. "buffer too small",
  164. "internal error",
  165. "corrupted stream",
  166. "request not implemented",
  167. "invalid state",
  168. "memory allocation failed"
  169. };
  170. if (error > 0 || error < -7)
  171. return "unknown error";
  172. else
  173. return error_strings[-error];
  174. }
  175. const char *opus_get_version_string(void)
  176. {
  177. return "libopus " OPUS_VERSION
  178. #ifdef FIXED_POINT
  179. "-fixed"
  180. #endif
  181. #ifdef FUZZING
  182. "-fuzzing"
  183. #endif
  184. ;
  185. }