summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean-Marc Valin <jmvalin@jmvalin.ca>2016-10-27 00:11:26 -0400
committerJean-Marc Valin <jmvalin@jmvalin.ca>2016-10-27 00:11:26 -0400
commit6d0628493550f4ef0eaecf3058a502404bc6dca2 (patch)
tree8409cae6214606d1e8b142fd6e5d98affd0cb957
parent4667b6992a738683e4b7d6667b9850ca0ee5fd7b (diff)
downloadopus-6d0628493550f4ef0eaecf3058a502404bc6dca2.tar.gz
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.
-rw-r--r--src/opus_encoder.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/opus_encoder.c b/src/opus_encoder.c
index 0b666469..7ed05893 100644
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -1894,21 +1894,28 @@ opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_
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;
}