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 *****************************************************************************/
53 #include <vlc_plugin.h>
56 #include <vlc_demux.h>
57 #include <vlc_charset.h>
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close ( vlc_object_t * );
67 set_description( _("Real demuxer" ) );
68 set_capability( "demux", 15 );
69 set_category( CAT_INPUT );
70 set_subcategory( SUBCAT_INPUT_DEMUX );
71 set_callbacks( Open, Close );
72 add_shortcut( "real" );
76 /*****************************************************************************
78 *****************************************************************************/
92 int i_coded_frame_size;
97 block_t **p_subpackets;
104 int64_t i_data_offset;
106 uint32_t i_data_packets_count;
107 uint32_t i_data_packets;
108 int64_t i_data_offset_next;
116 char* psz_description;
119 real_track_t **track;
121 uint8_t buffer[65536];
126 static int Demux( demux_t *p_demux );
127 static int Control( demux_t *p_demux, int i_query, va_list args );
129 static int HeaderRead( demux_t *p_demux );
130 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
132 /*****************************************************************************
134 *****************************************************************************/
135 static int Open( vlc_object_t *p_this )
137 demux_t *p_demux = (demux_t*)p_this;
140 const uint8_t *p_peek;
142 if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
143 if( !memcmp( p_peek, ".ra", 3 ) )
145 msg_Warn( p_demux, ".ra files unsuported" );
148 if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
150 /* Fill p_demux field */
151 p_demux->pf_demux = Demux;
152 p_demux->pf_control = Control;
154 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
159 memset( p_sys, 0, sizeof( demux_sys_t ) );
161 p_sys->i_data_offset = 0;
167 /* Parse the headers */
168 if( HeaderRead( p_demux ) )
171 msg_Err( p_demux, "invalid header" );
172 for( i = 0; i < p_sys->i_track; i++ )
174 real_track_t *tk = p_sys->track[i];
178 es_out_Del( p_demux->out, tk->p_es );
182 if( p_sys->i_track > 0 )
184 free( p_sys->track );
193 /*****************************************************************************
195 *****************************************************************************/
196 static void Close( vlc_object_t *p_this )
198 demux_t *p_demux = (demux_t*)p_this;
199 demux_sys_t *p_sys = p_demux->p_sys;
202 for( i = 0; i < p_sys->i_track; i++ )
204 real_track_t *tk = p_sys->track[i];
205 int j = tk->i_subpackets;
207 if( tk->p_frame ) block_Release( tk->p_frame );
208 es_format_Clean( &tk->fmt );
212 if( tk->p_subpackets[ j ] )
213 block_Release( tk->p_subpackets[ j ] );
215 if( tk->i_subpackets ) free( tk->p_subpackets );
220 free( p_sys->psz_title );
221 free( p_sys->psz_artist );
222 free( p_sys->psz_copyright );
223 free( p_sys->psz_description );
225 if( p_sys->i_track > 0 ) free( p_sys->track );
230 /*****************************************************************************
232 *****************************************************************************/
233 static int Demux( demux_t *p_demux )
235 demux_sys_t *p_sys = p_demux->p_sys;
237 int i_size, i_id, i_flags, i;
239 real_track_t *tk = NULL;
242 if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
243 p_sys->i_data_packets_count )
245 if( stream_Read( p_demux->s, header, 18 ) < 18 )
249 if( strncmp( (char *)header, "DATA", 4 ) )
253 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
254 p_sys->i_data_size = GetDWBE( &header[4] );
255 p_sys->i_data_packets_count = GetDWBE( &header[10] );
256 p_sys->i_data_packets = 0;
257 p_sys->i_data_offset_next = GetDWBE( &header[14] );
259 msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
260 p_sys->i_data_packets_count,
261 (uint32_t)p_sys->i_data_offset_next );
264 if( stream_Read( p_demux->s, header, 12 ) < 12 ) return 0;
266 i_size = GetWBE( &header[2] ) - 12;
267 i_id = GetWBE( &header[4] );
268 i_pts = 1000 * GetDWBE( &header[6] );
269 i_pts += 1000; /* Avoid 0 pts */
270 i_flags= header[11]; /* flags 0x02 -> keyframe */
273 msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
274 p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
277 p_sys->i_data_packets++;
279 stream_Read( p_demux->s, p_sys->buffer, i_size );
281 for( i = 0; i < p_sys->i_track; i++ )
283 if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
288 msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
291 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b_selected );
293 if( tk->fmt.i_cat == VIDEO_ES && b_selected )
295 uint8_t *p = p_sys->buffer;
297 while( p < &p_sys->buffer[i_size - 2] )
306 if( (h&0xc0) == 0x40 )
310 i_len = &p_sys->buffer[i_size] - p;
316 i_subseq = (*p++)&0x7f;
318 i_len = (p[0] << 8)|p[1]; p += 2;
319 if( (i_len&0xc000) == 0 )
322 i_len |= (p[0] << 8)|p[1]; p += 2;
330 i_offset = (p[0] << 8)|p[1]; p += 2;
331 if( (i_offset&0xc000) == 0 )
334 i_offset |= (p[0] << 8)|p[1]; p += 2;
335 i_offset &= 0x3fffffff;
344 i_copy = i_len - i_offset;
345 if( i_copy > &p_sys->buffer[i_size] - p )
347 i_copy = &p_sys->buffer[i_size] - p;
349 else if( i_copy < 0 )
354 msg_Dbg( p_demux, " - len=%d offset=%d size=%d subseq=%d seqnum=%d",
355 i_len, i_offset, i_copy, i_subseq, i_seqnum );
357 if( (h&0xc0) == 0x80 )
359 /* last fragment -> fixes */
361 i_offset = i_len - i_copy;
362 msg_Dbg( p_demux, "last fixing copy=%d offset=%d",
367 ( tk->p_frame->i_dts != i_pts ||
368 tk->i_frame != i_len ) )
370 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
372 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
378 if( (h&0xc0) != 0x80 && (h&0xc0) != 0x00 && !tk->p_frame )
386 if( tk->p_frame == NULL )
388 msg_Dbg( p_demux, "new frame size=%d", i_len );
390 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
394 memset( &tk->p_frame->p_buffer[8], 0, i_len );
395 tk->p_frame->i_dts = i_pts;
396 tk->p_frame->i_pts = i_pts;
398 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
399 ((uint32_t*)tk->p_frame->p_buffer)[1] = 0; /* chunk counts */
402 if( i_offset < tk->i_frame)
404 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
406 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
407 i_ck, i_offset, i_copy );
409 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +0 ] = 1;
410 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +1 ] = i_offset;
413 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
418 if( (h&0xc0) != 0x80 )
427 int i_ck = ((uint32_t*)tk->p_frame->p_buffer)[1]++;
429 if( (h&0xc0) == 0x80 )
433 i_offset = i_len - i_offset;
435 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
436 memcpy( &tk->p_frame->p_buffer[i_offset+ 8], p, i_copy );
439 if( p_sys->i_pcr < tk->p_frame->i_dts )
441 p_sys->i_pcr = tk->p_frame->i_dts;
442 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
443 (int64_t)p_sys->i_pcr );
445 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
453 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
454 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
458 if( (h&0xc0) != 0x00 )
463 if( !( p_frame = block_New( p_demux, i_copy + 8 + 8 ) ) )
467 p_frame->i_dts = i_pts;
468 p_frame->i_pts = i_pts;
470 ((uint32_t*)p_frame->p_buffer)[0] = i_copy;
471 ((uint32_t*)p_frame->p_buffer)[1] = 1;
472 ((uint32_t*)(p_frame->p_buffer+i_copy+8))[0] = 0;
473 memcpy( &p_frame->p_buffer[8], p, i_copy );
477 if( p_sys->i_pcr < p_frame->i_dts )
479 p_sys->i_pcr = p_frame->i_dts;
480 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
481 (int64_t)p_sys->i_pcr );
483 es_out_Send( p_demux->out, tk->p_es, p_frame );
489 if( !( tk->p_frame = block_New( p_demux, i_len + 8 + 1000) ) )
493 memset( &tk->p_frame->p_buffer[8], 0, i_len );
494 tk->p_frame->i_dts = i_pts;
495 tk->p_frame->i_pts = i_pts;
497 ((uint32_t*)tk->p_frame->p_buffer)[0] = i_len; /* len */
498 ((uint32_t*)tk->p_frame->p_buffer)[1] = 1; /* chunk counts */
499 ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[0] = i_offset;
500 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
507 else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
509 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
510 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
511 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
512 tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
514 uint8_t *p_buf = p_sys->buffer;
515 int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
519 if( i_flags & 2 ) y = tk->i_subpacket = 0;
521 if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
522 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
523 for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
525 block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
526 memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
527 p_buf += tk->i_subpacket_size;
529 i_index = tk->i_subpacket_h * i +
530 ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
532 p_block->i_dts = p_block->i_pts = i_pts;
533 tk->p_subpackets[i_index] = p_block;
537 if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
538 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
539 for( i = 0; i < tk->i_subpacket_h / 2; i++ )
541 block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
542 memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
543 p_buf += tk->i_coded_frame_size;
545 i_index = (i * 2 * tk->i_frame_size) /
546 tk->i_coded_frame_size + y;
548 p_block->i_dts = p_block->i_pts = i_pts;
549 tk->p_subpackets[i_index] = p_block;
553 while( tk->i_out_subpacket != tk->i_subpackets &&
554 tk->p_subpackets[tk->i_out_subpacket] )
557 if (tk->i_out_subpacket == 0)
559 p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
560 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
561 (int64_t)p_sys->i_pcr );
564 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
565 tk->p_subpackets[tk->i_out_subpacket] = 0;
567 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
568 es_out_Send( p_demux->out, tk->p_es, p_block );
570 tk->i_out_subpacket++;
573 if( tk->i_subpacket == tk->i_subpackets &&
574 tk->i_out_subpacket != tk->i_subpackets )
576 msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
577 "this shouldn't happen" );
580 if( tk->i_subpacket == tk->i_subpackets )
583 tk->i_out_subpacket = 0;
589 if( p_sys->i_pcr < i_pts )
591 p_sys->i_pcr = i_pts;
592 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
593 (int64_t)p_sys->i_pcr );
596 if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
598 int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
599 uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
602 for( i = 0; i < i_sub; i++ )
604 int i_sub_size = GetWBE( &p_sys->buffer[2+i*2]);
605 block_t *p_block = block_New( p_demux, i_sub_size );
608 memcpy( p_block->p_buffer, p_sub, i_sub_size );
612 p_block->i_pts = ( i == 0 ? i_pts : 0 );
614 es_out_Send( p_demux->out, tk->p_es, p_block );
620 block_t *p_block = block_New( p_demux, i_size );
622 if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
624 uint8_t *src = p_sys->buffer;
625 uint8_t *dst = p_block->p_buffer;
628 while( dst < &p_block->p_buffer[i_size- 1])
638 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
640 p_block->i_dts = p_block->i_pts = i_pts;
641 es_out_Send( p_demux->out, tk->p_es, p_block );
650 /*****************************************************************************
652 *****************************************************************************/
653 static int Control( demux_t *p_demux, int i_query, va_list args )
655 demux_sys_t *p_sys = p_demux->p_sys;
665 case DEMUX_GET_POSITION:
666 pf = (double*) va_arg( args, double* );
667 i64 = stream_Size( p_demux->s );
670 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
678 case DEMUX_SET_POSITION:
679 f = (double) va_arg( args, double );
680 i64 = stream_Size( p_demux->s );
682 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
684 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
687 pi64 = (int64_t*)va_arg( args, int64_t * );
688 if( p_sys->i_mux_rate > 0 )
690 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
697 case DEMUX_GET_LENGTH:
698 pi64 = (int64_t*)va_arg( args, int64_t * );
700 /* the commented following lines are fen's implementation, which doesn't seem to
701 * work for one reason or another -- FK */
702 /*if( p_sys->i_mux_rate > 0 )
704 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
707 if( p_sys->i_our_duration > 0 )
709 /* our stored duration is in ms, so... */
710 *pi64 = (int64_t)1000 * p_sys->i_our_duration;
719 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
721 /* the core will crash if we provide NULL strings, so check
722 * every string first */
723 if( p_sys->psz_title )
724 vlc_meta_SetTitle( p_meta, p_sys->psz_title );
725 if( p_sys->psz_artist )
726 vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
727 if( p_sys->psz_copyright )
728 vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
729 if( p_sys->psz_description )
730 vlc_meta_SetDescription( p_meta, p_sys->psz_description );
742 /*****************************************************************************
744 *****************************************************************************/
745 static int HeaderRead( demux_t *p_demux )
747 demux_sys_t *p_sys = p_demux->p_sys;
748 uint8_t header[100]; /* FIXME */
757 /* Read the header */
758 if( stream_Read( p_demux->s, header, 10 ) < 10 )
762 i_id = VLC_FOURCC( header[0], header[1], header[2], header[3] );
763 i_size = GetDWBE( &header[4] );
764 i_version = GetWBE( &header[8] );
766 msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
767 (char*)&i_id, i_size, i_version );
769 if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
771 msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
774 i_skip = i_size - 10;
776 if( i_id == VLC_FOURCC('.','R','M','F') )
778 if( stream_Read( p_demux->s, header, 8 ) < 8 ) return VLC_EGENERIC;
779 msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
780 GetDWBE( &header[0] ), GetDWBE( &header[4] ) );
784 else if( i_id == VLC_FOURCC('P','R','O','P') )
788 if( stream_Read(p_demux->s, header, 40) < 40 ) return VLC_EGENERIC;
790 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
791 GetDWBE(&header[0]), GetDWBE(&header[4]) );
792 msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
793 GetDWBE(&header[8]), GetDWBE(&header[12]) );
794 msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&header[16]) );
795 msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&header[20]) );
796 msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&header[24]) );
797 msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&header[28]) );
798 msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&header[32]) );
799 msg_Dbg( p_demux, " - num streams=%d", GetWBE(&header[36]) );
801 /* set the duration for export in control */
802 p_sys->i_our_duration = (int)GetDWBE(&header[20]);
804 i_flags = GetWBE(&header[38]);
805 msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
807 i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
808 i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
809 i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
812 else if( i_id == VLC_FOURCC('C','O','N','T') )
817 /* FIXME FIXME: should convert from whatever the character
818 * encoding of the input meta data is to UTF-8. */
820 stream_Read( p_demux->s, header, 2 );
821 if( ( i_len = GetWBE( header ) ) > 0 )
823 psz = malloc( i_len + 1 );
824 stream_Read( p_demux->s, psz, i_len );
828 msg_Dbg( p_demux, " - title=`%s'", psz );
829 p_sys->psz_title = psz;
834 stream_Read( p_demux->s, header, 2 );
835 if( ( i_len = GetWBE( header ) ) > 0 )
837 psz = malloc( i_len + 1 );
838 stream_Read( p_demux->s, psz, i_len );
842 msg_Dbg( p_demux, " - author=`%s'", psz );
843 p_sys->psz_artist = psz;
848 stream_Read( p_demux->s, header, 2 );
849 if( ( i_len = GetWBE( header ) ) > 0 )
851 psz = malloc( i_len + 1 );
852 stream_Read( p_demux->s, psz, i_len );
856 msg_Dbg( p_demux, " - copyright=`%s'", psz );
857 p_sys->psz_copyright = psz;
862 stream_Read( p_demux->s, header, 2 );
863 if( ( i_len = GetWBE( header ) ) > 0 )
865 psz = malloc( i_len + 1 );
866 stream_Read( p_demux->s, psz, i_len );
870 msg_Dbg( p_demux, " - comment=`%s'", psz );
871 p_sys->psz_description = psz;
876 else if( i_id == VLC_FOURCC('M','D','P','R') )
878 /* Media properties header */
883 if( stream_Read(p_demux->s, header, 30) < 30 ) return VLC_EGENERIC;
884 i_num = GetWBE( header );
885 msg_Dbg( p_demux, " - id=0x%x", i_num );
886 msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
887 GetDWBE(&header[2]), GetDWBE(&header[6]) );
888 msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
889 GetDWBE(&header[10]), GetDWBE(&header[14]) );
890 msg_Dbg( p_demux, " - start time=%d", GetDWBE(&header[18]) );
891 msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&header[22]) );
892 msg_Dbg( p_demux, " - duration=%d", GetDWBE(&header[26]) );
896 stream_Read( p_demux->s, header, 1 );
897 if( ( i_len = header[0] ) > 0 )
899 psz = malloc( i_len + 1 );
900 stream_Read( p_demux->s, psz, i_len );
903 msg_Dbg( p_demux, " - name=`%s'", psz );
909 stream_Read( p_demux->s, header, 1 );
910 if( ( i_len = header[0] ) > 0 )
912 psz = malloc( i_len + 1 );
913 stream_Read( p_demux->s, psz, i_len );
916 msg_Dbg( p_demux, " - mime=`%s'", psz );
922 stream_Read( p_demux->s, header, 4 );
923 if( ( i_len = GetDWBE( header ) ) > 0 )
925 ReadCodecSpecificData( p_demux, i_len, i_num );
926 stream_Read( p_demux->s, NULL, i_len );
932 else if( i_id == VLC_FOURCC('D','A','T','A') )
934 stream_Read( p_demux->s, header, 8 );
936 p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
937 p_sys->i_data_size = i_size;
938 p_sys->i_data_packets_count = GetDWBE( header );
939 p_sys->i_data_packets = 0;
940 p_sys->i_data_offset_next = GetDWBE( &header[4] );
942 msg_Dbg( p_demux, " - packets count=%d next=%u",
943 p_sys->i_data_packets_count,
944 (uint32_t)p_sys->i_data_offset_next );
946 /* we have finished the header */
952 msg_Dbg( p_demux, "unknown chunk" );
955 if( i_skip < 0 ) return VLC_EGENERIC;
956 stream_Read( p_demux->s, NULL, i_skip );
959 /* TODO read index if possible */
964 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
966 demux_sys_t *p_sys = p_demux->p_sys;
969 const uint8_t *p_peek;
971 msg_Dbg( p_demux, " - specific data len=%d", i_len );
972 if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
974 if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
976 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
977 p_peek[10], p_peek[11] ) );
978 fmt.video.i_width = GetWBE( &p_peek[12] );
979 fmt.video.i_height= GetWBE( &p_peek[14] );
982 fmt.p_extra = malloc( 8 );
983 ((uint32_t*)fmt.p_extra)[0] = GetDWBE( &p_peek[26] );
984 ((uint32_t*)fmt.p_extra)[1] = GetDWBE( &p_peek[30] );
986 msg_Dbg( p_demux, " - video 0x%08x 0x%08x",
987 ((uint32_t*)fmt.p_extra)[0], ((uint32_t*)fmt.p_extra)[1] );
989 if( GetDWBE( &p_peek[30] ) == 0x10003000 ||
990 GetDWBE( &p_peek[30] ) == 0x10003001 )
992 fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
994 else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
995 GetDWBE( &p_peek[30] ) == 0x20100001 ||
996 GetDWBE( &p_peek[30] ) == 0x20200002 )
998 fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
1000 else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
1002 fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
1004 else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
1006 fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
1009 msg_Dbg( p_demux, " - video %4.4s %dx%d",
1010 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
1012 tk = malloc( sizeof( real_track_t ) );
1013 tk->i_out_subpacket = 0;
1014 tk->i_subpacket = 0;
1015 tk->i_subpackets = 0;
1016 tk->p_subpackets = NULL;
1021 tk->p_es = es_out_Add( p_demux->out, &fmt );
1023 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
1025 else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
1027 int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
1028 int i_frame_size, i_subpacket_size;
1030 int i_version = GetWBE( &p_peek[4] ); /* [0..3] = '.','r','a',0xfd */
1031 msg_Dbg( p_demux, " - audio version=%d", i_version );
1033 p_peek += 6; /* 4 + version */
1034 es_format_Init( &fmt, AUDIO_ES, 0 );
1036 if( i_version == 3 )
1041 i_header_size = GetWBE( p_peek ); p_peek += 2; /* Size from now */
1042 p_peek += 10; /* Unknown */
1044 p_peek += 4; /* Data Size */
1047 i_len = *p_peek ; p_peek++;
1050 psz = malloc( i_len + 1 );
1051 memcpy( psz, p_peek, i_len );
1055 msg_Dbg( p_demux, " - title=`%s'", psz );
1056 p_sys->psz_title = psz;
1061 i_len = *p_peek ; p_peek++;
1064 psz = malloc( i_len + 1 );
1065 memcpy( psz, p_peek, i_len );
1069 msg_Dbg( p_demux, " - artist=`%s'", psz );
1070 p_sys->psz_artist = psz;
1075 i_len = *p_peek ; p_peek++;
1078 psz = malloc( i_len + 1 );
1079 memcpy( psz, p_peek, i_len );
1083 msg_Dbg( p_demux, " - Copyright=`%s'", psz );
1084 p_sys->psz_copyright = psz;
1089 i_len = *p_peek ; p_peek++;
1092 psz = malloc( i_len + 1 );
1093 memcpy( psz, p_peek, i_len );
1097 msg_Dbg( p_demux, " - Comment=`%s'", psz );
1098 p_sys->psz_description = psz;
1100 /* This might be unusefull */
1103 p_peek ++; /* Unknown */
1104 p_peek ++; /* FourCC length = 4 */
1105 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1108 fmt.audio.i_channels = 1; /* This is always the case in rm3 */
1109 fmt.audio.i_rate = 8000;
1111 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1112 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1114 else /* RMF version 4/5 */
1116 p_peek += 2; /* 00 00 */
1117 p_peek += 4; /* .ra4 or .ra5 */
1118 p_peek += 4; /* data size */
1119 p_peek += 2; /* version (4 or 5) */
1120 i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
1121 i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
1122 i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
1123 p_peek += 4; /* ?? */
1124 p_peek += 4; /* ?? */
1125 p_peek += 4; /* ?? */
1126 i_subpacket_h = GetWBE( p_peek ); p_peek += 2; /* 1 */
1127 i_frame_size = GetWBE( p_peek ); p_peek += 2; /* frame size */
1128 i_subpacket_size = GetWBE( p_peek ); p_peek += 2; /* subpacket_size */
1129 if( !i_subpacket_size || !i_frame_size || !i_coded_frame_size )
1130 return VLC_EGENERIC;
1131 p_peek += 2; /* ?? */
1133 if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
1135 fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */
1136 p_peek += 2; /* ?? */
1137 fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
1138 fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
1139 fmt.audio.i_blockalign = i_frame_size;
1141 if( i_version == 5 )
1143 memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4; /* genr */
1144 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
1148 p_peek += p_peek[0] + 1; /* descr 1 */
1149 memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
1150 p_peek += p_peek[0] + 1;
1153 msg_Dbg( p_demux, " - audio codec=%4.4s channels=%d rate=%dHz",
1154 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
1156 p_peek += 3; /* ?? */
1157 if( i_version == 5 ) p_peek++;
1158 /* Extra Data then: DWord + byte[] */
1159 fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
1162 switch( fmt.i_codec )
1164 case VLC_FOURCC('1','4','_','4'):
1165 /* fmt.audio.i_blockalign = 0x14 */
1167 case VLC_FOURCC('l','p','c','J'):
1168 /* fmt.audio.i_blockalign = 0x14 */
1169 fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
1172 case VLC_FOURCC('2','8','_','8'):
1174 fmt.audio.i_blockalign = i_coded_frame_size;
1177 case VLC_FOURCC( 'd','n','e','t' ):
1178 fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
1181 case VLC_FOURCC( 'r','a','a','c' ):
1182 case VLC_FOURCC( 'r','a','c','p' ):
1183 fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1185 if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
1186 if( fmt.i_extra > 0 )
1188 fmt.p_extra = malloc( fmt.i_extra );
1189 if( fmt.p_extra == NULL )
1191 msg_Err( p_demux, "Error in the extra data" );
1192 return VLC_EGENERIC;
1194 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1198 case VLC_FOURCC('s','i','p','r'):
1199 fmt.audio.i_flavor = i_flavor;
1200 case VLC_FOURCC('c','o','o','k'):
1201 case VLC_FOURCC('a','t','r','c'):
1202 if( !memcmp( p_genr, "genr", 4 ) )
1203 fmt.audio.i_blockalign = i_subpacket_size;
1205 fmt.audio.i_blockalign = i_coded_frame_size;
1207 if( fmt.i_extra > 0 )
1209 fmt.p_extra = malloc( fmt.i_extra );
1210 if( fmt.p_extra == NULL )
1212 msg_Err( p_demux, "Error in the extra data" );
1213 return VLC_EGENERIC;
1215 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
1219 case VLC_FOURCC('r','a','l','f'):
1220 msg_Dbg( p_demux, " - audio codec not supported=%4.4s",
1221 (char*)&fmt.i_codec );
1225 msg_Dbg( p_demux, " - unknown audio codec=%4.4s",
1226 (char*)&fmt.i_codec );
1230 if( fmt.i_codec != 0 )
1232 msg_Dbg( p_demux, " - extra data=%d", fmt.i_extra );
1234 tk = malloc( sizeof( real_track_t ) );
1240 tk->i_subpacket_h = i_subpacket_h;
1241 tk->i_subpacket_size = i_subpacket_size;
1242 tk->i_coded_frame_size = i_coded_frame_size;
1243 tk->i_frame_size = i_frame_size;
1245 tk->i_out_subpacket = 0;
1246 tk->i_subpacket = 0;
1247 tk->i_subpackets = 0;
1248 tk->p_subpackets = NULL;
1249 if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
1250 || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
1253 i_subpacket_h * i_frame_size / tk->i_subpacket_size;
1255 calloc( tk->i_subpackets, sizeof(block_t *) );
1257 else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
1260 i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
1262 calloc( tk->i_subpackets, sizeof(block_t *) );
1265 /* Check if the calloc went correctly */
1266 if( tk->p_subpackets == NULL )
1268 tk->i_subpackets = 0;
1269 msg_Err( p_demux, "Can't alloc subpacket" );
1270 return VLC_EGENERIC;
1273 tk->p_es = es_out_Add( p_demux->out, &fmt );
1275 TAB_APPEND( p_sys->i_track, p_sys->track, tk );