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
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];
141 int64_t i_index_offset;
143 real_index_t *p_index;
146 static int Demux( demux_t * );
147 static int Control( demux_t *, int i_query, va_list args );
150 static void DemuxVideo( demux_t *, real_track_t *tk, mtime_t i_dts, unsigned i_flags );
151 static void DemuxAudio( demux_t *, real_track_t *tk, mtime_t i_pts, unsigned i_flags );
153 static int ControlSeekByte( demux_t *, int64_t i_bytes );
154 static int ControlSeekTime( demux_t *, mtime_t i_time );
156 static int HeaderRead( demux_t *p_demux );
157 static int CodecParse( demux_t *p_demux, int i_len, int i_num );
159 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size );
160 static int RLength( const uint8_t **pp_data, int *pi_data );
161 static uint8_t R8( const uint8_t **pp_data, int *pi_data );
162 static uint16_t R16( const uint8_t **pp_data, int *pi_data );
163 static uint32_t R32( const uint8_t **pp_data, int *pi_data );
165 /*****************************************************************************
167 *****************************************************************************/
168 static int Open( vlc_object_t *p_this )
170 demux_t *p_demux = (demux_t*)p_this;
173 const uint8_t *p_peek;
174 bool b_real_audio = false;
176 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
180 if( !memcmp( p_peek, ".ra", 3 ) )
182 msg_Err( p_demux, ".ra files unsuported" );
185 /* Real Media Format */
186 else if( memcmp( p_peek, ".RMF", 4 ) )
191 /* Fill p_demux field */
192 p_demux->pf_demux = Demux;
193 p_demux->pf_control = Control;
195 p_demux->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
199 p_sys->i_data_offset = 0;
204 p_sys->b_seek = false;
205 p_sys->b_real_audio = b_real_audio;
207 /* Parse the headers */
208 /* Real Audio files */
211 CodecParse( p_demux, 32, 0 ); /* At least 32 */
212 return VLC_EGENERIC; /* We don't know how to read
213 correctly the data yet */
216 else if( HeaderRead( p_demux ) )
218 msg_Err( p_demux, "invalid header" );
226 /*****************************************************************************
228 *****************************************************************************/
229 static void Close( vlc_object_t *p_this )
231 demux_t *p_demux = (demux_t*)p_this;
232 demux_sys_t *p_sys = p_demux->p_sys;
234 for( int i = 0; i < p_sys->i_track; i++ )
236 real_track_t *tk = p_sys->track[i];
238 es_format_Clean( &tk->fmt );
241 block_Release( tk->p_frame );
243 for( int j = 0; j < tk->i_subpackets; j++ )
245 if( tk->p_subpackets[ j ] )
246 block_Release( tk->p_subpackets[ j ] );
248 if( tk->i_subpackets )
250 free( tk->p_subpackets );
251 free( tk->p_subpackets_timecode );
255 if( p_sys->i_track > 0 )
256 free( p_sys->track );
258 free( p_sys->psz_title );
259 free( p_sys->psz_artist );
260 free( p_sys->psz_copyright );
261 free( p_sys->psz_description );
262 free( p_sys->p_index );
268 /*****************************************************************************
270 *****************************************************************************/
271 static int Demux( demux_t *p_demux )
273 demux_sys_t *p_sys = p_demux->p_sys;
276 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
277 p_sys->i_data_packets_count )
279 if( stream_Read( p_demux->s, header, 18 ) < 18 )
282 if( memcmp( header, "DATA", 4 ) )
285 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
286 p_sys->i_data_size = GetDWBE( &header[4] );
287 p_sys->i_data_packets_count = GetDWBE( &header[10] );
288 p_sys->i_data_packets = 0;
289 p_sys->i_data_offset_next = GetDWBE( &header[14] );
291 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
292 p_sys->i_data_packets_count,
293 (unsigned int)p_sys->i_data_offset_next );
296 /* Read Packet Header */
297 if( stream_Read( p_demux->s, header, 12 ) < 12 )
299 //const int i_version = GetWBE( &header[0] );
300 const int i_size = GetWBE( &header[2] ) - 12;
301 const int i_id = GetWBE( &header[4] );
302 const int64_t i_pts = 1 + 1000 * GetDWBE( &header[6] );
303 const int i_flags= header[11]; /* flags 0x02 -> keyframe */
305 p_sys->i_data_packets++;
309 msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
313 assert( i_size <= sizeof(p_sys->buffer) );
315 p_sys->i_buffer = stream_Read( p_demux->s, p_sys->buffer, i_size );
316 if( p_sys->i_buffer < i_size )
319 real_track_t *tk = NULL;
320 for( int i = 0; i < p_sys->i_track; i++ )
322 if( p_sys->track[i]->i_id == i_id )
323 tk = p_sys->track[i];
328 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
332 if( tk->fmt.i_cat == VIDEO_ES )
334 DemuxVideo( p_demux, tk, i_pts, i_flags );
338 assert( tk->fmt.i_cat == AUDIO_ES );
339 DemuxAudio( p_demux, tk, i_pts, i_flags );
344 for( int i = 0; i < p_sys->i_track; i++ )
346 real_track_t *tk = p_sys->track[i];
348 if( i_pcr <= 0 || ( tk->i_last_dts > 0 && tk->i_last_dts < i_pcr ) )
349 i_pcr = tk->i_last_dts;
351 if( i_pcr > 0 && i_pcr != p_sys->i_pcr )
353 p_sys->i_pcr = i_pcr;
354 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
359 /*****************************************************************************
361 *****************************************************************************/
362 static int Control( demux_t *p_demux, int i_query, va_list args )
364 demux_sys_t *p_sys = p_demux->p_sys;
371 case DEMUX_GET_POSITION:
372 pf = (double*) va_arg( args, double* );
374 /* read stream size maybe failed in rtsp streaming,
375 so use duration to determin the position at first */
376 if( p_sys->i_our_duration > 0 )
378 *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
383 i64 = stream_Size( p_demux->s );
385 *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
389 pi64 = (int64_t*)va_arg( args, int64_t * );
391 if( p_sys->i_our_duration > 0 )
393 *pi64 = p_sys->i_pcr;
397 /* same as GET_POSTION */
398 i64 = stream_Size( p_demux->s );
399 if( p_sys->i_our_duration > 0 && i64 > 0 )
401 *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
408 case DEMUX_SET_POSITION:
409 f = (double) va_arg( args, double );
410 i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
412 if( !p_sys->p_index && i64 != 0 )
415 msg_Err( p_demux,"Seek No Index Real File failed!" );
416 return VLC_EGENERIC; // no index!
420 /* it is a rtsp stream , it is specials in access/rtsp/... */
421 msg_Dbg(p_demux, "Seek in real rtsp stream!");
422 p_sys->i_pcr = INT64_C(1000) * ( p_sys->i_our_duration * f );
423 p_sys->b_seek = true;
424 return stream_Seek( p_demux->s, p_sys->i_pcr );
426 return ControlSeekByte( p_demux, i64 );
429 if( !p_sys->p_index )
432 i64 = (int64_t) va_arg( args, int64_t );
433 return ControlSeekTime( p_demux, i64 );
435 case DEMUX_GET_LENGTH:
436 pi64 = (int64_t*)va_arg( args, int64_t * );
439 if( p_sys->i_our_duration <= 0 )
442 /* our stored duration is in ms, so... */
443 *pi64 = INT64_C(1000) * p_sys->i_our_duration;
448 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
450 /* the core will crash if we provide NULL strings, so check
451 * every string first */
452 if( p_sys->psz_title )
453 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
454 if( p_sys->psz_artist )
455 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
456 if( p_sys->psz_copyright )
457 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
458 if( p_sys->psz_description )
459 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
470 /*****************************************************************************
472 *****************************************************************************/
473 static void CheckPcr( demux_t *p_demux, real_track_t *tk, mtime_t i_dts )
475 demux_sys_t *p_sys = p_demux->p_sys;
478 tk->i_last_dts = i_dts;
480 if( p_sys->i_pcr > 0 || i_dts <= 0 )
483 p_sys->i_pcr = i_dts;
484 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
487 static void DemuxVideo( demux_t *p_demux, real_track_t *tk, mtime_t i_dts, unsigned i_flags )
489 demux_sys_t *p_sys = p_demux->p_sys;
491 const uint8_t *p_data = p_sys->buffer;
492 int i_data = p_sys->i_buffer;
496 uint8_t i_hdr = R8( &p_data, &i_data );
497 uint8_t i_type = i_hdr >> 6;
506 R8( &p_data, &i_data );
513 else if( i_type == 3 )
515 i_len = RLength( &p_data, &i_data );
516 i_pos = RLength( &p_data, &i_data );
517 i_frame_num = R8( &p_data, &i_data );
523 assert( i_type == 0 || i_type == 2 );
524 i_seq = R8( &p_data, &i_data );
525 i_len = RLength( &p_data, &i_data );
527 i_pos = RLength( &p_data, &i_data );
528 i_frame_num = R8( &p_data, &i_data );
531 if( (i_seq & 0x7f) == 1 || tk->i_frame_num != i_frame_num )
533 tk->i_frame_slice = 0;
534 tk->i_frame_slice_count = 2 * (i_hdr & 0x3f) + 1;
535 tk->i_frame_pos = 2*4 * tk->i_frame_slice_count + 1;
536 tk->i_frame_size = i_len + 2*4 * tk->i_frame_slice_count + 1;
537 tk->i_frame_num = i_frame_num;
540 block_Release( tk->p_frame );
542 tk->p_frame = block_New( p_demux, tk->i_frame_size );
545 tk->i_frame_slice = 0;
546 tk->i_frame_slice_count = 0;
550 tk->p_frame->i_dts = i_dts;
551 tk->p_frame->i_pts = 0;
553 tk->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
561 i_frame_data = i_len;
565 i_frame_data = i_data;
566 if( i_type == 2 && i_frame_data > i_pos )
567 i_frame_data = i_pos;
569 if( i_frame_data > i_data )
574 if( tk->i_frame_slice > tk->i_frame_slice_count )
578 SetDWLE( &tk->p_frame->p_buffer[2*4*(tk->i_frame_slice-1) + 1 + 0], 1 );
579 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) );
581 if( tk->i_frame_pos + i_frame_data > tk->i_frame_size )
584 memcpy( &tk->p_frame->p_buffer[tk->i_frame_pos], p_data, i_frame_data );
585 RVoid( &p_data, &i_data, i_frame_data );
586 tk->i_frame_pos += i_frame_data;
588 if( i_type != 0 || tk->i_frame_pos >= tk->i_frame_size )
590 /* Fix the buffer once the real number of slice is known */
591 tk->p_frame->p_buffer[0] = tk->i_frame_slice - 1;
592 tk->p_frame->i_buffer = tk->i_frame_pos - 2*4*( tk->i_frame_slice_count - tk->i_frame_slice );
594 memmove( &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice ],
595 &tk->p_frame->p_buffer[1+2*4*tk->i_frame_slice_count],
596 tk->i_frame_pos - (2*4*tk->i_frame_slice_count + 1) );
599 CheckPcr( p_demux, tk, tk->p_frame->i_dts );
600 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
602 tk->i_frame_size = 0;
608 static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned int i_flags )
610 demux_sys_t *p_sys = p_demux->p_sys;
611 uint8_t *p_buf = p_sys->buffer;
614 if( (i_flags & 2) || p_sys->b_seek )
617 tk->i_out_subpacket = 0;
618 p_sys->b_seek = false;
621 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
622 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ) ||
623 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
625 const int i_num = tk->i_frame_size / tk->i_subpacket_size;
626 const int y = tk->i_subpacket / ( tk->i_frame_size / tk->i_subpacket_size );
628 for( int i = 0; i < i_num; i++ )
630 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
633 if( &p_buf[tk->i_subpacket_size] > &p_sys->buffer[p_sys->i_buffer] )
636 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
640 p_buf += tk->i_subpacket_size;
642 int i_index = tk->i_subpacket_h * i +
643 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
645 if( tk->p_subpackets[i_index] != NULL )
647 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
648 block_Release( tk->p_subpackets[i_index] );
651 tk->p_subpackets[i_index] = p_block;
652 if( tk->i_subpacket == 0 )
653 tk->p_subpackets_timecode[0] = i_pts;
659 const int y = tk->i_subpacket / (tk->i_subpacket_h / 2);
660 assert( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) );
662 for( int i = 0; i < tk->i_subpacket_h / 2; i++ )
664 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
667 if( &p_buf[tk->i_coded_frame_size] > &p_sys->buffer[p_sys->i_buffer] )
670 int i_index = (i * 2 * tk->i_frame_size / tk->i_coded_frame_size) + y;
672 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
674 p_block->i_pts = i_index == 0 ? i_pts : 0;
676 p_buf += tk->i_coded_frame_size;
678 if( tk->p_subpackets[i_index] != NULL )
680 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
681 block_Release( tk->p_subpackets[i_index] );
684 tk->p_subpackets[i_index] = p_block;
689 while( tk->i_out_subpacket != tk->i_subpackets &&
690 tk->p_subpackets[tk->i_out_subpacket] )
692 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
693 tk->p_subpackets[tk->i_out_subpacket] = NULL;
695 if( tk->p_subpackets_timecode[tk->i_out_subpacket] )
698 p_block->i_pts = tk->p_subpackets_timecode[tk->i_out_subpacket];
700 tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
702 tk->i_out_subpacket++;
704 CheckPcr( p_demux, tk, p_block->i_pts );
705 es_out_Send( p_demux->out, tk->p_es, p_block );
708 if( tk->i_subpacket == tk->i_subpackets &&
709 tk->i_out_subpacket != tk->i_subpackets )
711 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
712 "this shouldn't happen" );
715 if( tk->i_subpacket == tk->i_subpackets )
718 tk->i_out_subpacket = 0;
721 static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
723 demux_sys_t *p_sys = p_demux->p_sys;
725 if( p_sys->i_buffer < 2 )
728 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
729 if( p_sys->i_buffer < 2+2*i_sub )
732 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
734 for( int i = 0; i < i_sub; i++ )
736 const int i_sub_size = GetWBE( &p_sys->buffer[2+i*2] );
737 block_t *p_block = block_New( p_demux, i_sub_size );
741 if( &p_sub[i_sub_size] > &p_sys->buffer[p_sys->i_buffer] )
744 memcpy( p_block->p_buffer, p_sub, i_sub_size );
748 p_block->i_pts = ( i == 0 ? i_pts : 0 );
750 CheckPcr( p_demux, tk, p_block->i_pts );
751 es_out_Send( p_demux->out, tk->p_es, p_block );
754 static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
756 demux_sys_t *p_sys = p_demux->p_sys;
758 if( p_sys->i_buffer <= 0 )
761 block_t *p_block = block_New( p_demux, p_sys->i_buffer );
765 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
767 uint8_t *p_src = p_sys->buffer;
768 uint8_t *p_dst = p_block->p_buffer;
771 while( p_dst < &p_block->p_buffer[p_sys->i_buffer - 1])
781 memcpy( p_block->p_buffer, p_sys->buffer, p_sys->i_buffer );
784 p_block->i_pts = i_pts;
786 CheckPcr( p_demux, tk, p_block->i_pts );
787 es_out_Send( p_demux->out, tk->p_es, p_block );
790 static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned i_flags )
792 switch( tk->fmt.i_codec )
794 case VLC_FOURCC( 'c', 'o', 'o', 'k' ):
795 case VLC_FOURCC( 'a', 't', 'r', 'c' ):
796 case VLC_FOURCC( 's', 'i', 'p', 'r' ):
797 case VLC_FOURCC( '2', '8', '_', '8' ):
798 DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
800 case VLC_FOURCC( 'm','p','4','a' ):
801 DemuxAudioMethod2( p_demux, tk, i_pts );
804 DemuxAudioMethod3( p_demux, tk, i_pts );
809 /*****************************************************************************
810 * Helpers: seek/control
811 *****************************************************************************/
812 static int ControlGoToIndex( demux_t *p_demux, real_index_t *p_index )
814 demux_sys_t *p_sys = p_demux->p_sys;
816 p_sys->b_seek = true;
817 p_sys->i_pcr = INT64_C(1000) * p_index->i_time_offset;
818 for( int i = 0; i < p_sys->i_track; i++ )
819 p_sys->track[i]->i_last_dts = 0;
820 return stream_Seek( p_demux->s, p_index->i_file_offset );
822 static int ControlSeekTime( demux_t *p_demux, mtime_t i_time )
824 demux_sys_t *p_sys = p_demux->p_sys;
825 real_index_t *p_index = p_sys->p_index;
827 while( p_index->i_file_offset != 0 )
829 if( p_index->i_time_offset * INT64_C(1000) > i_time )
831 if( p_index != p_sys->p_index )
837 if( p_index->i_file_offset == 0 )
839 return ControlGoToIndex( p_demux, p_index );
841 static int ControlSeekByte( demux_t *p_demux, int64_t i_bytes )
843 demux_sys_t *p_sys = p_demux->p_sys;
844 real_index_t *p_index = p_sys->p_index;
846 while( p_index->i_file_offset != 0 )
848 if( p_index->i_file_offset > i_bytes )
850 if( p_index != p_sys->p_index )
856 if( p_index->i_file_offset == 0 )
858 return ControlGoToIndex( p_demux, p_index );
861 /*****************************************************************************
862 * Helpers: header reading
863 *****************************************************************************/
866 * This function will read a pascal string with size stored in 2 bytes from
869 * FIXME what is the right charset ?
871 static char *StreamReadString2( stream_t *s )
875 if( stream_Read( s, p_tmp, 2 ) < 2 )
878 const int i_length = GetWBE( p_tmp );
882 char *psz_string = calloc( 1, i_length + 1 );
884 stream_Read( s, psz_string, i_length ); /* Valid even if !psz_string */
887 EnsureUTF8( psz_string );
892 * This function will read a pascal string with size stored in 1 byte from a
895 * FIXME what is the right charset ?
897 static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data )
899 const uint8_t *p_data = *pp_data;
900 int i_data = *pi_data;
902 char *psz_string = NULL;
907 int i_length = *p_data++; i_data--;
908 if( i_length > i_data )
913 psz_string = strndup( (const char*)p_data, i_length );
915 EnsureUTF8( psz_string );
928 * This function parses(skip) the .RMF identification chunk.
930 static int HeaderRMF( demux_t *p_demux )
934 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
937 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
938 GetDWBE( &p_buffer[0] ), GetDWBE( &p_buffer[4] ) );
942 * This function parses the PROP properties chunk.
944 static int HeaderPROP( demux_t *p_demux )
946 demux_sys_t *p_sys = p_demux->p_sys;
948 uint8_t p_buffer[40];
951 if( stream_Read( p_demux->s, p_buffer, 40 ) < 40 )
954 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
955 GetDWBE(&p_buffer[0]), GetDWBE(&p_buffer[4]) );
956 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
957 GetDWBE(&p_buffer[8]), GetDWBE(&p_buffer[12]) );
958 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&p_buffer[16]) );
959 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&p_buffer[20]) );
960 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&p_buffer[24]) );
961 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&p_buffer[28]) );
962 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&p_buffer[32]) );
963 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&p_buffer[36]) );
965 /* set the duration for export in control */
966 p_sys->i_our_duration = GetDWBE(&p_buffer[20]);
968 p_sys->i_index_offset = GetDWBE(&p_buffer[28]);
970 i_flags = GetWBE(&p_buffer[38]);
971 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
973 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
974 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
975 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
980 * This functions parses the CONT commentairs chunk.
982 static int HeaderCONT( demux_t *p_demux )
984 demux_sys_t *p_sys = p_demux->p_sys;
987 p_sys->psz_title = StreamReadString2( p_demux->s );
988 if( p_sys->psz_title )
989 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
992 p_sys->psz_artist = StreamReadString2( p_demux->s );
993 if( p_sys->psz_artist )
994 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
997 p_sys->psz_copyright = StreamReadString2( p_demux->s );
998 if( p_sys->psz_copyright )
999 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1002 p_sys->psz_description = StreamReadString2( p_demux->s );
1003 if( p_sys->psz_description )
1004 msg_Dbg( p_demux, " - comment=`%s'", p_sys->psz_description );
1009 * This function parses the MDPR (Media properties) chunk.
1011 static int HeaderMDPR( demux_t *p_demux )
1013 uint8_t p_buffer[30];
1015 if( stream_Read( p_demux->s, p_buffer, 30 ) < 30 )
1016 return VLC_EGENERIC;
1018 const int i_num = GetWBE( &p_buffer[0] );
1019 msg_Dbg( p_demux, " - id=0x%x", i_num );
1020 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
1021 GetDWBE(&p_buffer[2]), GetDWBE(&p_buffer[6]) );
1022 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
1023 GetDWBE(&p_buffer[10]), GetDWBE(&p_buffer[14]) );
1024 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&p_buffer[18]) );
1025 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&p_buffer[22]) );
1026 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&p_buffer[26]) );
1029 const uint8_t *p_peek;
1030 int i_peek_org = stream_Peek( p_demux->s, &p_peek, 2 * 256 );
1031 int i_peek = i_peek_org;
1033 return VLC_EGENERIC;
1035 char *psz_name = MemoryReadString1( &p_peek, &i_peek );
1038 msg_Dbg( p_demux, " - name=`%s'", psz_name );
1041 char *psz_mime = MemoryReadString1( &p_peek, &i_peek );
1044 msg_Dbg( p_demux, " - mime=`%s'", psz_mime );
1047 const int i_skip = i_peek_org - i_peek;
1048 if( i_skip > 0 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
1049 return VLC_EGENERIC;
1052 if( stream_Read( p_demux->s, p_buffer, 4 ) < 4 )
1053 return VLC_EGENERIC;
1055 const uint32_t i_size = GetDWBE( p_buffer );
1058 CodecParse( p_demux, i_size, i_num );
1059 if( stream_Read( p_demux->s, NULL, i_size ) < i_size )
1060 return VLC_EGENERIC;
1065 * This function parses DATA chunk (it contains the actual movie data).
1067 static int HeaderDATA( demux_t *p_demux, uint32_t i_size )
1069 demux_sys_t *p_sys = p_demux->p_sys;
1070 uint8_t p_buffer[8];
1072 if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
1073 return VLC_EGENERIC;
1075 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
1076 p_sys->i_data_size = i_size;
1077 p_sys->i_data_packets_count = GetDWBE( p_buffer );
1078 p_sys->i_data_packets = 0;
1079 p_sys->i_data_offset_next = GetDWBE( &p_buffer[4] );
1081 msg_Dbg( p_demux, " - packets count=%d next=%u",
1082 p_sys->i_data_packets_count,
1083 (unsigned int)p_sys->i_data_offset_next );
1087 * This function parses the INDX (movie index chunk).
1088 * It is optional but seeking without it is ... hard.
1090 static void HeaderINDX( demux_t *p_demux )
1092 demux_sys_t *p_sys = p_demux->p_sys;
1095 uint32_t i_index_count;
1097 if( p_sys->i_index_offset == 0 )
1100 stream_Seek( p_demux->s, p_sys->i_index_offset );
1102 if( stream_Read( p_demux->s, buffer, 20 ) < 20 )
1105 const uint32_t i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
1106 const uint32_t i_size = GetDWBE( &buffer[4] );
1107 int i_version = GetWBE( &buffer[8] );
1109 msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
1110 (char*)&i_id, i_size, i_version );
1112 if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
1115 i_index_count = GetDWBE( &buffer[10] );
1117 msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
1119 if( i_index_count >= ( 0xffffffff / sizeof(*p_sys->p_index) ) )
1122 if( GetDWBE( &buffer[16] ) > 0 )
1123 msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
1124 GetDWBE( &buffer[16] ) );
1126 /* One extra entry is allocated (that MUST be set to 0) to identify the
1128 * TODO add a clean entry count (easier to build index on the fly) */
1129 p_sys->p_index = calloc( i_index_count + 1, sizeof(*p_sys->p_index) );
1130 if( !p_sys->p_index )
1133 for( unsigned int i = 0; i < i_index_count; i++ )
1135 uint8_t p_entry[14];
1137 if( stream_Read( p_demux->s, p_entry, 14 ) < 14 )
1140 if( GetWBE( &p_entry[0] ) != 0 )
1142 msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
1143 GetWBE( &p_entry[0] ) );
1147 real_index_t *p_idx = &p_sys->p_index[i];
1149 p_idx->i_time_offset = GetDWBE( &p_entry[2] );
1150 p_idx->i_file_offset = GetDWBE( &p_entry[6] );
1151 p_idx->i_frame_index = GetDWBE( &p_entry[10] );
1154 "Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32,
1155 p_idx->i_time_offset,
1156 p_idx->i_file_offset,
1157 p_idx->i_frame_index );
1163 * This function parses the complete RM headers and move the
1164 * stream pointer to the data to be read.
1166 static int HeaderRead( demux_t *p_demux )
1168 demux_sys_t *p_sys = p_demux->p_sys;
1172 const int64_t i_stream_position = stream_Tell( p_demux->s );
1173 uint8_t header[100]; /* FIXME */
1175 /* Read the header */
1176 if( stream_Read( p_demux->s, header, 10 ) < 10 )
1177 return VLC_EGENERIC;
1179 const uint32_t i_id = VLC_FOURCC( header[0], header[1],
1180 header[2], header[3] );
1181 const uint32_t i_size = GetDWBE( &header[4] );
1182 const int i_version = GetWBE( &header[8] );
1184 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
1185 (char*)&i_id, i_size, i_version );
1188 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
1190 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
1191 return VLC_EGENERIC;
1197 case VLC_FOURCC('.','R','M','F'):
1198 i_ret = HeaderRMF( p_demux );
1200 case VLC_FOURCC('P','R','O','P'):
1201 i_ret = HeaderPROP( p_demux );
1203 case VLC_FOURCC('C','O','N','T'):
1204 i_ret = HeaderCONT( p_demux );
1206 case VLC_FOURCC('M','D','P','R'):
1207 i_ret = HeaderMDPR( p_demux );
1209 case VLC_FOURCC('D','A','T','A'):
1210 i_ret = HeaderDATA( p_demux, i_size );
1214 msg_Dbg( p_demux, "unknown chunk" );
1215 i_ret = VLC_SUCCESS;
1221 if( i_id == VLC_FOURCC('D','A','T','A') ) /* In this case, parsing is finished */
1224 /* Skip unread data */
1225 const int64_t i_stream_current = stream_Tell( p_demux->s );
1226 const int64_t i_stream_skip = (i_stream_position + i_size) - i_stream_current;
1228 if( i_stream_skip > 0 )
1230 if( stream_Read( p_demux->s, NULL, i_stream_skip ) != i_stream_skip )
1231 return VLC_EGENERIC;
1233 else if( i_stream_skip < 0 )
1235 return VLC_EGENERIC;
1239 /* read index if possible */
1240 if( p_sys->i_index_offset > 0 )
1242 const int64_t i_position = stream_Tell( p_demux->s );
1244 HeaderINDX( p_demux );
1246 if( stream_Seek( p_demux->s, i_position ) )
1247 return VLC_EGENERIC;
1252 static void CodecMetaRead( demux_t *p_demux, const uint8_t **pp_data, int *pi_data )
1254 demux_sys_t *p_sys = p_demux->p_sys;
1257 p_sys->psz_title = MemoryReadString1( pp_data, pi_data );
1258 if( p_sys->psz_title )
1259 msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
1262 p_sys->psz_artist = MemoryReadString1( pp_data, pi_data );
1263 if( p_sys->psz_artist )
1264 msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
1267 p_sys->psz_copyright = MemoryReadString1( pp_data, pi_data );
1268 if( p_sys->psz_copyright )
1269 msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
1272 p_sys->psz_description = MemoryReadString1( pp_data, pi_data );
1273 if( p_sys->psz_description )
1274 msg_Dbg( p_demux, " - Comment=`%s'", p_sys->psz_description );
1277 static int CodecVideoParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1279 demux_sys_t *p_sys = p_demux->p_sys;
1282 return VLC_EGENERIC;
1286 es_format_Init( &fmt, VIDEO_ES,
1287 VLC_FOURCC( p_data[8], p_data[9], p_data[10], p_data[11] ) );
1288 fmt.video.i_width = GetWBE( &p_data[12] );
1289 fmt.video.i_height= GetWBE( &p_data[14] );
1290 fmt.video.i_frame_rate = (GetWBE( &p_data[22] ) << 16) | GetWBE( &p_data[24] );
1291 fmt.video.i_frame_rate_base = 1 << 16;
1294 fmt.p_extra = malloc( 8 );
1298 memcpy( fmt.p_extra, &p_data[26], 8 );
1300 //msg_Dbg( p_demux, " - video 0x%08x 0x%08x", dw0, dw1 );
1303 switch( GetDWBE( &p_data[30] ) )
1307 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
1313 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
1316 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
1319 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1322 msg_Dbg( p_demux, " - video %4.4s %dx%d - %8.8x",
1323 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height, GetDWBE( &p_data[30] ) );
1325 real_track_t *tk = malloc( sizeof( *tk ) );
1328 es_format_Clean( &fmt );
1331 tk->i_out_subpacket = 0;
1332 tk->i_subpacket = 0;
1333 tk->i_subpackets = 0;
1334 tk->p_subpackets = NULL;
1335 tk->p_subpackets_timecode = NULL;
1338 tk->i_frame_num = -1;
1339 tk->i_frame_size = 0;
1342 tk->p_es = es_out_Add( p_demux->out, &fmt );
1344 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1347 static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data, int i_data )
1349 demux_sys_t *p_sys = p_demux->p_sys;
1353 return VLC_EGENERIC;
1356 int i_coded_frame_size = 0;
1357 int i_subpacket_h = 0;
1358 int i_frame_size = 0;
1359 int i_subpacket_size = 0;
1361 int i_version = GetWBE( &p_data[4] );
1362 int i_extra_codec = 0;
1364 msg_Dbg( p_demux, " - audio version=%d", i_version );
1366 es_format_Init( &fmt, AUDIO_ES, 0 );
1368 RVoid( &p_data, &i_data, 6 ); /* 4 + version */
1369 if( i_version == 3 ) /* RMF version 3 or .ra version 3 */
1371 RVoid( &p_data, &i_data, 2 + 10 + 4 );
1374 CodecMetaRead( p_demux, &p_data, &i_data );
1376 RVoid( &p_data, &i_data, 1 + 1 );
1378 memcpy( &fmt.i_codec, p_data, 4 );
1379 RVoid( &p_data, &i_data, 4 );
1381 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1382 fmt.audio.i_rate = 8000;
1384 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1385 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1387 else /* RMF version 4/5 or .ra version 4 */
1389 RVoid( &p_data, &i_data, 2 + 4 + 4 + 2 + 4 );
1390 i_flavor = R16( &p_data, &i_data );
1391 i_coded_frame_size = R32( &p_data, &i_data );
1392 RVoid( &p_data, &i_data, 4 + 4 + 4 );
1393 i_subpacket_h = R16( &p_data, &i_data );
1394 i_frame_size = R16( &p_data, &i_data );
1395 i_subpacket_size = R16( &p_data, &i_data );
1396 if( !i_frame_size || !i_coded_frame_size )
1397 return VLC_EGENERIC;
1399 RVoid( &p_data, &i_data, 2 + (i_version == 5 ? 6 : 0 ) );
1401 fmt.audio.i_rate = R16( &p_data, &i_data );
1402 RVoid( &p_data, &i_data, 2 );
1403 fmt.audio.i_bitspersample = R16( &p_data, &i_data );
1404 fmt.audio.i_channels = R16( &p_data, &i_data );
1405 fmt.audio.i_blockalign = i_frame_size;
1407 if( i_version == 5 )
1411 memcpy( p_genr, &p_data[0], 4 );
1412 memcpy( &fmt.i_codec, &p_data[4], 4 );
1414 RVoid( &p_data, &i_data, 8 );
1416 else /* version 4 */
1419 RVoid( &p_data, &i_data, 1 + *p_data );
1420 if( i_data >= 1 + 4 )
1421 memcpy( &fmt.i_codec, &p_data[1], 4 );
1423 RVoid( &p_data, &i_data, 1 + *p_data );
1426 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1427 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1429 RVoid( &p_data, &i_data, 3 );
1431 if( p_sys->b_real_audio )
1433 CodecMetaRead( p_demux, &p_data, &i_data );
1437 if( i_version == 5 )
1438 RVoid( &p_data, &i_data, 1 );
1439 i_extra_codec = R32( &p_data, &i_data );
1443 switch( fmt.i_codec )
1445 case VLC_FOURCC('l','p','c','J'):
1446 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1447 case VLC_FOURCC('1','4','_','4'):
1448 fmt.audio.i_blockalign = 0x14 ;
1451 case VLC_FOURCC('2','8','_','8'):
1452 fmt.audio.i_blockalign = i_coded_frame_size;
1455 case VLC_FOURCC( 'd','n','e','t' ):
1456 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1459 case VLC_FOURCC( 'r','a','a','c' ):
1460 case VLC_FOURCC( 'r','a','c','p' ):
1461 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1463 if( i_extra_codec > 0 )
1466 RVoid( &p_data, &i_data, 1 );
1468 if( i_extra_codec > 0 )
1470 fmt.p_extra = malloc( i_extra_codec );
1471 if( !fmt.p_extra || i_extra_codec > i_data )
1474 fmt.i_extra = i_extra_codec;
1475 memcpy( fmt.p_extra, p_data, fmt.i_extra );
1479 case VLC_FOURCC('s','i','p','r'):
1480 fmt.audio.i_flavor = i_flavor;
1481 case VLC_FOURCC('c','o','o','k'):
1482 case VLC_FOURCC('a','t','r','c'):
1483 if( i_subpacket_size <= 0 )
1485 es_format_Clean( &fmt );
1486 return VLC_EGENERIC;
1488 if( !memcmp( p_genr, "genr", 4 ) )
1489 fmt.audio.i_blockalign = i_subpacket_size;
1491 fmt.audio.i_blockalign = i_coded_frame_size;
1493 if( i_extra_codec > 0 )
1495 fmt.p_extra = malloc( i_extra_codec );
1496 if( !fmt.p_extra || i_extra_codec > i_data )
1499 fmt.i_extra = i_extra_codec;
1500 memcpy( fmt.p_extra, p_data, fmt.i_extra );
1505 case VLC_FOURCC('r','a','l','f'):
1506 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1507 (char*)&fmt.i_codec );
1511 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1512 (char*)&fmt.i_codec );
1515 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1518 real_track_t *tk = malloc( sizeof( *tk ) );
1521 es_format_Clean( &fmt );
1526 tk->i_frame_size = 0;
1529 tk->i_subpacket_h = i_subpacket_h;
1530 tk->i_subpacket_size = i_subpacket_size;
1531 tk->i_coded_frame_size = i_coded_frame_size;
1532 tk->i_frame_size = i_frame_size;
1534 tk->i_out_subpacket = 0;
1535 tk->i_subpacket = 0;
1536 tk->i_subpackets = 0;
1537 tk->p_subpackets = NULL;
1538 tk->p_subpackets_timecode = NULL;
1539 if( fmt.i_codec == VLC_FOURCC('c','o','o','k') ||
1540 fmt.i_codec == VLC_FOURCC('a','t','r','c') ||
1541 fmt.i_codec == VLC_FOURCC('s','i','p','r') )
1544 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1546 calloc( tk->i_subpackets, sizeof(block_t *) );
1547 tk->p_subpackets_timecode =
1548 calloc( tk->i_subpackets , sizeof( int64_t ) );
1550 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1553 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1555 calloc( tk->i_subpackets, sizeof(block_t *) );
1556 tk->p_subpackets_timecode =
1557 calloc( tk->i_subpackets , sizeof( int64_t ) );
1560 /* Check if the calloc went correctly */
1561 if( tk->i_subpacket > 0 && ( !tk->p_subpackets || !tk->p_subpackets_timecode ) )
1563 free( tk->p_subpackets_timecode );
1564 free( tk->p_subpackets );
1566 msg_Err( p_demux, "Can't alloc subpacket" );
1567 return VLC_EGENERIC;
1571 tk->p_es = es_out_Add( p_demux->out, &fmt );
1573 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1579 static int CodecParse( demux_t *p_demux, int i_len, int i_num )
1581 const uint8_t *p_peek;
1583 msg_Dbg( p_demux, " - specific data len=%d", i_len );
1584 if( stream_Peek( p_demux->s, &p_peek, i_len ) < i_len )
1585 return VLC_EGENERIC;
1587 if( i_len >= 8 && !memcmp( &p_peek[4], "VIDO", 4 ) )
1589 return CodecVideoParse( p_demux, i_num, p_peek, i_len );
1591 else if( i_len >= 4 && !memcmp( &p_peek[0], ".ra\xfd", 4 ) )
1593 return CodecAudioParse( p_demux, i_num, p_peek, i_len );
1598 /*****************************************************************************
1599 * Helpers: memory buffer fct.
1600 *****************************************************************************/
1601 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size )
1603 if( i_size > *pi_data )
1609 #define RX(name, type, size, code ) \
1610 static type name( const uint8_t **pp_data, int *pi_data ) { \
1611 if( *pi_data < (size) ) \
1614 RVoid( pp_data, pi_data, size ); \
1617 RX(R8, uint8_t, 1, **pp_data )
1618 RX(R16, uint16_t, 2, GetWBE( *pp_data ) )
1619 RX(R32, uint32_t, 4, GetDWBE( *pp_data ) )
1620 static int RLength( const uint8_t **pp_data, int *pi_data )
1622 const int v0 = R16( pp_data, pi_data ) & 0x7FFF;
1625 return (v0 << 16) | R16( pp_data, pi_data );