]> git.sesse.net Git - vlc/blobdiff - modules/demux/real.c
MP4: support for the \xa9too atom
[vlc] / modules / demux / real.c
index c48fcbe8cde4270125ce0bf881022ed0277c5d64..970d8d37ef71c633e55c2d1dd5823493b70b7ba2 100644 (file)
@@ -33,7 +33,7 @@
  *               - dnet is twisted "The byte order of the data is reversed
  *                                  from standard AC3" but ok
  *               - 28_8 is ok.
- *               - sipr should be fine, but our decoder suxx :)
+ *               - sipr doesn't work
  *               - 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
  *****************************************************************************/
@@ -133,7 +135,7 @@ 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;
@@ -143,6 +145,8 @@ struct demux_sys_t
     real_index_t *p_index;
 };
 
+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 );
 
@@ -199,7 +203,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;
@@ -297,21 +301,18 @@ static int Demux( demux_t *p_demux )
     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 size_t  i_size = GetWBE( &header[2] ) - 12;
     const int     i_id   = GetWBE( &header[4] );
-    const int64_t i_pts  = 1 + 1000 * GetDWBE( &header[6] );
+    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 <= 0 )
+    if( i_size > sizeof(p_sys->buffer) )
     {
         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;
@@ -340,15 +341,15 @@ static int Demux( demux_t *p_demux )
     }
 
     /* Update PCR */
-    mtime_t i_pcr = 0;
+    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 <= 0 || ( tk->i_last_dts > 0 && tk->i_last_dts < i_pcr ) )
+        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 > 0 && i_pcr != p_sys->i_pcr )
+    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 );
@@ -375,14 +376,18 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
                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;
+                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;
             }
 
-            *pf = 0.0;
             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:
@@ -390,7 +395,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
 
             if( p_sys->i_our_duration > 0 )
             {
-                *pi64 = p_sys->i_pcr;
+                *pi64 = p_sys->i_pcr > VLC_TS_INVALID ? p_sys->i_pcr : 0;
                 return VLC_SUCCESS;
             }
 
@@ -419,9 +424,9 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             {
                 /* 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->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 );
+                return stream_Seek( p_demux->s, p_sys->i_pcr - VLC_TS_0 );
             }
             return ControlSeekByte( p_demux, i64 );
 
@@ -435,9 +440,11 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         case DEMUX_GET_LENGTH:
             pi64 = (int64_t*)va_arg( args, int64_t * );
  
-            *pi64 = 0;
             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;
@@ -474,10 +481,10 @@ 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;
@@ -542,17 +549,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;
@@ -571,7 +577,7 @@ static void DemuxVideo( demux_t *p_demux, real_track_t *tk, mtime_t i_dts, unsig
 
         /* */
         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;
 
         /* */
@@ -618,9 +624,9 @@ static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts
         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' ) ||
-        tk->fmt.i_codec == VLC_FOURCC( 's', 'i', 'p', 'r' ) )
+    if( tk->fmt.i_codec == VLC_CODEC_COOK ||
+        tk->fmt.i_codec == VLC_CODEC_ATRAC3 ||
+        tk->fmt.i_codec == VLC_CODEC_SIPR )
     {
         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 );
@@ -635,7 +641,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;
 
@@ -657,7 +663,7 @@ static void DemuxAudioMethod1( demux_t *p_demux, real_track_t *tk, mtime_t i_pts
     else
     {
         const int y = tk->i_subpacket / (tk->i_subpacket_h / 2);
-        assert( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) );
+        assert( tk->fmt.i_codec == VLC_CODEC_RA_288 );
 
         for( int i = 0; i < tk->i_subpacket_h / 2; i++ )
         {
@@ -671,7 +677,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_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;
 
@@ -718,6 +724,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;
@@ -725,13 +732,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 );
@@ -745,7 +752,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 );
@@ -762,7 +769,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;
@@ -791,13 +798,13 @@ static void DemuxAudio( demux_t *p_demux, real_track_t *tk, mtime_t i_pts, unsig
 {
     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' ):
+    case VLC_CODEC_COOK:
+    case VLC_CODEC_ATRAC3:
+    case VLC_CODEC_SIPR:
+    case VLC_CODEC_RA_288:
         DemuxAudioMethod1( p_demux, tk, i_pts, i_flags );
         break;
-    case VLC_FOURCC( 'm','p','4','a' ):
+    case VLC_CODEC_MP4A:
         DemuxAudioMethod2( p_demux, tk, i_pts );
         break;
     default:
@@ -1056,7 +1063,8 @@ 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;
@@ -1306,19 +1314,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",
@@ -1353,7 +1361,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;
@@ -1445,22 +1453,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 )
         {
@@ -1478,11 +1488,17 @@ 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'):
-        if( i_subpacket_size <= 0 )
+    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];
+        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;
@@ -1492,6 +1508,11 @@ static int CodecAudioParse( demux_t *p_demux, int i_tk_id, const uint8_t *p_data
         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( i_extra_codec );
@@ -1514,7 +1535,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 ) );
@@ -1538,9 +1559,9 @@ 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') ||
-        fmt.i_codec == VLC_FOURCC('s','i','p','r') )
+    if( fmt.i_codec == VLC_CODEC_COOK ||
+        fmt.i_codec == VLC_CODEC_ATRAC3 ||
+        fmt.i_codec == VLC_CODEC_SIPR )
     {
         tk->i_subpackets =
             i_subpacket_h * i_frame_size / tk->i_subpacket_size;
@@ -1549,7 +1570,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;