]> git.sesse.net Git - vlc/blobdiff - modules/demux/real.c
Use VLC_ENOMEM instead of VLC_EGENERIC.
[vlc] / modules / demux / real.c
index 176392aeb583ff500b32a45dbf48c0cea9536aa8..8f382873052533812eb0b8cbdb664de50c10fb79 100644 (file)
  * 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
  *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc/vlc.h>
 
 
@@ -103,9 +128,6 @@ static int Control( demux_t *p_demux, int i_query, va_list args );
 static int HeaderRead( demux_t *p_demux );
 static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num );
 
-// Map flavour to bytes per second
-static int sipr_fl2bps[4] = {813, 1062, 625, 2000}; // 6.5, 8.5, 5, 16 kbit per second
-
 /*****************************************************************************
  * Open
  *****************************************************************************/
@@ -117,13 +139,24 @@ static int Open( vlc_object_t *p_this )
     const uint8_t *p_peek;
 
     if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 ) 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;
     p_demux->pf_control = Control;
+
     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
+    if( p_sys == NULL )
+    {
+        return VLC_ENOMEM;
+    }
     memset( p_sys, 0, sizeof( demux_sys_t ) );
+
     p_sys->i_data_offset = 0;
     p_sys->i_track = 0;
     p_sys->track   = NULL;
@@ -183,10 +216,10 @@ static void Close( vlc_object_t *p_this )
         free( tk );
     }
 
-    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 );
+    free( p_sys->psz_title );
+    free( p_sys->psz_artist );
+    free( p_sys->psz_copyright );
+    free( p_sys->psz_description );
 
     if( p_sys->i_track > 0 ) free( p_sys->track );
     free( p_sys );
@@ -335,13 +368,6 @@ static int Demux( demux_t *p_demux )
             {
                 msg_Dbg( p_demux, "sending size=%d", tk->p_frame->i_buffer );
 
-                if( p_sys->i_pcr < tk->p_frame->i_dts )
-                {
-                    p_sys->i_pcr = tk->p_frame->i_dts;
-
-                    es_out_Control( p_demux->out, ES_OUT_SET_PCR,
-                                    (int64_t)p_sys->i_pcr );
-                }
                 es_out_Send( p_demux->out, tk->p_es, tk->p_frame );
 
                 tk->i_frame = 0;
@@ -379,7 +405,9 @@ static int Demux( demux_t *p_demux )
                 msg_Dbg( p_demux, "copying new buffer n=%d offset=%d copy=%d",
                          i_ck, i_offset, i_copy );
 
-                ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck] = i_offset;
+                ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +0 ] = 1;
+                ((uint32_t*)(tk->p_frame->p_buffer+i_len+8))[i_ck*2 +1 ] = i_offset;
+
 
                 memcpy( &tk->p_frame->p_buffer[i_offset + 8], p, i_copy );
             }
@@ -477,40 +505,10 @@ static int Demux( demux_t *p_demux )
     }
     else if( tk->fmt.i_cat == AUDIO_ES && b_selected )
     {
-        /* Set PCR */
-        if( p_sys->i_pcr < i_pts )
-        {
-            p_sys->i_pcr = i_pts;
-            es_out_Control( p_demux->out, ES_OUT_SET_PCR,
-                            (int64_t)p_sys->i_pcr );
-        }
-
-        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];
-
-            int i;
-            for( i = 0; i < i_sub; i++ )
-            {
-                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 )
-                {
-                    memcpy( p_block->p_buffer, p_sub, i_sub_size );
-                    p_sub += i_sub_size;
-
-                    p_block->i_dts =
-                    p_block->i_pts = ( i == 0 ? i_pts : 0 );
-
-                    es_out_Send( p_demux->out, tk->p_es, p_block );
-                }
-            }
-        }
-        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') )
+        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);
@@ -554,6 +552,14 @@ static int Demux( demux_t *p_demux )
             while( tk->i_out_subpacket != tk->i_subpackets &&
                    tk->p_subpackets[tk->i_out_subpacket] )
             {
+                /* Set the PCR */
+                if (tk->i_out_subpacket == 0)
+                {
+                    p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
+                    es_out_Control( p_demux->out, ES_OUT_SET_PCR,
+                            (int64_t)p_sys->i_pcr );
+                }
+
                 block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
                 tk->p_subpackets[tk->i_out_subpacket] = 0;
 
@@ -578,28 +584,61 @@ static int Demux( demux_t *p_demux )
         }
         else
         {
-            block_t *p_block = block_New( p_demux, i_size );
+            /* Set PCR */
+            if( p_sys->i_pcr < i_pts )
+            {
+                p_sys->i_pcr = i_pts;
+                es_out_Control( p_demux->out, ES_OUT_SET_PCR,
+                        (int64_t)p_sys->i_pcr );
+            }
 
-            if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
+            if( tk->fmt.i_codec == VLC_FOURCC( 'm','p','4','a' ) )
             {
-                uint8_t *src = p_sys->buffer;
-                uint8_t *dst = p_block->p_buffer;
+                int     i_sub = (p_sys->buffer[1] >> 4)&0x0f;
+                uint8_t *p_sub = &p_sys->buffer[2+2*i_sub];
 
-                /* byte swap data */
-                while( dst < &p_block->p_buffer[i_size- 1])
+                int i;
+                for( i = 0; i < i_sub; i++ )
                 {
-                    *dst++ = src[1];
-                    *dst++ = src[0];
+                    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 )
+                    {
+                        memcpy( p_block->p_buffer, p_sub, i_sub_size );
+                        p_sub += i_sub_size;
+
+                        p_block->i_dts =
+                            p_block->i_pts = ( i == 0 ? i_pts : 0 );
 
-                    src += 2;
+                        es_out_Send( p_demux->out, tk->p_es, p_block );
+                    }
                 }
             }
             else
             {
-                memcpy( p_block->p_buffer, p_sys->buffer, i_size );
+                block_t *p_block = block_New( p_demux, i_size );
+
+                if( tk->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
+                {
+                    uint8_t *src = p_sys->buffer;
+                    uint8_t *dst = p_block->p_buffer;
+
+                    /* byte swap data */
+                    while( dst < &p_block->p_buffer[i_size- 1])
+                    {
+                        *dst++ = src[1];
+                        *dst++ = src[0];
+
+                        src += 2;
+                    }
+                }
+                else
+                {
+                    memcpy( p_block->p_buffer, p_sys->buffer, i_size );
+                }
+                p_block->i_dts = p_block->i_pts = i_pts;
+                es_out_Send( p_demux->out, tk->p_es, p_block );
             }
-            p_block->i_dts = p_block->i_pts = i_pts;
-            es_out_Send( p_demux->out, tk->p_es, p_block );
         }
     }
 
@@ -784,10 +823,9 @@ static int HeaderRead( demux_t *p_demux )
                 stream_Read( p_demux->s, psz, i_len );
                 psz[i_len] = '\0';
 
-                msg_Dbg( p_demux, "    - title=`%s'", psz );
                 EnsureUTF8( psz );
-                asprintf( &p_sys->psz_title, psz );
-                free( psz );
+                msg_Dbg( p_demux, "    - title=`%s'", psz );
+                p_sys->psz_title = psz;
                 i_skip -= i_len;
             }
             i_skip -= 2;
@@ -799,10 +837,9 @@ static int HeaderRead( demux_t *p_demux )
                 stream_Read( p_demux->s, psz, i_len );
                 psz[i_len] = '\0';
 
-                msg_Dbg( p_demux, "    - author=`%s'", psz );
                 EnsureUTF8( psz );
-                asprintf( &p_sys->psz_artist, psz );
-                free( psz );
+                msg_Dbg( p_demux, "    - author=`%s'", psz );
+                p_sys->psz_artist = psz;
                 i_skip -= i_len;
             }
             i_skip -= 2;
@@ -814,10 +851,9 @@ static int HeaderRead( demux_t *p_demux )
                 stream_Read( p_demux->s, psz, i_len );
                 psz[i_len] = '\0';
 
-                msg_Dbg( p_demux, "    - copyright=`%s'", psz );
                 EnsureUTF8( psz );
-                asprintf( &p_sys->psz_copyright, psz );
-                free( psz );
+                msg_Dbg( p_demux, "    - copyright=`%s'", psz );
+                p_sys->psz_copyright = psz;
                 i_skip -= i_len;
             }
             i_skip -= 2;
@@ -829,10 +865,9 @@ static int HeaderRead( demux_t *p_demux )
                 stream_Read( p_demux->s, psz, i_len );
                 psz[i_len] = '\0';
 
-                msg_Dbg( p_demux, "    - comment=`%s'", psz );
                 EnsureUTF8( psz );
-                asprintf( &p_sys->psz_description, psz );
-                free( psz );
+                msg_Dbg( p_demux, "    - comment=`%s'", psz );
+                p_sys->psz_description = psz;
                 i_skip -= i_len;
             }
             i_skip -= 2;
@@ -990,6 +1025,7 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
     {
         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 );
 
@@ -1014,10 +1050,9 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                 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 );
+                msg_Dbg( p_demux, "    - title=`%s'", psz );
+                p_sys->psz_title = psz;
             }
             p_peek += i_len;
 
@@ -1029,10 +1064,9 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                 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 );
+                msg_Dbg( p_demux, "    - artist=`%s'", psz );
+                p_sys->psz_artist = psz;
             }
             p_peek += i_len;
 
@@ -1044,10 +1078,9 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                 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 );
+                msg_Dbg( p_demux, "    - Copyright=`%s'", psz );
+                p_sys->psz_copyright = psz;
             }
             p_peek += i_len;
 
@@ -1059,10 +1092,9 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                 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 );
+                msg_Dbg( p_demux, "    - Comment=`%s'", psz );
+                p_sys->psz_description = psz;
             }
             /* This might be unusefull */
             p_peek += i_len;
@@ -1078,9 +1110,8 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
             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 += 2;                                           /* 00 00 */
             p_peek += 4;                                    /* .ra4 or .ra5 */
             p_peek += 4;                                       /* data size */
@@ -1094,6 +1125,8 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
             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 */
+            if( !i_subpacket_size || !i_frame_size || !i_coded_frame_size )
+                 return VLC_EGENERIC;
             p_peek += 2;                                               /* ?? */
 
             if( i_version == 5 ) p_peek += 6;                 /* 0, srate, 0 */
@@ -1106,7 +1139,7 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
 
             if( i_version == 5 )
             {
-                p_peek += 4;                                         /* genr */
+                memcpy( (char *)p_genr, p_peek, 4 ); p_peek += 4;    /* genr */
                 memcpy( (char *)&fmt.i_codec, p_peek, 4 ); p_peek += 4;
             }
             else
@@ -1121,50 +1154,72 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
 
             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('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('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' ):
-            fmt.i_extra = GetDWBE( p_peek ); p_peek += 4;
-            // For version == 4, there might need an extra p_peek++
+            fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
+
             if( fmt.i_extra > 0 ) { fmt.i_extra--; p_peek++; }
             if( fmt.i_extra > 0 )
             {
                 fmt.p_extra = malloc( fmt.i_extra );
+                if( fmt.p_extra == NULL )
+                {
+                    msg_Err( p_demux, "Error in the extra data" );
+                    return VLC_EGENERIC;
+                }
                 memcpy( fmt.p_extra, p_peek, fmt.i_extra );
             }
-
-            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'):
         case VLC_FOURCC('a','t','r','c'):
-            fmt.audio.i_blockalign = i_subpacket_size;
-            if( !(fmt.i_extra = GetDWBE( p_peek )) ) break;
-            fmt.p_extra = malloc( fmt.i_extra );
-            memcpy( fmt.p_extra, p_peek + 4, fmt.i_extra );
-            break;
+            if( !memcmp( p_genr, "genr", 4 ) )
+                fmt.audio.i_blockalign = i_subpacket_size;
+            else
+                fmt.audio.i_blockalign = i_coded_frame_size;
 
-        case VLC_FOURCC('2','8','_','8'):
-            fmt.i_extra = 0;
-            fmt.audio.i_blockalign = i_coded_frame_size;
+            if( fmt.i_extra > 0 )
+            {
+                fmt.p_extra = malloc( fmt.i_extra );
+                if( fmt.p_extra == NULL )
+                {
+                    msg_Err( p_demux, "Error in the extra data" );
+                    return VLC_EGENERIC;
+                }
+                memcpy( fmt.p_extra, p_peek, fmt.i_extra );
+            }
             break;
 
-        case VLC_FOURCC('s','i','p','r'):
-            fmt.i_extra = 0;
-            fmt.audio.i_blockalign = i_coded_frame_size;
-            fmt.audio.i_bitspersample = sipr_fl2bps[ i_flavor ];
+        case VLC_FOURCC('r','a','l','f'):
+            msg_Dbg( p_demux, "    - audio codec not supported=%4.4s",
+                     (char*)&fmt.i_codec );
             break;
 
-        case VLC_FOURCC('l','p','c','J'):
-            fmt.i_codec = VLC_FOURCC( '1','4','_','4' );
-
         default:
             msg_Dbg( p_demux, "    - unknown audio codec=%4.4s",
                      (char*)&fmt.i_codec );
@@ -1198,17 +1253,23 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                 tk->i_subpackets =
                     i_subpacket_h * i_frame_size / tk->i_subpacket_size;
                 tk->p_subpackets =
-                    malloc( tk->i_subpackets * sizeof(block_t *) );
+                    calloc( 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 *) );
+                    calloc( tk->i_subpackets, sizeof(block_t *) );
             }
 
-            for( i = 0; i < tk->i_subpackets; i++ ) tk->p_subpackets[i] = NULL;
+            /* Check if the calloc went correctly */
+            if( tk->p_subpackets == NULL )
+            {
+                tk->i_subpackets = 0;
+                msg_Err( p_demux, "Can't alloc subpacket" );
+                return VLC_EGENERIC;
+            }
 
             tk->p_es = es_out_Add( p_demux->out, &fmt );