Statistics
| Branch: | Revision:

ffmpeg / libavformat / rm.c @ 20cd83e2

History | View | Annotate | Download (37.6 KB)

1 de6d9b64 Fabrice Bellard
/*
2 7fbde343 Aurelien Jacobs
 * "Real" compatible muxer and demuxer.
3 19720f15 Fabrice Bellard
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21
#include "avformat.h"
22
23
/* in ms */
24 115329f1 Diego Biurrun
#define BUFFER_DURATION 0
25 de6d9b64 Fabrice Bellard
26
typedef struct {
27
    int nb_packets;
28
    int packet_total_size;
29
    int packet_max_size;
30
    /* codec related output */
31
    int bit_rate;
32
    float frame_rate;
33
    int nb_frames;    /* current frame number */
34
    int total_frames; /* total number of frames */
35
    int num;
36
    AVCodecContext *enc;
37
} StreamInfo;
38
39
typedef struct {
40
    StreamInfo streams[2];
41
    StreamInfo *audio_stream, *video_stream;
42
    int data_pos; /* position of the data after the header */
43
    int nb_packets;
44 c9510803 Fabrice Bellard
    int old_format;
45 a8b81ec1 Michael Niedermayer
    int current_stream;
46
    int remaining_len;
47 e0f7e329 Benjamin Larsson
    /// Audio descrambling matrix parameters
48
    uint8_t *audiobuf; ///< place to store reordered audio data
49
    int64_t audiotimestamp; ///< Audio packet timestamp
50
    int sub_packet_cnt; // Subpacket counter, used while reading
51
    int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
52
    int audio_stream_num; ///< Stream number for audio packets
53
    int audio_pkt_cnt; ///< Output packet counter
54
    int audio_framesize; /// Audio frame size from container
55 a194f595 Roberto Togni
    int sub_packet_lengths[16]; /// Length of each aac subpacket
56 de6d9b64 Fabrice Bellard
} RMContext;
57
58 a9e35095 Diego Biurrun
#ifdef CONFIG_MUXERS
59 de6d9b64 Fabrice Bellard
static void put_str(ByteIOContext *s, const char *tag)
60
{
61
    put_be16(s,strlen(tag));
62
    while (*tag) {
63
        put_byte(s, *tag++);
64
    }
65
}
66
67
static void put_str8(ByteIOContext *s, const char *tag)
68
{
69
    put_byte(s, strlen(tag));
70
    while (*tag) {
71
        put_byte(s, *tag++);
72
    }
73
}
74
75 115329f1 Diego Biurrun
static void rv10_write_header(AVFormatContext *ctx,
76 de6d9b64 Fabrice Bellard
                              int data_size, int index_pos)
77
{
78
    RMContext *rm = ctx->priv_data;
79
    ByteIOContext *s = &ctx->pb;
80
    StreamInfo *stream;
81
    unsigned char *data_offset_ptr, *start_ptr;
82
    const char *desc, *mimetype;
83
    int nb_packets, packet_total_size, packet_max_size, size, packet_avg_size, i;
84
    int bit_rate, v, duration, flags, data_pos;
85
86
    start_ptr = s->buf_ptr;
87
88
    put_tag(s, ".RMF");
89
    put_be32(s,18); /* header size */
90
    put_be16(s,0);
91
    put_be32(s,0);
92
    put_be32(s,4 + ctx->nb_streams); /* num headers */
93
94
    put_tag(s,"PROP");
95
    put_be32(s, 50);
96
    put_be16(s, 0);
97
    packet_max_size = 0;
98
    packet_total_size = 0;
99
    nb_packets = 0;
100
    bit_rate = 0;
101
    duration = 0;
102
    for(i=0;i<ctx->nb_streams;i++) {
103
        StreamInfo *stream = &rm->streams[i];
104
        bit_rate += stream->bit_rate;
105
        if (stream->packet_max_size > packet_max_size)
106
            packet_max_size = stream->packet_max_size;
107
        nb_packets += stream->nb_packets;
108
        packet_total_size += stream->packet_total_size;
109
        /* select maximum duration */
110
        v = (int) (1000.0 * (float)stream->total_frames / stream->frame_rate);
111
        if (v > duration)
112
            duration = v;
113
    }
114
    put_be32(s, bit_rate); /* max bit rate */
115
    put_be32(s, bit_rate); /* avg bit rate */
116
    put_be32(s, packet_max_size);        /* max packet size */
117
    if (nb_packets > 0)
118
        packet_avg_size = packet_total_size / nb_packets;
119
    else
120
        packet_avg_size = 0;
121
    put_be32(s, packet_avg_size);        /* avg packet size */
122
    put_be32(s, nb_packets);  /* num packets */
123
    put_be32(s, duration); /* duration */
124
    put_be32(s, BUFFER_DURATION);           /* preroll */
125
    put_be32(s, index_pos);           /* index offset */
126
    /* computation of data the data offset */
127
    data_offset_ptr = s->buf_ptr;
128
    put_be32(s, 0);           /* data offset : will be patched after */
129
    put_be16(s, ctx->nb_streams);    /* num streams */
130
    flags = 1 | 2; /* save allowed & perfect play */
131
    if (url_is_streamed(s))
132
        flags |= 4; /* live broadcast */
133
    put_be16(s, flags);
134 115329f1 Diego Biurrun
135 de6d9b64 Fabrice Bellard
    /* comments */
136
137
    put_tag(s,"CONT");
138 115329f1 Diego Biurrun
    size = strlen(ctx->title) + strlen(ctx->author) + strlen(ctx->copyright) +
139 de6d9b64 Fabrice Bellard
        strlen(ctx->comment) + 4 * 2 + 10;
140
    put_be32(s,size);
141
    put_be16(s,0);
142
    put_str(s, ctx->title);
143
    put_str(s, ctx->author);
144
    put_str(s, ctx->copyright);
145
    put_str(s, ctx->comment);
146 115329f1 Diego Biurrun
147 de6d9b64 Fabrice Bellard
    for(i=0;i<ctx->nb_streams;i++) {
148
        int codec_data_size;
149
150
        stream = &rm->streams[i];
151 115329f1 Diego Biurrun
152 de6d9b64 Fabrice Bellard
        if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
153
            desc = "The Audio Stream";
154
            mimetype = "audio/x-pn-realaudio";
155
            codec_data_size = 73;
156
        } else {
157
            desc = "The Video Stream";
158
            mimetype = "video/x-pn-realvideo";
159
            codec_data_size = 34;
160
        }
161
162
        put_tag(s,"MDPR");
163
        size = 10 + 9 * 4 + strlen(desc) + strlen(mimetype) + codec_data_size;
164
        put_be32(s, size);
165
        put_be16(s, 0);
166
167
        put_be16(s, i); /* stream number */
168
        put_be32(s, stream->bit_rate); /* max bit rate */
169
        put_be32(s, stream->bit_rate); /* avg bit rate */
170
        put_be32(s, stream->packet_max_size);        /* max packet size */
171
        if (stream->nb_packets > 0)
172 115329f1 Diego Biurrun
            packet_avg_size = stream->packet_total_size /
173 de6d9b64 Fabrice Bellard
                stream->nb_packets;
174
        else
175
            packet_avg_size = 0;
176
        put_be32(s, packet_avg_size);        /* avg packet size */
177
        put_be32(s, 0);           /* start time */
178
        put_be32(s, BUFFER_DURATION);           /* preroll */
179
        /* duration */
180 5d6ef63f Philip Gladstone
        if (url_is_streamed(s) || !stream->total_frames)
181
            put_be32(s, (int)(3600 * 1000));
182
        else
183
            put_be32(s, (int)(stream->total_frames * 1000 / stream->frame_rate));
184 de6d9b64 Fabrice Bellard
        put_str8(s, desc);
185
        put_str8(s, mimetype);
186
        put_be32(s, codec_data_size);
187 115329f1 Diego Biurrun
188 de6d9b64 Fabrice Bellard
        if (stream->enc->codec_type == CODEC_TYPE_AUDIO) {
189
            int coded_frame_size, fscode, sample_rate;
190
            sample_rate = stream->enc->sample_rate;
191 115329f1 Diego Biurrun
            coded_frame_size = (stream->enc->bit_rate *
192 de6d9b64 Fabrice Bellard
                                stream->enc->frame_size) / (8 * sample_rate);
193
            /* audio codec info */
194
            put_tag(s, ".ra");
195
            put_byte(s, 0xfd);
196
            put_be32(s, 0x00040000); /* version */
197
            put_tag(s, ".ra4");
198
            put_be32(s, 0x01b53530); /* stream length */
199
            put_be16(s, 4); /* unknown */
200
            put_be32(s, 0x39); /* header size */
201
202
            switch(sample_rate) {
203
            case 48000:
204
            case 24000:
205
            case 12000:
206
                fscode = 1;
207
                break;
208
            default:
209
            case 44100:
210
            case 22050:
211
            case 11025:
212
                fscode = 2;
213
                break;
214
            case 32000:
215
            case 16000:
216
            case 8000:
217
                fscode = 3;
218
            }
219
            put_be16(s, fscode); /* codec additional info, for AC3, seems
220
                                     to be a frequency code */
221
            /* special hack to compensate rounding errors... */
222
            if (coded_frame_size == 557)
223
                coded_frame_size--;
224
            put_be32(s, coded_frame_size); /* frame length */
225
            put_be32(s, 0x51540); /* unknown */
226
            put_be32(s, 0x249f0); /* unknown */
227
            put_be32(s, 0x249f0); /* unknown */
228
            put_be16(s, 0x01);
229
            /* frame length : seems to be very important */
230 115329f1 Diego Biurrun
            put_be16(s, coded_frame_size);
231 de6d9b64 Fabrice Bellard
            put_be32(s, 0); /* unknown */
232
            put_be16(s, stream->enc->sample_rate); /* sample rate */
233
            put_be32(s, 0x10); /* unknown */
234
            put_be16(s, stream->enc->channels);
235
            put_str8(s, "Int0"); /* codec name */
236
            put_str8(s, "dnet"); /* codec name */
237
            put_be16(s, 0); /* title length */
238
            put_be16(s, 0); /* author length */
239
            put_be16(s, 0); /* copyright length */
240
            put_byte(s, 0); /* end of header */
241
        } else {
242
            /* video codec info */
243
            put_be32(s,34); /* size */
244 d0271e8a Michael Niedermayer
            if(stream->enc->codec_id == CODEC_ID_RV10)
245
                put_tag(s,"VIDORV10");
246
            else
247
                put_tag(s,"VIDORV20");
248 de6d9b64 Fabrice Bellard
            put_be16(s, stream->enc->width);
249
            put_be16(s, stream->enc->height);
250 5d6ef63f Philip Gladstone
            put_be16(s, (int) stream->frame_rate); /* frames per seconds ? */
251 de6d9b64 Fabrice Bellard
            put_be32(s,0);     /* unknown meaning */
252 5d6ef63f Philip Gladstone
            put_be16(s, (int) stream->frame_rate);  /* unknown meaning */
253 de6d9b64 Fabrice Bellard
            put_be32(s,0);     /* unknown meaning */
254
            put_be16(s, 8);    /* unknown meaning */
255
            /* Seems to be the codec version: only use basic H263. The next
256
               versions seems to add a diffential DC coding as in
257
               MPEG... nothing new under the sun */
258 d0271e8a Michael Niedermayer
            if(stream->enc->codec_id == CODEC_ID_RV10)
259 115329f1 Diego Biurrun
                put_be32(s,0x10000000);
260 d0271e8a Michael Niedermayer
            else
261 115329f1 Diego Biurrun
                put_be32(s,0x20103001);
262
            //put_be32(s,0x10003000);
263 de6d9b64 Fabrice Bellard
        }
264
    }
265
266
    /* patch data offset field */
267
    data_pos = s->buf_ptr - start_ptr;
268
    rm->data_pos = data_pos;
269
    data_offset_ptr[0] = data_pos >> 24;
270
    data_offset_ptr[1] = data_pos >> 16;
271
    data_offset_ptr[2] = data_pos >> 8;
272
    data_offset_ptr[3] = data_pos;
273 115329f1 Diego Biurrun
274 de6d9b64 Fabrice Bellard
    /* data stream */
275
    put_tag(s,"DATA");
276
    put_be32(s,data_size + 10 + 8);
277
    put_be16(s,0);
278
279
    put_be32(s, nb_packets); /* number of packets */
280
    put_be32(s,0); /* next data header */
281
}
282
283 115329f1 Diego Biurrun
static void write_packet_header(AVFormatContext *ctx, StreamInfo *stream,
284 de6d9b64 Fabrice Bellard
                                int length, int key_frame)
285
{
286
    int timestamp;
287
    ByteIOContext *s = &ctx->pb;
288
289
    stream->nb_packets++;
290
    stream->packet_total_size += length;
291
    if (length > stream->packet_max_size)
292
        stream->packet_max_size =  length;
293
294
    put_be16(s,0); /* version */
295
    put_be16(s,length + 12);
296
    put_be16(s, stream->num); /* stream number */
297
    timestamp = (1000 * (float)stream->nb_frames) / stream->frame_rate;
298
    put_be32(s, timestamp); /* timestamp */
299
    put_byte(s, 0); /* reserved */
300
    put_byte(s, key_frame ? 2 : 0); /* flags */
301
}
302
303
static int rm_write_header(AVFormatContext *s)
304
{
305 c9a65ca8 Fabrice Bellard
    RMContext *rm = s->priv_data;
306 de6d9b64 Fabrice Bellard
    StreamInfo *stream;
307
    int n;
308
    AVCodecContext *codec;
309
310
    for(n=0;n<s->nb_streams;n++) {
311
        s->streams[n]->id = n;
312 01f4895c Michael Niedermayer
        codec = s->streams[n]->codec;
313 de6d9b64 Fabrice Bellard
        stream = &rm->streams[n];
314
        memset(stream, 0, sizeof(StreamInfo));
315
        stream->num = n;
316
        stream->bit_rate = codec->bit_rate;
317
        stream->enc = codec;
318
319
        switch(codec->codec_type) {
320
        case CODEC_TYPE_AUDIO:
321
            rm->audio_stream = stream;
322
            stream->frame_rate = (float)codec->sample_rate / (float)codec->frame_size;
323
            /* XXX: dummy values */
324
            stream->packet_max_size = 1024;
325
            stream->nb_packets = 0;
326
            stream->total_frames = stream->nb_packets;
327
            break;
328
        case CODEC_TYPE_VIDEO:
329
            rm->video_stream = stream;
330 c0df9d75 Michael Niedermayer
            stream->frame_rate = (float)codec->time_base.den / (float)codec->time_base.num;
331 de6d9b64 Fabrice Bellard
            /* XXX: dummy values */
332
            stream->packet_max_size = 4096;
333
            stream->nb_packets = 0;
334
            stream->total_frames = stream->nb_packets;
335
            break;
336 5d6ef63f Philip Gladstone
        default:
337 71c32f19 Michael Niedermayer
            return -1;
338 de6d9b64 Fabrice Bellard
        }
339
    }
340
341
    rv10_write_header(s, 0, 0);
342
    put_flush_packet(&s->pb);
343
    return 0;
344
}
345
346 3c895fc0 Michael Niedermayer
static int rm_write_audio(AVFormatContext *s, const uint8_t *buf, int size, int flags)
347 de6d9b64 Fabrice Bellard
{
348 0c1a9eda Zdenek Kabelac
    uint8_t *buf1;
349 de6d9b64 Fabrice Bellard
    RMContext *rm = s->priv_data;
350
    ByteIOContext *pb = &s->pb;
351
    StreamInfo *stream = rm->audio_stream;
352
    int i;
353
354 8be1c656 Fabrice Bellard
    /* XXX: suppress this malloc */
355 0c1a9eda Zdenek Kabelac
    buf1= (uint8_t*) av_malloc( size * sizeof(uint8_t) );
356 115329f1 Diego Biurrun
357 3c895fc0 Michael Niedermayer
    write_packet_header(s, stream, size, !!(flags & PKT_FLAG_KEY));
358 115329f1 Diego Biurrun
359 de6d9b64 Fabrice Bellard
    /* for AC3, the words seems to be reversed */
360
    for(i=0;i<size;i+=2) {
361
        buf1[i] = buf[i+1];
362
        buf1[i+1] = buf[i];
363
    }
364
    put_buffer(pb, buf1, size);
365
    put_flush_packet(pb);
366
    stream->nb_frames++;
367 1ea4f593 Fabrice Bellard
    av_free(buf1);
368 de6d9b64 Fabrice Bellard
    return 0;
369
}
370
371 3c895fc0 Michael Niedermayer
static int rm_write_video(AVFormatContext *s, const uint8_t *buf, int size, int flags)
372 de6d9b64 Fabrice Bellard
{
373
    RMContext *rm = s->priv_data;
374
    ByteIOContext *pb = &s->pb;
375
    StreamInfo *stream = rm->video_stream;
376 3c895fc0 Michael Niedermayer
    int key_frame = !!(flags & PKT_FLAG_KEY);
377 5d6ef63f Philip Gladstone
378 de6d9b64 Fabrice Bellard
    /* XXX: this is incorrect: should be a parameter */
379
380
    /* Well, I spent some time finding the meaning of these bits. I am
381
       not sure I understood everything, but it works !! */
382
#if 1
383
    write_packet_header(s, stream, size + 7, key_frame);
384
    /* bit 7: '1' if final packet of a frame converted in several packets */
385 115329f1 Diego Biurrun
    put_byte(pb, 0x81);
386 de6d9b64 Fabrice Bellard
    /* bit 7: '1' if I frame. bits 6..0 : sequence number in current
387
       frame starting from 1 */
388
    if (key_frame) {
389 115329f1 Diego Biurrun
        put_byte(pb, 0x81);
390 de6d9b64 Fabrice Bellard
    } else {
391 115329f1 Diego Biurrun
        put_byte(pb, 0x01);
392 de6d9b64 Fabrice Bellard
    }
393 80016c48 Michael Niedermayer
    put_be16(pb, 0x4000 + (size)); /* total frame size */
394
    put_be16(pb, 0x4000 + (size));              /* offset from the start or the end */
395 de6d9b64 Fabrice Bellard
#else
396
    /* full frame */
397
    write_packet_header(s, size + 6);
398 115329f1 Diego Biurrun
    put_byte(pb, 0xc0);
399 80016c48 Michael Niedermayer
    put_be16(pb, 0x4000 + size); /* total frame size */
400 de6d9b64 Fabrice Bellard
    put_be16(pb, 0x4000 + packet_number * 126); /* position in stream */
401
#endif
402 115329f1 Diego Biurrun
    put_byte(pb, stream->nb_frames & 0xff);
403
404 de6d9b64 Fabrice Bellard
    put_buffer(pb, buf, size);
405
    put_flush_packet(pb);
406
407
    stream->nb_frames++;
408
    return 0;
409
}
410
411 e928649b Michael Niedermayer
static int rm_write_packet(AVFormatContext *s, AVPacket *pkt)
412 de6d9b64 Fabrice Bellard
{
413 115329f1 Diego Biurrun
    if (s->streams[pkt->stream_index]->codec->codec_type ==
414 de6d9b64 Fabrice Bellard
        CODEC_TYPE_AUDIO)
415 3c895fc0 Michael Niedermayer
        return rm_write_audio(s, pkt->data, pkt->size, pkt->flags);
416 de6d9b64 Fabrice Bellard
    else
417 3c895fc0 Michael Niedermayer
        return rm_write_video(s, pkt->data, pkt->size, pkt->flags);
418 de6d9b64 Fabrice Bellard
}
419 115329f1 Diego Biurrun
420 de6d9b64 Fabrice Bellard
static int rm_write_trailer(AVFormatContext *s)
421
{
422
    RMContext *rm = s->priv_data;
423
    int data_size, index_pos, i;
424
    ByteIOContext *pb = &s->pb;
425
426
    if (!url_is_streamed(&s->pb)) {
427
        /* end of file: finish to write header */
428
        index_pos = url_fseek(pb, 0, SEEK_CUR);
429
        data_size = index_pos - rm->data_pos;
430
431
        /* index */
432
        put_tag(pb, "INDX");
433
        put_be32(pb, 10 + 10 * s->nb_streams);
434
        put_be16(pb, 0);
435 115329f1 Diego Biurrun
436 de6d9b64 Fabrice Bellard
        for(i=0;i<s->nb_streams;i++) {
437
            put_be32(pb, 0); /* zero indices */
438
            put_be16(pb, i); /* stream number */
439
            put_be32(pb, 0); /* next index */
440
        }
441
        /* undocumented end header */
442
        put_be32(pb, 0);
443
        put_be32(pb, 0);
444 115329f1 Diego Biurrun
445 de6d9b64 Fabrice Bellard
        url_fseek(pb, 0, SEEK_SET);
446
        for(i=0;i<s->nb_streams;i++)
447
            rm->streams[i].total_frames = rm->streams[i].nb_frames;
448
        rv10_write_header(s, data_size, index_pos);
449
    } else {
450
        /* undocumented end header */
451
        put_be32(pb, 0);
452
        put_be32(pb, 0);
453
    }
454
    put_flush_packet(pb);
455
    return 0;
456
}
457 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
458 de6d9b64 Fabrice Bellard
459
/***************************************************/
460
461
static void get_str(ByteIOContext *pb, char *buf, int buf_size)
462
{
463
    int len, i;
464
    char *q;
465
466
    len = get_be16(pb);
467
    q = buf;
468
    for(i=0;i<len;i++) {
469
        if (i < buf_size - 1)
470
            *q++ = get_byte(pb);
471
    }
472
    *q = '\0';
473
}
474
475
static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
476
{
477
    int len, i;
478
    char *q;
479
480
    len = get_byte(pb);
481
    q = buf;
482
    for(i=0;i<len;i++) {
483
        if (i < buf_size - 1)
484
            *q++ = get_byte(pb);
485
    }
486
    *q = '\0';
487
}
488
489 a9d4a6ef Roberto Togni
static int rm_read_audio_stream_info(AVFormatContext *s, AVStream *st,
490 c9510803 Fabrice Bellard
                                      int read_all)
491
{
492 e0f7e329 Benjamin Larsson
    RMContext *rm = s->priv_data;
493 c9510803 Fabrice Bellard
    ByteIOContext *pb = &s->pb;
494 1e4668d1 Roberto Togni
    char buf[256];
495 c9510803 Fabrice Bellard
    uint32_t version;
496
    int i;
497
498
    /* ra type header */
499
    version = get_be32(pb); /* version */
500
    if (((version >> 16) & 0xff) == 3) {
501 1e4668d1 Roberto Togni
        int64_t startpos = url_ftell(pb);
502 c9510803 Fabrice Bellard
        /* very old version */
503
        for(i = 0; i < 14; i++)
504
            get_byte(pb);
505
        get_str8(pb, s->title, sizeof(s->title));
506
        get_str8(pb, s->author, sizeof(s->author));
507
        get_str8(pb, s->copyright, sizeof(s->copyright));
508
        get_str8(pb, s->comment, sizeof(s->comment));
509 1e4668d1 Roberto Togni
        if ((startpos + (version & 0xffff)) >= url_ftell(pb) + 2) {
510
        // fourcc (should always be "lpcJ")
511 c9510803 Fabrice Bellard
        get_byte(pb);
512
        get_str8(pb, buf, sizeof(buf));
513 1e4668d1 Roberto Togni
        }
514
        // Skip extra header crap (this should never happen)
515
        if ((startpos + (version & 0xffff)) > url_ftell(pb))
516
            url_fskip(pb, (version & 0xffff) + startpos - url_ftell(pb));
517 01f4895c Michael Niedermayer
        st->codec->sample_rate = 8000;
518
        st->codec->channels = 1;
519
        st->codec->codec_type = CODEC_TYPE_AUDIO;
520
        st->codec->codec_id = CODEC_ID_RA_144;
521 c9510803 Fabrice Bellard
    } else {
522 e0f7e329 Benjamin Larsson
        int flavor, sub_packet_h, coded_framesize, sub_packet_size;
523 c9510803 Fabrice Bellard
        /* old version (4) */
524
        get_be32(pb); /* .ra4 */
525 72aef198 Michael Niedermayer
        get_be32(pb); /* data size */
526
        get_be16(pb); /* version2 */
527 c9510803 Fabrice Bellard
        get_be32(pb); /* header size */
528 72aef198 Michael Niedermayer
        flavor= get_be16(pb); /* add codec info / flavor */
529 e0f7e329 Benjamin Larsson
        rm->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
530 c9510803 Fabrice Bellard
        get_be32(pb); /* ??? */
531
        get_be32(pb); /* ??? */
532
        get_be32(pb); /* ??? */
533 115329f1 Diego Biurrun
        rm->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
534 01f4895c Michael Niedermayer
        st->codec->block_align= get_be16(pb); /* frame size */
535 e0f7e329 Benjamin Larsson
        rm->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
536 72aef198 Michael Niedermayer
        get_be16(pb); /* ??? */
537 e0f7e329 Benjamin Larsson
        if (((version >> 16) & 0xff) == 5) {
538
            get_be16(pb); get_be16(pb); get_be16(pb); }
539 01f4895c Michael Niedermayer
        st->codec->sample_rate = get_be16(pb);
540 c9510803 Fabrice Bellard
        get_be32(pb);
541 01f4895c Michael Niedermayer
        st->codec->channels = get_be16(pb);
542 e0f7e329 Benjamin Larsson
        if (((version >> 16) & 0xff) == 5) {
543
            get_be32(pb);
544 bb270c08 Diego Biurrun
            buf[0] = get_byte(pb);
545
            buf[1] = get_byte(pb);
546
            buf[2] = get_byte(pb);
547
            buf[3] = get_byte(pb);
548
            buf[4] = 0;
549
        } else {
550 c9510803 Fabrice Bellard
        get_str8(pb, buf, sizeof(buf)); /* desc */
551
        get_str8(pb, buf, sizeof(buf)); /* desc */
552 bb270c08 Diego Biurrun
        }
553 01f4895c Michael Niedermayer
        st->codec->codec_type = CODEC_TYPE_AUDIO;
554 c9510803 Fabrice Bellard
        if (!strcmp(buf, "dnet")) {
555 01f4895c Michael Niedermayer
            st->codec->codec_id = CODEC_ID_AC3;
556 72aef198 Michael Niedermayer
        } else if (!strcmp(buf, "28_8")) {
557 01f4895c Michael Niedermayer
            st->codec->codec_id = CODEC_ID_RA_288;
558 e0f7e329 Benjamin Larsson
            st->codec->extradata_size= 0;
559
            rm->audio_framesize = st->codec->block_align;
560
            st->codec->block_align = coded_framesize;
561 a443a253 Michael Niedermayer
562
            if(rm->audio_framesize >= UINT_MAX / sub_packet_h){
563
                av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
564
                return -1;
565
            }
566
567 e0f7e329 Benjamin Larsson
            rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
568
        } else if (!strcmp(buf, "cook")) {
569
            int codecdata_length, i;
570
            get_be16(pb); get_byte(pb);
571
            if (((version >> 16) & 0xff) == 5)
572
                get_byte(pb);
573
            codecdata_length = get_be32(pb);
574 a443a253 Michael Niedermayer
            if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
575
                av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
576
                return -1;
577
            }
578
579 e0f7e329 Benjamin Larsson
            st->codec->codec_id = CODEC_ID_COOK;
580
            st->codec->extradata_size= codecdata_length;
581 d76319b1 Jindřich Makovička
            st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
582 e0f7e329 Benjamin Larsson
            for(i = 0; i < codecdata_length; i++)
583
                ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
584
            rm->audio_framesize = st->codec->block_align;
585
            st->codec->block_align = rm->sub_packet_size;
586 a443a253 Michael Niedermayer
587
            if(rm->audio_framesize >= UINT_MAX / sub_packet_h){
588
                av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
589
                return -1;
590
            }
591
592 e0f7e329 Benjamin Larsson
            rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
593 a194f595 Roberto Togni
        } else if (!strcmp(buf, "raac") || !strcmp(buf, "racp")) {
594
            int codecdata_length, i;
595
            get_be16(pb); get_byte(pb);
596
            if (((version >> 16) & 0xff) == 5)
597
                get_byte(pb);
598
            st->codec->codec_id = CODEC_ID_AAC;
599
            codecdata_length = get_be32(pb);
600 a9d4a6ef Roberto Togni
            if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
601
                av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
602
                return -1;
603
            }
604 a194f595 Roberto Togni
            if (codecdata_length >= 1) {
605
                st->codec->extradata_size = codecdata_length - 1;
606
                st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
607
                get_byte(pb);
608
                for(i = 0; i < st->codec->extradata_size; i++)
609
                    ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
610
            }
611 c9510803 Fabrice Bellard
        } else {
612 01f4895c Michael Niedermayer
            st->codec->codec_id = CODEC_ID_NONE;
613
            pstrcpy(st->codec->codec_name, sizeof(st->codec->codec_name),
614 c9510803 Fabrice Bellard
                    buf);
615
        }
616
        if (read_all) {
617
            get_byte(pb);
618
            get_byte(pb);
619
            get_byte(pb);
620 115329f1 Diego Biurrun
621 c9510803 Fabrice Bellard
            get_str8(pb, s->title, sizeof(s->title));
622
            get_str8(pb, s->author, sizeof(s->author));
623
            get_str8(pb, s->copyright, sizeof(s->copyright));
624
            get_str8(pb, s->comment, sizeof(s->comment));
625
        }
626
    }
627 a9d4a6ef Roberto Togni
    return 0;
628 c9510803 Fabrice Bellard
}
629
630
static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
631
{
632
    RMContext *rm = s->priv_data;
633
    AVStream *st;
634
635
    rm->old_format = 1;
636
    st = av_new_stream(s, 0);
637
    if (!st)
638 a9d4a6ef Roberto Togni
        return -1;
639
    return rm_read_audio_stream_info(s, st, 1);
640 c9510803 Fabrice Bellard
}
641
642 de6d9b64 Fabrice Bellard
static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
643
{
644 c9a65ca8 Fabrice Bellard
    RMContext *rm = s->priv_data;
645 de6d9b64 Fabrice Bellard
    AVStream *st;
646
    ByteIOContext *pb = &s->pb;
647
    unsigned int tag, v;
648
    int tag_size, size, codec_data_size, i;
649 0c1a9eda Zdenek Kabelac
    int64_t codec_pos;
650 36c32bdd Diego Biurrun
    unsigned int start_time, duration;
651 de6d9b64 Fabrice Bellard
    char buf[128];
652 5d6ef63f Philip Gladstone
    int flags = 0;
653 de6d9b64 Fabrice Bellard
654 c9510803 Fabrice Bellard
    tag = get_le32(pb);
655
    if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
656
        /* very old .ra format */
657
        return rm_read_header_old(s, ap);
658
    } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
659 0bd586c5 Mike Melanson
        return AVERROR_IO;
660 c9510803 Fabrice Bellard
    }
661 de6d9b64 Fabrice Bellard
662
    get_be32(pb); /* header size */
663
    get_be16(pb);
664
    get_be32(pb);
665
    get_be32(pb); /* number of headers */
666 115329f1 Diego Biurrun
667 de6d9b64 Fabrice Bellard
    for(;;) {
668
        if (url_feof(pb))
669
            goto fail;
670
        tag = get_le32(pb);
671
        tag_size = get_be32(pb);
672
        get_be16(pb);
673
#if 0
674 115329f1 Diego Biurrun
        printf("tag=%c%c%c%c (%08x) size=%d\n",
675 de6d9b64 Fabrice Bellard
               (tag) & 0xff,
676
               (tag >> 8) & 0xff,
677
               (tag >> 16) & 0xff,
678
               (tag >> 24) & 0xff,
679
               tag,
680
               tag_size);
681
#endif
682 9786bd4d Michael Niedermayer
        if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
683 de6d9b64 Fabrice Bellard
            goto fail;
684
        switch(tag) {
685
        case MKTAG('P', 'R', 'O', 'P'):
686
            /* file header */
687
            get_be32(pb); /* max bit rate */
688
            get_be32(pb); /* avg bit rate */
689
            get_be32(pb); /* max packet size */
690
            get_be32(pb); /* avg packet size */
691
            get_be32(pb); /* nb packets */
692
            get_be32(pb); /* duration */
693
            get_be32(pb); /* preroll */
694
            get_be32(pb); /* index offset */
695
            get_be32(pb); /* data offset */
696
            get_be16(pb); /* nb streams */
697 5d6ef63f Philip Gladstone
            flags = get_be16(pb); /* flags */
698 de6d9b64 Fabrice Bellard
            break;
699
        case MKTAG('C', 'O', 'N', 'T'):
700
            get_str(pb, s->title, sizeof(s->title));
701
            get_str(pb, s->author, sizeof(s->author));
702
            get_str(pb, s->copyright, sizeof(s->copyright));
703
            get_str(pb, s->comment, sizeof(s->comment));
704
            break;
705
        case MKTAG('M', 'D', 'P', 'R'):
706 247eadca Fabrice Bellard
            st = av_new_stream(s, 0);
707 de6d9b64 Fabrice Bellard
            if (!st)
708
                goto fail;
709
            st->id = get_be16(pb);
710
            get_be32(pb); /* max bit rate */
711 01f4895c Michael Niedermayer
            st->codec->bit_rate = get_be32(pb); /* bit rate */
712 de6d9b64 Fabrice Bellard
            get_be32(pb); /* max packet size */
713
            get_be32(pb); /* avg packet size */
714 247eadca Fabrice Bellard
            start_time = get_be32(pb); /* start time */
715 de6d9b64 Fabrice Bellard
            get_be32(pb); /* preroll */
716 247eadca Fabrice Bellard
            duration = get_be32(pb); /* duration */
717 c0df9d75 Michael Niedermayer
            st->start_time = start_time;
718
            st->duration = duration;
719 de6d9b64 Fabrice Bellard
            get_str8(pb, buf, sizeof(buf)); /* desc */
720
            get_str8(pb, buf, sizeof(buf)); /* mimetype */
721
            codec_data_size = get_be32(pb);
722
            codec_pos = url_ftell(pb);
723 01f4895c Michael Niedermayer
            st->codec->codec_type = CODEC_TYPE_DATA;
724 d880240e Michael Niedermayer
            av_set_pts_info(st, 64, 1, 1000);
725 de6d9b64 Fabrice Bellard
726
            v = get_be32(pb);
727
            if (v == MKTAG(0xfd, 'a', 'r', '.')) {
728
                /* ra type header */
729 a9d4a6ef Roberto Togni
                if (rm_read_audio_stream_info(s, st, 0))
730
                    return -1;
731 de6d9b64 Fabrice Bellard
            } else {
732 423962dc Michael Niedermayer
                int fps, fps2;
733 de6d9b64 Fabrice Bellard
                if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
734
                fail1:
735 01f4895c Michael Niedermayer
                    av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
736 f7ff7bc6 Michael Niedermayer
                    goto skip;
737 de6d9b64 Fabrice Bellard
                }
738 01f4895c Michael Niedermayer
                st->codec->codec_tag = get_le32(pb);
739
//                av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
740
                if (   st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
741
                    && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
742
                    && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
743
                    && st->codec->codec_tag != MKTAG('R', 'V', '4', '0'))
744 de6d9b64 Fabrice Bellard
                    goto fail1;
745 01f4895c Michael Niedermayer
                st->codec->width = get_be16(pb);
746
                st->codec->height = get_be16(pb);
747
                st->codec->time_base.num= 1;
748 423962dc Michael Niedermayer
                fps= get_be16(pb);
749 01f4895c Michael Niedermayer
                st->codec->codec_type = CODEC_TYPE_VIDEO;
750 de6d9b64 Fabrice Bellard
                get_be32(pb);
751 423962dc Michael Niedermayer
                fps2= get_be16(pb);
752 de6d9b64 Fabrice Bellard
                get_be16(pb);
753 115329f1 Diego Biurrun
754 01f4895c Michael Niedermayer
                st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
755 a443a253 Michael Niedermayer
756
                if(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
757
                    //check is redundant as get_buffer() will catch this
758
                    av_log(s, AV_LOG_ERROR, "st->codec->extradata_size too large\n");
759
                    return -1;
760
                }
761 d76319b1 Jindřich Makovička
                st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
762 01f4895c Michael Niedermayer
                get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
763 115329f1 Diego Biurrun
764 423962dc Michael Niedermayer
//                av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
765 01f4895c Michael Niedermayer
                st->codec->time_base.den = fps * st->codec->time_base.num;
766 cb37931d Michael Niedermayer
                switch(((uint8_t*)st->codec->extradata)[4]>>4){
767 01f4895c Michael Niedermayer
                case 1: st->codec->codec_id = CODEC_ID_RV10; break;
768
                case 2: st->codec->codec_id = CODEC_ID_RV20; break;
769
                case 3: st->codec->codec_id = CODEC_ID_RV30; break;
770
                case 4: st->codec->codec_id = CODEC_ID_RV40; break;
771 3f05305d Michael Niedermayer
                default: goto fail1;
772
                }
773 de6d9b64 Fabrice Bellard
            }
774 f7ff7bc6 Michael Niedermayer
skip:
775 de6d9b64 Fabrice Bellard
            /* skip codec info */
776
            size = url_ftell(pb) - codec_pos;
777
            url_fskip(pb, codec_data_size - size);
778
            break;
779
        case MKTAG('D', 'A', 'T', 'A'):
780
            goto header_end;
781
        default:
782
            /* unknown tag: skip it */
783
            url_fskip(pb, tag_size - 10);
784
            break;
785
        }
786
    }
787
 header_end:
788
    rm->nb_packets = get_be32(pb); /* number of packets */
789 5d6ef63f Philip Gladstone
    if (!rm->nb_packets && (flags & 4))
790
        rm->nb_packets = 3600 * 25;
791 de6d9b64 Fabrice Bellard
    get_be32(pb); /* next data header */
792
    return 0;
793
794
 fail:
795
    for(i=0;i<s->nb_streams;i++) {
796 1ea4f593 Fabrice Bellard
        av_free(s->streams[i]);
797 de6d9b64 Fabrice Bellard
    }
798 0bd586c5 Mike Melanson
    return AVERROR_IO;
799 de6d9b64 Fabrice Bellard
}
800
801 4c2bc159 Michael Niedermayer
static int get_num(ByteIOContext *pb, int *len)
802
{
803
    int n, n1;
804
805
    n = get_be16(pb);
806
    (*len)-=2;
807
    if (n >= 0x4000) {
808
        return n - 0x4000;
809
    } else {
810
        n1 = get_be16(pb);
811
        (*len)-=2;
812
        return (n << 16) | n1;
813
    }
814
}
815
816 c9510803 Fabrice Bellard
/* multiple of 20 bytes for ra144 (ugly) */
817
#define RAW_PACKET_SIZE 1000
818
819 37e85dac Michael Niedermayer
static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
820 bc17df09 Michael Niedermayer
    RMContext *rm = s->priv_data;
821
    ByteIOContext *pb = &s->pb;
822
    int len, num, res, i;
823
    AVStream *st;
824 119cebf4 Michael Niedermayer
    uint32_t state=0xFFFFFFFF;
825 bc17df09 Michael Niedermayer
826
    while(!url_feof(pb)){
827 37e85dac Michael Niedermayer
        *pos= url_ftell(pb);
828 bc17df09 Michael Niedermayer
        if(rm->remaining_len > 0){
829
            num= rm->current_stream;
830
            len= rm->remaining_len;
831
            *timestamp = AV_NOPTS_VALUE;
832
            *flags= 0;
833
        }else{
834 119cebf4 Michael Niedermayer
            state= (state<<8) + get_byte(pb);
835 115329f1 Diego Biurrun
836 119cebf4 Michael Niedermayer
            if(state == MKBETAG('I', 'N', 'D', 'X')){
837
                len = get_be16(pb) - 6;
838
                if(len<0)
839
                    continue;
840
                goto skip;
841
            }
842 115329f1 Diego Biurrun
843 119cebf4 Michael Niedermayer
            if(state > (unsigned)0xFFFF || state < 12)
844 bc17df09 Michael Niedermayer
                continue;
845 119cebf4 Michael Niedermayer
            len=state;
846
            state= 0xFFFFFFFF;
847
848 bc17df09 Michael Niedermayer
            num = get_be16(pb);
849
            *timestamp = get_be32(pb);
850
            res= get_byte(pb); /* reserved */
851
            *flags = get_byte(pb); /* flags */
852 37e85dac Michael Niedermayer
853 115329f1 Diego Biurrun
854 bc17df09 Michael Niedermayer
            len -= 12;
855
        }
856
        for(i=0;i<s->nb_streams;i++) {
857
            st = s->streams[i];
858
            if (num == st->id)
859
                break;
860
        }
861
        if (i == s->nb_streams) {
862 119cebf4 Michael Niedermayer
skip:
863 bc17df09 Michael Niedermayer
            /* skip packet if unknown number */
864
            url_fskip(pb, len);
865
            rm->remaining_len -= len;
866
            continue;
867
        }
868
        *stream_index= i;
869 115329f1 Diego Biurrun
870 bc17df09 Michael Niedermayer
        return len;
871
    }
872
    return -1;
873
}
874
875 de6d9b64 Fabrice Bellard
static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
876
{
877
    RMContext *rm = s->priv_data;
878
    ByteIOContext *pb = &s->pb;
879
    AVStream *st;
880 bc17df09 Michael Niedermayer
    int i, len, tmp, j;
881 37e85dac Michael Niedermayer
    int64_t timestamp, pos;
882 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
883 bc17df09 Michael Niedermayer
    int flags;
884 de6d9b64 Fabrice Bellard
885 c69e7914 Roberto Togni
    if (rm->audio_pkt_cnt) {
886 e0f7e329 Benjamin Larsson
        // If there are queued audio packet return them first
887
        st = s->streams[rm->audio_stream_num];
888 a194f595 Roberto Togni
        if (st->codec->codec_id == CODEC_ID_AAC)
889
            av_get_packet(pb, pkt, rm->sub_packet_lengths[rm->sub_packet_cnt - rm->audio_pkt_cnt]);
890
        else {
891 e0f7e329 Benjamin Larsson
        av_new_packet(pkt, st->codec->block_align);
892
        memcpy(pkt->data, rm->audiobuf + st->codec->block_align *
893
               (rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
894
               st->codec->block_align);
895 a194f595 Roberto Togni
        }
896 e0f7e329 Benjamin Larsson
        rm->audio_pkt_cnt--;
897
        pkt->flags = 0;
898
        pkt->stream_index = rm->audio_stream_num;
899 c69e7914 Roberto Togni
    } else if (rm->old_format) {
900
        st = s->streams[0];
901
        if (st->codec->codec_id == CODEC_ID_RA_288) {
902
            int x, y;
903
904
            for (y = 0; y < rm->sub_packet_h; y++)
905
                for (x = 0; x < rm->sub_packet_h/2; x++)
906
                    if (get_buffer(pb, rm->audiobuf+x*2*rm->audio_framesize+y*rm->coded_framesize, rm->coded_framesize) <= 0)
907
                        return AVERROR_IO;
908
            rm->audio_stream_num = 0;
909
            rm->audio_pkt_cnt = rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - 1;
910
            // Release first audio packet
911
            av_new_packet(pkt, st->codec->block_align);
912
            memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
913
            pkt->flags |= PKT_FLAG_KEY; // Mark first packet as keyframe
914
            pkt->stream_index = 0;
915
        } else {
916
            /* just read raw bytes */
917
            len = RAW_PACKET_SIZE;
918
            len= av_get_packet(pb, pkt, len);
919
            pkt->stream_index = 0;
920
            if (len <= 0) {
921
                return AVERROR_IO;
922
            }
923
            pkt->size = len;
924
        }
925 c9510803 Fabrice Bellard
    } else {
926 37e85dac Michael Niedermayer
        int seq=1;
927 b9866ebc Michael Niedermayer
resync:
928 37e85dac Michael Niedermayer
        len=sync(s, &timestamp, &flags, &i, &pos);
929 bc17df09 Michael Niedermayer
        if(len<0)
930 012cd15f Michael Niedermayer
            return AVERROR_IO;
931 bc17df09 Michael Niedermayer
        st = s->streams[i];
932
933 01f4895c Michael Niedermayer
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
934 a8b81ec1 Michael Niedermayer
            int h, pic_num, len2, pos;
935
936
            h= get_byte(pb); len--;
937
            if(!(h & 0x40)){
938 37e85dac Michael Niedermayer
                seq = get_byte(pb); len--;
939 a8b81ec1 Michael Niedermayer
            }
940
941
            if((h & 0xc0) == 0x40){
942
                len2= pos= 0;
943
            }else{
944
                len2 = get_num(pb, &len);
945 c9510803 Fabrice Bellard
                pos = get_num(pb, &len);
946
            }
947
            /* picture number */
948 a8b81ec1 Michael Niedermayer
            pic_num= get_byte(pb); len--;
949
            rm->remaining_len= len;
950
            rm->current_stream= st->id;
951
952
//            av_log(NULL, AV_LOG_DEBUG, "%X len:%d pos:%d len2:%d pic_num:%d\n",h, len, pos, len2, pic_num);
953
            if(len2 && len2<len)
954
                len=len2;
955
            rm->remaining_len-= len;
956 e0f7e329 Benjamin Larsson
            av_get_packet(pb, pkt, len);
957
        }
958
959
        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
960
            if ((st->codec->codec_id == CODEC_ID_RA_288) ||
961
                (st->codec->codec_id == CODEC_ID_COOK)) {
962
                int x;
963
                int sps = rm->sub_packet_size;
964
                int cfs = rm->coded_framesize;
965
                int h = rm->sub_packet_h;
966
                int y = rm->sub_packet_cnt;
967
                int w = rm->audio_framesize;
968
969
                if (flags & 2)
970
                    y = rm->sub_packet_cnt = 0;
971
                if (!y)
972
                    rm->audiotimestamp = timestamp;
973
974
                switch(st->codec->codec_id) {
975
                    case CODEC_ID_RA_288:
976
                        for (x = 0; x < h/2; x++)
977
                            get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
978
                        break;
979
                    case CODEC_ID_COOK:
980
                        for (x = 0; x < w/sps; x++)
981
                            get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
982
                        break;
983
                }
984
985
                if (++(rm->sub_packet_cnt) < h)
986
                    goto resync;
987
                else {
988
                    rm->sub_packet_cnt = 0;
989
                    rm->audio_stream_num = i;
990
                    rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
991
                    // Release first audio packet
992
                    av_new_packet(pkt, st->codec->block_align);
993
                    memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
994
                    timestamp = rm->audiotimestamp;
995
                    flags = 2; // Mark first packet as keyframe
996
                }
997 a194f595 Roberto Togni
            } else if (st->codec->codec_id == CODEC_ID_AAC) {
998
                int x;
999
                rm->audio_stream_num = i;
1000
                rm->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
1001
                if (rm->sub_packet_cnt) {
1002
                    for (x = 0; x < rm->sub_packet_cnt; x++)
1003
                        rm->sub_packet_lengths[x] = get_be16(pb);
1004
                    // Release first audio packet
1005
                    rm->audio_pkt_cnt = rm->sub_packet_cnt - 1;
1006
                    av_get_packet(pb, pkt, rm->sub_packet_lengths[0]);
1007
                    flags = 2; // Mark first packet as keyframe
1008
                }
1009 e0f7e329 Benjamin Larsson
            } else
1010
                av_get_packet(pb, pkt, len);
1011 c9510803 Fabrice Bellard
        }
1012 b9866ebc Michael Niedermayer
1013 f3356e9c Michael Niedermayer
        if(  (st->discard >= AVDISCARD_NONKEY && !(flags&2))
1014
           || st->discard >= AVDISCARD_ALL){
1015 e0f7e329 Benjamin Larsson
            av_free_packet(pkt);
1016 b9866ebc Michael Niedermayer
            goto resync;
1017
        }
1018 115329f1 Diego Biurrun
1019 c9510803 Fabrice Bellard
        pkt->stream_index = i;
1020 d880240e Michael Niedermayer
1021
#if 0
1022 01f4895c Michael Niedermayer
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1023
            if(st->codec->codec_id == CODEC_ID_RV20){
1024 d880240e Michael Niedermayer
                int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
1025
                av_log(NULL, AV_LOG_DEBUG, "%d %Ld %d\n", timestamp, timestamp*512LL/25, seq);
1026

1027
                seq |= (timestamp&~0x3FFF);
1028
                if(seq - timestamp >  0x2000) seq -= 0x4000;
1029
                if(seq - timestamp < -0x2000) seq += 0x4000;
1030
            }
1031
        }
1032
#endif
1033
        pkt->pts= timestamp;
1034 37e85dac Michael Niedermayer
        if(flags&2){
1035 d880240e Michael Niedermayer
            pkt->flags |= PKT_FLAG_KEY;
1036 37e85dac Michael Niedermayer
            if((seq&0x7F) == 1)
1037 30a43f2d Michael Niedermayer
                av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
1038 37e85dac Michael Niedermayer
        }
1039 5d6ef63f Philip Gladstone
    }
1040
1041 de6d9b64 Fabrice Bellard
    /* for AC3, needs to swap bytes */
1042 01f4895c Michael Niedermayer
    if (st->codec->codec_id == CODEC_ID_AC3) {
1043 de6d9b64 Fabrice Bellard
        ptr = pkt->data;
1044
        for(j=0;j<len;j+=2) {
1045
            tmp = ptr[0];
1046
            ptr[0] = ptr[1];
1047
            ptr[1] = tmp;
1048
                ptr += 2;
1049
        }
1050
    }
1051
    return 0;
1052
}
1053
1054
static int rm_read_close(AVFormatContext *s)
1055
{
1056 e0f7e329 Benjamin Larsson
    RMContext *rm = s->priv_data;
1057
1058
    av_free(rm->audiobuf);
1059 de6d9b64 Fabrice Bellard
    return 0;
1060
}
1061
1062 c9a65ca8 Fabrice Bellard
static int rm_probe(AVProbeData *p)
1063
{
1064
    /* check file header */
1065
    if (p->buf_size <= 32)
1066
        return 0;
1067 c9510803 Fabrice Bellard
    if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
1068
         p->buf[2] == 'M' && p->buf[3] == 'F' &&
1069
         p->buf[4] == 0 && p->buf[5] == 0) ||
1070
        (p->buf[0] == '.' && p->buf[1] == 'r' &&
1071
         p->buf[2] == 'a' && p->buf[3] == 0xfd))
1072 c9a65ca8 Fabrice Bellard
        return AVPROBE_SCORE_MAX;
1073
    else
1074
        return 0;
1075
}
1076
1077 115329f1 Diego Biurrun
static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
1078 bc17df09 Michael Niedermayer
                               int64_t *ppos, int64_t pos_limit)
1079
{
1080
    RMContext *rm = s->priv_data;
1081
    int64_t pos, dts;
1082 37e85dac Michael Niedermayer
    int stream_index2, flags, len, h;
1083 bc17df09 Michael Niedermayer
1084
    pos = *ppos;
1085 115329f1 Diego Biurrun
1086 bc17df09 Michael Niedermayer
    if(rm->old_format)
1087
        return AV_NOPTS_VALUE;
1088
1089
    url_fseek(&s->pb, pos, SEEK_SET);
1090
    rm->remaining_len=0;
1091
    for(;;){
1092 37e85dac Michael Niedermayer
        int seq=1;
1093
        AVStream *st;
1094
1095
        len=sync(s, &dts, &flags, &stream_index2, &pos);
1096 bc17df09 Michael Niedermayer
        if(len<0)
1097
            return AV_NOPTS_VALUE;
1098 37e85dac Michael Niedermayer
1099
        st = s->streams[stream_index2];
1100 01f4895c Michael Niedermayer
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1101 37e85dac Michael Niedermayer
            h= get_byte(&s->pb); len--;
1102
            if(!(h & 0x40)){
1103
                seq = get_byte(&s->pb); len--;
1104 bc17df09 Michael Niedermayer
            }
1105
        }
1106 115329f1 Diego Biurrun
1107 37e85dac Michael Niedermayer
        if((flags&2) && (seq&0x7F) == 1){
1108
//            av_log(s, AV_LOG_DEBUG, "%d %d-%d %Ld %d\n", flags, stream_index2, stream_index, dts, seq);
1109 30a43f2d Michael Niedermayer
            av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
1110 37e85dac Michael Niedermayer
            if(stream_index2 == stream_index)
1111
                break;
1112
        }
1113
1114 bc17df09 Michael Niedermayer
        url_fskip(&s->pb, len);
1115
    }
1116
    *ppos = pos;
1117
    return dts;
1118
}
1119
1120 ff70e601 Måns Rullgård
#ifdef CONFIG_RM_DEMUXER
1121
AVInputFormat rm_demuxer = {
1122 c9a65ca8 Fabrice Bellard
    "rm",
1123
    "rm format",
1124
    sizeof(RMContext),
1125
    rm_probe,
1126
    rm_read_header,
1127
    rm_read_packet,
1128
    rm_read_close,
1129 bc17df09 Michael Niedermayer
    NULL,
1130
    rm_read_dts,
1131 c9a65ca8 Fabrice Bellard
};
1132 ff70e601 Måns Rullgård
#endif
1133
#ifdef CONFIG_RM_MUXER
1134
AVOutputFormat rm_muxer = {
1135 de6d9b64 Fabrice Bellard
    "rm",
1136
    "rm format",
1137 c6c11cb6 Ryutaroh Matsumoto
    "application/vnd.rn-realmedia",
1138 de6d9b64 Fabrice Bellard
    "rm,ra",
1139 c9a65ca8 Fabrice Bellard
    sizeof(RMContext),
1140 de6d9b64 Fabrice Bellard
    CODEC_ID_AC3,
1141
    CODEC_ID_RV10,
1142
    rm_write_header,
1143
    rm_write_packet,
1144
    rm_write_trailer,
1145
};
1146 ff70e601 Måns Rullgård
#endif