vq.c 11 KB

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