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 *****************************************************************************/
51 #include <vlc_demux.h>
52 #include <vlc_charset.h>
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close ( vlc_object_t * );
62 set_description( _("Real demuxer" ) );
63 set_capability( "demux2", 15 );
64 set_category( CAT_INPUT );
65 set_subcategory( SUBCAT_INPUT_DEMUX );
66 set_callbacks( Open, Close );
67 add_shortcut( "real" );
71 /*****************************************************************************
73 *****************************************************************************/
87 int i_coded_frame_size;
92 block_t **p_subpackets;
99 int64_t i_data_offset;
101 uint32_t i_data_packets_count;
102 uint32_t i_data_packets;
103 int64_t i_data_offset_next;
111 char* psz_description;
114 real_track_t **track;
116 uint8_t buffer[65536];
121 static int Demux( demux_t *p_demux );
122 static int Control( demux_t *p_demux, int i_query, va_list args );
124 static int HeaderRead( demux_t *p_demux );
125 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
127 /*****************************************************************************
129 *****************************************************************************/
130 static int Open( vlc_object_t *p_this )
132 demux_t *p_demux = (demux_t*)p_this;
135 const uint8_t *p_peek;
137 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
138 if( !memcmp( p_peek, ".ra", 3 ) )
140 msg_Warn( p_demux, ".ra files unsuported" );
143 if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
145 /* Fill p_demux field */
146 p_demux->pf_demux = Demux;
147 p_demux->pf_control = Control;
148 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
149 memset( p_sys, 0, sizeof( demux_sys_t ) );
150 p_sys->i_data_offset = 0;
156 /* Parse the headers */
157 if( HeaderRead( p_demux ) )
160 msg_Err( p_demux, "invalid header" );
161 for( i = 0; i < p_sys->i_track; i++ )
163 real_track_t *tk = p_sys->track[i];
167 es_out_Del( p_demux->out, tk->p_es );
171 if( p_sys->i_track > 0 )
173 free( p_sys->track );
182 /*****************************************************************************
184 *****************************************************************************/
185 static void Close( vlc_object_t *p_this )
187 demux_t *p_demux = (demux_t*)p_this;
188 demux_sys_t *p_sys = p_demux->p_sys;
191 for( i = 0; i < p_sys->i_track; i++ )
193 real_track_t *tk = p_sys->track[i];
194 int j = tk->i_subpackets;
196 if( tk->p_frame ) block_Release( tk->p_frame );
197 es_format_Clean( &tk->fmt );
201 if( tk->p_subpackets[ j ] )
202 block_Release( tk->p_subpackets[ j ] );
204 if( tk->i_subpackets ) free( tk->p_subpackets );
209 if( p_sys->psz_title ) free( p_sys->psz_title );
210 if( p_sys->psz_artist ) free( p_sys->psz_artist );
211 if( p_sys->psz_copyright ) free( p_sys->psz_copyright );
212 if( p_sys->psz_description ) free( p_sys->psz_description );
214 if( p_sys->i_track > 0 ) free( p_sys->track );
219 /*****************************************************************************
221 *****************************************************************************/
222 static int Demux( demux_t *p_demux )
224 demux_sys_t *p_sys = p_demux->p_sys;
226 int i_size, i_id, i_flags, i;
228 real_track_t *tk = NULL;
229 vlc_bool_t b_selected;
231 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
232 p_sys->i_data_packets_count )
234 if( stream_Read( p_demux->s, header, 18 ) < 18 )
238 if( strncmp( (char *)header, "DATA", 4 ) )
242 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
243 p_sys->i_data_size = GetDWBE( &header[4] );
244 p_sys->i_data_packets_count = GetDWBE( &header[10] );
245 p_sys->i_data_packets = 0;
246 p_sys->i_data_offset_next = GetDWBE( &header[14] );
248 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
249 p_sys->i_data_packets_count,
250 (uint32_t)p_sys->i_data_offset_next );
253 if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
255 i_size = GetWBE( &header[2] ) - 12;
256 i_id = GetWBE( &header[4] );
257 i_pts = 1000 * GetDWBE( &header[6] );
258 i_pts += 1000; /* Avoid 0 pts */
259 i_flags= header[11]; /* flags 0x02 -> keyframe */
262 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
263 p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
266 p_sys->i_data_packets++;
268 stream_Read( p_demux->s, p_sys->buffer, i_size );
270 for( i = 0; i < p_sys->i_track; i++ )
272 if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
277 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
280 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
282 if( tk->fmt.i_cat == VIDEO_ES && b_selected )
284 uint8_t *p = p_sys->buffer;
286 while( p < &p_sys->buffer[i_size - 2] )
295 if( (h&0xc0) == 0x40 )
299 i_len = &p_sys->buffer[i_size] - p;
305 i_subseq = (*p++)&0x7f;
307 i_len = (p[0] << 8)|p[1]; p += 2;
308 if( (i_len&0xc000) == 0 )
311 i_len |= (p[0] << 8)|p[1]; p += 2;
319 i_offset = (p[0] << 8)|p[1]; p += 2;
320 if( (i_offset&0xc000) == 0 )
323 i_offset |= (p[0] << 8)|p[1]; p += 2;
324 i_offset &= 0x3fffffff;
333 i_copy = i_len - i_offset;
334 if( i_copy > &p_sys->buffer[i_size] - p )
336 i_copy = &p_sys->buffer[i_size] - p;
338 else if( i_copy < 0 )
343 msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d",
344 i_len, i_offset, i_copy, i_subseq, i_seqnum );
346 if( (h&0xc0) == 0x80 )
348 /* last fragment -> fixes */
350 i_offset = i_len - i_copy;
351 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
356 ( tk->p_frame->i_dts != i_pts ||
357 tk->i_frame != i_len ) )
359 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
361 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
367 if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
375 if( tk->p_frame == NULL )
377 msg_Dbg( p_demux, "new frame size=%d", i_len );
379 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
383 memset( &tk->p_frame->p_buffer[8], 0, i_len );
384 tk->p_frame->i_dts = i_pts;
385 tk->p_frame->i_pts = i_pts;
387 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
388 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */
391 if( i_offset < tk->i_frame)
393 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
395 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
396 i_ck, i_offset, i_copy );
398 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +0 ] = 1;
399 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +1 ] = i_offset;
402 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
407 if( (h&0xc0) != 0x80 )
416 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
418 if( (h&0xc0) == 0x80 )
422 i_offset = i_len - i_offset;
424 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
425 memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
428 if( p_sys->i_pcr < tk->p_frame->i_dts )
430 p_sys->i_pcr = tk->p_frame->i_dts;
431 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
432 (int64_t)p_sys->i_pcr );
434 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
442 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
443 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
447 if( (h&0xc0) != 0x00 )
452 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
456 p_frame->i_dts = i_pts;
457 p_frame->i_pts = i_pts;
459 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
460 ((uint32_t*)p_frame->p_buffer)[1] = 1;
461 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
462 memcpy( &p_frame->p_buffer[8], p, i_copy );
466 if( p_sys->i_pcr < p_frame->i_dts )
468 p_sys->i_pcr = p_frame->i_dts;
469 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
470 (int64_t)p_sys->i_pcr );
472 es_out_Send( p_demux->out, tk->p_es, p_frame );
478 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
482 memset( &tk->p_frame->p_buffer[8], 0, i_len );
483 tk->p_frame->i_dts = i_pts;
484 tk->p_frame->i_pts = i_pts;
486 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
487 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */
488 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
489 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
496 else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
498 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
499 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
500 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
501 tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
503 uint8_t *p_buf = p_sys->buffer;
504 int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
508 if( i_flags & 2 ) y = tk->i_subpacket = 0;
510 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
511 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
512 for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
514 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
515 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
516 p_buf += tk->i_subpacket_size;
518 i_index = tk->i_subpacket_h * i +
519 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
521 p_block->i_dts = p_block->i_pts = i_pts;
522 tk->p_subpackets[i_index] = p_block;
526 if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
527 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
528 for( i = 0; i < tk->i_subpacket_h / 2; i++ )
530 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
531 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
532 p_buf += tk->i_coded_frame_size;
534 i_index = (i * 2 * tk->i_frame_size) /
535 tk->i_coded_frame_size + y;
537 p_block->i_dts = p_block->i_pts = i_pts;
538 tk->p_subpackets[i_index] = p_block;
542 while( tk->i_out_subpacket != tk->i_subpackets &&
543 tk->p_subpackets[tk->i_out_subpacket] )
546 if (tk->i_out_subpacket == 0)
548 p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
549 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
550 (int64_t)p_sys->i_pcr );
553 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
554 tk->p_subpackets[tk->i_out_subpacket] = 0;
556 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
557 es_out_Send( p_demux->out, tk->p_es, p_block );
559 tk->i_out_subpacket++;
562 if( tk->i_subpacket == tk->i_subpackets &&
563 tk->i_out_subpacket != tk->i_subpackets )
565 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
566 "this shouldn't happen" );
569 if( tk->i_subpacket == tk->i_subpackets )
572 tk->i_out_subpacket = 0;
578 if( p_sys->i_pcr < i_pts )
580 p_sys->i_pcr = i_pts;
581 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
582 (int64_t)p_sys->i_pcr );
585 if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
587 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
588 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
591 for( i = 0; i < i_sub; i++ )
593 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
594 block_t *p_block = block_New( p_demux, i_sub_size );
597 memcpy( p_block->p_buffer, p_sub, i_sub_size );
601 p_block->i_pts = ( i == 0 ? i_pts : 0 );
603 es_out_Send( p_demux->out, tk->p_es, p_block );
609 block_t *p_block = block_New( p_demux, i_size );
611 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
613 uint8_t *src = p_sys->buffer;
614 uint8_t *dst = p_block->p_buffer;
617 while( dst < &p_block->p_buffer[i_size- 1])
627 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
629 p_block->i_dts = p_block->i_pts = i_pts;
630 es_out_Send( p_demux->out, tk->p_es, p_block );
639 /*****************************************************************************
641 *****************************************************************************/
642 static int Control( demux_t *p_demux, int i_query, va_list args )
644 demux_sys_t *p_sys = p_demux->p_sys;
654 case DEMUX_GET_POSITION:
655 pf = (double*) va_arg( args, double* );
656 i64 = stream_Size( p_demux->s );
659 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
667 case DEMUX_SET_POSITION:
668 f = (double) va_arg( args, double );
669 i64 = stream_Size( p_demux->s );
671 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
673 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
676 pi64 = (int64_t*)va_arg( args, int64_t * );
677 if( p_sys->i_mux_rate > 0 )
679 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
686 case DEMUX_GET_LENGTH:
687 pi64 = (int64_t*)va_arg( args, int64_t * );
689 /* the commented following lines are fen's implementation, which doesn't seem to
690 * work for one reason or another -- FK */
691 /*if( p_sys->i_mux_rate > 0 )
693 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
696 if( p_sys->i_our_duration > 0 )
698 /* our stored duration is in ms, so... */
699 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
708 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
710 /* the core will crash if we provide NULL strings, so check
711 * every string first */
712 if( p_sys->psz_title )
713 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
714 if( p_sys->psz_artist )
715 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
716 if( p_sys->psz_copyright )
717 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
718 if( p_sys->psz_description )
719 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
731 /*****************************************************************************
733 *****************************************************************************/
734 static int HeaderRead( demux_t *p_demux )
736 demux_sys_t *p_sys = p_demux->p_sys;
737 uint8_t header[100]; /* FIXME */
746 /* Read the header */
747 if( stream_Read( p_demux->s, header, 10 ) < 10 )
751 i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
752 i_size = GetDWBE( &header[4] );
753 i_version = GetWBE( &header[8] );
755 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
756 (char*)&i_id, i_size, i_version );
758 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
760 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
763 i_skip = i_size - 10;
765 if( i_id == VLC_FOURCC('.','R','M','F') )
767 if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
768 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
769 GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
773 else if( i_id == VLC_FOURCC('P','R','O','P') )
777 if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
779 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
780 GetDWBE(&header[0]), GetDWBE(&header[4]) );
781 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
782 GetDWBE(&header[8]), GetDWBE(&header[12]) );
783 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) );
784 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) );
785 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) );
786 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) );
787 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) );
788 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) );
790 /* set the duration for export in control */
791 p_sys->i_our_duration = (int)GetDWBE(&header[20]);
793 i_flags = GetWBE(&header[38]);
794 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
796 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
797 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
798 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
801 else if( i_id == VLC_FOURCC('C','O','N','T') )
806 /* FIXME FIXME: should convert from whatever the character
807 * encoding of the input meta data is to UTF-8. */
809 stream_Read( p_demux->s, header, 2 );
810 if( ( i_len = GetWBE( header ) ) > 0 )
812 psz = malloc( i_len + 1 );
813 stream_Read( p_demux->s, psz, i_len );
816 msg_Dbg( p_demux, " - title=`%s'", psz );
818 asprintf( &p_sys->psz_title, psz );
824 stream_Read( p_demux->s, header, 2 );
825 if( ( i_len = GetWBE( header ) ) > 0 )
827 psz = malloc( i_len + 1 );
828 stream_Read( p_demux->s, psz, i_len );
831 msg_Dbg( p_demux, " - author=`%s'", psz );
833 asprintf( &p_sys->psz_artist, psz );
839 stream_Read( p_demux->s, header, 2 );
840 if( ( i_len = GetWBE( header ) ) > 0 )
842 psz = malloc( i_len + 1 );
843 stream_Read( p_demux->s, psz, i_len );
846 msg_Dbg( p_demux, " - copyright=`%s'", psz );
848 asprintf( &p_sys->psz_copyright, psz );
854 stream_Read( p_demux->s, header, 2 );
855 if( ( i_len = GetWBE( header ) ) > 0 )
857 psz = malloc( i_len + 1 );
858 stream_Read( p_demux->s, psz, i_len );
861 msg_Dbg( p_demux, " - comment=`%s'", psz );
863 asprintf( &p_sys->psz_description, psz );
869 else if( i_id == VLC_FOURCC('M','D','P','R') )
871 /* Media properties header */
876 if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
877 i_num = GetWBE( header );
878 msg_Dbg( p_demux, " - id=0x%x", i_num );
879 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
880 GetDWBE(&header[2]), GetDWBE(&header[6]) );
881 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
882 GetDWBE(&header[10]), GetDWBE(&header[14]) );
883 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&header[18]) );
884 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&header[22]) );
885 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&header[26]) );
889 stream_Read( p_demux->s, header, 1 );
890 if( ( i_len = header[0] ) > 0 )
892 psz = malloc( i_len + 1 );
893 stream_Read( p_demux->s, psz, i_len );
896 msg_Dbg( p_demux, " - name=`%s'", psz );
902 stream_Read( p_demux->s, header, 1 );
903 if( ( i_len = header[0] ) > 0 )
905 psz = malloc( i_len + 1 );
906 stream_Read( p_demux->s, psz, i_len );
909 msg_Dbg( p_demux, " - mime=`%s'", psz );
915 stream_Read( p_demux->s, header, 4 );
916 if( ( i_len = GetDWBE( header ) ) > 0 )
918 ReadCodecSpecificData( p_demux, i_len, i_num );
919 stream_Read( p_demux->s, NULL, i_len );
925 else if( i_id == VLC_FOURCC('D','A','T','A') )
927 stream_Read( p_demux->s, header, 8 );
929 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
930 p_sys->i_data_size = i_size;
931 p_sys->i_data_packets_count = GetDWBE( header );
932 p_sys->i_data_packets = 0;
933 p_sys->i_data_offset_next = GetDWBE( &header[4] );
935 msg_Dbg( p_demux, " - packets count=%d next=%u",
936 p_sys->i_data_packets_count,
937 (uint32_t)p_sys->i_data_offset_next );
939 /* we have finished the header */
945 msg_Dbg( p_demux, "unknown chunk" );
948 if( i_skip < 0 ) return VLC_EGENERIC;
949 stream_Read( p_demux->s, NULL, i_skip );
952 /* TODO read index if possible */
957 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
959 demux_sys_t *p_sys = p_demux->p_sys;
962 const uint8_t *p_peek;
964 msg_Dbg( p_demux, " - specific data len=%d", i_len );
965 if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
967 if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
969 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
970 p_peek[10], p_peek[11] ) );
971 fmt.video.i_width = GetWBE( &p_peek[12] );
972 fmt.video.i_height= GetWBE( &p_peek[14] );
975 fmt.p_extra = malloc( 8 );
976 ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
977 ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
979 msg_Dbg( p_demux, " - video 0x%08x 0x%08x",
980 ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
982 if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
983 GetDWBE( &p_peek[30] ) == 0x10003001 )
985 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
987 else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
988 GetDWBE( &p_peek[30] ) == 0x20100001 ||
989 GetDWBE( &p_peek[30] ) == 0x20200002 )
991 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
993 else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
995 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
997 else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
999 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1002 msg_Dbg( p_demux, " - video %4.4s %dx%d",
1003 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
1005 tk = malloc( sizeof( real_track_t ) );
1006 tk->i_out_subpacket = 0;
1007 tk->i_subpacket = 0;
1008 tk->i_subpackets = 0;
1009 tk->p_subpackets = NULL;
1014 tk->p_es = es_out_Add( p_demux->out, &fmt );
1016 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1018 else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
1020 int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
1021 int i_frame_size, i_subpacket_size;
1023 int i_version = GetWBE( &p_peek[4] ); /* [0..3] = '.','r','a',0xfd */
1024 msg_Dbg( p_demux, " - audio version=%d", i_version );
1026 p_peek += 6; /* 4 + version */
1027 es_format_Init( &fmt, AUDIO_ES, 0 );
1029 if( i_version == 3 )
1034 i_header_size = GetWBE( p_peek ); p_peek += 2; /* Size from now */
1035 p_peek += 10; /* Unknown */
1037 p_peek += 4; /* Data Size */
1040 i_len = *p_peek ; p_peek++;
1043 psz = malloc( i_len + 1 );
1044 memcpy( psz, p_peek, i_len );
1047 msg_Dbg( p_demux, " - title=`%s'", psz );
1049 asprintf( &p_sys->psz_title, psz );
1055 i_len = *p_peek ; p_peek++;
1058 psz = malloc( i_len + 1 );
1059 memcpy( psz, p_peek, i_len );
1062 msg_Dbg( p_demux, " - artist=`%s'", psz );
1064 asprintf( &p_sys->psz_artist, psz );
1070 i_len = *p_peek ; p_peek++;
1073 psz = malloc( i_len + 1 );
1074 memcpy( psz, p_peek, i_len );
1077 msg_Dbg( p_demux, " - Copyright=`%s'", psz );
1079 asprintf( &p_sys->psz_copyright, psz );
1085 i_len = *p_peek ; p_peek++;
1088 psz = malloc( i_len + 1 );
1089 memcpy( psz, p_peek, i_len );
1092 msg_Dbg( p_demux, " - Comment=`%s'", psz );
1094 asprintf( &p_sys->psz_description, psz );
1097 /* This might be unusefull */
1100 p_peek ++; /* Unknown */
1101 p_peek ++; /* FourCC length = 4 */
1102 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1105 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1106 fmt.audio.i_rate = 8000;
1108 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1109 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1111 else /* RMF version 4/5 */
1113 p_peek += 2; /* 00 00 */
1114 p_peek += 4; /* .ra4 or .ra5 */
1115 p_peek += 4; /* data size */
1116 p_peek += 2; /* version (4 or 5) */
1117 i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1118 i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1119 i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
1120 p_peek += 4; /* ?? */
1121 p_peek += 4; /* ?? */
1122 p_peek += 4; /* ?? */
1123 i_subpacket_h = GetWBE( p_peek ); p_peek += 2; /* 1 */
1124 i_frame_size = GetWBE( p_peek ); p_peek += 2; /* frame size */
1125 i_subpacket_size = GetWBE( p_peek ); p_peek += 2; /* subpacket_size */
1126 p_peek += 2; /* ?? */
1128 if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1130 fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */
1131 p_peek += 2; /* ?? */
1132 fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
1133 fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
1134 fmt.audio.i_blockalign = i_frame_size;
1136 if( i_version == 5 )
1138 memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4; /* genr */
1139 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1143 p_peek += p_peek[0] + 1; /* descr 1 */
1144 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
1145 p_peek += p_peek[0] + 1;
1148 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1149 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1151 p_peek += 3; /* ?? */
1152 if( i_version == 5 ) p_peek++;
1153 /* Extra Data then: DWord + byte[] */
1154 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1157 switch( fmt.i_codec )
1159 case VLC_FOURCC('1','4','_','4'):
1160 /* fmt.audio.i_blockalign = 0x14 */
1162 case VLC_FOURCC('l','p','c','J'):
1163 /* fmt.audio.i_blockalign = 0x14 */
1164 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1167 case VLC_FOURCC('2','8','_','8'):
1169 fmt.audio.i_blockalign = i_coded_frame_size;
1172 case VLC_FOURCC( 'd','n','e','t' ):
1173 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1176 case VLC_FOURCC( 'r','a','a','c' ):
1177 case VLC_FOURCC( 'r','a','c','p' ):
1178 if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1179 if( fmt.i_extra > 0 )
1181 fmt.p_extra = malloc( fmt.i_extra );
1182 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1185 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1188 case VLC_FOURCC('s','i','p','r'):
1189 fmt.audio.i_flavor = i_flavor;
1190 case VLC_FOURCC('c','o','o','k'):
1191 case VLC_FOURCC('a','t','r','c'):
1192 if( !memcmp( p_genr, "genr", 4 ) )
1193 fmt.audio.i_blockalign = i_subpacket_size;
1195 fmt.audio.i_blockalign = i_coded_frame_size;
1196 if( !fmt.i_extra ) break;
1197 fmt.p_extra = malloc( fmt.i_extra );
1198 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1201 case VLC_FOURCC('r','a','l','f'):
1202 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1203 (char*)&fmt.i_codec );
1207 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1208 (char*)&fmt.i_codec );
1212 if( fmt.i_codec != 0 )
1216 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1218 tk = malloc( sizeof( real_track_t ) );
1224 tk->i_subpacket_h = i_subpacket_h;
1225 tk->i_subpacket_size = i_subpacket_size;
1226 tk->i_coded_frame_size = i_coded_frame_size;
1227 tk->i_frame_size = i_frame_size;
1229 tk->i_out_subpacket = 0;
1230 tk->i_subpacket = 0;
1231 tk->i_subpackets = 0;
1232 tk->p_subpackets = NULL;
1233 if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
1234 || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1237 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1239 malloc( tk->i_subpackets * sizeof(block_t *) );
1241 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1244 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1246 malloc( tk->i_subpackets * sizeof(block_t *) );
1249 for( i = 0; i < tk->i_subpackets; i++ ) tk->p_subpackets[i] = NULL;
1251 tk->p_es = es_out_Add( p_demux->out, &fmt );
1253 TAB_APPEND( p_sys->i_track, p_sys->track, tk );