From 3883c978abdcb443ac6ca2b4a57d941c418e8b74 Mon Sep 17 00:00:00 2001 From: "roman.artiukhin" Date: Mon, 21 Aug 2023 12:57:27 +0300 Subject: Fix MP3 VBR seek jumps in wrong direction for long files Fix jumps in the wrong direction by seeking relative to the current position Change-Id: I5ca3d5bcb256dd8fb1cd17e6149878190571d359 --- lib/rbcodec/codecs/mpa.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/rbcodec/codecs/mpa.c b/lib/rbcodec/codecs/mpa.c index bafed3970c..c5d47a6791 100644 --- a/lib/rbcodec/codecs/mpa.c +++ b/lib/rbcodec/codecs/mpa.c @@ -192,12 +192,20 @@ static int get_file_pos(int newtime) pos = cur_toc * toc_sizestep; /* Interpolate between this TOC mark and the next TOC mark */ - newtime -= percent * pct_timestep; - pos += (uint64_t)plength * newtime / pct_timestep; + int newtime_toc = newtime - percent * pct_timestep; + pos += (uint64_t)plength * newtime_toc / pct_timestep; } else { /* No TOC exists, estimate the new position */ pos = (uint64_t)newtime * id3->filesize / id3->length; } + // VBR seek might be very inaccurate in long files + // So make sure that seeking actually happened in the intended direction + // Fix jumps in the wrong direction by seeking relative to the current position + long delta = id3->elapsed - newtime; + if ((delta >= 0 && pos > ci->curpos) || (delta < 0 && pos < ci->curpos)) + { + pos = ci->curpos - delta * id3->filesize / id3->length; + } } else if (id3->bitrate) { pos = newtime * (id3->bitrate / 8); } else { -- cgit v1.2.3