2 * "Real" compatible demuxer.
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/avstring.h"
23 #include "libavutil/intreadwrite.h"
28 AVPacket pkt
; ///< place to store merged video frame / reordered audio data
29 int videobufsize
; ///< current assembled frame size
30 int videobufpos
; ///< position for the next slice in the video buffer
31 int curpic_num
; ///< picture number of current frame
32 int cur_slice
, slices
;
33 int64_t pktpos
; ///< first slice position in file
34 /// Audio descrambling matrix parameters
35 int64_t audiotimestamp
; ///< Audio packet timestamp
36 int sub_packet_cnt
; // Subpacket counter, used while reading
37 int sub_packet_size
, sub_packet_h
, coded_framesize
; ///< Descrambling parameters from container
38 int audio_framesize
; /// Audio frame size from container
39 int sub_packet_lengths
[16]; /// Length of each subpacket
47 int audio_stream_num
; ///< Stream number for audio packets
48 int audio_pkt_cnt
; ///< Output packet counter
51 static inline void get_strl(ByteIOContext
*pb
, char *buf
, int buf_size
, int len
)
62 if (buf_size
> 0) *q
= '\0';
65 static void get_str16(ByteIOContext
*pb
, char *buf
, int buf_size
)
67 get_strl(pb
, buf
, buf_size
, get_be16(pb
));
70 static void get_str8(ByteIOContext
*pb
, char *buf
, int buf_size
)
72 get_strl(pb
, buf
, buf_size
, get_byte(pb
));
75 RMStream
*ff_rm_alloc_rmstream (void)
77 RMStream
*rms
= av_mallocz(sizeof(RMStream
));
82 void ff_rm_free_rmstream (RMStream
*rms
)
84 av_free_packet(&rms
->pkt
);
87 static int rm_read_audio_stream_info(AVFormatContext
*s
, ByteIOContext
*pb
,
88 AVStream
*st
, RMStream
*ast
, int read_all
)
94 version
= get_be32(pb
); /* version */
95 if (((version
>> 16) & 0xff) == 3) {
96 int64_t startpos
= url_ftell(pb
);
98 get_str8(pb
, s
->title
, sizeof(s
->title
));
99 get_str8(pb
, s
->author
, sizeof(s
->author
));
100 get_str8(pb
, s
->copyright
, sizeof(s
->copyright
));
101 get_str8(pb
, s
->comment
, sizeof(s
->comment
));
102 if ((startpos
+ (version
& 0xffff)) >= url_ftell(pb
) + 2) {
103 // fourcc (should always be "lpcJ")
105 get_str8(pb
, buf
, sizeof(buf
));
107 // Skip extra header crap (this should never happen)
108 if ((startpos
+ (version
& 0xffff)) > url_ftell(pb
))
109 url_fskip(pb
, (version
& 0xffff) + startpos
- url_ftell(pb
));
110 st
->codec
->sample_rate
= 8000;
111 st
->codec
->channels
= 1;
112 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
113 st
->codec
->codec_id
= CODEC_ID_RA_144
;
115 int flavor
, sub_packet_h
, coded_framesize
, sub_packet_size
;
116 /* old version (4) */
117 get_be32(pb
); /* .ra4 */
118 get_be32(pb
); /* data size */
119 get_be16(pb
); /* version2 */
120 get_be32(pb
); /* header size */
121 flavor
= get_be16(pb
); /* add codec info / flavor */
122 ast
->coded_framesize
= coded_framesize
= get_be32(pb
); /* coded frame size */
123 get_be32(pb
); /* ??? */
124 get_be32(pb
); /* ??? */
125 get_be32(pb
); /* ??? */
126 ast
->sub_packet_h
= sub_packet_h
= get_be16(pb
); /* 1 */
127 st
->codec
->block_align
= get_be16(pb
); /* frame size */
128 ast
->sub_packet_size
= sub_packet_size
= get_be16(pb
); /* sub packet size */
129 get_be16(pb
); /* ??? */
130 if (((version
>> 16) & 0xff) == 5) {
131 get_be16(pb
); get_be16(pb
); get_be16(pb
);
133 st
->codec
->sample_rate
= get_be16(pb
);
135 st
->codec
->channels
= get_be16(pb
);
136 if (((version
>> 16) & 0xff) == 5) {
138 get_buffer(pb
, buf
, 4);
141 get_str8(pb
, buf
, sizeof(buf
)); /* desc */
142 get_str8(pb
, buf
, sizeof(buf
)); /* desc */
144 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
145 if (!strcmp(buf
, "dnet")) {
146 st
->codec
->codec_id
= CODEC_ID_AC3
;
147 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
148 } else if (!strcmp(buf
, "28_8")) {
149 st
->codec
->codec_id
= CODEC_ID_RA_288
;
150 st
->codec
->extradata_size
= 0;
151 ast
->audio_framesize
= st
->codec
->block_align
;
152 st
->codec
->block_align
= coded_framesize
;
154 if(ast
->audio_framesize
>= UINT_MAX
/ sub_packet_h
){
155 av_log(s
, AV_LOG_ERROR
, "ast->audio_framesize * sub_packet_h too large\n");
159 av_new_packet(&ast
->pkt
, ast
->audio_framesize
* sub_packet_h
);
160 } else if ((!strcmp(buf
, "cook")) || (!strcmp(buf
, "atrc")) || (!strcmp(buf
, "sipr"))) {
161 int codecdata_length
;
162 get_be16(pb
); get_byte(pb
);
163 if (((version
>> 16) & 0xff) == 5)
165 codecdata_length
= get_be32(pb
);
166 if(codecdata_length
+ FF_INPUT_BUFFER_PADDING_SIZE
<= (unsigned)codecdata_length
){
167 av_log(s
, AV_LOG_ERROR
, "codecdata_length too large\n");
171 if(sub_packet_size
<= 0){
172 av_log(s
, AV_LOG_ERROR
, "sub_packet_size is invalid\n");
176 if (!strcmp(buf
, "cook")) st
->codec
->codec_id
= CODEC_ID_COOK
;
177 else if (!strcmp(buf
, "sipr")) st
->codec
->codec_id
= CODEC_ID_SIPR
;
178 else st
->codec
->codec_id
= CODEC_ID_ATRAC3
;
179 st
->codec
->extradata_size
= codecdata_length
;
180 st
->codec
->extradata
= av_mallocz(st
->codec
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
181 get_buffer(pb
, st
->codec
->extradata
, st
->codec
->extradata_size
);
182 ast
->audio_framesize
= st
->codec
->block_align
;
183 st
->codec
->block_align
= ast
->sub_packet_size
;
185 if(ast
->audio_framesize
>= UINT_MAX
/ sub_packet_h
){
186 av_log(s
, AV_LOG_ERROR
, "rm->audio_framesize * sub_packet_h too large\n");
190 av_new_packet(&ast
->pkt
, ast
->audio_framesize
* sub_packet_h
);
191 } else if (!strcmp(buf
, "raac") || !strcmp(buf
, "racp")) {
192 int codecdata_length
;
193 get_be16(pb
); get_byte(pb
);
194 if (((version
>> 16) & 0xff) == 5)
196 st
->codec
->codec_id
= CODEC_ID_AAC
;
197 codecdata_length
= get_be32(pb
);
198 if(codecdata_length
+ FF_INPUT_BUFFER_PADDING_SIZE
<= (unsigned)codecdata_length
){
199 av_log(s
, AV_LOG_ERROR
, "codecdata_length too large\n");
202 if (codecdata_length
>= 1) {
203 st
->codec
->extradata_size
= codecdata_length
- 1;
204 st
->codec
->extradata
= av_mallocz(st
->codec
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
206 get_buffer(pb
, st
->codec
->extradata
, st
->codec
->extradata_size
);
209 st
->codec
->codec_id
= CODEC_ID_NONE
;
210 av_strlcpy(st
->codec
->codec_name
, buf
, sizeof(st
->codec
->codec_name
));
217 get_str8(pb
, s
->title
, sizeof(s
->title
));
218 get_str8(pb
, s
->author
, sizeof(s
->author
));
219 get_str8(pb
, s
->copyright
, sizeof(s
->copyright
));
220 get_str8(pb
, s
->comment
, sizeof(s
->comment
));
227 ff_rm_read_mdpr_codecdata (AVFormatContext
*s
, ByteIOContext
*pb
,
228 AVStream
*st
, RMStream
*rst
, int codec_data_size
)
234 av_set_pts_info(st
, 64, 1, 1000);
235 codec_pos
= url_ftell(pb
);
237 if (v
== MKTAG(0xfd, 'a', 'r', '.')) {
239 if (rm_read_audio_stream_info(s
, pb
, st
, rst
, 0))
243 if (get_le32(pb
) != MKTAG('V', 'I', 'D', 'O')) {
245 av_log(st
->codec
, AV_LOG_ERROR
, "Unsupported video codec\n");
248 st
->codec
->codec_tag
= get_le32(pb
);
249 // av_log(NULL, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
250 if ( st
->codec
->codec_tag
!= MKTAG('R', 'V', '1', '0')
251 && st
->codec
->codec_tag
!= MKTAG('R', 'V', '2', '0')
252 && st
->codec
->codec_tag
!= MKTAG('R', 'V', '3', '0')
253 && st
->codec
->codec_tag
!= MKTAG('R', 'V', '4', '0')
254 && st
->codec
->codec_tag
!= MKTAG('R', 'V', 'T', 'R'))
256 st
->codec
->width
= get_be16(pb
);
257 st
->codec
->height
= get_be16(pb
);
258 st
->codec
->time_base
.num
= 1;
260 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
265 st
->codec
->extradata_size
= codec_data_size
- (url_ftell(pb
) - codec_pos
);
267 if(st
->codec
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
<= (unsigned)st
->codec
->extradata_size
){
268 //check is redundant as get_buffer() will catch this
269 av_log(s
, AV_LOG_ERROR
, "st->codec->extradata_size too large\n");
272 st
->codec
->extradata
= av_mallocz(st
->codec
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
273 if (!st
->codec
->extradata
)
274 return AVERROR(ENOMEM
);
275 get_buffer(pb
, st
->codec
->extradata
, st
->codec
->extradata_size
);
277 // av_log(NULL, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
278 st
->codec
->time_base
.den
= fps
* st
->codec
->time_base
.num
;
279 switch(((uint8_t*)st
->codec
->extradata
)[4]>>4){
280 case 1: st
->codec
->codec_id
= CODEC_ID_RV10
; break;
281 case 2: st
->codec
->codec_id
= CODEC_ID_RV20
; break;
282 case 3: st
->codec
->codec_id
= CODEC_ID_RV30
; break;
283 case 4: st
->codec
->codec_id
= CODEC_ID_RV40
; break;
289 /* skip codec info */
290 size
= url_ftell(pb
) - codec_pos
;
291 url_fskip(pb
, codec_data_size
- size
);
297 static int rm_read_header_old(AVFormatContext
*s
, AVFormatParameters
*ap
)
299 RMDemuxContext
*rm
= s
->priv_data
;
303 st
= av_new_stream(s
, 0);
306 st
->priv_data
= ff_rm_alloc_rmstream();
307 return rm_read_audio_stream_info(s
, s
->pb
, st
, st
->priv_data
, 1);
310 static int rm_read_header(AVFormatContext
*s
, AVFormatParameters
*ap
)
312 RMDemuxContext
*rm
= s
->priv_data
;
314 ByteIOContext
*pb
= s
->pb
;
317 unsigned int start_time
, duration
;
322 if (tag
== MKTAG('.', 'r', 'a', 0xfd)) {
323 /* very old .ra format */
324 return rm_read_header_old(s
, ap
);
325 } else if (tag
!= MKTAG('.', 'R', 'M', 'F')) {
329 get_be32(pb
); /* header size */
332 get_be32(pb
); /* number of headers */
338 tag_size
= get_be32(pb
);
341 printf("tag=%c%c%c%c (%08x) size=%d\n",
349 if (tag_size
< 10 && tag
!= MKTAG('D', 'A', 'T', 'A'))
352 case MKTAG('P', 'R', 'O', 'P'):
354 get_be32(pb
); /* max bit rate */
355 get_be32(pb
); /* avg bit rate */
356 get_be32(pb
); /* max packet size */
357 get_be32(pb
); /* avg packet size */
358 get_be32(pb
); /* nb packets */
359 get_be32(pb
); /* duration */
360 get_be32(pb
); /* preroll */
361 get_be32(pb
); /* index offset */
362 get_be32(pb
); /* data offset */
363 get_be16(pb
); /* nb streams */
364 flags
= get_be16(pb
); /* flags */
366 case MKTAG('C', 'O', 'N', 'T'):
367 get_str16(pb
, s
->title
, sizeof(s
->title
));
368 get_str16(pb
, s
->author
, sizeof(s
->author
));
369 get_str16(pb
, s
->copyright
, sizeof(s
->copyright
));
370 get_str16(pb
, s
->comment
, sizeof(s
->comment
));
372 case MKTAG('M', 'D', 'P', 'R'):
373 st
= av_new_stream(s
, 0);
375 return AVERROR(ENOMEM
);
376 st
->id
= get_be16(pb
);
377 get_be32(pb
); /* max bit rate */
378 st
->codec
->bit_rate
= get_be32(pb
); /* bit rate */
379 get_be32(pb
); /* max packet size */
380 get_be32(pb
); /* avg packet size */
381 start_time
= get_be32(pb
); /* start time */
382 get_be32(pb
); /* preroll */
383 duration
= get_be32(pb
); /* duration */
384 st
->start_time
= start_time
;
385 st
->duration
= duration
;
386 get_str8(pb
, buf
, sizeof(buf
)); /* desc */
387 get_str8(pb
, buf
, sizeof(buf
)); /* mimetype */
388 st
->codec
->codec_type
= CODEC_TYPE_DATA
;
389 st
->priv_data
= ff_rm_alloc_rmstream();
390 if (ff_rm_read_mdpr_codecdata(s
, s
->pb
, st
, st
->priv_data
,
394 case MKTAG('D', 'A', 'T', 'A'):
397 /* unknown tag: skip it */
398 url_fskip(pb
, tag_size
- 10);
403 rm
->nb_packets
= get_be32(pb
); /* number of packets */
404 if (!rm
->nb_packets
&& (flags
& 4))
405 rm
->nb_packets
= 3600 * 25;
406 get_be32(pb
); /* next data header */
410 static int get_num(ByteIOContext
*pb
, int *len
)
422 return (n
<< 16) | n1
;
426 /* multiple of 20 bytes for ra144 (ugly) */
427 #define RAW_PACKET_SIZE 1000
429 static int sync(AVFormatContext
*s
, int64_t *timestamp
, int *flags
, int *stream_index
, int64_t *pos
){
430 RMDemuxContext
*rm
= s
->priv_data
;
431 ByteIOContext
*pb
= s
->pb
;
432 int len
, num
, res
, i
;
434 uint32_t state
=0xFFFFFFFF;
436 while(!url_feof(pb
)){
437 *pos
= url_ftell(pb
) - 3;
438 if(rm
->remaining_len
> 0){
439 num
= rm
->current_stream
;
440 len
= rm
->remaining_len
;
441 *timestamp
= AV_NOPTS_VALUE
;
444 state
= (state
<<8) + get_byte(pb
);
446 if(state
== MKBETAG('I', 'N', 'D', 'X')){
447 len
= get_be16(pb
) - 6;
453 if(state
> (unsigned)0xFFFF || state
< 12)
459 *timestamp
= get_be32(pb
);
460 res
= get_byte(pb
); /* reserved */
461 *flags
= get_byte(pb
); /* flags */
466 for(i
=0;i
<s
->nb_streams
;i
++) {
471 if (i
== s
->nb_streams
) {
473 /* skip packet if unknown number */
475 rm
->remaining_len
-= len
;
485 static int rm_assemble_video_frame(AVFormatContext
*s
, ByteIOContext
*pb
,
486 RMDemuxContext
*rm
, RMStream
*vst
,
487 AVPacket
*pkt
, int len
)
489 int hdr
, seq
, pic_num
, len2
, pos
;
492 hdr
= get_byte(pb
); len
--;
495 if(type
!= 3){ // not frame as a part of packet
496 seq
= get_byte(pb
); len
--;
498 if(type
!= 1){ // not whole frame
499 len2
= get_num(pb
, &len
);
500 pos
= get_num(pb
, &len
);
501 pic_num
= get_byte(pb
); len
--;
505 rm
->remaining_len
= len
;
506 if(type
&1){ // frame, not slice
507 if(type
== 3) // frame as a part of packet
509 if(rm
->remaining_len
< len
)
511 rm
->remaining_len
-= len
;
512 if(av_new_packet(pkt
, len
+ 9) < 0)
515 AV_WL32(pkt
->data
+ 1, 1);
516 AV_WL32(pkt
->data
+ 5, 0);
517 get_buffer(pb
, pkt
->data
+ 9, len
);
520 //now we have to deal with single slice
522 if((seq
& 0x7F) == 1 || vst
->curpic_num
!= pic_num
){
523 vst
->slices
= ((hdr
& 0x3F) << 1) + 1;
524 vst
->videobufsize
= len2
+ 8*vst
->slices
+ 1;
525 av_free_packet(&vst
->pkt
); //FIXME this should be output.
526 if(av_new_packet(&vst
->pkt
, vst
->videobufsize
) < 0)
527 return AVERROR(ENOMEM
);
528 vst
->videobufpos
= 8*vst
->slices
+ 1;
530 vst
->curpic_num
= pic_num
;
531 vst
->pktpos
= url_ftell(pb
);
534 len
= FFMIN(len
, pos
);
536 if(++vst
->cur_slice
> vst
->slices
)
538 AV_WL32(vst
->pkt
.data
- 7 + 8*vst
->cur_slice
, 1);
539 AV_WL32(vst
->pkt
.data
- 3 + 8*vst
->cur_slice
, vst
->videobufpos
- 8*vst
->slices
- 1);
540 if(vst
->videobufpos
+ len
> vst
->videobufsize
)
542 if (get_buffer(pb
, vst
->pkt
.data
+ vst
->videobufpos
, len
) != len
)
544 vst
->videobufpos
+= len
;
545 rm
->remaining_len
-= len
;
547 if(type
== 2 || (vst
->videobufpos
) == vst
->videobufsize
){
548 vst
->pkt
.data
[0] = vst
->cur_slice
-1;
552 if(vst
->slices
!= vst
->cur_slice
) //FIXME find out how to set slices correct from the begin
553 memmove(pkt
->data
+ 1 + 8*vst
->cur_slice
, pkt
->data
+ 1 + 8*vst
->slices
,
554 vst
->videobufpos
- 1 - 8*vst
->slices
);
555 pkt
->size
= vst
->videobufpos
+ 8*(vst
->cur_slice
- vst
->slices
);
556 pkt
->pts
= AV_NOPTS_VALUE
;
557 pkt
->pos
= vst
->pktpos
;
565 rm_ac3_swap_bytes (AVStream
*st
, AVPacket
*pkt
)
570 if (st
->codec
->codec_id
== CODEC_ID_AC3
) {
572 for (j
=0;j
<pkt
->size
;j
+=2) {
573 FFSWAP(int, ptr
[0], ptr
[1]);
580 ff_rm_parse_packet (AVFormatContext
*s
, ByteIOContext
*pb
,
581 AVStream
*st
, RMStream
*ast
, int len
, AVPacket
*pkt
,
582 int *seq
, int *flags
, int64_t *timestamp
)
584 RMDemuxContext
*rm
= s
->priv_data
;
586 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
587 rm
->current_stream
= st
->id
;
588 if(rm_assemble_video_frame(s
, pb
, rm
, ast
, pkt
, len
))
589 return -1; //got partial frame
590 } else if (st
->codec
->codec_type
== CODEC_TYPE_AUDIO
) {
591 if ((st
->codec
->codec_id
== CODEC_ID_RA_288
) ||
592 (st
->codec
->codec_id
== CODEC_ID_COOK
) ||
593 (st
->codec
->codec_id
== CODEC_ID_ATRAC3
) ||
594 (st
->codec
->codec_id
== CODEC_ID_SIPR
)) {
596 int sps
= ast
->sub_packet_size
;
597 int cfs
= ast
->coded_framesize
;
598 int h
= ast
->sub_packet_h
;
599 int y
= ast
->sub_packet_cnt
;
600 int w
= ast
->audio_framesize
;
603 y
= ast
->sub_packet_cnt
= 0;
605 ast
->audiotimestamp
= *timestamp
;
607 switch(st
->codec
->codec_id
) {
608 case CODEC_ID_RA_288
:
609 for (x
= 0; x
< h
/2; x
++)
610 get_buffer(pb
, ast
->pkt
.data
+x
*2*w
+y
*cfs
, cfs
);
612 case CODEC_ID_ATRAC3
:
614 for (x
= 0; x
< w
/sps
; x
++)
615 get_buffer(pb
, ast
->pkt
.data
+sps
*(h
*x
+((h
+1)/2)*(y
&1)+(y
>>1)), sps
);
619 if (++(ast
->sub_packet_cnt
) < h
)
622 ast
->sub_packet_cnt
= 0;
623 rm
->audio_stream_num
= st
->index
;
624 rm
->audio_pkt_cnt
= h
* w
/ st
->codec
->block_align
- 1;
625 // Release first audio packet
626 av_new_packet(pkt
, st
->codec
->block_align
);
627 memcpy(pkt
->data
, ast
->pkt
.data
, st
->codec
->block_align
); //FIXME avoid this
628 *timestamp
= ast
->audiotimestamp
;
629 *flags
= 2; // Mark first packet as keyframe
631 } else if (st
->codec
->codec_id
== CODEC_ID_AAC
) {
633 rm
->audio_stream_num
= st
->index
;
634 ast
->sub_packet_cnt
= (get_be16(pb
) & 0xf0) >> 4;
635 if (ast
->sub_packet_cnt
) {
636 for (x
= 0; x
< ast
->sub_packet_cnt
; x
++)
637 ast
->sub_packet_lengths
[x
] = get_be16(pb
);
638 // Release first audio packet
639 rm
->audio_pkt_cnt
= ast
->sub_packet_cnt
- 1;
640 av_get_packet(pb
, pkt
, ast
->sub_packet_lengths
[0]);
641 *flags
= 2; // Mark first packet as keyframe
644 av_get_packet(pb
, pkt
, len
);
645 rm_ac3_swap_bytes(st
, pkt
);
648 av_get_packet(pb
, pkt
, len
);
650 if( (st
->discard
>= AVDISCARD_NONKEY
&& !(*flags
&2))
651 || st
->discard
>= AVDISCARD_ALL
){
656 pkt
->stream_index
= st
->index
;
659 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
660 if(st
->codec
->codec_id
== CODEC_ID_RV20
){
661 int seq
= 128*(pkt
->data
[2]&0x7F) + (pkt
->data
[3]>>1);
662 av_log(NULL
, AV_LOG_DEBUG
, "%d %"PRId64
" %d\n", *timestamp
, *timestamp
*512LL/25, seq
);
664 seq
|= (*timestamp
&~0x3FFF);
665 if(seq
- *timestamp
> 0x2000) seq
-= 0x4000;
666 if(seq
- *timestamp
< -0x2000) seq
+= 0x4000;
671 pkt
->pts
= *timestamp
;
673 pkt
->flags
|= PKT_FLAG_KEY
;
675 return st
->codec
->codec_type
== CODEC_TYPE_AUDIO
? rm
->audio_pkt_cnt
: 0;
679 ff_rm_retrieve_cache (AVFormatContext
*s
, ByteIOContext
*pb
,
680 AVStream
*st
, RMStream
*ast
, AVPacket
*pkt
)
682 RMDemuxContext
*rm
= s
->priv_data
;
684 assert (rm
->audio_pkt_cnt
> 0);
686 if (st
->codec
->codec_id
== CODEC_ID_AAC
)
687 av_get_packet(pb
, pkt
, ast
->sub_packet_lengths
[ast
->sub_packet_cnt
- rm
->audio_pkt_cnt
]);
689 av_new_packet(pkt
, st
->codec
->block_align
);
690 memcpy(pkt
->data
, ast
->pkt
.data
+ st
->codec
->block_align
* //FIXME avoid this
691 (ast
->sub_packet_h
* ast
->audio_framesize
/ st
->codec
->block_align
- rm
->audio_pkt_cnt
),
692 st
->codec
->block_align
);
696 pkt
->stream_index
= st
->index
;
698 return rm
->audio_pkt_cnt
;
701 static int rm_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
703 RMDemuxContext
*rm
= s
->priv_data
;
704 ByteIOContext
*pb
= s
->pb
;
707 int64_t timestamp
, pos
;
710 if (rm
->audio_pkt_cnt
) {
711 // If there are queued audio packet return them first
712 st
= s
->streams
[rm
->audio_stream_num
];
713 ff_rm_retrieve_cache(s
, s
->pb
, st
, st
->priv_data
, pkt
);
714 } else if (rm
->old_format
) {
719 if (st
->codec
->codec_id
== CODEC_ID_RA_288
) {
722 for (y
= 0; y
< ast
->sub_packet_h
; y
++)
723 for (x
= 0; x
< ast
->sub_packet_h
/2; x
++)
724 if (get_buffer(pb
, ast
->pkt
.data
+x
*2*ast
->audio_framesize
+y
*ast
->coded_framesize
, ast
->coded_framesize
) <= 0)
726 rm
->audio_stream_num
= 0;
727 rm
->audio_pkt_cnt
= ast
->sub_packet_h
* ast
->audio_framesize
/ st
->codec
->block_align
- 1;
728 // Release first audio packet
729 av_new_packet(pkt
, st
->codec
->block_align
);
730 memcpy(pkt
->data
, ast
->pkt
.data
, st
->codec
->block_align
); //FIXME avoid this
731 pkt
->flags
|= PKT_FLAG_KEY
; // Mark first packet as keyframe
732 pkt
->stream_index
= 0;
734 /* just read raw bytes */
735 len
= RAW_PACKET_SIZE
;
736 len
= av_get_packet(pb
, pkt
, len
);
737 pkt
->stream_index
= 0;
743 rm_ac3_swap_bytes(st
, pkt
);
747 len
=sync(s
, ×tamp
, &flags
, &i
, &pos
);
752 if (ff_rm_parse_packet (s
, s
->pb
, st
, st
->priv_data
, len
, pkt
,
753 &seq
, &flags
, ×tamp
) < 0)
756 if((flags
&2) && (seq
&0x7F) == 1)
757 av_add_index_entry(st
, pos
, timestamp
, 0, 0, AVINDEX_KEYFRAME
);
763 static int rm_read_close(AVFormatContext
*s
)
767 for (i
=0;i
<s
->nb_streams
;i
++)
768 ff_rm_free_rmstream(s
->streams
[i
]->priv_data
);
773 static int rm_probe(AVProbeData
*p
)
775 /* check file header */
776 if ((p
->buf
[0] == '.' && p
->buf
[1] == 'R' &&
777 p
->buf
[2] == 'M' && p
->buf
[3] == 'F' &&
778 p
->buf
[4] == 0 && p
->buf
[5] == 0) ||
779 (p
->buf
[0] == '.' && p
->buf
[1] == 'r' &&
780 p
->buf
[2] == 'a' && p
->buf
[3] == 0xfd))
781 return AVPROBE_SCORE_MAX
;
786 static int64_t rm_read_dts(AVFormatContext
*s
, int stream_index
,
787 int64_t *ppos
, int64_t pos_limit
)
789 RMDemuxContext
*rm
= s
->priv_data
;
791 int stream_index2
, flags
, len
, h
;
796 return AV_NOPTS_VALUE
;
798 url_fseek(s
->pb
, pos
, SEEK_SET
);
804 len
=sync(s
, &dts
, &flags
, &stream_index2
, &pos
);
806 return AV_NOPTS_VALUE
;
808 st
= s
->streams
[stream_index2
];
809 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
810 h
= get_byte(s
->pb
); len
--;
812 seq
= get_byte(s
->pb
); len
--;
816 if((flags
&2) && (seq
&0x7F) == 1){
817 // av_log(s, AV_LOG_DEBUG, "%d %d-%d %"PRId64" %d\n", flags, stream_index2, stream_index, dts, seq);
818 av_add_index_entry(st
, pos
, dts
, 0, 0, AVINDEX_KEYFRAME
);
819 if(stream_index2
== stream_index
)
823 url_fskip(s
->pb
, len
);
829 AVInputFormat rm_demuxer
= {
831 NULL_IF_CONFIG_SMALL("RM format"),
832 sizeof(RMDemuxContext
),
841 AVInputFormat rdt_demuxer
= {
843 NULL_IF_CONFIG_SMALL("RDT demuxer"),
844 sizeof(RMDemuxContext
),
845 NULL
, NULL
, NULL
, rm_read_close
, NULL
, NULL