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 *****************************************************************************/
55 #include <vlc_demux.h>
56 #include <vlc_charset.h>
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Open ( vlc_object_t * );
63 static void Close ( vlc_object_t * );
66 set_description( _("Real demuxer" ) );
67 set_capability( "demux2", 15 );
68 set_category( CAT_INPUT );
69 set_subcategory( SUBCAT_INPUT_DEMUX );
70 set_callbacks( Open, Close );
71 add_shortcut( "real" );
75 /*****************************************************************************
77 *****************************************************************************/
91 int i_coded_frame_size;
96 block_t **p_subpackets;
103 int64_t i_data_offset;
105 uint32_t i_data_packets_count;
106 uint32_t i_data_packets;
107 int64_t i_data_offset_next;
115 char* psz_description;
118 real_track_t **track;
120 uint8_t buffer[65536];
125 static int Demux( demux_t *p_demux );
126 static int Control( demux_t *p_demux, int i_query, va_list args );
128 static int HeaderRead( demux_t *p_demux );
129 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
131 /*****************************************************************************
133 *****************************************************************************/
134 static int Open( vlc_object_t *p_this )
136 demux_t *p_demux = (demux_t*)p_this;
139 const uint8_t *p_peek;
141 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
142 if( !memcmp( p_peek, ".ra", 3 ) )
144 msg_Warn( p_demux, ".ra files unsuported" );
147 if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
149 /* Fill p_demux field */
150 p_demux->pf_demux = Demux;
151 p_demux->pf_control = Control;
152 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
153 memset( p_sys, 0, sizeof( demux_sys_t ) );
154 p_sys->i_data_offset = 0;
160 /* Parse the headers */
161 if( HeaderRead( p_demux ) )
164 msg_Err( p_demux, "invalid header" );
165 for( i = 0; i < p_sys->i_track; i++ )
167 real_track_t *tk = p_sys->track[i];
171 es_out_Del( p_demux->out, tk->p_es );
175 if( p_sys->i_track > 0 )
177 free( p_sys->track );
186 /*****************************************************************************
188 *****************************************************************************/
189 static void Close( vlc_object_t *p_this )
191 demux_t *p_demux = (demux_t*)p_this;
192 demux_sys_t *p_sys = p_demux->p_sys;
195 for( i = 0; i < p_sys->i_track; i++ )
197 real_track_t *tk = p_sys->track[i];
198 int j = tk->i_subpackets;
200 if( tk->p_frame ) block_Release( tk->p_frame );
201 es_format_Clean( &tk->fmt );
205 if( tk->p_subpackets[ j ] )
206 block_Release( tk->p_subpackets[ j ] );
208 if( tk->i_subpackets ) free( tk->p_subpackets );
213 if( p_sys->psz_title ) free( p_sys->psz_title );
214 if( p_sys->psz_artist ) free( p_sys->psz_artist );
215 if( p_sys->psz_copyright ) free( p_sys->psz_copyright );
216 if( p_sys->psz_description ) free( p_sys->psz_description );
218 if( p_sys->i_track > 0 ) free( p_sys->track );
223 /*****************************************************************************
225 *****************************************************************************/
226 static int Demux( demux_t *p_demux )
228 demux_sys_t *p_sys = p_demux->p_sys;
230 int i_size, i_id, i_flags, i;
232 real_track_t *tk = NULL;
233 vlc_bool_t b_selected;
235 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
236 p_sys->i_data_packets_count )
238 if( stream_Read( p_demux->s, header, 18 ) < 18 )
242 if( strncmp( (char *)header, "DATA", 4 ) )
246 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
247 p_sys->i_data_size = GetDWBE( &header[4] );
248 p_sys->i_data_packets_count = GetDWBE( &header[10] );
249 p_sys->i_data_packets = 0;
250 p_sys->i_data_offset_next = GetDWBE( &header[14] );
252 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
253 p_sys->i_data_packets_count,
254 (uint32_t)p_sys->i_data_offset_next );
257 if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
259 i_size = GetWBE( &header[2] ) - 12;
260 i_id = GetWBE( &header[4] );
261 i_pts = 1000 * GetDWBE( &header[6] );
262 i_pts += 1000; /* Avoid 0 pts */
263 i_flags= header[11]; /* flags 0x02 -> keyframe */
266 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
267 p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
270 p_sys->i_data_packets++;
272 stream_Read( p_demux->s, p_sys->buffer, i_size );
274 for( i = 0; i < p_sys->i_track; i++ )
276 if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
281 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
284 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
286 if( tk->fmt.i_cat == VIDEO_ES && b_selected )
288 uint8_t *p = p_sys->buffer;
290 while( p < &p_sys->buffer[i_size - 2] )
299 if( (h&0xc0) == 0x40 )
303 i_len = &p_sys->buffer[i_size] - p;
309 i_subseq = (*p++)&0x7f;
311 i_len = (p[0] << 8)|p[1]; p += 2;
312 if( (i_len&0xc000) == 0 )
315 i_len |= (p[0] << 8)|p[1]; p += 2;
323 i_offset = (p[0] << 8)|p[1]; p += 2;
324 if( (i_offset&0xc000) == 0 )
327 i_offset |= (p[0] << 8)|p[1]; p += 2;
328 i_offset &= 0x3fffffff;
337 i_copy = i_len - i_offset;
338 if( i_copy > &p_sys->buffer[i_size] - p )
340 i_copy = &p_sys->buffer[i_size] - p;
342 else if( i_copy < 0 )
347 msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d",
348 i_len, i_offset, i_copy, i_subseq, i_seqnum );
350 if( (h&0xc0) == 0x80 )
352 /* last fragment -> fixes */
354 i_offset = i_len - i_copy;
355 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
360 ( tk->p_frame->i_dts != i_pts ||
361 tk->i_frame != i_len ) )
363 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
365 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
371 if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
379 if( tk->p_frame == NULL )
381 msg_Dbg( p_demux, "new frame size=%d", i_len );
383 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
387 memset( &tk->p_frame->p_buffer[8], 0, i_len );
388 tk->p_frame->i_dts = i_pts;
389 tk->p_frame->i_pts = i_pts;
391 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
392 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */
395 if( i_offset < tk->i_frame)
397 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
399 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
400 i_ck, i_offset, i_copy );
402 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +0 ] = 1;
403 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +1 ] = i_offset;
406 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
411 if( (h&0xc0) != 0x80 )
420 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
422 if( (h&0xc0) == 0x80 )
426 i_offset = i_len - i_offset;
428 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
429 memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
432 if( p_sys->i_pcr < tk->p_frame->i_dts )
434 p_sys->i_pcr = tk->p_frame->i_dts;
435 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
436 (int64_t)p_sys->i_pcr );
438 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
446 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
447 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
451 if( (h&0xc0) != 0x00 )
456 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
460 p_frame->i_dts = i_pts;
461 p_frame->i_pts = i_pts;
463 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
464 ((uint32_t*)p_frame->p_buffer)[1] = 1;
465 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
466 memcpy( &p_frame->p_buffer[8], p, i_copy );
470 if( p_sys->i_pcr < p_frame->i_dts )
472 p_sys->i_pcr = p_frame->i_dts;
473 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
474 (int64_t)p_sys->i_pcr );
476 es_out_Send( p_demux->out, tk->p_es, p_frame );
482 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
486 memset( &tk->p_frame->p_buffer[8], 0, i_len );
487 tk->p_frame->i_dts = i_pts;
488 tk->p_frame->i_pts = i_pts;
490 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
491 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */
492 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
493 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
500 else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
502 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
503 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
504 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
505 tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
507 uint8_t *p_buf = p_sys->buffer;
508 int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
512 if( i_flags & 2 ) y = tk->i_subpacket = 0;
514 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
515 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
516 for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
518 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
519 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
520 p_buf += tk->i_subpacket_size;
522 i_index = tk->i_subpacket_h * i +
523 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
525 p_block->i_dts = p_block->i_pts = i_pts;
526 tk->p_subpackets[i_index] = p_block;
530 if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
531 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
532 for( i = 0; i < tk->i_subpacket_h / 2; i++ )
534 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
535 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
536 p_buf += tk->i_coded_frame_size;
538 i_index = (i * 2 * tk->i_frame_size) /
539 tk->i_coded_frame_size + y;
541 p_block->i_dts = p_block->i_pts = i_pts;
542 tk->p_subpackets[i_index] = p_block;
546 while( tk->i_out_subpacket != tk->i_subpackets &&
547 tk->p_subpackets[tk->i_out_subpacket] )
550 if (tk->i_out_subpacket == 0)
552 p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
553 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
554 (int64_t)p_sys->i_pcr );
557 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
558 tk->p_subpackets[tk->i_out_subpacket] = 0;
560 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
561 es_out_Send( p_demux->out, tk->p_es, p_block );
563 tk->i_out_subpacket++;
566 if( tk->i_subpacket == tk->i_subpackets &&
567 tk->i_out_subpacket != tk->i_subpackets )
569 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
570 "this shouldn't happen" );
573 if( tk->i_subpacket == tk->i_subpackets )
576 tk->i_out_subpacket = 0;
582 if( p_sys->i_pcr < i_pts )
584 p_sys->i_pcr = i_pts;
585 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
586 (int64_t)p_sys->i_pcr );
589 if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
591 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
592 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
595 for( i = 0; i < i_sub; i++ )
597 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
598 block_t *p_block = block_New( p_demux, i_sub_size );
601 memcpy( p_block->p_buffer, p_sub, i_sub_size );
605 p_block->i_pts = ( i == 0 ? i_pts : 0 );
607 es_out_Send( p_demux->out, tk->p_es, p_block );
613 block_t *p_block = block_New( p_demux, i_size );
615 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
617 uint8_t *src = p_sys->buffer;
618 uint8_t *dst = p_block->p_buffer;
621 while( dst < &p_block->p_buffer[i_size- 1])
631 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
633 p_block->i_dts = p_block->i_pts = i_pts;
634 es_out_Send( p_demux->out, tk->p_es, p_block );
643 /*****************************************************************************
645 *****************************************************************************/
646 static int Control( demux_t *p_demux, int i_query, va_list args )
648 demux_sys_t *p_sys = p_demux->p_sys;
658 case DEMUX_GET_POSITION:
659 pf = (double*) va_arg( args, double* );
660 i64 = stream_Size( p_demux->s );
663 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
671 case DEMUX_SET_POSITION:
672 f = (double) va_arg( args, double );
673 i64 = stream_Size( p_demux->s );
675 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
677 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
680 pi64 = (int64_t*)va_arg( args, int64_t * );
681 if( p_sys->i_mux_rate > 0 )
683 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
690 case DEMUX_GET_LENGTH:
691 pi64 = (int64_t*)va_arg( args, int64_t * );
693 /* the commented following lines are fen's implementation, which doesn't seem to
694 * work for one reason or another -- FK */
695 /*if( p_sys->i_mux_rate > 0 )
697 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
700 if( p_sys->i_our_duration > 0 )
702 /* our stored duration is in ms, so... */
703 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
712 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
714 /* the core will crash if we provide NULL strings, so check
715 * every string first */
716 if( p_sys->psz_title )
717 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
718 if( p_sys->psz_artist )
719 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
720 if( p_sys->psz_copyright )
721 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
722 if( p_sys->psz_description )
723 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
735 /*****************************************************************************
737 *****************************************************************************/
738 static int HeaderRead( demux_t *p_demux )
740 demux_sys_t *p_sys = p_demux->p_sys;
741 uint8_t header[100]; /* FIXME */
750 /* Read the header */
751 if( stream_Read( p_demux->s, header, 10 ) < 10 )
755 i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
756 i_size = GetDWBE( &header[4] );
757 i_version = GetWBE( &header[8] );
759 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
760 (char*)&i_id, i_size, i_version );
762 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
764 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
767 i_skip = i_size - 10;
769 if( i_id == VLC_FOURCC('.','R','M','F') )
771 if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
772 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
773 GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
777 else if( i_id == VLC_FOURCC('P','R','O','P') )
781 if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
783 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
784 GetDWBE(&header[0]), GetDWBE(&header[4]) );
785 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
786 GetDWBE(&header[8]), GetDWBE(&header[12]) );
787 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) );
788 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) );
789 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) );
790 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) );
791 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) );
792 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) );
794 /* set the duration for export in control */
795 p_sys->i_our_duration = (int)GetDWBE(&header[20]);
797 i_flags = GetWBE(&header[38]);
798 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
800 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
801 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
802 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
805 else if( i_id == VLC_FOURCC('C','O','N','T') )
810 /* FIXME FIXME: should convert from whatever the character
811 * encoding of the input meta data is to UTF-8. */
813 stream_Read( p_demux->s, header, 2 );
814 if( ( i_len = GetWBE( header ) ) > 0 )
816 psz = malloc( i_len + 1 );
817 stream_Read( p_demux->s, psz, i_len );
821 msg_Dbg( p_demux, " - title=`%s'", psz );
822 p_sys->psz_title = psz;
827 stream_Read( p_demux->s, header, 2 );
828 if( ( i_len = GetWBE( header ) ) > 0 )
830 psz = malloc( i_len + 1 );
831 stream_Read( p_demux->s, psz, i_len );
835 msg_Dbg( p_demux, " - author=`%s'", psz );
836 p_sys->psz_artist = psz;
841 stream_Read( p_demux->s, header, 2 );
842 if( ( i_len = GetWBE( header ) ) > 0 )
844 psz = malloc( i_len + 1 );
845 stream_Read( p_demux->s, psz, i_len );
849 msg_Dbg( p_demux, " - copyright=`%s'", psz );
850 p_sys->psz_copyright = psz;
855 stream_Read( p_demux->s, header, 2 );
856 if( ( i_len = GetWBE( header ) ) > 0 )
858 psz = malloc( i_len + 1 );
859 stream_Read( p_demux->s, psz, i_len );
863 msg_Dbg( p_demux, " - comment=`%s'", psz );
864 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 );
1048 msg_Dbg( p_demux, " - title=`%s'", psz );
1049 p_sys->psz_title = psz;
1054 i_len = *p_peek ; p_peek++;
1057 psz = malloc( i_len + 1 );
1058 memcpy( psz, p_peek, i_len );
1062 msg_Dbg( p_demux, " - artist=`%s'", psz );
1063 p_sys->psz_artist = psz;
1068 i_len = *p_peek ; p_peek++;
1071 psz = malloc( i_len + 1 );
1072 memcpy( psz, p_peek, i_len );
1076 msg_Dbg( p_demux, " - Copyright=`%s'", psz );
1077 p_sys->psz_copyright = psz;
1082 i_len = *p_peek ; p_peek++;
1085 psz = malloc( i_len + 1 );
1086 memcpy( psz, p_peek, i_len );
1090 msg_Dbg( p_demux, " - Comment=`%s'", psz );
1091 p_sys->psz_description = psz;
1093 /* This might be unusefull */
1096 p_peek ++; /* Unknown */
1097 p_peek ++; /* FourCC length = 4 */
1098 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1101 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1102 fmt.audio.i_rate = 8000;
1104 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1105 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1107 else /* RMF version 4/5 */
1109 p_peek += 2; /* 00 00 */
1110 p_peek += 4; /* .ra4 or .ra5 */
1111 p_peek += 4; /* data size */
1112 p_peek += 2; /* version (4 or 5) */
1113 i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1114 i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1115 i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
1116 p_peek += 4; /* ?? */
1117 p_peek += 4; /* ?? */
1118 p_peek += 4; /* ?? */
1119 i_subpacket_h = GetWBE( p_peek ); p_peek += 2; /* 1 */
1120 i_frame_size = GetWBE( p_peek ); p_peek += 2; /* frame size */
1121 i_subpacket_size = GetWBE( p_peek ); p_peek += 2; /* subpacket_size */
1122 if( !i_subpacket_size || !i_frame_size || !i_coded_frame_size )
1123 return VLC_EGENERIC;
1124 p_peek += 2; /* ?? */
1126 if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1128 fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */
1129 p_peek += 2; /* ?? */
1130 fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
1131 fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
1132 fmt.audio.i_blockalign = i_frame_size;
1134 if( i_version == 5 )
1136 memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4; /* genr */
1137 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1141 p_peek += p_peek[0] + 1; /* descr 1 */
1142 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
1143 p_peek += p_peek[0] + 1;
1146 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1147 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1149 p_peek += 3; /* ?? */
1150 if( i_version == 5 ) p_peek++;
1151 /* Extra Data then: DWord + byte[] */
1152 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1155 switch( fmt.i_codec )
1157 case VLC_FOURCC('1','4','_','4'):
1158 /* fmt.audio.i_blockalign = 0x14 */
1160 case VLC_FOURCC('l','p','c','J'):
1161 /* fmt.audio.i_blockalign = 0x14 */
1162 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1165 case VLC_FOURCC('2','8','_','8'):
1167 fmt.audio.i_blockalign = i_coded_frame_size;
1170 case VLC_FOURCC( 'd','n','e','t' ):
1171 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1174 case VLC_FOURCC( 'r','a','a','c' ):
1175 case VLC_FOURCC( 'r','a','c','p' ):
1176 if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1177 if( fmt.i_extra > 0 )
1179 fmt.p_extra = malloc( fmt.i_extra );
1180 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1183 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1186 case VLC_FOURCC('s','i','p','r'):
1187 fmt.audio.i_flavor = i_flavor;
1188 case VLC_FOURCC('c','o','o','k'):
1189 case VLC_FOURCC('a','t','r','c'):
1190 if( !memcmp( p_genr, "genr", 4 ) )
1191 fmt.audio.i_blockalign = i_subpacket_size;
1193 fmt.audio.i_blockalign = i_coded_frame_size;
1194 if( !fmt.i_extra ) break;
1195 fmt.p_extra = malloc( fmt.i_extra );
1196 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1199 case VLC_FOURCC('r','a','l','f'):
1200 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1201 (char*)&fmt.i_codec );
1205 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1206 (char*)&fmt.i_codec );
1210 if( fmt.i_codec != 0 )
1214 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1216 tk = malloc( sizeof( real_track_t ) );
1222 tk->i_subpacket_h = i_subpacket_h;
1223 tk->i_subpacket_size = i_subpacket_size;
1224 tk->i_coded_frame_size = i_coded_frame_size;
1225 tk->i_frame_size = i_frame_size;
1227 tk->i_out_subpacket = 0;
1228 tk->i_subpacket = 0;
1229 tk->i_subpackets = 0;
1230 tk->p_subpackets = NULL;
1231 if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
1232 || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1235 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1237 calloc( tk->i_subpackets, sizeof(block_t *) );
1239 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1242 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1244 calloc( tk->i_subpackets, sizeof(block_t *) );
1247 tk->p_es = es_out_Add( p_demux->out, &fmt );
1249 TAB_APPEND( p_sys->i_track, p_sys->track, tk );