Skip to content
Permalink
Browse files

avformat/mp3dec: Make MP3 seek fast

When AVFMT_FLAG_FAST_SEEK is specified, make MP3 seek operation as
fast as possible.

When no "-usetoc" is specified, the default operation is using TOC
if available; otherwise, uses linear interpolation. This is useful
when seeking a large MP3 file with no TOC available. One example is
Podcast, many MP3 files are large, but no CBR/VBR tags. Most of
them are actually CBR. Even in VBR cases, this option sacrifices the
accuracy of playback time in exchange for responsiveness.
  • Loading branch information...
Andy Wu authored and wm4 committed Sep 1, 2015
1 parent 237cf37 commit c43bd08f8b043df7e18110e5344283c37b8380c1
Showing with 13 additions and 6 deletions.
  1. +13 −6 libavformat/mp3dec.c
@@ -342,7 +342,7 @@ static int mp3_read_header(AVFormatContext *s)
int i;

if (mp3->usetoc < 0)
mp3->usetoc = (s->flags & AVFMT_FLAG_FAST_SEEK) ? 0 : 2;
mp3->usetoc = (s->flags & AVFMT_FLAG_FAST_SEEK) ? 1 : 2;

st = avformat_new_stream(s, NULL);
if (!st)
@@ -489,19 +489,26 @@ static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
AVStream *st = s->streams[0];
int64_t ret = av_index_search_timestamp(st, timestamp, flags);
int64_t best_pos;
int fast_seek = (s->flags & AVFMT_FLAG_FAST_SEEK) ? 1 : 0;
int64_t filesize = mp3->header_filesize;

if (mp3->usetoc == 2)
return -1; // generic index code

if ( mp3->is_cbr
if (filesize <= 0) {
int64_t size = avio_size(s->pb);
if (size > 0 && size > s->internal->data_offset)
filesize = size - s->internal->data_offset;
}

if ( (mp3->is_cbr || fast_seek)
&& (mp3->usetoc == 0 || !mp3->xing_toc)
&& st->duration > 0
&& mp3->header_filesize > s->internal->data_offset
&& mp3->frames) {
&& filesize > 0) {
ie = &ie1;
timestamp = av_clip64(timestamp, 0, st->duration);
ie->timestamp = timestamp;
ie->pos = av_rescale(timestamp, mp3->header_filesize, st->duration) + s->internal->data_offset;
ie->pos = av_rescale(timestamp, filesize, st->duration) + s->internal->data_offset;
} else if (mp3->xing_toc) {
if (ret < 0)
return ret;
@@ -515,7 +522,7 @@ static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
if (best_pos < 0)
return best_pos;

if (mp3->is_cbr && ie == &ie1) {
if (mp3->is_cbr && ie == &ie1 && mp3->frames) {
int frame_duration = av_rescale(st->duration, 1, mp3->frames);
ie1.timestamp = frame_duration * av_rescale(best_pos - s->internal->data_offset, mp3->frames, mp3->header_filesize);
}

0 comments on commit c43bd08

Please sign in to comment.
You can’t perform that action at this time.