Statistics
| Branch: | Revision:

ffmpeg / libavformat / rm.c @ c04c3282

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

975
                seq |= (timestamp&~0x3FFF);
976
                if(seq - timestamp >  0x2000) seq -= 0x4000;
977
                if(seq - timestamp < -0x2000) seq += 0x4000;
978
            }
979
        }
980
#endif
981
        pkt->pts= timestamp;
982 37e85dac Michael Niedermayer
        if(flags&2){
983 d880240e Michael Niedermayer
            pkt->flags |= PKT_FLAG_KEY;
984 37e85dac Michael Niedermayer
            if((seq&0x7F) == 1)
985 30a43f2d Michael Niedermayer
                av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
986 37e85dac Michael Niedermayer
        }
987 5d6ef63f Philip Gladstone
    }
988
989 de6d9b64 Fabrice Bellard
    /* for AC3, needs to swap bytes */
990 01f4895c Michael Niedermayer
    if (st->codec->codec_id == CODEC_ID_AC3) {
991 de6d9b64 Fabrice Bellard
        ptr = pkt->data;
992
        for(j=0;j<len;j+=2) {
993
            tmp = ptr[0];
994
            ptr[0] = ptr[1];
995
            ptr[1] = tmp;
996
                ptr += 2;
997
        }
998
    }
999
    return 0;
1000
}
1001
1002
static int rm_read_close(AVFormatContext *s)
1003
{
1004 e0f7e329 Benjamin Larsson
    RMContext *rm = s->priv_data;
1005
1006
    av_free(rm->audiobuf);
1007 de6d9b64 Fabrice Bellard
    return 0;
1008
}
1009
1010 c9a65ca8 Fabrice Bellard
static int rm_probe(AVProbeData *p)
1011
{
1012
    /* check file header */
1013
    if (p->buf_size <= 32)
1014
        return 0;
1015 c9510803 Fabrice Bellard
    if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
1016
         p->buf[2] == 'M' && p->buf[3] == 'F' &&
1017
         p->buf[4] == 0 && p->buf[5] == 0) ||
1018
        (p->buf[0] == '.' && p->buf[1] == 'r' &&
1019
         p->buf[2] == 'a' && p->buf[3] == 0xfd))
1020 c9a65ca8 Fabrice Bellard
        return AVPROBE_SCORE_MAX;
1021
    else
1022
        return 0;
1023
}
1024
1025 115329f1 Diego Biurrun
static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
1026 bc17df09 Michael Niedermayer
                               int64_t *ppos, int64_t pos_limit)
1027
{
1028
    RMContext *rm = s->priv_data;
1029
    int64_t pos, dts;
1030 37e85dac Michael Niedermayer
    int stream_index2, flags, len, h;
1031 bc17df09 Michael Niedermayer
1032
    pos = *ppos;
1033 115329f1 Diego Biurrun
1034 bc17df09 Michael Niedermayer
    if(rm->old_format)
1035
        return AV_NOPTS_VALUE;
1036
1037
    url_fseek(&s->pb, pos, SEEK_SET);
1038
    rm->remaining_len=0;
1039
    for(;;){
1040 37e85dac Michael Niedermayer
        int seq=1;
1041
        AVStream *st;
1042
1043
        len=sync(s, &dts, &flags, &stream_index2, &pos);
1044 bc17df09 Michael Niedermayer
        if(len<0)
1045
            return AV_NOPTS_VALUE;
1046 37e85dac Michael Niedermayer
1047
        st = s->streams[stream_index2];
1048 01f4895c Michael Niedermayer
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
1049 37e85dac Michael Niedermayer
            h= get_byte(&s->pb); len--;
1050
            if(!(h & 0x40)){
1051
                seq = get_byte(&s->pb); len--;
1052 bc17df09 Michael Niedermayer
            }
1053
        }
1054 115329f1 Diego Biurrun
1055 37e85dac Michael Niedermayer
        if((flags&2) && (seq&0x7F) == 1){
1056
//            av_log(s, AV_LOG_DEBUG, "%d %d-%d %Ld %d\n", flags, stream_index2, stream_index, dts, seq);
1057 30a43f2d Michael Niedermayer
            av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
1058 37e85dac Michael Niedermayer
            if(stream_index2 == stream_index)
1059
                break;
1060
        }
1061
1062 bc17df09 Michael Niedermayer
        url_fskip(&s->pb, len);
1063
    }
1064
    *ppos = pos;
1065
    return dts;
1066
}
1067
1068 c18a2692 Zdenek Kabelac
static AVInputFormat rm_iformat = {
1069 c9a65ca8 Fabrice Bellard
    "rm",
1070
    "rm format",
1071
    sizeof(RMContext),
1072
    rm_probe,
1073
    rm_read_header,
1074
    rm_read_packet,
1075
    rm_read_close,
1076 bc17df09 Michael Niedermayer
    NULL,
1077
    rm_read_dts,
1078 c9a65ca8 Fabrice Bellard
};
1079
1080 a9e35095 Diego Biurrun
#ifdef CONFIG_MUXERS
1081 c18a2692 Zdenek Kabelac
static AVOutputFormat rm_oformat = {
1082 de6d9b64 Fabrice Bellard
    "rm",
1083
    "rm format",
1084 c6c11cb6 Ryutaroh Matsumoto
    "application/vnd.rn-realmedia",
1085 de6d9b64 Fabrice Bellard
    "rm,ra",
1086 c9a65ca8 Fabrice Bellard
    sizeof(RMContext),
1087 de6d9b64 Fabrice Bellard
    CODEC_ID_AC3,
1088
    CODEC_ID_RV10,
1089
    rm_write_header,
1090
    rm_write_packet,
1091
    rm_write_trailer,
1092
};
1093 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
1094 c9a65ca8 Fabrice Bellard
1095
int rm_init(void)
1096
{
1097
    av_register_input_format(&rm_iformat);
1098 a9e35095 Diego Biurrun
#ifdef CONFIG_MUXERS
1099 c9a65ca8 Fabrice Bellard
    av_register_output_format(&rm_oformat);
1100 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
1101 c9a65ca8 Fabrice Bellard
    return 0;
1102
}