Statistics
| Branch: | Revision:

ffmpeg / libavformat / ffmdec.c @ e356fc57

History | View | Annotate | Download (16.1 KB)

1 de6d9b64 Fabrice Bellard
/*
2 85a6b01d Baptiste Coudurier
 * FFM (ffserver live feed) demuxer
3 406792e7 Diego Biurrun
 * Copyright (c) 2001 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 66d2ff2a Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 66d2ff2a Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 66d2ff2a Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 85a6b01d Baptiste Coudurier
22 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
23 de6d9b64 Fabrice Bellard
#include "avformat.h"
24 85a6b01d Baptiste Coudurier
#include "ffm.h"
25 b250f9c6 Aurelien Jacobs
#if CONFIG_FFSERVER
26 8be1c656 Fabrice Bellard
#include <unistd.h>
27 de6d9b64 Fabrice Bellard
28 bc5c918e Diego Biurrun
int64_t ffm_read_write_index(int fd)
29 278f987a Baptiste Coudurier
{
30
    uint8_t buf[8];
31
32
    lseek(fd, 8, SEEK_SET);
33 2dc91884 Baptiste Coudurier
    if (read(fd, buf, 8) != 8)
34
        return AVERROR(EIO);
35 278f987a Baptiste Coudurier
    return AV_RB64(buf);
36
}
37
38 6fcce4f9 Patrik Kullman
int ffm_write_write_index(int fd, int64_t pos)
39 278f987a Baptiste Coudurier
{
40
    uint8_t buf[8];
41
    int i;
42
43
    for(i=0;i<8;i++)
44
        buf[i] = (pos >> (56 - i * 8)) & 0xff;
45
    lseek(fd, 8, SEEK_SET);
46 6fcce4f9 Patrik Kullman
    if (write(fd, buf, 8) != 8)
47
        return AVERROR(EIO);
48
    return 8;
49 278f987a Baptiste Coudurier
}
50
51 bc5c918e Diego Biurrun
void ffm_set_write_index(AVFormatContext *s, int64_t pos, int64_t file_size)
52 278f987a Baptiste Coudurier
{
53
    FFMContext *ffm = s->priv_data;
54
    ffm->write_index = pos;
55
    ffm->file_size = file_size;
56
}
57
#endif // CONFIG_FFSERVER
58
59 de6d9b64 Fabrice Bellard
static int ffm_is_avail_data(AVFormatContext *s, int size)
60
{
61
    FFMContext *ffm = s->priv_data;
62 bc5c918e Diego Biurrun
    int64_t pos, avail_size;
63 de6d9b64 Fabrice Bellard
    int len;
64
65
    len = ffm->packet_end - ffm->packet_ptr;
66 ba26712b Baptiste Coudurier
    if (size <= len)
67
        return 1;
68 899681cd Björn Axelsson
    pos = url_ftell(s->pb);
69 b9edbe99 Baptiste Coudurier
    if (!ffm->write_index) {
70 21c6438f Baptiste Coudurier
        if (pos == ffm->file_size)
71 b9edbe99 Baptiste Coudurier
            return AVERROR_EOF;
72
        avail_size = ffm->file_size - pos;
73
    } else {
74 de6d9b64 Fabrice Bellard
    if (pos == ffm->write_index) {
75
        /* exactly at the end of stream */
76 b9edbe99 Baptiste Coudurier
        return AVERROR(EAGAIN);
77 de6d9b64 Fabrice Bellard
    } else if (pos < ffm->write_index) {
78
        avail_size = ffm->write_index - pos;
79
    } else {
80
        avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
81
    }
82 b9edbe99 Baptiste Coudurier
    }
83 de6d9b64 Fabrice Bellard
    avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
84
    if (size <= avail_size)
85
        return 1;
86
    else
87 b9edbe99 Baptiste Coudurier
        return AVERROR(EAGAIN);
88 de6d9b64 Fabrice Bellard
}
89
90 7c45723a Baptiste Coudurier
static int ffm_resync(AVFormatContext *s, int state)
91
{
92
    av_log(s, AV_LOG_ERROR, "resyncing\n");
93
    while (state != PACKET_ID) {
94
        if (url_feof(s->pb)) {
95
            av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
96
            return -1;
97
        }
98 b7effd4e Anton Khirnov
        state = (state << 8) | avio_r8(s->pb);
99 7c45723a Baptiste Coudurier
    }
100
    return 0;
101
}
102
103 de6d9b64 Fabrice Bellard
/* first is true if we read the frame header */
104 4606ac8d Zdenek Kabelac
static int ffm_read_data(AVFormatContext *s,
105 b30bb535 Baptiste Coudurier
                         uint8_t *buf, int size, int header)
106 de6d9b64 Fabrice Bellard
{
107
    FFMContext *ffm = s->priv_data;
108 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
109 7c45723a Baptiste Coudurier
    int len, fill_size, size1, frame_offset, id;
110 de6d9b64 Fabrice Bellard
111
    size1 = size;
112
    while (size > 0) {
113
    redo:
114
        len = ffm->packet_end - ffm->packet_ptr;
115 e5ece183 Baptiste Coudurier
        if (len < 0)
116
            return -1;
117 de6d9b64 Fabrice Bellard
        if (len > size)
118
            len = size;
119
        if (len == 0) {
120
            if (url_ftell(pb) == ffm->file_size)
121 6b4aa5da Anton Khirnov
                avio_seek(pb, ffm->packet_size, SEEK_SET);
122 5e57424d Philip Gladstone
    retry_read:
123 b7effd4e Anton Khirnov
            id = avio_rb16(pb); /* PACKET_ID */
124 7c45723a Baptiste Coudurier
            if (id != PACKET_ID)
125
                if (ffm_resync(s, id) < 0)
126
                    return -1;
127 b7effd4e Anton Khirnov
            fill_size = avio_rb16(pb);
128
            ffm->dts = avio_rb64(pb);
129
            frame_offset = avio_rb16(pb);
130
            avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
131 de6d9b64 Fabrice Bellard
            ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
132 eea2f032 Baptiste Coudurier
            if (ffm->packet_end < ffm->packet || frame_offset < 0)
133 71c32f19 Michael Niedermayer
                return -1;
134 de6d9b64 Fabrice Bellard
            /* if first packet or resynchronization packet, we must
135
               handle it specifically */
136
            if (ffm->first_packet || (frame_offset & 0x8000)) {
137 5e57424d Philip Gladstone
                if (!frame_offset) {
138
                    /* This packet has no frame headers in it */
139
                    if (url_ftell(pb) >= ffm->packet_size * 3) {
140 6b4aa5da Anton Khirnov
                        avio_seek(pb, -ffm->packet_size * 2, SEEK_CUR);
141 5e57424d Philip Gladstone
                        goto retry_read;
142
                    }
143
                    /* This is bad, we cannot find a valid frame header */
144
                    return 0;
145
                }
146 de6d9b64 Fabrice Bellard
                ffm->first_packet = 0;
147 a077f3bc Baptiste Coudurier
                if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE)
148 71c32f19 Michael Niedermayer
                    return -1;
149 de6d9b64 Fabrice Bellard
                ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
150 b30bb535 Baptiste Coudurier
                if (!header)
151 de6d9b64 Fabrice Bellard
                    break;
152
            } else {
153
                ffm->packet_ptr = ffm->packet;
154
            }
155
            goto redo;
156
        }
157
        memcpy(buf, ffm->packet_ptr, len);
158
        buf += len;
159
        ffm->packet_ptr += len;
160
        size -= len;
161 b30bb535 Baptiste Coudurier
        header = 0;
162 de6d9b64 Fabrice Bellard
    }
163
    return size1 - size;
164
}
165
166 4adcbbbe Baptiste Coudurier
//#define DEBUG_SEEK
167
168 92a0f338 Baptiste Coudurier
/* ensure that acutal seeking happens between FFM_PACKET_SIZE
169
   and file_size - FFM_PACKET_SIZE */
170 bc5c918e Diego Biurrun
static void ffm_seek1(AVFormatContext *s, int64_t pos1)
171 a1e01307 Baptiste Coudurier
{
172
    FFMContext *ffm = s->priv_data;
173 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
174 bc5c918e Diego Biurrun
    int64_t pos;
175 a1e01307 Baptiste Coudurier
176 92a0f338 Baptiste Coudurier
    pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
177
    pos = FFMAX(pos, FFM_PACKET_SIZE);
178 a1e01307 Baptiste Coudurier
#ifdef DEBUG_SEEK
179 8999c833 Baptiste Coudurier
    av_log(s, AV_LOG_DEBUG, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
180 a1e01307 Baptiste Coudurier
#endif
181 6b4aa5da Anton Khirnov
    avio_seek(pb, pos, SEEK_SET);
182 a1e01307 Baptiste Coudurier
}
183
184 bc5c918e Diego Biurrun
static int64_t get_dts(AVFormatContext *s, int64_t pos)
185 979b1a06 Baptiste Coudurier
{
186 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
187 fabb990e Baptiste Coudurier
    int64_t dts;
188 979b1a06 Baptiste Coudurier
189
    ffm_seek1(s, pos);
190 e356fc57 Anton Khirnov
    avio_seek(pb, 4, SEEK_CUR);
191 b7effd4e Anton Khirnov
    dts = avio_rb64(pb);
192 979b1a06 Baptiste Coudurier
#ifdef DEBUG_SEEK
193 be2a6e2f Baptiste Coudurier
    av_log(s, AV_LOG_DEBUG, "dts=%0.6f\n", dts / 1000000.0);
194 979b1a06 Baptiste Coudurier
#endif
195 fabb990e Baptiste Coudurier
    return dts;
196 979b1a06 Baptiste Coudurier
}
197 de6d9b64 Fabrice Bellard
198 91628427 Philip Gladstone
static void adjust_write_index(AVFormatContext *s)
199
{
200
    FFMContext *ffm = s->priv_data;
201 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
202 91628427 Philip Gladstone
    int64_t pts;
203 bc5c918e Diego Biurrun
    //int64_t orig_write_index = ffm->write_index;
204
    int64_t pos_min, pos_max;
205 91628427 Philip Gladstone
    int64_t pts_start;
206 bc5c918e Diego Biurrun
    int64_t ptr = url_ftell(pb);
207 91628427 Philip Gladstone
208
209
    pos_min = 0;
210
    pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
211
212 fabb990e Baptiste Coudurier
    pts_start = get_dts(s, pos_min);
213 91628427 Philip Gladstone
214 fabb990e Baptiste Coudurier
    pts = get_dts(s, pos_max);
215 91628427 Philip Gladstone
216 115329f1 Diego Biurrun
    if (pts - 100000 > pts_start)
217 79292510 Michael Niedermayer
        goto end;
218 91628427 Philip Gladstone
219
    ffm->write_index = FFM_PACKET_SIZE;
220
221 fabb990e Baptiste Coudurier
    pts_start = get_dts(s, pos_min);
222 91628427 Philip Gladstone
223 fabb990e Baptiste Coudurier
    pts = get_dts(s, pos_max);
224 91628427 Philip Gladstone
225
    if (pts - 100000 <= pts_start) {
226
        while (1) {
227 bc5c918e Diego Biurrun
            int64_t newpos;
228 91628427 Philip Gladstone
            int64_t newpts;
229
230
            newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
231
232
            if (newpos == pos_min)
233
                break;
234
235 fabb990e Baptiste Coudurier
            newpts = get_dts(s, newpos);
236 91628427 Philip Gladstone
237
            if (newpts - 100000 <= pts) {
238
                pos_max = newpos;
239
                pts = newpts;
240
            } else {
241
                pos_min = newpos;
242
            }
243
        }
244
        ffm->write_index += pos_max;
245
    }
246
247 949b1a13 Steve L'Homme
    //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
248 fabb990e Baptiste Coudurier
    //printf("pts range %0.6f - %0.6f\n", get_dts(s, 0) / 1000000. , get_dts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
249 91628427 Philip Gladstone
250 79292510 Michael Niedermayer
 end:
251 6b4aa5da Anton Khirnov
    avio_seek(pb, ptr, SEEK_SET);
252 91628427 Philip Gladstone
}
253
254
255 e36a3906 Reimar Döffinger
static int ffm_close(AVFormatContext *s)
256
{
257
    int i;
258
259
    for (i = 0; i < s->nb_streams; i++)
260
        av_freep(&s->streams[i]->codec->rc_eq);
261
262
    return 0;
263
}
264
265 527b46ba Reimar Döffinger
266 de6d9b64 Fabrice Bellard
static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
267
{
268 c9a65ca8 Fabrice Bellard
    FFMContext *ffm = s->priv_data;
269 de6d9b64 Fabrice Bellard
    AVStream *st;
270 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
271 de6d9b64 Fabrice Bellard
    AVCodecContext *codec;
272 fa26a29d Fabrice Bellard
    int i, nb_streams;
273 0c1a9eda Zdenek Kabelac
    uint32_t tag;
274 de6d9b64 Fabrice Bellard
275
    /* header */
276 b7effd4e Anton Khirnov
    tag = avio_rl32(pb);
277 de6d9b64 Fabrice Bellard
    if (tag != MKTAG('F', 'F', 'M', '1'))
278
        goto fail;
279 b7effd4e Anton Khirnov
    ffm->packet_size = avio_rb32(pb);
280 de6d9b64 Fabrice Bellard
    if (ffm->packet_size != FFM_PACKET_SIZE)
281
        goto fail;
282 b7effd4e Anton Khirnov
    ffm->write_index = avio_rb64(pb);
283 de6d9b64 Fabrice Bellard
    /* get also filesize */
284
    if (!url_is_streamed(pb)) {
285 a965c478 Aurelien Jacobs
        ffm->file_size = url_fsize(pb);
286 b9edbe99 Baptiste Coudurier
        if (ffm->write_index)
287
            adjust_write_index(s);
288 de6d9b64 Fabrice Bellard
    } else {
289 8da9266c Måns Rullgård
        ffm->file_size = (UINT64_C(1) << 63) - 1;
290 de6d9b64 Fabrice Bellard
    }
291
292 b7effd4e Anton Khirnov
    nb_streams = avio_rb32(pb);
293
    avio_rb32(pb); /* total bitrate */
294 de6d9b64 Fabrice Bellard
    /* read each stream */
295 fa26a29d Fabrice Bellard
    for(i=0;i<nb_streams;i++) {
296 75bdb984 Philip Gladstone
        char rc_eq_buf[128];
297
298 fa26a29d Fabrice Bellard
        st = av_new_stream(s, 0);
299 de6d9b64 Fabrice Bellard
        if (!st)
300
            goto fail;
301 115329f1 Diego Biurrun
302 e928649b Michael Niedermayer
        av_set_pts_info(st, 64, 1, 1000000);
303 115329f1 Diego Biurrun
304 01f4895c Michael Niedermayer
        codec = st->codec;
305 de6d9b64 Fabrice Bellard
        /* generic info */
306 b7effd4e Anton Khirnov
        codec->codec_id = avio_rb32(pb);
307
        codec->codec_type = avio_r8(pb); /* codec_type */
308
        codec->bit_rate = avio_rb32(pb);
309
        st->quality = avio_rb32(pb);
310
        codec->flags = avio_rb32(pb);
311
        codec->flags2 = avio_rb32(pb);
312
        codec->debug = avio_rb32(pb);
313 de6d9b64 Fabrice Bellard
        /* specific info */
314
        switch(codec->codec_type) {
315 72415b2a Stefano Sabatini
        case AVMEDIA_TYPE_VIDEO:
316 b7effd4e Anton Khirnov
            codec->time_base.num = avio_rb32(pb);
317
            codec->time_base.den = avio_rb32(pb);
318
            codec->width = avio_rb16(pb);
319
            codec->height = avio_rb16(pb);
320
            codec->gop_size = avio_rb16(pb);
321
            codec->pix_fmt = avio_rb32(pb);
322
            codec->qmin = avio_r8(pb);
323
            codec->qmax = avio_r8(pb);
324
            codec->max_qdiff = avio_r8(pb);
325
            codec->qcompress = avio_rb16(pb) / 10000.0;
326
            codec->qblur = avio_rb16(pb) / 10000.0;
327
            codec->bit_rate_tolerance = avio_rb32(pb);
328 e9a9e0c2 Fabrice Bellard
            codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
329 b7effd4e Anton Khirnov
            codec->rc_max_rate = avio_rb32(pb);
330
            codec->rc_min_rate = avio_rb32(pb);
331
            codec->rc_buffer_size = avio_rb32(pb);
332
            codec->i_quant_factor = av_int2dbl(avio_rb64(pb));
333
            codec->b_quant_factor = av_int2dbl(avio_rb64(pb));
334
            codec->i_quant_offset = av_int2dbl(avio_rb64(pb));
335
            codec->b_quant_offset = av_int2dbl(avio_rb64(pb));
336
            codec->dct_algo = avio_rb32(pb);
337
            codec->strict_std_compliance = avio_rb32(pb);
338
            codec->max_b_frames = avio_rb32(pb);
339
            codec->luma_elim_threshold = avio_rb32(pb);
340
            codec->chroma_elim_threshold = avio_rb32(pb);
341
            codec->mpeg_quant = avio_rb32(pb);
342
            codec->intra_dc_precision = avio_rb32(pb);
343
            codec->me_method = avio_rb32(pb);
344
            codec->mb_decision = avio_rb32(pb);
345
            codec->nsse_weight = avio_rb32(pb);
346
            codec->frame_skip_cmp = avio_rb32(pb);
347
            codec->rc_buffer_aggressivity = av_int2dbl(avio_rb64(pb));
348
            codec->codec_tag = avio_rb32(pb);
349
            codec->thread_count = avio_r8(pb);
350
            codec->coder_type = avio_rb32(pb);
351
            codec->me_cmp = avio_rb32(pb);
352
            codec->partitions = avio_rb32(pb);
353
            codec->me_subpel_quality = avio_rb32(pb);
354
            codec->me_range = avio_rb32(pb);
355
            codec->keyint_min = avio_rb32(pb);
356
            codec->scenechange_threshold = avio_rb32(pb);
357
            codec->b_frame_strategy = avio_rb32(pb);
358
            codec->qcompress = av_int2dbl(avio_rb64(pb));
359
            codec->qblur = av_int2dbl(avio_rb64(pb));
360
            codec->max_qdiff = avio_rb32(pb);
361
            codec->refs = avio_rb32(pb);
362
            codec->directpred = avio_rb32(pb);
363 de6d9b64 Fabrice Bellard
            break;
364 72415b2a Stefano Sabatini
        case AVMEDIA_TYPE_AUDIO:
365 b7effd4e Anton Khirnov
            codec->sample_rate = avio_rb32(pb);
366
            codec->channels = avio_rl16(pb);
367
            codec->frame_size = avio_rl16(pb);
368
            codec->sample_fmt = (int16_t) avio_rl16(pb);
369 de6d9b64 Fabrice Bellard
            break;
370 20f01548 Philip Gladstone
        default:
371 75bdb984 Philip Gladstone
            goto fail;
372 de6d9b64 Fabrice Bellard
        }
373 7080cbe2 Baptiste Coudurier
        if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
374 b7effd4e Anton Khirnov
            codec->extradata_size = avio_rb32(pb);
375 7080cbe2 Baptiste Coudurier
            codec->extradata = av_malloc(codec->extradata_size);
376
            if (!codec->extradata)
377
                return AVERROR(ENOMEM);
378 b7effd4e Anton Khirnov
            avio_read(pb, codec->extradata, codec->extradata_size);
379 7080cbe2 Baptiste Coudurier
        }
380 de6d9b64 Fabrice Bellard
    }
381
382
    /* get until end of block reached */
383
    while ((url_ftell(pb) % ffm->packet_size) != 0)
384 b7effd4e Anton Khirnov
        avio_r8(pb);
385 de6d9b64 Fabrice Bellard
386
    /* init packet demux */
387
    ffm->packet_ptr = ffm->packet;
388
    ffm->packet_end = ffm->packet;
389
    ffm->frame_offset = 0;
390 fabb990e Baptiste Coudurier
    ffm->dts = 0;
391 de6d9b64 Fabrice Bellard
    ffm->read_state = READ_HEADER;
392
    ffm->first_packet = 1;
393
    return 0;
394
 fail:
395 527b46ba Reimar Döffinger
    ffm_close(s);
396 de6d9b64 Fabrice Bellard
    return -1;
397
}
398
399
/* return < 0 if eof */
400
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
401
{
402
    int size;
403
    FFMContext *ffm = s->priv_data;
404 b9edbe99 Baptiste Coudurier
    int duration, ret;
405 eee99eb3 Baptiste Coudurier
406 de6d9b64 Fabrice Bellard
    switch(ffm->read_state) {
407
    case READ_HEADER:
408 b9edbe99 Baptiste Coudurier
        if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
409
            return ret;
410
411 dfd2a005 Luca Barbato
        av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
412 e785efc4 Albert Astals Cid
               url_ftell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
413 115329f1 Diego Biurrun
        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
414 66d2ff2a Fabrice Bellard
            FRAME_HEADER_SIZE)
415 b9edbe99 Baptiste Coudurier
            return -1;
416 3c6a9f66 Baptiste Coudurier
        if (ffm->header[1] & FLAG_DTS)
417
            if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
418 b9edbe99 Baptiste Coudurier
                return -1;
419 de6d9b64 Fabrice Bellard
#if 0
420 a299832f Baptiste Coudurier
        av_hexdump_log(s, AV_LOG_DEBUG, ffm->header, FRAME_HEADER_SIZE);
421 de6d9b64 Fabrice Bellard
#endif
422
        ffm->read_state = READ_DATA;
423
        /* fall thru */
424
    case READ_DATA:
425 80fb8234 Ronald S. Bultje
        size = AV_RB24(ffm->header + 2);
426 b9edbe99 Baptiste Coudurier
        if ((ret = ffm_is_avail_data(s, size)) < 0)
427
            return ret;
428 de6d9b64 Fabrice Bellard
429 80fb8234 Ronald S. Bultje
        duration = AV_RB24(ffm->header + 5);
430 20f01548 Philip Gladstone
431 de6d9b64 Fabrice Bellard
        av_new_packet(pkt, size);
432
        pkt->stream_index = ffm->header[0];
433 2ea2340e Baptiste Coudurier
        if ((unsigned)pkt->stream_index >= s->nb_streams) {
434
            av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
435
            av_free_packet(pkt);
436
            ffm->read_state = READ_HEADER;
437 b9edbe99 Baptiste Coudurier
            return -1;
438 2ea2340e Baptiste Coudurier
        }
439 899681cd Björn Axelsson
        pkt->pos = url_ftell(s->pb);
440 de6d9b64 Fabrice Bellard
        if (ffm->header[1] & FLAG_KEY_FRAME)
441 cc947f04 Jean-Daniel Dupas
            pkt->flags |= AV_PKT_FLAG_KEY;
442 4606ac8d Zdenek Kabelac
443 de6d9b64 Fabrice Bellard
        ffm->read_state = READ_HEADER;
444
        if (ffm_read_data(s, pkt->data, size, 0) != size) {
445
            /* bad case: desynchronized packet. we cancel all the packet loading */
446
            av_free_packet(pkt);
447 b9edbe99 Baptiste Coudurier
            return -1;
448 de6d9b64 Fabrice Bellard
        }
449 3c6a9f66 Baptiste Coudurier
        pkt->pts = AV_RB64(ffm->header+8);
450
        if (ffm->header[1] & FLAG_DTS)
451
            pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
452
        else
453
            pkt->dts = pkt->pts;
454 20f01548 Philip Gladstone
        pkt->duration = duration;
455 de6d9b64 Fabrice Bellard
        break;
456
    }
457
    return 0;
458
}
459
460
/* seek to a given time in the file. The file read pointer is
461 aa6b38c2 Diego Biurrun
   positioned at or before pts. XXX: the following code is quite
462 de6d9b64 Fabrice Bellard
   approximative */
463 7b3c1382 Michael Niedermayer
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
464 de6d9b64 Fabrice Bellard
{
465
    FFMContext *ffm = s->priv_data;
466 bc5c918e Diego Biurrun
    int64_t pos_min, pos_max, pos;
467 0c1a9eda Zdenek Kabelac
    int64_t pts_min, pts_max, pts;
468 de6d9b64 Fabrice Bellard
    double pos1;
469
470
#ifdef DEBUG_SEEK
471 8999c833 Baptiste Coudurier
    av_log(s, AV_LOG_DEBUG, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
472 de6d9b64 Fabrice Bellard
#endif
473
    /* find the position using linear interpolation (better than
474
       dichotomy in typical cases) */
475 92a0f338 Baptiste Coudurier
    pos_min = FFM_PACKET_SIZE;
476
    pos_max = ffm->file_size - FFM_PACKET_SIZE;
477 de6d9b64 Fabrice Bellard
    while (pos_min <= pos_max) {
478 fabb990e Baptiste Coudurier
        pts_min = get_dts(s, pos_min);
479
        pts_max = get_dts(s, pos_max);
480 de6d9b64 Fabrice Bellard
        /* linear interpolation */
481 4606ac8d Zdenek Kabelac
        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
482 de6d9b64 Fabrice Bellard
            (double)(pts_max - pts_min);
483 0c1a9eda Zdenek Kabelac
        pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
484 de6d9b64 Fabrice Bellard
        if (pos <= pos_min)
485
            pos = pos_min;
486
        else if (pos >= pos_max)
487
            pos = pos_max;
488 fabb990e Baptiste Coudurier
        pts = get_dts(s, pos);
489 de6d9b64 Fabrice Bellard
        /* check if we are lucky */
490
        if (pts == wanted_pts) {
491
            goto found;
492
        } else if (pts > wanted_pts) {
493
            pos_max = pos - FFM_PACKET_SIZE;
494
        } else {
495
            pos_min = pos + FFM_PACKET_SIZE;
496
        }
497
    }
498 7b3c1382 Michael Niedermayer
    pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
499 92a0f338 Baptiste Coudurier
500 de6d9b64 Fabrice Bellard
 found:
501
    ffm_seek1(s, pos);
502 c07d64c8 Baptiste Coudurier
503
    /* reset read state */
504
    ffm->read_state = READ_HEADER;
505
    ffm->packet_ptr = ffm->packet;
506
    ffm->packet_end = ffm->packet;
507
    ffm->first_packet = 1;
508
509 de6d9b64 Fabrice Bellard
    return 0;
510
}
511
512 07c4ed85 Philip Gladstone
static int ffm_probe(AVProbeData *p)
513
{
514 87e87886 Michael Niedermayer
    if (
515 115329f1 Diego Biurrun
        p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
516 66d2ff2a Fabrice Bellard
        p->buf[3] == '1')
517 07c4ed85 Philip Gladstone
        return AVPROBE_SCORE_MAX + 1;
518
    return 0;
519
}
520
521 c6610a21 Diego Elio Pettenò
AVInputFormat ff_ffm_demuxer = {
522 c9a65ca8 Fabrice Bellard
    "ffm",
523 af274fd1 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
524 66d2ff2a Fabrice Bellard
    sizeof(FFMContext),
525 07c4ed85 Philip Gladstone
    ffm_probe,
526 c9a65ca8 Fabrice Bellard
    ffm_read_header,
527
    ffm_read_packet,
528 2dc22a64 Vitor Sessak
    ffm_close,
529 c9a65ca8 Fabrice Bellard
    ffm_seek,
530
};