]> git.sesse.net Git - vlc/blobdiff - modules/demux/real.c
Real demux: kill warnings
[vlc] / modules / demux / real.c
index e92a1c83a17d0803c82b536f67cbad1f0eda656a..720528dc810d2235d9637223a95dfa07b97c7fba 100644 (file)
@@ -31,9 +31,9 @@
  *               - cook is ok.
  *               - raac, racp are ok.
  *               - dnet is twisted "The byte order of the data is reversed
- *                                  from standard AC3"
- *               - 28_8 seem problematic.
- *               - sipr should be fine, but our decoder suxx :)
+ *                                  from standard AC3" but ok
+ *               - 28_8 is ok.
+ *               - sipr is ok.
  *               - ralf is unsupported, but hardly any sample exist.
  *               - mp3 is unsupported, one sample exists...
  *
@@ -58,6 +58,8 @@
 #include <vlc_charset.h>
 #include <vlc_meta.h>
 
+#include <assert.h>
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -66,12 +68,11 @@ static void Close  ( vlc_object_t * );
 
 vlc_module_begin ()
     set_description( N_("Real demuxer" ) )
-    set_capability( "demux", 0 )
+    set_capability( "demux", 50 )
     set_category( CAT_INPUT )
     set_subcategory( SUBCAT_INPUT_DEMUX )
     set_callbacks( Open, Close )
-    add_shortcut( "real" )
-    add_shortcut( "rm" )
+    add_shortcut( "real", "rm" )
 vlc_module_end ()
 
 /*****************************************************************************
@@ -85,10 +86,10 @@ typedef struct
 
     es_out_id_t *p_es;
 
-    int         i_frame_size;
+    unsigned    i_frame_size;
 
     int         i_frame_num;
-    int         i_frame_pos;
+    unsigned    i_frame_pos;
     int         i_frame_slice;
     int         i_frame_slice_count;
     block_t     *p_frame;
@@ -103,6 +104,8 @@ typedef struct
     mtime_t     *p_subpackets_timecode;
     int         i_out_subpacket;
 
+    block_t     *p_sipr_packet;
+    int         i_sipr_subpacket_count;
     mtime_t     i_last_dts;
 } real_track_t;
 
@@ -111,7 +114,7 @@ typedef struct
     uint32_t i_file_offset;
     uint32_t i_time_offset;
     uint32_t i_frame_index;
-} rm_index_t;
+} real_index_t;
 
 struct demux_sys_t
 {
@@ -133,31 +136,37 @@ struct demux_sys_t
     int          i_track;
     real_track_t **track;
 
-    int     i_buffer;
+    size_t     i_buffer;
     uint8_t buffer[65536];
 
     int64_t     i_pcr;
-    vlc_meta_t *p_meta;
 
     int64_t     i_index_offset;
     bool        b_seek;
-    rm_index_t *p_index;
+    real_index_t *p_index;
 };
 
-static int Demux( demux_t *p_demux );
-static int Control( demux_t *p_demux, int i_query, va_list args );
+static const unsigned char i_subpacket_size_sipr[4] = { 29, 19, 37, 20 };
+
+static int Demux( demux_t * );
+static int Control( demux_t *, int i_query, va_list args );
+
+
+static void DemuxVideo( demux_t *, real_track_t *tk, mtime_t i_dts, unsigned i_flags );
+static void DemuxAudio( demux_t *, real_track_t *tk, mtime_t i_pts, unsigned i_flags );
+
+static int ControlSeekByte( demux_t *, int64_t i_bytes );
+static int ControlSeekTime( demux_t *, mtime_t i_time );
 
 static int HeaderRead( demux_t *p_demux );
 static int CodecParse( demux_t *p_demux, int i_len, int i_num );
 
-static char *StreamReadString2( stream_t *s );
-static char *MemoryReadString1( const uint8_t **pp_data, int *pi_data );
-
 static void     RVoid( const uint8_t **pp_data, int *pi_data, int i_size );
 static int      RLength( const uint8_t **pp_data, int *pi_data );
 static uint8_t  R8( const uint8_t **pp_data, int *pi_data );
 static uint16_t R16( const uint8_t **pp_data, int *pi_data );
 static uint32_t R32( const uint8_t **pp_data, int *pi_data );
+static void     SiprPacketReorder(uint8_t *buf, int sub_packet_h, int framesize);
 
 /*****************************************************************************
  * Open
@@ -196,7 +205,7 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_data_offset = 0;
     p_sys->i_track = 0;
     p_sys->track   = NULL;
-    p_sys->i_pcr   = 0;
+    p_sys->i_pcr   = VLC_TS_INVALID;
 
     p_sys->b_seek  = false;
     p_sys->b_real_audio = b_real_audio;
@@ -212,20 +221,8 @@ static int Open( vlc_object_t *p_this )
     /* RMF files */
     else if( HeaderRead( p_demux ) )
     {
-        int i;
         msg_Err( p_demux, "invalid header" );
-        for( i = 0; i < p_sys->i_track; i++ )
-        {
-            real_track_t *tk = p_sys->track[i];
-
-            if( tk->p_es )
-                es_out_Del( p_demux->out, tk->p_es );
-
-            free( tk );
-        }
-        if( p_sys->i_track > 0 )
-            free( p_sys->track );
-        free( p_sys );
+        Close( p_this );
         return VLC_EGENERIC;
     }
 
@@ -259,6 +256,8 @@ static void Close( vlc_object_t *p_this )
             free( tk->p_subpackets );
             free( tk->p_subpackets_timecode );
         }
+        if( tk->p_sipr_packet )
+            block_Release( tk->p_sipr_packet );
         free( tk );
     }
     if( p_sys->i_track > 0 )
@@ -277,14 +276,219 @@ static void Close( vlc_object_t *p_this )
 /*****************************************************************************
  * Demux:
  *****************************************************************************/
+static int Demux( demux_t *p_demux )
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
+    uint8_t     header[18];
+
+    if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
+        p_sys->i_data_packets_count )
+    {
+        if( stream_Read( p_demux->s, header, 18 ) < 18 )
+            return 0;
+
+        if( memcmp( header, "DATA", 4 ) )
+            return 0;
+
+        p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
+        p_sys->i_data_size   = GetDWBE( &header[4] );
+        p_sys->i_data_packets_count = GetDWBE( &header[10] );
+        p_sys->i_data_packets = 0;
+        p_sys->i_data_offset_next = GetDWBE( &header[14] );
+
+        msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
+                 p_sys->i_data_packets_count,
+                 (unsigned int)p_sys->i_data_offset_next );
+    }
+
+    /* Read Packet Header */
+    if( stream_Read( p_demux->s, header, 12 ) < 12 )
+        return 0;
+    //const int i_version = GetWBE( &header[0] );
+    const size_t  i_size = GetWBE( &header[2] ) - 12;
+    const int     i_id   = GetWBE( &header[4] );
+    const int64_t i_pts  = VLC_TS_0 + 1000 * GetDWBE( &header[6] );
+    const int     i_flags= header[11]; /* flags 0x02 -> keyframe */
+
+    p_sys->i_data_packets++;
+    if( i_size > sizeof(p_sys->buffer) )
+    {
+        msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
+        return 1;
+    }
+
+    p_sys->i_buffer = stream_Read( p_demux->s, p_sys->buffer, i_size );
+    if( p_sys->i_buffer < i_size )
+        return 0;
+
+    real_track_t *tk = NULL;
+    for( int i = 0; i < p_sys->i_track; i++ )
+    {
+        if( p_sys->track[i]->i_id == i_id )
+            tk = p_sys->track[i];
+    }
+
+    if( !tk )
+    {
+        msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
+        return 1;
+    }
+
+    if( tk->fmt.i_cat == VIDEO_ES )
+    {
+        DemuxVideo( p_demux, tk, i_pts, i_flags );
+    }
+    else
+    {
+        assert( tk->fmt.i_cat == AUDIO_ES );
+        DemuxAudio( p_demux, tk, i_pts, i_flags );
+    }
+
+    /* Update PCR */
+    mtime_t i_pcr = VLC_TS_INVALID;
+    for( int i = 0; i < p_sys->i_track; i++ )
+    {
+        real_track_t *tk = p_sys->track[i];
+
+        if( i_pcr <= VLC_TS_INVALID || ( tk->i_last_dts > VLC_TS_INVALID && tk->i_last_dts < i_pcr ) )
+            i_pcr = tk->i_last_dts;
+    }
+    if( i_pcr > VLC_TS_INVALID && i_pcr != p_sys->i_pcr )
+    {
+        p_sys->i_pcr = i_pcr;
+        es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
+    }
+    return 1;
+}
+
+/*****************************************************************************
+ * Control:
+ *****************************************************************************/
+static int Control( demux_t *p_demux, int i_query, va_list args )
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
+    double f, *pf;
+    int64_t i64;
+    int64_t *pi64;
+
+    switch( i_query )
+    {
+        case DEMUX_GET_POSITION:
+            pf = (double*) va_arg( args, double* );
+
+            /* read stream size maybe failed in rtsp streaming, 
+               so use duration to determin the position at first  */
+            if( p_sys->i_our_duration > 0 )
+            {
+                if( p_sys->i_pcr > VLC_TS_INVALID )
+                    *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
+                else
+                    *pf = 0.0;
+                return VLC_SUCCESS;
+            }
+
+            i64 = stream_Size( p_demux->s );
+            if( i64 > 0 )
+                *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
+            else
+                *pf = 0.0;
+            return VLC_SUCCESS;
+
+        case DEMUX_GET_TIME:
+            pi64 = (int64_t*)va_arg( args, int64_t * );
+
+            if( p_sys->i_our_duration > 0 )
+            {
+                *pi64 = p_sys->i_pcr > VLC_TS_INVALID ? p_sys->i_pcr : 0;
+                return VLC_SUCCESS;
+            }
+
+            /* same as GET_POSTION */
+            i64 = stream_Size( p_demux->s );
+            if( p_sys->i_our_duration > 0 && i64 > 0 )
+            {
+                *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
+                return VLC_SUCCESS;
+            }
+
+            *pi64 = 0;
+            return VLC_EGENERIC;
+
+        case DEMUX_SET_POSITION:
+            f = (double) va_arg( args, double );
+            i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
+
+            if( !p_sys->p_index && i64 != 0 )
+            {
+                /* TODO seek */
+                msg_Err( p_demux,"Seek No Index Real File failed!" );
+                return VLC_EGENERIC; // no index!
+            }
+            else if( i64 == 0 )
+            {
+                /* it is a rtsp stream , it is specials in access/rtsp/... */
+                msg_Dbg(p_demux, "Seek in real rtsp stream!");
+                p_sys->i_pcr = VLC_TS_0 + INT64_C(1000) * ( p_sys->i_our_duration * f  );
+                p_sys->b_seek = true;
+                return stream_Seek( p_demux->s, p_sys->i_pcr - VLC_TS_0 );
+            }
+            return ControlSeekByte( p_demux, i64 );
+
+        case DEMUX_SET_TIME:
+            if( !p_sys->p_index )
+                return VLC_EGENERIC;
+
+            i64 = (int64_t) va_arg( args, int64_t );
+            return ControlSeekTime( p_demux, i64 );
+
+        case DEMUX_GET_LENGTH:
+            pi64 = (int64_t*)va_arg( args, int64_t * );
+            if( p_sys->i_our_duration <= 0 )
+            {
+                *pi64 = 0;
+                return VLC_EGENERIC;
+            }
+
+            /* our stored duration is in ms, so... */
+            *pi64 = INT64_C(1000) * p_sys->i_our_duration;
+            return VLC_SUCCESS;
+
+        case DEMUX_GET_META:
+        {
+            vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
+
+            /* the core will crash if we provide NULL strings, so check
+             * every string first */
+            if( p_sys->psz_title )
+                vlc_meta_SetTitle( p_meta, p_sys->psz_title );
+            if( p_sys->psz_artist )
+                vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
+            if( p_sys->psz_copyright )
+                vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
+            if( p_sys->psz_description )
+                vlc_meta_SetDescription( p_meta, p_sys->psz_description );
+            return VLC_SUCCESS;
+        }
+
+        case DEMUX_GET_FPS:
+        default:
+            return VLC_EGENERIC;
+    }
+    return VLC_EGENERIC;
+}
+
+/*****************************************************************************
+ * Helpers: demux
+ *****************************************************************************/
 static void CheckPcr( demux_t *p_demux, real_track_t *tk, mtime_t i_dts )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
 
-    if( i_dts > 0 )
+    if( i_dts > VLC_TS_INVALID )
         tk->i_last_dts = i_dts;
 
-    if( p_sys->i_pcr > 0 || i_dts <= 0 )
+    if( p_sys->i_pcr > VLC_TS_INVALID || i_dts <= VLC_TS_INVALID )
         return;
 
     p_sys->i_pcr = i_dts;
@@ -349,17 +553,16 @@ static void DemuxVideo( demux_t *p_demux, real_track_t *tk, mtime_t i_dts, unsig
             tk->p_frame = block_New( p_demux, tk->i_frame_size );
             if( !tk->p_frame )
             {
-                tk->i_frame_slice = 0;
-                tk->i_frame_slice_count = 0;
+                tk->i_frame_size = 0;
                 return;
             }
 
             tk->p_frame->i_dts = i_dts;
-            tk->p_frame->i_pts = 0;
+            tk->p_frame->i_pts = VLC_TS_INVALID;
             if( i_flags & 0x02 )
                 tk->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
 
-            i_dts = 0;
+            i_dts = VLC_TS_INVALID;
         }
 
         int i_frame_data;
@@ -373,10 +576,12 @@ static void DemuxVideo( demux_t *p_demux, real_track_t *tk, mtime_t i_dts, unsig
             if( i_type == 2 && i_frame_data > i_pos )
                 i_frame_data = i_pos;
         }
+        if( i_frame_data > i_data )
+            break;
 
         /* */
         tk->i_frame_slice++;
-        if( tk->i_frame_slice > tk->i_frame_slice_count )
+        if( tk->i_frame_slice > tk->i_frame_slice_count || !tk->p_frame )
             break;
 
         /* */
@@ -413,22 +618,22 @@ static void DemuxVideo( demux_t *p_demux, real_track_t *tk, mtime_t i_dts, unsig
 static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned int i_flags )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
-
     uint8_t *p_buf = p_sys->buffer;
-    int y = tk->i_subpacket / ( tk->i_frame_size / tk->i_subpacket_size );
 
     /* Sanity check */
     if( (i_flags & 2) || p_sys->b_seek )
     {
-        y = tk->i_subpacket = 0;
+        tk->i_subpacket = 0;
         tk->i_out_subpacket = 0;
         p_sys->b_seek = false;
     }
 
-    if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
-        tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ) )
+    if( tk->fmt.i_codec == VLC_CODEC_COOK ||
+        tk->fmt.i_codec == VLC_CODEC_ATRAC3 )
     {
         const int i_num = tk->i_frame_size / tk->i_subpacket_size;
+        const int y = tk->i_subpacket / ( tk->i_frame_size / tk->i_subpacket_size );
+
         for( int i = 0; i < i_num; i++ )
         {
             block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
@@ -439,7 +644,7 @@ static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts
 
             memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
             p_block->i_dts =
-            p_block->i_pts = 0;
+            p_block->i_pts = VLC_TS_INVALID;
 
             p_buf += tk->i_subpacket_size;
 
@@ -460,23 +665,22 @@ static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts
     }
     else
     {
-        assert( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
-                tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) );
+        const int y = tk->i_subpacket / (tk->i_subpacket_h / 2);
+        assert( tk->fmt.i_codec == VLC_CODEC_RA_288 );
 
         for( int i = 0; i < tk->i_subpacket_h / 2; i++ )
         {
             block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
             if( !p_block )
                 return;
-            if( &p_buf[tk->i_subpacket_size] > &p_sys->buffer[p_sys->i_buffer] )
+            if( &p_buf[tk->i_coded_frame_size] > &p_sys->buffer[p_sys->i_buffer] )
                 return;
 
-            int i_index = (i * 2 * tk->i_frame_size) /
-                          tk->i_coded_frame_size + y;
+            int i_index = (i * 2 * tk->i_frame_size / tk->i_coded_frame_size) + y;
 
             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_block->i_pts = i_index == 0 ? i_pts : VLC_TS_INVALID;
 
             p_buf += tk->i_coded_frame_size;
 
@@ -495,7 +699,8 @@ static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts
            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] = 0;
+        tk->p_subpackets[tk->i_out_subpacket] = NULL;
+
         if( tk->p_subpackets_timecode[tk->i_out_subpacket] )
         {
             p_block->i_dts =
@@ -522,6 +727,7 @@ static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts
         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;
@@ -529,13 +735,13 @@ static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts
     if( p_sys->i_buffer < 2 )
         return;
 
-    int i_sub = (p_sys->buffer[1] >> 4)&0x0f;
+    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( int i = 0; i < i_sub; i++ )
+    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 );
@@ -549,7 +755,7 @@ static void DemuxAudioMethod2( demux_t *p_demux, real_track_t *tk, mtime_t i_pts
         p_sub += i_sub_size;
 
         p_block->i_dts =
-        p_block->i_pts = ( i == 0 ? i_pts : 0 );
+        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 );
@@ -566,7 +772,7 @@ static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t i_pts
     if( !p_block )
         return;
 
-    if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
+    if( tk->fmt.i_codec == VLC_CODEC_A52 )
     {
         uint8_t *p_src = p_sys->buffer;
         uint8_t *p_dst = p_block->p_buffer;
@@ -591,348 +797,219 @@ static void DemuxAudioMethod3( demux_t *p_demux, real_track_t *tk, mtime_t 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 int i_flags )
+// 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)
 {
-    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;
+    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 int Demux( demux_t *p_demux )
+static void DemuxAudioSipr( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
-    uint8_t     header[18];
+    block_t *p_block = tk->p_sipr_packet;
 
-    if( p_sys->i_data_packets >= p_sys->i_data_packets_count &&
-        p_sys->i_data_packets_count )
-    {
-        if( stream_Read( p_demux->s, header, 18 ) < 18 )
-            return 0;
-
-        if( memcmp( header, "DATA", 4 ) )
-            return 0;
-
-        p_sys->i_data_offset = stream_Tell( p_demux->s ) - 18;
-        p_sys->i_data_size   = GetDWBE( &header[4] );
-        p_sys->i_data_packets_count = GetDWBE( &header[10] );
-        p_sys->i_data_packets = 0;
-        p_sys->i_data_offset_next = GetDWBE( &header[14] );
-
-        msg_Dbg( p_demux, "entering new DATA packets=%d next=%u",
-                 p_sys->i_data_packets_count,
-                 (unsigned int)p_sys->i_data_offset_next );
-    }
-
-    /* Read Packet Header */
-    if( stream_Read( p_demux->s, header, 12 ) < 12 )
-        return 0;
-    //const int i_version = GetWBE( &header[0] );
-    const int     i_size = GetWBE( &header[2] ) - 12;
-    const int     i_id   = GetWBE( &header[4] );
-    const int64_t i_pts  = 1 + 1000 * GetDWBE( &header[6] );
-    const int     i_flags= header[11]; /* flags 0x02 -> keyframe */
-
-    p_sys->i_data_packets++;
-
-    if( i_size <= 0 )
-    {
-        msg_Err( p_demux, "Got a NUKK size to read. (Invalid format?)" );
-        return 1;
-    }
-
-    assert( i_size <= sizeof(p_sys->buffer) );
-
-    p_sys->i_buffer = stream_Read( p_demux->s, p_sys->buffer, i_size );
-    if( p_sys->i_buffer < i_size )
-        return 0;
-
-    real_track_t *tk = NULL;
-    for( int i = 0; i < p_sys->i_track; i++ )
-    {
-        if( p_sys->track[i]->i_id == i_id )
-            tk = p_sys->track[i];
-    }
-
-    if( !tk )
-    {
-        msg_Warn( p_demux, "unknown track id(0x%x)", i_id );
-        return 1;
-    }
+    if( p_sys->i_buffer < tk->i_frame_size )
+        return;
 
-    if( tk->fmt.i_cat == VIDEO_ES )
-    {
-        DemuxVideo( p_demux, tk, i_pts, i_flags );
-    }
-    else
+    if( !p_block )
     {
-        assert( tk->fmt.i_cat == AUDIO_ES );
-        DemuxAudio( p_demux, tk, i_pts, i_flags );
+        p_block = block_New( p_demux, tk->i_frame_size * tk->i_subpacket_h );
+        if( !p_block )
+            return;
+        tk->p_sipr_packet = p_block;
     }
 
-    /* Update PCR */
-    mtime_t i_pcr = 0;
-    for( int i = 0; i < p_sys->i_track; i++ )
-    {
-        real_track_t *tk = p_sys->track[i];
-
-        if( i_pcr <= 0 || ( tk->i_last_dts > 0 && tk->i_last_dts > i_pcr ) )
-            i_pcr = tk->i_last_dts;
-    }
-    if( i_pcr > 0 && i_pcr != p_sys->i_pcr )
+    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_sys->i_pcr = i_pcr;
-        es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
+        p_block->i_dts =
+        p_block->i_pts = i_pts;
     }
-    return 1;
-}
 
-/*****************************************************************************
- * Control:
- *****************************************************************************/
-static int ControlGoToIndex( demux_t *p_demux, rm_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;
-    rm_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;
-    rm_index_t *p_index = p_sys->p_index;
+    if( ++tk->i_sipr_subpacket_count < tk->i_subpacket_h )
+        return;
 
-    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 );
+    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 int Control( demux_t *p_demux, int i_query, va_list args )
+static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsigned i_flags )
 {
-    demux_sys_t *p_sys = p_demux->p_sys;
-    double f, *pf;
-    int64_t i64;
-    int64_t *pi64;
-
-    switch( i_query )
+    switch( tk->fmt.i_codec )
     {
-        case DEMUX_GET_POSITION:
-            pf = (double*) va_arg( args, double* );
-
-            /* read stream size maybe failed in rtsp streaming, 
-               so use duration to determin the position at first  */
-            if( p_sys->i_our_duration > 0 )
-            {
-                *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
-                return VLC_SUCCESS;
-            }
-
-            *pf = 0.0;
-            i64 = stream_Size( p_demux->s );
-            if( i64 > 0 )
-                *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
-            return VLC_SUCCESS;
-
-        case DEMUX_GET_TIME:
-            pi64 = (int64_t*)va_arg( args, int64_t * );
-
-            if( p_sys->i_our_duration > 0 )
-            {
-                *pi64 = p_sys->i_pcr;
-                return VLC_SUCCESS;
-            }
-
-            /* same as GET_POSTION */
-            i64 = stream_Size( p_demux->s );
-            if( p_sys->i_our_duration > 0 && i64 > 0 )
-            {
-                *pi64 = (int64_t)( 1000.0 * p_sys->i_our_duration * stream_Tell( p_demux->s ) / i64 );
-                return VLC_SUCCESS;
-            }
-
-            *pi64 = 0;
-            return VLC_EGENERIC;
-
-        case DEMUX_SET_POSITION:
-            f = (double) va_arg( args, double );
-            i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
-
-            if( !p_sys->p_index && i64 != 0 )
-            {
-                /* TODO seek */
-                msg_Err( p_demux,"Seek No Index Real File failed!" );
-                return VLC_EGENERIC; // no index!
-            }
-            else if( i64 == 0 )
-            {
-                /* it is a rtsp stream , it is specials in access/rtsp/... */
-                msg_Dbg(p_demux, "Seek in real rtsp stream!");
-                p_sys->i_pcr = INT64_C(1000) * ( p_sys->i_our_duration * f  );
-                p_sys->b_seek = true;
-                return stream_Seek( p_demux->s, p_sys->i_pcr );
-            }
-            return ControlSeekByte( p_demux, i64 );
-
-        case DEMUX_SET_TIME:
-            if( !p_sys->p_index )
-                return VLC_EGENERIC;
-
-            i64 = (int64_t) va_arg( args, int64_t );
-            return ControlSeekTime( p_demux, i64 );
-
-        case DEMUX_GET_LENGTH:
-            pi64 = (int64_t*)va_arg( args, int64_t * );
-            *pi64 = 0;
-            if( p_sys->i_our_duration <= 0 )
-                return VLC_EGENERIC;
-
-            /* our stored duration is in ms, so... */
-            *pi64 = INT64_C(1000) * p_sys->i_our_duration;
-            return VLC_SUCCESS;
-
-        case DEMUX_GET_META:
-        {
-            vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
-
-            /* the core will crash if we provide NULL strings, so check
-             * every string first */
-            if( p_sys->psz_title )
-                vlc_meta_SetTitle( p_meta, p_sys->psz_title );
-            if( p_sys->psz_artist )
-                vlc_meta_SetArtist( p_meta, p_sys->psz_artist );
-            if( p_sys->psz_copyright )
-                vlc_meta_SetCopyright( p_meta, p_sys->psz_copyright );
-            if( p_sys->psz_description )
-                vlc_meta_SetDescription( p_meta, p_sys->psz_description );
-            return VLC_SUCCESS;
-        }
-
-        case DEMUX_GET_FPS:
-        default:
-            return VLC_EGENERIC;
+    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;
     }
-    return VLC_EGENERIC;
 }
 
 /*****************************************************************************
- * ReadRealIndex:
+ * Helpers: seek/control
  *****************************************************************************/
+static int ControlGoToIndex( demux_t *p_demux, real_index_t *p_index )
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
 
-static void ReadRealIndex( demux_t *p_demux )
+    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;
-    uint8_t       buffer[100];
-    uint32_t      i_id;
-    uint32_t      i_size;
-    int           i_version;
-    unsigned int  i;
+    real_index_t *p_index = p_sys->p_index;
 
-    uint32_t      i_index_count;
+    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;
 
-    if ( p_sys->i_index_offset == 0 )
-        return;
+    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 );
+}
 
-    stream_Seek( p_demux->s, p_sys->i_index_offset );
+/*****************************************************************************
+ * Helpers: header reading
+ *****************************************************************************/
 
-    if ( stream_Read( p_demux->s, buffer, 20 ) < 20 )
-        return ;
+/**
+ * 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];
 
-    i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
-    i_size      = GetDWBE( &buffer[4] );
-    i_version   = GetWBE( &buffer[8] );
+    if( stream_Read( s, p_tmp, 2 ) < 2 )
+        return NULL;
 
-    msg_Dbg( p_demux, "Real index %4.4s size=%d version=%d",
-                 (char*)&i_id, i_size, i_version );
+    const int i_length = GetWBE( p_tmp );
+    if( i_length <= 0 )
+        return NULL;
 
-    if( (i_size < 20) && (i_id != VLC_FOURCC('I','N','D','X')) )
-        return;
+    char *psz_string = calloc( 1, i_length + 1 );
 
-    i_index_count = GetDWBE( &buffer[10] );
+    stream_Read( s, psz_string, i_length ); /* Valid even if !psz_string */
 
-    msg_Dbg( p_demux, "Real Index : num : %d ", i_index_count );
+    if( psz_string )
+        EnsureUTF8( psz_string );
+    return psz_string;
+}
 
-    if( i_index_count >= ( 0xffffffff / sizeof( rm_index_t ) ) )
-        return;
+/**
+ * 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;
 
-    if( GetDWBE( &buffer[16] ) > 0 )
-        msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
-                        GetDWBE( &buffer[16] )  );
+    char *psz_string = NULL;
 
-    p_sys->p_index = malloc( ( i_index_count + 1 ) * sizeof( rm_index_t ) );
-    if( p_sys->p_index == NULL )
-        return;
+    if( i_data < 1 )
+        goto exit;
 
-    for( i = 0; i < i_index_count; i++ )
-    {
-        if( stream_Read( p_demux->s, buffer, 14 ) < 14 )
-            return ;
+    int i_length = *p_data++; i_data--;
+    if( i_length > i_data )
+        i_length = i_data;
 
-        if( GetWBE( &buffer[0] ) != 0 )
-        {
-            msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
-                              GetWBE( &buffer[0] ) );
-            return;
-        }
+    if( i_length > 0 )
+    {
+        psz_string = strndup( (const char*)p_data, i_length );
+        if( psz_string )
+            EnsureUTF8( psz_string );
 
-        p_sys->p_index[i].i_time_offset = GetDWBE( &buffer[2] );
-        p_sys->p_index[i].i_file_offset = GetDWBE( &buffer[6] );
-        p_sys->p_index[i].i_frame_index = GetDWBE( &buffer[10] );
-        msg_Dbg( p_demux,
-                 "Real Index: time %"PRIu32" file %"PRIu32" frame %"PRIu32,
-                 p_sys->p_index[i].i_time_offset,
-                 p_sys->p_index[i].i_file_offset,
-                 p_sys->p_index[i].i_frame_index );
+        p_data += i_length;
+        i_data -= i_length;
     }
-    memset( p_sys->p_index + i_index_count, 0, sizeof( rm_index_t ) );
+
+exit:
+    *pp_data = p_data;
+    *pi_data = i_data;
+    return psz_string;
 }
 
-/*****************************************************************************
- * HeaderRead:
- *****************************************************************************/
+/**
+ * This function parses(skip) the .RMF identification chunk.
+ */
 static int HeaderRMF( demux_t *p_demux )
 {
     uint8_t p_buffer[8];
@@ -944,6 +1021,9 @@ static int HeaderRMF( demux_t *p_demux )
              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;
@@ -979,6 +1059,9 @@ static int HeaderPROP( demux_t *p_demux )
 
     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;
@@ -1005,11 +1088,13 @@ static int HeaderCONT( demux_t *p_demux )
 
     return VLC_SUCCESS;
 }
+/**
+ * This function parses the MDPR (Media properties) chunk.
+ */
 static int HeaderMDPR( demux_t *p_demux )
 {
     uint8_t p_buffer[30];
 
-    /* Media properties header */
     if( stream_Read( p_demux->s, p_buffer, 30 ) < 30 )
         return VLC_EGENERIC;
 
@@ -1054,11 +1139,15 @@ static int HeaderMDPR( demux_t *p_demux )
     if( i_size > 0 )
     {
         CodecParse( p_demux, i_size, i_num );
-        if( stream_Read( p_demux->s, NULL, i_size ) < i_size )
+        unsigned size = stream_Read( p_demux->s, NULL, i_size );
+        if( 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;
@@ -1078,13 +1167,92 @@ static int HeaderDATA( demux_t *p_demux, uint32_t i_size )
              (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;
 
-    p_sys->p_meta = vlc_meta_New();
-
     for( ;; )
     {
         const int64_t i_stream_position = stream_Tell( p_demux->s );
@@ -1159,7 +1327,7 @@ static int HeaderRead( demux_t *p_demux )
     {
         const int64_t i_position = stream_Tell( p_demux->s );
 
-        ReadRealIndex( p_demux );
+        HeaderINDX( p_demux );
 
         if( stream_Seek( p_demux->s, i_position ) )
             return VLC_EGENERIC;
@@ -1167,67 +1335,6 @@ static int HeaderRead( demux_t *p_demux )
     return VLC_SUCCESS;
 }
 
-/**
- * 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 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;
-}
-
 static void CodecMetaRead( demux_t *p_demux, const uint8_t **pp_data, int *pi_data )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
@@ -1283,19 +1390,19 @@ static int CodecVideoParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
     {
     case 0x10003000:
     case 0x10003001:
-        fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
+        fmt.i_codec = VLC_CODEC_RV13;
         break;
     case 0x20001000:
     case 0x20100001:
     case 0x20200002:
     case 0x20201002:
-        fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
+        fmt.i_codec = VLC_CODEC_RV20;
         break;
     case 0x30202002:
-        fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
+        fmt.i_codec = VLC_CODEC_RV30;
         break;
     case 0x40000000:
-        fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
+        fmt.i_codec = VLC_CODEC_RV40;
         break;
     }
     msg_Dbg( p_demux, "    - video %4.4s %dx%d - %8.8x",
@@ -1318,6 +1425,7 @@ static int CodecVideoParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
     tk->i_frame_size = 0;
     tk->p_frame = NULL;
     tk->i_last_dts = 0;
+    tk->p_sipr_packet = NULL;
     tk->p_es = es_out_Add( p_demux->out, &fmt );
 
     TAB_APPEND( p_sys->i_track, p_sys->track, tk );
@@ -1330,7 +1438,7 @@ static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
 
     if( i_data < 6 )
         return VLC_EGENERIC;
-    
+
     int i_flavor = 0;
     int i_coded_frame_size = 0;
     int i_subpacket_h = 0;
@@ -1372,9 +1480,9 @@ static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
         i_subpacket_h = R16( &p_data, &i_data );
         i_frame_size = R16( &p_data, &i_data );
         i_subpacket_size = R16( &p_data, &i_data );
-        if( (!i_subpacket_size && !p_sys->b_real_audio ) ||
-            !i_frame_size || !i_coded_frame_size )
-             return VLC_EGENERIC;
+        if( !i_frame_size || !i_coded_frame_size )
+            return VLC_EGENERIC;
+
         RVoid( &p_data, &i_data, 2 + (i_version == 5 ? 6 : 0 ) );
 
         fmt.audio.i_rate = R16( &p_data, &i_data );
@@ -1422,22 +1530,24 @@ static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
     switch( fmt.i_codec )
     {
     case VLC_FOURCC('l','p','c','J'):
-        fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
     case VLC_FOURCC('1','4','_','4'):
+        fmt.i_codec = VLC_CODEC_RA_144;
         fmt.audio.i_blockalign = 0x14 ;
         break;
 
     case VLC_FOURCC('2','8','_','8'):
+        fmt.i_codec = VLC_CODEC_RA_288;
         fmt.audio.i_blockalign = i_coded_frame_size;
         break;
 
+    case VLC_FOURCC( 'a','5','2',' ' ):
     case VLC_FOURCC( 'd','n','e','t' ):
-        fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
+        fmt.i_codec = VLC_CODEC_A52;
         break;
 
     case VLC_FOURCC( 'r','a','a','c' ):
     case VLC_FOURCC( 'r','a','c','p' ):
-        fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
+        fmt.i_codec = VLC_CODEC_MP4A;
 
         if( i_extra_codec > 0 )
         {
@@ -1446,8 +1556,8 @@ static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
         }
         if( i_extra_codec > 0 )
         {
-            fmt.p_extra = malloc( fmt.i_extra );
-            if( !fmt.p_extra || fmt.i_extra > i_data )
+            fmt.p_extra = malloc( i_extra_codec );
+            if( !fmt.p_extra || i_extra_codec > i_data )
                 return VLC_ENOMEM;
 
             fmt.i_extra = i_extra_codec;
@@ -1455,19 +1565,39 @@ static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
         }
         break;
 
-    case VLC_FOURCC('s','i','p','r'):
-        fmt.audio.i_flavor = i_flavor;
-    case VLC_FOURCC('c','o','o','k'):
-    case VLC_FOURCC('a','t','r','c'):
+    case VLC_FOURCC( 's','i','p','r' ):
+        fmt.i_codec = VLC_CODEC_SIPR;
+        if( i_flavor > 3 )
+            return VLC_EGENERIC;
+
+        i_subpacket_size = i_subpacket_size_sipr[i_flavor];
+        // The libavcodec sipr decoder requires stream bitrate
+        // to be set during initialization so that the correct mode
+        // can be selected.
+        fmt.i_bitrate = fmt.audio.i_rate;
+        msg_Dbg( p_demux, "    - sipr flavor=%i", i_flavor );
+
+    case VLC_FOURCC( 'c','o','o','k' ):
+    case VLC_FOURCC( 'a','t','r','c' ):
+        if( i_subpacket_size <= 0 || i_frame_size / i_subpacket_size <= 0 )
+        {
+            es_format_Clean( &fmt );
+            return VLC_EGENERIC;
+        }
         if( !memcmp( p_genr, "genr", 4 ) )
             fmt.audio.i_blockalign = i_subpacket_size;
         else
             fmt.audio.i_blockalign = i_coded_frame_size;
 
+        if( fmt.i_codec == VLC_FOURCC( 'c','o','o','k' ) )
+            fmt.i_codec = VLC_CODEC_COOK;
+        else if( fmt.i_codec == VLC_FOURCC( 'a','t','r','c' ) )
+            fmt.i_codec = VLC_CODEC_ATRAC3;
+
         if( i_extra_codec > 0 )
         {
-            fmt.p_extra = malloc( fmt.i_extra );
-            if( !fmt.p_extra || fmt.i_extra > i_data )
+            fmt.p_extra = malloc( i_extra_codec );
+            if( !fmt.p_extra || i_extra_codec > i_data )
                 return VLC_ENOMEM;
 
             fmt.i_extra = i_extra_codec;
@@ -1486,7 +1616,7 @@ static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
                  (char*)&fmt.i_codec );
         break;
     }
-    msg_Dbg( p_demux, "        - extra data=%d", fmt.i_extra );
+    msg_Dbg( p_demux, "    - extra data=%d", fmt.i_extra );
 
     /* */
     real_track_t *tk = malloc( sizeof( *tk ) );
@@ -1510,8 +1640,12 @@ static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
     tk->i_subpackets = 0;
     tk->p_subpackets = NULL;
     tk->p_subpackets_timecode = NULL;
-    if( fmt.i_codec == VLC_FOURCC('c','o','o','k') ||
-        fmt.i_codec == VLC_FOURCC('a','t','r','c') )
+
+    tk->p_sipr_packet          = NULL;
+    tk->i_sipr_subpacket_count = 0;
+
+    if( fmt.i_codec == VLC_CODEC_COOK ||
+        fmt.i_codec == VLC_CODEC_ATRAC3 )
     {
         tk->i_subpackets =
             i_subpacket_h * i_frame_size / tk->i_subpacket_size;
@@ -1520,7 +1654,7 @@ static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
         tk->p_subpackets_timecode =
             calloc( tk->i_subpackets , sizeof( int64_t ) );
     }
-    else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
+    else if( fmt.i_codec == VLC_CODEC_RA_288 )
     {
         tk->i_subpackets =
             i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
@@ -1531,7 +1665,7 @@ static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
     }
 
     /* Check if the calloc went correctly */
-    if( !tk->p_subpackets && !tk->p_subpackets_timecode)
+    if( tk->i_subpacket > 0 && ( !tk->p_subpackets || !tk->p_subpackets_timecode ) )
     {
         free( tk->p_subpackets_timecode );
         free( tk->p_subpackets );
@@ -1567,7 +1701,10 @@ static int CodecParse( demux_t *p_demux, int i_len, int i_num )
     }
     return VLC_SUCCESS;
 }
-/* Small memory buffer helpers */
+
+/*****************************************************************************
+ * Helpers: memory buffer fct.
+ *****************************************************************************/
 static void RVoid( const uint8_t **pp_data, int *pi_data, int i_size )
 {
     if( i_size > *pi_data )