add kega video binary codec for fourcc kgv1
[mplayer/glamo.git] / libmpdemux / demux_real.c
blobde6ff785ee85ea1573036322300487a3654ca2a1
1 /*
2 * Real parser & demuxer
3 * copyright (C) 2001 Alex Beregszaszi
4 * copyright (C) 2005, 2006 Roberto Togni
5 * based on FFmpeg's libav/rm.c
7 * audio codecs: (supported by RealPlayer8 for Linux)
8 * DNET - RealAudio 3.0, really it's AC3 in swapped-byteorder
9 * SIPR - SiproLab's audio codec, ACELP decoder working with MPlayer,
10 * needs fine-tuning too :)
11 * ATRC - RealAudio 8 (ATRAC3) - www.minidisc.org/atrac3_article.pdf,
12 * ACM decoder uploaded, needs some fine-tuning to work
13 * -> RealAudio 8
14 * COOK/COKR - Real Cooker -> RealAudio G2
16 * video codecs: (supported by RealPlayer8 for Linux)
17 * RV10 - H.263 based, working with libavcodec's decoder
18 * RV20-RV40 - using RealPlayer's codec plugins
20 * This file is part of MPlayer.
22 * MPlayer is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * MPlayer is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License along
33 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
34 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <inttypes.h>
42 #include "config.h"
43 #include "mp_msg.h"
44 #include "help_mp.h"
45 #include "mpbswap.h"
47 #include "stream/stream.h"
48 #include "demuxer.h"
49 #include "stheader.h"
51 //#define mp_dbg(mod,lev, args... ) mp_msg_c((mod<<8)|lev, ## args )
53 #define MKTAG(a, b, c, d) (a | (b << 8) | (c << 16) | (d << 24))
55 #define MAX_STREAMS 32
57 static unsigned char sipr_swaps[38][2]={
58 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
59 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
60 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
61 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
62 {77,80} };
64 typedef struct {
65 unsigned int timestamp;
66 int offset;
67 // int packetno;
68 // int len; /* only filled by our index generator */
69 // int flags; /* only filled by our index generator */
70 } real_index_table_t;
72 typedef struct {
73 /* for seeking */
74 int index_chunk_offset;
75 real_index_table_t *index_table[MAX_STREAMS];
77 // int *index_table[MAX_STREAMS];
78 int index_table_size[MAX_STREAMS];
79 int index_malloc_size[MAX_STREAMS];
80 int data_chunk_offset;
81 int num_of_packets;
82 int current_packet;
84 // need for seek
85 int audio_need_keyframe;
86 int video_after_seek;
88 int current_apacket;
89 int current_vpacket;
91 // timestamp correction:
92 int64_t kf_base;// timestamp of the prev. video keyframe
93 unsigned int kf_pts; // timestamp of next video keyframe
94 unsigned int a_pts; // previous audio timestamp
95 double v_pts; // previous video timestamp
96 unsigned long duration;
98 /* stream id table */
99 // int last_a_stream;
100 // int a_streams[MAX_STREAMS];
101 // int last_v_stream;
102 // int v_streams[MAX_STREAMS];
105 * Used to demux multirate files
107 int is_multirate; ///< != 0 for multirate files
108 int str_data_offset[MAX_STREAMS]; ///< Data chunk offset for every audio/video stream
109 int audio_curpos; ///< Current file position for audio demuxing
110 int video_curpos; ///< Current file position for video demuxing
111 int a_num_of_packets; ///< Number of audio packets
112 int v_num_of_packets; ///< Number of video packets
113 int a_bitrate; ///< Audio bitrate
114 int v_bitrate; ///< Video bitrate
115 int stream_switch; ///< Flag used to switch audio/video demuxing
118 * Used to reorder audio data
120 unsigned int intl_id[MAX_STREAMS]; ///< interleaver id, per stream
121 int sub_packet_size[MAX_STREAMS]; ///< sub packet size, per stream
122 int sub_packet_h[MAX_STREAMS]; ///< number of coded frames per block
123 int coded_framesize[MAX_STREAMS]; ///< coded frame size, per stream
124 int audiopk_size[MAX_STREAMS]; ///< audio packet size
125 unsigned char *audio_buf; ///< place to store reordered audio data
126 double *audio_timestamp; ///< timestamp for each audio packet
127 int sub_packet_cnt; ///< number of subpacket already received
128 int audio_filepos; ///< file position of first audio packet in block
129 } real_priv_t;
131 //! use at most 200 MB of memory for index, corresponds to around 25 million entries
132 #define MAX_INDEX_ENTRIES (200*1024*1024 / sizeof(real_index_table_t))
134 /* originally from FFmpeg */
135 static void get_str(int isbyte, demuxer_t *demuxer, char *buf, int buf_size)
137 int len;
139 if (isbyte)
140 len = stream_read_char(demuxer->stream);
141 else
142 len = stream_read_word(demuxer->stream);
144 stream_read(demuxer->stream, buf, (len > buf_size) ? buf_size : len);
145 if (len > buf_size)
146 stream_skip(demuxer->stream, len-buf_size);
148 mp_msg(MSGT_DEMUX, MSGL_V, "read_str: %d bytes read\n", len);
151 static void dump_index(demuxer_t *demuxer, int stream_id)
153 real_priv_t *priv = demuxer->priv;
154 real_index_table_t *index;
155 int i, entries;
157 if (!mp_msg_test(MSGT_DEMUX,MSGL_V))
158 return;
160 if ((unsigned)stream_id >= MAX_STREAMS)
161 return;
163 index = priv->index_table[stream_id];
164 entries = priv->index_table_size[stream_id];
166 mp_msg(MSGT_DEMUX, MSGL_V, "Index table for stream %d\n", stream_id);
167 for (i = 0; i < entries; i++)
169 #if 1
170 mp_msg(MSGT_DEMUX, MSGL_V,"i: %d, pos: %d, timestamp: %u\n", i, index[i].offset, index[i].timestamp);
171 #else
172 mp_msg(MSGT_DEMUX, MSGL_V,"packetno: %x pos: %x len: %x timestamp: %x flags: %x\n",
173 index[i].packetno, index[i].offset, index[i].len, index[i].timestamp,
174 index[i].flags);
175 #endif
179 static int parse_index_chunk(demuxer_t *demuxer)
181 real_priv_t *priv = demuxer->priv;
182 int origpos = stream_tell(demuxer->stream);
183 int next_header_pos = priv->index_chunk_offset;
184 int i, entries, stream_id;
186 read_index:
187 stream_seek(demuxer->stream, next_header_pos);
189 i = stream_read_dword_le(demuxer->stream);
190 if ((i == -256) || (i != MKTAG('I', 'N', 'D', 'X')))
192 mp_msg(MSGT_DEMUX, MSGL_WARN,"Something went wrong, no index chunk found on given address (%d)\n",
193 next_header_pos);
194 index_mode = -1;
195 if (i == -256)
196 stream_reset(demuxer->stream);
197 stream_seek(demuxer->stream, origpos);
198 return 0;
199 //goto end;
202 mp_msg(MSGT_DEMUX, MSGL_V,"Reading index table from index chunk (%d)\n",
203 next_header_pos);
205 i = stream_read_dword(demuxer->stream);
206 mp_msg(MSGT_DEMUX, MSGL_V,"size: %d bytes\n", i);
208 i = stream_read_word(demuxer->stream);
209 if (i != 0)
210 mp_msg(MSGT_DEMUX, MSGL_WARN,"Hmm, index table with unknown version (%d), please report it to MPlayer developers!\n", i);
212 entries = stream_read_dword(demuxer->stream);
213 mp_msg(MSGT_DEMUX, MSGL_V,"entries: %d\n", entries);
215 stream_id = stream_read_word(demuxer->stream);
216 mp_msg(MSGT_DEMUX, MSGL_V,"stream_id: %d\n", stream_id);
218 next_header_pos = stream_read_dword(demuxer->stream);
219 mp_msg(MSGT_DEMUX, MSGL_V,"next_header_pos: %d\n", next_header_pos);
220 if (entries <= 0 || entries > MAX_INDEX_ENTRIES)
222 if (next_header_pos)
223 goto read_index;
224 i = entries;
225 goto end;
228 priv->index_table_size[stream_id] = entries;
229 priv->index_table[stream_id] = calloc(priv->index_table_size[stream_id], sizeof(real_index_table_t));
231 for (i = 0; i < entries; i++)
233 stream_skip(demuxer->stream, 2); /* version */
234 priv->index_table[stream_id][i].timestamp = stream_read_dword(demuxer->stream);
235 priv->index_table[stream_id][i].offset = stream_read_dword(demuxer->stream);
236 stream_skip(demuxer->stream, 4); /* packetno */
237 // priv->index_table[stream_id][i].packetno = stream_read_dword(demuxer->stream);
238 // printf("Index table: Stream#%d: entry: %d: pos: %d\n",
239 // stream_id, i, priv->index_table[stream_id][i].offset);
242 dump_index(demuxer, stream_id);
244 if (next_header_pos > 0)
245 goto read_index;
247 end:
248 if (i == -256)
249 stream_reset(demuxer->stream);
250 stream_seek(demuxer->stream, origpos);
251 if (i == -256)
252 return 0;
253 else
254 return 1;
257 #if 1
259 static void add_index_item(demuxer_t *demuxer, int stream_id, unsigned int timestamp, int offset)
261 if ((unsigned)stream_id < MAX_STREAMS)
263 real_priv_t *priv = demuxer->priv;
264 real_index_table_t *index;
265 if (priv->index_table_size[stream_id] >= MAX_INDEX_ENTRIES) {
266 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Index too large during building\n");
267 return;
269 if (priv->index_table_size[stream_id] >= priv->index_malloc_size[stream_id])
271 if (priv->index_malloc_size[stream_id] == 0)
272 priv->index_malloc_size[stream_id] = 2048;
273 else
274 priv->index_malloc_size[stream_id] += priv->index_malloc_size[stream_id] / 2;
275 // in case we have a really large chunk...
276 if (priv->index_table_size[stream_id] >=
277 priv->index_malloc_size[stream_id])
278 priv->index_malloc_size[stream_id] =
279 priv->index_table_size[stream_id] + 1;
280 priv->index_table[stream_id] = realloc(priv->index_table[stream_id], priv->index_malloc_size[stream_id]*sizeof(priv->index_table[0][0]));
282 if (priv->index_table_size[stream_id] > 0)
284 index = &priv->index_table[stream_id][priv->index_table_size[stream_id] - 1];
285 if (index->timestamp >= timestamp || index->offset >= offset)
286 return;
288 index = &priv->index_table[stream_id][priv->index_table_size[stream_id]++];
289 index->timestamp = timestamp;
290 index->offset = offset;
294 static void add_index_segment(demuxer_t *demuxer, int seek_stream_id, int64_t seek_timestamp)
296 int tag, len, stream_id, flags;
297 unsigned int timestamp;
298 if (seek_timestamp != -1 && (unsigned)seek_stream_id >= MAX_STREAMS)
299 return;
300 while (1)
302 demuxer->filepos = stream_tell(demuxer->stream);
304 tag = stream_read_dword(demuxer->stream);
305 if (tag == MKTAG('A', 'T', 'A', 'D'))
307 stream_skip(demuxer->stream, 14);
308 continue; /* skip to next loop */
310 len = tag & 0xffff;
311 if (tag == -256 || len < 12)
312 break;
314 stream_id = stream_read_word(demuxer->stream);
315 timestamp = stream_read_dword(demuxer->stream);
317 stream_skip(demuxer->stream, 1); /* reserved */
318 flags = stream_read_char(demuxer->stream);
320 if (flags == -256)
321 break;
323 if (flags & 2)
325 add_index_item(demuxer, stream_id, timestamp, demuxer->filepos);
326 if (stream_id == seek_stream_id && timestamp >= seek_timestamp)
328 stream_seek(demuxer->stream, demuxer->filepos);
329 return;
332 // 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);
333 /* skip data */
334 stream_skip(demuxer->stream, len-12);
338 static int generate_index(demuxer_t *demuxer)
340 real_priv_t *priv = demuxer->priv;
341 int origpos = stream_tell(demuxer->stream);
342 int data_pos = priv->data_chunk_offset-10;
343 int i;
344 int tag;
346 stream_seek(demuxer->stream, data_pos);
347 tag = stream_read_dword(demuxer->stream);
348 if (tag != MKTAG('A', 'T', 'A', 'D'))
350 mp_msg(MSGT_DEMUX, MSGL_WARN,"Something went wrong, no data chunk found on given address (%d)\n", data_pos);
352 else
354 stream_skip(demuxer->stream, 14);
355 add_index_segment(demuxer, -1, -1);
357 for (i = 0; i < MAX_STREAMS; i++)
359 if (priv->index_table_size[i] > 0)
361 dump_index(demuxer, i);
364 stream_reset(demuxer->stream);
365 stream_seek(demuxer->stream, origpos);
366 return 0;
369 #else
371 static int generate_index(demuxer_t *demuxer)
373 real_priv_t *priv = demuxer->priv;
374 int origpos = stream_tell(demuxer->stream);
375 int data_pos = priv->data_chunk_offset-10;
376 int num_of_packets = 0;
377 int i, entries = 0;
378 int len, stream_id = 0, flags;
379 unsigned int timestamp;
380 int tab_pos = 0;
382 read_index:
383 stream_seek(demuxer->stream, data_pos);
385 i = stream_read_dword_le(demuxer->stream);
386 if ((i == -256) || (i != MKTAG('D', 'A', 'T', 'A')))
388 mp_msg(MSGT_DEMUX, MSGL_WARN,"Something went wrong, no data chunk found on given address (%d)\n",
389 data_pos);
390 goto end;
392 stream_skip(demuxer->stream, 4); /* chunk size */
393 stream_skip(demuxer->stream, 2); /* version */
395 num_of_packets = stream_read_dword(demuxer->stream);
396 mp_msg(MSGT_DEMUX, MSGL_V,"Generating index table from raw data (pos: 0x%x) for %d packets\n",
397 data_pos, num_of_packets);
399 data_pos = stream_read_dword_le(demuxer->stream)-10; /* next data chunk */
401 for (i = 0; i < MAX_STREAMS; i++)
403 priv->index_table_size[i] = num_of_packets;
404 priv->index_table[i] = calloc(priv->index_table_size[i], sizeof(real_index_table_t));
405 // priv->index_table[stream_id] = realloc(priv->index_table[stream_id],
406 // priv->index_table_size[stream_id] * sizeof(real_index_table_t));
409 tab_pos = 0;
411 // memset(priv->index_table_size, 0, sizeof(int)*MAX_STREAMS);
412 // memset(priv->index_table, 0, sizeof(real_index_table_t)*MAX_STREAMS);
414 while (tab_pos < num_of_packets)
416 i = stream_read_char(demuxer->stream);
417 if (i == -256)
418 goto end;
419 stream_skip(demuxer->stream, 1);
420 // stream_skip(demuxer->stream, 2); /* version */
422 len = stream_read_word(demuxer->stream);
423 stream_id = stream_read_word(demuxer->stream);
424 timestamp = stream_read_dword(demuxer->stream);
426 stream_skip(demuxer->stream, 1); /* reserved */
427 flags = stream_read_char(demuxer->stream);
429 i = tab_pos;
431 // priv->index_table_size[stream_id] = i;
432 // if (priv->index_table[stream_id] == NULL)
433 // priv->index_table[stream_id] = malloc(priv->index_table_size[stream_id] * sizeof(real_index_table_t));
434 // else
435 // priv->index_table[stream_id] = realloc(priv->index_table[stream_id],
436 // priv->index_table_size[stream_id] * sizeof(real_index_table_t));
438 priv->index_table[stream_id][i].timestamp = timestamp;
439 priv->index_table[stream_id][i].offset = stream_tell(demuxer->stream)-12;
440 priv->index_table[stream_id][i].len = len;
441 priv->index_table[stream_id][i].packetno = entries;
442 priv->index_table[stream_id][i].flags = flags;
444 tab_pos++;
446 /* skip data */
447 stream_skip(demuxer->stream, len-12);
449 dump_index(demuxer, stream_id);
450 if (data_pos)
451 goto read_index;
453 end:
454 if (i == -256)
455 stream_reset(demuxer->stream);
456 stream_seek(demuxer->stream, origpos);
457 if (i == -256)
458 return 0;
459 else
460 return 1;
462 #endif
465 static int real_check_file(demuxer_t* demuxer)
467 real_priv_t *priv;
468 int c;
470 mp_msg(MSGT_DEMUX,MSGL_V,"Checking for REAL\n");
472 c = stream_read_dword_le(demuxer->stream);
473 if (c == -256)
474 return 0; /* EOF */
475 if (c != MKTAG('.', 'R', 'M', 'F'))
476 return 0; /* bad magic */
478 priv = malloc(sizeof(real_priv_t));
479 memset(priv, 0, sizeof(real_priv_t));
480 demuxer->priv = priv;
482 return DEMUXER_TYPE_REAL;
485 void hexdump(char *, unsigned long);
487 #define SKIP_BITS(n) buffer<<=n
488 #define SHOW_BITS(n) ((buffer)>>(32-(n)))
490 double real_fix_timestamp(unsigned char *buf, unsigned int timestamp, unsigned int format, int64_t *kf_base, int *kf_pts, double *pts){
491 double v_pts;
492 unsigned char *s = buf + 1 + (*buf+1)*8;
493 uint32_t buffer= (s[0]<<24) + (s[1]<<16) + (s[2]<<8) + s[3];
494 unsigned int kf=timestamp;
495 int pict_type;
496 unsigned int orig_kf;
498 #if 1
499 if(format==mmioFOURCC('R','V','3','0') || format==mmioFOURCC('R','V','4','0')){
500 if(format==mmioFOURCC('R','V','3','0')){
501 SKIP_BITS(3);
502 pict_type= SHOW_BITS(2);
503 SKIP_BITS(2 + 7);
504 }else{
505 SKIP_BITS(1);
506 pict_type= SHOW_BITS(2);
507 SKIP_BITS(2 + 7 + 3);
509 orig_kf=
510 kf= SHOW_BITS(13); // kf= 2*SHOW_BITS(12);
511 // if(pict_type==0)
512 if(pict_type<=1){
513 // I frame, sync timestamps:
514 *kf_base=(int64_t)timestamp-kf;
515 mp_msg(MSGT_DEMUX, MSGL_DBG2,"\nTS: base=%08"PRIX64"\n",*kf_base);
516 kf=timestamp;
517 } else {
518 // P/B frame, merge timestamps:
519 int64_t tmp=(int64_t)timestamp-*kf_base;
520 kf|=tmp&(~0x1fff); // combine with packet timestamp
521 if(kf<tmp-4096) kf+=8192; else // workaround wrap-around problems
522 if(kf>tmp+4096) kf-=8192;
523 kf+=*kf_base;
525 if(pict_type != 3){ // P || I frame -> swap timestamps
526 unsigned int tmp=kf;
527 kf=*kf_pts;
528 *kf_pts=tmp;
529 // if(kf<=tmp) kf=0;
531 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);
533 #endif
534 v_pts=kf*0.001f;
535 // if(pts && (v_pts<*pts || !kf)) v_pts=*pts+frametime;
536 if(pts) *pts=v_pts;
537 return v_pts;
540 typedef struct dp_hdr_s {
541 uint32_t chunks; // number of chunks
542 uint32_t timestamp; // timestamp from packet header
543 uint32_t len; // length of actual data
544 uint32_t chunktab; // offset to chunk offset array
545 } dp_hdr_t;
547 static void queue_video_packet(real_priv_t *priv, demux_stream_t *ds, demux_packet_t *dp)
549 dp_hdr_t hdr = *(dp_hdr_t*)dp->buffer;
550 unsigned char *tmp = malloc(8*(1+hdr.chunks));
551 memcpy(tmp, dp->buffer+hdr.chunktab, 8*(1+hdr.chunks));
552 memmove(dp->buffer+1+8*(1+hdr.chunks), dp->buffer+sizeof(dp_hdr_t), hdr.len);
553 memcpy(dp->buffer+1, tmp, 8*(1+hdr.chunks));
554 *dp->buffer = (uint8_t)hdr.chunks;
555 free(tmp);
557 if(priv->video_after_seek){
558 priv->kf_base = 0;
559 priv->kf_pts = hdr.timestamp;
560 priv->video_after_seek = 0;
562 if(hdr.len >= 3) /* this check may be useless */
563 dp->pts = real_fix_timestamp(dp->buffer, hdr.timestamp,
564 ((sh_video_t *)ds->sh)->format,
565 &priv->kf_base, &priv->kf_pts,
566 &priv->v_pts);
567 ds_add_packet(ds, dp);
570 // return value:
571 // 0 = EOF or no stream found
572 // 1 = successfully read a packet
573 static int demux_real_fill_buffer(demuxer_t *demuxer, demux_stream_t *dsds)
575 real_priv_t *priv = demuxer->priv;
576 demux_stream_t *ds = NULL;
577 int len;
578 unsigned int timestamp;
579 int stream_id;
580 #ifdef CRACK_MATRIX
581 int i;
582 #endif
583 int flags;
584 int version;
585 int reserved;
586 demux_packet_t *dp;
587 int x, sps, cfs, sph, spc, w;
588 int audioreorder_getnextpk = 0;
590 // Don't demux video if video codec init failed
591 if (demuxer->video->id >= 0 && !demuxer->video->sh)
592 demuxer->video->id = -2;
594 while(!stream_eof(demuxer->stream)){
596 /* Handle audio/video demxing switch for multirate files (non-interleaved) */
597 if (priv->is_multirate && priv->stream_switch) {
598 if (priv->current_apacket >= priv->index_table_size[demuxer->audio->id])
599 demuxer->audio->eof = 1;
600 if (priv->current_vpacket >= priv->index_table_size[demuxer->video->id])
601 demuxer->video->eof = 1;
602 if (demuxer->audio->eof && demuxer->video->eof)
603 return 0;
604 else if (!demuxer->audio->eof && demuxer->video->eof)
605 stream_seek(demuxer->stream, priv->audio_curpos); // Get audio
606 else if (demuxer->audio->eof && !demuxer->video->eof)
607 stream_seek(demuxer->stream, priv->video_curpos); // Get video
608 else if (priv->index_table[demuxer->audio->id][priv->current_apacket].timestamp <
609 priv->index_table[demuxer->video->id][priv->current_vpacket].timestamp)
610 stream_seek(demuxer->stream, priv->audio_curpos); // Get audio
611 else
612 stream_seek(demuxer->stream, priv->video_curpos); // Get video
613 priv->stream_switch = 0;
616 demuxer->filepos = stream_tell(demuxer->stream);
617 version = stream_read_word(demuxer->stream); /* version */
618 len = stream_read_word(demuxer->stream);
619 if ((version==0x4441) && (len==0x5441)) { // new data chunk
620 mp_msg(MSGT_DEMUX,MSGL_INFO,"demux_real: New data chunk is coming!!!\n");
621 if (priv->is_multirate)
622 return 0; // EOF
623 stream_skip(demuxer->stream,14);
624 demuxer->filepos = stream_tell(demuxer->stream);
625 version = stream_read_word(demuxer->stream); /* version */
626 len = stream_read_word(demuxer->stream);
627 } else if ((version == 0x494e) && (len == 0x4458)) {
628 mp_msg(MSGT_DEMUX,MSGL_V,"demux_real: Found INDX chunk. EOF.\n");
629 demuxer->stream->eof=1;
630 return 0;
634 if (len == -256){ /* EOF */
635 // printf("len==-256!\n");
636 return 0;
638 if (len < 12){
639 mp_msg(MSGT_DEMUX, MSGL_V,"%08X: packet v%d len=%d \n",(int)demuxer->filepos,(int)version,(int)len);
640 mp_msg(MSGT_DEMUX, MSGL_WARN,"bad packet len (%d)\n", len);
641 if ((unsigned)demuxer->video->id < MAX_STREAMS) {
642 if (priv->current_vpacket + 1 < priv->index_table_size[demuxer->video->id]) {
643 stream_seek(demuxer->stream, priv->index_table[demuxer->video->id][++priv->current_vpacket].offset);
645 } else if ((unsigned)demuxer->audio->id < MAX_STREAMS) {
646 if (priv->current_apacket + 1 < priv->index_table_size[demuxer->audio->id]) {
647 stream_seek(demuxer->stream, priv->index_table[demuxer->audio->id][++priv->current_apacket].offset);
650 continue; //goto loop;
653 stream_id = stream_read_word(demuxer->stream);
654 timestamp = stream_read_dword(demuxer->stream);
655 reserved = stream_read_char(demuxer->stream);
656 flags = stream_read_char(demuxer->stream);
657 /* flags: */
658 /* 0x1 - reliable */
659 /* 0x2 - keyframe */
661 if (version == 1) {
662 int tmp;
663 tmp = stream_read_char(demuxer->stream);
664 mp_msg(MSGT_DEMUX, MSGL_DBG2,"Version: %d, skipped byte is %d\n", version, tmp);
665 len--;
668 if (flags & 2)
669 add_index_item(demuxer, stream_id, timestamp, demuxer->filepos);
671 // printf("%08X: packet v%d len=%4d id=%d pts=%6d rvd=%d flags=%d \n",
672 // (int)demuxer->filepos,(int)version,(int)len, stream_id,
673 // (int) timestamp, reserved, flags);
675 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "\npacket#%d: pos: 0x%0x, len: %d, id: %d, pts: %u, flags: %x rvd:%d\n",
676 priv->current_packet, (int)demuxer->filepos, len, stream_id, timestamp, flags, reserved);
678 priv->current_packet++;
679 len -= 12;
681 // printf("s_id=%d aid=%d vid=%d \n",stream_id,demuxer->audio->id,demuxer->video->id);
683 /* check stream_id: */
685 if(demuxer->audio->id==stream_id){
686 if (priv->audio_need_keyframe == 1&& flags != 0x2)
687 goto discard;
688 got_audio:
689 ds=demuxer->audio;
690 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "packet is audio (id: %d)\n", stream_id);
692 if (flags & 2) {
693 priv->sub_packet_cnt = 0;
694 audioreorder_getnextpk = 0;
697 // parse audio chunk:
699 #ifdef CRACK_MATRIX
700 int spos=stream_tell(demuxer->stream);
701 static int cnt=0;
702 static int cnt2=CRACK_MATRIX;
703 #endif
704 if (((sh_audio_t *)ds->sh)->format == mmioFOURCC('M', 'P', '4', 'A')) {
705 uint16_t sub_packet_lengths[16], sub_packets, i;
706 int totlen = 0;
707 /* AAC in Real: several AAC frames in one Real packet. */
708 /* Second byte, upper four bits: number of AAC frames */
709 /* next n * 2 bytes: length of the AAC frames in bytes, BE */
710 if (len < 2)
711 goto discard;
712 sub_packets = (stream_read_word(demuxer->stream) & 0xf0) >> 4;
713 if (len < 2 * sub_packets)
714 goto discard;
715 for (i = 0; i < sub_packets; i++)
716 totlen += sub_packet_lengths[i] = stream_read_word(demuxer->stream);
717 if (len < totlen )
718 goto discard;
719 for (i = 0; i < sub_packets; i++) {
720 demux_packet_t *dp = new_demux_packet(sub_packet_lengths[i]);
721 stream_read(demuxer->stream, dp->buffer, sub_packet_lengths[i]);
722 if (priv->a_pts != timestamp)
723 dp->pts = timestamp / 1000.0;
724 priv->a_pts = timestamp;
725 dp->pos = demuxer->filepos;
726 ds_add_packet(ds, dp);
728 return 1;
730 if ((priv->intl_id[stream_id] == mmioFOURCC('I', 'n', 't', '4')) ||
731 (priv->intl_id[stream_id] == mmioFOURCC('g', 'e', 'n', 'r')) ||
732 (priv->intl_id[stream_id] == mmioFOURCC('s', 'i', 'p', 'r'))) {
733 sps = priv->sub_packet_size[stream_id];
734 sph = priv->sub_packet_h[stream_id];
735 cfs = priv->coded_framesize[stream_id];
736 w = priv->audiopk_size[stream_id];
737 spc = priv->sub_packet_cnt;
738 switch (priv->intl_id[stream_id]) {
739 case mmioFOURCC('I', 'n', 't', '4'):
740 if (len < cfs * sph/2)
741 goto discard;
742 for (x = 0; x < sph / 2; x++)
743 stream_read(demuxer->stream, priv->audio_buf + x * 2 * w + spc * cfs, cfs);
744 break;
745 case mmioFOURCC('g', 'e', 'n', 'r'):
746 if (len < w)
747 goto discard;
748 for (x = 0; x < w / sps; x++)
749 stream_read(demuxer->stream, priv->audio_buf + sps * (sph * x + ((sph + 1) / 2) * (spc & 1) +
750 (spc >> 1)), sps);
751 break;
752 case mmioFOURCC('s', 'i', 'p', 'r'):
753 if (len < w)
754 goto discard;
755 stream_read(demuxer->stream, priv->audio_buf + spc * w, w);
756 if (spc == sph - 1) {
757 int n;
758 int bs = sph * w * 2 / 96; // nibbles per subpacket
759 // Perform reordering
760 for(n=0; n < 38; n++) {
761 int j;
762 int i = bs * sipr_swaps[n][0];
763 int o = bs * sipr_swaps[n][1];
764 // swap nibbles of block 'i' with 'o' TODO: optimize
765 for(j = 0;j < bs; j++) {
766 int x = (i & 1) ? (priv->audio_buf[i >> 1] >> 4) : (priv->audio_buf[i >> 1] & 0x0F);
767 int y = (o & 1) ? (priv->audio_buf[o >> 1] >> 4) : (priv->audio_buf[o >> 1] & 0x0F);
768 if(o & 1)
769 priv->audio_buf[o >> 1] = (priv->audio_buf[o >> 1] & 0x0F) | (x << 4);
770 else
771 priv->audio_buf[o >> 1] = (priv->audio_buf[o >> 1] & 0xF0) | x;
772 if(i & 1)
773 priv->audio_buf[i >> 1] = (priv->audio_buf[i >> 1] & 0x0F) | (y << 4);
774 else
775 priv->audio_buf[i >> 1] = (priv->audio_buf[i >> 1] & 0xF0) | y;
776 ++i; ++o;
780 break;
782 priv->audio_need_keyframe = 0;
783 priv->audio_timestamp[priv->sub_packet_cnt] = (priv->a_pts==timestamp) ? MP_NOPTS_VALUE : (timestamp/1000.0);
784 priv->a_pts = timestamp;
785 if (priv->sub_packet_cnt == 0)
786 priv->audio_filepos = demuxer->filepos;
787 if (++(priv->sub_packet_cnt) < sph)
788 audioreorder_getnextpk = 1;
789 else {
790 int apk_usize = ((WAVEFORMATEX*)((sh_audio_t*)ds->sh)->wf)->nBlockAlign;
791 audioreorder_getnextpk = 0;
792 priv->sub_packet_cnt = 0;
793 // Release all the audio packets
794 for (x = 0; x < sph*w/apk_usize; x++) {
795 dp = new_demux_packet(apk_usize);
796 memcpy(dp->buffer, priv->audio_buf + x * apk_usize, apk_usize);
797 /* Put timestamp only on packets that correspond to original audio packets in file */
798 if (x * apk_usize % w == 0)
799 dp->pts = priv->audio_timestamp[x * apk_usize / w];
800 dp->pos = priv->audio_filepos; // all equal
801 dp->flags = x ? 0 : 0x10; // Mark first packet as keyframe
802 ds_add_packet(ds, dp);
805 } else { // No interleaving
806 dp = new_demux_packet(len);
807 stream_read(demuxer->stream, dp->buffer, len);
809 #ifdef CRACK_MATRIX
810 mp_msg(MSGT_DEMUX, MSGL_V,"*** audio block len=%d\n",len);
811 { // HACK - used for reverse engineering the descrambling matrix
812 FILE* f=fopen("test.rm","r+");
813 fseek(f,spos,SEEK_SET);
814 ++cnt;
815 // for(i=0;i<len;i++) dp->buffer[i]=i/0x12;
816 // for(i=0;i<len;i++) dp->buffer[i]=i;
817 // for(i=0;i<len;i++) dp->buffer[i]=cnt;
818 // for(i=0;i<len;i++) dp->buffer[i]=cnt<<4;
819 for(i=0;i<len;i++) dp->buffer[i]=(i==cnt2) ? (cnt+16*(8+cnt)) : 0;
820 if(cnt==6){ cnt=0; ++cnt2; }
821 fwrite(dp->buffer, len, 1, f);
822 fclose(f);
823 if(cnt2>0x150) *((int*)NULL)=1; // sig11 :)
825 #endif
826 #if 0
827 if( ((sh_audio_t *)ds->sh)->format == 0x2000) {
828 // if DNET, swap bytes, as DNET is byte-swapped AC3:
829 char *ptr = dp->buffer;
830 int i;
831 for (i = 0; i < len; i += 2)
833 const char tmp = ptr[0];
834 ptr[0] = ptr[1];
835 ptr[1] = tmp;
836 ptr += 2;
839 #endif
840 if (priv->audio_need_keyframe == 1) {
841 priv->audio_need_keyframe = 0;
842 } else if(priv->a_pts != timestamp)
843 dp->pts = timestamp/1000.0;
844 priv->a_pts=timestamp;
845 dp->pos = demuxer->filepos;
846 dp->flags = (flags & 0x2) ? 0x10 : 0;
847 ds_add_packet(ds, dp);
849 } // codec_id check, codec default case
851 // we will not use audio index if we use -idx and have a video
852 if(((!demuxer->video->sh && index_mode == 2) || priv->is_multirate) && (unsigned)demuxer->audio->id < MAX_STREAMS) {
853 while (priv->current_apacket + 1 < priv->index_table_size[demuxer->audio->id] &&
854 timestamp > priv->index_table[demuxer->audio->id][priv->current_apacket].timestamp) {
855 priv->current_apacket += 1;
856 priv->stream_switch = 1;
858 if (priv->stream_switch)
859 priv->audio_curpos = stream_tell(demuxer->stream);
862 // If we're reordering audio packets and we need more data get it
863 if (audioreorder_getnextpk)
864 continue;
866 return 1;
869 if(demuxer->video->id==stream_id){
870 got_video:
871 ds=demuxer->video;
872 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "packet is video (id: %d)\n", stream_id);
874 // parse video chunk:
876 // we need a more complicated, 2nd level demuxing, as the video
877 // frames are stored fragmented in the video chunks :(
878 sh_video_t *sh_video = ds->sh;
879 demux_packet_t *dp;
880 unsigned vpkg_header, vpkg_length, vpkg_offset;
881 int vpkg_seqnum=-1;
882 int vpkg_subseq=0;
884 while(len>2){
885 dp_hdr_t* dp_hdr;
886 unsigned char* dp_data;
887 uint32_t* extra;
889 // printf("xxx len=%d \n",len);
891 // read packet header
892 // bit 7: 1=last block in block chain
893 // bit 6: 1=short header (only one block?)
894 vpkg_header=stream_read_char(demuxer->stream); --len;
895 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "hdr: %02X (len=%d) ",vpkg_header,len);
897 if (0x40==(vpkg_header&0xc0)) {
898 // seems to be a very short header
899 // 2 bytes, purpose of the second byte yet unknown
900 int bummer;
901 bummer=stream_read_char(demuxer->stream); --len;
902 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "%02X",bummer);
903 vpkg_offset=0;
904 vpkg_length=len;
905 } else {
907 if (0==(vpkg_header&0x40)) {
908 // sub-seqnum (bits 0-6: number of fragment. bit 7: ???)
909 vpkg_subseq=stream_read_char(demuxer->stream);
910 --len;
911 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "subseq: %02X ",vpkg_subseq);
912 vpkg_subseq&=0x7f;
915 // size of the complete packet
916 // bit 14 is always one (same applies to the offset)
917 vpkg_length=stream_read_word(demuxer->stream);
918 len-=2;
919 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "l: %02X %02X ",vpkg_length>>8,vpkg_length&0xff);
920 if (!(vpkg_length&0xC000)) {
921 vpkg_length<<=16;
922 vpkg_length|=(uint16_t)stream_read_word(demuxer->stream);
923 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "l+: %02X %02X ",(vpkg_length>>8)&0xff,vpkg_length&0xff);
924 len-=2;
925 } else
926 vpkg_length&=0x3fff;
928 // offset of the following data inside the complete packet
929 // Note: if (hdr&0xC0)==0x80 then offset is relative to the
930 // _end_ of the packet, so it's equal to fragment size!!!
931 vpkg_offset=stream_read_word(demuxer->stream);
932 len-=2;
933 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "o: %02X %02X ",vpkg_offset>>8,vpkg_offset&0xff);
934 if (!(vpkg_offset&0xC000)) {
935 vpkg_offset<<=16;
936 vpkg_offset|=(uint16_t)stream_read_word(demuxer->stream);
937 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "o+: %02X %02X ",(vpkg_offset>>8)&0xff,vpkg_offset&0xff);
938 len-=2;
939 } else
940 vpkg_offset&=0x3fff;
942 vpkg_seqnum=stream_read_char(demuxer->stream); --len;
943 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "seq: %02X ",vpkg_seqnum);
945 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "\n");
946 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "blklen=%d\n", len);
947 mp_msg(MSGT_DEMUX,MSGL_DBG2, "block: hdr=0x%0x, len=%d, offset=%d, seqnum=%d\n",
948 vpkg_header, vpkg_length, vpkg_offset, vpkg_seqnum);
950 if(ds->asf_packet){
951 dp=ds->asf_packet;
952 dp_hdr=(dp_hdr_t*)dp->buffer;
953 dp_data=dp->buffer+sizeof(dp_hdr_t);
954 extra=(uint32_t*)(dp->buffer+dp_hdr->chunktab);
955 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "we have an incomplete packet (oldseq=%d new=%d)\n",ds->asf_seq,vpkg_seqnum);
956 // we have an incomplete packet:
957 if(ds->asf_seq!=vpkg_seqnum){
958 // this fragment is for new packet, close the old one
959 mp_msg(MSGT_DEMUX,MSGL_DBG2, "closing probably incomplete packet, len: %d \n",dp->len);
960 queue_video_packet(priv, ds, dp);
961 ds->asf_packet=NULL;
962 } else {
963 // append data to it!
964 ++dp_hdr->chunks;
965 mp_msg(MSGT_DEMUX,MSGL_DBG2,"[chunks=%d subseq=%d]\n",dp_hdr->chunks,vpkg_subseq);
966 if(dp_hdr->chunktab+8*(1+dp_hdr->chunks)>dp->len){
967 // increase buffer size, this should not happen!
968 mp_msg(MSGT_DEMUX,MSGL_WARN, "chunktab buffer too small!!!!!\n");
969 dp->len=dp_hdr->chunktab+8*(4+dp_hdr->chunks);
970 dp->buffer=realloc(dp->buffer,dp->len+MP_INPUT_BUFFER_PADDING_SIZE);
971 memset(dp->buffer + dp->len, 0, MP_INPUT_BUFFER_PADDING_SIZE);
972 // re-calc pointers:
973 dp_hdr=(dp_hdr_t*)dp->buffer;
974 dp_data=dp->buffer+sizeof(dp_hdr_t);
975 extra=(uint32_t*)(dp->buffer+dp_hdr->chunktab);
977 extra[2*dp_hdr->chunks+0]=le2me_32(1);
978 extra[2*dp_hdr->chunks+1]=le2me_32(dp_hdr->len);
979 if(0x80==(vpkg_header&0xc0)){
980 // last fragment!
981 if(dp_hdr->len!=vpkg_length-vpkg_offset)
982 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);
983 if (vpkg_offset > dp->len - sizeof(dp_hdr_t) - dp_hdr->len) vpkg_offset = dp->len - sizeof(dp_hdr_t) - dp_hdr->len;
984 stream_read(demuxer->stream, dp_data+dp_hdr->len, vpkg_offset);
985 if((dp_data[dp_hdr->len]&0x20) && (sh_video->format==0x30335652)) --dp_hdr->chunks; else
986 dp_hdr->len+=vpkg_offset;
987 len-=vpkg_offset;
988 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "fragment (%d bytes) appended, %d bytes left\n",vpkg_offset,len);
989 // we know that this is the last fragment -> we can close the packet!
990 queue_video_packet(priv, ds, dp);
991 ds->asf_packet=NULL;
992 // continue parsing
993 continue;
995 // non-last fragment:
996 if(dp_hdr->len!=vpkg_offset)
997 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);
998 if (len > dp->len - sizeof(dp_hdr_t) - dp_hdr->len) len = dp->len - sizeof(dp_hdr_t) - dp_hdr->len;
999 stream_read(demuxer->stream, dp_data+dp_hdr->len, len);
1000 if((dp_data[dp_hdr->len]&0x20) && (sh_video->format==0x30335652)) --dp_hdr->chunks; else
1001 dp_hdr->len+=len;
1002 len=0;
1003 break; // no more fragments in this chunk!
1006 // create new packet!
1007 dp = new_demux_packet(sizeof(dp_hdr_t)+vpkg_length+8*(1+2*(vpkg_header&0x3F)));
1008 // the timestamp seems to be in milliseconds
1009 dp->pos = demuxer->filepos;
1010 dp->flags = (flags & 0x2) ? 0x10 : 0;
1011 ds->asf_seq = vpkg_seqnum;
1012 dp_hdr=(dp_hdr_t*)dp->buffer;
1013 dp_hdr->chunks=0;
1014 dp_hdr->timestamp=timestamp;
1015 dp_hdr->chunktab=sizeof(dp_hdr_t)+vpkg_length;
1016 dp_data=dp->buffer+sizeof(dp_hdr_t);
1017 extra=(uint32_t*)(dp->buffer+dp_hdr->chunktab);
1018 extra[0]=le2me_32(1); extra[1]=0; // offset of the first chunk
1019 if(0x00==(vpkg_header&0xc0)){
1020 // first fragment:
1021 if (len > dp->len - sizeof(dp_hdr_t)) len = dp->len - sizeof(dp_hdr_t);
1022 dp_hdr->len=len;
1023 stream_read(demuxer->stream, dp_data, len);
1024 ds->asf_packet=dp;
1025 len=0;
1026 break;
1028 // whole packet (not fragmented):
1029 if (vpkg_length > len) {
1030 mp_msg(MSGT_DEMUX, MSGL_WARN,"\n******** WARNING: vpkg_length=%i > len=%i ********\n", vpkg_length, len);
1032 * To keep the video stream rolling, we need to break
1033 * here. We shouldn't touch len to make sure rest of the
1034 * broken packet is skipped.
1036 break;
1038 dp_hdr->len=vpkg_length; len-=vpkg_length;
1039 stream_read(demuxer->stream, dp_data, vpkg_length);
1040 queue_video_packet(priv, ds, dp);
1042 } // while(len>0)
1044 if(len){
1045 mp_msg(MSGT_DEMUX, MSGL_WARN,"\n******** !!!!!!!! BUG!! len=%d !!!!!!!!!!! ********\n",len);
1046 if(len>0) stream_skip(demuxer->stream, len);
1049 if ((unsigned)demuxer->video->id < MAX_STREAMS) {
1050 while (priv->current_vpacket + 1 < priv->index_table_size[demuxer->video->id] &&
1051 timestamp > priv->index_table[demuxer->video->id][priv->current_vpacket + 1].timestamp) {
1052 priv->current_vpacket += 1;
1053 priv->stream_switch = 1;
1055 if (priv->stream_switch)
1056 priv->video_curpos = stream_tell(demuxer->stream);
1059 return 1;
1062 if((unsigned)stream_id<MAX_STREAMS){
1064 if(demuxer->audio->id==-1 && demuxer->a_streams[stream_id]){
1065 sh_audio_t *sh = demuxer->a_streams[stream_id];
1066 demuxer->audio->id=stream_id;
1067 sh->ds=demuxer->audio;
1068 demuxer->audio->sh=sh;
1069 priv->audio_buf = calloc(priv->sub_packet_h[demuxer->audio->id], priv->audiopk_size[demuxer->audio->id]);
1070 priv->audio_timestamp = calloc(priv->sub_packet_h[demuxer->audio->id], sizeof(double));
1071 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected RM audio ID = %d\n",stream_id);
1072 goto got_audio;
1075 if(demuxer->video->id==-1 && demuxer->v_streams[stream_id]){
1076 sh_video_t *sh = demuxer->v_streams[stream_id];
1077 demuxer->video->id=stream_id;
1078 sh->ds=demuxer->video;
1079 demuxer->video->sh=sh;
1080 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected RM video ID = %d\n",stream_id);
1081 goto got_video;
1086 mp_msg(MSGT_DEMUX,MSGL_DBG2, "unknown stream id (%d)\n", stream_id);
1087 discard:
1088 stream_skip(demuxer->stream, len);
1089 }// goto loop;
1090 return 0;
1093 void print_wave_header(WAVEFORMATEX *h, int verbose_level);
1095 static demuxer_t* demux_open_real(demuxer_t* demuxer)
1097 real_priv_t* priv = demuxer->priv;
1098 int num_of_headers;
1099 int a_streams=0;
1100 int v_streams=0;
1101 int i;
1102 int header_size;
1104 header_size = stream_read_dword(demuxer->stream); /* header size */
1105 mp_msg(MSGT_DEMUX,MSGL_V, "real: Header size: %d\n", header_size);
1106 i = stream_read_word(demuxer->stream); /* version */
1107 mp_msg(MSGT_DEMUX,MSGL_V, "real: Header object version: %d\n", i);
1108 if (header_size == 0x10)
1109 i = stream_read_word(demuxer->stream);
1110 else /* we should test header_size here too. */
1111 i = stream_read_dword(demuxer->stream);
1112 mp_msg(MSGT_DEMUX,MSGL_V, "real: File version: %d\n", i);
1113 num_of_headers = stream_read_dword(demuxer->stream);
1115 /* parse chunks */
1116 for (i = 1; i <= num_of_headers; i++)
1117 // for (i = 1; ; i++)
1119 int chunk_id, chunk_pos, chunk_size;
1121 chunk_pos = stream_tell(demuxer->stream);
1122 chunk_id = stream_read_dword_le(demuxer->stream);
1123 chunk_size = stream_read_dword(demuxer->stream);
1125 stream_skip(demuxer->stream, 2); /* version */
1127 mp_msg(MSGT_DEMUX,MSGL_V, "Chunk: %.4s (%x) (size: 0x%x, offset: 0x%x)\n",
1128 (char *)&chunk_id, chunk_id, chunk_size, chunk_pos);
1130 if (chunk_id != MKTAG('D', 'A', 'T', 'A') && chunk_size < 10){
1131 mp_msg(MSGT_DEMUX,MSGL_ERR,"demux_real: invalid chunksize! (%d)\n",chunk_size);
1132 break; //return;
1135 switch(chunk_id)
1137 case MKTAG('P', 'R', 'O', 'P'):
1138 /* Properties header */
1140 stream_skip(demuxer->stream, 4); /* max bitrate */
1141 stream_skip(demuxer->stream, 4); /* avg bitrate */
1142 stream_skip(demuxer->stream, 4); /* max packet size */
1143 stream_skip(demuxer->stream, 4); /* avg packet size */
1144 stream_skip(demuxer->stream, 4); /* nb packets */
1145 priv->duration = stream_read_dword(demuxer->stream)/1000; /* duration */
1146 stream_skip(demuxer->stream, 4); /* preroll */
1147 priv->index_chunk_offset = stream_read_dword(demuxer->stream);
1148 mp_msg(MSGT_DEMUX,MSGL_V,"First index chunk offset: 0x%x\n", priv->index_chunk_offset);
1149 priv->data_chunk_offset = stream_read_dword(demuxer->stream)+10;
1150 mp_msg(MSGT_DEMUX,MSGL_V,"First data chunk offset: 0x%x\n", priv->data_chunk_offset);
1151 stream_skip(demuxer->stream, 2); /* nb streams */
1152 #if 0
1153 stream_skip(demuxer->stream, 2); /* flags */
1154 #else
1156 int flags = stream_read_word(demuxer->stream);
1158 if (flags)
1160 mp_msg(MSGT_DEMUX,MSGL_V,"Flags (%x): ", flags);
1161 if (flags & 0x1)
1162 mp_msg(MSGT_DEMUX,MSGL_V,"[save allowed] ");
1163 if (flags & 0x2)
1164 mp_msg(MSGT_DEMUX,MSGL_V,"[perfect play (more buffers)] ");
1165 if (flags & 0x4)
1166 mp_msg(MSGT_DEMUX,MSGL_V,"[live broadcast] ");
1167 mp_msg(MSGT_DEMUX,MSGL_V,"\n");
1170 #endif
1171 break;
1172 case MKTAG('C', 'O', 'N', 'T'):
1174 /* Content description header */
1175 char *buf;
1176 int len;
1178 len = stream_read_word(demuxer->stream);
1179 if (len > 0)
1181 buf = malloc(len+1);
1182 stream_read(demuxer->stream, buf, len);
1183 buf[len] = 0;
1184 demux_info_add(demuxer, "title", buf);
1185 free(buf);
1188 len = stream_read_word(demuxer->stream);
1189 if (len > 0)
1191 buf = malloc(len+1);
1192 stream_read(demuxer->stream, buf, len);
1193 buf[len] = 0;
1194 demux_info_add(demuxer, "author", buf);
1195 free(buf);
1198 len = stream_read_word(demuxer->stream);
1199 if (len > 0)
1201 buf = malloc(len+1);
1202 stream_read(demuxer->stream, buf, len);
1203 buf[len] = 0;
1204 demux_info_add(demuxer, "copyright", buf);
1205 free(buf);
1208 len = stream_read_word(demuxer->stream);
1209 if (len > 0)
1211 buf = malloc(len+1);
1212 stream_read(demuxer->stream, buf, len);
1213 buf[len] = 0;
1214 demux_info_add(demuxer, "comment", buf);
1215 free(buf);
1217 break;
1219 case MKTAG('M', 'D', 'P', 'R'):
1221 /* Media properties header */
1222 int stream_id;
1223 int bitrate;
1224 int codec_data_size;
1225 int codec_pos;
1226 int tmp;
1227 int len;
1228 char *descr, *mimet = NULL;
1230 stream_id = stream_read_word(demuxer->stream);
1231 mp_msg(MSGT_DEMUX,MSGL_V,"Found new stream (id: %d)\n", stream_id);
1233 stream_skip(demuxer->stream, 4); /* max bitrate */
1234 bitrate = stream_read_dword(demuxer->stream); /* avg bitrate */
1235 stream_skip(demuxer->stream, 4); /* max packet size */
1236 stream_skip(demuxer->stream, 4); /* avg packet size */
1237 stream_skip(demuxer->stream, 4); /* start time */
1238 stream_skip(demuxer->stream, 4); /* preroll */
1239 stream_skip(demuxer->stream, 4); /* duration */
1241 if ((len = stream_read_char(demuxer->stream)) > 0) {
1242 descr = malloc(len+1);
1243 stream_read(demuxer->stream, descr, len);
1244 descr[len] = 0;
1245 mp_msg(MSGT_DEMUX, MSGL_INFO,"Stream description: %s\n", descr);
1246 free(descr);
1248 if ((len = stream_read_char(demuxer->stream)) > 0) {
1249 mimet = malloc(len+1);
1250 stream_read(demuxer->stream, mimet, len);
1251 mimet[len] = 0;
1252 mp_msg(MSGT_DEMUX, MSGL_INFO,"Stream mimetype: %s\n", mimet);
1255 /* Type specific header */
1256 codec_data_size = stream_read_dword(demuxer->stream);
1257 codec_pos = stream_tell(demuxer->stream);
1259 #ifdef MP_DEBUG
1260 #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");}
1261 #endif
1263 if (!strncmp(mimet,"audio/",6)) {
1264 if (strstr(mimet,"x-pn-realaudio") || strstr(mimet,"x-pn-multirate-realaudio")) {
1265 tmp = stream_read_dword(demuxer->stream);
1266 if (tmp != MKTAG(0xfd, 'a', 'r', '.'))
1268 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: can't find .ra in codec data\n");
1269 } else {
1270 /* audio header */
1271 sh_audio_t *sh = new_sh_audio(demuxer, stream_id);
1272 char buf[128]; /* for codec name */
1273 int frame_size;
1274 int sub_packet_size;
1275 int sub_packet_h;
1276 int version;
1277 int flavor;
1278 int coded_frame_size;
1279 int codecdata_length;
1280 int i;
1281 char *buft;
1282 int hdr_size;
1283 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_AudioID, "real", stream_id);
1284 mp_msg(MSGT_DEMUX,MSGL_V,"Found audio stream!\n");
1285 version = stream_read_word(demuxer->stream);
1286 mp_msg(MSGT_DEMUX,MSGL_V,"version: %d\n", version);
1287 if (version == 3) {
1288 stream_skip(demuxer->stream, 2);
1289 stream_skip(demuxer->stream, 10);
1290 stream_skip(demuxer->stream, 4);
1291 // Name, author, (c) are also in CONT tag
1292 if ((i = stream_read_char(demuxer->stream)) != 0) {
1293 buft = malloc(i+1);
1294 stream_read(demuxer->stream, buft, i);
1295 buft[i] = 0;
1296 demux_info_add(demuxer, "Name", buft);
1297 free(buft);
1299 if ((i = stream_read_char(demuxer->stream)) != 0) {
1300 buft = malloc(i+1);
1301 stream_read(demuxer->stream, buft, i);
1302 buft[i] = 0;
1303 demux_info_add(demuxer, "Author", buft);
1304 free(buft);
1306 if ((i = stream_read_char(demuxer->stream)) != 0) {
1307 buft = malloc(i+1);
1308 stream_read(demuxer->stream, buft, i);
1309 buft[i] = 0;
1310 demux_info_add(demuxer, "Copyright", buft);
1311 free(buft);
1313 if ((i = stream_read_char(demuxer->stream)) != 0)
1314 mp_msg(MSGT_DEMUX,MSGL_WARN,"Last header byte is not zero!\n");
1315 stream_skip(demuxer->stream, 1);
1316 i = stream_read_char(demuxer->stream);
1317 sh->format = stream_read_dword_le(demuxer->stream);
1318 if (i != 4) {
1319 mp_msg(MSGT_DEMUX,MSGL_WARN,"Audio FourCC size is not 4 (%d), please report to "
1320 "MPlayer developers\n", i);
1321 stream_skip(demuxer->stream, i - 4);
1323 if (sh->format != mmioFOURCC('l','p','c','J')) {
1324 mp_msg(MSGT_DEMUX,MSGL_WARN,"Version 3 audio with FourCC %8x, please report to "
1325 "MPlayer developers\n", sh->format);
1327 sh->channels = 1;
1328 sh->samplesize = 16;
1329 sh->samplerate = 8000;
1330 frame_size = 240;
1331 strcpy(buf, "14_4");
1332 } else {
1333 stream_skip(demuxer->stream, 2); // 00 00
1334 stream_skip(demuxer->stream, 4); /* .ra4 or .ra5 */
1335 stream_skip(demuxer->stream, 4); // ???
1336 stream_skip(demuxer->stream, 2); /* version (4 or 5) */
1337 hdr_size = stream_read_dword(demuxer->stream); // header size
1338 mp_msg(MSGT_DEMUX,MSGL_V,"header size: %d\n", hdr_size);
1339 flavor = stream_read_word(demuxer->stream);/* codec flavor id */
1340 coded_frame_size = stream_read_dword(demuxer->stream);/* needed by codec */
1341 mp_msg(MSGT_DEMUX,MSGL_V,"coded_frame_size: %d\n", coded_frame_size);
1342 stream_skip(demuxer->stream, 4); // big number
1343 stream_skip(demuxer->stream, 4); // bigger number
1344 stream_skip(demuxer->stream, 4); // 2 || -''-
1345 sub_packet_h = stream_read_word(demuxer->stream);
1346 mp_msg(MSGT_DEMUX,MSGL_V,"sub_packet_h: %d\n", sub_packet_h);
1347 frame_size = stream_read_word(demuxer->stream);
1348 mp_msg(MSGT_DEMUX,MSGL_V,"frame_size: %d\n", frame_size);
1349 sub_packet_size = stream_read_word(demuxer->stream);
1350 mp_msg(MSGT_DEMUX,MSGL_V,"sub_packet_size: %d\n", sub_packet_size);
1351 stream_skip(demuxer->stream, 2); // 0
1353 if (version == 5)
1354 stream_skip(demuxer->stream, 6); //0,srate,0
1356 sh->samplerate = stream_read_word(demuxer->stream);
1357 stream_skip(demuxer->stream, 2); // 0
1358 sh->samplesize = stream_read_word(demuxer->stream)/8;
1359 sh->channels = stream_read_word(demuxer->stream);
1360 mp_msg(MSGT_DEMUX,MSGL_V,"samplerate: %d, channels: %d\n",
1361 sh->samplerate, sh->channels);
1363 if (version == 5)
1365 stream_read(demuxer->stream, buf, 4); // interleaver id
1366 priv->intl_id[stream_id] = MKTAG(buf[0], buf[1], buf[2], buf[3]);
1367 stream_read(demuxer->stream, buf, 4); // fourcc
1368 buf[4] = 0;
1370 else
1372 /* Interleaver id */
1373 get_str(1, demuxer, buf, sizeof(buf));
1374 priv->intl_id[stream_id] = MKTAG(buf[0], buf[1], buf[2], buf[3]);
1375 /* Codec FourCC */
1376 get_str(1, demuxer, buf, sizeof(buf));
1380 /* Emulate WAVEFORMATEX struct: */
1381 sh->wf = malloc(sizeof(WAVEFORMATEX));
1382 memset(sh->wf, 0, sizeof(WAVEFORMATEX));
1383 sh->wf->nChannels = sh->channels;
1384 sh->wf->wBitsPerSample = sh->samplesize*8;
1385 sh->wf->nSamplesPerSec = sh->samplerate;
1386 sh->wf->nAvgBytesPerSec = bitrate/8;
1387 sh->wf->nBlockAlign = frame_size;
1388 sh->wf->cbSize = 0;
1389 sh->format = MKTAG(buf[0], buf[1], buf[2], buf[3]);
1391 switch (sh->format)
1393 case MKTAG('d', 'n', 'e', 't'):
1394 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: DNET -> AC3\n");
1395 // sh->format = 0x2000;
1396 break;
1397 case MKTAG('1', '4', '_', '4'):
1398 sh->wf->nBlockAlign = 0x14;
1399 break;
1401 case MKTAG('2', '8', '_', '8'):
1402 sh->wf->nBlockAlign = coded_frame_size;
1403 break;
1405 case MKTAG('s', 'i', 'p', 'r'):
1406 case MKTAG('a', 't', 'r', 'c'):
1407 case MKTAG('c', 'o', 'o', 'k'):
1408 // realaudio codec plugins - common:
1409 stream_skip(demuxer->stream,3); // Skip 3 unknown bytes
1410 if (version==5)
1411 stream_skip(demuxer->stream,1); // Skip 1 additional unknown byte
1412 codecdata_length=stream_read_dword(demuxer->stream);
1413 // Check extradata len, we can't store bigger values in cbSize anyway
1414 if ((unsigned)codecdata_length > 0xffff) {
1415 mp_msg(MSGT_DEMUX,MSGL_ERR,"Extradata too big (%d)\n", codecdata_length);
1416 goto skip_this_chunk;
1418 sh->wf->cbSize = codecdata_length;
1419 sh->wf = realloc(sh->wf, sizeof(WAVEFORMATEX)+sh->wf->cbSize);
1420 stream_read(demuxer->stream, ((char*)(sh->wf+1)), codecdata_length); // extras
1421 if (priv->intl_id[stream_id] == MKTAG('g', 'e', 'n', 'r'))
1422 sh->wf->nBlockAlign = sub_packet_size;
1423 else
1424 sh->wf->nBlockAlign = coded_frame_size;
1426 break;
1428 case MKTAG('r', 'a', 'a', 'c'):
1429 case MKTAG('r', 'a', 'c', 'p'):
1430 /* This is just AAC. The two or five bytes of */
1431 /* config data needed for libfaad are stored */
1432 /* after the audio headers. */
1433 stream_skip(demuxer->stream,3); // Skip 3 unknown bytes
1434 if (version==5)
1435 stream_skip(demuxer->stream,1); // Skip 1 additional unknown byte
1436 codecdata_length=stream_read_dword(demuxer->stream);
1437 if (codecdata_length>=1) {
1438 sh->codecdata_len = codecdata_length - 1;
1439 sh->codecdata = calloc(sh->codecdata_len, 1);
1440 stream_skip(demuxer->stream, 1);
1441 stream_read(demuxer->stream, sh->codecdata, sh->codecdata_len);
1443 sh->format = mmioFOURCC('M', 'P', '4', 'A');
1444 break;
1445 default:
1446 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: Unknown (%s)\n", buf);
1449 // Interleaver setup
1450 priv->sub_packet_size[stream_id] = sub_packet_size;
1451 priv->sub_packet_h[stream_id] = sub_packet_h;
1452 priv->coded_framesize[stream_id] = coded_frame_size;
1453 priv->audiopk_size[stream_id] = frame_size;
1455 sh->wf->wFormatTag = sh->format;
1457 mp_msg(MSGT_DEMUX,MSGL_V,"audio fourcc: %.4s (%x)\n", (char *)&sh->format, sh->format);
1458 if ( mp_msg_test(MSGT_DEMUX,MSGL_V) )
1459 print_wave_header(sh->wf, MSGL_V);
1461 /* Select audio stream with highest bitrate if multirate file*/
1462 if (priv->is_multirate && ((demuxer->audio->id == -1) ||
1463 ((demuxer->audio->id >= 0) && priv->a_bitrate && (bitrate > priv->a_bitrate)))) {
1464 demuxer->audio->id = stream_id;
1465 priv->a_bitrate = bitrate;
1466 mp_msg(MSGT_DEMUX,MSGL_DBG2,"Multirate autoselected audio id %d with bitrate %d\n", stream_id, bitrate);
1469 if(demuxer->audio->id==stream_id){
1470 sh->ds=demuxer->audio;
1471 demuxer->audio->sh=sh;
1472 priv->audio_buf = calloc(priv->sub_packet_h[demuxer->audio->id], priv->audiopk_size[demuxer->audio->id]);
1473 priv->audio_timestamp = calloc(priv->sub_packet_h[demuxer->audio->id], sizeof(double));
1476 ++a_streams;
1478 #ifdef stream_skip
1479 #undef stream_skip
1480 #endif
1482 } else if (strstr(mimet,"X-MP3-draft-00")) {
1483 sh_audio_t *sh = new_sh_audio(demuxer, stream_id);
1484 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_AudioID, "real", stream_id);
1486 /* Emulate WAVEFORMATEX struct: */
1487 sh->wf = malloc(sizeof(WAVEFORMATEX));
1488 memset(sh->wf, 0, sizeof(WAVEFORMATEX));
1489 sh->wf->nChannels = 0;//sh->channels;
1490 sh->wf->wBitsPerSample = 16;
1491 sh->wf->nSamplesPerSec = 0;//sh->samplerate;
1492 sh->wf->nAvgBytesPerSec = 0;//bitrate;
1493 sh->wf->nBlockAlign = 0;//frame_size;
1494 sh->wf->cbSize = 0;
1495 sh->wf->wFormatTag = sh->format = mmioFOURCC('a','d','u',0x55);
1497 if(demuxer->audio->id==stream_id){
1498 sh->ds=demuxer->audio;
1499 demuxer->audio->sh=sh;
1502 ++a_streams;
1503 } else if (strstr(mimet,"x-ralf-mpeg4")) {
1504 mp_msg(MSGT_DEMUX,MSGL_ERR,"Real lossless audio not supported yet\n");
1505 } else if (strstr(mimet,"x-pn-encrypted-ra")) {
1506 mp_msg(MSGT_DEMUX,MSGL_ERR,"Encrypted audio is not supported\n");
1507 } else {
1508 mp_msg(MSGT_DEMUX,MSGL_V,"Unknown audio stream format\n");
1510 } else if (!strncmp(mimet,"video/",6)) {
1511 if (strstr(mimet,"x-pn-realvideo") || strstr(mimet,"x-pn-multirate-realvideo")) {
1512 stream_skip(demuxer->stream, 4); // VIDO length, same as codec_data_size
1513 tmp = stream_read_dword(demuxer->stream);
1514 if(tmp != MKTAG('O', 'D', 'I', 'V'))
1516 mp_msg(MSGT_DEMUX,MSGL_V,"Video: can't find VIDO in codec data\n");
1517 } else {
1518 /* video header */
1519 sh_video_t *sh = new_sh_video(demuxer, stream_id);
1520 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_VideoID, "real", stream_id);
1522 sh->format = stream_read_dword_le(demuxer->stream); /* fourcc */
1523 mp_msg(MSGT_DEMUX,MSGL_V,"video fourcc: %.4s (%x)\n", (char *)&sh->format, sh->format);
1525 /* emulate BITMAPINFOHEADER */
1526 sh->bih = malloc(sizeof(BITMAPINFOHEADER));
1527 memset(sh->bih, 0, sizeof(BITMAPINFOHEADER));
1528 sh->bih->biSize = sizeof(BITMAPINFOHEADER);
1529 sh->disp_w = sh->bih->biWidth = stream_read_word(demuxer->stream);
1530 sh->disp_h = sh->bih->biHeight = stream_read_word(demuxer->stream);
1531 sh->bih->biPlanes = 1;
1532 sh->bih->biBitCount = 24;
1533 sh->bih->biCompression = sh->format;
1534 sh->bih->biSizeImage= sh->bih->biWidth*sh->bih->biHeight*3;
1536 sh->fps = (float) stream_read_word(demuxer->stream);
1537 if (sh->fps<=0) sh->fps=24; // we probably won't even care about fps
1538 sh->frametime = 1.0f/sh->fps;
1540 #if 1
1541 stream_skip(demuxer->stream, 4);
1542 #else
1543 mp_msg(MSGT_DEMUX, MSGL_V,"unknown1: 0x%X \n",stream_read_dword(demuxer->stream));
1544 mp_msg(MSGT_DEMUX, MSGL_V,"unknown2: 0x%X \n",stream_read_word(demuxer->stream));
1545 mp_msg(MSGT_DEMUX, MSGL_V,"unknown3: 0x%X \n",stream_read_word(demuxer->stream));
1546 #endif
1547 // if(sh->format==0x30335652 || sh->format==0x30325652 )
1548 if(1)
1550 int tmp=stream_read_word(demuxer->stream);
1551 if(tmp>0){
1552 sh->fps=tmp; sh->frametime = 1.0f/sh->fps;
1554 } else {
1555 int fps=stream_read_word(demuxer->stream);
1556 mp_msg(MSGT_DEMUX, MSGL_WARN,"realvid: ignoring FPS = %d\n",fps);
1558 stream_skip(demuxer->stream, 2);
1561 // read and store codec extradata
1562 unsigned int cnt = codec_data_size - (stream_tell(demuxer->stream) - codec_pos);
1563 if (cnt > 0x7fffffff - sizeof(BITMAPINFOHEADER)) {
1564 mp_msg(MSGT_DEMUX, MSGL_ERR,"Extradata too big (%u)\n", cnt);
1565 } else {
1566 sh->bih = realloc(sh->bih, sizeof(BITMAPINFOHEADER) + cnt);
1567 sh->bih->biSize += cnt;
1568 stream_read(demuxer->stream, ((unsigned char*)(sh->bih+1)), cnt);
1571 if(sh->format == 0x30315652 && ((unsigned char*)(sh->bih+1))[6] == 0x30)
1572 sh->bih->biCompression = sh->format = mmioFOURCC('R', 'V', '1', '3');
1574 /* Select video stream with highest bitrate if multirate file*/
1575 if (priv->is_multirate && ((demuxer->video->id == -1) ||
1576 ((demuxer->video->id >= 0) && priv->v_bitrate && (bitrate > priv->v_bitrate)))) {
1577 demuxer->video->id = stream_id;
1578 priv->v_bitrate = bitrate;
1579 mp_msg(MSGT_DEMUX,MSGL_DBG2,"Multirate autoselected video id %d with bitrate %d\n", stream_id, bitrate);
1582 if(demuxer->video->id==stream_id){
1583 sh->ds=demuxer->video;
1584 demuxer->video->sh=sh;
1587 ++v_streams;
1590 } else {
1591 mp_msg(MSGT_DEMUX,MSGL_V,"Unknown video stream format\n");
1593 } else if (strstr(mimet,"logical-")) {
1594 if (strstr(mimet,"fileinfo")) {
1595 mp_msg(MSGT_DEMUX,MSGL_V,"Got a logical-fileinfo chunk\n");
1596 } else if (strstr(mimet,"-audio") || strstr(mimet,"-video")) {
1597 int i, stream_cnt;
1598 int stream_list[MAX_STREAMS];
1600 priv->is_multirate = 1;
1601 stream_skip(demuxer->stream, 4); // Length of codec data (repeated)
1602 stream_cnt = stream_read_dword(demuxer->stream); // Get number of audio or video streams
1603 if ((unsigned)stream_cnt >= MAX_STREAMS) {
1604 mp_msg(MSGT_DEMUX,MSGL_ERR,"Too many streams in %s. Big troubles ahead.\n", mimet);
1605 goto skip_this_chunk;
1607 for (i = 0; i < stream_cnt; i++)
1608 stream_list[i] = stream_read_word(demuxer->stream);
1609 for (i = 0; i < stream_cnt; i++)
1610 if ((unsigned)stream_list[i] >= MAX_STREAMS) {
1611 mp_msg(MSGT_DEMUX,MSGL_ERR,"Stream id out of range: %d. Ignored.\n", stream_list[i]);
1612 stream_skip(demuxer->stream, 4); // Skip DATA offset for broken stream
1613 } else {
1614 priv->str_data_offset[stream_list[i]] = stream_read_dword(demuxer->stream);
1615 mp_msg(MSGT_DEMUX,MSGL_V,"Stream %d with DATA offset 0x%08x\n", stream_list[i], priv->str_data_offset[stream_list[i]]);
1617 // Skip the rest of this chunk
1618 } else
1619 mp_msg(MSGT_DEMUX,MSGL_V,"Unknown logical stream\n");
1621 else {
1622 mp_msg(MSGT_DEMUX, MSGL_ERR, "Not audio/video stream or unsupported!\n");
1624 // break;
1625 // default:
1626 skip_this_chunk:
1627 /* skip codec info */
1628 tmp = stream_tell(demuxer->stream) - codec_pos;
1629 mp_msg(MSGT_DEMUX,MSGL_V,"### skipping %d bytes of codec info\n", codec_data_size - tmp);
1630 #if 0
1631 { int i;
1632 for(i=0;i<codec_data_size - tmp;i++)
1633 mp_msg(MSGT_DEMUX, MSGL_V," %02X",stream_read_char(demuxer->stream));
1634 mp_msg(MSGT_DEMUX, MSGL_V,"\n");
1636 #else
1637 stream_skip(demuxer->stream, codec_data_size - tmp);
1638 #endif
1639 if (mimet)
1640 free (mimet);
1641 break;
1642 // }
1644 case MKTAG('D', 'A', 'T', 'A'):
1645 goto header_end;
1646 case MKTAG('I', 'N', 'D', 'X'):
1647 default:
1648 mp_msg(MSGT_DEMUX,MSGL_V,"Unknown chunk: %x\n", chunk_id);
1649 stream_skip(demuxer->stream, chunk_size - 10);
1650 break;
1654 header_end:
1655 if(priv->is_multirate) {
1656 mp_msg(MSGT_DEMUX,MSGL_V,"Selected video id %d audio id %d\n", demuxer->video->id, demuxer->audio->id);
1657 /* Perform some sanity checks to avoid checking streams id all over the code*/
1658 if (demuxer->audio->id >= MAX_STREAMS) {
1659 mp_msg(MSGT_DEMUX,MSGL_ERR,"Invalid audio stream %d. No sound will be played.\n", demuxer->audio->id);
1660 demuxer->audio->id = -2;
1661 } else if ((demuxer->audio->id >= 0) && (priv->str_data_offset[demuxer->audio->id] == 0)) {
1662 mp_msg(MSGT_DEMUX,MSGL_ERR,"Audio stream %d not found. No sound will be played.\n", demuxer->audio->id);
1663 demuxer->audio->id = -2;
1665 if (demuxer->video->id >= MAX_STREAMS) {
1666 mp_msg(MSGT_DEMUX,MSGL_ERR,"Invalid video stream %d. No video will be played.\n", demuxer->video->id);
1667 demuxer->video->id = -2;
1668 } else if ((demuxer->video->id >= 0) && (priv->str_data_offset[demuxer->video->id] == 0)) {
1669 mp_msg(MSGT_DEMUX,MSGL_ERR,"Video stream %d not found. No video will be played.\n", demuxer->video->id);
1670 demuxer->video->id = -2;
1674 if(priv->is_multirate && ((demuxer->video->id >= 0) || (demuxer->audio->id >=0))) {
1675 /* If audio or video only, seek to right place and behave like standard file */
1676 if (demuxer->video->id < 0) {
1677 // Stream is audio only, or -novideo
1678 stream_seek(demuxer->stream, priv->data_chunk_offset = priv->str_data_offset[demuxer->audio->id]+10);
1679 priv->is_multirate = 0;
1681 if (demuxer->audio->id < 0) {
1682 // Stream is video only, or -nosound
1683 stream_seek(demuxer->stream, priv->data_chunk_offset = priv->str_data_offset[demuxer->video->id]+10);
1684 priv->is_multirate = 0;
1688 if(!priv->is_multirate) {
1689 // printf("i=%d num_of_headers=%d \n",i,num_of_headers);
1690 priv->num_of_packets = stream_read_dword(demuxer->stream);
1691 stream_skip(demuxer->stream, 4); /* next data header */
1693 mp_msg(MSGT_DEMUX,MSGL_V,"Packets in file: %d\n", priv->num_of_packets);
1695 if (priv->num_of_packets == 0)
1696 priv->num_of_packets = -10;
1697 } else {
1698 priv->audio_curpos = priv->str_data_offset[demuxer->audio->id] + 18;
1699 stream_seek(demuxer->stream, priv->str_data_offset[demuxer->audio->id]+10);
1700 priv->a_num_of_packets=priv->a_num_of_packets = stream_read_dword(demuxer->stream);
1701 priv->video_curpos = priv->str_data_offset[demuxer->video->id] + 18;
1702 stream_seek(demuxer->stream, priv->str_data_offset[demuxer->video->id]+10);
1703 priv->v_num_of_packets = stream_read_dword(demuxer->stream);
1704 priv->stream_switch = 1;
1705 /* Index required for multirate playback, force building if it's not there */
1706 /* but respect user request to force index regeneration */
1707 if (index_mode == -1)
1708 index_mode = 1;
1712 priv->audio_need_keyframe = 0;
1713 priv->video_after_seek = 0;
1715 switch (index_mode){
1716 case -1: // untouched
1717 if (priv->index_chunk_offset && parse_index_chunk(demuxer))
1719 demuxer->seekable = 1;
1721 break;
1722 case 1: // use (generate index)
1723 if (priv->index_chunk_offset && parse_index_chunk(demuxer))
1725 demuxer->seekable = 1;
1726 } else {
1727 generate_index(demuxer);
1728 demuxer->seekable = 1;
1730 break;
1731 case 2: // force generating index
1732 generate_index(demuxer);
1733 demuxer->seekable = 1;
1734 break;
1735 default: // do nothing
1736 break;
1739 // detect streams:
1740 if(demuxer->video->id==-1 && v_streams>0){
1741 // find the valid video stream:
1742 if(!ds_fill_buffer(demuxer->video)){
1743 mp_msg(MSGT_DEMUXER,MSGL_INFO,"RM: " MSGTR_MissingVideoStream);
1746 if(demuxer->audio->id==-1 && a_streams>0){
1747 // find the valid audio stream:
1748 if(!ds_fill_buffer(demuxer->audio)){
1749 mp_msg(MSGT_DEMUXER,MSGL_INFO,"RM: " MSGTR_MissingAudioStream);
1753 if(demuxer->video->sh){
1754 sh_video_t *sh=demuxer->video->sh;
1755 mp_msg(MSGT_DEMUX,MSGL_V,"VIDEO: %.4s [%08X,%08X] %dx%d (aspect %4.2f) %4.2f fps\n",
1756 (char *)&sh->format,((unsigned int*)(sh->bih+1))[1],((unsigned int*)(sh->bih+1))[0],
1757 sh->disp_w,sh->disp_h,sh->aspect,sh->fps);
1760 if(demuxer->audio->sh){
1761 sh_audio_t *sh=demuxer->audio->sh;
1762 mp_msg(MSGT_DEMUX,MSGL_V,"AUDIO: %.4s [%08X]\n",
1763 (char *)&sh->format,sh->format);
1766 return demuxer;
1769 static void demux_close_real(demuxer_t *demuxer)
1771 int i;
1772 real_priv_t* priv = demuxer->priv;
1774 if (priv){
1775 for(i=0; i<MAX_STREAMS; i++)
1776 if(priv->index_table[i])
1777 free(priv->index_table[i]);
1778 if (priv->audio_buf)
1779 free(priv->audio_buf);
1780 if (priv->audio_timestamp)
1781 free(priv->audio_timestamp);
1782 free(priv);
1785 return;
1788 /* please upload RV10 samples WITH INDEX CHUNK */
1789 static void demux_seek_real(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
1791 real_priv_t *priv = demuxer->priv;
1792 demux_stream_t *d_audio = demuxer->audio;
1793 demux_stream_t *d_video = demuxer->video;
1794 sh_audio_t *sh_audio = d_audio->sh;
1795 sh_video_t *sh_video = d_video->sh;
1796 int vid = d_video->id, aid = d_audio->id;
1797 int next_offset = 0;
1798 int64_t cur_timestamp = 0;
1799 int streams = 0;
1800 int retried = 0;
1803 if (sh_video && (unsigned)vid < MAX_STREAMS && priv->index_table_size[vid])
1804 streams |= 1;
1805 if (sh_audio && (unsigned)aid < MAX_STREAMS && priv->index_table_size[aid])
1806 streams |= 2;
1808 // printf("streams: %d\n", streams);
1810 if (!streams)
1811 return;
1813 if (flags & SEEK_ABSOLUTE)
1814 priv->current_apacket = priv->current_vpacket = 0;
1815 if (flags & SEEK_FACTOR)
1816 rel_seek_secs *= priv->duration;
1818 if ((streams & 1) && priv->current_vpacket >= priv->index_table_size[vid])
1819 priv->current_vpacket = priv->index_table_size[vid] - 1;
1820 if ((streams & 2) && priv->current_apacket >= priv->index_table_size[aid])
1821 priv->current_apacket = priv->index_table_size[aid] - 1;
1823 // if (index_mode == 1 || index_mode == 2) {
1824 if (streams & 1) {// use the video index if we have one
1825 cur_timestamp = priv->index_table[vid][priv->current_vpacket].timestamp;
1826 if (rel_seek_secs > 0)
1827 while ((priv->index_table[vid][priv->current_vpacket].timestamp - cur_timestamp) < rel_seek_secs * 1000){
1828 priv->current_vpacket += 1;
1829 if (priv->current_vpacket >= priv->index_table_size[vid]) {
1830 priv->current_vpacket = priv->index_table_size[vid] - 1;
1831 if (!retried) {
1832 stream_seek(demuxer->stream, priv->index_table[vid][priv->current_vpacket].offset);
1833 add_index_segment(demuxer, vid, cur_timestamp + rel_seek_secs * 1000);
1834 retried = 1;
1836 else
1837 break;
1840 else if (rel_seek_secs < 0) {
1841 while ((cur_timestamp - priv->index_table[vid][priv->current_vpacket].timestamp) < - rel_seek_secs * 1000){
1842 priv->current_vpacket -= 1;
1843 if (priv->current_vpacket < 0) {
1844 priv->current_vpacket = 0;
1845 break;
1849 priv->video_curpos = priv->index_table[vid][priv->current_vpacket].offset;
1850 priv->audio_need_keyframe = !priv->is_multirate;
1851 priv->video_after_seek = 1;
1853 if (streams & 2) {
1854 if (!(streams & 1)) {
1855 cur_timestamp =
1856 priv->index_table[aid][priv->current_apacket].timestamp;
1858 if (rel_seek_secs > 0)
1859 while ((priv->index_table[aid][priv->current_apacket].timestamp - cur_timestamp) < rel_seek_secs * 1000){
1860 priv->current_apacket += 1;
1861 if (priv->current_apacket >= priv->index_table_size[aid]) {
1862 priv->current_apacket = priv->index_table_size[aid] - 1;
1863 break;
1866 else if (rel_seek_secs < 0)
1867 while ((cur_timestamp - priv->index_table[aid][priv->current_apacket].timestamp) < - rel_seek_secs * 1000){
1868 priv->current_apacket -= 1;
1869 if (priv->current_apacket < 0) {
1870 priv->current_apacket = 0;
1871 break;
1874 priv->audio_curpos = priv->index_table[aid][priv->current_apacket].offset;
1876 // }
1877 next_offset = streams & 1 ? priv->video_curpos : priv->audio_curpos;
1878 // printf("seek: pos: %d, current packets: a: %d, v: %d\n",
1879 // next_offset, priv->current_apacket, priv->current_vpacket);
1880 if (next_offset)
1881 stream_seek(demuxer->stream, next_offset);
1883 demux_real_fill_buffer(demuxer, NULL);
1886 static int demux_real_control(demuxer_t *demuxer, int cmd, void *arg)
1888 real_priv_t *priv = demuxer->priv;
1889 unsigned int lastpts = priv->v_pts ? priv->v_pts : priv->a_pts;
1891 switch (cmd) {
1892 case DEMUXER_CTRL_GET_TIME_LENGTH:
1893 if (priv->duration == 0)
1894 return DEMUXER_CTRL_DONTKNOW;
1896 *((double *)arg) = (double)priv->duration;
1897 return DEMUXER_CTRL_OK;
1899 case DEMUXER_CTRL_GET_PERCENT_POS:
1900 if (priv->duration == 0)
1901 return DEMUXER_CTRL_DONTKNOW;
1903 *((int *)arg) = (int)(100 * lastpts / priv->duration);
1904 return DEMUXER_CTRL_OK;
1906 default:
1907 return DEMUXER_CTRL_NOTIMPL;
1912 const demuxer_desc_t demuxer_desc_real = {
1913 "Realmedia demuxer",
1914 "real",
1915 "REAL",
1916 "Alex Beregszasi, Florian Schneider, A'rpi, Roberto Togni",
1917 "handles new .RMF files",
1918 DEMUXER_TYPE_REAL,
1919 1, // safe autodetect
1920 real_check_file,
1921 demux_real_fill_buffer,
1922 demux_open_real,
1923 demux_close_real,
1924 demux_seek_real,
1925 demux_real_control