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 // Map flavour to bytes per second
128 static int sipr_fl2bps[4] = {813, 1062, 625, 2000}; // 6.5, 8.5, 5, 16 kbit per second
130 /*****************************************************************************
132 *****************************************************************************/
133 static int Open( vlc_object_t *p_this )
135 demux_t *p_demux = (demux_t*)p_this;
138 const uint8_t *p_peek;
140 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
141 if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
143 /* Fill p_demux field */
144 p_demux->pf_demux = Demux;
145 p_demux->pf_control = Control;
146 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
147 memset( p_sys, 0, sizeof( demux_sys_t ) );
148 p_sys->i_data_offset = 0;
154 /* Parse the headers */
155 if( HeaderRead( p_demux ) )
158 msg_Err( p_demux, "invalid header" );
159 for( i = 0; i < p_sys->i_track; i++ )
161 real_track_t *tk = p_sys->track[i];
165 es_out_Del( p_demux->out, tk->p_es );
169 if( p_sys->i_track > 0 )
171 free( p_sys->track );
180 /*****************************************************************************
182 *****************************************************************************/
183 static void Close( vlc_object_t *p_this )
185 demux_t *p_demux = (demux_t*)p_this;
186 demux_sys_t *p_sys = p_demux->p_sys;
189 for( i = 0; i < p_sys->i_track; i++ )
191 real_track_t *tk = p_sys->track[i];
192 int j = tk->i_subpackets;
194 if( tk->p_frame ) block_Release( tk->p_frame );
195 es_format_Clean( &tk->fmt );
199 if( tk->p_subpackets[ j ] )
200 block_Release( tk->p_subpackets[ j ] );
202 if( tk->i_subpackets ) free( tk->p_subpackets );
207 if( p_sys->psz_title ) free( p_sys->psz_title );
208 if( p_sys->psz_artist ) free( p_sys->psz_artist );
209 if( p_sys->psz_copyright ) free( p_sys->psz_copyright );
210 if( p_sys->psz_description ) free( p_sys->psz_description );
212 if( p_sys->i_track > 0 ) free( p_sys->track );
217 /*****************************************************************************
219 *****************************************************************************/
220 static int Demux( demux_t *p_demux )
222 demux_sys_t *p_sys = p_demux->p_sys;
224 int i_size, i_id, i_flags, i;
226 real_track_t *tk = NULL;
227 vlc_bool_t b_selected;
229 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
230 p_sys->i_data_packets_count )
232 if( stream_Read( p_demux->s, header, 18 ) < 18 )
236 if( strncmp( (char *)header, "DATA", 4 ) )
240 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
241 p_sys->i_data_size = GetDWBE( &header[4] );
242 p_sys->i_data_packets_count = GetDWBE( &header[10] );
243 p_sys->i_data_packets = 0;
244 p_sys->i_data_offset_next = GetDWBE( &header[14] );
246 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
247 p_sys->i_data_packets_count,
248 (uint32_t)p_sys->i_data_offset_next );
251 if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
253 i_size = GetWBE( &header[2] ) - 12;
254 i_id = GetWBE( &header[4] );
255 i_pts = 1000 * GetDWBE( &header[6] );
256 i_pts += 1000; /* Avoid 0 pts */
257 i_flags= header[11]; /* flags 0x02 -> keyframe */
260 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
261 p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
264 p_sys->i_data_packets++;
266 stream_Read( p_demux->s, p_sys->buffer, i_size );
268 for( i = 0; i < p_sys->i_track; i++ )
270 if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
275 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
278 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
280 if( tk->fmt.i_cat == VIDEO_ES && b_selected )
282 uint8_t *p = p_sys->buffer;
284 while( p < &p_sys->buffer[i_size - 2] )
293 if( (h&0xc0) == 0x40 )
297 i_len = &p_sys->buffer[i_size] - p;
303 i_subseq = (*p++)&0x7f;
305 i_len = (p[0] << 8)|p[1]; p += 2;
306 if( (i_len&0xc000) == 0 )
309 i_len |= (p[0] << 8)|p[1]; p += 2;
317 i_offset = (p[0] << 8)|p[1]; p += 2;
318 if( (i_offset&0xc000) == 0 )
321 i_offset |= (p[0] << 8)|p[1]; p += 2;
322 i_offset &= 0x3fffffff;
331 i_copy = i_len - i_offset;
332 if( i_copy > &p_sys->buffer[i_size] - p )
334 i_copy = &p_sys->buffer[i_size] - p;
336 else if( i_copy < 0 )
341 msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d",
342 i_len, i_offset, i_copy, i_subseq, i_seqnum );
344 if( (h&0xc0) == 0x80 )
346 /* last fragment -> fixes */
348 i_offset = i_len - i_copy;
349 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
354 ( tk->p_frame->i_dts != i_pts ||
355 tk->i_frame != i_len ) )
357 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
359 if( p_sys->i_pcr < tk->p_frame->i_dts )
361 p_sys->i_pcr = tk->p_frame->i_dts;
363 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
364 (int64_t)p_sys->i_pcr );
366 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
372 if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
380 if( tk->p_frame == NULL )
382 msg_Dbg( p_demux, "new frame size=%d", i_len );
384 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
388 memset( &tk->p_frame->p_buffer[8], 0, i_len );
389 tk->p_frame->i_dts = i_pts;
390 tk->p_frame->i_pts = i_pts;
392 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
393 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */
396 if( i_offset < tk->i_frame)
398 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
400 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
401 i_ck, i_offset, i_copy );
403 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
405 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
410 if( (h&0xc0) != 0x80 )
419 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
421 if( (h&0xc0) == 0x80 )
425 i_offset = i_len - i_offset;
427 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
428 memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
431 if( p_sys->i_pcr < tk->p_frame->i_dts )
433 p_sys->i_pcr = tk->p_frame->i_dts;
434 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
435 (int64_t)p_sys->i_pcr );
437 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
445 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
446 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
450 if( (h&0xc0) != 0x00 )
455 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
459 p_frame->i_dts = i_pts;
460 p_frame->i_pts = i_pts;
462 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
463 ((uint32_t*)p_frame->p_buffer)[1] = 1;
464 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
465 memcpy( &p_frame->p_buffer[8], p, i_copy );
469 if( p_sys->i_pcr < p_frame->i_dts )
471 p_sys->i_pcr = p_frame->i_dts;
472 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
473 (int64_t)p_sys->i_pcr );
475 es_out_Send( p_demux->out, tk->p_es, p_frame );
481 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
485 memset( &tk->p_frame->p_buffer[8], 0, i_len );
486 tk->p_frame->i_dts = i_pts;
487 tk->p_frame->i_pts = i_pts;
489 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
490 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */
491 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
492 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
499 else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
502 if( p_sys->i_pcr < i_pts )
504 p_sys->i_pcr = i_pts;
505 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
506 (int64_t)p_sys->i_pcr );
509 if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
511 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
512 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
515 for( i = 0; i < i_sub; i++ )
517 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
518 block_t *p_block = block_New( p_demux, i_sub_size );
521 memcpy( p_block->p_buffer, p_sub, i_sub_size );
525 p_block->i_pts = ( i == 0 ? i_pts : 0 );
527 es_out_Send( p_demux->out, tk->p_es, p_block );
531 else if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
532 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
533 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
534 tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
536 uint8_t *p_buf = p_sys->buffer;
537 int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
541 if( i_flags & 2 ) y = tk->i_subpacket = 0;
543 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
544 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
545 for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
547 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
548 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
549 p_buf += tk->i_subpacket_size;
551 i_index = tk->i_subpacket_h * i +
552 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
554 p_block->i_dts = p_block->i_pts = i_pts;
555 tk->p_subpackets[i_index] = p_block;
559 if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
560 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
561 for( i = 0; i < tk->i_subpacket_h / 2; i++ )
563 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
564 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
565 p_buf += tk->i_coded_frame_size;
567 i_index = (i * 2 * tk->i_frame_size) /
568 tk->i_coded_frame_size + y;
570 p_block->i_dts = p_block->i_pts = i_pts;
571 tk->p_subpackets[i_index] = p_block;
575 while( tk->i_out_subpacket != tk->i_subpackets &&
576 tk->p_subpackets[tk->i_out_subpacket] )
578 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
579 tk->p_subpackets[tk->i_out_subpacket] = 0;
581 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
582 es_out_Send( p_demux->out, tk->p_es, p_block );
584 tk->i_out_subpacket++;
587 if( tk->i_subpacket == tk->i_subpackets &&
588 tk->i_out_subpacket != tk->i_subpackets )
590 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
591 "this shouldn't happen" );
594 if( tk->i_subpacket == tk->i_subpackets )
597 tk->i_out_subpacket = 0;
602 block_t *p_block = block_New( p_demux, i_size );
604 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
606 uint8_t *src = p_sys->buffer;
607 uint8_t *dst = p_block->p_buffer;
610 while( dst < &p_block->p_buffer[i_size- 1])
620 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
622 p_block->i_dts = p_block->i_pts = i_pts;
623 es_out_Send( p_demux->out, tk->p_es, p_block );
631 /*****************************************************************************
633 *****************************************************************************/
634 static int Control( demux_t *p_demux, int i_query, va_list args )
636 demux_sys_t *p_sys = p_demux->p_sys;
646 case DEMUX_GET_POSITION:
647 pf = (double*) va_arg( args, double* );
648 i64 = stream_Size( p_demux->s );
651 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
659 case DEMUX_SET_POSITION:
660 f = (double) va_arg( args, double );
661 i64 = stream_Size( p_demux->s );
663 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
665 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
668 pi64 = (int64_t*)va_arg( args, int64_t * );
669 if( p_sys->i_mux_rate > 0 )
671 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
678 case DEMUX_GET_LENGTH:
679 pi64 = (int64_t*)va_arg( args, int64_t * );
681 /* the commented following lines are fen's implementation, which doesn't seem to
682 * work for one reason or another -- FK */
683 /*if( p_sys->i_mux_rate > 0 )
685 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
688 if( p_sys->i_our_duration > 0 )
690 /* our stored duration is in ms, so... */
691 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
700 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
702 /* the core will crash if we provide NULL strings, so check
703 * every string first */
704 if( p_sys->psz_title )
705 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
706 if( p_sys->psz_artist )
707 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
708 if( p_sys->psz_copyright )
709 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
710 if( p_sys->psz_description )
711 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
723 /*****************************************************************************
725 *****************************************************************************/
726 static int HeaderRead( demux_t *p_demux )
728 demux_sys_t *p_sys = p_demux->p_sys;
729 uint8_t header[100]; /* FIXME */
738 /* Read the header */
739 if( stream_Read( p_demux->s, header, 10 ) < 10 )
743 i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
744 i_size = GetDWBE( &header[4] );
745 i_version = GetWBE( &header[8] );
747 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
748 (char*)&i_id, i_size, i_version );
750 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
752 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
755 i_skip = i_size - 10;
757 if( i_id == VLC_FOURCC('.','R','M','F') )
759 if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
760 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
761 GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
765 else if( i_id == VLC_FOURCC('P','R','O','P') )
769 if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
771 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
772 GetDWBE(&header[0]), GetDWBE(&header[4]) );
773 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
774 GetDWBE(&header[8]), GetDWBE(&header[12]) );
775 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) );
776 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) );
777 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) );
778 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) );
779 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) );
780 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) );
782 /* set the duration for export in control */
783 p_sys->i_our_duration = (int)GetDWBE(&header[20]);
785 i_flags = GetWBE(&header[38]);
786 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
788 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
789 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
790 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
793 else if( i_id == VLC_FOURCC('C','O','N','T') )
798 /* FIXME FIXME: should convert from whatever the character
799 * encoding of the input meta data is to UTF-8. */
801 stream_Read( p_demux->s, header, 2 );
802 if( ( i_len = GetWBE( header ) ) > 0 )
804 psz = malloc( i_len + 1 );
805 stream_Read( p_demux->s, psz, i_len );
808 msg_Dbg( p_demux, " - title=`%s'", psz );
810 asprintf( &p_sys->psz_title, psz );
816 stream_Read( p_demux->s, header, 2 );
817 if( ( i_len = GetWBE( header ) ) > 0 )
819 psz = malloc( i_len + 1 );
820 stream_Read( p_demux->s, psz, i_len );
823 msg_Dbg( p_demux, " - author=`%s'", psz );
825 asprintf( &p_sys->psz_artist, psz );
831 stream_Read( p_demux->s, header, 2 );
832 if( ( i_len = GetWBE( header ) ) > 0 )
834 psz = malloc( i_len + 1 );
835 stream_Read( p_demux->s, psz, i_len );
838 msg_Dbg( p_demux, " - copyright=`%s'", psz );
840 asprintf( &p_sys->psz_copyright, psz );
846 stream_Read( p_demux->s, header, 2 );
847 if( ( i_len = GetWBE( header ) ) > 0 )
849 psz = malloc( i_len + 1 );
850 stream_Read( p_demux->s, psz, i_len );
853 msg_Dbg( p_demux, " - comment=`%s'", psz );
855 asprintf( &p_sys->psz_description, psz );
861 else if( i_id == VLC_FOURCC('M','D','P','R') )
863 /* Media properties header */
868 if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
869 i_num = GetWBE( header );
870 msg_Dbg( p_demux, " - id=0x%x", i_num );
871 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
872 GetDWBE(&header[2]), GetDWBE(&header[6]) );
873 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
874 GetDWBE(&header[10]), GetDWBE(&header[14]) );
875 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&header[18]) );
876 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&header[22]) );
877 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&header[26]) );
881 stream_Read( p_demux->s, header, 1 );
882 if( ( i_len = header[0] ) > 0 )
884 psz = malloc( i_len + 1 );
885 stream_Read( p_demux->s, psz, i_len );
888 msg_Dbg( p_demux, " - name=`%s'", psz );
894 stream_Read( p_demux->s, header, 1 );
895 if( ( i_len = header[0] ) > 0 )
897 psz = malloc( i_len + 1 );
898 stream_Read( p_demux->s, psz, i_len );
901 msg_Dbg( p_demux, " - mime=`%s'", psz );
907 stream_Read( p_demux->s, header, 4 );
908 if( ( i_len = GetDWBE( header ) ) > 0 )
910 ReadCodecSpecificData( p_demux, i_len, i_num );
911 stream_Read( p_demux->s, NULL, i_len );
917 else if( i_id == VLC_FOURCC('D','A','T','A') )
919 stream_Read( p_demux->s, header, 8 );
921 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
922 p_sys->i_data_size = i_size;
923 p_sys->i_data_packets_count = GetDWBE( header );
924 p_sys->i_data_packets = 0;
925 p_sys->i_data_offset_next = GetDWBE( &header[4] );
927 msg_Dbg( p_demux, " - packets count=%d next=%u",
928 p_sys->i_data_packets_count,
929 (uint32_t)p_sys->i_data_offset_next );
931 /* we have finished the header */
937 msg_Dbg( p_demux, "unknown chunk" );
940 if( i_skip < 0 ) return VLC_EGENERIC;
941 stream_Read( p_demux->s, NULL, i_skip );
944 /* TODO read index if possible */
949 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
951 demux_sys_t *p_sys = p_demux->p_sys;
954 const uint8_t *p_peek;
956 msg_Dbg( p_demux, " - specific data len=%d", i_len );
957 if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
959 if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
961 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
962 p_peek[10], p_peek[11] ) );
963 fmt.video.i_width = GetWBE( &p_peek[12] );
964 fmt.video.i_height= GetWBE( &p_peek[14] );
967 fmt.p_extra = malloc( 8 );
968 ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
969 ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
971 msg_Dbg( p_demux, " - video 0x%08x 0x%08x",
972 ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
974 if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
975 GetDWBE( &p_peek[30] ) == 0x10003001 )
977 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
979 else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
980 GetDWBE( &p_peek[30] ) == 0x20100001 ||
981 GetDWBE( &p_peek[30] ) == 0x20200002 )
983 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
985 else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
987 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
989 else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
991 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
994 msg_Dbg( p_demux, " - video %4.4s %dx%d",
995 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
997 tk = malloc( sizeof( real_track_t ) );
998 tk->i_out_subpacket = 0;
1000 tk->i_subpackets = 0;
1001 tk->p_subpackets = NULL;
1006 tk->p_es = es_out_Add( p_demux->out, &fmt );
1008 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1010 else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
1012 int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
1013 int i_frame_size, i_subpacket_size;
1014 int i_version = GetWBE( &p_peek[4] ); /* [0..3] = '.','r','a',0xfd */
1015 msg_Dbg( p_demux, " - audio version=%d", i_version );
1017 p_peek += 6; /* 4 + version */
1018 es_format_Init( &fmt, AUDIO_ES, 0 );
1020 if( i_version == 3 )
1025 i_header_size = GetWBE( p_peek ); p_peek += 2; /* Size from now */
1026 p_peek += 10; /* Unknown */
1028 p_peek += 4; /* Data Size */
1031 i_len = *p_peek ; p_peek++;
1034 psz = malloc( i_len + 1 );
1035 memcpy( psz, p_peek, i_len );
1038 msg_Dbg( p_demux, " - title=`%s'", psz );
1040 asprintf( &p_sys->psz_title, psz );
1046 i_len = *p_peek ; p_peek++;
1049 psz = malloc( i_len + 1 );
1050 memcpy( psz, p_peek, i_len );
1053 msg_Dbg( p_demux, " - artist=`%s'", psz );
1055 asprintf( &p_sys->psz_artist, psz );
1061 i_len = *p_peek ; p_peek++;
1064 psz = malloc( i_len + 1 );
1065 memcpy( psz, p_peek, i_len );
1068 msg_Dbg( p_demux, " - Copyright=`%s'", psz );
1070 asprintf( &p_sys->psz_copyright, psz );
1076 i_len = *p_peek ; p_peek++;
1079 psz = malloc( i_len + 1 );
1080 memcpy( psz, p_peek, i_len );
1083 msg_Dbg( p_demux, " - Comment=`%s'", psz );
1085 asprintf( &p_sys->psz_description, psz );
1088 /* This might be unusefull */
1091 p_peek ++; /* Unknown */
1092 p_peek ++; /* FourCC length = 4 */
1093 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1096 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1097 fmt.audio.i_rate = 8000;
1099 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1100 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1105 p_peek += 2; /* 00 00 */
1106 p_peek += 4; /* .ra4 or .ra5 */
1107 p_peek += 4; /* data size */
1108 p_peek += 2; /* version (4 or 5) */
1109 i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1110 i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1111 i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
1112 p_peek += 4; /* ?? */
1113 p_peek += 4; /* ?? */
1114 p_peek += 4; /* ?? */
1115 i_subpacket_h = GetWBE( p_peek ); p_peek += 2; /* 1 */
1116 i_frame_size = GetWBE( p_peek ); p_peek += 2; /* frame size */
1117 i_subpacket_size = GetWBE( p_peek ); p_peek += 2; /* subpacket_size */
1118 p_peek += 2; /* ?? */
1120 if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1122 fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */
1123 p_peek += 2; /* ?? */
1124 fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
1125 fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
1126 fmt.audio.i_blockalign = i_frame_size;
1128 if( i_version == 5 )
1130 p_peek += 4; /* genr */
1131 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1135 p_peek += p_peek[0] + 1; /* descr 1 */
1136 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
1137 p_peek += p_peek[0] + 1;
1140 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1141 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1143 p_peek += 3; /* ?? */
1144 if( i_version == 5 ) p_peek++;
1145 /* Extra Data then: DWord + byte[] */
1146 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1149 switch( fmt.i_codec )
1151 case VLC_FOURCC('1','4','_','4'):
1153 case VLC_FOURCC('l','p','c','J'):
1154 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1157 case VLC_FOURCC('2','8','_','8'):
1159 fmt.audio.i_blockalign = i_coded_frame_size;
1162 case VLC_FOURCC( 'd','n','e','t' ):
1163 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1166 case VLC_FOURCC( 'r','a','a','c' ):
1167 case VLC_FOURCC( 'r','a','c','p' ):
1168 if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1169 if( fmt.i_extra > 0 )
1171 fmt.p_extra = malloc( fmt.i_extra );
1172 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1175 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1178 case VLC_FOURCC('c','o','o','k'):
1179 case VLC_FOURCC('a','t','r','c'):
1180 fmt.audio.i_blockalign = i_subpacket_size;
1181 if( !fmt.i_extra ) break;
1182 fmt.p_extra = malloc( fmt.i_extra );
1183 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1186 case VLC_FOURCC('s','i','p','r'):
1188 fmt.audio.i_blockalign = i_coded_frame_size;
1189 fmt.audio.i_bitspersample = sipr_fl2bps[ i_flavor ];
1192 case VLC_FOURCC('r','a','l','f'):
1193 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1194 (char*)&fmt.i_codec );
1198 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1199 (char*)&fmt.i_codec );
1203 if( fmt.i_codec != 0 )
1207 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1209 tk = malloc( sizeof( real_track_t ) );
1215 tk->i_subpacket_h = i_subpacket_h;
1216 tk->i_subpacket_size = i_subpacket_size;
1217 tk->i_coded_frame_size = i_coded_frame_size;
1218 tk->i_frame_size = i_frame_size;
1220 tk->i_out_subpacket = 0;
1221 tk->i_subpacket = 0;
1222 tk->i_subpackets = 0;
1223 tk->p_subpackets = NULL;
1224 if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
1225 || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1228 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1230 malloc( tk->i_subpackets * sizeof(block_t *) );
1232 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1235 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1237 malloc( tk->i_subpackets * sizeof(block_t *) );
1240 for( i = 0; i < tk->i_subpackets; i++ ) tk->p_subpackets[i] = NULL;
1242 tk->p_es = es_out_Add( p_demux->out, &fmt );
1244 TAB_APPEND( p_sys->i_track, p_sys->track, tk );