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...
45 /*****************************************************************************
47 *****************************************************************************/
53 #include <vlc_plugin.h>
56 #include <vlc_demux.h>
57 #include <vlc_charset.h>
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close ( vlc_object_t * );
67 set_description( N_("Real demuxer" ) );
68 set_capability( "demux", 15 );
69 set_category( CAT_INPUT );
70 set_subcategory( SUBCAT_INPUT_DEMUX );
71 set_callbacks( Open, Close );
72 add_shortcut( "real" );
76 /*****************************************************************************
78 *****************************************************************************/
92 int i_coded_frame_size;
97 block_t **p_subpackets;
98 int64_t *p_subpackets_timecode;
105 uint32_t file_offset;
106 uint32_t time_offset;
107 uint32_t frame_index;
112 int64_t i_data_offset;
114 uint32_t i_data_packets_count;
115 uint32_t i_data_packets;
116 int64_t i_data_offset_next;
124 char* psz_description;
127 real_track_t **track;
129 uint8_t buffer[65536];
134 int64_t i_index_offset;
136 rm_index_t * p_index;
139 static int Demux( demux_t *p_demux );
140 static int Control( demux_t *p_demux, int i_query, va_list args );
142 static int HeaderRead( demux_t *p_demux );
143 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
145 /*****************************************************************************
147 *****************************************************************************/
148 static int Open( vlc_object_t *p_this )
150 demux_t *p_demux = (demux_t*)p_this;
153 const uint8_t *p_peek;
155 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
156 if( !memcmp( p_peek, ".ra", 3 ) )
158 msg_Warn( p_demux, ".ra files unsuported" );
161 if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
163 /* Fill p_demux field */
164 p_demux->pf_demux = Demux;
165 p_demux->pf_control = Control;
167 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
172 memset( p_sys, 0, sizeof( demux_sys_t ) );
174 p_sys->i_data_offset = 0;
181 /* Parse the headers */
182 if( HeaderRead( p_demux ) )
185 msg_Err( p_demux, "invalid header" );
186 for( i = 0; i < p_sys->i_track; i++ )
188 real_track_t *tk = p_sys->track[i];
192 es_out_Del( p_demux->out, tk->p_es );
196 if( p_sys->i_track > 0 )
198 free( p_sys->track );
207 /*****************************************************************************
209 *****************************************************************************/
210 static void Close( vlc_object_t *p_this )
212 demux_t *p_demux = (demux_t*)p_this;
213 demux_sys_t *p_sys = p_demux->p_sys;
216 for( i = 0; i < p_sys->i_track; i++ )
218 real_track_t *tk = p_sys->track[i];
219 int j = tk->i_subpackets;
221 if( tk->p_frame ) block_Release( tk->p_frame );
222 es_format_Clean( &tk->fmt );
226 if( tk->p_subpackets[ j ] )
227 block_Release( tk->p_subpackets[ j ] );
229 if( tk->i_subpackets )
231 free( tk->p_subpackets );
232 free( tk->p_subpackets_timecode );
238 free( p_sys->psz_title );
239 free( p_sys->psz_artist );
240 free( p_sys->psz_copyright );
241 free( p_sys->psz_description );
242 free( p_sys->p_index );
244 if( p_sys->i_track > 0 ) free( p_sys->track );
249 /*****************************************************************************
251 *****************************************************************************/
252 static int Demux( demux_t *p_demux )
254 demux_sys_t *p_sys = p_demux->p_sys;
256 int i_size, i_id, i_flags, i;
258 real_track_t *tk = NULL;
261 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
262 p_sys->i_data_packets_count )
264 if( stream_Read( p_demux->s, header, 18 ) < 18 )
268 if( strncmp( (char *)header, "DATA", 4 ) )
272 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
273 p_sys->i_data_size = GetDWBE( &header[4] );
274 p_sys->i_data_packets_count = GetDWBE( &header[10] );
275 p_sys->i_data_packets = 0;
276 p_sys->i_data_offset_next = GetDWBE( &header[14] );
278 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
279 p_sys->i_data_packets_count,
280 (uint32_t)p_sys->i_data_offset_next );
283 if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
285 i_size = GetWBE( &header[2] ) - 12;
286 i_id = GetWBE( &header[4] );
287 i_pts = 1000 * GetDWBE( &header[6] );
288 i_pts += 1000; /* Avoid 0 pts */
289 i_flags= header[11]; /* flags 0x02 -> keyframe */
292 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
293 p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
296 p_sys->i_data_packets++;
298 stream_Read( p_demux->s, p_sys->buffer, i_size );
300 for( i = 0; i < p_sys->i_track; i++ )
302 if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
307 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
310 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
312 if( tk->fmt.i_cat == VIDEO_ES && b_selected )
314 uint8_t *p = p_sys->buffer;
316 while( p < &p_sys->buffer[i_size - 2] )
325 if( (h&0xc0) == 0x40 )
329 i_len = &p_sys->buffer[i_size] - p;
335 i_subseq = (*p++)&0x7f;
337 i_len = (p[0] << 8)|p[1]; p += 2;
338 if( (i_len&0xc000) == 0 )
341 i_len |= (p[0] << 8)|p[1]; p += 2;
349 i_offset = (p[0] << 8)|p[1]; p += 2;
350 if( (i_offset&0xc000) == 0 )
353 i_offset |= (p[0] << 8)|p[1]; p += 2;
354 i_offset &= 0x3fffffff;
363 i_copy = i_len - i_offset;
364 if( i_copy > &p_sys->buffer[i_size] - p )
366 i_copy = &p_sys->buffer[i_size] - p;
368 else if( i_copy < 0 )
373 msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d",
374 i_len, i_offset, i_copy, i_subseq, i_seqnum );
376 if( (h&0xc0) == 0x80 )
378 /* last fragment -> fixes */
380 i_offset = i_len - i_copy;
381 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
386 ( tk->p_frame->i_dts != i_pts ||
387 tk->i_frame != i_len ) )
389 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
391 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
397 if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
405 if( tk->p_frame == NULL )
407 msg_Dbg( p_demux, "new frame size=%d", i_len );
409 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
413 memset( &tk->p_frame->p_buffer[8], 0, i_len );
414 tk->p_frame->i_dts = i_pts;
415 tk->p_frame->i_pts = i_pts;
417 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
418 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */
421 if( i_offset < tk->i_frame)
423 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
425 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
426 i_ck, i_offset, i_copy );
428 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +0 ] = 1;
429 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +1 ] = i_offset;
432 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
437 if( (h&0xc0) != 0x80 )
446 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
448 if( (h&0xc0) == 0x80 )
452 i_offset = i_len - i_offset;
454 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
455 memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
458 if( p_sys->i_pcr < tk->p_frame->i_dts )
460 p_sys->i_pcr = tk->p_frame->i_dts;
461 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
462 (int64_t)p_sys->i_pcr );
464 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
472 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
473 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
477 if( (h&0xc0) != 0x00 )
482 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
486 p_frame->i_dts = i_pts;
487 p_frame->i_pts = i_pts;
489 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
490 ((uint32_t*)p_frame->p_buffer)[1] = 1;
491 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
492 memcpy( &p_frame->p_buffer[8], p, i_copy );
496 if( p_sys->i_pcr < p_frame->i_dts )
498 p_sys->i_pcr = p_frame->i_dts;
499 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
500 (int64_t)p_sys->i_pcr );
502 es_out_Send( p_demux->out, tk->p_es, p_frame );
508 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
512 memset( &tk->p_frame->p_buffer[8], 0, i_len );
513 tk->p_frame->i_dts = i_pts;
514 tk->p_frame->i_pts = i_pts;
516 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
517 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */
518 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
519 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
526 else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
528 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
529 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
530 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
531 tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
533 uint8_t *p_buf = p_sys->buffer;
534 int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
538 if( i_flags & 2 || ( p_sys->b_seek ) )
540 y = tk->i_subpacket = 0;
541 tk->i_out_subpacket = 0;
545 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
546 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
548 int num = tk->i_frame_size / tk->i_subpacket_size;
549 for( i = 0; i < num; i++ )
551 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
552 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
553 p_buf += tk->i_subpacket_size;
555 i_index = tk->i_subpacket_h * i +
556 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
558 if ( tk->p_subpackets[i_index] != NULL )
560 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
561 free( tk->p_subpackets[i_index] );
564 tk->p_subpackets[i_index] = p_block;
566 p_block->i_dts = p_block->i_pts = 0;
568 tk->p_subpackets_timecode[tk->i_subpacket - num] = i_pts;
571 if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
572 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
573 for( i = 0; i < tk->i_subpacket_h / 2; i++ )
575 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
576 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
577 p_buf += tk->i_coded_frame_size;
579 i_index = (i * 2 * tk->i_frame_size) /
580 tk->i_coded_frame_size + y;
582 p_block->i_dts = p_block->i_pts = i_pts;
583 tk->p_subpackets[i_index] = p_block;
587 while( tk->i_out_subpacket != tk->i_subpackets &&
588 tk->p_subpackets[tk->i_out_subpacket] )
592 if (tk->i_out_subpacket == 0)
594 p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
595 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
596 (int64_t)p_sys->i_pcr );
599 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
600 tk->p_subpackets[tk->i_out_subpacket] = 0;
602 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
605 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
606 tk->p_subpackets[tk->i_out_subpacket] = 0;
607 //if ( p_block->i_dts )
608 if ( tk->p_subpackets_timecode[tk->i_out_subpacket] )
610 p_block->i_dts = p_block->i_pts =
611 tk->p_subpackets_timecode[tk->i_out_subpacket];
612 tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
614 p_sys->i_pcr = p_block->i_dts;
615 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
616 (int64_t)p_sys->i_pcr );
618 es_out_Send( p_demux->out, tk->p_es, p_block );
620 tk->i_out_subpacket++;
623 if( tk->i_subpacket == tk->i_subpackets &&
624 tk->i_out_subpacket != tk->i_subpackets )
626 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
627 "this shouldn't happen" );
630 if( tk->i_subpacket == tk->i_subpackets )
633 tk->i_out_subpacket = 0;
639 if( p_sys->i_pcr < i_pts )
641 p_sys->i_pcr = i_pts;
642 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
643 (int64_t)p_sys->i_pcr );
646 if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
648 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
649 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
652 for( i = 0; i < i_sub; i++ )
654 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
655 block_t *p_block = block_New( p_demux, i_sub_size );
658 memcpy( p_block->p_buffer, p_sub, i_sub_size );
662 p_block->i_pts = ( i == 0 ? i_pts : 0 );
664 es_out_Send( p_demux->out, tk->p_es, p_block );
670 block_t *p_block = block_New( p_demux, i_size );
672 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
674 uint8_t *src = p_sys->buffer;
675 uint8_t *dst = p_block->p_buffer;
678 while( dst < &p_block->p_buffer[i_size- 1])
688 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
690 p_block->i_dts = p_block->i_pts = i_pts;
691 es_out_Send( p_demux->out, tk->p_es, p_block );
700 /*****************************************************************************
702 *****************************************************************************/
703 static int Control( demux_t *p_demux, int i_query, va_list args )
705 demux_sys_t *p_sys = p_demux->p_sys;
708 rm_index_t * p_index;
713 case DEMUX_GET_POSITION:
714 pf = (double*) va_arg( args, double* );
716 if( p_sys->i_our_duration > 0 )
718 *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
722 /* read stream size maybe failed in rtsp streaming,
723 so use duration to determin the position at first */
724 i64 = stream_Size( p_demux->s );
727 *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
736 pi64 = (int64_t*)va_arg( args, int64_t * );
738 if( p_sys->i_our_duration > 0 )
740 *pi64 = p_sys->i_pcr;
744 /* same as GET_POSTION */
745 i64 = stream_Size( p_demux->s );
746 if( p_sys->i_our_duration > 0 && i64 > 0 )
748 *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
755 case DEMUX_SET_POSITION:
756 f = (double) va_arg( args, double );
757 i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
759 //msg_Dbg(p_demux,"Seek Real DEMUX_SET_POSITION : %f file_offset :"I64Fd" p_sys->i_pcr "I64Fd" ", f, i64 , p_sys->i_pcr );
761 if ( p_sys->i_index_offset == 0 && i64 != 0 )
763 msg_Err(p_demux,"Seek No Index Real File failed!" );
764 return VLC_EGENERIC; // no index!
768 /* it is a rtsp stream , it is specials in access/rtsp/... */
770 msg_Dbg(p_demux, "Seek in real rtsp stream!");
771 p_sys->i_pcr = (int64_t)1000 * ( p_sys->i_our_duration * f );
773 es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
776 return stream_Seek( p_demux->s, p_sys->i_pcr );
779 if ( p_sys->i_index_offset > 0 )
781 p_index = p_sys->p_index;
782 while( p_index->file_offset !=0 )
784 if ( p_index->file_offset > i64 )
787 msg_Dbg(p_demux, "Seek Real find! %d %d %d", p_index->time_offset, p_index->file_offset ,(uint32_t) i64);
789 if ( p_index != p_sys->p_index ) p_index --;
790 i64 = p_index->file_offset;
796 //msg_Dbg(p_demux, "Seek Real pcr from :"I64Fd" to "I64Fd" ", p_sys->i_pcr , 1000 * (int64_t) p_index->time_offset );
797 p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
799 es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
801 return stream_Seek( p_demux->s, i64 );
804 i64 = (int64_t) va_arg( args, int64_t ) / 1000;
805 //msg_Dbg(p_demux,"DEMUX_SET_TIME :OK "I64Fd" ",i64);
807 p_index = p_sys->p_index;
808 while( p_index->file_offset !=0 )
810 if ( p_index->time_offset > i64 )
812 if ( p_index != p_sys->p_index )
814 i64 = p_index->file_offset;
820 p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
821 es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
823 return stream_Seek( p_demux->s, i64 );
825 case DEMUX_GET_LENGTH:
826 pi64 = (int64_t*)va_arg( args, int64_t * );
828 /* the commented following lines are fen's implementation, which doesn't seem to
829 * work for one reason or another -- FK */
830 /*if( p_sys->i_mux_rate > 0 )
832 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
835 if( p_sys->i_our_duration > 0 )
837 /* our stored duration is in ms, so... */
838 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
847 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
849 /* the core will crash if we provide NULL strings, so check
850 * every string first */
851 if( p_sys->psz_title )
852 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
853 if( p_sys->psz_artist )
854 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
855 if( p_sys->psz_copyright )
856 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
857 if( p_sys->psz_description )
858 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
869 /*****************************************************************************
871 *****************************************************************************/
873 static void ReadRealIndex( demux_t *p_demux )
875 demux_sys_t *p_sys = p_demux->p_sys;
882 uint32_t i_index_count;
884 if ( p_sys->i_index_offset == 0 )
887 stream_Seek( p_demux->s, p_sys->i_index_offset );
889 if ( stream_Read( p_demux->s, buffer, 20 ) < 20 )
892 i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
893 i_size = GetDWBE( &buffer[4] );
894 i_version = GetWBE( &buffer[8] );
896 msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
897 (char*)&i_id, i_size, i_version );
899 if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
902 i_index_count = GetDWBE( &buffer[10] );
904 msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
906 if ( i_index_count == 0 )
909 if ( GetDWBE( &buffer[16] ) > 0 )
910 msg_Dbg( p_demux, "Real Index: next index is exist? %d ", GetDWBE( &buffer[16] ) );
912 p_sys->p_index = ( rm_index_t *) malloc( sizeof(rm_index_t) * (i_index_count+1) );
913 if ( p_sys->p_index == NULL )
915 msg_Err( p_demux, "Real Index: Error , fail to malloc index buffer " );
919 memset( p_sys->p_index, 0, sizeof(rm_index_t) * (i_index_count+1) );
921 for( i=0; i<i_index_count; i++ )
923 if ( stream_Read( p_demux->s, buffer, 14 ) < 14 )
926 if ( GetWBE( &buffer[0] ) != 0 )
928 msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ", GetWBE( &buffer[0] ) );
932 p_sys->p_index[i].time_offset = GetDWBE( &buffer[2] );
933 p_sys->p_index[i].file_offset = GetDWBE( &buffer[6] );
934 p_sys->p_index[i].frame_index = GetDWBE( &buffer[10] );
936 msg_Dbg( p_demux, "Real Index: time %d file %d frame %d ", p_sys->p_index[i].time_offset, p_sys->p_index[i].file_offset , p_sys->p_index[i].frame_index );
943 /*****************************************************************************
945 *****************************************************************************/
946 static int HeaderRead( demux_t *p_demux )
948 demux_sys_t *p_sys = p_demux->p_sys;
949 uint8_t header[100]; /* FIXME */
955 p_sys->p_meta = vlc_meta_New();
959 /* Read the header */
960 if( stream_Read( p_demux->s, header, 10 ) < 10 )
964 i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
965 i_size = GetDWBE( &header[4] );
966 i_version = GetWBE( &header[8] );
968 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
969 (char*)&i_id, i_size, i_version );
971 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
973 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
976 i_skip = i_size - 10;
978 if( i_id == VLC_FOURCC('.','R','M','F') )
980 if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
981 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
982 GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
986 else if( i_id == VLC_FOURCC('P','R','O','P') )
990 if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
992 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
993 GetDWBE(&header[0]), GetDWBE(&header[4]) );
994 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
995 GetDWBE(&header[8]), GetDWBE(&header[12]) );
996 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) );
997 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) );
998 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) );
999 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) );
1000 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) );
1001 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) );
1003 /* set the duration for export in control */
1004 p_sys->i_our_duration = (int)GetDWBE(&header[20]);
1006 p_sys->i_index_offset = GetDWBE(&header[28]);
1008 i_flags = GetWBE(&header[38]);
1009 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
1011 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
1012 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
1013 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
1016 else if( i_id == VLC_FOURCC('C','O','N','T') )
1021 /* FIXME FIXME: should convert from whatever the character
1022 * encoding of the input meta data is to UTF-8. */
1024 stream_Read( p_demux->s, header, 2 );
1025 if( ( i_len = GetWBE( header ) ) > 0 )
1027 psz = malloc( i_len + 1 );
1028 stream_Read( p_demux->s, psz, i_len );
1032 msg_Dbg( p_demux, " - title=`%s'", psz );
1033 p_sys->psz_title = psz;
1034 vlc_meta_Add( p_sys->p_meta, VLC_META_TITLE, psz );
1040 stream_Read( p_demux->s, header, 2 );
1041 if( ( i_len = GetWBE( header ) ) > 0 )
1043 psz = malloc( i_len + 1 );
1044 stream_Read( p_demux->s, psz, i_len );
1048 msg_Dbg( p_demux, " - author=`%s'", psz );
1049 p_sys->psz_artist = psz;
1050 vlc_meta_Add( p_sys->p_meta, VLC_META_ARTIST, psz );
1056 stream_Read( p_demux->s, header, 2 );
1057 if( ( i_len = GetWBE( header ) ) > 0 )
1059 psz = malloc( i_len + 1 );
1060 stream_Read( p_demux->s, psz, i_len );
1064 msg_Dbg( p_demux, " - copyright=`%s'", psz );
1065 p_sys->psz_copyright = psz;
1066 vlc_meta_Add( p_sys->p_meta, VLC_META_COPYRIGHT, psz );
1072 stream_Read( p_demux->s, header, 2 );
1073 if( ( i_len = GetWBE( header ) ) > 0 )
1075 psz = malloc( i_len + 1 );
1076 stream_Read( p_demux->s, psz, i_len );
1080 msg_Dbg( p_demux, " - comment=`%s'", psz );
1081 p_sys->psz_description = psz;
1082 vlc_meta_Add( p_sys->p_meta, VLC_META_DESCRIPTION, psz );
1088 else if( i_id == VLC_FOURCC('M','D','P','R') )
1090 /* Media properties header */
1095 if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
1096 i_num = GetWBE( header );
1097 msg_Dbg( p_demux, " - id=0x%x", i_num );
1098 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
1099 GetDWBE(&header[2]), GetDWBE(&header[6]) );
1100 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
1101 GetDWBE(&header[10]), GetDWBE(&header[14]) );
1102 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&header[18]) );
1103 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&header[22]) );
1104 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&header[26]) );
1108 stream_Read( p_demux->s, header, 1 );
1109 if( ( i_len = header[0] ) > 0 )
1111 psz = malloc( i_len + 1 );
1112 stream_Read( p_demux->s, psz, i_len );
1115 msg_Dbg( p_demux, " - name=`%s'", psz );
1121 stream_Read( p_demux->s, header, 1 );
1122 if( ( i_len = header[0] ) > 0 )
1124 psz = malloc( i_len + 1 );
1125 stream_Read( p_demux->s, psz, i_len );
1128 msg_Dbg( p_demux, " - mime=`%s'", psz );
1134 stream_Read( p_demux->s, header, 4 );
1135 if( ( i_len = GetDWBE( header ) ) > 0 )
1137 ReadCodecSpecificData( p_demux, i_len, i_num );
1138 stream_Read( p_demux->s, NULL, i_len );
1144 else if( i_id == VLC_FOURCC('D','A','T','A') )
1146 stream_Read( p_demux->s, header, 8 );
1148 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
1149 p_sys->i_data_size = i_size;
1150 p_sys->i_data_packets_count = GetDWBE( header );
1151 p_sys->i_data_packets = 0;
1152 p_sys->i_data_offset_next = GetDWBE( &header[4] );
1154 msg_Dbg( p_demux, " - packets count=%d next=%u",
1155 p_sys->i_data_packets_count,
1156 (uint32_t)p_sys->i_data_offset_next );
1158 /* we have finished the header */
1164 msg_Dbg( p_demux, "unknown chunk" );
1167 if( i_skip < 0 ) return VLC_EGENERIC;
1168 stream_Read( p_demux->s, NULL, i_skip );
1171 /* TODO read index if possible */
1173 if ( p_sys->i_index_offset > 0 )
1176 pos = stream_Tell( p_demux->s );
1177 ReadRealIndex( p_demux );
1178 stream_Seek( p_demux->s, pos );
1184 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
1186 demux_sys_t *p_sys = p_demux->p_sys;
1189 const uint8_t *p_peek;
1191 msg_Dbg( p_demux, " - specific data len=%d", i_len );
1192 if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
1194 if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
1196 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
1197 p_peek[10], p_peek[11] ) );
1198 fmt.video.i_width = GetWBE( &p_peek[12] );
1199 fmt.video.i_height= GetWBE( &p_peek[14] );
1202 fmt.p_extra = malloc( 8 );
1203 ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
1204 ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
1206 msg_Dbg( p_demux, " - video 0x%08x 0x%08x",
1207 ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
1209 if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
1210 GetDWBE( &p_peek[30] ) == 0x10003001 )
1212 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
1214 else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
1215 GetDWBE( &p_peek[30] ) == 0x20100001 ||
1216 GetDWBE( &p_peek[30] ) == 0x20200002 )
1218 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
1220 else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
1222 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
1224 else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
1226 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1229 msg_Dbg( p_demux, " - video %4.4s %dx%d",
1230 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
1232 tk = malloc( sizeof( real_track_t ) );
1233 tk->i_out_subpacket = 0;
1234 tk->i_subpacket = 0;
1235 tk->i_subpackets = 0;
1236 tk->p_subpackets = NULL;
1237 tk->p_subpackets_timecode = NULL;
1242 tk->p_es = es_out_Add( p_demux->out, &fmt );
1244 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1246 else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
1248 int i_header_size = 0;
1250 int i_coded_frame_size = 0;
1251 int i_subpacket_h = 0;
1252 int i_frame_size = 0;
1253 int i_subpacket_size = 0;
1255 int i_version = GetWBE( &p_peek[4] ); /* [0..3] = '.','r','a',0xfd */
1256 msg_Dbg( p_demux, " - audio version=%d", i_version );
1258 p_peek += 6; /* 4 + version */
1259 es_format_Init( &fmt, AUDIO_ES, 0 );
1261 if( i_version == 3 )
1266 i_header_size = GetWBE( p_peek ); p_peek += 2; /* Size from now */
1267 p_peek += 10; /* Unknown */
1269 p_peek += 4; /* Data Size */
1272 i_len = *p_peek ; p_peek++;
1275 psz = malloc( i_len + 1 );
1276 memcpy( psz, p_peek, i_len );
1280 msg_Dbg( p_demux, " - title=`%s'", psz );
1281 p_sys->psz_title = psz;
1286 i_len = *p_peek ; p_peek++;
1289 psz = malloc( i_len + 1 );
1290 memcpy( psz, p_peek, i_len );
1294 msg_Dbg( p_demux, " - artist=`%s'", psz );
1295 p_sys->psz_artist = psz;
1300 i_len = *p_peek ; p_peek++;
1303 psz = malloc( i_len + 1 );
1304 memcpy( psz, p_peek, i_len );
1308 msg_Dbg( p_demux, " - Copyright=`%s'", psz );
1309 p_sys->psz_copyright = psz;
1314 i_len = *p_peek ; p_peek++;
1317 psz = malloc( i_len + 1 );
1318 memcpy( psz, p_peek, i_len );
1322 msg_Dbg( p_demux, " - Comment=`%s'", psz );
1323 p_sys->psz_description = psz;
1325 /* This might be unusefull */
1328 p_peek ++; /* Unknown */
1329 p_peek ++; /* FourCC length = 4 */
1330 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1333 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1334 fmt.audio.i_rate = 8000;
1336 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1337 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1339 else /* RMF version 4/5 */
1341 p_peek += 2; /* 00 00 */
1342 p_peek += 4; /* .ra4 or .ra5 */
1343 p_peek += 4; /* data size */
1344 p_peek += 2; /* version (4 or 5) */
1345 i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1346 i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1347 i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
1348 p_peek += 4; /* ?? */
1349 p_peek += 4; /* ?? */
1350 p_peek += 4; /* ?? */
1351 i_subpacket_h = GetWBE( p_peek ); p_peek += 2; /* 1 */
1352 i_frame_size = GetWBE( p_peek ); p_peek += 2; /* frame size */
1353 i_subpacket_size = GetWBE( p_peek ); p_peek += 2; /* subpacket_size */
1354 if( !i_subpacket_size || !i_frame_size || !i_coded_frame_size )
1355 return VLC_EGENERIC;
1356 p_peek += 2; /* ?? */
1358 if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1360 fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */
1361 p_peek += 2; /* ?? */
1362 fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
1363 fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
1364 fmt.audio.i_blockalign = i_frame_size;
1366 if( i_version == 5 )
1368 memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4; /* genr */
1369 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1373 p_peek += p_peek[0] + 1; /* descr 1 */
1374 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
1375 p_peek += p_peek[0] + 1;
1378 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1379 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1381 p_peek += 3; /* ?? */
1382 if( i_version == 5 ) p_peek++;
1383 /* Extra Data then: DWord + byte[] */
1384 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1387 switch( fmt.i_codec )
1389 case VLC_FOURCC('1','4','_','4'):
1390 /* fmt.audio.i_blockalign = 0x14 */
1392 case VLC_FOURCC('l','p','c','J'):
1393 /* fmt.audio.i_blockalign = 0x14 */
1394 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1397 case VLC_FOURCC('2','8','_','8'):
1399 fmt.audio.i_blockalign = i_coded_frame_size;
1402 case VLC_FOURCC( 'd','n','e','t' ):
1403 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1406 case VLC_FOURCC( 'r','a','a','c' ):
1407 case VLC_FOURCC( 'r','a','c','p' ):
1408 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1410 if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1411 if( fmt.i_extra > 0 )
1413 fmt.p_extra = malloc( fmt.i_extra );
1414 if( fmt.p_extra == NULL )
1416 msg_Err( p_demux, "Error in the extra data" );
1417 return VLC_EGENERIC;
1419 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1423 case VLC_FOURCC('s','i','p','r'):
1424 fmt.audio.i_flavor = i_flavor;
1425 case VLC_FOURCC('c','o','o','k'):
1426 case VLC_FOURCC('a','t','r','c'):
1427 if( !memcmp( p_genr, "genr", 4 ) )
1428 fmt.audio.i_blockalign = i_subpacket_size;
1430 fmt.audio.i_blockalign = i_coded_frame_size;
1432 if( fmt.i_extra > 0 )
1434 fmt.p_extra = malloc( fmt.i_extra );
1435 if( fmt.p_extra == NULL )
1437 msg_Err( p_demux, "Error in the extra data" );
1438 return VLC_EGENERIC;
1440 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1444 case VLC_FOURCC('r','a','l','f'):
1445 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1446 (char*)&fmt.i_codec );
1450 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1451 (char*)&fmt.i_codec );
1455 if( fmt.i_codec != 0 )
1457 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1459 tk = malloc( sizeof( real_track_t ) );
1465 tk->i_subpacket_h = i_subpacket_h;
1466 tk->i_subpacket_size = i_subpacket_size;
1467 tk->i_coded_frame_size = i_coded_frame_size;
1468 tk->i_frame_size = i_frame_size;
1470 tk->i_out_subpacket = 0;
1471 tk->i_subpacket = 0;
1472 tk->i_subpackets = 0;
1473 tk->p_subpackets = NULL;
1474 tk->p_subpackets_timecode = NULL;
1475 if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
1476 || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1479 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1481 calloc( tk->i_subpackets, sizeof(block_t *) );
1482 tk->p_subpackets_timecode =
1483 calloc( tk->i_subpackets , sizeof( int64_t ) );
1485 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1488 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1490 calloc( tk->i_subpackets, sizeof(block_t *) );
1491 tk->p_subpackets_timecode =
1492 calloc( tk->i_subpackets , sizeof( int64_t ) );
1495 /* Check if the calloc went correctly */
1496 if( tk->p_subpackets == NULL )
1498 tk->i_subpackets = 0;
1499 msg_Err( p_demux, "Can't alloc subpacket" );
1500 return VLC_EGENERIC;
1503 tk->p_es = es_out_Add( p_demux->out, &fmt );
1505 TAB_APPEND( p_sys->i_track, p_sys->track, tk );