2
2
* "Real" compatible demuxer.
3
3
* Copyright (c) 2000, 2001 Fabrice Bellard
5
* This file is part of FFmpeg.
5
* This file is part of Libav.
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.
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.
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
49
49
int audio_pkt_cnt; ///< Output packet counter
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') },
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 },
82
65
const unsigned char ff_sipr_subpk_size[4] = { 29, 19, 37, 20 };
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)
90
73
for(i=0;i<len;i++) {
92
75
if (i < buf_size - 1)
95
78
if (buf_size > 0) *q = '\0';
98
static void get_str8(ByteIOContext *pb, char *buf, int buf_size)
81
static void get_str8(AVIOContext *pb, char *buf, int buf_size)
100
get_strl(pb, buf, buf_size, get_byte(pb));
83
get_strl(pb, buf, buf_size, avio_r8(pb));
103
static int rm_read_extradata(ByteIOContext *pb, AVCodecContext *avctx, unsigned size)
86
static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
105
88
if (size >= 1<<24)
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);
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);
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);
133
int header_size = avio_rb16(pb);
134
int64_t startpos = avio_tell(pb);
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")
157
140
get_str8(pb, buf, sizeof(buf));
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);
187
st->codec->sample_rate = get_be16(pb);
189
st->codec->channels = get_be16(pb);
170
st->codec->sample_rate = avio_rb16(pb);
172
st->codec->channels = avio_rb16(pb);
190
173
if (version == 5) {
192
get_buffer(pb, buf, 4);
175
avio_read(pb, buf, 4);
195
178
get_str8(pb, buf, sizeof(buf)); /* desc */
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)
223
codecdata_length = get_be32(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");
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;
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);
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)
262
st->codec->codec_id = CODEC_ID_AAC;
263
codecdata_length = get_be32(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");
268
247
if (codecdata_length >= 1) {
270
249
if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
296
275
av_set_pts_info(st, 64, 1, 1000);
297
codec_pos = url_ftell(pb);
276
codec_pos = avio_tell(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))
305
if (get_le32(pb) != MKTAG('V', 'I', 'D', 'O')) {
284
if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
307
286
av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
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)
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;
319
299
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
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)
327
307
// av_log(s, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
350
330
* of the INDX chunk, and will bail out if not. */
351
331
static int rm_read_index(AVFormatContext *s)
353
ByteIOContext *pb = s->pb;
333
AVIOContext *pb = s->pb;
354
334
unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
358
if (get_le32(pb) != MKTAG('I','N','D','X'))
338
if (avio_rl32(pb) != MKTAG('I','N','D','X'))
340
size = avio_rb32(pb);
364
n_pkts = get_be32(pb);
365
str_id = get_be16(pb);
366
next_off = get_be32(pb);
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];
375
355
for (n = 0; n < n_pkts; n++) {
379
url_fskip(pb, 4); /* packet no. */
359
avio_skip(pb, 4); /* packet no. */
381
361
av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
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)
388
368
} while (next_off);
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 */
463
443
case MKTAG('C', 'O', 'N', 'T'):
464
444
rm_read_metadata(s, 1);
467
447
st = av_new_stream(s, 0);
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,
488
468
case MKTAG('D', 'A', 'T', 'A'):
491
471
/* unknown tag: skip it */
492
url_fskip(pb, tag_size - 10);
472
avio_skip(pb, tag_size - 10);
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 */
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);
512
static int get_num(ByteIOContext *pb, int *len)
493
static int get_num(AVIOContext *pb, int *len)
519
500
if (n >= 0x4000) {
520
501
return n - 0x4000;
524
505
return (n << 16) | n1;
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;
535
516
uint32_t state=0xFFFFFFFF;
537
while(!url_feof(pb)){
518
while(!pb->eof_reached){
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;
546
state= (state<<8) + get_byte(pb);
527
state= (state<<8) + avio_r8(pb);
548
529
if(state == MKBETAG('I', 'N', 'D', 'X')){
549
530
int n_pkts, expected_len;
552
n_pkts = get_be32(pb);
533
n_pkts = avio_rb32(pb);
553
534
expected_len = 20 + n_pkts * 14;
555
536
/* some files don't add index entries to chunk size... */
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)
603
584
int hdr, seq, pic_num, len2, pos;
606
hdr = get_byte(pb); len--;
587
hdr = avio_r8(pb); len--;
609
590
if(type != 3){ // not frame as a part of packet
610
seq = get_byte(pb); len--;
591
seq = avio_r8(pb); len--;
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--;
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);
634
615
//now we have to deal with single slice
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)
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);
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);
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);
772
753
} else if (st->codec->codec_id == CODEC_ID_AAC) {
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;
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);
865
846
len=sync(s, ×tamp, &flags, &i, &pos);
867
848
st = s->streams[i];
870
if(len<0 || url_feof(s->pb))
851
if(len<0 || s->pb->eof_reached)
871
852
return AVERROR(EIO);
873
854
res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,