celt.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #if defined(MIPSr1_ASM)
  50. #include "mips/celt_mipsr1.h"
  51. #endif
  52. int resampling_factor(opus_int32 rate)
  53. {
  54. int ret;
  55. switch (rate)
  56. {
  57. case 48000:
  58. ret = 1;
  59. break;
  60. case 24000:
  61. ret = 2;
  62. break;
  63. case 16000:
  64. ret = 3;
  65. break;
  66. case 12000:
  67. ret = 4;
  68. break;
  69. case 8000:
  70. ret = 6;
  71. break;
  72. default:
  73. #ifndef CUSTOM_MODES
  74. celt_assert(0);
  75. #endif
  76. ret = 0;
  77. break;
  78. }
  79. return ret;
  80. }
  81. #if !defined(OVERRIDE_COMB_FILTER_CONST) || defined(NON_STATIC_COMB_FILTER_CONST_C)
  82. /* This version should be faster on ARM */
  83. #ifdef OPUS_ARM_ASM
  84. #ifndef NON_STATIC_COMB_FILTER_CONST_C
  85. static
  86. #endif
  87. void comb_filter_const_c(opus_val32 *y, opus_val32 *x, int T, int N,
  88. opus_val16 g10, opus_val16 g11, opus_val16 g12)
  89. {
  90. opus_val32 x0, x1, x2, x3, x4;
  91. int i;
  92. x4 = SHL32(x[-T-2], 1);
  93. x3 = SHL32(x[-T-1], 1);
  94. x2 = SHL32(x[-T], 1);
  95. x1 = SHL32(x[-T+1], 1);
  96. for (i=0;i<N-4;i+=5)
  97. {
  98. opus_val32 t;
  99. x0=SHL32(x[i-T+2],1);
  100. t = MAC16_32_Q16(x[i], g10, x2);
  101. t = MAC16_32_Q16(t, g11, ADD32(x1,x3));
  102. t = MAC16_32_Q16(t, g12, ADD32(x0,x4));
  103. y[i] = t;
  104. x4=SHL32(x[i-T+3],1);
  105. t = MAC16_32_Q16(x[i+1], g10, x1);
  106. t = MAC16_32_Q16(t, g11, ADD32(x0,x2));
  107. t = MAC16_32_Q16(t, g12, ADD32(x4,x3));
  108. y[i+1] = t;
  109. x3=SHL32(x[i-T+4],1);
  110. t = MAC16_32_Q16(x[i+2], g10, x0);
  111. t = MAC16_32_Q16(t, g11, ADD32(x4,x1));
  112. t = MAC16_32_Q16(t, g12, ADD32(x3,x2));
  113. y[i+2] = t;
  114. x2=SHL32(x[i-T+5],1);
  115. t = MAC16_32_Q16(x[i+3], g10, x4);
  116. t = MAC16_32_Q16(t, g11, ADD32(x3,x0));
  117. t = MAC16_32_Q16(t, g12, ADD32(x2,x1));
  118. y[i+3] = t;
  119. x1=SHL32(x[i-T+6],1);
  120. t = MAC16_32_Q16(x[i+4], g10, x3);
  121. t = MAC16_32_Q16(t, g11, ADD32(x2,x4));
  122. t = MAC16_32_Q16(t, g12, ADD32(x1,x0));
  123. y[i+4] = t;
  124. }
  125. #ifdef CUSTOM_MODES
  126. for (;i<N;i++)
  127. {
  128. opus_val32 t;
  129. x0=SHL32(x[i-T+2],1);
  130. t = MAC16_32_Q16(x[i], g10, x2);
  131. t = MAC16_32_Q16(t, g11, ADD32(x1,x3));
  132. t = MAC16_32_Q16(t, g12, ADD32(x0,x4));
  133. y[i] = t;
  134. x4=x3;
  135. x3=x2;
  136. x2=x1;
  137. x1=x0;
  138. }
  139. #endif
  140. }
  141. #else
  142. #ifndef NON_STATIC_COMB_FILTER_CONST_C
  143. static
  144. #endif
  145. void comb_filter_const_c(opus_val32 *y, opus_val32 *x, int T, int N,
  146. opus_val16 g10, opus_val16 g11, opus_val16 g12)
  147. {
  148. opus_val32 x0, x1, x2, x3, x4;
  149. int i;
  150. x4 = x[-T-2];
  151. x3 = x[-T-1];
  152. x2 = x[-T];
  153. x1 = x[-T+1];
  154. for (i=0;i<N;i++)
  155. {
  156. x0=x[i-T+2];
  157. y[i] = x[i]
  158. + MULT16_32_Q15(g10,x2)
  159. + MULT16_32_Q15(g11,ADD32(x1,x3))
  160. + MULT16_32_Q15(g12,ADD32(x0,x4));
  161. x4=x3;
  162. x3=x2;
  163. x2=x1;
  164. x1=x0;
  165. }
  166. }
  167. #endif
  168. #endif
  169. #ifndef OVERRIDE_comb_filter
  170. void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
  171. opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
  172. const opus_val16 *window, int overlap, int arch)
  173. {
  174. int i;
  175. /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
  176. opus_val16 g00, g01, g02, g10, g11, g12;
  177. opus_val32 x0, x1, x2, x3, x4;
  178. static const opus_val16 gains[3][3] = {
  179. {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
  180. {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
  181. {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
  182. if (g0==0 && g1==0)
  183. {
  184. /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
  185. if (x!=y)
  186. OPUS_MOVE(y, x, N);
  187. return;
  188. }
  189. g00 = MULT16_16_P15(g0, gains[tapset0][0]);
  190. g01 = MULT16_16_P15(g0, gains[tapset0][1]);
  191. g02 = MULT16_16_P15(g0, gains[tapset0][2]);
  192. g10 = MULT16_16_P15(g1, gains[tapset1][0]);
  193. g11 = MULT16_16_P15(g1, gains[tapset1][1]);
  194. g12 = MULT16_16_P15(g1, gains[tapset1][2]);
  195. x1 = x[-T1+1];
  196. x2 = x[-T1 ];
  197. x3 = x[-T1-1];
  198. x4 = x[-T1-2];
  199. /* If the filter didn't change, we don't need the overlap */
  200. if (g0==g1 && T0==T1 && tapset0==tapset1)
  201. overlap=0;
  202. for (i=0;i<overlap;i++)
  203. {
  204. opus_val16 f;
  205. x0=x[i-T1+2];
  206. f = MULT16_16_Q15(window[i],window[i]);
  207. y[i] = x[i]
  208. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
  209. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),ADD32(x[i-T0+1],x[i-T0-1]))
  210. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),ADD32(x[i-T0+2],x[i-T0-2]))
  211. + MULT16_32_Q15(MULT16_16_Q15(f,g10),x2)
  212. + MULT16_32_Q15(MULT16_16_Q15(f,g11),ADD32(x1,x3))
  213. + MULT16_32_Q15(MULT16_16_Q15(f,g12),ADD32(x0,x4));
  214. x4=x3;
  215. x3=x2;
  216. x2=x1;
  217. x1=x0;
  218. }
  219. if (g1==0)
  220. {
  221. /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
  222. if (x!=y)
  223. OPUS_MOVE(y+overlap, x+overlap, N-overlap);
  224. return;
  225. }
  226. /* Compute the part with the constant filter. */
  227. comb_filter_const(y+i, x+i, T1, N-i, g10, g11, g12, arch);
  228. }
  229. #endif /* OVERRIDE_comb_filter */
  230. const signed char tf_select_table[4][8] = {
  231. {0, -1, 0, -1, 0,-1, 0,-1},
  232. {0, -1, 0, -2, 1, 0, 1,-1},
  233. {0, -2, 0, -3, 2, 0, 1,-1},
  234. {0, -2, 0, -3, 3, 0, 1,-1},
  235. };
  236. void init_caps(const CELTMode *m,int *cap,int LM,int C)
  237. {
  238. int i;
  239. for (i=0;i<m->nbEBands;i++)
  240. {
  241. int N;
  242. N=(m->eBands[i+1]-m->eBands[i])<<LM;
  243. cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
  244. }
  245. }
  246. const char *opus_strerror(int error)
  247. {
  248. static const char * const error_strings[8] = {
  249. "success",
  250. "invalid argument",
  251. "buffer too small",
  252. "internal error",
  253. "corrupted stream",
  254. "request not implemented",
  255. "invalid state",
  256. "memory allocation failed"
  257. };
  258. if (error > 0 || error < -7)
  259. return "unknown error";
  260. else
  261. return error_strings[-error];
  262. }
  263. const char *opus_get_version_string(void)
  264. {
  265. return "libopus " PACKAGE_VERSION
  266. /* Applications may rely on the presence of this substring in the version
  267. string to determine if they have a fixed-point or floating-point build
  268. at runtime. */
  269. #ifdef FIXED_POINT
  270. "-fixed"
  271. #endif
  272. #ifdef FUZZING
  273. "-fuzzing"
  274. #endif
  275. ;
  276. }