shithub: opus-tools

Download patch

ref: 7dcae7d606d1c0563876af0df5c053c75ff7139a
parent: 5adadc5626ee2d5d3a3ca21e70fd195b9d002a0b
author: Gregory Maxwell <greg@xiph.org>
date: Wed Aug 1 09:21:40 EDT 2012

More correct handling of the resampler buffer and flushing delay.

--- a/src/audio-in.c
+++ b/src/audio-in.c
@@ -890,17 +890,18 @@
       int i;
       int reading, ret;
       unsigned in_len, out_len;
-      reading=1024-*inbuf;
+      out_len=samples-out_samples;
+      reading=rs->bufsize-*inbuf;
+      if(reading>1024)reading=1024;
       ret=rs->real_reader(rs->real_readdata, pcmbuf+*inbuf*rs->channels, reading);
       *inbuf+=ret;
       in_len=*inbuf;
-      out_len=samples-out_samples;
       speex_resampler_process_interleaved_float(rs->resampler, pcmbuf, &in_len, buffer+out_samples*rs->channels, &out_len);
+      out_samples+=out_len;
       if(ret==0&&in_len==0){
         for(i=out_samples*rs->channels;i<samples*rs->channels;i++)buffer[i]=0;
         return out_samples;
       }
-      out_samples+=out_len;
       for(i=0;i<rs->channels*(*inbuf-(long int)in_len);i++)pcmbuf[i]=pcmbuf[i+rs->channels*in_len];
       *inbuf-=in_len;
     }
@@ -911,7 +912,7 @@
     resampler *rs = calloc(1, sizeof(resampler));
     int err;
 
-    rs->bufsize = 2048; /* Shrug */
+    rs->bufsize = 5760*2; /* Have at least two output frames worth, just in case of ugly ratios */
     rs->bufpos = 0;
 
     rs->real_reader = opt->read_samples;
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -881,7 +881,7 @@
 
     /*If the stream is over or we're sure that the delayed flush will fire,
       go ahead and flush now to avoid adding delay.*/
-    while((op.e_o_s||(enc_granulepos+frame_size-last_granulepos>max_ogg_delay)||
+    while((op.e_o_s||(enc_granulepos+(frame_size*48000/coding_rate)-last_granulepos>max_ogg_delay)||
            (last_segments>=255))?
 #ifdef OLD_LIBOGG
     /*Libogg > 1.2.2 allows us to achieve lower overhead by