cwrs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2009 Xiph.Org Foundation
  3. Copyright (c) 2007-2009 Timothy B. Terriberry
  4. Written by Timothy B. Terriberry and Jean-Marc Valin */
  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 FOUNDATION OR
  18. 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. #include "os_support.h"
  30. #include "cwrs.h"
  31. #include "mathops.h"
  32. #include "arch.h"
  33. #ifdef CUSTOM_MODES
  34. /*Guaranteed to return a conservatively large estimate of the binary logarithm
  35. with frac bits of fractional precision.
  36. Tested for all possible 32-bit inputs with frac=4, where the maximum
  37. overestimation is 0.06254243 bits.*/
  38. int log2_frac(opus_uint32 val, int frac)
  39. {
  40. int l;
  41. l=EC_ILOG(val);
  42. if(val&(val-1)){
  43. /*This is (val>>l-16), but guaranteed to round up, even if adding a bias
  44. before the shift would cause overflow (e.g., for 0xFFFFxxxx).*/
  45. if(l>16)val=(val>>(l-16))+(((val&((1<<(l-16))-1))+(1<<(l-16))-1)>>(l-16));
  46. else val<<=16-l;
  47. l=(l-1)<<frac;
  48. /*Note that we always need one iteration, since the rounding up above means
  49. that we might need to adjust the integer part of the logarithm.*/
  50. do{
  51. int b;
  52. b=(int)(val>>16);
  53. l+=b<<frac;
  54. val=(val+b)>>b;
  55. val=(val*val+0x7FFF)>>15;
  56. }
  57. while(frac-->0);
  58. /*If val is not exactly 0x8000, then we have to round up the remainder.*/
  59. return l+(val>0x8000);
  60. }
  61. /*Exact powers of two require no rounding.*/
  62. else return (l-1)<<frac;
  63. }
  64. #endif
  65. #ifndef SMALL_FOOTPRINT
  66. #define MASK32 (0xFFFFFFFF)
  67. /*INV_TABLE[i] holds the multiplicative inverse of (2*i+1) mod 2**32.*/
  68. static const opus_uint32 INV_TABLE[53]={
  69. 0x00000001,0xAAAAAAAB,0xCCCCCCCD,0xB6DB6DB7,
  70. 0x38E38E39,0xBA2E8BA3,0xC4EC4EC5,0xEEEEEEEF,
  71. 0xF0F0F0F1,0x286BCA1B,0x3CF3CF3D,0xE9BD37A7,
  72. 0xC28F5C29,0x684BDA13,0x4F72C235,0xBDEF7BDF,
  73. 0x3E0F83E1,0x8AF8AF8B,0x914C1BAD,0x96F96F97,
  74. 0xC18F9C19,0x2FA0BE83,0xA4FA4FA5,0x677D46CF,
  75. 0x1A1F58D1,0xFAFAFAFB,0x8C13521D,0x586FB587,
  76. 0xB823EE09,0xA08AD8F3,0xC10C9715,0xBEFBEFBF,
  77. 0xC0FC0FC1,0x07A44C6B,0xA33F128D,0xE327A977,
  78. 0xC7E3F1F9,0x962FC963,0x3F2B3885,0x613716AF,
  79. 0x781948B1,0x2B2E43DB,0xFCFCFCFD,0x6FD0EB67,
  80. 0xFA3F47E9,0xD2FD2FD3,0x3F4FD3F5,0xD4E25B9F,
  81. 0x5F02A3A1,0xBF5A814B,0x7C32B16D,0xD3431B57,
  82. 0xD8FD8FD9,
  83. };
  84. /*Computes (_a*_b-_c)/(2*_d+1) when the quotient is known to be exact.
  85. _a, _b, _c, and _d may be arbitrary so long as the arbitrary precision result
  86. fits in 32 bits, but currently the table for multiplicative inverses is only
  87. valid for _d<=52.*/
  88. static inline opus_uint32 imusdiv32odd(opus_uint32 _a,opus_uint32 _b,
  89. opus_uint32 _c,int _d){
  90. celt_assert(_d<=52);
  91. return (_a*_b-_c)*INV_TABLE[_d]&MASK32;
  92. }
  93. /*Computes (_a*_b-_c)/_d when the quotient is known to be exact.
  94. _d does not actually have to be even, but imusdiv32odd will be faster when
  95. it's odd, so you should use that instead.
  96. _a and _d are assumed to be small (e.g., _a*_d fits in 32 bits; currently the
  97. table for multiplicative inverses is only valid for _d<=54).
  98. _b and _c may be arbitrary so long as the arbitrary precision reuslt fits in
  99. 32 bits.*/
  100. static inline opus_uint32 imusdiv32even(opus_uint32 _a,opus_uint32 _b,
  101. opus_uint32 _c,int _d){
  102. opus_uint32 inv;
  103. int mask;
  104. int shift;
  105. int one;
  106. celt_assert(_d>0);
  107. celt_assert(_d<=54);
  108. shift=EC_ILOG(_d^(_d-1));
  109. inv=INV_TABLE[(_d-1)>>shift];
  110. shift--;
  111. one=1<<shift;
  112. mask=one-1;
  113. return (_a*(_b>>shift)-(_c>>shift)+
  114. ((_a*(_b&mask)+one-(_c&mask))>>shift)-1)*inv&MASK32;
  115. }
  116. #endif /* SMALL_FOOTPRINT */
  117. /*Although derived separately, the pulse vector coding scheme is equivalent to
  118. a Pyramid Vector Quantizer \cite{Fis86}.
  119. Some additional notes about an early version appear at
  120. http://people.xiph.org/~tterribe/notes/cwrs.html, but the codebook ordering
  121. and the definitions of some terms have evolved since that was written.
  122. The conversion from a pulse vector to an integer index (encoding) and back
  123. (decoding) is governed by two related functions, V(N,K) and U(N,K).
  124. V(N,K) = the number of combinations, with replacement, of N items, taken K
  125. at a time, when a sign bit is added to each item taken at least once (i.e.,
  126. the number of N-dimensional unit pulse vectors with K pulses).
  127. One way to compute this is via
  128. V(N,K) = K>0 ? sum(k=1...K,2**k*choose(N,k)*choose(K-1,k-1)) : 1,
  129. where choose() is the binomial function.
  130. A table of values for N<10 and K<10 looks like:
  131. V[10][10] = {
  132. {1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  133. {1, 2, 2, 2, 2, 2, 2, 2, 2, 2},
  134. {1, 4, 8, 12, 16, 20, 24, 28, 32, 36},
  135. {1, 6, 18, 38, 66, 102, 146, 198, 258, 326},
  136. {1, 8, 32, 88, 192, 360, 608, 952, 1408, 1992},
  137. {1, 10, 50, 170, 450, 1002, 1970, 3530, 5890, 9290},
  138. {1, 12, 72, 292, 912, 2364, 5336, 10836, 20256, 35436},
  139. {1, 14, 98, 462, 1666, 4942, 12642, 28814, 59906, 115598},
  140. {1, 16, 128, 688, 2816, 9424, 27008, 68464, 157184, 332688},
  141. {1, 18, 162, 978, 4482, 16722, 53154, 148626, 374274, 864146}
  142. };
  143. U(N,K) = the number of such combinations wherein N-1 objects are taken at
  144. most K-1 at a time.
  145. This is given by
  146. U(N,K) = sum(k=0...K-1,V(N-1,k))
  147. = K>0 ? (V(N-1,K-1) + V(N,K-1))/2 : 0.
  148. The latter expression also makes clear that U(N,K) is half the number of such
  149. combinations wherein the first object is taken at least once.
  150. Although it may not be clear from either of these definitions, U(N,K) is the
  151. natural function to work with when enumerating the pulse vector codebooks,
  152. not V(N,K).
  153. U(N,K) is not well-defined for N=0, but with the extension
  154. U(0,K) = K>0 ? 0 : 1,
  155. the function becomes symmetric: U(N,K) = U(K,N), with a similar table:
  156. U[10][10] = {
  157. {1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  158. {0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  159. {0, 1, 3, 5, 7, 9, 11, 13, 15, 17},
  160. {0, 1, 5, 13, 25, 41, 61, 85, 113, 145},
  161. {0, 1, 7, 25, 63, 129, 231, 377, 575, 833},
  162. {0, 1, 9, 41, 129, 321, 681, 1289, 2241, 3649},
  163. {0, 1, 11, 61, 231, 681, 1683, 3653, 7183, 13073},
  164. {0, 1, 13, 85, 377, 1289, 3653, 8989, 19825, 40081},
  165. {0, 1, 15, 113, 575, 2241, 7183, 19825, 48639, 108545},
  166. {0, 1, 17, 145, 833, 3649, 13073, 40081, 108545, 265729}
  167. };
  168. With this extension, V(N,K) may be written in terms of U(N,K):
  169. V(N,K) = U(N,K) + U(N,K+1)
  170. for all N>=0, K>=0.
  171. Thus U(N,K+1) represents the number of combinations where the first element
  172. is positive or zero, and U(N,K) represents the number of combinations where
  173. it is negative.
  174. With a large enough table of U(N,K) values, we could write O(N) encoding
  175. and O(min(N*log(K),N+K)) decoding routines, but such a table would be
  176. prohibitively large for small embedded devices (K may be as large as 32767
  177. for small N, and N may be as large as 200).
  178. Both functions obey the same recurrence relation:
  179. V(N,K) = V(N-1,K) + V(N,K-1) + V(N-1,K-1),
  180. U(N,K) = U(N-1,K) + U(N,K-1) + U(N-1,K-1),
  181. for all N>0, K>0, with different initial conditions at N=0 or K=0.
  182. This allows us to construct a row of one of the tables above given the
  183. previous row or the next row.
  184. Thus we can derive O(NK) encoding and decoding routines with O(K) memory
  185. using only addition and subtraction.
  186. When encoding, we build up from the U(2,K) row and work our way forwards.
  187. When decoding, we need to start at the U(N,K) row and work our way backwards,
  188. which requires a means of computing U(N,K).
  189. U(N,K) may be computed from two previous values with the same N:
  190. U(N,K) = ((2*N-1)*U(N,K-1) - U(N,K-2))/(K-1) + U(N,K-2)
  191. for all N>1, and since U(N,K) is symmetric, a similar relation holds for two
  192. previous values with the same K:
  193. U(N,K>1) = ((2*K-1)*U(N-1,K) - U(N-2,K))/(N-1) + U(N-2,K)
  194. for all K>1.
  195. This allows us to construct an arbitrary row of the U(N,K) table by starting
  196. with the first two values, which are constants.
  197. This saves roughly 2/3 the work in our O(NK) decoding routine, but costs O(K)
  198. multiplications.
  199. Similar relations can be derived for V(N,K), but are not used here.
  200. For N>0 and K>0, U(N,K) and V(N,K) take on the form of an (N-1)-degree
  201. polynomial for fixed N.
  202. The first few are
  203. U(1,K) = 1,
  204. U(2,K) = 2*K-1,
  205. U(3,K) = (2*K-2)*K+1,
  206. U(4,K) = (((4*K-6)*K+8)*K-3)/3,
  207. U(5,K) = ((((2*K-4)*K+10)*K-8)*K+3)/3,
  208. and
  209. V(1,K) = 2,
  210. V(2,K) = 4*K,
  211. V(3,K) = 4*K*K+2,
  212. V(4,K) = 8*(K*K+2)*K/3,
  213. V(5,K) = ((4*K*K+20)*K*K+6)/3,
  214. for all K>0.
  215. This allows us to derive O(N) encoding and O(N*log(K)) decoding routines for
  216. small N (and indeed decoding is also O(N) for N<3).
  217. @ARTICLE{Fis86,
  218. author="Thomas R. Fischer",
  219. title="A Pyramid Vector Quantizer",
  220. journal="IEEE Transactions on Information Theory",
  221. volume="IT-32",
  222. number=4,
  223. pages="568--583",
  224. month=Jul,
  225. year=1986
  226. }*/
  227. #ifndef SMALL_FOOTPRINT
  228. /*Compute U(2,_k).
  229. Note that this may be called with _k=32768 (maxK[2]+1).*/
  230. static inline unsigned ucwrs2(unsigned _k){
  231. celt_assert(_k>0);
  232. return _k+(_k-1);
  233. }
  234. /*Compute V(2,_k).*/
  235. static inline opus_uint32 ncwrs2(int _k){
  236. celt_assert(_k>0);
  237. return 4*(opus_uint32)_k;
  238. }
  239. /*Compute U(3,_k).
  240. Note that this may be called with _k=32768 (maxK[3]+1).*/
  241. static inline opus_uint32 ucwrs3(unsigned _k){
  242. celt_assert(_k>0);
  243. return (2*(opus_uint32)_k-2)*_k+1;
  244. }
  245. /*Compute V(3,_k).*/
  246. static inline opus_uint32 ncwrs3(int _k){
  247. celt_assert(_k>0);
  248. return 2*(2*(unsigned)_k*(opus_uint32)_k+1);
  249. }
  250. /*Compute U(4,_k).*/
  251. static inline opus_uint32 ucwrs4(int _k){
  252. celt_assert(_k>0);
  253. return imusdiv32odd(2*_k,(2*_k-3)*(opus_uint32)_k+4,3,1);
  254. }
  255. /*Compute V(4,_k).*/
  256. static inline opus_uint32 ncwrs4(int _k){
  257. celt_assert(_k>0);
  258. return ((_k*(opus_uint32)_k+2)*_k)/3<<3;
  259. }
  260. #endif /* SMALL_FOOTPRINT */
  261. /*Computes the next row/column of any recurrence that obeys the relation
  262. u[i][j]=u[i-1][j]+u[i][j-1]+u[i-1][j-1].
  263. _ui0 is the base case for the new row/column.*/
  264. static inline void unext(opus_uint32 *_ui,unsigned _len,opus_uint32 _ui0){
  265. opus_uint32 ui1;
  266. unsigned j;
  267. /*This do-while will overrun the array if we don't have storage for at least
  268. 2 values.*/
  269. j=1; do {
  270. ui1=UADD32(UADD32(_ui[j],_ui[j-1]),_ui0);
  271. _ui[j-1]=_ui0;
  272. _ui0=ui1;
  273. } while (++j<_len);
  274. _ui[j-1]=_ui0;
  275. }
  276. /*Computes the previous row/column of any recurrence that obeys the relation
  277. u[i-1][j]=u[i][j]-u[i][j-1]-u[i-1][j-1].
  278. _ui0 is the base case for the new row/column.*/
  279. static inline void uprev(opus_uint32 *_ui,unsigned _n,opus_uint32 _ui0){
  280. opus_uint32 ui1;
  281. unsigned j;
  282. /*This do-while will overrun the array if we don't have storage for at least
  283. 2 values.*/
  284. j=1; do {
  285. ui1=USUB32(USUB32(_ui[j],_ui[j-1]),_ui0);
  286. _ui[j-1]=_ui0;
  287. _ui0=ui1;
  288. } while (++j<_n);
  289. _ui[j-1]=_ui0;
  290. }
  291. /*Compute V(_n,_k), as well as U(_n,0..._k+1).
  292. _u: On exit, _u[i] contains U(_n,i) for i in [0..._k+1].*/
  293. static opus_uint32 ncwrs_urow(unsigned _n,unsigned _k,opus_uint32 *_u){
  294. opus_uint32 um2;
  295. unsigned len;
  296. unsigned k;
  297. len=_k+2;
  298. /*We require storage at least 3 values (e.g., _k>0).*/
  299. celt_assert(len>=3);
  300. _u[0]=0;
  301. _u[1]=um2=1;
  302. #ifndef SMALL_FOOTPRINT
  303. /*_k>52 doesn't work in the false branch due to the limits of INV_TABLE,
  304. but _k isn't tested here because k<=52 for n=7*/
  305. if(_n<=6)
  306. #endif
  307. {
  308. /*If _n==0, _u[0] should be 1 and the rest should be 0.*/
  309. /*If _n==1, _u[i] should be 1 for i>1.*/
  310. celt_assert(_n>=2);
  311. /*If _k==0, the following do-while loop will overflow the buffer.*/
  312. celt_assert(_k>0);
  313. k=2;
  314. do _u[k]=(k<<1)-1;
  315. while(++k<len);
  316. for(k=2;k<_n;k++)unext(_u+1,_k+1,1);
  317. }
  318. #ifndef SMALL_FOOTPRINT
  319. else{
  320. opus_uint32 um1;
  321. opus_uint32 n2m1;
  322. _u[2]=n2m1=um1=(_n<<1)-1;
  323. for(k=3;k<len;k++){
  324. /*U(N,K) = ((2*N-1)*U(N,K-1)-U(N,K-2))/(K-1) + U(N,K-2)*/
  325. _u[k]=um2=imusdiv32even(n2m1,um1,um2,k-1)+um2;
  326. if(++k>=len)break;
  327. _u[k]=um1=imusdiv32odd(n2m1,um2,um1,(k-1)>>1)+um1;
  328. }
  329. }
  330. #endif /* SMALL_FOOTPRINT */
  331. return _u[_k]+_u[_k+1];
  332. }
  333. #ifndef SMALL_FOOTPRINT
  334. /*Returns the _i'th combination of _k elements (at most 32767) chosen from a
  335. set of size 1 with associated sign bits.
  336. _y: Returns the vector of pulses.*/
  337. static inline void cwrsi1(int _k,opus_uint32 _i,int *_y){
  338. int s;
  339. s=-(int)_i;
  340. _y[0]=(_k+s)^s;
  341. }
  342. /*Returns the _i'th combination of _k elements (at most 32767) chosen from a
  343. set of size 2 with associated sign bits.
  344. _y: Returns the vector of pulses.*/
  345. static inline void cwrsi2(int _k,opus_uint32 _i,int *_y){
  346. opus_uint32 p;
  347. int s;
  348. int yj;
  349. p=ucwrs2(_k+1U);
  350. s=-(_i>=p);
  351. _i-=p&s;
  352. yj=_k;
  353. _k=(_i+1)>>1;
  354. p=_k?ucwrs2(_k):0;
  355. _i-=p;
  356. yj-=_k;
  357. _y[0]=(yj+s)^s;
  358. cwrsi1(_k,_i,_y+1);
  359. }
  360. /*Returns the _i'th combination of _k elements (at most 32767) chosen from a
  361. set of size 3 with associated sign bits.
  362. _y: Returns the vector of pulses.*/
  363. static void cwrsi3(int _k,opus_uint32 _i,int *_y){
  364. opus_uint32 p;
  365. int s;
  366. int yj;
  367. p=ucwrs3(_k+1U);
  368. s=-(_i>=p);
  369. _i-=p&s;
  370. yj=_k;
  371. /*Finds the maximum _k such that ucwrs3(_k)<=_i (tested for all
  372. _i<2147418113=U(3,32768)).*/
  373. _k=_i>0?(isqrt32(2*_i-1)+1)>>1:0;
  374. p=_k?ucwrs3(_k):0;
  375. _i-=p;
  376. yj-=_k;
  377. _y[0]=(yj+s)^s;
  378. cwrsi2(_k,_i,_y+1);
  379. }
  380. /*Returns the _i'th combination of _k elements (at most 1172) chosen from a set
  381. of size 4 with associated sign bits.
  382. _y: Returns the vector of pulses.*/
  383. static void cwrsi4(int _k,opus_uint32 _i,int *_y){
  384. opus_uint32 p;
  385. int s;
  386. int yj;
  387. int kl;
  388. int kr;
  389. p=ucwrs4(_k+1);
  390. s=-(_i>=p);
  391. _i-=p&s;
  392. yj=_k;
  393. /*We could solve a cubic for k here, but the form of the direct solution does
  394. not lend itself well to exact integer arithmetic.
  395. Instead we do a binary search on U(4,K).*/
  396. kl=0;
  397. kr=_k;
  398. for(;;){
  399. _k=(kl+kr)>>1;
  400. p=_k?ucwrs4(_k):0;
  401. if(p<_i){
  402. if(_k>=kr)break;
  403. kl=_k+1;
  404. }
  405. else if(p>_i)kr=_k-1;
  406. else break;
  407. }
  408. _i-=p;
  409. yj-=_k;
  410. _y[0]=(yj+s)^s;
  411. cwrsi3(_k,_i,_y+1);
  412. }
  413. #endif /* SMALL_FOOTPRINT */
  414. /*Returns the _i'th combination of _k elements chosen from a set of size _n
  415. with associated sign bits.
  416. _y: Returns the vector of pulses.
  417. _u: Must contain entries [0..._k+1] of row _n of U() on input.
  418. Its contents will be destructively modified.*/
  419. static void cwrsi(int _n,int _k,opus_uint32 _i,int *_y,opus_uint32 *_u){
  420. int j;
  421. celt_assert(_n>0);
  422. j=0;
  423. do{
  424. opus_uint32 p;
  425. int s;
  426. int yj;
  427. p=_u[_k+1];
  428. s=-(_i>=p);
  429. _i-=p&s;
  430. yj=_k;
  431. p=_u[_k];
  432. while(p>_i)p=_u[--_k];
  433. _i-=p;
  434. yj-=_k;
  435. _y[j]=(yj+s)^s;
  436. uprev(_u,_k+2,0);
  437. }
  438. while(++j<_n);
  439. }
  440. /*Returns the index of the given combination of K elements chosen from a set
  441. of size 1 with associated sign bits.
  442. _y: The vector of pulses, whose sum of absolute values is K.
  443. _k: Returns K.*/
  444. static inline opus_uint32 icwrs1(const int *_y,int *_k){
  445. *_k=abs(_y[0]);
  446. return _y[0]<0;
  447. }
  448. #ifndef SMALL_FOOTPRINT
  449. /*Returns the index of the given combination of K elements chosen from a set
  450. of size 2 with associated sign bits.
  451. _y: The vector of pulses, whose sum of absolute values is K.
  452. _k: Returns K.*/
  453. static inline opus_uint32 icwrs2(const int *_y,int *_k){
  454. opus_uint32 i;
  455. int k;
  456. i=icwrs1(_y+1,&k);
  457. i+=k?ucwrs2(k):0;
  458. k+=abs(_y[0]);
  459. if(_y[0]<0)i+=ucwrs2(k+1U);
  460. *_k=k;
  461. return i;
  462. }
  463. /*Returns the index of the given combination of K elements chosen from a set
  464. of size 3 with associated sign bits.
  465. _y: The vector of pulses, whose sum of absolute values is K.
  466. _k: Returns K.*/
  467. static inline opus_uint32 icwrs3(const int *_y,int *_k){
  468. opus_uint32 i;
  469. int k;
  470. i=icwrs2(_y+1,&k);
  471. i+=k?ucwrs3(k):0;
  472. k+=abs(_y[0]);
  473. if(_y[0]<0)i+=ucwrs3(k+1U);
  474. *_k=k;
  475. return i;
  476. }
  477. /*Returns the index of the given combination of K elements chosen from a set
  478. of size 4 with associated sign bits.
  479. _y: The vector of pulses, whose sum of absolute values is K.
  480. _k: Returns K.*/
  481. static inline opus_uint32 icwrs4(const int *_y,int *_k){
  482. opus_uint32 i;
  483. int k;
  484. i=icwrs3(_y+1,&k);
  485. i+=k?ucwrs4(k):0;
  486. k+=abs(_y[0]);
  487. if(_y[0]<0)i+=ucwrs4(k+1);
  488. *_k=k;
  489. return i;
  490. }
  491. #endif /* SMALL_FOOTPRINT */
  492. /*Returns the index of the given combination of K elements chosen from a set
  493. of size _n with associated sign bits.
  494. _y: The vector of pulses, whose sum of absolute values must be _k.
  495. _nc: Returns V(_n,_k).*/
  496. static inline opus_uint32 icwrs(int _n,int _k,opus_uint32 *_nc,const int *_y,
  497. opus_uint32 *_u){
  498. opus_uint32 i;
  499. int j;
  500. int k;
  501. /*We can't unroll the first two iterations of the loop unless _n>=2.*/
  502. celt_assert(_n>=2);
  503. _u[0]=0;
  504. for(k=1;k<=_k+1;k++)_u[k]=(k<<1)-1;
  505. i=icwrs1(_y+_n-1,&k);
  506. j=_n-2;
  507. i+=_u[k];
  508. k+=abs(_y[j]);
  509. if(_y[j]<0)i+=_u[k+1];
  510. while(j-->0){
  511. unext(_u,_k+2,0);
  512. i+=_u[k];
  513. k+=abs(_y[j]);
  514. if(_y[j]<0)i+=_u[k+1];
  515. }
  516. *_nc=_u[k]+_u[k+1];
  517. return i;
  518. }
  519. #ifdef CUSTOM_MODES
  520. void get_required_bits(opus_int16 *_bits,int _n,int _maxk,int _frac){
  521. int k;
  522. /*_maxk==0 => there's nothing to do.*/
  523. celt_assert(_maxk>0);
  524. _bits[0]=0;
  525. if (_n==1)
  526. {
  527. for (k=1;k<=_maxk;k++)
  528. _bits[k] = 1<<_frac;
  529. }
  530. else {
  531. VARDECL(opus_uint32,u);
  532. SAVE_STACK;
  533. ALLOC(u,_maxk+2U,opus_uint32);
  534. ncwrs_urow(_n,_maxk,u);
  535. for(k=1;k<=_maxk;k++)
  536. _bits[k]=log2_frac(u[k]+u[k+1],_frac);
  537. RESTORE_STACK;
  538. }
  539. }
  540. #endif /* CUSTOM_MODES */
  541. void encode_pulses(const int *_y,int _n,int _k,ec_enc *_enc){
  542. opus_uint32 i;
  543. celt_assert(_k>0);
  544. #ifndef SMALL_FOOTPRINT
  545. switch(_n){
  546. case 2:{
  547. i=icwrs2(_y,&_k);
  548. ec_enc_uint(_enc,i,ncwrs2(_k));
  549. }break;
  550. case 3:{
  551. i=icwrs3(_y,&_k);
  552. ec_enc_uint(_enc,i,ncwrs3(_k));
  553. }break;
  554. case 4:{
  555. i=icwrs4(_y,&_k);
  556. ec_enc_uint(_enc,i,ncwrs4(_k));
  557. }break;
  558. default:
  559. {
  560. #endif
  561. VARDECL(opus_uint32,u);
  562. opus_uint32 nc;
  563. SAVE_STACK;
  564. ALLOC(u,_k+2U,opus_uint32);
  565. i=icwrs(_n,_k,&nc,_y,u);
  566. ec_enc_uint(_enc,i,nc);
  567. RESTORE_STACK;
  568. #ifndef SMALL_FOOTPRINT
  569. }
  570. break;
  571. }
  572. #endif
  573. }
  574. void decode_pulses(int *_y,int _n,int _k,ec_dec *_dec)
  575. {
  576. celt_assert(_k>0);
  577. #ifndef SMALL_FOOTPRINT
  578. switch(_n){
  579. case 2:cwrsi2(_k,ec_dec_uint(_dec,ncwrs2(_k)),_y);break;
  580. case 3:cwrsi3(_k,ec_dec_uint(_dec,ncwrs3(_k)),_y);break;
  581. case 4:cwrsi4(_k,ec_dec_uint(_dec,ncwrs4(_k)),_y);break;
  582. default:
  583. {
  584. #endif
  585. VARDECL(opus_uint32,u);
  586. SAVE_STACK;
  587. ALLOC(u,_k+2U,opus_uint32);
  588. cwrsi(_n,_k,ec_dec_uint(_dec,ncwrs_urow(_n,_k,u)),_y,u);
  589. RESTORE_STACK;
  590. #ifndef SMALL_FOOTPRINT
  591. }
  592. break;
  593. }
  594. #endif
  595. }