Statistics
| Branch: | Revision:

ffmpeg / libavformat / rmdec.c @ c5efef7b

History | View | Annotate | Download (26.9 KB)

1 de6d9b64 Fabrice Bellard
/*
2 7b2a2ce5 Aurelien Jacobs
 * "Real" compatible demuxer.
3 406792e7 Diego Biurrun
 * 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 245976da Diego Biurrun
22
#include "libavutil/avstring.h"
23 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
24 de6d9b64 Fabrice Bellard
#include "avformat.h"
25 ba614729 Ronald S. Bultje
#include "rm.h"
26 1edddc99 Ronald S. Bultje
27 ba614729 Ronald S. Bultje
struct RMStream {
28 0b4ff0c9 Michael Niedermayer
    AVPacket pkt;      ///< place to store merged video frame / reordered audio data
29 1edddc99 Ronald S. Bultje
    int videobufsize;  ///< current assembled frame size
30
    int videobufpos;   ///< position for the next slice in the video buffer
31
    int curpic_num;    ///< picture number of current frame
32
    int cur_slice, slices;
33
    int64_t pktpos;    ///< first slice position in file
34
    /// Audio descrambling matrix parameters
35
    int64_t audiotimestamp; ///< Audio packet timestamp
36
    int sub_packet_cnt; // Subpacket counter, used while reading
37
    int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
38 ba614729 Ronald S. Bultje
    int audio_framesize; /// Audio frame size from container
39
    int sub_packet_lengths[16]; /// Length of each subpacket
40
};
41
42
typedef struct {
43
    int nb_packets;
44
    int old_format;
45
    int current_stream;
46
    int remaining_len;
47 1edddc99 Ronald S. Bultje
    int audio_stream_num; ///< Stream number for audio packets
48
    int audio_pkt_cnt; ///< Output packet counter
49
} RMDemuxContext;
50 de6d9b64 Fabrice Bellard
51 36cf20f5 Ronald S. Bultje
static inline void get_strl(ByteIOContext *pb, char *buf, int buf_size, int len)
52 de6d9b64 Fabrice Bellard
{
53 36cf20f5 Ronald S. Bultje
    int i;
54 28263f5f Ronald S. Bultje
    char *q, r;
55 de6d9b64 Fabrice Bellard
56
    q = buf;
57
    for(i=0;i<len;i++) {
58 28263f5f Ronald S. Bultje
        r = get_byte(pb);
59 de6d9b64 Fabrice Bellard
        if (i < buf_size - 1)
60 28263f5f Ronald S. Bultje
            *q++ = r;
61 de6d9b64 Fabrice Bellard
    }
62 28263f5f Ronald S. Bultje
    if (buf_size > 0) *q = '\0';
63 de6d9b64 Fabrice Bellard
}
64
65 36cf20f5 Ronald S. Bultje
static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
66
{
67
    get_strl(pb, buf, buf_size, get_byte(pb));
68 de6d9b64 Fabrice Bellard
}
69
70 7379d5bc Aurelien Jacobs
static void rm_read_metadata(AVFormatContext *s, int wide)
71
{
72
    char buf[1024];
73
    int i;
74
    for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
75
        int len = wide ? get_be16(s->pb) : get_byte(s->pb);
76
        get_strl(s->pb, buf, sizeof(buf), len);
77
        av_metadata_set(&s->metadata, ff_rm_metadata[i], buf);
78
    }
79
}
80
81 ba614729 Ronald S. Bultje
RMStream *ff_rm_alloc_rmstream (void)
82
{
83
    RMStream *rms = av_mallocz(sizeof(RMStream));
84
    rms->curpic_num = -1;
85
    return rms;
86
}
87
88
void ff_rm_free_rmstream (RMStream *rms)
89
{
90 0b4ff0c9 Michael Niedermayer
    av_free_packet(&rms->pkt);
91 ba614729 Ronald S. Bultje
}
92
93 fcc995a5 Ronald S. Bultje
static int rm_read_audio_stream_info(AVFormatContext *s, ByteIOContext *pb,
94 886e89d0 Ronald S. Bultje
                                     AVStream *st, RMStream *ast, int read_all)
95 c9510803 Fabrice Bellard
{
96 1e4668d1 Roberto Togni
    char buf[256];
97 c9510803 Fabrice Bellard
    uint32_t version;
98
99
    /* ra type header */
100
    version = get_be32(pb); /* version */
101
    if (((version >> 16) & 0xff) == 3) {
102 1e4668d1 Roberto Togni
        int64_t startpos = url_ftell(pb);
103 b9b2b8c9 Ronald S. Bultje
        url_fskip(pb, 14);
104 7379d5bc Aurelien Jacobs
        rm_read_metadata(s, 0);
105 1e4668d1 Roberto Togni
        if ((startpos + (version & 0xffff)) >= url_ftell(pb) + 2) {
106 76f940b2 Ronald S. Bultje
            // fourcc (should always be "lpcJ")
107
            get_byte(pb);
108
            get_str8(pb, buf, sizeof(buf));
109 1e4668d1 Roberto Togni
        }
110
        // Skip extra header crap (this should never happen)
111
        if ((startpos + (version & 0xffff)) > url_ftell(pb))
112
            url_fskip(pb, (version & 0xffff) + startpos - url_ftell(pb));
113 01f4895c Michael Niedermayer
        st->codec->sample_rate = 8000;
114
        st->codec->channels = 1;
115
        st->codec->codec_type = CODEC_TYPE_AUDIO;
116
        st->codec->codec_id = CODEC_ID_RA_144;
117 c9510803 Fabrice Bellard
    } else {
118 e0f7e329 Benjamin Larsson
        int flavor, sub_packet_h, coded_framesize, sub_packet_size;
119 c9510803 Fabrice Bellard
        /* old version (4) */
120
        get_be32(pb); /* .ra4 */
121 72aef198 Michael Niedermayer
        get_be32(pb); /* data size */
122
        get_be16(pb); /* version2 */
123 c9510803 Fabrice Bellard
        get_be32(pb); /* header size */
124 72aef198 Michael Niedermayer
        flavor= get_be16(pb); /* add codec info / flavor */
125 ba614729 Ronald S. Bultje
        ast->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
126 c9510803 Fabrice Bellard
        get_be32(pb); /* ??? */
127
        get_be32(pb); /* ??? */
128
        get_be32(pb); /* ??? */
129 ba614729 Ronald S. Bultje
        ast->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
130 01f4895c Michael Niedermayer
        st->codec->block_align= get_be16(pb); /* frame size */
131 ba614729 Ronald S. Bultje
        ast->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
132 72aef198 Michael Niedermayer
        get_be16(pb); /* ??? */
133 e0f7e329 Benjamin Larsson
        if (((version >> 16) & 0xff) == 5) {
134 127ed7b0 Ronald S. Bultje
            get_be16(pb); get_be16(pb); get_be16(pb);
135
        }
136 01f4895c Michael Niedermayer
        st->codec->sample_rate = get_be16(pb);
137 c9510803 Fabrice Bellard
        get_be32(pb);
138 01f4895c Michael Niedermayer
        st->codec->channels = get_be16(pb);
139 e0f7e329 Benjamin Larsson
        if (((version >> 16) & 0xff) == 5) {
140
            get_be32(pb);
141 b9b2b8c9 Ronald S. Bultje
            get_buffer(pb, buf, 4);
142 bb270c08 Diego Biurrun
            buf[4] = 0;
143
        } else {
144 e344c1ea Steve L'Homme
            get_str8(pb, buf, sizeof(buf)); /* desc */
145
            get_str8(pb, buf, sizeof(buf)); /* desc */
146 bb270c08 Diego Biurrun
        }
147 01f4895c Michael Niedermayer
        st->codec->codec_type = CODEC_TYPE_AUDIO;
148 c9510803 Fabrice Bellard
        if (!strcmp(buf, "dnet")) {
149 01f4895c Michael Niedermayer
            st->codec->codec_id = CODEC_ID_AC3;
150 bf415121 Justin Ruggles
            st->need_parsing = AVSTREAM_PARSE_FULL;
151 72aef198 Michael Niedermayer
        } else if (!strcmp(buf, "28_8")) {
152 01f4895c Michael Niedermayer
            st->codec->codec_id = CODEC_ID_RA_288;
153 e0f7e329 Benjamin Larsson
            st->codec->extradata_size= 0;
154 ba614729 Ronald S. Bultje
            ast->audio_framesize = st->codec->block_align;
155 e0f7e329 Benjamin Larsson
            st->codec->block_align = coded_framesize;
156 a443a253 Michael Niedermayer
157 ba614729 Ronald S. Bultje
            if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
158
                av_log(s, AV_LOG_ERROR, "ast->audio_framesize * sub_packet_h too large\n");
159 a443a253 Michael Niedermayer
                return -1;
160
            }
161
162 0b4ff0c9 Michael Niedermayer
            av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
163 2426bb96 Benjamin Larsson
        } else if ((!strcmp(buf, "cook")) || (!strcmp(buf, "atrc")) || (!strcmp(buf, "sipr"))) {
164 b9b2b8c9 Ronald S. Bultje
            int codecdata_length;
165 e0f7e329 Benjamin Larsson
            get_be16(pb); get_byte(pb);
166
            if (((version >> 16) & 0xff) == 5)
167
                get_byte(pb);
168
            codecdata_length = get_be32(pb);
169 a443a253 Michael Niedermayer
            if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
170
                av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
171
                return -1;
172
            }
173
174 f23938f5 Michael Niedermayer
            if(sub_packet_size <= 0){
175
                av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
176
                return -1;
177
            }
178
179 10e26bc7 Benjamin Larsson
            if (!strcmp(buf, "cook")) st->codec->codec_id = CODEC_ID_COOK;
180 2426bb96 Benjamin Larsson
            else if (!strcmp(buf, "sipr")) st->codec->codec_id = CODEC_ID_SIPR;
181 10e26bc7 Benjamin Larsson
            else st->codec->codec_id = CODEC_ID_ATRAC3;
182 e0f7e329 Benjamin Larsson
            st->codec->extradata_size= codecdata_length;
183 d76319b1 Jindřich Makovička
            st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
184 b9b2b8c9 Ronald S. Bultje
            get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
185 ba614729 Ronald S. Bultje
            ast->audio_framesize = st->codec->block_align;
186
            st->codec->block_align = ast->sub_packet_size;
187 a443a253 Michael Niedermayer
188 ba614729 Ronald S. Bultje
            if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
189 a443a253 Michael Niedermayer
                av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
190
                return -1;
191
            }
192
193 0b4ff0c9 Michael Niedermayer
            av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
194 a194f595 Roberto Togni
        } else if (!strcmp(buf, "raac") || !strcmp(buf, "racp")) {
195 b9b2b8c9 Ronald S. Bultje
            int codecdata_length;
196 a194f595 Roberto Togni
            get_be16(pb); get_byte(pb);
197
            if (((version >> 16) & 0xff) == 5)
198
                get_byte(pb);
199
            st->codec->codec_id = CODEC_ID_AAC;
200
            codecdata_length = get_be32(pb);
201 a9d4a6ef Roberto Togni
            if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
202
                av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
203
                return -1;
204
            }
205 a194f595 Roberto Togni
            if (codecdata_length >= 1) {
206
                st->codec->extradata_size = codecdata_length - 1;
207
                st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
208
                get_byte(pb);
209 b9b2b8c9 Ronald S. Bultje
                get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
210 a194f595 Roberto Togni
            }
211 c9510803 Fabrice Bellard
        } else {
212 01f4895c Michael Niedermayer
            st->codec->codec_id = CODEC_ID_NONE;
213 75e61b0e Måns Rullgård
            av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
214 c9510803 Fabrice Bellard
        }
215
        if (read_all) {
216
            get_byte(pb);
217
            get_byte(pb);
218
            get_byte(pb);
219 7379d5bc Aurelien Jacobs
            rm_read_metadata(s, 0);
220 c9510803 Fabrice Bellard
        }
221
    }
222 a9d4a6ef Roberto Togni
    return 0;
223 c9510803 Fabrice Bellard
}
224
225 0fd36362 Ronald S. Bultje
int
226 fcc995a5 Ronald S. Bultje
ff_rm_read_mdpr_codecdata (AVFormatContext *s, ByteIOContext *pb,
227 886e89d0 Ronald S. Bultje
                           AVStream *st, RMStream *rst, int codec_data_size)
228 ff1463dc Ronald S. Bultje
{
229
    unsigned int v;
230 79d5045d Ronald S. Bultje
    int size;
231 ff1463dc Ronald S. Bultje
    int64_t codec_pos;
232
233 15995030 Ronald S. Bultje
    av_set_pts_info(st, 64, 1, 1000);
234 ff1463dc Ronald S. Bultje
    codec_pos = url_ftell(pb);
235
    v = get_be32(pb);
236
    if (v == MKTAG(0xfd, 'a', 'r', '.')) {
237
        /* ra type header */
238 886e89d0 Ronald S. Bultje
        if (rm_read_audio_stream_info(s, pb, st, rst, 0))
239 ff1463dc Ronald S. Bultje
            return -1;
240
    } else {
241
        int fps, fps2;
242
        if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
243
        fail1:
244
            av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
245
            goto skip;
246
        }
247
        st->codec->codec_tag = get_le32(pb);
248 0fd7c133 Benoit Fouet
//        av_log(s, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
249 ff1463dc Ronald S. Bultje
        if (   st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
250
            && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
251
            && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
252 b93dd838 Vitor Sessak
            && st->codec->codec_tag != MKTAG('R', 'V', '4', '0')
253
            && st->codec->codec_tag != MKTAG('R', 'V', 'T', 'R'))
254 ff1463dc Ronald S. Bultje
            goto fail1;
255
        st->codec->width = get_be16(pb);
256
        st->codec->height = get_be16(pb);
257
        st->codec->time_base.num= 1;
258
        fps= get_be16(pb);
259
        st->codec->codec_type = CODEC_TYPE_VIDEO;
260
        get_be32(pb);
261
        fps2= get_be16(pb);
262
        get_be16(pb);
263
264
        st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
265
266
        if(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
267
            //check is redundant as get_buffer() will catch this
268
            av_log(s, AV_LOG_ERROR, "st->codec->extradata_size too large\n");
269
            return -1;
270
        }
271
        st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
272 437fd77b Panagiotis Issaris
        if (!st->codec->extradata)
273
            return AVERROR(ENOMEM);
274 ff1463dc Ronald S. Bultje
        get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
275
276 0fd7c133 Benoit Fouet
//        av_log(s, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
277 ff1463dc Ronald S. Bultje
        st->codec->time_base.den = fps * st->codec->time_base.num;
278
        switch(((uint8_t*)st->codec->extradata)[4]>>4){
279
        case 1: st->codec->codec_id = CODEC_ID_RV10; break;
280
        case 2: st->codec->codec_id = CODEC_ID_RV20; break;
281
        case 3: st->codec->codec_id = CODEC_ID_RV30; break;
282
        case 4: st->codec->codec_id = CODEC_ID_RV40; break;
283
        default: goto fail1;
284
        }
285
    }
286
287
skip:
288
    /* skip codec info */
289
    size = url_ftell(pb) - codec_pos;
290
    url_fskip(pb, codec_data_size - size);
291
292 36c9e40a Benoit Fouet
    return 0;
293 ff1463dc Ronald S. Bultje
}
294
295
296 c9510803 Fabrice Bellard
static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
297
{
298 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
299 c9510803 Fabrice Bellard
    AVStream *st;
300
301
    rm->old_format = 1;
302
    st = av_new_stream(s, 0);
303
    if (!st)
304 a9d4a6ef Roberto Togni
        return -1;
305 886e89d0 Ronald S. Bultje
    st->priv_data = ff_rm_alloc_rmstream();
306
    return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
307 c9510803 Fabrice Bellard
}
308
309 de6d9b64 Fabrice Bellard
static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
310
{
311 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
312 de6d9b64 Fabrice Bellard
    AVStream *st;
313 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
314 ff1463dc Ronald S. Bultje
    unsigned int tag;
315 07cb073b Diego Biurrun
    int tag_size;
316 36c32bdd Diego Biurrun
    unsigned int start_time, duration;
317 de6d9b64 Fabrice Bellard
    char buf[128];
318 5d6ef63f Philip Gladstone
    int flags = 0;
319 de6d9b64 Fabrice Bellard
320 c9510803 Fabrice Bellard
    tag = get_le32(pb);
321
    if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
322
        /* very old .ra format */
323
        return rm_read_header_old(s, ap);
324
    } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
325 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
326 c9510803 Fabrice Bellard
    }
327 de6d9b64 Fabrice Bellard
328
    get_be32(pb); /* header size */
329
    get_be16(pb);
330
    get_be32(pb);
331
    get_be32(pb); /* number of headers */
332 115329f1 Diego Biurrun
333 de6d9b64 Fabrice Bellard
    for(;;) {
334
        if (url_feof(pb))
335 ccafd472 Baptiste Coudurier
            return -1;
336 de6d9b64 Fabrice Bellard
        tag = get_le32(pb);
337
        tag_size = get_be32(pb);
338
        get_be16(pb);
339
#if 0
340 115329f1 Diego Biurrun
        printf("tag=%c%c%c%c (%08x) size=%d\n",
341 de6d9b64 Fabrice Bellard
               (tag) & 0xff,
342
               (tag >> 8) & 0xff,
343
               (tag >> 16) & 0xff,
344
               (tag >> 24) & 0xff,
345
               tag,
346
               tag_size);
347
#endif
348 9786bd4d Michael Niedermayer
        if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
349 ccafd472 Baptiste Coudurier
            return -1;
350 de6d9b64 Fabrice Bellard
        switch(tag) {
351
        case MKTAG('P', 'R', 'O', 'P'):
352
            /* file header */
353
            get_be32(pb); /* max bit rate */
354
            get_be32(pb); /* avg bit rate */
355
            get_be32(pb); /* max packet size */
356
            get_be32(pb); /* avg packet size */
357
            get_be32(pb); /* nb packets */
358
            get_be32(pb); /* duration */
359
            get_be32(pb); /* preroll */
360
            get_be32(pb); /* index offset */
361
            get_be32(pb); /* data offset */
362
            get_be16(pb); /* nb streams */
363 5d6ef63f Philip Gladstone
            flags = get_be16(pb); /* flags */
364 de6d9b64 Fabrice Bellard
            break;
365
        case MKTAG('C', 'O', 'N', 'T'):
366 7379d5bc Aurelien Jacobs
            rm_read_metadata(s, 1);
367 de6d9b64 Fabrice Bellard
            break;
368
        case MKTAG('M', 'D', 'P', 'R'):
369 247eadca Fabrice Bellard
            st = av_new_stream(s, 0);
370 de6d9b64 Fabrice Bellard
            if (!st)
371 ccafd472 Baptiste Coudurier
                return AVERROR(ENOMEM);
372 de6d9b64 Fabrice Bellard
            st->id = get_be16(pb);
373
            get_be32(pb); /* max bit rate */
374 01f4895c Michael Niedermayer
            st->codec->bit_rate = get_be32(pb); /* bit rate */
375 de6d9b64 Fabrice Bellard
            get_be32(pb); /* max packet size */
376
            get_be32(pb); /* avg packet size */
377 247eadca Fabrice Bellard
            start_time = get_be32(pb); /* start time */
378 de6d9b64 Fabrice Bellard
            get_be32(pb); /* preroll */
379 247eadca Fabrice Bellard
            duration = get_be32(pb); /* duration */
380 c0df9d75 Michael Niedermayer
            st->start_time = start_time;
381
            st->duration = duration;
382 de6d9b64 Fabrice Bellard
            get_str8(pb, buf, sizeof(buf)); /* desc */
383
            get_str8(pb, buf, sizeof(buf)); /* mimetype */
384 01f4895c Michael Niedermayer
            st->codec->codec_type = CODEC_TYPE_DATA;
385 886e89d0 Ronald S. Bultje
            st->priv_data = ff_rm_alloc_rmstream();
386
            if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
387
                                          get_be32(pb)) < 0)
388 ff1463dc Ronald S. Bultje
                return -1;
389 de6d9b64 Fabrice Bellard
            break;
390
        case MKTAG('D', 'A', 'T', 'A'):
391
            goto header_end;
392
        default:
393
            /* unknown tag: skip it */
394
            url_fskip(pb, tag_size - 10);
395
            break;
396
        }
397
    }
398
 header_end:
399
    rm->nb_packets = get_be32(pb); /* number of packets */
400 5d6ef63f Philip Gladstone
    if (!rm->nb_packets && (flags & 4))
401
        rm->nb_packets = 3600 * 25;
402 de6d9b64 Fabrice Bellard
    get_be32(pb); /* next data header */
403
    return 0;
404
}
405
406 4c2bc159 Michael Niedermayer
static int get_num(ByteIOContext *pb, int *len)
407
{
408
    int n, n1;
409
410
    n = get_be16(pb);
411
    (*len)-=2;
412 e3052ce7 Roberto Togni
    n &= 0x7FFF;
413 4c2bc159 Michael Niedermayer
    if (n >= 0x4000) {
414
        return n - 0x4000;
415
    } else {
416
        n1 = get_be16(pb);
417
        (*len)-=2;
418
        return (n << 16) | n1;
419
    }
420
}
421
422 c9510803 Fabrice Bellard
/* multiple of 20 bytes for ra144 (ugly) */
423
#define RAW_PACKET_SIZE 1000
424
425 37e85dac Michael Niedermayer
static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
426 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
427 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
428 bc17df09 Michael Niedermayer
    int len, num, res, i;
429
    AVStream *st;
430 119cebf4 Michael Niedermayer
    uint32_t state=0xFFFFFFFF;
431 bc17df09 Michael Niedermayer
432
    while(!url_feof(pb)){
433 fd31550d Michael Niedermayer
        *pos= url_ftell(pb) - 3;
434 bc17df09 Michael Niedermayer
        if(rm->remaining_len > 0){
435
            num= rm->current_stream;
436
            len= rm->remaining_len;
437
            *timestamp = AV_NOPTS_VALUE;
438
            *flags= 0;
439
        }else{
440 119cebf4 Michael Niedermayer
            state= (state<<8) + get_byte(pb);
441 115329f1 Diego Biurrun
442 119cebf4 Michael Niedermayer
            if(state == MKBETAG('I', 'N', 'D', 'X')){
443 b5b9ff2a Ronald S. Bultje
                int n_pkts, expected_len;
444
                len = get_be32(pb);
445
                url_fskip(pb, 2);
446
                n_pkts = get_be32(pb);
447
                expected_len = 20 + n_pkts * 14;
448
                if (len == 20)
449
                    /* some files don't add index entries to chunk size... */
450
                    len = expected_len;
451
                else if (len != expected_len)
452
                    av_log(s, AV_LOG_WARNING,
453
                           "Index size %d (%d pkts) is wrong, should be %d.\n",
454
                           len, n_pkts, expected_len);
455
                len -= 14; // we already read part of the index header
456 119cebf4 Michael Niedermayer
                if(len<0)
457
                    continue;
458
                goto skip;
459
            }
460 115329f1 Diego Biurrun
461 119cebf4 Michael Niedermayer
            if(state > (unsigned)0xFFFF || state < 12)
462 bc17df09 Michael Niedermayer
                continue;
463 119cebf4 Michael Niedermayer
            len=state;
464
            state= 0xFFFFFFFF;
465
466 bc17df09 Michael Niedermayer
            num = get_be16(pb);
467
            *timestamp = get_be32(pb);
468
            res= get_byte(pb); /* reserved */
469
            *flags = get_byte(pb); /* flags */
470 37e85dac Michael Niedermayer
471 115329f1 Diego Biurrun
472 bc17df09 Michael Niedermayer
            len -= 12;
473
        }
474
        for(i=0;i<s->nb_streams;i++) {
475
            st = s->streams[i];
476
            if (num == st->id)
477
                break;
478
        }
479
        if (i == s->nb_streams) {
480 119cebf4 Michael Niedermayer
skip:
481 bc17df09 Michael Niedermayer
            /* skip packet if unknown number */
482
            url_fskip(pb, len);
483 b8cc5a9f Ronald S. Bultje
            rm->remaining_len = 0;
484 bc17df09 Michael Niedermayer
            continue;
485
        }
486
        *stream_index= i;
487 115329f1 Diego Biurrun
488 bc17df09 Michael Niedermayer
        return len;
489
    }
490
    return -1;
491
}
492
493 fcc995a5 Ronald S. Bultje
static int rm_assemble_video_frame(AVFormatContext *s, ByteIOContext *pb,
494 ba614729 Ronald S. Bultje
                                   RMDemuxContext *rm, RMStream *vst,
495 7a160bca Ronald S. Bultje
                                   AVPacket *pkt, int len, int *pseq)
496 383b123e Kostya Shishkov
{
497
    int hdr, seq, pic_num, len2, pos;
498
    int type;
499
500
    hdr = get_byte(pb); len--;
501
    type = hdr >> 6;
502 f48d5457 Michael Niedermayer
503
    if(type != 3){  // not frame as a part of packet
504 383b123e Kostya Shishkov
        seq = get_byte(pb); len--;
505 f48d5457 Michael Niedermayer
    }
506
    if(type != 1){  // not whole frame
507 383b123e Kostya Shishkov
        len2 = get_num(pb, &len);
508 f48d5457 Michael Niedermayer
        pos  = get_num(pb, &len);
509 383b123e Kostya Shishkov
        pic_num = get_byte(pb); len--;
510 f48d5457 Michael Niedermayer
    }
511
    if(len<0)
512
        return -1;
513
    rm->remaining_len = len;
514
    if(type&1){     // frame, not slice
515
        if(type == 3)  // frame as a part of packet
516
            len= len2;
517
        if(rm->remaining_len < len)
518 e45673cb Michael Niedermayer
            return -1;
519 f48d5457 Michael Niedermayer
        rm->remaining_len -= len;
520 383b123e Kostya Shishkov
        if(av_new_packet(pkt, len + 9) < 0)
521
            return AVERROR(EIO);
522
        pkt->data[0] = 0;
523
        AV_WL32(pkt->data + 1, 1);
524
        AV_WL32(pkt->data + 5, 0);
525
        get_buffer(pb, pkt->data + 9, len);
526
        return 0;
527
    }
528
    //now we have to deal with single slice
529
530 7a160bca Ronald S. Bultje
    *pseq = seq;
531 ba614729 Ronald S. Bultje
    if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
532
        vst->slices = ((hdr & 0x3F) << 1) + 1;
533
        vst->videobufsize = len2 + 8*vst->slices + 1;
534 0b4ff0c9 Michael Niedermayer
        av_free_packet(&vst->pkt); //FIXME this should be output.
535
        if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
536 52537534 Roberto Togni
            return AVERROR(ENOMEM);
537 ba614729 Ronald S. Bultje
        vst->videobufpos = 8*vst->slices + 1;
538
        vst->cur_slice = 0;
539
        vst->curpic_num = pic_num;
540
        vst->pktpos = url_ftell(pb);
541 383b123e Kostya Shishkov
    }
542 e35033e6 Roberto Togni
    if(type == 2)
543 383b123e Kostya Shishkov
        len = FFMIN(len, pos);
544
545 ba614729 Ronald S. Bultje
    if(++vst->cur_slice > vst->slices)
546 383b123e Kostya Shishkov
        return 1;
547 0b4ff0c9 Michael Niedermayer
    AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
548
    AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
549 ba614729 Ronald S. Bultje
    if(vst->videobufpos + len > vst->videobufsize)
550 383b123e Kostya Shishkov
        return 1;
551 0b4ff0c9 Michael Niedermayer
    if (get_buffer(pb, vst->pkt.data + vst->videobufpos, len) != len)
552 383b123e Kostya Shishkov
        return AVERROR(EIO);
553 ba614729 Ronald S. Bultje
    vst->videobufpos += len;
554 383b123e Kostya Shishkov
    rm->remaining_len-= len;
555
556 ba614729 Ronald S. Bultje
    if(type == 2 || (vst->videobufpos) == vst->videobufsize){
557 188e67ea Michael Niedermayer
        vst->pkt.data[0] = vst->cur_slice-1;
558
        *pkt= vst->pkt;
559 6fde7afa Dominique Leuenberger
        vst->pkt.data= NULL;
560 188e67ea Michael Niedermayer
        vst->pkt.size= 0;
561
        if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
562 0b4ff0c9 Michael Niedermayer
            memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
563 ba614729 Ronald S. Bultje
                vst->videobufpos - 1 - 8*vst->slices);
564 ce0453aa Michael Niedermayer
        pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
565 188e67ea Michael Niedermayer
        pkt->pts = AV_NOPTS_VALUE;
566
        pkt->pos = vst->pktpos;
567
        return 0;
568 383b123e Kostya Shishkov
    }
569
570
    return 1;
571
}
572
573 5c470b91 Ronald S. Bultje
static inline void
574
rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
575
{
576
    uint8_t *ptr;
577
    int j;
578
579
    if (st->codec->codec_id == CODEC_ID_AC3) {
580
        ptr = pkt->data;
581
        for (j=0;j<pkt->size;j+=2) {
582
            FFSWAP(int, ptr[0], ptr[1]);
583
            ptr += 2;
584
        }
585
    }
586
}
587
588 0fd36362 Ronald S. Bultje
int
589 fcc995a5 Ronald S. Bultje
ff_rm_parse_packet (AVFormatContext *s, ByteIOContext *pb,
590 886e89d0 Ronald S. Bultje
                    AVStream *st, RMStream *ast, int len, AVPacket *pkt,
591 afd302fa Ronald S. Bultje
                    int *seq, int *flags, int64_t *timestamp)
592
{
593 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
594 afd302fa Ronald S. Bultje
595
    if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
596
        rm->current_stream= st->id;
597 7a160bca Ronald S. Bultje
        if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq))
598 afd302fa Ronald S. Bultje
            return -1; //got partial frame
599
    } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
600
        if ((st->codec->codec_id == CODEC_ID_RA_288) ||
601
            (st->codec->codec_id == CODEC_ID_COOK) ||
602 2426bb96 Benjamin Larsson
            (st->codec->codec_id == CODEC_ID_ATRAC3) ||
603
            (st->codec->codec_id == CODEC_ID_SIPR)) {
604 afd302fa Ronald S. Bultje
            int x;
605 ba614729 Ronald S. Bultje
            int sps = ast->sub_packet_size;
606
            int cfs = ast->coded_framesize;
607
            int h = ast->sub_packet_h;
608
            int y = ast->sub_packet_cnt;
609
            int w = ast->audio_framesize;
610 afd302fa Ronald S. Bultje
611
            if (*flags & 2)
612 ba614729 Ronald S. Bultje
                y = ast->sub_packet_cnt = 0;
613 afd302fa Ronald S. Bultje
            if (!y)
614 ba614729 Ronald S. Bultje
                ast->audiotimestamp = *timestamp;
615 afd302fa Ronald S. Bultje
616
            switch(st->codec->codec_id) {
617
                case CODEC_ID_RA_288:
618
                    for (x = 0; x < h/2; x++)
619 0b4ff0c9 Michael Niedermayer
                        get_buffer(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
620 afd302fa Ronald S. Bultje
                    break;
621
                case CODEC_ID_ATRAC3:
622
                case CODEC_ID_COOK:
623
                    for (x = 0; x < w/sps; x++)
624 0b4ff0c9 Michael Niedermayer
                        get_buffer(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
625 afd302fa Ronald S. Bultje
                    break;
626
            }
627
628 ba614729 Ronald S. Bultje
            if (++(ast->sub_packet_cnt) < h)
629 afd302fa Ronald S. Bultje
                return -1;
630 ba614729 Ronald S. Bultje
                ast->sub_packet_cnt = 0;
631 afd302fa Ronald S. Bultje
                rm->audio_stream_num = st->index;
632 c5efef7b Ronald S. Bultje
                rm->audio_pkt_cnt = h * w / st->codec->block_align;
633 afd302fa Ronald S. Bultje
        } else if (st->codec->codec_id == CODEC_ID_AAC) {
634
            int x;
635
            rm->audio_stream_num = st->index;
636 ba614729 Ronald S. Bultje
            ast->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
637
            if (ast->sub_packet_cnt) {
638
                for (x = 0; x < ast->sub_packet_cnt; x++)
639
                    ast->sub_packet_lengths[x] = get_be16(pb);
640 c5efef7b Ronald S. Bultje
                rm->audio_pkt_cnt = ast->sub_packet_cnt;
641
                ast->audiotimestamp = *timestamp;
642
            } else
643
                return -1;
644 badbe09a Roberto Togni
        } else {
645 afd302fa Ronald S. Bultje
            av_get_packet(pb, pkt, len);
646 e29378ff Roberto Togni
            rm_ac3_swap_bytes(st, pkt);
647 badbe09a Roberto Togni
        }
648 afd302fa Ronald S. Bultje
    } else
649
        av_get_packet(pb, pkt, len);
650
651
    pkt->stream_index = st->index;
652
653
#if 0
654
    if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
655
        if(st->codec->codec_id == CODEC_ID_RV20){
656
            int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
657 0fd7c133 Benoit Fouet
            av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
658 afd302fa Ronald S. Bultje

659
            seq |= (*timestamp&~0x3FFF);
660
            if(seq - *timestamp >  0x2000) seq -= 0x4000;
661
            if(seq - *timestamp < -0x2000) seq += 0x4000;
662
        }
663
    }
664
#endif
665
666
    pkt->pts= *timestamp;
667
    if (*flags & 2)
668
        pkt->flags |= PKT_FLAG_KEY;
669
670 a15ebf34 Ronald S. Bultje
    return st->codec->codec_type == CODEC_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
671 afd302fa Ronald S. Bultje
}
672
673 a9f84821 Ronald S. Bultje
int
674 fcc995a5 Ronald S. Bultje
ff_rm_retrieve_cache (AVFormatContext *s, ByteIOContext *pb,
675 886e89d0 Ronald S. Bultje
                      AVStream *st, RMStream *ast, AVPacket *pkt)
676 d4d1b5fb Ronald S. Bultje
{
677 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
678 d4d1b5fb Ronald S. Bultje
679
    assert (rm->audio_pkt_cnt > 0);
680
681
    if (st->codec->codec_id == CODEC_ID_AAC)
682 ba614729 Ronald S. Bultje
        av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
683 d4d1b5fb Ronald S. Bultje
    else {
684
        av_new_packet(pkt, st->codec->block_align);
685 0b4ff0c9 Michael Niedermayer
        memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
686 ba614729 Ronald S. Bultje
               (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
687 d4d1b5fb Ronald S. Bultje
               st->codec->block_align);
688
    }
689
    rm->audio_pkt_cnt--;
690
    pkt->flags = 0;
691
    pkt->stream_index = st->index;
692 a9f84821 Ronald S. Bultje
693
    return rm->audio_pkt_cnt;
694 d4d1b5fb Ronald S. Bultje
}
695
696 de6d9b64 Fabrice Bellard
static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
697
{
698 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
699 de6d9b64 Fabrice Bellard
    AVStream *st;
700 6a2d3fc3 Ronald S. Bultje
    int i, len, res, seq = 1;
701 37e85dac Michael Niedermayer
    int64_t timestamp, pos;
702 7a160bca Ronald S. Bultje
    int old_flags, flags;
703 de6d9b64 Fabrice Bellard
704 6a2d3fc3 Ronald S. Bultje
    for (;;) {
705 7268f0bd Ronald S. Bultje
        if (rm->audio_pkt_cnt) {
706
            // If there are queued audio packet return them first
707
            st = s->streams[rm->audio_stream_num];
708
            ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
709 6a2d3fc3 Ronald S. Bultje
        } else {
710
            if (rm->old_format) {
711 7268f0bd Ronald S. Bultje
                RMStream *ast;
712 7ccefd08 Ronald S. Bultje
713 7268f0bd Ronald S. Bultje
                st = s->streams[0];
714
                ast = st->priv_data;
715 6a2d3fc3 Ronald S. Bultje
                timestamp = AV_NOPTS_VALUE;
716
                len = !ast->audio_framesize ? RAW_PACKET_SIZE :
717
                    ast->coded_framesize * ast->sub_packet_h / 2;
718
                flags = (seq++ == 1) ? 2 : 0;
719 7268f0bd Ronald S. Bultje
            } else {
720
                len=sync(s, &timestamp, &flags, &i, &pos);
721
                st = s->streams[i];
722 6a2d3fc3 Ronald S. Bultje
            }
723
724
            if(len<0 || url_feof(s->pb))
725
                return AVERROR(EIO);
726 bc17df09 Michael Niedermayer
727 7268f0bd Ronald S. Bultje
            old_flags = flags;
728
            res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
729
                                      &seq, &flags, &timestamp);
730
            if((old_flags&2) && (seq&0x7F) == 1)
731
                av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
732 6a2d3fc3 Ronald S. Bultje
            if (res)
733
                continue;
734
        }
735 d880240e Michael Niedermayer
736 dc3685e1 Ronald S. Bultje
        if(  (st->discard >= AVDISCARD_NONKEY && !(flags&2))
737
           || st->discard >= AVDISCARD_ALL){
738
            av_free_packet(pkt);
739 6a2d3fc3 Ronald S. Bultje
        } else
740
            break;
741 5d6ef63f Philip Gladstone
    }
742
743 de6d9b64 Fabrice Bellard
    return 0;
744
}
745
746
static int rm_read_close(AVFormatContext *s)
747
{
748 ba614729 Ronald S. Bultje
    int i;
749
750
    for (i=0;i<s->nb_streams;i++)
751
        ff_rm_free_rmstream(s->streams[i]->priv_data);
752 e0f7e329 Benjamin Larsson
753 de6d9b64 Fabrice Bellard
    return 0;
754
}
755
756 c9a65ca8 Fabrice Bellard
static int rm_probe(AVProbeData *p)
757
{
758
    /* check file header */
759 c9510803 Fabrice Bellard
    if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
760
         p->buf[2] == 'M' && p->buf[3] == 'F' &&
761
         p->buf[4] == 0 && p->buf[5] == 0) ||
762
        (p->buf[0] == '.' && p->buf[1] == 'r' &&
763
         p->buf[2] == 'a' && p->buf[3] == 0xfd))
764 c9a65ca8 Fabrice Bellard
        return AVPROBE_SCORE_MAX;
765
    else
766
        return 0;
767
}
768
769 115329f1 Diego Biurrun
static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
770 bc17df09 Michael Niedermayer
                               int64_t *ppos, int64_t pos_limit)
771
{
772 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
773 bc17df09 Michael Niedermayer
    int64_t pos, dts;
774 37e85dac Michael Niedermayer
    int stream_index2, flags, len, h;
775 bc17df09 Michael Niedermayer
776
    pos = *ppos;
777 115329f1 Diego Biurrun
778 bc17df09 Michael Niedermayer
    if(rm->old_format)
779
        return AV_NOPTS_VALUE;
780
781 899681cd Björn Axelsson
    url_fseek(s->pb, pos, SEEK_SET);
782 bc17df09 Michael Niedermayer
    rm->remaining_len=0;
783
    for(;;){
784 37e85dac Michael Niedermayer
        int seq=1;
785
        AVStream *st;
786
787
        len=sync(s, &dts, &flags, &stream_index2, &pos);
788 bc17df09 Michael Niedermayer
        if(len<0)
789
            return AV_NOPTS_VALUE;
790 37e85dac Michael Niedermayer
791
        st = s->streams[stream_index2];
792 01f4895c Michael Niedermayer
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
793 899681cd Björn Axelsson
            h= get_byte(s->pb); len--;
794 37e85dac Michael Niedermayer
            if(!(h & 0x40)){
795 899681cd Björn Axelsson
                seq = get_byte(s->pb); len--;
796 bc17df09 Michael Niedermayer
            }
797
        }
798 115329f1 Diego Biurrun
799 37e85dac Michael Niedermayer
        if((flags&2) && (seq&0x7F) == 1){
800 949b1a13 Steve L'Homme
//            av_log(s, AV_LOG_DEBUG, "%d %d-%d %"PRId64" %d\n", flags, stream_index2, stream_index, dts, seq);
801 30a43f2d Michael Niedermayer
            av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
802 37e85dac Michael Niedermayer
            if(stream_index2 == stream_index)
803
                break;
804
        }
805
806 899681cd Björn Axelsson
        url_fskip(s->pb, len);
807 bc17df09 Michael Niedermayer
    }
808
    *ppos = pos;
809
    return dts;
810
}
811
812 ff70e601 Måns Rullgård
AVInputFormat rm_demuxer = {
813 c9a65ca8 Fabrice Bellard
    "rm",
814 517ac243 Diego Biurrun
    NULL_IF_CONFIG_SMALL("RealMedia format"),
815 1edddc99 Ronald S. Bultje
    sizeof(RMDemuxContext),
816 c9a65ca8 Fabrice Bellard
    rm_probe,
817
    rm_read_header,
818
    rm_read_packet,
819
    rm_read_close,
820 bc17df09 Michael Niedermayer
    NULL,
821
    rm_read_dts,
822 c9a65ca8 Fabrice Bellard
};
823 ff13ba92 Ronald S. Bultje
824
AVInputFormat rdt_demuxer = {
825
    "rdt",
826
    NULL_IF_CONFIG_SMALL("RDT demuxer"),
827 1edddc99 Ronald S. Bultje
    sizeof(RMDemuxContext),
828 88297e80 Michael Niedermayer
    NULL,
829
    NULL,
830
    NULL,
831
    rm_read_close,
832 ff13ba92 Ronald S. Bultje
};