opus_pvq.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * Copyright (c) 2007-2008 CSIRO
  3. * Copyright (c) 2007-2009 Xiph.Org Foundation
  4. * Copyright (c) 2008-2009 Gregory Maxwell
  5. * Copyright (c) 2012 Andrew D'Addesio
  6. * Copyright (c) 2013-2014 Mozilla Corporation
  7. * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include "opustab.h"
  26. #include "opus_pvq.h"
  27. #define CELT_PVQ_U(n, k) (ff_celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)])
  28. #define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, (k) + 1))
  29. static inline int16_t celt_cos(int16_t x)
  30. {
  31. x = (MUL16(x, x) + 4096) >> 13;
  32. x = (32767-x) + ROUND_MUL16(x, (-7651 + ROUND_MUL16(x, (8277 + ROUND_MUL16(-626, x)))));
  33. return x + 1;
  34. }
  35. static inline int celt_log2tan(int isin, int icos)
  36. {
  37. int lc, ls;
  38. lc = opus_ilog(icos);
  39. ls = opus_ilog(isin);
  40. icos <<= 15 - lc;
  41. isin <<= 15 - ls;
  42. return (ls << 11) - (lc << 11) +
  43. ROUND_MUL16(isin, ROUND_MUL16(isin, -2597) + 7932) -
  44. ROUND_MUL16(icos, ROUND_MUL16(icos, -2597) + 7932);
  45. }
  46. static inline int celt_bits2pulses(const uint8_t *cache, int bits)
  47. {
  48. // TODO: Find the size of cache and make it into an array in the parameters list
  49. int i, low = 0, high;
  50. high = cache[0];
  51. bits--;
  52. for (i = 0; i < 6; i++) {
  53. int center = (low + high + 1) >> 1;
  54. if (cache[center] >= bits)
  55. high = center;
  56. else
  57. low = center;
  58. }
  59. return (bits - (low == 0 ? -1 : cache[low]) <= cache[high] - bits) ? low : high;
  60. }
  61. static inline int celt_pulses2bits(const uint8_t *cache, int pulses)
  62. {
  63. // TODO: Find the size of cache and make it into an array in the parameters list
  64. return (pulses == 0) ? 0 : cache[pulses] + 1;
  65. }
  66. static inline void celt_normalize_residual(const int * av_restrict iy, float * av_restrict X,
  67. int N, float g)
  68. {
  69. int i;
  70. for (i = 0; i < N; i++)
  71. X[i] = g * iy[i];
  72. }
  73. static void celt_exp_rotation_impl(float *X, uint32_t len, uint32_t stride,
  74. float c, float s)
  75. {
  76. float *Xptr;
  77. int i;
  78. Xptr = X;
  79. for (i = 0; i < len - stride; i++) {
  80. float x1 = Xptr[0];
  81. float x2 = Xptr[stride];
  82. Xptr[stride] = c * x2 + s * x1;
  83. *Xptr++ = c * x1 - s * x2;
  84. }
  85. Xptr = &X[len - 2 * stride - 1];
  86. for (i = len - 2 * stride - 1; i >= 0; i--) {
  87. float x1 = Xptr[0];
  88. float x2 = Xptr[stride];
  89. Xptr[stride] = c * x2 + s * x1;
  90. *Xptr-- = c * x1 - s * x2;
  91. }
  92. }
  93. static inline void celt_exp_rotation(float *X, uint32_t len,
  94. uint32_t stride, uint32_t K,
  95. enum CeltSpread spread, const int encode)
  96. {
  97. uint32_t stride2 = 0;
  98. float c, s;
  99. float gain, theta;
  100. int i;
  101. if (2*K >= len || spread == CELT_SPREAD_NONE)
  102. return;
  103. gain = (float)len / (len + (20 - 5*spread) * K);
  104. theta = M_PI * gain * gain / 4;
  105. c = cosf(theta);
  106. s = sinf(theta);
  107. if (len >= stride << 3) {
  108. stride2 = 1;
  109. /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding.
  110. It's basically incrementing long as (stride2+0.5)^2 < len/stride. */
  111. while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len)
  112. stride2++;
  113. }
  114. len /= stride;
  115. for (i = 0; i < stride; i++) {
  116. if (encode) {
  117. celt_exp_rotation_impl(X + i * len, len, 1, c, -s);
  118. if (stride2)
  119. celt_exp_rotation_impl(X + i * len, len, stride2, s, -c);
  120. } else {
  121. if (stride2)
  122. celt_exp_rotation_impl(X + i * len, len, stride2, s, c);
  123. celt_exp_rotation_impl(X + i * len, len, 1, c, s);
  124. }
  125. }
  126. }
  127. static inline uint32_t celt_extract_collapse_mask(const int *iy, uint32_t N, uint32_t B)
  128. {
  129. int i, j, N0 = N / B;
  130. uint32_t collapse_mask = 0;
  131. if (B <= 1)
  132. return 1;
  133. for (i = 0; i < B; i++)
  134. for (j = 0; j < N0; j++)
  135. collapse_mask |= (!!iy[i*N0+j]) << i;
  136. return collapse_mask;
  137. }
  138. static inline void celt_stereo_merge(float *X, float *Y, float mid, int N)
  139. {
  140. int i;
  141. float xp = 0, side = 0;
  142. float E[2];
  143. float mid2;
  144. float gain[2];
  145. /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
  146. for (i = 0; i < N; i++) {
  147. xp += X[i] * Y[i];
  148. side += Y[i] * Y[i];
  149. }
  150. /* Compensating for the mid normalization */
  151. xp *= mid;
  152. mid2 = mid;
  153. E[0] = mid2 * mid2 + side - 2 * xp;
  154. E[1] = mid2 * mid2 + side + 2 * xp;
  155. if (E[0] < 6e-4f || E[1] < 6e-4f) {
  156. for (i = 0; i < N; i++)
  157. Y[i] = X[i];
  158. return;
  159. }
  160. gain[0] = 1.0f / sqrtf(E[0]);
  161. gain[1] = 1.0f / sqrtf(E[1]);
  162. for (i = 0; i < N; i++) {
  163. float value[2];
  164. /* Apply mid scaling (side is already scaled) */
  165. value[0] = mid * X[i];
  166. value[1] = Y[i];
  167. X[i] = gain[0] * (value[0] - value[1]);
  168. Y[i] = gain[1] * (value[0] + value[1]);
  169. }
  170. }
  171. static void celt_interleave_hadamard(float *tmp, float *X, int N0,
  172. int stride, int hadamard)
  173. {
  174. int i, j, N = N0*stride;
  175. const uint8_t *order = &ff_celt_hadamard_order[hadamard ? stride - 2 : 30];
  176. for (i = 0; i < stride; i++)
  177. for (j = 0; j < N0; j++)
  178. tmp[j*stride+i] = X[order[i]*N0+j];
  179. memcpy(X, tmp, N*sizeof(float));
  180. }
  181. static void celt_deinterleave_hadamard(float *tmp, float *X, int N0,
  182. int stride, int hadamard)
  183. {
  184. int i, j, N = N0*stride;
  185. const uint8_t *order = &ff_celt_hadamard_order[hadamard ? stride - 2 : 30];
  186. for (i = 0; i < stride; i++)
  187. for (j = 0; j < N0; j++)
  188. tmp[order[i]*N0+j] = X[j*stride+i];
  189. memcpy(X, tmp, N*sizeof(float));
  190. }
  191. static void celt_haar1(float *X, int N0, int stride)
  192. {
  193. int i, j;
  194. N0 >>= 1;
  195. for (i = 0; i < stride; i++) {
  196. for (j = 0; j < N0; j++) {
  197. float x0 = X[stride * (2 * j + 0) + i];
  198. float x1 = X[stride * (2 * j + 1) + i];
  199. X[stride * (2 * j + 0) + i] = (x0 + x1) * M_SQRT1_2;
  200. X[stride * (2 * j + 1) + i] = (x0 - x1) * M_SQRT1_2;
  201. }
  202. }
  203. }
  204. static inline int celt_compute_qn(int N, int b, int offset, int pulse_cap,
  205. int stereo)
  206. {
  207. int qn, qb;
  208. int N2 = 2 * N - 1;
  209. if (stereo && N == 2)
  210. N2--;
  211. /* The upper limit ensures that in a stereo split with itheta==16384, we'll
  212. * always have enough bits left over to code at least one pulse in the
  213. * side; otherwise it would collapse, since it doesn't get folded. */
  214. qb = FFMIN3(b - pulse_cap - (4 << 3), (b + N2 * offset) / N2, 8 << 3);
  215. qn = (qb < (1 << 3 >> 1)) ? 1 : ((ff_celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1;
  216. return qn;
  217. }
  218. /* Convert the quantized vector to an index */
  219. static inline uint32_t celt_icwrsi(uint32_t N, uint32_t K, const int *y)
  220. {
  221. int i, idx = 0, sum = 0;
  222. for (i = N - 1; i >= 0; i--) {
  223. const uint32_t i_s = CELT_PVQ_U(N - i, sum + FFABS(y[i]) + 1);
  224. idx += CELT_PVQ_U(N - i, sum) + (y[i] < 0)*i_s;
  225. sum += FFABS(y[i]);
  226. }
  227. return idx;
  228. }
  229. // this code was adapted from libopus
  230. static inline uint64_t celt_cwrsi(uint32_t N, uint32_t K, uint32_t i, int *y)
  231. {
  232. uint64_t norm = 0;
  233. uint32_t q, p;
  234. int s, val;
  235. int k0;
  236. while (N > 2) {
  237. /*Lots of pulses case:*/
  238. if (K >= N) {
  239. const uint32_t *row = ff_celt_pvq_u_row[N];
  240. /* Are the pulses in this dimension negative? */
  241. p = row[K + 1];
  242. s = -(i >= p);
  243. i -= p & s;
  244. /*Count how many pulses were placed in this dimension.*/
  245. k0 = K;
  246. q = row[N];
  247. if (q > i) {
  248. K = N;
  249. do {
  250. p = ff_celt_pvq_u_row[--K][N];
  251. } while (p > i);
  252. } else
  253. for (p = row[K]; p > i; p = row[K])
  254. K--;
  255. i -= p;
  256. val = (k0 - K + s) ^ s;
  257. norm += val * val;
  258. *y++ = val;
  259. } else { /*Lots of dimensions case:*/
  260. /*Are there any pulses in this dimension at all?*/
  261. p = ff_celt_pvq_u_row[K ][N];
  262. q = ff_celt_pvq_u_row[K + 1][N];
  263. if (p <= i && i < q) {
  264. i -= p;
  265. *y++ = 0;
  266. } else {
  267. /*Are the pulses in this dimension negative?*/
  268. s = -(i >= q);
  269. i -= q & s;
  270. /*Count how many pulses were placed in this dimension.*/
  271. k0 = K;
  272. do p = ff_celt_pvq_u_row[--K][N];
  273. while (p > i);
  274. i -= p;
  275. val = (k0 - K + s) ^ s;
  276. norm += val * val;
  277. *y++ = val;
  278. }
  279. }
  280. N--;
  281. }
  282. /* N == 2 */
  283. p = 2 * K + 1;
  284. s = -(i >= p);
  285. i -= p & s;
  286. k0 = K;
  287. K = (i + 1) / 2;
  288. if (K)
  289. i -= 2 * K - 1;
  290. val = (k0 - K + s) ^ s;
  291. norm += val * val;
  292. *y++ = val;
  293. /* N==1 */
  294. s = -i;
  295. val = (K + s) ^ s;
  296. norm += val * val;
  297. *y = val;
  298. return norm;
  299. }
  300. static inline void celt_encode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K)
  301. {
  302. ff_opus_rc_enc_uint(rc, celt_icwrsi(N, K, y), CELT_PVQ_V(N, K));
  303. }
  304. static inline float celt_decode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K)
  305. {
  306. const uint32_t idx = ff_opus_rc_dec_uint(rc, CELT_PVQ_V(N, K));
  307. return celt_cwrsi(N, K, idx, y);
  308. }
  309. /*
  310. * Faster than libopus's search, operates entirely in the signed domain.
  311. * Slightly worse/better depending on N, K and the input vector.
  312. */
  313. static float ppp_pvq_search_c(float *X, int *y, int K, int N)
  314. {
  315. int i, y_norm = 0;
  316. float res = 0.0f, xy_norm = 0.0f;
  317. for (i = 0; i < N; i++)
  318. res += FFABS(X[i]);
  319. res = K/(res + FLT_EPSILON);
  320. for (i = 0; i < N; i++) {
  321. y[i] = lrintf(res*X[i]);
  322. y_norm += y[i]*y[i];
  323. xy_norm += y[i]*X[i];
  324. K -= FFABS(y[i]);
  325. }
  326. while (K) {
  327. int max_idx = 0, phase = FFSIGN(K);
  328. float max_num = 0.0f;
  329. float max_den = 1.0f;
  330. y_norm += 1.0f;
  331. for (i = 0; i < N; i++) {
  332. /* If the sum has been overshot and the best place has 0 pulses allocated
  333. * to it, attempting to decrease it further will actually increase the
  334. * sum. Prevent this by disregarding any 0 positions when decrementing. */
  335. const int ca = 1 ^ ((y[i] == 0) & (phase < 0));
  336. const int y_new = y_norm + 2*phase*FFABS(y[i]);
  337. float xy_new = xy_norm + 1*phase*FFABS(X[i]);
  338. xy_new = xy_new * xy_new;
  339. if (ca && (max_den*xy_new) > (y_new*max_num)) {
  340. max_den = y_new;
  341. max_num = xy_new;
  342. max_idx = i;
  343. }
  344. }
  345. K -= phase;
  346. phase *= FFSIGN(X[max_idx]);
  347. xy_norm += 1*phase*X[max_idx];
  348. y_norm += 2*phase*y[max_idx];
  349. y[max_idx] += phase;
  350. }
  351. return (float)y_norm;
  352. }
  353. static uint32_t celt_alg_quant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K,
  354. enum CeltSpread spread, uint32_t blocks, float gain,
  355. CeltPVQ *pvq)
  356. {
  357. int *y = pvq->qcoeff;
  358. celt_exp_rotation(X, N, blocks, K, spread, 1);
  359. gain /= sqrtf(pvq->pvq_search(X, y, K, N));
  360. celt_encode_pulses(rc, y, N, K);
  361. celt_normalize_residual(y, X, N, gain);
  362. celt_exp_rotation(X, N, blocks, K, spread, 0);
  363. return celt_extract_collapse_mask(y, N, blocks);
  364. }
  365. /** Decode pulse vector and combine the result with the pitch vector to produce
  366. the final normalised signal in the current band. */
  367. static uint32_t celt_alg_unquant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K,
  368. enum CeltSpread spread, uint32_t blocks, float gain,
  369. CeltPVQ *pvq)
  370. {
  371. int *y = pvq->qcoeff;
  372. gain /= sqrtf(celt_decode_pulses(rc, y, N, K));
  373. celt_normalize_residual(y, X, N, gain);
  374. celt_exp_rotation(X, N, blocks, K, spread, 0);
  375. return celt_extract_collapse_mask(y, N, blocks);
  376. }
  377. static int celt_calc_theta(const float *X, const float *Y, int coupling, int N)
  378. {
  379. int i;
  380. float e[2] = { 0.0f, 0.0f };
  381. if (coupling) { /* Coupling case */
  382. for (i = 0; i < N; i++) {
  383. e[0] += (X[i] + Y[i])*(X[i] + Y[i]);
  384. e[1] += (X[i] - Y[i])*(X[i] - Y[i]);
  385. }
  386. } else {
  387. for (i = 0; i < N; i++) {
  388. e[0] += X[i]*X[i];
  389. e[1] += Y[i]*Y[i];
  390. }
  391. }
  392. return lrintf(32768.0f*atan2f(sqrtf(e[1]), sqrtf(e[0]))/M_PI);
  393. }
  394. static void celt_stereo_is_decouple(float *X, float *Y, float e_l, float e_r, int N)
  395. {
  396. int i;
  397. const float energy_n = 1.0f/(sqrtf(e_l*e_l + e_r*e_r) + FLT_EPSILON);
  398. e_l *= energy_n;
  399. e_r *= energy_n;
  400. for (i = 0; i < N; i++)
  401. X[i] = e_l*X[i] + e_r*Y[i];
  402. }
  403. static void celt_stereo_ms_decouple(float *X, float *Y, int N)
  404. {
  405. int i;
  406. for (i = 0; i < N; i++) {
  407. const float Xret = X[i];
  408. X[i] = (X[i] + Y[i])*M_SQRT1_2;
  409. Y[i] = (Y[i] - Xret)*M_SQRT1_2;
  410. }
  411. }
  412. static av_always_inline uint32_t quant_band_template(CeltPVQ *pvq, CeltFrame *f,
  413. OpusRangeCoder *rc,
  414. const int band, float *X,
  415. float *Y, int N, int b,
  416. uint32_t blocks, float *lowband,
  417. int duration, float *lowband_out,
  418. int level, float gain,
  419. float *lowband_scratch,
  420. int fill, int quant)
  421. {
  422. int i;
  423. const uint8_t *cache;
  424. int stereo = !!Y, split = stereo;
  425. int imid = 0, iside = 0;
  426. uint32_t N0 = N;
  427. int N_B = N / blocks;
  428. int N_B0 = N_B;
  429. int B0 = blocks;
  430. int time_divide = 0;
  431. int recombine = 0;
  432. int inv = 0;
  433. float mid = 0, side = 0;
  434. int longblocks = (B0 == 1);
  435. uint32_t cm = 0;
  436. if (N == 1) {
  437. float *x = X;
  438. for (i = 0; i <= stereo; i++) {
  439. int sign = 0;
  440. if (f->remaining2 >= 1 << 3) {
  441. if (quant) {
  442. sign = x[0] < 0;
  443. ff_opus_rc_put_raw(rc, sign, 1);
  444. } else {
  445. sign = ff_opus_rc_get_raw(rc, 1);
  446. }
  447. f->remaining2 -= 1 << 3;
  448. }
  449. x[0] = 1.0f - 2.0f*sign;
  450. x = Y;
  451. }
  452. if (lowband_out)
  453. lowband_out[0] = X[0];
  454. return 1;
  455. }
  456. if (!stereo && level == 0) {
  457. int tf_change = f->tf_change[band];
  458. int k;
  459. if (tf_change > 0)
  460. recombine = tf_change;
  461. /* Band recombining to increase frequency resolution */
  462. if (lowband &&
  463. (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) {
  464. for (i = 0; i < N; i++)
  465. lowband_scratch[i] = lowband[i];
  466. lowband = lowband_scratch;
  467. }
  468. for (k = 0; k < recombine; k++) {
  469. if (quant || lowband)
  470. celt_haar1(quant ? X : lowband, N >> k, 1 << k);
  471. fill = ff_celt_bit_interleave[fill & 0xF] | ff_celt_bit_interleave[fill >> 4] << 2;
  472. }
  473. blocks >>= recombine;
  474. N_B <<= recombine;
  475. /* Increasing the time resolution */
  476. while ((N_B & 1) == 0 && tf_change < 0) {
  477. if (quant || lowband)
  478. celt_haar1(quant ? X : lowband, N_B, blocks);
  479. fill |= fill << blocks;
  480. blocks <<= 1;
  481. N_B >>= 1;
  482. time_divide++;
  483. tf_change++;
  484. }
  485. B0 = blocks;
  486. N_B0 = N_B;
  487. /* Reorganize the samples in time order instead of frequency order */
  488. if (B0 > 1 && (quant || lowband))
  489. celt_deinterleave_hadamard(pvq->hadamard_tmp, quant ? X : lowband,
  490. N_B >> recombine, B0 << recombine,
  491. longblocks);
  492. }
  493. /* If we need 1.5 more bit than we can produce, split the band in two. */
  494. cache = ff_celt_cache_bits +
  495. ff_celt_cache_index[(duration + 1) * CELT_MAX_BANDS + band];
  496. if (!stereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) {
  497. N >>= 1;
  498. Y = X + N;
  499. split = 1;
  500. duration -= 1;
  501. if (blocks == 1)
  502. fill = (fill & 1) | (fill << 1);
  503. blocks = (blocks + 1) >> 1;
  504. }
  505. if (split) {
  506. int qn;
  507. int itheta = quant ? celt_calc_theta(X, Y, stereo, N) : 0;
  508. int mbits, sbits, delta;
  509. int qalloc;
  510. int pulse_cap;
  511. int offset;
  512. int orig_fill;
  513. int tell;
  514. /* Decide on the resolution to give to the split parameter theta */
  515. pulse_cap = ff_celt_log_freq_range[band] + duration * 8;
  516. offset = (pulse_cap >> 1) - (stereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE :
  517. CELT_QTHETA_OFFSET);
  518. qn = (stereo && band >= f->intensity_stereo) ? 1 :
  519. celt_compute_qn(N, b, offset, pulse_cap, stereo);
  520. tell = opus_rc_tell_frac(rc);
  521. if (qn != 1) {
  522. if (quant)
  523. itheta = (itheta*qn + 8192) >> 14;
  524. /* Entropy coding of the angle. We use a uniform pdf for the
  525. * time split, a step for stereo, and a triangular one for the rest. */
  526. if (quant) {
  527. if (stereo && N > 2)
  528. ff_opus_rc_enc_uint_step(rc, itheta, qn / 2);
  529. else if (stereo || B0 > 1)
  530. ff_opus_rc_enc_uint(rc, itheta, qn + 1);
  531. else
  532. ff_opus_rc_enc_uint_tri(rc, itheta, qn);
  533. itheta = itheta * 16384 / qn;
  534. if (stereo) {
  535. if (itheta == 0)
  536. celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band],
  537. f->block[1].lin_energy[band], N);
  538. else
  539. celt_stereo_ms_decouple(X, Y, N);
  540. }
  541. } else {
  542. if (stereo && N > 2)
  543. itheta = ff_opus_rc_dec_uint_step(rc, qn / 2);
  544. else if (stereo || B0 > 1)
  545. itheta = ff_opus_rc_dec_uint(rc, qn+1);
  546. else
  547. itheta = ff_opus_rc_dec_uint_tri(rc, qn);
  548. itheta = itheta * 16384 / qn;
  549. }
  550. } else if (stereo) {
  551. if (quant) {
  552. inv = itheta > 8192;
  553. if (inv) {
  554. for (i = 0; i < N; i++)
  555. Y[i] *= -1;
  556. }
  557. celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band],
  558. f->block[1].lin_energy[band], N);
  559. if (b > 2 << 3 && f->remaining2 > 2 << 3) {
  560. ff_opus_rc_enc_log(rc, inv, 2);
  561. } else {
  562. inv = 0;
  563. }
  564. } else {
  565. inv = (b > 2 << 3 && f->remaining2 > 2 << 3) ? ff_opus_rc_dec_log(rc, 2) : 0;
  566. inv = f->apply_phase_inv ? inv : 0;
  567. }
  568. itheta = 0;
  569. }
  570. qalloc = opus_rc_tell_frac(rc) - tell;
  571. b -= qalloc;
  572. orig_fill = fill;
  573. if (itheta == 0) {
  574. imid = 32767;
  575. iside = 0;
  576. fill = av_mod_uintp2(fill, blocks);
  577. delta = -16384;
  578. } else if (itheta == 16384) {
  579. imid = 0;
  580. iside = 32767;
  581. fill &= ((1 << blocks) - 1) << blocks;
  582. delta = 16384;
  583. } else {
  584. imid = celt_cos(itheta);
  585. iside = celt_cos(16384-itheta);
  586. /* This is the mid vs side allocation that minimizes squared error
  587. in that band. */
  588. delta = ROUND_MUL16((N - 1) << 7, celt_log2tan(iside, imid));
  589. }
  590. mid = imid / 32768.0f;
  591. side = iside / 32768.0f;
  592. /* This is a special case for N=2 that only works for stereo and takes
  593. advantage of the fact that mid and side are orthogonal to encode
  594. the side with just one bit. */
  595. if (N == 2 && stereo) {
  596. int c;
  597. int sign = 0;
  598. float tmp;
  599. float *x2, *y2;
  600. mbits = b;
  601. /* Only need one bit for the side */
  602. sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0;
  603. mbits -= sbits;
  604. c = (itheta > 8192);
  605. f->remaining2 -= qalloc+sbits;
  606. x2 = c ? Y : X;
  607. y2 = c ? X : Y;
  608. if (sbits) {
  609. if (quant) {
  610. sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
  611. ff_opus_rc_put_raw(rc, sign, 1);
  612. } else {
  613. sign = ff_opus_rc_get_raw(rc, 1);
  614. }
  615. }
  616. sign = 1 - 2 * sign;
  617. /* We use orig_fill here because we want to fold the side, but if
  618. itheta==16384, we'll have cleared the low bits of fill. */
  619. cm = pvq->quant_band(pvq, f, rc, band, x2, NULL, N, mbits, blocks, lowband, duration,
  620. lowband_out, level, gain, lowband_scratch, orig_fill);
  621. /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
  622. and there's no need to worry about mixing with the other channel. */
  623. y2[0] = -sign * x2[1];
  624. y2[1] = sign * x2[0];
  625. X[0] *= mid;
  626. X[1] *= mid;
  627. Y[0] *= side;
  628. Y[1] *= side;
  629. tmp = X[0];
  630. X[0] = tmp - Y[0];
  631. Y[0] = tmp + Y[0];
  632. tmp = X[1];
  633. X[1] = tmp - Y[1];
  634. Y[1] = tmp + Y[1];
  635. } else {
  636. /* "Normal" split code */
  637. float *next_lowband2 = NULL;
  638. float *next_lowband_out1 = NULL;
  639. int next_level = 0;
  640. int rebalance;
  641. uint32_t cmt;
  642. /* Give more bits to low-energy MDCTs than they would
  643. * otherwise deserve */
  644. if (B0 > 1 && !stereo && (itheta & 0x3fff)) {
  645. if (itheta > 8192)
  646. /* Rough approximation for pre-echo masking */
  647. delta -= delta >> (4 - duration);
  648. else
  649. /* Corresponds to a forward-masking slope of
  650. * 1.5 dB per 10 ms */
  651. delta = FFMIN(0, delta + (N << 3 >> (5 - duration)));
  652. }
  653. mbits = av_clip((b - delta) / 2, 0, b);
  654. sbits = b - mbits;
  655. f->remaining2 -= qalloc;
  656. if (lowband && !stereo)
  657. next_lowband2 = lowband + N; /* >32-bit split case */
  658. /* Only stereo needs to pass on lowband_out.
  659. * Otherwise, it's handled at the end */
  660. if (stereo)
  661. next_lowband_out1 = lowband_out;
  662. else
  663. next_level = level + 1;
  664. rebalance = f->remaining2;
  665. if (mbits >= sbits) {
  666. /* In stereo mode, we do not apply a scaling to the mid
  667. * because we need the normalized mid for folding later */
  668. cm = pvq->quant_band(pvq, f, rc, band, X, NULL, N, mbits, blocks,
  669. lowband, duration, next_lowband_out1, next_level,
  670. stereo ? 1.0f : (gain * mid), lowband_scratch, fill);
  671. rebalance = mbits - (rebalance - f->remaining2);
  672. if (rebalance > 3 << 3 && itheta != 0)
  673. sbits += rebalance - (3 << 3);
  674. /* For a stereo split, the high bits of fill are always zero,
  675. * so no folding will be done to the side. */
  676. cmt = pvq->quant_band(pvq, f, rc, band, Y, NULL, N, sbits, blocks,
  677. next_lowband2, duration, NULL, next_level,
  678. gain * side, NULL, fill >> blocks);
  679. cm |= cmt << ((B0 >> 1) & (stereo - 1));
  680. } else {
  681. /* For a stereo split, the high bits of fill are always zero,
  682. * so no folding will be done to the side. */
  683. cm = pvq->quant_band(pvq, f, rc, band, Y, NULL, N, sbits, blocks,
  684. next_lowband2, duration, NULL, next_level,
  685. gain * side, NULL, fill >> blocks);
  686. cm <<= ((B0 >> 1) & (stereo - 1));
  687. rebalance = sbits - (rebalance - f->remaining2);
  688. if (rebalance > 3 << 3 && itheta != 16384)
  689. mbits += rebalance - (3 << 3);
  690. /* In stereo mode, we do not apply a scaling to the mid because
  691. * we need the normalized mid for folding later */
  692. cm |= pvq->quant_band(pvq, f, rc, band, X, NULL, N, mbits, blocks,
  693. lowband, duration, next_lowband_out1, next_level,
  694. stereo ? 1.0f : (gain * mid), lowband_scratch, fill);
  695. }
  696. }
  697. } else {
  698. /* This is the basic no-split case */
  699. uint32_t q = celt_bits2pulses(cache, b);
  700. uint32_t curr_bits = celt_pulses2bits(cache, q);
  701. f->remaining2 -= curr_bits;
  702. /* Ensures we can never bust the budget */
  703. while (f->remaining2 < 0 && q > 0) {
  704. f->remaining2 += curr_bits;
  705. curr_bits = celt_pulses2bits(cache, --q);
  706. f->remaining2 -= curr_bits;
  707. }
  708. if (q != 0) {
  709. /* Finally do the actual (de)quantization */
  710. if (quant) {
  711. cm = celt_alg_quant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
  712. f->spread, blocks, gain, pvq);
  713. } else {
  714. cm = celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
  715. f->spread, blocks, gain, pvq);
  716. }
  717. } else {
  718. /* If there's no pulse, fill the band anyway */
  719. uint32_t cm_mask = (1 << blocks) - 1;
  720. fill &= cm_mask;
  721. if (fill) {
  722. if (!lowband) {
  723. /* Noise */
  724. for (i = 0; i < N; i++)
  725. X[i] = (((int32_t)celt_rng(f)) >> 20);
  726. cm = cm_mask;
  727. } else {
  728. /* Folded spectrum */
  729. for (i = 0; i < N; i++) {
  730. /* About 48 dB below the "normal" folding level */
  731. X[i] = lowband[i] + (((celt_rng(f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256);
  732. }
  733. cm = fill;
  734. }
  735. celt_renormalize_vector(X, N, gain);
  736. } else {
  737. memset(X, 0, N*sizeof(float));
  738. }
  739. }
  740. }
  741. /* This code is used by the decoder and by the resynthesis-enabled encoder */
  742. if (stereo) {
  743. if (N > 2)
  744. celt_stereo_merge(X, Y, mid, N);
  745. if (inv) {
  746. for (i = 0; i < N; i++)
  747. Y[i] *= -1;
  748. }
  749. } else if (level == 0) {
  750. int k;
  751. /* Undo the sample reorganization going from time order to frequency order */
  752. if (B0 > 1)
  753. celt_interleave_hadamard(pvq->hadamard_tmp, X, N_B >> recombine,
  754. B0 << recombine, longblocks);
  755. /* Undo time-freq changes that we did earlier */
  756. N_B = N_B0;
  757. blocks = B0;
  758. for (k = 0; k < time_divide; k++) {
  759. blocks >>= 1;
  760. N_B <<= 1;
  761. cm |= cm >> blocks;
  762. celt_haar1(X, N_B, blocks);
  763. }
  764. for (k = 0; k < recombine; k++) {
  765. cm = ff_celt_bit_deinterleave[cm];
  766. celt_haar1(X, N0>>k, 1<<k);
  767. }
  768. blocks <<= recombine;
  769. /* Scale output for later folding */
  770. if (lowband_out) {
  771. float n = sqrtf(N0);
  772. for (i = 0; i < N0; i++)
  773. lowband_out[i] = n * X[i];
  774. }
  775. cm = av_mod_uintp2(cm, blocks);
  776. }
  777. return cm;
  778. }
  779. static QUANT_FN(pvq_decode_band)
  780. {
  781. #if CONFIG_OPUS_DECODER
  782. return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration,
  783. lowband_out, level, gain, lowband_scratch, fill, 0);
  784. #else
  785. return 0;
  786. #endif
  787. }
  788. static QUANT_FN(pvq_encode_band)
  789. {
  790. #if CONFIG_OPUS_ENCODER
  791. return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration,
  792. lowband_out, level, gain, lowband_scratch, fill, 1);
  793. #else
  794. return 0;
  795. #endif
  796. }
  797. int av_cold ff_celt_pvq_init(CeltPVQ **pvq, int encode)
  798. {
  799. CeltPVQ *s = av_malloc(sizeof(CeltPVQ));
  800. if (!s)
  801. return AVERROR(ENOMEM);
  802. s->pvq_search = ppp_pvq_search_c;
  803. s->quant_band = encode ? pvq_encode_band : pvq_decode_band;
  804. if (ARCH_X86)
  805. ff_opus_dsp_init_x86(s);
  806. *pvq = s;
  807. return 0;
  808. }
  809. void av_cold ff_celt_pvq_uninit(CeltPVQ **pvq)
  810. {
  811. av_freep(pvq);
  812. }