stream_cddb: Remove unused static functions
[mplayer/glamo.git] / libmpdemux / demux_real.c
blob99abb03827e29c5f26000a6987b27fe1f43d9b89
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 "mpbswap.h"
46 #include "stream/stream.h"
47 #include "aviprint.h"
48 #include "demuxer.h"
49 #include "stheader.h"
50 #include "demux_real.h"
52 //#define mp_dbg(mod,lev, args... ) mp_msg_c((mod<<8)|lev, ## args )
54 #define MKTAG(a, b, c, d) (a | (b << 8) | (c << 16) | (d << 24))
56 #define MAX_STREAMS 32
58 static unsigned char sipr_swaps[38][2]={
59 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
60 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
61 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
62 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
63 {77,80} };
65 typedef struct {
66 unsigned int timestamp;
67 int offset;
68 // int packetno;
69 // int len; /* only filled by our index generator */
70 // int flags; /* only filled by our index generator */
71 } real_index_table_t;
73 typedef struct {
74 /* for seeking */
75 int index_chunk_offset;
76 real_index_table_t *index_table[MAX_STREAMS];
78 // int *index_table[MAX_STREAMS];
79 int index_table_size[MAX_STREAMS];
80 int index_malloc_size[MAX_STREAMS];
81 int data_chunk_offset;
82 int num_of_packets;
83 int current_packet;
85 // need for seek
86 int audio_need_keyframe;
87 int video_after_seek;
89 int current_apacket;
90 int current_vpacket;
92 // timestamp correction:
93 int64_t kf_base;// timestamp of the prev. video keyframe
94 unsigned int kf_pts; // timestamp of next video keyframe
95 unsigned int a_pts; // previous audio timestamp
96 double v_pts; // previous video timestamp
97 unsigned long duration;
99 /* stream id table */
100 // int last_a_stream;
101 // int a_streams[MAX_STREAMS];
102 // int last_v_stream;
103 // int v_streams[MAX_STREAMS];
106 * Used to demux multirate files
108 int is_multirate; ///< != 0 for multirate files
109 int str_data_offset[MAX_STREAMS]; ///< Data chunk offset for every audio/video stream
110 int audio_curpos; ///< Current file position for audio demuxing
111 int video_curpos; ///< Current file position for video demuxing
112 int a_num_of_packets; ///< Number of audio packets
113 int v_num_of_packets; ///< Number of video packets
114 int a_bitrate; ///< Audio bitrate
115 int v_bitrate; ///< Video bitrate
116 int stream_switch; ///< Flag used to switch audio/video demuxing
119 * Used to reorder audio data
121 unsigned int intl_id[MAX_STREAMS]; ///< interleaver id, per stream
122 int sub_packet_size[MAX_STREAMS]; ///< sub packet size, per stream
123 int sub_packet_h[MAX_STREAMS]; ///< number of coded frames per block
124 int coded_framesize[MAX_STREAMS]; ///< coded frame size, per stream
125 int audiopk_size[MAX_STREAMS]; ///< audio packet size
126 unsigned char *audio_buf; ///< place to store reordered audio data
127 double *audio_timestamp; ///< timestamp for each audio packet
128 int sub_packet_cnt; ///< number of subpacket already received
129 int audio_filepos; ///< file position of first audio packet in block
130 } real_priv_t;
132 //! use at most 200 MB of memory for index, corresponds to around 25 million entries
133 #define MAX_INDEX_ENTRIES (200*1024*1024 / sizeof(real_index_table_t))
135 /* originally from FFmpeg */
136 static void get_str(int isbyte, demuxer_t *demuxer, char *buf, int buf_size)
138 int len;
140 if (isbyte)
141 len = stream_read_char(demuxer->stream);
142 else
143 len = stream_read_word(demuxer->stream);
145 stream_read(demuxer->stream, buf, (len > buf_size) ? buf_size : len);
146 if (len > buf_size)
147 stream_skip(demuxer->stream, len-buf_size);
149 mp_msg(MSGT_DEMUX, MSGL_V, "read_str: %d bytes read\n", len);
152 static void dump_index(demuxer_t *demuxer, int stream_id)
154 real_priv_t *priv = demuxer->priv;
155 real_index_table_t *index;
156 int i, entries;
158 if (!mp_msg_test(MSGT_DEMUX,MSGL_V))
159 return;
161 if ((unsigned)stream_id >= MAX_STREAMS)
162 return;
164 index = priv->index_table[stream_id];
165 entries = priv->index_table_size[stream_id];
167 mp_msg(MSGT_DEMUX, MSGL_V, "Index table for stream %d\n", stream_id);
168 for (i = 0; i < entries; i++)
170 #if 1
171 mp_msg(MSGT_DEMUX, MSGL_V,"i: %d, pos: %d, timestamp: %u\n", i, index[i].offset, index[i].timestamp);
172 #else
173 mp_msg(MSGT_DEMUX, MSGL_V,"packetno: %x pos: %x len: %x timestamp: %x flags: %x\n",
174 index[i].packetno, index[i].offset, index[i].len, index[i].timestamp,
175 index[i].flags);
176 #endif
180 static int parse_index_chunk(demuxer_t *demuxer)
182 real_priv_t *priv = demuxer->priv;
183 int origpos = stream_tell(demuxer->stream);
184 int next_header_pos = priv->index_chunk_offset;
185 int i, entries, stream_id;
187 read_index:
188 stream_seek(demuxer->stream, next_header_pos);
190 i = stream_read_dword_le(demuxer->stream);
191 if ((i == -256) || (i != MKTAG('I', 'N', 'D', 'X')))
193 mp_msg(MSGT_DEMUX, MSGL_WARN,"Something went wrong, no index chunk found on given address (%d)\n",
194 next_header_pos);
195 index_mode = -1;
196 if (i == -256)
197 stream_reset(demuxer->stream);
198 stream_seek(demuxer->stream, origpos);
199 return 0;
200 //goto end;
203 mp_msg(MSGT_DEMUX, MSGL_V,"Reading index table from index chunk (%d)\n",
204 next_header_pos);
206 i = stream_read_dword(demuxer->stream);
207 mp_msg(MSGT_DEMUX, MSGL_V,"size: %d bytes\n", i);
209 i = stream_read_word(demuxer->stream);
210 if (i != 0)
211 mp_msg(MSGT_DEMUX, MSGL_WARN,"Hmm, index table with unknown version (%d), please report it to MPlayer developers!\n", i);
213 entries = stream_read_dword(demuxer->stream);
214 mp_msg(MSGT_DEMUX, MSGL_V,"entries: %d\n", entries);
216 stream_id = stream_read_word(demuxer->stream);
217 mp_msg(MSGT_DEMUX, MSGL_V,"stream_id: %d\n", stream_id);
219 next_header_pos = stream_read_dword(demuxer->stream);
220 mp_msg(MSGT_DEMUX, MSGL_V,"next_header_pos: %d\n", next_header_pos);
221 if (entries <= 0 || entries > MAX_INDEX_ENTRIES)
223 if (next_header_pos)
224 goto read_index;
225 i = entries;
226 goto end;
229 priv->index_table_size[stream_id] = entries;
230 priv->index_table[stream_id] = calloc(priv->index_table_size[stream_id], sizeof(real_index_table_t));
232 for (i = 0; i < entries; i++)
234 stream_skip(demuxer->stream, 2); /* version */
235 priv->index_table[stream_id][i].timestamp = stream_read_dword(demuxer->stream);
236 priv->index_table[stream_id][i].offset = stream_read_dword(demuxer->stream);
237 stream_skip(demuxer->stream, 4); /* packetno */
238 // priv->index_table[stream_id][i].packetno = stream_read_dword(demuxer->stream);
239 // printf("Index table: Stream#%d: entry: %d: pos: %d\n",
240 // stream_id, i, priv->index_table[stream_id][i].offset);
243 dump_index(demuxer, stream_id);
245 if (next_header_pos > 0)
246 goto read_index;
248 end:
249 if (i == -256)
250 stream_reset(demuxer->stream);
251 stream_seek(demuxer->stream, origpos);
252 if (i == -256)
253 return 0;
254 else
255 return 1;
258 #if 1
260 static void add_index_item(demuxer_t *demuxer, int stream_id, unsigned int timestamp, int offset)
262 if ((unsigned)stream_id < MAX_STREAMS)
264 real_priv_t *priv = demuxer->priv;
265 real_index_table_t *index;
266 if (priv->index_table_size[stream_id] >= MAX_INDEX_ENTRIES) {
267 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Index too large during building\n");
268 return;
270 if (priv->index_table_size[stream_id] >= priv->index_malloc_size[stream_id])
272 if (priv->index_malloc_size[stream_id] == 0)
273 priv->index_malloc_size[stream_id] = 2048;
274 else
275 priv->index_malloc_size[stream_id] += priv->index_malloc_size[stream_id] / 2;
276 // in case we have a really large chunk...
277 if (priv->index_table_size[stream_id] >=
278 priv->index_malloc_size[stream_id])
279 priv->index_malloc_size[stream_id] =
280 priv->index_table_size[stream_id] + 1;
281 priv->index_table[stream_id] = realloc(priv->index_table[stream_id], priv->index_malloc_size[stream_id]*sizeof(priv->index_table[0][0]));
283 if (priv->index_table_size[stream_id] > 0)
285 index = &priv->index_table[stream_id][priv->index_table_size[stream_id] - 1];
286 if (index->timestamp >= timestamp || index->offset >= offset)
287 return;
289 index = &priv->index_table[stream_id][priv->index_table_size[stream_id]++];
290 index->timestamp = timestamp;
291 index->offset = offset;
295 static void add_index_segment(demuxer_t *demuxer, int seek_stream_id, int64_t seek_timestamp)
297 int tag, len, stream_id, flags;
298 unsigned int timestamp;
299 if (seek_timestamp != -1 && (unsigned)seek_stream_id >= MAX_STREAMS)
300 return;
301 while (1)
303 demuxer->filepos = stream_tell(demuxer->stream);
305 tag = stream_read_dword(demuxer->stream);
306 if (tag == MKTAG('A', 'T', 'A', 'D'))
308 stream_skip(demuxer->stream, 14);
309 continue; /* skip to next loop */
311 len = tag & 0xffff;
312 if (tag == -256 || len < 12)
313 break;
315 stream_id = stream_read_word(demuxer->stream);
316 timestamp = stream_read_dword(demuxer->stream);
318 stream_skip(demuxer->stream, 1); /* reserved */
319 flags = stream_read_char(demuxer->stream);
321 if (flags == -256)
322 break;
324 if (flags & 2)
326 add_index_item(demuxer, stream_id, timestamp, demuxer->filepos);
327 if (stream_id == seek_stream_id && timestamp >= seek_timestamp)
329 stream_seek(demuxer->stream, demuxer->filepos);
330 return;
333 // 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);
334 /* skip data */
335 stream_skip(demuxer->stream, len-12);
339 static int generate_index(demuxer_t *demuxer)
341 real_priv_t *priv = demuxer->priv;
342 int origpos = stream_tell(demuxer->stream);
343 int data_pos = priv->data_chunk_offset-10;
344 int i;
345 int tag;
347 stream_seek(demuxer->stream, data_pos);
348 tag = stream_read_dword(demuxer->stream);
349 if (tag != MKTAG('A', 'T', 'A', 'D'))
351 mp_msg(MSGT_DEMUX, MSGL_WARN,"Something went wrong, no data chunk found on given address (%d)\n", data_pos);
353 else
355 stream_skip(demuxer->stream, 14);
356 add_index_segment(demuxer, -1, -1);
358 for (i = 0; i < MAX_STREAMS; i++)
360 if (priv->index_table_size[i] > 0)
362 dump_index(demuxer, i);
365 stream_reset(demuxer->stream);
366 stream_seek(demuxer->stream, origpos);
367 return 0;
370 #else
372 static int generate_index(demuxer_t *demuxer)
374 real_priv_t *priv = demuxer->priv;
375 int origpos = stream_tell(demuxer->stream);
376 int data_pos = priv->data_chunk_offset-10;
377 int num_of_packets = 0;
378 int i, entries = 0;
379 int len, stream_id = 0, flags;
380 unsigned int timestamp;
381 int tab_pos = 0;
383 read_index:
384 stream_seek(demuxer->stream, data_pos);
386 i = stream_read_dword_le(demuxer->stream);
387 if ((i == -256) || (i != MKTAG('D', 'A', 'T', 'A')))
389 mp_msg(MSGT_DEMUX, MSGL_WARN,"Something went wrong, no data chunk found on given address (%d)\n",
390 data_pos);
391 goto end;
393 stream_skip(demuxer->stream, 4); /* chunk size */
394 stream_skip(demuxer->stream, 2); /* version */
396 num_of_packets = stream_read_dword(demuxer->stream);
397 mp_msg(MSGT_DEMUX, MSGL_V,"Generating index table from raw data (pos: 0x%x) for %d packets\n",
398 data_pos, num_of_packets);
400 data_pos = stream_read_dword_le(demuxer->stream)-10; /* next data chunk */
402 for (i = 0; i < MAX_STREAMS; i++)
404 priv->index_table_size[i] = num_of_packets;
405 priv->index_table[i] = calloc(priv->index_table_size[i], sizeof(real_index_table_t));
406 // priv->index_table[stream_id] = realloc(priv->index_table[stream_id],
407 // priv->index_table_size[stream_id] * sizeof(real_index_table_t));
410 tab_pos = 0;
412 // memset(priv->index_table_size, 0, sizeof(int)*MAX_STREAMS);
413 // memset(priv->index_table, 0, sizeof(real_index_table_t)*MAX_STREAMS);
415 while (tab_pos < num_of_packets)
417 i = stream_read_char(demuxer->stream);
418 if (i == -256)
419 goto end;
420 stream_skip(demuxer->stream, 1);
421 // stream_skip(demuxer->stream, 2); /* version */
423 len = stream_read_word(demuxer->stream);
424 stream_id = stream_read_word(demuxer->stream);
425 timestamp = stream_read_dword(demuxer->stream);
427 stream_skip(demuxer->stream, 1); /* reserved */
428 flags = stream_read_char(demuxer->stream);
430 i = tab_pos;
432 // priv->index_table_size[stream_id] = i;
433 // if (priv->index_table[stream_id] == NULL)
434 // priv->index_table[stream_id] = malloc(priv->index_table_size[stream_id] * sizeof(real_index_table_t));
435 // else
436 // priv->index_table[stream_id] = realloc(priv->index_table[stream_id],
437 // priv->index_table_size[stream_id] * sizeof(real_index_table_t));
439 priv->index_table[stream_id][i].timestamp = timestamp;
440 priv->index_table[stream_id][i].offset = stream_tell(demuxer->stream)-12;
441 priv->index_table[stream_id][i].len = len;
442 priv->index_table[stream_id][i].packetno = entries;
443 priv->index_table[stream_id][i].flags = flags;
445 tab_pos++;
447 /* skip data */
448 stream_skip(demuxer->stream, len-12);
450 dump_index(demuxer, stream_id);
451 if (data_pos)
452 goto read_index;
454 end:
455 if (i == -256)
456 stream_reset(demuxer->stream);
457 stream_seek(demuxer->stream, origpos);
458 if (i == -256)
459 return 0;
460 else
461 return 1;
463 #endif
466 static int real_check_file(demuxer_t* demuxer)
468 real_priv_t *priv;
469 int c;
471 mp_msg(MSGT_DEMUX,MSGL_V,"Checking for REAL\n");
473 c = stream_read_dword_le(demuxer->stream);
474 if (c == -256)
475 return 0; /* EOF */
476 if (c != MKTAG('.', 'R', 'M', 'F'))
477 return 0; /* bad magic */
479 priv = malloc(sizeof(real_priv_t));
480 memset(priv, 0, sizeof(real_priv_t));
481 demuxer->priv = priv;
483 return DEMUXER_TYPE_REAL;
486 void hexdump(char *, unsigned long);
488 #define SKIP_BITS(n) buffer<<=n
489 #define SHOW_BITS(n) ((buffer)>>(32-(n)))
491 double real_fix_timestamp(unsigned char *buf, unsigned int timestamp, unsigned int format, int64_t *kf_base, int *kf_pts, double *pts){
492 double v_pts;
493 unsigned char *s = buf + 1 + (*buf+1)*8;
494 uint32_t buffer= (s[0]<<24) + (s[1]<<16) + (s[2]<<8) + s[3];
495 unsigned int kf=timestamp;
496 int pict_type;
497 unsigned int orig_kf;
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 v_pts=kf*0.001f;
534 // if(pts && (v_pts<*pts || !kf)) v_pts=*pts+frametime;
535 if(pts) *pts=v_pts;
536 return v_pts;
539 typedef struct dp_hdr_s {
540 uint32_t chunks; // number of chunks
541 uint32_t timestamp; // timestamp from packet header
542 uint32_t len; // length of actual data
543 uint32_t chunktab; // offset to chunk offset array
544 } dp_hdr_t;
546 static void queue_video_packet(real_priv_t *priv, demux_stream_t *ds, demux_packet_t *dp)
548 dp_hdr_t hdr = *(dp_hdr_t*)dp->buffer;
549 unsigned char *tmp = malloc(8*(1+hdr.chunks));
550 memcpy(tmp, dp->buffer+hdr.chunktab, 8*(1+hdr.chunks));
551 memmove(dp->buffer+1+8*(1+hdr.chunks), dp->buffer+sizeof(dp_hdr_t), hdr.len);
552 memcpy(dp->buffer+1, tmp, 8*(1+hdr.chunks));
553 *dp->buffer = (uint8_t)hdr.chunks;
554 free(tmp);
556 if(priv->video_after_seek){
557 priv->kf_base = 0;
558 priv->kf_pts = hdr.timestamp;
559 priv->video_after_seek = 0;
561 if(hdr.len >= 3) /* this check may be useless */
562 dp->pts = real_fix_timestamp(dp->buffer, hdr.timestamp,
563 ((sh_video_t *)ds->sh)->format,
564 &priv->kf_base, &priv->kf_pts,
565 &priv->v_pts);
566 ds_add_packet(ds, dp);
569 // return value:
570 // 0 = EOF or no stream found
571 // 1 = successfully read a packet
572 static int demux_real_fill_buffer(demuxer_t *demuxer, demux_stream_t *dsds)
574 real_priv_t *priv = demuxer->priv;
575 demux_stream_t *ds = NULL;
576 int len;
577 unsigned int timestamp;
578 int stream_id;
579 #ifdef CRACK_MATRIX
580 int i;
581 #endif
582 int flags;
583 int version;
584 int reserved;
585 demux_packet_t *dp;
586 int x, sps, cfs, sph, spc, w;
587 int audioreorder_getnextpk = 0;
589 // Don't demux video if video codec init failed
590 if (demuxer->video->id >= 0 && !demuxer->video->sh)
591 demuxer->video->id = -2;
593 while(!stream_eof(demuxer->stream)){
595 /* Handle audio/video demxing switch for multirate files (non-interleaved) */
596 if (priv->is_multirate && priv->stream_switch) {
597 if (priv->current_apacket >= priv->index_table_size[demuxer->audio->id])
598 demuxer->audio->eof = 1;
599 if (priv->current_vpacket >= priv->index_table_size[demuxer->video->id])
600 demuxer->video->eof = 1;
601 if (demuxer->audio->eof && demuxer->video->eof)
602 return 0;
603 else if (!demuxer->audio->eof && demuxer->video->eof)
604 stream_seek(demuxer->stream, priv->audio_curpos); // Get audio
605 else if (demuxer->audio->eof && !demuxer->video->eof)
606 stream_seek(demuxer->stream, priv->video_curpos); // Get video
607 else if (priv->index_table[demuxer->audio->id][priv->current_apacket].timestamp <
608 priv->index_table[demuxer->video->id][priv->current_vpacket].timestamp)
609 stream_seek(demuxer->stream, priv->audio_curpos); // Get audio
610 else
611 stream_seek(demuxer->stream, priv->video_curpos); // Get video
612 priv->stream_switch = 0;
615 demuxer->filepos = stream_tell(demuxer->stream);
616 version = stream_read_word(demuxer->stream); /* version */
617 len = stream_read_word(demuxer->stream);
618 if ((version==0x4441) && (len==0x5441)) { // new data chunk
619 mp_msg(MSGT_DEMUX,MSGL_INFO,"demux_real: New data chunk is coming!!!\n");
620 if (priv->is_multirate)
621 return 0; // EOF
622 stream_skip(demuxer->stream,14);
623 demuxer->filepos = stream_tell(demuxer->stream);
624 version = stream_read_word(demuxer->stream); /* version */
625 len = stream_read_word(demuxer->stream);
626 } else if ((version == 0x494e) && (len == 0x4458)) {
627 mp_msg(MSGT_DEMUX,MSGL_V,"demux_real: Found INDX chunk. EOF.\n");
628 demuxer->stream->eof=1;
629 return 0;
633 if (len == -256){ /* EOF */
634 // printf("len==-256!\n");
635 return 0;
637 if (len < 12){
638 mp_msg(MSGT_DEMUX, MSGL_V,"%08X: packet v%d len=%d \n",(int)demuxer->filepos,(int)version,(int)len);
639 mp_msg(MSGT_DEMUX, MSGL_WARN,"bad packet len (%d)\n", len);
640 if ((unsigned)demuxer->video->id < MAX_STREAMS) {
641 if (priv->current_vpacket + 1 < priv->index_table_size[demuxer->video->id]) {
642 stream_seek(demuxer->stream, priv->index_table[demuxer->video->id][++priv->current_vpacket].offset);
644 } else if ((unsigned)demuxer->audio->id < MAX_STREAMS) {
645 if (priv->current_apacket + 1 < priv->index_table_size[demuxer->audio->id]) {
646 stream_seek(demuxer->stream, priv->index_table[demuxer->audio->id][++priv->current_apacket].offset);
649 continue; //goto loop;
652 stream_id = stream_read_word(demuxer->stream);
653 timestamp = stream_read_dword(demuxer->stream);
654 reserved = stream_read_char(demuxer->stream);
655 flags = stream_read_char(demuxer->stream);
656 /* flags: */
657 /* 0x1 - reliable */
658 /* 0x2 - keyframe */
660 if (version == 1) {
661 int tmp;
662 tmp = stream_read_char(demuxer->stream);
663 mp_msg(MSGT_DEMUX, MSGL_DBG2,"Version: %d, skipped byte is %d\n", version, tmp);
664 len--;
667 if (flags & 2)
668 add_index_item(demuxer, stream_id, timestamp, demuxer->filepos);
670 // printf("%08X: packet v%d len=%4d id=%d pts=%6d rvd=%d flags=%d \n",
671 // (int)demuxer->filepos,(int)version,(int)len, stream_id,
672 // (int) timestamp, reserved, flags);
674 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "\npacket#%d: pos: 0x%0x, len: %d, id: %d, pts: %u, flags: %x rvd:%d\n",
675 priv->current_packet, (int)demuxer->filepos, len, stream_id, timestamp, flags, reserved);
677 priv->current_packet++;
678 len -= 12;
680 // printf("s_id=%d aid=%d vid=%d \n",stream_id,demuxer->audio->id,demuxer->video->id);
682 /* check stream_id: */
684 if(demuxer->audio->id==stream_id){
685 if (priv->audio_need_keyframe == 1&& flags != 0x2)
686 goto discard;
687 got_audio:
688 ds=demuxer->audio;
689 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "packet is audio (id: %d)\n", stream_id);
691 if (flags & 2) {
692 priv->sub_packet_cnt = 0;
693 audioreorder_getnextpk = 0;
696 // parse audio chunk:
698 #ifdef CRACK_MATRIX
699 int spos=stream_tell(demuxer->stream);
700 static int cnt=0;
701 static int cnt2=CRACK_MATRIX;
702 #endif
703 if (((sh_audio_t *)ds->sh)->format == mmioFOURCC('M', 'P', '4', 'A')) {
704 uint16_t sub_packet_lengths[16], sub_packets, i;
705 int totlen = 0;
706 /* AAC in Real: several AAC frames in one Real packet. */
707 /* Second byte, upper four bits: number of AAC frames */
708 /* next n * 2 bytes: length of the AAC frames in bytes, BE */
709 if (len < 2)
710 goto discard;
711 sub_packets = (stream_read_word(demuxer->stream) & 0xf0) >> 4;
712 if (len < 2 * sub_packets)
713 goto discard;
714 for (i = 0; i < sub_packets; i++)
715 totlen += sub_packet_lengths[i] = stream_read_word(demuxer->stream);
716 if (len < totlen )
717 goto discard;
718 for (i = 0; i < sub_packets; i++) {
719 demux_packet_t *dp = new_demux_packet(sub_packet_lengths[i]);
720 stream_read(demuxer->stream, dp->buffer, sub_packet_lengths[i]);
721 if (priv->a_pts != timestamp)
722 dp->pts = timestamp / 1000.0;
723 priv->a_pts = timestamp;
724 dp->pos = demuxer->filepos;
725 ds_add_packet(ds, dp);
727 return 1;
729 if ((priv->intl_id[stream_id] == mmioFOURCC('I', 'n', 't', '4')) ||
730 (priv->intl_id[stream_id] == mmioFOURCC('g', 'e', 'n', 'r')) ||
731 (priv->intl_id[stream_id] == mmioFOURCC('s', 'i', 'p', 'r'))) {
732 sps = priv->sub_packet_size[stream_id];
733 sph = priv->sub_packet_h[stream_id];
734 cfs = priv->coded_framesize[stream_id];
735 w = priv->audiopk_size[stream_id];
736 spc = priv->sub_packet_cnt;
737 switch (priv->intl_id[stream_id]) {
738 case mmioFOURCC('I', 'n', 't', '4'):
739 if (len < cfs * sph/2)
740 goto discard;
741 for (x = 0; x < sph / 2; x++)
742 stream_read(demuxer->stream, priv->audio_buf + x * 2 * w + spc * cfs, cfs);
743 break;
744 case mmioFOURCC('g', 'e', 'n', 'r'):
745 if (len < w)
746 goto discard;
747 for (x = 0; x < w / sps; x++)
748 stream_read(demuxer->stream, priv->audio_buf + sps * (sph * x + ((sph + 1) / 2) * (spc & 1) +
749 (spc >> 1)), sps);
750 break;
751 case mmioFOURCC('s', 'i', 'p', 'r'):
752 if (len < w)
753 goto discard;
754 stream_read(demuxer->stream, priv->audio_buf + spc * w, w);
755 if (spc == sph - 1) {
756 int n;
757 int bs = sph * w * 2 / 96; // nibbles per subpacket
758 // Perform reordering
759 for(n=0; n < 38; n++) {
760 int j;
761 int i = bs * sipr_swaps[n][0];
762 int o = bs * sipr_swaps[n][1];
763 // swap nibbles of block 'i' with 'o' TODO: optimize
764 for(j = 0;j < bs; j++) {
765 int x = (i & 1) ? (priv->audio_buf[i >> 1] >> 4) : (priv->audio_buf[i >> 1] & 0x0F);
766 int y = (o & 1) ? (priv->audio_buf[o >> 1] >> 4) : (priv->audio_buf[o >> 1] & 0x0F);
767 if(o & 1)
768 priv->audio_buf[o >> 1] = (priv->audio_buf[o >> 1] & 0x0F) | (x << 4);
769 else
770 priv->audio_buf[o >> 1] = (priv->audio_buf[o >> 1] & 0xF0) | x;
771 if(i & 1)
772 priv->audio_buf[i >> 1] = (priv->audio_buf[i >> 1] & 0x0F) | (y << 4);
773 else
774 priv->audio_buf[i >> 1] = (priv->audio_buf[i >> 1] & 0xF0) | y;
775 ++i; ++o;
779 break;
781 priv->audio_need_keyframe = 0;
782 priv->audio_timestamp[priv->sub_packet_cnt] = (priv->a_pts==timestamp) ? MP_NOPTS_VALUE : (timestamp/1000.0);
783 priv->a_pts = timestamp;
784 if (priv->sub_packet_cnt == 0)
785 priv->audio_filepos = demuxer->filepos;
786 if (++(priv->sub_packet_cnt) < sph)
787 audioreorder_getnextpk = 1;
788 else {
789 int apk_usize = ((WAVEFORMATEX*)((sh_audio_t*)ds->sh)->wf)->nBlockAlign;
790 audioreorder_getnextpk = 0;
791 priv->sub_packet_cnt = 0;
792 // Release all the audio packets
793 for (x = 0; x < sph*w/apk_usize; x++) {
794 dp = new_demux_packet(apk_usize);
795 memcpy(dp->buffer, priv->audio_buf + x * apk_usize, apk_usize);
796 /* Put timestamp only on packets that correspond to original audio packets in file */
797 if (x * apk_usize % w == 0)
798 dp->pts = priv->audio_timestamp[x * apk_usize / w];
799 dp->pos = priv->audio_filepos; // all equal
800 dp->flags = x ? 0 : 0x10; // Mark first packet as keyframe
801 ds_add_packet(ds, dp);
804 } else { // No interleaving
805 dp = new_demux_packet(len);
806 stream_read(demuxer->stream, dp->buffer, len);
808 #ifdef CRACK_MATRIX
809 mp_msg(MSGT_DEMUX, MSGL_V,"*** audio block len=%d\n",len);
810 { // HACK - used for reverse engineering the descrambling matrix
811 FILE* f=fopen("test.rm","r+");
812 fseek(f,spos,SEEK_SET);
813 ++cnt;
814 // for(i=0;i<len;i++) dp->buffer[i]=i/0x12;
815 // for(i=0;i<len;i++) dp->buffer[i]=i;
816 // for(i=0;i<len;i++) dp->buffer[i]=cnt;
817 // for(i=0;i<len;i++) dp->buffer[i]=cnt<<4;
818 for(i=0;i<len;i++) dp->buffer[i]=(i==cnt2) ? (cnt+16*(8+cnt)) : 0;
819 if(cnt==6){ cnt=0; ++cnt2; }
820 fwrite(dp->buffer, len, 1, f);
821 fclose(f);
822 if(cnt2>0x150) *((int*)NULL)=1; // sig11 :)
824 #endif
825 #if 0
826 if( ((sh_audio_t *)ds->sh)->format == 0x2000) {
827 // if DNET, swap bytes, as DNET is byte-swapped AC3:
828 char *ptr = dp->buffer;
829 int i;
830 for (i = 0; i < len; i += 2)
832 const char tmp = ptr[0];
833 ptr[0] = ptr[1];
834 ptr[1] = tmp;
835 ptr += 2;
838 #endif
839 if (priv->audio_need_keyframe == 1) {
840 priv->audio_need_keyframe = 0;
841 } else if(priv->a_pts != timestamp)
842 dp->pts = timestamp/1000.0;
843 priv->a_pts=timestamp;
844 dp->pos = demuxer->filepos;
845 dp->flags = (flags & 0x2) ? 0x10 : 0;
846 ds_add_packet(ds, dp);
848 } // codec_id check, codec default case
850 // we will not use audio index if we use -idx and have a video
851 if(((!demuxer->video->sh && index_mode == 2) || priv->is_multirate) && (unsigned)demuxer->audio->id < MAX_STREAMS) {
852 while (priv->current_apacket + 1 < priv->index_table_size[demuxer->audio->id] &&
853 timestamp > priv->index_table[demuxer->audio->id][priv->current_apacket].timestamp) {
854 priv->current_apacket += 1;
855 priv->stream_switch = 1;
857 if (priv->stream_switch)
858 priv->audio_curpos = stream_tell(demuxer->stream);
861 // If we're reordering audio packets and we need more data get it
862 if (audioreorder_getnextpk)
863 continue;
865 return 1;
868 if(demuxer->video->id==stream_id){
869 got_video:
870 ds=demuxer->video;
871 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "packet is video (id: %d)\n", stream_id);
873 // parse video chunk:
875 // we need a more complicated, 2nd level demuxing, as the video
876 // frames are stored fragmented in the video chunks :(
877 sh_video_t *sh_video = ds->sh;
878 demux_packet_t *dp;
879 unsigned vpkg_header, vpkg_length, vpkg_offset;
880 int vpkg_seqnum=-1;
881 int vpkg_subseq=0;
883 while(len>2){
884 dp_hdr_t* dp_hdr;
885 unsigned char* dp_data;
886 uint32_t* extra;
888 // printf("xxx len=%d \n",len);
890 // read packet header
891 // bit 7: 1=last block in block chain
892 // bit 6: 1=short header (only one block?)
893 vpkg_header=stream_read_char(demuxer->stream); --len;
894 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "hdr: %02X (len=%d) ",vpkg_header,len);
896 if (0x40==(vpkg_header&0xc0)) {
897 // seems to be a very short header
898 // 2 bytes, purpose of the second byte yet unknown
899 int bummer;
900 bummer=stream_read_char(demuxer->stream); --len;
901 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "%02X",bummer);
902 vpkg_offset=0;
903 vpkg_length=len;
904 } else {
906 if (0==(vpkg_header&0x40)) {
907 // sub-seqnum (bits 0-6: number of fragment. bit 7: ???)
908 vpkg_subseq=stream_read_char(demuxer->stream);
909 --len;
910 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "subseq: %02X ",vpkg_subseq);
911 vpkg_subseq&=0x7f;
914 // size of the complete packet
915 // bit 14 is always one (same applies to the offset)
916 vpkg_length=stream_read_word(demuxer->stream);
917 len-=2;
918 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "l: %02X %02X ",vpkg_length>>8,vpkg_length&0xff);
919 if (!(vpkg_length&0xC000)) {
920 vpkg_length<<=16;
921 vpkg_length|=(uint16_t)stream_read_word(demuxer->stream);
922 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "l+: %02X %02X ",(vpkg_length>>8)&0xff,vpkg_length&0xff);
923 len-=2;
924 } else
925 vpkg_length&=0x3fff;
927 // offset of the following data inside the complete packet
928 // Note: if (hdr&0xC0)==0x80 then offset is relative to the
929 // _end_ of the packet, so it's equal to fragment size!!!
930 vpkg_offset=stream_read_word(demuxer->stream);
931 len-=2;
932 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "o: %02X %02X ",vpkg_offset>>8,vpkg_offset&0xff);
933 if (!(vpkg_offset&0xC000)) {
934 vpkg_offset<<=16;
935 vpkg_offset|=(uint16_t)stream_read_word(demuxer->stream);
936 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "o+: %02X %02X ",(vpkg_offset>>8)&0xff,vpkg_offset&0xff);
937 len-=2;
938 } else
939 vpkg_offset&=0x3fff;
941 vpkg_seqnum=stream_read_char(demuxer->stream); --len;
942 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "seq: %02X ",vpkg_seqnum);
944 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "\n");
945 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "blklen=%d\n", len);
946 mp_msg(MSGT_DEMUX,MSGL_DBG2, "block: hdr=0x%0x, len=%d, offset=%d, seqnum=%d\n",
947 vpkg_header, vpkg_length, vpkg_offset, vpkg_seqnum);
949 if(ds->asf_packet){
950 dp=ds->asf_packet;
951 dp_hdr=(dp_hdr_t*)dp->buffer;
952 dp_data=dp->buffer+sizeof(dp_hdr_t);
953 extra=(uint32_t*)(dp->buffer+dp_hdr->chunktab);
954 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "we have an incomplete packet (oldseq=%d new=%d)\n",ds->asf_seq,vpkg_seqnum);
955 // we have an incomplete packet:
956 if(ds->asf_seq!=vpkg_seqnum){
957 // this fragment is for new packet, close the old one
958 mp_msg(MSGT_DEMUX,MSGL_DBG2, "closing probably incomplete packet, len: %d \n",dp->len);
959 queue_video_packet(priv, ds, dp);
960 ds->asf_packet=NULL;
961 } else {
962 // append data to it!
963 ++dp_hdr->chunks;
964 mp_msg(MSGT_DEMUX,MSGL_DBG2,"[chunks=%d subseq=%d]\n",dp_hdr->chunks,vpkg_subseq);
965 if(dp_hdr->chunktab+8*(1+dp_hdr->chunks)>dp->len){
966 // increase buffer size, this should not happen!
967 mp_msg(MSGT_DEMUX,MSGL_WARN, "chunktab buffer too small!!!!!\n");
968 dp->len=dp_hdr->chunktab+8*(4+dp_hdr->chunks);
969 dp->buffer=realloc(dp->buffer,dp->len+MP_INPUT_BUFFER_PADDING_SIZE);
970 memset(dp->buffer + dp->len, 0, MP_INPUT_BUFFER_PADDING_SIZE);
971 // re-calc pointers:
972 dp_hdr=(dp_hdr_t*)dp->buffer;
973 dp_data=dp->buffer+sizeof(dp_hdr_t);
974 extra=(uint32_t*)(dp->buffer+dp_hdr->chunktab);
976 extra[2*dp_hdr->chunks+0]=le2me_32(1);
977 extra[2*dp_hdr->chunks+1]=le2me_32(dp_hdr->len);
978 if(0x80==(vpkg_header&0xc0)){
979 // last fragment!
980 if(dp_hdr->len!=vpkg_length-vpkg_offset)
981 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);
982 if (vpkg_offset > dp->len - sizeof(dp_hdr_t) - dp_hdr->len) vpkg_offset = dp->len - sizeof(dp_hdr_t) - dp_hdr->len;
983 stream_read(demuxer->stream, dp_data+dp_hdr->len, vpkg_offset);
984 if((dp_data[dp_hdr->len]&0x20) && (sh_video->format==0x30335652)) --dp_hdr->chunks; else
985 dp_hdr->len+=vpkg_offset;
986 len-=vpkg_offset;
987 mp_dbg(MSGT_DEMUX,MSGL_DBG2, "fragment (%d bytes) appended, %d bytes left\n",vpkg_offset,len);
988 // we know that this is the last fragment -> we can close the packet!
989 queue_video_packet(priv, ds, dp);
990 ds->asf_packet=NULL;
991 // continue parsing
992 continue;
994 // non-last fragment:
995 if(dp_hdr->len!=vpkg_offset)
996 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);
997 if (len > dp->len - sizeof(dp_hdr_t) - dp_hdr->len) len = dp->len - sizeof(dp_hdr_t) - dp_hdr->len;
998 stream_read(demuxer->stream, dp_data+dp_hdr->len, len);
999 if((dp_data[dp_hdr->len]&0x20) && (sh_video->format==0x30335652)) --dp_hdr->chunks; else
1000 dp_hdr->len+=len;
1001 len=0;
1002 break; // no more fragments in this chunk!
1005 // create new packet!
1006 dp = new_demux_packet(sizeof(dp_hdr_t)+vpkg_length+8*(1+2*(vpkg_header&0x3F)));
1007 // the timestamp seems to be in milliseconds
1008 dp->pos = demuxer->filepos;
1009 dp->flags = (flags & 0x2) ? 0x10 : 0;
1010 ds->asf_seq = vpkg_seqnum;
1011 dp_hdr=(dp_hdr_t*)dp->buffer;
1012 dp_hdr->chunks=0;
1013 dp_hdr->timestamp=timestamp;
1014 dp_hdr->chunktab=sizeof(dp_hdr_t)+vpkg_length;
1015 dp_data=dp->buffer+sizeof(dp_hdr_t);
1016 extra=(uint32_t*)(dp->buffer+dp_hdr->chunktab);
1017 extra[0]=le2me_32(1); extra[1]=0; // offset of the first chunk
1018 if(0x00==(vpkg_header&0xc0)){
1019 // first fragment:
1020 if (len > dp->len - sizeof(dp_hdr_t)) len = dp->len - sizeof(dp_hdr_t);
1021 dp_hdr->len=len;
1022 stream_read(demuxer->stream, dp_data, len);
1023 ds->asf_packet=dp;
1024 len=0;
1025 break;
1027 // whole packet (not fragmented):
1028 if (vpkg_length > len) {
1029 mp_msg(MSGT_DEMUX, MSGL_WARN,"\n******** WARNING: vpkg_length=%i > len=%i ********\n", vpkg_length, len);
1031 * To keep the video stream rolling, we need to break
1032 * here. We shouldn't touch len to make sure rest of the
1033 * broken packet is skipped.
1035 break;
1037 dp_hdr->len=vpkg_length; len-=vpkg_length;
1038 stream_read(demuxer->stream, dp_data, vpkg_length);
1039 queue_video_packet(priv, ds, dp);
1041 } // while(len>0)
1043 if(len){
1044 mp_msg(MSGT_DEMUX, MSGL_WARN,"\n******** !!!!!!!! BUG!! len=%d !!!!!!!!!!! ********\n",len);
1045 if(len>0) stream_skip(demuxer->stream, len);
1048 if ((unsigned)demuxer->video->id < MAX_STREAMS) {
1049 while (priv->current_vpacket + 1 < priv->index_table_size[demuxer->video->id] &&
1050 timestamp > priv->index_table[demuxer->video->id][priv->current_vpacket + 1].timestamp) {
1051 priv->current_vpacket += 1;
1052 priv->stream_switch = 1;
1054 if (priv->stream_switch)
1055 priv->video_curpos = stream_tell(demuxer->stream);
1058 return 1;
1061 if((unsigned)stream_id<MAX_STREAMS){
1063 if(demuxer->audio->id==-1 && demuxer->a_streams[stream_id]){
1064 sh_audio_t *sh = demuxer->a_streams[stream_id];
1065 demuxer->audio->id=stream_id;
1066 sh->ds=demuxer->audio;
1067 demuxer->audio->sh=sh;
1068 priv->audio_buf = calloc(priv->sub_packet_h[demuxer->audio->id], priv->audiopk_size[demuxer->audio->id]);
1069 priv->audio_timestamp = calloc(priv->sub_packet_h[demuxer->audio->id], sizeof(double));
1070 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected RM audio ID = %d\n",stream_id);
1071 goto got_audio;
1074 if(demuxer->video->id==-1 && demuxer->v_streams[stream_id]){
1075 sh_video_t *sh = demuxer->v_streams[stream_id];
1076 demuxer->video->id=stream_id;
1077 sh->ds=demuxer->video;
1078 demuxer->video->sh=sh;
1079 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected RM video ID = %d\n",stream_id);
1080 goto got_video;
1085 mp_msg(MSGT_DEMUX,MSGL_DBG2, "unknown stream id (%d)\n", stream_id);
1086 discard:
1087 stream_skip(demuxer->stream, len);
1088 }// goto loop;
1089 return 0;
1092 static demuxer_t* demux_open_real(demuxer_t* demuxer)
1094 real_priv_t* priv = demuxer->priv;
1095 int num_of_headers;
1096 int a_streams=0;
1097 int v_streams=0;
1098 int i;
1099 int header_size;
1101 header_size = stream_read_dword(demuxer->stream); /* header size */
1102 mp_msg(MSGT_DEMUX,MSGL_V, "real: Header size: %d\n", header_size);
1103 i = stream_read_word(demuxer->stream); /* version */
1104 mp_msg(MSGT_DEMUX,MSGL_V, "real: Header object version: %d\n", i);
1105 if (header_size == 0x10)
1106 i = stream_read_word(demuxer->stream);
1107 else /* we should test header_size here too. */
1108 i = stream_read_dword(demuxer->stream);
1109 mp_msg(MSGT_DEMUX,MSGL_V, "real: File version: %d\n", i);
1110 num_of_headers = stream_read_dword(demuxer->stream);
1112 /* parse chunks */
1113 for (i = 1; i <= num_of_headers; i++)
1114 // for (i = 1; ; i++)
1116 int chunk_id, chunk_pos, chunk_size;
1118 chunk_pos = stream_tell(demuxer->stream);
1119 chunk_id = stream_read_dword_le(demuxer->stream);
1120 chunk_size = stream_read_dword(demuxer->stream);
1122 stream_skip(demuxer->stream, 2); /* version */
1124 mp_msg(MSGT_DEMUX,MSGL_V, "Chunk: %.4s (%x) (size: 0x%x, offset: 0x%x)\n",
1125 (char *)&chunk_id, chunk_id, chunk_size, chunk_pos);
1127 if (chunk_id != MKTAG('D', 'A', 'T', 'A') && chunk_size < 10){
1128 mp_msg(MSGT_DEMUX,MSGL_ERR,"demux_real: invalid chunksize! (%d)\n",chunk_size);
1129 break; //return;
1132 switch(chunk_id)
1134 case MKTAG('P', 'R', 'O', 'P'):
1135 /* Properties header */
1137 stream_skip(demuxer->stream, 4); /* max bitrate */
1138 stream_skip(demuxer->stream, 4); /* avg bitrate */
1139 stream_skip(demuxer->stream, 4); /* max packet size */
1140 stream_skip(demuxer->stream, 4); /* avg packet size */
1141 stream_skip(demuxer->stream, 4); /* nb packets */
1142 priv->duration = stream_read_dword(demuxer->stream)/1000; /* duration */
1143 stream_skip(demuxer->stream, 4); /* preroll */
1144 priv->index_chunk_offset = stream_read_dword(demuxer->stream);
1145 mp_msg(MSGT_DEMUX,MSGL_V,"First index chunk offset: 0x%x\n", priv->index_chunk_offset);
1146 priv->data_chunk_offset = stream_read_dword(demuxer->stream)+10;
1147 mp_msg(MSGT_DEMUX,MSGL_V,"First data chunk offset: 0x%x\n", priv->data_chunk_offset);
1148 stream_skip(demuxer->stream, 2); /* nb streams */
1149 #if 0
1150 stream_skip(demuxer->stream, 2); /* flags */
1151 #else
1153 int flags = stream_read_word(demuxer->stream);
1155 if (flags)
1157 mp_msg(MSGT_DEMUX,MSGL_V,"Flags (%x): ", flags);
1158 if (flags & 0x1)
1159 mp_msg(MSGT_DEMUX,MSGL_V,"[save allowed] ");
1160 if (flags & 0x2)
1161 mp_msg(MSGT_DEMUX,MSGL_V,"[perfect play (more buffers)] ");
1162 if (flags & 0x4)
1163 mp_msg(MSGT_DEMUX,MSGL_V,"[live broadcast] ");
1164 mp_msg(MSGT_DEMUX,MSGL_V,"\n");
1167 #endif
1168 break;
1169 case MKTAG('C', 'O', 'N', 'T'):
1171 /* Content description header */
1172 char *buf;
1173 int len;
1175 len = stream_read_word(demuxer->stream);
1176 if (len > 0)
1178 buf = malloc(len+1);
1179 stream_read(demuxer->stream, buf, len);
1180 buf[len] = 0;
1181 demux_info_add(demuxer, "title", buf);
1182 free(buf);
1185 len = stream_read_word(demuxer->stream);
1186 if (len > 0)
1188 buf = malloc(len+1);
1189 stream_read(demuxer->stream, buf, len);
1190 buf[len] = 0;
1191 demux_info_add(demuxer, "author", buf);
1192 free(buf);
1195 len = stream_read_word(demuxer->stream);
1196 if (len > 0)
1198 buf = malloc(len+1);
1199 stream_read(demuxer->stream, buf, len);
1200 buf[len] = 0;
1201 demux_info_add(demuxer, "copyright", buf);
1202 free(buf);
1205 len = stream_read_word(demuxer->stream);
1206 if (len > 0)
1208 buf = malloc(len+1);
1209 stream_read(demuxer->stream, buf, len);
1210 buf[len] = 0;
1211 demux_info_add(demuxer, "comment", buf);
1212 free(buf);
1214 break;
1216 case MKTAG('M', 'D', 'P', 'R'):
1218 /* Media properties header */
1219 int stream_id;
1220 int bitrate;
1221 int codec_data_size;
1222 int codec_pos;
1223 int tmp;
1224 int len;
1225 char *descr, *mimet = NULL;
1227 stream_id = stream_read_word(demuxer->stream);
1228 mp_msg(MSGT_DEMUX,MSGL_V,"Found new stream (id: %d)\n", stream_id);
1230 stream_skip(demuxer->stream, 4); /* max bitrate */
1231 bitrate = stream_read_dword(demuxer->stream); /* avg bitrate */
1232 stream_skip(demuxer->stream, 4); /* max packet size */
1233 stream_skip(demuxer->stream, 4); /* avg packet size */
1234 stream_skip(demuxer->stream, 4); /* start time */
1235 stream_skip(demuxer->stream, 4); /* preroll */
1236 stream_skip(demuxer->stream, 4); /* duration */
1238 if ((len = stream_read_char(demuxer->stream)) > 0) {
1239 descr = malloc(len+1);
1240 stream_read(demuxer->stream, descr, len);
1241 descr[len] = 0;
1242 mp_msg(MSGT_DEMUX, MSGL_INFO,"Stream description: %s\n", descr);
1243 free(descr);
1245 if ((len = stream_read_char(demuxer->stream)) > 0) {
1246 mimet = malloc(len+1);
1247 stream_read(demuxer->stream, mimet, len);
1248 mimet[len] = 0;
1249 mp_msg(MSGT_DEMUX, MSGL_INFO,"Stream mimetype: %s\n", mimet);
1252 /* Type specific header */
1253 codec_data_size = stream_read_dword(demuxer->stream);
1254 codec_pos = stream_tell(demuxer->stream);
1256 #ifdef MP_DEBUG
1257 #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");}
1258 #endif
1260 if (!strncmp(mimet,"audio/",6)) {
1261 if (strstr(mimet,"x-pn-realaudio") || strstr(mimet,"x-pn-multirate-realaudio")) {
1262 tmp = stream_read_dword(demuxer->stream);
1263 if (tmp != MKTAG(0xfd, 'a', 'r', '.'))
1265 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: can't find .ra in codec data\n");
1266 } else {
1267 /* audio header */
1268 sh_audio_t *sh = new_sh_audio(demuxer, stream_id);
1269 char buf[128]; /* for codec name */
1270 int frame_size;
1271 int sub_packet_size;
1272 int sub_packet_h;
1273 int version;
1274 int flavor;
1275 int coded_frame_size;
1276 int codecdata_length;
1277 int i;
1278 char *buft;
1279 int hdr_size;
1280 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[%s] Audio stream found, -aid %d\n", "real", stream_id);
1281 mp_msg(MSGT_DEMUX,MSGL_V,"Found audio stream!\n");
1282 version = stream_read_word(demuxer->stream);
1283 mp_msg(MSGT_DEMUX,MSGL_V,"version: %d\n", version);
1284 if (version == 3) {
1285 stream_skip(demuxer->stream, 2);
1286 stream_skip(demuxer->stream, 10);
1287 stream_skip(demuxer->stream, 4);
1288 // Name, author, (c) are also in CONT tag
1289 if ((i = stream_read_char(demuxer->stream)) != 0) {
1290 buft = malloc(i+1);
1291 stream_read(demuxer->stream, buft, i);
1292 buft[i] = 0;
1293 demux_info_add(demuxer, "Name", buft);
1294 free(buft);
1296 if ((i = stream_read_char(demuxer->stream)) != 0) {
1297 buft = malloc(i+1);
1298 stream_read(demuxer->stream, buft, i);
1299 buft[i] = 0;
1300 demux_info_add(demuxer, "Author", buft);
1301 free(buft);
1303 if ((i = stream_read_char(demuxer->stream)) != 0) {
1304 buft = malloc(i+1);
1305 stream_read(demuxer->stream, buft, i);
1306 buft[i] = 0;
1307 demux_info_add(demuxer, "Copyright", buft);
1308 free(buft);
1310 if ((i = stream_read_char(demuxer->stream)) != 0)
1311 mp_msg(MSGT_DEMUX,MSGL_WARN,"Last header byte is not zero!\n");
1312 stream_skip(demuxer->stream, 1);
1313 i = stream_read_char(demuxer->stream);
1314 sh->format = stream_read_dword_le(demuxer->stream);
1315 if (i != 4) {
1316 mp_msg(MSGT_DEMUX,MSGL_WARN,"Audio FourCC size is not 4 (%d), please report to "
1317 "MPlayer developers\n", i);
1318 stream_skip(demuxer->stream, i - 4);
1320 if (sh->format != mmioFOURCC('l','p','c','J')) {
1321 mp_msg(MSGT_DEMUX,MSGL_WARN,"Version 3 audio with FourCC %8x, please report to "
1322 "MPlayer developers\n", sh->format);
1324 sh->channels = 1;
1325 sh->samplesize = 16;
1326 sh->samplerate = 8000;
1327 frame_size = 240;
1328 strcpy(buf, "14_4");
1329 } else {
1330 stream_skip(demuxer->stream, 2); // 00 00
1331 stream_skip(demuxer->stream, 4); /* .ra4 or .ra5 */
1332 stream_skip(demuxer->stream, 4); // ???
1333 stream_skip(demuxer->stream, 2); /* version (4 or 5) */
1334 hdr_size = stream_read_dword(demuxer->stream); // header size
1335 mp_msg(MSGT_DEMUX,MSGL_V,"header size: %d\n", hdr_size);
1336 flavor = stream_read_word(demuxer->stream);/* codec flavor id */
1337 coded_frame_size = stream_read_dword(demuxer->stream);/* needed by codec */
1338 mp_msg(MSGT_DEMUX,MSGL_V,"coded_frame_size: %d\n", coded_frame_size);
1339 stream_skip(demuxer->stream, 4); // big number
1340 stream_skip(demuxer->stream, 4); // bigger number
1341 stream_skip(demuxer->stream, 4); // 2 || -''-
1342 sub_packet_h = stream_read_word(demuxer->stream);
1343 mp_msg(MSGT_DEMUX,MSGL_V,"sub_packet_h: %d\n", sub_packet_h);
1344 frame_size = stream_read_word(demuxer->stream);
1345 mp_msg(MSGT_DEMUX,MSGL_V,"frame_size: %d\n", frame_size);
1346 sub_packet_size = stream_read_word(demuxer->stream);
1347 mp_msg(MSGT_DEMUX,MSGL_V,"sub_packet_size: %d\n", sub_packet_size);
1348 stream_skip(demuxer->stream, 2); // 0
1350 if (version == 5)
1351 stream_skip(demuxer->stream, 6); //0,srate,0
1353 sh->samplerate = stream_read_word(demuxer->stream);
1354 stream_skip(demuxer->stream, 2); // 0
1355 sh->samplesize = stream_read_word(demuxer->stream)/8;
1356 sh->channels = stream_read_word(demuxer->stream);
1357 mp_msg(MSGT_DEMUX,MSGL_V,"samplerate: %d, channels: %d\n",
1358 sh->samplerate, sh->channels);
1360 if (version == 5)
1362 stream_read(demuxer->stream, buf, 4); // interleaver id
1363 priv->intl_id[stream_id] = MKTAG(buf[0], buf[1], buf[2], buf[3]);
1364 stream_read(demuxer->stream, buf, 4); // fourcc
1365 buf[4] = 0;
1367 else
1369 /* Interleaver id */
1370 get_str(1, demuxer, buf, sizeof(buf));
1371 priv->intl_id[stream_id] = MKTAG(buf[0], buf[1], buf[2], buf[3]);
1372 /* Codec FourCC */
1373 get_str(1, demuxer, buf, sizeof(buf));
1377 /* Emulate WAVEFORMATEX struct: */
1378 sh->wf = malloc(sizeof(WAVEFORMATEX));
1379 memset(sh->wf, 0, sizeof(WAVEFORMATEX));
1380 sh->wf->nChannels = sh->channels;
1381 sh->wf->wBitsPerSample = sh->samplesize*8;
1382 sh->wf->nSamplesPerSec = sh->samplerate;
1383 sh->wf->nAvgBytesPerSec = bitrate/8;
1384 sh->wf->nBlockAlign = frame_size;
1385 sh->wf->cbSize = 0;
1386 sh->format = MKTAG(buf[0], buf[1], buf[2], buf[3]);
1388 switch (sh->format)
1390 case MKTAG('d', 'n', 'e', 't'):
1391 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: DNET -> AC3\n");
1392 // sh->format = 0x2000;
1393 break;
1394 case MKTAG('1', '4', '_', '4'):
1395 sh->wf->nBlockAlign = 0x14;
1396 break;
1398 case MKTAG('2', '8', '_', '8'):
1399 sh->wf->nBlockAlign = coded_frame_size;
1400 break;
1402 case MKTAG('s', 'i', 'p', 'r'):
1403 case MKTAG('a', 't', 'r', 'c'):
1404 case MKTAG('c', 'o', 'o', 'k'):
1405 // realaudio codec plugins - common:
1406 stream_skip(demuxer->stream,3); // Skip 3 unknown bytes
1407 if (version==5)
1408 stream_skip(demuxer->stream,1); // Skip 1 additional unknown byte
1409 codecdata_length=stream_read_dword(demuxer->stream);
1410 // Check extradata len, we can't store bigger values in cbSize anyway
1411 if ((unsigned)codecdata_length > 0xffff) {
1412 mp_msg(MSGT_DEMUX,MSGL_ERR,"Extradata too big (%d)\n", codecdata_length);
1413 goto skip_this_chunk;
1415 sh->wf->cbSize = codecdata_length;
1416 sh->wf = realloc(sh->wf, sizeof(WAVEFORMATEX)+sh->wf->cbSize);
1417 stream_read(demuxer->stream, ((char*)(sh->wf+1)), codecdata_length); // extras
1418 if (priv->intl_id[stream_id] == MKTAG('g', 'e', 'n', 'r'))
1419 sh->wf->nBlockAlign = sub_packet_size;
1420 else
1421 sh->wf->nBlockAlign = coded_frame_size;
1423 break;
1425 case MKTAG('r', 'a', 'a', 'c'):
1426 case MKTAG('r', 'a', 'c', 'p'):
1427 /* This is just AAC. The two or five bytes of */
1428 /* config data needed for libfaad are stored */
1429 /* after the audio headers. */
1430 stream_skip(demuxer->stream,3); // Skip 3 unknown bytes
1431 if (version==5)
1432 stream_skip(demuxer->stream,1); // Skip 1 additional unknown byte
1433 codecdata_length=stream_read_dword(demuxer->stream);
1434 if (codecdata_length>=1) {
1435 sh->codecdata_len = codecdata_length - 1;
1436 sh->codecdata = calloc(sh->codecdata_len, 1);
1437 stream_skip(demuxer->stream, 1);
1438 stream_read(demuxer->stream, sh->codecdata, sh->codecdata_len);
1440 sh->format = mmioFOURCC('M', 'P', '4', 'A');
1441 break;
1442 default:
1443 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: Unknown (%s)\n", buf);
1446 // Interleaver setup
1447 priv->sub_packet_size[stream_id] = sub_packet_size;
1448 priv->sub_packet_h[stream_id] = sub_packet_h;
1449 priv->coded_framesize[stream_id] = coded_frame_size;
1450 priv->audiopk_size[stream_id] = frame_size;
1452 sh->wf->wFormatTag = sh->format;
1454 mp_msg(MSGT_DEMUX,MSGL_V,"audio fourcc: %.4s (%x)\n", (char *)&sh->format, sh->format);
1455 if ( mp_msg_test(MSGT_DEMUX,MSGL_V) )
1456 print_wave_header(sh->wf, MSGL_V);
1458 /* Select audio stream with highest bitrate if multirate file*/
1459 if (priv->is_multirate && ((demuxer->audio->id == -1) ||
1460 ((demuxer->audio->id >= 0) && priv->a_bitrate && (bitrate > priv->a_bitrate)))) {
1461 demuxer->audio->id = stream_id;
1462 priv->a_bitrate = bitrate;
1463 mp_msg(MSGT_DEMUX,MSGL_DBG2,"Multirate autoselected audio id %d with bitrate %d\n", stream_id, bitrate);
1466 if(demuxer->audio->id==stream_id){
1467 sh->ds=demuxer->audio;
1468 demuxer->audio->sh=sh;
1469 priv->audio_buf = calloc(priv->sub_packet_h[demuxer->audio->id], priv->audiopk_size[demuxer->audio->id]);
1470 priv->audio_timestamp = calloc(priv->sub_packet_h[demuxer->audio->id], sizeof(double));
1473 ++a_streams;
1475 #ifdef stream_skip
1476 #undef stream_skip
1477 #endif
1479 } else if (strstr(mimet,"X-MP3-draft-00")) {
1480 sh_audio_t *sh = new_sh_audio(demuxer, stream_id);
1481 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[%s] Audio stream found, -aid %d\n", "real", stream_id);
1483 /* Emulate WAVEFORMATEX struct: */
1484 sh->wf = malloc(sizeof(WAVEFORMATEX));
1485 memset(sh->wf, 0, sizeof(WAVEFORMATEX));
1486 sh->wf->nChannels = 0;//sh->channels;
1487 sh->wf->wBitsPerSample = 16;
1488 sh->wf->nSamplesPerSec = 0;//sh->samplerate;
1489 sh->wf->nAvgBytesPerSec = 0;//bitrate;
1490 sh->wf->nBlockAlign = 0;//frame_size;
1491 sh->wf->cbSize = 0;
1492 sh->wf->wFormatTag = sh->format = mmioFOURCC('a','d','u',0x55);
1494 if(demuxer->audio->id==stream_id){
1495 sh->ds=demuxer->audio;
1496 demuxer->audio->sh=sh;
1499 ++a_streams;
1500 } else if (strstr(mimet,"x-ralf-mpeg4")) {
1501 mp_msg(MSGT_DEMUX,MSGL_ERR,"Real lossless audio not supported yet\n");
1502 } else if (strstr(mimet,"x-pn-encrypted-ra")) {
1503 mp_msg(MSGT_DEMUX,MSGL_ERR,"Encrypted audio is not supported\n");
1504 } else {
1505 mp_msg(MSGT_DEMUX,MSGL_V,"Unknown audio stream format\n");
1507 } else if (!strncmp(mimet,"video/",6)) {
1508 if (strstr(mimet,"x-pn-realvideo") || strstr(mimet,"x-pn-multirate-realvideo")) {
1509 stream_skip(demuxer->stream, 4); // VIDO length, same as codec_data_size
1510 tmp = stream_read_dword(demuxer->stream);
1511 if(tmp != MKTAG('O', 'D', 'I', 'V'))
1513 mp_msg(MSGT_DEMUX,MSGL_V,"Video: can't find VIDO in codec data\n");
1514 } else {
1515 /* video header */
1516 sh_video_t *sh = new_sh_video(demuxer, stream_id);
1517 mp_tmsg(MSGT_DEMUX, MSGL_INFO, "[%s] Video stream found, -vid %d\n", "real", stream_id);
1519 sh->format = stream_read_dword_le(demuxer->stream); /* fourcc */
1520 mp_msg(MSGT_DEMUX,MSGL_V,"video fourcc: %.4s (%x)\n", (char *)&sh->format, sh->format);
1522 /* emulate BITMAPINFOHEADER */
1523 sh->bih = malloc(sizeof(BITMAPINFOHEADER));
1524 memset(sh->bih, 0, sizeof(BITMAPINFOHEADER));
1525 sh->bih->biSize = sizeof(BITMAPINFOHEADER);
1526 sh->disp_w = sh->bih->biWidth = stream_read_word(demuxer->stream);
1527 sh->disp_h = sh->bih->biHeight = stream_read_word(demuxer->stream);
1528 sh->bih->biPlanes = 1;
1529 sh->bih->biBitCount = 24;
1530 sh->bih->biCompression = sh->format;
1531 sh->bih->biSizeImage= sh->bih->biWidth*sh->bih->biHeight*3;
1533 sh->fps = (float) stream_read_word(demuxer->stream);
1534 if (sh->fps<=0) sh->fps=24; // we probably won't even care about fps
1535 sh->frametime = 1.0f/sh->fps;
1537 #if 1
1538 stream_skip(demuxer->stream, 4);
1539 #else
1540 mp_msg(MSGT_DEMUX, MSGL_V,"unknown1: 0x%X \n",stream_read_dword(demuxer->stream));
1541 mp_msg(MSGT_DEMUX, MSGL_V,"unknown2: 0x%X \n",stream_read_word(demuxer->stream));
1542 mp_msg(MSGT_DEMUX, MSGL_V,"unknown3: 0x%X \n",stream_read_word(demuxer->stream));
1543 #endif
1544 // if(sh->format==0x30335652 || sh->format==0x30325652 )
1545 if(1)
1547 int tmp=stream_read_word(demuxer->stream);
1548 if(tmp>0){
1549 sh->fps=tmp; sh->frametime = 1.0f/sh->fps;
1551 } else {
1552 int fps=stream_read_word(demuxer->stream);
1553 mp_msg(MSGT_DEMUX, MSGL_WARN,"realvid: ignoring FPS = %d\n",fps);
1555 stream_skip(demuxer->stream, 2);
1558 // read and store codec extradata
1559 unsigned int cnt = codec_data_size - (stream_tell(demuxer->stream) - codec_pos);
1560 if (cnt > 0x7fffffff - sizeof(BITMAPINFOHEADER)) {
1561 mp_msg(MSGT_DEMUX, MSGL_ERR,"Extradata too big (%u)\n", cnt);
1562 } else {
1563 sh->bih = realloc(sh->bih, sizeof(BITMAPINFOHEADER) + cnt);
1564 sh->bih->biSize += cnt;
1565 stream_read(demuxer->stream, ((unsigned char*)(sh->bih+1)), cnt);
1568 if(sh->format == 0x30315652 && ((unsigned char*)(sh->bih+1))[6] == 0x30)
1569 sh->bih->biCompression = sh->format = mmioFOURCC('R', 'V', '1', '3');
1571 /* Select video stream with highest bitrate if multirate file*/
1572 if (priv->is_multirate && ((demuxer->video->id == -1) ||
1573 ((demuxer->video->id >= 0) && priv->v_bitrate && (bitrate > priv->v_bitrate)))) {
1574 demuxer->video->id = stream_id;
1575 priv->v_bitrate = bitrate;
1576 mp_msg(MSGT_DEMUX,MSGL_DBG2,"Multirate autoselected video id %d with bitrate %d\n", stream_id, bitrate);
1579 if(demuxer->video->id==stream_id){
1580 sh->ds=demuxer->video;
1581 demuxer->video->sh=sh;
1584 ++v_streams;
1587 } else {
1588 mp_msg(MSGT_DEMUX,MSGL_V,"Unknown video stream format\n");
1590 } else if (strstr(mimet,"logical-")) {
1591 if (strstr(mimet,"fileinfo")) {
1592 mp_msg(MSGT_DEMUX,MSGL_V,"Got a logical-fileinfo chunk\n");
1593 } else if (strstr(mimet,"-audio") || strstr(mimet,"-video")) {
1594 int i, stream_cnt;
1595 int stream_list[MAX_STREAMS];
1597 priv->is_multirate = 1;
1598 stream_skip(demuxer->stream, 4); // Length of codec data (repeated)
1599 stream_cnt = stream_read_dword(demuxer->stream); // Get number of audio or video streams
1600 if ((unsigned)stream_cnt >= MAX_STREAMS) {
1601 mp_msg(MSGT_DEMUX,MSGL_ERR,"Too many streams in %s. Big troubles ahead.\n", mimet);
1602 goto skip_this_chunk;
1604 for (i = 0; i < stream_cnt; i++)
1605 stream_list[i] = stream_read_word(demuxer->stream);
1606 for (i = 0; i < stream_cnt; i++)
1607 if ((unsigned)stream_list[i] >= MAX_STREAMS) {
1608 mp_msg(MSGT_DEMUX,MSGL_ERR,"Stream id out of range: %d. Ignored.\n", stream_list[i]);
1609 stream_skip(demuxer->stream, 4); // Skip DATA offset for broken stream
1610 } else {
1611 priv->str_data_offset[stream_list[i]] = stream_read_dword(demuxer->stream);
1612 mp_msg(MSGT_DEMUX,MSGL_V,"Stream %d with DATA offset 0x%08x\n", stream_list[i], priv->str_data_offset[stream_list[i]]);
1614 // Skip the rest of this chunk
1615 } else
1616 mp_msg(MSGT_DEMUX,MSGL_V,"Unknown logical stream\n");
1618 else {
1619 mp_msg(MSGT_DEMUX, MSGL_ERR, "Not audio/video stream or unsupported!\n");
1621 // break;
1622 // default:
1623 skip_this_chunk:
1624 /* skip codec info */
1625 tmp = stream_tell(demuxer->stream) - codec_pos;
1626 mp_msg(MSGT_DEMUX,MSGL_V,"### skipping %d bytes of codec info\n", codec_data_size - tmp);
1627 #if 0
1628 { int i;
1629 for(i=0;i<codec_data_size - tmp;i++)
1630 mp_msg(MSGT_DEMUX, MSGL_V," %02X",stream_read_char(demuxer->stream));
1631 mp_msg(MSGT_DEMUX, MSGL_V,"\n");
1633 #else
1634 stream_skip(demuxer->stream, codec_data_size - tmp);
1635 #endif
1636 if (mimet)
1637 free (mimet);
1638 break;
1639 // }
1641 case MKTAG('D', 'A', 'T', 'A'):
1642 goto header_end;
1643 case MKTAG('I', 'N', 'D', 'X'):
1644 default:
1645 mp_msg(MSGT_DEMUX,MSGL_V,"Unknown chunk: %x\n", chunk_id);
1646 stream_skip(demuxer->stream, chunk_size - 10);
1647 break;
1651 header_end:
1652 if(priv->is_multirate) {
1653 mp_msg(MSGT_DEMUX,MSGL_V,"Selected video id %d audio id %d\n", demuxer->video->id, demuxer->audio->id);
1654 /* Perform some sanity checks to avoid checking streams id all over the code*/
1655 if (demuxer->audio->id >= MAX_STREAMS) {
1656 mp_msg(MSGT_DEMUX,MSGL_ERR,"Invalid audio stream %d. No sound will be played.\n", demuxer->audio->id);
1657 demuxer->audio->id = -2;
1658 } else if ((demuxer->audio->id >= 0) && (priv->str_data_offset[demuxer->audio->id] == 0)) {
1659 mp_msg(MSGT_DEMUX,MSGL_ERR,"Audio stream %d not found. No sound will be played.\n", demuxer->audio->id);
1660 demuxer->audio->id = -2;
1662 if (demuxer->video->id >= MAX_STREAMS) {
1663 mp_msg(MSGT_DEMUX,MSGL_ERR,"Invalid video stream %d. No video will be played.\n", demuxer->video->id);
1664 demuxer->video->id = -2;
1665 } else if ((demuxer->video->id >= 0) && (priv->str_data_offset[demuxer->video->id] == 0)) {
1666 mp_msg(MSGT_DEMUX,MSGL_ERR,"Video stream %d not found. No video will be played.\n", demuxer->video->id);
1667 demuxer->video->id = -2;
1671 if(priv->is_multirate && ((demuxer->video->id >= 0) || (demuxer->audio->id >=0))) {
1672 /* If audio or video only, seek to right place and behave like standard file */
1673 if (demuxer->video->id < 0) {
1674 // Stream is audio only, or -novideo
1675 stream_seek(demuxer->stream, priv->data_chunk_offset = priv->str_data_offset[demuxer->audio->id]+10);
1676 priv->is_multirate = 0;
1678 if (demuxer->audio->id < 0) {
1679 // Stream is video only, or -nosound
1680 stream_seek(demuxer->stream, priv->data_chunk_offset = priv->str_data_offset[demuxer->video->id]+10);
1681 priv->is_multirate = 0;
1685 if(!priv->is_multirate) {
1686 // printf("i=%d num_of_headers=%d \n",i,num_of_headers);
1687 priv->num_of_packets = stream_read_dword(demuxer->stream);
1688 stream_skip(demuxer->stream, 4); /* next data header */
1690 mp_msg(MSGT_DEMUX,MSGL_V,"Packets in file: %d\n", priv->num_of_packets);
1692 if (priv->num_of_packets == 0)
1693 priv->num_of_packets = -10;
1694 } else {
1695 priv->audio_curpos = priv->str_data_offset[demuxer->audio->id] + 18;
1696 stream_seek(demuxer->stream, priv->str_data_offset[demuxer->audio->id]+10);
1697 priv->a_num_of_packets=priv->a_num_of_packets = stream_read_dword(demuxer->stream);
1698 priv->video_curpos = priv->str_data_offset[demuxer->video->id] + 18;
1699 stream_seek(demuxer->stream, priv->str_data_offset[demuxer->video->id]+10);
1700 priv->v_num_of_packets = stream_read_dword(demuxer->stream);
1701 priv->stream_switch = 1;
1702 /* Index required for multirate playback, force building if it's not there */
1703 /* but respect user request to force index regeneration */
1704 if (index_mode == -1)
1705 index_mode = 1;
1709 priv->audio_need_keyframe = 0;
1710 priv->video_after_seek = 0;
1712 switch (index_mode){
1713 case -1: // untouched
1714 if ((demuxer->stream->flags & MP_STREAM_SEEK) == MP_STREAM_SEEK &&
1715 priv->index_chunk_offset && parse_index_chunk(demuxer))
1717 demuxer->seekable = 1;
1719 break;
1720 case 1: // use (generate index)
1721 if (priv->index_chunk_offset && parse_index_chunk(demuxer))
1723 demuxer->seekable = 1;
1724 } else {
1725 generate_index(demuxer);
1726 demuxer->seekable = 1;
1728 break;
1729 case 2: // force generating index
1730 generate_index(demuxer);
1731 demuxer->seekable = 1;
1732 break;
1733 default: // do nothing
1734 break;
1737 // detect streams:
1738 if(demuxer->video->id==-1 && v_streams>0){
1739 // find the valid video stream:
1740 if(!ds_fill_buffer(demuxer->video)){
1741 mp_msg(MSGT_DEMUXER, MSGL_INFO, "RM: %s",
1742 mp_gtext("No video stream found.\n"));
1745 if(demuxer->audio->id==-1 && a_streams>0){
1746 // find the valid audio stream:
1747 if(!ds_fill_buffer(demuxer->audio)){
1748 mp_msg(MSGT_DEMUXER, MSGL_INFO, "RM: %s",
1749 mp_gtext("No audio stream found -> no sound.\n"));
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