Statistics
| Branch: | Revision:

ffmpeg / libavformat / rmdec.c @ e356fc57

History | View | Annotate | Download (30.7 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 461ef741 Kostya Shishkov
#include "riff.h"
26 ba614729 Ronald S. Bultje
#include "rm.h"
27 1edddc99 Ronald S. Bultje
28 ba614729 Ronald S. Bultje
struct RMStream {
29 0b4ff0c9 Michael Niedermayer
    AVPacket pkt;      ///< place to store merged video frame / reordered audio data
30 1edddc99 Ronald S. Bultje
    int videobufsize;  ///< current assembled frame size
31
    int videobufpos;   ///< position for the next slice in the video buffer
32
    int curpic_num;    ///< picture number of current frame
33
    int cur_slice, slices;
34
    int64_t pktpos;    ///< first slice position in file
35
    /// Audio descrambling matrix parameters
36
    int64_t audiotimestamp; ///< Audio packet timestamp
37
    int sub_packet_cnt; // Subpacket counter, used while reading
38
    int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
39 ba614729 Ronald S. Bultje
    int audio_framesize; /// Audio frame size from container
40
    int sub_packet_lengths[16]; /// Length of each subpacket
41
};
42
43
typedef struct {
44
    int nb_packets;
45
    int old_format;
46
    int current_stream;
47
    int remaining_len;
48 1edddc99 Ronald S. Bultje
    int audio_stream_num; ///< Stream number for audio packets
49
    int audio_pkt_cnt; ///< Output packet counter
50
} RMDemuxContext;
51 de6d9b64 Fabrice Bellard
52 cf16c17a Ronald S. Bultje
static const unsigned char sipr_swaps[38][2] = {
53
    {  0, 63 }, {  1, 22 }, {  2, 44 }, {  3, 90 },
54
    {  5, 81 }, {  7, 31 }, {  8, 86 }, {  9, 58 },
55
    { 10, 36 }, { 12, 68 }, { 13, 39 }, { 14, 73 },
56
    { 15, 53 }, { 16, 69 }, { 17, 57 }, { 19, 88 },
57
    { 20, 34 }, { 21, 71 }, { 24, 46 }, { 25, 94 },
58
    { 26, 54 }, { 28, 75 }, { 29, 50 }, { 32, 70 },
59
    { 33, 92 }, { 35, 74 }, { 38, 85 }, { 40, 56 },
60
    { 42, 87 }, { 43, 65 }, { 45, 59 }, { 48, 79 },
61
    { 49, 93 }, { 51, 89 }, { 55, 95 }, { 61, 76 },
62
    { 67, 83 }, { 77, 80 }
63
};
64
65 adae9fa6 Aurelien Jacobs
const unsigned char ff_sipr_subpk_size[4] = { 29, 19, 37, 20 };
66 cf16c17a Ronald S. Bultje
67 ae628ec1 Anton Khirnov
static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
68 de6d9b64 Fabrice Bellard
{
69 36cf20f5 Ronald S. Bultje
    int i;
70 28263f5f Ronald S. Bultje
    char *q, r;
71 de6d9b64 Fabrice Bellard
72
    q = buf;
73
    for(i=0;i<len;i++) {
74 b7effd4e Anton Khirnov
        r = avio_r8(pb);
75 de6d9b64 Fabrice Bellard
        if (i < buf_size - 1)
76 28263f5f Ronald S. Bultje
            *q++ = r;
77 de6d9b64 Fabrice Bellard
    }
78 28263f5f Ronald S. Bultje
    if (buf_size > 0) *q = '\0';
79 de6d9b64 Fabrice Bellard
}
80
81 ae628ec1 Anton Khirnov
static void get_str8(AVIOContext *pb, char *buf, int buf_size)
82 36cf20f5 Ronald S. Bultje
{
83 b7effd4e Anton Khirnov
    get_strl(pb, buf, buf_size, avio_r8(pb));
84 de6d9b64 Fabrice Bellard
}
85
86 ae628ec1 Anton Khirnov
static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
87 0dae3e13 Kostya Shishkov
{
88
    if (size >= 1<<24)
89
        return -1;
90
    avctx->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
91
    if (!avctx->extradata)
92 2874c81c Stefano Sabatini
        return AVERROR(ENOMEM);
93 b7effd4e Anton Khirnov
    avctx->extradata_size = avio_read(pb, avctx->extradata, size);
94 0dae3e13 Kostya Shishkov
    memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
95
    if (avctx->extradata_size != size)
96
        return AVERROR(EIO);
97
    return 0;
98
}
99
100 7379d5bc Aurelien Jacobs
static void rm_read_metadata(AVFormatContext *s, int wide)
101
{
102
    char buf[1024];
103
    int i;
104
    for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
105 b7effd4e Anton Khirnov
        int len = wide ? avio_rb16(s->pb) : avio_r8(s->pb);
106 7379d5bc Aurelien Jacobs
        get_strl(s->pb, buf, sizeof(buf), len);
107 2ef6c124 Stefano Sabatini
        av_metadata_set2(&s->metadata, ff_rm_metadata[i], buf, 0);
108 7379d5bc Aurelien Jacobs
    }
109
}
110
111 ba614729 Ronald S. Bultje
RMStream *ff_rm_alloc_rmstream (void)
112
{
113
    RMStream *rms = av_mallocz(sizeof(RMStream));
114
    rms->curpic_num = -1;
115
    return rms;
116
}
117
118
void ff_rm_free_rmstream (RMStream *rms)
119
{
120 0b4ff0c9 Michael Niedermayer
    av_free_packet(&rms->pkt);
121 ba614729 Ronald S. Bultje
}
122
123 ae628ec1 Anton Khirnov
static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
124 886e89d0 Ronald S. Bultje
                                     AVStream *st, RMStream *ast, int read_all)
125 c9510803 Fabrice Bellard
{
126 1e4668d1 Roberto Togni
    char buf[256];
127 c9510803 Fabrice Bellard
    uint32_t version;
128 0dae3e13 Kostya Shishkov
    int ret;
129 c9510803 Fabrice Bellard
130
    /* ra type header */
131 b7effd4e Anton Khirnov
    version = avio_rb16(pb); /* version */
132 119c61a3 Kostya Shishkov
    if (version == 3) {
133 b7effd4e Anton Khirnov
        int header_size = avio_rb16(pb);
134 1e4668d1 Roberto Togni
        int64_t startpos = url_ftell(pb);
135 e356fc57 Anton Khirnov
        avio_seek(pb, 14, SEEK_CUR);
136 7379d5bc Aurelien Jacobs
        rm_read_metadata(s, 0);
137 119c61a3 Kostya Shishkov
        if ((startpos + header_size) >= url_ftell(pb) + 2) {
138 76f940b2 Ronald S. Bultje
            // fourcc (should always be "lpcJ")
139 b7effd4e Anton Khirnov
            avio_r8(pb);
140 76f940b2 Ronald S. Bultje
            get_str8(pb, buf, sizeof(buf));
141 1e4668d1 Roberto Togni
        }
142
        // Skip extra header crap (this should never happen)
143 119c61a3 Kostya Shishkov
        if ((startpos + header_size) > url_ftell(pb))
144 e356fc57 Anton Khirnov
            avio_seek(pb, header_size + startpos - url_ftell(pb), SEEK_CUR);
145 01f4895c Michael Niedermayer
        st->codec->sample_rate = 8000;
146
        st->codec->channels = 1;
147 72415b2a Stefano Sabatini
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
148 01f4895c Michael Niedermayer
        st->codec->codec_id = CODEC_ID_RA_144;
149 c9510803 Fabrice Bellard
    } else {
150 e0f7e329 Benjamin Larsson
        int flavor, sub_packet_h, coded_framesize, sub_packet_size;
151 851ef875 Kostya Shishkov
        int codecdata_length;
152 c9510803 Fabrice Bellard
        /* old version (4) */
153 e356fc57 Anton Khirnov
        avio_seek(pb, 2, SEEK_CUR); /* unused */
154 b7effd4e Anton Khirnov
        avio_rb32(pb); /* .ra4 */
155
        avio_rb32(pb); /* data size */
156
        avio_rb16(pb); /* version2 */
157
        avio_rb32(pb); /* header size */
158
        flavor= avio_rb16(pb); /* add codec info / flavor */
159
        ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
160
        avio_rb32(pb); /* ??? */
161
        avio_rb32(pb); /* ??? */
162
        avio_rb32(pb); /* ??? */
163
        ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
164
        st->codec->block_align= avio_rb16(pb); /* frame size */
165
        ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
166
        avio_rb16(pb); /* ??? */
167 119c61a3 Kostya Shishkov
        if (version == 5) {
168 b7effd4e Anton Khirnov
            avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
169 127ed7b0 Ronald S. Bultje
        }
170 b7effd4e Anton Khirnov
        st->codec->sample_rate = avio_rb16(pb);
171
        avio_rb32(pb);
172
        st->codec->channels = avio_rb16(pb);
173 119c61a3 Kostya Shishkov
        if (version == 5) {
174 b7effd4e Anton Khirnov
            avio_rb32(pb);
175
            avio_read(pb, buf, 4);
176 bb270c08 Diego Biurrun
            buf[4] = 0;
177
        } else {
178 e344c1ea Steve L'Homme
            get_str8(pb, buf, sizeof(buf)); /* desc */
179
            get_str8(pb, buf, sizeof(buf)); /* desc */
180 bb270c08 Diego Biurrun
        }
181 72415b2a Stefano Sabatini
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
182 461ef741 Kostya Shishkov
        st->codec->codec_tag  = AV_RL32(buf);
183 74e8ccad Francesco Lavra
        st->codec->codec_id   = ff_codec_get_id(ff_rm_codec_tags,
184
                                                st->codec->codec_tag);
185 461ef741 Kostya Shishkov
        switch (st->codec->codec_id) {
186
        case CODEC_ID_AC3:
187 bf415121 Justin Ruggles
            st->need_parsing = AVSTREAM_PARSE_FULL;
188 461ef741 Kostya Shishkov
            break;
189
        case CODEC_ID_RA_288:
190 e0f7e329 Benjamin Larsson
            st->codec->extradata_size= 0;
191 ba614729 Ronald S. Bultje
            ast->audio_framesize = st->codec->block_align;
192 e0f7e329 Benjamin Larsson
            st->codec->block_align = coded_framesize;
193 a443a253 Michael Niedermayer
194 ba614729 Ronald S. Bultje
            if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
195
                av_log(s, AV_LOG_ERROR, "ast->audio_framesize * sub_packet_h too large\n");
196 a443a253 Michael Niedermayer
                return -1;
197
            }
198
199 0b4ff0c9 Michael Niedermayer
            av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
200 461ef741 Kostya Shishkov
            break;
201
        case CODEC_ID_COOK:
202
        case CODEC_ID_ATRAC3:
203
        case CODEC_ID_SIPR:
204 b7effd4e Anton Khirnov
            avio_rb16(pb); avio_r8(pb);
205 119c61a3 Kostya Shishkov
            if (version == 5)
206 b7effd4e Anton Khirnov
                avio_r8(pb);
207
            codecdata_length = avio_rb32(pb);
208 a443a253 Michael Niedermayer
            if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
209
                av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
210
                return -1;
211
            }
212
213 cf16c17a Ronald S. Bultje
            ast->audio_framesize = st->codec->block_align;
214
            if (st->codec->codec_id == CODEC_ID_SIPR) {
215
                if (flavor > 3) {
216
                    av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
217
                           flavor);
218
                    return -1;
219
                }
220 adae9fa6 Aurelien Jacobs
                st->codec->block_align = ff_sipr_subpk_size[flavor];
221 cf16c17a Ronald S. Bultje
            } else {
222
                if(sub_packet_size <= 0){
223
                    av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
224
                    return -1;
225
                }
226
                st->codec->block_align = ast->sub_packet_size;
227
            }
228 f9664ca0 Ronald S. Bultje
            if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
229 0dae3e13 Kostya Shishkov
                return ret;
230 a443a253 Michael Niedermayer
231 ba614729 Ronald S. Bultje
            if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
232 a443a253 Michael Niedermayer
                av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
233
                return -1;
234
            }
235
236 0b4ff0c9 Michael Niedermayer
            av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
237 461ef741 Kostya Shishkov
            break;
238
        case CODEC_ID_AAC:
239 b7effd4e Anton Khirnov
            avio_rb16(pb); avio_r8(pb);
240 119c61a3 Kostya Shishkov
            if (version == 5)
241 b7effd4e Anton Khirnov
                avio_r8(pb);
242
            codecdata_length = avio_rb32(pb);
243 a9d4a6ef Roberto Togni
            if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
244
                av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
245
                return -1;
246
            }
247 a194f595 Roberto Togni
            if (codecdata_length >= 1) {
248 b7effd4e Anton Khirnov
                avio_r8(pb);
249 f9664ca0 Ronald S. Bultje
                if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
250 0dae3e13 Kostya Shishkov
                    return ret;
251 a194f595 Roberto Togni
            }
252 461ef741 Kostya Shishkov
            break;
253
        default:
254 75e61b0e Måns Rullgård
            av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
255 c9510803 Fabrice Bellard
        }
256
        if (read_all) {
257 b7effd4e Anton Khirnov
            avio_r8(pb);
258
            avio_r8(pb);
259
            avio_r8(pb);
260 7379d5bc Aurelien Jacobs
            rm_read_metadata(s, 0);
261 c9510803 Fabrice Bellard
        }
262
    }
263 a9d4a6ef Roberto Togni
    return 0;
264 c9510803 Fabrice Bellard
}
265
266 0fd36362 Ronald S. Bultje
int
267 ae628ec1 Anton Khirnov
ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
268 886e89d0 Ronald S. Bultje
                           AVStream *st, RMStream *rst, int codec_data_size)
269 ff1463dc Ronald S. Bultje
{
270
    unsigned int v;
271 79d5045d Ronald S. Bultje
    int size;
272 ff1463dc Ronald S. Bultje
    int64_t codec_pos;
273 0dae3e13 Kostya Shishkov
    int ret;
274 ff1463dc Ronald S. Bultje
275 15995030 Ronald S. Bultje
    av_set_pts_info(st, 64, 1, 1000);
276 ff1463dc Ronald S. Bultje
    codec_pos = url_ftell(pb);
277 b7effd4e Anton Khirnov
    v = avio_rb32(pb);
278 ff1463dc Ronald S. Bultje
    if (v == MKTAG(0xfd, 'a', 'r', '.')) {
279
        /* ra type header */
280 886e89d0 Ronald S. Bultje
        if (rm_read_audio_stream_info(s, pb, st, rst, 0))
281 ff1463dc Ronald S. Bultje
            return -1;
282
    } else {
283
        int fps, fps2;
284 b7effd4e Anton Khirnov
        if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
285 ff1463dc Ronald S. Bultje
        fail1:
286
            av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
287
            goto skip;
288
        }
289 b7effd4e Anton Khirnov
        st->codec->codec_tag = avio_rl32(pb);
290 74e8ccad Francesco Lavra
        st->codec->codec_id  = ff_codec_get_id(ff_rm_codec_tags,
291
                                               st->codec->codec_tag);
292 0fd7c133 Benoit Fouet
//        av_log(s, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
293 461ef741 Kostya Shishkov
        if (st->codec->codec_id == CODEC_ID_NONE)
294 ff1463dc Ronald S. Bultje
            goto fail1;
295 b7effd4e Anton Khirnov
        st->codec->width = avio_rb16(pb);
296
        st->codec->height = avio_rb16(pb);
297 ff1463dc Ronald S. Bultje
        st->codec->time_base.num= 1;
298 b7effd4e Anton Khirnov
        fps= avio_rb16(pb);
299 72415b2a Stefano Sabatini
        st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
300 b7effd4e Anton Khirnov
        avio_rb32(pb);
301
        fps2= avio_rb16(pb);
302
        avio_rb16(pb);
303 ff1463dc Ronald S. Bultje
304 f9664ca0 Ronald S. Bultje
        if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (url_ftell(pb) - codec_pos))) < 0)
305 0dae3e13 Kostya Shishkov
            return ret;
306 ff1463dc Ronald S. Bultje
307 0fd7c133 Benoit Fouet
//        av_log(s, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
308 ff1463dc Ronald S. Bultje
        st->codec->time_base.den = fps * st->codec->time_base.num;
309 461ef741 Kostya Shishkov
        //XXX: do we really need that?
310 17986563 Michael Niedermayer
        switch(st->codec->extradata[4]>>4){
311 ff1463dc Ronald S. Bultje
        case 1: st->codec->codec_id = CODEC_ID_RV10; break;
312
        case 2: st->codec->codec_id = CODEC_ID_RV20; break;
313
        case 3: st->codec->codec_id = CODEC_ID_RV30; break;
314
        case 4: st->codec->codec_id = CODEC_ID_RV40; break;
315 005d9b57 Michael Niedermayer
        default:
316 17986563 Michael Niedermayer
            av_log(st->codec, AV_LOG_ERROR, "extra:%02X %02X %02X %02X %02X\n", st->codec->extradata[0], st->codec->extradata[1], st->codec->extradata[2], st->codec->extradata[3], st->codec->extradata[4]);
317 005d9b57 Michael Niedermayer
            goto fail1;
318 ff1463dc Ronald S. Bultje
        }
319
    }
320
321
skip:
322
    /* skip codec info */
323
    size = url_ftell(pb) - codec_pos;
324 e356fc57 Anton Khirnov
    avio_seek(pb, codec_data_size - size, SEEK_CUR);
325 ff1463dc Ronald S. Bultje
326 36c9e40a Benoit Fouet
    return 0;
327 ff1463dc Ronald S. Bultje
}
328
329 2d6f3014 Ronald S. Bultje
/** this function assumes that the demuxer has already seeked to the start
330
 * of the INDX chunk, and will bail out if not. */
331
static int rm_read_index(AVFormatContext *s)
332
{
333 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
334 2d6f3014 Ronald S. Bultje
    unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
335
    AVStream *st;
336
337
    do {
338 b7effd4e Anton Khirnov
        if (avio_rl32(pb) != MKTAG('I','N','D','X'))
339 2d6f3014 Ronald S. Bultje
            return -1;
340 b7effd4e Anton Khirnov
        size     = avio_rb32(pb);
341 2d6f3014 Ronald S. Bultje
        if (size < 20)
342
            return -1;
343 e356fc57 Anton Khirnov
        avio_seek(pb, 2, SEEK_CUR);
344 b7effd4e Anton Khirnov
        n_pkts   = avio_rb32(pb);
345
        str_id   = avio_rb16(pb);
346
        next_off = avio_rb32(pb);
347 2d6f3014 Ronald S. Bultje
        for (n = 0; n < s->nb_streams; n++)
348
            if (s->streams[n]->id == str_id) {
349
                st = s->streams[n];
350
                break;
351
            }
352
        if (n == s->nb_streams)
353
            goto skip;
354
355
        for (n = 0; n < n_pkts; n++) {
356 e356fc57 Anton Khirnov
            avio_seek(pb, 2, SEEK_CUR);
357 b7effd4e Anton Khirnov
            pts = avio_rb32(pb);
358
            pos = avio_rb32(pb);
359 e356fc57 Anton Khirnov
            avio_seek(pb, 4, SEEK_CUR); /* packet no. */
360 2d6f3014 Ronald S. Bultje
361
            av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
362
        }
363
364
skip:
365
        if (next_off && url_ftell(pb) != next_off &&
366 6b4aa5da Anton Khirnov
            avio_seek(pb, next_off, SEEK_SET) < 0)
367 2d6f3014 Ronald S. Bultje
            return -1;
368
    } while (next_off);
369
370
    return 0;
371
}
372 ff1463dc Ronald S. Bultje
373 c9510803 Fabrice Bellard
static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
374
{
375 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
376 c9510803 Fabrice Bellard
    AVStream *st;
377
378
    rm->old_format = 1;
379
    st = av_new_stream(s, 0);
380
    if (!st)
381 a9d4a6ef Roberto Togni
        return -1;
382 886e89d0 Ronald S. Bultje
    st->priv_data = ff_rm_alloc_rmstream();
383
    return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
384 c9510803 Fabrice Bellard
}
385
386 de6d9b64 Fabrice Bellard
static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
387
{
388 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
389 de6d9b64 Fabrice Bellard
    AVStream *st;
390 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
391 ff1463dc Ronald S. Bultje
    unsigned int tag;
392 07cb073b Diego Biurrun
    int tag_size;
393 36c32bdd Diego Biurrun
    unsigned int start_time, duration;
394 2d6f3014 Ronald S. Bultje
    unsigned int data_off = 0, indx_off = 0;
395 de6d9b64 Fabrice Bellard
    char buf[128];
396 5d6ef63f Philip Gladstone
    int flags = 0;
397 de6d9b64 Fabrice Bellard
398 b7effd4e Anton Khirnov
    tag = avio_rl32(pb);
399 c9510803 Fabrice Bellard
    if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
400
        /* very old .ra format */
401
        return rm_read_header_old(s, ap);
402
    } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
403 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
404 c9510803 Fabrice Bellard
    }
405 de6d9b64 Fabrice Bellard
406 b7effd4e Anton Khirnov
    avio_rb32(pb); /* header size */
407
    avio_rb16(pb);
408
    avio_rb32(pb);
409
    avio_rb32(pb); /* number of headers */
410 115329f1 Diego Biurrun
411 de6d9b64 Fabrice Bellard
    for(;;) {
412
        if (url_feof(pb))
413 ccafd472 Baptiste Coudurier
            return -1;
414 b7effd4e Anton Khirnov
        tag = avio_rl32(pb);
415
        tag_size = avio_rb32(pb);
416
        avio_rb16(pb);
417 de6d9b64 Fabrice Bellard
#if 0
418 115329f1 Diego Biurrun
        printf("tag=%c%c%c%c (%08x) size=%d\n",
419 de6d9b64 Fabrice Bellard
               (tag) & 0xff,
420
               (tag >> 8) & 0xff,
421
               (tag >> 16) & 0xff,
422
               (tag >> 24) & 0xff,
423
               tag,
424
               tag_size);
425
#endif
426 9786bd4d Michael Niedermayer
        if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
427 ccafd472 Baptiste Coudurier
            return -1;
428 de6d9b64 Fabrice Bellard
        switch(tag) {
429
        case MKTAG('P', 'R', 'O', 'P'):
430
            /* file header */
431 b7effd4e Anton Khirnov
            avio_rb32(pb); /* max bit rate */
432
            avio_rb32(pb); /* avg bit rate */
433
            avio_rb32(pb); /* max packet size */
434
            avio_rb32(pb); /* avg packet size */
435
            avio_rb32(pb); /* nb packets */
436
            avio_rb32(pb); /* duration */
437
            avio_rb32(pb); /* preroll */
438
            indx_off = avio_rb32(pb); /* index offset */
439
            data_off = avio_rb32(pb); /* data offset */
440
            avio_rb16(pb); /* nb streams */
441
            flags = avio_rb16(pb); /* flags */
442 de6d9b64 Fabrice Bellard
            break;
443
        case MKTAG('C', 'O', 'N', 'T'):
444 7379d5bc Aurelien Jacobs
            rm_read_metadata(s, 1);
445 de6d9b64 Fabrice Bellard
            break;
446
        case MKTAG('M', 'D', 'P', 'R'):
447 247eadca Fabrice Bellard
            st = av_new_stream(s, 0);
448 de6d9b64 Fabrice Bellard
            if (!st)
449 ccafd472 Baptiste Coudurier
                return AVERROR(ENOMEM);
450 b7effd4e Anton Khirnov
            st->id = avio_rb16(pb);
451
            avio_rb32(pb); /* max bit rate */
452
            st->codec->bit_rate = avio_rb32(pb); /* bit rate */
453
            avio_rb32(pb); /* max packet size */
454
            avio_rb32(pb); /* avg packet size */
455
            start_time = avio_rb32(pb); /* start time */
456
            avio_rb32(pb); /* preroll */
457
            duration = avio_rb32(pb); /* duration */
458 c0df9d75 Michael Niedermayer
            st->start_time = start_time;
459
            st->duration = duration;
460 de6d9b64 Fabrice Bellard
            get_str8(pb, buf, sizeof(buf)); /* desc */
461
            get_str8(pb, buf, sizeof(buf)); /* mimetype */
462 72415b2a Stefano Sabatini
            st->codec->codec_type = AVMEDIA_TYPE_DATA;
463 886e89d0 Ronald S. Bultje
            st->priv_data = ff_rm_alloc_rmstream();
464
            if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
465 b7effd4e Anton Khirnov
                                          avio_rb32(pb)) < 0)
466 ff1463dc Ronald S. Bultje
                return -1;
467 de6d9b64 Fabrice Bellard
            break;
468
        case MKTAG('D', 'A', 'T', 'A'):
469
            goto header_end;
470
        default:
471
            /* unknown tag: skip it */
472 e356fc57 Anton Khirnov
            avio_seek(pb, tag_size - 10, SEEK_CUR);
473 de6d9b64 Fabrice Bellard
            break;
474
        }
475
    }
476
 header_end:
477 b7effd4e Anton Khirnov
    rm->nb_packets = avio_rb32(pb); /* number of packets */
478 5d6ef63f Philip Gladstone
    if (!rm->nb_packets && (flags & 4))
479
        rm->nb_packets = 3600 * 25;
480 b7effd4e Anton Khirnov
    avio_rb32(pb); /* next data header */
481 2d6f3014 Ronald S. Bultje
482
    if (!data_off)
483
        data_off = url_ftell(pb) - 18;
484 c32b7017 Reimar Döffinger
    if (indx_off && !url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX) &&
485 6b4aa5da Anton Khirnov
        avio_seek(pb, indx_off, SEEK_SET) >= 0) {
486 2d6f3014 Ronald S. Bultje
        rm_read_index(s);
487 6b4aa5da Anton Khirnov
        avio_seek(pb, data_off + 18, SEEK_SET);
488 2d6f3014 Ronald S. Bultje
    }
489
490 de6d9b64 Fabrice Bellard
    return 0;
491
}
492
493 ae628ec1 Anton Khirnov
static int get_num(AVIOContext *pb, int *len)
494 4c2bc159 Michael Niedermayer
{
495
    int n, n1;
496
497 b7effd4e Anton Khirnov
    n = avio_rb16(pb);
498 4c2bc159 Michael Niedermayer
    (*len)-=2;
499 e3052ce7 Roberto Togni
    n &= 0x7FFF;
500 4c2bc159 Michael Niedermayer
    if (n >= 0x4000) {
501
        return n - 0x4000;
502
    } else {
503 b7effd4e Anton Khirnov
        n1 = avio_rb16(pb);
504 4c2bc159 Michael Niedermayer
        (*len)-=2;
505
        return (n << 16) | n1;
506
    }
507
}
508
509 c9510803 Fabrice Bellard
/* multiple of 20 bytes for ra144 (ugly) */
510
#define RAW_PACKET_SIZE 1000
511
512 37e85dac Michael Niedermayer
static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
513 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
514 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
515 bc17df09 Michael Niedermayer
    AVStream *st;
516 119cebf4 Michael Niedermayer
    uint32_t state=0xFFFFFFFF;
517 bc17df09 Michael Niedermayer
518
    while(!url_feof(pb)){
519 ddf8e75e Ronald S. Bultje
        int len, num, i;
520 fd31550d Michael Niedermayer
        *pos= url_ftell(pb) - 3;
521 bc17df09 Michael Niedermayer
        if(rm->remaining_len > 0){
522
            num= rm->current_stream;
523
            len= rm->remaining_len;
524
            *timestamp = AV_NOPTS_VALUE;
525
            *flags= 0;
526
        }else{
527 b7effd4e Anton Khirnov
            state= (state<<8) + avio_r8(pb);
528 115329f1 Diego Biurrun
529 119cebf4 Michael Niedermayer
            if(state == MKBETAG('I', 'N', 'D', 'X')){
530 b5b9ff2a Ronald S. Bultje
                int n_pkts, expected_len;
531 b7effd4e Anton Khirnov
                len = avio_rb32(pb);
532 e356fc57 Anton Khirnov
                avio_seek(pb, 2, SEEK_CUR);
533 b7effd4e Anton Khirnov
                n_pkts = avio_rb32(pb);
534 b5b9ff2a Ronald S. Bultje
                expected_len = 20 + n_pkts * 14;
535
                if (len == 20)
536
                    /* some files don't add index entries to chunk size... */
537
                    len = expected_len;
538
                else if (len != expected_len)
539
                    av_log(s, AV_LOG_WARNING,
540
                           "Index size %d (%d pkts) is wrong, should be %d.\n",
541
                           len, n_pkts, expected_len);
542
                len -= 14; // we already read part of the index header
543 119cebf4 Michael Niedermayer
                if(len<0)
544
                    continue;
545
                goto skip;
546 98287358 Ronald S. Bultje
            } else if (state == MKBETAG('D','A','T','A')) {
547
                av_log(s, AV_LOG_WARNING,
548
                       "DATA tag in middle of chunk, file may be broken.\n");
549 119cebf4 Michael Niedermayer
            }
550 115329f1 Diego Biurrun
551 83c0ba3d Ronald S. Bultje
            if(state > (unsigned)0xFFFF || state <= 12)
552 bc17df09 Michael Niedermayer
                continue;
553 e75e603c Ronald S. Bultje
            len=state - 12;
554 119cebf4 Michael Niedermayer
            state= 0xFFFFFFFF;
555
556 b7effd4e Anton Khirnov
            num = avio_rb16(pb);
557
            *timestamp = avio_rb32(pb);
558
            avio_r8(pb); /* reserved */
559
            *flags = avio_r8(pb); /* flags */
560 bc17df09 Michael Niedermayer
        }
561
        for(i=0;i<s->nb_streams;i++) {
562
            st = s->streams[i];
563
            if (num == st->id)
564
                break;
565
        }
566
        if (i == s->nb_streams) {
567 119cebf4 Michael Niedermayer
skip:
568 bc17df09 Michael Niedermayer
            /* skip packet if unknown number */
569 e356fc57 Anton Khirnov
            avio_seek(pb, len, SEEK_CUR);
570 b8cc5a9f Ronald S. Bultje
            rm->remaining_len = 0;
571 bc17df09 Michael Niedermayer
            continue;
572
        }
573
        *stream_index= i;
574 115329f1 Diego Biurrun
575 bc17df09 Michael Niedermayer
        return len;
576
    }
577
    return -1;
578
}
579
580 ae628ec1 Anton Khirnov
static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
581 ba614729 Ronald S. Bultje
                                   RMDemuxContext *rm, RMStream *vst,
582 7a160bca Ronald S. Bultje
                                   AVPacket *pkt, int len, int *pseq)
583 383b123e Kostya Shishkov
{
584
    int hdr, seq, pic_num, len2, pos;
585
    int type;
586
587 b7effd4e Anton Khirnov
    hdr = avio_r8(pb); len--;
588 383b123e Kostya Shishkov
    type = hdr >> 6;
589 f48d5457 Michael Niedermayer
590
    if(type != 3){  // not frame as a part of packet
591 b7effd4e Anton Khirnov
        seq = avio_r8(pb); len--;
592 f48d5457 Michael Niedermayer
    }
593
    if(type != 1){  // not whole frame
594 383b123e Kostya Shishkov
        len2 = get_num(pb, &len);
595 f48d5457 Michael Niedermayer
        pos  = get_num(pb, &len);
596 b7effd4e Anton Khirnov
        pic_num = avio_r8(pb); len--;
597 f48d5457 Michael Niedermayer
    }
598
    if(len<0)
599
        return -1;
600
    rm->remaining_len = len;
601
    if(type&1){     // frame, not slice
602
        if(type == 3)  // frame as a part of packet
603
            len= len2;
604
        if(rm->remaining_len < len)
605 e45673cb Michael Niedermayer
            return -1;
606 f48d5457 Michael Niedermayer
        rm->remaining_len -= len;
607 383b123e Kostya Shishkov
        if(av_new_packet(pkt, len + 9) < 0)
608
            return AVERROR(EIO);
609
        pkt->data[0] = 0;
610
        AV_WL32(pkt->data + 1, 1);
611
        AV_WL32(pkt->data + 5, 0);
612 b7effd4e Anton Khirnov
        avio_read(pb, pkt->data + 9, len);
613 383b123e Kostya Shishkov
        return 0;
614
    }
615
    //now we have to deal with single slice
616
617 7a160bca Ronald S. Bultje
    *pseq = seq;
618 ba614729 Ronald S. Bultje
    if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
619
        vst->slices = ((hdr & 0x3F) << 1) + 1;
620
        vst->videobufsize = len2 + 8*vst->slices + 1;
621 0b4ff0c9 Michael Niedermayer
        av_free_packet(&vst->pkt); //FIXME this should be output.
622
        if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
623 52537534 Roberto Togni
            return AVERROR(ENOMEM);
624 ba614729 Ronald S. Bultje
        vst->videobufpos = 8*vst->slices + 1;
625
        vst->cur_slice = 0;
626
        vst->curpic_num = pic_num;
627
        vst->pktpos = url_ftell(pb);
628 383b123e Kostya Shishkov
    }
629 e35033e6 Roberto Togni
    if(type == 2)
630 383b123e Kostya Shishkov
        len = FFMIN(len, pos);
631
632 ba614729 Ronald S. Bultje
    if(++vst->cur_slice > vst->slices)
633 383b123e Kostya Shishkov
        return 1;
634 0b4ff0c9 Michael Niedermayer
    AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
635
    AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
636 ba614729 Ronald S. Bultje
    if(vst->videobufpos + len > vst->videobufsize)
637 383b123e Kostya Shishkov
        return 1;
638 b7effd4e Anton Khirnov
    if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
639 383b123e Kostya Shishkov
        return AVERROR(EIO);
640 ba614729 Ronald S. Bultje
    vst->videobufpos += len;
641 383b123e Kostya Shishkov
    rm->remaining_len-= len;
642
643 ba614729 Ronald S. Bultje
    if(type == 2 || (vst->videobufpos) == vst->videobufsize){
644 188e67ea Michael Niedermayer
        vst->pkt.data[0] = vst->cur_slice-1;
645
        *pkt= vst->pkt;
646 6fde7afa Dominique Leuenberger
        vst->pkt.data= NULL;
647 188e67ea Michael Niedermayer
        vst->pkt.size= 0;
648
        if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
649 0b4ff0c9 Michael Niedermayer
            memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
650 ba614729 Ronald S. Bultje
                vst->videobufpos - 1 - 8*vst->slices);
651 ce0453aa Michael Niedermayer
        pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
652 188e67ea Michael Niedermayer
        pkt->pts = AV_NOPTS_VALUE;
653
        pkt->pos = vst->pktpos;
654 cbb5da57 Reimar Döffinger
        vst->slices = 0;
655 188e67ea Michael Niedermayer
        return 0;
656 383b123e Kostya Shishkov
    }
657
658
    return 1;
659
}
660
661 5c470b91 Ronald S. Bultje
static inline void
662
rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
663
{
664
    uint8_t *ptr;
665
    int j;
666
667
    if (st->codec->codec_id == CODEC_ID_AC3) {
668
        ptr = pkt->data;
669
        for (j=0;j<pkt->size;j+=2) {
670
            FFSWAP(int, ptr[0], ptr[1]);
671
            ptr += 2;
672
        }
673
    }
674
}
675
676 e6b09f43 Ronald S. Bultje
/**
677
 * Perform 4-bit block reordering for SIPR data.
678
 * @todo This can be optimized, e.g. use memcpy() if data blocks are aligned
679
 */
680 1ab0406d Aurelien Jacobs
void ff_rm_reorder_sipr_data(uint8_t *buf, int sub_packet_h, int framesize)
681 cf16c17a Ronald S. Bultje
{
682 1ab0406d Aurelien Jacobs
    int n, bs = sub_packet_h * framesize * 2 / 96; // nibbles per subpacket
683 cf16c17a Ronald S. Bultje
684
    for (n = 0; n < 38; n++) {
685
        int j;
686
        int i = bs * sipr_swaps[n][0];
687
        int o = bs * sipr_swaps[n][1];
688
689
        /* swap 4bit-nibbles of block 'i' with 'o' */
690
        for (j = 0; j < bs; j++, i++, o++) {
691
            int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF,
692
                y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF;
693
694
            buf[o >> 1] = (x << (4 * (o & 1))) |
695
                (buf[o >> 1] & (0xF << (4 * !(o & 1))));
696
            buf[i >> 1] = (y << (4 * (i & 1))) |
697
                (buf[i >> 1] & (0xF << (4 * !(i & 1))));
698
        }
699
    }
700
}
701
702 0fd36362 Ronald S. Bultje
int
703 ae628ec1 Anton Khirnov
ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
704 886e89d0 Ronald S. Bultje
                    AVStream *st, RMStream *ast, int len, AVPacket *pkt,
705 34bddc39 Ronald S. Bultje
                    int *seq, int flags, int64_t timestamp)
706 afd302fa Ronald S. Bultje
{
707 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
708 afd302fa Ronald S. Bultje
709 72415b2a Stefano Sabatini
    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
710 afd302fa Ronald S. Bultje
        rm->current_stream= st->id;
711 7a160bca Ronald S. Bultje
        if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq))
712 afd302fa Ronald S. Bultje
            return -1; //got partial frame
713 72415b2a Stefano Sabatini
    } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
714 afd302fa Ronald S. Bultje
        if ((st->codec->codec_id == CODEC_ID_RA_288) ||
715
            (st->codec->codec_id == CODEC_ID_COOK) ||
716 2426bb96 Benjamin Larsson
            (st->codec->codec_id == CODEC_ID_ATRAC3) ||
717
            (st->codec->codec_id == CODEC_ID_SIPR)) {
718 afd302fa Ronald S. Bultje
            int x;
719 ba614729 Ronald S. Bultje
            int sps = ast->sub_packet_size;
720
            int cfs = ast->coded_framesize;
721
            int h = ast->sub_packet_h;
722
            int y = ast->sub_packet_cnt;
723
            int w = ast->audio_framesize;
724 afd302fa Ronald S. Bultje
725 34bddc39 Ronald S. Bultje
            if (flags & 2)
726 ba614729 Ronald S. Bultje
                y = ast->sub_packet_cnt = 0;
727 afd302fa Ronald S. Bultje
            if (!y)
728 34bddc39 Ronald S. Bultje
                ast->audiotimestamp = timestamp;
729 afd302fa Ronald S. Bultje
730
            switch(st->codec->codec_id) {
731
                case CODEC_ID_RA_288:
732
                    for (x = 0; x < h/2; x++)
733 b7effd4e Anton Khirnov
                        avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
734 afd302fa Ronald S. Bultje
                    break;
735
                case CODEC_ID_ATRAC3:
736
                case CODEC_ID_COOK:
737
                    for (x = 0; x < w/sps; x++)
738 b7effd4e Anton Khirnov
                        avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
739 afd302fa Ronald S. Bultje
                    break;
740 cf16c17a Ronald S. Bultje
                case CODEC_ID_SIPR:
741 b7effd4e Anton Khirnov
                    avio_read(pb, ast->pkt.data + y * w, w);
742 cf16c17a Ronald S. Bultje
                    break;
743 afd302fa Ronald S. Bultje
            }
744
745 ba614729 Ronald S. Bultje
            if (++(ast->sub_packet_cnt) < h)
746 afd302fa Ronald S. Bultje
                return -1;
747 cf16c17a Ronald S. Bultje
            if (st->codec->codec_id == CODEC_ID_SIPR)
748 1ab0406d Aurelien Jacobs
                ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
749 cf16c17a Ronald S. Bultje
750 508fe07a Ronald S. Bultje
             ast->sub_packet_cnt = 0;
751
             rm->audio_stream_num = st->index;
752
             rm->audio_pkt_cnt = h * w / st->codec->block_align;
753 afd302fa Ronald S. Bultje
        } else if (st->codec->codec_id == CODEC_ID_AAC) {
754
            int x;
755
            rm->audio_stream_num = st->index;
756 b7effd4e Anton Khirnov
            ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
757 ba614729 Ronald S. Bultje
            if (ast->sub_packet_cnt) {
758
                for (x = 0; x < ast->sub_packet_cnt; x++)
759 b7effd4e Anton Khirnov
                    ast->sub_packet_lengths[x] = avio_rb16(pb);
760 c5efef7b Ronald S. Bultje
                rm->audio_pkt_cnt = ast->sub_packet_cnt;
761 34bddc39 Ronald S. Bultje
                ast->audiotimestamp = timestamp;
762 c5efef7b Ronald S. Bultje
            } else
763
                return -1;
764 badbe09a Roberto Togni
        } else {
765 afd302fa Ronald S. Bultje
            av_get_packet(pb, pkt, len);
766 e29378ff Roberto Togni
            rm_ac3_swap_bytes(st, pkt);
767 badbe09a Roberto Togni
        }
768 afd302fa Ronald S. Bultje
    } else
769
        av_get_packet(pb, pkt, len);
770
771
    pkt->stream_index = st->index;
772
773
#if 0
774 72415b2a Stefano Sabatini
    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
775 afd302fa Ronald S. Bultje
        if(st->codec->codec_id == CODEC_ID_RV20){
776
            int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
777 0fd7c133 Benoit Fouet
            av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
778 afd302fa Ronald S. Bultje

779 34bddc39 Ronald S. Bultje
            seq |= (timestamp&~0x3FFF);
780
            if(seq - timestamp >  0x2000) seq -= 0x4000;
781
            if(seq - timestamp < -0x2000) seq += 0x4000;
782 afd302fa Ronald S. Bultje
        }
783
    }
784
#endif
785
786 34bddc39 Ronald S. Bultje
    pkt->pts= timestamp;
787
    if (flags & 2)
788 cc947f04 Jean-Daniel Dupas
        pkt->flags |= AV_PKT_FLAG_KEY;
789 afd302fa Ronald S. Bultje
790 72415b2a Stefano Sabatini
    return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
791 afd302fa Ronald S. Bultje
}
792
793 a9f84821 Ronald S. Bultje
int
794 ae628ec1 Anton Khirnov
ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
795 886e89d0 Ronald S. Bultje
                      AVStream *st, RMStream *ast, AVPacket *pkt)
796 d4d1b5fb Ronald S. Bultje
{
797 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
798 d4d1b5fb Ronald S. Bultje
799
    assert (rm->audio_pkt_cnt > 0);
800
801
    if (st->codec->codec_id == CODEC_ID_AAC)
802 ba614729 Ronald S. Bultje
        av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
803 d4d1b5fb Ronald S. Bultje
    else {
804
        av_new_packet(pkt, st->codec->block_align);
805 0b4ff0c9 Michael Niedermayer
        memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
806 ba614729 Ronald S. Bultje
               (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
807 d4d1b5fb Ronald S. Bultje
               st->codec->block_align);
808
    }
809
    rm->audio_pkt_cnt--;
810 55d330f9 Ronald S. Bultje
    if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
811
        ast->audiotimestamp = AV_NOPTS_VALUE;
812 cc947f04 Jean-Daniel Dupas
        pkt->flags = AV_PKT_FLAG_KEY;
813 55d330f9 Ronald S. Bultje
    } else
814 c8829279 Ronald S. Bultje
        pkt->flags = 0;
815 d4d1b5fb Ronald S. Bultje
    pkt->stream_index = st->index;
816 a9f84821 Ronald S. Bultje
817
    return rm->audio_pkt_cnt;
818 d4d1b5fb Ronald S. Bultje
}
819
820 de6d9b64 Fabrice Bellard
static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
821
{
822 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
823 de6d9b64 Fabrice Bellard
    AVStream *st;
824 6a2d3fc3 Ronald S. Bultje
    int i, len, res, seq = 1;
825 37e85dac Michael Niedermayer
    int64_t timestamp, pos;
826 7a4a64e7 Ronald S. Bultje
    int flags;
827 de6d9b64 Fabrice Bellard
828 6a2d3fc3 Ronald S. Bultje
    for (;;) {
829 7268f0bd Ronald S. Bultje
        if (rm->audio_pkt_cnt) {
830
            // If there are queued audio packet return them first
831
            st = s->streams[rm->audio_stream_num];
832
            ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
833 190413cf Ronald S. Bultje
            flags = 0;
834 6a2d3fc3 Ronald S. Bultje
        } else {
835
            if (rm->old_format) {
836 7268f0bd Ronald S. Bultje
                RMStream *ast;
837 7ccefd08 Ronald S. Bultje
838 7268f0bd Ronald S. Bultje
                st = s->streams[0];
839
                ast = st->priv_data;
840 6a2d3fc3 Ronald S. Bultje
                timestamp = AV_NOPTS_VALUE;
841
                len = !ast->audio_framesize ? RAW_PACKET_SIZE :
842
                    ast->coded_framesize * ast->sub_packet_h / 2;
843
                flags = (seq++ == 1) ? 2 : 0;
844 7b374c9f Ronald S. Bultje
                pos = url_ftell(s->pb);
845 7268f0bd Ronald S. Bultje
            } else {
846
                len=sync(s, &timestamp, &flags, &i, &pos);
847 c3df4a3b Måns Rullgård
                if (len > 0)
848
                    st = s->streams[i];
849 6a2d3fc3 Ronald S. Bultje
            }
850
851
            if(len<0 || url_feof(s->pb))
852
                return AVERROR(EIO);
853 bc17df09 Michael Niedermayer
854 7268f0bd Ronald S. Bultje
            res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
855 34bddc39 Ronald S. Bultje
                                      &seq, flags, timestamp);
856 7a4a64e7 Ronald S. Bultje
            if((flags&2) && (seq&0x7F) == 1)
857 7268f0bd Ronald S. Bultje
                av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
858 6a2d3fc3 Ronald S. Bultje
            if (res)
859
                continue;
860
        }
861 d880240e Michael Niedermayer
862 dc3685e1 Ronald S. Bultje
        if(  (st->discard >= AVDISCARD_NONKEY && !(flags&2))
863
           || st->discard >= AVDISCARD_ALL){
864
            av_free_packet(pkt);
865 6a2d3fc3 Ronald S. Bultje
        } else
866
            break;
867 5d6ef63f Philip Gladstone
    }
868
869 de6d9b64 Fabrice Bellard
    return 0;
870
}
871
872
static int rm_read_close(AVFormatContext *s)
873
{
874 ba614729 Ronald S. Bultje
    int i;
875
876
    for (i=0;i<s->nb_streams;i++)
877
        ff_rm_free_rmstream(s->streams[i]->priv_data);
878 e0f7e329 Benjamin Larsson
879 de6d9b64 Fabrice Bellard
    return 0;
880
}
881
882 c9a65ca8 Fabrice Bellard
static int rm_probe(AVProbeData *p)
883
{
884
    /* check file header */
885 c9510803 Fabrice Bellard
    if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
886
         p->buf[2] == 'M' && p->buf[3] == 'F' &&
887
         p->buf[4] == 0 && p->buf[5] == 0) ||
888
        (p->buf[0] == '.' && p->buf[1] == 'r' &&
889
         p->buf[2] == 'a' && p->buf[3] == 0xfd))
890 c9a65ca8 Fabrice Bellard
        return AVPROBE_SCORE_MAX;
891
    else
892
        return 0;
893
}
894
895 115329f1 Diego Biurrun
static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
896 bc17df09 Michael Niedermayer
                               int64_t *ppos, int64_t pos_limit)
897
{
898 1edddc99 Ronald S. Bultje
    RMDemuxContext *rm = s->priv_data;
899 bc17df09 Michael Niedermayer
    int64_t pos, dts;
900 37e85dac Michael Niedermayer
    int stream_index2, flags, len, h;
901 bc17df09 Michael Niedermayer
902
    pos = *ppos;
903 115329f1 Diego Biurrun
904 bc17df09 Michael Niedermayer
    if(rm->old_format)
905
        return AV_NOPTS_VALUE;
906
907 6b4aa5da Anton Khirnov
    avio_seek(s->pb, pos, SEEK_SET);
908 bc17df09 Michael Niedermayer
    rm->remaining_len=0;
909
    for(;;){
910 37e85dac Michael Niedermayer
        int seq=1;
911
        AVStream *st;
912
913
        len=sync(s, &dts, &flags, &stream_index2, &pos);
914 bc17df09 Michael Niedermayer
        if(len<0)
915
            return AV_NOPTS_VALUE;
916 37e85dac Michael Niedermayer
917
        st = s->streams[stream_index2];
918 72415b2a Stefano Sabatini
        if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
919 b7effd4e Anton Khirnov
            h= avio_r8(s->pb); len--;
920 37e85dac Michael Niedermayer
            if(!(h & 0x40)){
921 b7effd4e Anton Khirnov
                seq = avio_r8(s->pb); len--;
922 bc17df09 Michael Niedermayer
            }
923
        }
924 115329f1 Diego Biurrun
925 37e85dac Michael Niedermayer
        if((flags&2) && (seq&0x7F) == 1){
926 949b1a13 Steve L'Homme
//            av_log(s, AV_LOG_DEBUG, "%d %d-%d %"PRId64" %d\n", flags, stream_index2, stream_index, dts, seq);
927 30a43f2d Michael Niedermayer
            av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
928 37e85dac Michael Niedermayer
            if(stream_index2 == stream_index)
929
                break;
930
        }
931
932 e356fc57 Anton Khirnov
        avio_seek(s->pb, len, SEEK_CUR);
933 bc17df09 Michael Niedermayer
    }
934
    *ppos = pos;
935
    return dts;
936
}
937
938 c6610a21 Diego Elio Pettenò
AVInputFormat ff_rm_demuxer = {
939 c9a65ca8 Fabrice Bellard
    "rm",
940 517ac243 Diego Biurrun
    NULL_IF_CONFIG_SMALL("RealMedia format"),
941 1edddc99 Ronald S. Bultje
    sizeof(RMDemuxContext),
942 c9a65ca8 Fabrice Bellard
    rm_probe,
943
    rm_read_header,
944
    rm_read_packet,
945
    rm_read_close,
946 bc17df09 Michael Niedermayer
    NULL,
947
    rm_read_dts,
948 c9a65ca8 Fabrice Bellard
};
949 ff13ba92 Ronald S. Bultje
950 c6610a21 Diego Elio Pettenò
AVInputFormat ff_rdt_demuxer = {
951 ff13ba92 Ronald S. Bultje
    "rdt",
952
    NULL_IF_CONFIG_SMALL("RDT demuxer"),
953 1edddc99 Ronald S. Bultje
    sizeof(RMDemuxContext),
954 88297e80 Michael Niedermayer
    NULL,
955
    NULL,
956
    NULL,
957
    rm_read_close,
958 ff13ba92 Ronald S. Bultje
};