]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/opus_pvq.c
avcodec/hevcdec: move SEI message parsing into a separate header
[ffmpeg] / libavcodec / opus_pvq.c
index e424cb2e03c9b83854e26392793ba9ac2dc517d3..fa349c47da08387f4b6ec3694ef2ced987c825f8 100644 (file)
@@ -33,7 +33,7 @@ static inline int16_t celt_cos(int16_t x)
 {
     x = (MUL16(x, x) + 4096) >> 13;
     x = (32767-x) + ROUND_MUL16(x, (-7651 + ROUND_MUL16(x, (8277 + ROUND_MUL16(-626, x)))));
-    return 1+x;
+    return x + 1;
 }
 
 static inline int celt_log2tan(int isin, int icos)
@@ -89,18 +89,16 @@ static void celt_exp_rotation_impl(float *X, uint32_t len, uint32_t stride,
 
     Xptr = X;
     for (i = 0; i < len - stride; i++) {
-        float x1, x2;
-        x1           = Xptr[0];
-        x2           = Xptr[stride];
+        float x1     = Xptr[0];
+        float x2     = Xptr[stride];
         Xptr[stride] = c * x2 + s * x1;
         *Xptr++      = c * x1 - s * x2;
     }
 
     Xptr = &X[len - 2 * stride - 1];
     for (i = len - 2 * stride - 1; i >= 0; i--) {
-        float x1, x2;
-        x1           = Xptr[0];
-        x2           = Xptr[stride];
+        float x1     = Xptr[0];
+        float x2     = Xptr[stride];
         Xptr[stride] = c * x2 + s * x1;
         *Xptr--      = c * x1 - s * x2;
     }
@@ -132,8 +130,6 @@ static inline void celt_exp_rotation(float *X, uint32_t len,
             stride2++;
     }
 
-    /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
-    extract_collapse_mask().*/
     len /= stride;
     for (i = 0; i < stride; i++) {
         if (encode) {
@@ -150,20 +146,15 @@ static inline void celt_exp_rotation(float *X, uint32_t len,
 
 static inline uint32_t celt_extract_collapse_mask(const int *iy, uint32_t N, uint32_t B)
 {
-    uint32_t collapse_mask;
-    int N0;
-    int i, j;
+    int i, j, N0 = N / B;
+    uint32_t collapse_mask = 0;
 
     if (B <= 1)
         return 1;
 
-    /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
-    exp_rotation().*/
-    N0 = N/B;
-    collapse_mask = 0;
     for (i = 0; i < B; i++)
         for (j = 0; j < N0; j++)
-            collapse_mask |= (iy[i*N0+j]!=0)<<i;
+            collapse_mask |= (!!iy[i*N0+j]) << i;
     return collapse_mask;
 }
 
@@ -173,7 +164,7 @@ static inline void celt_stereo_merge(float *X, float *Y, float mid, int N)
     float xp = 0, side = 0;
     float E[2];
     float mid2;
-    float t, gain[2];
+    float gain[2];
 
     /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
     for (i = 0; i < N; i++) {
@@ -192,10 +183,8 @@ static inline void celt_stereo_merge(float *X, float *Y, float mid, int N)
         return;
     }
 
-    t = E[0];
-    gain[0] = 1.0f / sqrtf(t);
-    t = E[1];
-    gain[1] = 1.0f / sqrtf(t);
+    gain[0] = 1.0f / sqrtf(E[0]);
+    gain[1] = 1.0f / sqrtf(E[1]);
 
     for (i = 0; i < N; i++) {
         float value[2];
@@ -210,43 +199,27 @@ static inline void celt_stereo_merge(float *X, float *Y, float mid, int N)
 static void celt_interleave_hadamard(float *tmp, float *X, int N0,
                                      int stride, int hadamard)
 {
-    int i, j;
-    int N = N0*stride;
+    int i, j, N = N0*stride;
+    const uint8_t *order = &ff_celt_hadamard_order[hadamard ? stride - 2 : 30];
 
-    if (hadamard) {
-        const uint8_t *ordery = ff_celt_hadamard_ordery + stride - 2;
-        for (i = 0; i < stride; i++)
-            for (j = 0; j < N0; j++)
-                tmp[j*stride+i] = X[ordery[i]*N0+j];
-    } else {
-        for (i = 0; i < stride; i++)
-            for (j = 0; j < N0; j++)
-                tmp[j*stride+i] = X[i*N0+j];
-    }
+    for (i = 0; i < stride; i++)
+        for (j = 0; j < N0; j++)
+            tmp[j*stride+i] = X[order[i]*N0+j];
 
-    for (i = 0; i < N; i++)
-        X[i] = tmp[i];
+    memcpy(X, tmp, N*sizeof(float));
 }
 
 static void celt_deinterleave_hadamard(float *tmp, float *X, int N0,
                                        int stride, int hadamard)
 {
-    int i, j;
-    int N = N0*stride;
+    int i, j, N = N0*stride;
+    const uint8_t *order = &ff_celt_hadamard_order[hadamard ? stride - 2 : 30];
 
-    if (hadamard) {
-        const uint8_t *ordery = ff_celt_hadamard_ordery + stride - 2;
-        for (i = 0; i < stride; i++)
-            for (j = 0; j < N0; j++)
-                tmp[ordery[i]*N0+j] = X[j*stride+i];
-    } else {
-        for (i = 0; i < stride; i++)
-            for (j = 0; j < N0; j++)
-                tmp[i*N0+j] = X[j*stride+i];
-    }
+    for (i = 0; i < stride; i++)
+        for (j = 0; j < N0; j++)
+            tmp[order[i]*N0+j] = X[j*stride+i];
 
-    for (i = 0; i < N; i++)
-        X[i] = tmp[i];
+    memcpy(X, tmp, N*sizeof(float));
 }
 
 static void celt_haar1(float *X, int N0, int stride)
@@ -264,11 +237,11 @@ static void celt_haar1(float *X, int N0, int stride)
 }
 
 static inline int celt_compute_qn(int N, int b, int offset, int pulse_cap,
-                                  int dualstereo)
+                                  int stereo)
 {
     int qn, qb;
     int N2 = 2 * N - 1;
-    if (dualstereo && N == 2)
+    if (stereo && N == 2)
         N2--;
 
     /* The upper limit ensures that in a stereo split with itheta==16384, we'll
@@ -295,13 +268,11 @@ static inline uint32_t celt_icwrsi(uint32_t N, uint32_t K, const int *y)
 static inline uint64_t celt_cwrsi(uint32_t N, uint32_t K, uint32_t i, int *y)
 {
     uint64_t norm = 0;
-    uint32_t p;
+    uint32_t q, p;
     int s, val;
     int k0;
 
     while (N > 2) {
-        uint32_t q;
-
         /*Lots of pulses case:*/
         if (K >= N) {
             const uint32_t *row = ff_celt_pvq_u_row[N];
@@ -441,9 +412,10 @@ static int celt_pvq_search(float *X, int *y, int K, int N)
 }
 
 static uint32_t celt_alg_quant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K,
-                               enum CeltSpread spread, uint32_t blocks, float gain)
+                               enum CeltSpread spread, uint32_t blocks, float gain,
+                               void *scratch)
 {
-    int y[176];
+    int *y = scratch;
 
     celt_exp_rotation(X, N, blocks, K, spread, 1);
     gain /= sqrtf(celt_pvq_search(X, y, K, N));
@@ -456,9 +428,10 @@ static uint32_t celt_alg_quant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_
 /** Decode pulse vector and combine the result with the pitch vector to produce
     the final normalised signal in the current band. */
 static uint32_t celt_alg_unquant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K,
-                                 enum CeltSpread spread, uint32_t blocks, float gain)
+                                 enum CeltSpread spread, uint32_t blocks, float gain,
+                                 void *scratch)
 {
-    int y[176];
+    int *y = scratch;
 
     gain /= sqrtf(celt_decode_pulses(rc, y, N, K));
     celt_normalize_residual(y, X, N, gain);
@@ -466,373 +439,19 @@ static uint32_t celt_alg_unquant(OpusRangeCoder *rc, float *X, uint32_t N, uint3
     return celt_extract_collapse_mask(y, N, blocks);
 }
 
-uint32_t ff_celt_decode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
-                             float *X, float *Y, int N, int b, uint32_t blocks,
-                             float *lowband, int duration, float *lowband_out, int level,
-                             float gain, float *lowband_scratch, int fill)
-{
-    const uint8_t *cache;
-    int dualstereo, split;
-    int imid = 0, iside = 0;
-    uint32_t N0 = N;
-    int N_B;
-    int N_B0;
-    int B0 = blocks;
-    int time_divide = 0;
-    int recombine = 0;
-    int inv = 0;
-    float mid = 0, side = 0;
-    int longblocks = (B0 == 1);
-    uint32_t cm = 0;
-
-    N_B0 = N_B = N / blocks;
-    split = dualstereo = (Y != NULL);
-
-    if (N == 1) {
-        /* special case for one sample */
-        int i;
-        float *x = X;
-        for (i = 0; i <= dualstereo; i++) {
-            int sign = 0;
-            if (f->remaining2 >= 1<<3) {
-                sign           = ff_opus_rc_get_raw(rc, 1);
-                f->remaining2 -= 1 << 3;
-                b             -= 1 << 3;
-            }
-            x[0] = sign ? -1.0f : 1.0f;
-            x = Y;
-        }
-        if (lowband_out)
-            lowband_out[0] = X[0];
-        return 1;
-    }
-
-    if (!dualstereo && level == 0) {
-        int tf_change = f->tf_change[band];
-        int k;
-        if (tf_change > 0)
-            recombine = tf_change;
-        /* Band recombining to increase frequency resolution */
-
-        if (lowband &&
-            (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) {
-            int j;
-            for (j = 0; j < N; j++)
-                lowband_scratch[j] = lowband[j];
-            lowband = lowband_scratch;
-        }
-
-        for (k = 0; k < recombine; k++) {
-            if (lowband)
-                celt_haar1(lowband, N >> k, 1 << k);
-            fill = ff_celt_bit_interleave[fill & 0xF] | ff_celt_bit_interleave[fill >> 4] << 2;
-        }
-        blocks >>= recombine;
-        N_B <<= recombine;
-
-        /* Increasing the time resolution */
-        while ((N_B & 1) == 0 && tf_change < 0) {
-            if (lowband)
-                celt_haar1(lowband, N_B, blocks);
-            fill |= fill << blocks;
-            blocks <<= 1;
-            N_B >>= 1;
-            time_divide++;
-            tf_change++;
-        }
-        B0 = blocks;
-        N_B0 = N_B;
-
-        /* Reorganize the samples in time order instead of frequency order */
-        if (B0 > 1 && lowband)
-            celt_deinterleave_hadamard(f->scratch, lowband, N_B >> recombine,
-                                       B0 << recombine, longblocks);
-    }
-
-    /* If we need 1.5 more bit than we can produce, split the band in two. */
-    cache = ff_celt_cache_bits +
-            ff_celt_cache_index[(duration + 1) * CELT_MAX_BANDS + band];
-    if (!dualstereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) {
-        N >>= 1;
-        Y = X + N;
-        split = 1;
-        duration -= 1;
-        if (blocks == 1)
-            fill = (fill & 1) | (fill << 1);
-        blocks = (blocks + 1) >> 1;
-    }
-
-    if (split) {
-        int qn;
-        int itheta = 0;
-        int mbits, sbits, delta;
-        int qalloc;
-        int pulse_cap;
-        int offset;
-        int orig_fill;
-        int tell;
-
-        /* Decide on the resolution to give to the split parameter theta */
-        pulse_cap = ff_celt_log_freq_range[band] + duration * 8;
-        offset = (pulse_cap >> 1) - (dualstereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE :
-                                                          CELT_QTHETA_OFFSET);
-        qn = (dualstereo && band >= f->intensity_stereo) ? 1 :
-             celt_compute_qn(N, b, offset, pulse_cap, dualstereo);
-        tell = opus_rc_tell_frac(rc);
-        if (qn != 1) {
-            /* Entropy coding of the angle. We use a uniform pdf for the
-            time split, a step for stereo, and a triangular one for the rest. */
-            if (dualstereo && N > 2)
-                itheta = ff_opus_rc_dec_uint_step(rc, qn/2);
-            else if (dualstereo || B0 > 1)
-                itheta = ff_opus_rc_dec_uint(rc, qn+1);
-            else
-                itheta = ff_opus_rc_dec_uint_tri(rc, qn);
-            itheta = itheta * 16384 / qn;
-            /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate.
-            Let's do that at higher complexity */
-        } else if (dualstereo) {
-            inv = (b > 2 << 3 && f->remaining2 > 2 << 3) ? ff_opus_rc_dec_log(rc, 2) : 0;
-            itheta = 0;
-        }
-        qalloc = opus_rc_tell_frac(rc) - tell;
-        b -= qalloc;
-
-        orig_fill = fill;
-        if (itheta == 0) {
-            imid = 32767;
-            iside = 0;
-            fill = av_mod_uintp2(fill, blocks);
-            delta = -16384;
-        } else if (itheta == 16384) {
-            imid = 0;
-            iside = 32767;
-            fill &= ((1 << blocks) - 1) << blocks;
-            delta = 16384;
-        } else {
-            imid = celt_cos(itheta);
-            iside = celt_cos(16384-itheta);
-            /* This is the mid vs side allocation that minimizes squared error
-            in that band. */
-            delta = ROUND_MUL16((N - 1) << 7, celt_log2tan(iside, imid));
-        }
-
-        mid  = imid  / 32768.0f;
-        side = iside / 32768.0f;
-
-        /* This is a special case for N=2 that only works for stereo and takes
-        advantage of the fact that mid and side are orthogonal to encode
-        the side with just one bit. */
-        if (N == 2 && dualstereo) {
-            int c;
-            int sign = 0;
-            float tmp;
-            float *x2, *y2;
-            mbits = b;
-            /* Only need one bit for the side */
-            sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0;
-            mbits -= sbits;
-            c = (itheta > 8192);
-            f->remaining2 -= qalloc+sbits;
-
-            x2 = c ? Y : X;
-            y2 = c ? X : Y;
-            if (sbits)
-                sign = ff_opus_rc_get_raw(rc, 1);
-            sign = 1 - 2 * sign;
-            /* We use orig_fill here because we want to fold the side, but if
-            itheta==16384, we'll have cleared the low bits of fill. */
-            cm = ff_celt_decode_band(f, rc, band, x2, NULL, N, mbits, blocks,
-                                     lowband, duration, lowband_out, level, gain,
-                                     lowband_scratch, orig_fill);
-            /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
-            and there's no need to worry about mixing with the other channel. */
-            y2[0] = -sign * x2[1];
-            y2[1] =  sign * x2[0];
-            X[0] *= mid;
-            X[1] *= mid;
-            Y[0] *= side;
-            Y[1] *= side;
-            tmp = X[0];
-            X[0] = tmp - Y[0];
-            Y[0] = tmp + Y[0];
-            tmp = X[1];
-            X[1] = tmp - Y[1];
-            Y[1] = tmp + Y[1];
-        } else {
-            /* "Normal" split code */
-            float *next_lowband2     = NULL;
-            float *next_lowband_out1 = NULL;
-            int next_level = 0;
-            int rebalance;
-
-            /* Give more bits to low-energy MDCTs than they would
-             * otherwise deserve */
-            if (B0 > 1 && !dualstereo && (itheta & 0x3fff)) {
-                if (itheta > 8192)
-                    /* Rough approximation for pre-echo masking */
-                    delta -= delta >> (4 - duration);
-                else
-                    /* Corresponds to a forward-masking slope of
-                     * 1.5 dB per 10 ms */
-                    delta = FFMIN(0, delta + (N << 3 >> (5 - duration)));
-            }
-            mbits = av_clip((b - delta) / 2, 0, b);
-            sbits = b - mbits;
-            f->remaining2 -= qalloc;
-
-            if (lowband && !dualstereo)
-                next_lowband2 = lowband + N; /* >32-bit split case */
-
-            /* Only stereo needs to pass on lowband_out.
-             * Otherwise, it's handled at the end */
-            if (dualstereo)
-                next_lowband_out1 = lowband_out;
-            else
-                next_level = level + 1;
-
-            rebalance = f->remaining2;
-            if (mbits >= sbits) {
-                /* In stereo mode, we do not apply a scaling to the mid
-                 * because we need the normalized mid for folding later */
-                cm = ff_celt_decode_band(f, rc, band, X, NULL, N, mbits, blocks,
-                                         lowband, duration, next_lowband_out1,
-                                         next_level, dualstereo ? 1.0f : (gain * mid),
-                                         lowband_scratch, fill);
-
-                rebalance = mbits - (rebalance - f->remaining2);
-                if (rebalance > 3 << 3 && itheta != 0)
-                    sbits += rebalance - (3 << 3);
-
-                /* For a stereo split, the high bits of fill are always zero,
-                 * so no folding will be done to the side. */
-                cm |= ff_celt_decode_band(f, rc, band, Y, NULL, N, sbits, blocks,
-                                          next_lowband2, duration, NULL,
-                                          next_level, gain * side, NULL,
-                                          fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
-            } else {
-                /* For a stereo split, the high bits of fill are always zero,
-                 * so no folding will be done to the side. */
-                cm = ff_celt_decode_band(f, rc, band, Y, NULL, N, sbits, blocks,
-                                         next_lowband2, duration, NULL,
-                                         next_level, gain * side, NULL,
-                                         fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
-
-                rebalance = sbits - (rebalance - f->remaining2);
-                if (rebalance > 3 << 3 && itheta != 16384)
-                    mbits += rebalance - (3 << 3);
-
-                /* In stereo mode, we do not apply a scaling to the mid because
-                 * we need the normalized mid for folding later */
-                cm |= ff_celt_decode_band(f, rc, band, X, NULL, N, mbits, blocks,
-                                          lowband, duration, next_lowband_out1,
-                                          next_level, dualstereo ? 1.0f : (gain * mid),
-                                          lowband_scratch, fill);
-            }
-        }
-    } else {
-        /* This is the basic no-split case */
-        uint32_t q         = celt_bits2pulses(cache, b);
-        uint32_t curr_bits = celt_pulses2bits(cache, q);
-        f->remaining2 -= curr_bits;
-
-        /* Ensures we can never bust the budget */
-        while (f->remaining2 < 0 && q > 0) {
-            f->remaining2 += curr_bits;
-            curr_bits      = celt_pulses2bits(cache, --q);
-            f->remaining2 -= curr_bits;
-        }
-
-        if (q != 0) {
-            /* Finally do the actual quantization */
-            cm = celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
-                                  f->spread, blocks, gain);
-        } else {
-            /* If there's no pulse, fill the band anyway */
-            int j;
-            uint32_t cm_mask = (1 << blocks) - 1;
-            fill &= cm_mask;
-            if (!fill) {
-                for (j = 0; j < N; j++)
-                    X[j] = 0.0f;
-            } else {
-                if (!lowband) {
-                    /* Noise */
-                    for (j = 0; j < N; j++)
-                        X[j] = (((int32_t)celt_rng(f)) >> 20);
-                    cm = cm_mask;
-                } else {
-                    /* Folded spectrum */
-                    for (j = 0; j < N; j++) {
-                        /* About 48 dB below the "normal" folding level */
-                        X[j] = lowband[j] + (((celt_rng(f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256);
-                    }
-                    cm = fill;
-                }
-                celt_renormalize_vector(X, N, gain);
-            }
-        }
-    }
-
-    /* This code is used by the decoder and by the resynthesis-enabled encoder */
-    if (dualstereo) {
-        int j;
-        if (N != 2)
-            celt_stereo_merge(X, Y, mid, N);
-        if (inv) {
-            for (j = 0; j < N; j++)
-                Y[j] *= -1;
-        }
-    } else if (level == 0) {
-        int k;
-
-        /* Undo the sample reorganization going from time order to frequency order */
-        if (B0 > 1)
-            celt_interleave_hadamard(f->scratch, X, N_B>>recombine,
-                                     B0<<recombine, longblocks);
-
-        /* Undo time-freq changes that we did earlier */
-        N_B = N_B0;
-        blocks = B0;
-        for (k = 0; k < time_divide; k++) {
-            blocks >>= 1;
-            N_B <<= 1;
-            cm |= cm >> blocks;
-            celt_haar1(X, N_B, blocks);
-        }
-
-        for (k = 0; k < recombine; k++) {
-            cm = ff_celt_bit_deinterleave[cm];
-            celt_haar1(X, N0>>k, 1<<k);
-        }
-        blocks <<= recombine;
-
-        /* Scale output for later folding */
-        if (lowband_out) {
-            int j;
-            float n = sqrtf(N0);
-            for (j = 0; j < N0; j++)
-                lowband_out[j] = n * X[j];
-        }
-        cm = av_mod_uintp2(cm, blocks);
-    }
-
-    return cm;
-}
-
-/* This has to be, AND MUST BE done by the psychoacoustic system, this has a very
- * big impact on the entire quantization and especially huge on transients */
 static int celt_calc_theta(const float *X, const float *Y, int coupling, int N)
 {
-    int j;
+    int i;
     float e[2] = { 0.0f, 0.0f };
-    for (j = 0; j < N; j++) {
-        if (coupling) { /* Coupling case */
-            e[0] += (X[j] + Y[j])*(X[j] + Y[j]);
-            e[1] += (X[j] - Y[j])*(X[j] - Y[j]);
-        } else {
-            e[0] += X[j]*X[j];
-            e[1] += Y[j]*Y[j];
+    if (coupling) { /* Coupling case */
+        for (i = 0; i < N; i++) {
+            e[0] += (X[i] + Y[i])*(X[i] + Y[i]);
+            e[1] += (X[i] - Y[i])*(X[i] - Y[i]);
+        }
+    } else {
+        for (i = 0; i < N; i++) {
+            e[0] += X[i]*X[i];
+            e[1] += Y[i]*Y[i];
         }
     }
     return lrintf(32768.0f*atan2f(sqrtf(e[1]), sqrtf(e[0]))/M_PI);
@@ -851,21 +470,30 @@ static void celt_stereo_is_decouple(float *X, float *Y, float e_l, float e_r, in
 static void celt_stereo_ms_decouple(float *X, float *Y, int N)
 {
     int i;
-    const float decouple_norm = 1.0f/sqrtf(1.0f + 1.0f);
     for (i = 0; i < N; i++) {
         const float Xret = X[i];
-        X[i] = (X[i] + Y[i])*decouple_norm;
-        Y[i] = (Y[i] - Xret)*decouple_norm;
+        X[i] = (X[i] + Y[i])*M_SQRT1_2;
+        Y[i] = (Y[i] - Xret)*M_SQRT1_2;
     }
 }
 
-uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
-                             float *X, float *Y, int N, int b, uint32_t blocks,
-                             float *lowband, int duration, float *lowband_out, int level,
-                             float gain, float *lowband_scratch, int fill)
+#define QUANT_FN(name) uint32_t (*name)(CeltFrame *f, OpusRangeCoder *rc,      \
+                                        const int band, float *X, float *Y,    \
+                                        int N, int b, uint32_t blocks,         \
+                                        float *lowband, int duration,          \
+                                        float *lowband_out, int level,         \
+                                        float gain, float *lowband_scratch,    \
+                                        int fill)
+
+static av_always_inline uint32_t quant_band_template(CeltFrame *f, OpusRangeCoder *rc, const int band,
+                                                     float *X, float *Y, int N, int b, uint32_t blocks,
+                                                     float *lowband, int duration, float *lowband_out,
+                                                     int level, float gain, float *lowband_scratch,
+                                                     int fill, int quant)
 {
+    int i;
     const uint8_t *cache;
-    int dualstereo, split;
+    int stereo = !!Y, split = stereo;
     int imid = 0, iside = 0;
     uint32_t N0 = N;
     int N_B = N / blocks;
@@ -877,20 +505,22 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
     float mid = 0, side = 0;
     int longblocks = (B0 == 1);
     uint32_t cm = 0;
-
-    split = dualstereo = (Y != NULL);
+    QUANT_FN(rec) = quant ? ff_celt_encode_band : ff_celt_decode_band;
 
     if (N == 1) {
-        /* special case for one sample - the decoder's output will be +- 1.0f!!! */
-        int i;
         float *x = X;
-        for (i = 0; i <= dualstereo; i++) {
-            if (f->remaining2 >= 1<<3) {
-                ff_opus_rc_put_raw(rc, x[0] < 0, 1);
+        for (i = 0; i <= stereo; i++) {
+            int sign = 0;
+            if (f->remaining2 >= 1 << 3) {
+                if (quant) {
+                    sign = x[0] < 0;
+                    ff_opus_rc_put_raw(rc, sign, 1);
+                } else {
+                    sign = ff_opus_rc_get_raw(rc, 1);
+                }
                 f->remaining2 -= 1 << 3;
-                b             -= 1 << 3;
             }
-            x[0] = 1.0f - 2.0f*(x[0] < 0);
+            x[0] = 1.0f - 2.0f*sign;
             x = Y;
         }
         if (lowband_out)
@@ -898,7 +528,7 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
         return 1;
     }
 
-    if (!dualstereo && level == 0) {
+    if (!stereo && level == 0) {
         int tf_change = f->tf_change[band];
         int k;
         if (tf_change > 0)
@@ -907,14 +537,14 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
 
         if (lowband &&
             (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) {
-            int j;
-            for (j = 0; j < N; j++)
-                lowband_scratch[j] = lowband[j];
+            for (i = 0; i < N; i++)
+                lowband_scratch[i] = lowband[i];
             lowband = lowband_scratch;
         }
 
         for (k = 0; k < recombine; k++) {
-            celt_haar1(X, N >> k, 1 << k);
+            if (quant || lowband)
+                celt_haar1(quant ? X : lowband, N >> k, 1 << k);
             fill = ff_celt_bit_interleave[fill & 0xF] | ff_celt_bit_interleave[fill >> 4] << 2;
         }
         blocks >>= recombine;
@@ -922,7 +552,8 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
 
         /* Increasing the time resolution */
         while ((N_B & 1) == 0 && tf_change < 0) {
-            celt_haar1(X, N_B, blocks);
+            if (quant || lowband)
+                celt_haar1(quant ? X : lowband, N_B, blocks);
             fill |= fill << blocks;
             blocks <<= 1;
             N_B >>= 1;
@@ -933,15 +564,16 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
         N_B0 = N_B;
 
         /* Reorganize the samples in time order instead of frequency order */
-        if (B0 > 1)
-            celt_deinterleave_hadamard(f->scratch, X, N_B >> recombine,
-                                       B0 << recombine, longblocks);
+        if (B0 > 1 && (quant || lowband))
+            celt_deinterleave_hadamard(f->scratch, quant ? X : lowband,
+                                       N_B >> recombine, B0 << recombine,
+                                       longblocks);
     }
 
     /* If we need 1.5 more bit than we can produce, split the band in two. */
     cache = ff_celt_cache_bits +
             ff_celt_cache_index[(duration + 1) * CELT_MAX_BANDS + band];
-    if (!dualstereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) {
+    if (!stereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) {
         N >>= 1;
         Y = X + N;
         split = 1;
@@ -953,7 +585,7 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
 
     if (split) {
         int qn;
-        int itheta = celt_calc_theta(X, Y, dualstereo, N);
+        int itheta = quant ? celt_calc_theta(X, Y, stereo, N) : 0;
         int mbits, sbits, delta;
         int qalloc;
         int pulse_cap;
@@ -963,49 +595,58 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
 
         /* Decide on the resolution to give to the split parameter theta */
         pulse_cap = ff_celt_log_freq_range[band] + duration * 8;
-        offset = (pulse_cap >> 1) - (dualstereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE :
+        offset = (pulse_cap >> 1) - (stereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE :
                                                           CELT_QTHETA_OFFSET);
-        qn = (dualstereo && band >= f->intensity_stereo) ? 1 :
-             celt_compute_qn(N, b, offset, pulse_cap, dualstereo);
+        qn = (stereo && band >= f->intensity_stereo) ? 1 :
+             celt_compute_qn(N, b, offset, pulse_cap, stereo);
         tell = opus_rc_tell_frac(rc);
-
         if (qn != 1) {
-
-            itheta = (itheta*qn + 8192) >> 14;
-
+            if (quant)
+                itheta = (itheta*qn + 8192) >> 14;
             /* Entropy coding of the angle. We use a uniform pdf for the
              * time split, a step for stereo, and a triangular one for the rest. */
-            if (dualstereo && N > 2)
-                ff_opus_rc_enc_uint_step(rc, itheta, qn / 2);
-            else if (dualstereo || B0 > 1)
-                ff_opus_rc_enc_uint(rc, itheta, qn + 1);
-            else
-                ff_opus_rc_enc_uint_tri(rc, itheta, qn);
-            itheta = itheta * 16384 / qn;
-
-            if (dualstereo) {
-                if (itheta == 0)
-                    celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band],
-                                            f->block[1].lin_energy[band], N);
+            if (quant) {
+                if (stereo && N > 2)
+                    ff_opus_rc_enc_uint_step(rc, itheta, qn / 2);
+                else if (stereo || B0 > 1)
+                    ff_opus_rc_enc_uint(rc, itheta, qn + 1);
                 else
-                    celt_stereo_ms_decouple(X, Y, N);
+                    ff_opus_rc_enc_uint_tri(rc, itheta, qn);
+                itheta = itheta * 16384 / qn;
+                if (stereo) {
+                    if (itheta == 0)
+                        celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band],
+                                                f->block[1].lin_energy[band], N);
+                    else
+                        celt_stereo_ms_decouple(X, Y, N);
+                }
+            } else {
+                if (stereo && N > 2)
+                    itheta = ff_opus_rc_dec_uint_step(rc, qn / 2);
+                else if (stereo || B0 > 1)
+                    itheta = ff_opus_rc_dec_uint(rc, qn+1);
+                else
+                    itheta = ff_opus_rc_dec_uint_tri(rc, qn);
+                itheta = itheta * 16384 / qn;
             }
-        } else if (dualstereo) {
-             inv = itheta > 8192;
-             if (inv) {
-                int j;
-                for (j = 0; j < N; j++)
-                   Y[j] = -Y[j];
-             }
-             celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band],
-                                     f->block[1].lin_energy[band], N);
-
-            if (b > 2 << 3 && f->remaining2 > 2 << 3) {
-                ff_opus_rc_enc_log(rc, inv, 2);
+        } else if (stereo) {
+            if (quant) {
+                inv = itheta > 8192;
+                 if (inv) {
+                    for (i = 0; i < N; i++)
+                       Y[i] *= -1;
+                 }
+                 celt_stereo_is_decouple(X, Y, f->block[0].lin_energy[band],
+                                         f->block[1].lin_energy[band], N);
+
+                if (b > 2 << 3 && f->remaining2 > 2 << 3) {
+                    ff_opus_rc_enc_log(rc, inv, 2);
+                } else {
+                    inv = 0;
+                }
             } else {
-                inv = 0;
+                inv = (b > 2 << 3 && f->remaining2 > 2 << 3) ? ff_opus_rc_dec_log(rc, 2) : 0;
             }
-
             itheta = 0;
         }
         qalloc = opus_rc_tell_frac(rc) - tell;
@@ -1036,7 +677,7 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
         /* This is a special case for N=2 that only works for stereo and takes
         advantage of the fact that mid and side are orthogonal to encode
         the side with just one bit. */
-        if (N == 2 && dualstereo) {
+        if (N == 2 && stereo) {
             int c;
             int sign = 0;
             float tmp;
@@ -1051,15 +692,18 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
             x2 = c ? Y : X;
             y2 = c ? X : Y;
             if (sbits) {
-                sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
-                ff_opus_rc_put_raw(rc, sign, 1);
+                if (quant) {
+                    sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
+                    ff_opus_rc_put_raw(rc, sign, 1);
+                } else {
+                    sign = ff_opus_rc_get_raw(rc, 1);
+                }
             }
             sign = 1 - 2 * sign;
             /* We use orig_fill here because we want to fold the side, but if
             itheta==16384, we'll have cleared the low bits of fill. */
-            cm = ff_celt_encode_band(f, rc, band, x2, NULL, N, mbits, blocks,
-                                     lowband, duration, lowband_out, level, gain,
-                                     lowband_scratch, orig_fill);
+            cm = rec(f, rc, band, x2, NULL, N, mbits, blocks, lowband, duration,
+                     lowband_out, level, gain, lowband_scratch, orig_fill);
             /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
             and there's no need to worry about mixing with the other channel. */
             y2[0] = -sign * x2[1];
@@ -1080,10 +724,11 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
             float *next_lowband_out1 = NULL;
             int next_level = 0;
             int rebalance;
+            uint32_t cmt;
 
             /* Give more bits to low-energy MDCTs than they would
              * otherwise deserve */
-            if (B0 > 1 && !dualstereo && (itheta & 0x3fff)) {
+            if (B0 > 1 && !stereo && (itheta & 0x3fff)) {
                 if (itheta > 8192)
                     /* Rough approximation for pre-echo masking */
                     delta -= delta >> (4 - duration);
@@ -1096,12 +741,12 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
             sbits = b - mbits;
             f->remaining2 -= qalloc;
 
-            if (lowband && !dualstereo)
+            if (lowband && !stereo)
                 next_lowband2 = lowband + N; /* >32-bit split case */
 
             /* Only stereo needs to pass on lowband_out.
              * Otherwise, it's handled at the end */
-            if (dualstereo)
+            if (stereo)
                 next_lowband_out1 = lowband_out;
             else
                 next_level = level + 1;
@@ -1110,39 +755,34 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
             if (mbits >= sbits) {
                 /* In stereo mode, we do not apply a scaling to the mid
                  * because we need the normalized mid for folding later */
-                cm = ff_celt_encode_band(f, rc, band, X, NULL, N, mbits, blocks,
-                                         lowband, duration, next_lowband_out1,
-                                         next_level, dualstereo ? 1.0f : (gain * mid),
-                                         lowband_scratch, fill);
-
+                cm = rec(f, rc, band, X, NULL, N, mbits, blocks, lowband,
+                         duration, next_lowband_out1, next_level,
+                         stereo ? 1.0f : (gain * mid), lowband_scratch, fill);
                 rebalance = mbits - (rebalance - f->remaining2);
                 if (rebalance > 3 << 3 && itheta != 0)
                     sbits += rebalance - (3 << 3);
 
                 /* For a stereo split, the high bits of fill are always zero,
                  * so no folding will be done to the side. */
-                cm |= ff_celt_encode_band(f, rc, band, Y, NULL, N, sbits, blocks,
-                                          next_lowband2, duration, NULL,
-                                          next_level, gain * side, NULL,
-                                          fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
+                cmt = rec(f, rc, band, Y, NULL, N, sbits, blocks, next_lowband2,
+                          duration, NULL, next_level, gain * side, NULL,
+                          fill >> blocks);
+                cm |= cmt << ((B0 >> 1) & (stereo - 1));
             } else {
                 /* For a stereo split, the high bits of fill are always zero,
                  * so no folding will be done to the side. */
-                cm = ff_celt_encode_band(f, rc, band, Y, NULL, N, sbits, blocks,
-                                         next_lowband2, duration, NULL,
-                                         next_level, gain * side, NULL,
-                                         fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
-
+                cm = rec(f, rc, band, Y, NULL, N, sbits, blocks, next_lowband2,
+                         duration, NULL, next_level, gain * side, NULL, fill >> blocks);
+                cm <<= ((B0 >> 1) & (stereo - 1));
                 rebalance = sbits - (rebalance - f->remaining2);
                 if (rebalance > 3 << 3 && itheta != 16384)
                     mbits += rebalance - (3 << 3);
 
                 /* In stereo mode, we do not apply a scaling to the mid because
                  * we need the normalized mid for folding later */
-                cm |= ff_celt_encode_band(f, rc, band, X, NULL, N, mbits, blocks,
-                                          lowband, duration, next_lowband_out1,
-                                          next_level, dualstereo ? 1.0f : (gain * mid),
-                                          lowband_scratch, fill);
+                cm |= rec(f, rc, band, X, NULL, N, mbits, blocks, lowband, duration,
+                          next_lowband_out1, next_level, stereo ? 1.0f : (gain * mid),
+                          lowband_scratch, fill);
             }
         }
     } else {
@@ -1159,44 +799,46 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
         }
 
         if (q != 0) {
-            /* Finally do the actual quantization */
-            cm = celt_alg_quant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
-                                f->spread, blocks, gain);
+            /* Finally do the actual (de)quantization */
+            if (quant) {
+                cm = celt_alg_quant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
+                                    f->spread, blocks, gain, f->scratch);
+            } else {
+                cm = celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
+                                      f->spread, blocks, gain, f->scratch);
+            }
         } else {
             /* If there's no pulse, fill the band anyway */
-            int j;
             uint32_t cm_mask = (1 << blocks) - 1;
             fill &= cm_mask;
-            if (!fill) {
-                for (j = 0; j < N; j++)
-                    X[j] = 0.0f;
-            } else {
+            if (fill) {
                 if (!lowband) {
                     /* Noise */
-                    for (j = 0; j < N; j++)
-                        X[j] = (((int32_t)celt_rng(f)) >> 20);
+                    for (i = 0; i < N; i++)
+                        X[i] = (((int32_t)celt_rng(f)) >> 20);
                     cm = cm_mask;
                 } else {
                     /* Folded spectrum */
-                    for (j = 0; j < N; j++) {
+                    for (i = 0; i < N; i++) {
                         /* About 48 dB below the "normal" folding level */
-                        X[j] = lowband[j] + (((celt_rng(f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256);
+                        X[i] = lowband[i] + (((celt_rng(f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256);
                     }
                     cm = fill;
                 }
                 celt_renormalize_vector(X, N, gain);
+            } else {
+                memset(X, 0, N*sizeof(float));
             }
         }
     }
 
     /* This code is used by the decoder and by the resynthesis-enabled encoder */
-    if (dualstereo) {
-        int j;
-        if (N != 2)
+    if (stereo) {
+        if (N > 2)
             celt_stereo_merge(X, Y, mid, N);
         if (inv) {
-            for (j = 0; j < N; j++)
-                Y[j] *= -1;
+            for (i = 0; i < N; i++)
+                Y[i] *= -1;
         }
     } else if (level == 0) {
         int k;
@@ -1204,7 +846,7 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
         /* Undo the sample reorganization going from time order to frequency order */
         if (B0 > 1)
             celt_interleave_hadamard(f->scratch, X, N_B >> recombine,
-                                     B0<<recombine, longblocks);
+                                     B0 << recombine, longblocks);
 
         /* Undo time-freq changes that we did earlier */
         N_B = N_B0;
@@ -1224,10 +866,9 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
 
         /* Scale output for later folding */
         if (lowband_out) {
-            int j;
             float n = sqrtf(N0);
-            for (j = 0; j < N0; j++)
-                lowband_out[j] = n * X[j];
+            for (i = 0; i < N0; i++)
+                lowband_out[i] = n * X[i];
         }
         cm = av_mod_uintp2(cm, blocks);
     }
@@ -1235,6 +876,26 @@ uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
     return cm;
 }
 
+uint32_t ff_celt_decode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
+                             float *X, float *Y, int N, int b, uint32_t blocks,
+                             float *lowband, int duration, float *lowband_out,
+                             int level, float gain, float *lowband_scratch,
+                             int fill)
+{
+    return quant_band_template(f, rc, band, X, Y, N, b, blocks, lowband, duration,
+                               lowband_out, level, gain, lowband_scratch, fill, 0);
+}
+
+uint32_t ff_celt_encode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
+                             float *X, float *Y, int N, int b, uint32_t blocks,
+                             float *lowband, int duration, float *lowband_out,
+                             int level, float gain, float *lowband_scratch,
+                             int fill)
+{
+    return quant_band_template(f, rc, band, X, Y, N, b, blocks, lowband, duration,
+                               lowband_out, level, gain, lowband_scratch, fill, 1);
+}
+
 float ff_celt_quant_band_cost(CeltFrame *f, OpusRangeCoder *rc, int band, float *bits,
                               float lambda)
 {