2 * "Real" compatible demuxer.
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avstring.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/dict.h"
31 #define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/Atrac
32 #define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
33 #define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
34 #define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
35 #define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
36 #define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
39 AVPacket pkt
; ///< place to store merged video frame / reordered audio data
40 int videobufsize
; ///< current assembled frame size
41 int videobufpos
; ///< position for the next slice in the video buffer
42 int curpic_num
; ///< picture number of current frame
43 int cur_slice
, slices
;
44 int64_t pktpos
; ///< first slice position in file
45 /// Audio descrambling matrix parameters
46 int64_t audiotimestamp
; ///< Audio packet timestamp
47 int sub_packet_cnt
; // Subpacket counter, used while reading
48 int sub_packet_size
, sub_packet_h
, coded_framesize
; ///< Descrambling parameters from container
49 int audio_framesize
; /// Audio frame size from container
50 int sub_packet_lengths
[16]; /// Length of each subpacket
51 int32_t deint_id
; ///< deinterleaver used in audio stream
59 int audio_stream_num
; ///< Stream number for audio packets
60 int audio_pkt_cnt
; ///< Output packet counter
63 static inline void get_strl(AVIOContext
*pb
, char *buf
, int buf_size
, int len
)
74 if (buf_size
> 0) *q
= '\0';
77 static void get_str8(AVIOContext
*pb
, char *buf
, int buf_size
)
79 get_strl(pb
, buf
, buf_size
, avio_r8(pb
));
82 static int rm_read_extradata(AVIOContext
*pb
, AVCodecContext
*avctx
, unsigned size
)
86 avctx
->extradata
= av_malloc(size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
87 if (!avctx
->extradata
)
88 return AVERROR(ENOMEM
);
89 avctx
->extradata_size
= avio_read(pb
, avctx
->extradata
, size
);
90 memset(avctx
->extradata
+ avctx
->extradata_size
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
91 if (avctx
->extradata_size
!= size
)
96 static void rm_read_metadata(AVFormatContext
*s
, int wide
)
100 for (i
=0; i
<FF_ARRAY_ELEMS(ff_rm_metadata
); i
++) {
101 int len
= wide
? avio_rb16(s
->pb
) : avio_r8(s
->pb
);
102 get_strl(s
->pb
, buf
, sizeof(buf
), len
);
103 av_dict_set(&s
->metadata
, ff_rm_metadata
[i
], buf
, 0);
107 RMStream
*ff_rm_alloc_rmstream (void)
109 RMStream
*rms
= av_mallocz(sizeof(RMStream
));
110 rms
->curpic_num
= -1;
114 void ff_rm_free_rmstream (RMStream
*rms
)
116 av_free_packet(&rms
->pkt
);
119 static int rm_read_audio_stream_info(AVFormatContext
*s
, AVIOContext
*pb
,
120 AVStream
*st
, RMStream
*ast
, int read_all
)
127 version
= avio_rb16(pb
); /* version */
129 int header_size
= avio_rb16(pb
);
130 int64_t startpos
= avio_tell(pb
);
132 rm_read_metadata(s
, 0);
133 if ((startpos
+ header_size
) >= avio_tell(pb
) + 2) {
134 // fourcc (should always be "lpcJ")
136 get_str8(pb
, buf
, sizeof(buf
));
138 // Skip extra header crap (this should never happen)
139 if ((startpos
+ header_size
) > avio_tell(pb
))
140 avio_skip(pb
, header_size
+ startpos
- avio_tell(pb
));
141 st
->codec
->sample_rate
= 8000;
142 st
->codec
->channels
= 1;
143 st
->codec
->channel_layout
= AV_CH_LAYOUT_MONO
;
144 st
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
145 st
->codec
->codec_id
= AV_CODEC_ID_RA_144
;
146 ast
->deint_id
= DEINT_ID_INT0
;
148 int flavor
, sub_packet_h
, coded_framesize
, sub_packet_size
;
149 int codecdata_length
;
150 /* old version (4) */
151 avio_skip(pb
, 2); /* unused */
152 avio_rb32(pb
); /* .ra4 */
153 avio_rb32(pb
); /* data size */
154 avio_rb16(pb
); /* version2 */
155 avio_rb32(pb
); /* header size */
156 flavor
= avio_rb16(pb
); /* add codec info / flavor */
157 ast
->coded_framesize
= coded_framesize
= avio_rb32(pb
); /* coded frame size */
158 avio_rb32(pb
); /* ??? */
159 avio_rb32(pb
); /* ??? */
160 avio_rb32(pb
); /* ??? */
161 ast
->sub_packet_h
= sub_packet_h
= avio_rb16(pb
); /* 1 */
162 st
->codec
->block_align
= avio_rb16(pb
); /* frame size */
163 ast
->sub_packet_size
= sub_packet_size
= avio_rb16(pb
); /* sub packet size */
164 avio_rb16(pb
); /* ??? */
166 avio_rb16(pb
); avio_rb16(pb
); avio_rb16(pb
);
168 st
->codec
->sample_rate
= avio_rb16(pb
);
170 st
->codec
->channels
= avio_rb16(pb
);
172 ast
->deint_id
= avio_rl32(pb
);
173 avio_read(pb
, buf
, 4);
176 get_str8(pb
, buf
, sizeof(buf
)); /* desc */
177 ast
->deint_id
= AV_RL32(buf
);
178 get_str8(pb
, buf
, sizeof(buf
)); /* desc */
180 st
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
181 st
->codec
->codec_tag
= AV_RL32(buf
);
182 st
->codec
->codec_id
= ff_codec_get_id(ff_rm_codec_tags
,
183 st
->codec
->codec_tag
);
185 switch (st
->codec
->codec_id
) {
186 case AV_CODEC_ID_AC3
:
187 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
189 case AV_CODEC_ID_RA_288
:
190 st
->codec
->extradata_size
= 0;
191 ast
->audio_framesize
= st
->codec
->block_align
;
192 st
->codec
->block_align
= coded_framesize
;
194 case AV_CODEC_ID_COOK
:
195 st
->need_parsing
= AVSTREAM_PARSE_HEADERS
;
196 case AV_CODEC_ID_ATRAC3
:
197 case AV_CODEC_ID_SIPR
:
198 avio_rb16(pb
); avio_r8(pb
);
201 codecdata_length
= avio_rb32(pb
);
202 if(codecdata_length
+ FF_INPUT_BUFFER_PADDING_SIZE
<= (unsigned)codecdata_length
){
203 av_log(s
, AV_LOG_ERROR
, "codecdata_length too large\n");
207 ast
->audio_framesize
= st
->codec
->block_align
;
208 if (st
->codec
->codec_id
== AV_CODEC_ID_SIPR
) {
210 av_log(s
, AV_LOG_ERROR
, "bad SIPR file flavor %d\n",
214 st
->codec
->block_align
= ff_sipr_subpk_size
[flavor
];
216 if(sub_packet_size
<= 0){
217 av_log(s
, AV_LOG_ERROR
, "sub_packet_size is invalid\n");
220 st
->codec
->block_align
= ast
->sub_packet_size
;
222 if ((ret
= rm_read_extradata(pb
, st
->codec
, codecdata_length
)) < 0)
225 case AV_CODEC_ID_AAC
:
226 avio_rb16(pb
); avio_r8(pb
);
229 codecdata_length
= avio_rb32(pb
);
230 if(codecdata_length
+ FF_INPUT_BUFFER_PADDING_SIZE
<= (unsigned)codecdata_length
){
231 av_log(s
, AV_LOG_ERROR
, "codecdata_length too large\n");
234 if (codecdata_length
>= 1) {
236 if ((ret
= rm_read_extradata(pb
, st
->codec
, codecdata_length
- 1)) < 0)
241 av_strlcpy(st
->codec
->codec_name
, buf
, sizeof(st
->codec
->codec_name
));
243 if (ast
->deint_id
== DEINT_ID_INT4
||
244 ast
->deint_id
== DEINT_ID_GENR
||
245 ast
->deint_id
== DEINT_ID_SIPR
) {
246 if (st
->codec
->block_align
<= 0 ||
247 ast
->audio_framesize
* sub_packet_h
> (unsigned)INT_MAX
||
248 ast
->audio_framesize
* sub_packet_h
< st
->codec
->block_align
)
249 return AVERROR_INVALIDDATA
;
250 if (av_new_packet(&ast
->pkt
, ast
->audio_framesize
* sub_packet_h
) < 0)
251 return AVERROR(ENOMEM
);
253 switch (ast
->deint_id
) {
255 if (ast
->coded_framesize
> ast
->audio_framesize
||
257 ast
->coded_framesize
* sub_packet_h
> (2 + (sub_packet_h
& 1)) * ast
->audio_framesize
)
258 return AVERROR_INVALIDDATA
;
261 if (ast
->sub_packet_size
<= 0 ||
262 ast
->sub_packet_size
> ast
->audio_framesize
)
263 return AVERROR_INVALIDDATA
;
271 av_log(NULL
,0,"Unknown interleaver %X\n", ast
->deint_id
);
272 return AVERROR_INVALIDDATA
;
279 rm_read_metadata(s
, 0);
286 ff_rm_read_mdpr_codecdata (AVFormatContext
*s
, AVIOContext
*pb
,
287 AVStream
*st
, RMStream
*rst
, int codec_data_size
)
294 avpriv_set_pts_info(st
, 64, 1, 1000);
295 codec_pos
= avio_tell(pb
);
297 if (v
== MKTAG(0xfd, 'a', 'r', '.')) {
299 if (rm_read_audio_stream_info(s
, pb
, st
, rst
, 0))
301 } else if (v
== MKBETAG('L', 'S', 'D', ':')) {
302 avio_seek(pb
, -4, SEEK_CUR
);
303 if ((ret
= rm_read_extradata(pb
, st
->codec
, codec_data_size
)) < 0)
306 st
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
307 st
->codec
->codec_tag
= AV_RL32(st
->codec
->extradata
);
308 st
->codec
->codec_id
= ff_codec_get_id(ff_rm_codec_tags
,
309 st
->codec
->codec_tag
);
312 if (avio_rl32(pb
) != MKTAG('V', 'I', 'D', 'O')) {
314 av_log(st
->codec
, AV_LOG_ERROR
, "Unsupported video codec\n");
317 st
->codec
->codec_tag
= avio_rl32(pb
);
318 st
->codec
->codec_id
= ff_codec_get_id(ff_rm_codec_tags
,
319 st
->codec
->codec_tag
);
320 av_dlog(s
, "%X %X\n", st
->codec
->codec_tag
, MKTAG('R', 'V', '2', '0'));
321 if (st
->codec
->codec_id
== AV_CODEC_ID_NONE
)
323 st
->codec
->width
= avio_rb16(pb
);
324 st
->codec
->height
= avio_rb16(pb
);
325 avio_skip(pb
, 2); // looks like bits per sample
326 avio_skip(pb
, 4); // always zero?
327 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
328 st
->need_parsing
= AVSTREAM_PARSE_TIMESTAMPS
;
331 if ((ret
= rm_read_extradata(pb
, st
->codec
, codec_data_size
- (avio_tell(pb
) - codec_pos
))) < 0)
334 av_reduce(&st
->avg_frame_rate
.den
, &st
->avg_frame_rate
.num
,
335 0x10000, fps
, (1 << 30) - 1);
336 #if FF_API_R_FRAME_RATE
337 st
->r_frame_rate
= st
->avg_frame_rate
;
342 /* skip codec info */
343 size
= avio_tell(pb
) - codec_pos
;
344 avio_skip(pb
, codec_data_size
- size
);
349 /** this function assumes that the demuxer has already seeked to the start
350 * of the INDX chunk, and will bail out if not. */
351 static int rm_read_index(AVFormatContext
*s
)
353 AVIOContext
*pb
= s
->pb
;
354 unsigned int size
, n_pkts
, str_id
, next_off
, n
, pos
, pts
;
358 if (avio_rl32(pb
) != MKTAG('I','N','D','X'))
360 size
= avio_rb32(pb
);
364 n_pkts
= avio_rb32(pb
);
365 str_id
= avio_rb16(pb
);
366 next_off
= avio_rb32(pb
);
367 for (n
= 0; n
< s
->nb_streams
; n
++)
368 if (s
->streams
[n
]->id
== str_id
) {
372 if (n
== s
->nb_streams
) {
373 av_log(s
, AV_LOG_ERROR
,
374 "Invalid stream index %d for index at pos %"PRId64
"\n",
375 str_id
, avio_tell(pb
));
377 } else if ((avio_size(pb
) - avio_tell(pb
)) / 14 < n_pkts
) {
378 av_log(s
, AV_LOG_ERROR
,
379 "Nr. of packets in packet index for stream index %d "
380 "exceeds filesize (%"PRId64
" at %"PRId64
" = %"PRId64
")\n",
381 str_id
, avio_size(pb
), avio_tell(pb
),
382 (avio_size(pb
) - avio_tell(pb
)) / 14);
386 for (n
= 0; n
< n_pkts
; n
++) {
390 avio_skip(pb
, 4); /* packet no. */
392 av_add_index_entry(st
, pos
, pts
, 0, 0, AVINDEX_KEYFRAME
);
396 if (next_off
&& avio_tell(pb
) < next_off
&&
397 avio_seek(pb
, next_off
, SEEK_SET
) < 0) {
398 av_log(s
, AV_LOG_ERROR
,
399 "Non-linear index detected, not supported\n");
407 static int rm_read_header_old(AVFormatContext
*s
)
409 RMDemuxContext
*rm
= s
->priv_data
;
413 st
= avformat_new_stream(s
, NULL
);
416 st
->priv_data
= ff_rm_alloc_rmstream();
417 return rm_read_audio_stream_info(s
, s
->pb
, st
, st
->priv_data
, 1);
420 static int rm_read_header(AVFormatContext
*s
)
422 RMDemuxContext
*rm
= s
->priv_data
;
424 AVIOContext
*pb
= s
->pb
;
427 unsigned int start_time
, duration
;
428 unsigned int data_off
= 0, indx_off
= 0;
433 if (tag
== MKTAG('.', 'r', 'a', 0xfd)) {
434 /* very old .ra format */
435 return rm_read_header_old(s
);
436 } else if (tag
!= MKTAG('.', 'R', 'M', 'F')) {
440 tag_size
= avio_rb32(pb
);
441 avio_skip(pb
, tag_size
- 8);
447 tag_size
= avio_rb32(pb
);
449 av_dlog(s
, "tag=%c%c%c%c (%08x) size=%d\n",
456 if (tag_size
< 10 && tag
!= MKTAG('D', 'A', 'T', 'A'))
459 case MKTAG('P', 'R', 'O', 'P'):
461 avio_rb32(pb
); /* max bit rate */
462 avio_rb32(pb
); /* avg bit rate */
463 avio_rb32(pb
); /* max packet size */
464 avio_rb32(pb
); /* avg packet size */
465 avio_rb32(pb
); /* nb packets */
466 avio_rb32(pb
); /* duration */
467 avio_rb32(pb
); /* preroll */
468 indx_off
= avio_rb32(pb
); /* index offset */
469 data_off
= avio_rb32(pb
); /* data offset */
470 avio_rb16(pb
); /* nb streams */
471 flags
= avio_rb16(pb
); /* flags */
473 case MKTAG('C', 'O', 'N', 'T'):
474 rm_read_metadata(s
, 1);
476 case MKTAG('M', 'D', 'P', 'R'):
477 st
= avformat_new_stream(s
, NULL
);
479 return AVERROR(ENOMEM
);
480 st
->id
= avio_rb16(pb
);
481 avio_rb32(pb
); /* max bit rate */
482 st
->codec
->bit_rate
= avio_rb32(pb
); /* bit rate */
483 avio_rb32(pb
); /* max packet size */
484 avio_rb32(pb
); /* avg packet size */
485 start_time
= avio_rb32(pb
); /* start time */
486 avio_rb32(pb
); /* preroll */
487 duration
= avio_rb32(pb
); /* duration */
488 st
->start_time
= start_time
;
489 st
->duration
= duration
;
490 get_str8(pb
, buf
, sizeof(buf
)); /* desc */
491 get_str8(pb
, buf
, sizeof(buf
)); /* mimetype */
492 st
->codec
->codec_type
= AVMEDIA_TYPE_DATA
;
493 st
->priv_data
= ff_rm_alloc_rmstream();
494 if (ff_rm_read_mdpr_codecdata(s
, s
->pb
, st
, st
->priv_data
,
498 case MKTAG('D', 'A', 'T', 'A'):
501 /* unknown tag: skip it */
502 avio_skip(pb
, tag_size
- 10);
507 rm
->nb_packets
= avio_rb32(pb
); /* number of packets */
508 if (!rm
->nb_packets
&& (flags
& 4))
509 rm
->nb_packets
= 3600 * 25;
510 avio_rb32(pb
); /* next data header */
513 data_off
= avio_tell(pb
) - 18;
514 if (indx_off
&& pb
->seekable
&& !(s
->flags
& AVFMT_FLAG_IGNIDX
) &&
515 avio_seek(pb
, indx_off
, SEEK_SET
) >= 0) {
517 avio_seek(pb
, data_off
+ 18, SEEK_SET
);
523 static int get_num(AVIOContext
*pb
, int *len
)
535 return (n
<< 16) | n1
;
539 /* multiple of 20 bytes for ra144 (ugly) */
540 #define RAW_PACKET_SIZE 1000
542 static int sync(AVFormatContext
*s
, int64_t *timestamp
, int *flags
, int *stream_index
, int64_t *pos
){
543 RMDemuxContext
*rm
= s
->priv_data
;
544 AVIOContext
*pb
= s
->pb
;
546 uint32_t state
=0xFFFFFFFF;
548 while(!pb
->eof_reached
){
550 *pos
= avio_tell(pb
) - 3;
551 if(rm
->remaining_len
> 0){
552 num
= rm
->current_stream
;
553 len
= rm
->remaining_len
;
554 *timestamp
= AV_NOPTS_VALUE
;
557 state
= (state
<<8) + avio_r8(pb
);
559 if(state
== MKBETAG('I', 'N', 'D', 'X')){
560 int n_pkts
, expected_len
;
563 n_pkts
= avio_rb32(pb
);
564 expected_len
= 20 + n_pkts
* 14;
566 /* some files don't add index entries to chunk size... */
568 else if (len
!= expected_len
)
569 av_log(s
, AV_LOG_WARNING
,
570 "Index size %d (%d pkts) is wrong, should be %d.\n",
571 len
, n_pkts
, expected_len
);
572 len
-= 14; // we already read part of the index header
576 } else if (state
== MKBETAG('D','A','T','A')) {
577 av_log(s
, AV_LOG_WARNING
,
578 "DATA tag in middle of chunk, file may be broken.\n");
581 if(state
> (unsigned)0xFFFF || state
<= 12)
587 *timestamp
= avio_rb32(pb
);
588 avio_r8(pb
); /* reserved */
589 *flags
= avio_r8(pb
); /* flags */
591 for(i
=0;i
<s
->nb_streams
;i
++) {
596 if (i
== s
->nb_streams
) {
598 /* skip packet if unknown number */
600 rm
->remaining_len
= 0;
610 static int rm_assemble_video_frame(AVFormatContext
*s
, AVIOContext
*pb
,
611 RMDemuxContext
*rm
, RMStream
*vst
,
612 AVPacket
*pkt
, int len
, int *pseq
,
615 int hdr
, seq
, pic_num
, len2
, pos
;
618 hdr
= avio_r8(pb
); len
--;
621 if(type
!= 3){ // not frame as a part of packet
622 seq
= avio_r8(pb
); len
--;
624 if(type
!= 1){ // not whole frame
625 len2
= get_num(pb
, &len
);
626 pos
= get_num(pb
, &len
);
627 pic_num
= avio_r8(pb
); len
--;
631 rm
->remaining_len
= len
;
632 if(type
&1){ // frame, not slice
633 if(type
== 3){ // frame as a part of packet
637 if(rm
->remaining_len
< len
)
639 rm
->remaining_len
-= len
;
640 if(av_new_packet(pkt
, len
+ 9) < 0)
643 AV_WL32(pkt
->data
+ 1, 1);
644 AV_WL32(pkt
->data
+ 5, 0);
645 avio_read(pb
, pkt
->data
+ 9, len
);
648 //now we have to deal with single slice
651 if((seq
& 0x7F) == 1 || vst
->curpic_num
!= pic_num
){
652 vst
->slices
= ((hdr
& 0x3F) << 1) + 1;
653 vst
->videobufsize
= len2
+ 8*vst
->slices
+ 1;
654 av_free_packet(&vst
->pkt
); //FIXME this should be output.
655 if(av_new_packet(&vst
->pkt
, vst
->videobufsize
) < 0)
656 return AVERROR(ENOMEM
);
657 vst
->videobufpos
= 8*vst
->slices
+ 1;
659 vst
->curpic_num
= pic_num
;
660 vst
->pktpos
= avio_tell(pb
);
663 len
= FFMIN(len
, pos
);
665 if(++vst
->cur_slice
> vst
->slices
)
667 AV_WL32(vst
->pkt
.data
- 7 + 8*vst
->cur_slice
, 1);
668 AV_WL32(vst
->pkt
.data
- 3 + 8*vst
->cur_slice
, vst
->videobufpos
- 8*vst
->slices
- 1);
669 if(vst
->videobufpos
+ len
> vst
->videobufsize
)
671 if (avio_read(pb
, vst
->pkt
.data
+ vst
->videobufpos
, len
) != len
)
673 vst
->videobufpos
+= len
;
674 rm
->remaining_len
-= len
;
676 if (type
== 2 || vst
->videobufpos
== vst
->videobufsize
) {
677 vst
->pkt
.data
[0] = vst
->cur_slice
-1;
682 #if FF_API_DESTRUCT_PACKET
683 vst
->pkt
.destruct
= NULL
;
685 if(vst
->slices
!= vst
->cur_slice
) //FIXME find out how to set slices correct from the begin
686 memmove(pkt
->data
+ 1 + 8*vst
->cur_slice
, pkt
->data
+ 1 + 8*vst
->slices
,
687 vst
->videobufpos
- 1 - 8*vst
->slices
);
688 pkt
->size
= vst
->videobufpos
+ 8*(vst
->cur_slice
- vst
->slices
);
689 pkt
->pts
= AV_NOPTS_VALUE
;
690 pkt
->pos
= vst
->pktpos
;
699 rm_ac3_swap_bytes (AVStream
*st
, AVPacket
*pkt
)
704 if (st
->codec
->codec_id
== AV_CODEC_ID_AC3
) {
706 for (j
=0;j
<pkt
->size
;j
+=2) {
707 FFSWAP(int, ptr
[0], ptr
[1]);
714 ff_rm_parse_packet (AVFormatContext
*s
, AVIOContext
*pb
,
715 AVStream
*st
, RMStream
*ast
, int len
, AVPacket
*pkt
,
716 int *seq
, int flags
, int64_t timestamp
)
718 RMDemuxContext
*rm
= s
->priv_data
;
720 if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
721 rm
->current_stream
= st
->id
;
722 if(rm_assemble_video_frame(s
, pb
, rm
, ast
, pkt
, len
, seq
, ×tamp
))
723 return -1; //got partial frame
724 } else if (st
->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
725 if ((ast
->deint_id
== DEINT_ID_GENR
) ||
726 (ast
->deint_id
== DEINT_ID_INT4
) ||
727 (ast
->deint_id
== DEINT_ID_SIPR
)) {
729 int sps
= ast
->sub_packet_size
;
730 int cfs
= ast
->coded_framesize
;
731 int h
= ast
->sub_packet_h
;
732 int y
= ast
->sub_packet_cnt
;
733 int w
= ast
->audio_framesize
;
736 y
= ast
->sub_packet_cnt
= 0;
738 ast
->audiotimestamp
= timestamp
;
740 switch (ast
->deint_id
) {
742 for (x
= 0; x
< h
/2; x
++)
743 avio_read(pb
, ast
->pkt
.data
+x
*2*w
+y
*cfs
, cfs
);
746 for (x
= 0; x
< w
/sps
; x
++)
747 avio_read(pb
, ast
->pkt
.data
+sps
*(h
*x
+((h
+1)/2)*(y
&1)+(y
>>1)), sps
);
750 avio_read(pb
, ast
->pkt
.data
+ y
* w
, w
);
754 if (++(ast
->sub_packet_cnt
) < h
)
756 if (ast
->deint_id
== DEINT_ID_SIPR
)
757 ff_rm_reorder_sipr_data(ast
->pkt
.data
, h
, w
);
759 ast
->sub_packet_cnt
= 0;
760 rm
->audio_stream_num
= st
->index
;
761 rm
->audio_pkt_cnt
= h
* w
/ st
->codec
->block_align
;
762 } else if ((ast
->deint_id
== DEINT_ID_VBRF
) ||
763 (ast
->deint_id
== DEINT_ID_VBRS
)) {
765 rm
->audio_stream_num
= st
->index
;
766 ast
->sub_packet_cnt
= (avio_rb16(pb
) & 0xf0) >> 4;
767 if (ast
->sub_packet_cnt
) {
768 for (x
= 0; x
< ast
->sub_packet_cnt
; x
++)
769 ast
->sub_packet_lengths
[x
] = avio_rb16(pb
);
770 rm
->audio_pkt_cnt
= ast
->sub_packet_cnt
;
771 ast
->audiotimestamp
= timestamp
;
775 av_get_packet(pb
, pkt
, len
);
776 rm_ac3_swap_bytes(st
, pkt
);
779 av_get_packet(pb
, pkt
, len
);
781 pkt
->stream_index
= st
->index
;
784 if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
785 if(st
->codec
->codec_id
== AV_CODEC_ID_RV20
){
786 int seq
= 128*(pkt
->data
[2]&0x7F) + (pkt
->data
[3]>>1);
787 av_log(s
, AV_LOG_DEBUG
, "%d %"PRId64
" %d\n", *timestamp
, *timestamp
*512LL/25, seq
);
789 seq
|= (timestamp
&~0x3FFF);
790 if(seq
- timestamp
> 0x2000) seq
-= 0x4000;
791 if(seq
- timestamp
< -0x2000) seq
+= 0x4000;
796 pkt
->pts
= timestamp
;
798 pkt
->flags
|= AV_PKT_FLAG_KEY
;
800 return st
->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
? rm
->audio_pkt_cnt
: 0;
804 ff_rm_retrieve_cache (AVFormatContext
*s
, AVIOContext
*pb
,
805 AVStream
*st
, RMStream
*ast
, AVPacket
*pkt
)
807 RMDemuxContext
*rm
= s
->priv_data
;
809 assert (rm
->audio_pkt_cnt
> 0);
811 if (ast
->deint_id
== DEINT_ID_VBRF
||
812 ast
->deint_id
== DEINT_ID_VBRS
)
813 av_get_packet(pb
, pkt
, ast
->sub_packet_lengths
[ast
->sub_packet_cnt
- rm
->audio_pkt_cnt
]);
815 av_new_packet(pkt
, st
->codec
->block_align
);
816 memcpy(pkt
->data
, ast
->pkt
.data
+ st
->codec
->block_align
* //FIXME avoid this
817 (ast
->sub_packet_h
* ast
->audio_framesize
/ st
->codec
->block_align
- rm
->audio_pkt_cnt
),
818 st
->codec
->block_align
);
821 if ((pkt
->pts
= ast
->audiotimestamp
) != AV_NOPTS_VALUE
) {
822 ast
->audiotimestamp
= AV_NOPTS_VALUE
;
823 pkt
->flags
= AV_PKT_FLAG_KEY
;
826 pkt
->stream_index
= st
->index
;
828 return rm
->audio_pkt_cnt
;
831 static int rm_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
833 RMDemuxContext
*rm
= s
->priv_data
;
835 int i
, len
, res
, seq
= 1;
836 int64_t timestamp
, pos
;
840 if (rm
->audio_pkt_cnt
) {
841 // If there are queued audio packet return them first
842 st
= s
->streams
[rm
->audio_stream_num
];
843 ff_rm_retrieve_cache(s
, s
->pb
, st
, st
->priv_data
, pkt
);
846 if (rm
->old_format
) {
851 timestamp
= AV_NOPTS_VALUE
;
852 len
= !ast
->audio_framesize
? RAW_PACKET_SIZE
:
853 ast
->coded_framesize
* ast
->sub_packet_h
/ 2;
854 flags
= (seq
++ == 1) ? 2 : 0;
855 pos
= avio_tell(s
->pb
);
857 len
=sync(s
, ×tamp
, &flags
, &i
, &pos
);
862 if(len
<0 || s
->pb
->eof_reached
)
865 res
= ff_rm_parse_packet (s
, s
->pb
, st
, st
->priv_data
, len
, pkt
,
866 &seq
, flags
, timestamp
);
867 if((flags
&2) && (seq
&0x7F) == 1)
868 av_add_index_entry(st
, pos
, timestamp
, 0, 0, AVINDEX_KEYFRAME
);
873 if( (st
->discard
>= AVDISCARD_NONKEY
&& !(flags
&2))
874 || st
->discard
>= AVDISCARD_ALL
){
883 static int rm_read_close(AVFormatContext
*s
)
887 for (i
=0;i
<s
->nb_streams
;i
++)
888 ff_rm_free_rmstream(s
->streams
[i
]->priv_data
);
893 static int rm_probe(AVProbeData
*p
)
895 /* check file header */
896 if ((p
->buf
[0] == '.' && p
->buf
[1] == 'R' &&
897 p
->buf
[2] == 'M' && p
->buf
[3] == 'F' &&
898 p
->buf
[4] == 0 && p
->buf
[5] == 0) ||
899 (p
->buf
[0] == '.' && p
->buf
[1] == 'r' &&
900 p
->buf
[2] == 'a' && p
->buf
[3] == 0xfd))
901 return AVPROBE_SCORE_MAX
;
906 static int64_t rm_read_dts(AVFormatContext
*s
, int stream_index
,
907 int64_t *ppos
, int64_t pos_limit
)
909 RMDemuxContext
*rm
= s
->priv_data
;
911 int stream_index2
, flags
, len
, h
;
916 return AV_NOPTS_VALUE
;
918 avio_seek(s
->pb
, pos
, SEEK_SET
);
924 len
=sync(s
, &dts
, &flags
, &stream_index2
, &pos
);
926 return AV_NOPTS_VALUE
;
928 st
= s
->streams
[stream_index2
];
929 if (st
->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
930 h
= avio_r8(s
->pb
); len
--;
932 seq
= avio_r8(s
->pb
); len
--;
936 if((flags
&2) && (seq
&0x7F) == 1){
937 av_dlog(s
, "%d %d-%d %"PRId64
" %d\n",
938 flags
, stream_index2
, stream_index
, dts
, seq
);
939 av_add_index_entry(st
, pos
, dts
, 0, 0, AVINDEX_KEYFRAME
);
940 if(stream_index2
== stream_index
)
944 avio_skip(s
->pb
, len
);
950 AVInputFormat ff_rm_demuxer
= {
952 .long_name
= NULL_IF_CONFIG_SMALL("RealMedia"),
953 .priv_data_size
= sizeof(RMDemuxContext
),
954 .read_probe
= rm_probe
,
955 .read_header
= rm_read_header
,
956 .read_packet
= rm_read_packet
,
957 .read_close
= rm_read_close
,
958 .read_timestamp
= rm_read_dts
,
961 AVInputFormat ff_rdt_demuxer
= {
963 .long_name
= NULL_IF_CONFIG_SMALL("RDT demuxer"),
964 .priv_data_size
= sizeof(RMDemuxContext
),
965 .read_close
= rm_read_close
,
966 .flags
= AVFMT_NOFILE
,