ffm.c 20.9 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * FFM (ffserver live feed) encoder and decoder
3
 * Copyright (c) 2001 Fabrice Bellard.
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5 6 7 8
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
9
 *
10
 * This library is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
14
 *
15 16 17
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Fabrice Bellard's avatar
Fabrice Bellard committed
18 19
 */
#include "avformat.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
20
#include <unistd.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
21 22 23 24 25 26

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

/* each packet contains frames (which can span several packets */
27
#define FRAME_HEADER_SIZE    8
Fabrice Bellard's avatar
Fabrice Bellard committed
28 29 30
#define FLAG_KEY_FRAME       0x01

typedef struct FFMStream {
31
    int64_t pts;
Fabrice Bellard's avatar
Fabrice Bellard committed
32 33 34 35 36 37 38 39 40 41
} FFMStream;

enum {
    READ_HEADER,
    READ_DATA,
};

typedef struct FFMContext {
    /* only reading mode */
    offset_t write_index, file_size;
42
    int read_state;
43
    uint8_t header[FRAME_HEADER_SIZE];
Fabrice Bellard's avatar
Fabrice Bellard committed
44 45 46 47 48

    /* read and write */
    int first_packet; /* true if first packet, needed to set the discontinuity tag */
    int packet_size;
    int frame_offset;
49 50 51
    int64_t pts;
    uint8_t *packet_ptr, *packet_end;
    uint8_t packet[FFM_PACKET_SIZE];
Fabrice Bellard's avatar
Fabrice Bellard committed
52 53
} FFMContext;

54 55
static int64_t get_pts(AVFormatContext *s, offset_t pos);

Fabrice Bellard's avatar
Fabrice Bellard committed
56 57 58
/* disable pts hack for testing */
int ffm_nopts = 0;

59
#ifdef CONFIG_ENCODERS
Fabrice Bellard's avatar
Fabrice Bellard committed
60 61 62 63 64 65 66 67
static void flush_packet(AVFormatContext *s)
{
    FFMContext *ffm = s->priv_data;
    int fill_size, h;
    ByteIOContext *pb = &s->pb;

    fill_size = ffm->packet_end - ffm->packet_ptr;
    memset(ffm->packet_ptr, 0, fill_size);
68

69 70 71
    if (url_ftell(pb) % ffm->packet_size) 
        av_abort();

Fabrice Bellard's avatar
Fabrice Bellard committed
72
    /* put header */
73
    put_be16(pb, PACKET_ID);
Fabrice Bellard's avatar
Fabrice Bellard committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    put_be16(pb, fill_size);
    put_be64(pb, ffm->pts);
    h = ffm->frame_offset;
    if (ffm->first_packet)
        h |= 0x8000;
    put_be16(pb, h);
    put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);

    /* prepare next packet */
    ffm->frame_offset = 0; /* no key frame */
    ffm->pts = 0; /* no pts */
    ffm->packet_ptr = ffm->packet;
    ffm->first_packet = 0;
}

/* 'first' is true if first data of a frame */
90
static void ffm_write_data(AVFormatContext *s,
91
                           const uint8_t *buf, int size,
92
                           int64_t pts, int first)
Fabrice Bellard's avatar
Fabrice Bellard committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
{
    FFMContext *ffm = s->priv_data;
    int len;

    if (first && ffm->frame_offset == 0)
        ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
    if (first && ffm->pts == 0)
        ffm->pts = pts;

    /* write as many packets as needed */
    while (size > 0) {
        len = ffm->packet_end - ffm->packet_ptr;
        if (len > size)
            len = size;
        memcpy(ffm->packet_ptr, buf, len);
108

Fabrice Bellard's avatar
Fabrice Bellard committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        ffm->packet_ptr += len;
        buf += len;
        size -= len;
        if (ffm->packet_ptr >= ffm->packet_end) {
            /* special case : no pts in packet : we leave the current one */
            if (ffm->pts == 0)
                ffm->pts = pts;

            flush_packet(s);
        }
    }
}

static int ffm_write_header(AVFormatContext *s)
{
Fabrice Bellard's avatar
Fabrice Bellard committed
124
    FFMContext *ffm = s->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
125 126 127 128 129 130 131
    AVStream *st;
    FFMStream *fst;
    ByteIOContext *pb = &s->pb;
    AVCodecContext *codec;
    int bit_rate, i;

    ffm->packet_size = FFM_PACKET_SIZE;
132

Fabrice Bellard's avatar
Fabrice Bellard committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    /* header */
    put_tag(pb, "FFM1");
    put_be32(pb, ffm->packet_size);
    /* XXX: store write position in other file ? */
    put_be64(pb, ffm->packet_size); /* current write position */

    put_be32(pb, s->nb_streams);
    bit_rate = 0;
    for(i=0;i<s->nb_streams;i++) {
        st = s->streams[i];
        bit_rate += st->codec.bit_rate;
    }
    put_be32(pb, bit_rate);

    /* list of streams */
    for(i=0;i<s->nb_streams;i++) {
        st = s->streams[i];
150
        fst = av_mallocz(sizeof(FFMStream));
Fabrice Bellard's avatar
Fabrice Bellard committed
151 152
        if (!fst)
            goto fail;
153
        av_set_pts_info(st, 64, 1, 1000000);
Fabrice Bellard's avatar
Fabrice Bellard committed
154 155 156 157 158 159 160
        st->priv_data = fst;

        codec = &st->codec;
        /* generic info */
        put_be32(pb, codec->codec_id);
        put_byte(pb, codec->codec_type);
        put_be32(pb, codec->bit_rate);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
161
	put_be32(pb, st->quality);
162
        put_be32(pb, codec->flags);
Fabrice Bellard's avatar
Fabrice Bellard committed
163 164 165
        /* specific info */
        switch(codec->codec_type) {
        case CODEC_TYPE_VIDEO:
166 167
            put_be32(pb, codec->frame_rate_base);
            put_be32(pb, codec->frame_rate);
Fabrice Bellard's avatar
Fabrice Bellard committed
168 169
            put_be16(pb, codec->width);
            put_be16(pb, codec->height);
170 171 172 173 174 175
            put_be16(pb, codec->gop_size);
            put_byte(pb, codec->qmin);
            put_byte(pb, codec->qmax);
            put_byte(pb, codec->max_qdiff);
            put_be16(pb, (int) (codec->qcompress * 10000.0));
            put_be16(pb, (int) (codec->qblur * 10000.0));
176
            put_be32(pb, codec->bit_rate_tolerance);
177
            put_strz(pb, codec->rc_eq);
178 179 180
            put_be32(pb, codec->rc_max_rate);
            put_be32(pb, codec->rc_min_rate);
            put_be32(pb, codec->rc_buffer_size);
181 182 183 184
            put_be64_double(pb, codec->i_quant_factor);
            put_be64_double(pb, codec->b_quant_factor);
            put_be64_double(pb, codec->i_quant_offset);
            put_be64_double(pb, codec->b_quant_offset);
185
            put_be32(pb, codec->dct_algo);
Fabrice Bellard's avatar
Fabrice Bellard committed
186 187 188 189
            break;
        case CODEC_TYPE_AUDIO:
            put_be32(pb, codec->sample_rate);
            put_le16(pb, codec->channels);
190
            put_le16(pb, codec->frame_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
191
            break;
192
        default:
Michael Niedermayer's avatar
Michael Niedermayer committed
193
            return -1;
Fabrice Bellard's avatar
Fabrice Bellard committed
194 195
        }
        /* hack to have real time */
Fabrice Bellard's avatar
Fabrice Bellard committed
196 197 198
        if (ffm_nopts)
            fst->pts = 0;
        else
199
            fst->pts = av_gettime();
Fabrice Bellard's avatar
Fabrice Bellard committed
200 201 202 203 204 205 206 207 208 209 210
    }

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

    put_flush_packet(pb);

    /* init packet mux */
    ffm->packet_ptr = ffm->packet;
    ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
Michael Niedermayer's avatar
Michael Niedermayer committed
211
    assert(ffm->packet_end >= ffm->packet);
Fabrice Bellard's avatar
Fabrice Bellard committed
212 213 214 215 216 217 218 219
    ffm->frame_offset = 0;
    ffm->pts = 0;
    ffm->first_packet = 1;

    return 0;
 fail:
    for(i=0;i<s->nb_streams;i++) {
        st = s->streams[i];
220
        av_freep(&st->priv_data);
Fabrice Bellard's avatar
Fabrice Bellard committed
221 222 223 224
    }
    return -1;
}

225
static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
Fabrice Bellard's avatar
Fabrice Bellard committed
226
{
227
    AVStream *st = s->streams[pkt->stream_index];
Fabrice Bellard's avatar
Fabrice Bellard committed
228
    FFMStream *fst = st->priv_data;
229 230
    int64_t pts;
    uint8_t header[FRAME_HEADER_SIZE];
231
    int duration;
232
    int size= pkt->size;
233

234
    //XXX/FIXME use duration from pkt
235 236 237
    if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
        duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
    } else {
238
        duration = (1000000.0 * st->codec.frame_rate_base / (float)st->codec.frame_rate);
239
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
240 241 242

    pts = fst->pts;
    /* packet size & key_frame */
243
    header[0] = pkt->stream_index;
Fabrice Bellard's avatar
Fabrice Bellard committed
244
    header[1] = 0;
245
    if (pkt->flags & PKT_FLAG_KEY)
Fabrice Bellard's avatar
Fabrice Bellard committed
246 247 248 249
        header[1] |= FLAG_KEY_FRAME;
    header[2] = (size >> 16) & 0xff;
    header[3] = (size >> 8) & 0xff;
    header[4] = size & 0xff;
250 251 252
    header[5] = (duration >> 16) & 0xff;
    header[6] = (duration >> 8) & 0xff;
    header[7] = duration & 0xff;
Fabrice Bellard's avatar
Fabrice Bellard committed
253
    ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
254
    ffm_write_data(s, pkt->data, size, pts, 0);
Fabrice Bellard's avatar
Fabrice Bellard committed
255

256
    fst->pts += duration;
Fabrice Bellard's avatar
Fabrice Bellard committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270
    return 0;
}

static int ffm_write_trailer(AVFormatContext *s)
{
    ByteIOContext *pb = &s->pb;
    FFMContext *ffm = s->priv_data;

    /* flush packets */
    if (ffm->packet_ptr > ffm->packet)
        flush_packet(s);

    put_flush_packet(pb);

271
    if (!url_is_streamed(pb)) {
272
        int64_t size;
273 274 275 276 277 278 279
        /* update the write offset */
        size = url_ftell(pb);
        url_fseek(pb, 8, SEEK_SET);
        put_be64(pb, size);
        put_flush_packet(pb);
    }

Fabrice Bellard's avatar
Fabrice Bellard committed
280 281
    return 0;
}
282
#endif //CONFIG_ENCODERS
Fabrice Bellard's avatar
Fabrice Bellard committed
283 284 285 286 287 288 289 290 291 292

/* ffm demux */

static int ffm_is_avail_data(AVFormatContext *s, int size)
{
    FFMContext *ffm = s->priv_data;
    offset_t pos, avail_size;
    int len;

    len = ffm->packet_end - ffm->packet_ptr;
293 294 295 296 297
    if (!ffm_nopts) {
        /* XXX: I don't understand this test, so I disabled it for testing */
        if (size <= len)
            return 1;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    pos = url_ftell(&s->pb);
    if (pos == ffm->write_index) {
        /* exactly at the end of stream */
        return 0;
    } else if (pos < ffm->write_index) {
        avail_size = ffm->write_index - pos;
    } else {
        avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
    }
    avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
    if (size <= avail_size)
        return 1;
    else
        return 0;
}

/* first is true if we read the frame header */
315
static int ffm_read_data(AVFormatContext *s,
316
                         uint8_t *buf, int size, int first)
Fabrice Bellard's avatar
Fabrice Bellard committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330
{
    FFMContext *ffm = s->priv_data;
    ByteIOContext *pb = &s->pb;
    int len, fill_size, size1, frame_offset;

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


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


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

    pts_start = get_pts(s, pos_min);

    pts = get_pts(s, pos_max);

    if (pts - 100000 > pts_start) 
392
        goto end;
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

    ffm->write_index = FFM_PACKET_SIZE;

    pts_start = get_pts(s, pos_min);

    pts = get_pts(s, pos_max);

    if (pts - 100000 <= pts_start) {
        while (1) {
            offset_t newpos;
            int64_t newpts;

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

            if (newpos == pos_min)
                break;

            newpts = get_pts(s, newpos);

            if (newpts - 100000 <= pts) {
                pos_max = newpos;
                pts = newpts;
            } else {
                pos_min = newpos;
            }
        }
        ffm->write_index += pos_max;
    }

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

425
 end:
426 427 428 429
    url_fseek(pb, ptr, SEEK_SET);
}


Fabrice Bellard's avatar
Fabrice Bellard committed
430 431
static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
Fabrice Bellard's avatar
Fabrice Bellard committed
432
    FFMContext *ffm = s->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
433 434 435 436
    AVStream *st;
    FFMStream *fst;
    ByteIOContext *pb = &s->pb;
    AVCodecContext *codec;
Fabrice Bellard's avatar
Fabrice Bellard committed
437
    int i, nb_streams;
438
    uint32_t tag;
Fabrice Bellard's avatar
Fabrice Bellard committed
439 440 441 442 443 444 445 446 447 448 449 450

    /* header */
    tag = get_le32(pb);
    if (tag != MKTAG('F', 'F', 'M', '1'))
        goto fail;
    ffm->packet_size = get_be32(pb);
    if (ffm->packet_size != FFM_PACKET_SIZE)
        goto fail;
    ffm->write_index = get_be64(pb);
    /* get also filesize */
    if (!url_is_streamed(pb)) {
        ffm->file_size = url_filesize(url_fileno(pb));
451
        adjust_write_index(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
452
    } else {
453
        ffm->file_size = (uint64_t_C(1) << 63) - 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
454 455
    }

Fabrice Bellard's avatar
Fabrice Bellard committed
456
    nb_streams = get_be32(pb);
Fabrice Bellard's avatar
Fabrice Bellard committed
457 458
    get_be32(pb); /* total bitrate */
    /* read each stream */
Fabrice Bellard's avatar
Fabrice Bellard committed
459
    for(i=0;i<nb_streams;i++) {
460 461
        char rc_eq_buf[128];

Fabrice Bellard's avatar
Fabrice Bellard committed
462
        st = av_new_stream(s, 0);
Fabrice Bellard's avatar
Fabrice Bellard committed
463 464
        if (!st)
            goto fail;
465
        fst = av_mallocz(sizeof(FFMStream));
Fabrice Bellard's avatar
Fabrice Bellard committed
466 467
        if (!fst)
            goto fail;
468 469 470
            
        av_set_pts_info(st, 64, 1, 1000000);
            
Fabrice Bellard's avatar
Fabrice Bellard committed
471 472 473 474 475 476 477
        st->priv_data = fst;

        codec = &st->codec;
        /* generic info */
        st->codec.codec_id = get_be32(pb);
        st->codec.codec_type = get_byte(pb); /* codec_type */
        codec->bit_rate = get_be32(pb);
Michael Niedermayer's avatar
cleanup  
Michael Niedermayer committed
478
	st->quality = get_be32(pb);
479
        codec->flags = get_be32(pb);
Fabrice Bellard's avatar
Fabrice Bellard committed
480 481 482
        /* specific info */
        switch(codec->codec_type) {
        case CODEC_TYPE_VIDEO:
483 484
            codec->frame_rate_base = get_be32(pb);
            codec->frame_rate = get_be32(pb);
Fabrice Bellard's avatar
Fabrice Bellard committed
485 486
            codec->width = get_be16(pb);
            codec->height = get_be16(pb);
487 488 489 490 491 492
            codec->gop_size = get_be16(pb);
            codec->qmin = get_byte(pb);
            codec->qmax = get_byte(pb);
            codec->max_qdiff = get_byte(pb);
            codec->qcompress = get_be16(pb) / 10000.0;
            codec->qblur = get_be16(pb) / 10000.0;
493
            codec->bit_rate_tolerance = get_be32(pb);
Fabrice Bellard's avatar
Fabrice Bellard committed
494
            codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
495 496 497
            codec->rc_max_rate = get_be32(pb);
            codec->rc_min_rate = get_be32(pb);
            codec->rc_buffer_size = get_be32(pb);
498 499 500 501
            codec->i_quant_factor = get_be64_double(pb);
            codec->b_quant_factor = get_be64_double(pb);
            codec->i_quant_offset = get_be64_double(pb);
            codec->b_quant_offset = get_be64_double(pb);
502
            codec->dct_algo = get_be32(pb);
Fabrice Bellard's avatar
Fabrice Bellard committed
503 504 505 506
            break;
        case CODEC_TYPE_AUDIO:
            codec->sample_rate = get_be32(pb);
            codec->channels = get_le16(pb);
507
            codec->frame_size = get_le16(pb);
Fabrice Bellard's avatar
Fabrice Bellard committed
508
            break;
509
        default:
510
            goto fail;
Fabrice Bellard's avatar
Fabrice Bellard committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
        }

    }

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

    /* init packet demux */
    ffm->packet_ptr = ffm->packet;
    ffm->packet_end = ffm->packet;
    ffm->frame_offset = 0;
    ffm->pts = 0;
    ffm->read_state = READ_HEADER;
    ffm->first_packet = 1;
    return 0;
 fail:
    for(i=0;i<s->nb_streams;i++) {
        st = s->streams[i];
        if (st) {
531
            av_freep(&st->priv_data);
532
            av_free(st);
Fabrice Bellard's avatar
Fabrice Bellard committed
533 534 535 536 537 538 539 540 541 542
        }
    }
    return -1;
}

/* return < 0 if eof */
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    int size;
    FFMContext *ffm = s->priv_data;
543
    int duration;
Fabrice Bellard's avatar
Fabrice Bellard committed
544 545 546

    switch(ffm->read_state) {
    case READ_HEADER:
547
        if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
Fabrice Bellard's avatar
Fabrice Bellard committed
548
            return -EAGAIN;
549
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
550
#if 0
551
        printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
Fabrice Bellard's avatar
Fabrice Bellard committed
552 553
               url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
#endif
554 555
        if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) != 
            FRAME_HEADER_SIZE)
Fabrice Bellard's avatar
Fabrice Bellard committed
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
            return -EAGAIN;
#if 0
        {
            int i;
            for(i=0;i<FRAME_HEADER_SIZE;i++)
                printf("%02x ", ffm->header[i]);
            printf("\n");
        }
#endif
        ffm->read_state = READ_DATA;
        /* fall thru */
    case READ_DATA:
        size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
        if (!ffm_is_avail_data(s, size)) {
            return -EAGAIN;
        }

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

Fabrice Bellard's avatar
Fabrice Bellard committed
575 576 577 578
        av_new_packet(pkt, size);
        pkt->stream_index = ffm->header[0];
        if (ffm->header[1] & FLAG_KEY_FRAME)
            pkt->flags |= PKT_FLAG_KEY;
579

Fabrice Bellard's avatar
Fabrice Bellard committed
580 581 582 583 584 585
        ffm->read_state = READ_HEADER;
        if (ffm_read_data(s, pkt->data, size, 0) != size) {
            /* bad case: desynchronized packet. we cancel all the packet loading */
            av_free_packet(pkt);
            return -EAGAIN;
        }
586 587
        pkt->pts = ffm->pts;
        pkt->duration = duration;
Fabrice Bellard's avatar
Fabrice Bellard committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
        break;
    }
    return 0;
}

//#define DEBUG_SEEK

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

    pos = pos1 + ffm->write_index;
    if (pos >= ffm->file_size)
        pos -= (ffm->file_size - FFM_PACKET_SIZE);
#ifdef DEBUG_SEEK
    printf("seek to %Lx -> %Lx\n", pos1, pos);
#endif
    url_fseek(pb, pos, SEEK_SET);
}

612
static int64_t get_pts(AVFormatContext *s, offset_t pos)
Fabrice Bellard's avatar
Fabrice Bellard committed
613 614
{
    ByteIOContext *pb = &s->pb;
615
    int64_t pts;
Fabrice Bellard's avatar
Fabrice Bellard committed
616

617
    ffm_seek1(s, pos);
Fabrice Bellard's avatar
Fabrice Bellard committed
618 619 620 621 622 623 624 625 626 627 628
    url_fskip(pb, 4);
    pts = get_be64(pb);
#ifdef DEBUG_SEEK
    printf("pts=%0.6f\n", pts / 1000000.0);
#endif
    return pts;
}

/* seek to a given time in the file. The file read pointer is
   positionned at or before pts. XXX: the following code is quite
   approximative */
629
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
Fabrice Bellard's avatar
Fabrice Bellard committed
630 631 632
{
    FFMContext *ffm = s->priv_data;
    offset_t pos_min, pos_max, pos;
633
    int64_t pts_min, pts_max, pts;
Fabrice Bellard's avatar
Fabrice Bellard committed
634 635 636 637 638 639 640 641 642 643 644 645 646
    double pos1;

#ifdef DEBUG_SEEK
    printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
#endif
    /* find the position using linear interpolation (better than
       dichotomy in typical cases) */
    pos_min = 0;
    pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
    while (pos_min <= pos_max) {
        pts_min = get_pts(s, pos_min);
        pts_max = get_pts(s, pos_max);
        /* linear interpolation */
647
        pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
Fabrice Bellard's avatar
Fabrice Bellard committed
648
            (double)(pts_max - pts_min);
649
        pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
Fabrice Bellard's avatar
Fabrice Bellard committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663
        if (pos <= pos_min)
            pos = pos_min;
        else if (pos >= pos_max)
            pos = pos_max;
        pts = get_pts(s, pos);
        /* check if we are lucky */
        if (pts == wanted_pts) {
            goto found;
        } else if (pts > wanted_pts) {
            pos_max = pos - FFM_PACKET_SIZE;
        } else {
            pos_min = pos + FFM_PACKET_SIZE;
        }
    }
664
    pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
Fabrice Bellard's avatar
Fabrice Bellard committed
665 666 667 668 669 670 671 672 673
    if (pos > 0)
        pos -= FFM_PACKET_SIZE;
 found:
    ffm_seek1(s, pos);
    return 0;
}

offset_t ffm_read_write_index(int fd)
{
674
    uint8_t buf[8];
Fabrice Bellard's avatar
Fabrice Bellard committed
675 676 677 678 679 680
    offset_t pos;
    int i;

    lseek(fd, 8, SEEK_SET);
    read(fd, buf, 8);
    pos = 0;
681
    for(i=0;i<8;i++)
Fabrice Bellard's avatar
Fabrice Bellard committed
682
        pos |= (int64_t)buf[i] << (56 - i * 8);
Fabrice Bellard's avatar
Fabrice Bellard committed
683 684 685 686 687
    return pos;
}

void ffm_write_write_index(int fd, offset_t pos)
{
688
    uint8_t buf[8];
Fabrice Bellard's avatar
Fabrice Bellard committed
689 690
    int i;

691
    for(i=0;i<8;i++)
Fabrice Bellard's avatar
Fabrice Bellard committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
        buf[i] = (pos >> (56 - i * 8)) & 0xff;
    lseek(fd, 8, SEEK_SET);
    write(fd, buf, 8);
}

void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
{
    FFMContext *ffm = s->priv_data;
    ffm->write_index = pos;
    ffm->file_size = file_size;
}

static int ffm_read_close(AVFormatContext *s)
{
    AVStream *st;
    int i;

    for(i=0;i<s->nb_streams;i++) {
        st = s->streams[i];
711
        av_freep(&st->priv_data);
Fabrice Bellard's avatar
Fabrice Bellard committed
712 713 714 715
    }
    return 0;
}

716 717
static int ffm_probe(AVProbeData *p)
{
718 719 720
    if (p->buf_size >= 4 &&
        p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' && 
        p->buf[3] == '1')
721 722 723 724
        return AVPROBE_SCORE_MAX + 1;
    return 0;
}

Zdenek Kabelac's avatar
Zdenek Kabelac committed
725
static AVInputFormat ffm_iformat = {
Fabrice Bellard's avatar
Fabrice Bellard committed
726 727
    "ffm",
    "ffm format",
728
    sizeof(FFMContext),
729
    ffm_probe,
Fabrice Bellard's avatar
Fabrice Bellard committed
730 731 732 733 734 735
    ffm_read_header,
    ffm_read_packet,
    ffm_read_close,
    ffm_seek,
};

736
#ifdef CONFIG_ENCODERS
Zdenek Kabelac's avatar
Zdenek Kabelac committed
737
static AVOutputFormat ffm_oformat = {
Fabrice Bellard's avatar
Fabrice Bellard committed
738 739 740 741
    "ffm",
    "ffm format",
    "",
    "ffm",
742
    sizeof(FFMContext),
Fabrice Bellard's avatar
Fabrice Bellard committed
743 744 745 746 747 748 749
    /* not really used */
    CODEC_ID_MP2,
    CODEC_ID_MPEG1VIDEO,
    ffm_write_header,
    ffm_write_packet,
    ffm_write_trailer,
};
750
#endif //CONFIG_ENCODERS
Fabrice Bellard's avatar
Fabrice Bellard committed
751 752 753 754

int ffm_init(void)
{
    av_register_input_format(&ffm_iformat);
755
#ifdef CONFIG_ENCODERS
Fabrice Bellard's avatar
Fabrice Bellard committed
756
    av_register_output_format(&ffm_oformat);
757
#endif //CONFIG_ENCODERS
Fabrice Bellard's avatar
Fabrice Bellard committed
758 759
    return 0;
}