Statistics
| Branch: | Revision:

ffmpeg / libavformat / ffm.c @ 25f8db58

History | View | Annotate | Download (22.4 KB)

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