You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

940 lines
30KB

  1. /*
  2. * Copyright (c) 2012 Andrew D'Addesio
  3. * Copyright (c) 2013-2014 Mozilla Corporation
  4. * Copyright (c) 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Opus CELT decoder
  25. */
  26. #include "opus_celt.h"
  27. #include "opustab.h"
  28. #include "opus_pvq.h"
  29. /* Use the 2D z-transform to apply prediction in both the time domain (alpha)
  30. * and the frequency domain (beta) */
  31. static void celt_decode_coarse_energy(CeltFrame *f, OpusRangeCoder *rc)
  32. {
  33. int i, j;
  34. float prev[2] = { 0 };
  35. float alpha = ff_celt_alpha_coef[f->size];
  36. float beta = ff_celt_beta_coef[f->size];
  37. const uint8_t *model = ff_celt_coarse_energy_dist[f->size][0];
  38. /* intra frame */
  39. if (opus_rc_tell(rc) + 3 <= f->framebits && ff_opus_rc_dec_log(rc, 3)) {
  40. alpha = 0.0f;
  41. beta = 1.0f - (4915.0f/32768.0f);
  42. model = ff_celt_coarse_energy_dist[f->size][1];
  43. }
  44. for (i = 0; i < CELT_MAX_BANDS; i++) {
  45. for (j = 0; j < f->channels; j++) {
  46. CeltBlock *block = &f->block[j];
  47. float value;
  48. int available;
  49. if (i < f->start_band || i >= f->end_band) {
  50. block->energy[i] = 0.0;
  51. continue;
  52. }
  53. available = f->framebits - opus_rc_tell(rc);
  54. if (available >= 15) {
  55. /* decode using a Laplace distribution */
  56. int k = FFMIN(i, 20) << 1;
  57. value = ff_opus_rc_dec_laplace(rc, model[k] << 7, model[k+1] << 6);
  58. } else if (available >= 2) {
  59. int x = ff_opus_rc_dec_cdf(rc, ff_celt_model_energy_small);
  60. value = (x>>1) ^ -(x&1);
  61. } else if (available >= 1) {
  62. value = -(float)ff_opus_rc_dec_log(rc, 1);
  63. } else value = -1;
  64. block->energy[i] = FFMAX(-9.0f, block->energy[i]) * alpha + prev[j] + value;
  65. prev[j] += beta * value;
  66. }
  67. }
  68. }
  69. static void celt_decode_fine_energy(CeltFrame *f, OpusRangeCoder *rc)
  70. {
  71. int i;
  72. for (i = f->start_band; i < f->end_band; i++) {
  73. int j;
  74. if (!f->fine_bits[i])
  75. continue;
  76. for (j = 0; j < f->channels; j++) {
  77. CeltBlock *block = &f->block[j];
  78. int q2;
  79. float offset;
  80. q2 = ff_opus_rc_get_raw(rc, f->fine_bits[i]);
  81. offset = (q2 + 0.5f) * (1 << (14 - f->fine_bits[i])) / 16384.0f - 0.5f;
  82. block->energy[i] += offset;
  83. }
  84. }
  85. }
  86. static void celt_decode_final_energy(CeltFrame *f, OpusRangeCoder *rc)
  87. {
  88. int priority, i, j;
  89. int bits_left = f->framebits - opus_rc_tell(rc);
  90. for (priority = 0; priority < 2; priority++) {
  91. for (i = f->start_band; i < f->end_band && bits_left >= f->channels; i++) {
  92. if (f->fine_priority[i] != priority || f->fine_bits[i] >= CELT_MAX_FINE_BITS)
  93. continue;
  94. for (j = 0; j < f->channels; j++) {
  95. int q2;
  96. float offset;
  97. q2 = ff_opus_rc_get_raw(rc, 1);
  98. offset = (q2 - 0.5f) * (1 << (14 - f->fine_bits[i] - 1)) / 16384.0f;
  99. f->block[j].energy[i] += offset;
  100. bits_left--;
  101. }
  102. }
  103. }
  104. }
  105. static void celt_decode_tf_changes(CeltFrame *f, OpusRangeCoder *rc)
  106. {
  107. int i, diff = 0, tf_select = 0, tf_changed = 0, tf_select_bit;
  108. int consumed, bits = f->transient ? 2 : 4;
  109. consumed = opus_rc_tell(rc);
  110. tf_select_bit = (f->size != 0 && consumed+bits+1 <= f->framebits);
  111. for (i = f->start_band; i < f->end_band; i++) {
  112. if (consumed+bits+tf_select_bit <= f->framebits) {
  113. diff ^= ff_opus_rc_dec_log(rc, bits);
  114. consumed = opus_rc_tell(rc);
  115. tf_changed |= diff;
  116. }
  117. f->tf_change[i] = diff;
  118. bits = f->transient ? 4 : 5;
  119. }
  120. if (tf_select_bit && ff_celt_tf_select[f->size][f->transient][0][tf_changed] !=
  121. ff_celt_tf_select[f->size][f->transient][1][tf_changed])
  122. tf_select = ff_opus_rc_dec_log(rc, 1);
  123. for (i = f->start_band; i < f->end_band; i++) {
  124. f->tf_change[i] = ff_celt_tf_select[f->size][f->transient][tf_select][f->tf_change[i]];
  125. }
  126. }
  127. static void celt_decode_allocation(CeltFrame *f, OpusRangeCoder *rc)
  128. {
  129. // approx. maximum bit allocation for each band before boost/trim
  130. int cap[CELT_MAX_BANDS];
  131. int boost[CELT_MAX_BANDS];
  132. int threshold[CELT_MAX_BANDS];
  133. int bits1[CELT_MAX_BANDS];
  134. int bits2[CELT_MAX_BANDS];
  135. int trim_offset[CELT_MAX_BANDS];
  136. int skip_start_band = f->start_band;
  137. int dynalloc = 6;
  138. int alloctrim = 5;
  139. int extrabits = 0;
  140. int skip_bit = 0;
  141. int intensity_stereo_bit = 0;
  142. int dual_stereo_bit = 0;
  143. int remaining, bandbits;
  144. int low, high, total, done;
  145. int totalbits;
  146. int consumed;
  147. int i, j;
  148. consumed = opus_rc_tell(rc);
  149. /* obtain spread flag */
  150. f->spread = CELT_SPREAD_NORMAL;
  151. if (consumed + 4 <= f->framebits)
  152. f->spread = ff_opus_rc_dec_cdf(rc, ff_celt_model_spread);
  153. /* generate static allocation caps */
  154. for (i = 0; i < CELT_MAX_BANDS; i++) {
  155. cap[i] = (ff_celt_static_caps[f->size][f->channels - 1][i] + 64)
  156. * ff_celt_freq_range[i] << (f->channels - 1) << f->size >> 2;
  157. }
  158. /* obtain band boost */
  159. totalbits = f->framebits << 3; // convert to 1/8 bits
  160. consumed = opus_rc_tell_frac(rc);
  161. for (i = f->start_band; i < f->end_band; i++) {
  162. int quanta, band_dynalloc;
  163. boost[i] = 0;
  164. quanta = ff_celt_freq_range[i] << (f->channels - 1) << f->size;
  165. quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
  166. band_dynalloc = dynalloc;
  167. while (consumed + (band_dynalloc<<3) < totalbits && boost[i] < cap[i]) {
  168. int add = ff_opus_rc_dec_log(rc, band_dynalloc);
  169. consumed = opus_rc_tell_frac(rc);
  170. if (!add)
  171. break;
  172. boost[i] += quanta;
  173. totalbits -= quanta;
  174. band_dynalloc = 1;
  175. }
  176. /* dynalloc is more likely to occur if it's already been used for earlier bands */
  177. if (boost[i])
  178. dynalloc = FFMAX(2, dynalloc - 1);
  179. }
  180. /* obtain allocation trim */
  181. if (consumed + (6 << 3) <= totalbits)
  182. alloctrim = ff_opus_rc_dec_cdf(rc, ff_celt_model_alloc_trim);
  183. /* anti-collapse bit reservation */
  184. totalbits = (f->framebits << 3) - opus_rc_tell_frac(rc) - 1;
  185. f->anticollapse_needed = 0;
  186. if (f->blocks > 1 && f->size >= 2 &&
  187. totalbits >= ((f->size + 2) << 3))
  188. f->anticollapse_needed = 1 << 3;
  189. totalbits -= f->anticollapse_needed;
  190. /* band skip bit reservation */
  191. if (totalbits >= 1 << 3)
  192. skip_bit = 1 << 3;
  193. totalbits -= skip_bit;
  194. /* intensity/dual stereo bit reservation */
  195. if (f->channels == 2) {
  196. intensity_stereo_bit = ff_celt_log2_frac[f->end_band - f->start_band];
  197. if (intensity_stereo_bit <= totalbits) {
  198. totalbits -= intensity_stereo_bit;
  199. if (totalbits >= 1 << 3) {
  200. dual_stereo_bit = 1 << 3;
  201. totalbits -= 1 << 3;
  202. }
  203. } else
  204. intensity_stereo_bit = 0;
  205. }
  206. for (i = f->start_band; i < f->end_band; i++) {
  207. int trim = alloctrim - 5 - f->size;
  208. int band = ff_celt_freq_range[i] * (f->end_band - i - 1);
  209. int duration = f->size + 3;
  210. int scale = duration + f->channels - 1;
  211. /* PVQ minimum allocation threshold, below this value the band is
  212. * skipped */
  213. threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4,
  214. f->channels << 3);
  215. trim_offset[i] = trim * (band << scale) >> 6;
  216. if (ff_celt_freq_range[i] << f->size == 1)
  217. trim_offset[i] -= f->channels << 3;
  218. }
  219. /* bisection */
  220. low = 1;
  221. high = CELT_VECTORS - 1;
  222. while (low <= high) {
  223. int center = (low + high) >> 1;
  224. done = total = 0;
  225. for (i = f->end_band - 1; i >= f->start_band; i--) {
  226. bandbits = ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]
  227. << (f->channels - 1) << f->size >> 2;
  228. if (bandbits)
  229. bandbits = FFMAX(0, bandbits + trim_offset[i]);
  230. bandbits += boost[i];
  231. if (bandbits >= threshold[i] || done) {
  232. done = 1;
  233. total += FFMIN(bandbits, cap[i]);
  234. } else if (bandbits >= f->channels << 3)
  235. total += f->channels << 3;
  236. }
  237. if (total > totalbits)
  238. high = center - 1;
  239. else
  240. low = center + 1;
  241. }
  242. high = low--;
  243. for (i = f->start_band; i < f->end_band; i++) {
  244. bits1[i] = ff_celt_freq_range[i] * ff_celt_static_alloc[low][i]
  245. << (f->channels - 1) << f->size >> 2;
  246. bits2[i] = high >= CELT_VECTORS ? cap[i] :
  247. ff_celt_freq_range[i] * ff_celt_static_alloc[high][i]
  248. << (f->channels - 1) << f->size >> 2;
  249. if (bits1[i])
  250. bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]);
  251. if (bits2[i])
  252. bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]);
  253. if (low)
  254. bits1[i] += boost[i];
  255. bits2[i] += boost[i];
  256. if (boost[i])
  257. skip_start_band = i;
  258. bits2[i] = FFMAX(0, bits2[i] - bits1[i]);
  259. }
  260. /* bisection */
  261. low = 0;
  262. high = 1 << CELT_ALLOC_STEPS;
  263. for (i = 0; i < CELT_ALLOC_STEPS; i++) {
  264. int center = (low + high) >> 1;
  265. done = total = 0;
  266. for (j = f->end_band - 1; j >= f->start_band; j--) {
  267. bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
  268. if (bandbits >= threshold[j] || done) {
  269. done = 1;
  270. total += FFMIN(bandbits, cap[j]);
  271. } else if (bandbits >= f->channels << 3)
  272. total += f->channels << 3;
  273. }
  274. if (total > totalbits)
  275. high = center;
  276. else
  277. low = center;
  278. }
  279. done = total = 0;
  280. for (i = f->end_band - 1; i >= f->start_band; i--) {
  281. bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
  282. if (bandbits >= threshold[i] || done)
  283. done = 1;
  284. else
  285. bandbits = (bandbits >= f->channels << 3) ?
  286. f->channels << 3 : 0;
  287. bandbits = FFMIN(bandbits, cap[i]);
  288. f->pulses[i] = bandbits;
  289. total += bandbits;
  290. }
  291. /* band skipping */
  292. for (f->coded_bands = f->end_band; ; f->coded_bands--) {
  293. int allocation;
  294. j = f->coded_bands - 1;
  295. if (j == skip_start_band) {
  296. /* all remaining bands are not skipped */
  297. totalbits += skip_bit;
  298. break;
  299. }
  300. /* determine the number of bits available for coding "do not skip" markers */
  301. remaining = totalbits - total;
  302. bandbits = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
  303. remaining -= bandbits * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[f->start_band]);
  304. allocation = f->pulses[j] + bandbits * ff_celt_freq_range[j]
  305. + FFMAX(0, remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[f->start_band]));
  306. /* a "do not skip" marker is only coded if the allocation is
  307. above the chosen threshold */
  308. if (allocation >= FFMAX(threshold[j], (f->channels + 1) <<3 )) {
  309. if (ff_opus_rc_dec_log(rc, 1))
  310. break;
  311. total += 1 << 3;
  312. allocation -= 1 << 3;
  313. }
  314. /* the band is skipped, so reclaim its bits */
  315. total -= f->pulses[j];
  316. if (intensity_stereo_bit) {
  317. total -= intensity_stereo_bit;
  318. intensity_stereo_bit = ff_celt_log2_frac[j - f->start_band];
  319. total += intensity_stereo_bit;
  320. }
  321. total += f->pulses[j] = (allocation >= f->channels << 3) ?
  322. f->channels << 3 : 0;
  323. }
  324. /* obtain stereo flags */
  325. f->intensity_stereo = 0;
  326. f->dual_stereo = 0;
  327. if (intensity_stereo_bit)
  328. f->intensity_stereo = f->start_band +
  329. ff_opus_rc_dec_uint(rc, f->coded_bands + 1 - f->start_band);
  330. if (f->intensity_stereo <= f->start_band)
  331. totalbits += dual_stereo_bit; /* no intensity stereo means no dual stereo */
  332. else if (dual_stereo_bit)
  333. f->dual_stereo = ff_opus_rc_dec_log(rc, 1);
  334. /* supply the remaining bits in this frame to lower bands */
  335. remaining = totalbits - total;
  336. bandbits = remaining / (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
  337. remaining -= bandbits * (ff_celt_freq_bands[f->coded_bands] - ff_celt_freq_bands[f->start_band]);
  338. for (i = f->start_band; i < f->coded_bands; i++) {
  339. int bits = FFMIN(remaining, ff_celt_freq_range[i]);
  340. f->pulses[i] += bits + bandbits * ff_celt_freq_range[i];
  341. remaining -= bits;
  342. }
  343. for (i = f->start_band; i < f->coded_bands; i++) {
  344. int N = ff_celt_freq_range[i] << f->size;
  345. int prev_extra = extrabits;
  346. f->pulses[i] += extrabits;
  347. if (N > 1) {
  348. int dof; // degrees of freedom
  349. int temp; // dof * channels * log(dof)
  350. int offset; // fine energy quantization offset, i.e.
  351. // extra bits assigned over the standard
  352. // totalbits/dof
  353. int fine_bits, max_bits;
  354. extrabits = FFMAX(0, f->pulses[i] - cap[i]);
  355. f->pulses[i] -= extrabits;
  356. /* intensity stereo makes use of an extra degree of freedom */
  357. dof = N * f->channels
  358. + (f->channels == 2 && N > 2 && !f->dual_stereo && i < f->intensity_stereo);
  359. temp = dof * (ff_celt_log_freq_range[i] + (f->size<<3));
  360. offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
  361. if (N == 2) /* dof=2 is the only case that doesn't fit the model */
  362. offset += dof<<1;
  363. /* grant an additional bias for the first and second pulses */
  364. if (f->pulses[i] + offset < 2 * (dof << 3))
  365. offset += temp >> 2;
  366. else if (f->pulses[i] + offset < 3 * (dof << 3))
  367. offset += temp >> 3;
  368. fine_bits = (f->pulses[i] + offset + (dof << 2)) / (dof << 3);
  369. max_bits = FFMIN((f->pulses[i]>>3) >> (f->channels - 1),
  370. CELT_MAX_FINE_BITS);
  371. max_bits = FFMAX(max_bits, 0);
  372. f->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
  373. /* if fine_bits was rounded down or capped,
  374. give priority for the final fine energy pass */
  375. f->fine_priority[i] = (f->fine_bits[i] * (dof<<3) >= f->pulses[i] + offset);
  376. /* the remaining bits are assigned to PVQ */
  377. f->pulses[i] -= f->fine_bits[i] << (f->channels - 1) << 3;
  378. } else {
  379. /* all bits go to fine energy except for the sign bit */
  380. extrabits = FFMAX(0, f->pulses[i] - (f->channels << 3));
  381. f->pulses[i] -= extrabits;
  382. f->fine_bits[i] = 0;
  383. f->fine_priority[i] = 1;
  384. }
  385. /* hand back a limited number of extra fine energy bits to this band */
  386. if (extrabits > 0) {
  387. int fineextra = FFMIN(extrabits >> (f->channels + 2),
  388. CELT_MAX_FINE_BITS - f->fine_bits[i]);
  389. f->fine_bits[i] += fineextra;
  390. fineextra <<= f->channels + 2;
  391. f->fine_priority[i] = (fineextra >= extrabits - prev_extra);
  392. extrabits -= fineextra;
  393. }
  394. }
  395. f->remaining = extrabits;
  396. /* skipped bands dedicate all of their bits for fine energy */
  397. for (; i < f->end_band; i++) {
  398. f->fine_bits[i] = f->pulses[i] >> (f->channels - 1) >> 3;
  399. f->pulses[i] = 0;
  400. f->fine_priority[i] = f->fine_bits[i] < 1;
  401. }
  402. }
  403. static void celt_denormalize(CeltFrame *f, CeltBlock *block, float *data)
  404. {
  405. int i, j;
  406. for (i = f->start_band; i < f->end_band; i++) {
  407. float *dst = data + (ff_celt_freq_bands[i] << f->size);
  408. float log_norm = block->energy[i] + ff_celt_mean_energy[i];
  409. float norm = exp2f(FFMIN(log_norm, 32.0f));
  410. for (j = 0; j < ff_celt_freq_range[i] << f->size; j++)
  411. dst[j] *= norm;
  412. }
  413. }
  414. static void celt_postfilter_apply_transition(CeltBlock *block, float *data)
  415. {
  416. const int T0 = block->pf_period_old;
  417. const int T1 = block->pf_period;
  418. float g00, g01, g02;
  419. float g10, g11, g12;
  420. float x0, x1, x2, x3, x4;
  421. int i;
  422. if (block->pf_gains[0] == 0.0 &&
  423. block->pf_gains_old[0] == 0.0)
  424. return;
  425. g00 = block->pf_gains_old[0];
  426. g01 = block->pf_gains_old[1];
  427. g02 = block->pf_gains_old[2];
  428. g10 = block->pf_gains[0];
  429. g11 = block->pf_gains[1];
  430. g12 = block->pf_gains[2];
  431. x1 = data[-T1 + 1];
  432. x2 = data[-T1];
  433. x3 = data[-T1 - 1];
  434. x4 = data[-T1 - 2];
  435. for (i = 0; i < CELT_OVERLAP; i++) {
  436. float w = ff_celt_window2[i];
  437. x0 = data[i - T1 + 2];
  438. data[i] += (1.0 - w) * g00 * data[i - T0] +
  439. (1.0 - w) * g01 * (data[i - T0 - 1] + data[i - T0 + 1]) +
  440. (1.0 - w) * g02 * (data[i - T0 - 2] + data[i - T0 + 2]) +
  441. w * g10 * x2 +
  442. w * g11 * (x1 + x3) +
  443. w * g12 * (x0 + x4);
  444. x4 = x3;
  445. x3 = x2;
  446. x2 = x1;
  447. x1 = x0;
  448. }
  449. }
  450. static void celt_postfilter_apply(CeltBlock *block, float *data, int len)
  451. {
  452. const int T = block->pf_period;
  453. float g0, g1, g2;
  454. float x0, x1, x2, x3, x4;
  455. int i;
  456. if (block->pf_gains[0] == 0.0 || len <= 0)
  457. return;
  458. g0 = block->pf_gains[0];
  459. g1 = block->pf_gains[1];
  460. g2 = block->pf_gains[2];
  461. x4 = data[-T - 2];
  462. x3 = data[-T - 1];
  463. x2 = data[-T];
  464. x1 = data[-T + 1];
  465. for (i = 0; i < len; i++) {
  466. x0 = data[i - T + 2];
  467. data[i] += g0 * x2 +
  468. g1 * (x1 + x3) +
  469. g2 * (x0 + x4);
  470. x4 = x3;
  471. x3 = x2;
  472. x2 = x1;
  473. x1 = x0;
  474. }
  475. }
  476. static void celt_postfilter(CeltFrame *f, CeltBlock *block)
  477. {
  478. int len = f->blocksize * f->blocks;
  479. celt_postfilter_apply_transition(block, block->buf + 1024);
  480. block->pf_period_old = block->pf_period;
  481. memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
  482. block->pf_period = block->pf_period_new;
  483. memcpy(block->pf_gains, block->pf_gains_new, sizeof(block->pf_gains));
  484. if (len > CELT_OVERLAP) {
  485. celt_postfilter_apply_transition(block, block->buf + 1024 + CELT_OVERLAP);
  486. celt_postfilter_apply(block, block->buf + 1024 + 2 * CELT_OVERLAP,
  487. len - 2 * CELT_OVERLAP);
  488. block->pf_period_old = block->pf_period;
  489. memcpy(block->pf_gains_old, block->pf_gains, sizeof(block->pf_gains));
  490. }
  491. memmove(block->buf, block->buf + len, (1024 + CELT_OVERLAP / 2) * sizeof(float));
  492. }
  493. static int parse_postfilter(CeltFrame *f, OpusRangeCoder *rc, int consumed)
  494. {
  495. int i;
  496. memset(f->block[0].pf_gains_new, 0, sizeof(f->block[0].pf_gains_new));
  497. memset(f->block[1].pf_gains_new, 0, sizeof(f->block[1].pf_gains_new));
  498. if (f->start_band == 0 && consumed + 16 <= f->framebits) {
  499. int has_postfilter = ff_opus_rc_dec_log(rc, 1);
  500. if (has_postfilter) {
  501. float gain;
  502. int tapset, octave, period;
  503. octave = ff_opus_rc_dec_uint(rc, 6);
  504. period = (16 << octave) + ff_opus_rc_get_raw(rc, 4 + octave) - 1;
  505. gain = 0.09375f * (ff_opus_rc_get_raw(rc, 3) + 1);
  506. tapset = (opus_rc_tell(rc) + 2 <= f->framebits) ?
  507. ff_opus_rc_dec_cdf(rc, ff_celt_model_tapset) : 0;
  508. for (i = 0; i < 2; i++) {
  509. CeltBlock *block = &f->block[i];
  510. block->pf_period_new = FFMAX(period, CELT_POSTFILTER_MINPERIOD);
  511. block->pf_gains_new[0] = gain * ff_celt_postfilter_taps[tapset][0];
  512. block->pf_gains_new[1] = gain * ff_celt_postfilter_taps[tapset][1];
  513. block->pf_gains_new[2] = gain * ff_celt_postfilter_taps[tapset][2];
  514. }
  515. }
  516. consumed = opus_rc_tell(rc);
  517. }
  518. return consumed;
  519. }
  520. static void process_anticollapse(CeltFrame *f, CeltBlock *block, float *X)
  521. {
  522. int i, j, k;
  523. for (i = f->start_band; i < f->end_band; i++) {
  524. int renormalize = 0;
  525. float *xptr;
  526. float prev[2];
  527. float Ediff, r;
  528. float thresh, sqrt_1;
  529. int depth;
  530. /* depth in 1/8 bits */
  531. depth = (1 + f->pulses[i]) / (ff_celt_freq_range[i] << f->size);
  532. thresh = exp2f(-1.0 - 0.125f * depth);
  533. sqrt_1 = 1.0f / sqrtf(ff_celt_freq_range[i] << f->size);
  534. xptr = X + (ff_celt_freq_bands[i] << f->size);
  535. prev[0] = block->prev_energy[0][i];
  536. prev[1] = block->prev_energy[1][i];
  537. if (f->channels == 1) {
  538. CeltBlock *block1 = &f->block[1];
  539. prev[0] = FFMAX(prev[0], block1->prev_energy[0][i]);
  540. prev[1] = FFMAX(prev[1], block1->prev_energy[1][i]);
  541. }
  542. Ediff = block->energy[i] - FFMIN(prev[0], prev[1]);
  543. Ediff = FFMAX(0, Ediff);
  544. /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
  545. short blocks don't have the same energy as long */
  546. r = exp2f(1 - Ediff);
  547. if (f->size == 3)
  548. r *= M_SQRT2;
  549. r = FFMIN(thresh, r) * sqrt_1;
  550. for (k = 0; k < 1 << f->size; k++) {
  551. /* Detect collapse */
  552. if (!(block->collapse_masks[i] & 1 << k)) {
  553. /* Fill with noise */
  554. for (j = 0; j < ff_celt_freq_range[i]; j++)
  555. xptr[(j << f->size) + k] = (celt_rng(f) & 0x8000) ? r : -r;
  556. renormalize = 1;
  557. }
  558. }
  559. /* We just added some energy, so we need to renormalize */
  560. if (renormalize)
  561. celt_renormalize_vector(xptr, ff_celt_freq_range[i] << f->size, 1.0f);
  562. }
  563. }
  564. int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc,
  565. float **output, int channels, int frame_size,
  566. int start_band, int end_band)
  567. {
  568. int i, j, downmix = 0;
  569. int consumed; // bits of entropy consumed thus far for this frame
  570. MDCT15Context *imdct;
  571. if (channels != 1 && channels != 2) {
  572. av_log(f->avctx, AV_LOG_ERROR, "Invalid number of coded channels: %d\n",
  573. channels);
  574. return AVERROR_INVALIDDATA;
  575. }
  576. if (start_band < 0 || start_band > end_band || end_band > CELT_MAX_BANDS) {
  577. av_log(f->avctx, AV_LOG_ERROR, "Invalid start/end band: %d %d\n",
  578. start_band, end_band);
  579. return AVERROR_INVALIDDATA;
  580. }
  581. f->silence = 0;
  582. f->transient = 0;
  583. f->anticollapse = 0;
  584. f->flushed = 0;
  585. f->channels = channels;
  586. f->start_band = start_band;
  587. f->end_band = end_band;
  588. f->framebits = rc->rb.bytes * 8;
  589. f->size = av_log2(frame_size / CELT_SHORT_BLOCKSIZE);
  590. if (f->size > CELT_MAX_LOG_BLOCKS ||
  591. frame_size != CELT_SHORT_BLOCKSIZE * (1 << f->size)) {
  592. av_log(f->avctx, AV_LOG_ERROR, "Invalid CELT frame size: %d\n",
  593. frame_size);
  594. return AVERROR_INVALIDDATA;
  595. }
  596. if (!f->output_channels)
  597. f->output_channels = channels;
  598. for (i = 0; i < f->channels; i++) {
  599. memset(f->block[i].coeffs, 0, sizeof(f->block[i].coeffs));
  600. memset(f->block[i].collapse_masks, 0, sizeof(f->block[i].collapse_masks));
  601. }
  602. consumed = opus_rc_tell(rc);
  603. /* obtain silence flag */
  604. if (consumed >= f->framebits)
  605. f->silence = 1;
  606. else if (consumed == 1)
  607. f->silence = ff_opus_rc_dec_log(rc, 15);
  608. if (f->silence) {
  609. consumed = f->framebits;
  610. rc->total_bits += f->framebits - opus_rc_tell(rc);
  611. }
  612. /* obtain post-filter options */
  613. consumed = parse_postfilter(f, rc, consumed);
  614. /* obtain transient flag */
  615. if (f->size != 0 && consumed+3 <= f->framebits)
  616. f->transient = ff_opus_rc_dec_log(rc, 3);
  617. f->blocks = f->transient ? 1 << f->size : 1;
  618. f->blocksize = frame_size / f->blocks;
  619. imdct = f->imdct[f->transient ? 0 : f->size];
  620. if (channels == 1) {
  621. for (i = 0; i < CELT_MAX_BANDS; i++)
  622. f->block[0].energy[i] = FFMAX(f->block[0].energy[i], f->block[1].energy[i]);
  623. }
  624. celt_decode_coarse_energy(f, rc);
  625. celt_decode_tf_changes (f, rc);
  626. celt_decode_allocation (f, rc);
  627. celt_decode_fine_energy (f, rc);
  628. ff_celt_quant_bands (f, rc);
  629. if (f->anticollapse_needed)
  630. f->anticollapse = ff_opus_rc_get_raw(rc, 1);
  631. celt_decode_final_energy(f, rc);
  632. /* apply anti-collapse processing and denormalization to
  633. * each coded channel */
  634. for (i = 0; i < f->channels; i++) {
  635. CeltBlock *block = &f->block[i];
  636. if (f->anticollapse)
  637. process_anticollapse(f, block, f->block[i].coeffs);
  638. celt_denormalize(f, block, f->block[i].coeffs);
  639. }
  640. /* stereo -> mono downmix */
  641. if (f->output_channels < f->channels) {
  642. f->dsp->vector_fmac_scalar(f->block[0].coeffs, f->block[1].coeffs, 1.0, FFALIGN(frame_size, 16));
  643. downmix = 1;
  644. } else if (f->output_channels > f->channels)
  645. memcpy(f->block[1].coeffs, f->block[0].coeffs, frame_size * sizeof(float));
  646. if (f->silence) {
  647. for (i = 0; i < 2; i++) {
  648. CeltBlock *block = &f->block[i];
  649. for (j = 0; j < FF_ARRAY_ELEMS(block->energy); j++)
  650. block->energy[j] = CELT_ENERGY_SILENCE;
  651. }
  652. memset(f->block[0].coeffs, 0, sizeof(f->block[0].coeffs));
  653. memset(f->block[1].coeffs, 0, sizeof(f->block[1].coeffs));
  654. }
  655. /* transform and output for each output channel */
  656. for (i = 0; i < f->output_channels; i++) {
  657. CeltBlock *block = &f->block[i];
  658. float m = block->emph_coeff;
  659. /* iMDCT and overlap-add */
  660. for (j = 0; j < f->blocks; j++) {
  661. float *dst = block->buf + 1024 + j * f->blocksize;
  662. imdct->imdct_half(imdct, dst + CELT_OVERLAP / 2, f->block[i].coeffs + j,
  663. f->blocks);
  664. f->dsp->vector_fmul_window(dst, dst, dst + CELT_OVERLAP / 2,
  665. ff_celt_window, CELT_OVERLAP / 2);
  666. }
  667. if (downmix)
  668. f->dsp->vector_fmul_scalar(&block->buf[1024], &block->buf[1024], 0.5f, frame_size);
  669. /* postfilter */
  670. celt_postfilter(f, block);
  671. /* deemphasis and output scaling */
  672. for (j = 0; j < frame_size; j++) {
  673. const float tmp = block->buf[1024 - frame_size + j] + m;
  674. m = tmp * CELT_EMPH_COEFF;
  675. output[i][j] = tmp;
  676. }
  677. block->emph_coeff = m;
  678. }
  679. if (channels == 1)
  680. memcpy(f->block[1].energy, f->block[0].energy, sizeof(f->block[0].energy));
  681. for (i = 0; i < 2; i++ ) {
  682. CeltBlock *block = &f->block[i];
  683. if (!f->transient) {
  684. memcpy(block->prev_energy[1], block->prev_energy[0], sizeof(block->prev_energy[0]));
  685. memcpy(block->prev_energy[0], block->energy, sizeof(block->prev_energy[0]));
  686. } else {
  687. for (j = 0; j < CELT_MAX_BANDS; j++)
  688. block->prev_energy[0][j] = FFMIN(block->prev_energy[0][j], block->energy[j]);
  689. }
  690. for (j = 0; j < f->start_band; j++) {
  691. block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
  692. block->energy[j] = 0.0;
  693. }
  694. for (j = f->end_band; j < CELT_MAX_BANDS; j++) {
  695. block->prev_energy[0][j] = CELT_ENERGY_SILENCE;
  696. block->energy[j] = 0.0;
  697. }
  698. }
  699. f->seed = rc->range;
  700. return 0;
  701. }
  702. void ff_celt_flush(CeltFrame *f)
  703. {
  704. int i, j;
  705. if (f->flushed)
  706. return;
  707. for (i = 0; i < 2; i++) {
  708. CeltBlock *block = &f->block[i];
  709. for (j = 0; j < CELT_MAX_BANDS; j++)
  710. block->prev_energy[0][j] = block->prev_energy[1][j] = CELT_ENERGY_SILENCE;
  711. memset(block->energy, 0, sizeof(block->energy));
  712. memset(block->buf, 0, sizeof(block->buf));
  713. memset(block->pf_gains, 0, sizeof(block->pf_gains));
  714. memset(block->pf_gains_old, 0, sizeof(block->pf_gains_old));
  715. memset(block->pf_gains_new, 0, sizeof(block->pf_gains_new));
  716. block->emph_coeff = 0.0;
  717. }
  718. f->seed = 0;
  719. f->flushed = 1;
  720. }
  721. void ff_celt_free(CeltFrame **f)
  722. {
  723. CeltFrame *frm = *f;
  724. int i;
  725. if (!frm)
  726. return;
  727. for (i = 0; i < FF_ARRAY_ELEMS(frm->imdct); i++)
  728. ff_mdct15_uninit(&frm->imdct[i]);
  729. ff_celt_pvq_uninit(&frm->pvq);
  730. av_freep(&frm->dsp);
  731. av_freep(f);
  732. }
  733. int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels,
  734. int apply_phase_inv)
  735. {
  736. CeltFrame *frm;
  737. int i, ret;
  738. if (output_channels != 1 && output_channels != 2) {
  739. av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n",
  740. output_channels);
  741. return AVERROR(EINVAL);
  742. }
  743. frm = av_mallocz(sizeof(*frm));
  744. if (!frm)
  745. return AVERROR(ENOMEM);
  746. frm->avctx = avctx;
  747. frm->output_channels = output_channels;
  748. frm->apply_phase_inv = apply_phase_inv;
  749. for (i = 0; i < FF_ARRAY_ELEMS(frm->imdct); i++)
  750. if ((ret = ff_mdct15_init(&frm->imdct[i], 1, i + 3, -1.0f/32768)) < 0)
  751. goto fail;
  752. if ((ret = ff_celt_pvq_init(&frm->pvq, 0)) < 0)
  753. goto fail;
  754. frm->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  755. if (!frm->dsp) {
  756. ret = AVERROR(ENOMEM);
  757. goto fail;
  758. }
  759. ff_celt_flush(frm);
  760. *f = frm;
  761. return 0;
  762. fail:
  763. ff_celt_free(&frm);
  764. return ret;
  765. }