1 /*****************************************************************************
2 * real.c: Real demuxer.
3 *****************************************************************************
4 * Copyright (C) 2004, 2006-2007 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc_demux.h>
31 #include <vlc_charset.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open ( vlc_object_t * );
38 static void Close ( vlc_object_t * );
41 set_description( _("Real demuxer" ) );
42 set_capability( "demux2", 15 );
43 set_category( CAT_INPUT );
44 set_subcategory( SUBCAT_INPUT_DEMUX );
45 set_callbacks( Open, Close );
46 add_shortcut( "real" );
50 /*****************************************************************************
52 *****************************************************************************/
66 int i_coded_frame_size;
71 block_t **p_subpackets;
78 int64_t i_data_offset;
80 uint32_t i_data_packets_count;
81 uint32_t i_data_packets;
82 int64_t i_data_offset_next;
90 char* psz_description;
95 uint8_t buffer[65536];
100 static int Demux( demux_t *p_demux );
101 static int Control( demux_t *p_demux, int i_query, va_list args );
103 static int HeaderRead( demux_t *p_demux );
104 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
106 // Map flavour to bytes per second
107 static int sipr_fl2bps[4] = {813, 1062, 625, 2000}; // 6.5, 8.5, 5, 16 kbit per second
109 /*****************************************************************************
111 *****************************************************************************/
112 static int Open( vlc_object_t *p_this )
114 demux_t *p_demux = (demux_t*)p_this;
117 const uint8_t *p_peek;
119 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
120 if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
122 /* Fill p_demux field */
123 p_demux->pf_demux = Demux;
124 p_demux->pf_control = Control;
125 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
126 memset( p_sys, 0, sizeof( demux_sys_t ) );
127 p_sys->i_data_offset = 0;
133 /* Parse the headers */
134 if( HeaderRead( p_demux ) )
137 msg_Err( p_demux, "invalid header" );
138 for( i = 0; i < p_sys->i_track; i++ )
140 real_track_t *tk = p_sys->track[i];
144 es_out_Del( p_demux->out, tk->p_es );
148 if( p_sys->i_track > 0 )
150 free( p_sys->track );
159 /*****************************************************************************
161 *****************************************************************************/
162 static void Close( vlc_object_t *p_this )
164 demux_t *p_demux = (demux_t*)p_this;
165 demux_sys_t *p_sys = p_demux->p_sys;
168 for( i = 0; i < p_sys->i_track; i++ )
170 real_track_t *tk = p_sys->track[i];
171 int j = tk->i_subpackets;
173 if( tk->p_frame ) block_Release( tk->p_frame );
174 es_format_Clean( &tk->fmt );
178 if( tk->p_subpackets[ j ] )
179 block_Release( tk->p_subpackets[ j ] );
181 if( tk->i_subpackets ) free( tk->p_subpackets );
186 if( p_sys->psz_title ) free( p_sys->psz_title );
187 if( p_sys->psz_artist ) free( p_sys->psz_artist );
188 if( p_sys->psz_copyright ) free( p_sys->psz_copyright );
189 if( p_sys->psz_description ) free( p_sys->psz_description );
191 if( p_sys->i_track > 0 ) free( p_sys->track );
196 /*****************************************************************************
198 *****************************************************************************/
199 static int Demux( demux_t *p_demux )
201 demux_sys_t *p_sys = p_demux->p_sys;
203 int i_size, i_id, i_flags, i;
205 real_track_t *tk = NULL;
206 vlc_bool_t b_selected;
208 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
209 p_sys->i_data_packets_count )
211 if( stream_Read( p_demux->s, header, 18 ) < 18 )
215 if( strncmp( (char *)header, "DATA", 4 ) )
219 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
220 p_sys->i_data_size = GetDWBE( &header[4] );
221 p_sys->i_data_packets_count = GetDWBE( &header[10] );
222 p_sys->i_data_packets = 0;
223 p_sys->i_data_offset_next = GetDWBE( &header[14] );
225 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
226 p_sys->i_data_packets_count,
227 (uint32_t)p_sys->i_data_offset_next );
230 if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
232 i_size = GetWBE( &header[2] ) - 12;
233 i_id = GetWBE( &header[4] );
234 i_pts = 1000 * GetDWBE( &header[6] );
235 i_pts += 1000; /* Avoid 0 pts */
236 i_flags= header[11]; /* flags 0x02 -> keyframe */
239 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
240 p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
243 p_sys->i_data_packets++;
245 stream_Read( p_demux->s, p_sys->buffer, i_size );
247 for( i = 0; i < p_sys->i_track; i++ )
249 if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
254 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
257 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
259 if( tk->fmt.i_cat == VIDEO_ES && b_selected )
261 uint8_t *p = p_sys->buffer;
263 while( p < &p_sys->buffer[i_size - 2] )
272 if( (h&0xc0) == 0x40 )
276 i_len = &p_sys->buffer[i_size] - p;
282 i_subseq = (*p++)&0x7f;
284 i_len = (p[0] << 8)|p[1]; p += 2;
285 if( (i_len&0xc000) == 0 )
288 i_len |= (p[0] << 8)|p[1]; p += 2;
296 i_offset = (p[0] << 8)|p[1]; p += 2;
297 if( (i_offset&0xc000) == 0 )
300 i_offset |= (p[0] << 8)|p[1]; p += 2;
301 i_offset &= 0x3fffffff;
310 i_copy = i_len - i_offset;
311 if( i_copy > &p_sys->buffer[i_size] - p )
313 i_copy = &p_sys->buffer[i_size] - p;
315 else if( i_copy < 0 )
320 msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d",
321 i_len, i_offset, i_copy, i_subseq, i_seqnum );
323 if( (h&0xc0) == 0x80 )
325 /* last fragment -> fixes */
327 i_offset = i_len - i_copy;
328 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
333 ( tk->p_frame->i_dts != i_pts ||
334 tk->i_frame != i_len ) )
336 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
338 if( p_sys->i_pcr < tk->p_frame->i_dts )
340 p_sys->i_pcr = tk->p_frame->i_dts;
342 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
343 (int64_t)p_sys->i_pcr );
345 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
351 if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
359 if( tk->p_frame == NULL )
361 msg_Dbg( p_demux, "new frame size=%d", i_len );
363 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
367 memset( &tk->p_frame->p_buffer[8], 0, i_len );
368 tk->p_frame->i_dts = i_pts;
369 tk->p_frame->i_pts = i_pts;
371 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
372 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */
375 if( i_offset < tk->i_frame)
377 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
379 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
380 i_ck, i_offset, i_copy );
382 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
384 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
389 if( (h&0xc0) != 0x80 )
398 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
400 if( (h&0xc0) == 0x80 )
404 i_offset = i_len - i_offset;
406 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
407 memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
410 if( p_sys->i_pcr < tk->p_frame->i_dts )
412 p_sys->i_pcr = tk->p_frame->i_dts;
413 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
414 (int64_t)p_sys->i_pcr );
416 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
424 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
425 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
429 if( (h&0xc0) != 0x00 )
434 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
438 p_frame->i_dts = i_pts;
439 p_frame->i_pts = i_pts;
441 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
442 ((uint32_t*)p_frame->p_buffer)[1] = 1;
443 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
444 memcpy( &p_frame->p_buffer[8], p, i_copy );
448 if( p_sys->i_pcr < p_frame->i_dts )
450 p_sys->i_pcr = p_frame->i_dts;
451 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
452 (int64_t)p_sys->i_pcr );
454 es_out_Send( p_demux->out, tk->p_es, p_frame );
460 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
464 memset( &tk->p_frame->p_buffer[8], 0, i_len );
465 tk->p_frame->i_dts = i_pts;
466 tk->p_frame->i_pts = i_pts;
468 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
469 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */
470 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
471 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
478 else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
481 if( p_sys->i_pcr < i_pts )
483 p_sys->i_pcr = i_pts;
484 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
485 (int64_t)p_sys->i_pcr );
488 if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
490 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
491 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
494 for( i = 0; i < i_sub; i++ )
496 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
497 block_t *p_block = block_New( p_demux, i_sub_size );
500 memcpy( p_block->p_buffer, p_sub, i_sub_size );
504 p_block->i_pts = ( i == 0 ? i_pts : 0 );
506 es_out_Send( p_demux->out, tk->p_es, p_block );
510 else if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
511 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
512 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
513 tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
515 uint8_t *p_buf = p_sys->buffer;
516 int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
520 if( i_flags & 2 ) y = tk->i_subpacket = 0;
522 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
523 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
524 for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
526 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
527 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
528 p_buf += tk->i_subpacket_size;
530 i_index = tk->i_subpacket_h * i +
531 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
533 p_block->i_dts = p_block->i_pts = i_pts;
534 tk->p_subpackets[i_index] = p_block;
538 if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
539 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
540 for( i = 0; i < tk->i_subpacket_h / 2; i++ )
542 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
543 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
544 p_buf += tk->i_coded_frame_size;
546 i_index = (i * 2 * tk->i_frame_size) /
547 tk->i_coded_frame_size + y;
549 p_block->i_dts = p_block->i_pts = i_pts;
550 tk->p_subpackets[i_index] = p_block;
554 while( tk->i_out_subpacket != tk->i_subpackets &&
555 tk->p_subpackets[tk->i_out_subpacket] )
557 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
558 tk->p_subpackets[tk->i_out_subpacket] = 0;
560 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
561 es_out_Send( p_demux->out, tk->p_es, p_block );
563 tk->i_out_subpacket++;
566 if( tk->i_subpacket == tk->i_subpackets &&
567 tk->i_out_subpacket != tk->i_subpackets )
569 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
570 "this shouldn't happen" );
573 if( tk->i_subpacket == tk->i_subpackets )
576 tk->i_out_subpacket = 0;
581 block_t *p_block = block_New( p_demux, i_size );
583 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
585 uint8_t *src = p_sys->buffer;
586 uint8_t *dst = p_block->p_buffer;
589 while( dst < &p_block->p_buffer[i_size- 1])
599 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
601 p_block->i_dts = p_block->i_pts = i_pts;
602 es_out_Send( p_demux->out, tk->p_es, p_block );
610 /*****************************************************************************
612 *****************************************************************************/
613 static int Control( demux_t *p_demux, int i_query, va_list args )
615 demux_sys_t *p_sys = p_demux->p_sys;
625 case DEMUX_GET_POSITION:
626 pf = (double*) va_arg( args, double* );
627 i64 = stream_Size( p_demux->s );
630 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
638 case DEMUX_SET_POSITION:
639 f = (double) va_arg( args, double );
640 i64 = stream_Size( p_demux->s );
642 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
644 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
647 pi64 = (int64_t*)va_arg( args, int64_t * );
648 if( p_sys->i_mux_rate > 0 )
650 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
657 case DEMUX_GET_LENGTH:
658 pi64 = (int64_t*)va_arg( args, int64_t * );
660 /* the commented following lines are fen's implementation, which doesn't seem to
661 * work for one reason or another -- FK */
662 /*if( p_sys->i_mux_rate > 0 )
664 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
667 if( p_sys->i_our_duration > 0 )
669 /* our stored duration is in ms, so... */
670 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
679 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
681 /* the core will crash if we provide NULL strings, so check
682 * every string first */
683 if( p_sys->psz_title )
684 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
685 if( p_sys->psz_artist )
686 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
687 if( p_sys->psz_copyright )
688 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
689 if( p_sys->psz_description )
690 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
702 /*****************************************************************************
704 *****************************************************************************/
705 static int HeaderRead( demux_t *p_demux )
707 demux_sys_t *p_sys = p_demux->p_sys;
708 uint8_t header[100]; /* FIXME */
717 /* Read the header */
718 if( stream_Read( p_demux->s, header, 10 ) < 10 )
722 i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
723 i_size = GetDWBE( &header[4] );
724 i_version = GetWBE( &header[8] );
726 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
727 (char*)&i_id, i_size, i_version );
729 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
731 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
734 i_skip = i_size - 10;
736 if( i_id == VLC_FOURCC('.','R','M','F') )
738 if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
739 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
740 GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
744 else if( i_id == VLC_FOURCC('P','R','O','P') )
748 if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
750 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
751 GetDWBE(&header[0]), GetDWBE(&header[4]) );
752 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
753 GetDWBE(&header[8]), GetDWBE(&header[12]) );
754 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) );
755 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) );
756 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) );
757 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) );
758 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) );
759 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) );
761 /* set the duration for export in control */
762 p_sys->i_our_duration = (int)GetDWBE(&header[20]);
764 i_flags = GetWBE(&header[38]);
765 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
767 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
768 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
769 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
772 else if( i_id == VLC_FOURCC('C','O','N','T') )
777 /* FIXME FIXME: should convert from whatever the character
778 * encoding of the input meta data is to UTF-8. */
780 stream_Read( p_demux->s, header, 2 );
781 if( ( i_len = GetWBE( header ) ) > 0 )
783 psz = malloc( i_len + 1 );
784 stream_Read( p_demux->s, psz, i_len );
787 msg_Dbg( p_demux, " - title=`%s'", psz );
789 asprintf( &p_sys->psz_title, psz );
795 stream_Read( p_demux->s, header, 2 );
796 if( ( i_len = GetWBE( header ) ) > 0 )
798 psz = malloc( i_len + 1 );
799 stream_Read( p_demux->s, psz, i_len );
802 msg_Dbg( p_demux, " - author=`%s'", psz );
804 asprintf( &p_sys->psz_artist, psz );
810 stream_Read( p_demux->s, header, 2 );
811 if( ( i_len = GetWBE( header ) ) > 0 )
813 psz = malloc( i_len + 1 );
814 stream_Read( p_demux->s, psz, i_len );
817 msg_Dbg( p_demux, " - copyright=`%s'", psz );
819 asprintf( &p_sys->psz_copyright, psz );
825 stream_Read( p_demux->s, header, 2 );
826 if( ( i_len = GetWBE( header ) ) > 0 )
828 psz = malloc( i_len + 1 );
829 stream_Read( p_demux->s, psz, i_len );
832 msg_Dbg( p_demux, " - comment=`%s'", psz );
834 asprintf( &p_sys->psz_description, psz );
840 else if( i_id == VLC_FOURCC('M','D','P','R') )
842 /* Media properties header */
847 if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
848 i_num = GetWBE( header );
849 msg_Dbg( p_demux, " - id=0x%x", i_num );
850 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
851 GetDWBE(&header[2]), GetDWBE(&header[6]) );
852 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
853 GetDWBE(&header[10]), GetDWBE(&header[14]) );
854 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&header[18]) );
855 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&header[22]) );
856 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&header[26]) );
860 stream_Read( p_demux->s, header, 1 );
861 if( ( i_len = header[0] ) > 0 )
863 psz = malloc( i_len + 1 );
864 stream_Read( p_demux->s, psz, i_len );
867 msg_Dbg( p_demux, " - name=`%s'", psz );
873 stream_Read( p_demux->s, header, 1 );
874 if( ( i_len = header[0] ) > 0 )
876 psz = malloc( i_len + 1 );
877 stream_Read( p_demux->s, psz, i_len );
880 msg_Dbg( p_demux, " - mime=`%s'", psz );
886 stream_Read( p_demux->s, header, 4 );
887 if( ( i_len = GetDWBE( header ) ) > 0 )
889 ReadCodecSpecificData( p_demux, i_len, i_num );
890 stream_Read( p_demux->s, NULL, i_len );
896 else if( i_id == VLC_FOURCC('D','A','T','A') )
898 stream_Read( p_demux->s, header, 8 );
900 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
901 p_sys->i_data_size = i_size;
902 p_sys->i_data_packets_count = GetDWBE( header );
903 p_sys->i_data_packets = 0;
904 p_sys->i_data_offset_next = GetDWBE( &header[4] );
906 msg_Dbg( p_demux, " - packets count=%d next=%u",
907 p_sys->i_data_packets_count,
908 (uint32_t)p_sys->i_data_offset_next );
910 /* we have finished the header */
916 msg_Dbg( p_demux, "unknown chunk" );
919 if( i_skip < 0 ) return VLC_EGENERIC;
920 stream_Read( p_demux->s, NULL, i_skip );
923 /* TODO read index if possible */
928 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
930 demux_sys_t *p_sys = p_demux->p_sys;
933 const uint8_t *p_peek;
935 msg_Dbg( p_demux, " - specific data len=%d", i_len );
936 if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
938 if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
940 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
941 p_peek[10], p_peek[11] ) );
942 fmt.video.i_width = GetWBE( &p_peek[12] );
943 fmt.video.i_height= GetWBE( &p_peek[14] );
946 fmt.p_extra = malloc( 8 );
947 ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
948 ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
950 msg_Dbg( p_demux, " - video 0x%08x 0x%08x",
951 ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
953 if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
954 GetDWBE( &p_peek[30] ) == 0x10003001 )
956 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
958 else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
959 GetDWBE( &p_peek[30] ) == 0x20100001 ||
960 GetDWBE( &p_peek[30] ) == 0x20200002 )
962 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
964 else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
966 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
968 else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
970 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
973 msg_Dbg( p_demux, " - video %4.4s %dx%d",
974 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
976 tk = malloc( sizeof( real_track_t ) );
977 tk->i_out_subpacket = 0;
979 tk->i_subpackets = 0;
980 tk->p_subpackets = NULL;
985 tk->p_es = es_out_Add( p_demux->out, &fmt );
987 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
989 else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
991 int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
992 int i_frame_size, i_subpacket_size;
993 int i_version = GetWBE( &p_peek[4] ); /* [0..3] = '.','r','a',0xfd */
994 msg_Dbg( p_demux, " - audio version=%d", i_version );
996 p_peek += 6; /* 4 + version */
997 es_format_Init( &fmt, AUDIO_ES, 0 );
1004 i_header_size = GetWBE( p_peek ); p_peek += 2; /* Size from now */
1005 p_peek += 10; /* Unknown */
1007 p_peek += 4; /* Data Size */
1010 i_len = *p_peek ; p_peek++;
1013 psz = malloc( i_len + 1 );
1014 memcpy( psz, p_peek, i_len );
1017 msg_Dbg( p_demux, " - title=`%s'", psz );
1019 asprintf( &p_sys->psz_title, psz );
1025 i_len = *p_peek ; p_peek++;
1028 psz = malloc( i_len + 1 );
1029 memcpy( psz, p_peek, i_len );
1032 msg_Dbg( p_demux, " - artist=`%s'", psz );
1034 asprintf( &p_sys->psz_artist, psz );
1040 i_len = *p_peek ; p_peek++;
1043 psz = malloc( i_len + 1 );
1044 memcpy( psz, p_peek, i_len );
1047 msg_Dbg( p_demux, " - Copyright=`%s'", psz );
1049 asprintf( &p_sys->psz_copyright, psz );
1055 i_len = *p_peek ; p_peek++;
1058 psz = malloc( i_len + 1 );
1059 memcpy( psz, p_peek, i_len );
1062 msg_Dbg( p_demux, " - Comment=`%s'", psz );
1064 asprintf( &p_sys->psz_description, psz );
1067 /* This might be unusefull */
1070 p_peek ++; /* Unknown */
1071 p_peek ++; /* FourCC length = 4 */
1072 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1075 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1076 fmt.audio.i_rate = 8000;
1078 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1079 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1084 p_peek += 2; /* 00 00 */
1085 p_peek += 4; /* .ra4 or .ra5 */
1086 p_peek += 4; /* data size */
1087 p_peek += 2; /* version (4 or 5) */
1088 i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1089 i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1090 i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
1091 p_peek += 4; /* ?? */
1092 p_peek += 4; /* ?? */
1093 p_peek += 4; /* ?? */
1094 i_subpacket_h = GetWBE( p_peek ); p_peek += 2; /* 1 */
1095 i_frame_size = GetWBE( p_peek ); p_peek += 2; /* frame size */
1096 i_subpacket_size = GetWBE( p_peek ); p_peek += 2; /* subpacket_size */
1097 p_peek += 2; /* ?? */
1099 if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1101 fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */
1102 p_peek += 2; /* ?? */
1103 fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
1104 fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
1105 fmt.audio.i_blockalign = i_frame_size;
1107 if( i_version == 5 )
1109 p_peek += 4; /* genr */
1110 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1114 p_peek += p_peek[0] + 1; /* descr 1 */
1115 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
1116 p_peek += p_peek[0] + 1;
1119 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1120 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1122 p_peek += 3; /* ?? */
1123 if( i_version == 5 ) p_peek++;
1126 switch( fmt.i_codec )
1128 case VLC_FOURCC( 'd','n','e','t' ):
1129 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1132 case VLC_FOURCC( 'r','a','a','c' ):
1133 case VLC_FOURCC( 'r','a','c','p' ):
1134 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1135 // For version == 4, there might need an extra p_peek++
1136 if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1137 if( fmt.i_extra > 0 )
1139 fmt.p_extra = malloc( fmt.i_extra );
1140 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1143 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1146 case VLC_FOURCC('c','o','o','k'):
1147 case VLC_FOURCC('a','t','r','c'):
1148 fmt.audio.i_blockalign = i_subpacket_size;
1149 if( !(fmt.i_extra = GetDWBE( p_peek )) ) break;
1150 fmt.p_extra = malloc( fmt.i_extra );
1151 memcpy( fmt.p_extra, p_peek + 4, fmt.i_extra );
1154 case VLC_FOURCC('2','8','_','8'):
1156 fmt.audio.i_blockalign = i_coded_frame_size;
1159 case VLC_FOURCC('s','i','p','r'):
1161 fmt.audio.i_blockalign = i_coded_frame_size;
1162 fmt.audio.i_bitspersample = sipr_fl2bps[ i_flavor ];
1165 case VLC_FOURCC('l','p','c','J'):
1166 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1169 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1170 (char*)&fmt.i_codec );
1174 if( fmt.i_codec != 0 )
1178 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1180 tk = malloc( sizeof( real_track_t ) );
1186 tk->i_subpacket_h = i_subpacket_h;
1187 tk->i_subpacket_size = i_subpacket_size;
1188 tk->i_coded_frame_size = i_coded_frame_size;
1189 tk->i_frame_size = i_frame_size;
1191 tk->i_out_subpacket = 0;
1192 tk->i_subpacket = 0;
1193 tk->i_subpackets = 0;
1194 tk->p_subpackets = NULL;
1195 if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
1196 || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1199 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1201 malloc( tk->i_subpackets * sizeof(block_t *) );
1203 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1206 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1208 malloc( tk->i_subpackets * sizeof(block_t *) );
1211 for( i = 0; i < tk->i_subpackets; i++ ) tk->p_subpackets[i] = NULL;
1213 tk->p_es = es_out_Add( p_demux->out, &fmt );
1215 TAB_APPEND( p_sys->i_track, p_sys->track, tk );