1 /*****************************************************************************
2 * real.c: Real demuxer.
3 *****************************************************************************
4 * Copyright (C) 2004, 2006 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 <stdlib.h> /* malloc(), free() */
32 #include <vlc/input.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
42 set_description( _("Real demuxer" ) );
43 set_capability( "demux2", 15 );
44 set_category( CAT_INPUT );
45 set_subcategory( SUBCAT_INPUT_DEMUX );
46 set_callbacks( Open, Close );
47 add_shortcut( "real" );
51 /*****************************************************************************
53 *****************************************************************************/
67 int i_coded_frame_size;
72 block_t **p_subpackets;
79 int64_t i_data_offset;
81 uint32_t i_data_packets_count;
82 uint32_t i_data_packets;
83 int64_t i_data_offset_next;
91 char* psz_description;
96 uint8_t buffer[65536];
101 static int Demux( demux_t *p_demux );
102 static int Control( demux_t *p_demux, int i_query, va_list args );
104 static int HeaderRead( demux_t *p_demux );
105 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
107 /*****************************************************************************
109 *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
112 demux_t *p_demux = (demux_t*)p_this;
117 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
118 if( strncmp( (char *)p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
120 /* Fill p_demux field */
121 p_demux->pf_demux = Demux;
122 p_demux->pf_control = Control;
123 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
124 memset( p_sys, 0, sizeof( demux_sys_t ) );
125 p_sys->i_data_offset = 0;
131 /* Parse the headers */
132 if( HeaderRead( p_demux ) )
135 msg_Err( p_demux, "invalid header" );
136 for( i = 0; i < p_sys->i_track; i++ )
138 real_track_t *tk = p_sys->track[i];
142 es_out_Del( p_demux->out, tk->p_es );
146 if( p_sys->i_track > 0 )
148 free( p_sys->track );
157 /*****************************************************************************
159 *****************************************************************************/
160 static void Close( vlc_object_t *p_this )
162 demux_t *p_demux = (demux_t*)p_this;
163 demux_sys_t *p_sys = p_demux->p_sys;
166 for( i = 0; i < p_sys->i_track; i++ )
168 real_track_t *tk = p_sys->track[i];
169 int j = tk->i_subpackets;
171 if( tk->p_frame ) block_Release( tk->p_frame );
172 es_format_Clean( &tk->fmt );
176 if( tk->p_subpackets[ j ] )
177 block_Release( tk->p_subpackets[ j ] );
179 if( tk->i_subpackets ) free( tk->p_subpackets );
184 if( p_sys->psz_title ) free( p_sys->psz_title );
185 if( p_sys->psz_artist ) free( p_sys->psz_artist );
186 if( p_sys->psz_copyright ) free( p_sys->psz_copyright );
187 if( p_sys->psz_description ) free( p_sys->psz_description );
189 if( p_sys->i_track > 0 ) free( p_sys->track );
194 /*****************************************************************************
196 *****************************************************************************/
197 static int Demux( demux_t *p_demux )
199 demux_sys_t *p_sys = p_demux->p_sys;
201 int i_size, i_id, i_flags, i;
203 real_track_t *tk = NULL;
204 vlc_bool_t b_selected;
206 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
207 p_sys->i_data_packets_count )
209 if( stream_Read( p_demux->s, header, 18 ) < 18 )
213 if( strncmp( (char *)header, "DATA", 4 ) )
217 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
218 p_sys->i_data_size = GetDWBE( &header[4] );
219 p_sys->i_data_packets_count = GetDWBE( &header[10] );
220 p_sys->i_data_packets = 0;
221 p_sys->i_data_offset_next = GetDWBE( &header[14] );
223 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
224 p_sys->i_data_packets_count,
225 (uint32_t)p_sys->i_data_offset_next );
228 if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
230 i_size = GetWBE( &header[2] ) - 12;
231 i_id = GetWBE( &header[4] );
232 i_pts = 1000 * GetDWBE( &header[6] );
233 i_pts += 1000; /* Avoid 0 pts */
234 i_flags= header[11]; /* flags 0x02 -> keyframe */
237 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
238 p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
241 p_sys->i_data_packets++;
243 stream_Read( p_demux->s, p_sys->buffer, i_size );
245 for( i = 0; i < p_sys->i_track; i++ )
247 if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
252 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
255 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
257 if( tk->fmt.i_cat == VIDEO_ES && b_selected )
259 uint8_t *p = p_sys->buffer;
261 while( p < &p_sys->buffer[i_size - 2] )
270 if( (h&0xc0) == 0x40 )
274 i_len = &p_sys->buffer[i_size] - p;
280 i_subseq = (*p++)&0x7f;
282 i_len = (p[0] << 8)|p[1]; p += 2;
283 if( (i_len&0xc000) == 0 )
286 i_len |= (p[0] << 8)|p[1]; p += 2;
294 i_offset = (p[0] << 8)|p[1]; p += 2;
295 if( (i_offset&0xc000) == 0 )
298 i_offset |= (p[0] << 8)|p[1]; p += 2;
299 i_offset &= 0x3fffffff;
308 i_copy = i_len - i_offset;
309 if( i_copy > &p_sys->buffer[i_size] - p )
311 i_copy = &p_sys->buffer[i_size] - p;
313 else if( i_copy < 0 )
318 msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d",
319 i_len, i_offset, i_copy, i_subseq, i_seqnum );
321 if( (h&0xc0) == 0x80 )
323 /* last fragment -> fixes */
325 i_offset = i_len - i_copy;
326 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
331 ( tk->p_frame->i_dts != i_pts ||
332 tk->i_frame != i_len ) )
334 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
336 if( p_sys->i_pcr < tk->p_frame->i_dts )
338 p_sys->i_pcr = tk->p_frame->i_dts;
340 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
341 (int64_t)p_sys->i_pcr );
343 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
349 if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
357 if( tk->p_frame == NULL )
359 msg_Dbg( p_demux, "new frame size=%d", i_len );
361 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
365 memset( &tk->p_frame->p_buffer[8], 0, i_len );
366 tk->p_frame->i_dts = i_pts;
367 tk->p_frame->i_pts = i_pts;
369 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
370 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */
373 if( i_offset < tk->i_frame)
375 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
377 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
378 i_ck, i_offset, i_copy );
380 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
382 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
387 if( (h&0xc0) != 0x80 )
396 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
398 if( (h&0xc0) == 0x80 )
402 i_offset = i_len - i_offset;
404 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
405 memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
408 if( p_sys->i_pcr < tk->p_frame->i_dts )
410 p_sys->i_pcr = tk->p_frame->i_dts;
411 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
412 (int64_t)p_sys->i_pcr );
414 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
422 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
423 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
427 if( (h&0xc0) != 0x00 )
432 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
436 p_frame->i_dts = i_pts;
437 p_frame->i_pts = i_pts;
439 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
440 ((uint32_t*)p_frame->p_buffer)[1] = 1;
441 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
442 memcpy( &p_frame->p_buffer[8], p, i_copy );
446 if( p_sys->i_pcr < p_frame->i_dts )
448 p_sys->i_pcr = p_frame->i_dts;
449 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
450 (int64_t)p_sys->i_pcr );
452 es_out_Send( p_demux->out, tk->p_es, p_frame );
458 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
462 memset( &tk->p_frame->p_buffer[8], 0, i_len );
463 tk->p_frame->i_dts = i_pts;
464 tk->p_frame->i_pts = i_pts;
466 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
467 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */
468 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
469 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
476 else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
479 if( p_sys->i_pcr < i_pts )
481 p_sys->i_pcr = i_pts;
482 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
483 (int64_t)p_sys->i_pcr );
486 if( tk->fmt.i_codec == VLC_FOURCC( 'm', 'p', '4', 'a' ) )
488 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
489 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
492 for( i = 0; i < i_sub; i++ )
494 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
495 block_t *p_block = block_New( p_demux, i_sub_size );
498 memcpy( p_block->p_buffer, p_sub, i_sub_size );
502 p_block->i_pts = ( i == 0 ? i_pts : 0 );
504 es_out_Send( p_demux->out, tk->p_es, p_block );
508 else if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
509 tk->fmt.i_codec == VLC_FOURCC('2','8','_','8') )
511 uint8_t *p_buf = p_sys->buffer;
512 int y = tk->i_subpacket / (tk->i_frame_size /tk->i_subpacket_size);
516 if( i_flags & 2 ) y = tk->i_subpacket = 0;
518 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) )
519 for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
521 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
522 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
523 p_buf += tk->i_subpacket_size;
525 i_index = tk->i_subpacket_h * i +
526 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
528 p_block->i_dts = p_block->i_pts = i_pts;
529 tk->p_subpackets[i_index] = p_block;
533 if( tk->fmt.i_codec == VLC_FOURCC('2','8','_','8') )
534 for( i = 0; i < tk->i_subpacket_h / 2; i++ )
536 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
537 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
538 p_buf += tk->i_coded_frame_size;
540 i_index = (i * 2 * tk->i_frame_size) /
541 tk->i_coded_frame_size + y;
543 p_block->i_dts = p_block->i_pts = i_pts;
544 tk->p_subpackets[i_index] = p_block;
548 while( tk->i_out_subpacket != tk->i_subpackets &&
549 tk->p_subpackets[tk->i_out_subpacket] )
551 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
552 tk->p_subpackets[tk->i_out_subpacket] = 0;
554 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
555 es_out_Send( p_demux->out, tk->p_es, p_block );
557 tk->i_out_subpacket++;
560 if( tk->i_subpacket == tk->i_subpackets &&
561 tk->i_out_subpacket != tk->i_subpackets )
563 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
564 "this shouldn't happen" );
567 if( tk->i_subpacket == tk->i_subpackets )
570 tk->i_out_subpacket = 0;
575 block_t *p_block = block_New( p_demux, i_size );
577 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
579 uint8_t *src = p_sys->buffer;
580 uint8_t *dst = p_block->p_buffer;
583 while( dst < &p_block->p_buffer[i_size- 1])
593 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
595 p_block->i_dts = p_block->i_pts = i_pts;
596 es_out_Send( p_demux->out, tk->p_es, p_block );
604 /*****************************************************************************
606 *****************************************************************************/
607 static int Control( demux_t *p_demux, int i_query, va_list args )
609 demux_sys_t *p_sys = p_demux->p_sys;
619 case DEMUX_GET_POSITION:
620 pf = (double*) va_arg( args, double* );
621 i64 = stream_Size( p_demux->s );
624 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
632 case DEMUX_SET_POSITION:
633 f = (double) va_arg( args, double );
634 i64 = stream_Size( p_demux->s );
636 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
638 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
641 pi64 = (int64_t*)va_arg( args, int64_t * );
642 if( p_sys->i_mux_rate > 0 )
644 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
651 case DEMUX_GET_LENGTH:
652 pi64 = (int64_t*)va_arg( args, int64_t * );
654 /* the commented following lines are fen's implementation, which doesn't seem to
655 * work for one reason or another -- FK */
656 /*if( p_sys->i_mux_rate > 0 )
658 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
661 if( p_sys->i_our_duration > 0 )
663 /* our stored duration is in ms, so... */
664 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
673 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
675 /* the core will crash if we provide NULL strings, so check
676 * every string first */
677 if( p_sys->psz_title )
678 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
679 if( p_sys->psz_artist )
680 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
681 if( p_sys->psz_copyright )
682 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
683 if( p_sys->psz_description )
684 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
696 /*****************************************************************************
698 *****************************************************************************/
699 static int HeaderRead( demux_t *p_demux )
701 demux_sys_t *p_sys = p_demux->p_sys;
702 uint8_t header[100]; /* FIXME */
711 /* Read the header */
712 if( stream_Read( p_demux->s, header, 10 ) < 10 )
716 i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
717 i_size = GetDWBE( &header[4] );
718 i_version = GetWBE( &header[8] );
720 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
721 (char*)&i_id, i_size, i_version );
723 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
725 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
728 i_skip = i_size - 10;
730 if( i_id == VLC_FOURCC('.','R','M','F') )
732 if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
733 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
734 GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
738 else if( i_id == VLC_FOURCC('P','R','O','P') )
742 if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
744 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
745 GetDWBE(&header[0]), GetDWBE(&header[4]) );
746 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
747 GetDWBE(&header[8]), GetDWBE(&header[12]) );
748 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) );
749 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) );
750 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) );
751 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) );
752 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) );
753 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) );
755 /* set the duration for export in control */
756 p_sys->i_our_duration = (int)GetDWBE(&header[20]);
758 i_flags = GetWBE(&header[38]);
759 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
761 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
762 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
763 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
766 else if( i_id == VLC_FOURCC('C','O','N','T') )
771 /* FIXME FIXME: should convert from whatever the character
772 * encoding of the input meta data is to UTF-8. */
774 stream_Read( p_demux->s, header, 2 );
775 if( ( i_len = GetWBE( header ) ) > 0 )
777 psz = malloc( i_len + 1 );
778 stream_Read( p_demux->s, psz, i_len );
781 msg_Dbg( p_demux, " - title=`%s'", psz );
783 asprintf( &p_sys->psz_title, psz );
789 stream_Read( p_demux->s, header, 2 );
790 if( ( i_len = GetWBE( header ) ) > 0 )
792 psz = malloc( i_len + 1 );
793 stream_Read( p_demux->s, psz, i_len );
796 msg_Dbg( p_demux, " - author=`%s'", psz );
798 asprintf( &p_sys->psz_artist, psz );
804 stream_Read( p_demux->s, header, 2 );
805 if( ( i_len = GetWBE( header ) ) > 0 )
807 psz = malloc( i_len + 1 );
808 stream_Read( p_demux->s, psz, i_len );
811 msg_Dbg( p_demux, " - copyright=`%s'", psz );
813 asprintf( &p_sys->psz_copyright, psz );
819 stream_Read( p_demux->s, header, 2 );
820 if( ( i_len = GetWBE( header ) ) > 0 )
822 psz = malloc( i_len + 1 );
823 stream_Read( p_demux->s, psz, i_len );
826 msg_Dbg( p_demux, " - comment=`%s'", psz );
828 asprintf( &p_sys->psz_description, psz );
834 else if( i_id == VLC_FOURCC('M','D','P','R') )
836 /* Media properties header */
841 if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
842 i_num = GetWBE( header );
843 msg_Dbg( p_demux, " - id=0x%x", i_num );
844 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
845 GetDWBE(&header[2]), GetDWBE(&header[6]) );
846 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
847 GetDWBE(&header[10]), GetDWBE(&header[14]) );
848 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&header[18]) );
849 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&header[22]) );
850 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&header[26]) );
854 stream_Read( p_demux->s, header, 1 );
855 if( ( i_len = header[0] ) > 0 )
857 psz = malloc( i_len + 1 );
858 stream_Read( p_demux->s, psz, i_len );
861 msg_Dbg( p_demux, " - name=`%s'", psz );
867 stream_Read( p_demux->s, header, 1 );
868 if( ( i_len = header[0] ) > 0 )
870 psz = malloc( i_len + 1 );
871 stream_Read( p_demux->s, psz, i_len );
874 msg_Dbg( p_demux, " - mime=`%s'", psz );
880 stream_Read( p_demux->s, header, 4 );
881 if( ( i_len = GetDWBE( header ) ) > 0 )
883 ReadCodecSpecificData( p_demux, i_len, i_num );
884 stream_Read( p_demux->s, NULL, i_len );
890 else if( i_id == VLC_FOURCC('D','A','T','A') )
892 stream_Read( p_demux->s, header, 8 );
894 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
895 p_sys->i_data_size = i_size;
896 p_sys->i_data_packets_count = GetDWBE( header );
897 p_sys->i_data_packets = 0;
898 p_sys->i_data_offset_next = GetDWBE( &header[4] );
900 msg_Dbg( p_demux, " - packets count=%d next=%u",
901 p_sys->i_data_packets_count,
902 (uint32_t)p_sys->i_data_offset_next );
904 /* we have finished the header */
910 msg_Dbg( p_demux, "unknown chunk" );
913 if( i_skip < 0 ) return VLC_EGENERIC;
914 stream_Read( p_demux->s, NULL, i_skip );
917 /* TODO read index if possible */
922 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
924 demux_sys_t *p_sys = p_demux->p_sys;
929 msg_Dbg( p_demux, " - specific data len=%d", i_len );
930 if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
932 if( !strncmp( (char *)&p_peek[4], "VIDO", 4 ) )
934 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
935 p_peek[10], p_peek[11] ) );
936 fmt.video.i_width = GetWBE( &p_peek[12] );
937 fmt.video.i_height= GetWBE( &p_peek[14] );
940 fmt.p_extra = malloc( 8 );
941 ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
942 ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
944 msg_Dbg( p_demux, " - video 0x%08x 0x%08x",
945 ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
947 if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
948 GetDWBE( &p_peek[30] ) == 0x10003001 )
950 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
952 else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
953 GetDWBE( &p_peek[30] ) == 0x20100001 ||
954 GetDWBE( &p_peek[30] ) == 0x20200002 )
956 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
958 else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
960 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
962 else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
964 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
967 msg_Dbg( p_demux, " - video %4.4s %dx%d",
968 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
970 tk = malloc( sizeof( real_track_t ) );
971 tk->i_out_subpacket = 0;
973 tk->i_subpackets = 0;
974 tk->p_subpackets = NULL;
979 tk->p_es = es_out_Add( p_demux->out, &fmt );
981 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
983 else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
985 int i_version = GetWBE( &p_peek[4] );
986 int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
987 int i_frame_size, i_subpacket_size;
989 msg_Dbg( p_demux, " - audio version=%d", i_version );
992 es_format_Init( &fmt, AUDIO_ES, 0 );
996 msg_Dbg( p_demux, " - audio version 3 is not supported!" );
1000 p_peek += 2; /* 00 00 */
1001 p_peek += 4; /* .ra4 or .ra5 */
1002 p_peek += 4; /* ?? */
1003 p_peek += 2; /* version (4 or 5) */
1004 i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1005 i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1006 i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4;
1007 p_peek += 4; /* ?? */
1008 p_peek += 4; /* ?? */
1009 p_peek += 4; /* ?? */
1010 i_subpacket_h = GetWBE( p_peek ); p_peek += 2;
1011 i_frame_size = GetWBE( p_peek ); p_peek += 2;
1012 i_subpacket_size = GetWBE( p_peek ); p_peek += 2;
1013 p_peek += 2; /* ?? */
1015 if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1017 fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2;
1018 p_peek += 2; /* ?? */
1019 fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;
1020 fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2;
1021 fmt.audio.i_blockalign = i_frame_size;
1023 if( i_version == 5 )
1025 p_peek += 4; /* genr */
1026 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1030 p_peek += p_peek[0] + 1; /* descr 1 */
1031 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
1032 p_peek += p_peek[0] + 1;
1035 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1036 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1038 p_peek += 3; /* ?? */
1039 if( i_version == 5 ) p_peek++;
1041 switch( fmt.i_codec )
1043 case VLC_FOURCC( 'd', 'n', 'e', 't' ):
1044 fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1047 case VLC_FOURCC( 'r', 'a', 'a', 'c' ):
1048 case VLC_FOURCC( 'r', 'a', 'c', 'p' ):
1049 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1050 if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1051 if( fmt.i_extra > 0 )
1053 fmt.p_extra = malloc( fmt.i_extra );
1054 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1057 fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1060 case VLC_FOURCC('c','o','o','k'):
1061 fmt.audio.i_blockalign = i_subpacket_size;
1062 if( !(fmt.i_extra = GetDWBE( p_peek )) ) break;
1063 fmt.p_extra = malloc( fmt.i_extra );
1064 memcpy( fmt.p_extra, p_peek + 4, fmt.i_extra );
1067 case VLC_FOURCC('2','8','_','8'):
1068 fmt.audio.i_blockalign = i_coded_frame_size;
1072 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1073 (char*)&fmt.i_codec );
1077 if( fmt.i_codec != 0 )
1081 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1083 tk = malloc( sizeof( real_track_t ) );
1089 tk->i_subpacket_h = i_subpacket_h;
1090 tk->i_subpacket_size = i_subpacket_size;
1091 tk->i_coded_frame_size = i_coded_frame_size;
1092 tk->i_frame_size = i_frame_size;
1094 tk->i_out_subpacket = 0;
1095 tk->i_subpacket = 0;
1096 tk->i_subpackets = 0;
1097 tk->p_subpackets = NULL;
1098 if( fmt.i_codec == VLC_FOURCC('c','o','o','k') )
1101 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1103 malloc( tk->i_subpackets * sizeof(block_t *) );
1105 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1108 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1110 malloc( tk->i_subpackets * sizeof(block_t *) );
1113 for( i = 0; i < tk->i_subpackets; i++ ) tk->p_subpackets[i] = NULL;
1115 tk->p_es = es_out_Add( p_demux->out, &fmt );
1117 TAB_APPEND( p_sys->i_track, p_sys->track, tk );