Statistics
| Branch: | Revision:

ffmpeg / libavformat / ffm.c @ e19fbbec

History | View | Annotate | Download (22.7 KB)

1 de6d9b64 Fabrice Bellard
/*
2 7fbde343 Aurelien Jacobs
 * FFM (ffserver live feed) muxer and demuxer
3 66d2ff2a Fabrice Bellard
 * 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
#include "avformat.h"
22 8be1c656 Fabrice Bellard
#include <unistd.h>
23 de6d9b64 Fabrice Bellard
24
/* The FFM file is made of blocks of fixed size */
25
#define FFM_HEADER_SIZE 14
26
#define PACKET_ID       0x666d
27
28
/* each packet contains frames (which can span several packets */
29 20f01548 Philip Gladstone
#define FRAME_HEADER_SIZE    8
30 de6d9b64 Fabrice Bellard
#define FLAG_KEY_FRAME       0x01
31
32
typedef struct FFMStream {
33 0c1a9eda Zdenek Kabelac
    int64_t pts;
34 de6d9b64 Fabrice Bellard
} FFMStream;
35
36
enum {
37
    READ_HEADER,
38
    READ_DATA,
39
};
40
41
typedef struct FFMContext {
42
    /* only reading mode */
43
    offset_t write_index, file_size;
44 4606ac8d Zdenek Kabelac
    int read_state;
45 0c1a9eda Zdenek Kabelac
    uint8_t header[FRAME_HEADER_SIZE];
46 de6d9b64 Fabrice Bellard
47
    /* read and write */
48
    int first_packet; /* true if first packet, needed to set the discontinuity tag */
49 d271481f Michael Niedermayer
    int first_frame_in_packet; /* true if first frame in packet, needed to know if PTS information is valid */
50 de6d9b64 Fabrice Bellard
    int packet_size;
51
    int frame_offset;
52 0c1a9eda Zdenek Kabelac
    int64_t pts;
53
    uint8_t *packet_ptr, *packet_end;
54
    uint8_t packet[FFM_PACKET_SIZE];
55 de6d9b64 Fabrice Bellard
} FFMContext;
56
57 91628427 Philip Gladstone
static int64_t get_pts(AVFormatContext *s, offset_t pos);
58
59 c9a65ca8 Fabrice Bellard
/* disable pts hack for testing */
60
int ffm_nopts = 0;
61
62 a9e35095 Diego Biurrun
#ifdef CONFIG_MUXERS
63 de6d9b64 Fabrice Bellard
static void flush_packet(AVFormatContext *s)
64
{
65
    FFMContext *ffm = s->priv_data;
66
    int fill_size, h;
67
    ByteIOContext *pb = &s->pb;
68
69
    fill_size = ffm->packet_end - ffm->packet_ptr;
70
    memset(ffm->packet_ptr, 0, fill_size);
71 4606ac8d Zdenek Kabelac
72 115329f1 Diego Biurrun
    if (url_ftell(pb) % ffm->packet_size)
73 91628427 Philip Gladstone
        av_abort();
74
75 de6d9b64 Fabrice Bellard
    /* put header */
76 4606ac8d Zdenek Kabelac
    put_be16(pb, PACKET_ID);
77 de6d9b64 Fabrice Bellard
    put_be16(pb, fill_size);
78
    put_be64(pb, ffm->pts);
79
    h = ffm->frame_offset;
80
    if (ffm->first_packet)
81
        h |= 0x8000;
82
    put_be16(pb, h);
83
    put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
84 34f633df Philip Gladstone
    put_flush_packet(pb);
85 de6d9b64 Fabrice Bellard
86
    /* prepare next packet */
87
    ffm->frame_offset = 0; /* no key frame */
88
    ffm->pts = 0; /* no pts */
89
    ffm->packet_ptr = ffm->packet;
90
    ffm->first_packet = 0;
91
}
92
93
/* 'first' is true if first data of a frame */
94 4606ac8d Zdenek Kabelac
static void ffm_write_data(AVFormatContext *s,
95 49057904 Fabrice Bellard
                           const uint8_t *buf, int size,
96 0c1a9eda Zdenek Kabelac
                           int64_t pts, int first)
97 de6d9b64 Fabrice Bellard
{
98
    FFMContext *ffm = s->priv_data;
99
    int len;
100
101
    if (first && ffm->frame_offset == 0)
102
        ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
103
    if (first && ffm->pts == 0)
104
        ffm->pts = pts;
105
106
    /* write as many packets as needed */
107
    while (size > 0) {
108
        len = ffm->packet_end - ffm->packet_ptr;
109
        if (len > size)
110
            len = size;
111
        memcpy(ffm->packet_ptr, buf, len);
112 4606ac8d Zdenek Kabelac
113 de6d9b64 Fabrice Bellard
        ffm->packet_ptr += len;
114
        buf += len;
115
        size -= len;
116
        if (ffm->packet_ptr >= ffm->packet_end) {
117
            /* special case : no pts in packet : we leave the current one */
118
            if (ffm->pts == 0)
119
                ffm->pts = pts;
120
121
            flush_packet(s);
122
        }
123
    }
124
}
125
126
static int ffm_write_header(AVFormatContext *s)
127
{
128 c9a65ca8 Fabrice Bellard
    FFMContext *ffm = s->priv_data;
129 de6d9b64 Fabrice Bellard
    AVStream *st;
130
    FFMStream *fst;
131
    ByteIOContext *pb = &s->pb;
132
    AVCodecContext *codec;
133
    int bit_rate, i;
134
135
    ffm->packet_size = FFM_PACKET_SIZE;
136 4606ac8d Zdenek Kabelac
137 de6d9b64 Fabrice Bellard
    /* header */
138 26aa0f89 Alex Beregszaszi
    put_le32(pb, MKTAG('F', 'F', 'M', '1'));
139 de6d9b64 Fabrice Bellard
    put_be32(pb, ffm->packet_size);
140
    /* XXX: store write position in other file ? */
141
    put_be64(pb, ffm->packet_size); /* current write position */
142
143
    put_be32(pb, s->nb_streams);
144
    bit_rate = 0;
145
    for(i=0;i<s->nb_streams;i++) {
146
        st = s->streams[i];
147 01f4895c Michael Niedermayer
        bit_rate += st->codec->bit_rate;
148 de6d9b64 Fabrice Bellard
    }
149
    put_be32(pb, bit_rate);
150
151
    /* list of streams */
152
    for(i=0;i<s->nb_streams;i++) {
153
        st = s->streams[i];
154 16300e23 Philip Gladstone
        fst = av_mallocz(sizeof(FFMStream));
155 de6d9b64 Fabrice Bellard
        if (!fst)
156
            goto fail;
157 53e2f9ca Michael Niedermayer
        av_set_pts_info(st, 64, 1, 1000000);
158 de6d9b64 Fabrice Bellard
        st->priv_data = fst;
159
160 01f4895c Michael Niedermayer
        codec = st->codec;
161 de6d9b64 Fabrice Bellard
        /* generic info */
162
        put_be32(pb, codec->codec_id);
163
        put_byte(pb, codec->codec_type);
164
        put_be32(pb, codec->bit_rate);
165 bb270c08 Diego Biurrun
        put_be32(pb, st->quality);
166 ccac2e27 Philip Gladstone
        put_be32(pb, codec->flags);
167 22332126 Philip Gladstone
        put_be32(pb, codec->flags2);
168
        put_be32(pb, codec->debug);
169 de6d9b64 Fabrice Bellard
        /* specific info */
170
        switch(codec->codec_type) {
171
        case CODEC_TYPE_VIDEO:
172 c0df9d75 Michael Niedermayer
            put_be32(pb, codec->time_base.num);
173
            put_be32(pb, codec->time_base.den);
174 de6d9b64 Fabrice Bellard
            put_be16(pb, codec->width);
175
            put_be16(pb, codec->height);
176 20f01548 Philip Gladstone
            put_be16(pb, codec->gop_size);
177 22332126 Philip Gladstone
            put_be32(pb, codec->pix_fmt);
178 20f01548 Philip Gladstone
            put_byte(pb, codec->qmin);
179
            put_byte(pb, codec->qmax);
180
            put_byte(pb, codec->max_qdiff);
181
            put_be16(pb, (int) (codec->qcompress * 10000.0));
182
            put_be16(pb, (int) (codec->qblur * 10000.0));
183 3884a3c3 Philip Gladstone
            put_be32(pb, codec->bit_rate_tolerance);
184 3b4b29dc Fabrice Bellard
            put_strz(pb, codec->rc_eq);
185 75bdb984 Philip Gladstone
            put_be32(pb, codec->rc_max_rate);
186
            put_be32(pb, codec->rc_min_rate);
187
            put_be32(pb, codec->rc_buffer_size);
188 7cb8f314 Michael Niedermayer
            put_be64(pb, av_dbl2int(codec->i_quant_factor));
189
            put_be64(pb, av_dbl2int(codec->b_quant_factor));
190
            put_be64(pb, av_dbl2int(codec->i_quant_offset));
191
            put_be64(pb, av_dbl2int(codec->b_quant_offset));
192 75bdb984 Philip Gladstone
            put_be32(pb, codec->dct_algo);
193 22332126 Philip Gladstone
            put_be32(pb, codec->strict_std_compliance);
194
            put_be32(pb, codec->max_b_frames);
195
            put_be32(pb, codec->luma_elim_threshold);
196
            put_be32(pb, codec->chroma_elim_threshold);
197
            put_be32(pb, codec->mpeg_quant);
198
            put_be32(pb, codec->intra_dc_precision);
199
            put_be32(pb, codec->me_method);
200
            put_be32(pb, codec->mb_decision);
201
            put_be32(pb, codec->nsse_weight);
202
            put_be32(pb, codec->frame_skip_cmp);
203 7cb8f314 Michael Niedermayer
            put_be64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
204 038a1243 Alex Beregszaszi
            put_be32(pb, codec->codec_tag);
205 de6d9b64 Fabrice Bellard
            break;
206
        case CODEC_TYPE_AUDIO:
207
            put_be32(pb, codec->sample_rate);
208
            put_le16(pb, codec->channels);
209 20f01548 Philip Gladstone
            put_le16(pb, codec->frame_size);
210 de6d9b64 Fabrice Bellard
            break;
211 20f01548 Philip Gladstone
        default:
212 71c32f19 Michael Niedermayer
            return -1;
213 de6d9b64 Fabrice Bellard
        }
214
        /* hack to have real time */
215 c9a65ca8 Fabrice Bellard
        if (ffm_nopts)
216
            fst->pts = 0;
217
        else
218 a11bf0bd Fabrice Bellard
            fst->pts = av_gettime();
219 de6d9b64 Fabrice Bellard
    }
220
221
    /* flush until end of block reached */
222
    while ((url_ftell(pb) % ffm->packet_size) != 0)
223
        put_byte(pb, 0);
224
225
    put_flush_packet(pb);
226
227
    /* init packet mux */
228
    ffm->packet_ptr = ffm->packet;
229
    ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
230 71c32f19 Michael Niedermayer
    assert(ffm->packet_end >= ffm->packet);
231 de6d9b64 Fabrice Bellard
    ffm->frame_offset = 0;
232
    ffm->pts = 0;
233
    ffm->first_packet = 1;
234
235
    return 0;
236
 fail:
237
    for(i=0;i<s->nb_streams;i++) {
238
        st = s->streams[i];
239 789bee12 Philip Gladstone
        av_freep(&st->priv_data);
240 de6d9b64 Fabrice Bellard
    }
241
    return -1;
242
}
243
244 e928649b Michael Niedermayer
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
245 de6d9b64 Fabrice Bellard
{
246 e928649b Michael Niedermayer
    AVStream *st = s->streams[pkt->stream_index];
247 de6d9b64 Fabrice Bellard
    FFMStream *fst = st->priv_data;
248 0c1a9eda Zdenek Kabelac
    int64_t pts;
249
    uint8_t header[FRAME_HEADER_SIZE];
250 20f01548 Philip Gladstone
    int duration;
251 e928649b Michael Niedermayer
    int size= pkt->size;
252 20f01548 Philip Gladstone
253 e928649b Michael Niedermayer
    //XXX/FIXME use duration from pkt
254 01f4895c Michael Niedermayer
    if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
255
        duration = ((float)st->codec->frame_size / st->codec->sample_rate * 1000000.0);
256 20f01548 Philip Gladstone
    } else {
257 01f4895c Michael Niedermayer
        duration = (1000000.0 * st->codec->time_base.num / (float)st->codec->time_base.den);
258 20f01548 Philip Gladstone
    }
259 de6d9b64 Fabrice Bellard
260
    pts = fst->pts;
261
    /* packet size & key_frame */
262 e928649b Michael Niedermayer
    header[0] = pkt->stream_index;
263 de6d9b64 Fabrice Bellard
    header[1] = 0;
264 e928649b Michael Niedermayer
    if (pkt->flags & PKT_FLAG_KEY)
265 de6d9b64 Fabrice Bellard
        header[1] |= FLAG_KEY_FRAME;
266
    header[2] = (size >> 16) & 0xff;
267
    header[3] = (size >> 8) & 0xff;
268
    header[4] = size & 0xff;
269 20f01548 Philip Gladstone
    header[5] = (duration >> 16) & 0xff;
270
    header[6] = (duration >> 8) & 0xff;
271
    header[7] = duration & 0xff;
272 de6d9b64 Fabrice Bellard
    ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
273 e928649b Michael Niedermayer
    ffm_write_data(s, pkt->data, size, pts, 0);
274 de6d9b64 Fabrice Bellard
275 20f01548 Philip Gladstone
    fst->pts += duration;
276 de6d9b64 Fabrice Bellard
    return 0;
277
}
278
279
static int ffm_write_trailer(AVFormatContext *s)
280
{
281
    ByteIOContext *pb = &s->pb;
282
    FFMContext *ffm = s->priv_data;
283
284
    /* flush packets */
285
    if (ffm->packet_ptr > ffm->packet)
286
        flush_packet(s);
287
288
    put_flush_packet(pb);
289
290 66d2ff2a Fabrice Bellard
    if (!url_is_streamed(pb)) {
291 0c1a9eda Zdenek Kabelac
        int64_t size;
292 66d2ff2a Fabrice Bellard
        /* update the write offset */
293
        size = url_ftell(pb);
294
        url_fseek(pb, 8, SEEK_SET);
295
        put_be64(pb, size);
296
        put_flush_packet(pb);
297
    }
298
299 de6d9b64 Fabrice Bellard
    return 0;
300
}
301 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
302 de6d9b64 Fabrice Bellard
303
/* ffm demux */
304
305
static int ffm_is_avail_data(AVFormatContext *s, int size)
306
{
307
    FFMContext *ffm = s->priv_data;
308
    offset_t pos, avail_size;
309
    int len;
310
311
    len = ffm->packet_end - ffm->packet_ptr;
312 66d2ff2a Fabrice Bellard
    if (!ffm_nopts) {
313
        /* XXX: I don't understand this test, so I disabled it for testing */
314
        if (size <= len)
315
            return 1;
316
    }
317 de6d9b64 Fabrice Bellard
    pos = url_ftell(&s->pb);
318
    if (pos == ffm->write_index) {
319
        /* exactly at the end of stream */
320
        return 0;
321
    } else if (pos < ffm->write_index) {
322
        avail_size = ffm->write_index - pos;
323
    } else {
324
        avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
325
    }
326
    avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
327
    if (size <= avail_size)
328
        return 1;
329
    else
330
        return 0;
331
}
332
333
/* first is true if we read the frame header */
334 4606ac8d Zdenek Kabelac
static int ffm_read_data(AVFormatContext *s,
335 0c1a9eda Zdenek Kabelac
                         uint8_t *buf, int size, int first)
336 de6d9b64 Fabrice Bellard
{
337
    FFMContext *ffm = s->priv_data;
338
    ByteIOContext *pb = &s->pb;
339
    int len, fill_size, size1, frame_offset;
340
341
    size1 = size;
342
    while (size > 0) {
343
    redo:
344
        len = ffm->packet_end - ffm->packet_ptr;
345
        if (len > size)
346
            len = size;
347
        if (len == 0) {
348
            if (url_ftell(pb) == ffm->file_size)
349
                url_fseek(pb, ffm->packet_size, SEEK_SET);
350 5e57424d Philip Gladstone
    retry_read:
351 de6d9b64 Fabrice Bellard
            get_be16(pb); /* PACKET_ID */
352
            fill_size = get_be16(pb);
353
            ffm->pts = get_be64(pb);
354 d271481f Michael Niedermayer
            ffm->first_frame_in_packet = 1;
355 de6d9b64 Fabrice Bellard
            frame_offset = get_be16(pb);
356
            get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
357
            ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
358 91628427 Philip Gladstone
            if (ffm->packet_end < ffm->packet)
359 71c32f19 Michael Niedermayer
                return -1;
360 de6d9b64 Fabrice Bellard
            /* if first packet or resynchronization packet, we must
361
               handle it specifically */
362
            if (ffm->first_packet || (frame_offset & 0x8000)) {
363 5e57424d Philip Gladstone
                if (!frame_offset) {
364
                    /* This packet has no frame headers in it */
365
                    if (url_ftell(pb) >= ffm->packet_size * 3) {
366
                        url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
367
                        goto retry_read;
368
                    }
369
                    /* This is bad, we cannot find a valid frame header */
370
                    return 0;
371
                }
372 de6d9b64 Fabrice Bellard
                ffm->first_packet = 0;
373 5e57424d Philip Gladstone
                if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
374 71c32f19 Michael Niedermayer
                    return -1;
375 de6d9b64 Fabrice Bellard
                ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
376
                if (!first)
377
                    break;
378
            } else {
379
                ffm->packet_ptr = ffm->packet;
380
            }
381
            goto redo;
382
        }
383
        memcpy(buf, ffm->packet_ptr, len);
384
        buf += len;
385
        ffm->packet_ptr += len;
386
        size -= len;
387
        first = 0;
388
    }
389
    return size1 - size;
390
}
391
392
393 91628427 Philip Gladstone
static void adjust_write_index(AVFormatContext *s)
394
{
395
    FFMContext *ffm = s->priv_data;
396
    ByteIOContext *pb = &s->pb;
397
    int64_t pts;
398
    //offset_t orig_write_index = ffm->write_index;
399
    offset_t pos_min, pos_max;
400
    int64_t pts_start;
401
    offset_t ptr = url_ftell(pb);
402
403
404
    pos_min = 0;
405
    pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
406
407
    pts_start = get_pts(s, pos_min);
408
409
    pts = get_pts(s, pos_max);
410
411 115329f1 Diego Biurrun
    if (pts - 100000 > pts_start)
412 79292510 Michael Niedermayer
        goto end;
413 91628427 Philip Gladstone
414
    ffm->write_index = FFM_PACKET_SIZE;
415
416
    pts_start = get_pts(s, pos_min);
417
418
    pts = get_pts(s, pos_max);
419
420
    if (pts - 100000 <= pts_start) {
421
        while (1) {
422
            offset_t newpos;
423
            int64_t newpts;
424
425
            newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
426
427
            if (newpos == pos_min)
428
                break;
429
430
            newpts = get_pts(s, newpos);
431
432
            if (newpts - 100000 <= pts) {
433
                pos_max = newpos;
434
                pts = newpts;
435
            } else {
436
                pos_min = newpos;
437
            }
438
        }
439
        ffm->write_index += pos_max;
440
    }
441
442 949b1a13 Steve L'Homme
    //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
443 91628427 Philip Gladstone
    //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
444
445 79292510 Michael Niedermayer
 end:
446 91628427 Philip Gladstone
    url_fseek(pb, ptr, SEEK_SET);
447
}
448
449
450 de6d9b64 Fabrice Bellard
static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
451
{
452 c9a65ca8 Fabrice Bellard
    FFMContext *ffm = s->priv_data;
453 de6d9b64 Fabrice Bellard
    AVStream *st;
454
    FFMStream *fst;
455
    ByteIOContext *pb = &s->pb;
456
    AVCodecContext *codec;
457 fa26a29d Fabrice Bellard
    int i, nb_streams;
458 0c1a9eda Zdenek Kabelac
    uint32_t tag;
459 de6d9b64 Fabrice Bellard
460
    /* header */
461
    tag = get_le32(pb);
462
    if (tag != MKTAG('F', 'F', 'M', '1'))
463
        goto fail;
464
    ffm->packet_size = get_be32(pb);
465
    if (ffm->packet_size != FFM_PACKET_SIZE)
466
        goto fail;
467
    ffm->write_index = get_be64(pb);
468
    /* get also filesize */
469
    if (!url_is_streamed(pb)) {
470 a965c478 Aurelien Jacobs
        ffm->file_size = url_fsize(pb);
471 91628427 Philip Gladstone
        adjust_write_index(s);
472 de6d9b64 Fabrice Bellard
    } else {
473 8da9266c Måns Rullgård
        ffm->file_size = (UINT64_C(1) << 63) - 1;
474 de6d9b64 Fabrice Bellard
    }
475
476 fa26a29d Fabrice Bellard
    nb_streams = get_be32(pb);
477 de6d9b64 Fabrice Bellard
    get_be32(pb); /* total bitrate */
478
    /* read each stream */
479 fa26a29d Fabrice Bellard
    for(i=0;i<nb_streams;i++) {
480 75bdb984 Philip Gladstone
        char rc_eq_buf[128];
481
482 fa26a29d Fabrice Bellard
        st = av_new_stream(s, 0);
483 de6d9b64 Fabrice Bellard
        if (!st)
484
            goto fail;
485 16300e23 Philip Gladstone
        fst = av_mallocz(sizeof(FFMStream));
486 de6d9b64 Fabrice Bellard
        if (!fst)
487
            goto fail;
488 26aa0f89 Alex Beregszaszi
        s->streams[i] = st;
489 115329f1 Diego Biurrun
490 e928649b Michael Niedermayer
        av_set_pts_info(st, 64, 1, 1000000);
491 115329f1 Diego Biurrun
492 de6d9b64 Fabrice Bellard
        st->priv_data = fst;
493
494 01f4895c Michael Niedermayer
        codec = st->codec;
495 de6d9b64 Fabrice Bellard
        /* generic info */
496 26aa0f89 Alex Beregszaszi
        codec->codec_id = get_be32(pb);
497
        codec->codec_type = get_byte(pb); /* codec_type */
498 de6d9b64 Fabrice Bellard
        codec->bit_rate = get_be32(pb);
499 26aa0f89 Alex Beregszaszi
        st->quality = get_be32(pb);
500 ccac2e27 Philip Gladstone
        codec->flags = get_be32(pb);
501 22332126 Philip Gladstone
        codec->flags2 = get_be32(pb);
502
        codec->debug = get_be32(pb);
503 de6d9b64 Fabrice Bellard
        /* specific info */
504
        switch(codec->codec_type) {
505
        case CODEC_TYPE_VIDEO:
506 c0df9d75 Michael Niedermayer
            codec->time_base.num = get_be32(pb);
507
            codec->time_base.den = get_be32(pb);
508 de6d9b64 Fabrice Bellard
            codec->width = get_be16(pb);
509
            codec->height = get_be16(pb);
510 20f01548 Philip Gladstone
            codec->gop_size = get_be16(pb);
511 22332126 Philip Gladstone
            codec->pix_fmt = get_be32(pb);
512 20f01548 Philip Gladstone
            codec->qmin = get_byte(pb);
513
            codec->qmax = get_byte(pb);
514
            codec->max_qdiff = get_byte(pb);
515
            codec->qcompress = get_be16(pb) / 10000.0;
516
            codec->qblur = get_be16(pb) / 10000.0;
517 3884a3c3 Philip Gladstone
            codec->bit_rate_tolerance = get_be32(pb);
518 e9a9e0c2 Fabrice Bellard
            codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
519 75bdb984 Philip Gladstone
            codec->rc_max_rate = get_be32(pb);
520
            codec->rc_min_rate = get_be32(pb);
521
            codec->rc_buffer_size = get_be32(pb);
522 7cb8f314 Michael Niedermayer
            codec->i_quant_factor = av_int2dbl(get_be64(pb));
523
            codec->b_quant_factor = av_int2dbl(get_be64(pb));
524
            codec->i_quant_offset = av_int2dbl(get_be64(pb));
525
            codec->b_quant_offset = av_int2dbl(get_be64(pb));
526 75bdb984 Philip Gladstone
            codec->dct_algo = get_be32(pb);
527 22332126 Philip Gladstone
            codec->strict_std_compliance = get_be32(pb);
528
            codec->max_b_frames = get_be32(pb);
529
            codec->luma_elim_threshold = get_be32(pb);
530
            codec->chroma_elim_threshold = get_be32(pb);
531
            codec->mpeg_quant = get_be32(pb);
532
            codec->intra_dc_precision = get_be32(pb);
533
            codec->me_method = get_be32(pb);
534
            codec->mb_decision = get_be32(pb);
535
            codec->nsse_weight = get_be32(pb);
536
            codec->frame_skip_cmp = get_be32(pb);
537 7cb8f314 Michael Niedermayer
            codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
538 038a1243 Alex Beregszaszi
            codec->codec_tag = get_be32(pb);
539 de6d9b64 Fabrice Bellard
            break;
540
        case CODEC_TYPE_AUDIO:
541
            codec->sample_rate = get_be32(pb);
542
            codec->channels = get_le16(pb);
543 20f01548 Philip Gladstone
            codec->frame_size = get_le16(pb);
544 de6d9b64 Fabrice Bellard
            break;
545 20f01548 Philip Gladstone
        default:
546 75bdb984 Philip Gladstone
            goto fail;
547 de6d9b64 Fabrice Bellard
        }
548
549
    }
550
551
    /* get until end of block reached */
552
    while ((url_ftell(pb) % ffm->packet_size) != 0)
553
        get_byte(pb);
554
555
    /* init packet demux */
556
    ffm->packet_ptr = ffm->packet;
557
    ffm->packet_end = ffm->packet;
558
    ffm->frame_offset = 0;
559
    ffm->pts = 0;
560
    ffm->read_state = READ_HEADER;
561
    ffm->first_packet = 1;
562
    return 0;
563
 fail:
564
    for(i=0;i<s->nb_streams;i++) {
565
        st = s->streams[i];
566
        if (st) {
567 789bee12 Philip Gladstone
            av_freep(&st->priv_data);
568 1ea4f593 Fabrice Bellard
            av_free(st);
569 de6d9b64 Fabrice Bellard
        }
570
    }
571
    return -1;
572
}
573
574
/* return < 0 if eof */
575
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
576
{
577
    int size;
578
    FFMContext *ffm = s->priv_data;
579 20f01548 Philip Gladstone
    int duration;
580 de6d9b64 Fabrice Bellard
581
    switch(ffm->read_state) {
582
    case READ_HEADER:
583 66d2ff2a Fabrice Bellard
        if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
584 8fa36ae0 François Revol
            return AVERROR(EAGAIN);
585 66d2ff2a Fabrice Bellard
        }
586 de6d9b64 Fabrice Bellard
#if 0
587 949b1a13 Steve L'Homme
        printf("pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
588 de6d9b64 Fabrice Bellard
               url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
589
#endif
590 115329f1 Diego Biurrun
        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
591 66d2ff2a Fabrice Bellard
            FRAME_HEADER_SIZE)
592 8fa36ae0 François Revol
            return AVERROR(EAGAIN);
593 de6d9b64 Fabrice Bellard
#if 0
594
        {
595
            int i;
596
            for(i=0;i<FRAME_HEADER_SIZE;i++)
597
                printf("%02x ", ffm->header[i]);
598
            printf("\n");
599
        }
600
#endif
601
        ffm->read_state = READ_DATA;
602
        /* fall thru */
603
    case READ_DATA:
604
        size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
605
        if (!ffm_is_avail_data(s, size)) {
606 8fa36ae0 François Revol
            return AVERROR(EAGAIN);
607 de6d9b64 Fabrice Bellard
        }
608
609 20f01548 Philip Gladstone
        duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
610
611 de6d9b64 Fabrice Bellard
        av_new_packet(pkt, size);
612
        pkt->stream_index = ffm->header[0];
613 115329f1 Diego Biurrun
        pkt->pos = url_ftell(&s->pb);
614 de6d9b64 Fabrice Bellard
        if (ffm->header[1] & FLAG_KEY_FRAME)
615
            pkt->flags |= PKT_FLAG_KEY;
616 4606ac8d Zdenek Kabelac
617 de6d9b64 Fabrice Bellard
        ffm->read_state = READ_HEADER;
618
        if (ffm_read_data(s, pkt->data, size, 0) != size) {
619
            /* bad case: desynchronized packet. we cancel all the packet loading */
620
            av_free_packet(pkt);
621 8fa36ae0 François Revol
            return AVERROR(EAGAIN);
622 de6d9b64 Fabrice Bellard
        }
623 d271481f Michael Niedermayer
        if (ffm->first_frame_in_packet)
624
        {
625
            pkt->pts = ffm->pts;
626
            ffm->first_frame_in_packet = 0;
627
        }
628 20f01548 Philip Gladstone
        pkt->duration = duration;
629 de6d9b64 Fabrice Bellard
        break;
630
    }
631
    return 0;
632
}
633
634
//#define DEBUG_SEEK
635
636
/* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
637
   by the write position inside this function */
638
static void ffm_seek1(AVFormatContext *s, offset_t pos1)
639
{
640
    FFMContext *ffm = s->priv_data;
641
    ByteIOContext *pb = &s->pb;
642
    offset_t pos;
643
644
    pos = pos1 + ffm->write_index;
645
    if (pos >= ffm->file_size)
646
        pos -= (ffm->file_size - FFM_PACKET_SIZE);
647
#ifdef DEBUG_SEEK
648 949b1a13 Steve L'Homme
    printf("seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
649 de6d9b64 Fabrice Bellard
#endif
650
    url_fseek(pb, pos, SEEK_SET);
651
}
652
653 0c1a9eda Zdenek Kabelac
static int64_t get_pts(AVFormatContext *s, offset_t pos)
654 de6d9b64 Fabrice Bellard
{
655
    ByteIOContext *pb = &s->pb;
656 0c1a9eda Zdenek Kabelac
    int64_t pts;
657 de6d9b64 Fabrice Bellard
658 4606ac8d Zdenek Kabelac
    ffm_seek1(s, pos);
659 de6d9b64 Fabrice Bellard
    url_fskip(pb, 4);
660
    pts = get_be64(pb);
661
#ifdef DEBUG_SEEK
662
    printf("pts=%0.6f\n", pts / 1000000.0);
663
#endif
664
    return pts;
665
}
666
667
/* seek to a given time in the file. The file read pointer is
668
   positionned at or before pts. XXX: the following code is quite
669
   approximative */
670 7b3c1382 Michael Niedermayer
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
671 de6d9b64 Fabrice Bellard
{
672
    FFMContext *ffm = s->priv_data;
673
    offset_t pos_min, pos_max, pos;
674 0c1a9eda Zdenek Kabelac
    int64_t pts_min, pts_max, pts;
675 de6d9b64 Fabrice Bellard
    double pos1;
676
677
#ifdef DEBUG_SEEK
678
    printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
679
#endif
680
    /* find the position using linear interpolation (better than
681
       dichotomy in typical cases) */
682
    pos_min = 0;
683
    pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
684
    while (pos_min <= pos_max) {
685
        pts_min = get_pts(s, pos_min);
686
        pts_max = get_pts(s, pos_max);
687
        /* linear interpolation */
688 4606ac8d Zdenek Kabelac
        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
689 de6d9b64 Fabrice Bellard
            (double)(pts_max - pts_min);
690 0c1a9eda Zdenek Kabelac
        pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
691 de6d9b64 Fabrice Bellard
        if (pos <= pos_min)
692
            pos = pos_min;
693
        else if (pos >= pos_max)
694
            pos = pos_max;
695
        pts = get_pts(s, pos);
696
        /* check if we are lucky */
697
        if (pts == wanted_pts) {
698
            goto found;
699
        } else if (pts > wanted_pts) {
700
            pos_max = pos - FFM_PACKET_SIZE;
701
        } else {
702
            pos_min = pos + FFM_PACKET_SIZE;
703
        }
704
    }
705 7b3c1382 Michael Niedermayer
    pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
706 de6d9b64 Fabrice Bellard
    if (pos > 0)
707
        pos -= FFM_PACKET_SIZE;
708
 found:
709
    ffm_seek1(s, pos);
710
    return 0;
711
}
712
713 04f46ced Gildas Bazin
#ifdef CONFIG_FFSERVER
714 de6d9b64 Fabrice Bellard
offset_t ffm_read_write_index(int fd)
715
{
716 0c1a9eda Zdenek Kabelac
    uint8_t buf[8];
717 de6d9b64 Fabrice Bellard
    offset_t pos;
718
    int i;
719
720
    lseek(fd, 8, SEEK_SET);
721
    read(fd, buf, 8);
722
    pos = 0;
723 4606ac8d Zdenek Kabelac
    for(i=0;i<8;i++)
724 fa26a29d Fabrice Bellard
        pos |= (int64_t)buf[i] << (56 - i * 8);
725 de6d9b64 Fabrice Bellard
    return pos;
726
}
727
728
void ffm_write_write_index(int fd, offset_t pos)
729
{
730 0c1a9eda Zdenek Kabelac
    uint8_t buf[8];
731 de6d9b64 Fabrice Bellard
    int i;
732
733 4606ac8d Zdenek Kabelac
    for(i=0;i<8;i++)
734 de6d9b64 Fabrice Bellard
        buf[i] = (pos >> (56 - i * 8)) & 0xff;
735
    lseek(fd, 8, SEEK_SET);
736
    write(fd, buf, 8);
737
}
738
739
void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
740
{
741
    FFMContext *ffm = s->priv_data;
742
    ffm->write_index = pos;
743
    ffm->file_size = file_size;
744
}
745 04f46ced Gildas Bazin
#endif // CONFIG_FFSERVER
746 de6d9b64 Fabrice Bellard
747
static int ffm_read_close(AVFormatContext *s)
748
{
749
    AVStream *st;
750
    int i;
751
752
    for(i=0;i<s->nb_streams;i++) {
753
        st = s->streams[i];
754 789bee12 Philip Gladstone
        av_freep(&st->priv_data);
755 de6d9b64 Fabrice Bellard
    }
756
    return 0;
757
}
758
759 07c4ed85 Philip Gladstone
static int ffm_probe(AVProbeData *p)
760
{
761 66d2ff2a Fabrice Bellard
    if (p->buf_size >= 4 &&
762 115329f1 Diego Biurrun
        p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
763 66d2ff2a Fabrice Bellard
        p->buf[3] == '1')
764 07c4ed85 Philip Gladstone
        return AVPROBE_SCORE_MAX + 1;
765
    return 0;
766
}
767
768 ff70e601 Måns Rullgård
#ifdef CONFIG_FFM_DEMUXER
769
AVInputFormat ffm_demuxer = {
770 c9a65ca8 Fabrice Bellard
    "ffm",
771
    "ffm format",
772 66d2ff2a Fabrice Bellard
    sizeof(FFMContext),
773 07c4ed85 Philip Gladstone
    ffm_probe,
774 c9a65ca8 Fabrice Bellard
    ffm_read_header,
775
    ffm_read_packet,
776
    ffm_read_close,
777
    ffm_seek,
778
};
779 ff70e601 Måns Rullgård
#endif
780
#ifdef CONFIG_FFM_MUXER
781
AVOutputFormat ffm_muxer = {
782 de6d9b64 Fabrice Bellard
    "ffm",
783
    "ffm format",
784
    "",
785
    "ffm",
786 66d2ff2a Fabrice Bellard
    sizeof(FFMContext),
787 de6d9b64 Fabrice Bellard
    /* not really used */
788
    CODEC_ID_MP2,
789
    CODEC_ID_MPEG1VIDEO,
790
    ffm_write_header,
791
    ffm_write_packet,
792
    ffm_write_trailer,
793
};
794 ff70e601 Måns Rullgård
#endif //CONFIG_FFM_MUXER