Account for redundancy signalling when computing st->silk_mode.maxBits
Without that, we could bust the budget and end up with the
if (ec_tell(&enc) <= 8*nb_compr_bytes)
being false, followed by an assert failure later.
diff --git a/src/opus_encoder.c b/src/opus_encoder.c
index 0b66646..7ed0589 100644
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -1894,21 +1894,28 @@
st->silk_mode.useCBR = !st->use_vbr;
/* Call SILK encoder for the low band */
- nBytes = IMIN(1275, max_data_bytes-1-redundancy_bytes);
- st->silk_mode.maxBits = nBytes*8;
+ /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */
+ st->silk_mode.maxBits = IMIN(1275, max_data_bytes-1-redundancy_bytes)*8;
+ if (redundancy)
+ {
+ /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */
+ st->silk_mode.maxBits -= 1;
+ if (st->mode == MODE_HYBRID)
+ st->silk_mode.maxBits -= 20;
+ }
if (st->silk_mode.useCBR)
{
if (st->mode == MODE_HYBRID)
{
- st->silk_mode.maxBits = st->silk_mode.bitRate * frame_size / st->Fs;
+ st->silk_mode.maxBits = IMIN(st->silk_mode.maxBits, st->silk_mode.bitRate * frame_size / st->Fs);
}
} else {
/* Constrained VBR. */
if (st->mode == MODE_HYBRID)
{
/* Compute SILK bitrate corresponding to the max total bits available */
- opus_int32 maxBitRate = compute_silk_rate_for_hybrid(nBytes*8*st->Fs / frame_size,
+ opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size,
curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded);
st->silk_mode.maxBits = maxBitRate * frame_size / st->Fs;
}