Statistics
| Branch: | Revision:

ffmpeg / libav / ffm.c @ 789bee12

History | View | Annotate | Download (17.4 KB)

1
/*
2
 * FFM (ffserver live feed) encoder and decoder
3
 * Copyright (c) 2001 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 */
19
#include "avformat.h"
20
#include <unistd.h>
21

    
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
#define FRAME_HEADER_SIZE    8
28
#define FLAG_KEY_FRAME       0x01
29

    
30
typedef struct FFMStream {
31
    INT64 pts;
32
} 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
    int read_state;
43
    UINT8 header[FRAME_HEADER_SIZE];
44

    
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
    INT64 pts;
50
    UINT8 *packet_ptr, *packet_end;
51
    UINT8 packet[1]; /* must be last */
52
} FFMContext;
53

    
54
/* disable pts hack for testing */
55
int ffm_nopts = 0;
56

    
57
static void flush_packet(AVFormatContext *s)
58
{
59
    FFMContext *ffm = s->priv_data;
60
    int fill_size, h;
61
    ByteIOContext *pb = &s->pb;
62

    
63
    fill_size = ffm->packet_end - ffm->packet_ptr;
64
    memset(ffm->packet_ptr, 0, fill_size);
65

    
66
    /* put header */
67
    put_be16(pb, PACKET_ID);
68
    put_be16(pb, fill_size);
69
    put_be64(pb, ffm->pts);
70
    h = ffm->frame_offset;
71
    if (ffm->first_packet)
72
        h |= 0x8000;
73
    put_be16(pb, h);
74
    put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
75

    
76
    /* prepare next packet */
77
    ffm->frame_offset = 0; /* no key frame */
78
    ffm->pts = 0; /* no pts */
79
    ffm->packet_ptr = ffm->packet;
80
    ffm->first_packet = 0;
81
}
82

    
83
/* 'first' is true if first data of a frame */
84
static void ffm_write_data(AVFormatContext *s,
85
                           UINT8 *buf, int size,
86
                           INT64 pts, int first)
87
{
88
    FFMContext *ffm = s->priv_data;
89
    int len;
90

    
91
    if (first && ffm->frame_offset == 0)
92
        ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
93
    if (first && ffm->pts == 0)
94
        ffm->pts = pts;
95

    
96
    /* write as many packets as needed */
97
    while (size > 0) {
98
        len = ffm->packet_end - ffm->packet_ptr;
99
        if (len > size)
100
            len = size;
101
        memcpy(ffm->packet_ptr, buf, len);
102

    
103
        ffm->packet_ptr += len;
104
        buf += len;
105
        size -= len;
106
        if (ffm->packet_ptr >= ffm->packet_end) {
107
            /* special case : no pts in packet : we leave the current one */
108
            if (ffm->pts == 0)
109
                ffm->pts = pts;
110

    
111
            flush_packet(s);
112
        }
113
    }
114
}
115

    
116
static int ffm_write_header(AVFormatContext *s)
117
{
118
    FFMContext *ffm = s->priv_data;
119
    AVStream *st;
120
    FFMStream *fst;
121
    ByteIOContext *pb = &s->pb;
122
    AVCodecContext *codec;
123
    int bit_rate, i;
124

    
125
    ffm->packet_size = FFM_PACKET_SIZE;
126

    
127
    /* header */
128
    put_tag(pb, "FFM1");
129
    put_be32(pb, ffm->packet_size);
130
    /* XXX: store write position in other file ? */
131
    put_be64(pb, ffm->packet_size); /* current write position */
132

    
133
    put_be32(pb, s->nb_streams);
134
    bit_rate = 0;
135
    for(i=0;i<s->nb_streams;i++) {
136
        st = s->streams[i];
137
        bit_rate += st->codec.bit_rate;
138
    }
139
    put_be32(pb, bit_rate);
140

    
141
    /* list of streams */
142
    for(i=0;i<s->nb_streams;i++) {
143
        st = s->streams[i];
144
        fst = av_mallocz(sizeof(FFMStream) + ffm->packet_size);
145
        if (!fst)
146
            goto fail;
147
        st->priv_data = fst;
148

    
149
        codec = &st->codec;
150
        /* generic info */
151
        put_be32(pb, codec->codec_id);
152
        put_byte(pb, codec->codec_type);
153
        put_be32(pb, codec->bit_rate);
154
        put_be32(pb, codec->flags);
155
        /* specific info */
156
        switch(codec->codec_type) {
157
        case CODEC_TYPE_VIDEO:
158
            put_be32(pb, (codec->frame_rate * 1000) / FRAME_RATE_BASE);
159
            put_be16(pb, codec->width);
160
            put_be16(pb, codec->height);
161
            put_be16(pb, codec->gop_size);
162
            put_byte(pb, codec->qmin);
163
            put_byte(pb, codec->qmax);
164
            put_byte(pb, codec->max_qdiff);
165
            put_be16(pb, (int) (codec->qcompress * 10000.0));
166
            put_be16(pb, (int) (codec->qblur * 10000.0));
167
            put_be32(pb, codec->bit_rate_tolerance);
168
            break;
169
        case CODEC_TYPE_AUDIO:
170
            put_be32(pb, codec->sample_rate);
171
            put_le16(pb, codec->channels);
172
            put_le16(pb, codec->frame_size);
173
            break;
174
        default:
175
            av_abort();
176
        }
177
        /* hack to have real time */
178
        if (ffm_nopts)
179
            fst->pts = 0;
180
        else
181
            fst->pts = gettime();
182
    }
183

    
184
    /* flush until end of block reached */
185
    while ((url_ftell(pb) % ffm->packet_size) != 0)
186
        put_byte(pb, 0);
187

    
188
    put_flush_packet(pb);
189

    
190
    /* init packet mux */
191
    ffm->packet_ptr = ffm->packet;
192
    ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
193
    ffm->frame_offset = 0;
194
    ffm->pts = 0;
195
    ffm->first_packet = 1;
196

    
197
    return 0;
198
 fail:
199
    for(i=0;i<s->nb_streams;i++) {
200
        st = s->streams[i];
201
        av_freep(&st->priv_data);
202
    }
203
    return -1;
204
}
205

    
206
static int ffm_write_packet(AVFormatContext *s, int stream_index,
207
                            UINT8 *buf, int size, int force_pts)
208
{
209
    AVStream *st = s->streams[stream_index];
210
    FFMStream *fst = st->priv_data;
211
    INT64 pts;
212
    UINT8 header[FRAME_HEADER_SIZE];
213
    int duration;
214

    
215
    if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
216
        duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
217
    } else {
218
        duration = (1000000.0 * FRAME_RATE_BASE / (float)st->codec.frame_rate);
219
    }
220

    
221
    pts = fst->pts;
222
    /* packet size & key_frame */
223
    header[0] = stream_index;
224
    header[1] = 0;
225
    if (st->codec.key_frame)
226
        header[1] |= FLAG_KEY_FRAME;
227
    header[2] = (size >> 16) & 0xff;
228
    header[3] = (size >> 8) & 0xff;
229
    header[4] = size & 0xff;
230
    header[5] = (duration >> 16) & 0xff;
231
    header[6] = (duration >> 8) & 0xff;
232
    header[7] = duration & 0xff;
233
    ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
234
    ffm_write_data(s, buf, size, pts, 0);
235

    
236
    fst->pts += duration;
237
    return 0;
238
}
239

    
240
static int ffm_write_trailer(AVFormatContext *s)
241
{
242
    ByteIOContext *pb = &s->pb;
243
    FFMContext *ffm = s->priv_data;
244
    int i;
245

    
246
    /* flush packets */
247
    if (ffm->packet_ptr > ffm->packet)
248
        flush_packet(s);
249

    
250
    put_flush_packet(pb);
251

    
252
    for(i=0;i<s->nb_streams;i++)
253
        av_freep(&s->streams[i]->priv_data);
254
    return 0;
255
}
256

    
257
/* ffm demux */
258

    
259
static int ffm_is_avail_data(AVFormatContext *s, int size)
260
{
261
    FFMContext *ffm = s->priv_data;
262
    offset_t pos, avail_size;
263
    int len;
264

    
265
    len = ffm->packet_end - ffm->packet_ptr;
266
    if (size <= len)
267
        return 1;
268
    pos = url_ftell(&s->pb);
269
    if (pos == ffm->write_index) {
270
        /* exactly at the end of stream */
271
        return 0;
272
    } else if (pos < ffm->write_index) {
273
        avail_size = ffm->write_index - pos;
274
    } else {
275
        avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
276
    }
277
    avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
278
    if (size <= avail_size)
279
        return 1;
280
    else
281
        return 0;
282
}
283

    
284
/* first is true if we read the frame header */
285
static int ffm_read_data(AVFormatContext *s,
286
                          UINT8 *buf, int size, int first)
287
{
288
    FFMContext *ffm = s->priv_data;
289
    ByteIOContext *pb = &s->pb;
290
    int len, fill_size, size1, frame_offset;
291

    
292
    size1 = size;
293
    while (size > 0) {
294
    redo:
295
        len = ffm->packet_end - ffm->packet_ptr;
296
        if (len > size)
297
            len = size;
298
        if (len == 0) {
299
            if (url_ftell(pb) == ffm->file_size)
300
                url_fseek(pb, ffm->packet_size, SEEK_SET);
301
    retry_read:
302
            get_be16(pb); /* PACKET_ID */
303
            fill_size = get_be16(pb);
304
            ffm->pts = get_be64(pb);
305
            frame_offset = get_be16(pb);
306
            get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
307
            ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
308
            /* if first packet or resynchronization packet, we must
309
               handle it specifically */
310
            if (ffm->first_packet || (frame_offset & 0x8000)) {
311
                if (!frame_offset) {
312
                    /* This packet has no frame headers in it */
313
                    if (url_ftell(pb) >= ffm->packet_size * 3) {
314
                        url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
315
                        goto retry_read;
316
                    }
317
                    /* This is bad, we cannot find a valid frame header */
318
                    return 0;
319
                }
320
                ffm->first_packet = 0;
321
                if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
322
                    av_abort();
323
                ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
324
                if (!first)
325
                    break;
326
            } else {
327
                ffm->packet_ptr = ffm->packet;
328
            }
329
            goto redo;
330
        }
331
        memcpy(buf, ffm->packet_ptr, len);
332
        buf += len;
333
        ffm->packet_ptr += len;
334
        size -= len;
335
        first = 0;
336
    }
337
    return size1 - size;
338
}
339

    
340

    
341
static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
342
{
343
    FFMContext *ffm = s->priv_data;
344
    AVStream *st;
345
    FFMStream *fst;
346
    ByteIOContext *pb = &s->pb;
347
    AVCodecContext *codec;
348
    int i;
349
    UINT32 tag;
350

    
351
    /* header */
352
    tag = get_le32(pb);
353
    if (tag != MKTAG('F', 'F', 'M', '1'))
354
        goto fail;
355
    ffm->packet_size = get_be32(pb);
356
    if (ffm->packet_size != FFM_PACKET_SIZE)
357
        goto fail;
358
    ffm->write_index = get_be64(pb);
359
    /* get also filesize */
360
    if (!url_is_streamed(pb)) {
361
        ffm->file_size = url_filesize(url_fileno(pb));
362
    } else {
363
        ffm->file_size = (UINT64_C(1) << 63) - 1;
364
    }
365

    
366
    s->nb_streams = get_be32(pb);
367
    get_be32(pb); /* total bitrate */
368
    /* read each stream */
369
    for(i=0;i<s->nb_streams;i++) {
370
        st = av_mallocz(sizeof(AVStream));
371
        if (!st)
372
            goto fail;
373
        s->streams[i] = st;
374
        fst = av_mallocz(sizeof(FFMStream) + ffm->packet_size);
375
        if (!fst)
376
            goto fail;
377
        st->priv_data = fst;
378

    
379
        codec = &st->codec;
380
        /* generic info */
381
        st->codec.codec_id = get_be32(pb);
382
        st->codec.codec_type = get_byte(pb); /* codec_type */
383
        codec->bit_rate = get_be32(pb);
384
        codec->flags = get_be32(pb);
385
        /* specific info */
386
        switch(codec->codec_type) {
387
        case CODEC_TYPE_VIDEO:
388
            codec->frame_rate = ((INT64)get_be32(pb) * FRAME_RATE_BASE) / 1000;
389
            codec->width = get_be16(pb);
390
            codec->height = get_be16(pb);
391
            codec->gop_size = get_be16(pb);
392
            codec->qmin = get_byte(pb);
393
            codec->qmax = get_byte(pb);
394
            codec->max_qdiff = get_byte(pb);
395
            codec->qcompress = get_be16(pb) / 10000.0;
396
            codec->qblur = get_be16(pb) / 10000.0;
397
            codec->bit_rate_tolerance = get_be32(pb);
398
            break;
399
        case CODEC_TYPE_AUDIO:
400
            codec->sample_rate = get_be32(pb);
401
            codec->channels = get_le16(pb);
402
            codec->frame_size = get_le16(pb);
403
            break;
404
        default:
405
            av_abort();
406
        }
407

    
408
    }
409

    
410
    /* get until end of block reached */
411
    while ((url_ftell(pb) % ffm->packet_size) != 0)
412
        get_byte(pb);
413

    
414
    /* init packet demux */
415
    ffm->packet_ptr = ffm->packet;
416
    ffm->packet_end = ffm->packet;
417
    ffm->frame_offset = 0;
418
    ffm->pts = 0;
419
    ffm->read_state = READ_HEADER;
420
    ffm->first_packet = 1;
421
    return 0;
422
 fail:
423
    for(i=0;i<s->nb_streams;i++) {
424
        st = s->streams[i];
425
        if (st) {
426
            av_freep(&st->priv_data);
427
            av_free(st);
428
        }
429
    }
430
    return -1;
431
}
432

    
433
/* return < 0 if eof */
434
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
435
{
436
    int size;
437
    FFMContext *ffm = s->priv_data;
438
    int duration;
439

    
440
    switch(ffm->read_state) {
441
    case READ_HEADER:
442
        if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE))
443
            return -EAGAIN;
444
#if 0
445
        printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
446
               url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
447
#endif
448
        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != FRAME_HEADER_SIZE)
449
            return -EAGAIN;
450

    
451
#if 0
452
        {
453
            int i;
454
            for(i=0;i<FRAME_HEADER_SIZE;i++)
455
                printf("%02x ", ffm->header[i]);
456
            printf("\n");
457
        }
458
#endif
459
        ffm->read_state = READ_DATA;
460
        /* fall thru */
461
    case READ_DATA:
462
        size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
463
        if (!ffm_is_avail_data(s, size)) {
464
            return -EAGAIN;
465
        }
466

    
467
        duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
468

    
469
        av_new_packet(pkt, size);
470
        pkt->stream_index = ffm->header[0];
471
        if (ffm->header[1] & FLAG_KEY_FRAME)
472
            pkt->flags |= PKT_FLAG_KEY;
473

    
474
        ffm->read_state = READ_HEADER;
475
        if (ffm_read_data(s, pkt->data, size, 0) != size) {
476
            /* bad case: desynchronized packet. we cancel all the packet loading */
477
            av_free_packet(pkt);
478
            return -EAGAIN;
479
        }
480
        pkt->pts = ffm->pts;
481
        pkt->duration = duration;
482
        break;
483
    }
484
    return 0;
485
}
486

    
487
//#define DEBUG_SEEK
488

    
489
/* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
490
   by the write position inside this function */
491
static void ffm_seek1(AVFormatContext *s, offset_t pos1)
492
{
493
    FFMContext *ffm = s->priv_data;
494
    ByteIOContext *pb = &s->pb;
495
    offset_t pos;
496

    
497
    pos = pos1 + ffm->write_index;
498
    if (pos >= ffm->file_size)
499
        pos -= (ffm->file_size - FFM_PACKET_SIZE);
500
#ifdef DEBUG_SEEK
501
    printf("seek to %Lx -> %Lx\n", pos1, pos);
502
#endif
503
    url_fseek(pb, pos, SEEK_SET);
504
}
505

    
506
static INT64 get_pts(AVFormatContext *s, offset_t pos)
507
{
508
    ByteIOContext *pb = &s->pb;
509
    INT64 pts;
510

    
511
    ffm_seek1(s, pos);
512
    url_fskip(pb, 4);
513
    pts = get_be64(pb);
514
#ifdef DEBUG_SEEK
515
    printf("pts=%0.6f\n", pts / 1000000.0);
516
#endif
517
    return pts;
518
}
519

    
520
/* seek to a given time in the file. The file read pointer is
521
   positionned at or before pts. XXX: the following code is quite
522
   approximative */
523
static int ffm_seek(AVFormatContext *s, INT64 wanted_pts)
524
{
525
    FFMContext *ffm = s->priv_data;
526
    offset_t pos_min, pos_max, pos;
527
    INT64 pts_min, pts_max, pts;
528
    double pos1;
529

    
530
#ifdef DEBUG_SEEK
531
    printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
532
#endif
533
    /* find the position using linear interpolation (better than
534
       dichotomy in typical cases) */
535
    pos_min = 0;
536
    pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
537
    while (pos_min <= pos_max) {
538
        pts_min = get_pts(s, pos_min);
539
        pts_max = get_pts(s, pos_max);
540
        /* linear interpolation */
541
        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
542
            (double)(pts_max - pts_min);
543
        pos = (((INT64)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
544
        if (pos <= pos_min)
545
            pos = pos_min;
546
        else if (pos >= pos_max)
547
            pos = pos_max;
548
        pts = get_pts(s, pos);
549
        /* check if we are lucky */
550
        if (pts == wanted_pts) {
551
            goto found;
552
        } else if (pts > wanted_pts) {
553
            pos_max = pos - FFM_PACKET_SIZE;
554
        } else {
555
            pos_min = pos + FFM_PACKET_SIZE;
556
        }
557
    }
558
    pos = pos_min;
559
    if (pos > 0)
560
        pos -= FFM_PACKET_SIZE;
561
 found:
562
    ffm_seek1(s, pos);
563
    return 0;
564
}
565

    
566
offset_t ffm_read_write_index(int fd)
567
{
568
    UINT8 buf[8];
569
    offset_t pos;
570
    int i;
571

    
572
    lseek(fd, 8, SEEK_SET);
573
    read(fd, buf, 8);
574
    pos = 0;
575
    for(i=0;i<8;i++)
576
        pos |= buf[i] << (56 - i * 8);
577
    return pos;
578
}
579

    
580
void ffm_write_write_index(int fd, offset_t pos)
581
{
582
    UINT8 buf[8];
583
    int i;
584

    
585
    for(i=0;i<8;i++)
586
        buf[i] = (pos >> (56 - i * 8)) & 0xff;
587
    lseek(fd, 8, SEEK_SET);
588
    write(fd, buf, 8);
589
}
590

    
591
void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
592
{
593
    FFMContext *ffm = s->priv_data;
594
    ffm->write_index = pos;
595
    ffm->file_size = file_size;
596
}
597

    
598
static int ffm_read_close(AVFormatContext *s)
599
{
600
    AVStream *st;
601
    int i;
602

    
603
    for(i=0;i<s->nb_streams;i++) {
604
        st = s->streams[i];
605
        av_freep(&st->priv_data);
606
    }
607
    av_freep(&s->priv_data);
608
    return 0;
609
}
610

    
611
AVInputFormat ffm_iformat = {
612
    "ffm",
613
    "ffm format",
614
    sizeof(FFMContext) + FFM_PACKET_SIZE,
615
    NULL,
616
    ffm_read_header,
617
    ffm_read_packet,
618
    ffm_read_close,
619
    ffm_seek,
620
    extensions: "ffm",
621
};
622

    
623
AVOutputFormat ffm_oformat = {
624
    "ffm",
625
    "ffm format",
626
    "",
627
    "ffm",
628
    sizeof(FFMContext) + FFM_PACKET_SIZE,
629
    /* not really used */
630
    CODEC_ID_MP2,
631
    CODEC_ID_MPEG1VIDEO,
632
    ffm_write_header,
633
    ffm_write_packet,
634
    ffm_write_trailer,
635
};
636

    
637
int ffm_init(void)
638
{
639
    av_register_input_format(&ffm_iformat);
640
    av_register_output_format(&ffm_oformat);
641
    return 0;
642
}