vq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2009 Xiph.Org Foundation
  3. Written by Jean-Marc Valin */
  4. /*
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  17. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include "mathops.h"
  29. #include "cwrs.h"
  30. #include "vq.h"
  31. #include "arch.h"
  32. #include "os_support.h"
  33. #include "bands.h"
  34. #include "rate.h"
  35. #include "pitch.h"
  36. #if defined(MIPSr1_ASM)
  37. #include "mips/vq_mipsr1.h"
  38. #endif
  39. #ifndef OVERRIDE_vq_exp_rotation1
  40. static void exp_rotation1(celt_norm *X, int len, int stride, opus_val16 c, opus_val16 s)
  41. {
  42. int i;
  43. opus_val16 ms;
  44. celt_norm *Xptr;
  45. Xptr = X;
  46. ms = NEG16(s);
  47. for (i=0;i<len-stride;i++)
  48. {
  49. celt_norm x1, x2;
  50. x1 = Xptr[0];
  51. x2 = Xptr[stride];
  52. Xptr[stride] = EXTRACT16(PSHR32(MAC16_16(MULT16_16(c, x2), s, x1), 15));
  53. *Xptr++ = EXTRACT16(PSHR32(MAC16_16(MULT16_16(c, x1), ms, x2), 15));
  54. }
  55. Xptr = &X[len-2*stride-1];
  56. for (i=len-2*stride-1;i>=0;i--)
  57. {
  58. celt_norm x1, x2;
  59. x1 = Xptr[0];
  60. x2 = Xptr[stride];
  61. Xptr[stride] = EXTRACT16(PSHR32(MAC16_16(MULT16_16(c, x2), s, x1), 15));
  62. *Xptr-- = EXTRACT16(PSHR32(MAC16_16(MULT16_16(c, x1), ms, x2), 15));
  63. }
  64. }
  65. #endif /* OVERRIDE_vq_exp_rotation1 */
  66. void exp_rotation(celt_norm *X, int len, int dir, int stride, int K, int spread)
  67. {
  68. static const int SPREAD_FACTOR[3]={15,10,5};
  69. int i;
  70. opus_val16 c, s;
  71. opus_val16 gain, theta;
  72. int stride2=0;
  73. int factor;
  74. if (2*K>=len || spread==SPREAD_NONE)
  75. return;
  76. factor = SPREAD_FACTOR[spread-1];
  77. gain = celt_div((opus_val32)MULT16_16(Q15_ONE,len),(opus_val32)(len+factor*K));
  78. theta = HALF16(MULT16_16_Q15(gain,gain));
  79. c = celt_cos_norm(EXTEND32(theta));
  80. s = celt_cos_norm(EXTEND32(SUB16(Q15ONE,theta))); /* sin(theta) */
  81. if (len>=8*stride)
  82. {
  83. stride2 = 1;
  84. /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding.
  85. It's basically incrementing long as (stride2+0.5)^2 < len/stride. */
  86. while ((stride2*stride2+stride2)*stride + (stride>>2) < len)
  87. stride2++;
  88. }
  89. /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
  90. extract_collapse_mask().*/
  91. len = celt_udiv(len, stride);
  92. for (i=0;i<stride;i++)
  93. {
  94. if (dir < 0)
  95. {
  96. if (stride2)
  97. exp_rotation1(X+i*len, len, stride2, s, c);
  98. exp_rotation1(X+i*len, len, 1, c, s);
  99. } else {
  100. exp_rotation1(X+i*len, len, 1, c, -s);
  101. if (stride2)
  102. exp_rotation1(X+i*len, len, stride2, s, -c);
  103. }
  104. }
  105. }
  106. /** Takes the pitch vector and the decoded residual vector, computes the gain
  107. that will give ||p+g*y||=1 and mixes the residual with the pitch. */
  108. static void normalise_residual(int * OPUS_RESTRICT iy, celt_norm * OPUS_RESTRICT X,
  109. int N, opus_val32 Ryy, opus_val16 gain)
  110. {
  111. int i;
  112. #ifdef FIXED_POINT
  113. int k;
  114. #endif
  115. opus_val32 t;
  116. opus_val16 g;
  117. #ifdef FIXED_POINT
  118. k = celt_ilog2(Ryy)>>1;
  119. #endif
  120. t = VSHR32(Ryy, 2*(k-7));
  121. g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
  122. i=0;
  123. do
  124. X[i] = EXTRACT16(PSHR32(MULT16_16(g, iy[i]), k+1));
  125. while (++i < N);
  126. }
  127. static unsigned extract_collapse_mask(int *iy, int N, int B)
  128. {
  129. unsigned collapse_mask;
  130. int N0;
  131. int i;
  132. if (B<=1)
  133. return 1;
  134. /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
  135. exp_rotation().*/
  136. N0 = celt_udiv(N, B);
  137. collapse_mask = 0;
  138. i=0; do {
  139. int j;
  140. unsigned tmp=0;
  141. j=0; do {
  142. tmp |= iy[i*N0+j];
  143. } while (++j<N0);
  144. collapse_mask |= (tmp!=0)<<i;
  145. } while (++i<B);
  146. return collapse_mask;
  147. }
  148. opus_val16 op_pvq_search_c(celt_norm *X, int *iy, int K, int N, int arch)
  149. {
  150. VARDECL(celt_norm, y);
  151. VARDECL(int, signx);
  152. int i, j;
  153. int pulsesLeft;
  154. opus_val32 sum;
  155. opus_val32 xy;
  156. opus_val16 yy;
  157. SAVE_STACK;
  158. (void)arch;
  159. ALLOC(y, N, celt_norm);
  160. ALLOC(signx, N, int);
  161. /* Get rid of the sign */
  162. sum = 0;
  163. j=0; do {
  164. signx[j] = X[j]<0;
  165. /* OPT: Make sure the compiler doesn't use a branch on ABS16(). */
  166. X[j] = ABS16(X[j]);
  167. iy[j] = 0;
  168. y[j] = 0;
  169. } while (++j<N);
  170. xy = yy = 0;
  171. pulsesLeft = K;
  172. /* Do a pre-search by projecting on the pyramid */
  173. if (K > (N>>1))
  174. {
  175. opus_val16 rcp;
  176. j=0; do {
  177. sum += X[j];
  178. } while (++j<N);
  179. /* If X is too small, just replace it with a pulse at 0 */
  180. #ifdef FIXED_POINT
  181. if (sum <= K)
  182. #else
  183. /* Prevents infinities and NaNs from causing too many pulses
  184. to be allocated. 64 is an approximation of infinity here. */
  185. if (!(sum > EPSILON && sum < 64))
  186. #endif
  187. {
  188. X[0] = QCONST16(1.f,14);
  189. j=1; do
  190. X[j]=0;
  191. while (++j<N);
  192. sum = QCONST16(1.f,14);
  193. }
  194. #ifdef FIXED_POINT
  195. rcp = EXTRACT16(MULT16_32_Q16(K, celt_rcp(sum)));
  196. #else
  197. /* Using K+e with e < 1 guarantees we cannot get more than K pulses. */
  198. rcp = EXTRACT16(MULT16_32_Q16(K+0.8f, celt_rcp(sum)));
  199. #endif
  200. j=0; do {
  201. #ifdef FIXED_POINT
  202. /* It's really important to round *towards zero* here */
  203. iy[j] = MULT16_16_Q15(X[j],rcp);
  204. #else
  205. iy[j] = (int)floor(rcp*X[j]);
  206. #endif
  207. y[j] = (celt_norm)iy[j];
  208. yy = MAC16_16(yy, y[j],y[j]);
  209. xy = MAC16_16(xy, X[j],y[j]);
  210. y[j] *= 2;
  211. pulsesLeft -= iy[j];
  212. } while (++j<N);
  213. }
  214. celt_sig_assert(pulsesLeft>=0);
  215. /* This should never happen, but just in case it does (e.g. on silence)
  216. we fill the first bin with pulses. */
  217. #ifdef FIXED_POINT_DEBUG
  218. celt_sig_assert(pulsesLeft<=N+3);
  219. #endif
  220. if (pulsesLeft > N+3)
  221. {
  222. opus_val16 tmp = (opus_val16)pulsesLeft;
  223. yy = MAC16_16(yy, tmp, tmp);
  224. yy = MAC16_16(yy, tmp, y[0]);
  225. iy[0] += pulsesLeft;
  226. pulsesLeft=0;
  227. }
  228. for (i=0;i<pulsesLeft;i++)
  229. {
  230. opus_val16 Rxy, Ryy;
  231. int best_id;
  232. opus_val32 best_num;
  233. opus_val16 best_den;
  234. #ifdef FIXED_POINT
  235. int rshift;
  236. #endif
  237. #ifdef FIXED_POINT
  238. rshift = 1+celt_ilog2(K-pulsesLeft+i+1);
  239. #endif
  240. best_id = 0;
  241. /* The squared magnitude term gets added anyway, so we might as well
  242. add it outside the loop */
  243. yy = ADD16(yy, 1);
  244. /* Calculations for position 0 are out of the loop, in part to reduce
  245. mispredicted branches (since the if condition is usually false)
  246. in the loop. */
  247. /* Temporary sums of the new pulse(s) */
  248. Rxy = EXTRACT16(SHR32(ADD32(xy, EXTEND32(X[0])),rshift));
  249. /* We're multiplying y[j] by two so we don't have to do it here */
  250. Ryy = ADD16(yy, y[0]);
  251. /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
  252. Rxy is positive because the sign is pre-computed) */
  253. Rxy = MULT16_16_Q15(Rxy,Rxy);
  254. best_den = Ryy;
  255. best_num = Rxy;
  256. j=1;
  257. do {
  258. /* Temporary sums of the new pulse(s) */
  259. Rxy = EXTRACT16(SHR32(ADD32(xy, EXTEND32(X[j])),rshift));
  260. /* We're multiplying y[j] by two so we don't have to do it here */
  261. Ryy = ADD16(yy, y[j]);
  262. /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
  263. Rxy is positive because the sign is pre-computed) */
  264. Rxy = MULT16_16_Q15(Rxy,Rxy);
  265. /* The idea is to check for num/den >= best_num/best_den, but that way
  266. we can do it without any division */
  267. /* OPT: It's not clear whether a cmov is faster than a branch here
  268. since the condition is more often false than true and using
  269. a cmov introduces data dependencies across iterations. The optimal
  270. choice may be architecture-dependent. */
  271. if (opus_unlikely(MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num)))
  272. {
  273. best_den = Ryy;
  274. best_num = Rxy;
  275. best_id = j;
  276. }
  277. } while (++j<N);
  278. /* Updating the sums of the new pulse(s) */
  279. xy = ADD32(xy, EXTEND32(X[best_id]));
  280. /* We're multiplying y[j] by two so we don't have to do it here */
  281. yy = ADD16(yy, y[best_id]);
  282. /* Only now that we've made the final choice, update y/iy */
  283. /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
  284. y[best_id] += 2;
  285. iy[best_id]++;
  286. }
  287. /* Put the original sign back */
  288. j=0;
  289. do {
  290. /*iy[j] = signx[j] ? -iy[j] : iy[j];*/
  291. /* OPT: The is more likely to be compiled without a branch than the code above
  292. but has the same performance otherwise. */
  293. iy[j] = (iy[j]^-signx[j]) + signx[j];
  294. } while (++j<N);
  295. RESTORE_STACK;
  296. return yy;
  297. }
  298. unsigned alg_quant(celt_norm *X, int N, int K, int spread, int B, ec_enc *enc,
  299. opus_val16 gain, int resynth, int arch)
  300. {
  301. VARDECL(int, iy);
  302. opus_val16 yy;
  303. unsigned collapse_mask;
  304. SAVE_STACK;
  305. celt_assert2(K>0, "alg_quant() needs at least one pulse");
  306. celt_assert2(N>1, "alg_quant() needs at least two dimensions");
  307. /* Covers vectorization by up to 4. */
  308. ALLOC(iy, N+3, int);
  309. exp_rotation(X, N, 1, B, K, spread);
  310. yy = op_pvq_search(X, iy, K, N, arch);
  311. encode_pulses(iy, N, K, enc);
  312. if (resynth)
  313. {
  314. normalise_residual(iy, X, N, yy, gain);
  315. exp_rotation(X, N, -1, B, K, spread);
  316. }
  317. collapse_mask = extract_collapse_mask(iy, N, B);
  318. RESTORE_STACK;
  319. return collapse_mask;
  320. }
  321. /** Decode pulse vector and combine the result with the pitch vector to produce
  322. the final normalised signal in the current band. */
  323. unsigned alg_unquant(celt_norm *X, int N, int K, int spread, int B,
  324. ec_dec *dec, opus_val16 gain)
  325. {
  326. opus_val32 Ryy;
  327. unsigned collapse_mask;
  328. VARDECL(int, iy);
  329. SAVE_STACK;
  330. celt_assert2(K>0, "alg_unquant() needs at least one pulse");
  331. celt_assert2(N>1, "alg_unquant() needs at least two dimensions");
  332. ALLOC(iy, N, int);
  333. Ryy = decode_pulses(iy, N, K, dec);
  334. normalise_residual(iy, X, N, Ryy, gain);
  335. exp_rotation(X, N, -1, B, K, spread);
  336. collapse_mask = extract_collapse_mask(iy, N, B);
  337. RESTORE_STACK;
  338. return collapse_mask;
  339. }
  340. #ifndef OVERRIDE_renormalise_vector
  341. void renormalise_vector(celt_norm *X, int N, opus_val16 gain, int arch)
  342. {
  343. int i;
  344. #ifdef FIXED_POINT
  345. int k;
  346. #endif
  347. opus_val32 E;
  348. opus_val16 g;
  349. opus_val32 t;
  350. celt_norm *xptr;
  351. E = EPSILON + celt_inner_prod(X, X, N, arch);
  352. #ifdef FIXED_POINT
  353. k = celt_ilog2(E)>>1;
  354. #endif
  355. t = VSHR32(E, 2*(k-7));
  356. g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
  357. xptr = X;
  358. for (i=0;i<N;i++)
  359. {
  360. *xptr = EXTRACT16(PSHR32(MULT16_16(g, *xptr), k+1));
  361. xptr++;
  362. }
  363. /*return celt_sqrt(E);*/
  364. }
  365. #endif /* OVERRIDE_renormalise_vector */
  366. int stereo_itheta(const celt_norm *X, const celt_norm *Y, int stereo, int N, int arch)
  367. {
  368. int i;
  369. int itheta;
  370. opus_val16 mid, side;
  371. opus_val32 Emid, Eside;
  372. Emid = Eside = EPSILON;
  373. if (stereo)
  374. {
  375. for (i=0;i<N;i++)
  376. {
  377. celt_norm m, s;
  378. m = ADD16(SHR16(X[i],1),SHR16(Y[i],1));
  379. s = SUB16(SHR16(X[i],1),SHR16(Y[i],1));
  380. Emid = MAC16_16(Emid, m, m);
  381. Eside = MAC16_16(Eside, s, s);
  382. }
  383. } else {
  384. Emid += celt_inner_prod(X, X, N, arch);
  385. Eside += celt_inner_prod(Y, Y, N, arch);
  386. }
  387. mid = celt_sqrt(Emid);
  388. side = celt_sqrt(Eside);
  389. #ifdef FIXED_POINT
  390. /* 0.63662 = 2/pi */
  391. itheta = MULT16_16_Q15(QCONST16(0.63662f,15),celt_atan2p(side, mid));
  392. #else
  393. itheta = (int)floor(.5f+16384*0.63662f*fast_atan2f(side,mid));
  394. #endif
  395. return itheta;
  396. }