]> git.sesse.net Git - vlc/blobdiff - modules/demux/real.c
matroska demuxer: supports Real Video
[vlc] / modules / demux / real.c
index b1a785a3f245c262d3fbafea887960029d1fe607..66a56cd759ce09fe4ae19e09936d696677d8ff3a 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * real.c: Real demuxer.
  *****************************************************************************
- * Copyright (C) 2004 the VideoLAN team
+ * Copyright (C) 2004, 2006-2007 the VideoLAN team
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+/**
+ * Status of this demuxer:
+ * Real Media format
+ * -----------------
+ *
+ * version v3 w/ 14_4/lpcJ is ok.
+ * version v4/5: - atrac3 is ok.
+ *               - 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 :)
+ *               - ralf is unsupported, but hardly any sample exist.
+ *               - mp3 is unsupported, one sample exists...
+ *
+ * Real Audio Only
+ * ---------------
+ * Not supported...
+ */
+
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
-
 #include <vlc/vlc.h>
-#include <vlc/input.h>
+
+
+#include <vlc_demux.h>
+#include <vlc_charset.h>
+#include <vlc_meta.h>
 
 /*****************************************************************************
  * Module descriptor
@@ -59,6 +82,16 @@ typedef struct
     int         i_frame;
     block_t     *p_frame;
 
+    int         i_subpacket_h;
+    int         i_subpacket_size;
+    int         i_coded_frame_size;
+    int         i_frame_size;
+
+    int         i_subpacket;
+    int         i_subpackets;
+    block_t     **p_subpackets;
+    int         i_out_subpacket;
+
 } real_track_t;
 
 struct demux_sys_t
@@ -69,6 +102,14 @@ struct demux_sys_t
     uint32_t i_data_packets;
     int64_t  i_data_offset_next;
 
+    int  i_our_duration;
+    int  i_mux_rate;
+
+    char* psz_title;
+    char* psz_artist;
+    char* psz_copyright;
+    char* psz_description;
+
     int          i_track;
     real_track_t **track;
 
@@ -91,10 +132,15 @@ static int Open( vlc_object_t *p_this )
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys;
 
-    uint8_t     *p_peek;
+    const uint8_t *p_peek;
 
     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) return VLC_EGENERIC;
-    if( strncmp( (char *)p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
+    if( !memcmp( p_peek, ".ra", 3 ) )
+    {
+        msg_Warn( p_demux, ".ra files unsuported" );
+        return VLC_EGENERIC;
+    }
+    if( memcmp( p_peek, ".RMF", 4 ) ) return VLC_EGENERIC;
 
     /* Fill p_demux field */
     p_demux->pf_demux = Demux;
@@ -145,19 +191,27 @@ static void Close( vlc_object_t *p_this )
     for( i = 0; i < p_sys->i_track; i++ )
     {
         real_track_t *tk = p_sys->track[i];
+        int j = tk->i_subpackets;
 
-        if( tk->p_frame )
+        if( tk->p_frame ) block_Release( tk->p_frame );
+        es_format_Clean( &tk->fmt );
+
+        while(  j-- )
         {
-            block_Release( tk->p_frame );
+            if( tk->p_subpackets[ j ] )
+                block_Release( tk->p_subpackets[ j ] );
         }
+        if( tk->i_subpackets ) free( tk->p_subpackets );
+
         free( tk );
     }
 
-    if( p_sys->i_track > 0 )
-    {
-        free( p_sys->track );
-    }
+    if( p_sys->psz_title ) free( p_sys->psz_title );
+    if( p_sys->psz_artist ) free( p_sys->psz_artist );
+    if( p_sys->psz_copyright ) free( p_sys->psz_copyright );
+    if( p_sys->psz_description ) free( p_sys->psz_description );
 
+    if( p_sys->i_track > 0 ) free( p_sys->track );
     free( p_sys );
 }
 
@@ -169,10 +223,8 @@ static int Demux( demux_t *p_demux )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
     uint8_t     header[18];
-    int         i_size;
-    int         i_id;
+    int         i_size, i_id, i_flags, i;
     int64_t     i_pts;
-    int         i;
     real_track_t *tk = NULL;
     vlc_bool_t  b_selected;
 
@@ -204,8 +256,7 @@ static int Demux( demux_t *p_demux )
     i_id   = GetWBE( &header[4] );
     i_pts  = 1000 * GetDWBE( &header[6] );
     i_pts += 1000; /* Avoid 0 pts */
-
-    /* header[11] -> flags 0x02 -> keyframe */
+    i_flags= header[11]; /* flags 0x02 -> keyframe */
 
 #if 0
     msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
@@ -218,10 +269,7 @@ static int Demux( demux_t *p_demux )
 
     for( i = 0; i < p_sys->i_track; i++ )
     {
-        if( p_sys->track[i]->i_id == i_id )
-        {
-            tk = p_sys->track[i];
-        }
+        if( p_sys->track[i]->i_id == i_id ) tk = p_sys->track[i];
     }
 
     if( tk == NULL )
@@ -460,7 +508,7 @@ static int Demux( demux_t *p_demux )
                             (int64_t)p_sys->i_pcr );
         }
 
-        if( tk->fmt.i_codec == VLC_FOURCC( 'm', 'p', '4', 'a' ) )
+        if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
         {
             int     i_sub = (p_sys->buffer[1] >> 4)&0x0f;
             uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
@@ -482,6 +530,75 @@ static int Demux( demux_t *p_demux )
                 }
             }
         }
+        else if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
+                 tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c') ||
+                 tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r') ||
+                 tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8') )
+        {
+            uint8_t *p_buf = p_sys->buffer;
+            int y = tk->i_subpacket / ( tk->i_frame_size /tk->i_subpacket_size);
+            int i_index, i;
+
+            /* Sanity check */
+            if( i_flags & 2 ) y = tk->i_subpacket = 0;
+
+            if( tk->fmt.i_codec == VLC_FOURCC( 'c', 'o', 'o', 'k' ) ||
+                tk->fmt.i_codec == VLC_FOURCC( 'a', 't', 'r', 'c' ))
+            for( i = 0; i < tk->i_frame_size / tk->i_subpacket_size; i++ )
+            {
+                block_t *p_block = block_New( p_demux, tk->i_subpacket_size );
+                memcpy( p_block->p_buffer, p_buf, tk->i_subpacket_size );
+                p_buf += tk->i_subpacket_size;
+
+                i_index = tk->i_subpacket_h * i +
+                    ((tk->i_subpacket_h + 1) / 2) * (y&1) + (y>>1);
+
+                p_block->i_dts = p_block->i_pts = i_pts;
+                tk->p_subpackets[i_index] = p_block;
+                tk->i_subpacket++;
+            }
+
+            if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
+                tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
+            for( i = 0; i < tk->i_subpacket_h / 2; i++ )
+            {
+                block_t *p_block = block_New( p_demux, tk->i_coded_frame_size);
+                memcpy( p_block->p_buffer, p_buf, tk->i_coded_frame_size );
+                p_buf += tk->i_coded_frame_size;
+
+                i_index = (i * 2 * tk->i_frame_size) /
+                    tk->i_coded_frame_size + y;
+
+                p_block->i_dts = p_block->i_pts = i_pts;
+                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] = 0;
+
+                if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
+                es_out_Send( p_demux->out, tk->p_es, p_block );
+
+                tk->i_out_subpacket++;
+            }
+
+            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;
+            }
+        }
         else
         {
             block_t *p_block = block_New( p_demux, i_size );
@@ -504,9 +621,7 @@ static int Demux( demux_t *p_demux )
             {
                 memcpy( p_block->p_buffer, p_sys->buffer, i_size );
             }
-            p_block->i_dts =
-            p_block->i_pts = i_pts;
-
+            p_block->i_dts = p_block->i_pts = i_pts;
             es_out_Send( p_demux->out, tk->p_es, p_block );
         }
     }
@@ -520,13 +635,16 @@ static int Demux( demux_t *p_demux )
  *****************************************************************************/
 static int Control( demux_t *p_demux, int i_query, va_list args )
 {
-#if 0
     demux_sys_t *p_sys = p_demux->p_sys;
+#if 0
     double f, *pf;
-    int64_t i64, *pi64;
+    int64_t i64;
+#endif
+    int64_t *pi64;
 
     switch( i_query )
     {
+#if 0
         case DEMUX_GET_POSITION:
             pf = (double*) va_arg( args, double* );
             i64 = stream_Size( p_demux->s );
@@ -539,6 +657,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
                 *pf = 0.0;
             }
             return VLC_SUCCESS;
+
         case DEMUX_SET_POSITION:
             f = (double) va_arg( args, double );
             i64 = stream_Size( p_demux->s );
@@ -556,23 +675,50 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             }
             *pi64 = 0;
             return VLC_EGENERIC;
+#endif
 
         case DEMUX_GET_LENGTH:
             pi64 = (int64_t*)va_arg( args, int64_t * );
-            if( p_sys->i_mux_rate > 0 )
+            /* the commented following lines are fen's implementation, which doesn't seem to
+             * work for one reason or another -- FK */
+            /*if( p_sys->i_mux_rate > 0 )
             {
                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
+                return VLC_SUCCESS;
+            }*/
+            if( p_sys->i_our_duration > 0 )
+            {
+                /* our stored duration is in ms, so... */
+                *pi64 = (int64_t)1000 * p_sys->i_our_duration;
                 return VLC_SUCCESS;
             }
             *pi64 = 0;
             return VLC_EGENERIC;
 
+        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_SET_TIME:
         case DEMUX_GET_FPS:
         default:
             return VLC_EGENERIC;
     }
-#endif
     return VLC_EGENERIC;
 }
 
@@ -603,7 +749,7 @@ static int HeaderRead( demux_t *p_demux )
         msg_Dbg( p_demux, "object %4.4s size=%d version=%d",
                  (char*)&i_id, i_size, i_version );
 
-        if( i_size < 10 )
+        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;
@@ -634,6 +780,10 @@ static int HeaderRead( demux_t *p_demux )
             msg_Dbg( p_demux, "    - index offset=%d", GetDWBE(&header[28]) );
             msg_Dbg( p_demux, "    - data offset=%d", GetDWBE(&header[32]) );
             msg_Dbg( p_demux, "    - num streams=%d", GetWBE(&header[36]) );
+            /* set the duration for export in control */
+            p_sys->i_our_duration = (int)GetDWBE(&header[20]);
             i_flags = GetWBE(&header[38]);
             msg_Dbg( p_demux, "    - flags=0x%x %s%s%s",
                      i_flags,
@@ -646,6 +796,9 @@ static int HeaderRead( demux_t *p_demux )
         {
             int i_len;
             char *psz;
+            /* FIXME FIXME: should convert from whatever the character
+             * encoding of the input meta data is to UTF-8. */
 
             stream_Read( p_demux->s, header, 2 );
             if( ( i_len = GetWBE( header ) ) > 0 )
@@ -655,6 +808,8 @@ static int HeaderRead( demux_t *p_demux )
                 psz[i_len] = '\0';
 
                 msg_Dbg( p_demux, "    - title=`%s'", psz );
+                EnsureUTF8( psz );
+                asprintf( &p_sys->psz_title, psz );
                 free( psz );
                 i_skip -= i_len;
             }
@@ -668,6 +823,8 @@ static int HeaderRead( demux_t *p_demux )
                 psz[i_len] = '\0';
 
                 msg_Dbg( p_demux, "    - author=`%s'", psz );
+                EnsureUTF8( psz );
+                asprintf( &p_sys->psz_artist, psz );
                 free( psz );
                 i_skip -= i_len;
             }
@@ -681,6 +838,8 @@ static int HeaderRead( demux_t *p_demux )
                 psz[i_len] = '\0';
 
                 msg_Dbg( p_demux, "    - copyright=`%s'", psz );
+                EnsureUTF8( psz );
+                asprintf( &p_sys->psz_copyright, psz );
                 free( psz );
                 i_skip -= i_len;
             }
@@ -694,6 +853,8 @@ static int HeaderRead( demux_t *p_demux )
                 psz[i_len] = '\0';
 
                 msg_Dbg( p_demux, "    - comment=`%s'", psz );
+                EnsureUTF8( psz );
+                asprintf( &p_sys->psz_description, psz );
                 free( psz );
                 i_skip -= i_len;
             }
@@ -716,6 +877,7 @@ static int HeaderRead( demux_t *p_demux )
             msg_Dbg( p_demux, "    - start time=%d", GetDWBE(&header[18]) );
             msg_Dbg( p_demux, "    - preroll=%d", GetDWBE(&header[22]) );
             msg_Dbg( p_demux, "    - duration=%d", GetDWBE(&header[26]) );
             i_skip -= 30;
 
             stream_Read( p_demux->s, header, 1 );
@@ -791,12 +953,12 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
     demux_sys_t *p_sys = p_demux->p_sys;
     es_format_t fmt;
     real_track_t *tk;
-    uint8_t *p_peek;
+    const uint8_t *p_peek;
 
     msg_Dbg( p_demux, "    - specific data len=%d", i_len );
     if( stream_Peek(p_demux->s, &p_peek, i_len) < i_len ) return VLC_EGENERIC;
 
-    if( !strncmp( (char *)&p_peek[4], "VIDO", 4 ) )
+    if( ( i_len >= 8 ) && !memcmp( &p_peek[4], "VIDO", 4 ) )
     {
         es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[8], p_peek[9],
                         p_peek[10], p_peek[11] ) );
@@ -816,11 +978,29 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
         {
             fmt.i_codec = VLC_FOURCC( 'R','V','1','3' );
         }
+        else if( GetDWBE( &p_peek[30] ) == 0x20001000 ||
+                 GetDWBE( &p_peek[30] ) == 0x20100001 ||
+                 GetDWBE( &p_peek[30] ) == 0x20200002 )
+        {
+            fmt.i_codec = VLC_FOURCC( 'R','V','2','0' );
+        }
+        else if( GetDWBE( &p_peek[30] ) == 0x30202002 )
+        {
+            fmt.i_codec = VLC_FOURCC( 'R','V','3','0' );
+        }
+        else if( GetDWBE( &p_peek[30] ) == 0x40000000 )
+        {
+            fmt.i_codec = VLC_FOURCC( 'R','V','4','0' );
+        }
 
         msg_Dbg( p_demux, "    - video %4.4s %dx%d",
                  (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
 
         tk = malloc( sizeof( real_track_t ) );
+        tk->i_out_subpacket = 0;
+        tk->i_subpacket = 0;
+        tk->i_subpackets = 0;
+        tk->p_subpackets = NULL;
         tk->i_id = i_num;
         tk->fmt = fmt;
         tk->i_frame = 0;
@@ -831,71 +1011,164 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
     }
     else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
     {
-        int i_version = GetWBE( &p_peek[4] );
         int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
         int i_frame_size, i_subpacket_size;
-
+        char p_genr[4];
+        int i_version = GetWBE( &p_peek[4] );   /* [0..3] = '.','r','a',0xfd */
         msg_Dbg( p_demux, "    - audio version=%d", i_version );
 
-        p_peek += 6;
+        p_peek += 6;                                          /* 4 + version */
         es_format_Init( &fmt, AUDIO_ES, 0 );
 
         if( i_version == 3 )
         {
-            msg_Dbg( p_demux, "    - audio version 3 is not supported!" );
-            return VLC_EGENERIC;
-        }
+            int i_len;
+            char *psz;
 
-        p_peek += 2; /* 00 00 */
-        p_peek += 4; /* .ra4 or .ra5 */
-        p_peek += 4; /* ?? */
-        p_peek += 2; /* version (4 or 5) */
-        i_header_size = GetDWBE( p_peek ); p_peek += 4; /* header size */
-        i_flavor = GetWBE( p_peek ); p_peek += 2; /* codec flavor */
-        i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4;
-        p_peek += 4; /* ?? */
-        p_peek += 4; /* ?? */
-        p_peek += 4; /* ?? */
-        i_subpacket_h = GetWBE( p_peek ); p_peek += 2;
-        i_frame_size = GetWBE( p_peek ); p_peek += 2;
-        i_subpacket_size = GetWBE( p_peek ); p_peek += 2;
-        p_peek += 2; /* ?? */
-
-        if( i_version == 5 ) p_peek += 6; /* 0, srate, 0 */
-
-        fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2;
-        p_peek += 2; /* ?? */
-        fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;
-        fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2;
-        fmt.audio.i_blockalign = i_frame_size;
-
-        if( i_version == 5 )
-        {
-            p_peek += 4; /* genr */
+            i_header_size = GetWBE( p_peek ); p_peek += 2;  /* Size from now */
+            p_peek += 10;                                         /* Unknown */
+
+            p_peek += 4;                                        /* Data Size */
+
+            /* Title */
+            i_len = *p_peek ; p_peek++;
+            if( i_len > 0 )
+            {
+                psz = malloc( i_len + 1 );
+                memcpy( psz, p_peek, i_len );
+                psz[i_len] = '\0';
+
+                msg_Dbg( p_demux, "    - title=`%s'", psz );
+                EnsureUTF8( psz );
+                asprintf( &p_sys->psz_title, psz );
+                free( psz );
+            }
+            p_peek += i_len;
+
+            /* Authors */
+            i_len = *p_peek ; p_peek++;
+            if( i_len > 0 )
+            {
+                psz = malloc( i_len + 1 );
+                memcpy( psz, p_peek, i_len );
+                psz[i_len] = '\0';
+
+                msg_Dbg( p_demux, "    - artist=`%s'", psz );
+                EnsureUTF8( psz );
+                asprintf( &p_sys->psz_artist, psz );
+                free( psz );
+            }
+            p_peek += i_len;
+
+            /* Copyright */
+            i_len = *p_peek ; p_peek++;
+            if( i_len > 0 )
+            {
+                psz = malloc( i_len + 1 );
+                memcpy( psz, p_peek, i_len );
+                psz[i_len] = '\0';
+
+                msg_Dbg( p_demux, "    - Copyright=`%s'", psz );
+                EnsureUTF8( psz );
+                asprintf( &p_sys->psz_copyright, psz );
+                free( psz );
+            }
+            p_peek += i_len;
+
+            /* Comment */
+            i_len = *p_peek ; p_peek++;
+            if( i_len > 0 )
+            {
+                psz = malloc( i_len + 1 );
+                memcpy( psz, p_peek, i_len );
+                psz[i_len] = '\0';
+
+                msg_Dbg( p_demux, "    - Comment=`%s'", psz );
+                EnsureUTF8( psz );
+                asprintf( &p_sys->psz_description, psz );
+                free( psz );
+            }
+            /* This might be unusefull */
+            p_peek += i_len;
+
+            p_peek ++;                                           /* Unknown */
+            p_peek ++;                                 /* FourCC length = 4 */
             memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
+            /* Up to here :) */
+
+            fmt.audio.i_channels = 1;      /* This is always the case in rm3 */
+            fmt.audio.i_rate = 8000;
+
+            msg_Dbg( p_demux, "    - audio codec=%4.4s channels=%d rate=%dHz",
+                 (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
         }
-        else
+        else /* RMF version 4/5 */
         {
-            p_peek += p_peek[0] + 1; /* descr 1 */
-            memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 ); /* descr 2 */
-            p_peek += p_peek[0] + 1;
-        }
+            p_peek += 2;                                           /* 00 00 */
+            p_peek += 4;                                    /* .ra4 or .ra5 */
+            p_peek += 4;                                       /* data size */
+            p_peek += 2;                                /* version (4 or 5) */
+            i_header_size = GetDWBE( p_peek ); p_peek += 4;   /* header size */
+            i_flavor = GetWBE( p_peek ); p_peek += 2;        /* codec flavor */
+            i_coded_frame_size = GetDWBE( p_peek ); p_peek += 4; /* coded frame size*/
+            p_peek += 4;                                               /* ?? */
+            p_peek += 4;                                               /* ?? */
+            p_peek += 4;                                               /* ?? */
+            i_subpacket_h = GetWBE( p_peek ); p_peek += 2;              /* 1 */
+            i_frame_size = GetWBE( p_peek ); p_peek += 2;      /* frame size */
+            i_subpacket_size = GetWBE( p_peek ); p_peek += 2;  /* subpacket_size */
+            p_peek += 2;                                               /* ?? */
+
+            if( i_version == 5 ) p_peek += 6;                 /* 0, srate, 0 */
+
+            fmt.audio.i_rate = GetWBE( p_peek ); p_peek += 2; /* Sample Rate */
+            p_peek += 2;                                               /* ?? */
+            fmt.audio.i_bitspersample = GetWBE( p_peek ); p_peek += 2;/* Sure?*/
+            fmt.audio.i_channels = GetWBE( p_peek ); p_peek += 2; /* Channels */
+            fmt.audio.i_blockalign = i_frame_size;
+
+            if( i_version == 5 )
+            {
+                memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4;    /* genr */
+                memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
+            }
+            else
+            {
+                p_peek += p_peek[0] + 1;                          /* descr 1 */
+                memcpy( (char *)&fmt.i_codec, p_peek + 1, 4 );    /* descr 2 */
+                p_peek += p_peek[0] + 1;
+            }
 
-        msg_Dbg( p_demux, "    - audio codec=%4.4s channels=%d rate=%dHz",
+            msg_Dbg( p_demux, "    - audio codec=%4.4s channels=%d rate=%dHz",
                  (char*)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate );
 
-        p_peek += 3; /* ?? */
-        if( i_version == 5 ) p_peek++;
+            p_peek += 3;                                               /* ?? */
+            if( i_version == 5 ) p_peek++;
+            /* Extra Data then: DWord + byte[] */
+            fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
+        }
 
         switch( fmt.i_codec )
         {
-        case VLC_FOURCC( 'd', 'n', 'e', 't' ):
-            fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
+        case VLC_FOURCC('1','4','_','4'):
+            /* fmt.audio.i_blockalign = 0x14 */
+            break;
+        case VLC_FOURCC('l','p','c','J'):
+            /* fmt.audio.i_blockalign = 0x14 */
+            fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
             break;
 
-        case VLC_FOURCC( 'r', 'a', 'a', 'c' ):
-        case VLC_FOURCC( 'r', 'a', 'c', 'p' ):
-            fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
+        case VLC_FOURCC('2','8','_','8'):
+            fmt.i_extra = 0;
+            fmt.audio.i_blockalign = i_coded_frame_size;
+            break;
+
+        case VLC_FOURCC( 'd','n','e','t' ):
+            fmt.i_codec = VLC_FOURCC( 'a','5','2',' ' );
+            break;
+
+        case VLC_FOURCC( 'r','a','a','c' ):
+        case VLC_FOURCC( 'r','a','c','p' ):
             if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
             if( fmt.i_extra > 0 )
             {
@@ -903,20 +1176,25 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
             }
 
-            fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
+            fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
             break;
 
+        case VLC_FOURCC('s','i','p','r'):
+            fmt.audio.i_flavor = i_flavor;
         case VLC_FOURCC('c','o','o','k'):
-            fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
-            fmt.p_extra = malloc( fmt.i_extra + 10 );
-
-            ((short*)(fmt.p_extra))[0] = i_subpacket_size;
-            ((short*)(fmt.p_extra))[1] = i_subpacket_h;
-            ((short*)(fmt.p_extra))[2] = i_flavor;
-            ((short*)(fmt.p_extra))[3] = i_coded_frame_size;
-            ((short*)(fmt.p_extra))[4] = fmt.i_extra;
-            if( fmt.i_extra ) memcpy( fmt.p_extra + 10, p_peek, fmt.i_extra );
-            fmt.i_extra += 10;
+        case VLC_FOURCC('a','t','r','c'):
+            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_extra ) break;
+            fmt.p_extra = malloc( fmt.i_extra );
+            memcpy( fmt.p_extra, p_peek, fmt.i_extra );
+            break;
+
+        case VLC_FOURCC('r','a','l','f'):
+            msg_Dbg( p_demux, "    - audio codec not supported=%4.4s",
+                     (char*)&fmt.i_codec );
             break;
 
         default:
@@ -927,6 +1205,8 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
 
         if( fmt.i_codec != 0 )
         {
+            int i;
+
             msg_Dbg( p_demux, "        - extra data=%d", fmt.i_extra );
 
             tk = malloc( sizeof( real_track_t ) );
@@ -934,6 +1214,34 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
             tk->fmt = fmt;
             tk->i_frame = 0;
             tk->p_frame = NULL;
+
+            tk->i_subpacket_h = i_subpacket_h;
+            tk->i_subpacket_size = i_subpacket_size;
+            tk->i_coded_frame_size = i_coded_frame_size;
+            tk->i_frame_size = i_frame_size;
+
+            tk->i_out_subpacket = 0;
+            tk->i_subpacket = 0;
+            tk->i_subpackets = 0;
+            tk->p_subpackets = NULL;
+            if( fmt.i_codec == VLC_FOURCC('c','o','o','k')
+             || fmt.i_codec == VLC_FOURCC('a','t','r','c') )
+            {
+                tk->i_subpackets =
+                    i_subpacket_h * i_frame_size / tk->i_subpacket_size;
+                tk->p_subpackets =
+                    malloc( tk->i_subpackets * sizeof(block_t *) );
+            }
+            else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
+            {
+                tk->i_subpackets =
+                    i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
+                tk->p_subpackets =
+                    malloc( tk->i_subpackets * sizeof(block_t *) );
+            }
+
+            for( i = 0; i < tk->i_subpackets; i++ ) tk->p_subpackets[i] = NULL;
+
             tk->p_es = es_out_Add( p_demux->out, &fmt );
 
             TAB_APPEND( p_sys->i_track, p_sys->track, tk );