shithub: opus-tools

Download patch

ref: a0cb8eba36c090da246bdfee87b18345434a144b
parent: 88c756eef2c0578cc10709eaec176f4bd32cd43c
author: Mark Harris <mark.hsj@gmail.com>
date: Wed Sep 30 21:55:33 EDT 2015

opusenc: Don't read excess data after samples

When reading WAV/AIFF data with a known length, don't try to read
(and then discard) data beyond that length, to avoid blocking or
adding unnecessary latency when the data is not from a file.

--- a/src/audio-in.c
+++ b/src/audio-in.c
@@ -718,18 +718,13 @@
 {
     wavfile *f = (wavfile *)in;
     int sampbyte = f->samplesize / 8;
-    signed char *buf = alloca(samples*sampbyte*f->channels);
-    long bytes_read = fread(buf, 1, samples*sampbyte*f->channels, f->f);
+    int realsamples = f->totalsamples > 0 && samples > (f->totalsamples - f->samplesread)
+        ? f->totalsamples - f->samplesread : samples;
+    signed char *buf = alloca(realsamples*sampbyte*f->channels);
     int i,j;
-    opus_int64 realsamples;
     int *ch_permute = f->channel_permute;
 
-    if(f->totalsamples > 0 && f->samplesread +
-            bytes_read/(sampbyte*f->channels) > f->totalsamples) {
-        bytes_read = sampbyte*f->channels*(f->totalsamples - f->samplesread);
-    }
-
-    realsamples = bytes_read/(sampbyte*f->channels);
+    realsamples = fread(buf, sampbyte*f->channels, realsamples, f->f);
     f->samplesread += realsamples;
 
     if(f->samplesize==8)
@@ -814,16 +809,12 @@
 long wav_ieee_read(void *in, float *buffer, int samples)
 {
     wavfile *f = (wavfile *)in;
-    float *buf = alloca(samples*4*f->channels); /* de-interleave buffer */
-    long bytes_read = fread(buf,1,samples*4*f->channels, f->f);
+    int realsamples = f->totalsamples > 0 && samples > (f->totalsamples - f->samplesread)
+        ? f->totalsamples - f->samplesread : samples;
+    float *buf = alloca(realsamples*4*f->channels); /* de-interleave buffer */
     int i,j;
-    opus_int64 realsamples;
 
-    if(f->totalsamples > 0 && f->samplesread +
-            bytes_read/(4*f->channels) > f->totalsamples)
-        bytes_read = 4*f->channels*(f->totalsamples - f->samplesread);
-
-    realsamples = bytes_read/(4*f->channels);
+    realsamples = fread(buf, 4*f->channels, realsamples, f->f);
     f->samplesread += realsamples;
 
     for(i=0; i < realsamples; i++)