Statistics
| Branch: | Revision:

ffmpeg / libavformat / ffmdec.c @ 45a8a02a

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 a2704c97 Anton Khirnov
    pos = avio_tell(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 66e5b1df Anton Khirnov
        if (s->pb->eof_reached) {
95 7c45723a Baptiste Coudurier
            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 a2704c97 Anton Khirnov
            if (avio_tell(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 a2704c97 Anton Khirnov
                    if (avio_tell(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 45a8a02a Anton Khirnov
    avio_skip(pb, 4);
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 a2704c97 Anton Khirnov
    int64_t ptr = avio_tell(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 76aa876e Anton Khirnov
        ffm->file_size = avio_size(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 e16ead07 Anton Khirnov
            avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
329
            codec->rc_eq = av_strdup(rc_eq_buf);
330 b7effd4e Anton Khirnov
            codec->rc_max_rate = avio_rb32(pb);
331
            codec->rc_min_rate = avio_rb32(pb);
332
            codec->rc_buffer_size = avio_rb32(pb);
333
            codec->i_quant_factor = av_int2dbl(avio_rb64(pb));
334
            codec->b_quant_factor = av_int2dbl(avio_rb64(pb));
335
            codec->i_quant_offset = av_int2dbl(avio_rb64(pb));
336
            codec->b_quant_offset = av_int2dbl(avio_rb64(pb));
337
            codec->dct_algo = avio_rb32(pb);
338
            codec->strict_std_compliance = avio_rb32(pb);
339
            codec->max_b_frames = avio_rb32(pb);
340
            codec->luma_elim_threshold = avio_rb32(pb);
341
            codec->chroma_elim_threshold = avio_rb32(pb);
342
            codec->mpeg_quant = avio_rb32(pb);
343
            codec->intra_dc_precision = avio_rb32(pb);
344
            codec->me_method = avio_rb32(pb);
345
            codec->mb_decision = avio_rb32(pb);
346
            codec->nsse_weight = avio_rb32(pb);
347
            codec->frame_skip_cmp = avio_rb32(pb);
348
            codec->rc_buffer_aggressivity = av_int2dbl(avio_rb64(pb));
349
            codec->codec_tag = avio_rb32(pb);
350
            codec->thread_count = avio_r8(pb);
351
            codec->coder_type = avio_rb32(pb);
352
            codec->me_cmp = avio_rb32(pb);
353
            codec->partitions = avio_rb32(pb);
354
            codec->me_subpel_quality = avio_rb32(pb);
355
            codec->me_range = avio_rb32(pb);
356
            codec->keyint_min = avio_rb32(pb);
357
            codec->scenechange_threshold = avio_rb32(pb);
358
            codec->b_frame_strategy = avio_rb32(pb);
359
            codec->qcompress = av_int2dbl(avio_rb64(pb));
360
            codec->qblur = av_int2dbl(avio_rb64(pb));
361
            codec->max_qdiff = avio_rb32(pb);
362
            codec->refs = avio_rb32(pb);
363
            codec->directpred = avio_rb32(pb);
364 de6d9b64 Fabrice Bellard
            break;
365 72415b2a Stefano Sabatini
        case AVMEDIA_TYPE_AUDIO:
366 b7effd4e Anton Khirnov
            codec->sample_rate = avio_rb32(pb);
367
            codec->channels = avio_rl16(pb);
368
            codec->frame_size = avio_rl16(pb);
369
            codec->sample_fmt = (int16_t) avio_rl16(pb);
370 de6d9b64 Fabrice Bellard
            break;
371 20f01548 Philip Gladstone
        default:
372 75bdb984 Philip Gladstone
            goto fail;
373 de6d9b64 Fabrice Bellard
        }
374 7080cbe2 Baptiste Coudurier
        if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
375 b7effd4e Anton Khirnov
            codec->extradata_size = avio_rb32(pb);
376 7080cbe2 Baptiste Coudurier
            codec->extradata = av_malloc(codec->extradata_size);
377
            if (!codec->extradata)
378
                return AVERROR(ENOMEM);
379 b7effd4e Anton Khirnov
            avio_read(pb, codec->extradata, codec->extradata_size);
380 7080cbe2 Baptiste Coudurier
        }
381 de6d9b64 Fabrice Bellard
    }
382
383
    /* get until end of block reached */
384 a2704c97 Anton Khirnov
    while ((avio_tell(pb) % ffm->packet_size) != 0)
385 b7effd4e Anton Khirnov
        avio_r8(pb);
386 de6d9b64 Fabrice Bellard
387
    /* init packet demux */
388
    ffm->packet_ptr = ffm->packet;
389
    ffm->packet_end = ffm->packet;
390
    ffm->frame_offset = 0;
391 fabb990e Baptiste Coudurier
    ffm->dts = 0;
392 de6d9b64 Fabrice Bellard
    ffm->read_state = READ_HEADER;
393
    ffm->first_packet = 1;
394
    return 0;
395
 fail:
396 527b46ba Reimar Döffinger
    ffm_close(s);
397 de6d9b64 Fabrice Bellard
    return -1;
398
}
399
400
/* return < 0 if eof */
401
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
402
{
403
    int size;
404
    FFMContext *ffm = s->priv_data;
405 b9edbe99 Baptiste Coudurier
    int duration, ret;
406 eee99eb3 Baptiste Coudurier
407 de6d9b64 Fabrice Bellard
    switch(ffm->read_state) {
408
    case READ_HEADER:
409 b9edbe99 Baptiste Coudurier
        if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
410
            return ret;
411
412 dfd2a005 Luca Barbato
        av_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
413 a2704c97 Anton Khirnov
               avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
414 115329f1 Diego Biurrun
        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
415 66d2ff2a Fabrice Bellard
            FRAME_HEADER_SIZE)
416 b9edbe99 Baptiste Coudurier
            return -1;
417 3c6a9f66 Baptiste Coudurier
        if (ffm->header[1] & FLAG_DTS)
418
            if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
419 b9edbe99 Baptiste Coudurier
                return -1;
420 de6d9b64 Fabrice Bellard
#if 0
421 a299832f Baptiste Coudurier
        av_hexdump_log(s, AV_LOG_DEBUG, ffm->header, FRAME_HEADER_SIZE);
422 de6d9b64 Fabrice Bellard
#endif
423
        ffm->read_state = READ_DATA;
424
        /* fall thru */
425
    case READ_DATA:
426 80fb8234 Ronald S. Bultje
        size = AV_RB24(ffm->header + 2);
427 b9edbe99 Baptiste Coudurier
        if ((ret = ffm_is_avail_data(s, size)) < 0)
428
            return ret;
429 de6d9b64 Fabrice Bellard
430 80fb8234 Ronald S. Bultje
        duration = AV_RB24(ffm->header + 5);
431 20f01548 Philip Gladstone
432 de6d9b64 Fabrice Bellard
        av_new_packet(pkt, size);
433
        pkt->stream_index = ffm->header[0];
434 2ea2340e Baptiste Coudurier
        if ((unsigned)pkt->stream_index >= s->nb_streams) {
435
            av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
436
            av_free_packet(pkt);
437
            ffm->read_state = READ_HEADER;
438 b9edbe99 Baptiste Coudurier
            return -1;
439 2ea2340e Baptiste Coudurier
        }
440 a2704c97 Anton Khirnov
        pkt->pos = avio_tell(s->pb);
441 de6d9b64 Fabrice Bellard
        if (ffm->header[1] & FLAG_KEY_FRAME)
442 cc947f04 Jean-Daniel Dupas
            pkt->flags |= AV_PKT_FLAG_KEY;
443 4606ac8d Zdenek Kabelac
444 de6d9b64 Fabrice Bellard
        ffm->read_state = READ_HEADER;
445
        if (ffm_read_data(s, pkt->data, size, 0) != size) {
446
            /* bad case: desynchronized packet. we cancel all the packet loading */
447
            av_free_packet(pkt);
448 b9edbe99 Baptiste Coudurier
            return -1;
449 de6d9b64 Fabrice Bellard
        }
450 3c6a9f66 Baptiste Coudurier
        pkt->pts = AV_RB64(ffm->header+8);
451
        if (ffm->header[1] & FLAG_DTS)
452
            pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
453
        else
454
            pkt->dts = pkt->pts;
455 20f01548 Philip Gladstone
        pkt->duration = duration;
456 de6d9b64 Fabrice Bellard
        break;
457
    }
458
    return 0;
459
}
460
461
/* seek to a given time in the file. The file read pointer is
462 aa6b38c2 Diego Biurrun
   positioned at or before pts. XXX: the following code is quite
463 de6d9b64 Fabrice Bellard
   approximative */
464 7b3c1382 Michael Niedermayer
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
465 de6d9b64 Fabrice Bellard
{
466
    FFMContext *ffm = s->priv_data;
467 bc5c918e Diego Biurrun
    int64_t pos_min, pos_max, pos;
468 0c1a9eda Zdenek Kabelac
    int64_t pts_min, pts_max, pts;
469 de6d9b64 Fabrice Bellard
    double pos1;
470
471
#ifdef DEBUG_SEEK
472 8999c833 Baptiste Coudurier
    av_log(s, AV_LOG_DEBUG, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
473 de6d9b64 Fabrice Bellard
#endif
474
    /* find the position using linear interpolation (better than
475
       dichotomy in typical cases) */
476 92a0f338 Baptiste Coudurier
    pos_min = FFM_PACKET_SIZE;
477
    pos_max = ffm->file_size - FFM_PACKET_SIZE;
478 de6d9b64 Fabrice Bellard
    while (pos_min <= pos_max) {
479 fabb990e Baptiste Coudurier
        pts_min = get_dts(s, pos_min);
480
        pts_max = get_dts(s, pos_max);
481 de6d9b64 Fabrice Bellard
        /* linear interpolation */
482 4606ac8d Zdenek Kabelac
        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
483 de6d9b64 Fabrice Bellard
            (double)(pts_max - pts_min);
484 0c1a9eda Zdenek Kabelac
        pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
485 de6d9b64 Fabrice Bellard
        if (pos <= pos_min)
486
            pos = pos_min;
487
        else if (pos >= pos_max)
488
            pos = pos_max;
489 fabb990e Baptiste Coudurier
        pts = get_dts(s, pos);
490 de6d9b64 Fabrice Bellard
        /* check if we are lucky */
491
        if (pts == wanted_pts) {
492
            goto found;
493
        } else if (pts > wanted_pts) {
494
            pos_max = pos - FFM_PACKET_SIZE;
495
        } else {
496
            pos_min = pos + FFM_PACKET_SIZE;
497
        }
498
    }
499 7b3c1382 Michael Niedermayer
    pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
500 92a0f338 Baptiste Coudurier
501 de6d9b64 Fabrice Bellard
 found:
502
    ffm_seek1(s, pos);
503 c07d64c8 Baptiste Coudurier
504
    /* reset read state */
505
    ffm->read_state = READ_HEADER;
506
    ffm->packet_ptr = ffm->packet;
507
    ffm->packet_end = ffm->packet;
508
    ffm->first_packet = 1;
509
510 de6d9b64 Fabrice Bellard
    return 0;
511
}
512
513 07c4ed85 Philip Gladstone
static int ffm_probe(AVProbeData *p)
514
{
515 87e87886 Michael Niedermayer
    if (
516 115329f1 Diego Biurrun
        p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
517 66d2ff2a Fabrice Bellard
        p->buf[3] == '1')
518 07c4ed85 Philip Gladstone
        return AVPROBE_SCORE_MAX + 1;
519
    return 0;
520
}
521
522 c6610a21 Diego Elio Pettenò
AVInputFormat ff_ffm_demuxer = {
523 c9a65ca8 Fabrice Bellard
    "ffm",
524 af274fd1 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
525 66d2ff2a Fabrice Bellard
    sizeof(FFMContext),
526 07c4ed85 Philip Gladstone
    ffm_probe,
527 c9a65ca8 Fabrice Bellard
    ffm_read_header,
528
    ffm_read_packet,
529 2dc22a64 Vitor Sessak
    ffm_close,
530 c9a65ca8 Fabrice Bellard
    ffm_seek,
531
};