vq.c 13 KB

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