~ubuntu-branches/ubuntu/oneiric/libav/oneiric

« back to all changes in this revision

Viewing changes to libavformat/rmdec.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2011-04-30 14:27:42 UTC
  • mfrom: (1.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110430142742-quvblxk1tj6adlh5
Tags: 4:0.7~b1-1ubuntu1
* Merge from debian. Remaining changes:
  - don't build against libfaad, libdirac, librtmp and libopenjpeg
    (all in universe)
  - explicitly --enable-pic on powerpc, cf. LP #654666
  - different arm configure bits that should probably better be
    merged into debian
* Cherry-picked from git: 
  - install doc/APIChanges and refer to them in NEWS.Debian (Closes: #623682)
  - don't try to install non-existing documentation, fixes FTBFS on powerpc

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * "Real" compatible demuxer.
3
3
 * Copyright (c) 2000, 2001 Fabrice Bellard
4
4
 *
5
 
 * This file is part of FFmpeg.
 
5
 * This file is part of Libav.
6
6
 *
7
 
 * FFmpeg is free software; you can redistribute it and/or
 
7
 * Libav is free software; you can redistribute it and/or
8
8
 * modify it under the terms of the GNU Lesser General Public
9
9
 * License as published by the Free Software Foundation; either
10
10
 * version 2.1 of the License, or (at your option) any later version.
11
11
 *
12
 
 * FFmpeg is distributed in the hope that it will be useful,
 
12
 * Libav is distributed in the hope that it will be useful,
13
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
15
 * Lesser General Public License for more details.
16
16
 *
17
17
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with FFmpeg; if not, write to the Free Software
 
18
 * License along with Libav; if not, write to the Free Software
19
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
20
 */
21
21
 
49
49
    int audio_pkt_cnt; ///< Output packet counter
50
50
} RMDemuxContext;
51
51
 
52
 
static const AVCodecTag rm_codec_tags[] = {
53
 
    { CODEC_ID_RV10,   MKTAG('R','V','1','0') },
54
 
    { CODEC_ID_RV20,   MKTAG('R','V','2','0') },
55
 
    { CODEC_ID_RV20,   MKTAG('R','V','T','R') },
56
 
    { CODEC_ID_RV30,   MKTAG('R','V','3','0') },
57
 
    { CODEC_ID_RV40,   MKTAG('R','V','4','0') },
58
 
    { CODEC_ID_AC3,    MKTAG('d','n','e','t') },
59
 
    { CODEC_ID_RA_144, MKTAG('l','p','c','J') },
60
 
    { CODEC_ID_RA_288, MKTAG('2','8','_','8') },
61
 
    { CODEC_ID_COOK,   MKTAG('c','o','o','k') },
62
 
    { CODEC_ID_ATRAC3, MKTAG('a','t','r','c') },
63
 
    { CODEC_ID_SIPR,   MKTAG('s','i','p','r') },
64
 
    { CODEC_ID_AAC,    MKTAG('r','a','a','c') },
65
 
    { CODEC_ID_AAC,    MKTAG('r','a','c','p') },
66
 
    { CODEC_ID_NONE },
67
 
};
68
 
 
69
52
static const unsigned char sipr_swaps[38][2] = {
70
53
    {  0, 63 }, {  1, 22 }, {  2, 44 }, {  3, 90 },
71
54
    {  5, 81 }, {  7, 31 }, {  8, 86 }, {  9, 58 },
81
64
 
82
65
const unsigned char ff_sipr_subpk_size[4] = { 29, 19, 37, 20 };
83
66
 
84
 
static inline void get_strl(ByteIOContext *pb, char *buf, int buf_size, int len)
 
67
static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
85
68
{
86
69
    int i;
87
70
    char *q, r;
88
71
 
89
72
    q = buf;
90
73
    for(i=0;i<len;i++) {
91
 
        r = get_byte(pb);
 
74
        r = avio_r8(pb);
92
75
        if (i < buf_size - 1)
93
76
            *q++ = r;
94
77
    }
95
78
    if (buf_size > 0) *q = '\0';
96
79
}
97
80
 
98
 
static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
 
81
static void get_str8(AVIOContext *pb, char *buf, int buf_size)
99
82
{
100
 
    get_strl(pb, buf, buf_size, get_byte(pb));
 
83
    get_strl(pb, buf, buf_size, avio_r8(pb));
101
84
}
102
85
 
103
 
static int rm_read_extradata(ByteIOContext *pb, AVCodecContext *avctx, unsigned size)
 
86
static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
104
87
{
105
88
    if (size >= 1<<24)
106
89
        return -1;
107
90
    avctx->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
108
91
    if (!avctx->extradata)
109
92
        return AVERROR(ENOMEM);
110
 
    avctx->extradata_size = get_buffer(pb, avctx->extradata, size);
 
93
    avctx->extradata_size = avio_read(pb, avctx->extradata, size);
111
94
    memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
112
95
    if (avctx->extradata_size != size)
113
96
        return AVERROR(EIO);
119
102
    char buf[1024];
120
103
    int i;
121
104
    for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
122
 
        int len = wide ? get_be16(s->pb) : get_byte(s->pb);
 
105
        int len = wide ? avio_rb16(s->pb) : avio_r8(s->pb);
123
106
        get_strl(s->pb, buf, sizeof(buf), len);
124
107
        av_metadata_set2(&s->metadata, ff_rm_metadata[i], buf, 0);
125
108
    }
137
120
    av_free_packet(&rms->pkt);
138
121
}
139
122
 
140
 
static int rm_read_audio_stream_info(AVFormatContext *s, ByteIOContext *pb,
 
123
static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
141
124
                                     AVStream *st, RMStream *ast, int read_all)
142
125
{
143
126
    char buf[256];
145
128
    int ret;
146
129
 
147
130
    /* ra type header */
148
 
    version = get_be16(pb); /* version */
 
131
    version = avio_rb16(pb); /* version */
149
132
    if (version == 3) {
150
 
        int header_size = get_be16(pb);
151
 
        int64_t startpos = url_ftell(pb);
152
 
        url_fskip(pb, 14);
 
133
        int header_size = avio_rb16(pb);
 
134
        int64_t startpos = avio_tell(pb);
 
135
        avio_skip(pb, 14);
153
136
        rm_read_metadata(s, 0);
154
 
        if ((startpos + header_size) >= url_ftell(pb) + 2) {
 
137
        if ((startpos + header_size) >= avio_tell(pb) + 2) {
155
138
            // fourcc (should always be "lpcJ")
156
 
            get_byte(pb);
 
139
            avio_r8(pb);
157
140
            get_str8(pb, buf, sizeof(buf));
158
141
        }
159
142
        // Skip extra header crap (this should never happen)
160
 
        if ((startpos + header_size) > url_ftell(pb))
161
 
            url_fskip(pb, header_size + startpos - url_ftell(pb));
 
143
        if ((startpos + header_size) > avio_tell(pb))
 
144
            avio_skip(pb, header_size + startpos - avio_tell(pb));
162
145
        st->codec->sample_rate = 8000;
163
146
        st->codec->channels = 1;
164
147
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
167
150
        int flavor, sub_packet_h, coded_framesize, sub_packet_size;
168
151
        int codecdata_length;
169
152
        /* old version (4) */
170
 
        url_fskip(pb, 2); /* unused */
171
 
        get_be32(pb); /* .ra4 */
172
 
        get_be32(pb); /* data size */
173
 
        get_be16(pb); /* version2 */
174
 
        get_be32(pb); /* header size */
175
 
        flavor= get_be16(pb); /* add codec info / flavor */
176
 
        ast->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
177
 
        get_be32(pb); /* ??? */
178
 
        get_be32(pb); /* ??? */
179
 
        get_be32(pb); /* ??? */
180
 
        ast->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
181
 
        st->codec->block_align= get_be16(pb); /* frame size */
182
 
        ast->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
183
 
        get_be16(pb); /* ??? */
 
153
        avio_skip(pb, 2); /* unused */
 
154
        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); /* ??? */
184
167
        if (version == 5) {
185
 
            get_be16(pb); get_be16(pb); get_be16(pb);
 
168
            avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
186
169
        }
187
 
        st->codec->sample_rate = get_be16(pb);
188
 
        get_be32(pb);
189
 
        st->codec->channels = get_be16(pb);
 
170
        st->codec->sample_rate = avio_rb16(pb);
 
171
        avio_rb32(pb);
 
172
        st->codec->channels = avio_rb16(pb);
190
173
        if (version == 5) {
191
 
            get_be32(pb);
192
 
            get_buffer(pb, buf, 4);
 
174
            avio_rb32(pb);
 
175
            avio_read(pb, buf, 4);
193
176
            buf[4] = 0;
194
177
        } else {
195
178
            get_str8(pb, buf, sizeof(buf)); /* desc */
197
180
        }
198
181
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
199
182
        st->codec->codec_tag  = AV_RL32(buf);
200
 
        st->codec->codec_id   = ff_codec_get_id(rm_codec_tags, st->codec->codec_tag);
 
183
        st->codec->codec_id   = ff_codec_get_id(ff_rm_codec_tags,
 
184
                                                st->codec->codec_tag);
201
185
        switch (st->codec->codec_id) {
202
186
        case CODEC_ID_AC3:
203
187
            st->need_parsing = AVSTREAM_PARSE_FULL;
217
201
        case CODEC_ID_COOK:
218
202
        case CODEC_ID_ATRAC3:
219
203
        case CODEC_ID_SIPR:
220
 
            get_be16(pb); get_byte(pb);
 
204
            avio_rb16(pb); avio_r8(pb);
221
205
            if (version == 5)
222
 
                get_byte(pb);
223
 
            codecdata_length = get_be32(pb);
 
206
                avio_r8(pb);
 
207
            codecdata_length = avio_rb32(pb);
224
208
            if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
225
209
                av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
226
210
                return -1;
227
211
            }
228
212
 
229
 
            if (!strcmp(buf, "cook")) st->codec->codec_id = CODEC_ID_COOK;
230
 
            else if (!strcmp(buf, "sipr")) st->codec->codec_id = CODEC_ID_SIPR;
231
 
            else st->codec->codec_id = CODEC_ID_ATRAC3;
232
 
 
233
213
            ast->audio_framesize = st->codec->block_align;
234
214
            if (st->codec->codec_id == CODEC_ID_SIPR) {
235
215
                if (flavor > 3) {
256
236
            av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
257
237
            break;
258
238
        case CODEC_ID_AAC:
259
 
            get_be16(pb); get_byte(pb);
 
239
            avio_rb16(pb); avio_r8(pb);
260
240
            if (version == 5)
261
 
                get_byte(pb);
262
 
            st->codec->codec_id = CODEC_ID_AAC;
263
 
            codecdata_length = get_be32(pb);
 
241
                avio_r8(pb);
 
242
            codecdata_length = avio_rb32(pb);
264
243
            if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
265
244
                av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
266
245
                return -1;
267
246
            }
268
247
            if (codecdata_length >= 1) {
269
 
                get_byte(pb);
 
248
                avio_r8(pb);
270
249
                if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
271
250
                    return ret;
272
251
            }
275
254
            av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
276
255
        }
277
256
        if (read_all) {
278
 
            get_byte(pb);
279
 
            get_byte(pb);
280
 
            get_byte(pb);
 
257
            avio_r8(pb);
 
258
            avio_r8(pb);
 
259
            avio_r8(pb);
281
260
            rm_read_metadata(s, 0);
282
261
        }
283
262
    }
285
264
}
286
265
 
287
266
int
288
 
ff_rm_read_mdpr_codecdata (AVFormatContext *s, ByteIOContext *pb,
 
267
ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
289
268
                           AVStream *st, RMStream *rst, int codec_data_size)
290
269
{
291
270
    unsigned int v;
294
273
    int ret;
295
274
 
296
275
    av_set_pts_info(st, 64, 1, 1000);
297
 
    codec_pos = url_ftell(pb);
298
 
    v = get_be32(pb);
 
276
    codec_pos = avio_tell(pb);
 
277
    v = avio_rb32(pb);
299
278
    if (v == MKTAG(0xfd, 'a', 'r', '.')) {
300
279
        /* ra type header */
301
280
        if (rm_read_audio_stream_info(s, pb, st, rst, 0))
302
281
            return -1;
303
282
    } else {
304
283
        int fps, fps2;
305
 
        if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
 
284
        if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
306
285
        fail1:
307
286
            av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
308
287
            goto skip;
309
288
        }
310
 
        st->codec->codec_tag = get_le32(pb);
311
 
        st->codec->codec_id  = ff_codec_get_id(rm_codec_tags, st->codec->codec_tag);
 
289
        st->codec->codec_tag = avio_rl32(pb);
 
290
        st->codec->codec_id  = ff_codec_get_id(ff_rm_codec_tags,
 
291
                                               st->codec->codec_tag);
312
292
//        av_log(s, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
313
293
        if (st->codec->codec_id == CODEC_ID_NONE)
314
294
            goto fail1;
315
 
        st->codec->width = get_be16(pb);
316
 
        st->codec->height = get_be16(pb);
 
295
        st->codec->width = avio_rb16(pb);
 
296
        st->codec->height = avio_rb16(pb);
317
297
        st->codec->time_base.num= 1;
318
 
        fps= get_be16(pb);
 
298
        fps= avio_rb16(pb);
319
299
        st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
320
 
        get_be32(pb);
321
 
        fps2= get_be16(pb);
322
 
        get_be16(pb);
 
300
        avio_rb32(pb);
 
301
        fps2= avio_rb16(pb);
 
302
        avio_rb16(pb);
323
303
 
324
 
        if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (url_ftell(pb) - codec_pos))) < 0)
 
304
        if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
325
305
            return ret;
326
306
 
327
307
//        av_log(s, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
340
320
 
341
321
skip:
342
322
    /* skip codec info */
343
 
    size = url_ftell(pb) - codec_pos;
344
 
    url_fskip(pb, codec_data_size - size);
 
323
    size = avio_tell(pb) - codec_pos;
 
324
    avio_skip(pb, codec_data_size - size);
345
325
 
346
326
    return 0;
347
327
}
350
330
 * of the INDX chunk, and will bail out if not. */
351
331
static int rm_read_index(AVFormatContext *s)
352
332
{
353
 
    ByteIOContext *pb = s->pb;
 
333
    AVIOContext *pb = s->pb;
354
334
    unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
355
335
    AVStream *st;
356
336
 
357
337
    do {
358
 
        if (get_le32(pb) != MKTAG('I','N','D','X'))
 
338
        if (avio_rl32(pb) != MKTAG('I','N','D','X'))
359
339
            return -1;
360
 
        size     = get_be32(pb);
 
340
        size     = avio_rb32(pb);
361
341
        if (size < 20)
362
342
            return -1;
363
 
        url_fskip(pb, 2);
364
 
        n_pkts   = get_be32(pb);
365
 
        str_id   = get_be16(pb);
366
 
        next_off = get_be32(pb);
 
343
        avio_skip(pb, 2);
 
344
        n_pkts   = avio_rb32(pb);
 
345
        str_id   = avio_rb16(pb);
 
346
        next_off = avio_rb32(pb);
367
347
        for (n = 0; n < s->nb_streams; n++)
368
348
            if (s->streams[n]->id == str_id) {
369
349
                st = s->streams[n];
373
353
            goto skip;
374
354
 
375
355
        for (n = 0; n < n_pkts; n++) {
376
 
            url_fskip(pb, 2);
377
 
            pts = get_be32(pb);
378
 
            pos = get_be32(pb);
379
 
            url_fskip(pb, 4); /* packet no. */
 
356
            avio_skip(pb, 2);
 
357
            pts = avio_rb32(pb);
 
358
            pos = avio_rb32(pb);
 
359
            avio_skip(pb, 4); /* packet no. */
380
360
 
381
361
            av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
382
362
        }
383
363
 
384
364
skip:
385
 
        if (next_off && url_ftell(pb) != next_off &&
386
 
            url_fseek(pb, next_off, SEEK_SET) < 0)
 
365
        if (next_off && avio_tell(pb) != next_off &&
 
366
            avio_seek(pb, next_off, SEEK_SET) < 0)
387
367
            return -1;
388
368
    } while (next_off);
389
369
 
407
387
{
408
388
    RMDemuxContext *rm = s->priv_data;
409
389
    AVStream *st;
410
 
    ByteIOContext *pb = s->pb;
 
390
    AVIOContext *pb = s->pb;
411
391
    unsigned int tag;
412
392
    int tag_size;
413
393
    unsigned int start_time, duration;
415
395
    char buf[128];
416
396
    int flags = 0;
417
397
 
418
 
    tag = get_le32(pb);
 
398
    tag = avio_rl32(pb);
419
399
    if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
420
400
        /* very old .ra format */
421
401
        return rm_read_header_old(s, ap);
423
403
        return AVERROR(EIO);
424
404
    }
425
405
 
426
 
    get_be32(pb); /* header size */
427
 
    get_be16(pb);
428
 
    get_be32(pb);
429
 
    get_be32(pb); /* number of headers */
 
406
    avio_rb32(pb); /* header size */
 
407
    avio_rb16(pb);
 
408
    avio_rb32(pb);
 
409
    avio_rb32(pb); /* number of headers */
430
410
 
431
411
    for(;;) {
432
 
        if (url_feof(pb))
 
412
        if (pb->eof_reached)
433
413
            return -1;
434
 
        tag = get_le32(pb);
435
 
        tag_size = get_be32(pb);
436
 
        get_be16(pb);
 
414
        tag = avio_rl32(pb);
 
415
        tag_size = avio_rb32(pb);
 
416
        avio_rb16(pb);
437
417
#if 0
438
418
        printf("tag=%c%c%c%c (%08x) size=%d\n",
439
419
               (tag) & 0xff,
448
428
        switch(tag) {
449
429
        case MKTAG('P', 'R', 'O', 'P'):
450
430
            /* file header */
451
 
            get_be32(pb); /* max bit rate */
452
 
            get_be32(pb); /* avg bit rate */
453
 
            get_be32(pb); /* max packet size */
454
 
            get_be32(pb); /* avg packet size */
455
 
            get_be32(pb); /* nb packets */
456
 
            get_be32(pb); /* duration */
457
 
            get_be32(pb); /* preroll */
458
 
            indx_off = get_be32(pb); /* index offset */
459
 
            data_off = get_be32(pb); /* data offset */
460
 
            get_be16(pb); /* nb streams */
461
 
            flags = get_be16(pb); /* flags */
 
431
            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 */
462
442
            break;
463
443
        case MKTAG('C', 'O', 'N', 'T'):
464
444
            rm_read_metadata(s, 1);
467
447
            st = av_new_stream(s, 0);
468
448
            if (!st)
469
449
                return AVERROR(ENOMEM);
470
 
            st->id = get_be16(pb);
471
 
            get_be32(pb); /* max bit rate */
472
 
            st->codec->bit_rate = get_be32(pb); /* bit rate */
473
 
            get_be32(pb); /* max packet size */
474
 
            get_be32(pb); /* avg packet size */
475
 
            start_time = get_be32(pb); /* start time */
476
 
            get_be32(pb); /* preroll */
477
 
            duration = get_be32(pb); /* duration */
 
450
            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 */
478
458
            st->start_time = start_time;
479
459
            st->duration = duration;
480
460
            get_str8(pb, buf, sizeof(buf)); /* desc */
482
462
            st->codec->codec_type = AVMEDIA_TYPE_DATA;
483
463
            st->priv_data = ff_rm_alloc_rmstream();
484
464
            if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
485
 
                                          get_be32(pb)) < 0)
 
465
                                          avio_rb32(pb)) < 0)
486
466
                return -1;
487
467
            break;
488
468
        case MKTAG('D', 'A', 'T', 'A'):
489
469
            goto header_end;
490
470
        default:
491
471
            /* unknown tag: skip it */
492
 
            url_fskip(pb, tag_size - 10);
 
472
            avio_skip(pb, tag_size - 10);
493
473
            break;
494
474
        }
495
475
    }
496
476
 header_end:
497
 
    rm->nb_packets = get_be32(pb); /* number of packets */
 
477
    rm->nb_packets = avio_rb32(pb); /* number of packets */
498
478
    if (!rm->nb_packets && (flags & 4))
499
479
        rm->nb_packets = 3600 * 25;
500
 
    get_be32(pb); /* next data header */
 
480
    avio_rb32(pb); /* next data header */
501
481
 
502
482
    if (!data_off)
503
 
        data_off = url_ftell(pb) - 18;
504
 
    if (indx_off && url_fseek(pb, indx_off, SEEK_SET) >= 0) {
 
483
        data_off = avio_tell(pb) - 18;
 
484
    if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
 
485
        avio_seek(pb, indx_off, SEEK_SET) >= 0) {
505
486
        rm_read_index(s);
506
 
        url_fseek(pb, data_off + 18, SEEK_SET);
 
487
        avio_seek(pb, data_off + 18, SEEK_SET);
507
488
    }
508
489
 
509
490
    return 0;
510
491
}
511
492
 
512
 
static int get_num(ByteIOContext *pb, int *len)
 
493
static int get_num(AVIOContext *pb, int *len)
513
494
{
514
495
    int n, n1;
515
496
 
516
 
    n = get_be16(pb);
 
497
    n = avio_rb16(pb);
517
498
    (*len)-=2;
518
499
    n &= 0x7FFF;
519
500
    if (n >= 0x4000) {
520
501
        return n - 0x4000;
521
502
    } else {
522
 
        n1 = get_be16(pb);
 
503
        n1 = avio_rb16(pb);
523
504
        (*len)-=2;
524
505
        return (n << 16) | n1;
525
506
    }
530
511
 
531
512
static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
532
513
    RMDemuxContext *rm = s->priv_data;
533
 
    ByteIOContext *pb = s->pb;
 
514
    AVIOContext *pb = s->pb;
534
515
    AVStream *st;
535
516
    uint32_t state=0xFFFFFFFF;
536
517
 
537
 
    while(!url_feof(pb)){
 
518
    while(!pb->eof_reached){
538
519
        int len, num, i;
539
 
        *pos= url_ftell(pb) - 3;
 
520
        *pos= avio_tell(pb) - 3;
540
521
        if(rm->remaining_len > 0){
541
522
            num= rm->current_stream;
542
523
            len= rm->remaining_len;
543
524
            *timestamp = AV_NOPTS_VALUE;
544
525
            *flags= 0;
545
526
        }else{
546
 
            state= (state<<8) + get_byte(pb);
 
527
            state= (state<<8) + avio_r8(pb);
547
528
 
548
529
            if(state == MKBETAG('I', 'N', 'D', 'X')){
549
530
                int n_pkts, expected_len;
550
 
                len = get_be32(pb);
551
 
                url_fskip(pb, 2);
552
 
                n_pkts = get_be32(pb);
 
531
                len = avio_rb32(pb);
 
532
                avio_skip(pb, 2);
 
533
                n_pkts = avio_rb32(pb);
553
534
                expected_len = 20 + n_pkts * 14;
554
535
                if (len == 20)
555
536
                    /* some files don't add index entries to chunk size... */
572
553
            len=state - 12;
573
554
            state= 0xFFFFFFFF;
574
555
 
575
 
            num = get_be16(pb);
576
 
            *timestamp = get_be32(pb);
577
 
            get_byte(pb); /* reserved */
578
 
            *flags = get_byte(pb); /* flags */
 
556
            num = avio_rb16(pb);
 
557
            *timestamp = avio_rb32(pb);
 
558
            avio_r8(pb); /* reserved */
 
559
            *flags = avio_r8(pb); /* flags */
579
560
        }
580
561
        for(i=0;i<s->nb_streams;i++) {
581
562
            st = s->streams[i];
585
566
        if (i == s->nb_streams) {
586
567
skip:
587
568
            /* skip packet if unknown number */
588
 
            url_fskip(pb, len);
 
569
            avio_skip(pb, len);
589
570
            rm->remaining_len = 0;
590
571
            continue;
591
572
        }
596
577
    return -1;
597
578
}
598
579
 
599
 
static int rm_assemble_video_frame(AVFormatContext *s, ByteIOContext *pb,
 
580
static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
600
581
                                   RMDemuxContext *rm, RMStream *vst,
601
582
                                   AVPacket *pkt, int len, int *pseq)
602
583
{
603
584
    int hdr, seq, pic_num, len2, pos;
604
585
    int type;
605
586
 
606
 
    hdr = get_byte(pb); len--;
 
587
    hdr = avio_r8(pb); len--;
607
588
    type = hdr >> 6;
608
589
 
609
590
    if(type != 3){  // not frame as a part of packet
610
 
        seq = get_byte(pb); len--;
 
591
        seq = avio_r8(pb); len--;
611
592
    }
612
593
    if(type != 1){  // not whole frame
613
594
        len2 = get_num(pb, &len);
614
595
        pos  = get_num(pb, &len);
615
 
        pic_num = get_byte(pb); len--;
 
596
        pic_num = avio_r8(pb); len--;
616
597
    }
617
598
    if(len<0)
618
599
        return -1;
628
609
        pkt->data[0] = 0;
629
610
        AV_WL32(pkt->data + 1, 1);
630
611
        AV_WL32(pkt->data + 5, 0);
631
 
        get_buffer(pb, pkt->data + 9, len);
 
612
        avio_read(pb, pkt->data + 9, len);
632
613
        return 0;
633
614
    }
634
615
    //now we have to deal with single slice
643
624
        vst->videobufpos = 8*vst->slices + 1;
644
625
        vst->cur_slice = 0;
645
626
        vst->curpic_num = pic_num;
646
 
        vst->pktpos = url_ftell(pb);
 
627
        vst->pktpos = avio_tell(pb);
647
628
    }
648
629
    if(type == 2)
649
630
        len = FFMIN(len, pos);
654
635
    AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
655
636
    if(vst->videobufpos + len > vst->videobufsize)
656
637
        return 1;
657
 
    if (get_buffer(pb, vst->pkt.data + vst->videobufpos, len) != len)
 
638
    if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
658
639
        return AVERROR(EIO);
659
640
    vst->videobufpos += len;
660
641
    rm->remaining_len-= len;
719
700
}
720
701
 
721
702
int
722
 
ff_rm_parse_packet (AVFormatContext *s, ByteIOContext *pb,
 
703
ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
723
704
                    AVStream *st, RMStream *ast, int len, AVPacket *pkt,
724
705
                    int *seq, int flags, int64_t timestamp)
725
706
{
749
730
            switch(st->codec->codec_id) {
750
731
                case CODEC_ID_RA_288:
751
732
                    for (x = 0; x < h/2; x++)
752
 
                        get_buffer(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
 
733
                        avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
753
734
                    break;
754
735
                case CODEC_ID_ATRAC3:
755
736
                case CODEC_ID_COOK:
756
737
                    for (x = 0; x < w/sps; x++)
757
 
                        get_buffer(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
 
738
                        avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
758
739
                    break;
759
740
                case CODEC_ID_SIPR:
760
 
                    get_buffer(pb, ast->pkt.data + y * w, w);
 
741
                    avio_read(pb, ast->pkt.data + y * w, w);
761
742
                    break;
762
743
            }
763
744
 
772
753
        } else if (st->codec->codec_id == CODEC_ID_AAC) {
773
754
            int x;
774
755
            rm->audio_stream_num = st->index;
775
 
            ast->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
 
756
            ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
776
757
            if (ast->sub_packet_cnt) {
777
758
                for (x = 0; x < ast->sub_packet_cnt; x++)
778
 
                    ast->sub_packet_lengths[x] = get_be16(pb);
 
759
                    ast->sub_packet_lengths[x] = avio_rb16(pb);
779
760
                rm->audio_pkt_cnt = ast->sub_packet_cnt;
780
761
                ast->audiotimestamp = timestamp;
781
762
            } else
810
791
}
811
792
 
812
793
int
813
 
ff_rm_retrieve_cache (AVFormatContext *s, ByteIOContext *pb,
 
794
ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
814
795
                      AVStream *st, RMStream *ast, AVPacket *pkt)
815
796
{
816
797
    RMDemuxContext *rm = s->priv_data;
860
841
                len = !ast->audio_framesize ? RAW_PACKET_SIZE :
861
842
                    ast->coded_framesize * ast->sub_packet_h / 2;
862
843
                flags = (seq++ == 1) ? 2 : 0;
863
 
                pos = url_ftell(s->pb);
 
844
                pos = avio_tell(s->pb);
864
845
            } else {
865
846
                len=sync(s, &timestamp, &flags, &i, &pos);
866
847
                if (len > 0)
867
848
                    st = s->streams[i];
868
849
            }
869
850
 
870
 
            if(len<0 || url_feof(s->pb))
 
851
            if(len<0 || s->pb->eof_reached)
871
852
                return AVERROR(EIO);
872
853
 
873
854
            res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
923
904
    if(rm->old_format)
924
905
        return AV_NOPTS_VALUE;
925
906
 
926
 
    url_fseek(s->pb, pos, SEEK_SET);
 
907
    avio_seek(s->pb, pos, SEEK_SET);
927
908
    rm->remaining_len=0;
928
909
    for(;;){
929
910
        int seq=1;
935
916
 
936
917
        st = s->streams[stream_index2];
937
918
        if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
938
 
            h= get_byte(s->pb); len--;
 
919
            h= avio_r8(s->pb); len--;
939
920
            if(!(h & 0x40)){
940
 
                seq = get_byte(s->pb); len--;
 
921
                seq = avio_r8(s->pb); len--;
941
922
            }
942
923
        }
943
924
 
948
929
                break;
949
930
        }
950
931
 
951
 
        url_fskip(s->pb, len);
 
932
        avio_skip(s->pb, len);
952
933
    }
953
934
    *ppos = pos;
954
935
    return dts;
955
936
}
956
937
 
957
 
AVInputFormat rm_demuxer = {
 
938
AVInputFormat ff_rm_demuxer = {
958
939
    "rm",
959
940
    NULL_IF_CONFIG_SMALL("RealMedia format"),
960
941
    sizeof(RMDemuxContext),
966
947
    rm_read_dts,
967
948
};
968
949
 
969
 
AVInputFormat rdt_demuxer = {
 
950
AVInputFormat ff_rdt_demuxer = {
970
951
    "rdt",
971
952
    NULL_IF_CONFIG_SMALL("RDT demuxer"),
972
953
    sizeof(RMDemuxContext),