2 * Copyright (c) 2012 Andrew D'Addesio
3 * Copyright (c) 2013-2014 Mozilla Corporation
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/float_dsp.h"
30 #include "libavutil/libm.h"
40 CELT_SPREAD_AGGRESSIVE
43 typedef struct CeltFrame {
44 float energy[CELT_MAX_BANDS];
45 float prev_energy[2][CELT_MAX_BANDS];
47 uint8_t collapse_masks[CELT_MAX_BANDS];
49 /* buffer for mdct output + postfilter */
50 DECLARE_ALIGNED(32, float, buf)[2048];
52 /* postfilter parameters */
54 float pf_gains_new[3];
58 float pf_gains_old[3];
64 // constant values that do not change during context lifetime
65 AVCodecContext *avctx;
66 IMDCT15Context *imdct[4];
67 AVFloatDSPContext *dsp;
70 // values that have inter-frame effect and must be reset on flush
75 // values that only affect a single frame
80 /* number of iMDCT blocks in the frame */
82 /* size of each block */
93 enum CeltSpread spread;
97 int fine_bits [CELT_MAX_BANDS];
98 int fine_priority[CELT_MAX_BANDS];
99 int pulses [CELT_MAX_BANDS];
100 int tf_change [CELT_MAX_BANDS];
102 DECLARE_ALIGNED(32, float, coeffs)[2][CELT_MAX_FRAME_SIZE];
103 DECLARE_ALIGNED(32, float, scratch)[22 * 8]; // MAX(ff_celt_freq_range) * 1<<CELT_MAX_LOG_BLOCKS
106 static inline int16_t celt_cos(int16_t x)
108 x = (MUL16(x, x) + 4096) >> 13;
109 x = (32767-x) + ROUND_MUL16(x, (-7651 + ROUND_MUL16(x, (8277 + ROUND_MUL16(-626, x)))));
113 static inline int celt_log2tan(int isin, int icos)
116 lc = opus_ilog(icos);
117 ls = opus_ilog(isin);
120 return (ls << 11) - (lc << 11) +
121 ROUND_MUL16(isin, ROUND_MUL16(isin, -2597) + 7932) -
122 ROUND_MUL16(icos, ROUND_MUL16(icos, -2597) + 7932);
125 static inline uint32_t celt_rng(CeltContext *s)
127 s->seed = 1664525 * s->seed + 1013904223;
131 static void celt_decode_coarse_energy(CeltContext *s, OpusRangeCoder *rc)
136 const uint8_t *model;
138 /* use the 2D z-transform to apply prediction in both */
139 /* the time domain (alpha) and the frequency domain (beta) */
141 if (opus_rc_tell(rc)+3 <= s->framebits && ff_opus_rc_dec_log(rc, 3)) {
144 beta = 1.0f - 4915.0f/32768.0f;
145 model = ff_celt_coarse_energy_dist[s->duration][1];
147 alpha = ff_celt_alpha_coef[s->duration];
148 beta = 1.0f - ff_celt_beta_coef[s->duration];
149 model = ff_celt_coarse_energy_dist[s->duration][0];
152 for (i = 0; i < CELT_MAX_BANDS; i++) {
153 for (j = 0; j < s->coded_channels; j++) {
154 CeltFrame *frame = &s->frame[j];
158 if (i < s->startband || i >= s->endband) {
159 frame->energy[i] = 0.0;
163 available = s->framebits - opus_rc_tell(rc);
164 if (available >= 15) {
165 /* decode using a Laplace distribution */
166 int k = FFMIN(i, 20) << 1;
167 value = ff_opus_rc_dec_laplace(rc, model[k] << 7, model[k+1] << 6);
168 } else if (available >= 2) {
169 int x = ff_opus_rc_dec_cdf(rc, ff_celt_model_energy_small);
170 value = (x>>1) ^ -(x&1);
171 } else if (available >= 1) {
172 value = -(float)ff_opus_rc_dec_log(rc, 1);
175 frame->energy[i] = FFMAX(-9.0f, frame->energy[i]) * alpha + prev[j] + value;
176 prev[j] += beta * value;
181 static void celt_decode_fine_energy(CeltContext *s, OpusRangeCoder *rc)
184 for (i = s->startband; i < s->endband; i++) {
186 if (!s->fine_bits[i])
189 for (j = 0; j < s->coded_channels; j++) {
190 CeltFrame *frame = &s->frame[j];
193 q2 = ff_opus_rc_get_raw(rc, s->fine_bits[i]);
194 offset = (q2 + 0.5f) * (1 << (14 - s->fine_bits[i])) / 16384.0f - 0.5f;
195 frame->energy[i] += offset;
200 static void celt_decode_final_energy(CeltContext *s, OpusRangeCoder *rc,
205 for (priority = 0; priority < 2; priority++) {
206 for (i = s->startband; i < s->endband && bits_left >= s->coded_channels; i++) {
207 if (s->fine_priority[i] != priority || s->fine_bits[i] >= CELT_MAX_FINE_BITS)
210 for (j = 0; j < s->coded_channels; j++) {
213 q2 = ff_opus_rc_get_raw(rc, 1);
214 offset = (q2 - 0.5f) * (1 << (14 - s->fine_bits[i] - 1)) / 16384.0f;
215 s->frame[j].energy[i] += offset;
222 static void celt_decode_tf_changes(CeltContext *s, OpusRangeCoder *rc,
225 int i, diff = 0, tf_select = 0, tf_changed = 0, tf_select_bit;
226 int consumed, bits = transient ? 2 : 4;
228 consumed = opus_rc_tell(rc);
229 tf_select_bit = (s->duration != 0 && consumed+bits+1 <= s->framebits);
231 for (i = s->startband; i < s->endband; i++) {
232 if (consumed+bits+tf_select_bit <= s->framebits) {
233 diff ^= ff_opus_rc_dec_log(rc, bits);
234 consumed = opus_rc_tell(rc);
237 s->tf_change[i] = diff;
238 bits = transient ? 4 : 5;
241 if (tf_select_bit && ff_celt_tf_select[s->duration][transient][0][tf_changed] !=
242 ff_celt_tf_select[s->duration][transient][1][tf_changed])
243 tf_select = ff_opus_rc_dec_log(rc, 1);
245 for (i = s->startband; i < s->endband; i++) {
246 s->tf_change[i] = ff_celt_tf_select[s->duration][transient][tf_select][s->tf_change[i]];
250 static void celt_decode_allocation(CeltContext *s, OpusRangeCoder *rc)
252 // approx. maximum bit allocation for each band before boost/trim
253 int cap[CELT_MAX_BANDS];
254 int boost[CELT_MAX_BANDS];
255 int threshold[CELT_MAX_BANDS];
256 int bits1[CELT_MAX_BANDS];
257 int bits2[CELT_MAX_BANDS];
258 int trim_offset[CELT_MAX_BANDS];
260 int skip_startband = s->startband;
266 int intensitystereo_bit = 0;
267 int dualstereo_bit = 0;
269 int remaining, bandbits;
270 int low, high, total, done;
275 consumed = opus_rc_tell(rc);
277 /* obtain spread flag */
278 s->spread = CELT_SPREAD_NORMAL;
279 if (consumed + 4 <= s->framebits)
280 s->spread = ff_opus_rc_dec_cdf(rc, ff_celt_model_spread);
282 /* generate static allocation caps */
283 for (i = 0; i < CELT_MAX_BANDS; i++) {
284 cap[i] = (ff_celt_static_caps[s->duration][s->coded_channels - 1][i] + 64)
285 * ff_celt_freq_range[i] << (s->coded_channels - 1) << s->duration >> 2;
288 /* obtain band boost */
289 totalbits = s->framebits << 3; // convert to 1/8 bits
290 consumed = opus_rc_tell_frac(rc);
291 for (i = s->startband; i < s->endband; i++) {
292 int quanta, band_dynalloc;
296 quanta = ff_celt_freq_range[i] << (s->coded_channels - 1) << s->duration;
297 quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
298 band_dynalloc = dynalloc;
299 while (consumed + (band_dynalloc<<3) < totalbits && boost[i] < cap[i]) {
300 int add = ff_opus_rc_dec_log(rc, band_dynalloc);
301 consumed = opus_rc_tell_frac(rc);
309 /* dynalloc is more likely to occur if it's already been used for earlier bands */
311 dynalloc = FFMAX(2, dynalloc - 1);
314 /* obtain allocation trim */
315 if (consumed + (6 << 3) <= totalbits)
316 alloctrim = ff_opus_rc_dec_cdf(rc, ff_celt_model_alloc_trim);
318 /* anti-collapse bit reservation */
319 totalbits = (s->framebits << 3) - opus_rc_tell_frac(rc) - 1;
320 s->anticollapse_bit = 0;
321 if (s->blocks > 1 && s->duration >= 2 &&
322 totalbits >= ((s->duration + 2) << 3))
323 s->anticollapse_bit = 1 << 3;
324 totalbits -= s->anticollapse_bit;
326 /* band skip bit reservation */
327 if (totalbits >= 1 << 3)
329 totalbits -= skip_bit;
331 /* intensity/dual stereo bit reservation */
332 if (s->coded_channels == 2) {
333 intensitystereo_bit = ff_celt_log2_frac[s->endband - s->startband];
334 if (intensitystereo_bit <= totalbits) {
335 totalbits -= intensitystereo_bit;
336 if (totalbits >= 1 << 3) {
337 dualstereo_bit = 1 << 3;
341 intensitystereo_bit = 0;
344 for (i = s->startband; i < s->endband; i++) {
345 int trim = alloctrim - 5 - s->duration;
346 int band = ff_celt_freq_range[i] * (s->endband - i - 1);
347 int duration = s->duration + 3;
348 int scale = duration + s->coded_channels - 1;
350 /* PVQ minimum allocation threshold, below this value the band is
352 threshold[i] = FFMAX(3 * ff_celt_freq_range[i] << duration >> 4,
353 s->coded_channels << 3);
355 trim_offset[i] = trim * (band << scale) >> 6;
357 if (ff_celt_freq_range[i] << s->duration == 1)
358 trim_offset[i] -= s->coded_channels << 3;
363 high = CELT_VECTORS - 1;
364 while (low <= high) {
365 int center = (low + high) >> 1;
368 for (i = s->endband - 1; i >= s->startband; i--) {
369 bandbits = ff_celt_freq_range[i] * ff_celt_static_alloc[center][i]
370 << (s->coded_channels - 1) << s->duration >> 2;
373 bandbits = FFMAX(0, bandbits + trim_offset[i]);
374 bandbits += boost[i];
376 if (bandbits >= threshold[i] || done) {
378 total += FFMIN(bandbits, cap[i]);
379 } else if (bandbits >= s->coded_channels << 3)
380 total += s->coded_channels << 3;
383 if (total > totalbits)
390 for (i = s->startband; i < s->endband; i++) {
391 bits1[i] = ff_celt_freq_range[i] * ff_celt_static_alloc[low][i]
392 << (s->coded_channels - 1) << s->duration >> 2;
393 bits2[i] = high >= CELT_VECTORS ? cap[i] :
394 ff_celt_freq_range[i] * ff_celt_static_alloc[high][i]
395 << (s->coded_channels - 1) << s->duration >> 2;
398 bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]);
400 bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]);
402 bits1[i] += boost[i];
403 bits2[i] += boost[i];
407 bits2[i] = FFMAX(0, bits2[i] - bits1[i]);
412 high = 1 << CELT_ALLOC_STEPS;
413 for (i = 0; i < CELT_ALLOC_STEPS; i++) {
414 int center = (low + high) >> 1;
417 for (j = s->endband - 1; j >= s->startband; j--) {
418 bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
420 if (bandbits >= threshold[j] || done) {
422 total += FFMIN(bandbits, cap[j]);
423 } else if (bandbits >= s->coded_channels << 3)
424 total += s->coded_channels << 3;
426 if (total > totalbits)
433 for (i = s->endband - 1; i >= s->startband; i--) {
434 bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
436 if (bandbits >= threshold[i] || done)
439 bandbits = (bandbits >= s->coded_channels << 3) ?
440 s->coded_channels << 3 : 0;
442 bandbits = FFMIN(bandbits, cap[i]);
443 s->pulses[i] = bandbits;
448 for (s->codedbands = s->endband; ; s->codedbands--) {
450 j = s->codedbands - 1;
452 if (j == skip_startband) {
453 /* all remaining bands are not skipped */
454 totalbits += skip_bit;
458 /* determine the number of bits available for coding "do not skip" markers */
459 remaining = totalbits - total;
460 bandbits = remaining / (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[s->startband]);
461 remaining -= bandbits * (ff_celt_freq_bands[j+1] - ff_celt_freq_bands[s->startband]);
462 allocation = s->pulses[j] + bandbits * ff_celt_freq_range[j]
463 + FFMAX(0, remaining - (ff_celt_freq_bands[j] - ff_celt_freq_bands[s->startband]));
465 /* a "do not skip" marker is only coded if the allocation is
466 above the chosen threshold */
467 if (allocation >= FFMAX(threshold[j], (s->coded_channels + 1) <<3 )) {
468 if (ff_opus_rc_dec_log(rc, 1))
472 allocation -= 1 << 3;
475 /* the band is skipped, so reclaim its bits */
476 total -= s->pulses[j];
477 if (intensitystereo_bit) {
478 total -= intensitystereo_bit;
479 intensitystereo_bit = ff_celt_log2_frac[j - s->startband];
480 total += intensitystereo_bit;
483 total += s->pulses[j] = (allocation >= s->coded_channels << 3) ?
484 s->coded_channels << 3 : 0;
487 /* obtain stereo flags */
488 s->intensitystereo = 0;
490 if (intensitystereo_bit)
491 s->intensitystereo = s->startband +
492 ff_opus_rc_dec_uint(rc, s->codedbands + 1 - s->startband);
493 if (s->intensitystereo <= s->startband)
494 totalbits += dualstereo_bit; /* no intensity stereo means no dual stereo */
495 else if (dualstereo_bit)
496 s->dualstereo = ff_opus_rc_dec_log(rc, 1);
498 /* supply the remaining bits in this frame to lower bands */
499 remaining = totalbits - total;
500 bandbits = remaining / (ff_celt_freq_bands[s->codedbands] - ff_celt_freq_bands[s->startband]);
501 remaining -= bandbits * (ff_celt_freq_bands[s->codedbands] - ff_celt_freq_bands[s->startband]);
502 for (i = s->startband; i < s->codedbands; i++) {
503 int bits = FFMIN(remaining, ff_celt_freq_range[i]);
505 s->pulses[i] += bits + bandbits * ff_celt_freq_range[i];
509 for (i = s->startband; i < s->codedbands; i++) {
510 int N = ff_celt_freq_range[i] << s->duration;
511 int prev_extra = extrabits;
512 s->pulses[i] += extrabits;
515 int dof; // degrees of freedom
516 int temp; // dof * channels * log(dof)
517 int offset; // fine energy quantization offset, i.e.
518 // extra bits assigned over the standard
520 int fine_bits, max_bits;
522 extrabits = FFMAX(0, s->pulses[i] - cap[i]);
523 s->pulses[i] -= extrabits;
525 /* intensity stereo makes use of an extra degree of freedom */
526 dof = N * s->coded_channels
527 + (s->coded_channels == 2 && N > 2 && !s->dualstereo && i < s->intensitystereo);
528 temp = dof * (ff_celt_log_freq_range[i] + (s->duration<<3));
529 offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
530 if (N == 2) /* dof=2 is the only case that doesn't fit the model */
533 /* grant an additional bias for the first and second pulses */
534 if (s->pulses[i] + offset < 2 * (dof << 3))
536 else if (s->pulses[i] + offset < 3 * (dof << 3))
539 fine_bits = (s->pulses[i] + offset + (dof << 2)) / (dof << 3);
540 max_bits = FFMIN((s->pulses[i]>>3) >> (s->coded_channels - 1),
543 max_bits = FFMAX(max_bits, 0);
545 s->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
547 /* if fine_bits was rounded down or capped,
548 give priority for the final fine energy pass */
549 s->fine_priority[i] = (s->fine_bits[i] * (dof<<3) >= s->pulses[i] + offset);
551 /* the remaining bits are assigned to PVQ */
552 s->pulses[i] -= s->fine_bits[i] << (s->coded_channels - 1) << 3;
554 /* all bits go to fine energy except for the sign bit */
555 extrabits = FFMAX(0, s->pulses[i] - (s->coded_channels << 3));
556 s->pulses[i] -= extrabits;
558 s->fine_priority[i] = 1;
561 /* hand back a limited number of extra fine energy bits to this band */
563 int fineextra = FFMIN(extrabits >> (s->coded_channels + 2),
564 CELT_MAX_FINE_BITS - s->fine_bits[i]);
565 s->fine_bits[i] += fineextra;
567 fineextra <<= s->coded_channels + 2;
568 s->fine_priority[i] = (fineextra >= extrabits - prev_extra);
569 extrabits -= fineextra;
572 s->remaining = extrabits;
574 /* skipped bands dedicate all of their bits for fine energy */
575 for (; i < s->endband; i++) {
576 s->fine_bits[i] = s->pulses[i] >> (s->coded_channels - 1) >> 3;
578 s->fine_priority[i] = s->fine_bits[i] < 1;
582 static inline int celt_bits2pulses(const uint8_t *cache, int bits)
584 // TODO: Find the size of cache and make it into an array in the parameters list
585 int i, low = 0, high;
590 for (i = 0; i < 6; i++) {
591 int center = (low + high + 1) >> 1;
592 if (cache[center] >= bits)
598 return (bits - (low == 0 ? -1 : cache[low]) <= cache[high] - bits) ? low : high;
601 static inline int celt_pulses2bits(const uint8_t *cache, int pulses)
603 // TODO: Find the size of cache and make it into an array in the parameters list
604 return (pulses == 0) ? 0 : cache[pulses] + 1;
607 static inline void celt_normalize_residual(const int * av_restrict iy, float * av_restrict X,
611 for (i = 0; i < N; i++)
615 static void celt_exp_rotation1(float *X, unsigned int len, unsigned int stride,
622 for (i = 0; i < len - stride; i++) {
626 Xptr[stride] = c * x2 + s * x1;
627 *Xptr++ = c * x1 - s * x2;
630 Xptr = &X[len - 2 * stride - 1];
631 for (i = len - 2 * stride - 1; i >= 0; i--) {
635 Xptr[stride] = c * x2 + s * x1;
636 *Xptr-- = c * x1 - s * x2;
640 static inline void celt_exp_rotation(float *X, unsigned int len,
641 unsigned int stride, unsigned int K,
642 enum CeltSpread spread)
644 unsigned int stride2 = 0;
649 if (2*K >= len || spread == CELT_SPREAD_NONE)
652 gain = (float)len / (len + (20 - 5*spread) * K);
653 theta = M_PI * gain * gain / 4;
658 if (len >= stride << 3) {
660 /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding.
661 It's basically incrementing long as (stride2+0.5)^2 < len/stride. */
662 while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len)
666 /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
667 extract_collapse_mask().*/
669 for (i = 0; i < stride; i++) {
671 celt_exp_rotation1(X + i * len, len, stride2, s, c);
672 celt_exp_rotation1(X + i * len, len, 1, c, s);
676 static inline unsigned int celt_extract_collapse_mask(const int *iy,
680 unsigned int collapse_mask;
687 /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
691 for (i = 0; i < B; i++)
692 for (j = 0; j < N0; j++)
693 collapse_mask |= (iy[i*N0+j]!=0)<<i;
694 return collapse_mask;
697 static inline void celt_renormalize_vector(float *X, int N, float gain)
701 for (i = 0; i < N; i++)
705 for (i = 0; i < N; i++)
709 static inline void celt_stereo_merge(float *X, float *Y, float mid, int N)
712 float xp = 0, side = 0;
717 /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
718 for (i = 0; i < N; i++) {
723 /* Compensating for the mid normalization */
726 E[0] = mid2 * mid2 + side - 2 * xp;
727 E[1] = mid2 * mid2 + side + 2 * xp;
728 if (E[0] < 6e-4f || E[1] < 6e-4f) {
729 for (i = 0; i < N; i++)
735 gain[0] = 1.0f / sqrtf(t);
737 gain[1] = 1.0f / sqrtf(t);
739 for (i = 0; i < N; i++) {
741 /* Apply mid scaling (side is already scaled) */
742 value[0] = mid * X[i];
744 X[i] = gain[0] * (value[0] - value[1]);
745 Y[i] = gain[1] * (value[0] + value[1]);
749 static void celt_interleave_hadamard(float *tmp, float *X, int N0,
750 int stride, int hadamard)
756 const uint8_t *ordery = ff_celt_hadamard_ordery + stride - 2;
757 for (i = 0; i < stride; i++)
758 for (j = 0; j < N0; j++)
759 tmp[j*stride+i] = X[ordery[i]*N0+j];
761 for (i = 0; i < stride; i++)
762 for (j = 0; j < N0; j++)
763 tmp[j*stride+i] = X[i*N0+j];
766 for (i = 0; i < N; i++)
770 static void celt_deinterleave_hadamard(float *tmp, float *X, int N0,
771 int stride, int hadamard)
777 const uint8_t *ordery = ff_celt_hadamard_ordery + stride - 2;
778 for (i = 0; i < stride; i++)
779 for (j = 0; j < N0; j++)
780 tmp[ordery[i]*N0+j] = X[j*stride+i];
782 for (i = 0; i < stride; i++)
783 for (j = 0; j < N0; j++)
784 tmp[i*N0+j] = X[j*stride+i];
787 for (i = 0; i < N; i++)
791 static void celt_haar1(float *X, int N0, int stride)
795 for (i = 0; i < stride; i++) {
796 for (j = 0; j < N0; j++) {
797 float x0 = X[stride * (2 * j + 0) + i];
798 float x1 = X[stride * (2 * j + 1) + i];
799 X[stride * (2 * j + 0) + i] = (x0 + x1) * M_SQRT1_2;
800 X[stride * (2 * j + 1) + i] = (x0 - x1) * M_SQRT1_2;
805 static inline int celt_compute_qn(int N, int b, int offset, int pulse_cap,
810 if (dualstereo && N == 2)
813 /* The upper limit ensures that in a stereo split with itheta==16384, we'll
814 * always have enough bits left over to code at least one pulse in the
815 * side; otherwise it would collapse, since it doesn't get folded. */
816 qb = FFMIN3(b - pulse_cap - (4 << 3), (b + N2 * offset) / N2, 8 << 3);
817 qn = (qb < (1 << 3 >> 1)) ? 1 : ((ff_celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1;
821 // this code was adapted from libopus
822 static inline uint64_t celt_cwrsi(unsigned int N, unsigned int K, unsigned int i, int *y)
832 /*Lots of pulses case:*/
834 const uint32_t *row = ff_celt_pvq_u_row[N];
836 /* Are the pulses in this dimension negative? */
841 /*Count how many pulses were placed in this dimension.*/
847 p = ff_celt_pvq_u_row[--K][N];
850 for (p = row[K]; p > i; p = row[K])
854 val = (k0 - K + s) ^ s;
857 } else { /*Lots of dimensions case:*/
858 /*Are there any pulses in this dimension at all?*/
859 p = ff_celt_pvq_u_row[K ][N];
860 q = ff_celt_pvq_u_row[K + 1][N];
862 if (p <= i && i < q) {
866 /*Are the pulses in this dimension negative?*/
870 /*Count how many pulses were placed in this dimension.*/
872 do p = ff_celt_pvq_u_row[--K][N];
876 val = (k0 - K + s) ^ s;
894 val = (k0 - K + s) ^ s;
907 static inline float celt_decode_pulses(OpusRangeCoder *rc, int *y, unsigned int N, unsigned int K)
910 #define CELT_PVQ_U(n, k) (ff_celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)])
911 #define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, (k) + 1))
912 idx = ff_opus_rc_dec_uint(rc, CELT_PVQ_V(N, K));
913 return celt_cwrsi(N, K, idx, y);
916 /** Decode pulse vector and combine the result with the pitch vector to produce
917 the final normalised signal in the current band. */
918 static inline unsigned int celt_alg_unquant(OpusRangeCoder *rc, float *X,
919 unsigned int N, unsigned int K,
920 enum CeltSpread spread,
921 unsigned int blocks, float gain)
925 gain /= sqrtf(celt_decode_pulses(rc, y, N, K));
926 celt_normalize_residual(y, X, N, gain);
927 celt_exp_rotation(X, N, blocks, K, spread);
928 return celt_extract_collapse_mask(y, N, blocks);
931 static unsigned int celt_decode_band(CeltContext *s, OpusRangeCoder *rc,
932 const int band, float *X, float *Y,
933 int N, int b, unsigned int blocks,
934 float *lowband, int duration,
935 float *lowband_out, int level,
936 float gain, float *lowband_scratch,
939 const uint8_t *cache;
940 int dualstereo, split;
941 int imid = 0, iside = 0;
949 float mid = 0, side = 0;
950 int longblocks = (B0 == 1);
953 N_B0 = N_B = N / blocks;
954 split = dualstereo = (Y != NULL);
957 /* special case for one sample */
960 for (i = 0; i <= dualstereo; i++) {
962 if (s->remaining2 >= 1<<3) {
963 sign = ff_opus_rc_get_raw(rc, 1);
964 s->remaining2 -= 1 << 3;
967 x[0] = sign ? -1.0f : 1.0f;
971 lowband_out[0] = X[0];
975 if (!dualstereo && level == 0) {
976 int tf_change = s->tf_change[band];
979 recombine = tf_change;
980 /* Band recombining to increase frequency resolution */
983 (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) {
985 for (j = 0; j < N; j++)
986 lowband_scratch[j] = lowband[j];
987 lowband = lowband_scratch;
990 for (k = 0; k < recombine; k++) {
992 celt_haar1(lowband, N >> k, 1 << k);
993 fill = ff_celt_bit_interleave[fill & 0xF] | ff_celt_bit_interleave[fill >> 4] << 2;
995 blocks >>= recombine;
998 /* Increasing the time resolution */
999 while ((N_B & 1) == 0 && tf_change < 0) {
1001 celt_haar1(lowband, N_B, blocks);
1002 fill |= fill << blocks;
1011 /* Reorganize the samples in time order instead of frequency order */
1012 if (B0 > 1 && lowband)
1013 celt_deinterleave_hadamard(s->scratch, lowband, N_B >> recombine,
1014 B0 << recombine, longblocks);
1017 /* If we need 1.5 more bit than we can produce, split the band in two. */
1018 cache = ff_celt_cache_bits +
1019 ff_celt_cache_index[(duration + 1) * CELT_MAX_BANDS + band];
1020 if (!dualstereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) {
1026 fill = (fill & 1) | (fill << 1);
1027 blocks = (blocks + 1) >> 1;
1033 int mbits, sbits, delta;
1040 /* Decide on the resolution to give to the split parameter theta */
1041 pulse_cap = ff_celt_log_freq_range[band] + duration * 8;
1042 offset = (pulse_cap >> 1) - (dualstereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE :
1043 CELT_QTHETA_OFFSET);
1044 qn = (dualstereo && band >= s->intensitystereo) ? 1 :
1045 celt_compute_qn(N, b, offset, pulse_cap, dualstereo);
1046 tell = opus_rc_tell_frac(rc);
1048 /* Entropy coding of the angle. We use a uniform pdf for the
1049 time split, a step for stereo, and a triangular one for the rest. */
1050 if (dualstereo && N > 2)
1051 itheta = ff_opus_rc_dec_uint_step(rc, qn/2);
1052 else if (dualstereo || B0 > 1)
1053 itheta = ff_opus_rc_dec_uint(rc, qn+1);
1055 itheta = ff_opus_rc_dec_uint_tri(rc, qn);
1056 itheta = itheta * 16384 / qn;
1057 /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate.
1058 Let's do that at higher complexity */
1059 } else if (dualstereo) {
1060 inv = (b > 2 << 3 && s->remaining2 > 2 << 3) ? ff_opus_rc_dec_log(rc, 2) : 0;
1063 qalloc = opus_rc_tell_frac(rc) - tell;
1070 fill = av_mod_uintp2(fill, blocks);
1072 } else if (itheta == 16384) {
1075 fill &= ((1 << blocks) - 1) << blocks;
1078 imid = celt_cos(itheta);
1079 iside = celt_cos(16384-itheta);
1080 /* This is the mid vs side allocation that minimizes squared error
1082 delta = ROUND_MUL16((N - 1) << 7, celt_log2tan(iside, imid));
1085 mid = imid / 32768.0f;
1086 side = iside / 32768.0f;
1088 /* This is a special case for N=2 that only works for stereo and takes
1089 advantage of the fact that mid and side are orthogonal to encode
1090 the side with just one bit. */
1091 if (N == 2 && dualstereo) {
1097 /* Only need one bit for the side */
1098 sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0;
1100 c = (itheta > 8192);
1101 s->remaining2 -= qalloc+sbits;
1106 sign = ff_opus_rc_get_raw(rc, 1);
1107 sign = 1 - 2 * sign;
1108 /* We use orig_fill here because we want to fold the side, but if
1109 itheta==16384, we'll have cleared the low bits of fill. */
1110 cm = celt_decode_band(s, rc, band, x2, NULL, N, mbits, blocks,
1111 lowband, duration, lowband_out, level, gain,
1112 lowband_scratch, orig_fill);
1113 /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
1114 and there's no need to worry about mixing with the other channel. */
1115 y2[0] = -sign * x2[1];
1116 y2[1] = sign * x2[0];
1128 /* "Normal" split code */
1129 float *next_lowband2 = NULL;
1130 float *next_lowband_out1 = NULL;
1134 /* Give more bits to low-energy MDCTs than they would
1135 * otherwise deserve */
1136 if (B0 > 1 && !dualstereo && (itheta & 0x3fff)) {
1138 /* Rough approximation for pre-echo masking */
1139 delta -= delta >> (4 - duration);
1141 /* Corresponds to a forward-masking slope of
1142 * 1.5 dB per 10 ms */
1143 delta = FFMIN(0, delta + (N << 3 >> (5 - duration)));
1145 mbits = av_clip((b - delta) / 2, 0, b);
1147 s->remaining2 -= qalloc;
1149 if (lowband && !dualstereo)
1150 next_lowband2 = lowband + N; /* >32-bit split case */
1152 /* Only stereo needs to pass on lowband_out.
1153 * Otherwise, it's handled at the end */
1155 next_lowband_out1 = lowband_out;
1157 next_level = level + 1;
1159 rebalance = s->remaining2;
1160 if (mbits >= sbits) {
1161 /* In stereo mode, we do not apply a scaling to the mid
1162 * because we need the normalized mid for folding later */
1163 cm = celt_decode_band(s, rc, band, X, NULL, N, mbits, blocks,
1164 lowband, duration, next_lowband_out1,
1165 next_level, dualstereo ? 1.0f : (gain * mid),
1166 lowband_scratch, fill);
1168 rebalance = mbits - (rebalance - s->remaining2);
1169 if (rebalance > 3 << 3 && itheta != 0)
1170 sbits += rebalance - (3 << 3);
1172 /* For a stereo split, the high bits of fill are always zero,
1173 * so no folding will be done to the side. */
1174 cm |= celt_decode_band(s, rc, band, Y, NULL, N, sbits, blocks,
1175 next_lowband2, duration, NULL,
1176 next_level, gain * side, NULL,
1177 fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
1179 /* For a stereo split, the high bits of fill are always zero,
1180 * so no folding will be done to the side. */
1181 cm = celt_decode_band(s, rc, band, Y, NULL, N, sbits, blocks,
1182 next_lowband2, duration, NULL,
1183 next_level, gain * side, NULL,
1184 fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
1186 rebalance = sbits - (rebalance - s->remaining2);
1187 if (rebalance > 3 << 3 && itheta != 16384)
1188 mbits += rebalance - (3 << 3);
1190 /* In stereo mode, we do not apply a scaling to the mid because
1191 * we need the normalized mid for folding later */
1192 cm |= celt_decode_band(s, rc, band, X, NULL, N, mbits, blocks,
1193 lowband, duration, next_lowband_out1,
1194 next_level, dualstereo ? 1.0f : (gain * mid),
1195 lowband_scratch, fill);
1199 /* This is the basic no-split case */
1200 unsigned int q = celt_bits2pulses(cache, b);
1201 unsigned int curr_bits = celt_pulses2bits(cache, q);
1202 s->remaining2 -= curr_bits;
1204 /* Ensures we can never bust the budget */
1205 while (s->remaining2 < 0 && q > 0) {
1206 s->remaining2 += curr_bits;
1207 curr_bits = celt_pulses2bits(cache, --q);
1208 s->remaining2 -= curr_bits;
1212 /* Finally do the actual quantization */
1213 cm = celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
1214 s->spread, blocks, gain);
1216 /* If there's no pulse, fill the band anyway */
1218 unsigned int cm_mask = (1 << blocks) - 1;
1221 for (j = 0; j < N; j++)
1226 for (j = 0; j < N; j++)
1227 X[j] = (((int32_t)celt_rng(s)) >> 20);
1230 /* Folded spectrum */
1231 for (j = 0; j < N; j++) {
1232 /* About 48 dB below the "normal" folding level */
1233 X[j] = lowband[j] + (((celt_rng(s)) & 0x8000) ? 1.0f / 256 : -1.0f / 256);
1237 celt_renormalize_vector(X, N, gain);
1242 /* This code is used by the decoder and by the resynthesis-enabled encoder */
1246 celt_stereo_merge(X, Y, mid, N);
1248 for (j = 0; j < N; j++)
1251 } else if (level == 0) {
1254 /* Undo the sample reorganization going from time order to frequency order */
1256 celt_interleave_hadamard(s->scratch, X, N_B>>recombine,
1257 B0<<recombine, longblocks);
1259 /* Undo time-freq changes that we did earlier */
1262 for (k = 0; k < time_divide; k++) {
1266 celt_haar1(X, N_B, blocks);
1269 for (k = 0; k < recombine; k++) {
1270 cm = ff_celt_bit_deinterleave[cm];
1271 celt_haar1(X, N0>>k, 1<<k);
1273 blocks <<= recombine;
1275 /* Scale output for later folding */
1278 float n = sqrtf(N0);
1279 for (j = 0; j < N0; j++)
1280 lowband_out[j] = n * X[j];
1282 cm = av_mod_uintp2(cm, blocks);
1287 static void celt_denormalize(CeltContext *s, CeltFrame *frame, float *data)
1291 for (i = s->startband; i < s->endband; i++) {
1292 float *dst = data + (ff_celt_freq_bands[i] << s->duration);
1293 float norm = exp2(frame->energy[i] + ff_celt_mean_energy[i]);
1295 for (j = 0; j < ff_celt_freq_range[i] << s->duration; j++)
1300 static void celt_postfilter_apply_transition(CeltFrame *frame, float *data)
1302 const int T0 = frame->pf_period_old;
1303 const int T1 = frame->pf_period;
1305 float g00, g01, g02;
1306 float g10, g11, g12;
1308 float x0, x1, x2, x3, x4;
1312 if (frame->pf_gains[0] == 0.0 &&
1313 frame->pf_gains_old[0] == 0.0)
1316 g00 = frame->pf_gains_old[0];
1317 g01 = frame->pf_gains_old[1];
1318 g02 = frame->pf_gains_old[2];
1319 g10 = frame->pf_gains[0];
1320 g11 = frame->pf_gains[1];
1321 g12 = frame->pf_gains[2];
1328 for (i = 0; i < CELT_OVERLAP; i++) {
1329 float w = ff_celt_window2[i];
1330 x0 = data[i - T1 + 2];
1332 data[i] += (1.0 - w) * g00 * data[i - T0] +
1333 (1.0 - w) * g01 * (data[i - T0 - 1] + data[i - T0 + 1]) +
1334 (1.0 - w) * g02 * (data[i - T0 - 2] + data[i - T0 + 2]) +
1336 w * g11 * (x1 + x3) +
1337 w * g12 * (x0 + x4);
1345 static void celt_postfilter_apply(CeltFrame *frame,
1346 float *data, int len)
1348 const int T = frame->pf_period;
1350 float x0, x1, x2, x3, x4;
1353 if (frame->pf_gains[0] == 0.0 || len <= 0)
1356 g0 = frame->pf_gains[0];
1357 g1 = frame->pf_gains[1];
1358 g2 = frame->pf_gains[2];
1365 for (i = 0; i < len; i++) {
1366 x0 = data[i - T + 2];
1367 data[i] += g0 * x2 +
1377 static void celt_postfilter(CeltContext *s, CeltFrame *frame)
1379 int len = s->blocksize * s->blocks;
1381 celt_postfilter_apply_transition(frame, frame->buf + 1024);
1383 frame->pf_period_old = frame->pf_period;
1384 memcpy(frame->pf_gains_old, frame->pf_gains, sizeof(frame->pf_gains));
1386 frame->pf_period = frame->pf_period_new;
1387 memcpy(frame->pf_gains, frame->pf_gains_new, sizeof(frame->pf_gains));
1389 if (len > CELT_OVERLAP) {
1390 celt_postfilter_apply_transition(frame, frame->buf + 1024 + CELT_OVERLAP);
1391 celt_postfilter_apply(frame, frame->buf + 1024 + 2 * CELT_OVERLAP,
1392 len - 2 * CELT_OVERLAP);
1394 frame->pf_period_old = frame->pf_period;
1395 memcpy(frame->pf_gains_old, frame->pf_gains, sizeof(frame->pf_gains));
1398 memmove(frame->buf, frame->buf + len, (1024 + CELT_OVERLAP / 2) * sizeof(float));
1401 static int parse_postfilter(CeltContext *s, OpusRangeCoder *rc, int consumed)
1403 static const float postfilter_taps[3][3] = {
1404 { 0.3066406250f, 0.2170410156f, 0.1296386719f },
1405 { 0.4638671875f, 0.2680664062f, 0.0 },
1406 { 0.7998046875f, 0.1000976562f, 0.0 }
1410 memset(s->frame[0].pf_gains_new, 0, sizeof(s->frame[0].pf_gains_new));
1411 memset(s->frame[1].pf_gains_new, 0, sizeof(s->frame[1].pf_gains_new));
1413 if (s->startband == 0 && consumed + 16 <= s->framebits) {
1414 int has_postfilter = ff_opus_rc_dec_log(rc, 1);
1415 if (has_postfilter) {
1417 int tapset, octave, period;
1419 octave = ff_opus_rc_dec_uint(rc, 6);
1420 period = (16 << octave) + ff_opus_rc_get_raw(rc, 4 + octave) - 1;
1421 gain = 0.09375f * (ff_opus_rc_get_raw(rc, 3) + 1);
1422 tapset = (opus_rc_tell(rc) + 2 <= s->framebits) ?
1423 ff_opus_rc_dec_cdf(rc, ff_celt_model_tapset) : 0;
1425 for (i = 0; i < 2; i++) {
1426 CeltFrame *frame = &s->frame[i];
1428 frame->pf_period_new = FFMAX(period, CELT_POSTFILTER_MINPERIOD);
1429 frame->pf_gains_new[0] = gain * postfilter_taps[tapset][0];
1430 frame->pf_gains_new[1] = gain * postfilter_taps[tapset][1];
1431 frame->pf_gains_new[2] = gain * postfilter_taps[tapset][2];
1435 consumed = opus_rc_tell(rc);
1441 static void process_anticollapse(CeltContext *s, CeltFrame *frame, float *X)
1445 for (i = s->startband; i < s->endband; i++) {
1446 int renormalize = 0;
1450 float thresh, sqrt_1;
1453 /* depth in 1/8 bits */
1454 depth = (1 + s->pulses[i]) / (ff_celt_freq_range[i] << s->duration);
1455 thresh = exp2f(-1.0 - 0.125f * depth);
1456 sqrt_1 = 1.0f / sqrtf(ff_celt_freq_range[i] << s->duration);
1458 xptr = X + (ff_celt_freq_bands[i] << s->duration);
1460 prev[0] = frame->prev_energy[0][i];
1461 prev[1] = frame->prev_energy[1][i];
1462 if (s->coded_channels == 1) {
1463 CeltFrame *frame1 = &s->frame[1];
1465 prev[0] = FFMAX(prev[0], frame1->prev_energy[0][i]);
1466 prev[1] = FFMAX(prev[1], frame1->prev_energy[1][i]);
1468 Ediff = frame->energy[i] - FFMIN(prev[0], prev[1]);
1469 Ediff = FFMAX(0, Ediff);
1471 /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
1472 short blocks don't have the same energy as long */
1473 r = exp2(1 - Ediff);
1474 if (s->duration == 3)
1476 r = FFMIN(thresh, r) * sqrt_1;
1477 for (k = 0; k < 1 << s->duration; k++) {
1478 /* Detect collapse */
1479 if (!(frame->collapse_masks[i] & 1 << k)) {
1480 /* Fill with noise */
1481 for (j = 0; j < ff_celt_freq_range[i]; j++)
1482 xptr[(j << s->duration) + k] = (celt_rng(s) & 0x8000) ? r : -r;
1487 /* We just added some energy, so we need to renormalize */
1489 celt_renormalize_vector(xptr, ff_celt_freq_range[i] << s->duration, 1.0f);
1493 static void celt_decode_bands(CeltContext *s, OpusRangeCoder *rc)
1495 float lowband_scratch[8 * 22];
1496 float norm[2 * 8 * 100];
1498 int totalbits = (s->framebits << 3) - s->anticollapse_bit;
1500 int update_lowband = 1;
1501 int lowband_offset = 0;
1505 memset(s->coeffs, 0, sizeof(s->coeffs));
1507 for (i = s->startband; i < s->endband; i++) {
1508 int band_offset = ff_celt_freq_bands[i] << s->duration;
1509 int band_size = ff_celt_freq_range[i] << s->duration;
1510 float *X = s->coeffs[0] + band_offset;
1511 float *Y = (s->coded_channels == 2) ? s->coeffs[1] + band_offset : NULL;
1513 int consumed = opus_rc_tell_frac(rc);
1514 float *norm2 = norm + 8 * 100;
1515 int effective_lowband = -1;
1519 /* Compute how many bits we want to allocate to this band */
1520 if (i != s->startband)
1521 s->remaining -= consumed;
1522 s->remaining2 = totalbits - consumed - 1;
1523 if (i <= s->codedbands - 1) {
1524 int curr_balance = s->remaining / FFMIN(3, s->codedbands-i);
1525 b = av_clip_uintp2(FFMIN(s->remaining2 + 1, s->pulses[i] + curr_balance), 14);
1529 if (ff_celt_freq_bands[i] - ff_celt_freq_range[i] >= ff_celt_freq_bands[s->startband] &&
1530 (update_lowband || lowband_offset == 0))
1533 /* Get a conservative estimate of the collapse_mask's for the bands we're
1534 going to be folding from. */
1535 if (lowband_offset != 0 && (s->spread != CELT_SPREAD_AGGRESSIVE ||
1536 s->blocks > 1 || s->tf_change[i] < 0)) {
1537 int foldstart, foldend;
1539 /* This ensures we never repeat spectral content within one band */
1540 effective_lowband = FFMAX(ff_celt_freq_bands[s->startband],
1541 ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]);
1542 foldstart = lowband_offset;
1543 while (ff_celt_freq_bands[--foldstart] > effective_lowband);
1544 foldend = lowband_offset - 1;
1545 while (ff_celt_freq_bands[++foldend] < effective_lowband + ff_celt_freq_range[i]);
1548 for (j = foldstart; j < foldend; j++) {
1549 cm[0] |= s->frame[0].collapse_masks[j];
1550 cm[1] |= s->frame[s->coded_channels - 1].collapse_masks[j];
1553 /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1554 always) be non-zero.*/
1555 cm[0] = cm[1] = (1 << s->blocks) - 1;
1557 if (s->dualstereo && i == s->intensitystereo) {
1558 /* Switch off dual stereo to do intensity */
1560 for (j = ff_celt_freq_bands[s->startband] << s->duration; j < band_offset; j++)
1561 norm[j] = (norm[j] + norm2[j]) / 2;
1564 if (s->dualstereo) {
1565 cm[0] = celt_decode_band(s, rc, i, X, NULL, band_size, b / 2, s->blocks,
1566 effective_lowband != -1 ? norm + (effective_lowband << s->duration) : NULL, s->duration,
1567 norm + band_offset, 0, 1.0f, lowband_scratch, cm[0]);
1569 cm[1] = celt_decode_band(s, rc, i, Y, NULL, band_size, b/2, s->blocks,
1570 effective_lowband != -1 ? norm2 + (effective_lowband << s->duration) : NULL, s->duration,
1571 norm2 + band_offset, 0, 1.0f, lowband_scratch, cm[1]);
1573 cm[0] = celt_decode_band(s, rc, i, X, Y, band_size, b, s->blocks,
1574 effective_lowband != -1 ? norm + (effective_lowband << s->duration) : NULL, s->duration,
1575 norm + band_offset, 0, 1.0f, lowband_scratch, cm[0]|cm[1]);
1580 s->frame[0].collapse_masks[i] = (uint8_t)cm[0];
1581 s->frame[s->coded_channels - 1].collapse_masks[i] = (uint8_t)cm[1];
1582 s->remaining += s->pulses[i] + consumed;
1584 /* Update the folding position only as long as we have 1 bit/sample depth */
1585 update_lowband = (b > band_size << 3);
1589 int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc,
1590 float **output, int coded_channels, int frame_size,
1591 int startband, int endband)
1595 int consumed; // bits of entropy consumed thus far for this frame
1598 int anticollapse = 0;
1599 IMDCT15Context *imdct;
1600 float imdct_scale = 1.0;
1602 if (coded_channels != 1 && coded_channels != 2) {
1603 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coded channels: %d\n",
1605 return AVERROR_INVALIDDATA;
1607 if (startband < 0 || startband > endband || endband > CELT_MAX_BANDS) {
1608 av_log(s->avctx, AV_LOG_ERROR, "Invalid start/end band: %d %d\n",
1609 startband, endband);
1610 return AVERROR_INVALIDDATA;
1614 s->coded_channels = coded_channels;
1615 s->startband = startband;
1616 s->endband = endband;
1617 s->framebits = rc->rb.bytes * 8;
1619 s->duration = av_log2(frame_size / CELT_SHORT_BLOCKSIZE);
1620 if (s->duration > CELT_MAX_LOG_BLOCKS ||
1621 frame_size != CELT_SHORT_BLOCKSIZE * (1 << s->duration)) {
1622 av_log(s->avctx, AV_LOG_ERROR, "Invalid CELT frame size: %d\n",
1624 return AVERROR_INVALIDDATA;
1627 if (!s->output_channels)
1628 s->output_channels = coded_channels;
1630 memset(s->frame[0].collapse_masks, 0, sizeof(s->frame[0].collapse_masks));
1631 memset(s->frame[1].collapse_masks, 0, sizeof(s->frame[1].collapse_masks));
1633 consumed = opus_rc_tell(rc);
1635 /* obtain silence flag */
1636 if (consumed >= s->framebits)
1638 else if (consumed == 1)
1639 silence = ff_opus_rc_dec_log(rc, 15);
1643 consumed = s->framebits;
1644 rc->total_read_bits += s->framebits - opus_rc_tell(rc);
1647 /* obtain post-filter options */
1648 consumed = parse_postfilter(s, rc, consumed);
1650 /* obtain transient flag */
1651 if (s->duration != 0 && consumed+3 <= s->framebits)
1652 transient = ff_opus_rc_dec_log(rc, 3);
1654 s->blocks = transient ? 1 << s->duration : 1;
1655 s->blocksize = frame_size / s->blocks;
1657 imdct = s->imdct[transient ? 0 : s->duration];
1659 if (coded_channels == 1) {
1660 for (i = 0; i < CELT_MAX_BANDS; i++)
1661 s->frame[0].energy[i] = FFMAX(s->frame[0].energy[i], s->frame[1].energy[i]);
1664 celt_decode_coarse_energy(s, rc);
1665 celt_decode_tf_changes (s, rc, transient);
1666 celt_decode_allocation (s, rc);
1667 celt_decode_fine_energy (s, rc);
1668 celt_decode_bands (s, rc);
1670 if (s->anticollapse_bit)
1671 anticollapse = ff_opus_rc_get_raw(rc, 1);
1673 celt_decode_final_energy(s, rc, s->framebits - opus_rc_tell(rc));
1675 /* apply anti-collapse processing and denormalization to
1676 * each coded channel */
1677 for (i = 0; i < s->coded_channels; i++) {
1678 CeltFrame *frame = &s->frame[i];
1681 process_anticollapse(s, frame, s->coeffs[i]);
1683 celt_denormalize(s, frame, s->coeffs[i]);
1686 /* stereo -> mono downmix */
1687 if (s->output_channels < s->coded_channels) {
1688 s->dsp->vector_fmac_scalar(s->coeffs[0], s->coeffs[1], 1.0, FFALIGN(frame_size, 16));
1690 } else if (s->output_channels > s->coded_channels)
1691 memcpy(s->coeffs[1], s->coeffs[0], frame_size * sizeof(float));
1694 for (i = 0; i < 2; i++) {
1695 CeltFrame *frame = &s->frame[i];
1697 for (j = 0; j < FF_ARRAY_ELEMS(frame->energy); j++)
1698 frame->energy[j] = CELT_ENERGY_SILENCE;
1700 memset(s->coeffs, 0, sizeof(s->coeffs));
1703 /* transform and output for each output channel */
1704 for (i = 0; i < s->output_channels; i++) {
1705 CeltFrame *frame = &s->frame[i];
1706 float m = frame->deemph_coeff;
1708 /* iMDCT and overlap-add */
1709 for (j = 0; j < s->blocks; j++) {
1710 float *dst = frame->buf + 1024 + j * s->blocksize;
1712 imdct->imdct_half(imdct, dst + CELT_OVERLAP / 2, s->coeffs[i] + j,
1713 s->blocks, imdct_scale);
1714 s->dsp->vector_fmul_window(dst, dst, dst + CELT_OVERLAP / 2,
1715 ff_celt_window, CELT_OVERLAP / 2);
1719 celt_postfilter(s, frame);
1721 /* deemphasis and output scaling */
1722 for (j = 0; j < frame_size; j++) {
1723 float tmp = frame->buf[1024 - frame_size + j] + m;
1724 m = tmp * CELT_DEEMPH_COEFF;
1725 output[i][j] = tmp / 32768.;
1727 frame->deemph_coeff = m;
1730 if (coded_channels == 1)
1731 memcpy(s->frame[1].energy, s->frame[0].energy, sizeof(s->frame[0].energy));
1733 for (i = 0; i < 2; i++ ) {
1734 CeltFrame *frame = &s->frame[i];
1737 memcpy(frame->prev_energy[1], frame->prev_energy[0], sizeof(frame->prev_energy[0]));
1738 memcpy(frame->prev_energy[0], frame->energy, sizeof(frame->prev_energy[0]));
1740 for (j = 0; j < CELT_MAX_BANDS; j++)
1741 frame->prev_energy[0][j] = FFMIN(frame->prev_energy[0][j], frame->energy[j]);
1744 for (j = 0; j < s->startband; j++) {
1745 frame->prev_energy[0][j] = CELT_ENERGY_SILENCE;
1746 frame->energy[j] = 0.0;
1748 for (j = s->endband; j < CELT_MAX_BANDS; j++) {
1749 frame->prev_energy[0][j] = CELT_ENERGY_SILENCE;
1750 frame->energy[j] = 0.0;
1754 s->seed = rc->range;
1759 void ff_celt_flush(CeltContext *s)
1766 for (i = 0; i < 2; i++) {
1767 CeltFrame *frame = &s->frame[i];
1769 for (j = 0; j < CELT_MAX_BANDS; j++)
1770 frame->prev_energy[0][j] = frame->prev_energy[1][j] = CELT_ENERGY_SILENCE;
1772 memset(frame->energy, 0, sizeof(frame->energy));
1773 memset(frame->buf, 0, sizeof(frame->buf));
1775 memset(frame->pf_gains, 0, sizeof(frame->pf_gains));
1776 memset(frame->pf_gains_old, 0, sizeof(frame->pf_gains_old));
1777 memset(frame->pf_gains_new, 0, sizeof(frame->pf_gains_new));
1779 frame->deemph_coeff = 0.0;
1786 void ff_celt_free(CeltContext **ps)
1788 CeltContext *s = *ps;
1794 for (i = 0; i < FF_ARRAY_ELEMS(s->imdct); i++)
1795 ff_imdct15_uninit(&s->imdct[i]);
1801 int ff_celt_init(AVCodecContext *avctx, CeltContext **ps, int output_channels)
1806 if (output_channels != 1 && output_channels != 2) {
1807 av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n",
1809 return AVERROR(EINVAL);
1812 s = av_mallocz(sizeof(*s));
1814 return AVERROR(ENOMEM);
1817 s->output_channels = output_channels;
1819 for (i = 0; i < FF_ARRAY_ELEMS(s->imdct); i++) {
1820 ret = ff_imdct15_init(&s->imdct[i], i + 3);
1825 s->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
1827 ret = AVERROR(ENOMEM);