Making sure redundant frames can never bust the buget

This is now part of the bit-stream
This commit is contained in:
Jean-Marc Valin 2011-09-05 10:28:27 -04:00
parent 12c3992963
commit e5736ae26a
2 changed files with 9 additions and 4 deletions

View file

@ -304,7 +304,7 @@ static int opus_decode_frame(OpusDecoder *st, const unsigned char *data,
} }
start_band = 0; 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 */ /* Check if we have a redundant 0-8 kHz band */
redundancy = ec_dec_bit_logp(&dec, 12); redundancy = ec_dec_bit_logp(&dec, 12);

View file

@ -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 */ /* Check if we have a redundant 0-8 kHz band */
ec_enc_bit_logp(&enc, redundancy, 12); ec_enc_bit_logp(&enc, redundancy, 12);
if (redundancy) 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, /* Target the same bit-rate for redundancy as for the rest,
up to a max of 257 bytes */ up to a max of 257 bytes */
redundancy_bytes = IMIN(257, st->bitrate_bps/1600); redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600);
ec_enc_bit_logp(&enc, celt_to_silk, 1); redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
if (st->mode == MODE_HYBRID) if (st->mode == MODE_HYBRID)
ec_enc_uint(&enc, redundancy_bytes-2, 256); ec_enc_uint(&enc, redundancy_bytes-2, 256);
} }
start_band = 17; start_band = 17;
} else {
redundancy = 0;
} }
if (st->mode == MODE_SILK_ONLY) if (st->mode == MODE_SILK_ONLY)