celt.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. t = SATURATE(t, SIG_SAT);
  104. y[i] = t;
  105. x4=SHL32(x[i-T+3],1);
  106. t = MAC16_32_Q16(x[i+1], g10, x1);
  107. t = MAC16_32_Q16(t, g11, ADD32(x0,x2));
  108. t = MAC16_32_Q16(t, g12, ADD32(x4,x3));
  109. t = SATURATE(t, SIG_SAT);
  110. y[i+1] = t;
  111. x3=SHL32(x[i-T+4],1);
  112. t = MAC16_32_Q16(x[i+2], g10, x0);
  113. t = MAC16_32_Q16(t, g11, ADD32(x4,x1));
  114. t = MAC16_32_Q16(t, g12, ADD32(x3,x2));
  115. t = SATURATE(t, SIG_SAT);
  116. y[i+2] = t;
  117. x2=SHL32(x[i-T+5],1);
  118. t = MAC16_32_Q16(x[i+3], g10, x4);
  119. t = MAC16_32_Q16(t, g11, ADD32(x3,x0));
  120. t = MAC16_32_Q16(t, g12, ADD32(x2,x1));
  121. t = SATURATE(t, SIG_SAT);
  122. y[i+3] = t;
  123. x1=SHL32(x[i-T+6],1);
  124. t = MAC16_32_Q16(x[i+4], g10, x3);
  125. t = MAC16_32_Q16(t, g11, ADD32(x2,x4));
  126. t = MAC16_32_Q16(t, g12, ADD32(x1,x0));
  127. t = SATURATE(t, SIG_SAT);
  128. y[i+4] = t;
  129. }
  130. #ifdef CUSTOM_MODES
  131. for (;i<N;i++)
  132. {
  133. opus_val32 t;
  134. x0=SHL32(x[i-T+2],1);
  135. t = MAC16_32_Q16(x[i], g10, x2);
  136. t = MAC16_32_Q16(t, g11, ADD32(x1,x3));
  137. t = MAC16_32_Q16(t, g12, ADD32(x0,x4));
  138. t = SATURATE(t, SIG_SAT);
  139. y[i] = t;
  140. x4=x3;
  141. x3=x2;
  142. x2=x1;
  143. x1=x0;
  144. }
  145. #endif
  146. }
  147. #else
  148. #ifndef NON_STATIC_COMB_FILTER_CONST_C
  149. static
  150. #endif
  151. void comb_filter_const_c(opus_val32 *y, opus_val32 *x, int T, int N,
  152. opus_val16 g10, opus_val16 g11, opus_val16 g12)
  153. {
  154. opus_val32 x0, x1, x2, x3, x4;
  155. int i;
  156. x4 = x[-T-2];
  157. x3 = x[-T-1];
  158. x2 = x[-T];
  159. x1 = x[-T+1];
  160. for (i=0;i<N;i++)
  161. {
  162. x0=x[i-T+2];
  163. y[i] = x[i]
  164. + MULT16_32_Q15(g10,x2)
  165. + MULT16_32_Q15(g11,ADD32(x1,x3))
  166. + MULT16_32_Q15(g12,ADD32(x0,x4));
  167. y[i] = SATURATE(y[i], SIG_SAT);
  168. x4=x3;
  169. x3=x2;
  170. x2=x1;
  171. x1=x0;
  172. }
  173. }
  174. #endif
  175. #endif
  176. #ifndef OVERRIDE_comb_filter
  177. void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
  178. opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
  179. const opus_val16 *window, int overlap, int arch)
  180. {
  181. int i;
  182. /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
  183. opus_val16 g00, g01, g02, g10, g11, g12;
  184. opus_val32 x0, x1, x2, x3, x4;
  185. static const opus_val16 gains[3][3] = {
  186. {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
  187. {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
  188. {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
  189. if (g0==0 && g1==0)
  190. {
  191. /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
  192. if (x!=y)
  193. OPUS_MOVE(y, x, N);
  194. return;
  195. }
  196. /* When the gain is zero, T0 and/or T1 is set to zero. We need
  197. to have then be at least 2 to avoid processing garbage data. */
  198. T0 = IMAX(T0, COMBFILTER_MINPERIOD);
  199. T1 = IMAX(T1, COMBFILTER_MINPERIOD);
  200. g00 = MULT16_16_P15(g0, gains[tapset0][0]);
  201. g01 = MULT16_16_P15(g0, gains[tapset0][1]);
  202. g02 = MULT16_16_P15(g0, gains[tapset0][2]);
  203. g10 = MULT16_16_P15(g1, gains[tapset1][0]);
  204. g11 = MULT16_16_P15(g1, gains[tapset1][1]);
  205. g12 = MULT16_16_P15(g1, gains[tapset1][2]);
  206. x1 = x[-T1+1];
  207. x2 = x[-T1 ];
  208. x3 = x[-T1-1];
  209. x4 = x[-T1-2];
  210. /* If the filter didn't change, we don't need the overlap */
  211. if (g0==g1 && T0==T1 && tapset0==tapset1)
  212. overlap=0;
  213. for (i=0;i<overlap;i++)
  214. {
  215. opus_val16 f;
  216. x0=x[i-T1+2];
  217. f = MULT16_16_Q15(window[i],window[i]);
  218. y[i] = x[i]
  219. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
  220. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),ADD32(x[i-T0+1],x[i-T0-1]))
  221. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),ADD32(x[i-T0+2],x[i-T0-2]))
  222. + MULT16_32_Q15(MULT16_16_Q15(f,g10),x2)
  223. + MULT16_32_Q15(MULT16_16_Q15(f,g11),ADD32(x1,x3))
  224. + MULT16_32_Q15(MULT16_16_Q15(f,g12),ADD32(x0,x4));
  225. y[i] = SATURATE(y[i], SIG_SAT);
  226. x4=x3;
  227. x3=x2;
  228. x2=x1;
  229. x1=x0;
  230. }
  231. if (g1==0)
  232. {
  233. /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
  234. if (x!=y)
  235. OPUS_MOVE(y+overlap, x+overlap, N-overlap);
  236. return;
  237. }
  238. /* Compute the part with the constant filter. */
  239. comb_filter_const(y+i, x+i, T1, N-i, g10, g11, g12, arch);
  240. }
  241. #endif /* OVERRIDE_comb_filter */
  242. /* TF change table. Positive values mean better frequency resolution (longer
  243. effective window), whereas negative values mean better time resolution
  244. (shorter effective window). The second index is computed as:
  245. 4*isTransient + 2*tf_select + per_band_flag */
  246. const signed char tf_select_table[4][8] = {
  247. /*isTransient=0 isTransient=1 */
  248. {0, -1, 0, -1, 0,-1, 0,-1}, /* 2.5 ms */
  249. {0, -1, 0, -2, 1, 0, 1,-1}, /* 5 ms */
  250. {0, -2, 0, -3, 2, 0, 1,-1}, /* 10 ms */
  251. {0, -2, 0, -3, 3, 0, 1,-1}, /* 20 ms */
  252. };
  253. void init_caps(const CELTMode *m,int *cap,int LM,int C)
  254. {
  255. int i;
  256. for (i=0;i<m->nbEBands;i++)
  257. {
  258. int N;
  259. N=(m->eBands[i+1]-m->eBands[i])<<LM;
  260. cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
  261. }
  262. }
  263. const char *opus_strerror(int error)
  264. {
  265. static const char * const error_strings[8] = {
  266. "success",
  267. "invalid argument",
  268. "buffer too small",
  269. "internal error",
  270. "corrupted stream",
  271. "request not implemented",
  272. "invalid state",
  273. "memory allocation failed"
  274. };
  275. if (error > 0 || error < -7)
  276. return "unknown error";
  277. else
  278. return error_strings[-error];
  279. }
  280. const char *opus_get_version_string(void)
  281. {
  282. return "libopus " PACKAGE_VERSION
  283. /* Applications may rely on the presence of this substring in the version
  284. string to determine if they have a fixed-point or floating-point build
  285. at runtime. */
  286. #ifdef FIXED_POINT
  287. "-fixed"
  288. #endif
  289. #ifdef FUZZING
  290. "-fuzzing"
  291. #endif
  292. ;
  293. }