+ *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 )
+ 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 = 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 );
+ unsigned size = stream_Read( p_demux->s, NULL, i_size );
+ if( size < i_size )