X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fmp3dec.c;h=007c6eaf8bd2a314f0e68ba6e8e2af49d3aca051;hb=385eb066ce8c4be42a3f1d8c390137499cb35dd5;hp=ff794ff277731584cd9319b43956909d088dd5a9;hpb=eb823df817660947fe9c8172f01319c3b972b588;p=ffmpeg diff --git a/libavformat/mp3dec.c b/libavformat/mp3dec.c index ff794ff2777..007c6eaf8bd 100644 --- a/libavformat/mp3dec.c +++ b/libavformat/mp3dec.c @@ -54,7 +54,7 @@ typedef struct { int is_cbr; } MP3DecContext; -static int check(AVFormatContext *s, int64_t pos); +static int check(AVIOContext *pb, int64_t pos); /* mp3 read */ @@ -98,7 +98,7 @@ static int mp3_read_probe(AVProbeData *p) avcodec_free_context(&avctx); // keep this in sync with ac3 probe, both need to avoid // issues with MPEG-files! - if (first_frames>=4) return AVPROBE_SCORE_EXTENSION + 1; + if (first_frames>=7) return AVPROBE_SCORE_EXTENSION + 1; else if(max_frames>200)return AVPROBE_SCORE_EXTENSION; else if(max_frames>=4 && max_frames >= p->buf_size/10000) return AVPROBE_SCORE_EXTENSION / 2; else if(ff_id3v2_match(buf0, ID3v2_DEFAULT_MAGIC) && 2*ff_id3v2_tag_len(buf0) >= p->buf_size) @@ -376,7 +376,7 @@ static int mp3_read_header(AVFormatContext *s) for (i = 0; i < 64 * 1024; i++) { if (!(i&1023)) ffio_ensure_seekback(s->pb, i + 1024 + 4); - if (check(s, off + i) >= 0) { + if (check(s->pb, off + i) >= 0) { av_log(s, AV_LOG_INFO, "Skipping %d bytes of junk at %"PRId64".\n", i, off); avio_seek(s->pb, off + i, SEEK_SET); break; @@ -418,64 +418,42 @@ static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt) return ret; } -static int check(AVFormatContext *s, int64_t pos) +#define SEEK_WINDOW 4096 + +static int check(AVIOContext *pb, int64_t pos) { - int64_t ret = avio_seek(s->pb, pos, SEEK_SET); + int64_t ret = avio_seek(pb, pos, SEEK_SET); unsigned header; MPADecodeHeader sd; if (ret < 0) return ret; - header = avio_rb32(s->pb); + + header = avio_rb32(pb); if (ff_mpa_check_header(header) < 0) return -1; if (avpriv_mpegaudio_decode_header(&sd, header) == 1) return -1; + return sd.frame_size; } -static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp, - int flags) +static int64_t mp3_sync(AVFormatContext *s, int64_t target_pos, int flags) { - MP3DecContext *mp3 = s->priv_data; - AVIndexEntry *ie, ie1; - AVStream *st = s->streams[0]; - int64_t ret = av_index_search_timestamp(st, timestamp, flags); - int i, j; int dir = (flags&AVSEEK_FLAG_BACKWARD) ? -1 : 1; int64_t best_pos; - int best_score; - - if (mp3->usetoc == 2) - return -1; // generic index code - - if ( mp3->is_cbr - && (mp3->usetoc == 0 || !mp3->xing_toc) - && st->duration > 0 - && mp3->header_filesize > s->internal->data_offset - && mp3->frames) { - 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; - } else if (mp3->xing_toc) { - if (ret < 0) - return ret; - - ie = &st->index_entries[ret]; - } else { - return -1; - } + int best_score, i, j; + int64_t ret; - avio_seek(s->pb, FFMAX(ie->pos - 4096, 0), SEEK_SET); - ret = avio_seek(s->pb, ie->pos, SEEK_SET); + avio_seek(s->pb, FFMAX(target_pos - SEEK_WINDOW, 0), SEEK_SET); + ret = avio_seek(s->pb, target_pos, SEEK_SET); if (ret < 0) return ret; #define MIN_VALID 3 - best_pos = ie->pos; + best_pos = target_pos; best_score = 999; - for(i=0; i<4096; i++) { - int64_t pos = ie->pos + (dir > 0 ? i - 1024 : -i); + for(i=0; i 0 ? i - SEEK_WINDOW/4 : -i); int64_t candidate = -1; int score = 999; @@ -483,10 +461,10 @@ static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp, continue; for(j=0; jpb, pos); if(ret < 0) break; - if ((ie->pos - pos)*dir <= 0 && abs(MIN_VALID/2-j) < score) { + if ((target_pos - pos)*dir <= 0 && abs(MIN_VALID/2-j) < score) { candidate = pos; score = abs(MIN_VALID/2-j); } @@ -500,9 +478,42 @@ static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp, } } - ret = avio_seek(s->pb, best_pos, SEEK_SET); - if (ret < 0) - return ret; + return avio_seek(s->pb, best_pos, SEEK_SET); +} + +static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp, + int flags) +{ + MP3DecContext *mp3 = s->priv_data; + AVIndexEntry *ie, ie1; + AVStream *st = s->streams[0]; + int64_t ret = av_index_search_timestamp(st, timestamp, flags); + int64_t best_pos; + + if (mp3->usetoc == 2) + return -1; // generic index code + + if ( mp3->is_cbr + && (mp3->usetoc == 0 || !mp3->xing_toc) + && st->duration > 0 + && mp3->header_filesize > s->internal->data_offset + && mp3->frames) { + 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; + } else if (mp3->xing_toc) { + if (ret < 0) + return ret; + + ie = &st->index_entries[ret]; + } else { + return -1; + } + + best_pos = mp3_sync(s, ie->pos, flags); + if (best_pos < 0) + return best_pos; if (mp3->is_cbr && ie == &ie1) { int frame_duration = av_rescale(st->duration, 1, mp3->frames);