static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
                    int flags)
{
    MP3Context *mp3 = s->priv_data;
    AVIndexEntry *ie;
    AVStream *st = s->streams[0];
    int64_t ret  = av_index_search_timestamp(st, timestamp, flags);
    uint32_t header = 0;
    int i, j;

    if (!mp3->xing_toc) {
        st->skip_samples = timestamp <= 0 ? mp3->start_pad + 528 + 1 : 0;

        return -1;
    }

    if (ret < 0)
        return ret;

    ie = &st->index_entries[ret];
    ret = avio_seek(s->pb, ie->pos, SEEK_SET);
    if (ret < 0)
        return ret;

#define MIN_VALID 3
    for(i=0; i<4096; i++) {
        int64_t pos = ie->pos + i;
        for(j=0; j<MIN_VALID; j++) {
            ret = check(s, pos);
            if(ret < 0)
                break;
            pos += ret;
        }
        if(j==MIN_VALID)
            break;
    }
    if(j!=MIN_VALID)
        i=0;

    ret = avio_seek(s->pb, ie->pos + i, SEEK_SET);
    if (ret < 0)
        return ret;
    ff_update_cur_dts(s, st, ie->timestamp);
    st->skip_samples = ie->timestamp <= 0 ? mp3->start_pad + 528 + 1 : 0;
    return 0;
}