shithub: mp3dec

Download patch

ref: 3334b210a481338c2d770fb19499ed3fc43f848e
parent: 87180cbdb314a2e3f583b88c43eb8f458a861ef8
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Tue Dec 29 17:46:15 EST 2020

if seeking is impossible, buffer some first for minimp3 to be able to seek

--- a/mp3dec.c
+++ b/mp3dec.c
@@ -4,15 +4,24 @@
 #define MINIMP3_IMPLEMENTATION
 #include "minimp3_ex.h"
 
-static int noseek;
+static int noseek, startsz;
+static uvlong curpos;
 static Biobuf in;
 static uchar inb[MINIMP3_BUF_SIZE];
+static uchar start[2*MINIMP3_BUF_SIZE];
 
 static size_t
 readcb(void *buf, size_t size, void *)
 {
 	int n;
-	n = Bread(&in, buf, size);
+	if(noseek && curpos < startsz){
+		n = startsz - curpos;
+		n = size < n ? size : n;
+		memmove(buf, start+curpos, n);
+		curpos += n;
+	}else{
+		n = Bread(&in, buf, size);
+	}
 	return n > 0 ? n : 0;
 }
 
@@ -19,8 +28,10 @@
 static int
 seekcb(uint64_t position, void *)
 {
-	if(!noseek)
-		Bseek(&in, position, 0);
+	if(noseek && curpos <= startsz)
+		curpos = position;
+	else
+		curpos = Bseek(&in, position, 0);
 	return 0;
 }
 
@@ -60,6 +71,8 @@
 	noseek = Bseek(&in, 0, 2) < 0;
 	if(!noseek)
 		Bseek(&in, 0, 0);
+	else
+		startsz = Bread(&in, start, sizeof(start));
 	if(mp3dec_ex_open_cb(&dec, &io, MP3D_SEEK_TO_SAMPLE|MP3D_DO_NOT_SCAN) != 0)
 		sysfatal("mp3dec_ex_open_cb");
 	if(seekto != 0.0)