From e5736ae26a7b40b1cf1193761a2ac24d14223cdf Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin <jmvalin@jmvalin.ca> Date: Mon, 5 Sep 2011 10:28:27 -0400 Subject: [PATCH] Making sure redundant frames can never bust the buget This is now part of the bit-stream --- src/opus_decoder.c | 2 +- src/opus_encoder.c | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/opus_decoder.c b/src/opus_decoder.c index efad173a5..0b5bf781d 100644 --- a/src/opus_decoder.c +++ b/src/opus_decoder.c @@ -304,7 +304,7 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data, } start_band = 0; - if (mode != MODE_CELT_ONLY && data != NULL) + if (mode != MODE_CELT_ONLY && data != NULL && ec_tell(&dec)+29+8*(st->mode == MODE_HYBRID) < 8*len) { /* Check if we have a redundant 0-8 kHz band */ redundancy = ec_dec_bit_logp(&dec, 12); diff --git a/src/opus_encoder.c b/src/opus_encoder.c index 91707c2af..a30e1cd9f 100644 --- a/src/opus_encoder.c +++ b/src/opus_encoder.c @@ -788,20 +788,25 @@ int opus_encode_float(OpusEncoder *st, const opus_val16 *pcm, int frame_size, } } - if (st->mode != MODE_CELT_ONLY) + if (st->mode != MODE_CELT_ONLY && ec_tell(&enc)+29+8*(st->mode == MODE_HYBRID) < 8*nb_compr_bytes) { /* Check if we have a redundant 0-8 kHz band */ ec_enc_bit_logp(&enc, redundancy, 12); if (redundancy) { + int max_redundancy; + ec_enc_bit_logp(&enc, celt_to_silk, 1); + max_redundancy = nb_compr_bytes-((ec_tell(&enc)+7)>>3)-(st->mode == MODE_HYBRID); /* Target the same bit-rate for redundancy as for the rest, up to a max of 257 bytes */ - redundancy_bytes = IMIN(257, st->bitrate_bps/1600); - ec_enc_bit_logp(&enc, celt_to_silk, 1); + redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600); + redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes)); if (st->mode == MODE_HYBRID) ec_enc_uint(&enc, redundancy_bytes-2, 256); } start_band = 17; + } else { + redundancy = 0; } if (st->mode == MODE_SILK_ONLY) -- GitLab