Skip to content
Snippets Groups Projects
ffmdec.c 20.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • Fabrice Bellard's avatar
    Fabrice Bellard committed
    /*
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
     * FFM (ffserver live feed) demuxer
    
     * Copyright (c) 2001 Fabrice Bellard
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * This file is part of FFmpeg.
     *
     * FFmpeg is free software; you can redistribute it and/or
    
     * modify it under the terms of the GNU Lesser General Public
     * License as published by the Free Software Foundation; either
    
     * version 2.1 of the License, or (at your option) any later version.
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * FFmpeg is distributed in the hope that it will be useful,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * Lesser General Public License for more details.
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     *
    
     * You should have received a copy of the GNU Lesser General Public
    
     * License along with FFmpeg; if not, write to the Free Software
    
     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     */
    
    #include "libavutil/intreadwrite.h"
    
    #include "libavutil/intfloat.h"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    #include "avformat.h"
    
    #include "internal.h"
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
    #include "ffm.h"
    
    #include "avio_internal.h"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    static int ffm_is_avail_data(AVFormatContext *s, int size)
    {
        FFMContext *ffm = s->priv_data;
    
        int64_t pos, avail_size;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        int len;
    
        len = ffm->packet_end - ffm->packet_ptr;
    
        if (size <= len)
            return 1;
    
        pos = avio_tell(s->pb);
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
            if (pos == ffm->file_size)
    
                return AVERROR_EOF;
            avail_size = ffm->file_size - pos;
        } else {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (pos == ffm->write_index) {
            /* exactly at the end of stream */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else if (pos < ffm->write_index) {
            avail_size = ffm->write_index - pos;
        } else {
            avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
        if (size <= avail_size)
            return 1;
        else
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    }
    
    
    static int ffm_resync(AVFormatContext *s, int state)
    {
        av_log(s, AV_LOG_ERROR, "resyncing\n");
        while (state != PACKET_ID) {
            if (url_feof(s->pb)) {
                av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
                return -1;
            }
    
            state = (state << 8) | avio_r8(s->pb);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    /* first is true if we read the frame header */
    
    static int ffm_read_data(AVFormatContext *s,
    
                             uint8_t *buf, int size, int header)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        FFMContext *ffm = s->priv_data;
    
        AVIOContext *pb = s->pb;
    
        int len, fill_size, size1, frame_offset, id;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        size1 = size;
        while (size > 0) {
        redo:
            len = ffm->packet_end - ffm->packet_ptr;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (len > size)
                len = size;
            if (len == 0) {
    
                if (avio_tell(pb) == ffm->file_size)
    
                    avio_seek(pb, ffm->packet_size, SEEK_SET);
    
                if (pb->buffer_size != ffm->packet_size) {
                    int64_t tell = avio_tell(pb);
    
                    ffio_set_buf_size(pb, ffm->packet_size);
    
                    avio_seek(pb, tell, SEEK_SET);
                }
    
                id = avio_rb16(pb); /* PACKET_ID */
    
                if (id != PACKET_ID)
                    if (ffm_resync(s, id) < 0)
                        return -1;
    
                fill_size = avio_rb16(pb);
                ffm->dts = avio_rb64(pb);
                frame_offset = avio_rb16(pb);
                avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
    
                if (ffm->packet_end < ffm->packet || frame_offset < 0)
    
                    return -1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                /* if first packet or resynchronization packet, we must
                   handle it specifically */
                if (ffm->first_packet || (frame_offset & 0x8000)) {
    
                    if (!frame_offset) {
                        /* This packet has no frame headers in it */
    
                        if (avio_tell(pb) >= ffm->packet_size * 3LL) {
                            avio_seek(pb, -ffm->packet_size * 2LL, SEEK_CUR);
    
                            goto retry_read;
                        }
                        /* This is bad, we cannot find a valid frame header */
                        return 0;
                    }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    ffm->first_packet = 0;
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
                    if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
    
                        return -1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                    ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                        break;
                } else {
                    ffm->packet_ptr = ffm->packet;
                }
                goto redo;
            }
            memcpy(buf, ffm->packet_ptr, len);
            buf += len;
            ffm->packet_ptr += len;
            size -= len;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
        return size1 - size;
    }
    
    
    /* ensure that acutal seeking happens between FFM_PACKET_SIZE
       and file_size - FFM_PACKET_SIZE */
    
    static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
    
    {
        FFMContext *ffm = s->priv_data;
    
        AVIOContext *pb = s->pb;
    
        pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
        pos = FFMAX(pos, FFM_PACKET_SIZE);
    
        av_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
    
    static int64_t get_dts(AVFormatContext *s, int64_t pos)
    
        AVIOContext *pb = s->pb;
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
        int64_t dts;
    
        dts = avio_rb64(pb);
    
        av_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
        return dts;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
    static void adjust_write_index(AVFormatContext *s)
    {
        FFMContext *ffm = s->priv_data;
    
        AVIOContext *pb = s->pb;
    
        //int64_t orig_write_index = ffm->write_index;
        int64_t pos_min, pos_max;
    
        int64_t ptr = avio_tell(pb);
    
    
    
        pos_min = 0;
        pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
    
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
        pts_start = get_dts(s, pos_min);
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
        pts = get_dts(s, pos_max);
    
        if (pts - 100000 > pts_start)
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
        pts_start = get_dts(s, pos_min);
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
        pts = get_dts(s, pos_max);
    
    
        if (pts - 100000 <= pts_start) {
            while (1) {
    
                int64_t newpts;
    
                newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
    
                if (newpos == pos_min)
                    break;
    
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
                newpts = get_dts(s, newpos);
    
    
                if (newpts - 100000 <= pts) {
                    pos_max = newpos;
                    pts = newpts;
                } else {
                    pos_min = newpos;
                }
            }
            ffm->write_index += pos_max;
        }
    
    
        avio_seek(pb, ptr, SEEK_SET);
    
    static int ffm_close(AVFormatContext *s)
    {
        int i;
    
        for (i = 0; i < s->nb_streams; i++)
            av_freep(&s->streams[i]->codec->rc_eq);
    
        return 0;
    }
    
    
    static int ffm2_read_header(AVFormatContext *s)
    {
        FFMContext *ffm = s->priv_data;
        AVStream *st;
        AVIOContext *pb = s->pb;
        AVCodecContext *codec;
    
        ffm->packet_size = avio_rb32(pb);
        if (ffm->packet_size != FFM_PACKET_SIZE)
            goto fail;
        ffm->write_index = avio_rb64(pb);
        /* get also filesize */
        if (pb->seekable) {
            ffm->file_size = avio_size(pb);
            if (ffm->write_index && 0)
                adjust_write_index(s);
        } else {
            ffm->file_size = (UINT64_C(1) << 63) - 1;
        }
    
        while(!url_feof(pb)) {
            unsigned id = avio_rb32(pb);
            unsigned size = avio_rb32(pb);
            int64_t next = avio_tell(pb) + size;
            char rc_eq_buf[128];
    
            if(!id)
                break;
    
            switch(id) {
            case MKBETAG('M', 'A', 'I', 'N'):
                avio_rb32(pb); /* nb_streams */
                avio_rb32(pb); /* total bitrate */
                break;
            case MKBETAG('C', 'O', 'M', 'M'):
                st = avformat_new_stream(s, NULL);
                if (!st)
                    goto fail;
    
                avpriv_set_pts_info(st, 64, 1, 1000000);
    
                codec = st->codec;
                /* generic info */
                codec->codec_id = avio_rb32(pb);
                codec->codec_type = avio_r8(pb);
                codec->bit_rate = avio_rb32(pb);
                codec->flags = avio_rb32(pb);
                codec->flags2 = avio_rb32(pb);
                codec->debug = avio_rb32(pb);
                if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
                    codec->extradata_size = avio_rb32(pb);
                    codec->extradata = av_malloc(codec->extradata_size);
                    if (!codec->extradata)
                        return AVERROR(ENOMEM);
                    avio_read(pb, codec->extradata, codec->extradata_size);
                }
                avio_seek(pb, next, SEEK_SET);
                id = avio_rb32(pb);
                size = avio_rb32(pb);
                next = avio_tell(pb) + size;
                switch(id) {
                case MKBETAG('S', 'T', 'V', 'I'):
                    codec->time_base.num = avio_rb32(pb);
                    codec->time_base.den = avio_rb32(pb);
                    codec->width = avio_rb16(pb);
                    codec->height = avio_rb16(pb);
                    codec->gop_size = avio_rb16(pb);
                    codec->pix_fmt = avio_rb32(pb);
                    codec->qmin = avio_r8(pb);
                    codec->qmax = avio_r8(pb);
                    codec->max_qdiff = avio_r8(pb);
                    codec->qcompress = avio_rb16(pb) / 10000.0;
                    codec->qblur = avio_rb16(pb) / 10000.0;
                    codec->bit_rate_tolerance = avio_rb32(pb);
                    avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
                    codec->rc_eq = av_strdup(rc_eq_buf);
                    codec->rc_max_rate = avio_rb32(pb);
                    codec->rc_min_rate = avio_rb32(pb);
                    codec->rc_buffer_size = avio_rb32(pb);
                    codec->i_quant_factor = av_int2double(avio_rb64(pb));
                    codec->b_quant_factor = av_int2double(avio_rb64(pb));
                    codec->i_quant_offset = av_int2double(avio_rb64(pb));
                    codec->b_quant_offset = av_int2double(avio_rb64(pb));
                    codec->dct_algo = avio_rb32(pb);
                    codec->strict_std_compliance = avio_rb32(pb);
                    codec->max_b_frames = avio_rb32(pb);
                    codec->mpeg_quant = avio_rb32(pb);
                    codec->intra_dc_precision = avio_rb32(pb);
                    codec->me_method = avio_rb32(pb);
                    codec->mb_decision = avio_rb32(pb);
                    codec->nsse_weight = avio_rb32(pb);
                    codec->frame_skip_cmp = avio_rb32(pb);
                    codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
                    codec->codec_tag = avio_rb32(pb);
                    codec->thread_count = avio_r8(pb);
                    codec->coder_type = avio_rb32(pb);
                    codec->me_cmp = avio_rb32(pb);
                    codec->me_subpel_quality = avio_rb32(pb);
                    codec->me_range = avio_rb32(pb);
                    codec->keyint_min = avio_rb32(pb);
                    codec->scenechange_threshold = avio_rb32(pb);
                    codec->b_frame_strategy = avio_rb32(pb);
                    codec->qcompress = av_int2double(avio_rb64(pb));
                    codec->qblur = av_int2double(avio_rb64(pb));
                    codec->max_qdiff = avio_rb32(pb);
                    codec->refs = avio_rb32(pb);
                    break;
                case MKBETAG('S', 'T', 'A', 'U'):
                    codec->sample_rate = avio_rb32(pb);
                    codec->channels = avio_rl16(pb);
                    codec->frame_size = avio_rl16(pb);
                    break;
                }
                break;
            }
            avio_seek(pb, next, SEEK_SET);
        }
    
        /* get until end of block reached */
        while ((avio_tell(pb) % ffm->packet_size) != 0)
            avio_r8(pb);
    
        /* init packet demux */
        ffm->packet_ptr = ffm->packet;
        ffm->packet_end = ffm->packet;
        ffm->frame_offset = 0;
        ffm->dts = 0;
        ffm->read_state = READ_HEADER;
        ffm->first_packet = 1;
        return 0;
     fail:
        ffm_close(s);
        return -1;
    }
    
    static int ffm_read_header(AVFormatContext *s)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        FFMContext *ffm = s->priv_data;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        AVStream *st;
    
        AVIOContext *pb = s->pb;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        AVCodecContext *codec;
    
        int i, nb_streams;
    
        uint32_t tag;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        /* header */
    
        tag = avio_rl32(pb);
    
        if (tag == MKTAG('F', 'F', 'M', '2'))
            return ffm2_read_header(s);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (tag != MKTAG('F', 'F', 'M', '1'))
            goto fail;
    
        ffm->packet_size = avio_rb32(pb);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (ffm->packet_size != FFM_PACKET_SIZE)
            goto fail;
    
        ffm->write_index = avio_rb64(pb);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* get also filesize */
    
        if (pb->seekable) {
    
            ffm->file_size = avio_size(pb);
    
            if (ffm->write_index && 0)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        } else {
    
            ffm->file_size = (UINT64_C(1) << 63) - 1;
    
        nb_streams = avio_rb32(pb);
        avio_rb32(pb); /* total bitrate */
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* read each stream */
    
        for(i=0;i<nb_streams;i++) {
    
            st = avformat_new_stream(s, NULL);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (!st)
                goto fail;
    
            avpriv_set_pts_info(st, 64, 1, 1000000);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* generic info */
    
            codec->codec_id = avio_rb32(pb);
            codec->codec_type = avio_r8(pb); /* codec_type */
            codec->bit_rate = avio_rb32(pb);
            codec->flags = avio_rb32(pb);
            codec->flags2 = avio_rb32(pb);
            codec->debug = avio_rb32(pb);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* specific info */
            switch(codec->codec_type) {
    
                codec->time_base.num = avio_rb32(pb);
                codec->time_base.den = avio_rb32(pb);
                codec->width = avio_rb16(pb);
                codec->height = avio_rb16(pb);
                codec->gop_size = avio_rb16(pb);
                codec->pix_fmt = avio_rb32(pb);
                codec->qmin = avio_r8(pb);
                codec->qmax = avio_r8(pb);
                codec->max_qdiff = avio_r8(pb);
                codec->qcompress = avio_rb16(pb) / 10000.0;
                codec->qblur = avio_rb16(pb) / 10000.0;
                codec->bit_rate_tolerance = avio_rb32(pb);
    
                avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
                codec->rc_eq = av_strdup(rc_eq_buf);
    
                codec->rc_max_rate = avio_rb32(pb);
                codec->rc_min_rate = avio_rb32(pb);
                codec->rc_buffer_size = avio_rb32(pb);
    
                codec->i_quant_factor = av_int2double(avio_rb64(pb));
                codec->b_quant_factor = av_int2double(avio_rb64(pb));
                codec->i_quant_offset = av_int2double(avio_rb64(pb));
                codec->b_quant_offset = av_int2double(avio_rb64(pb));
    
                codec->dct_algo = avio_rb32(pb);
                codec->strict_std_compliance = avio_rb32(pb);
                codec->max_b_frames = avio_rb32(pb);
                codec->mpeg_quant = avio_rb32(pb);
                codec->intra_dc_precision = avio_rb32(pb);
                codec->me_method = avio_rb32(pb);
                codec->mb_decision = avio_rb32(pb);
                codec->nsse_weight = avio_rb32(pb);
                codec->frame_skip_cmp = avio_rb32(pb);
    
                codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
    
                codec->codec_tag = avio_rb32(pb);
                codec->thread_count = avio_r8(pb);
                codec->coder_type = avio_rb32(pb);
                codec->me_cmp = avio_rb32(pb);
                codec->me_subpel_quality = avio_rb32(pb);
                codec->me_range = avio_rb32(pb);
                codec->keyint_min = avio_rb32(pb);
                codec->scenechange_threshold = avio_rb32(pb);
                codec->b_frame_strategy = avio_rb32(pb);
    
                codec->qcompress = av_int2double(avio_rb64(pb));
                codec->qblur = av_int2double(avio_rb64(pb));
    
                codec->max_qdiff = avio_rb32(pb);
                codec->refs = avio_rb32(pb);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
    
                codec->sample_rate = avio_rb32(pb);
                codec->channels = avio_rl16(pb);
                codec->frame_size = avio_rl16(pb);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                break;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
            if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
    
                codec->extradata_size = avio_rb32(pb);
    
                codec->extradata = av_malloc(codec->extradata_size);
                if (!codec->extradata)
                    return AVERROR(ENOMEM);
    
                avio_read(pb, codec->extradata, codec->extradata_size);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        }
    
        /* get until end of block reached */
    
        while ((avio_tell(pb) % ffm->packet_size) != 0)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        /* init packet demux */
        ffm->packet_ptr = ffm->packet;
        ffm->packet_end = ffm->packet;
        ffm->frame_offset = 0;
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
        ffm->dts = 0;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        ffm->read_state = READ_HEADER;
        ffm->first_packet = 1;
        return 0;
     fail:
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return -1;
    }
    
    /* return < 0 if eof */
    static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
    {
        int size;
        FFMContext *ffm = s->priv_data;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        switch(ffm->read_state) {
        case READ_HEADER:
    
            if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
                return ret;
    
    
            av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
    
                   avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
    
            if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
    
            if (ffm->header[1] & FLAG_DTS)
                if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            ffm->read_state = READ_DATA;
            /* fall thru */
        case READ_DATA:
    
            size = AV_RB24(ffm->header + 2);
    
            if ((ret = ffm_is_avail_data(s, size)) < 0)
                return ret;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
            duration = AV_RB24(ffm->header + 5);
    
            if (av_new_packet(pkt, size) < 0) {
                return AVERROR(ENOMEM);
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            pkt->stream_index = ffm->header[0];
    
            if ((unsigned)pkt->stream_index >= s->nb_streams) {
                av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
                av_free_packet(pkt);
                ffm->read_state = READ_HEADER;
    
            pkt->pos = avio_tell(s->pb);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (ffm->header[1] & FLAG_KEY_FRAME)
    
                pkt->flags |= AV_PKT_FLAG_KEY;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            ffm->read_state = READ_HEADER;
            if (ffm_read_data(s, pkt->data, size, 0) != size) {
                /* bad case: desynchronized packet. we cancel all the packet loading */
                av_free_packet(pkt);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            }
    
            pkt->pts = AV_RB64(ffm->header+8);
            if (ffm->header[1] & FLAG_DTS)
                pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
            else
                pkt->dts = pkt->pts;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            break;
        }
        return 0;
    }
    
    /* seek to a given time in the file. The file read pointer is
    
    Diego Biurrun's avatar
    Diego Biurrun committed
       positioned at or before pts. XXX: the following code is quite
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
       approximative */
    
    static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    {
        FFMContext *ffm = s->priv_data;
    
        int64_t pos_min, pos_max, pos;
    
        int64_t pts_min, pts_max, pts;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        double pos1;
    
    
        av_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* 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;
        }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        while (pos_min <= pos_max) {
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
            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;
            }
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* linear interpolation */
    
            pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
                (double)(pts_max - pts_min);
    
            pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (pos <= pos_min)
                pos = pos_min;
            else if (pos >= pos_max)
                pos = pos_max;
    
    Baptiste Coudurier's avatar
    Baptiste Coudurier committed
            pts = get_dts(s, pos);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            /* 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;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
     found:
    
    
        /* reset read state */
        ffm->read_state = READ_HEADER;
        ffm->packet_ptr = ffm->packet;
        ffm->packet_end = ffm->packet;
        ffm->first_packet = 1;
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        return 0;
    }
    
    
    static int ffm_probe(AVProbeData *p)
    {
    
            p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
    
            (p->buf[3] == '1' || p->buf[3] == '2'))
    
            return AVPROBE_SCORE_MAX + 1;
        return 0;
    }
    
    
        .long_name      = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
    
        .priv_data_size = sizeof(FFMContext),
        .read_probe     = ffm_probe,
        .read_header    = ffm_read_header,
        .read_packet    = ffm_read_packet,
        .read_close     = ffm_close,
        .read_seek      = ffm_seek,
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    };