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 *****************************************************************************/
25 * Status of this demuxer:
29 * version v3 w/ 14_4/lpcJ is ok.
30 * version v4/5: - atrac3 is ok.
32 * - raac, racp are ok.
33 * - dnet is twisted "The byte order of the data is reversed
35 * - 28_8 seem problematic.
36 * - sipr should be fine, but our decoder suxx :)
37 * - ralf is unsupported, but hardly any sample exist.
38 * - mp3 is unsupported, one sample exists...
42 * v3 and v4/5 headers are parsed.
46 /*****************************************************************************
48 *****************************************************************************/
54 #include <vlc_common.h>
55 #include <vlc_plugin.h>
57 #include <vlc_demux.h>
58 #include <vlc_charset.h>
61 /*****************************************************************************
63 *****************************************************************************/
64 static int Open ( vlc_object_t * );
65 static void Close ( vlc_object_t * );
68 set_description( N_("Real demuxer" ) )
69 set_capability( "demux", 0 )
70 set_category( CAT_INPUT )
71 set_subcategory( SUBCAT_INPUT_DEMUX )
72 set_callbacks( Open, Close )
73 add_shortcut( "real" )
77 /*****************************************************************************
79 *****************************************************************************/
93 int i_frame_slice_count;
98 int i_coded_frame_size;
102 block_t **p_subpackets;
103 mtime_t *p_subpackets_timecode;
111 uint32_t i_file_offset;
112 uint32_t i_time_offset;
113 uint32_t i_frame_index;
118 int64_t i_data_offset;
120 uint32_t i_data_packets_count;
121 uint32_t i_data_packets;
122 int64_t i_data_offset_next;
126 int64_t i_our_duration;
131 char* psz_description;
134 real_track_t **track;
137 uint8_t buffer[65536];
142 int64_t i_index_offset;
147 static int Demux( demux_t *p_demux );
148 static int Control( demux_t *p_demux, int i_query, va_list args );
150 static int HeaderRead( demux_t *p_demux );
151 static int CodecParse( demux_t *p_demux, int i_len, int i_num );
153 static char *StreamReadString2( stream_t *s );
154 static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data );
156 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size );
157 static int RLength( const uint8_t **pp_data, int *pi_data );
158 static uint8_t R8( const uint8_t **pp_data, int *pi_data );
159 static uint16_t R16( const uint8_t **pp_data, int *pi_data );
160 static uint32_t R32( const uint8_t **pp_data, int *pi_data );
162 /*****************************************************************************
164 *****************************************************************************/
165 static int Open( vlc_object_t *p_this )
167 demux_t *p_demux = (demux_t*)p_this;
170 const uint8_t *p_peek;
171 bool b_real_audio = false;
173 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
177 if( !memcmp( p_peek, ".ra", 3 ) )
179 msg_Err( p_demux, ".ra files unsuported" );
182 /* Real Media Format */
183 else if( memcmp( p_peek, ".RMF", 4 ) )
188 /* Fill p_demux field */
189 p_demux->pf_demux = Demux;
190 p_demux->pf_control = Control;
192 p_demux->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
196 p_sys->i_data_offset = 0;
201 p_sys->b_seek = false;
202 p_sys->b_real_audio = b_real_audio;
204 /* Parse the headers */
205 /* Real Audio files */
208 CodecParse( p_demux, 32, 0 ); /* At least 32 */
209 return VLC_EGENERIC; /* We don't know how to read
210 correctly the data yet */
213 else if( HeaderRead( p_demux ) )
216 msg_Err( p_demux, "invalid header" );
217 for( i = 0; i < p_sys->i_track; i++ )
219 real_track_t *tk = p_sys->track[i];
222 es_out_Del( p_demux->out, tk->p_es );
226 if( p_sys->i_track > 0 )
227 free( p_sys->track );
235 /*****************************************************************************
237 *****************************************************************************/
238 static void Close( vlc_object_t *p_this )
240 demux_t *p_demux = (demux_t*)p_this;
241 demux_sys_t *p_sys = p_demux->p_sys;
243 for( int i = 0; i < p_sys->i_track; i++ )
245 real_track_t *tk = p_sys->track[i];
247 es_format_Clean( &tk->fmt );
250 block_Release( tk->p_frame );
252 for( int j = 0; j < tk->i_subpackets; j++ )
254 if( tk->p_subpackets[ j ] )
255 block_Release( tk->p_subpackets[ j ] );
257 if( tk->i_subpackets )
259 free( tk->p_subpackets );
260 free( tk->p_subpackets_timecode );
264 if( p_sys->i_track > 0 )
265 free( p_sys->track );
267 free( p_sys->psz_title );
268 free( p_sys->psz_artist );
269 free( p_sys->psz_copyright );
270 free( p_sys->psz_description );
271 free( p_sys->p_index );
277 /*****************************************************************************
279 *****************************************************************************/
280 static void CheckPcr( demux_t *p_demux, real_track_t *tk, mtime_t i_dts )
282 demux_sys_t *p_sys = p_demux->p_sys;
285 tk->i_last_dts = i_dts;
287 if( p_sys->i_pcr > 0 || i_dts <= 0 )
290 p_sys->i_pcr = i_dts;
291 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
294 static void DemuxVideo( demux_t *p_demux, real_track_t *tk, mtime_t i_dts, unsigned i_flags )
296 demux_sys_t *p_sys = p_demux->p_sys;
298 const uint8_t *p_data = p_sys->buffer;
299 int i_data = p_sys->i_buffer;
303 uint8_t i_hdr = R8( &p_data, &i_data );
304 uint8_t i_type = i_hdr >> 6;
313 R8( &p_data, &i_data );
320 else if( i_type == 3 )
322 i_len = RLength( &p_data, &i_data );
323 i_pos = RLength( &p_data, &i_data );
324 i_frame_num = R8( &p_data, &i_data );
330 assert( i_type == 0 || i_type == 2 );
331 i_seq = R8( &p_data, &i_data );
332 i_len = RLength( &p_data, &i_data );
334 i_pos = RLength( &p_data, &i_data );
335 i_frame_num = R8( &p_data, &i_data );
338 if( (i_seq & 0x7f) == 1 || tk->i_frame_num != i_frame_num )
340 tk->i_frame_slice = 0;
341 tk->i_frame_slice_count = 2 * (i_hdr & 0x3f) + 1;
342 tk->i_frame_pos = 2*4 * tk->i_frame_slice_count + 1;
343 tk->i_frame_size = i_len + 2*4 * tk->i_frame_slice_count + 1;
344 tk->i_frame_num = i_frame_num;
347 block_Release( tk->p_frame );
349 tk->p_frame = block_New( p_demux, tk->i_frame_size );
352 tk->i_frame_slice = 0;
353 tk->i_frame_slice_count = 0;
357 tk->p_frame->i_dts = i_dts;
358 tk->p_frame->i_pts = 0;
360 tk->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
368 i_frame_data = i_len;
372 i_frame_data = i_data;
373 if( i_type == 2 && i_frame_data > i_pos )
374 i_frame_data = i_pos;
379 if( tk->i_frame_slice > tk->i_frame_slice_count )
383 SetDWLE( &tk->p_frame->p_buffer[2*4*(tk->i_frame_slice-1) + 1 + 0], 1 );
384 SetDWLE( &tk->p_frame->p_buffer[2*4*(tk->i_frame_slice-1) + 1 + 4], tk->i_frame_pos - (2*4 * tk->i_frame_slice_count + 1) );
386 if( tk->i_frame_pos + i_frame_data > tk->i_frame_size )
389 memcpy( &tk->p_frame->p_buffer[tk->i_frame_pos], p_data, i_frame_data );
390 RVoid( &p_data, &i_data, i_frame_data );
391 tk->i_frame_pos += i_frame_data;
393 if( i_type != 0 || tk->i_frame_pos >= tk->i_frame_size )
395 /* Fix the buffer once the real number of slice is known */
396 tk->p_frame->p_buffer[0] = tk->i_frame_slice - 1;
397 tk->p_frame->i_buffer = tk->i_frame_pos - 2*4*( tk->i_frame_slice_count - tk->i_frame_slice );
399 memmove( &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice ],
400 &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice_count],
401 tk->i_frame_pos - (2*4*tk->i_frame_slice_count + 1) );
404 CheckPcr( p_demux, tk, tk->p_frame->i_dts );
405 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
407 tk->i_frame_size = 0;
413 static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned int i_flags )
415 demux_sys_t *p_sys = p_demux->p_sys;
417 uint8_t *p_buf = p_sys->buffer;
418 int y = tk->i_subpacket / ( tk->i_frame_size / tk->i_subpacket_size );
421 if( (i_flags & 2) || p_sys->b_seek )
423 y = tk->i_subpacket = 0;
424 tk->i_out_subpacket = 0;
425 p_sys->b_seek = false;
428 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
429 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ) )
431 const int i_num = tk->i_frame_size / tk->i_subpacket_size;
432 for( int i = 0; i < i_num; i++ )
434 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
437 if( &p_buf[tk->i_subpacket_size] > &p_sys->buffer[p_sys->i_buffer] )
440 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
444 p_buf += tk->i_subpacket_size;
446 int i_index = tk->i_subpacket_h * i +
447 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
449 if( tk->p_subpackets[i_index] != NULL )
451 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
452 block_Release( tk->p_subpackets[i_index] );
455 tk->p_subpackets[i_index] = p_block;
456 if( tk->i_subpacket == 0 )
457 tk->p_subpackets_timecode[0] = i_pts;
463 assert( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
464 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) );
466 for( int i = 0; i < tk->i_subpacket_h / 2; i++ )
468 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
471 if( &p_buf[tk->i_subpacket_size] > &p_sys->buffer[p_sys->i_buffer] )
474 int i_index = (i * 2 * tk->i_frame_size) /
475 tk->i_coded_frame_size + y;
477 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
479 p_block->i_pts = i_index == 0 ? i_pts : 0;
481 p_buf += tk->i_coded_frame_size;
483 if( tk->p_subpackets[i_index] != NULL )
485 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
486 block_Release( tk->p_subpackets[i_index] );
489 tk->p_subpackets[i_index] = p_block;
494 while( tk->i_out_subpacket != tk->i_subpackets &&
495 tk->p_subpackets[tk->i_out_subpacket] )
497 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
498 tk->p_subpackets[tk->i_out_subpacket] = 0;
499 if( tk->p_subpackets_timecode[tk->i_out_subpacket] )
502 p_block->i_pts = tk->p_subpackets_timecode[tk->i_out_subpacket];
504 tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
506 tk->i_out_subpacket++;
508 CheckPcr( p_demux, tk, p_block->i_pts );
509 es_out_Send( p_demux->out, tk->p_es, p_block );
512 if( tk->i_subpacket == tk->i_subpackets &&
513 tk->i_out_subpacket != tk->i_subpackets )
515 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
516 "this shouldn't happen" );
519 if( tk->i_subpacket == tk->i_subpackets )
522 tk->i_out_subpacket = 0;
525 static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
527 demux_sys_t *p_sys = p_demux->p_sys;
529 if( p_sys->i_buffer < 2 )
532 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
533 if( p_sys->i_buffer < 2+2*i_sub )
536 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
538 for( int i = 0; i < i_sub; i++ )
540 const int i_sub_size = GetWBE( &p_sys->buffer[2+i*2] );
541 block_t *p_block = block_New( p_demux, i_sub_size );
545 if( &p_sub[i_sub_size] > &p_sys->buffer[p_sys->i_buffer] )
548 memcpy( p_block->p_buffer, p_sub, i_sub_size );
552 p_block->i_pts = ( i == 0 ? i_pts : 0 );
554 CheckPcr( p_demux, tk, p_block->i_pts );
555 es_out_Send( p_demux->out, tk->p_es, p_block );
558 static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
560 demux_sys_t *p_sys = p_demux->p_sys;
562 if( p_sys->i_buffer <= 0 )
565 block_t *p_block = block_New( p_demux, p_sys->i_buffer );
569 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
571 uint8_t *p_src = p_sys->buffer;
572 uint8_t *p_dst = p_block->p_buffer;
575 while( p_dst < &p_block->p_buffer[p_sys->i_buffer - 1])
585 memcpy( p_block->p_buffer, p_sys->buffer, p_sys->i_buffer );
588 p_block->i_pts = i_pts;
590 CheckPcr( p_demux, tk, p_block->i_pts );
591 es_out_Send( p_demux->out, tk->p_es, p_block );
594 static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned int i_flags )
596 switch( tk->fmt.i_codec )
598 case VLC_FOURCC( 'c', 'o', 'o', 'k' ):
599 case VLC_FOURCC( 'a', 't', 'r', 'c' ):
600 case VLC_FOURCC( 's', 'i', 'p', 'r' ):
601 case VLC_FOURCC( '2', '8', '_', '8' ):
602 DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
604 case VLC_FOURCC( 'm','p','4','a' ):
605 DemuxAudioMethod2( p_demux, tk, i_pts );
608 DemuxAudioMethod3( p_demux, tk, i_pts );
613 static int Demux( demux_t *p_demux )
615 demux_sys_t *p_sys = p_demux->p_sys;
618 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
619 p_sys->i_data_packets_count )
621 if( stream_Read( p_demux->s, header, 18 ) < 18 )
624 if( memcmp( header, "DATA", 4 ) )
627 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
628 p_sys->i_data_size = GetDWBE( &header[4] );
629 p_sys->i_data_packets_count = GetDWBE( &header[10] );
630 p_sys->i_data_packets = 0;
631 p_sys->i_data_offset_next = GetDWBE( &header[14] );
633 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
634 p_sys->i_data_packets_count,
635 (unsigned int)p_sys->i_data_offset_next );
638 /* Read Packet Header */
639 if( stream_Read( p_demux->s, header, 12 ) < 12 )
641 //const int i_version = GetWBE( &header[0] );
642 const int i_size = GetWBE( &header[2] ) - 12;
643 const int i_id = GetWBE( &header[4] );
644 const int64_t i_pts = 1 + 1000 * GetDWBE( &header[6] );
645 const int i_flags= header[11]; /* flags 0x02 -> keyframe */
647 p_sys->i_data_packets++;
651 msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
655 assert( i_size <= sizeof(p_sys->buffer) );
657 p_sys->i_buffer = stream_Read( p_demux->s, p_sys->buffer, i_size );
658 if( p_sys->i_buffer < i_size )
661 real_track_t *tk = NULL;
662 for( int i = 0; i < p_sys->i_track; i++ )
664 if( p_sys->track[i]->i_id == i_id )
665 tk = p_sys->track[i];
670 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
674 if( tk->fmt.i_cat == VIDEO_ES )
676 DemuxVideo( p_demux, tk, i_pts, i_flags );
680 assert( tk->fmt.i_cat == AUDIO_ES );
681 DemuxAudio( p_demux, tk, i_pts, i_flags );
686 for( int i = 0; i < p_sys->i_track; i++ )
688 real_track_t *tk = p_sys->track[i];
690 if( i_pcr <= 0 || ( tk->i_last_dts > 0 && tk->i_last_dts > i_pcr ) )
691 i_pcr = tk->i_last_dts;
693 if( i_pcr > 0 && i_pcr != p_sys->i_pcr )
695 p_sys->i_pcr = i_pcr;
696 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
701 /*****************************************************************************
703 *****************************************************************************/
704 static int ControlGoToIndex( demux_t *p_demux, rm_index_t *p_index )
706 demux_sys_t *p_sys = p_demux->p_sys;
708 p_sys->b_seek = true;
709 p_sys->i_pcr = INT64_C(1000) * p_index->i_time_offset;
710 for( int i = 0; i < p_sys->i_track; i++ )
711 p_sys->track[i]->i_last_dts = 0;
712 return stream_Seek( p_demux->s, p_index->i_file_offset );
714 static int ControlSeekTime( demux_t *p_demux, mtime_t i_time )
716 demux_sys_t *p_sys = p_demux->p_sys;
717 rm_index_t *p_index = p_sys->p_index;
719 while( p_index->i_file_offset != 0 )
721 if( p_index->i_time_offset * INT64_C(1000) > i_time )
723 if( p_index != p_sys->p_index )
729 if( p_index->i_file_offset == 0 )
731 return ControlGoToIndex( p_demux, p_index );
733 static int ControlSeekByte( demux_t *p_demux, int64_t i_bytes )
735 demux_sys_t *p_sys = p_demux->p_sys;
736 rm_index_t *p_index = p_sys->p_index;
738 while( p_index->i_file_offset != 0 )
740 if( p_index->i_file_offset > i_bytes )
742 if( p_index != p_sys->p_index )
748 if( p_index->i_file_offset == 0 )
750 return ControlGoToIndex( p_demux, p_index );
753 static int Control( demux_t *p_demux, int i_query, va_list args )
755 demux_sys_t *p_sys = p_demux->p_sys;
762 case DEMUX_GET_POSITION:
763 pf = (double*) va_arg( args, double* );
765 /* read stream size maybe failed in rtsp streaming,
766 so use duration to determin the position at first */
767 if( p_sys->i_our_duration > 0 )
769 *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
774 i64 = stream_Size( p_demux->s );
776 *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
780 pi64 = (int64_t*)va_arg( args, int64_t * );
782 if( p_sys->i_our_duration > 0 )
784 *pi64 = p_sys->i_pcr;
788 /* same as GET_POSTION */
789 i64 = stream_Size( p_demux->s );
790 if( p_sys->i_our_duration > 0 && i64 > 0 )
792 *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
799 case DEMUX_SET_POSITION:
800 f = (double) va_arg( args, double );
801 i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
803 if( !p_sys->p_index && i64 != 0 )
806 msg_Err( p_demux,"Seek No Index Real File failed!" );
807 return VLC_EGENERIC; // no index!
811 /* it is a rtsp stream , it is specials in access/rtsp/... */
812 msg_Dbg(p_demux, "Seek in real rtsp stream!");
813 p_sys->i_pcr = INT64_C(1000) * ( p_sys->i_our_duration * f );
814 p_sys->b_seek = true;
815 return stream_Seek( p_demux->s, p_sys->i_pcr );
817 return ControlSeekByte( p_demux, i64 );
820 if( !p_sys->p_index )
823 i64 = (int64_t) va_arg( args, int64_t );
824 return ControlSeekTime( p_demux, i64 );
826 case DEMUX_GET_LENGTH:
827 pi64 = (int64_t*)va_arg( args, int64_t * );
830 if( p_sys->i_our_duration <= 0 )
833 /* our stored duration is in ms, so... */
834 *pi64 = INT64_C(1000) * p_sys->i_our_duration;
839 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
841 /* the core will crash if we provide NULL strings, so check
842 * every string first */
843 if( p_sys->psz_title )
844 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
845 if( p_sys->psz_artist )
846 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
847 if( p_sys->psz_copyright )
848 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
849 if( p_sys->psz_description )
850 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
861 /*****************************************************************************
863 *****************************************************************************/
865 static void ReadRealIndex( demux_t *p_demux )
867 demux_sys_t *p_sys = p_demux->p_sys;
874 uint32_t i_index_count;
876 if ( p_sys->i_index_offset == 0 )
879 stream_Seek( p_demux->s, p_sys->i_index_offset );
881 if ( stream_Read( p_demux->s, buffer, 20 ) < 20 )
884 i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
885 i_size = GetDWBE( &buffer[4] );
886 i_version = GetWBE( &buffer[8] );
888 msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
889 (char*)&i_id, i_size, i_version );
891 if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
894 i_index_count = GetDWBE( &buffer[10] );
896 msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
898 if( i_index_count >= ( 0xffffffff / sizeof( rm_index_t ) ) )
901 if( GetDWBE( &buffer[16] ) > 0 )
902 msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
903 GetDWBE( &buffer[16] ) );
905 p_sys->p_index = malloc( ( i_index_count + 1 ) * sizeof( rm_index_t ) );
906 if( p_sys->p_index == NULL )
909 for( i = 0; i < i_index_count; i++ )
911 if( stream_Read( p_demux->s, buffer, 14 ) < 14 )
914 if( GetWBE( &buffer[0] ) != 0 )
916 msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
917 GetWBE( &buffer[0] ) );
921 p_sys->p_index[i].i_time_offset = GetDWBE( &buffer[2] );
922 p_sys->p_index[i].i_file_offset = GetDWBE( &buffer[6] );
923 p_sys->p_index[i].i_frame_index = GetDWBE( &buffer[10] );
925 "Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32,
926 p_sys->p_index[i].i_time_offset,
927 p_sys->p_index[i].i_file_offset,
928 p_sys->p_index[i].i_frame_index );
930 memset( p_sys->p_index + i_index_count, 0, sizeof( rm_index_t ) );
933 /*****************************************************************************
935 *****************************************************************************/
936 static int HeaderRMF( demux_t *p_demux )
940 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
943 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
944 GetDWBE( &p_buffer[0] ), GetDWBE( &p_buffer[4] ) );
947 static int HeaderPROP( demux_t *p_demux )
949 demux_sys_t *p_sys = p_demux->p_sys;
951 uint8_t p_buffer[40];
954 if( stream_Read( p_demux->s, p_buffer, 40 ) < 40 )
957 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
958 GetDWBE(&p_buffer[0]), GetDWBE(&p_buffer[4]) );
959 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
960 GetDWBE(&p_buffer[8]), GetDWBE(&p_buffer[12]) );
961 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&p_buffer[16]) );
962 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&p_buffer[20]) );
963 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&p_buffer[24]) );
964 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&p_buffer[28]) );
965 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&p_buffer[32]) );
966 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&p_buffer[36]) );
968 /* set the duration for export in control */
969 p_sys->i_our_duration = GetDWBE(&p_buffer[20]);
971 p_sys->i_index_offset = GetDWBE(&p_buffer[28]);
973 i_flags = GetWBE(&p_buffer[38]);
974 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
976 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
977 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
978 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
982 static int HeaderCONT( demux_t *p_demux )
984 demux_sys_t *p_sys = p_demux->p_sys;
987 p_sys->psz_title = StreamReadString2( p_demux->s );
988 if( p_sys->psz_title )
989 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
992 p_sys->psz_artist = StreamReadString2( p_demux->s );
993 if( p_sys->psz_artist )
994 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
997 p_sys->psz_copyright = StreamReadString2( p_demux->s );
998 if( p_sys->psz_copyright )
999 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1002 p_sys->psz_description = StreamReadString2( p_demux->s );
1003 if( p_sys->psz_description )
1004 msg_Dbg( p_demux, " - comment=`%s'", p_sys->psz_description );
1008 static int HeaderMDPR( demux_t *p_demux )
1010 uint8_t p_buffer[30];
1012 /* Media properties header */
1013 if( stream_Read( p_demux->s, p_buffer, 30 ) < 30 )
1014 return VLC_EGENERIC;
1016 const int i_num = GetWBE( &p_buffer[0] );
1017 msg_Dbg( p_demux, " - id=0x%x", i_num );
1018 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
1019 GetDWBE(&p_buffer[2]), GetDWBE(&p_buffer[6]) );
1020 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
1021 GetDWBE(&p_buffer[10]), GetDWBE(&p_buffer[14]) );
1022 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&p_buffer[18]) );
1023 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&p_buffer[22]) );
1024 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&p_buffer[26]) );
1027 const uint8_t *p_peek;
1028 int i_peek_org = stream_Peek( p_demux->s, &p_peek, 2 * 256 );
1029 int i_peek = i_peek_org;
1031 return VLC_EGENERIC;
1033 char *psz_name = MemoryReadString1( &p_peek, &i_peek );
1036 msg_Dbg( p_demux, " - name=`%s'", psz_name );
1039 char *psz_mime = MemoryReadString1( &p_peek, &i_peek );
1042 msg_Dbg( p_demux, " - mime=`%s'", psz_mime );
1045 const int i_skip = i_peek_org - i_peek;
1046 if( i_skip > 0 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
1047 return VLC_EGENERIC;
1050 if( stream_Read( p_demux->s, p_buffer, 4 ) < 4 )
1051 return VLC_EGENERIC;
1053 const uint32_t i_size = GetDWBE( p_buffer );
1056 CodecParse( p_demux, i_size, i_num );
1057 if( stream_Read( p_demux->s, NULL, i_size ) < i_size )
1058 return VLC_EGENERIC;
1062 static int HeaderDATA( demux_t *p_demux, uint32_t i_size )
1064 demux_sys_t *p_sys = p_demux->p_sys;
1065 uint8_t p_buffer[8];
1067 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
1068 return VLC_EGENERIC;
1070 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
1071 p_sys->i_data_size = i_size;
1072 p_sys->i_data_packets_count = GetDWBE( p_buffer );
1073 p_sys->i_data_packets = 0;
1074 p_sys->i_data_offset_next = GetDWBE( &p_buffer[4] );
1076 msg_Dbg( p_demux, " - packets count=%d next=%u",
1077 p_sys->i_data_packets_count,
1078 (unsigned int)p_sys->i_data_offset_next );
1082 static int HeaderRead( demux_t *p_demux )
1084 demux_sys_t *p_sys = p_demux->p_sys;
1086 p_sys->p_meta = vlc_meta_New();
1090 const int64_t i_stream_position = stream_Tell( p_demux->s );
1091 uint8_t header[100]; /* FIXME */
1093 /* Read the header */
1094 if( stream_Read( p_demux->s, header, 10 ) < 10 )
1095 return VLC_EGENERIC;
1097 const uint32_t i_id = VLC_FOURCC( header[0], header[1],
1098 header[2], header[3] );
1099 const uint32_t i_size = GetDWBE( &header[4] );
1100 const int i_version = GetWBE( &header[8] );
1102 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
1103 (char*)&i_id, i_size, i_version );
1106 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
1108 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
1109 return VLC_EGENERIC;
1115 case VLC_FOURCC('.','R','M','F'):
1116 i_ret = HeaderRMF( p_demux );
1118 case VLC_FOURCC('P','R','O','P'):
1119 i_ret = HeaderPROP( p_demux );
1121 case VLC_FOURCC('C','O','N','T'):
1122 i_ret = HeaderCONT( p_demux );
1124 case VLC_FOURCC('M','D','P','R'):
1125 i_ret = HeaderMDPR( p_demux );
1127 case VLC_FOURCC('D','A','T','A'):
1128 i_ret = HeaderDATA( p_demux, i_size );
1132 msg_Dbg( p_demux, "unknown chunk" );
1133 i_ret = VLC_SUCCESS;
1139 if( i_id == VLC_FOURCC('D','A','T','A') ) /* In this case, parsing is finished */
1142 /* Skip unread data */
1143 const int64_t i_stream_current = stream_Tell( p_demux->s );
1144 const int64_t i_stream_skip = (i_stream_position + i_size) - i_stream_current;
1146 if( i_stream_skip > 0 )
1148 if( stream_Read( p_demux->s, NULL, i_stream_skip ) != i_stream_skip )
1149 return VLC_EGENERIC;
1151 else if( i_stream_skip < 0 )
1153 return VLC_EGENERIC;
1157 /* read index if possible */
1158 if( p_sys->i_index_offset > 0 )
1160 const int64_t i_position = stream_Tell( p_demux->s );
1162 ReadRealIndex( p_demux );
1164 if( stream_Seek( p_demux->s, i_position ) )
1165 return VLC_EGENERIC;
1171 * This function will read a pascal string with size stored in 1 byte from a
1174 * FIXME what is the right charset ?
1176 static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data )
1178 const uint8_t *p_data = *pp_data;
1179 int i_data = *pi_data;
1181 char *psz_string = NULL;
1186 int i_length = *p_data++; i_data--;
1187 if( i_length > i_data )
1192 psz_string = strndup( (const char*)p_data, i_length );
1194 EnsureUTF8( psz_string );
1206 * This function will read a pascal string with size stored in 2 bytes from
1209 * FIXME what is the right charset ?
1211 static char *StreamReadString2( stream_t *s )
1215 if( stream_Read( s, p_tmp, 2 ) < 2 )
1218 const int i_length = GetWBE( p_tmp );
1222 char *psz_string = calloc( 1, i_length + 1 );
1224 stream_Read( s, psz_string, i_length ); /* Valid even if !psz_string */
1227 EnsureUTF8( psz_string );
1231 static void CodecMetaRead( demux_t *p_demux, const uint8_t **pp_data, int *pi_data )
1233 demux_sys_t *p_sys = p_demux->p_sys;
1236 p_sys->psz_title = MemoryReadString1( pp_data, pi_data );
1237 if( p_sys->psz_title )
1238 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
1241 p_sys->psz_artist = MemoryReadString1( pp_data, pi_data );
1242 if( p_sys->psz_artist )
1243 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
1246 p_sys->psz_copyright = MemoryReadString1( pp_data, pi_data );
1247 if( p_sys->psz_copyright )
1248 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1251 p_sys->psz_description = MemoryReadString1( pp_data, pi_data );
1252 if( p_sys->psz_description )
1253 msg_Dbg( p_demux, " - Comment=`%s'", p_sys->psz_description );
1256 static int CodecVideoParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1258 demux_sys_t *p_sys = p_demux->p_sys;
1261 return VLC_EGENERIC;
1265 es_format_Init( &fmt, VIDEO_ES,
1266 VLC_FOURCC( p_data[8], p_data[9], p_data[10], p_data[11] ) );
1267 fmt.video.i_width = GetWBE( &p_data[12] );
1268 fmt.video.i_height= GetWBE( &p_data[14] );
1269 fmt.video.i_frame_rate = (GetWBE( &p_data[22] ) << 16) | GetWBE( &p_data[24] );
1270 fmt.video.i_frame_rate_base = 1 << 16;
1273 fmt.p_extra = malloc( 8 );
1277 memcpy( fmt.p_extra, &p_data[26], 8 );
1279 //msg_Dbg( p_demux, " - video 0x%08x 0x%08x", dw0, dw1 );
1282 switch( GetDWBE( &p_data[30] ) )
1286 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
1292 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
1295 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
1298 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1301 msg_Dbg( p_demux, " - video %4.4s %dx%d - %8.8x",
1302 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height, GetDWBE( &p_data[30] ) );
1304 real_track_t *tk = malloc( sizeof( *tk ) );
1307 es_format_Clean( &fmt );
1310 tk->i_out_subpacket = 0;
1311 tk->i_subpacket = 0;
1312 tk->i_subpackets = 0;
1313 tk->p_subpackets = NULL;
1314 tk->p_subpackets_timecode = NULL;
1317 tk->i_frame_num = -1;
1318 tk->i_frame_size = 0;
1321 tk->p_es = es_out_Add( p_demux->out, &fmt );
1323 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1326 static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1328 demux_sys_t *p_sys = p_demux->p_sys;
1332 return VLC_EGENERIC;
1335 int i_coded_frame_size = 0;
1336 int i_subpacket_h = 0;
1337 int i_frame_size = 0;
1338 int i_subpacket_size = 0;
1340 int i_version = GetWBE( &p_data[4] );
1341 int i_extra_codec = 0;
1343 msg_Dbg( p_demux, " - audio version=%d", i_version );
1345 es_format_Init( &fmt, AUDIO_ES, 0 );
1347 RVoid( &p_data, &i_data, 6 ); /* 4 + version */
1348 if( i_version == 3 ) /* RMF version 3 or .ra version 3 */
1350 RVoid( &p_data, &i_data, 2 + 10 + 4 );
1353 CodecMetaRead( p_demux, &p_data, &i_data );
1355 RVoid( &p_data, &i_data, 1 + 1 );
1357 memcpy( &fmt.i_codec, p_data, 4 );
1358 RVoid( &p_data, &i_data, 4 );
1360 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1361 fmt.audio.i_rate = 8000;
1363 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1364 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1366 else /* RMF version 4/5 or .ra version 4 */
1368 RVoid( &p_data, &i_data, 2 + 4 + 4 + 2 + 4 );
1369 i_flavor = R16( &p_data, &i_data );
1370 i_coded_frame_size = R32( &p_data, &i_data );
1371 RVoid( &p_data, &i_data, 4 + 4 + 4 );
1372 i_subpacket_h = R16( &p_data, &i_data );
1373 i_frame_size = R16( &p_data, &i_data );
1374 i_subpacket_size = R16( &p_data, &i_data );
1375 if( (!i_subpacket_size && !p_sys->b_real_audio ) ||
1376 !i_frame_size || !i_coded_frame_size )
1377 return VLC_EGENERIC;
1378 RVoid( &p_data, &i_data, 2 + (i_version == 5 ? 6 : 0 ) );
1380 fmt.audio.i_rate = R16( &p_data, &i_data );
1381 RVoid( &p_data, &i_data, 2 );
1382 fmt.audio.i_bitspersample = R16( &p_data, &i_data );
1383 fmt.audio.i_channels = R16( &p_data, &i_data );
1384 fmt.audio.i_blockalign = i_frame_size;
1386 if( i_version == 5 )
1390 memcpy( p_genr, &p_data[0], 4 );
1391 memcpy( &fmt.i_codec, &p_data[4], 4 );
1393 RVoid( &p_data, &i_data, 8 );
1395 else /* version 4 */
1398 RVoid( &p_data, &i_data, 1 + *p_data );
1399 if( i_data >= 1 + 4 )
1400 memcpy( &fmt.i_codec, &p_data[1], 4 );
1402 RVoid( &p_data, &i_data, 1 + *p_data );
1405 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1406 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1408 RVoid( &p_data, &i_data, 3 );
1410 if( p_sys->b_real_audio )
1412 CodecMetaRead( p_demux, &p_data, &i_data );
1416 if( i_version == 5 )
1417 RVoid( &p_data, &i_data, 1 );
1418 i_extra_codec = R32( &p_data, &i_data );
1422 switch( fmt.i_codec )
1424 case VLC_FOURCC('l','p','c','J'):
1425 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1426 case VLC_FOURCC('1','4','_','4'):
1427 fmt.audio.i_blockalign = 0x14 ;
1430 case VLC_FOURCC('2','8','_','8'):
1431 fmt.audio.i_blockalign = i_coded_frame_size;
1434 case VLC_FOURCC( 'd','n','e','t' ):
1435 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1438 case VLC_FOURCC( 'r','a','a','c' ):
1439 case VLC_FOURCC( 'r','a','c','p' ):
1440 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1442 if( i_extra_codec > 0 )
1445 RVoid( &p_data, &i_data, 1 );
1447 if( i_extra_codec > 0 )
1449 fmt.p_extra = malloc( fmt.i_extra );
1450 if( !fmt.p_extra || fmt.i_extra > i_data )
1453 fmt.i_extra = i_extra_codec;
1454 memcpy( fmt.p_extra, p_data, fmt.i_extra );
1458 case VLC_FOURCC('s','i','p','r'):
1459 fmt.audio.i_flavor = i_flavor;
1460 case VLC_FOURCC('c','o','o','k'):
1461 case VLC_FOURCC('a','t','r','c'):
1462 if( !memcmp( p_genr, "genr", 4 ) )
1463 fmt.audio.i_blockalign = i_subpacket_size;
1465 fmt.audio.i_blockalign = i_coded_frame_size;
1467 if( i_extra_codec > 0 )
1469 fmt.p_extra = malloc( fmt.i_extra );
1470 if( !fmt.p_extra || fmt.i_extra > i_data )
1473 fmt.i_extra = i_extra_codec;
1474 memcpy( fmt.p_extra, p_data, fmt.i_extra );
1479 case VLC_FOURCC('r','a','l','f'):
1480 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1481 (char*)&fmt.i_codec );
1485 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1486 (char*)&fmt.i_codec );
1489 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1492 real_track_t *tk = malloc( sizeof( *tk ) );
1495 es_format_Clean( &fmt );
1500 tk->i_frame_size = 0;
1503 tk->i_subpacket_h = i_subpacket_h;
1504 tk->i_subpacket_size = i_subpacket_size;
1505 tk->i_coded_frame_size = i_coded_frame_size;
1506 tk->i_frame_size = i_frame_size;
1508 tk->i_out_subpacket = 0;
1509 tk->i_subpacket = 0;
1510 tk->i_subpackets = 0;
1511 tk->p_subpackets = NULL;
1512 tk->p_subpackets_timecode = NULL;
1513 if( fmt.i_codec == VLC_FOURCC('c','o','o','k') ||
1514 fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1517 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1519 calloc( tk->i_subpackets, sizeof(block_t *) );
1520 tk->p_subpackets_timecode =
1521 calloc( tk->i_subpackets , sizeof( int64_t ) );
1523 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1526 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1528 calloc( tk->i_subpackets, sizeof(block_t *) );
1529 tk->p_subpackets_timecode =
1530 calloc( tk->i_subpackets , sizeof( int64_t ) );
1533 /* Check if the calloc went correctly */
1534 if( !tk->p_subpackets && !tk->p_subpackets_timecode)
1536 free( tk->p_subpackets_timecode );
1537 free( tk->p_subpackets );
1539 msg_Err( p_demux, "Can't alloc subpacket" );
1540 return VLC_EGENERIC;
1544 tk->p_es = es_out_Add( p_demux->out, &fmt );
1546 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1552 static int CodecParse( demux_t *p_demux, int i_len, int i_num )
1554 const uint8_t *p_peek;
1556 msg_Dbg( p_demux, " - specific data len=%d", i_len );
1557 if( stream_Peek( p_demux->s, &p_peek, i_len ) < i_len )
1558 return VLC_EGENERIC;
1560 if( i_len >= 8 && !memcmp( &p_peek[4], "VIDO", 4 ) )
1562 return CodecVideoParse( p_demux, i_num, p_peek, i_len );
1564 else if( i_len >= 4 && !memcmp( &p_peek[0], ".ra\xfd", 4 ) )
1566 return CodecAudioParse( p_demux, i_num, p_peek, i_len );
1570 /* Small memory buffer helpers */
1571 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size )
1573 if( i_size > *pi_data )
1579 #define RX(name, type, size, code ) \
1580 static type name( const uint8_t **pp_data, int *pi_data ) { \
1581 if( *pi_data < (size) ) \
1584 RVoid( pp_data, pi_data, size ); \
1587 RX(R8, uint8_t, 1, **pp_data )
1588 RX(R16, uint16_t, 2, GetWBE( *pp_data ) )
1589 RX(R32, uint32_t, 4, GetDWBE( *pp_data ) )
1590 static int RLength( const uint8_t **pp_data, int *pi_data )
1592 const int v0 = R16( pp_data, pi_data ) & 0x7FFF;
1595 return (v0 << 16) | R16( pp_data, pi_data );