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 /*****************************************************************************
108 *****************************************************************************/
109 static int Open( vlc_object_t *p_this )
111 demux_t *p_demux = (demux_t*)p_this;
114 const uint8_t *p_peek;
116 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
117 if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
119 /* Fill p_demux field */
120 p_demux->pf_demux = Demux;
121 p_demux->pf_control = Control;
122 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
123 memset( p_sys, 0, sizeof( demux_sys_t ) );
124 p_sys->i_data_offset = 0;
130 /* Parse the headers */
131 if( HeaderRead( p_demux ) )
134 msg_Err( p_demux, "invalid header" );
135 for( i = 0; i < p_sys->i_track; i++ )
137 real_track_t *tk = p_sys->track[i];
141 es_out_Del( p_demux->out, tk->p_es );
145 if( p_sys->i_track > 0 )
147 free( p_sys->track );
156 /*****************************************************************************
158 *****************************************************************************/
159 static void Close( vlc_object_t *p_this )
161 demux_t *p_demux = (demux_t*)p_this;
162 demux_sys_t *p_sys = p_demux->p_sys;
165 for( i = 0; i < p_sys->i_track; i++ )
167 real_track_t *tk = p_sys->track[i];
168 int j = tk->i_subpackets;
170 if( tk->p_frame ) block_Release( tk->p_frame );
171 es_format_Clean( &tk->fmt );
175 if( tk->p_subpackets[ j ] )
176 block_Release( tk->p_subpackets[ j ] );
178 if( tk->i_subpackets ) free( tk->p_subpackets );
183 if( p_sys->psz_title ) free( p_sys->psz_title );
184 if( p_sys->psz_artist ) free( p_sys->psz_artist );
185 if( p_sys->psz_copyright ) free( p_sys->psz_copyright );
186 if( p_sys->psz_description ) free( p_sys->psz_description );
188 if( p_sys->i_track > 0 ) free( p_sys->track );
193 /*****************************************************************************
195 *****************************************************************************/
196 static int Demux( demux_t *p_demux )
198 demux_sys_t *p_sys = p_demux->p_sys;
200 int i_size, i_id, i_flags, i;
202 real_track_t *tk = NULL;
203 vlc_bool_t b_selected;
205 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
206 p_sys->i_data_packets_count )
208 if( stream_Read( p_demux->s, header, 18 ) < 18 )
212 if( strncmp( (char *)header, "DATA", 4 ) )
216 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
217 p_sys->i_data_size = GetDWBE( &header[4] );
218 p_sys->i_data_packets_count = GetDWBE( &header[10] );
219 p_sys->i_data_packets = 0;
220 p_sys->i_data_offset_next = GetDWBE( &header[14] );
222 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
223 p_sys->i_data_packets_count,
224 (uint32_t)p_sys->i_data_offset_next );
227 if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
229 i_size = GetWBE( &header[2] ) - 12;
230 i_id = GetWBE( &header[4] );
231 i_pts = 1000 * GetDWBE( &header[6] );
232 i_pts += 1000; /* Avoid 0 pts */
233 i_flags= header[11]; /* flags 0x02 -> keyframe */
236 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
237 p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
240 p_sys->i_data_packets++;
242 stream_Read( p_demux->s, p_sys->buffer, i_size );
244 for( i = 0; i < p_sys->i_track; i++ )
246 if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
251 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
254 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
256 if( tk->fmt.i_cat == VIDEO_ES && b_selected )
258 uint8_t *p = p_sys->buffer;
260 while( p < &p_sys->buffer[i_size - 2] )
269 if( (h&0xc0) == 0x40 )
273 i_len = &p_sys->buffer[i_size] - p;
279 i_subseq = (*p++)&0x7f;
281 i_len = (p[0] << 8)|p[1]; p += 2;
282 if( (i_len&0xc000) == 0 )
285 i_len |= (p[0] << 8)|p[1]; p += 2;
293 i_offset = (p[0] << 8)|p[1]; p += 2;
294 if( (i_offset&0xc000) == 0 )
297 i_offset |= (p[0] << 8)|p[1]; p += 2;
298 i_offset &= 0x3fffffff;
307 i_copy = i_len - i_offset;
308 if( i_copy > &p_sys->buffer[i_size] - p )
310 i_copy = &p_sys->buffer[i_size] - p;
312 else if( i_copy < 0 )
317 msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d",
318 i_len, i_offset, i_copy, i_subseq, i_seqnum );
320 if( (h&0xc0) == 0x80 )
322 /* last fragment -> fixes */
324 i_offset = i_len - i_copy;
325 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
330 ( tk->p_frame->i_dts != i_pts ||
331 tk->i_frame != i_len ) )
333 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
335 if( p_sys->i_pcr < tk->p_frame->i_dts )
337 p_sys->i_pcr = tk->p_frame->i_dts;
339 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
340 (int64_t)p_sys->i_pcr );
342 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
348 if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
356 if( tk->p_frame == NULL )
358 msg_Dbg( p_demux, "new frame size=%d", i_len );
360 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
364 memset( &tk->p_frame->p_buffer[8], 0, i_len );
365 tk->p_frame->i_dts = i_pts;
366 tk->p_frame->i_pts = i_pts;
368 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
369 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */
372 if( i_offset < tk->i_frame)
374 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
376 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
377 i_ck, i_offset, i_copy );
379 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
381 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
386 if( (h&0xc0) != 0x80 )
395 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
397 if( (h&0xc0) == 0x80 )
401 i_offset = i_len - i_offset;
403 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
404 memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
407 if( p_sys->i_pcr < tk->p_frame->i_dts )
409 p_sys->i_pcr = tk->p_frame->i_dts;
410 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
411 (int64_t)p_sys->i_pcr );
413 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
421 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
422 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
426 if( (h&0xc0) != 0x00 )
431 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
435 p_frame->i_dts = i_pts;
436 p_frame->i_pts = i_pts;
438 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
439 ((uint32_t*)p_frame->p_buffer)[1] = 1;
440 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
441 memcpy( &p_frame->p_buffer[8], p, i_copy );
445 if( p_sys->i_pcr < p_frame->i_dts )
447 p_sys->i_pcr = p_frame->i_dts;
448 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
449 (int64_t)p_sys->i_pcr );
451 es_out_Send( p_demux->out, tk->p_es, p_frame );
457 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
461 memset( &tk->p_frame->p_buffer[8], 0, i_len );
462 tk->p_frame->i_dts = i_pts;
463 tk->p_frame->i_pts = i_pts;
465 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
466 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */
467 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
468 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
475 else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
478 if( p_sys->i_pcr < i_pts )
480 p_sys->i_pcr = i_pts;
481 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
482 (int64_t)p_sys->i_pcr );
485 if( tk->fmt.i_codec == VLC_FOURCC( 'm', 'p', '4', 'a' ) )
487 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
488 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
491 for( i = 0; i < i_sub; i++ )
493 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
494 block_t *p_block = block_New( p_demux, i_sub_size );
497 memcpy( p_block->p_buffer, p_sub, i_sub_size );
501 p_block->i_pts = ( i == 0 ? i_pts : 0 );
503 es_out_Send( p_demux->out, tk->p_es, p_block );
507 else if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
508 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
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 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ) )
520 for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
522 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
523 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
524 p_buf += tk->i_subpacket_size;
526 i_index = tk->i_subpacket_h * i +
527 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
529 p_block->i_dts = p_block->i_pts = i_pts;
530 tk->p_subpackets[i_index] = p_block;
534 if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) )
535 for( i = 0; i < tk->i_subpacket_h / 2; i++ )
537 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
538 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
539 p_buf += tk->i_coded_frame_size;
541 i_index = (i * 2 * tk->i_frame_size) /
542 tk->i_coded_frame_size + y;
544 p_block->i_dts = p_block->i_pts = i_pts;
545 tk->p_subpackets[i_index] = p_block;
549 while( tk->i_out_subpacket != tk->i_subpackets &&
550 tk->p_subpackets[tk->i_out_subpacket] )
552 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
553 tk->p_subpackets[tk->i_out_subpacket] = 0;
555 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
556 es_out_Send( p_demux->out, tk->p_es, p_block );
558 tk->i_out_subpacket++;
561 if( tk->i_subpacket == tk->i_subpackets &&
562 tk->i_out_subpacket != tk->i_subpackets )
564 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
565 "this shouldn't happen" );
568 if( tk->i_subpacket == tk->i_subpackets )
571 tk->i_out_subpacket = 0;
576 block_t *p_block = block_New( p_demux, i_size );
578 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
580 uint8_t *src = p_sys->buffer;
581 uint8_t *dst = p_block->p_buffer;
584 while( dst < &p_block->p_buffer[i_size- 1])
594 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
596 p_block->i_dts = p_block->i_pts = i_pts;
597 es_out_Send( p_demux->out, tk->p_es, p_block );
605 /*****************************************************************************
607 *****************************************************************************/
608 static int Control( demux_t *p_demux, int i_query, va_list args )
610 demux_sys_t *p_sys = p_demux->p_sys;
620 case DEMUX_GET_POSITION:
621 pf = (double*) va_arg( args, double* );
622 i64 = stream_Size( p_demux->s );
625 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
633 case DEMUX_SET_POSITION:
634 f = (double) va_arg( args, double );
635 i64 = stream_Size( p_demux->s );
637 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
639 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
642 pi64 = (int64_t*)va_arg( args, int64_t * );
643 if( p_sys->i_mux_rate > 0 )
645 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
652 case DEMUX_GET_LENGTH:
653 pi64 = (int64_t*)va_arg( args, int64_t * );
655 /* the commented following lines are fen's implementation, which doesn't seem to
656 * work for one reason or another -- FK */
657 /*if( p_sys->i_mux_rate > 0 )
659 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
662 if( p_sys->i_our_duration > 0 )
664 /* our stored duration is in ms, so... */
665 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
674 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
676 /* the core will crash if we provide NULL strings, so check
677 * every string first */
678 if( p_sys->psz_title )
679 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
680 if( p_sys->psz_artist )
681 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
682 if( p_sys->psz_copyright )
683 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
684 if( p_sys->psz_description )
685 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
697 /*****************************************************************************
699 *****************************************************************************/
700 static int HeaderRead( demux_t *p_demux )
702 demux_sys_t *p_sys = p_demux->p_sys;
703 uint8_t header[100]; /* FIXME */
712 /* Read the header */
713 if( stream_Read( p_demux->s, header, 10 ) < 10 )
717 i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
718 i_size = GetDWBE( &header[4] );
719 i_version = GetWBE( &header[8] );
721 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
722 (char*)&i_id, i_size, i_version );
724 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
726 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
729 i_skip = i_size - 10;
731 if( i_id == VLC_FOURCC('.','R','M','F') )
733 if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
734 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
735 GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
739 else if( i_id == VLC_FOURCC('P','R','O','P') )
743 if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
745 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
746 GetDWBE(&header[0]), GetDWBE(&header[4]) );
747 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
748 GetDWBE(&header[8]), GetDWBE(&header[12]) );
749 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) );
750 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) );
751 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) );
752 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) );
753 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) );
754 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) );
756 /* set the duration for export in control */
757 p_sys->i_our_duration = (int)GetDWBE(&header[20]);
759 i_flags = GetWBE(&header[38]);
760 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
762 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
763 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
764 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
767 else if( i_id == VLC_FOURCC('C','O','N','T') )
772 /* FIXME FIXME: should convert from whatever the character
773 * encoding of the input meta data is to UTF-8. */
775 stream_Read( p_demux->s, header, 2 );
776 if( ( i_len = GetWBE( header ) ) > 0 )
778 psz = malloc( i_len + 1 );
779 stream_Read( p_demux->s, psz, i_len );
782 msg_Dbg( p_demux, " - title=`%s'", psz );
784 asprintf( &p_sys->psz_title, psz );
790 stream_Read( p_demux->s, header, 2 );
791 if( ( i_len = GetWBE( header ) ) > 0 )
793 psz = malloc( i_len + 1 );
794 stream_Read( p_demux->s, psz, i_len );
797 msg_Dbg( p_demux, " - author=`%s'", psz );
799 asprintf( &p_sys->psz_artist, psz );
805 stream_Read( p_demux->s, header, 2 );
806 if( ( i_len = GetWBE( header ) ) > 0 )
808 psz = malloc( i_len + 1 );
809 stream_Read( p_demux->s, psz, i_len );
812 msg_Dbg( p_demux, " - copyright=`%s'", psz );
814 asprintf( &p_sys->psz_copyright, psz );
820 stream_Read( p_demux->s, header, 2 );
821 if( ( i_len = GetWBE( header ) ) > 0 )
823 psz = malloc( i_len + 1 );
824 stream_Read( p_demux->s, psz, i_len );
827 msg_Dbg( p_demux, " - comment=`%s'", psz );
829 asprintf( &p_sys->psz_description, psz );
835 else if( i_id == VLC_FOURCC('M','D','P','R') )
837 /* Media properties header */
842 if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
843 i_num = GetWBE( header );
844 msg_Dbg( p_demux, " - id=0x%x", i_num );
845 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
846 GetDWBE(&header[2]), GetDWBE(&header[6]) );
847 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
848 GetDWBE(&header[10]), GetDWBE(&header[14]) );
849 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&header[18]) );
850 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&header[22]) );
851 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&header[26]) );
855 stream_Read( p_demux->s, header, 1 );
856 if( ( i_len = header[0] ) > 0 )
858 psz = malloc( i_len + 1 );
859 stream_Read( p_demux->s, psz, i_len );
862 msg_Dbg( p_demux, " - name=`%s'", psz );
868 stream_Read( p_demux->s, header, 1 );
869 if( ( i_len = header[0] ) > 0 )
871 psz = malloc( i_len + 1 );
872 stream_Read( p_demux->s, psz, i_len );
875 msg_Dbg( p_demux, " - mime=`%s'", psz );
881 stream_Read( p_demux->s, header, 4 );
882 if( ( i_len = GetDWBE( header ) ) > 0 )
884 ReadCodecSpecificData( p_demux, i_len, i_num );
885 stream_Read( p_demux->s, NULL, i_len );
891 else if( i_id == VLC_FOURCC('D','A','T','A') )
893 stream_Read( p_demux->s, header, 8 );
895 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
896 p_sys->i_data_size = i_size;
897 p_sys->i_data_packets_count = GetDWBE( header );
898 p_sys->i_data_packets = 0;
899 p_sys->i_data_offset_next = GetDWBE( &header[4] );
901 msg_Dbg( p_demux, " - packets count=%d next=%u",
902 p_sys->i_data_packets_count,
903 (uint32_t)p_sys->i_data_offset_next );
905 /* we have finished the header */
911 msg_Dbg( p_demux, "unknown chunk" );
914 if( i_skip < 0 ) return VLC_EGENERIC;
915 stream_Read( p_demux->s, NULL, i_skip );
918 /* TODO read index if possible */
923 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
925 demux_sys_t *p_sys = p_demux->p_sys;
928 const uint8_t *p_peek;
930 msg_Dbg( p_demux, " - specific data len=%d", i_len );
931 if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
933 if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
935 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
936 p_peek[10], p_peek[11] ) );
937 fmt.video.i_width = GetWBE( &p_peek[12] );
938 fmt.video.i_height= GetWBE( &p_peek[14] );
941 fmt.p_extra = malloc( 8 );
942 ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
943 ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
945 msg_Dbg( p_demux, " - video 0x%08x 0x%08x",
946 ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
948 if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
949 GetDWBE( &p_peek[30] ) == 0x10003001 )
951 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
953 else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
954 GetDWBE( &p_peek[30] ) == 0x20100001 ||
955 GetDWBE( &p_peek[30] ) == 0x20200002 )
957 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
959 else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
961 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
963 else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
965 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
968 msg_Dbg( p_demux, " - video %4.4s %dx%d",
969 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
971 tk = malloc( sizeof( real_track_t ) );
972 tk->i_out_subpacket = 0;
974 tk->i_subpackets = 0;
975 tk->p_subpackets = NULL;
980 tk->p_es = es_out_Add( p_demux->out, &fmt );
982 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
984 else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
986 int i_version = GetWBE( &p_peek[4] );
987 int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
988 int i_frame_size, i_subpacket_size;
990 msg_Dbg( p_demux, " - audio version=%d", i_version );
993 es_format_Init( &fmt, AUDIO_ES, 0 );
997 msg_Dbg( p_demux, " - audio version 3 is not supported!" );
1001 p_peek += 2; /* 00 00 */
1002 p_peek += 4; /* .ra4 or .ra5 */
1003 p_peek += 4; /* ?? */
1004 p_peek += 2; /* version (4 or 5) */
1005 i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1006 i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1007 i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4;
1008 p_peek += 4; /* ?? */
1009 p_peek += 4; /* ?? */
1010 p_peek += 4; /* ?? */
1011 i_subpacket_h = GetWBE( p_peek ); p_peek += 2;
1012 i_frame_size = GetWBE( p_peek ); p_peek += 2;
1013 i_subpacket_size = GetWBE( p_peek ); p_peek += 2;
1014 p_peek += 2; /* ?? */
1016 if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1018 fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2;
1019 p_peek += 2; /* ?? */
1020 fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;
1021 fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2;
1022 fmt.audio.i_blockalign = i_frame_size;
1024 if( i_version == 5 )
1026 p_peek += 4; /* genr */
1027 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1031 p_peek += p_peek[0] + 1; /* descr 1 */
1032 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
1033 p_peek += p_peek[0] + 1;
1036 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1037 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1039 p_peek += 3; /* ?? */
1040 if( i_version == 5 ) p_peek++;
1042 switch( fmt.i_codec )
1044 case VLC_FOURCC( 'd', 'n', 'e', 't' ):
1045 fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1048 case VLC_FOURCC( 'r', 'a', 'a', 'c' ):
1049 case VLC_FOURCC( 'r', 'a', 'c', 'p' ):
1050 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1051 if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1052 if( fmt.i_extra > 0 )
1054 fmt.p_extra = malloc( fmt.i_extra );
1055 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1058 fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1061 case VLC_FOURCC('c','o','o','k'):
1062 case VLC_FOURCC('a','t','r','c'):
1063 fmt.audio.i_blockalign = i_subpacket_size;
1064 if( !(fmt.i_extra = GetDWBE( p_peek )) ) break;
1065 fmt.p_extra = malloc( fmt.i_extra );
1066 memcpy( fmt.p_extra, p_peek + 4, fmt.i_extra );
1069 case VLC_FOURCC('2','8','_','8'):
1070 fmt.audio.i_blockalign = i_coded_frame_size;
1074 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1075 (char*)&fmt.i_codec );
1079 if( fmt.i_codec != 0 )
1083 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1085 tk = malloc( sizeof( real_track_t ) );
1091 tk->i_subpacket_h = i_subpacket_h;
1092 tk->i_subpacket_size = i_subpacket_size;
1093 tk->i_coded_frame_size = i_coded_frame_size;
1094 tk->i_frame_size = i_frame_size;
1096 tk->i_out_subpacket = 0;
1097 tk->i_subpacket = 0;
1098 tk->i_subpackets = 0;
1099 tk->p_subpackets = NULL;
1100 if( fmt.i_codec == VLC_FOURCC('c','o','o','k') )
1103 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1105 malloc( tk->i_subpackets * sizeof(block_t *) );
1107 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1110 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1112 malloc( tk->i_subpackets * sizeof(block_t *) );
1115 for( i = 0; i < tk->i_subpackets; i++ ) tk->p_subpackets[i] = NULL;
1117 tk->p_es = es_out_Add( p_demux->out, &fmt );
1119 TAB_APPEND( p_sys->i_track, p_sys->track, tk );