]> git.sesse.net Git - vlc/blobdiff - modules/demux/real.c
Cosmetic et metadata fixes.
[vlc] / modules / demux / real.c
index d60a4eea408eebb909cccf170ad7d101f5be74b2..cb683debcde7be45fbb491175be2cba79bc1b3a6 100644 (file)
@@ -50,6 +50,7 @@
 #endif
 
 #include <vlc/vlc.h>
+#include <vlc_plugin.h>
 
 
 #include <vlc_demux.h>
@@ -63,7 +64,7 @@ static int  Open    ( vlc_object_t * );
 static void Close  ( vlc_object_t * );
 
 vlc_module_begin();
-    set_description( _("Real demuxer" ) );
+    set_description( N_("Real demuxer" ) );
     set_capability( "demux", 15 );
     set_category( CAT_INPUT );
     set_subcategory( SUBCAT_INPUT_DEMUX );
@@ -94,10 +95,18 @@ typedef struct
     int         i_subpacket;
     int         i_subpackets;
     block_t     **p_subpackets;
+    int64_t        *p_subpackets_timecode;
     int         i_out_subpacket;
 
 } real_track_t;
 
+typedef struct
+{
+    uint32_t file_offset;
+    uint32_t time_offset;
+    uint32_t frame_index;
+} rm_index_t;
+
 struct demux_sys_t
 {
     int64_t  i_data_offset;
@@ -120,6 +129,11 @@ struct demux_sys_t
     uint8_t buffer[65536];
 
     int64_t     i_pcr;
+    vlc_meta_t *p_meta;
+
+    int64_t i_index_offset;
+    int        b_seek;
+    rm_index_t * p_index;
 };
 
 static int Demux( demux_t *p_demux );
@@ -152,9 +166,8 @@ static int Open( vlc_object_t *p_this )
 
     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;
@@ -162,6 +175,7 @@ static int Open( vlc_object_t *p_this )
     p_sys->track   = NULL;
     p_sys->i_pcr   = 1;
 
+    p_sys->b_seek  = 0;
 
     /* Parse the headers */
     if( HeaderRead( p_demux ) )
@@ -211,7 +225,11 @@ static void Close( vlc_object_t *p_this )
             if( tk->p_subpackets[ j ] )
                 block_Release( tk->p_subpackets[ j ] );
         }
-        if( tk->i_subpackets ) free( tk->p_subpackets );
+        if( tk->i_subpackets )
+        {
+            free( tk->p_subpackets );
+            free( tk->p_subpackets_timecode );
+        }
 
         free( tk );
     }
@@ -220,6 +238,7 @@ static void Close( vlc_object_t *p_this )
     free( p_sys->psz_artist );
     free( p_sys->psz_copyright );
     free( p_sys->psz_description );
+    free( p_sys->p_index );
 
     if( p_sys->i_track > 0 ) free( p_sys->track );
     free( p_sys );
@@ -268,10 +287,8 @@ static int Demux( demux_t *p_demux )
     i_pts += 1000; /* Avoid 0 pts */
     i_flags= header[11]; /* flags 0x02 -> keyframe */
 
-#if 0
     msg_Dbg( p_demux, "packet %d size=%d id=%d pts=%u",
              p_sys->i_data_packets, i_size, i_id, (uint32_t)(i_pts/1000) );
-#endif
 
     p_sys->i_data_packets++;
 
@@ -515,22 +532,37 @@ static int Demux( demux_t *p_demux )
             int i_index, i;
 
             /* Sanity check */
-            if( i_flags & 2 ) y = tk->i_subpacket = 0;
+            if( i_flags & 2 || ( p_sys->b_seek ) )
+            {
+                y = tk->i_subpacket = 0;
+                tk->i_out_subpacket = 0;
+                p_sys->b_seek = 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;
+                int num = tk->i_frame_size / tk->i_subpacket_size;
+                for( i = 0; i <  num; 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);
+                    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->p_subpackets[i_index]  != NULL )
+                    {
+                        msg_Dbg(p_demux, "p_subpackets[ %d ] not null!",  i_index );
+                        free( tk->p_subpackets[i_index] );
+                    }
+
+                    tk->p_subpackets[i_index] = p_block;
+                    tk->i_subpacket++;
+                    p_block->i_dts = p_block->i_pts = 0;
+                }
+                tk->p_subpackets_timecode[tk->i_subpacket - num] = i_pts;
             }
 
             if( tk->fmt.i_codec == VLC_FOURCC( '2', '8', '_', '8' ) ||
@@ -553,6 +585,7 @@ static int Demux( demux_t *p_demux )
                    tk->p_subpackets[tk->i_out_subpacket] )
             {
                 /* Set the PCR */
+#if 0
                 if (tk->i_out_subpacket == 0)
                 {
                     p_sys->i_pcr = tk->p_subpackets[tk->i_out_subpacket]->i_dts;
@@ -564,6 +597,20 @@ static int Demux( demux_t *p_demux )
                 tk->p_subpackets[tk->i_out_subpacket] = 0;
 
                 if( tk->i_out_subpacket ) p_block->i_dts = p_block->i_pts = 0;
+#endif
+
+                block_t *p_block = tk->p_subpackets[tk->i_out_subpacket];
+                tk->p_subpackets[tk->i_out_subpacket] = 0;
+                if ( tk->p_subpackets_timecode[tk->i_out_subpacket]  )
+                {
+                    p_block->i_dts = p_block->i_pts =
+                        tk->p_subpackets_timecode[tk->i_out_subpacket];
+                    tk->p_subpackets_timecode[tk->i_out_subpacket] = 0;
+
+                    p_sys->i_pcr = p_block->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, p_block );
 
                 tk->i_out_subpacket++;
@@ -652,21 +699,28 @@ static int Demux( demux_t *p_demux )
 static int Control( demux_t *p_demux, int i_query, va_list args )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
-#if 0
     double f, *pf;
     int64_t i64;
-#endif
+    rm_index_t * p_index;
     int64_t *pi64;
 
     switch( i_query )
     {
-#if 0
         case DEMUX_GET_POSITION:
             pf = (double*) va_arg( args, double* );
+
+            if( p_sys->i_our_duration > 0 )
+            {
+                *pf = (double)p_sys->i_pcr / 1000.0 / p_sys->i_our_duration;
+                return VLC_SUCCESS;
+            }
+
+            /* read stream size maybe failed in rtsp streaming, 
+               so use duration to determin the position at first  */
             i64 = stream_Size( p_demux->s );
             if( i64 > 0 )
             {
-                *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
+                *pf = (double)1.0*stream_Tell( p_demux->s ) / (double)i64;
             }
             else
             {
@@ -674,24 +728,90 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             }
             return VLC_SUCCESS;
 
-        case DEMUX_SET_POSITION:
-            f = (double) va_arg( args, double );
-            i64 = stream_Size( p_demux->s );
-
-            es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
-
-            return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
-
         case DEMUX_GET_TIME:
             pi64 = (int64_t*)va_arg( args, int64_t * );
-            if( p_sys->i_mux_rate > 0 )
+
+            if( p_sys->i_our_duration > 0 )
             {
-                *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
+                *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;
-#endif
+
+        case DEMUX_SET_POSITION:
+            f = (double) va_arg( args, double );
+            i64 = (int64_t) ( stream_Size( p_demux->s ) * f );
+
+            if ( p_sys->i_index_offset == 0 && i64 != 0 )
+            {
+                msg_Err(p_demux,"Seek No Index Real File failed!" );
+                return VLC_EGENERIC; // no index!
+            }
+            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_t)1000 * ( p_sys->i_our_duration * f  );
+
+                es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
+                p_sys->b_seek = 1;
+
+                return stream_Seek( p_demux->s, p_sys->i_pcr );
+            }
+
+            if ( p_sys->i_index_offset > 0 )
+            {
+                p_index = p_sys->p_index;
+                while( p_index->file_offset !=0 )
+                {
+                    if ( p_index->file_offset > i64 )
+                    {
+                        msg_Dbg( p_demux, "Seek Real find! %d %d %d",
+                                 p_index->time_offset, p_index->file_offset,
+                                 (uint32_t) i64 );
+                        if ( p_index != p_sys->p_index ) p_index --;
+                        i64 = p_index->file_offset;
+                        break;
+                    }
+                    p_index++;
+                }
+
+                p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
+
+                es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
+
+                return stream_Seek( p_demux->s, i64 );
+            }
+        case DEMUX_SET_TIME:
+            i64 = (int64_t) va_arg( args, int64_t ) / 1000;
+
+            p_index = p_sys->p_index;
+            while( p_index->file_offset !=0 )
+            {
+                if ( p_index->time_offset > i64 )
+                {
+                    if ( p_index != p_sys->p_index )
+                        p_index --;
+                    i64 = p_index->file_offset;
+                    break;
+                }
+                p_index++;
+            }
+
+            p_sys->i_pcr = 1000 * (int64_t) p_index->time_offset;
+            es_out_Control( p_demux->out, ES_OUT_RESET_PCR , p_sys->i_pcr );
+
+            return stream_Seek( p_demux->s, i64 );
 
         case DEMUX_GET_LENGTH:
             pi64 = (int64_t*)va_arg( args, int64_t * );
@@ -730,7 +850,6 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             return VLC_SUCCESS;
         }
 
-        case DEMUX_SET_TIME:
         case DEMUX_GET_FPS:
         default:
             return VLC_EGENERIC;
@@ -738,6 +857,83 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
     return VLC_EGENERIC;
 }
 
+/*****************************************************************************
+ * ReadRealIndex:
+ *****************************************************************************/
+
+static void ReadRealIndex( demux_t *p_demux )
+{
+    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;
+
+    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 ;
+
+    i_id = VLC_FOURCC( buffer[0], buffer[1], buffer[2], buffer[3] );
+    i_size      = GetDWBE( &buffer[4] );
+    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 == 0 )
+        return;
+
+    if( GetDWBE( &buffer[16] ) > 0 )
+        msg_Dbg( p_demux, "Real Index: Does next index exist? %d ",
+                        GetDWBE( &buffer[16] )  );
+
+    p_sys->p_index = 
+            (rm_index_t *)malloc( sizeof( rm_index_t ) * (i_index_count+1) );
+    if( p_sys->p_index == NULL )
+    {
+        msg_Err( p_demux, "Memory allocation error" ); 
+        return;
+    }
+
+    memset( p_sys->p_index, 0, sizeof(rm_index_t) * (i_index_count+1) );
+
+    for( i=0; i<i_index_count; i++ )
+    {
+        if( stream_Read( p_demux->s, buffer, 14 ) < 14 )
+            return ;
+
+        if( GetWBE( &buffer[0] ) != 0 )
+        {
+            msg_Dbg( p_demux, "Real Index: invaild version of index entry %d ",
+                              GetWBE( &buffer[0] ) );
+            return;
+        }
+
+        p_sys->p_index[i].time_offset = GetDWBE( &buffer[2] );
+        p_sys->p_index[i].file_offset = GetDWBE( &buffer[6] );
+        p_sys->p_index[i].frame_index = GetDWBE( &buffer[10] );
+        msg_Dbg( p_demux, "Real Index: time %d file %d frame %d ",
+                        p_sys->p_index[i].time_offset,
+                        p_sys->p_index[i].file_offset,
+                        p_sys->p_index[i].frame_index );
+
+    }
+}
+
 /*****************************************************************************
  * HeaderRead:
  *****************************************************************************/
@@ -750,6 +946,7 @@ static int HeaderRead( demux_t *p_demux )
     uint32_t    i_size;
     int64_t     i_skip;
     int         i_version;
+    p_sys->p_meta = vlc_meta_New();
 
     for( ;; )
     {
@@ -800,6 +997,8 @@ static int HeaderRead( demux_t *p_demux )
             /* set the duration for export in control */
             p_sys->i_our_duration = (int)GetDWBE(&header[20]);
  
+            p_sys->i_index_offset = GetDWBE(&header[28]);
+
             i_flags = GetWBE(&header[38]);
             msg_Dbg( p_demux, "    - flags=0x%x %s%s%s",
                      i_flags,
@@ -957,6 +1156,14 @@ static int HeaderRead( demux_t *p_demux )
 
     /* TODO read index if possible */
 
+    if ( p_sys->i_index_offset > 0 )
+    {
+        int64_t pos;
+        pos = stream_Tell( p_demux->s );
+        ReadRealIndex( p_demux );
+        stream_Seek( p_demux->s, pos );
+    }
+
     return VLC_SUCCESS;
 }
 
@@ -1013,6 +1220,7 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
         tk->i_subpacket = 0;
         tk->i_subpackets = 0;
         tk->p_subpackets = NULL;
+        tk->p_subpackets_timecode = NULL;
         tk->i_id = i_num;
         tk->fmt = fmt;
         tk->i_frame = 0;
@@ -1023,8 +1231,12 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
     }
     else if( !strncmp( (char *)p_peek, ".ra\xfd", 4 ) )
     {
-        int i_header_size, i_flavor, i_coded_frame_size, i_subpacket_h;
-        int i_frame_size, i_subpacket_size;
+        int i_header_size = 0;
+        int i_flavor = 0;
+        int i_coded_frame_size = 0;
+        int i_subpacket_h = 0;
+        int i_frame_size = 0;
+        int i_subpacket_size = 0;
         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 );
@@ -1245,6 +1457,7 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
             tk->i_subpacket = 0;
             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') )
             {
@@ -1252,6 +1465,8 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                     i_subpacket_h * i_frame_size / tk->i_subpacket_size;
                 tk->p_subpackets =
                     calloc( tk->i_subpackets, sizeof(block_t *) );
+                tk->p_subpackets_timecode =
+                    calloc( tk->i_subpackets , sizeof( int64_t ) );
             }
             else if( fmt.i_codec == VLC_FOURCC('2','8','_','8') )
             {
@@ -1259,6 +1474,8 @@ static int ReadCodecSpecificData( demux_t *p_demux, int i_len, int i_num )
                     i_subpacket_h * i_frame_size / tk->i_coded_frame_size;
                 tk->p_subpackets =
                     calloc( tk->i_subpackets, sizeof(block_t *) );
+                tk->p_subpackets_timecode =
+                    calloc( tk->i_subpackets , sizeof( int64_t ) );
             }
 
             /* Check if the calloc went correctly */