Revision 85a6b01d

View differences:

libavformat/Makefile
44 44
OBJS-$(CONFIG_DXA_DEMUXER)               += dxa.o riff.o
45 45
OBJS-$(CONFIG_EA_CDATA_DEMUXER)          += eacdata.o
46 46
OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
47
OBJS-$(CONFIG_FFM_DEMUXER)               += ffm.o
48
OBJS-$(CONFIG_FFM_MUXER)                 += ffm.o
47
OBJS-$(CONFIG_FFM_DEMUXER)               += ffmdec.o
48
OBJS-$(CONFIG_FFM_MUXER)                 += ffmenc.o
49 49
OBJS-$(CONFIG_FLAC_DEMUXER)              += raw.o
50 50
OBJS-$(CONFIG_FLAC_MUXER)                += raw.o
51 51
OBJS-$(CONFIG_FLIC_DEMUXER)              += flic.o
libavformat/ffm.c
1
/*
2
 * FFM (ffserver live feed) muxer and demuxer
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22
#include <unistd.h>
23

  
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
#define FRAME_HEADER_SIZE    8
30
#define FLAG_KEY_FRAME       0x01
31

  
32
typedef struct FFMStream {
33
    int64_t pts;
34
} 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
    int read_state;
45
    uint8_t header[FRAME_HEADER_SIZE];
46

  
47
    /* read and write */
48
    int first_packet; /* true if first packet, needed to set the discontinuity tag */
49
    int first_frame_in_packet; /* true if first frame in packet, needed to know if PTS information is valid */
50
    int packet_size;
51
    int frame_offset;
52
    int64_t pts;
53
    uint8_t *packet_ptr, *packet_end;
54
    uint8_t packet[FFM_PACKET_SIZE];
55
} FFMContext;
56

  
57
static int64_t get_pts(AVFormatContext *s, offset_t pos);
58

  
59
/* disable pts hack for testing */
60
int ffm_nopts = 0;
61

  
62
#ifdef CONFIG_MUXERS
63
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

  
72
    if (url_ftell(pb) % ffm->packet_size)
73
        av_abort();
74

  
75
    /* put header */
76
    put_be16(pb, PACKET_ID);
77
    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
    put_flush_packet(pb);
85

  
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
static void ffm_write_data(AVFormatContext *s,
95
                           const uint8_t *buf, int size,
96
                           int64_t pts, int first)
97
{
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

  
113
        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
    FFMContext *ffm = s->priv_data;
129
    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

  
137
    /* header */
138
    put_le32(pb, MKTAG('F', 'F', 'M', '1'));
139
    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
        bit_rate += st->codec->bit_rate;
148
    }
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
        fst = av_mallocz(sizeof(FFMStream));
155
        if (!fst)
156
            goto fail;
157
        av_set_pts_info(st, 64, 1, 1000000);
158
        st->priv_data = fst;
159

  
160
        codec = st->codec;
161
        /* generic info */
162
        put_be32(pb, codec->codec_id);
163
        put_byte(pb, codec->codec_type);
164
        put_be32(pb, codec->bit_rate);
165
        put_be32(pb, st->quality);
166
        put_be32(pb, codec->flags);
167
        put_be32(pb, codec->flags2);
168
        put_be32(pb, codec->debug);
169
        /* specific info */
170
        switch(codec->codec_type) {
171
        case CODEC_TYPE_VIDEO:
172
            put_be32(pb, codec->time_base.num);
173
            put_be32(pb, codec->time_base.den);
174
            put_be16(pb, codec->width);
175
            put_be16(pb, codec->height);
176
            put_be16(pb, codec->gop_size);
177
            put_be32(pb, codec->pix_fmt);
178
            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
            put_be32(pb, codec->bit_rate_tolerance);
184
            put_strz(pb, codec->rc_eq);
185
            put_be32(pb, codec->rc_max_rate);
186
            put_be32(pb, codec->rc_min_rate);
187
            put_be32(pb, codec->rc_buffer_size);
188
            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
            put_be32(pb, codec->dct_algo);
193
            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
            put_be64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
204
            put_be32(pb, codec->codec_tag);
205
            break;
206
        case CODEC_TYPE_AUDIO:
207
            put_be32(pb, codec->sample_rate);
208
            put_le16(pb, codec->channels);
209
            put_le16(pb, codec->frame_size);
210
            break;
211
        default:
212
            return -1;
213
        }
214
        /* hack to have real time */
215
        if (ffm_nopts)
216
            fst->pts = 0;
217
        else
218
            fst->pts = av_gettime();
219
    }
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
    assert(ffm->packet_end >= ffm->packet);
231
    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
        av_freep(&st->priv_data);
240
    }
241
    return -1;
242
}
243

  
244
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
245
{
246
    AVStream *st = s->streams[pkt->stream_index];
247
    FFMStream *fst = st->priv_data;
248
    int64_t pts;
249
    uint8_t header[FRAME_HEADER_SIZE];
250

  
251
    pts = fst->pts;
252
    /* packet size & key_frame */
253
    header[0] = pkt->stream_index;
254
    header[1] = 0;
255
    if (pkt->flags & PKT_FLAG_KEY)
256
        header[1] |= FLAG_KEY_FRAME;
257
    AV_WB24(header+2, pkt->size);
258
    AV_WB24(header+5, pkt->duration);
259
    ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
260
    ffm_write_data(s, pkt->data, pkt->size, pts, 0);
261

  
262
    fst->pts += pkt->duration;
263
    return 0;
264
}
265

  
266
static int ffm_write_trailer(AVFormatContext *s)
267
{
268
    ByteIOContext *pb = s->pb;
269
    FFMContext *ffm = s->priv_data;
270

  
271
    /* flush packets */
272
    if (ffm->packet_ptr > ffm->packet)
273
        flush_packet(s);
274

  
275
    put_flush_packet(pb);
276

  
277
    if (!url_is_streamed(pb)) {
278
        int64_t size;
279
        /* update the write offset */
280
        size = url_ftell(pb);
281
        url_fseek(pb, 8, SEEK_SET);
282
        put_be64(pb, size);
283
        put_flush_packet(pb);
284
    }
285

  
286
    return 0;
287
}
288
#endif //CONFIG_MUXERS
289

  
290
/* ffm demux */
291

  
292
static int ffm_is_avail_data(AVFormatContext *s, int size)
293
{
294
    FFMContext *ffm = s->priv_data;
295
    offset_t pos, avail_size;
296
    int len;
297

  
298
    len = ffm->packet_end - ffm->packet_ptr;
299
    if (size <= len)
300
        return 1;
301
    pos = url_ftell(s->pb);
302
    if (pos == ffm->write_index) {
303
        /* exactly at the end of stream */
304
        return 0;
305
    } else if (pos < ffm->write_index) {
306
        avail_size = ffm->write_index - pos;
307
    } else {
308
        avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
309
    }
310
    avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
311
    if (size <= avail_size)
312
        return 1;
313
    else
314
        return 0;
315
}
316

  
317
/* first is true if we read the frame header */
318
static int ffm_read_data(AVFormatContext *s,
319
                         uint8_t *buf, int size, int first)
320
{
321
    FFMContext *ffm = s->priv_data;
322
    ByteIOContext *pb = s->pb;
323
    int len, fill_size, size1, frame_offset;
324

  
325
    size1 = size;
326
    while (size > 0) {
327
    redo:
328
        len = ffm->packet_end - ffm->packet_ptr;
329
        if (len > size)
330
            len = size;
331
        if (len == 0) {
332
            if (url_ftell(pb) == ffm->file_size)
333
                url_fseek(pb, ffm->packet_size, SEEK_SET);
334
    retry_read:
335
            get_be16(pb); /* PACKET_ID */
336
            fill_size = get_be16(pb);
337
            ffm->pts = get_be64(pb);
338
            ffm->first_frame_in_packet = 1;
339
            frame_offset = get_be16(pb);
340
            get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
341
            ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
342
            if (ffm->packet_end < ffm->packet)
343
                return -1;
344
            /* if first packet or resynchronization packet, we must
345
               handle it specifically */
346
            if (ffm->first_packet || (frame_offset & 0x8000)) {
347
                if (!frame_offset) {
348
                    /* This packet has no frame headers in it */
349
                    if (url_ftell(pb) >= ffm->packet_size * 3) {
350
                        url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
351
                        goto retry_read;
352
                    }
353
                    /* This is bad, we cannot find a valid frame header */
354
                    return 0;
355
                }
356
                ffm->first_packet = 0;
357
                if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
358
                    return -1;
359
                ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
360
                if (!first)
361
                    break;
362
            } else {
363
                ffm->packet_ptr = ffm->packet;
364
            }
365
            goto redo;
366
        }
367
        memcpy(buf, ffm->packet_ptr, len);
368
        buf += len;
369
        ffm->packet_ptr += len;
370
        size -= len;
371
        first = 0;
372
    }
373
    return size1 - size;
374
}
375

  
376

  
377
static void adjust_write_index(AVFormatContext *s)
378
{
379
    FFMContext *ffm = s->priv_data;
380
    ByteIOContext *pb = s->pb;
381
    int64_t pts;
382
    //offset_t orig_write_index = ffm->write_index;
383
    offset_t pos_min, pos_max;
384
    int64_t pts_start;
385
    offset_t ptr = url_ftell(pb);
386

  
387

  
388
    pos_min = 0;
389
    pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
390

  
391
    pts_start = get_pts(s, pos_min);
392

  
393
    pts = get_pts(s, pos_max);
394

  
395
    if (pts - 100000 > pts_start)
396
        goto end;
397

  
398
    ffm->write_index = FFM_PACKET_SIZE;
399

  
400
    pts_start = get_pts(s, pos_min);
401

  
402
    pts = get_pts(s, pos_max);
403

  
404
    if (pts - 100000 <= pts_start) {
405
        while (1) {
406
            offset_t newpos;
407
            int64_t newpts;
408

  
409
            newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
410

  
411
            if (newpos == pos_min)
412
                break;
413

  
414
            newpts = get_pts(s, newpos);
415

  
416
            if (newpts - 100000 <= pts) {
417
                pos_max = newpos;
418
                pts = newpts;
419
            } else {
420
                pos_min = newpos;
421
            }
422
        }
423
        ffm->write_index += pos_max;
424
    }
425

  
426
    //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
427
    //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
428

  
429
 end:
430
    url_fseek(pb, ptr, SEEK_SET);
431
}
432

  
433

  
434
static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
435
{
436
    FFMContext *ffm = s->priv_data;
437
    AVStream *st;
438
    FFMStream *fst;
439
    ByteIOContext *pb = s->pb;
440
    AVCodecContext *codec;
441
    int i, nb_streams;
442
    uint32_t tag;
443

  
444
    /* header */
445
    tag = get_le32(pb);
446
    if (tag != MKTAG('F', 'F', 'M', '1'))
447
        goto fail;
448
    ffm->packet_size = get_be32(pb);
449
    if (ffm->packet_size != FFM_PACKET_SIZE)
450
        goto fail;
451
    ffm->write_index = get_be64(pb);
452
    /* get also filesize */
453
    if (!url_is_streamed(pb)) {
454
        ffm->file_size = url_fsize(pb);
455
        adjust_write_index(s);
456
    } else {
457
        ffm->file_size = (UINT64_C(1) << 63) - 1;
458
    }
459

  
460
    nb_streams = get_be32(pb);
461
    get_be32(pb); /* total bitrate */
462
    /* read each stream */
463
    for(i=0;i<nb_streams;i++) {
464
        char rc_eq_buf[128];
465

  
466
        st = av_new_stream(s, 0);
467
        if (!st)
468
            goto fail;
469
        fst = av_mallocz(sizeof(FFMStream));
470
        if (!fst)
471
            goto fail;
472
        s->streams[i] = st;
473

  
474
        av_set_pts_info(st, 64, 1, 1000000);
475

  
476
        st->priv_data = fst;
477

  
478
        codec = st->codec;
479
        /* generic info */
480
        codec->codec_id = get_be32(pb);
481
        codec->codec_type = get_byte(pb); /* codec_type */
482
        codec->bit_rate = get_be32(pb);
483
        st->quality = get_be32(pb);
484
        codec->flags = get_be32(pb);
485
        codec->flags2 = get_be32(pb);
486
        codec->debug = get_be32(pb);
487
        /* specific info */
488
        switch(codec->codec_type) {
489
        case CODEC_TYPE_VIDEO:
490
            codec->time_base.num = get_be32(pb);
491
            codec->time_base.den = get_be32(pb);
492
            codec->width = get_be16(pb);
493
            codec->height = get_be16(pb);
494
            codec->gop_size = get_be16(pb);
495
            codec->pix_fmt = get_be32(pb);
496
            codec->qmin = get_byte(pb);
497
            codec->qmax = get_byte(pb);
498
            codec->max_qdiff = get_byte(pb);
499
            codec->qcompress = get_be16(pb) / 10000.0;
500
            codec->qblur = get_be16(pb) / 10000.0;
501
            codec->bit_rate_tolerance = get_be32(pb);
502
            codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
503
            codec->rc_max_rate = get_be32(pb);
504
            codec->rc_min_rate = get_be32(pb);
505
            codec->rc_buffer_size = get_be32(pb);
506
            codec->i_quant_factor = av_int2dbl(get_be64(pb));
507
            codec->b_quant_factor = av_int2dbl(get_be64(pb));
508
            codec->i_quant_offset = av_int2dbl(get_be64(pb));
509
            codec->b_quant_offset = av_int2dbl(get_be64(pb));
510
            codec->dct_algo = get_be32(pb);
511
            codec->strict_std_compliance = get_be32(pb);
512
            codec->max_b_frames = get_be32(pb);
513
            codec->luma_elim_threshold = get_be32(pb);
514
            codec->chroma_elim_threshold = get_be32(pb);
515
            codec->mpeg_quant = get_be32(pb);
516
            codec->intra_dc_precision = get_be32(pb);
517
            codec->me_method = get_be32(pb);
518
            codec->mb_decision = get_be32(pb);
519
            codec->nsse_weight = get_be32(pb);
520
            codec->frame_skip_cmp = get_be32(pb);
521
            codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
522
            codec->codec_tag = get_be32(pb);
523
            break;
524
        case CODEC_TYPE_AUDIO:
525
            codec->sample_rate = get_be32(pb);
526
            codec->channels = get_le16(pb);
527
            codec->frame_size = get_le16(pb);
528
            break;
529
        default:
530
            goto fail;
531
        }
532

  
533
    }
534

  
535
    /* get until end of block reached */
536
    while ((url_ftell(pb) % ffm->packet_size) != 0)
537
        get_byte(pb);
538

  
539
    /* init packet demux */
540
    ffm->packet_ptr = ffm->packet;
541
    ffm->packet_end = ffm->packet;
542
    ffm->frame_offset = 0;
543
    ffm->pts = 0;
544
    ffm->read_state = READ_HEADER;
545
    ffm->first_packet = 1;
546
    return 0;
547
 fail:
548
    for(i=0;i<s->nb_streams;i++) {
549
        st = s->streams[i];
550
        if (st) {
551
            av_freep(&st->priv_data);
552
            av_free(st);
553
        }
554
    }
555
    return -1;
556
}
557

  
558
/* return < 0 if eof */
559
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
560
{
561
    int size;
562
    FFMContext *ffm = s->priv_data;
563
    int duration;
564

  
565
    switch(ffm->read_state) {
566
    case READ_HEADER:
567
        if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
568
            return AVERROR(EAGAIN);
569
        }
570
#if 0
571
        printf("pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
572
               url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
573
#endif
574
        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
575
            FRAME_HEADER_SIZE)
576
            return AVERROR(EAGAIN);
577
#if 0
578
        {
579
            int i;
580
            for(i=0;i<FRAME_HEADER_SIZE;i++)
581
                printf("%02x ", ffm->header[i]);
582
            printf("\n");
583
        }
584
#endif
585
        ffm->read_state = READ_DATA;
586
        /* fall thru */
587
    case READ_DATA:
588
        size = AV_RB24(ffm->header + 2);
589
        if (!ffm_is_avail_data(s, size)) {
590
            return AVERROR(EAGAIN);
591
        }
592

  
593
        duration = AV_RB24(ffm->header + 5);
594

  
595
        av_new_packet(pkt, size);
596
        pkt->stream_index = ffm->header[0];
597
        if ((unsigned)pkt->stream_index >= s->nb_streams) {
598
            av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
599
            av_free_packet(pkt);
600
            ffm->read_state = READ_HEADER;
601
            return AVERROR(EAGAIN);
602
        }
603
        pkt->pos = url_ftell(s->pb);
604
        if (ffm->header[1] & FLAG_KEY_FRAME)
605
            pkt->flags |= PKT_FLAG_KEY;
606

  
607
        ffm->read_state = READ_HEADER;
608
        if (ffm_read_data(s, pkt->data, size, 0) != size) {
609
            /* bad case: desynchronized packet. we cancel all the packet loading */
610
            av_free_packet(pkt);
611
            return AVERROR(EAGAIN);
612
        }
613
        if (ffm->first_frame_in_packet)
614
        {
615
            pkt->pts = ffm->pts;
616
            ffm->first_frame_in_packet = 0;
617
        }
618
        pkt->duration = duration;
619
        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 %"PRIx64" -> %"PRIx64"\n", pos1, pos);
639
#endif
640
    url_fseek(pb, pos, SEEK_SET);
641
}
642

  
643
static int64_t get_pts(AVFormatContext *s, offset_t pos)
644
{
645
    ByteIOContext *pb = s->pb;
646
    int64_t pts;
647

  
648
    ffm_seek1(s, pos);
649
    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
   positioned at or before pts. XXX: the following code is quite
659
   approximative */
660
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
661
{
662
    FFMContext *ffm = s->priv_data;
663
    offset_t pos_min, pos_max, pos;
664
    int64_t pts_min, pts_max, pts;
665
    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
        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
679
            (double)(pts_max - pts_min);
680
        pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
681
        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
    pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
696
    if (pos > 0)
697
        pos -= FFM_PACKET_SIZE;
698
 found:
699
    ffm_seek1(s, pos);
700
    return 0;
701
}
702

  
703
#ifdef CONFIG_FFSERVER
704
offset_t ffm_read_write_index(int fd)
705
{
706
    uint8_t buf[8];
707

  
708
    lseek(fd, 8, SEEK_SET);
709
    read(fd, buf, 8);
710
    return AV_RB64(buf);
711
}
712

  
713
void ffm_write_write_index(int fd, offset_t pos)
714
{
715
    uint8_t buf[8];
716
    int i;
717

  
718
    for(i=0;i<8;i++)
719
        buf[i] = (pos >> (56 - i * 8)) & 0xff;
720
    lseek(fd, 8, SEEK_SET);
721
    write(fd, buf, 8);
722
}
723

  
724
void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
725
{
726
    FFMContext *ffm = s->priv_data;
727
    ffm->write_index = pos;
728
    ffm->file_size = file_size;
729
}
730
#endif // CONFIG_FFSERVER
731

  
732
static int ffm_read_close(AVFormatContext *s)
733
{
734
    AVStream *st;
735
    int i;
736

  
737
    for(i=0;i<s->nb_streams;i++) {
738
        st = s->streams[i];
739
        av_freep(&st->priv_data);
740
    }
741
    return 0;
742
}
743

  
744
static int ffm_probe(AVProbeData *p)
745
{
746
    if (
747
        p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
748
        p->buf[3] == '1')
749
        return AVPROBE_SCORE_MAX + 1;
750
    return 0;
751
}
752

  
753
#ifdef CONFIG_FFM_DEMUXER
754
AVInputFormat ffm_demuxer = {
755
    "ffm",
756
    "ffm format",
757
    sizeof(FFMContext),
758
    ffm_probe,
759
    ffm_read_header,
760
    ffm_read_packet,
761
    ffm_read_close,
762
    ffm_seek,
763
};
764
#endif
765
#ifdef CONFIG_FFM_MUXER
766
AVOutputFormat ffm_muxer = {
767
    "ffm",
768
    "ffm format",
769
    "",
770
    "ffm",
771
    sizeof(FFMContext),
772
    /* not really used */
773
    CODEC_ID_MP2,
774
    CODEC_ID_MPEG1VIDEO,
775
    ffm_write_header,
776
    ffm_write_packet,
777
    ffm_write_trailer,
778
};
779
#endif //CONFIG_FFM_MUXER
libavformat/ffm.h
1
/*
2
 * FFM (ffserver live feed) common header
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

  
22
#ifndef FFMPEG_FFM_H
23
#define FFMPEG_FFM_H
24

  
25
#include "avformat.h"
26

  
27
/* The FFM file is made of blocks of fixed size */
28
#define FFM_HEADER_SIZE 14
29
#define PACKET_ID       0x666d
30

  
31
/* each packet contains frames (which can span several packets */
32
#define FRAME_HEADER_SIZE    8
33
#define FLAG_KEY_FRAME       0x01
34

  
35
typedef struct FFMStream {
36
    int64_t pts;
37
} FFMStream;
38

  
39
enum {
40
    READ_HEADER,
41
    READ_DATA,
42
};
43

  
44
typedef struct FFMContext {
45
    /* only reading mode */
46
    offset_t write_index, file_size;
47
    int read_state;
48
    uint8_t header[FRAME_HEADER_SIZE];
49

  
50
    /* read and write */
51
    int first_packet; /* true if first packet, needed to set the discontinuity tag */
52
    int first_frame_in_packet; /* true if first frame in packet, needed to know if PTS information is valid */
53
    int packet_size;
54
    int frame_offset;
55
    int64_t pts;
56
    uint8_t *packet_ptr, *packet_end;
57
    uint8_t packet[FFM_PACKET_SIZE];
58
} FFMContext;
59

  
60
#endif /* FFMPEG_FFM_H */
libavformat/ffmdec.c
1
/*
2
 * FFM (ffserver live feed) demuxer
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

  
22
#include "avformat.h"
23
#include "ffm.h"
24
#include <unistd.h>
25

  
26
static int64_t get_pts(AVFormatContext *s, offset_t pos);
27

  
28
static int ffm_is_avail_data(AVFormatContext *s, int size)
29
{
30
    FFMContext *ffm = s->priv_data;
31
    offset_t pos, avail_size;
32
    int len;
33

  
34
    len = ffm->packet_end - ffm->packet_ptr;
35
    if (size <= len)
36
        return 1;
37
    pos = url_ftell(s->pb);
38
    if (pos == ffm->write_index) {
39
        /* exactly at the end of stream */
40
        return 0;
41
    } else if (pos < ffm->write_index) {
42
        avail_size = ffm->write_index - pos;
43
    } else {
44
        avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
45
    }
46
    avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
47
    if (size <= avail_size)
48
        return 1;
49
    else
50
        return 0;
51
}
52

  
53
/* first is true if we read the frame header */
54
static int ffm_read_data(AVFormatContext *s,
55
                         uint8_t *buf, int size, int first)
56
{
57
    FFMContext *ffm = s->priv_data;
58
    ByteIOContext *pb = s->pb;
59
    int len, fill_size, size1, frame_offset;
60

  
61
    size1 = size;
62
    while (size > 0) {
63
    redo:
64
        len = ffm->packet_end - ffm->packet_ptr;
65
        if (len > size)
66
            len = size;
67
        if (len == 0) {
68
            if (url_ftell(pb) == ffm->file_size)
69
                url_fseek(pb, ffm->packet_size, SEEK_SET);
70
    retry_read:
71
            get_be16(pb); /* PACKET_ID */
72
            fill_size = get_be16(pb);
73
            ffm->pts = get_be64(pb);
74
            ffm->first_frame_in_packet = 1;
75
            frame_offset = get_be16(pb);
76
            get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
77
            ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
78
            if (ffm->packet_end < ffm->packet)
79
                return -1;
80
            /* if first packet or resynchronization packet, we must
81
               handle it specifically */
82
            if (ffm->first_packet || (frame_offset & 0x8000)) {
83
                if (!frame_offset) {
84
                    /* This packet has no frame headers in it */
85
                    if (url_ftell(pb) >= ffm->packet_size * 3) {
86
                        url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
87
                        goto retry_read;
88
                    }
89
                    /* This is bad, we cannot find a valid frame header */
90
                    return 0;
91
                }
92
                ffm->first_packet = 0;
93
                if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
94
                    return -1;
95
                ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
96
                if (!first)
97
                    break;
98
            } else {
99
                ffm->packet_ptr = ffm->packet;
100
            }
101
            goto redo;
102
        }
103
        memcpy(buf, ffm->packet_ptr, len);
104
        buf += len;
105
        ffm->packet_ptr += len;
106
        size -= len;
107
        first = 0;
108
    }
109
    return size1 - size;
110
}
111

  
112

  
113
static void adjust_write_index(AVFormatContext *s)
114
{
115
    FFMContext *ffm = s->priv_data;
116
    ByteIOContext *pb = s->pb;
117
    int64_t pts;
118
    //offset_t orig_write_index = ffm->write_index;
119
    offset_t pos_min, pos_max;
120
    int64_t pts_start;
121
    offset_t ptr = url_ftell(pb);
122

  
123

  
124
    pos_min = 0;
125
    pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
126

  
127
    pts_start = get_pts(s, pos_min);
128

  
129
    pts = get_pts(s, pos_max);
130

  
131
    if (pts - 100000 > pts_start)
132
        goto end;
133

  
134
    ffm->write_index = FFM_PACKET_SIZE;
135

  
136
    pts_start = get_pts(s, pos_min);
137

  
138
    pts = get_pts(s, pos_max);
139

  
140
    if (pts - 100000 <= pts_start) {
141
        while (1) {
142
            offset_t newpos;
143
            int64_t newpts;
144

  
145
            newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
146

  
147
            if (newpos == pos_min)
148
                break;
149

  
150
            newpts = get_pts(s, newpos);
151

  
152
            if (newpts - 100000 <= pts) {
153
                pos_max = newpos;
154
                pts = newpts;
155
            } else {
156
                pos_min = newpos;
157
            }
158
        }
159
        ffm->write_index += pos_max;
160
    }
161

  
162
    //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
163
    //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
164

  
165
 end:
166
    url_fseek(pb, ptr, SEEK_SET);
167
}
168

  
169

  
170
static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
171
{
172
    FFMContext *ffm = s->priv_data;
173
    AVStream *st;
174
    FFMStream *fst;
175
    ByteIOContext *pb = s->pb;
176
    AVCodecContext *codec;
177
    int i, nb_streams;
178
    uint32_t tag;
179

  
180
    /* header */
181
    tag = get_le32(pb);
182
    if (tag != MKTAG('F', 'F', 'M', '1'))
183
        goto fail;
184
    ffm->packet_size = get_be32(pb);
185
    if (ffm->packet_size != FFM_PACKET_SIZE)
186
        goto fail;
187
    ffm->write_index = get_be64(pb);
188
    /* get also filesize */
189
    if (!url_is_streamed(pb)) {
190
        ffm->file_size = url_fsize(pb);
191
        adjust_write_index(s);
192
    } else {
193
        ffm->file_size = (UINT64_C(1) << 63) - 1;
194
    }
195

  
196
    nb_streams = get_be32(pb);
197
    get_be32(pb); /* total bitrate */
198
    /* read each stream */
199
    for(i=0;i<nb_streams;i++) {
200
        char rc_eq_buf[128];
201

  
202
        st = av_new_stream(s, 0);
203
        if (!st)
204
            goto fail;
205
        fst = av_mallocz(sizeof(FFMStream));
206
        if (!fst)
207
            goto fail;
208
        s->streams[i] = st;
209

  
210
        av_set_pts_info(st, 64, 1, 1000000);
211

  
212
        st->priv_data = fst;
213

  
214
        codec = st->codec;
215
        /* generic info */
216
        codec->codec_id = get_be32(pb);
217
        codec->codec_type = get_byte(pb); /* codec_type */
218
        codec->bit_rate = get_be32(pb);
219
        st->quality = get_be32(pb);
220
        codec->flags = get_be32(pb);
221
        codec->flags2 = get_be32(pb);
222
        codec->debug = get_be32(pb);
223
        /* specific info */
224
        switch(codec->codec_type) {
225
        case CODEC_TYPE_VIDEO:
226
            codec->time_base.num = get_be32(pb);
227
            codec->time_base.den = get_be32(pb);
228
            codec->width = get_be16(pb);
229
            codec->height = get_be16(pb);
230
            codec->gop_size = get_be16(pb);
231
            codec->pix_fmt = get_be32(pb);
232
            codec->qmin = get_byte(pb);
233
            codec->qmax = get_byte(pb);
234
            codec->max_qdiff = get_byte(pb);
235
            codec->qcompress = get_be16(pb) / 10000.0;
236
            codec->qblur = get_be16(pb) / 10000.0;
237
            codec->bit_rate_tolerance = get_be32(pb);
238
            codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
239
            codec->rc_max_rate = get_be32(pb);
240
            codec->rc_min_rate = get_be32(pb);
241
            codec->rc_buffer_size = get_be32(pb);
242
            codec->i_quant_factor = av_int2dbl(get_be64(pb));
243
            codec->b_quant_factor = av_int2dbl(get_be64(pb));
244
            codec->i_quant_offset = av_int2dbl(get_be64(pb));
245
            codec->b_quant_offset = av_int2dbl(get_be64(pb));
246
            codec->dct_algo = get_be32(pb);
247
            codec->strict_std_compliance = get_be32(pb);
248
            codec->max_b_frames = get_be32(pb);
249
            codec->luma_elim_threshold = get_be32(pb);
250
            codec->chroma_elim_threshold = get_be32(pb);
251
            codec->mpeg_quant = get_be32(pb);
252
            codec->intra_dc_precision = get_be32(pb);
253
            codec->me_method = get_be32(pb);
254
            codec->mb_decision = get_be32(pb);
255
            codec->nsse_weight = get_be32(pb);
256
            codec->frame_skip_cmp = get_be32(pb);
257
            codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
258
            codec->codec_tag = get_be32(pb);
259
            break;
260
        case CODEC_TYPE_AUDIO:
261
            codec->sample_rate = get_be32(pb);
262
            codec->channels = get_le16(pb);
263
            codec->frame_size = get_le16(pb);
264
            break;
265
        default:
266
            goto fail;
267
        }
268

  
269
    }
270

  
271
    /* get until end of block reached */
272
    while ((url_ftell(pb) % ffm->packet_size) != 0)
273
        get_byte(pb);
274

  
275
    /* init packet demux */
276
    ffm->packet_ptr = ffm->packet;
277
    ffm->packet_end = ffm->packet;
278
    ffm->frame_offset = 0;
279
    ffm->pts = 0;
280
    ffm->read_state = READ_HEADER;
281
    ffm->first_packet = 1;
282
    return 0;
283
 fail:
284
    for(i=0;i<s->nb_streams;i++) {
285
        st = s->streams[i];
286
        if (st) {
287
            av_freep(&st->priv_data);
288
            av_free(st);
289
        }
290
    }
291
    return -1;
292
}
293

  
294
/* return < 0 if eof */
295
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
296
{
297
    int size;
298
    FFMContext *ffm = s->priv_data;
299
    int duration;
300

  
301
    switch(ffm->read_state) {
302
    case READ_HEADER:
303
        if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
304
            return AVERROR(EAGAIN);
305
        }
306
#if 0
307
        printf("pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
308
               url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
309
#endif
310
        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
311
            FRAME_HEADER_SIZE)
312
            return AVERROR(EAGAIN);
313
#if 0
314
        {
315
            int i;
316
            for(i=0;i<FRAME_HEADER_SIZE;i++)
317
                printf("%02x ", ffm->header[i]);
318
            printf("\n");
319
        }
320
#endif
321
        ffm->read_state = READ_DATA;
322
        /* fall thru */
323
    case READ_DATA:
324
        size = AV_RB24(ffm->header + 2);
325
        if (!ffm_is_avail_data(s, size)) {
326
            return AVERROR(EAGAIN);
327
        }
328

  
329
        duration = AV_RB24(ffm->header + 5);
330

  
331
        av_new_packet(pkt, size);
332
        pkt->stream_index = ffm->header[0];
333
        if ((unsigned)pkt->stream_index >= s->nb_streams) {
334
            av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
335
            av_free_packet(pkt);
336
            ffm->read_state = READ_HEADER;
337
            return AVERROR(EAGAIN);
338
        }
339
        pkt->pos = url_ftell(s->pb);
340
        if (ffm->header[1] & FLAG_KEY_FRAME)
341
            pkt->flags |= PKT_FLAG_KEY;
342

  
343
        ffm->read_state = READ_HEADER;
344
        if (ffm_read_data(s, pkt->data, size, 0) != size) {
345
            /* bad case: desynchronized packet. we cancel all the packet loading */
346
            av_free_packet(pkt);
347
            return AVERROR(EAGAIN);
348
        }
349
        if (ffm->first_frame_in_packet)
350
        {
351
            pkt->pts = ffm->pts;
352
            ffm->first_frame_in_packet = 0;
353
        }
354
        pkt->duration = duration;
355
        break;
356
    }
357
    return 0;
358
}
359

  
360
//#define DEBUG_SEEK
361

  
362
/* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
363
   by the write position inside this function */
364
static void ffm_seek1(AVFormatContext *s, offset_t pos1)
365
{
366
    FFMContext *ffm = s->priv_data;
367
    ByteIOContext *pb = s->pb;
368
    offset_t pos;
369

  
370
    pos = pos1 + ffm->write_index;
371
    if (pos >= ffm->file_size)
372
        pos -= (ffm->file_size - FFM_PACKET_SIZE);
373
#ifdef DEBUG_SEEK
374
    printf("seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
375
#endif
376
    url_fseek(pb, pos, SEEK_SET);
377
}
378

  
379
static int64_t get_pts(AVFormatContext *s, offset_t pos)
380
{
381
    ByteIOContext *pb = s->pb;
382
    int64_t pts;
383

  
384
    ffm_seek1(s, pos);
385
    url_fskip(pb, 4);
386
    pts = get_be64(pb);
387
#ifdef DEBUG_SEEK
388
    printf("pts=%0.6f\n", pts / 1000000.0);
389
#endif
390
    return pts;
391
}
392

  
393
/* seek to a given time in the file. The file read pointer is
394
   positioned at or before pts. XXX: the following code is quite
395
   approximative */
396
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
397
{
398
    FFMContext *ffm = s->priv_data;
399
    offset_t pos_min, pos_max, pos;
400
    int64_t pts_min, pts_max, pts;
401
    double pos1;
402

  
403
#ifdef DEBUG_SEEK
404
    printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
405
#endif
406
    /* find the position using linear interpolation (better than
407
       dichotomy in typical cases) */
408
    pos_min = 0;
409
    pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
410
    while (pos_min <= pos_max) {
411
        pts_min = get_pts(s, pos_min);
412
        pts_max = get_pts(s, pos_max);
413
        /* linear interpolation */
414
        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
415
            (double)(pts_max - pts_min);
416
        pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
417
        if (pos <= pos_min)
418
            pos = pos_min;
419
        else if (pos >= pos_max)
420
            pos = pos_max;
421
        pts = get_pts(s, pos);
422
        /* check if we are lucky */
423
        if (pts == wanted_pts) {
424
            goto found;
425
        } else if (pts > wanted_pts) {
426
            pos_max = pos - FFM_PACKET_SIZE;
427
        } else {
428
            pos_min = pos + FFM_PACKET_SIZE;
429
        }
430
    }
431
    pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
432
    if (pos > 0)
433
        pos -= FFM_PACKET_SIZE;
434
 found:
435
    ffm_seek1(s, pos);
436
    return 0;
437
}
438

  
439
#ifdef CONFIG_FFSERVER
440
offset_t ffm_read_write_index(int fd)
441
{
442
    uint8_t buf[8];
443

  
444
    lseek(fd, 8, SEEK_SET);
445
    read(fd, buf, 8);
446
    return AV_RB64(buf);
447
}
448

  
449
void ffm_write_write_index(int fd, offset_t pos)
450
{
451
    uint8_t buf[8];
452
    int i;
453

  
454
    for(i=0;i<8;i++)
455
        buf[i] = (pos >> (56 - i * 8)) & 0xff;
456
    lseek(fd, 8, SEEK_SET);
457
    write(fd, buf, 8);
458
}
459

  
460
void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
461
{
462
    FFMContext *ffm = s->priv_data;
463
    ffm->write_index = pos;
464
    ffm->file_size = file_size;
465
}
466
#endif // CONFIG_FFSERVER
467

  
468
static int ffm_read_close(AVFormatContext *s)
469
{
470
    AVStream *st;
471
    int i;
472

  
473
    for(i=0;i<s->nb_streams;i++) {
474
        st = s->streams[i];
475
        av_freep(&st->priv_data);
476
    }
477
    return 0;
478
}
479

  
480
static int ffm_probe(AVProbeData *p)
481
{
482
    if (
483
        p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
484
        p->buf[3] == '1')
485
        return AVPROBE_SCORE_MAX + 1;
486
    return 0;
487
}
488

  
489
AVInputFormat ffm_demuxer = {
490
    "ffm",
491
    "ffm format",
492
    sizeof(FFMContext),
493
    ffm_probe,
494
    ffm_read_header,
495
    ffm_read_packet,
496
    ffm_read_close,
497
    ffm_seek,
498
};
libavformat/ffmenc.c
1
/*
2
 * FFM (ffserver live feed) muxer
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

  
22
#include "avformat.h"
23
#include "ffm.h"
24
#include <unistd.h>
25

  
26
/* disable pts hack for testing */
27
int ffm_nopts = 0;
28

  
29
static void flush_packet(AVFormatContext *s)
30
{
31
    FFMContext *ffm = s->priv_data;
32
    int fill_size, h;
33
    ByteIOContext *pb = s->pb;
34

  
35
    fill_size = ffm->packet_end - ffm->packet_ptr;
36
    memset(ffm->packet_ptr, 0, fill_size);
37

  
38
    if (url_ftell(pb) % ffm->packet_size)
39
        av_abort();
40

  
41
    /* put header */
42
    put_be16(pb, PACKET_ID);
43
    put_be16(pb, fill_size);
44
    put_be64(pb, ffm->pts);
45
    h = ffm->frame_offset;
46
    if (ffm->first_packet)
47
        h |= 0x8000;
48
    put_be16(pb, h);
49
    put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
50
    put_flush_packet(pb);
51

  
52
    /* prepare next packet */
53
    ffm->frame_offset = 0; /* no key frame */
54
    ffm->pts = 0; /* no pts */
55
    ffm->packet_ptr = ffm->packet;
56
    ffm->first_packet = 0;
57
}
58

  
59
/* 'first' is true if first data of a frame */
60
static void ffm_write_data(AVFormatContext *s,
61
                           const uint8_t *buf, int size,
62
                           int64_t pts, int first)
63
{
64
    FFMContext *ffm = s->priv_data;
65
    int len;
66

  
67
    if (first && ffm->frame_offset == 0)
68
        ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
69
    if (first && ffm->pts == 0)
70
        ffm->pts = pts;
71

  
72
    /* write as many packets as needed */
73
    while (size > 0) {
74
        len = ffm->packet_end - ffm->packet_ptr;
75
        if (len > size)
76
            len = size;
77
        memcpy(ffm->packet_ptr, buf, len);
78

  
79
        ffm->packet_ptr += len;
80
        buf += len;
81
        size -= len;
82
        if (ffm->packet_ptr >= ffm->packet_end) {
83
            /* special case : no pts in packet : we leave the current one */
84
            if (ffm->pts == 0)
85
                ffm->pts = pts;
86

  
87
            flush_packet(s);
88
        }
89
    }
90
}
91

  
92
static int ffm_write_header(AVFormatContext *s)
93
{
94
    FFMContext *ffm = s->priv_data;
95
    AVStream *st;
96
    FFMStream *fst;
97
    ByteIOContext *pb = s->pb;
98
    AVCodecContext *codec;
99
    int bit_rate, i;
100

  
101
    ffm->packet_size = FFM_PACKET_SIZE;
102

  
103
    /* header */
104
    put_le32(pb, MKTAG('F', 'F', 'M', '1'));
105
    put_be32(pb, ffm->packet_size);
106
    /* XXX: store write position in other file ? */
107
    put_be64(pb, ffm->packet_size); /* current write position */
108

  
109
    put_be32(pb, s->nb_streams);
110
    bit_rate = 0;
111
    for(i=0;i<s->nb_streams;i++) {
112
        st = s->streams[i];
113
        bit_rate += st->codec->bit_rate;
114
    }
115
    put_be32(pb, bit_rate);
116

  
117
    /* list of streams */
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff