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( "demux", 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;
153 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
158 memset( p_sys, 0, sizeof( demux_sys_t ) );
160 p_sys->i_data_offset = 0;
166 /* Parse the headers */
167 if( HeaderRead( p_demux ) )
170 msg_Err( p_demux, "invalid header" );
171 for( i = 0; i < p_sys->i_track; i++ )
173 real_track_t *tk = p_sys->track[i];
177 es_out_Del( p_demux->out, tk->p_es );
181 if( p_sys->i_track > 0 )
183 free( p_sys->track );
192 /*****************************************************************************
194 *****************************************************************************/
195 static void Close( vlc_object_t *p_this )
197 demux_t *p_demux = (demux_t*)p_this;
198 demux_sys_t *p_sys = p_demux->p_sys;
201 for( i = 0; i < p_sys->i_track; i++ )
203 real_track_t *tk = p_sys->track[i];
204 int j = tk->i_subpackets;
206 if( tk->p_frame ) block_Release( tk->p_frame );
207 es_format_Clean( &tk->fmt );
211 if( tk->p_subpackets[ j ] )
212 block_Release( tk->p_subpackets[ j ] );
214 if( tk->i_subpackets ) free( tk->p_subpackets );
219 free( p_sys->psz_title );
220 free( p_sys->psz_artist );
221 free( p_sys->psz_copyright );
222 free( p_sys->psz_description );
224 if( p_sys->i_track > 0 ) free( p_sys->track );
229 /*****************************************************************************
231 *****************************************************************************/
232 static int Demux( demux_t *p_demux )
234 demux_sys_t *p_sys = p_demux->p_sys;
236 int i_size, i_id, i_flags, i;
238 real_track_t *tk = NULL;
241 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
242 p_sys->i_data_packets_count )
244 if( stream_Read( p_demux->s, header, 18 ) < 18 )
248 if( strncmp( (char *)header, "DATA", 4 ) )
252 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
253 p_sys->i_data_size = GetDWBE( &header[4] );
254 p_sys->i_data_packets_count = GetDWBE( &header[10] );
255 p_sys->i_data_packets = 0;
256 p_sys->i_data_offset_next = GetDWBE( &header[14] );
258 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
259 p_sys->i_data_packets_count,
260 (uint32_t)p_sys->i_data_offset_next );
263 if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
265 i_size = GetWBE( &header[2] ) - 12;
266 i_id = GetWBE( &header[4] );
267 i_pts = 1000 * GetDWBE( &header[6] );
268 i_pts += 1000; /* Avoid 0 pts */
269 i_flags= header[11]; /* flags 0x02 -> keyframe */
272 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
273 p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
276 p_sys->i_data_packets++;
278 stream_Read( p_demux->s, p_sys->buffer, i_size );
280 for( i = 0; i < p_sys->i_track; i++ )
282 if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
287 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
290 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
292 if( tk->fmt.i_cat == VIDEO_ES && b_selected )
294 uint8_t *p = p_sys->buffer;
296 while( p < &p_sys->buffer[i_size - 2] )
305 if( (h&0xc0) == 0x40 )
309 i_len = &p_sys->buffer[i_size] - p;
315 i_subseq = (*p++)&0x7f;
317 i_len = (p[0] << 8)|p[1]; p += 2;
318 if( (i_len&0xc000) == 0 )
321 i_len |= (p[0] << 8)|p[1]; p += 2;
329 i_offset = (p[0] << 8)|p[1]; p += 2;
330 if( (i_offset&0xc000) == 0 )
333 i_offset |= (p[0] << 8)|p[1]; p += 2;
334 i_offset &= 0x3fffffff;
343 i_copy = i_len - i_offset;
344 if( i_copy > &p_sys->buffer[i_size] - p )
346 i_copy = &p_sys->buffer[i_size] - p;
348 else if( i_copy < 0 )
353 msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d",
354 i_len, i_offset, i_copy, i_subseq, i_seqnum );
356 if( (h&0xc0) == 0x80 )
358 /* last fragment -> fixes */
360 i_offset = i_len - i_copy;
361 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
366 ( tk->p_frame->i_dts != i_pts ||
367 tk->i_frame != i_len ) )
369 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
371 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
377 if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
385 if( tk->p_frame == NULL )
387 msg_Dbg( p_demux, "new frame size=%d", i_len );
389 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
393 memset( &tk->p_frame->p_buffer[8], 0, i_len );
394 tk->p_frame->i_dts = i_pts;
395 tk->p_frame->i_pts = i_pts;
397 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
398 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */
401 if( i_offset < tk->i_frame)
403 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
405 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
406 i_ck, i_offset, i_copy );
408 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +0 ] = 1;
409 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +1 ] = i_offset;
412 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
417 if( (h&0xc0) != 0x80 )
426 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
428 if( (h&0xc0) == 0x80 )
432 i_offset = i_len - i_offset;
434 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
435 memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
438 if( p_sys->i_pcr < tk->p_frame->i_dts )
440 p_sys->i_pcr = tk->p_frame->i_dts;
441 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
442 (int64_t)p_sys->i_pcr );
444 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
452 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
453 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
457 if( (h&0xc0) != 0x00 )
462 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
466 p_frame->i_dts = i_pts;
467 p_frame->i_pts = i_pts;
469 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
470 ((uint32_t*)p_frame->p_buffer)[1] = 1;
471 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
472 memcpy( &p_frame->p_buffer[8], p, i_copy );
476 if( p_sys->i_pcr < p_frame->i_dts )
478 p_sys->i_pcr = p_frame->i_dts;
479 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
480 (int64_t)p_sys->i_pcr );
482 es_out_Send( p_demux->out, tk->p_es, p_frame );
488 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
492 memset( &tk->p_frame->p_buffer[8], 0, i_len );
493 tk->p_frame->i_dts = i_pts;
494 tk->p_frame->i_pts = i_pts;
496 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
497 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */
498 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
499 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
506 else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
508 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
509 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
510 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
511 tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
513 uint8_t *p_buf = p_sys->buffer;
514 int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
518 if( i_flags & 2 ) y = tk->i_subpacket = 0;
520 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
521 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
522 for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
524 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
525 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
526 p_buf += tk->i_subpacket_size;
528 i_index = tk->i_subpacket_h * i +
529 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
531 p_block->i_dts = p_block->i_pts = i_pts;
532 tk->p_subpackets[i_index] = p_block;
536 if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
537 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
538 for( i = 0; i < tk->i_subpacket_h / 2; i++ )
540 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
541 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
542 p_buf += tk->i_coded_frame_size;
544 i_index = (i * 2 * tk->i_frame_size) /
545 tk->i_coded_frame_size + y;
547 p_block->i_dts = p_block->i_pts = i_pts;
548 tk->p_subpackets[i_index] = p_block;
552 while( tk->i_out_subpacket != tk->i_subpackets &&
553 tk->p_subpackets[tk->i_out_subpacket] )
556 if (tk->i_out_subpacket == 0)
558 p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
559 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
560 (int64_t)p_sys->i_pcr );
563 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
564 tk->p_subpackets[tk->i_out_subpacket] = 0;
566 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
567 es_out_Send( p_demux->out, tk->p_es, p_block );
569 tk->i_out_subpacket++;
572 if( tk->i_subpacket == tk->i_subpackets &&
573 tk->i_out_subpacket != tk->i_subpackets )
575 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
576 "this shouldn't happen" );
579 if( tk->i_subpacket == tk->i_subpackets )
582 tk->i_out_subpacket = 0;
588 if( p_sys->i_pcr < i_pts )
590 p_sys->i_pcr = i_pts;
591 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
592 (int64_t)p_sys->i_pcr );
595 if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
597 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
598 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
601 for( i = 0; i < i_sub; i++ )
603 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
604 block_t *p_block = block_New( p_demux, i_sub_size );
607 memcpy( p_block->p_buffer, p_sub, i_sub_size );
611 p_block->i_pts = ( i == 0 ? i_pts : 0 );
613 es_out_Send( p_demux->out, tk->p_es, p_block );
619 block_t *p_block = block_New( p_demux, i_size );
621 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
623 uint8_t *src = p_sys->buffer;
624 uint8_t *dst = p_block->p_buffer;
627 while( dst < &p_block->p_buffer[i_size- 1])
637 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
639 p_block->i_dts = p_block->i_pts = i_pts;
640 es_out_Send( p_demux->out, tk->p_es, p_block );
649 /*****************************************************************************
651 *****************************************************************************/
652 static int Control( demux_t *p_demux, int i_query, va_list args )
654 demux_sys_t *p_sys = p_demux->p_sys;
664 case DEMUX_GET_POSITION:
665 pf = (double*) va_arg( args, double* );
666 i64 = stream_Size( p_demux->s );
669 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
677 case DEMUX_SET_POSITION:
678 f = (double) va_arg( args, double );
679 i64 = stream_Size( p_demux->s );
681 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
683 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
686 pi64 = (int64_t*)va_arg( args, int64_t * );
687 if( p_sys->i_mux_rate > 0 )
689 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
696 case DEMUX_GET_LENGTH:
697 pi64 = (int64_t*)va_arg( args, int64_t * );
699 /* the commented following lines are fen's implementation, which doesn't seem to
700 * work for one reason or another -- FK */
701 /*if( p_sys->i_mux_rate > 0 )
703 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
706 if( p_sys->i_our_duration > 0 )
708 /* our stored duration is in ms, so... */
709 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
718 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
720 /* the core will crash if we provide NULL strings, so check
721 * every string first */
722 if( p_sys->psz_title )
723 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
724 if( p_sys->psz_artist )
725 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
726 if( p_sys->psz_copyright )
727 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
728 if( p_sys->psz_description )
729 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
741 /*****************************************************************************
743 *****************************************************************************/
744 static int HeaderRead( demux_t *p_demux )
746 demux_sys_t *p_sys = p_demux->p_sys;
747 uint8_t header[100]; /* FIXME */
756 /* Read the header */
757 if( stream_Read( p_demux->s, header, 10 ) < 10 )
761 i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
762 i_size = GetDWBE( &header[4] );
763 i_version = GetWBE( &header[8] );
765 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
766 (char*)&i_id, i_size, i_version );
768 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
770 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
773 i_skip = i_size - 10;
775 if( i_id == VLC_FOURCC('.','R','M','F') )
777 if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
778 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
779 GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
783 else if( i_id == VLC_FOURCC('P','R','O','P') )
787 if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
789 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
790 GetDWBE(&header[0]), GetDWBE(&header[4]) );
791 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
792 GetDWBE(&header[8]), GetDWBE(&header[12]) );
793 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) );
794 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) );
795 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) );
796 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) );
797 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) );
798 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) );
800 /* set the duration for export in control */
801 p_sys->i_our_duration = (int)GetDWBE(&header[20]);
803 i_flags = GetWBE(&header[38]);
804 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
806 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
807 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
808 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
811 else if( i_id == VLC_FOURCC('C','O','N','T') )
816 /* FIXME FIXME: should convert from whatever the character
817 * encoding of the input meta data is to UTF-8. */
819 stream_Read( p_demux->s, header, 2 );
820 if( ( i_len = GetWBE( header ) ) > 0 )
822 psz = malloc( i_len + 1 );
823 stream_Read( p_demux->s, psz, i_len );
827 msg_Dbg( p_demux, " - title=`%s'", psz );
828 p_sys->psz_title = psz;
833 stream_Read( p_demux->s, header, 2 );
834 if( ( i_len = GetWBE( header ) ) > 0 )
836 psz = malloc( i_len + 1 );
837 stream_Read( p_demux->s, psz, i_len );
841 msg_Dbg( p_demux, " - author=`%s'", psz );
842 p_sys->psz_artist = psz;
847 stream_Read( p_demux->s, header, 2 );
848 if( ( i_len = GetWBE( header ) ) > 0 )
850 psz = malloc( i_len + 1 );
851 stream_Read( p_demux->s, psz, i_len );
855 msg_Dbg( p_demux, " - copyright=`%s'", psz );
856 p_sys->psz_copyright = psz;
861 stream_Read( p_demux->s, header, 2 );
862 if( ( i_len = GetWBE( header ) ) > 0 )
864 psz = malloc( i_len + 1 );
865 stream_Read( p_demux->s, psz, i_len );
869 msg_Dbg( p_demux, " - comment=`%s'", psz );
870 p_sys->psz_description = psz;
875 else if( i_id == VLC_FOURCC('M','D','P','R') )
877 /* Media properties header */
882 if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
883 i_num = GetWBE( header );
884 msg_Dbg( p_demux, " - id=0x%x", i_num );
885 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
886 GetDWBE(&header[2]), GetDWBE(&header[6]) );
887 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
888 GetDWBE(&header[10]), GetDWBE(&header[14]) );
889 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&header[18]) );
890 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&header[22]) );
891 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&header[26]) );
895 stream_Read( p_demux->s, header, 1 );
896 if( ( i_len = header[0] ) > 0 )
898 psz = malloc( i_len + 1 );
899 stream_Read( p_demux->s, psz, i_len );
902 msg_Dbg( p_demux, " - name=`%s'", psz );
908 stream_Read( p_demux->s, header, 1 );
909 if( ( i_len = header[0] ) > 0 )
911 psz = malloc( i_len + 1 );
912 stream_Read( p_demux->s, psz, i_len );
915 msg_Dbg( p_demux, " - mime=`%s'", psz );
921 stream_Read( p_demux->s, header, 4 );
922 if( ( i_len = GetDWBE( header ) ) > 0 )
924 ReadCodecSpecificData( p_demux, i_len, i_num );
925 stream_Read( p_demux->s, NULL, i_len );
931 else if( i_id == VLC_FOURCC('D','A','T','A') )
933 stream_Read( p_demux->s, header, 8 );
935 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
936 p_sys->i_data_size = i_size;
937 p_sys->i_data_packets_count = GetDWBE( header );
938 p_sys->i_data_packets = 0;
939 p_sys->i_data_offset_next = GetDWBE( &header[4] );
941 msg_Dbg( p_demux, " - packets count=%d next=%u",
942 p_sys->i_data_packets_count,
943 (uint32_t)p_sys->i_data_offset_next );
945 /* we have finished the header */
951 msg_Dbg( p_demux, "unknown chunk" );
954 if( i_skip < 0 ) return VLC_EGENERIC;
955 stream_Read( p_demux->s, NULL, i_skip );
958 /* TODO read index if possible */
963 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
965 demux_sys_t *p_sys = p_demux->p_sys;
968 const uint8_t *p_peek;
970 msg_Dbg( p_demux, " - specific data len=%d", i_len );
971 if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
973 if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
975 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
976 p_peek[10], p_peek[11] ) );
977 fmt.video.i_width = GetWBE( &p_peek[12] );
978 fmt.video.i_height= GetWBE( &p_peek[14] );
981 fmt.p_extra = malloc( 8 );
982 ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
983 ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
985 msg_Dbg( p_demux, " - video 0x%08x 0x%08x",
986 ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
988 if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
989 GetDWBE( &p_peek[30] ) == 0x10003001 )
991 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
993 else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
994 GetDWBE( &p_peek[30] ) == 0x20100001 ||
995 GetDWBE( &p_peek[30] ) == 0x20200002 )
997 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
999 else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
1001 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
1003 else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
1005 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1008 msg_Dbg( p_demux, " - video %4.4s %dx%d",
1009 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
1011 tk = malloc( sizeof( real_track_t ) );
1012 tk->i_out_subpacket = 0;
1013 tk->i_subpacket = 0;
1014 tk->i_subpackets = 0;
1015 tk->p_subpackets = NULL;
1020 tk->p_es = es_out_Add( p_demux->out, &fmt );
1022 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1024 else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
1026 int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
1027 int i_frame_size, i_subpacket_size;
1029 int i_version = GetWBE( &p_peek[4] ); /* [0..3] = '.','r','a',0xfd */
1030 msg_Dbg( p_demux, " - audio version=%d", i_version );
1032 p_peek += 6; /* 4 + version */
1033 es_format_Init( &fmt, AUDIO_ES, 0 );
1035 if( i_version == 3 )
1040 i_header_size = GetWBE( p_peek ); p_peek += 2; /* Size from now */
1041 p_peek += 10; /* Unknown */
1043 p_peek += 4; /* Data Size */
1046 i_len = *p_peek ; p_peek++;
1049 psz = malloc( i_len + 1 );
1050 memcpy( psz, p_peek, i_len );
1054 msg_Dbg( p_demux, " - title=`%s'", psz );
1055 p_sys->psz_title = psz;
1060 i_len = *p_peek ; p_peek++;
1063 psz = malloc( i_len + 1 );
1064 memcpy( psz, p_peek, i_len );
1068 msg_Dbg( p_demux, " - artist=`%s'", psz );
1069 p_sys->psz_artist = psz;
1074 i_len = *p_peek ; p_peek++;
1077 psz = malloc( i_len + 1 );
1078 memcpy( psz, p_peek, i_len );
1082 msg_Dbg( p_demux, " - Copyright=`%s'", psz );
1083 p_sys->psz_copyright = psz;
1088 i_len = *p_peek ; p_peek++;
1091 psz = malloc( i_len + 1 );
1092 memcpy( psz, p_peek, i_len );
1096 msg_Dbg( p_demux, " - Comment=`%s'", psz );
1097 p_sys->psz_description = psz;
1099 /* This might be unusefull */
1102 p_peek ++; /* Unknown */
1103 p_peek ++; /* FourCC length = 4 */
1104 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1107 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1108 fmt.audio.i_rate = 8000;
1110 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1111 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1113 else /* RMF version 4/5 */
1115 p_peek += 2; /* 00 00 */
1116 p_peek += 4; /* .ra4 or .ra5 */
1117 p_peek += 4; /* data size */
1118 p_peek += 2; /* version (4 or 5) */
1119 i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1120 i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1121 i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
1122 p_peek += 4; /* ?? */
1123 p_peek += 4; /* ?? */
1124 p_peek += 4; /* ?? */
1125 i_subpacket_h = GetWBE( p_peek ); p_peek += 2; /* 1 */
1126 i_frame_size = GetWBE( p_peek ); p_peek += 2; /* frame size */
1127 i_subpacket_size = GetWBE( p_peek ); p_peek += 2; /* subpacket_size */
1128 if( !i_subpacket_size || !i_frame_size || !i_coded_frame_size )
1129 return VLC_EGENERIC;
1130 p_peek += 2; /* ?? */
1132 if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1134 fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */
1135 p_peek += 2; /* ?? */
1136 fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
1137 fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
1138 fmt.audio.i_blockalign = i_frame_size;
1140 if( i_version == 5 )
1142 memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4; /* genr */
1143 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1147 p_peek += p_peek[0] + 1; /* descr 1 */
1148 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
1149 p_peek += p_peek[0] + 1;
1152 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1153 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1155 p_peek += 3; /* ?? */
1156 if( i_version == 5 ) p_peek++;
1157 /* Extra Data then: DWord + byte[] */
1158 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1161 switch( fmt.i_codec )
1163 case VLC_FOURCC('1','4','_','4'):
1164 /* fmt.audio.i_blockalign = 0x14 */
1166 case VLC_FOURCC('l','p','c','J'):
1167 /* fmt.audio.i_blockalign = 0x14 */
1168 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1171 case VLC_FOURCC('2','8','_','8'):
1173 fmt.audio.i_blockalign = i_coded_frame_size;
1176 case VLC_FOURCC( 'd','n','e','t' ):
1177 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1180 case VLC_FOURCC( 'r','a','a','c' ):
1181 case VLC_FOURCC( 'r','a','c','p' ):
1182 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1184 if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1185 if( fmt.i_extra > 0 )
1187 fmt.p_extra = malloc( fmt.i_extra );
1188 if( fmt.p_extra == NULL )
1190 msg_Err( p_demux, "Error in the extra data" );
1191 return VLC_EGENERIC;
1193 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1197 case VLC_FOURCC('s','i','p','r'):
1198 fmt.audio.i_flavor = i_flavor;
1199 case VLC_FOURCC('c','o','o','k'):
1200 case VLC_FOURCC('a','t','r','c'):
1201 if( !memcmp( p_genr, "genr", 4 ) )
1202 fmt.audio.i_blockalign = i_subpacket_size;
1204 fmt.audio.i_blockalign = i_coded_frame_size;
1206 if( fmt.i_extra > 0 )
1208 fmt.p_extra = malloc( fmt.i_extra );
1209 if( fmt.p_extra == NULL )
1211 msg_Err( p_demux, "Error in the extra data" );
1212 return VLC_EGENERIC;
1214 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1218 case VLC_FOURCC('r','a','l','f'):
1219 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1220 (char*)&fmt.i_codec );
1224 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1225 (char*)&fmt.i_codec );
1229 if( fmt.i_codec != 0 )
1231 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1233 tk = malloc( sizeof( real_track_t ) );
1239 tk->i_subpacket_h = i_subpacket_h;
1240 tk->i_subpacket_size = i_subpacket_size;
1241 tk->i_coded_frame_size = i_coded_frame_size;
1242 tk->i_frame_size = i_frame_size;
1244 tk->i_out_subpacket = 0;
1245 tk->i_subpacket = 0;
1246 tk->i_subpackets = 0;
1247 tk->p_subpackets = NULL;
1248 if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
1249 || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1252 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1254 calloc( tk->i_subpackets, sizeof(block_t *) );
1256 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1259 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1261 calloc( tk->i_subpackets, sizeof(block_t *) );
1264 /* Check if the calloc went correctly */
1265 if( tk->p_subpackets == NULL )
1267 tk->i_subpackets = 0;
1268 msg_Err( p_demux, "Can't alloc subpacket" );
1269 return VLC_EGENERIC;
1272 tk->p_es = es_out_Add( p_demux->out, &fmt );
1274 TAB_APPEND( p_sys->i_track, p_sys->track, tk );