Statistics
| Branch: | Revision:

ffmpeg / libavformat / rm.c @ 5aa0a644

History | View | Annotate | Download (26 KB)

1
/*
2
 * "Real" compatible mux and demux.
3
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library 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 GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
#include "avformat.h"
20

    
21
/* in ms */
22
#define BUFFER_DURATION 0 
23

    
24
typedef struct {
25
    int nb_packets;
26
    int packet_total_size;
27
    int packet_max_size;
28
    /* codec related output */
29
    int bit_rate;
30
    float frame_rate;
31
    int nb_frames;    /* current frame number */
32
    int total_frames; /* total number of frames */
33
    int num;
34
    AVCodecContext *enc;
35
} StreamInfo;
36

    
37
typedef struct {
38
    StreamInfo streams[2];
39
    StreamInfo *audio_stream, *video_stream;
40
    int data_pos; /* position of the data after the header */
41
    int nb_packets;
42
    int old_format;
43
} RMContext;
44

    
45
static void put_str(ByteIOContext *s, const char *tag)
46
{
47
    put_be16(s,strlen(tag));
48
    while (*tag) {
49
        put_byte(s, *tag++);
50
    }
51
}
52

    
53
static void put_str8(ByteIOContext *s, const char *tag)
54
{
55
    put_byte(s, strlen(tag));
56
    while (*tag) {
57
        put_byte(s, *tag++);
58
    }
59
}
60

    
61
static void rv10_write_header(AVFormatContext *ctx, 
62
                              int data_size, int index_pos)
63
{
64
    RMContext *rm = ctx->priv_data;
65
    ByteIOContext *s = &ctx->pb;
66
    StreamInfo *stream;
67
    unsigned char *data_offset_ptr, *start_ptr;
68
    const char *desc, *mimetype;
69
    int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
70
    int bit_rate, v, duration, flags, data_pos;
71

    
72
    start_ptr = s->buf_ptr;
73

    
74
    put_tag(s, ".RMF");
75
    put_be32(s,18); /* header size */
76
    put_be16(s,0);
77
    put_be32(s,0);
78
    put_be32(s,4 + ctx->nb_streams); /* num headers */
79

    
80
    put_tag(s,"PROP");
81
    put_be32(s, 50);
82
    put_be16(s, 0);
83
    packet_max_size = 0;
84
    packet_total_size = 0;
85
    nb_packets = 0;
86
    bit_rate = 0;
87
    duration = 0;
88
    for(i=0;i<ctx->nb_streams;i++) {
89
        StreamInfo *stream = &rm->streams[i];
90
        bit_rate += stream->bit_rate;
91
        if (stream->packet_max_size > packet_max_size)
92
            packet_max_size = stream->packet_max_size;
93
        nb_packets += stream->nb_packets;
94
        packet_total_size += stream->packet_total_size;
95
        /* select maximum duration */
96
        v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
97
        if (v > duration)
98
            duration = v;
99
    }
100
    put_be32(s, bit_rate); /* max bit rate */
101
    put_be32(s, bit_rate); /* avg bit rate */
102
    put_be32(s, packet_max_size);        /* max packet size */
103
    if (nb_packets > 0)
104
        packet_avg_size = packet_total_size / nb_packets;
105
    else
106
        packet_avg_size = 0;
107
    put_be32(s, packet_avg_size);        /* avg packet size */
108
    put_be32(s, nb_packets);  /* num packets */
109
    put_be32(s, duration); /* duration */
110
    put_be32(s, BUFFER_DURATION);           /* preroll */
111
    put_be32(s, index_pos);           /* index offset */
112
    /* computation of data the data offset */
113
    data_offset_ptr = s->buf_ptr;
114
    put_be32(s, 0);           /* data offset : will be patched after */
115
    put_be16(s, ctx->nb_streams);    /* num streams */
116
    flags = 1 | 2; /* save allowed & perfect play */
117
    if (url_is_streamed(s))
118
        flags |= 4; /* live broadcast */
119
    put_be16(s, flags);
120
    
121
    /* comments */
122

    
123
    put_tag(s,"CONT");
124
    size = strlen(ctx->title) + strlen(ctx->author) + strlen(ctx->copyright) + 
125
        strlen(ctx->comment) + 4 * 2 + 10;
126
    put_be32(s,size);
127
    put_be16(s,0);
128
    put_str(s, ctx->title);
129
    put_str(s, ctx->author);
130
    put_str(s, ctx->copyright);
131
    put_str(s, ctx->comment);
132
    
133
    for(i=0;i<ctx->nb_streams;i++) {
134
        int codec_data_size;
135

    
136
        stream = &rm->streams[i];
137
        
138
        if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
139
            desc = "The Audio Stream";
140
            mimetype = "audio/x-pn-realaudio";
141
            codec_data_size = 73;
142
        } else {
143
            desc = "The Video Stream";
144
            mimetype = "video/x-pn-realvideo";
145
            codec_data_size = 34;
146
        }
147

    
148
        put_tag(s,"MDPR");
149
        size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
150
        put_be32(s, size);
151
        put_be16(s, 0);
152

    
153
        put_be16(s, i); /* stream number */
154
        put_be32(s, stream->bit_rate); /* max bit rate */
155
        put_be32(s, stream->bit_rate); /* avg bit rate */
156
        put_be32(s, stream->packet_max_size);        /* max packet size */
157
        if (stream->nb_packets > 0)
158
            packet_avg_size = stream->packet_total_size / 
159
                stream->nb_packets;
160
        else
161
            packet_avg_size = 0;
162
        put_be32(s, packet_avg_size);        /* avg packet size */
163
        put_be32(s, 0);           /* start time */
164
        put_be32(s, BUFFER_DURATION);           /* preroll */
165
        /* duration */
166
        if (url_is_streamed(s) || !stream->total_frames)
167
            put_be32(s, (int)(3600 * 1000));
168
        else
169
            put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
170
        put_str8(s, desc);
171
        put_str8(s, mimetype);
172
        put_be32(s, codec_data_size);
173
        
174
        if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
175
            int coded_frame_size, fscode, sample_rate;
176
            sample_rate = stream->enc->sample_rate;
177
            coded_frame_size = (stream->enc->bit_rate * 
178
                                stream->enc->frame_size) / (8 * sample_rate);
179
            /* audio codec info */
180
            put_tag(s, ".ra");
181
            put_byte(s, 0xfd);
182
            put_be32(s, 0x00040000); /* version */
183
            put_tag(s, ".ra4");
184
            put_be32(s, 0x01b53530); /* stream length */
185
            put_be16(s, 4); /* unknown */
186
            put_be32(s, 0x39); /* header size */
187

    
188
            switch(sample_rate) {
189
            case 48000:
190
            case 24000:
191
            case 12000:
192
                fscode = 1;
193
                break;
194
            default:
195
            case 44100:
196
            case 22050:
197
            case 11025:
198
                fscode = 2;
199
                break;
200
            case 32000:
201
            case 16000:
202
            case 8000:
203
                fscode = 3;
204
            }
205
            put_be16(s, fscode); /* codec additional info, for AC3, seems
206
                                     to be a frequency code */
207
            /* special hack to compensate rounding errors... */
208
            if (coded_frame_size == 557)
209
                coded_frame_size--;
210
            put_be32(s, coded_frame_size); /* frame length */
211
            put_be32(s, 0x51540); /* unknown */
212
            put_be32(s, 0x249f0); /* unknown */
213
            put_be32(s, 0x249f0); /* unknown */
214
            put_be16(s, 0x01);
215
            /* frame length : seems to be very important */
216
            put_be16(s, coded_frame_size); 
217
            put_be32(s, 0); /* unknown */
218
            put_be16(s, stream->enc->sample_rate); /* sample rate */
219
            put_be32(s, 0x10); /* unknown */
220
            put_be16(s, stream->enc->channels);
221
            put_str8(s, "Int0"); /* codec name */
222
            put_str8(s, "dnet"); /* codec name */
223
            put_be16(s, 0); /* title length */
224
            put_be16(s, 0); /* author length */
225
            put_be16(s, 0); /* copyright length */
226
            put_byte(s, 0); /* end of header */
227
        } else {
228
            /* video codec info */
229
            put_be32(s,34); /* size */
230
            put_tag(s,"VIDORV10");
231
            put_be16(s, stream->enc->width);
232
            put_be16(s, stream->enc->height);
233
            put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */
234
            put_be32(s,0);     /* unknown meaning */
235
            put_be16(s, (int) stream->frame_rate);  /* unknown meaning */
236
            put_be32(s,0);     /* unknown meaning */
237
            put_be16(s, 8);    /* unknown meaning */
238
            /* Seems to be the codec version: only use basic H263. The next
239
               versions seems to add a diffential DC coding as in
240
               MPEG... nothing new under the sun */
241
            put_be32(s,0x10000000); 
242
            //put_be32(s,0x10003000); 
243
        }
244
    }
245

    
246
    /* patch data offset field */
247
    data_pos = s->buf_ptr - start_ptr;
248
    rm->data_pos = data_pos;
249
    data_offset_ptr[0] = data_pos >> 24;
250
    data_offset_ptr[1] = data_pos >> 16;
251
    data_offset_ptr[2] = data_pos >> 8;
252
    data_offset_ptr[3] = data_pos;
253
    
254
    /* data stream */
255
    put_tag(s,"DATA");
256
    put_be32(s,data_size + 10 + 8);
257
    put_be16(s,0);
258

    
259
    put_be32(s, nb_packets); /* number of packets */
260
    put_be32(s,0); /* next data header */
261
}
262

    
263
static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream, 
264
                                int length, int key_frame)
265
{
266
    int timestamp;
267
    ByteIOContext *s = &ctx->pb;
268

    
269
    stream->nb_packets++;
270
    stream->packet_total_size += length;
271
    if (length > stream->packet_max_size)
272
        stream->packet_max_size =  length;
273

    
274
    put_be16(s,0); /* version */
275
    put_be16(s,length + 12);
276
    put_be16(s, stream->num); /* stream number */
277
    timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
278
    put_be32(s, timestamp); /* timestamp */
279
    put_byte(s, 0); /* reserved */
280
    put_byte(s, key_frame ? 2 : 0); /* flags */
281
}
282

    
283
static int rm_write_header(AVFormatContext *s)
284
{
285
    RMContext *rm = s->priv_data;
286
    StreamInfo *stream;
287
    int n;
288
    AVCodecContext *codec;
289

    
290
    for(n=0;n<s->nb_streams;n++) {
291
        s->streams[n]->id = n;
292
        codec = &s->streams[n]->codec;
293
        stream = &rm->streams[n];
294
        memset(stream, 0, sizeof(StreamInfo));
295
        stream->num = n;
296
        stream->bit_rate = codec->bit_rate;
297
        stream->enc = codec;
298

    
299
        switch(codec->codec_type) {
300
        case CODEC_TYPE_AUDIO:
301
            rm->audio_stream = stream;
302
            stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
303
            /* XXX: dummy values */
304
            stream->packet_max_size = 1024;
305
            stream->nb_packets = 0;
306
            stream->total_frames = stream->nb_packets;
307
            break;
308
        case CODEC_TYPE_VIDEO:
309
            rm->video_stream = stream;
310
            stream->frame_rate = (float)codec->frame_rate / (float)codec->frame_rate_base;
311
            /* XXX: dummy values */
312
            stream->packet_max_size = 4096;
313
            stream->nb_packets = 0;
314
            stream->total_frames = stream->nb_packets;
315
            break;
316
        default:
317
            av_abort();
318
        }
319
    }
320

    
321
    rv10_write_header(s, 0, 0);
322
    put_flush_packet(&s->pb);
323
    return 0;
324
}
325

    
326
static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size)
327
{
328
    uint8_t *buf1;
329
    RMContext *rm = s->priv_data;
330
    ByteIOContext *pb = &s->pb;
331
    StreamInfo *stream = rm->audio_stream;
332
    int i;
333

    
334
    /* XXX: suppress this malloc */
335
    buf1= (uint8_t*) av_malloc( size * sizeof(uint8_t) );
336
    
337
    write_packet_header(s, stream, size, stream->enc->coded_frame->key_frame);
338
    
339
    /* for AC3, the words seems to be reversed */
340
    for(i=0;i<size;i+=2) {
341
        buf1[i] = buf[i+1];
342
        buf1[i+1] = buf[i];
343
    }
344
    put_buffer(pb, buf1, size);
345
    put_flush_packet(pb);
346
    stream->nb_frames++;
347
    av_free(buf1);
348
    return 0;
349
}
350

    
351
static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size)
352
{
353
    RMContext *rm = s->priv_data;
354
    ByteIOContext *pb = &s->pb;
355
    StreamInfo *stream = rm->video_stream;
356
    int key_frame = stream->enc->coded_frame->key_frame;
357

    
358
    /* XXX: this is incorrect: should be a parameter */
359

    
360
    /* Well, I spent some time finding the meaning of these bits. I am
361
       not sure I understood everything, but it works !! */
362
#if 1
363
    write_packet_header(s, stream, size + 7, key_frame);
364
    /* bit 7: '1' if final packet of a frame converted in several packets */
365
    put_byte(pb, 0x81); 
366
    /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
367
       frame starting from 1 */
368
    if (key_frame) {
369
        put_byte(pb, 0x81); 
370
    } else {
371
        put_byte(pb, 0x01); 
372
    }
373
    put_be16(pb, 0x4000 | (size)); /* total frame size */
374
    put_be16(pb, 0x4000 | (size));              /* offset from the start or the end */
375
#else
376
    /* full frame */
377
    write_packet_header(s, size + 6);
378
    put_byte(pb, 0xc0); 
379
    put_be16(pb, 0x4000 | size); /* total frame size */
380
    put_be16(pb, 0x4000 + packet_number * 126); /* position in stream */
381
#endif
382
    put_byte(pb, stream->nb_frames & 0xff); 
383
    
384
    put_buffer(pb, buf, size);
385
    put_flush_packet(pb);
386

    
387
    stream->nb_frames++;
388
    return 0;
389
}
390

    
391
static int rm_write_packet(AVFormatContext *s, int stream_index, 
392
                           const uint8_t *buf, int size, int64_t pts)
393
{
394
    if (s->streams[stream_index]->codec.codec_type == 
395
        CODEC_TYPE_AUDIO)
396
        return rm_write_audio(s, buf, size);
397
    else
398
        return rm_write_video(s, buf, size);
399
}
400
        
401
static int rm_write_trailer(AVFormatContext *s)
402
{
403
    RMContext *rm = s->priv_data;
404
    int data_size, index_pos, i;
405
    ByteIOContext *pb = &s->pb;
406

    
407
    if (!url_is_streamed(&s->pb)) {
408
        /* end of file: finish to write header */
409
        index_pos = url_fseek(pb, 0, SEEK_CUR);
410
        data_size = index_pos - rm->data_pos;
411

    
412
        /* index */
413
        put_tag(pb, "INDX");
414
        put_be32(pb, 10 + 10 * s->nb_streams);
415
        put_be16(pb, 0);
416
        
417
        for(i=0;i<s->nb_streams;i++) {
418
            put_be32(pb, 0); /* zero indices */
419
            put_be16(pb, i); /* stream number */
420
            put_be32(pb, 0); /* next index */
421
        }
422
        /* undocumented end header */
423
        put_be32(pb, 0);
424
        put_be32(pb, 0);
425
        
426
        url_fseek(pb, 0, SEEK_SET);
427
        for(i=0;i<s->nb_streams;i++)
428
            rm->streams[i].total_frames = rm->streams[i].nb_frames;
429
        rv10_write_header(s, data_size, index_pos);
430
    } else {
431
        /* undocumented end header */
432
        put_be32(pb, 0);
433
        put_be32(pb, 0);
434
    }
435
    put_flush_packet(pb);
436
    return 0;
437
}
438

    
439
/***************************************************/
440

    
441
static void get_str(ByteIOContext *pb, char *buf, int buf_size)
442
{
443
    int len, i;
444
    char *q;
445

    
446
    len = get_be16(pb);
447
    q = buf;
448
    for(i=0;i<len;i++) {
449
        if (i < buf_size - 1)
450
            *q++ = get_byte(pb);
451
    }
452
    *q = '\0';
453
}
454

    
455
static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
456
{
457
    int len, i;
458
    char *q;
459

    
460
    len = get_byte(pb);
461
    q = buf;
462
    for(i=0;i<len;i++) {
463
        if (i < buf_size - 1)
464
            *q++ = get_byte(pb);
465
    }
466
    *q = '\0';
467
}
468

    
469
static void rm_read_audio_stream_info(AVFormatContext *s, AVStream *st, 
470
                                      int read_all)
471
{
472
    ByteIOContext *pb = &s->pb;
473
    char buf[128];
474
    uint32_t version;
475
    int i;
476

    
477
    /* ra type header */
478
    version = get_be32(pb); /* version */
479
    if (((version >> 16) & 0xff) == 3) {
480
        /* very old version */
481
        for(i = 0; i < 14; i++)
482
            get_byte(pb);
483
        get_str8(pb, s->title, sizeof(s->title));
484
        get_str8(pb, s->author, sizeof(s->author));
485
        get_str8(pb, s->copyright, sizeof(s->copyright));
486
        get_str8(pb, s->comment, sizeof(s->comment));
487
        get_byte(pb);
488
        get_str8(pb, buf, sizeof(buf));
489
        st->codec.sample_rate = 8000;
490
        st->codec.channels = 1;
491
        st->codec.codec_type = CODEC_TYPE_AUDIO;
492
        st->codec.codec_id = CODEC_ID_RA_144;
493
    } else {
494
        /* old version (4) */
495
        get_be32(pb); /* .ra4 */
496
        get_be32(pb);
497
        get_be16(pb);
498
        get_be32(pb); /* header size */
499
        get_be16(pb); /* add codec info */
500
        get_be32(pb); /* coded frame size */
501
        get_be32(pb); /* ??? */
502
        get_be32(pb); /* ??? */
503
        get_be32(pb); /* ??? */
504
        get_be16(pb); /* 1 */ 
505
        get_be16(pb); /* coded frame size */
506
        get_be32(pb);
507
        st->codec.sample_rate = get_be16(pb);
508
        get_be32(pb);
509
        st->codec.channels = get_be16(pb);
510
        get_str8(pb, buf, sizeof(buf)); /* desc */
511
        get_str8(pb, buf, sizeof(buf)); /* desc */
512
        st->codec.codec_type = CODEC_TYPE_AUDIO;
513
        if (!strcmp(buf, "dnet")) {
514
            st->codec.codec_id = CODEC_ID_AC3;
515
        } else {
516
            st->codec.codec_id = CODEC_ID_NONE;
517
            pstrcpy(st->codec.codec_name, sizeof(st->codec.codec_name),
518
                    buf);
519
        }
520
        if (read_all) {
521
            get_byte(pb);
522
            get_byte(pb);
523
            get_byte(pb);
524
            
525
            get_str8(pb, s->title, sizeof(s->title));
526
            get_str8(pb, s->author, sizeof(s->author));
527
            get_str8(pb, s->copyright, sizeof(s->copyright));
528
            get_str8(pb, s->comment, sizeof(s->comment));
529
        }
530
    }
531
}
532

    
533
static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
534
{
535
    RMContext *rm = s->priv_data;
536
    AVStream *st;
537

    
538
    rm->old_format = 1;
539
    st = av_new_stream(s, 0);
540
    if (!st)
541
        goto fail;
542
    rm_read_audio_stream_info(s, st, 1);
543
    return 0;
544
 fail:
545
    return -1;
546
}
547

    
548
static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
549
{
550
    RMContext *rm = s->priv_data;
551
    AVStream *st;
552
    ByteIOContext *pb = &s->pb;
553
    unsigned int tag, v;
554
    int tag_size, size, codec_data_size, i;
555
    int64_t codec_pos;
556
    unsigned int h263_hack_version, start_time, duration;
557
    char buf[128];
558
    int flags = 0;
559

    
560
    tag = get_le32(pb);
561
    if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
562
        /* very old .ra format */
563
        return rm_read_header_old(s, ap);
564
    } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
565
        return -EIO;
566
    }
567

    
568
    get_be32(pb); /* header size */
569
    get_be16(pb);
570
    get_be32(pb);
571
    get_be32(pb); /* number of headers */
572
    
573
    for(;;) {
574
        if (url_feof(pb))
575
            goto fail;
576
        tag = get_le32(pb);
577
        tag_size = get_be32(pb);
578
        get_be16(pb);
579
#if 0
580
        printf("tag=%c%c%c%c (%08x) size=%d\n", 
581
               (tag) & 0xff,
582
               (tag >> 8) & 0xff,
583
               (tag >> 16) & 0xff,
584
               (tag >> 24) & 0xff,
585
               tag,
586
               tag_size);
587
#endif
588
        if (tag_size < 10)
589
            goto fail;
590
        switch(tag) {
591
        case MKTAG('P', 'R', 'O', 'P'):
592
            /* file header */
593
            get_be32(pb); /* max bit rate */
594
            get_be32(pb); /* avg bit rate */
595
            get_be32(pb); /* max packet size */
596
            get_be32(pb); /* avg packet size */
597
            get_be32(pb); /* nb packets */
598
            get_be32(pb); /* duration */
599
            get_be32(pb); /* preroll */
600
            get_be32(pb); /* index offset */
601
            get_be32(pb); /* data offset */
602
            get_be16(pb); /* nb streams */
603
            flags = get_be16(pb); /* flags */
604
            break;
605
        case MKTAG('C', 'O', 'N', 'T'):
606
            get_str(pb, s->title, sizeof(s->title));
607
            get_str(pb, s->author, sizeof(s->author));
608
            get_str(pb, s->copyright, sizeof(s->copyright));
609
            get_str(pb, s->comment, sizeof(s->comment));
610
            break;
611
        case MKTAG('M', 'D', 'P', 'R'):
612
            st = av_new_stream(s, 0);
613
            if (!st)
614
                goto fail;
615
            st->id = get_be16(pb);
616
            get_be32(pb); /* max bit rate */
617
            st->codec.bit_rate = get_be32(pb); /* bit rate */
618
            get_be32(pb); /* max packet size */
619
            get_be32(pb); /* avg packet size */
620
            start_time = get_be32(pb); /* start time */
621
            get_be32(pb); /* preroll */
622
            duration = get_be32(pb); /* duration */
623
            st->start_time = start_time * (AV_TIME_BASE / 1000);
624
            st->duration = duration * (AV_TIME_BASE / 1000);
625
            get_str8(pb, buf, sizeof(buf)); /* desc */
626
            get_str8(pb, buf, sizeof(buf)); /* mimetype */
627
            codec_data_size = get_be32(pb);
628
            codec_pos = url_ftell(pb);
629

    
630
            v = get_be32(pb);
631
            if (v == MKTAG(0xfd, 'a', 'r', '.')) {
632
                /* ra type header */
633
                rm_read_audio_stream_info(s, st, 0);
634
            } else {
635
                if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
636
                fail1:
637
                    fprintf(stderr, "Unsupported video codec\n");
638
                    goto fail;
639
                }
640
                st->codec.codec_tag = get_le32(pb);
641
                if (st->codec.codec_tag != MKTAG('R', 'V', '1', '0'))
642
                    goto fail1;
643
                st->codec.width = get_be16(pb);
644
                st->codec.height = get_be16(pb);
645
                st->codec.frame_rate_base= 1;
646
                st->codec.frame_rate = get_be16(pb) * st->codec.frame_rate_base;
647
                st->codec.codec_type = CODEC_TYPE_VIDEO;
648
                get_be32(pb);
649
                get_be16(pb);
650
                get_be32(pb);
651
                get_be16(pb);
652
                /* modification of h263 codec version (!) */
653
                h263_hack_version = get_be32(pb);
654
                switch(h263_hack_version) {
655
                case 0x10000000:
656
                case 0x10003000:
657
                case 0x10003001:
658
                    st->codec.sub_id = h263_hack_version;
659
                    st->codec.codec_id = CODEC_ID_RV10;
660
                    break;
661
                default:
662
                    /* not handled */
663
                    st->codec.codec_id = CODEC_ID_NONE;
664
                    break;
665
                }
666
            }
667
            /* skip codec info */
668
            size = url_ftell(pb) - codec_pos;
669
            url_fskip(pb, codec_data_size - size);
670
            break;
671
        case MKTAG('D', 'A', 'T', 'A'):
672
            goto header_end;
673
        default:
674
            /* unknown tag: skip it */
675
            url_fskip(pb, tag_size - 10);
676
            break;
677
        }
678
    }
679
 header_end:
680
    rm->nb_packets = get_be32(pb); /* number of packets */
681
    if (!rm->nb_packets && (flags & 4))
682
        rm->nb_packets = 3600 * 25;
683
    get_be32(pb); /* next data header */
684
    return 0;
685

    
686
 fail:
687
    for(i=0;i<s->nb_streams;i++) {
688
        av_free(s->streams[i]);
689
    }
690
    return -EIO;
691
}
692

    
693
static int get_num(ByteIOContext *pb, int *len)
694
{
695
    int n, n1;
696

    
697
    n = get_be16(pb);
698
    (*len)-=2;
699
    if (n >= 0x4000) {
700
        return n - 0x4000;
701
    } else {
702
        n1 = get_be16(pb);
703
        (*len)-=2;
704
        return (n << 16) | n1;
705
    }
706
}
707

    
708
/* multiple of 20 bytes for ra144 (ugly) */
709
#define RAW_PACKET_SIZE 1000
710

    
711
static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
712
{
713
    RMContext *rm = s->priv_data;
714
    ByteIOContext *pb = &s->pb;
715
    AVStream *st;
716
    int len, num, timestamp, i, tmp, j;
717
    uint8_t *ptr;
718
    int flags;
719

    
720
    if (rm->old_format) {
721
        /* just read raw bytes */
722
        len = RAW_PACKET_SIZE;
723
        av_new_packet(pkt, len);
724
        pkt->stream_index = 0;
725
        len = get_buffer(pb, pkt->data, len);
726
        if (len <= 0) {
727
            av_free_packet(pkt);
728
            return -EIO;
729
        }
730
        pkt->size = len;
731
        st = s->streams[0];
732
    } else {
733
    redo:
734
        if (rm->nb_packets == 0)
735
            return -EIO;
736
        get_be16(pb);
737
        len = get_be16(pb);
738
        if (len < 12)
739
            return -EIO;
740
        num = get_be16(pb);
741
        timestamp = get_be32(pb);
742
        get_byte(pb); /* reserved */
743
        flags = get_byte(pb); /* flags */
744
        rm->nb_packets--;
745
        len -= 12;
746
        
747
        st = NULL;
748
        for(i=0;i<s->nb_streams;i++) {
749
            st = s->streams[i];
750
            if (num == st->id)
751
                break;
752
        }
753
        if (i == s->nb_streams) {
754
            /* skip packet if unknown number */
755
            url_fskip(pb, len);
756
            goto redo;
757
        }
758
        
759
        if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
760
            int full_frame, h, pic_num;
761
            
762
            h= get_byte(pb);
763
            if ((h & 0xc0) == 0xc0) {
764
                int len2, pos;
765
                full_frame = 1;
766
                len2= get_num(pb, &len);
767
                pos = get_num(pb, &len);
768
                //printf("pos:%d\n",len);
769
                len -= 2;
770
            } else {
771
                int seq, frame_size, pos;
772
                full_frame = 0;
773
                seq = get_byte(pb);
774
                frame_size = get_num(pb, &len);
775
                pos = get_num(pb, &len);
776
                //printf("seq:%d, size:%d, pos:%d\n",seq,frame_size,pos);
777
                len -= 3;
778
            }
779
            /* picture number */
780
            pic_num= get_byte(pb);
781
            
782
            //XXX/FIXME/HACK, demuxer should be fixed to send complete frames ...
783
            if(st->codec.slice_offset==NULL) 
784
                st->codec.slice_offset= (int*)av_malloc(sizeof(int));
785
            st->codec.slice_count= full_frame; 
786
            st->codec.slice_offset[0]= 0;
787
        }
788
        
789
        av_new_packet(pkt, len);
790
        pkt->stream_index = i;
791
        get_buffer(pb, pkt->data, len);
792
    }
793

    
794
    /* for AC3, needs to swap bytes */
795
    if (st->codec.codec_id == CODEC_ID_AC3) {
796
        ptr = pkt->data;
797
        for(j=0;j<len;j+=2) {
798
            tmp = ptr[0];
799
            ptr[0] = ptr[1];
800
            ptr[1] = tmp;
801
                ptr += 2;
802
        }
803
    }
804
    return 0;
805
}
806

    
807
static int rm_read_close(AVFormatContext *s)
808
{
809
    return 0;
810
}
811

    
812
static int rm_probe(AVProbeData *p)
813
{
814
    /* check file header */
815
    if (p->buf_size <= 32)
816
        return 0;
817
    if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
818
         p->buf[2] == 'M' && p->buf[3] == 'F' &&
819
         p->buf[4] == 0 && p->buf[5] == 0) ||
820
        (p->buf[0] == '.' && p->buf[1] == 'r' &&
821
         p->buf[2] == 'a' && p->buf[3] == 0xfd))
822
        return AVPROBE_SCORE_MAX;
823
    else
824
        return 0;
825
}
826

    
827
static AVInputFormat rm_iformat = {
828
    "rm",
829
    "rm format",
830
    sizeof(RMContext),
831
    rm_probe,
832
    rm_read_header,
833
    rm_read_packet,
834
    rm_read_close,
835
};
836

    
837
static AVOutputFormat rm_oformat = {
838
    "rm",
839
    "rm format",
840
    "application/vnd.rn-realmedia",
841
    "rm,ra",
842
    sizeof(RMContext),
843
    CODEC_ID_AC3,
844
    CODEC_ID_RV10,
845
    rm_write_header,
846
    rm_write_packet,
847
    rm_write_trailer,
848
};
849

    
850
int rm_init(void)
851
{
852
    av_register_input_format(&rm_iformat);
853
    av_register_output_format(&rm_oformat);
854
    return 0;
855
}