+ memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
+ p_block->i_dts =
+ p_block->i_pts = i_index == 0 ? i_pts : 0;
+
+ p_buf += tk->i_coded_frame_size;
+
+ if( tk->p_subpackets[i_index] != NULL )
+ {
+ msg_Dbg(p_demux, "p_subpackets[ %d ] not null!", i_index );
+ block_Release( tk->p_subpackets[i_index] );
+ }
+
+ tk->p_subpackets[i_index] = p_block;
+ tk->i_subpacket++;
+ }
+ }
+
+ while( tk->i_out_subpacket != tk->i_subpackets &&
+ tk->p_subpackets[tk->i_out_subpacket] )
+ {
+ block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
+ tk->p_subpackets[tk->i_out_subpacket] = NULL;
+
+ if( tk->p_subpackets_timecode[tk->i_out_subpacket] )
+ {
+ p_block->i_dts =
+ p_block->i_pts = tk->p_subpackets_timecode[tk->i_out_subpacket];
+
+ tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
+ }
+ tk->i_out_subpacket++;
+
+ CheckPcr( p_demux, tk, p_block->i_pts );
+ es_out_Send( p_demux->out, tk->p_es, p_block );
+ }
+
+ if( tk->i_subpacket == tk->i_subpackets &&
+ tk->i_out_subpacket != tk->i_subpackets )
+ {
+ msg_Warn( p_demux, "i_subpacket != i_out_subpacket, "
+ "this shouldn't happen" );
+ }
+
+ if( tk->i_subpacket == tk->i_subpackets )
+ {
+ tk->i_subpacket = 0;
+ tk->i_out_subpacket = 0;
+ }
+}
+static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+
+ if( p_sys->i_buffer < 2 )
+ return;
+
+ int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
+ if( p_sys->i_buffer < 2+2*i_sub )
+ return;
+
+ uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
+
+ for( int i = 0; i < i_sub; i++ )
+ {
+ const int i_sub_size = GetWBE( &p_sys->buffer[2+i*2] );
+ block_t *p_block = block_New( p_demux, i_sub_size );
+ if( !p_block )
+ break;
+
+ if( &p_sub[i_sub_size] > &p_sys->buffer[p_sys->i_buffer] )
+ break;
+
+ memcpy( p_block->p_buffer, p_sub, i_sub_size );
+ p_sub += i_sub_size;
+
+ p_block->i_dts =
+ p_block->i_pts = ( i == 0 ? i_pts : 0 );
+
+ CheckPcr( p_demux, tk, p_block->i_pts );
+ es_out_Send( p_demux->out, tk->p_es, p_block );
+ }
+}
+static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+
+ if( p_sys->i_buffer <= 0 )
+ return;
+
+ block_t *p_block = block_New( p_demux, p_sys->i_buffer );
+ if( !p_block )
+ return;
+
+ if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
+ {
+ uint8_t *p_src = p_sys->buffer;
+ uint8_t *p_dst = p_block->p_buffer;
+
+ /* byte swap data */
+ while( p_dst < &p_block->p_buffer[p_sys->i_buffer - 1])
+ {
+ *p_dst++ = p_src[1];
+ *p_dst++ = p_src[0];
+
+ p_src += 2;
+ }
+ }
+ else
+ {
+ memcpy( p_block->p_buffer, p_sys->buffer, p_sys->i_buffer );
+ }
+ p_block->i_dts =
+ p_block->i_pts = i_pts;
+
+ CheckPcr( p_demux, tk, p_block->i_pts );
+ es_out_Send( p_demux->out, tk->p_es, p_block );
+}
+
+static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned i_flags )
+{
+ switch( tk->fmt.i_codec )
+ {
+ case VLC_FOURCC( 'c', 'o', 'o', 'k' ):
+ case VLC_FOURCC( 'a', 't', 'r', 'c' ):
+ case VLC_FOURCC( 's', 'i', 'p', 'r' ):
+ case VLC_FOURCC( '2', '8', '_', '8' ):
+ DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
+ break;
+ case VLC_FOURCC( 'm','p','4','a' ):
+ DemuxAudioMethod2( p_demux, tk, i_pts );
+ break;
+ default:
+ DemuxAudioMethod3( p_demux, tk, i_pts );
+ break;
+ }
+}
+
+/*****************************************************************************
+ * Helpers: seek/control
+ *****************************************************************************/
+static int ControlGoToIndex( demux_t *p_demux, real_index_t *p_index )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+
+ p_sys->b_seek = true;
+ p_sys->i_pcr = INT64_C(1000) * p_index->i_time_offset;
+ for( int i = 0; i < p_sys->i_track; i++ )
+ p_sys->track[i]->i_last_dts = 0;
+ return stream_Seek( p_demux->s, p_index->i_file_offset );
+}
+static int ControlSeekTime( demux_t *p_demux, mtime_t i_time )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ real_index_t *p_index = p_sys->p_index;
+
+ while( p_index->i_file_offset != 0 )
+ {
+ if( p_index->i_time_offset * INT64_C(1000) > i_time )
+ {
+ if( p_index != p_sys->p_index )
+ p_index--;
+ break;
+ }
+ p_index++;
+ }
+ if( p_index->i_file_offset == 0 )
+ return VLC_EGENERIC;
+ return ControlGoToIndex( p_demux, p_index );
+}
+static int ControlSeekByte( demux_t *p_demux, int64_t i_bytes )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ real_index_t *p_index = p_sys->p_index;
+
+ while( p_index->i_file_offset != 0 )
+ {
+ if( p_index->i_file_offset > i_bytes )
+ {
+ if( p_index != p_sys->p_index )
+ p_index--;
+ break;
+ }
+ p_index++;
+ }
+ if( p_index->i_file_offset == 0 )
+ return VLC_EGENERIC;
+ return ControlGoToIndex( p_demux, p_index );
+}
+
+/*****************************************************************************
+ * Helpers: header reading
+ *****************************************************************************/
+
+/**
+ * This function will read a pascal string with size stored in 2 bytes from
+ * a stream_t.
+ *
+ * FIXME what is the right charset ?
+ */
+static char *StreamReadString2( stream_t *s )
+{
+ uint8_t p_tmp[2];
+
+ if( stream_Read( s, p_tmp, 2 ) < 2 )
+ return NULL;
+
+ const int i_length = GetWBE( p_tmp );
+ if( i_length <= 0 )
+ return NULL;
+
+ char *psz_string = calloc( 1, i_length + 1 );
+
+ stream_Read( s, psz_string, i_length ); /* Valid even if !psz_string */
+
+ if( psz_string )
+ EnsureUTF8( psz_string );
+ return psz_string;
+}
+
+/**
+ * This function will read a pascal string with size stored in 1 byte from a
+ * memory buffer.
+ *
+ * FIXME what is the right charset ?
+ */
+static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data )
+{
+ const uint8_t *p_data = *pp_data;
+ int i_data = *pi_data;
+
+ char *psz_string = NULL;
+
+ if( i_data < 1 )
+ goto exit;
+
+ int i_length = *p_data++; i_data--;
+ if( i_length > i_data )
+ i_length = i_data;
+
+ if( i_length > 0 )
+ {
+ psz_string = strndup( (const char*)p_data, i_length );
+ if( psz_string )
+ EnsureUTF8( psz_string );
+
+ p_data += i_length;
+ i_data -= i_length;
+ }
+
+exit:
+ *pp_data = p_data;
+ *pi_data = i_data;
+ return psz_string;
+}
+
+/**
+ * This function parses(skip) the .RMF identification chunk.
+ */
+static int HeaderRMF( demux_t *p_demux )
+{
+ uint8_t p_buffer[8];
+
+ if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
+ return VLC_EGENERIC;
+
+ msg_Dbg( p_demux, " - file version=0x%x num headers=%d",
+ GetDWBE( &p_buffer[0] ), GetDWBE( &p_buffer[4] ) );
+ return VLC_SUCCESS;
+}
+/**
+ * This function parses the PROP properties chunk.
+ */
+static int HeaderPROP( demux_t *p_demux )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+
+ uint8_t p_buffer[40];
+ int i_flags;
+
+ if( stream_Read( p_demux->s, p_buffer, 40 ) < 40 )
+ return VLC_EGENERIC;
+
+ msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
+ GetDWBE(&p_buffer[0]), GetDWBE(&p_buffer[4]) );
+ msg_Dbg( p_demux, " - max packet size=%d avg bitrate=%d",
+ GetDWBE(&p_buffer[8]), GetDWBE(&p_buffer[12]) );
+ msg_Dbg( p_demux, " - packets count=%d", GetDWBE(&p_buffer[16]) );
+ msg_Dbg( p_demux, " - duration=%d ms", GetDWBE(&p_buffer[20]) );
+ msg_Dbg( p_demux, " - preroll=%d ms", GetDWBE(&p_buffer[24]) );
+ msg_Dbg( p_demux, " - index offset=%d", GetDWBE(&p_buffer[28]) );
+ msg_Dbg( p_demux, " - data offset=%d", GetDWBE(&p_buffer[32]) );
+ msg_Dbg( p_demux, " - num streams=%d", GetWBE(&p_buffer[36]) );
+
+ /* set the duration for export in control */
+ p_sys->i_our_duration = GetDWBE(&p_buffer[20]);
+
+ p_sys->i_index_offset = GetDWBE(&p_buffer[28]);
+
+ i_flags = GetWBE(&p_buffer[38]);
+ msg_Dbg( p_demux, " - flags=0x%x %s%s%s",
+ i_flags,
+ i_flags&0x0001 ? "PN_SAVE_ENABLED " : "",
+ i_flags&0x0002 ? "PN_PERFECT_PLAY_ENABLED " : "",
+ i_flags&0x0004 ? "PN_LIVE_BROADCAST" : "" );
+
+ return VLC_SUCCESS;
+}
+/**
+ * This functions parses the CONT commentairs chunk.
+ */
+static int HeaderCONT( demux_t *p_demux )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+
+ /* */
+ p_sys->psz_title = StreamReadString2( p_demux->s );
+ if( p_sys->psz_title )
+ msg_Dbg( p_demux, " - title=`%s'", p_sys->psz_title );
+
+ /* */
+ p_sys->psz_artist = StreamReadString2( p_demux->s );
+ if( p_sys->psz_artist )
+ msg_Dbg( p_demux, " - artist=`%s'", p_sys->psz_artist );
+
+ /* */
+ p_sys->psz_copyright = StreamReadString2( p_demux->s );
+ if( p_sys->psz_copyright )
+ msg_Dbg( p_demux, " - copyright=`%s'", p_sys->psz_copyright );
+
+ /* */
+ p_sys->psz_description = StreamReadString2( p_demux->s );
+ if( p_sys->psz_description )
+ msg_Dbg( p_demux, " - comment=`%s'", p_sys->psz_description );
+
+ return VLC_SUCCESS;
+}
+/**
+ * This function parses the MDPR (Media properties) chunk.
+ */
+static int HeaderMDPR( demux_t *p_demux )
+{
+ uint8_t p_buffer[30];
+
+ if( stream_Read( p_demux->s, p_buffer, 30 ) < 30 )
+ return VLC_EGENERIC;
+
+ const int i_num = GetWBE( &p_buffer[0] );
+ msg_Dbg( p_demux, " - id=0x%x", i_num );
+ msg_Dbg( p_demux, " - max bitrate=%d avg bitrate=%d",
+ GetDWBE(&p_buffer[2]), GetDWBE(&p_buffer[6]) );
+ msg_Dbg( p_demux, " - max packet size=%d avg packet size=%d",
+ GetDWBE(&p_buffer[10]), GetDWBE(&p_buffer[14]) );
+ msg_Dbg( p_demux, " - start time=%d", GetDWBE(&p_buffer[18]) );
+ msg_Dbg( p_demux, " - preroll=%d", GetDWBE(&p_buffer[22]) );
+ msg_Dbg( p_demux, " - duration=%d", GetDWBE(&p_buffer[26]) );
+
+ /* */
+ const uint8_t *p_peek;
+ int i_peek_org = stream_Peek( p_demux->s, &p_peek, 2 * 256 );
+ int i_peek = i_peek_org;
+ if( i_peek <= 0 )
+ return VLC_EGENERIC;
+
+ char *psz_name = MemoryReadString1( &p_peek, &i_peek );
+ if( psz_name )
+ {
+ msg_Dbg( p_demux, " - name=`%s'", psz_name );
+ free( psz_name );
+ }
+ char *psz_mime = MemoryReadString1( &p_peek, &i_peek );
+ if( psz_mime )
+ {
+ msg_Dbg( p_demux, " - mime=`%s'", psz_mime );
+ free( psz_mime );
+ }
+ const int i_skip = i_peek_org - i_peek;
+ if( i_skip > 0 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
+ return VLC_EGENERIC;
+
+ /* */
+ if( stream_Read( p_demux->s, p_buffer, 4 ) < 4 )
+ return VLC_EGENERIC;
+
+ const uint32_t i_size = GetDWBE( p_buffer );
+ if( i_size > 0 )
+ {
+ CodecParse( p_demux, i_size, i_num );
+ if( stream_Read( p_demux->s, NULL, i_size ) < i_size )
+ return VLC_EGENERIC;
+ }
+ return VLC_SUCCESS;
+}
+/**
+ * This function parses DATA chunk (it contains the actual movie data).
+ */
+static int HeaderDATA( demux_t *p_demux, uint32_t i_size )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ uint8_t p_buffer[8];
+
+ if( stream_Read( p_demux->s, p_buffer, 8 ) < 8 )
+ return VLC_EGENERIC;
+
+ p_sys->i_data_offset = stream_Tell( p_demux->s ) - 10;
+ p_sys->i_data_size = i_size;
+ p_sys->i_data_packets_count = GetDWBE( p_buffer );
+ p_sys->i_data_packets = 0;
+ p_sys->i_data_offset_next = GetDWBE( &p_buffer[4] );
+
+ msg_Dbg( p_demux, " - packets count=%d next=%u",
+ p_sys->i_data_packets_count,
+ (unsigned int)p_sys->i_data_offset_next );
+ return VLC_SUCCESS;
+}
+/**
+ * This function parses the INDX (movie index chunk).
+ * It is optional but seeking without it is ... hard.
+ */
+static void HeaderINDX( demux_t *p_demux )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ uint8_t buffer[20];
+
+ uint32_t i_index_count;
+
+ if( p_sys->i_index_offset == 0 )
+ return;
+
+ stream_Seek( p_demux->s, p_sys->i_index_offset );
+
+ if( stream_Read( p_demux->s, buffer, 20 ) < 20 )
+ return ;
+
+ const uint32_t i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
+ const uint32_t i_size = GetDWBE( &buffer[4] );
+ int i_version = GetWBE( &buffer[8] );
+
+ msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
+ (char*)&i_id, i_size, i_version );
+
+ if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
+ return;
+
+ i_index_count = GetDWBE( &buffer[10] );
+
+ msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
+
+ if( i_index_count >= ( 0xffffffff / sizeof(*p_sys->p_index) ) )
+ return;
+
+ if( GetDWBE( &buffer[16] ) > 0 )
+ msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
+ GetDWBE( &buffer[16] ) );
+
+ /* One extra entry is allocated (that MUST be set to 0) to identify the
+ * end of the index.
+ * TODO add a clean entry count (easier to build index on the fly) */
+ p_sys->p_index = calloc( i_index_count + 1, sizeof(*p_sys->p_index) );
+ if( !p_sys->p_index )
+ return;
+
+ for( unsigned int i = 0; i < i_index_count; i++ )
+ {
+ uint8_t p_entry[14];
+
+ if( stream_Read( p_demux->s, p_entry, 14 ) < 14 )
+ return ;
+
+ if( GetWBE( &p_entry[0] ) != 0 )
+ {
+ msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
+ GetWBE( &p_entry[0] ) );
+ return;
+ }
+
+ real_index_t *p_idx = &p_sys->p_index[i];
+
+ p_idx->i_time_offset = GetDWBE( &p_entry[2] );
+ p_idx->i_file_offset = GetDWBE( &p_entry[6] );
+ p_idx->i_frame_index = GetDWBE( &p_entry[10] );
+
+#if 0
+ msg_Dbg( p_demux,
+ "Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32,
+ p_idx->i_time_offset,
+ p_idx->i_file_offset,
+ p_idx->i_frame_index );
+#endif
+ }
+}
+
+
+/**
+ * This function parses the complete RM headers and move the
+ * stream pointer to the data to be read.
+ */
+static int HeaderRead( demux_t *p_demux )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+
+ for( ;; )
+ {
+ const int64_t i_stream_position = stream_Tell( p_demux->s );
+ uint8_t header[100]; /* FIXME */
+
+ /* Read the header */
+ if( stream_Read( p_demux->s, header, 10 ) < 10 )
+ return VLC_EGENERIC;
+
+ const uint32_t i_id = VLC_FOURCC( header[0], header[1],
+ header[2], header[3] );
+ const uint32_t i_size = GetDWBE( &header[4] );
+ const int i_version = GetWBE( &header[8] );
+
+ msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
+ (char*)&i_id, i_size, i_version );
+
+ /* */
+ if( i_size < 10 && i_id != VLC_FOURCC('D','A','T','A') )
+ {
+ msg_Dbg( p_demux, "invalid size for object %4.4s", (char*)&i_id );
+ return VLC_EGENERIC;
+ }
+
+ int i_ret;
+ switch( i_id )
+ {
+ case VLC_FOURCC('.','R','M','F'):
+ i_ret = HeaderRMF( p_demux );
+ break;
+ case VLC_FOURCC('P','R','O','P'):
+ i_ret = HeaderPROP( p_demux );
+ break;
+ case VLC_FOURCC('C','O','N','T'):
+ i_ret = HeaderCONT( p_demux );
+ break;
+ case VLC_FOURCC('M','D','P','R'):
+ i_ret = HeaderMDPR( p_demux );
+ break;
+ case VLC_FOURCC('D','A','T','A'):
+ i_ret = HeaderDATA( p_demux, i_size );
+ break;
+ default:
+ /* unknow header */
+ msg_Dbg( p_demux, "unknown chunk" );
+ i_ret = VLC_SUCCESS;
+ break;
+ }
+ if( i_ret )
+ return i_ret;
+
+ if( i_id == VLC_FOURCC('D','A','T','A') ) /* In this case, parsing is finished */
+ break;
+
+ /* Skip unread data */
+ const int64_t i_stream_current = stream_Tell( p_demux->s );
+ const int64_t i_stream_skip = (i_stream_position + i_size) - i_stream_current;
+
+ if( i_stream_skip > 0 )
+ {
+ if( stream_Read( p_demux->s, NULL, i_stream_skip ) != i_stream_skip )
+ return VLC_EGENERIC;
+ }
+ else if( i_stream_skip < 0 )
+ {