shithub: riscv

Download patch

ref: 0903d01134a93869bea5f80129e99f872c00712d
parent: 3d1f5d4812316a6845233041b11c91b76cdb668f
author: cinap_lenrek <[email protected]>
date: Tue Dec 11 14:55:02 EST 2012

audio: fix broken write() size for mp3dec/oggdec/flacdec

they accidently wrote additional bytes in the stream. this
confused the new audio/pcmconv (now actually checking
if it got complete samples).

--- a/sys/src/cmd/audio/flacdec/flacdec.c
+++ b/sys/src/cmd/audio/flacdec/flacdec.c
@@ -93,8 +93,10 @@
 			p += chans*b;
 		}
 	}
-	if(p > buf)
-		write(ifd, buf, p - buf);
+	n = b * chans * len;
+	if(n > 0)
+		write(ifd, buf, n);
+
 	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
 }
 
--- a/sys/src/cmd/audio/mp3dec/main.c
+++ b/sys/src/cmd/audio/mp3dec/main.c
@@ -67,6 +67,7 @@
 		close(pfd[1]);
 		ifd = pfd[0];
 	}
+
 	n = 4 * chans * pcm->length;
 	if(n > nbuf){
 		nbuf = n;
@@ -73,13 +74,13 @@
 		buf = realloc(buf, nbuf);
 		if(buf == nil)
 			sysfatal("realloc: %r");
+		memset(buf, 0, nbuf);
 	}
 	p = buf;
 	for(j=0; j < chans; j++){
 		s = pcm->samples[j];
-		n = pcm->length;
 		p = buf + j*4;
-		for(i=0; i < n; i++){
+		for(i=0; i < pcm->length; i++){
 			v = *s++;
 			p[0] = v, v>>=8;
 			p[1] = v, v>>=8;
@@ -88,8 +89,9 @@
 			p += chans*4;
 		}
 	}
-	if(p > buf)
-		write(ifd, buf, p - buf);
+	if(n > 0)
+		write(ifd, buf, n);
+
 	return MAD_FLOW_CONTINUE;
 }
 
--- a/sys/src/cmd/audio/oggdec/oggdec.c
+++ b/sys/src/cmd/audio/oggdec/oggdec.c
@@ -86,8 +86,8 @@
 			p += chans*sizeof(float);
 		}
 	}
-	if(p > buf)
-		write(ifd, buf, p - buf);
+	if(n > 0)
+		write(ifd, buf, n);
 }
 
 int main(){
--- a/sys/src/cmd/audio/pcmconv/pcmconv.c
+++ b/sys/src/cmd/audio/pcmconv/pcmconv.c
@@ -327,6 +327,25 @@
 	return d;
 }
 
+int
+cread(int fd, uchar *buf, int len, int mod)
+{
+	uchar *off = buf;
+
+	len -= (len % mod);
+Again:
+	len = read(fd, off, len);
+	if(len <= 0)
+		return len;
+	off += len;
+	len = off - buf;
+	if((len % mod) != 0){
+		len = mod - (len % mod);
+		goto Again;
+	}
+	return len;
+}
+
 void
 usage(void)
 {
@@ -404,13 +423,11 @@
 	for(;;){
 		if(l >= 0 && l < r)
 			r = l;
-		n = read(0, ibuf, r);
-		if(n < 0)
+		n = cread(0, ibuf, r, i.framesz);
+		if(n < 0 || n > sizeof(ibuf))
 			sysfatal("read: %r");
 		if(l > 0)
 			l -= n;
-		if(n % i.framesz)
-			sysfatal("bad framesize %d %% %d", n, i.framesz);
 		n /= i.framesz;
 		(*iconv)(in, ibuf, i.bits, i.framesz, n);
 		m = resample(&ch[0], in, out, n) - out;