shithub: opus

Download patch

ref: 9cca20aaafe487acc2da1912f77b35de70358640
parent: e7028175af1661abaa3447a6a84750662ab8dfe6
author: Gregory Maxwell <[email protected]>
date: Tue Dec 13 19:52:24 EST 2011

Various multistream fixes.

Fixes the encoder bitrate CTLs to correctly apply to all streams,
prevents the MS encoder from starving the latter streams by not
reserving a reasonable minimum amount of space for them.

--- a/src/opus_multistream.c
+++ b/src/opus_multistream.c
@@ -217,7 +217,8 @@
    return st;
 }
 
-
+/* Max size in case the encoder decides to return three frames */
+#define MS_FRAME_TMP (3*1275+7)
 #ifdef FIXED_POINT
 int opus_multistream_encode(
 #else
@@ -236,8 +237,7 @@
    char *ptr;
    int tot_size;
    VARDECL(opus_val16, buf);
-   /* Max size in case the encoder decides to return three frames */
-   unsigned char tmp_data[3*1275+7];
+   unsigned char tmp_data[MS_FRAME_TMP];
    OpusRepacketizer rp;
    ALLOC_STACK;
 
@@ -246,7 +246,7 @@
    coupled_size = opus_encoder_get_size(2);
    mono_size = opus_encoder_get_size(1);
 
-   if (max_data_bytes < 2*st->layout.nb_streams-1)
+   if (max_data_bytes < 4*st->layout.nb_streams-1)
    {
       RESTORE_STACK;
       return OPUS_BUFFER_TOO_SMALL;
@@ -280,8 +280,9 @@
       }
       /* number of bytes left (+Toc) */
       curr_max = max_data_bytes - tot_size;
-      /* Reserve one byte for the last stream and 2 for the others */
-      curr_max -= 2*(st->layout.nb_streams-s)-1;
+      /* Reserve three bytes for the last stream and four for the others */
+      curr_max -= IMAX(0,4*(st->layout.nb_streams-s-1)-1);
+      curr_max = IMIN(curr_max,MS_FRAME_TMP);
       len = opus_encode_native(enc, buf, frame_size, tmp_data, curr_max);
       if (len<0)
       {
@@ -375,6 +376,10 @@
       {
          OpusEncoder *enc;
          enc = (OpusEncoder*)ptr;
+         if (s < st->layout.nb_coupled_streams)
+            ptr += align(coupled_size);
+         else
+            ptr += align(mono_size);
          opus_encoder_ctl(enc, request, value * (s < st->layout.nb_coupled_streams ? 2 : 1));
       }
    }
@@ -389,6 +394,10 @@
          opus_int32 rate;
          OpusEncoder *enc;
          enc = (OpusEncoder*)ptr;
+         if (s < st->layout.nb_coupled_streams)
+            ptr += align(coupled_size);
+         else
+            ptr += align(mono_size);
          opus_encoder_ctl(enc, request, &rate);
          *value += rate;
       }