Skip to content

Commit

Permalink
Reallocate mid/side bits over cap as extra
Browse files Browse the repository at this point in the history
  • Loading branch information
jmvalin committed Dec 13, 2024
1 parent af9155f commit 22f1541
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
28 changes: 19 additions & 9 deletions celt/bands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm
int N, int b, int B, celt_norm *lowband,
int LM, celt_norm *lowband_out,
celt_norm *lowband_scratch, int fill
ARG_QEXT(int ext_b))
ARG_QEXT(int ext_b) ARG_QEXT(const int *cap))
{
int imid=0, iside=0;
int inv = 0;
Expand Down Expand Up @@ -1541,28 +1541,38 @@ static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm
rebalance = ctx->remaining_bits;
if (mbits >= sbits)
{
#ifdef ENABLE_QEXT
int qext_extra = 0;
/* Reallocate any mid bits that cannot be used to extra mid bits. */
if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2));
#endif
/* In stereo mode, we do not apply a scaling to the mid because we need the normalized
mid for folding later. */
cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
lowband_scratch, fill ARG_QEXT(ext_b/2));
lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
rebalance = mbits - (rebalance-ctx->remaining_bits);
if (rebalance > 3<<BITRES && itheta!=0)
sbits += rebalance - (3<<BITRES);

/* For a stereo split, the high bits of fill are always zero, so no
folding will be done to the side. */
cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2));
cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
} else {
#ifdef ENABLE_QEXT
int qext_extra = 0;
/* Reallocate any side bits that cannot be used to extra side bits. */
if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2));
#endif
/* For a stereo split, the high bits of fill are always zero, so no
folding will be done to the side. */
cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2));
cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
rebalance = sbits - (rebalance-ctx->remaining_bits);
if (rebalance > 3<<BITRES && itheta!=16384)
mbits += rebalance - (3<<BITRES);
/* In stereo mode, we do not apply a scaling to the mid because we need the normalized
mid for folding later. */
cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
lowband_scratch, fill ARG_QEXT(ext_b/2));
lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
}
}

Expand Down Expand Up @@ -1604,7 +1614,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end,
opus_int32 balance, ec_ctx *ec, int LM, int codedBands,
opus_uint32 *seed, int complexity, int arch, int disable_inv
#ifdef ENABLE_QEXT
, ec_ctx *ext_ec, int *extra_pulses, opus_int32 ext_total_bits
, ec_ctx *ext_ec, int *extra_pulses, opus_int32 ext_total_bits, const int *cap
#endif
)
{
Expand Down Expand Up @@ -1842,7 +1852,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end,
ctx.theta_round = -1;
x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b));
last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
dist0 = MULT16_32_Q15(w[0], celt_inner_prod(X_save, X, N, arch)) + MULT16_32_Q15(w[1], celt_inner_prod(Y_save, Y, N, arch));

/* Save first result. */
Expand Down Expand Up @@ -1884,7 +1894,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end,
ctx.theta_round = 1;
x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b));
last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
dist1 = MULT16_32_Q15(w[0], celt_inner_prod(X_save, X, N, arch)) + MULT16_32_Q15(w[1], celt_inner_prod(Y_save, Y, N, arch));
if (dist0 >= dist1) {
x_cm = cm2;
Expand All @@ -1906,7 +1916,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end,
ctx.theta_round = 0;
x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
}
} else {
x_cm = quant_band(&ctx, X, N, b, B,
Expand Down
2 changes: 1 addition & 1 deletion celt/bands.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end,
opus_int32 balance, ec_ctx *ec, int M, int codedBands, opus_uint32 *seed,
int complexity, int arch, int disable_inv
#ifdef ENABLE_QEXT
, ec_ctx *ext_ec, int *extra_pulses, opus_int32 total_ext_bits
, ec_ctx *ext_ec, int *extra_pulses, opus_int32 total_ext_bits, const int *cap
#endif
);

Expand Down
4 changes: 2 additions & 2 deletions celt/celt_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ int celt_decode_with_ec_dred(CELTDecoder * OPUS_RESTRICT st, const unsigned char
quant_all_bands(0, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks,
NULL, &extra_pulses[nbEBands], shortBlocks, spread_decision, qext_dual_stereo, qext_intensity, zeros,
qext_bytes*(8<<BITRES), 0, &ext_dec, LM, qext_end, &st->rng, 0,
st->arch, st->disable_inv, &dummy_dec, zeros, 0);
st->arch, st->disable_inv, &dummy_dec, zeros, 0, NULL);
}
}
#endif
Expand All @@ -1384,7 +1384,7 @@ int celt_decode_with_ec_dred(CELTDecoder * OPUS_RESTRICT st, const unsigned char
len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng, 0,
st->arch, st->disable_inv
#ifdef ENABLE_QEXT
, &ext_dec, extra_pulses, qext_bytes*(8<<BITRES)
, &ext_dec, extra_pulses, qext_bytes*(8<<BITRES), cap
#endif
);

Expand Down
4 changes: 2 additions & 2 deletions celt/celt_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2555,7 +2555,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in
quant_all_bands(1, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks,
qext_bandE, &extra_pulses[nbEBands], shortBlocks, st->spread_decision,
qext_dual_stereo, qext_intensity, zeros, qext_bytes*(8<<BITRES),
0, &ext_enc, LM, qext_end, &st->rng, st->complexity, st->arch, st->disable_inv, &dummy_enc, zeros, 0);
0, &ext_enc, LM, qext_end, &st->rng, st->complexity, st->arch, st->disable_inv, &dummy_enc, zeros, 0, NULL);
}
}
#endif
Expand All @@ -2567,7 +2567,7 @@ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, in
dual_stereo, st->intensity, tf_res, nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv,
balance, enc, LM, codedBands, &st->rng, st->complexity, st->arch, st->disable_inv
#ifdef ENABLE_QEXT
, &ext_enc, extra_pulses, qext_bytes*(8<<BITRES)
, &ext_enc, extra_pulses, qext_bytes*(8<<BITRES), cap
#endif
);

Expand Down

0 comments on commit 22f1541

Please sign in to comment.