end:
static void adjust_write_index(AVFormatContext *s){    FFMContext *ffm = s->priv_data;    ByteIOContext *pb = &s->pb;    int64_t pts;    //offset_t orig_write_index = ffm->write_index;    offset_t pos_min, pos_max;    int64_t pts_start;    offset_t ptr = url_ftell(pb);    pos_min = 0;    pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;    pts_start = get_pts(s, pos_min);    pts = get_pts(s, pos_max);    if (pts - 100000 > pts_start)         return;    ffm->write_index = FFM_PACKET_SIZE;    pts_start = get_pts(s, pos_min);    pts = get_pts(s, pos_max);    if (pts - 100000 <= pts_start) {        while (1) {            offset_t newpos;            int64_t newpts;            newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;            if (newpos == pos_min)                break;            newpts = get_pts(s, newpos);            if (newpts - 100000 <= pts) {                pos_max = newpos;                pts = newpts;            } else {                pos_min = newpos;            }        }        ffm->write_index += pos_max;    }    //printf("Adjusted write index from %lld to %lld: pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);    //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );    url_fseek(pb, ptr, SEEK_SET);}