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
34 * from standard AC3" but ok
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>
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
70 set_description( N_("Real demuxer" ) )
71 set_capability( "demux", 50 )
72 set_category( CAT_INPUT )
73 set_subcategory( SUBCAT_INPUT_DEMUX )
74 set_callbacks( Open, Close )
75 add_shortcut( "real" )
79 /*****************************************************************************
81 *****************************************************************************/
95 int i_frame_slice_count;
100 int i_coded_frame_size;
104 block_t **p_subpackets;
105 mtime_t *p_subpackets_timecode;
113 uint32_t i_file_offset;
114 uint32_t i_time_offset;
115 uint32_t i_frame_index;
120 int64_t i_data_offset;
122 uint32_t i_data_packets_count;
123 uint32_t i_data_packets;
124 int64_t i_data_offset_next;
128 int64_t i_our_duration;
133 char* psz_description;
136 real_track_t **track;
139 uint8_t buffer[65536];
143 int64_t i_index_offset;
145 real_index_t *p_index;
148 static const unsigned char i_subpacket_size_sipr[4] = { 29, 19, 37, 20 };
150 static int Demux( demux_t * );
151 static int Control( demux_t *, int i_query, va_list args );
154 static void DemuxVideo( demux_t *, real_track_t *tk, mtime_t i_dts, unsigned i_flags );
155 static void DemuxAudio( demux_t *, real_track_t *tk, mtime_t i_pts, unsigned i_flags );
157 static int ControlSeekByte( demux_t *, int64_t i_bytes );
158 static int ControlSeekTime( demux_t *, mtime_t i_time );
160 static int HeaderRead( demux_t *p_demux );
161 static int CodecParse( demux_t *p_demux, int i_len, int i_num );
163 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size );
164 static int RLength( const uint8_t **pp_data, int *pi_data );
165 static uint8_t R8( const uint8_t **pp_data, int *pi_data );
166 static uint16_t R16( const uint8_t **pp_data, int *pi_data );
167 static uint32_t R32( const uint8_t **pp_data, int *pi_data );
169 /*****************************************************************************
171 *****************************************************************************/
172 static int Open( vlc_object_t *p_this )
174 demux_t *p_demux = (demux_t*)p_this;
177 const uint8_t *p_peek;
178 bool b_real_audio = false;
180 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
184 if( !memcmp( p_peek, ".ra", 3 ) )
186 msg_Err( p_demux, ".ra files unsuported" );
189 /* Real Media Format */
190 else if( memcmp( p_peek, ".RMF", 4 ) )
195 /* Fill p_demux field */
196 p_demux->pf_demux = Demux;
197 p_demux->pf_control = Control;
199 p_demux->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
203 p_sys->i_data_offset = 0;
206 p_sys->i_pcr = VLC_TS_INVALID;
208 p_sys->b_seek = false;
209 p_sys->b_real_audio = b_real_audio;
211 /* Parse the headers */
212 /* Real Audio files */
215 CodecParse( p_demux, 32, 0 ); /* At least 32 */
216 return VLC_EGENERIC; /* We don't know how to read
217 correctly the data yet */
220 else if( HeaderRead( p_demux ) )
222 msg_Err( p_demux, "invalid header" );
230 /*****************************************************************************
232 *****************************************************************************/
233 static void Close( vlc_object_t *p_this )
235 demux_t *p_demux = (demux_t*)p_this;
236 demux_sys_t *p_sys = p_demux->p_sys;
238 for( int i = 0; i < p_sys->i_track; i++ )
240 real_track_t *tk = p_sys->track[i];
242 es_format_Clean( &tk->fmt );
245 block_Release( tk->p_frame );
247 for( int j = 0; j < tk->i_subpackets; j++ )
249 if( tk->p_subpackets[ j ] )
250 block_Release( tk->p_subpackets[ j ] );
252 if( tk->i_subpackets )
254 free( tk->p_subpackets );
255 free( tk->p_subpackets_timecode );
259 if( p_sys->i_track > 0 )
260 free( p_sys->track );
262 free( p_sys->psz_title );
263 free( p_sys->psz_artist );
264 free( p_sys->psz_copyright );
265 free( p_sys->psz_description );
266 free( p_sys->p_index );
272 /*****************************************************************************
274 *****************************************************************************/
275 static int Demux( demux_t *p_demux )
277 demux_sys_t *p_sys = p_demux->p_sys;
280 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
281 p_sys->i_data_packets_count )
283 if( stream_Read( p_demux->s, header, 18 ) < 18 )
286 if( memcmp( header, "DATA", 4 ) )
289 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
290 p_sys->i_data_size = GetDWBE( &header[4] );
291 p_sys->i_data_packets_count = GetDWBE( &header[10] );
292 p_sys->i_data_packets = 0;
293 p_sys->i_data_offset_next = GetDWBE( &header[14] );
295 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
296 p_sys->i_data_packets_count,
297 (unsigned int)p_sys->i_data_offset_next );
300 /* Read Packet Header */
301 if( stream_Read( p_demux->s, header, 12 ) < 12 )
303 //const int i_version = GetWBE( &header[0] );
304 const size_t i_size = GetWBE( &header[2] ) - 12;
305 const int i_id = GetWBE( &header[4] );
306 const int64_t i_pts = VLC_TS_0 + 1000 * GetDWBE( &header[6] );
307 const int i_flags= header[11]; /* flags 0x02 -> keyframe */
309 p_sys->i_data_packets++;
310 if( i_size > sizeof(p_sys->buffer) )
312 msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
316 p_sys->i_buffer = stream_Read( p_demux->s, p_sys->buffer, i_size );
317 if( p_sys->i_buffer < i_size )
320 real_track_t *tk = NULL;
321 for( int i = 0; i < p_sys->i_track; i++ )
323 if( p_sys->track[i]->i_id == i_id )
324 tk = p_sys->track[i];
329 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
333 if( tk->fmt.i_cat == VIDEO_ES )
335 DemuxVideo( p_demux, tk, i_pts, i_flags );
339 assert( tk->fmt.i_cat == AUDIO_ES );
340 DemuxAudio( p_demux, tk, i_pts, i_flags );
344 mtime_t i_pcr = VLC_TS_INVALID;
345 for( int i = 0; i < p_sys->i_track; i++ )
347 real_track_t *tk = p_sys->track[i];
349 if( i_pcr <= VLC_TS_INVALID || ( tk->i_last_dts > VLC_TS_INVALID && tk->i_last_dts < i_pcr ) )
350 i_pcr = tk->i_last_dts;
352 if( i_pcr > VLC_TS_INVALID && i_pcr != p_sys->i_pcr )
354 p_sys->i_pcr = i_pcr;
355 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
360 /*****************************************************************************
362 *****************************************************************************/
363 static int Control( demux_t *p_demux, int i_query, va_list args )
365 demux_sys_t *p_sys = p_demux->p_sys;
372 case DEMUX_GET_POSITION:
373 pf = (double*) va_arg( args, double* );
375 /* read stream size maybe failed in rtsp streaming,
376 so use duration to determin the position at first */
377 if( p_sys->i_our_duration > 0 )
379 if( p_sys->i_pcr > VLC_TS_INVALID )
380 *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
386 i64 = stream_Size( p_demux->s );
388 *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
394 pi64 = (int64_t*)va_arg( args, int64_t * );
396 if( p_sys->i_our_duration > 0 )
398 *pi64 = p_sys->i_pcr > VLC_TS_INVALID ? p_sys->i_pcr : 0;
402 /* same as GET_POSTION */
403 i64 = stream_Size( p_demux->s );
404 if( p_sys->i_our_duration > 0 && i64 > 0 )
406 *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
413 case DEMUX_SET_POSITION:
414 f = (double) va_arg( args, double );
415 i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
417 if( !p_sys->p_index && i64 != 0 )
420 msg_Err( p_demux,"Seek No Index Real File failed!" );
421 return VLC_EGENERIC; // no index!
425 /* it is a rtsp stream , it is specials in access/rtsp/... */
426 msg_Dbg(p_demux, "Seek in real rtsp stream!");
427 p_sys->i_pcr = VLC_TS_0 + INT64_C(1000) * ( p_sys->i_our_duration * f );
428 p_sys->b_seek = true;
429 return stream_Seek( p_demux->s, p_sys->i_pcr - VLC_TS_0 );
431 return ControlSeekByte( p_demux, i64 );
434 if( !p_sys->p_index )
437 i64 = (int64_t) va_arg( args, int64_t );
438 return ControlSeekTime( p_demux, i64 );
440 case DEMUX_GET_LENGTH:
441 pi64 = (int64_t*)va_arg( args, int64_t * );
443 if( p_sys->i_our_duration <= 0 )
449 /* our stored duration is in ms, so... */
450 *pi64 = INT64_C(1000) * p_sys->i_our_duration;
455 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
457 /* the core will crash if we provide NULL strings, so check
458 * every string first */
459 if( p_sys->psz_title )
460 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
461 if( p_sys->psz_artist )
462 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
463 if( p_sys->psz_copyright )
464 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
465 if( p_sys->psz_description )
466 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
477 /*****************************************************************************
479 *****************************************************************************/
480 static void CheckPcr( demux_t *p_demux, real_track_t *tk, mtime_t i_dts )
482 demux_sys_t *p_sys = p_demux->p_sys;
484 if( i_dts > VLC_TS_INVALID )
485 tk->i_last_dts = i_dts;
487 if( p_sys->i_pcr > VLC_TS_INVALID || i_dts <= VLC_TS_INVALID )
490 p_sys->i_pcr = i_dts;
491 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
494 static void DemuxVideo( demux_t *p_demux, real_track_t *tk, mtime_t i_dts, unsigned i_flags )
496 demux_sys_t *p_sys = p_demux->p_sys;
498 const uint8_t *p_data = p_sys->buffer;
499 int i_data = p_sys->i_buffer;
503 uint8_t i_hdr = R8( &p_data, &i_data );
504 uint8_t i_type = i_hdr >> 6;
513 R8( &p_data, &i_data );
520 else if( i_type == 3 )
522 i_len = RLength( &p_data, &i_data );
523 i_pos = RLength( &p_data, &i_data );
524 i_frame_num = R8( &p_data, &i_data );
530 assert( i_type == 0 || i_type == 2 );
531 i_seq = R8( &p_data, &i_data );
532 i_len = RLength( &p_data, &i_data );
534 i_pos = RLength( &p_data, &i_data );
535 i_frame_num = R8( &p_data, &i_data );
538 if( (i_seq & 0x7f) == 1 || tk->i_frame_num != i_frame_num )
540 tk->i_frame_slice = 0;
541 tk->i_frame_slice_count = 2 * (i_hdr & 0x3f) + 1;
542 tk->i_frame_pos = 2*4 * tk->i_frame_slice_count + 1;
543 tk->i_frame_size = i_len + 2*4 * tk->i_frame_slice_count + 1;
544 tk->i_frame_num = i_frame_num;
547 block_Release( tk->p_frame );
549 tk->p_frame = block_New( p_demux, tk->i_frame_size );
552 tk->i_frame_size = 0;
556 tk->p_frame->i_dts = i_dts;
557 tk->p_frame->i_pts = VLC_TS_INVALID;
559 tk->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
561 i_dts = VLC_TS_INVALID;
567 i_frame_data = i_len;
571 i_frame_data = i_data;
572 if( i_type == 2 && i_frame_data > i_pos )
573 i_frame_data = i_pos;
575 if( i_frame_data > i_data )
580 if( tk->i_frame_slice > tk->i_frame_slice_count || !tk->p_frame )
584 SetDWLE( &tk->p_frame->p_buffer[2*4*(tk->i_frame_slice-1) + 1 + 0], 1 );
585 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) );
587 if( tk->i_frame_pos + i_frame_data > tk->i_frame_size )
590 memcpy( &tk->p_frame->p_buffer[tk->i_frame_pos], p_data, i_frame_data );
591 RVoid( &p_data, &i_data, i_frame_data );
592 tk->i_frame_pos += i_frame_data;
594 if( i_type != 0 || tk->i_frame_pos >= tk->i_frame_size )
596 /* Fix the buffer once the real number of slice is known */
597 tk->p_frame->p_buffer[0] = tk->i_frame_slice - 1;
598 tk->p_frame->i_buffer = tk->i_frame_pos - 2*4*( tk->i_frame_slice_count - tk->i_frame_slice );
600 memmove( &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice ],
601 &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice_count],
602 tk->i_frame_pos - (2*4*tk->i_frame_slice_count + 1) );
605 CheckPcr( p_demux, tk, tk->p_frame->i_dts );
606 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
608 tk->i_frame_size = 0;
614 static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned int i_flags )
616 demux_sys_t *p_sys = p_demux->p_sys;
617 uint8_t *p_buf = p_sys->buffer;
620 if( (i_flags & 2) || p_sys->b_seek )
623 tk->i_out_subpacket = 0;
624 p_sys->b_seek = false;
627 if( tk->fmt.i_codec == VLC_CODEC_COOK ||
628 tk->fmt.i_codec == VLC_CODEC_ATRAC3 ||
629 tk->fmt.i_codec == VLC_CODEC_SIPR )
631 const int i_num = tk->i_frame_size / tk->i_subpacket_size;
632 const int y = tk->i_subpacket / ( tk->i_frame_size / tk->i_subpacket_size );
634 for( int i = 0; i < i_num; i++ )
636 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
639 if( &p_buf[tk->i_subpacket_size] > &p_sys->buffer[p_sys->i_buffer] )
642 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
644 p_block->i_pts = VLC_TS_INVALID;
646 p_buf += tk->i_subpacket_size;
648 int i_index = tk->i_subpacket_h * i +
649 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
651 if( tk->p_subpackets[i_index] != NULL )
653 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
654 block_Release( tk->p_subpackets[i_index] );
657 tk->p_subpackets[i_index] = p_block;
658 if( tk->i_subpacket == 0 )
659 tk->p_subpackets_timecode[0] = i_pts;
665 const int y = tk->i_subpacket / (tk->i_subpacket_h / 2);
666 assert( tk->fmt.i_codec == VLC_CODEC_RA_288 );
668 for( int i = 0; i < tk->i_subpacket_h / 2; i++ )
670 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
673 if( &p_buf[tk->i_coded_frame_size] > &p_sys->buffer[p_sys->i_buffer] )
676 int i_index = (i * 2 * tk->i_frame_size / tk->i_coded_frame_size) + y;
678 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
680 p_block->i_pts = i_index == 0 ? i_pts : VLC_TS_INVALID;
682 p_buf += tk->i_coded_frame_size;
684 if( tk->p_subpackets[i_index] != NULL )
686 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
687 block_Release( tk->p_subpackets[i_index] );
690 tk->p_subpackets[i_index] = p_block;
695 while( tk->i_out_subpacket != tk->i_subpackets &&
696 tk->p_subpackets[tk->i_out_subpacket] )
698 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
699 tk->p_subpackets[tk->i_out_subpacket] = NULL;
701 if( tk->p_subpackets_timecode[tk->i_out_subpacket] )
704 p_block->i_pts = tk->p_subpackets_timecode[tk->i_out_subpacket];
706 tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
708 tk->i_out_subpacket++;
710 CheckPcr( p_demux, tk, p_block->i_pts );
711 es_out_Send( p_demux->out, tk->p_es, p_block );
714 if( tk->i_subpacket == tk->i_subpackets &&
715 tk->i_out_subpacket != tk->i_subpackets )
717 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
718 "this shouldn't happen" );
721 if( tk->i_subpacket == tk->i_subpackets )
724 tk->i_out_subpacket = 0;
728 static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
730 demux_sys_t *p_sys = p_demux->p_sys;
732 if( p_sys->i_buffer < 2 )
735 unsigned i_sub = (p_sys->buffer[1] >> 4)&0x0f;
736 if( p_sys->i_buffer < 2+2*i_sub )
739 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
741 for( unsigned i = 0; i < i_sub; i++ )
743 const int i_sub_size = GetWBE( &p_sys->buffer[2+i*2] );
744 block_t *p_block = block_New( p_demux, i_sub_size );
748 if( &p_sub[i_sub_size] > &p_sys->buffer[p_sys->i_buffer] )
751 memcpy( p_block->p_buffer, p_sub, i_sub_size );
755 p_block->i_pts = i == 0 ? i_pts : VLC_TS_INVALID;
757 CheckPcr( p_demux, tk, p_block->i_pts );
758 es_out_Send( p_demux->out, tk->p_es, p_block );
761 static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
763 demux_sys_t *p_sys = p_demux->p_sys;
765 if( p_sys->i_buffer <= 0 )
768 block_t *p_block = block_New( p_demux, p_sys->i_buffer );
772 if( tk->fmt.i_codec == VLC_CODEC_A52 )
774 uint8_t *p_src = p_sys->buffer;
775 uint8_t *p_dst = p_block->p_buffer;
778 while( p_dst < &p_block->p_buffer[p_sys->i_buffer - 1])
788 memcpy( p_block->p_buffer, p_sys->buffer, p_sys->i_buffer );
791 p_block->i_pts = i_pts;
793 CheckPcr( p_demux, tk, p_block->i_pts );
794 es_out_Send( p_demux->out, tk->p_es, p_block );
797 static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned i_flags )
799 switch( tk->fmt.i_codec )
802 case VLC_CODEC_ATRAC3:
804 case VLC_CODEC_RA_288:
805 DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
808 DemuxAudioMethod2( p_demux, tk, i_pts );
811 DemuxAudioMethod3( p_demux, tk, i_pts );
816 /*****************************************************************************
817 * Helpers: seek/control
818 *****************************************************************************/
819 static int ControlGoToIndex( demux_t *p_demux, real_index_t *p_index )
821 demux_sys_t *p_sys = p_demux->p_sys;
823 p_sys->b_seek = true;
824 p_sys->i_pcr = INT64_C(1000) * p_index->i_time_offset;
825 for( int i = 0; i < p_sys->i_track; i++ )
826 p_sys->track[i]->i_last_dts = 0;
827 return stream_Seek( p_demux->s, p_index->i_file_offset );
829 static int ControlSeekTime( demux_t *p_demux, mtime_t i_time )
831 demux_sys_t *p_sys = p_demux->p_sys;
832 real_index_t *p_index = p_sys->p_index;
834 while( p_index->i_file_offset != 0 )
836 if( p_index->i_time_offset * INT64_C(1000) > i_time )
838 if( p_index != p_sys->p_index )
844 if( p_index->i_file_offset == 0 )
846 return ControlGoToIndex( p_demux, p_index );
848 static int ControlSeekByte( demux_t *p_demux, int64_t i_bytes )
850 demux_sys_t *p_sys = p_demux->p_sys;
851 real_index_t *p_index = p_sys->p_index;
853 while( p_index->i_file_offset != 0 )
855 if( p_index->i_file_offset > i_bytes )
857 if( p_index != p_sys->p_index )
863 if( p_index->i_file_offset == 0 )
865 return ControlGoToIndex( p_demux, p_index );
868 /*****************************************************************************
869 * Helpers: header reading
870 *****************************************************************************/
873 * This function will read a pascal string with size stored in 2 bytes from
876 * FIXME what is the right charset ?
878 static char *StreamReadString2( stream_t *s )
882 if( stream_Read( s, p_tmp, 2 ) < 2 )
885 const int i_length = GetWBE( p_tmp );
889 char *psz_string = calloc( 1, i_length + 1 );
891 stream_Read( s, psz_string, i_length ); /* Valid even if !psz_string */
894 EnsureUTF8( psz_string );
899 * This function will read a pascal string with size stored in 1 byte from a
902 * FIXME what is the right charset ?
904 static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data )
906 const uint8_t *p_data = *pp_data;
907 int i_data = *pi_data;
909 char *psz_string = NULL;
914 int i_length = *p_data++; i_data--;
915 if( i_length > i_data )
920 psz_string = strndup( (const char*)p_data, i_length );
922 EnsureUTF8( psz_string );
935 * This function parses(skip) the .RMF identification chunk.
937 static int HeaderRMF( demux_t *p_demux )
941 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
944 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
945 GetDWBE( &p_buffer[0] ), GetDWBE( &p_buffer[4] ) );
949 * This function parses the PROP properties chunk.
951 static int HeaderPROP( demux_t *p_demux )
953 demux_sys_t *p_sys = p_demux->p_sys;
955 uint8_t p_buffer[40];
958 if( stream_Read( p_demux->s, p_buffer, 40 ) < 40 )
961 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
962 GetDWBE(&p_buffer[0]), GetDWBE(&p_buffer[4]) );
963 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
964 GetDWBE(&p_buffer[8]), GetDWBE(&p_buffer[12]) );
965 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&p_buffer[16]) );
966 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&p_buffer[20]) );
967 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&p_buffer[24]) );
968 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&p_buffer[28]) );
969 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&p_buffer[32]) );
970 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&p_buffer[36]) );
972 /* set the duration for export in control */
973 p_sys->i_our_duration = GetDWBE(&p_buffer[20]);
975 p_sys->i_index_offset = GetDWBE(&p_buffer[28]);
977 i_flags = GetWBE(&p_buffer[38]);
978 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
980 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
981 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
982 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
987 * This functions parses the CONT commentairs chunk.
989 static int HeaderCONT( demux_t *p_demux )
991 demux_sys_t *p_sys = p_demux->p_sys;
994 p_sys->psz_title = StreamReadString2( p_demux->s );
995 if( p_sys->psz_title )
996 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
999 p_sys->psz_artist = StreamReadString2( p_demux->s );
1000 if( p_sys->psz_artist )
1001 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
1004 p_sys->psz_copyright = StreamReadString2( p_demux->s );
1005 if( p_sys->psz_copyright )
1006 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1009 p_sys->psz_description = StreamReadString2( p_demux->s );
1010 if( p_sys->psz_description )
1011 msg_Dbg( p_demux, " - comment=`%s'", p_sys->psz_description );
1016 * This function parses the MDPR (Media properties) chunk.
1018 static int HeaderMDPR( demux_t *p_demux )
1020 uint8_t p_buffer[30];
1022 if( stream_Read( p_demux->s, p_buffer, 30 ) < 30 )
1023 return VLC_EGENERIC;
1025 const int i_num = GetWBE( &p_buffer[0] );
1026 msg_Dbg( p_demux, " - id=0x%x", i_num );
1027 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
1028 GetDWBE(&p_buffer[2]), GetDWBE(&p_buffer[6]) );
1029 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
1030 GetDWBE(&p_buffer[10]), GetDWBE(&p_buffer[14]) );
1031 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&p_buffer[18]) );
1032 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&p_buffer[22]) );
1033 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&p_buffer[26]) );
1036 const uint8_t *p_peek;
1037 int i_peek_org = stream_Peek( p_demux->s, &p_peek, 2 * 256 );
1038 int i_peek = i_peek_org;
1040 return VLC_EGENERIC;
1042 char *psz_name = MemoryReadString1( &p_peek, &i_peek );
1045 msg_Dbg( p_demux, " - name=`%s'", psz_name );
1048 char *psz_mime = MemoryReadString1( &p_peek, &i_peek );
1051 msg_Dbg( p_demux, " - mime=`%s'", psz_mime );
1054 const int i_skip = i_peek_org - i_peek;
1055 if( i_skip > 0 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
1056 return VLC_EGENERIC;
1059 if( stream_Read( p_demux->s, p_buffer, 4 ) < 4 )
1060 return VLC_EGENERIC;
1062 const uint32_t i_size = GetDWBE( p_buffer );
1065 CodecParse( p_demux, i_size, i_num );
1066 unsigned size = stream_Read( p_demux->s, NULL, i_size );
1068 return VLC_EGENERIC;
1073 * This function parses DATA chunk (it contains the actual movie data).
1075 static int HeaderDATA( demux_t *p_demux, uint32_t i_size )
1077 demux_sys_t *p_sys = p_demux->p_sys;
1078 uint8_t p_buffer[8];
1080 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
1081 return VLC_EGENERIC;
1083 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
1084 p_sys->i_data_size = i_size;
1085 p_sys->i_data_packets_count = GetDWBE( p_buffer );
1086 p_sys->i_data_packets = 0;
1087 p_sys->i_data_offset_next = GetDWBE( &p_buffer[4] );
1089 msg_Dbg( p_demux, " - packets count=%d next=%u",
1090 p_sys->i_data_packets_count,
1091 (unsigned int)p_sys->i_data_offset_next );
1095 * This function parses the INDX (movie index chunk).
1096 * It is optional but seeking without it is ... hard.
1098 static void HeaderINDX( demux_t *p_demux )
1100 demux_sys_t *p_sys = p_demux->p_sys;
1103 uint32_t i_index_count;
1105 if( p_sys->i_index_offset == 0 )
1108 stream_Seek( p_demux->s, p_sys->i_index_offset );
1110 if( stream_Read( p_demux->s, buffer, 20 ) < 20 )
1113 const uint32_t i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
1114 const uint32_t i_size = GetDWBE( &buffer[4] );
1115 int i_version = GetWBE( &buffer[8] );
1117 msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
1118 (char*)&i_id, i_size, i_version );
1120 if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
1123 i_index_count = GetDWBE( &buffer[10] );
1125 msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
1127 if( i_index_count >= ( 0xffffffff / sizeof(*p_sys->p_index) ) )
1130 if( GetDWBE( &buffer[16] ) > 0 )
1131 msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
1132 GetDWBE( &buffer[16] ) );
1134 /* One extra entry is allocated (that MUST be set to 0) to identify the
1136 * TODO add a clean entry count (easier to build index on the fly) */
1137 p_sys->p_index = calloc( i_index_count + 1, sizeof(*p_sys->p_index) );
1138 if( !p_sys->p_index )
1141 for( unsigned int i = 0; i < i_index_count; i++ )
1143 uint8_t p_entry[14];
1145 if( stream_Read( p_demux->s, p_entry, 14 ) < 14 )
1148 if( GetWBE( &p_entry[0] ) != 0 )
1150 msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
1151 GetWBE( &p_entry[0] ) );
1155 real_index_t *p_idx = &p_sys->p_index[i];
1157 p_idx->i_time_offset = GetDWBE( &p_entry[2] );
1158 p_idx->i_file_offset = GetDWBE( &p_entry[6] );
1159 p_idx->i_frame_index = GetDWBE( &p_entry[10] );
1163 "Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32,
1164 p_idx->i_time_offset,
1165 p_idx->i_file_offset,
1166 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;
1182 const int64_t i_stream_position = stream_Tell( p_demux->s );
1183 uint8_t header[100]; /* FIXME */
1185 /* Read the header */
1186 if( stream_Read( p_demux->s, header, 10 ) < 10 )
1187 return VLC_EGENERIC;
1189 const uint32_t i_id = VLC_FOURCC( header[0], header[1],
1190 header[2], header[3] );
1191 const uint32_t i_size = GetDWBE( &header[4] );
1192 const int i_version = GetWBE( &header[8] );
1194 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
1195 (char*)&i_id, i_size, i_version );
1198 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
1200 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
1201 return VLC_EGENERIC;
1207 case VLC_FOURCC('.','R','M','F'):
1208 i_ret = HeaderRMF( p_demux );
1210 case VLC_FOURCC('P','R','O','P'):
1211 i_ret = HeaderPROP( p_demux );
1213 case VLC_FOURCC('C','O','N','T'):
1214 i_ret = HeaderCONT( p_demux );
1216 case VLC_FOURCC('M','D','P','R'):
1217 i_ret = HeaderMDPR( p_demux );
1219 case VLC_FOURCC('D','A','T','A'):
1220 i_ret = HeaderDATA( p_demux, i_size );
1224 msg_Dbg( p_demux, "unknown chunk" );
1225 i_ret = VLC_SUCCESS;
1231 if( i_id == VLC_FOURCC('D','A','T','A') ) /* In this case, parsing is finished */
1234 /* Skip unread data */
1235 const int64_t i_stream_current = stream_Tell( p_demux->s );
1236 const int64_t i_stream_skip = (i_stream_position + i_size) - i_stream_current;
1238 if( i_stream_skip > 0 )
1240 if( stream_Read( p_demux->s, NULL, i_stream_skip ) != i_stream_skip )
1241 return VLC_EGENERIC;
1243 else if( i_stream_skip < 0 )
1245 return VLC_EGENERIC;
1249 /* read index if possible */
1250 if( p_sys->i_index_offset > 0 )
1252 const int64_t i_position = stream_Tell( p_demux->s );
1254 HeaderINDX( p_demux );
1256 if( stream_Seek( p_demux->s, i_position ) )
1257 return VLC_EGENERIC;
1262 static void CodecMetaRead( demux_t *p_demux, const uint8_t **pp_data, int *pi_data )
1264 demux_sys_t *p_sys = p_demux->p_sys;
1267 p_sys->psz_title = MemoryReadString1( pp_data, pi_data );
1268 if( p_sys->psz_title )
1269 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
1272 p_sys->psz_artist = MemoryReadString1( pp_data, pi_data );
1273 if( p_sys->psz_artist )
1274 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
1277 p_sys->psz_copyright = MemoryReadString1( pp_data, pi_data );
1278 if( p_sys->psz_copyright )
1279 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1282 p_sys->psz_description = MemoryReadString1( pp_data, pi_data );
1283 if( p_sys->psz_description )
1284 msg_Dbg( p_demux, " - Comment=`%s'", p_sys->psz_description );
1287 static int CodecVideoParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1289 demux_sys_t *p_sys = p_demux->p_sys;
1292 return VLC_EGENERIC;
1296 es_format_Init( &fmt, VIDEO_ES,
1297 VLC_FOURCC( p_data[8], p_data[9], p_data[10], p_data[11] ) );
1298 fmt.video.i_width = GetWBE( &p_data[12] );
1299 fmt.video.i_height= GetWBE( &p_data[14] );
1300 fmt.video.i_frame_rate = (GetWBE( &p_data[22] ) << 16) | GetWBE( &p_data[24] );
1301 fmt.video.i_frame_rate_base = 1 << 16;
1304 fmt.p_extra = malloc( 8 );
1308 memcpy( fmt.p_extra, &p_data[26], 8 );
1310 //msg_Dbg( p_demux, " - video 0x%08x 0x%08x", dw0, dw1 );
1313 switch( GetDWBE( &p_data[30] ) )
1317 fmt.i_codec = VLC_CODEC_RV13;
1323 fmt.i_codec = VLC_CODEC_RV20;
1326 fmt.i_codec = VLC_CODEC_RV30;
1329 fmt.i_codec = VLC_CODEC_RV40;
1332 msg_Dbg( p_demux, " - video %4.4s %dx%d - %8.8x",
1333 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height, GetDWBE( &p_data[30] ) );
1335 real_track_t *tk = malloc( sizeof( *tk ) );
1338 es_format_Clean( &fmt );
1341 tk->i_out_subpacket = 0;
1342 tk->i_subpacket = 0;
1343 tk->i_subpackets = 0;
1344 tk->p_subpackets = NULL;
1345 tk->p_subpackets_timecode = NULL;
1348 tk->i_frame_num = -1;
1349 tk->i_frame_size = 0;
1352 tk->p_es = es_out_Add( p_demux->out, &fmt );
1354 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1357 static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1359 demux_sys_t *p_sys = p_demux->p_sys;
1363 return VLC_EGENERIC;
1366 int i_coded_frame_size = 0;
1367 int i_subpacket_h = 0;
1368 int i_frame_size = 0;
1369 int i_subpacket_size = 0;
1371 int i_version = GetWBE( &p_data[4] );
1372 int i_extra_codec = 0;
1374 msg_Dbg( p_demux, " - audio version=%d", i_version );
1376 es_format_Init( &fmt, AUDIO_ES, 0 );
1378 RVoid( &p_data, &i_data, 6 ); /* 4 + version */
1379 if( i_version == 3 ) /* RMF version 3 or .ra version 3 */
1381 RVoid( &p_data, &i_data, 2 + 10 + 4 );
1384 CodecMetaRead( p_demux, &p_data, &i_data );
1386 RVoid( &p_data, &i_data, 1 + 1 );
1388 memcpy( &fmt.i_codec, p_data, 4 );
1389 RVoid( &p_data, &i_data, 4 );
1391 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1392 fmt.audio.i_rate = 8000;
1394 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1395 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1397 else /* RMF version 4/5 or .ra version 4 */
1399 RVoid( &p_data, &i_data, 2 + 4 + 4 + 2 + 4 );
1400 i_flavor = R16( &p_data, &i_data );
1401 i_coded_frame_size = R32( &p_data, &i_data );
1402 RVoid( &p_data, &i_data, 4 + 4 + 4 );
1403 i_subpacket_h = R16( &p_data, &i_data );
1404 i_frame_size = R16( &p_data, &i_data );
1405 i_subpacket_size = R16( &p_data, &i_data );
1406 if( !i_frame_size || !i_coded_frame_size )
1407 return VLC_EGENERIC;
1409 RVoid( &p_data, &i_data, 2 + (i_version == 5 ? 6 : 0 ) );
1411 fmt.audio.i_rate = R16( &p_data, &i_data );
1412 RVoid( &p_data, &i_data, 2 );
1413 fmt.audio.i_bitspersample = R16( &p_data, &i_data );
1414 fmt.audio.i_channels = R16( &p_data, &i_data );
1415 fmt.audio.i_blockalign = i_frame_size;
1417 if( i_version == 5 )
1421 memcpy( p_genr, &p_data[0], 4 );
1422 memcpy( &fmt.i_codec, &p_data[4], 4 );
1424 RVoid( &p_data, &i_data, 8 );
1426 else /* version 4 */
1429 RVoid( &p_data, &i_data, 1 + *p_data );
1430 if( i_data >= 1 + 4 )
1431 memcpy( &fmt.i_codec, &p_data[1], 4 );
1433 RVoid( &p_data, &i_data, 1 + *p_data );
1436 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1437 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1439 RVoid( &p_data, &i_data, 3 );
1441 if( p_sys->b_real_audio )
1443 CodecMetaRead( p_demux, &p_data, &i_data );
1447 if( i_version == 5 )
1448 RVoid( &p_data, &i_data, 1 );
1449 i_extra_codec = R32( &p_data, &i_data );
1453 switch( fmt.i_codec )
1455 case VLC_FOURCC('l','p','c','J'):
1456 case VLC_FOURCC('1','4','_','4'):
1457 fmt.i_codec = VLC_CODEC_RA_144;
1458 fmt.audio.i_blockalign = 0x14 ;
1461 case VLC_FOURCC('2','8','_','8'):
1462 fmt.i_codec = VLC_CODEC_RA_288;
1463 fmt.audio.i_blockalign = i_coded_frame_size;
1466 case VLC_FOURCC( 'a','5','2',' ' ):
1467 case VLC_FOURCC( 'd','n','e','t' ):
1468 fmt.i_codec = VLC_CODEC_A52;
1471 case VLC_FOURCC( 'r','a','a','c' ):
1472 case VLC_FOURCC( 'r','a','c','p' ):
1473 fmt.i_codec = VLC_CODEC_MP4A;
1475 if( i_extra_codec > 0 )
1478 RVoid( &p_data, &i_data, 1 );
1480 if( i_extra_codec > 0 )
1482 fmt.p_extra = malloc( i_extra_codec );
1483 if( !fmt.p_extra || i_extra_codec > 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.i_codec = VLC_CODEC_SIPR;
1494 return VLC_EGENERIC;
1496 i_subpacket_size = i_subpacket_size_sipr[i_flavor];
1497 msg_Dbg( p_demux, " - sipr flavor=%i", i_flavor );
1499 case VLC_FOURCC( 'c','o','o','k' ):
1500 case VLC_FOURCC( 'a','t','r','c' ):
1501 if( i_subpacket_size <= 0 || i_frame_size / i_subpacket_size <= 0 )
1503 es_format_Clean( &fmt );
1504 return VLC_EGENERIC;
1506 if( !memcmp( p_genr, "genr", 4 ) )
1507 fmt.audio.i_blockalign = i_subpacket_size;
1509 fmt.audio.i_blockalign = i_coded_frame_size;
1511 if( fmt.i_codec == VLC_FOURCC( 'c','o','o','k' ) )
1512 fmt.i_codec = VLC_CODEC_COOK;
1513 else if( fmt.i_codec == VLC_FOURCC( 'a','t','r','c' ) )
1514 fmt.i_codec = VLC_CODEC_ATRAC3;
1516 if( i_extra_codec > 0 )
1518 fmt.p_extra = malloc( i_extra_codec );
1519 if( !fmt.p_extra || i_extra_codec > i_data )
1522 fmt.i_extra = i_extra_codec;
1523 memcpy( fmt.p_extra, p_data, fmt.i_extra );
1528 case VLC_FOURCC('r','a','l','f'):
1529 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1530 (char*)&fmt.i_codec );
1534 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1535 (char*)&fmt.i_codec );
1538 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1541 real_track_t *tk = malloc( sizeof( *tk ) );
1544 es_format_Clean( &fmt );
1549 tk->i_frame_size = 0;
1552 tk->i_subpacket_h = i_subpacket_h;
1553 tk->i_subpacket_size = i_subpacket_size;
1554 tk->i_coded_frame_size = i_coded_frame_size;
1555 tk->i_frame_size = i_frame_size;
1557 tk->i_out_subpacket = 0;
1558 tk->i_subpacket = 0;
1559 tk->i_subpackets = 0;
1560 tk->p_subpackets = NULL;
1561 tk->p_subpackets_timecode = NULL;
1562 if( fmt.i_codec == VLC_CODEC_COOK ||
1563 fmt.i_codec == VLC_CODEC_ATRAC3 ||
1564 fmt.i_codec == VLC_CODEC_SIPR )
1567 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1569 calloc( tk->i_subpackets, sizeof(block_t *) );
1570 tk->p_subpackets_timecode =
1571 calloc( tk->i_subpackets , sizeof( int64_t ) );
1573 else if( fmt.i_codec == VLC_CODEC_RA_288 )
1576 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1578 calloc( tk->i_subpackets, sizeof(block_t *) );
1579 tk->p_subpackets_timecode =
1580 calloc( tk->i_subpackets , sizeof( int64_t ) );
1583 /* Check if the calloc went correctly */
1584 if( tk->i_subpacket > 0 && ( !tk->p_subpackets || !tk->p_subpackets_timecode ) )
1586 free( tk->p_subpackets_timecode );
1587 free( tk->p_subpackets );
1589 msg_Err( p_demux, "Can't alloc subpacket" );
1590 return VLC_EGENERIC;
1594 tk->p_es = es_out_Add( p_demux->out, &fmt );
1596 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1602 static int CodecParse( demux_t *p_demux, int i_len, int i_num )
1604 const uint8_t *p_peek;
1606 msg_Dbg( p_demux, " - specific data len=%d", i_len );
1607 if( stream_Peek( p_demux->s, &p_peek, i_len ) < i_len )
1608 return VLC_EGENERIC;
1610 if( i_len >= 8 && !memcmp( &p_peek[4], "VIDO", 4 ) )
1612 return CodecVideoParse( p_demux, i_num, p_peek, i_len );
1614 else if( i_len >= 4 && !memcmp( &p_peek[0], ".ra\xfd", 4 ) )
1616 return CodecAudioParse( p_demux, i_num, p_peek, i_len );
1621 /*****************************************************************************
1622 * Helpers: memory buffer fct.
1623 *****************************************************************************/
1624 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size )
1626 if( i_size > *pi_data )
1632 #define RX(name, type, size, code ) \
1633 static type name( const uint8_t **pp_data, int *pi_data ) { \
1634 if( *pi_data < (size) ) \
1637 RVoid( pp_data, pi_data, size ); \
1640 RX(R8, uint8_t, 1, **pp_data )
1641 RX(R16, uint16_t, 2, GetWBE( *pp_data ) )
1642 RX(R32, uint32_t, 4, GetDWBE( *pp_data ) )
1643 static int RLength( const uint8_t **pp_data, int *pi_data )
1645 const int v0 = R16( pp_data, pi_data ) & 0x7FFF;
1648 return (v0 << 16) | R16( pp_data, pi_data );