+ 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;
+
+ unsigned 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( unsigned 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 : VLC_TS_INVALID;
+
+ 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_CODEC_A52 )
+ {
+ 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 );
+}
+
+// Sipr packet re-ordering code and index table borrowed from
+// the MPlayer Realmedia demuxer.
+static const uint8_t sipr_swap_index_table[38][2] = {
+ { 0, 63 }, { 1, 22 }, { 2, 44 }, { 3, 90 },
+ { 5, 81 }, { 7, 31 }, { 8, 86 }, { 9, 58 },
+ { 10, 36 }, { 12, 68 }, { 13, 39 }, { 14, 73 },
+ { 15, 53 }, { 16, 69 }, { 17, 57 }, { 19, 88 },
+ { 20, 34 }, { 21, 71 }, { 24, 46 }, { 25, 94 },
+ { 26, 54 }, { 28, 75 }, { 29, 50 }, { 32, 70 },
+ { 33, 92 }, { 35, 74 }, { 38, 85 }, { 40, 56 },
+ { 42, 87 }, { 43, 65 }, { 45, 59 }, { 48, 79 },
+ { 49, 93 }, { 51, 89 }, { 55, 95 }, { 61, 76 },
+ { 67, 83 }, { 77, 80 }
+};
+
+static void SiprPacketReorder(uint8_t *buf, int sub_packet_h, int framesize)
+{
+ int n, bs = sub_packet_h * framesize * 2 / 96; // nibbles per subpacket
+
+ for (n = 0; n < 38; n++) {
+ int j;
+ int i = bs * sipr_swap_index_table[n][0];
+ int o = bs * sipr_swap_index_table[n][1];
+
+ /* swap 4 bit-nibbles of block 'i' with 'o' */
+ for (j = 0; j < bs; j++, i++, o++) {
+ int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF,
+ y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF;
+
+ buf[o >> 1] = (x << (4 * (o & 1))) |
+ (buf[o >> 1] & (0xF << (4 * !(o & 1))));
+ buf[i >> 1] = (y << (4 * (i & 1))) |
+ (buf[i >> 1] & (0xF << (4 * !(i & 1))));
+ }
+ }
+}
+
+static void DemuxAudioSipr( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ block_t *p_block = tk->p_sipr_packet;
+
+ if( p_sys->i_buffer < tk->i_frame_size
+ || tk->i_sipr_subpacket_count >= tk->i_subpacket_h )
+ return;
+
+ if( !p_block )
+ {
+ p_block = block_New( p_demux, tk->i_frame_size * tk->i_subpacket_h );
+ if( !p_block )
+ return;
+ tk->p_sipr_packet = p_block;
+ }
+ memcpy( p_block->p_buffer + tk->i_sipr_subpacket_count * tk->i_frame_size,
+ p_sys->buffer, tk->i_frame_size );
+ if (!tk->i_sipr_subpacket_count)
+ {
+ p_block->i_dts =
+ p_block->i_pts = i_pts;
+ }
+
+ if( ++tk->i_sipr_subpacket_count < tk->i_subpacket_h )
+ return;
+
+ SiprPacketReorder(p_block->p_buffer, tk->i_subpacket_h, tk->i_frame_size);
+ CheckPcr( p_demux, tk, p_block->i_pts );
+ es_out_Send( p_demux->out, tk->p_es, p_block );
+ tk->i_sipr_subpacket_count = 0;
+ tk->p_sipr_packet = NULL;
+}
+
+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_CODEC_COOK:
+ case VLC_CODEC_ATRAC3:
+ case VLC_CODEC_RA_288:
+ DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
+ break;
+ case VLC_CODEC_MP4A:
+ DemuxAudioMethod2( p_demux, tk, i_pts );
+ break;
+ case VLC_CODEC_SIPR:
+ DemuxAudioSipr( 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 = xcalloc( 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] ) );