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", 50 )
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;
144 real_index_t *p_index;
147 static int Demux( demux_t * );
148 static int Control( demux_t *, int i_query, va_list args );
151 static void DemuxVideo( demux_t *, real_track_t *tk, mtime_t i_dts, unsigned i_flags );
152 static void DemuxAudio( demux_t *, real_track_t *tk, mtime_t i_pts, unsigned i_flags );
154 static int ControlSeekByte( demux_t *, int64_t i_bytes );
155 static int ControlSeekTime( demux_t *, mtime_t i_time );
157 static int HeaderRead( demux_t *p_demux );
158 static int CodecParse( demux_t *p_demux, int i_len, int i_num );
160 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size );
161 static int RLength( const uint8_t **pp_data, int *pi_data );
162 static uint8_t R8( const uint8_t **pp_data, int *pi_data );
163 static uint16_t R16( const uint8_t **pp_data, int *pi_data );
164 static uint32_t R32( const uint8_t **pp_data, int *pi_data );
166 /*****************************************************************************
168 *****************************************************************************/
169 static int Open( vlc_object_t *p_this )
171 demux_t *p_demux = (demux_t*)p_this;
174 const uint8_t *p_peek;
175 bool b_real_audio = false;
177 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
181 if( !memcmp( p_peek, ".ra", 3 ) )
183 msg_Err( p_demux, ".ra files unsuported" );
186 /* Real Media Format */
187 else if( memcmp( p_peek, ".RMF", 4 ) )
192 /* Fill p_demux field */
193 p_demux->pf_demux = Demux;
194 p_demux->pf_control = Control;
196 p_demux->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
200 p_sys->i_data_offset = 0;
205 p_sys->b_seek = false;
206 p_sys->b_real_audio = b_real_audio;
208 /* Parse the headers */
209 /* Real Audio files */
212 CodecParse( p_demux, 32, 0 ); /* At least 32 */
213 return VLC_EGENERIC; /* We don't know how to read
214 correctly the data yet */
217 else if( HeaderRead( p_demux ) )
220 msg_Err( p_demux, "invalid header" );
221 for( i = 0; i < p_sys->i_track; i++ )
223 real_track_t *tk = p_sys->track[i];
226 es_out_Del( p_demux->out, tk->p_es );
230 if( p_sys->i_track > 0 )
231 free( p_sys->track );
239 /*****************************************************************************
241 *****************************************************************************/
242 static void Close( vlc_object_t *p_this )
244 demux_t *p_demux = (demux_t*)p_this;
245 demux_sys_t *p_sys = p_demux->p_sys;
247 for( int i = 0; i < p_sys->i_track; i++ )
249 real_track_t *tk = p_sys->track[i];
251 es_format_Clean( &tk->fmt );
254 block_Release( tk->p_frame );
256 for( int j = 0; j < tk->i_subpackets; j++ )
258 if( tk->p_subpackets[ j ] )
259 block_Release( tk->p_subpackets[ j ] );
261 if( tk->i_subpackets )
263 free( tk->p_subpackets );
264 free( tk->p_subpackets_timecode );
268 if( p_sys->i_track > 0 )
269 free( p_sys->track );
271 free( p_sys->psz_title );
272 free( p_sys->psz_artist );
273 free( p_sys->psz_copyright );
274 free( p_sys->psz_description );
275 free( p_sys->p_index );
281 /*****************************************************************************
283 *****************************************************************************/
284 static int Demux( demux_t *p_demux )
286 demux_sys_t *p_sys = p_demux->p_sys;
289 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
290 p_sys->i_data_packets_count )
292 if( stream_Read( p_demux->s, header, 18 ) < 18 )
295 if( memcmp( header, "DATA", 4 ) )
298 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
299 p_sys->i_data_size = GetDWBE( &header[4] );
300 p_sys->i_data_packets_count = GetDWBE( &header[10] );
301 p_sys->i_data_packets = 0;
302 p_sys->i_data_offset_next = GetDWBE( &header[14] );
304 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
305 p_sys->i_data_packets_count,
306 (unsigned int)p_sys->i_data_offset_next );
309 /* Read Packet Header */
310 if( stream_Read( p_demux->s, header, 12 ) < 12 )
312 //const int i_version = GetWBE( &header[0] );
313 const int i_size = GetWBE( &header[2] ) - 12;
314 const int i_id = GetWBE( &header[4] );
315 const int64_t i_pts = 1 + 1000 * GetDWBE( &header[6] );
316 const int i_flags= header[11]; /* flags 0x02 -> keyframe */
318 p_sys->i_data_packets++;
322 msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
326 assert( i_size <= sizeof(p_sys->buffer) );
328 p_sys->i_buffer = stream_Read( p_demux->s, p_sys->buffer, i_size );
329 if( p_sys->i_buffer < i_size )
332 real_track_t *tk = NULL;
333 for( int i = 0; i < p_sys->i_track; i++ )
335 if( p_sys->track[i]->i_id == i_id )
336 tk = p_sys->track[i];
341 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
345 if( tk->fmt.i_cat == VIDEO_ES )
347 DemuxVideo( p_demux, tk, i_pts, i_flags );
351 assert( tk->fmt.i_cat == AUDIO_ES );
352 DemuxAudio( p_demux, tk, i_pts, i_flags );
357 for( int i = 0; i < p_sys->i_track; i++ )
359 real_track_t *tk = p_sys->track[i];
361 if( i_pcr <= 0 || ( tk->i_last_dts > 0 && tk->i_last_dts > i_pcr ) )
362 i_pcr = tk->i_last_dts;
364 if( i_pcr > 0 && i_pcr != p_sys->i_pcr )
366 p_sys->i_pcr = i_pcr;
367 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
372 /*****************************************************************************
374 *****************************************************************************/
375 static int Control( demux_t *p_demux, int i_query, va_list args )
377 demux_sys_t *p_sys = p_demux->p_sys;
384 case DEMUX_GET_POSITION:
385 pf = (double*) va_arg( args, double* );
387 /* read stream size maybe failed in rtsp streaming,
388 so use duration to determin the position at first */
389 if( p_sys->i_our_duration > 0 )
391 *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
396 i64 = stream_Size( p_demux->s );
398 *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
402 pi64 = (int64_t*)va_arg( args, int64_t * );
404 if( p_sys->i_our_duration > 0 )
406 *pi64 = p_sys->i_pcr;
410 /* same as GET_POSTION */
411 i64 = stream_Size( p_demux->s );
412 if( p_sys->i_our_duration > 0 && i64 > 0 )
414 *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
421 case DEMUX_SET_POSITION:
422 f = (double) va_arg( args, double );
423 i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
425 if( !p_sys->p_index && i64 != 0 )
428 msg_Err( p_demux,"Seek No Index Real File failed!" );
429 return VLC_EGENERIC; // no index!
433 /* it is a rtsp stream , it is specials in access/rtsp/... */
434 msg_Dbg(p_demux, "Seek in real rtsp stream!");
435 p_sys->i_pcr = INT64_C(1000) * ( p_sys->i_our_duration * f );
436 p_sys->b_seek = true;
437 return stream_Seek( p_demux->s, p_sys->i_pcr );
439 return ControlSeekByte( p_demux, i64 );
442 if( !p_sys->p_index )
445 i64 = (int64_t) va_arg( args, int64_t );
446 return ControlSeekTime( p_demux, i64 );
448 case DEMUX_GET_LENGTH:
449 pi64 = (int64_t*)va_arg( args, int64_t * );
452 if( p_sys->i_our_duration <= 0 )
455 /* our stored duration is in ms, so... */
456 *pi64 = INT64_C(1000) * p_sys->i_our_duration;
461 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
463 /* the core will crash if we provide NULL strings, so check
464 * every string first */
465 if( p_sys->psz_title )
466 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
467 if( p_sys->psz_artist )
468 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
469 if( p_sys->psz_copyright )
470 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
471 if( p_sys->psz_description )
472 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
483 /*****************************************************************************
485 *****************************************************************************/
486 static void CheckPcr( demux_t *p_demux, real_track_t *tk, mtime_t i_dts )
488 demux_sys_t *p_sys = p_demux->p_sys;
491 tk->i_last_dts = i_dts;
493 if( p_sys->i_pcr > 0 || i_dts <= 0 )
496 p_sys->i_pcr = i_dts;
497 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
500 static void DemuxVideo( demux_t *p_demux, real_track_t *tk, mtime_t i_dts, unsigned i_flags )
502 demux_sys_t *p_sys = p_demux->p_sys;
504 const uint8_t *p_data = p_sys->buffer;
505 int i_data = p_sys->i_buffer;
509 uint8_t i_hdr = R8( &p_data, &i_data );
510 uint8_t i_type = i_hdr >> 6;
519 R8( &p_data, &i_data );
526 else if( i_type == 3 )
528 i_len = RLength( &p_data, &i_data );
529 i_pos = RLength( &p_data, &i_data );
530 i_frame_num = R8( &p_data, &i_data );
536 assert( i_type == 0 || i_type == 2 );
537 i_seq = R8( &p_data, &i_data );
538 i_len = RLength( &p_data, &i_data );
540 i_pos = RLength( &p_data, &i_data );
541 i_frame_num = R8( &p_data, &i_data );
544 if( (i_seq & 0x7f) == 1 || tk->i_frame_num != i_frame_num )
546 tk->i_frame_slice = 0;
547 tk->i_frame_slice_count = 2 * (i_hdr & 0x3f) + 1;
548 tk->i_frame_pos = 2*4 * tk->i_frame_slice_count + 1;
549 tk->i_frame_size = i_len + 2*4 * tk->i_frame_slice_count + 1;
550 tk->i_frame_num = i_frame_num;
553 block_Release( tk->p_frame );
555 tk->p_frame = block_New( p_demux, tk->i_frame_size );
558 tk->i_frame_slice = 0;
559 tk->i_frame_slice_count = 0;
563 tk->p_frame->i_dts = i_dts;
564 tk->p_frame->i_pts = 0;
566 tk->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
574 i_frame_data = i_len;
578 i_frame_data = i_data;
579 if( i_type == 2 && i_frame_data > i_pos )
580 i_frame_data = i_pos;
585 if( tk->i_frame_slice > tk->i_frame_slice_count )
589 SetDWLE( &tk->p_frame->p_buffer[2*4*(tk->i_frame_slice-1) + 1 + 0], 1 );
590 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) );
592 if( tk->i_frame_pos + i_frame_data > tk->i_frame_size )
595 memcpy( &tk->p_frame->p_buffer[tk->i_frame_pos], p_data, i_frame_data );
596 RVoid( &p_data, &i_data, i_frame_data );
597 tk->i_frame_pos += i_frame_data;
599 if( i_type != 0 || tk->i_frame_pos >= tk->i_frame_size )
601 /* Fix the buffer once the real number of slice is known */
602 tk->p_frame->p_buffer[0] = tk->i_frame_slice - 1;
603 tk->p_frame->i_buffer = tk->i_frame_pos - 2*4*( tk->i_frame_slice_count - tk->i_frame_slice );
605 memmove( &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice ],
606 &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice_count],
607 tk->i_frame_pos - (2*4*tk->i_frame_slice_count + 1) );
610 CheckPcr( p_demux, tk, tk->p_frame->i_dts );
611 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
613 tk->i_frame_size = 0;
619 static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned int i_flags )
621 demux_sys_t *p_sys = p_demux->p_sys;
623 uint8_t *p_buf = p_sys->buffer;
624 int y = tk->i_subpacket / ( tk->i_frame_size / tk->i_subpacket_size );
627 if( (i_flags & 2) || p_sys->b_seek )
629 y = tk->i_subpacket = 0;
630 tk->i_out_subpacket = 0;
631 p_sys->b_seek = false;
634 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
635 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ) )
637 const int i_num = tk->i_frame_size / tk->i_subpacket_size;
638 for( int i = 0; i < i_num; i++ )
640 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
643 if( &p_buf[tk->i_subpacket_size] > &p_sys->buffer[p_sys->i_buffer] )
646 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
650 p_buf += tk->i_subpacket_size;
652 int i_index = tk->i_subpacket_h * i +
653 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
655 if( tk->p_subpackets[i_index] != NULL )
657 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
658 block_Release( tk->p_subpackets[i_index] );
661 tk->p_subpackets[i_index] = p_block;
662 if( tk->i_subpacket == 0 )
663 tk->p_subpackets_timecode[0] = i_pts;
669 assert( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
670 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) );
672 for( int i = 0; i < tk->i_subpacket_h / 2; i++ )
674 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
677 if( &p_buf[tk->i_subpacket_size] > &p_sys->buffer[p_sys->i_buffer] )
680 int i_index = (i * 2 * tk->i_frame_size) /
681 tk->i_coded_frame_size + y;
683 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
685 p_block->i_pts = i_index == 0 ? i_pts : 0;
687 p_buf += tk->i_coded_frame_size;
689 if( tk->p_subpackets[i_index] != NULL )
691 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
692 block_Release( tk->p_subpackets[i_index] );
695 tk->p_subpackets[i_index] = p_block;
700 while( tk->i_out_subpacket != tk->i_subpackets &&
701 tk->p_subpackets[tk->i_out_subpacket] )
703 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
704 tk->p_subpackets[tk->i_out_subpacket] = 0;
705 if( tk->p_subpackets_timecode[tk->i_out_subpacket] )
708 p_block->i_pts = tk->p_subpackets_timecode[tk->i_out_subpacket];
710 tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
712 tk->i_out_subpacket++;
714 CheckPcr( p_demux, tk, p_block->i_pts );
715 es_out_Send( p_demux->out, tk->p_es, p_block );
718 if( tk->i_subpacket == tk->i_subpackets &&
719 tk->i_out_subpacket != tk->i_subpackets )
721 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
722 "this shouldn't happen" );
725 if( tk->i_subpacket == tk->i_subpackets )
728 tk->i_out_subpacket = 0;
731 static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
733 demux_sys_t *p_sys = p_demux->p_sys;
735 if( p_sys->i_buffer < 2 )
738 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
739 if( p_sys->i_buffer < 2+2*i_sub )
742 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
744 for( int i = 0; i < i_sub; i++ )
746 const int i_sub_size = GetWBE( &p_sys->buffer[2+i*2] );
747 block_t *p_block = block_New( p_demux, i_sub_size );
751 if( &p_sub[i_sub_size] > &p_sys->buffer[p_sys->i_buffer] )
754 memcpy( p_block->p_buffer, p_sub, i_sub_size );
758 p_block->i_pts = ( i == 0 ? i_pts : 0 );
760 CheckPcr( p_demux, tk, p_block->i_pts );
761 es_out_Send( p_demux->out, tk->p_es, p_block );
764 static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
766 demux_sys_t *p_sys = p_demux->p_sys;
768 if( p_sys->i_buffer <= 0 )
771 block_t *p_block = block_New( p_demux, p_sys->i_buffer );
775 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
777 uint8_t *p_src = p_sys->buffer;
778 uint8_t *p_dst = p_block->p_buffer;
781 while( p_dst < &p_block->p_buffer[p_sys->i_buffer - 1])
791 memcpy( p_block->p_buffer, p_sys->buffer, p_sys->i_buffer );
794 p_block->i_pts = i_pts;
796 CheckPcr( p_demux, tk, p_block->i_pts );
797 es_out_Send( p_demux->out, tk->p_es, p_block );
800 static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned i_flags )
802 switch( tk->fmt.i_codec )
804 case VLC_FOURCC( 'c', 'o', 'o', 'k' ):
805 case VLC_FOURCC( 'a', 't', 'r', 'c' ):
806 case VLC_FOURCC( 's', 'i', 'p', 'r' ):
807 case VLC_FOURCC( '2', '8', '_', '8' ):
808 DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
810 case VLC_FOURCC( 'm','p','4','a' ):
811 DemuxAudioMethod2( p_demux, tk, i_pts );
814 DemuxAudioMethod3( p_demux, tk, i_pts );
819 /*****************************************************************************
820 * Helpers: seek/control
821 *****************************************************************************/
822 static int ControlGoToIndex( demux_t *p_demux, real_index_t *p_index )
824 demux_sys_t *p_sys = p_demux->p_sys;
826 p_sys->b_seek = true;
827 p_sys->i_pcr = INT64_C(1000) * p_index->i_time_offset;
828 for( int i = 0; i < p_sys->i_track; i++ )
829 p_sys->track[i]->i_last_dts = 0;
830 return stream_Seek( p_demux->s, p_index->i_file_offset );
832 static int ControlSeekTime( demux_t *p_demux, mtime_t i_time )
834 demux_sys_t *p_sys = p_demux->p_sys;
835 real_index_t *p_index = p_sys->p_index;
837 while( p_index->i_file_offset != 0 )
839 if( p_index->i_time_offset * INT64_C(1000) > i_time )
841 if( p_index != p_sys->p_index )
847 if( p_index->i_file_offset == 0 )
849 return ControlGoToIndex( p_demux, p_index );
851 static int ControlSeekByte( demux_t *p_demux, int64_t i_bytes )
853 demux_sys_t *p_sys = p_demux->p_sys;
854 real_index_t *p_index = p_sys->p_index;
856 while( p_index->i_file_offset != 0 )
858 if( p_index->i_file_offset > i_bytes )
860 if( p_index != p_sys->p_index )
866 if( p_index->i_file_offset == 0 )
868 return ControlGoToIndex( p_demux, p_index );
871 /*****************************************************************************
872 * Helpers: header reading
873 *****************************************************************************/
876 * This function will read a pascal string with size stored in 2 bytes from
879 * FIXME what is the right charset ?
881 static char *StreamReadString2( stream_t *s )
885 if( stream_Read( s, p_tmp, 2 ) < 2 )
888 const int i_length = GetWBE( p_tmp );
892 char *psz_string = calloc( 1, i_length + 1 );
894 stream_Read( s, psz_string, i_length ); /* Valid even if !psz_string */
897 EnsureUTF8( psz_string );
902 * This function will read a pascal string with size stored in 1 byte from a
905 * FIXME what is the right charset ?
907 static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data )
909 const uint8_t *p_data = *pp_data;
910 int i_data = *pi_data;
912 char *psz_string = NULL;
917 int i_length = *p_data++; i_data--;
918 if( i_length > i_data )
923 psz_string = strndup( (const char*)p_data, i_length );
925 EnsureUTF8( psz_string );
938 * This function parses(skip) the .RMF identification chunk.
940 static int HeaderRMF( demux_t *p_demux )
944 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
947 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
948 GetDWBE( &p_buffer[0] ), GetDWBE( &p_buffer[4] ) );
952 * This function parses the PROP properties chunk.
954 static int HeaderPROP( demux_t *p_demux )
956 demux_sys_t *p_sys = p_demux->p_sys;
958 uint8_t p_buffer[40];
961 if( stream_Read( p_demux->s, p_buffer, 40 ) < 40 )
964 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
965 GetDWBE(&p_buffer[0]), GetDWBE(&p_buffer[4]) );
966 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
967 GetDWBE(&p_buffer[8]), GetDWBE(&p_buffer[12]) );
968 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&p_buffer[16]) );
969 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&p_buffer[20]) );
970 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&p_buffer[24]) );
971 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&p_buffer[28]) );
972 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&p_buffer[32]) );
973 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&p_buffer[36]) );
975 /* set the duration for export in control */
976 p_sys->i_our_duration = GetDWBE(&p_buffer[20]);
978 p_sys->i_index_offset = GetDWBE(&p_buffer[28]);
980 i_flags = GetWBE(&p_buffer[38]);
981 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
983 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
984 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
985 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
990 * This functions parses the CONT commentairs chunk.
992 static int HeaderCONT( demux_t *p_demux )
994 demux_sys_t *p_sys = p_demux->p_sys;
997 p_sys->psz_title = StreamReadString2( p_demux->s );
998 if( p_sys->psz_title )
999 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
1002 p_sys->psz_artist = StreamReadString2( p_demux->s );
1003 if( p_sys->psz_artist )
1004 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
1007 p_sys->psz_copyright = StreamReadString2( p_demux->s );
1008 if( p_sys->psz_copyright )
1009 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1012 p_sys->psz_description = StreamReadString2( p_demux->s );
1013 if( p_sys->psz_description )
1014 msg_Dbg( p_demux, " - comment=`%s'", p_sys->psz_description );
1019 * This function parses the MDPR (Media properties) chunk.
1021 static int HeaderMDPR( demux_t *p_demux )
1023 uint8_t p_buffer[30];
1025 if( stream_Read( p_demux->s, p_buffer, 30 ) < 30 )
1026 return VLC_EGENERIC;
1028 const int i_num = GetWBE( &p_buffer[0] );
1029 msg_Dbg( p_demux, " - id=0x%x", i_num );
1030 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
1031 GetDWBE(&p_buffer[2]), GetDWBE(&p_buffer[6]) );
1032 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
1033 GetDWBE(&p_buffer[10]), GetDWBE(&p_buffer[14]) );
1034 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&p_buffer[18]) );
1035 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&p_buffer[22]) );
1036 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&p_buffer[26]) );
1039 const uint8_t *p_peek;
1040 int i_peek_org = stream_Peek( p_demux->s, &p_peek, 2 * 256 );
1041 int i_peek = i_peek_org;
1043 return VLC_EGENERIC;
1045 char *psz_name = MemoryReadString1( &p_peek, &i_peek );
1048 msg_Dbg( p_demux, " - name=`%s'", psz_name );
1051 char *psz_mime = MemoryReadString1( &p_peek, &i_peek );
1054 msg_Dbg( p_demux, " - mime=`%s'", psz_mime );
1057 const int i_skip = i_peek_org - i_peek;
1058 if( i_skip > 0 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
1059 return VLC_EGENERIC;
1062 if( stream_Read( p_demux->s, p_buffer, 4 ) < 4 )
1063 return VLC_EGENERIC;
1065 const uint32_t i_size = GetDWBE( p_buffer );
1068 CodecParse( p_demux, i_size, i_num );
1069 if( stream_Read( p_demux->s, NULL, i_size ) < i_size )
1070 return VLC_EGENERIC;
1075 * This function parses DATA chunk (it contains the actual movie data).
1077 static int HeaderDATA( demux_t *p_demux, uint32_t i_size )
1079 demux_sys_t *p_sys = p_demux->p_sys;
1080 uint8_t p_buffer[8];
1082 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
1083 return VLC_EGENERIC;
1085 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
1086 p_sys->i_data_size = i_size;
1087 p_sys->i_data_packets_count = GetDWBE( p_buffer );
1088 p_sys->i_data_packets = 0;
1089 p_sys->i_data_offset_next = GetDWBE( &p_buffer[4] );
1091 msg_Dbg( p_demux, " - packets count=%d next=%u",
1092 p_sys->i_data_packets_count,
1093 (unsigned int)p_sys->i_data_offset_next );
1097 * This function parses the INDX (movie index chunk).
1098 * It is optional but seeking without it is ... hard.
1100 static void HeaderINDX( demux_t *p_demux )
1102 demux_sys_t *p_sys = p_demux->p_sys;
1105 uint32_t i_index_count;
1107 if( p_sys->i_index_offset == 0 )
1110 stream_Seek( p_demux->s, p_sys->i_index_offset );
1112 if( stream_Read( p_demux->s, buffer, 20 ) < 20 )
1115 const uint32_t i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
1116 const uint32_t i_size = GetDWBE( &buffer[4] );
1117 int i_version = GetWBE( &buffer[8] );
1119 msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
1120 (char*)&i_id, i_size, i_version );
1122 if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
1125 i_index_count = GetDWBE( &buffer[10] );
1127 msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
1129 if( i_index_count >= ( 0xffffffff / sizeof(*p_sys->p_index) ) )
1132 if( GetDWBE( &buffer[16] ) > 0 )
1133 msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
1134 GetDWBE( &buffer[16] ) );
1136 /* One extra entry is allocated (that MUST be set to 0) to identify the
1138 * TODO add a clean entry count (easier to build index on the fly) */
1139 p_sys->p_index = calloc( i_index_count + 1, sizeof(*p_sys->p_index) );
1140 if( !p_sys->p_index )
1143 for( unsigned int i = 0; i < i_index_count; i++ )
1145 uint8_t p_entry[14];
1147 if( stream_Read( p_demux->s, p_entry, 14 ) < 14 )
1150 if( GetWBE( &p_entry[0] ) != 0 )
1152 msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
1153 GetWBE( &p_entry[0] ) );
1157 real_index_t *p_idx = &p_sys->p_index[i];
1159 p_idx->i_time_offset = GetDWBE( &p_entry[2] );
1160 p_idx->i_file_offset = GetDWBE( &p_entry[6] );
1161 p_idx->i_frame_index = GetDWBE( &p_entry[10] );
1164 "Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32,
1165 p_idx->i_time_offset,
1166 p_idx->i_file_offset,
1167 p_idx->i_frame_index );
1173 * This function parses the complete RM headers and move the
1174 * stream pointer to the data to be read.
1176 static int HeaderRead( demux_t *p_demux )
1178 demux_sys_t *p_sys = p_demux->p_sys;
1180 p_sys->p_meta = vlc_meta_New();
1184 const int64_t i_stream_position = stream_Tell( p_demux->s );
1185 uint8_t header[100]; /* FIXME */
1187 /* Read the header */
1188 if( stream_Read( p_demux->s, header, 10 ) < 10 )
1189 return VLC_EGENERIC;
1191 const uint32_t i_id = VLC_FOURCC( header[0], header[1],
1192 header[2], header[3] );
1193 const uint32_t i_size = GetDWBE( &header[4] );
1194 const int i_version = GetWBE( &header[8] );
1196 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
1197 (char*)&i_id, i_size, i_version );
1200 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
1202 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
1203 return VLC_EGENERIC;
1209 case VLC_FOURCC('.','R','M','F'):
1210 i_ret = HeaderRMF( p_demux );
1212 case VLC_FOURCC('P','R','O','P'):
1213 i_ret = HeaderPROP( p_demux );
1215 case VLC_FOURCC('C','O','N','T'):
1216 i_ret = HeaderCONT( p_demux );
1218 case VLC_FOURCC('M','D','P','R'):
1219 i_ret = HeaderMDPR( p_demux );
1221 case VLC_FOURCC('D','A','T','A'):
1222 i_ret = HeaderDATA( p_demux, i_size );
1226 msg_Dbg( p_demux, "unknown chunk" );
1227 i_ret = VLC_SUCCESS;
1233 if( i_id == VLC_FOURCC('D','A','T','A') ) /* In this case, parsing is finished */
1236 /* Skip unread data */
1237 const int64_t i_stream_current = stream_Tell( p_demux->s );
1238 const int64_t i_stream_skip = (i_stream_position + i_size) - i_stream_current;
1240 if( i_stream_skip > 0 )
1242 if( stream_Read( p_demux->s, NULL, i_stream_skip ) != i_stream_skip )
1243 return VLC_EGENERIC;
1245 else if( i_stream_skip < 0 )
1247 return VLC_EGENERIC;
1251 /* read index if possible */
1252 if( p_sys->i_index_offset > 0 )
1254 const int64_t i_position = stream_Tell( p_demux->s );
1256 HeaderINDX( p_demux );
1258 if( stream_Seek( p_demux->s, i_position ) )
1259 return VLC_EGENERIC;
1264 static void CodecMetaRead( demux_t *p_demux, const uint8_t **pp_data, int *pi_data )
1266 demux_sys_t *p_sys = p_demux->p_sys;
1269 p_sys->psz_title = MemoryReadString1( pp_data, pi_data );
1270 if( p_sys->psz_title )
1271 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
1274 p_sys->psz_artist = MemoryReadString1( pp_data, pi_data );
1275 if( p_sys->psz_artist )
1276 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
1279 p_sys->psz_copyright = MemoryReadString1( pp_data, pi_data );
1280 if( p_sys->psz_copyright )
1281 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1284 p_sys->psz_description = MemoryReadString1( pp_data, pi_data );
1285 if( p_sys->psz_description )
1286 msg_Dbg( p_demux, " - Comment=`%s'", p_sys->psz_description );
1289 static int CodecVideoParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1291 demux_sys_t *p_sys = p_demux->p_sys;
1294 return VLC_EGENERIC;
1298 es_format_Init( &fmt, VIDEO_ES,
1299 VLC_FOURCC( p_data[8], p_data[9], p_data[10], p_data[11] ) );
1300 fmt.video.i_width = GetWBE( &p_data[12] );
1301 fmt.video.i_height= GetWBE( &p_data[14] );
1302 fmt.video.i_frame_rate = (GetWBE( &p_data[22] ) << 16) | GetWBE( &p_data[24] );
1303 fmt.video.i_frame_rate_base = 1 << 16;
1306 fmt.p_extra = malloc( 8 );
1310 memcpy( fmt.p_extra, &p_data[26], 8 );
1312 //msg_Dbg( p_demux, " - video 0x%08x 0x%08x", dw0, dw1 );
1315 switch( GetDWBE( &p_data[30] ) )
1319 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
1325 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
1328 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
1331 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1334 msg_Dbg( p_demux, " - video %4.4s %dx%d - %8.8x",
1335 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height, GetDWBE( &p_data[30] ) );
1337 real_track_t *tk = malloc( sizeof( *tk ) );
1340 es_format_Clean( &fmt );
1343 tk->i_out_subpacket = 0;
1344 tk->i_subpacket = 0;
1345 tk->i_subpackets = 0;
1346 tk->p_subpackets = NULL;
1347 tk->p_subpackets_timecode = NULL;
1350 tk->i_frame_num = -1;
1351 tk->i_frame_size = 0;
1354 tk->p_es = es_out_Add( p_demux->out, &fmt );
1356 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1359 static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1361 demux_sys_t *p_sys = p_demux->p_sys;
1365 return VLC_EGENERIC;
1368 int i_coded_frame_size = 0;
1369 int i_subpacket_h = 0;
1370 int i_frame_size = 0;
1371 int i_subpacket_size = 0;
1373 int i_version = GetWBE( &p_data[4] );
1374 int i_extra_codec = 0;
1376 msg_Dbg( p_demux, " - audio version=%d", i_version );
1378 es_format_Init( &fmt, AUDIO_ES, 0 );
1380 RVoid( &p_data, &i_data, 6 ); /* 4 + version */
1381 if( i_version == 3 ) /* RMF version 3 or .ra version 3 */
1383 RVoid( &p_data, &i_data, 2 + 10 + 4 );
1386 CodecMetaRead( p_demux, &p_data, &i_data );
1388 RVoid( &p_data, &i_data, 1 + 1 );
1390 memcpy( &fmt.i_codec, p_data, 4 );
1391 RVoid( &p_data, &i_data, 4 );
1393 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1394 fmt.audio.i_rate = 8000;
1396 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1397 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1399 else /* RMF version 4/5 or .ra version 4 */
1401 RVoid( &p_data, &i_data, 2 + 4 + 4 + 2 + 4 );
1402 i_flavor = R16( &p_data, &i_data );
1403 i_coded_frame_size = R32( &p_data, &i_data );
1404 RVoid( &p_data, &i_data, 4 + 4 + 4 );
1405 i_subpacket_h = R16( &p_data, &i_data );
1406 i_frame_size = R16( &p_data, &i_data );
1407 i_subpacket_size = R16( &p_data, &i_data );
1408 if( (!i_subpacket_size && !p_sys->b_real_audio ) ||
1409 !i_frame_size || !i_coded_frame_size )
1410 return VLC_EGENERIC;
1411 RVoid( &p_data, &i_data, 2 + (i_version == 5 ? 6 : 0 ) );
1413 fmt.audio.i_rate = R16( &p_data, &i_data );
1414 RVoid( &p_data, &i_data, 2 );
1415 fmt.audio.i_bitspersample = R16( &p_data, &i_data );
1416 fmt.audio.i_channels = R16( &p_data, &i_data );
1417 fmt.audio.i_blockalign = i_frame_size;
1419 if( i_version == 5 )
1423 memcpy( p_genr, &p_data[0], 4 );
1424 memcpy( &fmt.i_codec, &p_data[4], 4 );
1426 RVoid( &p_data, &i_data, 8 );
1428 else /* version 4 */
1431 RVoid( &p_data, &i_data, 1 + *p_data );
1432 if( i_data >= 1 + 4 )
1433 memcpy( &fmt.i_codec, &p_data[1], 4 );
1435 RVoid( &p_data, &i_data, 1 + *p_data );
1438 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1439 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1441 RVoid( &p_data, &i_data, 3 );
1443 if( p_sys->b_real_audio )
1445 CodecMetaRead( p_demux, &p_data, &i_data );
1449 if( i_version == 5 )
1450 RVoid( &p_data, &i_data, 1 );
1451 i_extra_codec = R32( &p_data, &i_data );
1455 switch( fmt.i_codec )
1457 case VLC_FOURCC('l','p','c','J'):
1458 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1459 case VLC_FOURCC('1','4','_','4'):
1460 fmt.audio.i_blockalign = 0x14 ;
1463 case VLC_FOURCC('2','8','_','8'):
1464 fmt.audio.i_blockalign = i_coded_frame_size;
1467 case VLC_FOURCC( 'd','n','e','t' ):
1468 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1471 case VLC_FOURCC( 'r','a','a','c' ):
1472 case VLC_FOURCC( 'r','a','c','p' ):
1473 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1475 if( i_extra_codec > 0 )
1478 RVoid( &p_data, &i_data, 1 );
1480 if( i_extra_codec > 0 )
1482 fmt.p_extra = malloc( fmt.i_extra );
1483 if( !fmt.p_extra || fmt.i_extra > i_data )
1486 fmt.i_extra = i_extra_codec;
1487 memcpy( fmt.p_extra, p_data, fmt.i_extra );
1491 case VLC_FOURCC('s','i','p','r'):
1492 fmt.audio.i_flavor = i_flavor;
1493 case VLC_FOURCC('c','o','o','k'):
1494 case VLC_FOURCC('a','t','r','c'):
1495 if( !memcmp( p_genr, "genr", 4 ) )
1496 fmt.audio.i_blockalign = i_subpacket_size;
1498 fmt.audio.i_blockalign = i_coded_frame_size;
1500 if( i_extra_codec > 0 )
1502 fmt.p_extra = malloc( fmt.i_extra );
1503 if( !fmt.p_extra || fmt.i_extra > i_data )
1506 fmt.i_extra = i_extra_codec;
1507 memcpy( fmt.p_extra, p_data, fmt.i_extra );
1512 case VLC_FOURCC('r','a','l','f'):
1513 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1514 (char*)&fmt.i_codec );
1518 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1519 (char*)&fmt.i_codec );
1522 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1525 real_track_t *tk = malloc( sizeof( *tk ) );
1528 es_format_Clean( &fmt );
1533 tk->i_frame_size = 0;
1536 tk->i_subpacket_h = i_subpacket_h;
1537 tk->i_subpacket_size = i_subpacket_size;
1538 tk->i_coded_frame_size = i_coded_frame_size;
1539 tk->i_frame_size = i_frame_size;
1541 tk->i_out_subpacket = 0;
1542 tk->i_subpacket = 0;
1543 tk->i_subpackets = 0;
1544 tk->p_subpackets = NULL;
1545 tk->p_subpackets_timecode = NULL;
1546 if( fmt.i_codec == VLC_FOURCC('c','o','o','k') ||
1547 fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1550 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1552 calloc( tk->i_subpackets, sizeof(block_t *) );
1553 tk->p_subpackets_timecode =
1554 calloc( tk->i_subpackets , sizeof( int64_t ) );
1556 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1559 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1561 calloc( tk->i_subpackets, sizeof(block_t *) );
1562 tk->p_subpackets_timecode =
1563 calloc( tk->i_subpackets , sizeof( int64_t ) );
1566 /* Check if the calloc went correctly */
1567 if( !tk->p_subpackets && !tk->p_subpackets_timecode)
1569 free( tk->p_subpackets_timecode );
1570 free( tk->p_subpackets );
1572 msg_Err( p_demux, "Can't alloc subpacket" );
1573 return VLC_EGENERIC;
1577 tk->p_es = es_out_Add( p_demux->out, &fmt );
1579 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1585 static int CodecParse( demux_t *p_demux, int i_len, int i_num )
1587 const uint8_t *p_peek;
1589 msg_Dbg( p_demux, " - specific data len=%d", i_len );
1590 if( stream_Peek( p_demux->s, &p_peek, i_len ) < i_len )
1591 return VLC_EGENERIC;
1593 if( i_len >= 8 && !memcmp( &p_peek[4], "VIDO", 4 ) )
1595 return CodecVideoParse( p_demux, i_num, p_peek, i_len );
1597 else if( i_len >= 4 && !memcmp( &p_peek[0], ".ra\xfd", 4 ) )
1599 return CodecAudioParse( p_demux, i_num, p_peek, i_len );
1604 /*****************************************************************************
1605 * Helpers: memory buffer fct.
1606 *****************************************************************************/
1607 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size )
1609 if( i_size > *pi_data )
1615 #define RX(name, type, size, code ) \
1616 static type name( const uint8_t **pp_data, int *pi_data ) { \
1617 if( *pi_data < (size) ) \
1620 RVoid( pp_data, pi_data, size ); \
1623 RX(R8, uint8_t, 1, **pp_data )
1624 RX(R16, uint16_t, 2, GetWBE( *pp_data ) )
1625 RX(R32, uint32_t, 4, GetDWBE( *pp_data ) )
1626 static int RLength( const uint8_t **pp_data, int *pi_data )
1628 const int v0 = R16( pp_data, pi_data ) & 0x7FFF;
1631 return (v0 << 16) | R16( pp_data, pi_data );