vq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. static 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. unsigned alg_quant(celt_norm *X, int N, int K, int spread, int B, ec_enc *enc
  146. #ifdef RESYNTH
  147. , opus_val16 gain
  148. #endif
  149. )
  150. {
  151. VARDECL(celt_norm, y);
  152. VARDECL(int, iy);
  153. VARDECL(opus_val16, signx);
  154. int i, j;
  155. opus_val16 s;
  156. int pulsesLeft;
  157. opus_val32 sum;
  158. opus_val32 xy;
  159. opus_val16 yy;
  160. unsigned collapse_mask;
  161. SAVE_STACK;
  162. celt_assert2(K>0, "alg_quant() needs at least one pulse");
  163. celt_assert2(N>1, "alg_quant() needs at least two dimensions");
  164. ALLOC(y, N, celt_norm);
  165. ALLOC(iy, N, int);
  166. ALLOC(signx, N, opus_val16);
  167. exp_rotation(X, N, 1, B, K, spread);
  168. /* Get rid of the sign */
  169. sum = 0;
  170. j=0; do {
  171. if (X[j]>0)
  172. signx[j]=1;
  173. else {
  174. signx[j]=-1;
  175. X[j]=-X[j];
  176. }
  177. iy[j] = 0;
  178. y[j] = 0;
  179. } while (++j<N);
  180. xy = yy = 0;
  181. pulsesLeft = K;
  182. /* Do a pre-search by projecting on the pyramid */
  183. if (K > (N>>1))
  184. {
  185. opus_val16 rcp;
  186. j=0; do {
  187. sum += X[j];
  188. } while (++j<N);
  189. /* If X is too small, just replace it with a pulse at 0 */
  190. #ifdef FIXED_POINT
  191. if (sum <= K)
  192. #else
  193. /* Prevents infinities and NaNs from causing too many pulses
  194. to be allocated. 64 is an approximation of infinity here. */
  195. if (!(sum > EPSILON && sum < 64))
  196. #endif
  197. {
  198. X[0] = QCONST16(1.f,14);
  199. j=1; do
  200. X[j]=0;
  201. while (++j<N);
  202. sum = QCONST16(1.f,14);
  203. }
  204. rcp = EXTRACT16(MULT16_32_Q16(K-1, celt_rcp(sum)));
  205. j=0; do {
  206. #ifdef FIXED_POINT
  207. /* It's really important to round *towards zero* here */
  208. iy[j] = MULT16_16_Q15(X[j],rcp);
  209. #else
  210. iy[j] = (int)floor(rcp*X[j]);
  211. #endif
  212. y[j] = (celt_norm)iy[j];
  213. yy = MAC16_16(yy, y[j],y[j]);
  214. xy = MAC16_16(xy, X[j],y[j]);
  215. y[j] *= 2;
  216. pulsesLeft -= iy[j];
  217. } while (++j<N);
  218. }
  219. celt_assert2(pulsesLeft>=1, "Allocated too many pulses in the quick pass");
  220. /* This should never happen, but just in case it does (e.g. on silence)
  221. we fill the first bin with pulses. */
  222. #ifdef FIXED_POINT_DEBUG
  223. celt_assert2(pulsesLeft<=N+3, "Not enough pulses in the quick pass");
  224. #endif
  225. if (pulsesLeft > N+3)
  226. {
  227. opus_val16 tmp = (opus_val16)pulsesLeft;
  228. yy = MAC16_16(yy, tmp, tmp);
  229. yy = MAC16_16(yy, tmp, y[0]);
  230. iy[0] += pulsesLeft;
  231. pulsesLeft=0;
  232. }
  233. s = 1;
  234. for (i=0;i<pulsesLeft;i++)
  235. {
  236. int best_id;
  237. opus_val32 best_num = -VERY_LARGE16;
  238. opus_val16 best_den = 0;
  239. #ifdef FIXED_POINT
  240. int rshift;
  241. #endif
  242. #ifdef FIXED_POINT
  243. rshift = 1+celt_ilog2(K-pulsesLeft+i+1);
  244. #endif
  245. best_id = 0;
  246. /* The squared magnitude term gets added anyway, so we might as well
  247. add it outside the loop */
  248. yy = ADD16(yy, 1);
  249. j=0;
  250. do {
  251. opus_val16 Rxy, Ryy;
  252. /* Temporary sums of the new pulse(s) */
  253. Rxy = EXTRACT16(SHR32(ADD32(xy, EXTEND32(X[j])),rshift));
  254. /* We're multiplying y[j] by two so we don't have to do it here */
  255. Ryy = ADD16(yy, y[j]);
  256. /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
  257. Rxy is positive because the sign is pre-computed) */
  258. Rxy = MULT16_16_Q15(Rxy,Rxy);
  259. /* The idea is to check for num/den >= best_num/best_den, but that way
  260. we can do it without any division */
  261. /* OPT: Make sure to use conditional moves here */
  262. if (MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num))
  263. {
  264. best_den = Ryy;
  265. best_num = Rxy;
  266. best_id = j;
  267. }
  268. } while (++j<N);
  269. /* Updating the sums of the new pulse(s) */
  270. xy = ADD32(xy, EXTEND32(X[best_id]));
  271. /* We're multiplying y[j] by two so we don't have to do it here */
  272. yy = ADD16(yy, y[best_id]);
  273. /* Only now that we've made the final choice, update y/iy */
  274. /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
  275. y[best_id] += 2*s;
  276. iy[best_id]++;
  277. }
  278. /* Put the original sign back */
  279. j=0;
  280. do {
  281. X[j] = MULT16_16(signx[j],X[j]);
  282. if (signx[j] < 0)
  283. iy[j] = -iy[j];
  284. } while (++j<N);
  285. encode_pulses(iy, N, K, enc);
  286. #ifdef RESYNTH
  287. normalise_residual(iy, X, N, yy, gain);
  288. exp_rotation(X, N, -1, B, K, spread);
  289. #endif
  290. collapse_mask = extract_collapse_mask(iy, N, B);
  291. RESTORE_STACK;
  292. return collapse_mask;
  293. }
  294. /** Decode pulse vector and combine the result with the pitch vector to produce
  295. the final normalised signal in the current band. */
  296. unsigned alg_unquant(celt_norm *X, int N, int K, int spread, int B,
  297. ec_dec *dec, opus_val16 gain)
  298. {
  299. opus_val32 Ryy;
  300. unsigned collapse_mask;
  301. VARDECL(int, iy);
  302. SAVE_STACK;
  303. celt_assert2(K>0, "alg_unquant() needs at least one pulse");
  304. celt_assert2(N>1, "alg_unquant() needs at least two dimensions");
  305. ALLOC(iy, N, int);
  306. Ryy = decode_pulses(iy, N, K, dec);
  307. normalise_residual(iy, X, N, Ryy, gain);
  308. exp_rotation(X, N, -1, B, K, spread);
  309. collapse_mask = extract_collapse_mask(iy, N, B);
  310. RESTORE_STACK;
  311. return collapse_mask;
  312. }
  313. #ifndef OVERRIDE_renormalise_vector
  314. void renormalise_vector(celt_norm *X, int N, opus_val16 gain, int arch)
  315. {
  316. int i;
  317. #ifdef FIXED_POINT
  318. int k;
  319. #endif
  320. opus_val32 E;
  321. opus_val16 g;
  322. opus_val32 t;
  323. celt_norm *xptr;
  324. E = EPSILON + celt_inner_prod(X, X, N, arch);
  325. #ifdef FIXED_POINT
  326. k = celt_ilog2(E)>>1;
  327. #endif
  328. t = VSHR32(E, 2*(k-7));
  329. g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
  330. xptr = X;
  331. for (i=0;i<N;i++)
  332. {
  333. *xptr = EXTRACT16(PSHR32(MULT16_16(g, *xptr), k+1));
  334. xptr++;
  335. }
  336. /*return celt_sqrt(E);*/
  337. }
  338. #endif /* OVERRIDE_renormalise_vector */
  339. int stereo_itheta(const celt_norm *X, const celt_norm *Y, int stereo, int N, int arch)
  340. {
  341. int i;
  342. int itheta;
  343. opus_val16 mid, side;
  344. opus_val32 Emid, Eside;
  345. Emid = Eside = EPSILON;
  346. if (stereo)
  347. {
  348. for (i=0;i<N;i++)
  349. {
  350. celt_norm m, s;
  351. m = ADD16(SHR16(X[i],1),SHR16(Y[i],1));
  352. s = SUB16(SHR16(X[i],1),SHR16(Y[i],1));
  353. Emid = MAC16_16(Emid, m, m);
  354. Eside = MAC16_16(Eside, s, s);
  355. }
  356. } else {
  357. Emid += celt_inner_prod(X, X, N, arch);
  358. Eside += celt_inner_prod(Y, Y, N, arch);
  359. }
  360. mid = celt_sqrt(Emid);
  361. side = celt_sqrt(Eside);
  362. #ifdef FIXED_POINT
  363. /* 0.63662 = 2/pi */
  364. itheta = MULT16_16_Q15(QCONST16(0.63662f,15),celt_atan2p(side, mid));
  365. #else
  366. itheta = (int)floor(.5f+16384*0.63662f*atan2(side,mid));
  367. #endif
  368. return itheta;
  369. }