Statistics
| Branch: | Revision:

ffmpeg / libav / rm.c @ 41568e4a

History | View | Annotate | Download (23.5 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * "Real" compatible mux and demux.
3 19720f15 Fabrice Bellard
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 19720f15 Fabrice Bellard
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9 de6d9b64 Fabrice Bellard
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 de6d9b64 Fabrice Bellard
 */
19
#include "avformat.h"
20
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
} RMContext;
43
44
static void put_str(ByteIOContext *s, const char *tag)
45
{
46
    put_be16(s,strlen(tag));
47
    while (*tag) {
48
        put_byte(s, *tag++);
49
    }
50
}
51
52
static void put_str8(ByteIOContext *s, const char *tag)
53
{
54
    put_byte(s, strlen(tag));
55
    while (*tag) {
56
        put_byte(s, *tag++);
57
    }
58
}
59
60
static void rv10_write_header(AVFormatContext *ctx, 
61
                              int data_size, int index_pos)
62
{
63
    RMContext *rm = ctx->priv_data;
64
    ByteIOContext *s = &ctx->pb;
65
    StreamInfo *stream;
66
    unsigned char *data_offset_ptr, *start_ptr;
67
    const char *desc, *mimetype;
68
    int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
69
    int bit_rate, v, duration, flags, data_pos;
70
71
    start_ptr = s->buf_ptr;
72
73
    put_tag(s, ".RMF");
74
    put_be32(s,18); /* header size */
75
    put_be16(s,0);
76
    put_be32(s,0);
77
    put_be32(s,4 + ctx->nb_streams); /* num headers */
78
79
    put_tag(s,"PROP");
80
    put_be32(s, 50);
81
    put_be16(s, 0);
82
    packet_max_size = 0;
83
    packet_total_size = 0;
84
    nb_packets = 0;
85
    bit_rate = 0;
86
    duration = 0;
87
    for(i=0;i<ctx->nb_streams;i++) {
88
        StreamInfo *stream = &rm->streams[i];
89
        bit_rate += stream->bit_rate;
90
        if (stream->packet_max_size > packet_max_size)
91
            packet_max_size = stream->packet_max_size;
92
        nb_packets += stream->nb_packets;
93
        packet_total_size += stream->packet_total_size;
94
        /* select maximum duration */
95
        v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
96
        if (v > duration)
97
            duration = v;
98
    }
99
    put_be32(s, bit_rate); /* max bit rate */
100
    put_be32(s, bit_rate); /* avg bit rate */
101
    put_be32(s, packet_max_size);        /* max packet size */
102
    if (nb_packets > 0)
103
        packet_avg_size = packet_total_size / nb_packets;
104
    else
105
        packet_avg_size = 0;
106
    put_be32(s, packet_avg_size);        /* avg packet size */
107
    put_be32(s, nb_packets);  /* num packets */
108
    put_be32(s, duration); /* duration */
109
    put_be32(s, BUFFER_DURATION);           /* preroll */
110
    put_be32(s, index_pos);           /* index offset */
111
    /* computation of data the data offset */
112
    data_offset_ptr = s->buf_ptr;
113
    put_be32(s, 0);           /* data offset : will be patched after */
114
    put_be16(s, ctx->nb_streams);    /* num streams */
115
    flags = 1 | 2; /* save allowed & perfect play */
116
    if (url_is_streamed(s))
117
        flags |= 4; /* live broadcast */
118
    put_be16(s, flags);
119
    
120
    /* comments */
121
122
    put_tag(s,"CONT");
123
    size = strlen(ctx->title) + strlen(ctx->author) + strlen(ctx->copyright) + 
124
        strlen(ctx->comment) + 4 * 2 + 10;
125
    put_be32(s,size);
126
    put_be16(s,0);
127
    put_str(s, ctx->title);
128
    put_str(s, ctx->author);
129
    put_str(s, ctx->copyright);
130
    put_str(s, ctx->comment);
131
    
132
    for(i=0;i<ctx->nb_streams;i++) {
133
        int codec_data_size;
134
135
        stream = &rm->streams[i];
136
        
137
        if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
138
            desc = "The Audio Stream";
139
            mimetype = "audio/x-pn-realaudio";
140
            codec_data_size = 73;
141
        } else {
142
            desc = "The Video Stream";
143
            mimetype = "video/x-pn-realvideo";
144
            codec_data_size = 34;
145
        }
146
147
        put_tag(s,"MDPR");
148
        size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
149
        put_be32(s, size);
150
        put_be16(s, 0);
151
152
        put_be16(s, i); /* stream number */
153
        put_be32(s, stream->bit_rate); /* max bit rate */
154
        put_be32(s, stream->bit_rate); /* avg bit rate */
155
        put_be32(s, stream->packet_max_size);        /* max packet size */
156
        if (stream->nb_packets > 0)
157
            packet_avg_size = stream->packet_total_size / 
158
                stream->nb_packets;
159
        else
160
            packet_avg_size = 0;
161
        put_be32(s, packet_avg_size);        /* avg packet size */
162
        put_be32(s, 0);           /* start time */
163
        put_be32(s, BUFFER_DURATION);           /* preroll */
164
        /* duration */
165 5d6ef63f Philip Gladstone
        if (url_is_streamed(s) || !stream->total_frames)
166
            put_be32(s, (int)(3600 * 1000));
167
        else
168
            put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
169 de6d9b64 Fabrice Bellard
        put_str8(s, desc);
170
        put_str8(s, mimetype);
171
        put_be32(s, codec_data_size);
172
        
173
        if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
174
            int coded_frame_size, fscode, sample_rate;
175
            sample_rate = stream->enc->sample_rate;
176
            coded_frame_size = (stream->enc->bit_rate * 
177
                                stream->enc->frame_size) / (8 * sample_rate);
178
            /* audio codec info */
179
            put_tag(s, ".ra");
180
            put_byte(s, 0xfd);
181
            put_be32(s, 0x00040000); /* version */
182
            put_tag(s, ".ra4");
183
            put_be32(s, 0x01b53530); /* stream length */
184
            put_be16(s, 4); /* unknown */
185
            put_be32(s, 0x39); /* header size */
186
187
            switch(sample_rate) {
188
            case 48000:
189
            case 24000:
190
            case 12000:
191
                fscode = 1;
192
                break;
193
            default:
194
            case 44100:
195
            case 22050:
196
            case 11025:
197
                fscode = 2;
198
                break;
199
            case 32000:
200
            case 16000:
201
            case 8000:
202
                fscode = 3;
203
            }
204
            put_be16(s, fscode); /* codec additional info, for AC3, seems
205
                                     to be a frequency code */
206
            /* special hack to compensate rounding errors... */
207
            if (coded_frame_size == 557)
208
                coded_frame_size--;
209
            put_be32(s, coded_frame_size); /* frame length */
210
            put_be32(s, 0x51540); /* unknown */
211
            put_be32(s, 0x249f0); /* unknown */
212
            put_be32(s, 0x249f0); /* unknown */
213
            put_be16(s, 0x01);
214
            /* frame length : seems to be very important */
215
            put_be16(s, coded_frame_size); 
216
            put_be32(s, 0); /* unknown */
217
            put_be16(s, stream->enc->sample_rate); /* sample rate */
218
            put_be32(s, 0x10); /* unknown */
219
            put_be16(s, stream->enc->channels);
220
            put_str8(s, "Int0"); /* codec name */
221
            put_str8(s, "dnet"); /* codec name */
222
            put_be16(s, 0); /* title length */
223
            put_be16(s, 0); /* author length */
224
            put_be16(s, 0); /* copyright length */
225
            put_byte(s, 0); /* end of header */
226
        } else {
227
            /* video codec info */
228
            put_be32(s,34); /* size */
229
            put_tag(s,"VIDORV10");
230
            put_be16(s, stream->enc->width);
231
            put_be16(s, stream->enc->height);
232 5d6ef63f Philip Gladstone
            put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */
233 de6d9b64 Fabrice Bellard
            put_be32(s,0);     /* unknown meaning */
234 5d6ef63f Philip Gladstone
            put_be16(s, (int) stream->frame_rate);  /* unknown meaning */
235 de6d9b64 Fabrice Bellard
            put_be32(s,0);     /* unknown meaning */
236
            put_be16(s, 8);    /* unknown meaning */
237
            /* Seems to be the codec version: only use basic H263. The next
238
               versions seems to add a diffential DC coding as in
239
               MPEG... nothing new under the sun */
240
            put_be32(s,0x10000000); 
241
            //put_be32(s,0x10003000); 
242
        }
243
    }
244
245
    /* patch data offset field */
246
    data_pos = s->buf_ptr - start_ptr;
247
    rm->data_pos = data_pos;
248
    data_offset_ptr[0] = data_pos >> 24;
249
    data_offset_ptr[1] = data_pos >> 16;
250
    data_offset_ptr[2] = data_pos >> 8;
251
    data_offset_ptr[3] = data_pos;
252
    
253
    /* data stream */
254
    put_tag(s,"DATA");
255
    put_be32(s,data_size + 10 + 8);
256
    put_be16(s,0);
257
258
    put_be32(s, nb_packets); /* number of packets */
259
    put_be32(s,0); /* next data header */
260
}
261
262
static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream, 
263
                                int length, int key_frame)
264
{
265
    int timestamp;
266
    ByteIOContext *s = &ctx->pb;
267
268
    stream->nb_packets++;
269
    stream->packet_total_size += length;
270
    if (length > stream->packet_max_size)
271
        stream->packet_max_size =  length;
272
273
    put_be16(s,0); /* version */
274
    put_be16(s,length + 12);
275
    put_be16(s, stream->num); /* stream number */
276
    timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
277
    put_be32(s, timestamp); /* timestamp */
278
    put_byte(s, 0); /* reserved */
279
    put_byte(s, key_frame ? 2 : 0); /* flags */
280
}
281
282
static int rm_write_header(AVFormatContext *s)
283
{
284 c9a65ca8 Fabrice Bellard
    RMContext *rm = s->priv_data;
285 de6d9b64 Fabrice Bellard
    StreamInfo *stream;
286
    int n;
287
    AVCodecContext *codec;
288
289
    for(n=0;n<s->nb_streams;n++) {
290
        s->streams[n]->id = n;
291
        codec = &s->streams[n]->codec;
292
        stream = &rm->streams[n];
293
        memset(stream, 0, sizeof(StreamInfo));
294
        stream->num = n;
295
        stream->bit_rate = codec->bit_rate;
296
        stream->enc = codec;
297
298
        switch(codec->codec_type) {
299
        case CODEC_TYPE_AUDIO:
300
            rm->audio_stream = stream;
301
            stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
302
            /* XXX: dummy values */
303
            stream->packet_max_size = 1024;
304
            stream->nb_packets = 0;
305
            stream->total_frames = stream->nb_packets;
306
            break;
307
        case CODEC_TYPE_VIDEO:
308
            rm->video_stream = stream;
309
            stream->frame_rate = (float)codec->frame_rate / (float)FRAME_RATE_BASE;
310
            /* XXX: dummy values */
311
            stream->packet_max_size = 4096;
312
            stream->nb_packets = 0;
313
            stream->total_frames = stream->nb_packets;
314
            break;
315 5d6ef63f Philip Gladstone
        default:
316 42343f7e Philip Gladstone
            av_abort();
317 de6d9b64 Fabrice Bellard
        }
318
    }
319
320
    rv10_write_header(s, 0, 0);
321
    put_flush_packet(&s->pb);
322
    return 0;
323
}
324
325
static int rm_write_audio(AVFormatContext *s, UINT8 *buf, int size)
326
{
327 8be1c656 Fabrice Bellard
    UINT8 *buf1;
328 de6d9b64 Fabrice Bellard
    RMContext *rm = s->priv_data;
329
    ByteIOContext *pb = &s->pb;
330
    StreamInfo *stream = rm->audio_stream;
331
    int i;
332
333 8be1c656 Fabrice Bellard
    /* XXX: suppress this malloc */
334 1ea4f593 Fabrice Bellard
    buf1= (UINT8*) av_malloc( size * sizeof(UINT8) );
335 8be1c656 Fabrice Bellard
    
336 de6d9b64 Fabrice Bellard
    write_packet_header(s, stream, size, stream->enc->key_frame);
337
    
338
    /* for AC3, the words seems to be reversed */
339
    for(i=0;i<size;i+=2) {
340
        buf1[i] = buf[i+1];
341
        buf1[i+1] = buf[i];
342
    }
343
    put_buffer(pb, buf1, size);
344
    put_flush_packet(pb);
345
    stream->nb_frames++;
346 1ea4f593 Fabrice Bellard
    av_free(buf1);
347 de6d9b64 Fabrice Bellard
    return 0;
348
}
349
350
static int rm_write_video(AVFormatContext *s, UINT8 *buf, int size)
351
{
352
    RMContext *rm = s->priv_data;
353
    ByteIOContext *pb = &s->pb;
354
    StreamInfo *stream = rm->video_stream;
355
    int key_frame = stream->enc->key_frame;
356 5d6ef63f Philip Gladstone
357 de6d9b64 Fabrice Bellard
    /* XXX: this is incorrect: should be a parameter */
358
359
    /* Well, I spent some time finding the meaning of these bits. I am
360
       not sure I understood everything, but it works !! */
361
#if 1
362
    write_packet_header(s, stream, size + 7, key_frame);
363
    /* bit 7: '1' if final packet of a frame converted in several packets */
364
    put_byte(pb, 0x81); 
365
    /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
366
       frame starting from 1 */
367
    if (key_frame) {
368
        put_byte(pb, 0x81); 
369
    } else {
370
        put_byte(pb, 0x01); 
371
    }
372
    put_be16(pb, 0x4000 | (size)); /* total frame size */
373
    put_be16(pb, 0x4000 | (size));              /* offset from the start or the end */
374
#else
375
    /* full frame */
376
    write_packet_header(s, size + 6);
377
    put_byte(pb, 0xc0); 
378
    put_be16(pb, 0x4000 | size); /* total frame size */
379
    put_be16(pb, 0x4000 + packet_number * 126); /* position in stream */
380
#endif
381
    put_byte(pb, stream->nb_frames & 0xff); 
382
    
383
    put_buffer(pb, buf, size);
384
    put_flush_packet(pb);
385
386
    stream->nb_frames++;
387
    return 0;
388
}
389
390
static int rm_write_packet(AVFormatContext *s, int stream_index, 
391 10bb7023 Juanjo
                           UINT8 *buf, int size, int force_pts)
392 de6d9b64 Fabrice Bellard
{
393
    if (s->streams[stream_index]->codec.codec_type == 
394
        CODEC_TYPE_AUDIO)
395
        return rm_write_audio(s, buf, size);
396
    else
397
        return rm_write_video(s, buf, size);
398
}
399
        
400
static int rm_write_trailer(AVFormatContext *s)
401
{
402
    RMContext *rm = s->priv_data;
403
    int data_size, index_pos, i;
404
    ByteIOContext *pb = &s->pb;
405
406
    if (!url_is_streamed(&s->pb)) {
407
        /* end of file: finish to write header */
408
        index_pos = url_fseek(pb, 0, SEEK_CUR);
409
        data_size = index_pos - rm->data_pos;
410
411
        /* index */
412
        put_tag(pb, "INDX");
413
        put_be32(pb, 10 + 10 * s->nb_streams);
414
        put_be16(pb, 0);
415
        
416
        for(i=0;i<s->nb_streams;i++) {
417
            put_be32(pb, 0); /* zero indices */
418
            put_be16(pb, i); /* stream number */
419
            put_be32(pb, 0); /* next index */
420
        }
421
        /* undocumented end header */
422
        put_be32(pb, 0);
423
        put_be32(pb, 0);
424
        
425
        url_fseek(pb, 0, SEEK_SET);
426
        for(i=0;i<s->nb_streams;i++)
427
            rm->streams[i].total_frames = rm->streams[i].nb_frames;
428
        rv10_write_header(s, data_size, index_pos);
429
    } else {
430
        /* undocumented end header */
431
        put_be32(pb, 0);
432
        put_be32(pb, 0);
433
    }
434
    put_flush_packet(pb);
435
    return 0;
436
}
437
438
/***************************************************/
439
440
static void get_str(ByteIOContext *pb, char *buf, int buf_size)
441
{
442
    int len, i;
443
    char *q;
444
445
    len = get_be16(pb);
446
    q = buf;
447
    for(i=0;i<len;i++) {
448
        if (i < buf_size - 1)
449
            *q++ = get_byte(pb);
450
    }
451
    *q = '\0';
452
}
453
454
static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
455
{
456
    int len, i;
457
    char *q;
458
459
    len = get_byte(pb);
460
    q = buf;
461
    for(i=0;i<len;i++) {
462
        if (i < buf_size - 1)
463
            *q++ = get_byte(pb);
464
    }
465
    *q = '\0';
466
}
467
468
static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
469
{
470 c9a65ca8 Fabrice Bellard
    RMContext *rm = s->priv_data;
471 de6d9b64 Fabrice Bellard
    AVStream *st;
472
    ByteIOContext *pb = &s->pb;
473
    unsigned int tag, v;
474
    int tag_size, size, codec_data_size, i;
475
    INT64 codec_pos;
476
    unsigned int h263_hack_version;
477
    char buf[128];
478 5d6ef63f Philip Gladstone
    int flags = 0;
479 de6d9b64 Fabrice Bellard
480
    if (get_le32(pb) != MKTAG('.', 'R', 'M', 'F'))
481
        return -EIO;
482
483
    get_be32(pb); /* header size */
484
    get_be16(pb);
485
    get_be32(pb);
486
    get_be32(pb); /* number of headers */
487
    
488
    for(;;) {
489
        if (url_feof(pb))
490
            goto fail;
491
        tag = get_le32(pb);
492
        tag_size = get_be32(pb);
493
        get_be16(pb);
494
#if 0
495
        printf("tag=%c%c%c%c (%08x) size=%d\n", 
496
               (tag) & 0xff,
497
               (tag >> 8) & 0xff,
498
               (tag >> 16) & 0xff,
499
               (tag >> 24) & 0xff,
500
               tag,
501
               tag_size);
502
#endif
503
        if (tag_size < 10)
504
            goto fail;
505
        switch(tag) {
506
        case MKTAG('P', 'R', 'O', 'P'):
507
            /* file header */
508
            get_be32(pb); /* max bit rate */
509
            get_be32(pb); /* avg bit rate */
510
            get_be32(pb); /* max packet size */
511
            get_be32(pb); /* avg packet size */
512
            get_be32(pb); /* nb packets */
513
            get_be32(pb); /* duration */
514
            get_be32(pb); /* preroll */
515
            get_be32(pb); /* index offset */
516
            get_be32(pb); /* data offset */
517
            get_be16(pb); /* nb streams */
518 5d6ef63f Philip Gladstone
            flags = get_be16(pb); /* flags */
519 de6d9b64 Fabrice Bellard
            break;
520
        case MKTAG('C', 'O', 'N', 'T'):
521
            get_str(pb, s->title, sizeof(s->title));
522
            get_str(pb, s->author, sizeof(s->author));
523
            get_str(pb, s->copyright, sizeof(s->copyright));
524
            get_str(pb, s->comment, sizeof(s->comment));
525
            break;
526
        case MKTAG('M', 'D', 'P', 'R'):
527
            st = av_mallocz(sizeof(AVStream));
528
            if (!st)
529
                goto fail;
530
            s->streams[s->nb_streams++] = st;
531
            st->id = get_be16(pb);
532
            get_be32(pb); /* max bit rate */
533
            st->codec.bit_rate = get_be32(pb); /* bit rate */
534
            get_be32(pb); /* max packet size */
535
            get_be32(pb); /* avg packet size */
536
            get_be32(pb); /* start time */
537
            get_be32(pb); /* preroll */
538
            get_be32(pb); /* duration */
539
            get_str8(pb, buf, sizeof(buf)); /* desc */
540
            get_str8(pb, buf, sizeof(buf)); /* mimetype */
541
            codec_data_size = get_be32(pb);
542
            codec_pos = url_ftell(pb);
543
544
            v = get_be32(pb);
545
            if (v == MKTAG(0xfd, 'a', 'r', '.')) {
546
                /* ra type header */
547
                get_be32(pb); /* version */
548
                get_be32(pb); /* .ra4 */
549
                get_be32(pb);
550
                get_be16(pb);
551
                get_be32(pb); /* header size */
552
                get_be16(pb); /* add codec info */
553
                get_be32(pb); /* coded frame size */
554
                get_be32(pb); /* ??? */
555
                get_be32(pb); /* ??? */
556
                get_be32(pb); /* ??? */
557
                get_be16(pb); /* 1 */ 
558
                get_be16(pb); /* coded frame size */
559
                get_be32(pb);
560
                st->codec.sample_rate = get_be16(pb);
561
                get_be32(pb);
562
                st->codec.channels = get_be16(pb);
563
                get_str8(pb, buf, sizeof(buf)); /* desc */
564
                get_str8(pb, buf, sizeof(buf)); /* desc */
565
                st->codec.codec_type = CODEC_TYPE_AUDIO;
566
                if (!strcmp(buf, "dnet")) {
567
                    st->codec.codec_id = CODEC_ID_AC3;
568
                } else {
569
                    st->codec.codec_id = CODEC_ID_NONE;
570 c9a65ca8 Fabrice Bellard
                    pstrcpy(st->codec.codec_name, sizeof(st->codec.codec_name),
571 de6d9b64 Fabrice Bellard
                            buf);
572
                }
573
            } else {
574
                if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
575
                fail1:
576
                    fprintf(stderr, "Unsupported video codec\n");
577
                    goto fail;
578
                }
579
                st->codec.codec_tag = get_le32(pb);
580
                if (st->codec.codec_tag != MKTAG('R', 'V', '1', '0'))
581
                    goto fail1;
582
                st->codec.width = get_be16(pb);
583
                st->codec.height = get_be16(pb);
584
                st->codec.frame_rate = get_be16(pb) * FRAME_RATE_BASE;
585
                st->codec.codec_type = CODEC_TYPE_VIDEO;
586
                get_be32(pb);
587
                get_be16(pb);
588
                get_be32(pb);
589
                get_be16(pb);
590
                /* modification of h263 codec version (!) */
591
                h263_hack_version = get_be32(pb);
592
                switch(h263_hack_version) {
593
                case 0x10000000:
594
                case 0x10003000:
595
                case 0x10003001:
596 144f0625 Michael Niedermayer
                    st->codec.sub_id = h263_hack_version;
597 de6d9b64 Fabrice Bellard
                    st->codec.codec_id = CODEC_ID_RV10;
598
                    break;
599
                default:
600
                    /* not handled */
601
                    st->codec.codec_id = CODEC_ID_NONE;
602
                    break;
603
                }
604
            }
605
            /* skip codec info */
606
            size = url_ftell(pb) - codec_pos;
607
            url_fskip(pb, codec_data_size - size);
608
            break;
609
        case MKTAG('D', 'A', 'T', 'A'):
610
            goto header_end;
611
        default:
612
            /* unknown tag: skip it */
613
            url_fskip(pb, tag_size - 10);
614
            break;
615
        }
616
    }
617
 header_end:
618
    rm->nb_packets = get_be32(pb); /* number of packets */
619 5d6ef63f Philip Gladstone
    if (!rm->nb_packets && (flags & 4))
620
        rm->nb_packets = 3600 * 25;
621 de6d9b64 Fabrice Bellard
    get_be32(pb); /* next data header */
622
    return 0;
623
624
 fail:
625
    for(i=0;i<s->nb_streams;i++) {
626 1ea4f593 Fabrice Bellard
        av_free(s->streams[i]);
627 de6d9b64 Fabrice Bellard
    }
628
    return -EIO;
629
}
630
631 4c2bc159 Michael Niedermayer
static int get_num(ByteIOContext *pb, int *len)
632
{
633
    int n, n1;
634
635
    n = get_be16(pb);
636
    (*len)-=2;
637
    if (n >= 0x4000) {
638
        return n - 0x4000;
639
    } else {
640
        n1 = get_be16(pb);
641
        (*len)-=2;
642
        return (n << 16) | n1;
643
    }
644
}
645
646 de6d9b64 Fabrice Bellard
static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
647
{
648
    RMContext *rm = s->priv_data;
649
    ByteIOContext *pb = &s->pb;
650
    AVStream *st;
651
    int len, num, timestamp, i, tmp, j;
652
    UINT8 *ptr;
653 5d6ef63f Philip Gladstone
    int flags;
654 de6d9b64 Fabrice Bellard
655
 redo:
656
    if (rm->nb_packets == 0)
657
        return -EIO;
658
    get_be16(pb);
659
    len = get_be16(pb);
660
    if (len < 12)
661
        return -EIO;
662
    num = get_be16(pb);
663
    timestamp = get_be32(pb);
664
    get_byte(pb); /* reserved */
665 5d6ef63f Philip Gladstone
    flags = get_byte(pb); /* flags */
666 de6d9b64 Fabrice Bellard
    rm->nb_packets--;
667
    len -= 12;
668
669
    st = NULL;
670
    for(i=0;i<s->nb_streams;i++) {
671
        st = s->streams[i];
672
        if (num == st->id)
673
            break;
674
    }
675
    if (i == s->nb_streams) {
676
        /* skip packet if unknown number */
677
        url_fskip(pb, len);
678
        goto redo;
679
    }
680 5d6ef63f Philip Gladstone
681
    if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
682 4c2bc159 Michael Niedermayer
        int full_frame, h, pic_num;
683
 
684
        h= get_byte(pb);
685
        if ((h & 0xc0) == 0xc0) {
686
            int len2, pos;
687
            full_frame = 1;
688
            len2= get_num(pb, &len);
689
            pos = get_num(pb, &len);
690
            //printf("pos:%d\n",len);
691
            len -= 2;
692
        } else {
693
            int seq, frame_size, pos;
694
            full_frame = 0;
695
            seq = get_byte(pb);
696
            frame_size = get_num(pb, &len);
697
            pos = get_num(pb, &len);
698
            //printf("seq:%d, size:%d, pos:%d\n",seq,frame_size,pos);
699
            len -= 3;
700
        }
701
        /* picture number */
702
        pic_num= get_byte(pb);
703
704 144f0625 Michael Niedermayer
        //XXX/FIXME/HACK, demuxer should be fixed to send complete frames ...
705
        if(st->codec.slice_offset==NULL) st->codec.slice_offset= (int*)malloc(sizeof(int));
706
        st->codec.slice_count= full_frame; 
707
        st->codec.slice_offset[0]= 0;
708 5d6ef63f Philip Gladstone
    }
709
710 de6d9b64 Fabrice Bellard
    av_new_packet(pkt, len);
711
    pkt->stream_index = i;
712
    get_buffer(pb, pkt->data, len);
713 4c2bc159 Michael Niedermayer
714 de6d9b64 Fabrice Bellard
    /* for AC3, needs to swap bytes */
715
    if (st->codec.codec_id == CODEC_ID_AC3) {
716
        ptr = pkt->data;
717
        for(j=0;j<len;j+=2) {
718
            tmp = ptr[0];
719
            ptr[0] = ptr[1];
720
            ptr[1] = tmp;
721
                ptr += 2;
722
        }
723
    }
724
    return 0;
725
}
726
727
static int rm_read_close(AVFormatContext *s)
728
{
729
    return 0;
730
}
731
732 c9a65ca8 Fabrice Bellard
static int rm_probe(AVProbeData *p)
733
{
734
    /* check file header */
735
    if (p->buf_size <= 32)
736
        return 0;
737
    if (p->buf[0] == '.' && p->buf[1] == 'R' &&
738
        p->buf[2] == 'M' && p->buf[3] == 'F' &&
739
        p->buf[4] == 0 && p->buf[5] == 0)
740
        return AVPROBE_SCORE_MAX;
741
    else
742
        return 0;
743
}
744
745
AVInputFormat rm_iformat = {
746
    "rm",
747
    "rm format",
748
    sizeof(RMContext),
749
    rm_probe,
750
    rm_read_header,
751
    rm_read_packet,
752
    rm_read_close,
753
};
754
755
AVOutputFormat rm_oformat = {
756 de6d9b64 Fabrice Bellard
    "rm",
757
    "rm format",
758
    "audio/x-pn-realaudio",
759
    "rm,ra",
760 c9a65ca8 Fabrice Bellard
    sizeof(RMContext),
761 de6d9b64 Fabrice Bellard
    CODEC_ID_AC3,
762
    CODEC_ID_RV10,
763
    rm_write_header,
764
    rm_write_packet,
765
    rm_write_trailer,
766
};
767 c9a65ca8 Fabrice Bellard
768
int rm_init(void)
769
{
770
    av_register_input_format(&rm_iformat);
771
    av_register_output_format(&rm_oformat);
772
    return 0;
773
}