1 /*****************************************************************************
2 * real.c: Real demuxer.
3 *****************************************************************************
4 * Copyright (C) 2004, 2006-2007 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 * Status of this demuxer:
29 * version v3 w/ 14_4/lpcJ is ok.
30 * version v4/5: - atrac3 is ok.
32 * - raac, racp are ok.
33 * - dnet is twisted "The byte order of the data is reversed
35 * - 28_8 seem problematic.
36 * - sipr should be fine, but our decoder suxx :)
37 * - ralf is unsupported, but hardly any sample exist.
38 * - mp3 is unsupported, one sample exists...
42 * v3 and v4/5 headers are parsed.
46 /*****************************************************************************
48 *****************************************************************************/
54 #include <vlc_common.h>
55 #include <vlc_plugin.h>
57 #include <vlc_demux.h>
58 #include <vlc_charset.h>
61 /*****************************************************************************
63 *****************************************************************************/
64 static int Open ( vlc_object_t * );
65 static void Close ( vlc_object_t * );
68 set_description( N_("Real demuxer" ) )
69 set_capability( "demux", 0 )
70 set_category( CAT_INPUT )
71 set_subcategory( SUBCAT_INPUT_DEMUX )
72 set_callbacks( Open, Close )
73 add_shortcut( "real" )
77 /*****************************************************************************
79 *****************************************************************************/
93 int i_coded_frame_size;
98 block_t **p_subpackets;
99 int64_t *p_subpackets_timecode;
106 uint32_t file_offset;
107 uint32_t time_offset;
108 uint32_t frame_index;
113 int64_t i_data_offset;
115 uint32_t i_data_packets_count;
116 uint32_t i_data_packets;
117 int64_t i_data_offset_next;
119 bool b_is_real_audio;
127 char* psz_description;
130 real_track_t **track;
132 uint8_t buffer[65536];
137 int64_t i_index_offset;
142 static int Demux( demux_t *p_demux );
143 static int Control( demux_t *p_demux, int i_query, va_list args );
145 static int HeaderRead( demux_t *p_demux );
146 static const uint8_t * MetaRead( demux_t *p_demux, const uint8_t *p_peek );
147 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
149 /*****************************************************************************
151 *****************************************************************************/
152 static int Open( vlc_object_t *p_this )
154 demux_t *p_demux = (demux_t*)p_this;
157 const uint8_t *p_peek;
158 bool b_is_real_audio = false;
160 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
163 if( !memcmp( p_peek, ".ra", 3 ) )
165 msg_Err( p_demux, ".ra files unsuported" );
166 b_is_real_audio = true;
168 /* Real Media Format */
169 else if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
171 /* Fill p_demux field */
172 p_demux->pf_demux = Demux;
173 p_demux->pf_control = Control;
175 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
179 memset( p_sys, 0, sizeof( demux_sys_t ) );
181 p_sys->i_data_offset = 0;
187 p_sys->b_is_real_audio = b_is_real_audio;
189 /* Parse the headers */
190 /* Real Audio files */
191 if( b_is_real_audio )
193 ReadCodecSpecificData( p_demux, 32, 0 ); /* At least 32 */
194 return VLC_EGENERIC; /* We don't know how to read
195 correctly the data yet */
198 else if( HeaderRead( p_demux ) )
201 msg_Err( p_demux, "invalid header" );
202 for( i = 0; i < p_sys->i_track; i++ )
204 real_track_t *tk = p_sys->track[i];
208 es_out_Del( p_demux->out, tk->p_es );
212 if( p_sys->i_track > 0 )
214 free( p_sys->track );
223 /*****************************************************************************
225 *****************************************************************************/
226 static void Close( vlc_object_t *p_this )
228 demux_t *p_demux = (demux_t*)p_this;
229 demux_sys_t *p_sys = p_demux->p_sys;
232 for( i = 0; i < p_sys->i_track; i++ )
234 real_track_t *tk = p_sys->track[i];
235 int j = tk->i_subpackets;
237 if( tk->p_frame ) block_Release( tk->p_frame );
238 es_format_Clean( &tk->fmt );
242 if( tk->p_subpackets[ j ] )
243 block_Release( tk->p_subpackets[ j ] );
245 if( tk->i_subpackets )
247 free( tk->p_subpackets );
248 free( tk->p_subpackets_timecode );
254 free( p_sys->psz_title );
255 free( p_sys->psz_artist );
256 free( p_sys->psz_copyright );
257 free( p_sys->psz_description );
258 free( p_sys->p_index );
260 if( p_sys->i_track > 0 ) free( p_sys->track );
265 /*****************************************************************************
267 *****************************************************************************/
268 static int Demux( demux_t *p_demux )
270 demux_sys_t *p_sys = p_demux->p_sys;
272 int i_id, i_flags, i;
275 real_track_t *tk = NULL;
278 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
279 p_sys->i_data_packets_count )
281 if( stream_Read( p_demux->s, header, 18 ) < 18 )
285 if( strncmp( (char *)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 (uint32_t)p_sys->i_data_offset_next );
300 if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
302 // int i_version = GetWBE( &header[0] );
303 i_size = GetWBE( &header[2] ) - 12;
304 i_id = GetWBE( &header[4] );
305 i_pts = 1000 * GetDWBE( &header[6] );
306 i_pts += 1000; /* Avoid 0 pts */
307 i_flags= header[11]; /* flags 0x02 -> keyframe */
309 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
310 p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
312 p_sys->i_data_packets++;
316 msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
320 if( i_size > sizeof(p_sys->buffer) )
322 msg_Err( p_demux, "Got a size to read bigger than our buffer. (Invalid format?)" );
326 stream_Read( p_demux->s, p_sys->buffer, i_size );
328 for( i = 0; i < p_sys->i_track; i++ )
330 if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
335 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
338 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
340 if( tk->fmt.i_cat == VIDEO_ES && b_selected )
342 uint8_t *p = p_sys->buffer;
344 while( p < &p_sys->buffer[i_size - 2] )
353 if( (h&0xc0) == 0x40 )
357 i_len = &p_sys->buffer[i_size] - p;
363 i_subseq = (*p++)&0x7f;
365 i_len = (p[0] << 8)|p[1]; p += 2;
366 if( (i_len&0xc000) == 0 )
369 i_len |= (p[0] << 8)|p[1]; p += 2;
377 i_offset = (p[0] << 8)|p[1]; p += 2;
378 if( (i_offset&0xc000) == 0 )
381 i_offset |= (p[0] << 8)|p[1]; p += 2;
382 i_offset &= 0x3fffffff;
391 i_copy = i_len - i_offset;
392 if( i_copy > &p_sys->buffer[i_size] - p )
394 i_copy = &p_sys->buffer[i_size] - p;
396 else if( i_copy < 0 )
401 msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d",
402 i_len, i_offset, i_copy, i_subseq, i_seqnum );
404 if( (h&0xc0) == 0x80 )
406 /* last fragment -> fixes */
408 i_offset = i_len - i_copy;
409 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
414 ( tk->p_frame->i_dts != i_pts ||
415 tk->i_frame != i_len ) )
417 msg_Dbg( p_demux, "sending size=%zu", tk->p_frame->i_buffer );
419 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
425 if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
433 if( tk->p_frame == NULL )
435 msg_Dbg( p_demux, "new frame size=%d", i_len );
437 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
441 memset( &tk->p_frame->p_buffer[8], 0, i_len );
442 tk->p_frame->i_dts = i_pts;
443 tk->p_frame->i_pts = i_pts;
445 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
446 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */
449 if( i_offset < tk->i_frame)
451 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
453 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
454 i_ck, i_offset, i_copy );
456 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +0 ] = 1;
457 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +1 ] = i_offset;
460 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
465 if( (h&0xc0) != 0x80 )
474 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
476 if( (h&0xc0) == 0x80 )
480 i_offset = i_len - i_offset;
482 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
483 memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
486 if( p_sys->i_pcr < tk->p_frame->i_dts )
488 p_sys->i_pcr = tk->p_frame->i_dts;
489 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
490 (int64_t)p_sys->i_pcr );
492 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
500 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
501 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
505 if( (h&0xc0) != 0x00 )
510 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
514 p_frame->i_dts = i_pts;
515 p_frame->i_pts = i_pts;
517 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
518 ((uint32_t*)p_frame->p_buffer)[1] = 1;
519 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
520 memcpy( &p_frame->p_buffer[8], p, i_copy );
524 if( p_sys->i_pcr < p_frame->i_dts )
526 p_sys->i_pcr = p_frame->i_dts;
527 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
528 (int64_t)p_sys->i_pcr );
530 es_out_Send( p_demux->out, tk->p_es, p_frame );
536 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
540 memset( &tk->p_frame->p_buffer[8], 0, i_len );
541 tk->p_frame->i_dts = i_pts;
542 tk->p_frame->i_pts = i_pts;
544 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
545 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */
546 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
547 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
554 else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
556 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
557 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
558 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
559 tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
561 uint8_t *p_buf = p_sys->buffer;
562 int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
566 if( i_flags & 2 || ( p_sys->b_seek ) )
568 y = tk->i_subpacket = 0;
569 tk->i_out_subpacket = 0;
573 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
574 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
576 int num = tk->i_frame_size / tk->i_subpacket_size;
577 for( i = 0; i < num; i++ )
579 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
580 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
581 p_buf += tk->i_subpacket_size;
583 i_index = tk->i_subpacket_h * i +
584 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
586 if ( tk->p_subpackets[i_index] != NULL )
588 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
589 free( tk->p_subpackets[i_index] );
592 tk->p_subpackets[i_index] = p_block;
594 p_block->i_dts = p_block->i_pts = 0;
596 tk->p_subpackets_timecode[tk->i_subpacket - num] = i_pts;
599 if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
600 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
601 for( i = 0; i < tk->i_subpacket_h / 2; i++ )
603 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
604 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
605 p_buf += tk->i_coded_frame_size;
607 i_index = (i * 2 * tk->i_frame_size) /
608 tk->i_coded_frame_size + y;
610 p_block->i_dts = p_block->i_pts = i_pts;
611 tk->p_subpackets[i_index] = p_block;
615 while( tk->i_out_subpacket != tk->i_subpackets &&
616 tk->p_subpackets[tk->i_out_subpacket] )
620 if (tk->i_out_subpacket == 0)
622 p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
623 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
624 (int64_t)p_sys->i_pcr );
627 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
628 tk->p_subpackets[tk->i_out_subpacket] = 0;
630 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
633 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
634 tk->p_subpackets[tk->i_out_subpacket] = 0;
635 if ( tk->p_subpackets_timecode[tk->i_out_subpacket] )
637 p_block->i_dts = p_block->i_pts =
638 tk->p_subpackets_timecode[tk->i_out_subpacket];
639 tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
641 p_sys->i_pcr = p_block->i_dts;
642 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
643 (int64_t)p_sys->i_pcr );
645 es_out_Send( p_demux->out, tk->p_es, p_block );
647 tk->i_out_subpacket++;
650 if( tk->i_subpacket == tk->i_subpackets &&
651 tk->i_out_subpacket != tk->i_subpackets )
653 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
654 "this shouldn't happen" );
657 if( tk->i_subpacket == tk->i_subpackets )
660 tk->i_out_subpacket = 0;
666 if( p_sys->i_pcr < i_pts )
668 p_sys->i_pcr = i_pts;
669 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
670 (int64_t)p_sys->i_pcr );
673 if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
675 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
676 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
679 for( i = 0; i < i_sub; i++ )
681 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
682 block_t *p_block = block_New( p_demux, i_sub_size );
685 memcpy( p_block->p_buffer, p_sub, i_sub_size );
689 p_block->i_pts = ( i == 0 ? i_pts : 0 );
691 es_out_Send( p_demux->out, tk->p_es, p_block );
697 block_t *p_block = block_New( p_demux, i_size );
699 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
701 uint8_t *src = p_sys->buffer;
702 uint8_t *dst = p_block->p_buffer;
705 while( dst < &p_block->p_buffer[i_size- 1])
715 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
717 p_block->i_dts = p_block->i_pts = i_pts;
718 es_out_Send( p_demux->out, tk->p_es, p_block );
727 /*****************************************************************************
729 *****************************************************************************/
730 static int Control( demux_t *p_demux, int i_query, va_list args )
732 demux_sys_t *p_sys = p_demux->p_sys;
735 rm_index_t * p_index;
740 case DEMUX_GET_POSITION:
741 pf = (double*) va_arg( args, double* );
743 if( p_sys->i_our_duration > 0 )
745 *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
749 /* read stream size maybe failed in rtsp streaming,
750 so use duration to determin the position at first */
751 i64 = stream_Size( p_demux->s );
754 *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
763 pi64 = (int64_t*)va_arg( args, int64_t * );
765 if( p_sys->i_our_duration > 0 )
767 *pi64 = p_sys->i_pcr;
771 /* same as GET_POSTION */
772 i64 = stream_Size( p_demux->s );
773 if( p_sys->i_our_duration > 0 && i64 > 0 )
775 *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
782 case DEMUX_SET_POSITION:
783 f = (double) va_arg( args, double );
784 i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
786 if ( p_sys->i_index_offset == 0 && i64 != 0 )
788 msg_Err(p_demux,"Seek No Index Real File failed!" );
789 return VLC_EGENERIC; // no index!
793 /* it is a rtsp stream , it is specials in access/rtsp/... */
794 msg_Dbg(p_demux, "Seek in real rtsp stream!");
795 p_sys->i_pcr = (int64_t)1000 * ( p_sys->i_our_duration * f );
799 return stream_Seek( p_demux->s, p_sys->i_pcr );
802 if ( p_sys->i_index_offset > 0 )
804 p_index = p_sys->p_index;
805 while( p_index->file_offset !=0 )
807 if ( p_index->file_offset > i64 )
809 msg_Dbg( p_demux, "Seek Real find! %d %d %d",
810 p_index->time_offset, p_index->file_offset,
812 if ( p_index != p_sys->p_index ) p_index --;
813 i64 = p_index->file_offset;
819 p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
821 return stream_Seek( p_demux->s, i64 );
824 i64 = (int64_t) va_arg( args, int64_t ) / 1000;
826 p_index = p_sys->p_index;
827 while( p_index->file_offset !=0 )
829 if ( p_index->time_offset > i64 )
831 if ( p_index != p_sys->p_index )
833 i64 = p_index->file_offset;
839 p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
841 return stream_Seek( p_demux->s, i64 );
843 case DEMUX_GET_LENGTH:
844 pi64 = (int64_t*)va_arg( args, int64_t * );
846 /* the commented following lines are fen's implementation, which doesn't seem to
847 * work for one reason or another -- FK */
848 /*if( p_sys->i_mux_rate > 0 )
850 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
853 if( p_sys->i_our_duration > 0 )
855 /* our stored duration is in ms, so... */
856 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
865 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
867 /* the core will crash if we provide NULL strings, so check
868 * every string first */
869 if( p_sys->psz_title )
870 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
871 if( p_sys->psz_artist )
872 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
873 if( p_sys->psz_copyright )
874 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
875 if( p_sys->psz_description )
876 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
887 /*****************************************************************************
889 *****************************************************************************/
891 static void ReadRealIndex( demux_t *p_demux )
893 demux_sys_t *p_sys = p_demux->p_sys;
900 uint32_t i_index_count;
902 if ( p_sys->i_index_offset == 0 )
905 stream_Seek( p_demux->s, p_sys->i_index_offset );
907 if ( stream_Read( p_demux->s, buffer, 20 ) < 20 )
910 i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
911 i_size = GetDWBE( &buffer[4] );
912 i_version = GetWBE( &buffer[8] );
914 msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
915 (char*)&i_id, i_size, i_version );
917 if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
920 i_index_count = GetDWBE( &buffer[10] );
922 msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
924 if( i_index_count == 0 )
927 if( GetDWBE( &buffer[16] ) > 0 )
928 msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
929 GetDWBE( &buffer[16] ) );
931 p_sys->p_index = calloc( i_index_count + 1, sizeof( rm_index_t ) );
932 if( p_sys->p_index == NULL )
935 for( i=0; i<i_index_count; i++ )
937 if( stream_Read( p_demux->s, buffer, 14 ) < 14 )
940 if( GetWBE( &buffer[0] ) != 0 )
942 msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
943 GetWBE( &buffer[0] ) );
947 p_sys->p_index[i].time_offset = GetDWBE( &buffer[2] );
948 p_sys->p_index[i].file_offset = GetDWBE( &buffer[6] );
949 p_sys->p_index[i].frame_index = GetDWBE( &buffer[10] );
950 msg_Dbg( p_demux, "Real Index: time %d file %d frame %d ",
951 p_sys->p_index[i].time_offset,
952 p_sys->p_index[i].file_offset,
953 p_sys->p_index[i].frame_index );
958 /*****************************************************************************
960 *****************************************************************************/
961 static int HeaderRead( demux_t *p_demux )
963 demux_sys_t *p_sys = p_demux->p_sys;
964 uint8_t header[100]; /* FIXME */
970 p_sys->p_meta = vlc_meta_New();
974 /* Read the header */
975 if( stream_Read( p_demux->s, header, 10 ) < 10 )
979 i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
980 i_size = GetDWBE( &header[4] );
981 i_version = GetWBE( &header[8] );
983 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
984 (char*)&i_id, i_size, i_version );
986 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
988 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
991 i_skip = i_size - 10;
993 if( i_id == VLC_FOURCC('.','R','M','F') )
995 if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
996 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
997 GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
1001 else if( i_id == VLC_FOURCC('P','R','O','P') )
1005 if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
1007 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
1008 GetDWBE(&header[0]), GetDWBE(&header[4]) );
1009 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
1010 GetDWBE(&header[8]), GetDWBE(&header[12]) );
1011 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) );
1012 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) );
1013 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) );
1014 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) );
1015 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) );
1016 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) );
1018 /* set the duration for export in control */
1019 p_sys->i_our_duration = (int)GetDWBE(&header[20]);
1021 p_sys->i_index_offset = GetDWBE(&header[28]);
1023 i_flags = GetWBE(&header[38]);
1024 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
1026 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
1027 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
1028 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
1031 else if( i_id == VLC_FOURCC('C','O','N','T') )
1036 /* FIXME FIXME: should convert from whatever the character
1037 * encoding of the input meta data is to UTF-8. */
1039 stream_Read( p_demux->s, header, 2 );
1040 if( ( i_len = GetWBE( header ) ) > 0 )
1042 psz = malloc( i_len + 1 );
1043 stream_Read( p_demux->s, psz, i_len );
1047 msg_Dbg( p_demux, " - title=`%s'", psz );
1048 p_sys->psz_title = psz;
1053 stream_Read( p_demux->s, header, 2 );
1054 if( ( i_len = GetWBE( header ) ) > 0 )
1056 psz = malloc( i_len + 1 );
1057 stream_Read( p_demux->s, psz, i_len );
1061 msg_Dbg( p_demux, " - author=`%s'", psz );
1062 p_sys->psz_artist = psz;
1067 stream_Read( p_demux->s, header, 2 );
1068 if( ( i_len = GetWBE( header ) ) > 0 )
1070 psz = malloc( i_len + 1 );
1071 stream_Read( p_demux->s, psz, i_len );
1075 msg_Dbg( p_demux, " - copyright=`%s'", psz );
1076 p_sys->psz_copyright = psz;
1081 stream_Read( p_demux->s, header, 2 );
1082 if( ( i_len = GetWBE( header ) ) > 0 )
1084 psz = malloc( i_len + 1 );
1085 stream_Read( p_demux->s, psz, i_len );
1089 msg_Dbg( p_demux, " - comment=`%s'", psz );
1090 p_sys->psz_description = psz;
1095 else if( i_id == VLC_FOURCC('M','D','P','R') )
1097 /* Media properties header */
1102 if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
1103 i_num = GetWBE( header );
1104 msg_Dbg( p_demux, " - id=0x%x", i_num );
1105 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
1106 GetDWBE(&header[2]), GetDWBE(&header[6]) );
1107 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
1108 GetDWBE(&header[10]), GetDWBE(&header[14]) );
1109 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&header[18]) );
1110 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&header[22]) );
1111 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&header[26]) );
1115 stream_Read( p_demux->s, header, 1 );
1116 if( ( i_len = header[0] ) > 0 )
1118 psz = malloc( i_len + 1 );
1119 stream_Read( p_demux->s, psz, i_len );
1122 msg_Dbg( p_demux, " - name=`%s'", psz );
1128 stream_Read( p_demux->s, header, 1 );
1129 if( ( i_len = header[0] ) > 0 )
1131 psz = malloc( i_len + 1 );
1132 stream_Read( p_demux->s, psz, i_len );
1135 msg_Dbg( p_demux, " - mime=`%s'", psz );
1141 stream_Read( p_demux->s, header, 4 );
1142 if( ( i_len = GetDWBE( header ) ) > 0 )
1144 ReadCodecSpecificData( p_demux, i_len, i_num );
1145 stream_Read( p_demux->s, NULL, i_len );
1151 else if( i_id == VLC_FOURCC('D','A','T','A') )
1153 stream_Read( p_demux->s, header, 8 );
1155 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
1156 p_sys->i_data_size = i_size;
1157 p_sys->i_data_packets_count = GetDWBE( header );
1158 p_sys->i_data_packets = 0;
1159 p_sys->i_data_offset_next = GetDWBE( &header[4] );
1161 msg_Dbg( p_demux, " - packets count=%d next=%u",
1162 p_sys->i_data_packets_count,
1163 (uint32_t)p_sys->i_data_offset_next );
1165 /* we have finished the header */
1171 msg_Dbg( p_demux, "unknown chunk" );
1174 if( i_skip < 0 ) return VLC_EGENERIC;
1175 stream_Read( p_demux->s, NULL, i_skip );
1178 /* TODO read index if possible */
1180 if ( p_sys->i_index_offset > 0 )
1183 pos = stream_Tell( p_demux->s );
1184 ReadRealIndex( p_demux );
1185 stream_Seek( p_demux->s, pos );
1191 static const uint8_t * MetaRead( demux_t *p_demux, const uint8_t *p_peek )
1193 demux_sys_t *p_sys = p_demux->p_sys;
1199 i_len = *p_peek ; p_peek++;
1202 psz = malloc( i_len + 1 );
1203 memcpy( psz, p_peek, i_len );
1207 msg_Dbg( p_demux, " - title=`%s'", psz );
1208 p_sys->psz_title = psz;
1213 i_len = *p_peek ; p_peek++;
1216 psz = malloc( i_len + 1 );
1217 memcpy( psz, p_peek, i_len );
1221 msg_Dbg( p_demux, " - artist=`%s'", psz );
1222 p_sys->psz_artist = psz;
1227 i_len = *p_peek ; p_peek++;
1230 psz = malloc( i_len + 1 );
1231 memcpy( psz, p_peek, i_len );
1235 msg_Dbg( p_demux, " - Copyright=`%s'", psz );
1236 p_sys->psz_copyright = psz;
1241 i_len = *p_peek ; p_peek++;
1244 psz = malloc( i_len + 1 );
1245 memcpy( psz, p_peek, i_len );
1249 msg_Dbg( p_demux, " - Comment=`%s'", psz );
1250 p_sys->psz_description = psz;
1258 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
1260 demux_sys_t *p_sys = p_demux->p_sys;
1263 const uint8_t *p_peek;
1265 msg_Dbg( p_demux, " - specific data len=%d", i_len );
1266 if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
1268 if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
1270 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
1271 p_peek[10], p_peek[11] ) );
1272 fmt.video.i_width = GetWBE( &p_peek[12] );
1273 fmt.video.i_height= GetWBE( &p_peek[14] );
1276 fmt.p_extra = malloc( 8 );
1277 ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
1278 ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
1280 msg_Dbg( p_demux, " - video 0x%08x 0x%08x",
1281 ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
1283 if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
1284 GetDWBE( &p_peek[30] ) == 0x10003001 )
1286 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
1288 else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
1289 GetDWBE( &p_peek[30] ) == 0x20100001 ||
1290 GetDWBE( &p_peek[30] ) == 0x20200002 )
1292 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
1294 else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
1296 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
1298 else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
1300 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1303 msg_Dbg( p_demux, " - video %4.4s %dx%d",
1304 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
1306 tk = malloc( sizeof( real_track_t ) );
1307 tk->i_out_subpacket = 0;
1308 tk->i_subpacket = 0;
1309 tk->i_subpackets = 0;
1310 tk->p_subpackets = NULL;
1311 tk->p_subpackets_timecode = NULL;
1316 tk->p_es = es_out_Add( p_demux->out, &fmt );
1318 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1320 else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
1322 int i_header_size = 0;
1324 int i_coded_frame_size = 0;
1325 int i_subpacket_h = 0;
1326 int i_frame_size = 0;
1327 int i_subpacket_size = 0;
1329 int i_version = GetWBE( &p_peek[4] ); /* [0..3] = '.','r','a',0xfd */
1330 msg_Dbg( p_demux, " - audio version=%d", i_version );
1332 p_peek += 6; /* 4 + version */
1333 es_format_Init( &fmt, AUDIO_ES, 0 );
1335 if( i_version == 3 ) /* RMF version 3 or .ra version 3 */
1337 i_header_size = GetWBE( p_peek ); p_peek += 2; /* Size from now */
1338 p_peek += 10; /* Unknown */
1340 msg_Dbg( p_demux, "Data Size: %i", GetDWBE( p_peek ) );
1341 p_peek += 4; /* Data Size */
1344 p_peek = MetaRead( p_demux, p_peek );
1346 p_peek ++; /* Unknown */
1347 p_peek ++; /* FourCC length = 4 */
1348 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4; /* FourCC*/
1350 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1351 fmt.audio.i_rate = 8000;
1353 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1354 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1356 else /* RMF version 4/5 or .ra version 4 */
1358 p_peek += 2; /* 00 00 */
1359 p_peek += 4; /* .ra4 or .ra5 */
1360 p_peek += 4; /* data size */
1361 p_peek += 2; /* version (4 or 5) */
1362 i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1363 i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1364 i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
1365 p_peek += 4; /* ?? */
1366 p_peek += 4; /* ?? */
1367 p_peek += 4; /* ?? */
1368 i_subpacket_h = GetWBE( p_peek ); p_peek += 2; /* 1 */
1369 i_frame_size = GetWBE( p_peek ); p_peek += 2; /* frame size */
1370 i_subpacket_size = GetWBE( p_peek ); p_peek += 2; /* subpacket_size */
1371 if( (!i_subpacket_size && !p_sys->b_is_real_audio )
1372 || !i_frame_size || !i_coded_frame_size )
1373 return VLC_EGENERIC;
1374 p_peek += 2; /* ?? */
1376 if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1378 fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */
1379 p_peek += 2; /* ?? */
1380 fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
1381 fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
1382 fmt.audio.i_blockalign = i_frame_size;
1384 if( i_version == 5 )
1386 memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4; /* genr */
1387 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1389 else /* version 4 */
1391 p_peek += 1 + p_peek[0]; /* Inteleaver ID string and lenth */
1392 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* FourCC */
1393 p_peek += 1 + p_peek[0];
1396 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1397 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1399 p_peek += 3; /* ?? */
1401 if( p_sys->b_is_real_audio )
1403 p_peek = MetaRead( p_demux, p_peek );
1407 if( i_version == 5 ) p_peek++;
1408 /* Extra Data then: DWord + byte[] */
1409 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1413 switch( fmt.i_codec )
1415 case VLC_FOURCC('l','p','c','J'):
1416 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1417 case VLC_FOURCC('1','4','_','4'):
1418 fmt.audio.i_blockalign = 0x14 ;
1421 case VLC_FOURCC('2','8','_','8'):
1423 fmt.audio.i_blockalign = i_coded_frame_size;
1426 case VLC_FOURCC( 'd','n','e','t' ):
1427 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1430 case VLC_FOURCC( 'r','a','a','c' ):
1431 case VLC_FOURCC( 'r','a','c','p' ):
1432 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1434 if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1435 if( fmt.i_extra > 0 )
1437 fmt.p_extra = malloc( fmt.i_extra );
1438 if( fmt.p_extra == NULL )
1440 msg_Err( p_demux, "Error in the extra data" );
1441 return VLC_EGENERIC;
1443 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1447 case VLC_FOURCC('s','i','p','r'):
1448 fmt.audio.i_flavor = i_flavor;
1449 case VLC_FOURCC('c','o','o','k'):
1450 case VLC_FOURCC('a','t','r','c'):
1451 if( !memcmp( p_genr, "genr", 4 ) )
1452 fmt.audio.i_blockalign = i_subpacket_size;
1454 fmt.audio.i_blockalign = i_coded_frame_size;
1456 if( fmt.i_extra > 0 )
1458 fmt.p_extra = malloc( fmt.i_extra );
1459 if( fmt.p_extra == NULL )
1461 msg_Err( p_demux, "Error in the extra data" );
1462 return VLC_EGENERIC;
1464 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1468 case VLC_FOURCC('r','a','l','f'):
1469 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1470 (char*)&fmt.i_codec );
1474 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1475 (char*)&fmt.i_codec );
1479 if( fmt.i_codec != 0 )
1481 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1483 tk = malloc( sizeof( real_track_t ) );
1491 tk->i_subpacket_h = i_subpacket_h;
1492 tk->i_subpacket_size = i_subpacket_size;
1493 tk->i_coded_frame_size = i_coded_frame_size;
1494 tk->i_frame_size = i_frame_size;
1496 tk->i_out_subpacket = 0;
1497 tk->i_subpacket = 0;
1498 tk->i_subpackets = 0;
1499 tk->p_subpackets = NULL;
1500 tk->p_subpackets_timecode = NULL;
1501 if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
1502 || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1505 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1507 calloc( tk->i_subpackets, sizeof(block_t *) );
1508 tk->p_subpackets_timecode =
1509 calloc( tk->i_subpackets , sizeof( int64_t ) );
1511 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1514 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1516 calloc( tk->i_subpackets, sizeof(block_t *) );
1517 tk->p_subpackets_timecode =
1518 calloc( tk->i_subpackets , sizeof( int64_t ) );
1521 /* Check if the calloc went correctly */
1522 if( !tk->p_subpackets && !tk->p_subpackets_timecode)
1524 free( tk->p_subpackets_timecode );
1525 free( tk->p_subpackets );
1527 msg_Err( p_demux, "Can't alloc subpacket" );
1528 return VLC_EGENERIC;
1531 tk->p_es = es_out_Add( p_demux->out, &fmt );
1533 TAB_APPEND( p_sys->i_track, p_sys->track, tk );