5 (C) 2005, 2006 Roberto Togni
7 Based on FFmpeg's libav/rm.c.
9 Audio codecs: (supported by RealPlayer8 for Linux)
10 DNET - RealAudio 3.0, really it's AC3 in swapped-byteorder
11 SIPR - SiproLab's audio codec, ACELP decoder working with MPlayer,
12 needs fine-tuning too :)
13 ATRC - RealAudio 8 (ATRAC3) - www.minidisc.org/atrac3_article.pdf,
14 ACM decoder uploaded, needs some fine-tuning to work
16 COOK/COKR - Real Cooker -> RealAudio G2
18 Video codecs: (supported by RealPlayer8 for Linux)
19 RV10 - H.263 based, working with libavcodec's decoder
20 RV20-RV40 - using RealPlayer's codec plugins
33 #include "stream/stream.h"
37 //#define mp_dbg(mod,lev, args... ) mp_msg_c((mod<<8)|lev, ## args )
39 #define MKTAG(a, b, c, d) (a | (b << 8) | (c << 16) | (d << 24))
41 #define MAX_STREAMS 32
43 static unsigned char sipr_swaps
[38][2]={
44 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
45 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
46 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
47 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
51 unsigned int timestamp
;
54 // int len; /* only filled by our index generator */
55 // int flags; /* only filled by our index generator */
60 int index_chunk_offset
;
61 real_index_table_t
*index_table
[MAX_STREAMS
];
63 // int *index_table[MAX_STREAMS];
64 int index_table_size
[MAX_STREAMS
];
65 int index_malloc_size
[MAX_STREAMS
];
66 int data_chunk_offset
;
71 int audio_need_keyframe
;
77 // timestamp correction:
78 int64_t kf_base
;// timestamp of the prev. video keyframe
79 unsigned int kf_pts
; // timestamp of next video keyframe
80 unsigned int a_pts
; // previous audio timestamp
81 double v_pts
; // previous video timestamp
82 unsigned long duration
;
86 // int a_streams[MAX_STREAMS];
88 // int v_streams[MAX_STREAMS];
91 * Used to demux multirate files
93 int is_multirate
; ///< != 0 for multirate files
94 int str_data_offset
[MAX_STREAMS
]; ///< Data chunk offset for every audio/video stream
95 int audio_curpos
; ///< Current file position for audio demuxing
96 int video_curpos
; ///< Current file position for video demuxing
97 int a_num_of_packets
; ///< Number of audio packets
98 int v_num_of_packets
; ///< Number of video packets
99 int a_bitrate
; ///< Audio bitrate
100 int v_bitrate
; ///< Video bitrate
101 int stream_switch
; ///< Flag used to switch audio/video demuxing
104 * Used to reorder audio data
106 unsigned int intl_id
[MAX_STREAMS
]; ///< interleaver id, per stream
107 int sub_packet_size
[MAX_STREAMS
]; ///< sub packet size, per stream
108 int sub_packet_h
[MAX_STREAMS
]; ///< number of coded frames per block
109 int coded_framesize
[MAX_STREAMS
]; ///< coded frame size, per stream
110 int audiopk_size
[MAX_STREAMS
]; ///< audio packet size
111 unsigned char *audio_buf
; ///< place to store reordered audio data
112 double *audio_timestamp
; ///< timestamp for each audio packet
113 int sub_packet_cnt
; ///< number of subpacket already received
114 int audio_filepos
; ///< file position of first audio packet in block
117 //! use at most 200 MB of memory for index, corresponds to around 25 million entries
118 #define MAX_INDEX_ENTRIES (200*1024*1024 / sizeof(real_index_table_t))
120 /* originally from FFmpeg */
121 static void get_str(int isbyte
, demuxer_t
*demuxer
, char *buf
, int buf_size
)
126 len
= stream_read_char(demuxer
->stream
);
128 len
= stream_read_word(demuxer
->stream
);
130 stream_read(demuxer
->stream
, buf
, (len
> buf_size
) ? buf_size
: len
);
132 stream_skip(demuxer
->stream
, len
-buf_size
);
134 mp_msg(MSGT_DEMUX
, MSGL_V
, "read_str: %d bytes read\n", len
);
137 static void dump_index(demuxer_t
*demuxer
, int stream_id
)
139 real_priv_t
*priv
= demuxer
->priv
;
140 real_index_table_t
*index
;
143 if (!mp_msg_test(MSGT_DEMUX
,MSGL_V
))
146 if ((unsigned)stream_id
>= MAX_STREAMS
)
149 index
= priv
->index_table
[stream_id
];
150 entries
= priv
->index_table_size
[stream_id
];
152 mp_msg(MSGT_DEMUX
, MSGL_V
, "Index table for stream %d\n", stream_id
);
153 for (i
= 0; i
< entries
; i
++)
156 mp_msg(MSGT_DEMUX
, MSGL_V
,"i: %d, pos: %d, timestamp: %u\n", i
, index
[i
].offset
, index
[i
].timestamp
);
158 mp_msg(MSGT_DEMUX
, MSGL_V
,"packetno: %x pos: %x len: %x timestamp: %x flags: %x\n",
159 index
[i
].packetno
, index
[i
].offset
, index
[i
].len
, index
[i
].timestamp
,
165 static int parse_index_chunk(demuxer_t
*demuxer
)
167 real_priv_t
*priv
= demuxer
->priv
;
168 int origpos
= stream_tell(demuxer
->stream
);
169 int next_header_pos
= priv
->index_chunk_offset
;
170 int i
, entries
, stream_id
;
173 stream_seek(demuxer
->stream
, next_header_pos
);
175 i
= stream_read_dword_le(demuxer
->stream
);
176 if ((i
== -256) || (i
!= MKTAG('I', 'N', 'D', 'X')))
178 mp_msg(MSGT_DEMUX
, MSGL_WARN
,"Something went wrong, no index chunk found on given address (%d)\n",
182 stream_reset(demuxer
->stream
);
183 stream_seek(demuxer
->stream
, origpos
);
188 mp_msg(MSGT_DEMUX
, MSGL_V
,"Reading index table from index chunk (%d)\n",
191 i
= stream_read_dword(demuxer
->stream
);
192 mp_msg(MSGT_DEMUX
, MSGL_V
,"size: %d bytes\n", i
);
194 i
= stream_read_word(demuxer
->stream
);
196 mp_msg(MSGT_DEMUX
, MSGL_WARN
,"Hmm, index table with unknown version (%d), please report it to MPlayer developers!\n", i
);
198 entries
= stream_read_dword(demuxer
->stream
);
199 mp_msg(MSGT_DEMUX
, MSGL_V
,"entries: %d\n", entries
);
201 stream_id
= stream_read_word(demuxer
->stream
);
202 mp_msg(MSGT_DEMUX
, MSGL_V
,"stream_id: %d\n", stream_id
);
204 next_header_pos
= stream_read_dword(demuxer
->stream
);
205 mp_msg(MSGT_DEMUX
, MSGL_V
,"next_header_pos: %d\n", next_header_pos
);
206 if (entries
<= 0 || entries
> MAX_INDEX_ENTRIES
)
214 priv
->index_table_size
[stream_id
] = entries
;
215 priv
->index_table
[stream_id
] = calloc(priv
->index_table_size
[stream_id
], sizeof(real_index_table_t
));
217 for (i
= 0; i
< entries
; i
++)
219 stream_skip(demuxer
->stream
, 2); /* version */
220 priv
->index_table
[stream_id
][i
].timestamp
= stream_read_dword(demuxer
->stream
);
221 priv
->index_table
[stream_id
][i
].offset
= stream_read_dword(demuxer
->stream
);
222 stream_skip(demuxer
->stream
, 4); /* packetno */
223 // priv->index_table[stream_id][i].packetno = stream_read_dword(demuxer->stream);
224 // printf("Index table: Stream#%d: entry: %d: pos: %d\n",
225 // stream_id, i, priv->index_table[stream_id][i].offset);
228 dump_index(demuxer
, stream_id
);
230 if (next_header_pos
> 0)
235 stream_reset(demuxer
->stream
);
236 stream_seek(demuxer
->stream
, origpos
);
245 static void add_index_item(demuxer_t
*demuxer
, int stream_id
, unsigned int timestamp
, int offset
)
247 if ((unsigned)stream_id
< MAX_STREAMS
)
249 real_priv_t
*priv
= demuxer
->priv
;
250 real_index_table_t
*index
;
251 if (priv
->index_table_size
[stream_id
] >= MAX_INDEX_ENTRIES
) {
252 mp_msg(MSGT_DEMUXER
, MSGL_WARN
, "Index too large during building\n");
255 if (priv
->index_table_size
[stream_id
] >= priv
->index_malloc_size
[stream_id
])
257 if (priv
->index_malloc_size
[stream_id
] == 0)
258 priv
->index_malloc_size
[stream_id
] = 2048;
260 priv
->index_malloc_size
[stream_id
] += priv
->index_malloc_size
[stream_id
] / 2;
261 // in case we have a really large chunk...
262 if (priv
->index_table_size
[stream_id
] >=
263 priv
->index_malloc_size
[stream_id
])
264 priv
->index_malloc_size
[stream_id
] =
265 priv
->index_table_size
[stream_id
] + 1;
266 priv
->index_table
[stream_id
] = realloc(priv
->index_table
[stream_id
], priv
->index_malloc_size
[stream_id
]*sizeof(priv
->index_table
[0][0]));
268 if (priv
->index_table_size
[stream_id
] > 0)
270 index
= &priv
->index_table
[stream_id
][priv
->index_table_size
[stream_id
] - 1];
271 if (index
->timestamp
>= timestamp
|| index
->offset
>= offset
)
274 index
= &priv
->index_table
[stream_id
][priv
->index_table_size
[stream_id
]++];
275 index
->timestamp
= timestamp
;
276 index
->offset
= offset
;
280 static void add_index_segment(demuxer_t
*demuxer
, int seek_stream_id
, int64_t seek_timestamp
)
282 int tag
, len
, stream_id
, flags
;
283 unsigned int timestamp
;
284 if (seek_timestamp
!= -1 && (unsigned)seek_stream_id
>= MAX_STREAMS
)
288 demuxer
->filepos
= stream_tell(demuxer
->stream
);
290 tag
= stream_read_dword(demuxer
->stream
);
291 if (tag
== MKTAG('A', 'T', 'A', 'D'))
293 stream_skip(demuxer
->stream
, 14);
294 continue; /* skip to next loop */
297 if (tag
== -256 || len
< 12)
300 stream_id
= stream_read_word(demuxer
->stream
);
301 timestamp
= stream_read_dword(demuxer
->stream
);
303 stream_skip(demuxer
->stream
, 1); /* reserved */
304 flags
= stream_read_char(demuxer
->stream
);
311 add_index_item(demuxer
, stream_id
, timestamp
, demuxer
->filepos
);
312 if (stream_id
== seek_stream_id
&& timestamp
>= seek_timestamp
)
314 stream_seek(demuxer
->stream
, demuxer
->filepos
);
318 // printf("Index: stream=%d packet=%d timestamp=%u len=%d flags=0x%x datapos=0x%x\n", stream_id, entries, timestamp, len, flags, index->offset);
320 stream_skip(demuxer
->stream
, len
-12);
324 static int generate_index(demuxer_t
*demuxer
)
326 real_priv_t
*priv
= demuxer
->priv
;
327 int origpos
= stream_tell(demuxer
->stream
);
328 int data_pos
= priv
->data_chunk_offset
-10;
332 stream_seek(demuxer
->stream
, data_pos
);
333 tag
= stream_read_dword(demuxer
->stream
);
334 if (tag
!= MKTAG('A', 'T', 'A', 'D'))
336 mp_msg(MSGT_DEMUX
, MSGL_WARN
,"Something went wrong, no data chunk found on given address (%d)\n", data_pos
);
340 stream_skip(demuxer
->stream
, 14);
341 add_index_segment(demuxer
, -1, -1);
343 for (i
= 0; i
< MAX_STREAMS
; i
++)
345 if (priv
->index_table_size
[i
] > 0)
347 dump_index(demuxer
, i
);
350 stream_reset(demuxer
->stream
);
351 stream_seek(demuxer
->stream
, origpos
);
357 static int generate_index(demuxer_t
*demuxer
)
359 real_priv_t
*priv
= demuxer
->priv
;
360 int origpos
= stream_tell(demuxer
->stream
);
361 int data_pos
= priv
->data_chunk_offset
-10;
362 int num_of_packets
= 0;
364 int len
, stream_id
= 0, flags
;
365 unsigned int timestamp
;
369 stream_seek(demuxer
->stream
, data_pos
);
371 i
= stream_read_dword_le(demuxer
->stream
);
372 if ((i
== -256) || (i
!= MKTAG('D', 'A', 'T', 'A')))
374 mp_msg(MSGT_DEMUX
, MSGL_WARN
,"Something went wrong, no data chunk found on given address (%d)\n",
378 stream_skip(demuxer
->stream
, 4); /* chunk size */
379 stream_skip(demuxer
->stream
, 2); /* version */
381 num_of_packets
= stream_read_dword(demuxer
->stream
);
382 mp_msg(MSGT_DEMUX
, MSGL_V
,"Generating index table from raw data (pos: 0x%x) for %d packets\n",
383 data_pos
, num_of_packets
);
385 data_pos
= stream_read_dword_le(demuxer
->stream
)-10; /* next data chunk */
387 for (i
= 0; i
< MAX_STREAMS
; i
++)
389 priv
->index_table_size
[i
] = num_of_packets
;
390 priv
->index_table
[i
] = calloc(priv
->index_table_size
[i
], sizeof(real_index_table_t
));
391 // priv->index_table[stream_id] = realloc(priv->index_table[stream_id],
392 // priv->index_table_size[stream_id] * sizeof(real_index_table_t));
397 // memset(priv->index_table_size, 0, sizeof(int)*MAX_STREAMS);
398 // memset(priv->index_table, 0, sizeof(real_index_table_t)*MAX_STREAMS);
400 while (tab_pos
< num_of_packets
)
402 i
= stream_read_char(demuxer
->stream
);
405 stream_skip(demuxer
->stream
, 1);
406 // stream_skip(demuxer->stream, 2); /* version */
408 len
= stream_read_word(demuxer
->stream
);
409 stream_id
= stream_read_word(demuxer
->stream
);
410 timestamp
= stream_read_dword(demuxer
->stream
);
412 stream_skip(demuxer
->stream
, 1); /* reserved */
413 flags
= stream_read_char(demuxer
->stream
);
417 // priv->index_table_size[stream_id] = i;
418 // if (priv->index_table[stream_id] == NULL)
419 // priv->index_table[stream_id] = malloc(priv->index_table_size[stream_id] * sizeof(real_index_table_t));
421 // priv->index_table[stream_id] = realloc(priv->index_table[stream_id],
422 // priv->index_table_size[stream_id] * sizeof(real_index_table_t));
424 priv
->index_table
[stream_id
][i
].timestamp
= timestamp
;
425 priv
->index_table
[stream_id
][i
].offset
= stream_tell(demuxer
->stream
)-12;
426 priv
->index_table
[stream_id
][i
].len
= len
;
427 priv
->index_table
[stream_id
][i
].packetno
= entries
;
428 priv
->index_table
[stream_id
][i
].flags
= flags
;
433 stream_skip(demuxer
->stream
, len
-12);
435 dump_index(demuxer
, stream_id
);
441 stream_reset(demuxer
->stream
);
442 stream_seek(demuxer
->stream
, origpos
);
451 static int real_check_file(demuxer_t
* demuxer
)
456 mp_msg(MSGT_DEMUX
,MSGL_V
,"Checking for REAL\n");
458 c
= stream_read_dword_le(demuxer
->stream
);
461 if (c
!= MKTAG('.', 'R', 'M', 'F'))
462 return 0; /* bad magic */
464 priv
= malloc(sizeof(real_priv_t
));
465 memset(priv
, 0, sizeof(real_priv_t
));
466 demuxer
->priv
= priv
;
468 return DEMUXER_TYPE_REAL
;
471 void hexdump(char *, unsigned long);
473 #define SKIP_BITS(n) buffer<<=n
474 #define SHOW_BITS(n) ((buffer)>>(32-(n)))
476 double real_fix_timestamp(unsigned char *buf
, unsigned int timestamp
, unsigned int format
, int64_t *kf_base
, int *kf_pts
, double *pts
){
478 unsigned char *s
= buf
+ 1 + (*buf
+1)*8;
479 uint32_t buffer
= (s
[0]<<24) + (s
[1]<<16) + (s
[2]<<8) + s
[3];
480 unsigned int kf
=timestamp
;
482 unsigned int orig_kf
;
485 if(format
==mmioFOURCC('R','V','3','0') || format
==mmioFOURCC('R','V','4','0')){
486 if(format
==mmioFOURCC('R','V','3','0')){
488 pict_type
= SHOW_BITS(2);
492 pict_type
= SHOW_BITS(2);
493 SKIP_BITS(2 + 7 + 3);
496 kf
= SHOW_BITS(13); // kf= 2*SHOW_BITS(12);
499 // I frame, sync timestamps:
500 *kf_base
=(int64_t)timestamp
-kf
;
501 mp_msg(MSGT_DEMUX
, MSGL_DBG2
,"\nTS: base=%08"PRIX64
"\n",*kf_base
);
504 // P/B frame, merge timestamps:
505 int64_t tmp
=(int64_t)timestamp
-*kf_base
;
506 kf
|=tmp
&(~0x1fff); // combine with packet timestamp
507 if(kf
<tmp
-4096) kf
+=8192; else // workaround wrap-around problems
508 if(kf
>tmp
+4096) kf
-=8192;
511 if(pict_type
!= 3){ // P || I frame -> swap timestamps
517 mp_msg(MSGT_DEMUX
, MSGL_DBG2
,"\nTS: %08X -> %08X (%04X) %d %02X %02X %02X %02X %5u\n",timestamp
,kf
,orig_kf
,pict_type
,s
[0],s
[1],s
[2],s
[3],pts
?kf
-(unsigned int)(*pts
*1000.0):0);
521 // if(pts && (v_pts<*pts || !kf)) v_pts=*pts+frametime;
526 typedef struct dp_hdr_s
{
527 uint32_t chunks
; // number of chunks
528 uint32_t timestamp
; // timestamp from packet header
529 uint32_t len
; // length of actual data
530 uint32_t chunktab
; // offset to chunk offset array
533 static void queue_video_packet(real_priv_t
*priv
, demux_stream_t
*ds
, demux_packet_t
*dp
)
535 dp_hdr_t hdr
= *(dp_hdr_t
*)dp
->buffer
;
536 unsigned char *tmp
= malloc(8*(1+hdr
.chunks
));
537 memcpy(tmp
, dp
->buffer
+hdr
.chunktab
, 8*(1+hdr
.chunks
));
538 memmove(dp
->buffer
+1+8*(1+hdr
.chunks
), dp
->buffer
+sizeof(dp_hdr_t
), hdr
.len
);
539 memcpy(dp
->buffer
+1, tmp
, 8*(1+hdr
.chunks
));
540 *dp
->buffer
= (uint8_t)hdr
.chunks
;
543 if(priv
->video_after_seek
){
545 priv
->kf_pts
= hdr
.timestamp
;
546 priv
->video_after_seek
= 0;
548 if(hdr
.len
>= 3) /* this check may be useless */
549 dp
->pts
= real_fix_timestamp(dp
->buffer
, hdr
.timestamp
,
550 ((sh_video_t
*)ds
->sh
)->format
,
551 &priv
->kf_base
, &priv
->kf_pts
,
553 ds_add_packet(ds
, dp
);
557 // 0 = EOF or no stream found
558 // 1 = successfully read a packet
559 static int demux_real_fill_buffer(demuxer_t
*demuxer
, demux_stream_t
*dsds
)
561 real_priv_t
*priv
= demuxer
->priv
;
562 demux_stream_t
*ds
= NULL
;
564 unsigned int timestamp
;
573 int x
, sps
, cfs
, sph
, spc
, w
;
574 int audioreorder_getnextpk
= 0;
576 // Don't demux video if video codec init failed
577 if (demuxer
->video
->id
>= 0 && !demuxer
->video
->sh
)
578 demuxer
->video
->id
= -2;
580 while(!stream_eof(demuxer
->stream
)){
582 /* Handle audio/video demxing switch for multirate files (non-interleaved) */
583 if (priv
->is_multirate
&& priv
->stream_switch
) {
584 if (priv
->current_apacket
>= priv
->index_table_size
[demuxer
->audio
->id
])
585 demuxer
->audio
->eof
= 1;
586 if (priv
->current_vpacket
>= priv
->index_table_size
[demuxer
->video
->id
])
587 demuxer
->video
->eof
= 1;
588 if (demuxer
->audio
->eof
&& demuxer
->video
->eof
)
590 else if (!demuxer
->audio
->eof
&& demuxer
->video
->eof
)
591 stream_seek(demuxer
->stream
, priv
->audio_curpos
); // Get audio
592 else if (demuxer
->audio
->eof
&& !demuxer
->video
->eof
)
593 stream_seek(demuxer
->stream
, priv
->video_curpos
); // Get video
594 else if (priv
->index_table
[demuxer
->audio
->id
][priv
->current_apacket
].timestamp
<
595 priv
->index_table
[demuxer
->video
->id
][priv
->current_vpacket
].timestamp
)
596 stream_seek(demuxer
->stream
, priv
->audio_curpos
); // Get audio
598 stream_seek(demuxer
->stream
, priv
->video_curpos
); // Get video
599 priv
->stream_switch
= 0;
602 demuxer
->filepos
= stream_tell(demuxer
->stream
);
603 version
= stream_read_word(demuxer
->stream
); /* version */
604 len
= stream_read_word(demuxer
->stream
);
605 if ((version
==0x4441) && (len
==0x5441)) { // new data chunk
606 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"demux_real: New data chunk is coming!!!\n");
607 if (priv
->is_multirate
)
609 stream_skip(demuxer
->stream
,14);
610 demuxer
->filepos
= stream_tell(demuxer
->stream
);
611 version
= stream_read_word(demuxer
->stream
); /* version */
612 len
= stream_read_word(demuxer
->stream
);
613 } else if ((version
== 0x494e) && (len
== 0x4458)) {
614 mp_msg(MSGT_DEMUX
,MSGL_V
,"demux_real: Found INDX chunk. EOF.\n");
615 demuxer
->stream
->eof
=1;
620 if (len
== -256){ /* EOF */
621 // printf("len==-256!\n");
625 mp_msg(MSGT_DEMUX
, MSGL_V
,"%08X: packet v%d len=%d \n",(int)demuxer
->filepos
,(int)version
,(int)len
);
626 mp_msg(MSGT_DEMUX
, MSGL_WARN
,"bad packet len (%d)\n", len
);
627 if ((unsigned)demuxer
->video
->id
< MAX_STREAMS
) {
628 if (priv
->current_vpacket
+ 1 < priv
->index_table_size
[demuxer
->video
->id
]) {
629 stream_seek(demuxer
->stream
, priv
->index_table
[demuxer
->video
->id
][++priv
->current_vpacket
].offset
);
631 } else if ((unsigned)demuxer
->audio
->id
< MAX_STREAMS
) {
632 if (priv
->current_apacket
+ 1 < priv
->index_table_size
[demuxer
->audio
->id
]) {
633 stream_seek(demuxer
->stream
, priv
->index_table
[demuxer
->audio
->id
][++priv
->current_apacket
].offset
);
636 continue; //goto loop;
639 stream_id
= stream_read_word(demuxer
->stream
);
640 timestamp
= stream_read_dword(demuxer
->stream
);
641 reserved
= stream_read_char(demuxer
->stream
);
642 flags
= stream_read_char(demuxer
->stream
);
649 tmp
= stream_read_char(demuxer
->stream
);
650 mp_msg(MSGT_DEMUX
, MSGL_DBG2
,"Version: %d, skipped byte is %d\n", version
, tmp
);
655 add_index_item(demuxer
, stream_id
, timestamp
, demuxer
->filepos
);
657 // printf("%08X: packet v%d len=%4d id=%d pts=%6d rvd=%d flags=%d \n",
658 // (int)demuxer->filepos,(int)version,(int)len, stream_id,
659 // (int) timestamp, reserved, flags);
661 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "\npacket#%d: pos: 0x%0x, len: %d, id: %d, pts: %u, flags: %x rvd:%d\n",
662 priv
->current_packet
, (int)demuxer
->filepos
, len
, stream_id
, timestamp
, flags
, reserved
);
664 priv
->current_packet
++;
667 // printf("s_id=%d aid=%d vid=%d \n",stream_id,demuxer->audio->id,demuxer->video->id);
669 /* check stream_id: */
671 if(demuxer
->audio
->id
==stream_id
){
672 if (priv
->audio_need_keyframe
== 1&& flags
!= 0x2)
676 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "packet is audio (id: %d)\n", stream_id
);
679 priv
->sub_packet_cnt
= 0;
680 audioreorder_getnextpk
= 0;
683 // parse audio chunk:
686 int spos
=stream_tell(demuxer
->stream
);
688 static int cnt2
=CRACK_MATRIX
;
690 if (((sh_audio_t
*)ds
->sh
)->format
== mmioFOURCC('M', 'P', '4', 'A')) {
691 uint16_t sub_packet_lengths
[16], sub_packets
, i
;
693 /* AAC in Real: several AAC frames in one Real packet. */
694 /* Second byte, upper four bits: number of AAC frames */
695 /* next n * 2 bytes: length of the AAC frames in bytes, BE */
698 sub_packets
= (stream_read_word(demuxer
->stream
) & 0xf0) >> 4;
699 if (len
< 2 * sub_packets
)
701 for (i
= 0; i
< sub_packets
; i
++)
702 totlen
+= sub_packet_lengths
[i
] = stream_read_word(demuxer
->stream
);
705 for (i
= 0; i
< sub_packets
; i
++) {
706 demux_packet_t
*dp
= new_demux_packet(sub_packet_lengths
[i
]);
707 stream_read(demuxer
->stream
, dp
->buffer
, sub_packet_lengths
[i
]);
708 if (priv
->a_pts
!= timestamp
)
709 dp
->pts
= timestamp
/ 1000.0;
710 priv
->a_pts
= timestamp
;
711 dp
->pos
= demuxer
->filepos
;
712 ds_add_packet(ds
, dp
);
716 if ((priv
->intl_id
[stream_id
] == mmioFOURCC('I', 'n', 't', '4')) ||
717 (priv
->intl_id
[stream_id
] == mmioFOURCC('g', 'e', 'n', 'r')) ||
718 (priv
->intl_id
[stream_id
] == mmioFOURCC('s', 'i', 'p', 'r'))) {
719 sps
= priv
->sub_packet_size
[stream_id
];
720 sph
= priv
->sub_packet_h
[stream_id
];
721 cfs
= priv
->coded_framesize
[stream_id
];
722 w
= priv
->audiopk_size
[stream_id
];
723 spc
= priv
->sub_packet_cnt
;
724 switch (priv
->intl_id
[stream_id
]) {
725 case mmioFOURCC('I', 'n', 't', '4'):
726 if (len
< cfs
* sph
/2)
728 for (x
= 0; x
< sph
/ 2; x
++)
729 stream_read(demuxer
->stream
, priv
->audio_buf
+ x
* 2 * w
+ spc
* cfs
, cfs
);
731 case mmioFOURCC('g', 'e', 'n', 'r'):
734 for (x
= 0; x
< w
/ sps
; x
++)
735 stream_read(demuxer
->stream
, priv
->audio_buf
+ sps
* (sph
* x
+ ((sph
+ 1) / 2) * (spc
& 1) +
738 case mmioFOURCC('s', 'i', 'p', 'r'):
741 stream_read(demuxer
->stream
, priv
->audio_buf
+ spc
* w
, w
);
742 if (spc
== sph
- 1) {
744 int bs
= sph
* w
* 2 / 96; // nibbles per subpacket
745 // Perform reordering
746 for(n
=0; n
< 38; n
++) {
748 int i
= bs
* sipr_swaps
[n
][0];
749 int o
= bs
* sipr_swaps
[n
][1];
750 // swap nibbles of block 'i' with 'o' TODO: optimize
751 for(j
= 0;j
< bs
; j
++) {
752 int x
= (i
& 1) ? (priv
->audio_buf
[i
>> 1] >> 4) : (priv
->audio_buf
[i
>> 1] & 0x0F);
753 int y
= (o
& 1) ? (priv
->audio_buf
[o
>> 1] >> 4) : (priv
->audio_buf
[o
>> 1] & 0x0F);
755 priv
->audio_buf
[o
>> 1] = (priv
->audio_buf
[o
>> 1] & 0x0F) | (x
<< 4);
757 priv
->audio_buf
[o
>> 1] = (priv
->audio_buf
[o
>> 1] & 0xF0) | x
;
759 priv
->audio_buf
[i
>> 1] = (priv
->audio_buf
[i
>> 1] & 0x0F) | (y
<< 4);
761 priv
->audio_buf
[i
>> 1] = (priv
->audio_buf
[i
>> 1] & 0xF0) | y
;
768 priv
->audio_need_keyframe
= 0;
769 priv
->audio_timestamp
[priv
->sub_packet_cnt
] = (priv
->a_pts
==timestamp
) ? MP_NOPTS_VALUE
: (timestamp
/1000.0);
770 priv
->a_pts
= timestamp
;
771 if (priv
->sub_packet_cnt
== 0)
772 priv
->audio_filepos
= demuxer
->filepos
;
773 if (++(priv
->sub_packet_cnt
) < sph
)
774 audioreorder_getnextpk
= 1;
776 int apk_usize
= ((WAVEFORMATEX
*)((sh_audio_t
*)ds
->sh
)->wf
)->nBlockAlign
;
777 audioreorder_getnextpk
= 0;
778 priv
->sub_packet_cnt
= 0;
779 // Release all the audio packets
780 for (x
= 0; x
< sph
*w
/apk_usize
; x
++) {
781 dp
= new_demux_packet(apk_usize
);
782 memcpy(dp
->buffer
, priv
->audio_buf
+ x
* apk_usize
, apk_usize
);
783 /* Put timestamp only on packets that correspond to original audio packets in file */
784 if (x
* apk_usize
% w
== 0)
785 dp
->pts
= priv
->audio_timestamp
[x
* apk_usize
/ w
];
786 dp
->pos
= priv
->audio_filepos
; // all equal
787 dp
->flags
= x
? 0 : 0x10; // Mark first packet as keyframe
788 ds_add_packet(ds
, dp
);
791 } else { // No interleaving
792 dp
= new_demux_packet(len
);
793 stream_read(demuxer
->stream
, dp
->buffer
, len
);
796 mp_msg(MSGT_DEMUX
, MSGL_V
,"*** audio block len=%d\n",len
);
797 { // HACK - used for reverse engineering the descrambling matrix
798 FILE* f
=fopen("test.rm","r+");
799 fseek(f
,spos
,SEEK_SET
);
801 // for(i=0;i<len;i++) dp->buffer[i]=i/0x12;
802 // for(i=0;i<len;i++) dp->buffer[i]=i;
803 // for(i=0;i<len;i++) dp->buffer[i]=cnt;
804 // for(i=0;i<len;i++) dp->buffer[i]=cnt<<4;
805 for(i
=0;i
<len
;i
++) dp
->buffer
[i
]=(i
==cnt2
) ? (cnt
+16*(8+cnt
)) : 0;
806 if(cnt
==6){ cnt
=0; ++cnt2
; }
807 fwrite(dp
->buffer
, len
, 1, f
);
809 if(cnt2
>0x150) *((int*)NULL
)=1; // sig11 :)
813 if( ((sh_audio_t
*)ds
->sh
)->format
== 0x2000) {
814 // if DNET, swap bytes, as DNET is byte-swapped AC3:
815 char *ptr
= dp
->buffer
;
817 for (i
= 0; i
< len
; i
+= 2)
819 const char tmp
= ptr
[0];
826 if (priv
->audio_need_keyframe
== 1) {
827 priv
->audio_need_keyframe
= 0;
828 } else if(priv
->a_pts
!= timestamp
)
829 dp
->pts
= timestamp
/1000.0;
830 priv
->a_pts
=timestamp
;
831 dp
->pos
= demuxer
->filepos
;
832 dp
->flags
= (flags
& 0x2) ? 0x10 : 0;
833 ds_add_packet(ds
, dp
);
835 } // codec_id check, codec default case
837 // we will not use audio index if we use -idx and have a video
838 if(((!demuxer
->video
->sh
&& index_mode
== 2) || priv
->is_multirate
) && (unsigned)demuxer
->audio
->id
< MAX_STREAMS
) {
839 while (priv
->current_apacket
+ 1 < priv
->index_table_size
[demuxer
->audio
->id
] &&
840 timestamp
> priv
->index_table
[demuxer
->audio
->id
][priv
->current_apacket
].timestamp
) {
841 priv
->current_apacket
+= 1;
842 priv
->stream_switch
= 1;
844 if (priv
->stream_switch
)
845 priv
->audio_curpos
= stream_tell(demuxer
->stream
);
848 // If we're reordering audio packets and we need more data get it
849 if (audioreorder_getnextpk
)
855 if(demuxer
->video
->id
==stream_id
){
858 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "packet is video (id: %d)\n", stream_id
);
860 // parse video chunk:
862 // we need a more complicated, 2nd level demuxing, as the video
863 // frames are stored fragmented in the video chunks :(
864 sh_video_t
*sh_video
= ds
->sh
;
866 unsigned vpkg_header
, vpkg_length
, vpkg_offset
;
872 unsigned char* dp_data
;
875 // printf("xxx len=%d \n",len);
877 // read packet header
878 // bit 7: 1=last block in block chain
879 // bit 6: 1=short header (only one block?)
880 vpkg_header
=stream_read_char(demuxer
->stream
); --len
;
881 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "hdr: %02X (len=%d) ",vpkg_header
,len
);
883 if (0x40==(vpkg_header
&0xc0)) {
884 // seems to be a very short header
885 // 2 bytes, purpose of the second byte yet unknown
887 bummer
=stream_read_char(demuxer
->stream
); --len
;
888 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "%02X",bummer
);
893 if (0==(vpkg_header
&0x40)) {
894 // sub-seqnum (bits 0-6: number of fragment. bit 7: ???)
895 vpkg_subseq
=stream_read_char(demuxer
->stream
);
897 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "subseq: %02X ",vpkg_subseq
);
901 // size of the complete packet
902 // bit 14 is always one (same applies to the offset)
903 vpkg_length
=stream_read_word(demuxer
->stream
);
905 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "l: %02X %02X ",vpkg_length
>>8,vpkg_length
&0xff);
906 if (!(vpkg_length
&0xC000)) {
908 vpkg_length
|=(uint16_t)stream_read_word(demuxer
->stream
);
909 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "l+: %02X %02X ",(vpkg_length
>>8)&0xff,vpkg_length
&0xff);
914 // offset of the following data inside the complete packet
915 // Note: if (hdr&0xC0)==0x80 then offset is relative to the
916 // _end_ of the packet, so it's equal to fragment size!!!
917 vpkg_offset
=stream_read_word(demuxer
->stream
);
919 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "o: %02X %02X ",vpkg_offset
>>8,vpkg_offset
&0xff);
920 if (!(vpkg_offset
&0xC000)) {
922 vpkg_offset
|=(uint16_t)stream_read_word(demuxer
->stream
);
923 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "o+: %02X %02X ",(vpkg_offset
>>8)&0xff,vpkg_offset
&0xff);
928 vpkg_seqnum
=stream_read_char(demuxer
->stream
); --len
;
929 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "seq: %02X ",vpkg_seqnum
);
931 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "\n");
932 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "blklen=%d\n", len
);
933 mp_msg(MSGT_DEMUX
,MSGL_DBG2
, "block: hdr=0x%0x, len=%d, offset=%d, seqnum=%d\n",
934 vpkg_header
, vpkg_length
, vpkg_offset
, vpkg_seqnum
);
938 dp_hdr
=(dp_hdr_t
*)dp
->buffer
;
939 dp_data
=dp
->buffer
+sizeof(dp_hdr_t
);
940 extra
=(uint32_t*)(dp
->buffer
+dp_hdr
->chunktab
);
941 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "we have an incomplete packet (oldseq=%d new=%d)\n",ds
->asf_seq
,vpkg_seqnum
);
942 // we have an incomplete packet:
943 if(ds
->asf_seq
!=vpkg_seqnum
){
944 // this fragment is for new packet, close the old one
945 mp_msg(MSGT_DEMUX
,MSGL_DBG2
, "closing probably incomplete packet, len: %d \n",dp
->len
);
946 queue_video_packet(priv
, ds
, dp
);
949 // append data to it!
951 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"[chunks=%d subseq=%d]\n",dp_hdr
->chunks
,vpkg_subseq
);
952 if(dp_hdr
->chunktab
+8*(1+dp_hdr
->chunks
)>dp
->len
){
953 // increase buffer size, this should not happen!
954 mp_msg(MSGT_DEMUX
,MSGL_WARN
, "chunktab buffer too small!!!!!\n");
955 dp
->len
=dp_hdr
->chunktab
+8*(4+dp_hdr
->chunks
);
956 dp
->buffer
=realloc(dp
->buffer
,dp
->len
+MP_INPUT_BUFFER_PADDING_SIZE
);
957 memset(dp
->buffer
+ dp
->len
, 0, MP_INPUT_BUFFER_PADDING_SIZE
);
959 dp_hdr
=(dp_hdr_t
*)dp
->buffer
;
960 dp_data
=dp
->buffer
+sizeof(dp_hdr_t
);
961 extra
=(uint32_t*)(dp
->buffer
+dp_hdr
->chunktab
);
963 extra
[2*dp_hdr
->chunks
+0]=le2me_32(1);
964 extra
[2*dp_hdr
->chunks
+1]=le2me_32(dp_hdr
->len
);
965 if(0x80==(vpkg_header
&0xc0)){
967 if(dp_hdr
->len
!=vpkg_length
-vpkg_offset
)
968 mp_msg(MSGT_DEMUX
,MSGL_V
,"warning! assembled.len=%d frag.len=%d total.len=%d \n",dp
->len
,vpkg_offset
,vpkg_length
-vpkg_offset
);
969 if (vpkg_offset
> dp
->len
- sizeof(dp_hdr_t
) - dp_hdr
->len
) vpkg_offset
= dp
->len
- sizeof(dp_hdr_t
) - dp_hdr
->len
;
970 stream_read(demuxer
->stream
, dp_data
+dp_hdr
->len
, vpkg_offset
);
971 if((dp_data
[dp_hdr
->len
]&0x20) && (sh_video
->format
==0x30335652)) --dp_hdr
->chunks
; else
972 dp_hdr
->len
+=vpkg_offset
;
974 mp_dbg(MSGT_DEMUX
,MSGL_DBG2
, "fragment (%d bytes) appended, %d bytes left\n",vpkg_offset
,len
);
975 // we know that this is the last fragment -> we can close the packet!
976 queue_video_packet(priv
, ds
, dp
);
981 // non-last fragment:
982 if(dp_hdr
->len
!=vpkg_offset
)
983 mp_msg(MSGT_DEMUX
,MSGL_V
,"warning! assembled.len=%d offset=%d frag.len=%d total.len=%d \n",dp
->len
,vpkg_offset
,len
,vpkg_length
);
984 if (len
> dp
->len
- sizeof(dp_hdr_t
) - dp_hdr
->len
) len
= dp
->len
- sizeof(dp_hdr_t
) - dp_hdr
->len
;
985 stream_read(demuxer
->stream
, dp_data
+dp_hdr
->len
, len
);
986 if((dp_data
[dp_hdr
->len
]&0x20) && (sh_video
->format
==0x30335652)) --dp_hdr
->chunks
; else
989 break; // no more fragments in this chunk!
992 // create new packet!
993 dp
= new_demux_packet(sizeof(dp_hdr_t
)+vpkg_length
+8*(1+2*(vpkg_header
&0x3F)));
994 // the timestamp seems to be in milliseconds
995 dp
->pos
= demuxer
->filepos
;
996 dp
->flags
= (flags
& 0x2) ? 0x10 : 0;
997 ds
->asf_seq
= vpkg_seqnum
;
998 dp_hdr
=(dp_hdr_t
*)dp
->buffer
;
1000 dp_hdr
->timestamp
=timestamp
;
1001 dp_hdr
->chunktab
=sizeof(dp_hdr_t
)+vpkg_length
;
1002 dp_data
=dp
->buffer
+sizeof(dp_hdr_t
);
1003 extra
=(uint32_t*)(dp
->buffer
+dp_hdr
->chunktab
);
1004 extra
[0]=le2me_32(1); extra
[1]=0; // offset of the first chunk
1005 if(0x00==(vpkg_header
&0xc0)){
1007 if (len
> dp
->len
- sizeof(dp_hdr_t
)) len
= dp
->len
- sizeof(dp_hdr_t
);
1009 stream_read(demuxer
->stream
, dp_data
, len
);
1014 // whole packet (not fragmented):
1015 if (vpkg_length
> len
) {
1016 mp_msg(MSGT_DEMUX
, MSGL_WARN
,"\n******** WARNING: vpkg_length=%i > len=%i ********\n", vpkg_length
, len
);
1018 * To keep the video stream rolling, we need to break
1019 * here. We shouldn't touch len to make sure rest of the
1020 * broken packet is skipped.
1024 dp_hdr
->len
=vpkg_length
; len
-=vpkg_length
;
1025 stream_read(demuxer
->stream
, dp_data
, vpkg_length
);
1026 queue_video_packet(priv
, ds
, dp
);
1031 mp_msg(MSGT_DEMUX
, MSGL_WARN
,"\n******** !!!!!!!! BUG!! len=%d !!!!!!!!!!! ********\n",len
);
1032 if(len
>0) stream_skip(demuxer
->stream
, len
);
1035 if ((unsigned)demuxer
->video
->id
< MAX_STREAMS
) {
1036 while (priv
->current_vpacket
+ 1 < priv
->index_table_size
[demuxer
->video
->id
] &&
1037 timestamp
> priv
->index_table
[demuxer
->video
->id
][priv
->current_vpacket
+ 1].timestamp
) {
1038 priv
->current_vpacket
+= 1;
1039 priv
->stream_switch
= 1;
1041 if (priv
->stream_switch
)
1042 priv
->video_curpos
= stream_tell(demuxer
->stream
);
1048 if((unsigned)stream_id
<MAX_STREAMS
){
1050 if(demuxer
->audio
->id
==-1 && demuxer
->a_streams
[stream_id
]){
1051 sh_audio_t
*sh
= demuxer
->a_streams
[stream_id
];
1052 demuxer
->audio
->id
=stream_id
;
1053 sh
->ds
=demuxer
->audio
;
1054 demuxer
->audio
->sh
=sh
;
1055 priv
->audio_buf
= calloc(priv
->sub_packet_h
[demuxer
->audio
->id
], priv
->audiopk_size
[demuxer
->audio
->id
]);
1056 priv
->audio_timestamp
= calloc(priv
->sub_packet_h
[demuxer
->audio
->id
], sizeof(double));
1057 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected RM audio ID = %d\n",stream_id
);
1061 if(demuxer
->video
->id
==-1 && demuxer
->v_streams
[stream_id
]){
1062 sh_video_t
*sh
= demuxer
->v_streams
[stream_id
];
1063 demuxer
->video
->id
=stream_id
;
1064 sh
->ds
=demuxer
->video
;
1065 demuxer
->video
->sh
=sh
;
1066 mp_msg(MSGT_DEMUX
,MSGL_V
,"Auto-selected RM video ID = %d\n",stream_id
);
1072 mp_msg(MSGT_DEMUX
,MSGL_DBG2
, "unknown stream id (%d)\n", stream_id
);
1074 stream_skip(demuxer
->stream
, len
);
1079 void print_wave_header(WAVEFORMATEX
*h
, int verbose_level
);
1081 static demuxer_t
* demux_open_real(demuxer_t
* demuxer
)
1083 real_priv_t
* priv
= demuxer
->priv
;
1090 header_size
= stream_read_dword(demuxer
->stream
); /* header size */
1091 mp_msg(MSGT_DEMUX
,MSGL_V
, "real: Header size: %d\n", header_size
);
1092 i
= stream_read_word(demuxer
->stream
); /* version */
1093 mp_msg(MSGT_DEMUX
,MSGL_V
, "real: Header object version: %d\n", i
);
1094 if (header_size
== 0x10)
1095 i
= stream_read_word(demuxer
->stream
);
1096 else /* we should test header_size here too. */
1097 i
= stream_read_dword(demuxer
->stream
);
1098 mp_msg(MSGT_DEMUX
,MSGL_V
, "real: File version: %d\n", i
);
1099 num_of_headers
= stream_read_dword(demuxer
->stream
);
1102 for (i
= 1; i
<= num_of_headers
; i
++)
1103 // for (i = 1; ; i++)
1105 int chunk_id
, chunk_pos
, chunk_size
;
1107 chunk_pos
= stream_tell(demuxer
->stream
);
1108 chunk_id
= stream_read_dword_le(demuxer
->stream
);
1109 chunk_size
= stream_read_dword(demuxer
->stream
);
1111 stream_skip(demuxer
->stream
, 2); /* version */
1113 mp_msg(MSGT_DEMUX
,MSGL_V
, "Chunk: %.4s (%x) (size: 0x%x, offset: 0x%x)\n",
1114 (char *)&chunk_id
, chunk_id
, chunk_size
, chunk_pos
);
1116 if (chunk_size
< 10){
1117 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"demux_real: invalid chunksize! (%d)\n",chunk_size
);
1123 case MKTAG('P', 'R', 'O', 'P'):
1124 /* Properties header */
1126 stream_skip(demuxer
->stream
, 4); /* max bitrate */
1127 stream_skip(demuxer
->stream
, 4); /* avg bitrate */
1128 stream_skip(demuxer
->stream
, 4); /* max packet size */
1129 stream_skip(demuxer
->stream
, 4); /* avg packet size */
1130 stream_skip(demuxer
->stream
, 4); /* nb packets */
1131 priv
->duration
= stream_read_dword(demuxer
->stream
)/1000; /* duration */
1132 stream_skip(demuxer
->stream
, 4); /* preroll */
1133 priv
->index_chunk_offset
= stream_read_dword(demuxer
->stream
);
1134 mp_msg(MSGT_DEMUX
,MSGL_V
,"First index chunk offset: 0x%x\n", priv
->index_chunk_offset
);
1135 priv
->data_chunk_offset
= stream_read_dword(demuxer
->stream
)+10;
1136 mp_msg(MSGT_DEMUX
,MSGL_V
,"First data chunk offset: 0x%x\n", priv
->data_chunk_offset
);
1137 stream_skip(demuxer
->stream
, 2); /* nb streams */
1139 stream_skip(demuxer
->stream
, 2); /* flags */
1142 int flags
= stream_read_word(demuxer
->stream
);
1146 mp_msg(MSGT_DEMUX
,MSGL_V
,"Flags (%x): ", flags
);
1148 mp_msg(MSGT_DEMUX
,MSGL_V
,"[save allowed] ");
1150 mp_msg(MSGT_DEMUX
,MSGL_V
,"[perfect play (more buffers)] ");
1152 mp_msg(MSGT_DEMUX
,MSGL_V
,"[live broadcast] ");
1153 mp_msg(MSGT_DEMUX
,MSGL_V
,"\n");
1158 case MKTAG('C', 'O', 'N', 'T'):
1160 /* Content description header */
1164 len
= stream_read_word(demuxer
->stream
);
1167 buf
= malloc(len
+1);
1168 stream_read(demuxer
->stream
, buf
, len
);
1170 demux_info_add(demuxer
, "name", buf
);
1174 len
= stream_read_word(demuxer
->stream
);
1177 buf
= malloc(len
+1);
1178 stream_read(demuxer
->stream
, buf
, len
);
1180 demux_info_add(demuxer
, "author", buf
);
1184 len
= stream_read_word(demuxer
->stream
);
1187 buf
= malloc(len
+1);
1188 stream_read(demuxer
->stream
, buf
, len
);
1190 demux_info_add(demuxer
, "copyright", buf
);
1194 len
= stream_read_word(demuxer
->stream
);
1197 buf
= malloc(len
+1);
1198 stream_read(demuxer
->stream
, buf
, len
);
1200 demux_info_add(demuxer
, "comment", buf
);
1205 case MKTAG('M', 'D', 'P', 'R'):
1207 /* Media properties header */
1210 int codec_data_size
;
1214 char *descr
, *mimet
= NULL
;
1216 stream_id
= stream_read_word(demuxer
->stream
);
1217 mp_msg(MSGT_DEMUX
,MSGL_V
,"Found new stream (id: %d)\n", stream_id
);
1219 stream_skip(demuxer
->stream
, 4); /* max bitrate */
1220 bitrate
= stream_read_dword(demuxer
->stream
); /* avg bitrate */
1221 stream_skip(demuxer
->stream
, 4); /* max packet size */
1222 stream_skip(demuxer
->stream
, 4); /* avg packet size */
1223 stream_skip(demuxer
->stream
, 4); /* start time */
1224 stream_skip(demuxer
->stream
, 4); /* preroll */
1225 stream_skip(demuxer
->stream
, 4); /* duration */
1227 if ((len
= stream_read_char(demuxer
->stream
)) > 0) {
1228 descr
= malloc(len
+1);
1229 stream_read(demuxer
->stream
, descr
, len
);
1231 mp_msg(MSGT_DEMUX
, MSGL_INFO
,"Stream description: %s\n", descr
);
1234 if ((len
= stream_read_char(demuxer
->stream
)) > 0) {
1235 mimet
= malloc(len
+1);
1236 stream_read(demuxer
->stream
, mimet
, len
);
1238 mp_msg(MSGT_DEMUX
, MSGL_INFO
,"Stream mimetype: %s\n", mimet
);
1241 /* Type specific header */
1242 codec_data_size
= stream_read_dword(demuxer
->stream
);
1243 codec_pos
= stream_tell(demuxer
->stream
);
1246 #define stream_skip(st,siz) { int i; for(i=0;i<siz;i++) mp_msg(MSGT_DEMUX,MSGL_V," %02X",stream_read_char(st)); mp_msg(MSGT_DEMUX,MSGL_V,"\n");}
1249 if (!strncmp(mimet
,"audio/",6)) {
1250 if (strstr(mimet
,"x-pn-realaudio") || strstr(mimet
,"x-pn-multirate-realaudio")) {
1251 tmp
= stream_read_dword(demuxer
->stream
);
1252 if (tmp
!= MKTAG(0xfd, 'a', 'r', '.'))
1254 mp_msg(MSGT_DEMUX
,MSGL_V
,"Audio: can't find .ra in codec data\n");
1257 sh_audio_t
*sh
= new_sh_audio(demuxer
, stream_id
);
1258 char buf
[128]; /* for codec name */
1260 int sub_packet_size
;
1264 int coded_frame_size
;
1265 int codecdata_length
;
1269 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_AudioID
, "real", stream_id
);
1270 mp_msg(MSGT_DEMUX
,MSGL_V
,"Found audio stream!\n");
1271 version
= stream_read_word(demuxer
->stream
);
1272 mp_msg(MSGT_DEMUX
,MSGL_V
,"version: %d\n", version
);
1274 stream_skip(demuxer
->stream
, 2);
1275 stream_skip(demuxer
->stream
, 10);
1276 stream_skip(demuxer
->stream
, 4);
1277 // Name, author, (c) are also in CONT tag
1278 if ((i
= stream_read_char(demuxer
->stream
)) != 0) {
1280 stream_read(demuxer
->stream
, buft
, i
);
1282 demux_info_add(demuxer
, "Name", buft
);
1285 if ((i
= stream_read_char(demuxer
->stream
)) != 0) {
1287 stream_read(demuxer
->stream
, buft
, i
);
1289 demux_info_add(demuxer
, "Author", buft
);
1292 if ((i
= stream_read_char(demuxer
->stream
)) != 0) {
1294 stream_read(demuxer
->stream
, buft
, i
);
1296 demux_info_add(demuxer
, "Copyright", buft
);
1299 if ((i
= stream_read_char(demuxer
->stream
)) != 0)
1300 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"Last header byte is not zero!\n");
1301 stream_skip(demuxer
->stream
, 1);
1302 i
= stream_read_char(demuxer
->stream
);
1303 sh
->format
= stream_read_dword_le(demuxer
->stream
);
1305 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"Audio FourCC size is not 4 (%d), please report to "
1306 "MPlayer developers\n", i
);
1307 stream_skip(demuxer
->stream
, i
- 4);
1309 if (sh
->format
!= mmioFOURCC('l','p','c','J')) {
1310 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"Version 3 audio with FourCC %8x, please report to "
1311 "MPlayer developers\n", sh
->format
);
1314 sh
->samplesize
= 16;
1315 sh
->samplerate
= 8000;
1317 strcpy(buf
, "14_4");
1319 stream_skip(demuxer
->stream
, 2); // 00 00
1320 stream_skip(demuxer
->stream
, 4); /* .ra4 or .ra5 */
1321 stream_skip(demuxer
->stream
, 4); // ???
1322 stream_skip(demuxer
->stream
, 2); /* version (4 or 5) */
1323 hdr_size
= stream_read_dword(demuxer
->stream
); // header size
1324 mp_msg(MSGT_DEMUX
,MSGL_V
,"header size: %d\n", hdr_size
);
1325 flavor
= stream_read_word(demuxer
->stream
);/* codec flavor id */
1326 coded_frame_size
= stream_read_dword(demuxer
->stream
);/* needed by codec */
1327 mp_msg(MSGT_DEMUX
,MSGL_V
,"coded_frame_size: %d\n", coded_frame_size
);
1328 stream_skip(demuxer
->stream
, 4); // big number
1329 stream_skip(demuxer
->stream
, 4); // bigger number
1330 stream_skip(demuxer
->stream
, 4); // 2 || -''-
1331 sub_packet_h
= stream_read_word(demuxer
->stream
);
1332 mp_msg(MSGT_DEMUX
,MSGL_V
,"sub_packet_h: %d\n", sub_packet_h
);
1333 frame_size
= stream_read_word(demuxer
->stream
);
1334 mp_msg(MSGT_DEMUX
,MSGL_V
,"frame_size: %d\n", frame_size
);
1335 sub_packet_size
= stream_read_word(demuxer
->stream
);
1336 mp_msg(MSGT_DEMUX
,MSGL_V
,"sub_packet_size: %d\n", sub_packet_size
);
1337 stream_skip(demuxer
->stream
, 2); // 0
1340 stream_skip(demuxer
->stream
, 6); //0,srate,0
1342 sh
->samplerate
= stream_read_word(demuxer
->stream
);
1343 stream_skip(demuxer
->stream
, 2); // 0
1344 sh
->samplesize
= stream_read_word(demuxer
->stream
)/8;
1345 sh
->channels
= stream_read_word(demuxer
->stream
);
1346 mp_msg(MSGT_DEMUX
,MSGL_V
,"samplerate: %d, channels: %d\n",
1347 sh
->samplerate
, sh
->channels
);
1351 stream_read(demuxer
->stream
, buf
, 4); // interleaver id
1352 priv
->intl_id
[stream_id
] = MKTAG(buf
[0], buf
[1], buf
[2], buf
[3]);
1353 stream_read(demuxer
->stream
, buf
, 4); // fourcc
1358 /* Interleaver id */
1359 get_str(1, demuxer
, buf
, sizeof(buf
));
1360 priv
->intl_id
[stream_id
] = MKTAG(buf
[0], buf
[1], buf
[2], buf
[3]);
1362 get_str(1, demuxer
, buf
, sizeof(buf
));
1366 /* Emulate WAVEFORMATEX struct: */
1367 sh
->wf
= malloc(sizeof(WAVEFORMATEX
));
1368 memset(sh
->wf
, 0, sizeof(WAVEFORMATEX
));
1369 sh
->wf
->nChannels
= sh
->channels
;
1370 sh
->wf
->wBitsPerSample
= sh
->samplesize
*8;
1371 sh
->wf
->nSamplesPerSec
= sh
->samplerate
;
1372 sh
->wf
->nAvgBytesPerSec
= bitrate
/8;
1373 sh
->wf
->nBlockAlign
= frame_size
;
1375 sh
->format
= MKTAG(buf
[0], buf
[1], buf
[2], buf
[3]);
1379 case MKTAG('d', 'n', 'e', 't'):
1380 mp_msg(MSGT_DEMUX
,MSGL_V
,"Audio: DNET -> AC3\n");
1381 // sh->format = 0x2000;
1383 case MKTAG('1', '4', '_', '4'):
1384 sh
->wf
->nBlockAlign
= 0x14;
1387 case MKTAG('2', '8', '_', '8'):
1388 sh
->wf
->nBlockAlign
= coded_frame_size
;
1391 case MKTAG('s', 'i', 'p', 'r'):
1392 case MKTAG('a', 't', 'r', 'c'):
1393 case MKTAG('c', 'o', 'o', 'k'):
1394 // realaudio codec plugins - common:
1395 stream_skip(demuxer
->stream
,3); // Skip 3 unknown bytes
1397 stream_skip(demuxer
->stream
,1); // Skip 1 additional unknown byte
1398 codecdata_length
=stream_read_dword(demuxer
->stream
);
1399 // Check extradata len, we can't store bigger values in cbSize anyway
1400 if ((unsigned)codecdata_length
> 0xffff) {
1401 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"Extradata too big (%d)\n", codecdata_length
);
1402 goto skip_this_chunk
;
1404 sh
->wf
->cbSize
= codecdata_length
;
1405 sh
->wf
= realloc(sh
->wf
, sizeof(WAVEFORMATEX
)+sh
->wf
->cbSize
);
1406 stream_read(demuxer
->stream
, ((char*)(sh
->wf
+1)), codecdata_length
); // extras
1407 if (priv
->intl_id
[stream_id
] == MKTAG('g', 'e', 'n', 'r'))
1408 sh
->wf
->nBlockAlign
= sub_packet_size
;
1410 sh
->wf
->nBlockAlign
= coded_frame_size
;
1414 case MKTAG('r', 'a', 'a', 'c'):
1415 case MKTAG('r', 'a', 'c', 'p'):
1416 /* This is just AAC. The two or five bytes of */
1417 /* config data needed for libfaad are stored */
1418 /* after the audio headers. */
1419 stream_skip(demuxer
->stream
,3); // Skip 3 unknown bytes
1421 stream_skip(demuxer
->stream
,1); // Skip 1 additional unknown byte
1422 codecdata_length
=stream_read_dword(demuxer
->stream
);
1423 if (codecdata_length
>=1) {
1424 sh
->codecdata_len
= codecdata_length
- 1;
1425 sh
->codecdata
= calloc(sh
->codecdata_len
, 1);
1426 stream_skip(demuxer
->stream
, 1);
1427 stream_read(demuxer
->stream
, sh
->codecdata
, sh
->codecdata_len
);
1429 sh
->format
= mmioFOURCC('M', 'P', '4', 'A');
1432 mp_msg(MSGT_DEMUX
,MSGL_V
,"Audio: Unknown (%s)\n", buf
);
1435 // Interleaver setup
1436 priv
->sub_packet_size
[stream_id
] = sub_packet_size
;
1437 priv
->sub_packet_h
[stream_id
] = sub_packet_h
;
1438 priv
->coded_framesize
[stream_id
] = coded_frame_size
;
1439 priv
->audiopk_size
[stream_id
] = frame_size
;
1441 sh
->wf
->wFormatTag
= sh
->format
;
1443 mp_msg(MSGT_DEMUX
,MSGL_V
,"audio fourcc: %.4s (%x)\n", (char *)&sh
->format
, sh
->format
);
1444 if ( mp_msg_test(MSGT_DEMUX
,MSGL_V
) )
1445 print_wave_header(sh
->wf
, MSGL_V
);
1447 /* Select audio stream with highest bitrate if multirate file*/
1448 if (priv
->is_multirate
&& ((demuxer
->audio
->id
== -1) ||
1449 ((demuxer
->audio
->id
>= 0) && priv
->a_bitrate
&& (bitrate
> priv
->a_bitrate
)))) {
1450 demuxer
->audio
->id
= stream_id
;
1451 priv
->a_bitrate
= bitrate
;
1452 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"Multirate autoselected audio id %d with bitrate %d\n", stream_id
, bitrate
);
1455 if(demuxer
->audio
->id
==stream_id
){
1456 sh
->ds
=demuxer
->audio
;
1457 demuxer
->audio
->sh
=sh
;
1458 priv
->audio_buf
= calloc(priv
->sub_packet_h
[demuxer
->audio
->id
], priv
->audiopk_size
[demuxer
->audio
->id
]);
1459 priv
->audio_timestamp
= calloc(priv
->sub_packet_h
[demuxer
->audio
->id
], sizeof(double));
1468 } else if (strstr(mimet
,"X-MP3-draft-00")) {
1469 sh_audio_t
*sh
= new_sh_audio(demuxer
, stream_id
);
1470 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_AudioID
, "real", stream_id
);
1472 /* Emulate WAVEFORMATEX struct: */
1473 sh
->wf
= malloc(sizeof(WAVEFORMATEX
));
1474 memset(sh
->wf
, 0, sizeof(WAVEFORMATEX
));
1475 sh
->wf
->nChannels
= 0;//sh->channels;
1476 sh
->wf
->wBitsPerSample
= 16;
1477 sh
->wf
->nSamplesPerSec
= 0;//sh->samplerate;
1478 sh
->wf
->nAvgBytesPerSec
= 0;//bitrate;
1479 sh
->wf
->nBlockAlign
= 0;//frame_size;
1481 sh
->wf
->wFormatTag
= sh
->format
= mmioFOURCC('a','d','u',0x55);
1483 if(demuxer
->audio
->id
==stream_id
){
1484 sh
->ds
=demuxer
->audio
;
1485 demuxer
->audio
->sh
=sh
;
1489 } else if (strstr(mimet
,"x-ralf-mpeg4")) {
1490 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"Real lossless audio not supported yet\n");
1491 } else if (strstr(mimet
,"x-pn-encrypted-ra")) {
1492 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"Encrypted audio is not supported\n");
1494 mp_msg(MSGT_DEMUX
,MSGL_V
,"Unknown audio stream format\n");
1496 } else if (!strncmp(mimet
,"video/",6)) {
1497 if (strstr(mimet
,"x-pn-realvideo") || strstr(mimet
,"x-pn-multirate-realvideo")) {
1498 stream_skip(demuxer
->stream
, 4); // VIDO length, same as codec_data_size
1499 tmp
= stream_read_dword(demuxer
->stream
);
1500 if(tmp
!= MKTAG('O', 'D', 'I', 'V'))
1502 mp_msg(MSGT_DEMUX
,MSGL_V
,"Video: can't find VIDO in codec data\n");
1505 sh_video_t
*sh
= new_sh_video(demuxer
, stream_id
);
1506 mp_msg(MSGT_DEMUX
, MSGL_INFO
, MSGTR_VideoID
, "real", stream_id
);
1508 sh
->format
= stream_read_dword_le(demuxer
->stream
); /* fourcc */
1509 mp_msg(MSGT_DEMUX
,MSGL_V
,"video fourcc: %.4s (%x)\n", (char *)&sh
->format
, sh
->format
);
1511 /* emulate BITMAPINFOHEADER */
1512 sh
->bih
= malloc(sizeof(BITMAPINFOHEADER
));
1513 memset(sh
->bih
, 0, sizeof(BITMAPINFOHEADER
));
1514 sh
->bih
->biSize
= sizeof(BITMAPINFOHEADER
);
1515 sh
->disp_w
= sh
->bih
->biWidth
= stream_read_word(demuxer
->stream
);
1516 sh
->disp_h
= sh
->bih
->biHeight
= stream_read_word(demuxer
->stream
);
1517 sh
->bih
->biPlanes
= 1;
1518 sh
->bih
->biBitCount
= 24;
1519 sh
->bih
->biCompression
= sh
->format
;
1520 sh
->bih
->biSizeImage
= sh
->bih
->biWidth
*sh
->bih
->biHeight
*3;
1522 sh
->fps
= (float) stream_read_word(demuxer
->stream
);
1523 if (sh
->fps
<=0) sh
->fps
=24; // we probably won't even care about fps
1524 sh
->frametime
= 1.0f
/sh
->fps
;
1527 stream_skip(demuxer
->stream
, 4);
1529 mp_msg(MSGT_DEMUX
, MSGL_V
,"unknown1: 0x%X \n",stream_read_dword(demuxer
->stream
));
1530 mp_msg(MSGT_DEMUX
, MSGL_V
,"unknown2: 0x%X \n",stream_read_word(demuxer
->stream
));
1531 mp_msg(MSGT_DEMUX
, MSGL_V
,"unknown3: 0x%X \n",stream_read_word(demuxer
->stream
));
1533 // if(sh->format==0x30335652 || sh->format==0x30325652 )
1536 int tmp
=stream_read_word(demuxer
->stream
);
1538 sh
->fps
=tmp
; sh
->frametime
= 1.0f
/sh
->fps
;
1541 int fps
=stream_read_word(demuxer
->stream
);
1542 mp_msg(MSGT_DEMUX
, MSGL_WARN
,"realvid: ignoring FPS = %d\n",fps
);
1544 stream_skip(demuxer
->stream
, 2);
1547 // read and store codec extradata
1548 unsigned int cnt
= codec_data_size
- (stream_tell(demuxer
->stream
) - codec_pos
);
1549 if (cnt
> 0x7fffffff - sizeof(BITMAPINFOHEADER
)) {
1550 mp_msg(MSGT_DEMUX
, MSGL_ERR
,"Extradata too big (%u)\n", cnt
);
1552 sh
->bih
= realloc(sh
->bih
, sizeof(BITMAPINFOHEADER
) + cnt
);
1553 sh
->bih
->biSize
+= cnt
;
1554 stream_read(demuxer
->stream
, ((unsigned char*)(sh
->bih
+1)), cnt
);
1557 if(sh
->format
== 0x30315652 && ((unsigned char*)(sh
->bih
+1))[6] == 0x30)
1558 sh
->bih
->biCompression
= sh
->format
= mmioFOURCC('R', 'V', '1', '3');
1560 /* Select video stream with highest bitrate if multirate file*/
1561 if (priv
->is_multirate
&& ((demuxer
->video
->id
== -1) ||
1562 ((demuxer
->video
->id
>= 0) && priv
->v_bitrate
&& (bitrate
> priv
->v_bitrate
)))) {
1563 demuxer
->video
->id
= stream_id
;
1564 priv
->v_bitrate
= bitrate
;
1565 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"Multirate autoselected video id %d with bitrate %d\n", stream_id
, bitrate
);
1568 if(demuxer
->video
->id
==stream_id
){
1569 sh
->ds
=demuxer
->video
;
1570 demuxer
->video
->sh
=sh
;
1577 mp_msg(MSGT_DEMUX
,MSGL_V
,"Unknown video stream format\n");
1579 } else if (strstr(mimet
,"logical-")) {
1580 if (strstr(mimet
,"fileinfo")) {
1581 mp_msg(MSGT_DEMUX
,MSGL_V
,"Got a logical-fileinfo chunk\n");
1582 } else if (strstr(mimet
,"-audio") || strstr(mimet
,"-video")) {
1584 int stream_list
[MAX_STREAMS
];
1586 priv
->is_multirate
= 1;
1587 stream_skip(demuxer
->stream
, 4); // Length of codec data (repeated)
1588 stream_cnt
= stream_read_dword(demuxer
->stream
); // Get number of audio or video streams
1589 if ((unsigned)stream_cnt
>= MAX_STREAMS
) {
1590 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"Too many streams in %s. Big troubles ahead.\n", mimet
);
1591 goto skip_this_chunk
;
1593 for (i
= 0; i
< stream_cnt
; i
++)
1594 stream_list
[i
] = stream_read_word(demuxer
->stream
);
1595 for (i
= 0; i
< stream_cnt
; i
++)
1596 if ((unsigned)stream_list
[i
] >= MAX_STREAMS
) {
1597 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"Stream id out of range: %d. Ignored.\n", stream_list
[i
]);
1598 stream_skip(demuxer
->stream
, 4); // Skip DATA offset for broken stream
1600 priv
->str_data_offset
[stream_list
[i
]] = stream_read_dword(demuxer
->stream
);
1601 mp_msg(MSGT_DEMUX
,MSGL_V
,"Stream %d with DATA offset 0x%08x\n", stream_list
[i
], priv
->str_data_offset
[stream_list
[i
]]);
1603 // Skip the rest of this chunk
1605 mp_msg(MSGT_DEMUX
,MSGL_V
,"Unknown logical stream\n");
1608 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Not audio/video stream or unsupported!\n");
1613 /* skip codec info */
1614 tmp
= stream_tell(demuxer
->stream
) - codec_pos
;
1615 mp_msg(MSGT_DEMUX
,MSGL_V
,"### skipping %d bytes of codec info\n", codec_data_size
- tmp
);
1618 for(i
=0;i
<codec_data_size
- tmp
;i
++)
1619 mp_msg(MSGT_DEMUX
, MSGL_V
," %02X",stream_read_char(demuxer
->stream
));
1620 mp_msg(MSGT_DEMUX
, MSGL_V
,"\n");
1623 stream_skip(demuxer
->stream
, codec_data_size
- tmp
);
1630 case MKTAG('D', 'A', 'T', 'A'):
1632 case MKTAG('I', 'N', 'D', 'X'):
1634 mp_msg(MSGT_DEMUX
,MSGL_V
,"Unknown chunk: %x\n", chunk_id
);
1635 stream_skip(demuxer
->stream
, chunk_size
- 10);
1641 if(priv
->is_multirate
) {
1642 mp_msg(MSGT_DEMUX
,MSGL_V
,"Selected video id %d audio id %d\n", demuxer
->video
->id
, demuxer
->audio
->id
);
1643 /* Perform some sanity checks to avoid checking streams id all over the code*/
1644 if (demuxer
->audio
->id
>= MAX_STREAMS
) {
1645 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"Invalid audio stream %d. No sound will be played.\n", demuxer
->audio
->id
);
1646 demuxer
->audio
->id
= -2;
1647 } else if ((demuxer
->audio
->id
>= 0) && (priv
->str_data_offset
[demuxer
->audio
->id
] == 0)) {
1648 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"Audio stream %d not found. No sound will be played.\n", demuxer
->audio
->id
);
1649 demuxer
->audio
->id
= -2;
1651 if (demuxer
->video
->id
>= MAX_STREAMS
) {
1652 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"Invalid video stream %d. No video will be played.\n", demuxer
->video
->id
);
1653 demuxer
->video
->id
= -2;
1654 } else if ((demuxer
->video
->id
>= 0) && (priv
->str_data_offset
[demuxer
->video
->id
] == 0)) {
1655 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"Video stream %d not found. No video will be played.\n", demuxer
->video
->id
);
1656 demuxer
->video
->id
= -2;
1660 if(priv
->is_multirate
&& ((demuxer
->video
->id
>= 0) || (demuxer
->audio
->id
>=0))) {
1661 /* If audio or video only, seek to right place and behave like standard file */
1662 if (demuxer
->video
->id
< 0) {
1663 // Stream is audio only, or -novideo
1664 stream_seek(demuxer
->stream
, priv
->data_chunk_offset
= priv
->str_data_offset
[demuxer
->audio
->id
]+10);
1665 priv
->is_multirate
= 0;
1667 if (demuxer
->audio
->id
< 0) {
1668 // Stream is video only, or -nosound
1669 stream_seek(demuxer
->stream
, priv
->data_chunk_offset
= priv
->str_data_offset
[demuxer
->video
->id
]+10);
1670 priv
->is_multirate
= 0;
1674 if(!priv
->is_multirate
) {
1675 // printf("i=%d num_of_headers=%d \n",i,num_of_headers);
1676 priv
->num_of_packets
= stream_read_dword(demuxer
->stream
);
1677 stream_skip(demuxer
->stream
, 4); /* next data header */
1679 mp_msg(MSGT_DEMUX
,MSGL_V
,"Packets in file: %d\n", priv
->num_of_packets
);
1681 if (priv
->num_of_packets
== 0)
1682 priv
->num_of_packets
= -10;
1684 priv
->audio_curpos
= priv
->str_data_offset
[demuxer
->audio
->id
] + 18;
1685 stream_seek(demuxer
->stream
, priv
->str_data_offset
[demuxer
->audio
->id
]+10);
1686 priv
->a_num_of_packets
=priv
->a_num_of_packets
= stream_read_dword(demuxer
->stream
);
1687 priv
->video_curpos
= priv
->str_data_offset
[demuxer
->video
->id
] + 18;
1688 stream_seek(demuxer
->stream
, priv
->str_data_offset
[demuxer
->video
->id
]+10);
1689 priv
->v_num_of_packets
= stream_read_dword(demuxer
->stream
);
1690 priv
->stream_switch
= 1;
1691 /* Index required for multirate playback, force building if it's not there */
1692 /* but respect user request to force index regeneration */
1693 if (index_mode
== -1)
1698 priv
->audio_need_keyframe
= 0;
1699 priv
->video_after_seek
= 0;
1701 switch (index_mode
){
1702 case -1: // untouched
1703 if (priv
->index_chunk_offset
&& parse_index_chunk(demuxer
))
1705 demuxer
->seekable
= 1;
1708 case 1: // use (generate index)
1709 if (priv
->index_chunk_offset
&& parse_index_chunk(demuxer
))
1711 demuxer
->seekable
= 1;
1713 generate_index(demuxer
);
1714 demuxer
->seekable
= 1;
1717 case 2: // force generating index
1718 generate_index(demuxer
);
1719 demuxer
->seekable
= 1;
1721 default: // do nothing
1726 if(demuxer
->video
->id
==-1 && v_streams
>0){
1727 // find the valid video stream:
1728 if(!ds_fill_buffer(demuxer
->video
)){
1729 mp_msg(MSGT_DEMUXER
,MSGL_INFO
,"RM: " MSGTR_MissingVideoStream
);
1732 if(demuxer
->audio
->id
==-1 && a_streams
>0){
1733 // find the valid audio stream:
1734 if(!ds_fill_buffer(demuxer
->audio
)){
1735 mp_msg(MSGT_DEMUXER
,MSGL_INFO
,"RM: " MSGTR_MissingAudioStream
);
1739 if(demuxer
->video
->sh
){
1740 sh_video_t
*sh
=demuxer
->video
->sh
;
1741 mp_msg(MSGT_DEMUX
,MSGL_V
,"VIDEO: %.4s [%08X,%08X] %dx%d (aspect %4.2f) %4.2f fps\n",
1742 (char *)&sh
->format
,((unsigned int*)(sh
->bih
+1))[1],((unsigned int*)(sh
->bih
+1))[0],
1743 sh
->disp_w
,sh
->disp_h
,sh
->aspect
,sh
->fps
);
1746 if(demuxer
->audio
->sh
){
1747 sh_audio_t
*sh
=demuxer
->audio
->sh
;
1748 mp_msg(MSGT_DEMUX
,MSGL_V
,"AUDIO: %.4s [%08X]\n",
1749 (char *)&sh
->format
,sh
->format
);
1755 static void demux_close_real(demuxer_t
*demuxer
)
1758 real_priv_t
* priv
= demuxer
->priv
;
1761 for(i
=0; i
<MAX_STREAMS
; i
++)
1762 if(priv
->index_table
[i
])
1763 free(priv
->index_table
[i
]);
1764 if (priv
->audio_buf
)
1765 free(priv
->audio_buf
);
1766 if (priv
->audio_timestamp
)
1767 free(priv
->audio_timestamp
);
1774 /* please upload RV10 samples WITH INDEX CHUNK */
1775 static void demux_seek_real(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
)
1777 real_priv_t
*priv
= demuxer
->priv
;
1778 demux_stream_t
*d_audio
= demuxer
->audio
;
1779 demux_stream_t
*d_video
= demuxer
->video
;
1780 sh_audio_t
*sh_audio
= d_audio
->sh
;
1781 sh_video_t
*sh_video
= d_video
->sh
;
1782 int vid
= d_video
->id
, aid
= d_audio
->id
;
1783 int next_offset
= 0;
1784 int64_t cur_timestamp
= 0;
1789 if (sh_video
&& (unsigned)vid
< MAX_STREAMS
&& priv
->index_table_size
[vid
])
1791 if (sh_audio
&& (unsigned)aid
< MAX_STREAMS
&& priv
->index_table_size
[aid
])
1794 // printf("streams: %d\n", streams);
1799 if (flags
& SEEK_ABSOLUTE
)
1800 priv
->current_apacket
= priv
->current_vpacket
= 0;
1801 if (flags
& SEEK_FACTOR
)
1802 rel_seek_secs
*= priv
->duration
;
1804 if ((streams
& 1) && priv
->current_vpacket
>= priv
->index_table_size
[vid
])
1805 priv
->current_vpacket
= priv
->index_table_size
[vid
] - 1;
1806 if ((streams
& 2) && priv
->current_apacket
>= priv
->index_table_size
[aid
])
1807 priv
->current_apacket
= priv
->index_table_size
[aid
] - 1;
1809 // if (index_mode == 1 || index_mode == 2) {
1810 if (streams
& 1) {// use the video index if we have one
1811 cur_timestamp
= priv
->index_table
[vid
][priv
->current_vpacket
].timestamp
;
1812 if (rel_seek_secs
> 0)
1813 while ((priv
->index_table
[vid
][priv
->current_vpacket
].timestamp
- cur_timestamp
) < rel_seek_secs
* 1000){
1814 priv
->current_vpacket
+= 1;
1815 if (priv
->current_vpacket
>= priv
->index_table_size
[vid
]) {
1816 priv
->current_vpacket
= priv
->index_table_size
[vid
] - 1;
1818 stream_seek(demuxer
->stream
, priv
->index_table
[vid
][priv
->current_vpacket
].offset
);
1819 add_index_segment(demuxer
, vid
, cur_timestamp
+ rel_seek_secs
* 1000);
1826 else if (rel_seek_secs
< 0) {
1827 while ((cur_timestamp
- priv
->index_table
[vid
][priv
->current_vpacket
].timestamp
) < - rel_seek_secs
* 1000){
1828 priv
->current_vpacket
-= 1;
1829 if (priv
->current_vpacket
< 0) {
1830 priv
->current_vpacket
= 0;
1835 priv
->video_curpos
= priv
->index_table
[vid
][priv
->current_vpacket
].offset
;
1836 priv
->audio_need_keyframe
= !priv
->is_multirate
;
1837 priv
->video_after_seek
= 1;
1840 if (!(streams
& 1)) {
1842 priv
->index_table
[aid
][priv
->current_apacket
].timestamp
;
1844 if (rel_seek_secs
> 0)
1845 while ((priv
->index_table
[aid
][priv
->current_apacket
].timestamp
- cur_timestamp
) < rel_seek_secs
* 1000){
1846 priv
->current_apacket
+= 1;
1847 if (priv
->current_apacket
>= priv
->index_table_size
[aid
]) {
1848 priv
->current_apacket
= priv
->index_table_size
[aid
] - 1;
1852 else if (rel_seek_secs
< 0)
1853 while ((cur_timestamp
- priv
->index_table
[aid
][priv
->current_apacket
].timestamp
) < - rel_seek_secs
* 1000){
1854 priv
->current_apacket
-= 1;
1855 if (priv
->current_apacket
< 0) {
1856 priv
->current_apacket
= 0;
1860 priv
->audio_curpos
= priv
->index_table
[aid
][priv
->current_apacket
].offset
;
1863 next_offset
= streams
& 1 ? priv
->video_curpos
: priv
->audio_curpos
;
1864 // printf("seek: pos: %d, current packets: a: %d, v: %d\n",
1865 // next_offset, priv->current_apacket, priv->current_vpacket);
1867 stream_seek(demuxer
->stream
, next_offset
);
1869 demux_real_fill_buffer(demuxer
, NULL
);
1872 static int demux_real_control(demuxer_t
*demuxer
, int cmd
, void *arg
)
1874 real_priv_t
*priv
= demuxer
->priv
;
1875 unsigned int lastpts
= priv
->v_pts
? priv
->v_pts
: priv
->a_pts
;
1878 case DEMUXER_CTRL_GET_TIME_LENGTH
:
1879 if (priv
->duration
== 0)
1880 return DEMUXER_CTRL_DONTKNOW
;
1882 *((double *)arg
) = (double)priv
->duration
;
1883 return DEMUXER_CTRL_OK
;
1885 case DEMUXER_CTRL_GET_PERCENT_POS
:
1886 if (priv
->duration
== 0)
1887 return DEMUXER_CTRL_DONTKNOW
;
1889 *((int *)arg
) = (int)(100 * lastpts
/ priv
->duration
);
1890 return DEMUXER_CTRL_OK
;
1893 return DEMUXER_CTRL_NOTIMPL
;
1898 const demuxer_desc_t demuxer_desc_real
= {
1899 "Realmedia demuxer",
1902 "Alex Beregszasi, Florian Schneider, A'rpi, Roberto Togni",
1903 "handles new .RMF files",
1905 1, // safe autodetect
1907 demux_real_fill_buffer
,