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 *****************************************************************************/
52 #include <vlc_common.h>
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 ) );
171 memset( p_sys, 0, sizeof( demux_sys_t ) );
173 p_sys->i_data_offset = 0;
180 /* Parse the headers */
181 if( HeaderRead( p_demux ) )
184 msg_Err( p_demux, "invalid header" );
185 for( i = 0; i < p_sys->i_track; i++ )
187 real_track_t *tk = p_sys->track[i];
191 es_out_Del( p_demux->out, tk->p_es );
195 if( p_sys->i_track > 0 )
197 free( p_sys->track );
206 /*****************************************************************************
208 *****************************************************************************/
209 static void Close( vlc_object_t *p_this )
211 demux_t *p_demux = (demux_t*)p_this;
212 demux_sys_t *p_sys = p_demux->p_sys;
215 for( i = 0; i < p_sys->i_track; i++ )
217 real_track_t *tk = p_sys->track[i];
218 int j = tk->i_subpackets;
220 if( tk->p_frame ) block_Release( tk->p_frame );
221 es_format_Clean( &tk->fmt );
225 if( tk->p_subpackets[ j ] )
226 block_Release( tk->p_subpackets[ j ] );
228 if( tk->i_subpackets )
230 free( tk->p_subpackets );
231 free( tk->p_subpackets_timecode );
237 free( p_sys->psz_title );
238 free( p_sys->psz_artist );
239 free( p_sys->psz_copyright );
240 free( p_sys->psz_description );
241 free( p_sys->p_index );
243 if( p_sys->i_track > 0 ) free( p_sys->track );
248 /*****************************************************************************
250 *****************************************************************************/
251 static int Demux( demux_t *p_demux )
253 demux_sys_t *p_sys = p_demux->p_sys;
255 int i_size, i_id, i_flags, i;
257 real_track_t *tk = NULL;
260 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
261 p_sys->i_data_packets_count )
263 if( stream_Read( p_demux->s, header, 18 ) < 18 )
267 if( strncmp( (char *)header, "DATA", 4 ) )
271 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
272 p_sys->i_data_size = GetDWBE( &header[4] );
273 p_sys->i_data_packets_count = GetDWBE( &header[10] );
274 p_sys->i_data_packets = 0;
275 p_sys->i_data_offset_next = GetDWBE( &header[14] );
277 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
278 p_sys->i_data_packets_count,
279 (uint32_t)p_sys->i_data_offset_next );
282 if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
284 i_size = GetWBE( &header[2] ) - 12;
285 i_id = GetWBE( &header[4] );
286 i_pts = 1000 * GetDWBE( &header[6] );
287 i_pts += 1000; /* Avoid 0 pts */
288 i_flags= header[11]; /* flags 0x02 -> keyframe */
290 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
291 p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
293 p_sys->i_data_packets++;
295 stream_Read( p_demux->s, p_sys->buffer, i_size );
297 for( i = 0; i < p_sys->i_track; i++ )
299 if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
304 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
307 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
309 if( tk->fmt.i_cat == VIDEO_ES && b_selected )
311 uint8_t *p = p_sys->buffer;
313 while( p < &p_sys->buffer[i_size - 2] )
322 if( (h&0xc0) == 0x40 )
326 i_len = &p_sys->buffer[i_size] - p;
332 i_subseq = (*p++)&0x7f;
334 i_len = (p[0] << 8)|p[1]; p += 2;
335 if( (i_len&0xc000) == 0 )
338 i_len |= (p[0] << 8)|p[1]; p += 2;
346 i_offset = (p[0] << 8)|p[1]; p += 2;
347 if( (i_offset&0xc000) == 0 )
350 i_offset |= (p[0] << 8)|p[1]; p += 2;
351 i_offset &= 0x3fffffff;
360 i_copy = i_len - i_offset;
361 if( i_copy > &p_sys->buffer[i_size] - p )
363 i_copy = &p_sys->buffer[i_size] - p;
365 else if( i_copy < 0 )
370 msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d",
371 i_len, i_offset, i_copy, i_subseq, i_seqnum );
373 if( (h&0xc0) == 0x80 )
375 /* last fragment -> fixes */
377 i_offset = i_len - i_copy;
378 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
383 ( tk->p_frame->i_dts != i_pts ||
384 tk->i_frame != i_len ) )
386 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
388 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
394 if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
402 if( tk->p_frame == NULL )
404 msg_Dbg( p_demux, "new frame size=%d", i_len );
406 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
410 memset( &tk->p_frame->p_buffer[8], 0, i_len );
411 tk->p_frame->i_dts = i_pts;
412 tk->p_frame->i_pts = i_pts;
414 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
415 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */
418 if( i_offset < tk->i_frame)
420 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
422 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
423 i_ck, i_offset, i_copy );
425 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +0 ] = 1;
426 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +1 ] = i_offset;
429 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
434 if( (h&0xc0) != 0x80 )
443 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
445 if( (h&0xc0) == 0x80 )
449 i_offset = i_len - i_offset;
451 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
452 memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
455 if( p_sys->i_pcr < tk->p_frame->i_dts )
457 p_sys->i_pcr = tk->p_frame->i_dts;
458 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
459 (int64_t)p_sys->i_pcr );
461 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
469 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
470 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
474 if( (h&0xc0) != 0x00 )
479 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
483 p_frame->i_dts = i_pts;
484 p_frame->i_pts = i_pts;
486 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
487 ((uint32_t*)p_frame->p_buffer)[1] = 1;
488 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
489 memcpy( &p_frame->p_buffer[8], p, i_copy );
493 if( p_sys->i_pcr < p_frame->i_dts )
495 p_sys->i_pcr = p_frame->i_dts;
496 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
497 (int64_t)p_sys->i_pcr );
499 es_out_Send( p_demux->out, tk->p_es, p_frame );
505 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
509 memset( &tk->p_frame->p_buffer[8], 0, i_len );
510 tk->p_frame->i_dts = i_pts;
511 tk->p_frame->i_pts = i_pts;
513 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
514 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */
515 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
516 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
523 else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
525 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
526 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
527 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
528 tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
530 uint8_t *p_buf = p_sys->buffer;
531 int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
535 if( i_flags & 2 || ( p_sys->b_seek ) )
537 y = tk->i_subpacket = 0;
538 tk->i_out_subpacket = 0;
542 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
543 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
545 int num = tk->i_frame_size / tk->i_subpacket_size;
546 for( i = 0; i < num; i++ )
548 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
549 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
550 p_buf += tk->i_subpacket_size;
552 i_index = tk->i_subpacket_h * i +
553 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
555 if ( tk->p_subpackets[i_index] != NULL )
557 msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
558 free( tk->p_subpackets[i_index] );
561 tk->p_subpackets[i_index] = p_block;
563 p_block->i_dts = p_block->i_pts = 0;
565 tk->p_subpackets_timecode[tk->i_subpacket - num] = i_pts;
568 if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
569 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
570 for( i = 0; i < tk->i_subpacket_h / 2; i++ )
572 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
573 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
574 p_buf += tk->i_coded_frame_size;
576 i_index = (i * 2 * tk->i_frame_size) /
577 tk->i_coded_frame_size + y;
579 p_block->i_dts = p_block->i_pts = i_pts;
580 tk->p_subpackets[i_index] = p_block;
584 while( tk->i_out_subpacket != tk->i_subpackets &&
585 tk->p_subpackets[tk->i_out_subpacket] )
589 if (tk->i_out_subpacket == 0)
591 p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
592 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
593 (int64_t)p_sys->i_pcr );
596 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
597 tk->p_subpackets[tk->i_out_subpacket] = 0;
599 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
602 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
603 tk->p_subpackets[tk->i_out_subpacket] = 0;
604 if ( tk->p_subpackets_timecode[tk->i_out_subpacket] )
606 p_block->i_dts = p_block->i_pts =
607 tk->p_subpackets_timecode[tk->i_out_subpacket];
608 tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
610 p_sys->i_pcr = p_block->i_dts;
611 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
612 (int64_t)p_sys->i_pcr );
614 es_out_Send( p_demux->out, tk->p_es, p_block );
616 tk->i_out_subpacket++;
619 if( tk->i_subpacket == tk->i_subpackets &&
620 tk->i_out_subpacket != tk->i_subpackets )
622 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
623 "this shouldn't happen" );
626 if( tk->i_subpacket == tk->i_subpackets )
629 tk->i_out_subpacket = 0;
635 if( p_sys->i_pcr < i_pts )
637 p_sys->i_pcr = i_pts;
638 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
639 (int64_t)p_sys->i_pcr );
642 if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
644 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
645 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
648 for( i = 0; i < i_sub; i++ )
650 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
651 block_t *p_block = block_New( p_demux, i_sub_size );
654 memcpy( p_block->p_buffer, p_sub, i_sub_size );
658 p_block->i_pts = ( i == 0 ? i_pts : 0 );
660 es_out_Send( p_demux->out, tk->p_es, p_block );
666 block_t *p_block = block_New( p_demux, i_size );
668 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
670 uint8_t *src = p_sys->buffer;
671 uint8_t *dst = p_block->p_buffer;
674 while( dst < &p_block->p_buffer[i_size- 1])
684 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
686 p_block->i_dts = p_block->i_pts = i_pts;
687 es_out_Send( p_demux->out, tk->p_es, p_block );
696 /*****************************************************************************
698 *****************************************************************************/
699 static int Control( demux_t *p_demux, int i_query, va_list args )
701 demux_sys_t *p_sys = p_demux->p_sys;
704 rm_index_t * p_index;
709 case DEMUX_GET_POSITION:
710 pf = (double*) va_arg( args, double* );
712 if( p_sys->i_our_duration > 0 )
714 *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
718 /* read stream size maybe failed in rtsp streaming,
719 so use duration to determin the position at first */
720 i64 = stream_Size( p_demux->s );
723 *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
732 pi64 = (int64_t*)va_arg( args, int64_t * );
734 if( p_sys->i_our_duration > 0 )
736 *pi64 = p_sys->i_pcr;
740 /* same as GET_POSTION */
741 i64 = stream_Size( p_demux->s );
742 if( p_sys->i_our_duration > 0 && i64 > 0 )
744 *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
751 case DEMUX_SET_POSITION:
752 f = (double) va_arg( args, double );
753 i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
755 if ( p_sys->i_index_offset == 0 && i64 != 0 )
757 msg_Err(p_demux,"Seek No Index Real File failed!" );
758 return VLC_EGENERIC; // no index!
762 /* it is a rtsp stream , it is specials in access/rtsp/... */
763 msg_Dbg(p_demux, "Seek in real rtsp stream!");
764 p_sys->i_pcr = (int64_t)1000 * ( p_sys->i_our_duration * f );
766 es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
769 return stream_Seek( p_demux->s, p_sys->i_pcr );
772 if ( p_sys->i_index_offset > 0 )
774 p_index = p_sys->p_index;
775 while( p_index->file_offset !=0 )
777 if ( p_index->file_offset > i64 )
779 msg_Dbg( p_demux, "Seek Real find! %d %d %d",
780 p_index->time_offset, p_index->file_offset,
782 if ( p_index != p_sys->p_index ) p_index --;
783 i64 = p_index->file_offset;
789 p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
791 es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
793 return stream_Seek( p_demux->s, i64 );
796 i64 = (int64_t) va_arg( args, int64_t ) / 1000;
798 p_index = p_sys->p_index;
799 while( p_index->file_offset !=0 )
801 if ( p_index->time_offset > i64 )
803 if ( p_index != p_sys->p_index )
805 i64 = p_index->file_offset;
811 p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
812 es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
814 return stream_Seek( p_demux->s, i64 );
816 case DEMUX_GET_LENGTH:
817 pi64 = (int64_t*)va_arg( args, int64_t * );
819 /* the commented following lines are fen's implementation, which doesn't seem to
820 * work for one reason or another -- FK */
821 /*if( p_sys->i_mux_rate > 0 )
823 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
826 if( p_sys->i_our_duration > 0 )
828 /* our stored duration is in ms, so... */
829 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
838 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
840 /* the core will crash if we provide NULL strings, so check
841 * every string first */
842 if( p_sys->psz_title )
843 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
844 if( p_sys->psz_artist )
845 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
846 if( p_sys->psz_copyright )
847 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
848 if( p_sys->psz_description )
849 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
860 /*****************************************************************************
862 *****************************************************************************/
864 static void ReadRealIndex( demux_t *p_demux )
866 demux_sys_t *p_sys = p_demux->p_sys;
873 uint32_t i_index_count;
875 if ( p_sys->i_index_offset == 0 )
878 stream_Seek( p_demux->s, p_sys->i_index_offset );
880 if ( stream_Read( p_demux->s, buffer, 20 ) < 20 )
883 i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
884 i_size = GetDWBE( &buffer[4] );
885 i_version = GetWBE( &buffer[8] );
887 msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
888 (char*)&i_id, i_size, i_version );
890 if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
893 i_index_count = GetDWBE( &buffer[10] );
895 msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
897 if( i_index_count == 0 )
900 if( GetDWBE( &buffer[16] ) > 0 )
901 msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
902 GetDWBE( &buffer[16] ) );
905 (rm_index_t *)malloc( sizeof( rm_index_t ) * (i_index_count+1) );
906 if( p_sys->p_index == NULL )
908 msg_Err( p_demux, "Memory allocation error" );
912 memset( p_sys->p_index, 0, sizeof(rm_index_t) * (i_index_count+1) );
914 for( i=0; i<i_index_count; i++ )
916 if( stream_Read( p_demux->s, buffer, 14 ) < 14 )
919 if( GetWBE( &buffer[0] ) != 0 )
921 msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
922 GetWBE( &buffer[0] ) );
926 p_sys->p_index[i].time_offset = GetDWBE( &buffer[2] );
927 p_sys->p_index[i].file_offset = GetDWBE( &buffer[6] );
928 p_sys->p_index[i].frame_index = GetDWBE( &buffer[10] );
929 msg_Dbg( p_demux, "Real Index: time %d file %d frame %d ",
930 p_sys->p_index[i].time_offset,
931 p_sys->p_index[i].file_offset,
932 p_sys->p_index[i].frame_index );
937 /*****************************************************************************
939 *****************************************************************************/
940 static int HeaderRead( demux_t *p_demux )
942 demux_sys_t *p_sys = p_demux->p_sys;
943 uint8_t header[100]; /* FIXME */
949 p_sys->p_meta = vlc_meta_New();
953 /* Read the header */
954 if( stream_Read( p_demux->s, header, 10 ) < 10 )
958 i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
959 i_size = GetDWBE( &header[4] );
960 i_version = GetWBE( &header[8] );
962 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
963 (char*)&i_id, i_size, i_version );
965 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
967 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
970 i_skip = i_size - 10;
972 if( i_id == VLC_FOURCC('.','R','M','F') )
974 if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
975 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
976 GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
980 else if( i_id == VLC_FOURCC('P','R','O','P') )
984 if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
986 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
987 GetDWBE(&header[0]), GetDWBE(&header[4]) );
988 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
989 GetDWBE(&header[8]), GetDWBE(&header[12]) );
990 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) );
991 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) );
992 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) );
993 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) );
994 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) );
995 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) );
997 /* set the duration for export in control */
998 p_sys->i_our_duration = (int)GetDWBE(&header[20]);
1000 p_sys->i_index_offset = GetDWBE(&header[28]);
1002 i_flags = GetWBE(&header[38]);
1003 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
1005 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
1006 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
1007 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
1010 else if( i_id == VLC_FOURCC('C','O','N','T') )
1015 /* FIXME FIXME: should convert from whatever the character
1016 * encoding of the input meta data is to UTF-8. */
1018 stream_Read( p_demux->s, header, 2 );
1019 if( ( i_len = GetWBE( header ) ) > 0 )
1021 psz = malloc( i_len + 1 );
1022 stream_Read( p_demux->s, psz, i_len );
1026 msg_Dbg( p_demux, " - title=`%s'", psz );
1027 p_sys->psz_title = psz;
1032 stream_Read( p_demux->s, header, 2 );
1033 if( ( i_len = GetWBE( header ) ) > 0 )
1035 psz = malloc( i_len + 1 );
1036 stream_Read( p_demux->s, psz, i_len );
1040 msg_Dbg( p_demux, " - author=`%s'", psz );
1041 p_sys->psz_artist = psz;
1046 stream_Read( p_demux->s, header, 2 );
1047 if( ( i_len = GetWBE( header ) ) > 0 )
1049 psz = malloc( i_len + 1 );
1050 stream_Read( p_demux->s, psz, i_len );
1054 msg_Dbg( p_demux, " - copyright=`%s'", psz );
1055 p_sys->psz_copyright = psz;
1060 stream_Read( p_demux->s, header, 2 );
1061 if( ( i_len = GetWBE( header ) ) > 0 )
1063 psz = malloc( i_len + 1 );
1064 stream_Read( p_demux->s, psz, i_len );
1068 msg_Dbg( p_demux, " - comment=`%s'", psz );
1069 p_sys->psz_description = psz;
1074 else if( i_id == VLC_FOURCC('M','D','P','R') )
1076 /* Media properties header */
1081 if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
1082 i_num = GetWBE( header );
1083 msg_Dbg( p_demux, " - id=0x%x", i_num );
1084 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
1085 GetDWBE(&header[2]), GetDWBE(&header[6]) );
1086 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
1087 GetDWBE(&header[10]), GetDWBE(&header[14]) );
1088 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&header[18]) );
1089 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&header[22]) );
1090 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&header[26]) );
1094 stream_Read( p_demux->s, header, 1 );
1095 if( ( i_len = header[0] ) > 0 )
1097 psz = malloc( i_len + 1 );
1098 stream_Read( p_demux->s, psz, i_len );
1101 msg_Dbg( p_demux, " - name=`%s'", psz );
1107 stream_Read( p_demux->s, header, 1 );
1108 if( ( i_len = header[0] ) > 0 )
1110 psz = malloc( i_len + 1 );
1111 stream_Read( p_demux->s, psz, i_len );
1114 msg_Dbg( p_demux, " - mime=`%s'", psz );
1120 stream_Read( p_demux->s, header, 4 );
1121 if( ( i_len = GetDWBE( header ) ) > 0 )
1123 ReadCodecSpecificData( p_demux, i_len, i_num );
1124 stream_Read( p_demux->s, NULL, i_len );
1130 else if( i_id == VLC_FOURCC('D','A','T','A') )
1132 stream_Read( p_demux->s, header, 8 );
1134 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
1135 p_sys->i_data_size = i_size;
1136 p_sys->i_data_packets_count = GetDWBE( header );
1137 p_sys->i_data_packets = 0;
1138 p_sys->i_data_offset_next = GetDWBE( &header[4] );
1140 msg_Dbg( p_demux, " - packets count=%d next=%u",
1141 p_sys->i_data_packets_count,
1142 (uint32_t)p_sys->i_data_offset_next );
1144 /* we have finished the header */
1150 msg_Dbg( p_demux, "unknown chunk" );
1153 if( i_skip < 0 ) return VLC_EGENERIC;
1154 stream_Read( p_demux->s, NULL, i_skip );
1157 /* TODO read index if possible */
1159 if ( p_sys->i_index_offset > 0 )
1162 pos = stream_Tell( p_demux->s );
1163 ReadRealIndex( p_demux );
1164 stream_Seek( p_demux->s, pos );
1170 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
1172 demux_sys_t *p_sys = p_demux->p_sys;
1175 const uint8_t *p_peek;
1177 msg_Dbg( p_demux, " - specific data len=%d", i_len );
1178 if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
1180 if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
1182 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
1183 p_peek[10], p_peek[11] ) );
1184 fmt.video.i_width = GetWBE( &p_peek[12] );
1185 fmt.video.i_height= GetWBE( &p_peek[14] );
1188 fmt.p_extra = malloc( 8 );
1189 ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
1190 ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
1192 msg_Dbg( p_demux, " - video 0x%08x 0x%08x",
1193 ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
1195 if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
1196 GetDWBE( &p_peek[30] ) == 0x10003001 )
1198 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
1200 else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
1201 GetDWBE( &p_peek[30] ) == 0x20100001 ||
1202 GetDWBE( &p_peek[30] ) == 0x20200002 )
1204 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
1206 else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
1208 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
1210 else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
1212 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1215 msg_Dbg( p_demux, " - video %4.4s %dx%d",
1216 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
1218 tk = malloc( sizeof( real_track_t ) );
1219 tk->i_out_subpacket = 0;
1220 tk->i_subpacket = 0;
1221 tk->i_subpackets = 0;
1222 tk->p_subpackets = NULL;
1223 tk->p_subpackets_timecode = NULL;
1228 tk->p_es = es_out_Add( p_demux->out, &fmt );
1230 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1232 else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
1234 int i_header_size = 0;
1236 int i_coded_frame_size = 0;
1237 int i_subpacket_h = 0;
1238 int i_frame_size = 0;
1239 int i_subpacket_size = 0;
1241 int i_version = GetWBE( &p_peek[4] ); /* [0..3] = '.','r','a',0xfd */
1242 msg_Dbg( p_demux, " - audio version=%d", i_version );
1244 p_peek += 6; /* 4 + version */
1245 es_format_Init( &fmt, AUDIO_ES, 0 );
1247 if( i_version == 3 )
1252 i_header_size = GetWBE( p_peek ); p_peek += 2; /* Size from now */
1253 p_peek += 10; /* Unknown */
1255 p_peek += 4; /* Data Size */
1258 i_len = *p_peek ; p_peek++;
1261 psz = malloc( i_len + 1 );
1262 memcpy( psz, p_peek, i_len );
1266 msg_Dbg( p_demux, " - title=`%s'", psz );
1267 p_sys->psz_title = psz;
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, " - artist=`%s'", psz );
1281 p_sys->psz_artist = 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, " - Copyright=`%s'", psz );
1295 p_sys->psz_copyright = 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, " - Comment=`%s'", psz );
1309 p_sys->psz_description = psz;
1311 /* This might be unusefull */
1314 p_peek ++; /* Unknown */
1315 p_peek ++; /* FourCC length = 4 */
1316 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1319 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1320 fmt.audio.i_rate = 8000;
1322 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1323 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1325 else /* RMF version 4/5 */
1327 p_peek += 2; /* 00 00 */
1328 p_peek += 4; /* .ra4 or .ra5 */
1329 p_peek += 4; /* data size */
1330 p_peek += 2; /* version (4 or 5) */
1331 i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1332 i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1333 i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
1334 p_peek += 4; /* ?? */
1335 p_peek += 4; /* ?? */
1336 p_peek += 4; /* ?? */
1337 i_subpacket_h = GetWBE( p_peek ); p_peek += 2; /* 1 */
1338 i_frame_size = GetWBE( p_peek ); p_peek += 2; /* frame size */
1339 i_subpacket_size = GetWBE( p_peek ); p_peek += 2; /* subpacket_size */
1340 if( !i_subpacket_size || !i_frame_size || !i_coded_frame_size )
1341 return VLC_EGENERIC;
1342 p_peek += 2; /* ?? */
1344 if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1346 fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */
1347 p_peek += 2; /* ?? */
1348 fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
1349 fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
1350 fmt.audio.i_blockalign = i_frame_size;
1352 if( i_version == 5 )
1354 memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4; /* genr */
1355 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1359 p_peek += p_peek[0] + 1; /* descr 1 */
1360 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
1361 p_peek += p_peek[0] + 1;
1364 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1365 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1367 p_peek += 3; /* ?? */
1368 if( i_version == 5 ) p_peek++;
1369 /* Extra Data then: DWord + byte[] */
1370 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1373 switch( fmt.i_codec )
1375 case VLC_FOURCC('1','4','_','4'):
1376 /* fmt.audio.i_blockalign = 0x14 */
1378 case VLC_FOURCC('l','p','c','J'):
1379 /* fmt.audio.i_blockalign = 0x14 */
1380 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1383 case VLC_FOURCC('2','8','_','8'):
1385 fmt.audio.i_blockalign = i_coded_frame_size;
1388 case VLC_FOURCC( 'd','n','e','t' ):
1389 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1392 case VLC_FOURCC( 'r','a','a','c' ):
1393 case VLC_FOURCC( 'r','a','c','p' ):
1394 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1396 if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1397 if( fmt.i_extra > 0 )
1399 fmt.p_extra = malloc( fmt.i_extra );
1400 if( fmt.p_extra == NULL )
1402 msg_Err( p_demux, "Error in the extra data" );
1403 return VLC_EGENERIC;
1405 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1409 case VLC_FOURCC('s','i','p','r'):
1410 fmt.audio.i_flavor = i_flavor;
1411 case VLC_FOURCC('c','o','o','k'):
1412 case VLC_FOURCC('a','t','r','c'):
1413 if( !memcmp( p_genr, "genr", 4 ) )
1414 fmt.audio.i_blockalign = i_subpacket_size;
1416 fmt.audio.i_blockalign = i_coded_frame_size;
1418 if( fmt.i_extra > 0 )
1420 fmt.p_extra = malloc( fmt.i_extra );
1421 if( fmt.p_extra == NULL )
1423 msg_Err( p_demux, "Error in the extra data" );
1424 return VLC_EGENERIC;
1426 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1430 case VLC_FOURCC('r','a','l','f'):
1431 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1432 (char*)&fmt.i_codec );
1436 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1437 (char*)&fmt.i_codec );
1441 if( fmt.i_codec != 0 )
1443 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1445 tk = malloc( sizeof( real_track_t ) );
1451 tk->i_subpacket_h = i_subpacket_h;
1452 tk->i_subpacket_size = i_subpacket_size;
1453 tk->i_coded_frame_size = i_coded_frame_size;
1454 tk->i_frame_size = i_frame_size;
1456 tk->i_out_subpacket = 0;
1457 tk->i_subpacket = 0;
1458 tk->i_subpackets = 0;
1459 tk->p_subpackets = NULL;
1460 tk->p_subpackets_timecode = NULL;
1461 if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
1462 || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1465 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1467 calloc( tk->i_subpackets, sizeof(block_t *) );
1468 tk->p_subpackets_timecode =
1469 calloc( tk->i_subpackets , sizeof( int64_t ) );
1471 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1474 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1476 calloc( tk->i_subpackets, sizeof(block_t *) );
1477 tk->p_subpackets_timecode =
1478 calloc( tk->i_subpackets , sizeof( int64_t ) );
1481 /* Check if the calloc went correctly */
1482 if( tk->p_subpackets == NULL )
1484 tk->i_subpackets = 0;
1485 msg_Err( p_demux, "Can't alloc subpacket" );
1486 return VLC_EGENERIC;
1489 tk->p_es = es_out_Add( p_demux->out, &fmt );
1491 TAB_APPEND( p_sys->i_track, p_sys->track, tk );