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] = NULL;
706 if( tk->p_subpackets_timecode[tk->i_out_subpacket] )
709 p_block->i_pts = tk->p_subpackets_timecode[tk->i_out_subpacket];
711 tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
713 tk->i_out_subpacket++;
715 CheckPcr( p_demux, tk, p_block->i_pts );
716 es_out_Send( p_demux->out, tk->p_es, p_block );
719 if( tk->i_subpacket == tk->i_subpackets &&
720 tk->i_out_subpacket != tk->i_subpackets )
722 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
723 "this shouldn't happen" );
726 if( tk->i_subpacket == tk->i_subpackets )
729 tk->i_out_subpacket = 0;
732 static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
734 demux_sys_t *p_sys = p_demux->p_sys;
736 if( p_sys->i_buffer < 2 )
739 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
740 if( p_sys->i_buffer < 2+2*i_sub )
743 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
745 for( int i = 0; i < i_sub; i++ )
747 const int i_sub_size = GetWBE( &p_sys->buffer[2+i*2] );
748 block_t *p_block = block_New( p_demux, i_sub_size );
752 if( &p_sub[i_sub_size] > &p_sys->buffer[p_sys->i_buffer] )
755 memcpy( p_block->p_buffer, p_sub, i_sub_size );
759 p_block->i_pts = ( i == 0 ? i_pts : 0 );
761 CheckPcr( p_demux, tk, p_block->i_pts );
762 es_out_Send( p_demux->out, tk->p_es, p_block );
765 static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
767 demux_sys_t *p_sys = p_demux->p_sys;
769 if( p_sys->i_buffer <= 0 )
772 block_t *p_block = block_New( p_demux, p_sys->i_buffer );
776 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
778 uint8_t *p_src = p_sys->buffer;
779 uint8_t *p_dst = p_block->p_buffer;
782 while( p_dst < &p_block->p_buffer[p_sys->i_buffer - 1])
792 memcpy( p_block->p_buffer, p_sys->buffer, p_sys->i_buffer );
795 p_block->i_pts = i_pts;
797 CheckPcr( p_demux, tk, p_block->i_pts );
798 es_out_Send( p_demux->out, tk->p_es, p_block );
801 static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned i_flags )
803 switch( tk->fmt.i_codec )
805 case VLC_FOURCC( 'c', 'o', 'o', 'k' ):
806 case VLC_FOURCC( 'a', 't', 'r', 'c' ):
807 case VLC_FOURCC( 's', 'i', 'p', 'r' ):
808 case VLC_FOURCC( '2', '8', '_', '8' ):
809 DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
811 case VLC_FOURCC( 'm','p','4','a' ):
812 DemuxAudioMethod2( p_demux, tk, i_pts );
815 DemuxAudioMethod3( p_demux, tk, i_pts );
820 /*****************************************************************************
821 * Helpers: seek/control
822 *****************************************************************************/
823 static int ControlGoToIndex( demux_t *p_demux, real_index_t *p_index )
825 demux_sys_t *p_sys = p_demux->p_sys;
827 p_sys->b_seek = true;
828 p_sys->i_pcr = INT64_C(1000) * p_index->i_time_offset;
829 for( int i = 0; i < p_sys->i_track; i++ )
830 p_sys->track[i]->i_last_dts = 0;
831 return stream_Seek( p_demux->s, p_index->i_file_offset );
833 static int ControlSeekTime( demux_t *p_demux, mtime_t i_time )
835 demux_sys_t *p_sys = p_demux->p_sys;
836 real_index_t *p_index = p_sys->p_index;
838 while( p_index->i_file_offset != 0 )
840 if( p_index->i_time_offset * INT64_C(1000) > i_time )
842 if( p_index != p_sys->p_index )
848 if( p_index->i_file_offset == 0 )
850 return ControlGoToIndex( p_demux, p_index );
852 static int ControlSeekByte( demux_t *p_demux, int64_t i_bytes )
854 demux_sys_t *p_sys = p_demux->p_sys;
855 real_index_t *p_index = p_sys->p_index;
857 while( p_index->i_file_offset != 0 )
859 if( p_index->i_file_offset > i_bytes )
861 if( p_index != p_sys->p_index )
867 if( p_index->i_file_offset == 0 )
869 return ControlGoToIndex( p_demux, p_index );
872 /*****************************************************************************
873 * Helpers: header reading
874 *****************************************************************************/
877 * This function will read a pascal string with size stored in 2 bytes from
880 * FIXME what is the right charset ?
882 static char *StreamReadString2( stream_t *s )
886 if( stream_Read( s, p_tmp, 2 ) < 2 )
889 const int i_length = GetWBE( p_tmp );
893 char *psz_string = calloc( 1, i_length + 1 );
895 stream_Read( s, psz_string, i_length ); /* Valid even if !psz_string */
898 EnsureUTF8( psz_string );
903 * This function will read a pascal string with size stored in 1 byte from a
906 * FIXME what is the right charset ?
908 static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data )
910 const uint8_t *p_data = *pp_data;
911 int i_data = *pi_data;
913 char *psz_string = NULL;
918 int i_length = *p_data++; i_data--;
919 if( i_length > i_data )
924 psz_string = strndup( (const char*)p_data, i_length );
926 EnsureUTF8( psz_string );
939 * This function parses(skip) the .RMF identification chunk.
941 static int HeaderRMF( demux_t *p_demux )
945 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
948 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
949 GetDWBE( &p_buffer[0] ), GetDWBE( &p_buffer[4] ) );
953 * This function parses the PROP properties chunk.
955 static int HeaderPROP( demux_t *p_demux )
957 demux_sys_t *p_sys = p_demux->p_sys;
959 uint8_t p_buffer[40];
962 if( stream_Read( p_demux->s, p_buffer, 40 ) < 40 )
965 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
966 GetDWBE(&p_buffer[0]), GetDWBE(&p_buffer[4]) );
967 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
968 GetDWBE(&p_buffer[8]), GetDWBE(&p_buffer[12]) );
969 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&p_buffer[16]) );
970 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&p_buffer[20]) );
971 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&p_buffer[24]) );
972 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&p_buffer[28]) );
973 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&p_buffer[32]) );
974 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&p_buffer[36]) );
976 /* set the duration for export in control */
977 p_sys->i_our_duration = GetDWBE(&p_buffer[20]);
979 p_sys->i_index_offset = GetDWBE(&p_buffer[28]);
981 i_flags = GetWBE(&p_buffer[38]);
982 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
984 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
985 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
986 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
991 * This functions parses the CONT commentairs chunk.
993 static int HeaderCONT( demux_t *p_demux )
995 demux_sys_t *p_sys = p_demux->p_sys;
998 p_sys->psz_title = StreamReadString2( p_demux->s );
999 if( p_sys->psz_title )
1000 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
1003 p_sys->psz_artist = StreamReadString2( p_demux->s );
1004 if( p_sys->psz_artist )
1005 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
1008 p_sys->psz_copyright = StreamReadString2( p_demux->s );
1009 if( p_sys->psz_copyright )
1010 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1013 p_sys->psz_description = StreamReadString2( p_demux->s );
1014 if( p_sys->psz_description )
1015 msg_Dbg( p_demux, " - comment=`%s'", p_sys->psz_description );
1020 * This function parses the MDPR (Media properties) chunk.
1022 static int HeaderMDPR( demux_t *p_demux )
1024 uint8_t p_buffer[30];
1026 if( stream_Read( p_demux->s, p_buffer, 30 ) < 30 )
1027 return VLC_EGENERIC;
1029 const int i_num = GetWBE( &p_buffer[0] );
1030 msg_Dbg( p_demux, " - id=0x%x", i_num );
1031 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
1032 GetDWBE(&p_buffer[2]), GetDWBE(&p_buffer[6]) );
1033 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
1034 GetDWBE(&p_buffer[10]), GetDWBE(&p_buffer[14]) );
1035 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&p_buffer[18]) );
1036 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&p_buffer[22]) );
1037 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&p_buffer[26]) );
1040 const uint8_t *p_peek;
1041 int i_peek_org = stream_Peek( p_demux->s, &p_peek, 2 * 256 );
1042 int i_peek = i_peek_org;
1044 return VLC_EGENERIC;
1046 char *psz_name = MemoryReadString1( &p_peek, &i_peek );
1049 msg_Dbg( p_demux, " - name=`%s'", psz_name );
1052 char *psz_mime = MemoryReadString1( &p_peek, &i_peek );
1055 msg_Dbg( p_demux, " - mime=`%s'", psz_mime );
1058 const int i_skip = i_peek_org - i_peek;
1059 if( i_skip > 0 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
1060 return VLC_EGENERIC;
1063 if( stream_Read( p_demux->s, p_buffer, 4 ) < 4 )
1064 return VLC_EGENERIC;
1066 const uint32_t i_size = GetDWBE( p_buffer );
1069 CodecParse( p_demux, i_size, i_num );
1070 if( stream_Read( p_demux->s, NULL, i_size ) < i_size )
1071 return VLC_EGENERIC;
1076 * This function parses DATA chunk (it contains the actual movie data).
1078 static int HeaderDATA( demux_t *p_demux, uint32_t i_size )
1080 demux_sys_t *p_sys = p_demux->p_sys;
1081 uint8_t p_buffer[8];
1083 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
1084 return VLC_EGENERIC;
1086 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
1087 p_sys->i_data_size = i_size;
1088 p_sys->i_data_packets_count = GetDWBE( p_buffer );
1089 p_sys->i_data_packets = 0;
1090 p_sys->i_data_offset_next = GetDWBE( &p_buffer[4] );
1092 msg_Dbg( p_demux, " - packets count=%d next=%u",
1093 p_sys->i_data_packets_count,
1094 (unsigned int)p_sys->i_data_offset_next );
1098 * This function parses the INDX (movie index chunk).
1099 * It is optional but seeking without it is ... hard.
1101 static void HeaderINDX( demux_t *p_demux )
1103 demux_sys_t *p_sys = p_demux->p_sys;
1106 uint32_t i_index_count;
1108 if( p_sys->i_index_offset == 0 )
1111 stream_Seek( p_demux->s, p_sys->i_index_offset );
1113 if( stream_Read( p_demux->s, buffer, 20 ) < 20 )
1116 const uint32_t i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
1117 const uint32_t i_size = GetDWBE( &buffer[4] );
1118 int i_version = GetWBE( &buffer[8] );
1120 msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
1121 (char*)&i_id, i_size, i_version );
1123 if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
1126 i_index_count = GetDWBE( &buffer[10] );
1128 msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
1130 if( i_index_count >= ( 0xffffffff / sizeof(*p_sys->p_index) ) )
1133 if( GetDWBE( &buffer[16] ) > 0 )
1134 msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
1135 GetDWBE( &buffer[16] ) );
1137 /* One extra entry is allocated (that MUST be set to 0) to identify the
1139 * TODO add a clean entry count (easier to build index on the fly) */
1140 p_sys->p_index = calloc( i_index_count + 1, sizeof(*p_sys->p_index) );
1141 if( !p_sys->p_index )
1144 for( unsigned int i = 0; i < i_index_count; i++ )
1146 uint8_t p_entry[14];
1148 if( stream_Read( p_demux->s, p_entry, 14 ) < 14 )
1151 if( GetWBE( &p_entry[0] ) != 0 )
1153 msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
1154 GetWBE( &p_entry[0] ) );
1158 real_index_t *p_idx = &p_sys->p_index[i];
1160 p_idx->i_time_offset = GetDWBE( &p_entry[2] );
1161 p_idx->i_file_offset = GetDWBE( &p_entry[6] );
1162 p_idx->i_frame_index = GetDWBE( &p_entry[10] );
1165 "Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32,
1166 p_idx->i_time_offset,
1167 p_idx->i_file_offset,
1168 p_idx->i_frame_index );
1174 * This function parses the complete RM headers and move the
1175 * stream pointer to the data to be read.
1177 static int HeaderRead( demux_t *p_demux )
1179 demux_sys_t *p_sys = p_demux->p_sys;
1181 p_sys->p_meta = vlc_meta_New();
1185 const int64_t i_stream_position = stream_Tell( p_demux->s );
1186 uint8_t header[100]; /* FIXME */
1188 /* Read the header */
1189 if( stream_Read( p_demux->s, header, 10 ) < 10 )
1190 return VLC_EGENERIC;
1192 const uint32_t i_id = VLC_FOURCC( header[0], header[1],
1193 header[2], header[3] );
1194 const uint32_t i_size = GetDWBE( &header[4] );
1195 const int i_version = GetWBE( &header[8] );
1197 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
1198 (char*)&i_id, i_size, i_version );
1201 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
1203 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
1204 return VLC_EGENERIC;
1210 case VLC_FOURCC('.','R','M','F'):
1211 i_ret = HeaderRMF( p_demux );
1213 case VLC_FOURCC('P','R','O','P'):
1214 i_ret = HeaderPROP( p_demux );
1216 case VLC_FOURCC('C','O','N','T'):
1217 i_ret = HeaderCONT( p_demux );
1219 case VLC_FOURCC('M','D','P','R'):
1220 i_ret = HeaderMDPR( p_demux );
1222 case VLC_FOURCC('D','A','T','A'):
1223 i_ret = HeaderDATA( p_demux, i_size );
1227 msg_Dbg( p_demux, "unknown chunk" );
1228 i_ret = VLC_SUCCESS;
1234 if( i_id == VLC_FOURCC('D','A','T','A') ) /* In this case, parsing is finished */
1237 /* Skip unread data */
1238 const int64_t i_stream_current = stream_Tell( p_demux->s );
1239 const int64_t i_stream_skip = (i_stream_position + i_size) - i_stream_current;
1241 if( i_stream_skip > 0 )
1243 if( stream_Read( p_demux->s, NULL, i_stream_skip ) != i_stream_skip )
1244 return VLC_EGENERIC;
1246 else if( i_stream_skip < 0 )
1248 return VLC_EGENERIC;
1252 /* read index if possible */
1253 if( p_sys->i_index_offset > 0 )
1255 const int64_t i_position = stream_Tell( p_demux->s );
1257 HeaderINDX( p_demux );
1259 if( stream_Seek( p_demux->s, i_position ) )
1260 return VLC_EGENERIC;
1265 static void CodecMetaRead( demux_t *p_demux, const uint8_t **pp_data, int *pi_data )
1267 demux_sys_t *p_sys = p_demux->p_sys;
1270 p_sys->psz_title = MemoryReadString1( pp_data, pi_data );
1271 if( p_sys->psz_title )
1272 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
1275 p_sys->psz_artist = MemoryReadString1( pp_data, pi_data );
1276 if( p_sys->psz_artist )
1277 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
1280 p_sys->psz_copyright = MemoryReadString1( pp_data, pi_data );
1281 if( p_sys->psz_copyright )
1282 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1285 p_sys->psz_description = MemoryReadString1( pp_data, pi_data );
1286 if( p_sys->psz_description )
1287 msg_Dbg( p_demux, " - Comment=`%s'", p_sys->psz_description );
1290 static int CodecVideoParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1292 demux_sys_t *p_sys = p_demux->p_sys;
1295 return VLC_EGENERIC;
1299 es_format_Init( &fmt, VIDEO_ES,
1300 VLC_FOURCC( p_data[8], p_data[9], p_data[10], p_data[11] ) );
1301 fmt.video.i_width = GetWBE( &p_data[12] );
1302 fmt.video.i_height= GetWBE( &p_data[14] );
1303 fmt.video.i_frame_rate = (GetWBE( &p_data[22] ) << 16) | GetWBE( &p_data[24] );
1304 fmt.video.i_frame_rate_base = 1 << 16;
1307 fmt.p_extra = malloc( 8 );
1311 memcpy( fmt.p_extra, &p_data[26], 8 );
1313 //msg_Dbg( p_demux, " - video 0x%08x 0x%08x", dw0, dw1 );
1316 switch( GetDWBE( &p_data[30] ) )
1320 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
1326 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
1329 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
1332 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1335 msg_Dbg( p_demux, " - video %4.4s %dx%d - %8.8x",
1336 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height, GetDWBE( &p_data[30] ) );
1338 real_track_t *tk = malloc( sizeof( *tk ) );
1341 es_format_Clean( &fmt );
1344 tk->i_out_subpacket = 0;
1345 tk->i_subpacket = 0;
1346 tk->i_subpackets = 0;
1347 tk->p_subpackets = NULL;
1348 tk->p_subpackets_timecode = NULL;
1351 tk->i_frame_num = -1;
1352 tk->i_frame_size = 0;
1355 tk->p_es = es_out_Add( p_demux->out, &fmt );
1357 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1360 static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1362 demux_sys_t *p_sys = p_demux->p_sys;
1366 return VLC_EGENERIC;
1369 int i_coded_frame_size = 0;
1370 int i_subpacket_h = 0;
1371 int i_frame_size = 0;
1372 int i_subpacket_size = 0;
1374 int i_version = GetWBE( &p_data[4] );
1375 int i_extra_codec = 0;
1377 msg_Dbg( p_demux, " - audio version=%d", i_version );
1379 es_format_Init( &fmt, AUDIO_ES, 0 );
1381 RVoid( &p_data, &i_data, 6 ); /* 4 + version */
1382 if( i_version == 3 ) /* RMF version 3 or .ra version 3 */
1384 RVoid( &p_data, &i_data, 2 + 10 + 4 );
1387 CodecMetaRead( p_demux, &p_data, &i_data );
1389 RVoid( &p_data, &i_data, 1 + 1 );
1391 memcpy( &fmt.i_codec, p_data, 4 );
1392 RVoid( &p_data, &i_data, 4 );
1394 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1395 fmt.audio.i_rate = 8000;
1397 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1398 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1400 else /* RMF version 4/5 or .ra version 4 */
1402 RVoid( &p_data, &i_data, 2 + 4 + 4 + 2 + 4 );
1403 i_flavor = R16( &p_data, &i_data );
1404 i_coded_frame_size = R32( &p_data, &i_data );
1405 RVoid( &p_data, &i_data, 4 + 4 + 4 );
1406 i_subpacket_h = R16( &p_data, &i_data );
1407 i_frame_size = R16( &p_data, &i_data );
1408 i_subpacket_size = R16( &p_data, &i_data );
1409 if( (!i_subpacket_size && !p_sys->b_real_audio ) ||
1410 !i_frame_size || !i_coded_frame_size )
1411 return VLC_EGENERIC;
1412 RVoid( &p_data, &i_data, 2 + (i_version == 5 ? 6 : 0 ) );
1414 fmt.audio.i_rate = R16( &p_data, &i_data );
1415 RVoid( &p_data, &i_data, 2 );
1416 fmt.audio.i_bitspersample = R16( &p_data, &i_data );
1417 fmt.audio.i_channels = R16( &p_data, &i_data );
1418 fmt.audio.i_blockalign = i_frame_size;
1420 if( i_version == 5 )
1424 memcpy( p_genr, &p_data[0], 4 );
1425 memcpy( &fmt.i_codec, &p_data[4], 4 );
1427 RVoid( &p_data, &i_data, 8 );
1429 else /* version 4 */
1432 RVoid( &p_data, &i_data, 1 + *p_data );
1433 if( i_data >= 1 + 4 )
1434 memcpy( &fmt.i_codec, &p_data[1], 4 );
1436 RVoid( &p_data, &i_data, 1 + *p_data );
1439 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1440 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1442 RVoid( &p_data, &i_data, 3 );
1444 if( p_sys->b_real_audio )
1446 CodecMetaRead( p_demux, &p_data, &i_data );
1450 if( i_version == 5 )
1451 RVoid( &p_data, &i_data, 1 );
1452 i_extra_codec = R32( &p_data, &i_data );
1456 switch( fmt.i_codec )
1458 case VLC_FOURCC('l','p','c','J'):
1459 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1460 case VLC_FOURCC('1','4','_','4'):
1461 fmt.audio.i_blockalign = 0x14 ;
1464 case VLC_FOURCC('2','8','_','8'):
1465 fmt.audio.i_blockalign = i_coded_frame_size;
1468 case VLC_FOURCC( 'd','n','e','t' ):
1469 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1472 case VLC_FOURCC( 'r','a','a','c' ):
1473 case VLC_FOURCC( 'r','a','c','p' ):
1474 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1476 if( i_extra_codec > 0 )
1479 RVoid( &p_data, &i_data, 1 );
1481 if( i_extra_codec > 0 )
1483 fmt.p_extra = malloc( fmt.i_extra );
1484 if( !fmt.p_extra || fmt.i_extra > i_data )
1487 fmt.i_extra = i_extra_codec;
1488 memcpy( fmt.p_extra, p_data, fmt.i_extra );
1492 case VLC_FOURCC('s','i','p','r'):
1493 fmt.audio.i_flavor = i_flavor;
1494 case VLC_FOURCC('c','o','o','k'):
1495 case VLC_FOURCC('a','t','r','c'):
1496 if( !memcmp( p_genr, "genr", 4 ) )
1497 fmt.audio.i_blockalign = i_subpacket_size;
1499 fmt.audio.i_blockalign = i_coded_frame_size;
1501 if( i_extra_codec > 0 )
1503 fmt.p_extra = malloc( fmt.i_extra );
1504 if( !fmt.p_extra || fmt.i_extra > i_data )
1507 fmt.i_extra = i_extra_codec;
1508 memcpy( fmt.p_extra, p_data, fmt.i_extra );
1513 case VLC_FOURCC('r','a','l','f'):
1514 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1515 (char*)&fmt.i_codec );
1519 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1520 (char*)&fmt.i_codec );
1523 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1526 real_track_t *tk = malloc( sizeof( *tk ) );
1529 es_format_Clean( &fmt );
1534 tk->i_frame_size = 0;
1537 tk->i_subpacket_h = i_subpacket_h;
1538 tk->i_subpacket_size = i_subpacket_size;
1539 tk->i_coded_frame_size = i_coded_frame_size;
1540 tk->i_frame_size = i_frame_size;
1542 tk->i_out_subpacket = 0;
1543 tk->i_subpacket = 0;
1544 tk->i_subpackets = 0;
1545 tk->p_subpackets = NULL;
1546 tk->p_subpackets_timecode = NULL;
1547 if( fmt.i_codec == VLC_FOURCC('c','o','o','k') ||
1548 fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1551 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1553 calloc( tk->i_subpackets, sizeof(block_t *) );
1554 tk->p_subpackets_timecode =
1555 calloc( tk->i_subpackets , sizeof( int64_t ) );
1557 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1560 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1562 calloc( tk->i_subpackets, sizeof(block_t *) );
1563 tk->p_subpackets_timecode =
1564 calloc( tk->i_subpackets , sizeof( int64_t ) );
1567 /* Check if the calloc went correctly */
1568 if( !tk->p_subpackets && !tk->p_subpackets_timecode)
1570 free( tk->p_subpackets_timecode );
1571 free( tk->p_subpackets );
1573 msg_Err( p_demux, "Can't alloc subpacket" );
1574 return VLC_EGENERIC;
1578 tk->p_es = es_out_Add( p_demux->out, &fmt );
1580 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1586 static int CodecParse( demux_t *p_demux, int i_len, int i_num )
1588 const uint8_t *p_peek;
1590 msg_Dbg( p_demux, " - specific data len=%d", i_len );
1591 if( stream_Peek( p_demux->s, &p_peek, i_len ) < i_len )
1592 return VLC_EGENERIC;
1594 if( i_len >= 8 && !memcmp( &p_peek[4], "VIDO", 4 ) )
1596 return CodecVideoParse( p_demux, i_num, p_peek, i_len );
1598 else if( i_len >= 4 && !memcmp( &p_peek[0], ".ra\xfd", 4 ) )
1600 return CodecAudioParse( p_demux, i_num, p_peek, i_len );
1605 /*****************************************************************************
1606 * Helpers: memory buffer fct.
1607 *****************************************************************************/
1608 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size )
1610 if( i_size > *pi_data )
1616 #define RX(name, type, size, code ) \
1617 static type name( const uint8_t **pp_data, int *pi_data ) { \
1618 if( *pi_data < (size) ) \
1621 RVoid( pp_data, pi_data, size ); \
1624 RX(R8, uint8_t, 1, **pp_data )
1625 RX(R16, uint16_t, 2, GetWBE( *pp_data ) )
1626 RX(R32, uint32_t, 4, GetDWBE( *pp_data ) )
1627 static int RLength( const uint8_t **pp_data, int *pi_data )
1629 const int v0 = R16( pp_data, pi_data ) & 0x7FFF;
1632 return (v0 << 16) | R16( pp_data, pi_data );