Statistics
| Branch: | Revision:

ffmpeg / libavformat / ffm.c @ bb54f6ab

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