Statistics
| Branch: | Revision:

ffmpeg / libavformat / rmdec.c @ f23938f5

History | View | Annotate | Download (26.3 KB)

1
/*
2
 * "Real" compatible demuxer.
3
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
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
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "libavutil/avstring.h"
23
#include "avformat.h"
24
#include "rm.h"
25

    
26
static inline void get_strl(ByteIOContext *pb, char *buf, int buf_size, int len)
27
{
28
    int i;
29
    char *q, r;
30

    
31
    q = buf;
32
    for(i=0;i<len;i++) {
33
        r = get_byte(pb);
34
        if (i < buf_size - 1)
35
            *q++ = r;
36
    }
37
    if (buf_size > 0) *q = '\0';
38
}
39

    
40
static void get_str16(ByteIOContext *pb, char *buf, int buf_size)
41
{
42
    get_strl(pb, buf, buf_size, get_be16(pb));
43
}
44

    
45
static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
46
{
47
    get_strl(pb, buf, buf_size, get_byte(pb));
48
}
49

    
50
static int rm_read_audio_stream_info(AVFormatContext *s, AVStream *st,
51
                                      int read_all)
52
{
53
    RMContext *rm = s->priv_data;
54
    ByteIOContext *pb = s->pb;
55
    char buf[256];
56
    uint32_t version;
57
    int i;
58

    
59
    /* ra type header */
60
    version = get_be32(pb); /* version */
61
    if (((version >> 16) & 0xff) == 3) {
62
        int64_t startpos = url_ftell(pb);
63
        /* very old version */
64
        for(i = 0; i < 14; i++)
65
            get_byte(pb);
66
        get_str8(pb, s->title, sizeof(s->title));
67
        get_str8(pb, s->author, sizeof(s->author));
68
        get_str8(pb, s->copyright, sizeof(s->copyright));
69
        get_str8(pb, s->comment, sizeof(s->comment));
70
        if ((startpos + (version & 0xffff)) >= url_ftell(pb) + 2) {
71
        // fourcc (should always be "lpcJ")
72
        get_byte(pb);
73
        get_str8(pb, buf, sizeof(buf));
74
        }
75
        // Skip extra header crap (this should never happen)
76
        if ((startpos + (version & 0xffff)) > url_ftell(pb))
77
            url_fskip(pb, (version & 0xffff) + startpos - url_ftell(pb));
78
        st->codec->sample_rate = 8000;
79
        st->codec->channels = 1;
80
        st->codec->codec_type = CODEC_TYPE_AUDIO;
81
        st->codec->codec_id = CODEC_ID_RA_144;
82
    } else {
83
        int flavor, sub_packet_h, coded_framesize, sub_packet_size;
84
        /* old version (4) */
85
        get_be32(pb); /* .ra4 */
86
        get_be32(pb); /* data size */
87
        get_be16(pb); /* version2 */
88
        get_be32(pb); /* header size */
89
        flavor= get_be16(pb); /* add codec info / flavor */
90
        rm->coded_framesize = coded_framesize = get_be32(pb); /* coded frame size */
91
        get_be32(pb); /* ??? */
92
        get_be32(pb); /* ??? */
93
        get_be32(pb); /* ??? */
94
        rm->sub_packet_h = sub_packet_h = get_be16(pb); /* 1 */
95
        st->codec->block_align= get_be16(pb); /* frame size */
96
        rm->sub_packet_size = sub_packet_size = get_be16(pb); /* sub packet size */
97
        get_be16(pb); /* ??? */
98
        if (((version >> 16) & 0xff) == 5) {
99
            get_be16(pb); get_be16(pb); get_be16(pb); }
100
        st->codec->sample_rate = get_be16(pb);
101
        get_be32(pb);
102
        st->codec->channels = get_be16(pb);
103
        if (((version >> 16) & 0xff) == 5) {
104
            get_be32(pb);
105
            buf[0] = get_byte(pb);
106
            buf[1] = get_byte(pb);
107
            buf[2] = get_byte(pb);
108
            buf[3] = get_byte(pb);
109
            buf[4] = 0;
110
        } else {
111
            get_str8(pb, buf, sizeof(buf)); /* desc */
112
            get_str8(pb, buf, sizeof(buf)); /* desc */
113
        }
114
        st->codec->codec_type = CODEC_TYPE_AUDIO;
115
        if (!strcmp(buf, "dnet")) {
116
            st->codec->codec_id = CODEC_ID_AC3;
117
            st->need_parsing = AVSTREAM_PARSE_FULL;
118
        } else if (!strcmp(buf, "28_8")) {
119
            st->codec->codec_id = CODEC_ID_RA_288;
120
            st->codec->extradata_size= 0;
121
            rm->audio_framesize = st->codec->block_align;
122
            st->codec->block_align = coded_framesize;
123

    
124
            if(rm->audio_framesize >= UINT_MAX / sub_packet_h){
125
                av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
126
                return -1;
127
            }
128

    
129
            rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
130
        } else if ((!strcmp(buf, "cook")) || (!strcmp(buf, "atrc")) || (!strcmp(buf, "sipr"))) {
131
            int codecdata_length, i;
132
            get_be16(pb); get_byte(pb);
133
            if (((version >> 16) & 0xff) == 5)
134
                get_byte(pb);
135
            codecdata_length = get_be32(pb);
136
            if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
137
                av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
138
                return -1;
139
            }
140

    
141
            if(sub_packet_size <= 0){
142
                av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
143
                return -1;
144
            }
145

    
146
            if (!strcmp(buf, "cook")) st->codec->codec_id = CODEC_ID_COOK;
147
            else if (!strcmp(buf, "sipr")) st->codec->codec_id = CODEC_ID_SIPR;
148
            else st->codec->codec_id = CODEC_ID_ATRAC3;
149
            st->codec->extradata_size= codecdata_length;
150
            st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
151
            for(i = 0; i < codecdata_length; i++)
152
                ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
153
            rm->audio_framesize = st->codec->block_align;
154
            st->codec->block_align = rm->sub_packet_size;
155

    
156
            if(rm->audio_framesize >= UINT_MAX / sub_packet_h){
157
                av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
158
                return -1;
159
            }
160

    
161
            rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
162
        } else if (!strcmp(buf, "raac") || !strcmp(buf, "racp")) {
163
            int codecdata_length, i;
164
            get_be16(pb); get_byte(pb);
165
            if (((version >> 16) & 0xff) == 5)
166
                get_byte(pb);
167
            st->codec->codec_id = CODEC_ID_AAC;
168
            codecdata_length = get_be32(pb);
169
            if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
170
                av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
171
                return -1;
172
            }
173
            if (codecdata_length >= 1) {
174
                st->codec->extradata_size = codecdata_length - 1;
175
                st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
176
                get_byte(pb);
177
                for(i = 0; i < st->codec->extradata_size; i++)
178
                    ((uint8_t*)st->codec->extradata)[i] = get_byte(pb);
179
            }
180
        } else {
181
            st->codec->codec_id = CODEC_ID_NONE;
182
            av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
183
        }
184
        if (read_all) {
185
            get_byte(pb);
186
            get_byte(pb);
187
            get_byte(pb);
188

    
189
            get_str8(pb, s->title, sizeof(s->title));
190
            get_str8(pb, s->author, sizeof(s->author));
191
            get_str8(pb, s->copyright, sizeof(s->copyright));
192
            get_str8(pb, s->comment, sizeof(s->comment));
193
        }
194
    }
195
    return 0;
196
}
197

    
198
int
199
ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVStream *st, int codec_data_size)
200
{
201
    ByteIOContext *pb = s->pb;
202
    unsigned int v;
203
    int size;
204
    int64_t codec_pos;
205

    
206
    av_set_pts_info(st, 64, 1, 1000);
207
    codec_pos = url_ftell(pb);
208
    v = get_be32(pb);
209
    if (v == MKTAG(0xfd, 'a', 'r', '.')) {
210
        /* ra type header */
211
        if (rm_read_audio_stream_info(s, st, 0))
212
            return -1;
213
    } else {
214
        int fps, fps2;
215
        if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
216
        fail1:
217
            av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
218
            goto skip;
219
        }
220
        st->codec->codec_tag = get_le32(pb);
221
//        av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
222
        if (   st->codec->codec_tag != MKTAG('R', 'V', '1', '0')
223
            && st->codec->codec_tag != MKTAG('R', 'V', '2', '0')
224
            && st->codec->codec_tag != MKTAG('R', 'V', '3', '0')
225
            && st->codec->codec_tag != MKTAG('R', 'V', '4', '0')
226
            && st->codec->codec_tag != MKTAG('R', 'V', 'T', 'R'))
227
            goto fail1;
228
        st->codec->width = get_be16(pb);
229
        st->codec->height = get_be16(pb);
230
        st->codec->time_base.num= 1;
231
        fps= get_be16(pb);
232
        st->codec->codec_type = CODEC_TYPE_VIDEO;
233
        get_be32(pb);
234
        fps2= get_be16(pb);
235
        get_be16(pb);
236

    
237
        st->codec->extradata_size= codec_data_size - (url_ftell(pb) - codec_pos);
238

    
239
        if(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)st->codec->extradata_size){
240
            //check is redundant as get_buffer() will catch this
241
            av_log(s, AV_LOG_ERROR, "st->codec->extradata_size too large\n");
242
            return -1;
243
        }
244
        st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
245
        if (!st->codec->extradata)
246
            return AVERROR(ENOMEM);
247
        get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
248

    
249
//        av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
250
        st->codec->time_base.den = fps * st->codec->time_base.num;
251
        switch(((uint8_t*)st->codec->extradata)[4]>>4){
252
        case 1: st->codec->codec_id = CODEC_ID_RV10; break;
253
        case 2: st->codec->codec_id = CODEC_ID_RV20; break;
254
        case 3: st->codec->codec_id = CODEC_ID_RV30; break;
255
        case 4: st->codec->codec_id = CODEC_ID_RV40; break;
256
        default: goto fail1;
257
        }
258
    }
259

    
260
skip:
261
    /* skip codec info */
262
    size = url_ftell(pb) - codec_pos;
263
    url_fskip(pb, codec_data_size - size);
264

    
265
    return 0;
266
}
267

    
268

    
269
static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
270
{
271
    RMContext *rm = s->priv_data;
272
    AVStream *st;
273

    
274
    rm->old_format = 1;
275
    st = av_new_stream(s, 0);
276
    if (!st)
277
        return -1;
278
    return rm_read_audio_stream_info(s, st, 1);
279
}
280

    
281
static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap)
282
{
283
    RMContext *rm = s->priv_data;
284
    AVStream *st;
285
    ByteIOContext *pb = s->pb;
286
    unsigned int tag;
287
    int tag_size;
288
    unsigned int start_time, duration;
289
    char buf[128];
290
    int flags = 0;
291

    
292
    tag = get_le32(pb);
293
    if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
294
        /* very old .ra format */
295
        return rm_read_header_old(s, ap);
296
    } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
297
        return AVERROR(EIO);
298
    }
299

    
300
    get_be32(pb); /* header size */
301
    get_be16(pb);
302
    get_be32(pb);
303
    get_be32(pb); /* number of headers */
304

    
305
    for(;;) {
306
        if (url_feof(pb))
307
            return -1;
308
        tag = get_le32(pb);
309
        tag_size = get_be32(pb);
310
        get_be16(pb);
311
#if 0
312
        printf("tag=%c%c%c%c (%08x) size=%d\n",
313
               (tag) & 0xff,
314
               (tag >> 8) & 0xff,
315
               (tag >> 16) & 0xff,
316
               (tag >> 24) & 0xff,
317
               tag,
318
               tag_size);
319
#endif
320
        if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
321
            return -1;
322
        switch(tag) {
323
        case MKTAG('P', 'R', 'O', 'P'):
324
            /* file header */
325
            get_be32(pb); /* max bit rate */
326
            get_be32(pb); /* avg bit rate */
327
            get_be32(pb); /* max packet size */
328
            get_be32(pb); /* avg packet size */
329
            get_be32(pb); /* nb packets */
330
            get_be32(pb); /* duration */
331
            get_be32(pb); /* preroll */
332
            get_be32(pb); /* index offset */
333
            get_be32(pb); /* data offset */
334
            get_be16(pb); /* nb streams */
335
            flags = get_be16(pb); /* flags */
336
            break;
337
        case MKTAG('C', 'O', 'N', 'T'):
338
            get_str16(pb, s->title, sizeof(s->title));
339
            get_str16(pb, s->author, sizeof(s->author));
340
            get_str16(pb, s->copyright, sizeof(s->copyright));
341
            get_str16(pb, s->comment, sizeof(s->comment));
342
            break;
343
        case MKTAG('M', 'D', 'P', 'R'):
344
            st = av_new_stream(s, 0);
345
            if (!st)
346
                return AVERROR(ENOMEM);
347
            st->id = get_be16(pb);
348
            get_be32(pb); /* max bit rate */
349
            st->codec->bit_rate = get_be32(pb); /* bit rate */
350
            get_be32(pb); /* max packet size */
351
            get_be32(pb); /* avg packet size */
352
            start_time = get_be32(pb); /* start time */
353
            get_be32(pb); /* preroll */
354
            duration = get_be32(pb); /* duration */
355
            st->start_time = start_time;
356
            st->duration = duration;
357
            get_str8(pb, buf, sizeof(buf)); /* desc */
358
            get_str8(pb, buf, sizeof(buf)); /* mimetype */
359
            st->codec->codec_type = CODEC_TYPE_DATA;
360
            if (ff_rm_read_mdpr_codecdata(s, st, get_be32(pb)) < 0)
361
                return -1;
362
            break;
363
        case MKTAG('D', 'A', 'T', 'A'):
364
            goto header_end;
365
        default:
366
            /* unknown tag: skip it */
367
            url_fskip(pb, tag_size - 10);
368
            break;
369
        }
370
    }
371
 header_end:
372
    rm->nb_packets = get_be32(pb); /* number of packets */
373
    if (!rm->nb_packets && (flags & 4))
374
        rm->nb_packets = 3600 * 25;
375
    get_be32(pb); /* next data header */
376
    rm->curpic_num = -1;
377
    return 0;
378
}
379

    
380
static int get_num(ByteIOContext *pb, int *len)
381
{
382
    int n, n1;
383

    
384
    n = get_be16(pb);
385
    (*len)-=2;
386
    n &= 0x7FFF;
387
    if (n >= 0x4000) {
388
        return n - 0x4000;
389
    } else {
390
        n1 = get_be16(pb);
391
        (*len)-=2;
392
        return (n << 16) | n1;
393
    }
394
}
395

    
396
/* multiple of 20 bytes for ra144 (ugly) */
397
#define RAW_PACKET_SIZE 1000
398

    
399
static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
400
    RMContext *rm = s->priv_data;
401
    ByteIOContext *pb = s->pb;
402
    int len, num, res, i;
403
    AVStream *st;
404
    uint32_t state=0xFFFFFFFF;
405

    
406
    while(!url_feof(pb)){
407
        *pos= url_ftell(pb) - 3;
408
        if(rm->remaining_len > 0){
409
            num= rm->current_stream;
410
            len= rm->remaining_len;
411
            *timestamp = AV_NOPTS_VALUE;
412
            *flags= 0;
413
        }else{
414
            state= (state<<8) + get_byte(pb);
415

    
416
            if(state == MKBETAG('I', 'N', 'D', 'X')){
417
                len = get_be16(pb) - 6;
418
                if(len<0)
419
                    continue;
420
                goto skip;
421
            }
422

    
423
            if(state > (unsigned)0xFFFF || state < 12)
424
                continue;
425
            len=state;
426
            state= 0xFFFFFFFF;
427

    
428
            num = get_be16(pb);
429
            *timestamp = get_be32(pb);
430
            res= get_byte(pb); /* reserved */
431
            *flags = get_byte(pb); /* flags */
432

    
433

    
434
            len -= 12;
435
        }
436
        for(i=0;i<s->nb_streams;i++) {
437
            st = s->streams[i];
438
            if (num == st->id)
439
                break;
440
        }
441
        if (i == s->nb_streams) {
442
skip:
443
            /* skip packet if unknown number */
444
            url_fskip(pb, len);
445
            rm->remaining_len -= len;
446
            continue;
447
        }
448
        *stream_index= i;
449

    
450
        return len;
451
    }
452
    return -1;
453
}
454

    
455
static int rm_assemble_video_frame(AVFormatContext *s, RMContext *rm, AVPacket *pkt, int len)
456
{
457
    ByteIOContext *pb = s->pb;
458
    int hdr, seq, pic_num, len2, pos;
459
    int type;
460

    
461
    hdr = get_byte(pb); len--;
462
    type = hdr >> 6;
463
    switch(type){
464
    case 0: // slice
465
    case 2: // last slice
466
        seq = get_byte(pb); len--;
467
        len2 = get_num(pb, &len);
468
        pos = get_num(pb, &len);
469
        pic_num = get_byte(pb); len--;
470
        rm->remaining_len = len;
471
        break;
472
    case 1: //whole frame
473
        seq = get_byte(pb); len--;
474
        if(av_new_packet(pkt, len + 9) < 0)
475
            return AVERROR(EIO);
476
        pkt->data[0] = 0;
477
        AV_WL32(pkt->data + 1, 1);
478
        AV_WL32(pkt->data + 5, 0);
479
        get_buffer(pb, pkt->data + 9, len);
480
        rm->remaining_len = 0;
481
        return 0;
482
    case 3: //frame as a part of packet
483
        len2 = get_num(pb, &len);
484
        pos = get_num(pb, &len);
485
        pic_num = get_byte(pb); len--;
486
        rm->remaining_len = len - len2;
487
        if(av_new_packet(pkt, len2 + 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, len2);
493
        return 0;
494
    }
495
    //now we have to deal with single slice
496

    
497
    if((seq & 0x7F) == 1 || rm->curpic_num != pic_num){
498
        rm->slices = ((hdr & 0x3F) << 1) + 1;
499
        rm->videobufsize = len2 + 8*rm->slices + 1;
500
        av_free(rm->videobuf);
501
        if(!(rm->videobuf = av_malloc(rm->videobufsize)))
502
            return AVERROR(ENOMEM);
503
        rm->videobufpos = 8*rm->slices + 1;
504
        rm->cur_slice = 0;
505
        rm->curpic_num = pic_num;
506
        rm->pktpos = url_ftell(pb);
507
    }
508
    if(type == 2)
509
        len = FFMIN(len, pos);
510

    
511
    if(++rm->cur_slice > rm->slices)
512
        return 1;
513
    AV_WL32(rm->videobuf - 7 + 8*rm->cur_slice, 1);
514
    AV_WL32(rm->videobuf - 3 + 8*rm->cur_slice, rm->videobufpos - 8*rm->slices - 1);
515
    if(rm->videobufpos + len > rm->videobufsize)
516
        return 1;
517
    if (get_buffer(pb, rm->videobuf + rm->videobufpos, len) != len)
518
        return AVERROR(EIO);
519
    rm->videobufpos += len;
520
    rm->remaining_len-= len;
521

    
522
    if(type == 2 || (rm->videobufpos) == rm->videobufsize){
523
         rm->videobuf[0] = rm->cur_slice-1;
524
         if(av_new_packet(pkt, rm->videobufpos - 8*(rm->slices - rm->cur_slice)) < 0)
525
             return AVERROR(ENOMEM);
526
         memcpy(pkt->data, rm->videobuf, 1 + 8*rm->cur_slice);
527
         memcpy(pkt->data + 1 + 8*rm->cur_slice, rm->videobuf + 1 + 8*rm->slices,
528
                rm->videobufpos - 1 - 8*rm->slices);
529
         pkt->pts = AV_NOPTS_VALUE;
530
         pkt->pos = rm->pktpos;
531
         return 0;
532
    }
533

    
534
    return 1;
535
}
536

    
537
static inline void
538
rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
539
{
540
    uint8_t *ptr;
541
    int j;
542

    
543
    if (st->codec->codec_id == CODEC_ID_AC3) {
544
        ptr = pkt->data;
545
        for (j=0;j<pkt->size;j+=2) {
546
            FFSWAP(int, ptr[0], ptr[1]);
547
            ptr += 2;
548
        }
549
    }
550
}
551

    
552
int
553
ff_rm_parse_packet (AVFormatContext *s, AVStream *st, int len, AVPacket *pkt,
554
                    int *seq, int *flags, int64_t *timestamp)
555
{
556
    ByteIOContext *pb = s->pb;
557
    RMContext *rm = s->priv_data;
558

    
559
    if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
560
        rm->current_stream= st->id;
561
        if(rm_assemble_video_frame(s, rm, pkt, len) == 1)
562
            return -1; //got partial frame
563
    } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
564
        if ((st->codec->codec_id == CODEC_ID_RA_288) ||
565
            (st->codec->codec_id == CODEC_ID_COOK) ||
566
            (st->codec->codec_id == CODEC_ID_ATRAC3) ||
567
            (st->codec->codec_id == CODEC_ID_SIPR)) {
568
            int x;
569
            int sps = rm->sub_packet_size;
570
            int cfs = rm->coded_framesize;
571
            int h = rm->sub_packet_h;
572
            int y = rm->sub_packet_cnt;
573
            int w = rm->audio_framesize;
574

    
575
            if (*flags & 2)
576
                y = rm->sub_packet_cnt = 0;
577
            if (!y)
578
                rm->audiotimestamp = *timestamp;
579

    
580
            switch(st->codec->codec_id) {
581
                case CODEC_ID_RA_288:
582
                    for (x = 0; x < h/2; x++)
583
                        get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
584
                    break;
585
                case CODEC_ID_ATRAC3:
586
                case CODEC_ID_COOK:
587
                    for (x = 0; x < w/sps; x++)
588
                        get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
589
                    break;
590
            }
591

    
592
            if (++(rm->sub_packet_cnt) < h)
593
                return -1;
594
            else {
595
                rm->sub_packet_cnt = 0;
596
                rm->audio_stream_num = st->index;
597
                rm->audio_pkt_cnt = h * w / st->codec->block_align - 1;
598
                // Release first audio packet
599
                av_new_packet(pkt, st->codec->block_align);
600
                memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
601
                *timestamp = rm->audiotimestamp;
602
                *flags = 2; // Mark first packet as keyframe
603
            }
604
        } else if (st->codec->codec_id == CODEC_ID_AAC) {
605
            int x;
606
            rm->audio_stream_num = st->index;
607
            rm->sub_packet_cnt = (get_be16(pb) & 0xf0) >> 4;
608
            if (rm->sub_packet_cnt) {
609
                for (x = 0; x < rm->sub_packet_cnt; x++)
610
                    rm->sub_packet_lengths[x] = get_be16(pb);
611
                // Release first audio packet
612
                rm->audio_pkt_cnt = rm->sub_packet_cnt - 1;
613
                av_get_packet(pb, pkt, rm->sub_packet_lengths[0]);
614
                *flags = 2; // Mark first packet as keyframe
615
            }
616
        } else {
617
            av_get_packet(pb, pkt, len);
618
            rm_ac3_swap_bytes(st, pkt);
619
        }
620
    } else
621
        av_get_packet(pb, pkt, len);
622

    
623
    if(  (st->discard >= AVDISCARD_NONKEY && !(*flags&2))
624
       || st->discard >= AVDISCARD_ALL){
625
        av_free_packet(pkt);
626
        return -1;
627
    }
628

    
629
    pkt->stream_index = st->index;
630

    
631
#if 0
632
    if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
633
        if(st->codec->codec_id == CODEC_ID_RV20){
634
            int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
635
            av_log(NULL, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
636

637
            seq |= (*timestamp&~0x3FFF);
638
            if(seq - *timestamp >  0x2000) seq -= 0x4000;
639
            if(seq - *timestamp < -0x2000) seq += 0x4000;
640
        }
641
    }
642
#endif
643

    
644
    pkt->pts= *timestamp;
645
    if (*flags & 2)
646
        pkt->flags |= PKT_FLAG_KEY;
647

    
648
    return 0;
649
}
650

    
651
void
652
ff_rm_retrieve_cache (AVFormatContext *s, AVStream *st, AVPacket *pkt)
653
{
654
    ByteIOContext *pb = s->pb;
655
    RMContext *rm = s->priv_data;
656

    
657
    assert (rm->audio_pkt_cnt > 0);
658

    
659
    if (st->codec->codec_id == CODEC_ID_AAC)
660
        av_get_packet(pb, pkt, rm->sub_packet_lengths[rm->sub_packet_cnt - rm->audio_pkt_cnt]);
661
    else {
662
        av_new_packet(pkt, st->codec->block_align);
663
        memcpy(pkt->data, rm->audiobuf + st->codec->block_align *
664
               (rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
665
               st->codec->block_align);
666
    }
667
    rm->audio_pkt_cnt--;
668
    pkt->flags = 0;
669
    pkt->stream_index = st->index;
670
}
671

    
672
static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
673
{
674
    RMContext *rm = s->priv_data;
675
    ByteIOContext *pb = s->pb;
676
    AVStream *st;
677
    int i, len;
678
    int64_t timestamp, pos;
679
    int flags;
680

    
681
    if (rm->audio_pkt_cnt) {
682
        // If there are queued audio packet return them first
683
        st = s->streams[rm->audio_stream_num];
684
        ff_rm_retrieve_cache(s, st, pkt);
685
    } else if (rm->old_format) {
686
        st = s->streams[0];
687
        if (st->codec->codec_id == CODEC_ID_RA_288) {
688
            int x, y;
689

    
690
            for (y = 0; y < rm->sub_packet_h; y++)
691
                for (x = 0; x < rm->sub_packet_h/2; x++)
692
                    if (get_buffer(pb, rm->audiobuf+x*2*rm->audio_framesize+y*rm->coded_framesize, rm->coded_framesize) <= 0)
693
                        return AVERROR(EIO);
694
            rm->audio_stream_num = 0;
695
            rm->audio_pkt_cnt = rm->sub_packet_h * rm->audio_framesize / st->codec->block_align - 1;
696
            // Release first audio packet
697
            av_new_packet(pkt, st->codec->block_align);
698
            memcpy(pkt->data, rm->audiobuf, st->codec->block_align);
699
            pkt->flags |= PKT_FLAG_KEY; // Mark first packet as keyframe
700
            pkt->stream_index = 0;
701
        } else {
702
            /* just read raw bytes */
703
            len = RAW_PACKET_SIZE;
704
            len= av_get_packet(pb, pkt, len);
705
            pkt->stream_index = 0;
706
            if (len <= 0) {
707
                return AVERROR(EIO);
708
            }
709
            pkt->size = len;
710
        }
711
        rm_ac3_swap_bytes(st, pkt);
712
    } else {
713
        int seq=1;
714
resync:
715
        len=sync(s, &timestamp, &flags, &i, &pos);
716
        if(len<0)
717
            return AVERROR(EIO);
718
        st = s->streams[i];
719

    
720
        if (ff_rm_parse_packet (s, st, len, pkt, &seq, &flags, &timestamp) < 0)
721
            goto resync;
722

    
723
        if((flags&2) && (seq&0x7F) == 1)
724
            av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
725
    }
726

    
727
    return 0;
728
}
729

    
730
static int rm_read_close(AVFormatContext *s)
731
{
732
    RMContext *rm = s->priv_data;
733

    
734
    av_free(rm->audiobuf);
735
    av_free(rm->videobuf);
736
    return 0;
737
}
738

    
739
static int rm_probe(AVProbeData *p)
740
{
741
    /* check file header */
742
    if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
743
         p->buf[2] == 'M' && p->buf[3] == 'F' &&
744
         p->buf[4] == 0 && p->buf[5] == 0) ||
745
        (p->buf[0] == '.' && p->buf[1] == 'r' &&
746
         p->buf[2] == 'a' && p->buf[3] == 0xfd))
747
        return AVPROBE_SCORE_MAX;
748
    else
749
        return 0;
750
}
751

    
752
static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
753
                               int64_t *ppos, int64_t pos_limit)
754
{
755
    RMContext *rm = s->priv_data;
756
    int64_t pos, dts;
757
    int stream_index2, flags, len, h;
758

    
759
    pos = *ppos;
760

    
761
    if(rm->old_format)
762
        return AV_NOPTS_VALUE;
763

    
764
    url_fseek(s->pb, pos, SEEK_SET);
765
    rm->remaining_len=0;
766
    for(;;){
767
        int seq=1;
768
        AVStream *st;
769

    
770
        len=sync(s, &dts, &flags, &stream_index2, &pos);
771
        if(len<0)
772
            return AV_NOPTS_VALUE;
773

    
774
        st = s->streams[stream_index2];
775
        if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
776
            h= get_byte(s->pb); len--;
777
            if(!(h & 0x40)){
778
                seq = get_byte(s->pb); len--;
779
            }
780
        }
781

    
782
        if((flags&2) && (seq&0x7F) == 1){
783
//            av_log(s, AV_LOG_DEBUG, "%d %d-%d %"PRId64" %d\n", flags, stream_index2, stream_index, dts, seq);
784
            av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
785
            if(stream_index2 == stream_index)
786
                break;
787
        }
788

    
789
        url_fskip(s->pb, len);
790
    }
791
    *ppos = pos;
792
    return dts;
793
}
794

    
795
AVInputFormat rm_demuxer = {
796
    "rm",
797
    NULL_IF_CONFIG_SMALL("RM format"),
798
    sizeof(RMContext),
799
    rm_probe,
800
    rm_read_header,
801
    rm_read_packet,
802
    rm_read_close,
803
    NULL,
804
    rm_read_dts,
805
};
806

    
807
AVInputFormat rdt_demuxer = {
808
    "rdt",
809
    NULL_IF_CONFIG_SMALL("RDT demuxer"),
810
    sizeof(RMContext),
811
    NULL, NULL, NULL, rm_read_close, NULL, NULL
812
};