shithub: opus

Download patch

ref: 5609cec9a5e1ea8fcb056f2306a115cb3b61c4c9
parent: bbfc9c9ee5de680320aeae2f8bd981ebe7af6861
author: Jean-Marc Valin <[email protected]>
date: Tue Dec 13 09:52:43 EST 2011

Fixes two minor issues found in random testing at ridiculously low rate.

- When it cannot produce the rate it's being asked, the encoder now
  returns a "PLC packet"
- Makes it possible to use the CELT PLC for more than 20 ms

--- a/src/opus_decoder.c
+++ b/src/opus_decoder.c
@@ -246,6 +246,20 @@
       }
    }
 
+   /* For CELT/hybrid PLC of more than 20 ms, do multiple calls */
+   if (data==NULL && frame_size > F20 && mode != MODE_SILK_ONLY)
+   {
+      int nb_samples = 0;
+      do {
+         int ret = opus_decode_frame(st, NULL, 0, pcm, F20, 0);
+         if (ret != F20)
+            return OPUS_INTERNAL_ERROR;
+         pcm += F20*st->channels;
+         nb_samples += F20;
+      } while (nb_samples < frame_size);
+      RESTORE_STACK;
+      return frame_size;
+   }
    ALLOC(pcm_transition, F5*st->channels, opus_val16);
 
    if (data!=NULL && st->prev_mode > 0 && (
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -479,6 +479,11 @@
        RESTORE_STACK;
        return OPUS_BAD_ARG;
     }
+    if (max_data_bytes<=0)
+    {
+       RESTORE_STACK;
+       return OPUS_BAD_ARG;
+    }
     silk_enc = (char*)st+st->silk_enc_offset;
     celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
 
@@ -490,6 +495,22 @@
     st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
 
     frame_rate = st->Fs/frame_size;
+    if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
+       || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400)))
+    {
+       int tocmode = st->mode;
+       if (tocmode==0)
+          tocmode = MODE_SILK_ONLY;
+       if (frame_rate>100)
+          tocmode = MODE_CELT_ONLY;
+       if (frame_rate < 50)
+          tocmode = MODE_SILK_ONLY;
+       data[0] = gen_toc(tocmode, frame_rate,
+                         st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth,
+                         st->stream_channels);
+       RESTORE_STACK;
+       return 1;
+    }
     if (!st->use_vbr)
     {
        int cbrBytes;