diff --git a/celt/bands.c b/celt/bands.c index ef5fb449..7160fa4f 100644 --- a/celt/bands.c +++ b/celt/bands.c @@ -852,11 +852,11 @@ static unsigned quant_band_n1(int encode, celt_norm *X, celt_norm *Y, int b, return 1; } -/* This function is responsible for encoding and decoding a band for both - the mono and stereo case. Even in the mono case, it can split the band - in two and transmit the energy difference with the two half-bands. It - can be called recursively so bands can end up being split in 8 parts. */ -static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, celt_norm *Y, +/* This function is responsible for encoding and decoding a band the mono + case. It can split the band in two and transmit the energy difference with + the two half-bands. It can be called recursively so bands can end up being + split in 8 parts. */ +static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, int N, int b, int spread, int B, int intensity, int tf_change, celt_norm *lowband, ec_ctx *ec, opus_int32 *remaining_bits, int LM, celt_norm *lowband_out, const celt_ener *bandE, int level, opus_uint32 *seed, opus_val16 gain, celt_norm *lowband_scratch, int fill) @@ -864,7 +864,7 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c const unsigned char *cache; int q; int curr_bits; - int stereo, split; + int split; int imid=0, iside=0; int N0=N; int N_B=N; @@ -872,7 +872,6 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c int B0=B; int time_divide=0; int recombine=0; - int inv = 0; opus_val16 mid=0, side=0; int longBlocks; unsigned cm=0; @@ -881,21 +880,22 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c #else int resynth = !encode; #endif + celt_norm *Y=NULL; longBlocks = B0==1; N_B /= B; N_B0 = N_B; - split = stereo = Y != NULL; + split = 0; /* Special case for one sample */ if (N==1) { - return quant_band_n1(encode, X, Y, b, remaining_bits, ec, lowband_out); + return quant_band_n1(encode, X, NULL, b, remaining_bits, ec, lowband_out); } - if (!stereo && level == 0) + if (level == 0) { int k; if (tf_change>0) @@ -952,7 +952,7 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c /* If we need 1.5 more bit than we can produce, split the band in two. */ cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i]; - if (!stereo && LM != -1 && b > cache[cache[0]]+12 && N>2) + if (LM != -1 && b > cache[cache[0]]+12 && N>2) { N >>= 1; Y = X+N; @@ -969,13 +969,9 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c int itheta; int qalloc; struct split_ctx ctx; - int orig_fill; - - orig_fill = fill; compute_theta(&ctx, encode, m, i, X, Y, N, &b, B, B0, intensity, ec, - remaining_bits, LM, bandE, stereo, &fill); - inv = ctx.inv; + remaining_bits, LM, bandE, 0, &fill); imid = ctx.imid; iside = ctx.iside; delta = ctx.delta; @@ -992,56 +988,7 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c /* 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 && stereo) { - int c; - int sign=0; - celt_norm *x2, *y2; - mbits = b; - sbits = 0; - /* Only need one bit for the side */ - if (itheta != 0 && itheta != 16384) - sbits = 1< 8192; - *remaining_bits -= qalloc+sbits; - - x2 = c ? Y : X; - y2 = c ? X : Y; - if (sbits) - { - if (encode) - { - /* Here we only need to encode a sign for the side */ - sign = x2[0]*y2[1] - x2[1]*y2[0] < 0; - ec_enc_bits(ec, sign, 1); - } else { - sign = ec_dec_bits(ec, 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 = quant_band(encode, m, i, x2, NULL, N, mbits, spread, B, intensity, tf_change, lowband, ec, remaining_bits, LM, lowband_out, NULL, level, seed, 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]; - if (resynth) - { - celt_norm tmp; - X[0] = MULT16_16_Q15(mid, X[0]); - X[1] = MULT16_16_Q15(mid, X[1]); - Y[0] = MULT16_16_Q15(side, Y[0]); - Y[1] = MULT16_16_Q15(side, Y[1]); - tmp = X[0]; - X[0] = SUB16(tmp,Y[0]); - Y[0] = ADD16(tmp,Y[0]); - tmp = X[1]; - X[1] = SUB16(tmp,Y[1]); - Y[1] = ADD16(tmp,Y[1]); - } - } else { /* "Normal" split code */ celt_norm *next_lowband2=NULL; celt_norm *next_lowband_out1=NULL; @@ -1049,7 +996,7 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c opus_int32 rebalance; /* Give more bits to low-energy MDCTs than they would otherwise deserve */ - if (B0>1 && !stereo && (itheta&0x3fff)) + if (B0>1 && (itheta&0x3fff)) { if (itheta > 8192) /* Rough approximation for pre-echo masking */ @@ -1062,47 +1009,44 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c sbits = b-mbits; *remaining_bits -= qalloc; - if (lowband && !stereo) + if (lowband) next_lowband2 = lowband+N; /* >32-bit split case */ /* Only stereo needs to pass on lowband_out. Otherwise, it's handled at the end */ - if (stereo) - next_lowband_out1 = lowband_out; - else - next_level = level+1; + next_level = level+1; rebalance = *remaining_bits; 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 = quant_band(encode, m, i, X, NULL, N, mbits, spread, B, intensity, tf_change, + cm = quant_band(encode, m, i, X, N, mbits, spread, B, intensity, tf_change, lowband, ec, remaining_bits, LM, next_lowband_out1, - NULL, next_level, seed, stereo ? Q15ONE : MULT16_16_P15(gain,mid), lowband_scratch, fill); + NULL, next_level, seed, MULT16_16_P15(gain,mid), lowband_scratch, fill); rebalance = mbits - (rebalance-*remaining_bits); if (rebalance > 3<>B)<<((B0>>1)&(stereo-1)); + NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B0>>1)&(-1)); } else { /* For a stereo split, the high bits of fill are always zero, so no folding will be done to the side. */ - cm = quant_band(encode, m, i, Y, NULL, N, sbits, spread, B, intensity, tf_change, + cm = quant_band(encode, m, i, Y, N, sbits, spread, B, intensity, tf_change, next_lowband2, ec, remaining_bits, LM, NULL, - NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B0>>1)&(stereo-1)); + NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B0>>1)&(-1)); rebalance = sbits - (rebalance-*remaining_bits); if (rebalance > 3< 8192; + *remaining_bits -= qalloc+sbits; + + x2 = c ? Y : X; + y2 = c ? X : Y; + if (sbits) + { + if (encode) + { + /* Here we only need to encode a sign for the side */ + sign = x2[0]*y2[1] - x2[1]*y2[0] < 0; + ec_enc_bits(ec, sign, 1); + } else { + sign = ec_dec_bits(ec, 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 = quant_band(encode, m, i, x2, N, mbits, spread, B, intensity, tf_change, lowband, ec, remaining_bits, LM, lowband_out, NULL, level, seed, 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]; + if (resynth) + { + celt_norm tmp; + X[0] = MULT16_16_Q15(mid, X[0]); + X[1] = MULT16_16_Q15(mid, X[1]); + Y[0] = MULT16_16_Q15(side, Y[0]); + Y[1] = MULT16_16_Q15(side, Y[1]); + tmp = X[0]; + X[0] = SUB16(tmp,Y[0]); + Y[0] = ADD16(tmp,Y[0]); + tmp = X[1]; + X[1] = SUB16(tmp,Y[1]); + Y[1] = ADD16(tmp,Y[1]); + } + } else { + /* "Normal" split code */ + celt_norm *next_lowband2=NULL; + celt_norm *next_lowband_out1=NULL; + int next_level=0; + opus_int32 rebalance; + + mbits = IMAX(0, IMIN(b, (b-delta)/2)); + sbits = b-mbits; + *remaining_bits -= qalloc; + + /* Only stereo needs to pass on lowband_out. Otherwise, it's + handled at the end */ + next_lowband_out1 = lowband_out; + + rebalance = *remaining_bits; + 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 = quant_band(encode, m, i, X, N, mbits, spread, B, intensity, tf_change, + lowband, ec, remaining_bits, LM, next_lowband_out1, + NULL, next_level, seed, Q15ONE, lowband_scratch, fill); + rebalance = mbits - (rebalance-*remaining_bits); + if (rebalance > 3<>B)<<((B>>1)&(1-1)); + } else { + /* For a stereo split, the high bits of fill are always zero, so no + folding will be done to the side. */ + cm = quant_band(encode, m, i, Y, N, sbits, spread, B, intensity, tf_change, + next_lowband2, ec, remaining_bits, LM, NULL, + NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B>>1)&(1-1)); + rebalance = sbits - (rebalance-*remaining_bits); + if (rebalance > 3<