shithub: opus

Download patch

ref: e5736ae26a7b40b1cf1193761a2ac24d14223cdf
parent: 12c39929630012d40e8b7a096f2382072e9749a7
author: Jean-Marc Valin <[email protected]>
date: Mon Sep 5 06:28:27 EDT 2011

Making sure redundant frames can never bust the buget

This is now part of the bit-stream

--- a/src/opus_decoder.c
+++ b/src/opus_decoder.c
@@ -304,7 +304,7 @@
     }
 
     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);
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -788,20 +788,25 @@
         }
     }
 
-    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)