static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)

{

    FFMContext *ffm = s->priv_data;

    int64_t pos_min, pos_max, pos;

    int64_t pts_min, pts_max, pts;

    double pos1;



    av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);

    /* find the position using linear interpolation (better than

       dichotomy in typical cases) */

    if (ffm->write_index && ffm->write_index < ffm->file_size) {

        if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {

            pos_min = FFM_PACKET_SIZE;

            pos_max = ffm->write_index - FFM_PACKET_SIZE;

        } else {

            pos_min = ffm->write_index;

            pos_max = ffm->file_size - FFM_PACKET_SIZE;

        }

    } else {

        pos_min = FFM_PACKET_SIZE;

        pos_max = ffm->file_size - FFM_PACKET_SIZE;

    }

    while (pos_min <= pos_max) {

        pts_min = get_dts(s, pos_min);

        pts_max = get_dts(s, pos_max);

        if (pts_min > wanted_pts || pts_max <= wanted_pts) {

            pos = pts_min > wanted_pts ? pos_min : pos_max;

            goto found;

        }

        /* linear interpolation */

        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /

            (double)(pts_max - pts_min);

        pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;

        if (pos <= pos_min)

            pos = pos_min;

        else if (pos >= pos_max)

            pos = pos_max;

        pts = get_dts(s, pos);

        /* check if we are lucky */

        if (pts == wanted_pts) {

            goto found;

        } else if (pts > wanted_pts) {

            pos_max = pos - FFM_PACKET_SIZE;

        } else {

            pos_min = pos + FFM_PACKET_SIZE;

        }

    }

    pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;



 found:

    if (ffm_seek1(s, pos) < 0)

        return -1;



    /* reset read state */

    ffm->read_state = READ_HEADER;

    ffm->packet_ptr = ffm->packet;

    ffm->packet_end = ffm->packet;

    ffm->first_packet = 1;



    return 0;

}

static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)

{

    FFMContext *ffm = s->priv_data;

    int64_t pos_min, pos_max, pos;

    int64_t pts_min, pts_max, pts;

    double pos1;



    av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);

    



    if (ffm->write_index && ffm->write_index < ffm->file_size) {

        if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {

            pos_min = FFM_PACKET_SIZE;

            pos_max = ffm->write_index - FFM_PACKET_SIZE;

        } else {

            pos_min = ffm->write_index;

            pos_max = ffm->file_size - FFM_PACKET_SIZE;

        }

    } else {

        pos_min = FFM_PACKET_SIZE;

        pos_max = ffm->file_size - FFM_PACKET_SIZE;

    }

    while (pos_min <= pos_max) {

        pts_min = get_dts(s, pos_min);

        pts_max = get_dts(s, pos_max);

        if (pts_min > wanted_pts || pts_max <= wanted_pts) {

            pos = pts_min > wanted_pts ? pos_min : pos_max;

            goto found;

        }

        

        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /

            (double)(pts_max - pts_min);

        pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;

        if (pos <= pos_min)

            pos = pos_min;

        else if (pos >= pos_max)

            pos = pos_max;

        pts = get_dts(s, pos);

        

        if (pts == wanted_pts) {

            goto found;

        } else if (pts > wanted_pts) {

            pos_max = pos - FFM_PACKET_SIZE;

        } else {

            pos_min = pos + FFM_PACKET_SIZE;

        }

    }

    pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;



 found:

    if (ffm_seek1(s, pos) < 0)

        return -1;



    

    ffm->read_state = READ_HEADER;

    ffm->packet_ptr = ffm->packet;

    ffm->packet_end = ffm->packet;

    ffm->first_packet = 1;



    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, int VAR_1, int64_t VAR_2, int VAR_3)

{

    FFMContext *ffm = VAR_0->priv_data;

    int64_t pos_min, pos_max, pos;

    int64_t pts_min, pts_max, pts;

    double VAR_4;



    av_dlog(VAR_0, "VAR_2=%0.6f\n", VAR_2 / 1000000.0);

    



    if (ffm->write_index && ffm->write_index < ffm->file_size) {

        if (get_dts(VAR_0, FFM_PACKET_SIZE) < VAR_2) {

            pos_min = FFM_PACKET_SIZE;

            pos_max = ffm->write_index - FFM_PACKET_SIZE;

        } else {

            pos_min = ffm->write_index;

            pos_max = ffm->file_size - FFM_PACKET_SIZE;

        }

    } else {

        pos_min = FFM_PACKET_SIZE;

        pos_max = ffm->file_size - FFM_PACKET_SIZE;

    }

    while (pos_min <= pos_max) {

        pts_min = get_dts(VAR_0, pos_min);

        pts_max = get_dts(VAR_0, pos_max);

        if (pts_min > VAR_2 || pts_max <= VAR_2) {

            pos = pts_min > VAR_2 ? pos_min : pos_max;

            goto found;

        }

        

        VAR_4 = (double)(pos_max - pos_min) * (double)(VAR_2 - pts_min) /

            (double)(pts_max - pts_min);

        pos = (((int64_t)VAR_4) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;

        if (pos <= pos_min)

            pos = pos_min;

        else if (pos >= pos_max)

            pos = pos_max;

        pts = get_dts(VAR_0, pos);

        

        if (pts == VAR_2) {

            goto found;

        } else if (pts > VAR_2) {

            pos_max = pos - FFM_PACKET_SIZE;

        } else {

            pos_min = pos + FFM_PACKET_SIZE;

        }

    }

    pos = (VAR_3 & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;



 found:

    if (ffm_seek1(VAR_0, pos) < 0)

        return -1;



    

    ffm->read_state = READ_HEADER;

    ffm->packet_ptr = ffm->packet;

    ffm->packet_end = ffm->packet;

    ffm->first_packet = 1;



    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, int VAR_1, int64_t VAR_2, int VAR_3)
{ FFMContext *ffm = VAR_0->priv_data; int64_t pos_min, pos_max, pos; int64_t pts_min, pts_max, pts; double VAR_4; av_dlog(VAR_0, "VAR_2=%0.6f\n", VAR_2 / 1000000.0); if (ffm->write_index && ffm->write_index < ffm->file_size) { if (get_dts(VAR_0, FFM_PACKET_SIZE) < VAR_2) { pos_min = FFM_PACKET_SIZE; pos_max = ffm->write_index - FFM_PACKET_SIZE; } else { pos_min = ffm->write_index; pos_max = ffm->file_size - FFM_PACKET_SIZE; } } else { pos_min = FFM_PACKET_SIZE; pos_max = ffm->file_size - FFM_PACKET_SIZE; } while (pos_min <= pos_max) { pts_min = get_dts(VAR_0, pos_min); pts_max = get_dts(VAR_0, pos_max); if (pts_min > VAR_2 || pts_max <= VAR_2) { pos = pts_min > VAR_2 ? pos_min : pos_max; goto found; } VAR_4 = (double)(pos_max - pos_min) * (double)(VAR_2 - pts_min) /
(double)(pts_max - pts_min); pos = (((int64_t)VAR_4) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE; if (pos <= pos_min)
pos = pos_min; else if (pos >= pos_max)
pos = pos_max; pts = get_dts(VAR_0, pos); if (pts == VAR_2) { goto found; } else if (pts > VAR_2) { pos_max = pos - FFM_PACKET_SIZE; } else { pos_min = pos + FFM_PACKET_SIZE; } } pos = (VAR_3 & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max; found:
if (ffm_seek1(VAR_0, pos) < 0)
return -1; ffm->read_state = READ_HEADER; ffm->packet_ptr = ffm->packet; ffm->packet_end = ffm->packet; ffm->first_packet = 1; return 0; }