Statistics
| Branch: | Revision:

ffmpeg / libavformat / rmdec.c @ d7fb5a18

History | View | Annotate | Download (27.2 KB)

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

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