]> git.sesse.net Git - vlc/blobdiff - modules/demux/vobsub.c
mkv.cpp: process the LinkPGCN DVD command correctly
[vlc] / modules / demux / vobsub.c
index cb7c462230b2e7d7d80c3cf04af8f83769bf0a09..24aae6cde6314084a49cdbdcc4044a710b8a4690 100644 (file)
@@ -46,11 +46,14 @@ static void Close( vlc_object_t *p_this );
 
 vlc_module_begin();
     set_description( _("Vobsub subtitles demux") );
-    set_capability( "demux2", 0 );
+    set_category( CAT_INPUT );
+    set_subcategory( SUBCAT_INPUT_DEMUX );
+    set_capability( "demux2", 1 );
     
     set_callbacks( Open, Close );
 
     add_shortcut( "vobsub" );
+    add_shortcut( "subtitle" );
 vlc_module_end();
 
 /*****************************************************************************
@@ -68,7 +71,7 @@ static void TextUnload( text_t * );
 
 typedef struct
 {
-    mtime_t i_start;
+    int64_t i_start;
     int     i_vobsub_location;
 } subtitle_t;
 
@@ -92,8 +95,8 @@ struct demux_sys_t
     FILE        *p_vobsub_file;
     
     /* all tracks */
-    int                     i_tracks;
-    vobsub_track_t          *track;
+    int            i_tracks;
+    vobsub_track_t *track;
     
     int         i_original_frame_width;
     int         i_original_frame_height;
@@ -112,46 +115,40 @@ static int Open ( vlc_object_t *p_this )
 {
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys;
-    int i_max;
+    char *psz_vobname, *s;
+    int i_len;
 
-    if( strcmp( p_demux->psz_demux, "vobsub" ) )
-    {
-        msg_Dbg( p_demux, "vobsub demux discarded" );
-        return VLC_EGENERIC;
-    }
-
-    p_demux->pf_demux = Demux;
-    p_demux->pf_control = Control;
-    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
-    p_sys->p_vobsub_file = NULL;
-    p_sys->i_tracks = 0;
-    p_sys->track = (vobsub_track_t*)malloc( sizeof( vobsub_track_t ) );
-    p_sys->i_original_frame_width = -1;
-    p_sys->i_original_frame_height = -1;
-
-    char *s = NULL;
     if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
     {
         if( !strcasestr( s, "# VobSub index file" ) )
         {
-            msg_Err( p_demux, "this doesn't seem to be a vobsub file, bailing" );
+            msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
             free( s );
+            if( stream_Seek( p_demux->s, 0 ) )
+            {
+                msg_Warn( p_demux, "failed to rewind" );
+            }
             return VLC_EGENERIC;
         }
         free( s );
-        s = NULL;
 
-        if( stream_Seek( p_demux->s, 0 ) )
-        {
-            msg_Warn( p_demux, "failed to rewind" );
-        }
     }
     else
     {
-        msg_Err( p_demux, "could not read vobsub IDX file" );
+        msg_Dbg( p_demux, "could not read vobsub IDX file" );
         return VLC_EGENERIC;
     }
 
+    p_demux->pf_demux = Demux;
+    p_demux->pf_control = Control;
+    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
+    p_sys->i_length = 0;
+    p_sys->p_vobsub_file = NULL;
+    p_sys->i_tracks = 0;
+    p_sys->track = (vobsub_track_t *)malloc( sizeof( vobsub_track_t ) );
+    p_sys->i_original_frame_width = -1;
+    p_sys->i_original_frame_height = -1;
+
     /* Load the whole file */
     TextLoad( &p_sys->txt, p_demux->s );
 
@@ -161,8 +158,22 @@ static int Open ( vlc_object_t *p_this )
     /* Unload */
     TextUnload( &p_sys->txt );
 
-    int i_len = strlen( p_demux->psz_path );
-    char *psz_vobname = strdup( p_demux->psz_path );
+    /* Find the total length of the vobsubs */
+    if( p_sys->i_tracks > 0 )
+    {
+        int i;
+        for( i = 0; i < p_sys->i_tracks; i++ )
+        {
+            if( p_sys->track[i].i_subtitles > 1 )
+            {
+                if( p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start > p_sys->i_length )
+                    p_sys->i_length = (int64_t) p_sys->track[i].p_subtitles[p_sys->track[i].i_subtitles-1].i_start + ( 1 *1000 *1000 );
+            }
+        }
+    }
+
+    i_len = strlen( p_demux->psz_path );
+    psz_vobname = strdup( p_demux->psz_path );
 
     strcpy( psz_vobname + i_len - 4, ".sub" );
 
@@ -171,6 +182,9 @@ static int Open ( vlc_object_t *p_this )
     {
         msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
                  psz_vobname );
+        free( p_sys );
+        free( psz_vobname );
+        return VLC_EGENERIC;
     }
     free( psz_vobname );
 
@@ -202,66 +216,89 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
     int64_t *pi64, i64;
+    int i;
     double *pf, f;
 
     switch( i_query )
     {
         case DEMUX_GET_LENGTH:
             pi64 = (int64_t*)va_arg( args, int64_t * );
-            //*pi64 = p_sys->i_length;
+            *pi64 = (int64_t) p_sys->i_length;
             return VLC_SUCCESS;
 
         case DEMUX_GET_TIME:
             pi64 = (int64_t*)va_arg( args, int64_t * );
-            /*if( p_sys->i_current_subtitle < p_sys->i_subtitles )
+            for( i = 0; i < p_sys->i_tracks; i++ )
+            {
+                vlc_bool_t b_selected;
+                /* Check the ES is selected */
+                es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
+                                p_sys->track[i].p_es, &b_selected );
+                if( b_selected ) break;
+            }
+            if( i < p_sys->i_tracks && p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles )
             {
-                *pi64 = p_sys->subtitle[p_sys->i_current_subtitle].i_start;
+                *pi64 = p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start;
                 return VLC_SUCCESS;
-            }*/
+            }
             return VLC_EGENERIC;
 
         case DEMUX_SET_TIME:
             i64 = (int64_t)va_arg( args, int64_t );
-            /*p_sys->i_current_subtitle = 0;
-            while( p_sys->i_current_subtitle < p_sys->i_subtitles &&
-                   p_sys->subtitle[p_sys->i_current_subtitle].i_start < i64 )
+            for( i = 0; i < p_sys->i_tracks; i++ )
             {
-                p_sys->i_current_subtitle++;
+                p_sys->track[i].i_current_subtitle = 0;
+                while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
+                       p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
+                {
+                    p_sys->track[i].i_current_subtitle++;
+                }
+
+                if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
+                    return VLC_EGENERIC;
             }
-
-            if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
-                return VLC_EGENERIC;*/
             return VLC_SUCCESS;
 
         case DEMUX_GET_POSITION:
             pf = (double*)va_arg( args, double * );
-            /*if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
+            for( i = 0; i < p_sys->i_tracks; i++ )
+            {
+                vlc_bool_t b_selected;
+                /* Check the ES is selected */
+                es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
+                                p_sys->track[i].p_es, &b_selected );
+                if( b_selected ) break;
+            }
+            if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
             {
                 *pf = 1.0;
             }
-            else if( p_sys->i_subtitles > 0 )
+            else if( p_sys->track[i].i_subtitles > 0 )
             {
-                *pf = (double)p_sys->subtitle[p_sys->i_current_subtitle].i_start /
+                *pf = (double)p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start /
                       (double)p_sys->i_length;
             }
             else
             {
                 *pf = 0.0;
-            }*/
+            }
             return VLC_SUCCESS;
 
         case DEMUX_SET_POSITION:
             f = (double)va_arg( args, double );
-            /*i64 = f * p_sys->i_length;
+            i64 = (int64_t) f * p_sys->i_length;
 
-            p_sys->i_current_subtitle = 0;
-            while( p_sys->i_current_subtitle < p_sys->i_subtitles &&
-                   p_sys->subtitle[p_sys->i_current_subtitle].i_start < i64 )
+            for( i = 0; i < p_sys->i_tracks; i++ )
             {
-                p_sys->i_current_subtitle++;
+                p_sys->track[i].i_current_subtitle = 0;
+                while( p_sys->track[i].i_current_subtitle < p_sys->track[i].i_subtitles &&
+                       p_sys->track[i].p_subtitles[p_sys->track[i].i_current_subtitle].i_start < i64 )
+                {
+                    p_sys->track[i].i_current_subtitle++;
+                }
+                if( p_sys->track[i].i_current_subtitle >= p_sys->track[i].i_subtitles )
+                    return VLC_EGENERIC;
             }
-            if( p_sys->i_current_subtitle >= p_sys->i_subtitles )
-                return VLC_EGENERIC;*/
             return VLC_SUCCESS;
 
         case DEMUX_SET_NEXT_DEMUX_TIME:
@@ -292,13 +329,13 @@ static int Demux( demux_t *p_demux )
     {
 #define tk p_sys->track[i]
         if( tk.i_current_subtitle >= tk.i_subtitles )
-            return 0;
+            continue;
 
-        i_maxdate = p_sys->i_next_demux_date;
+        i_maxdate = (int64_t) p_sys->i_next_demux_date;
         if( i_maxdate <= 0 && tk.i_current_subtitle < tk.i_subtitles )
         {
             /* Should not happen */
-            i_maxdate = tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
+            i_maxdate = (int64_t) tk.p_subtitles[tk.i_current_subtitle].i_start + 1;
         }
 
         while( tk.i_current_subtitle < tk.i_subtitles &&
@@ -449,7 +486,7 @@ static int ParseVobSubIDX( demux_t *p_demux )
             es_format_t fmt;
 
             /* Lets start a new track */
-            if( sscanf( line, "id: %s, index: %d",
+            if( sscanf( line, "id: %2s, index: %d",
                         language, &i_track_id ) == 2 )
             {
                 p_sys->i_tracks++;
@@ -460,7 +497,7 @@ static int ParseVobSubIDX( demux_t *p_demux )
                 memset( current_tk, 0, sizeof( vobsub_track_t ) );
                 current_tk->i_current_subtitle = 0;
                 current_tk->i_subtitles = 0;
-                current_tk->p_subtitles = NULL;
+                current_tk->p_subtitles = (subtitle_t*)malloc( sizeof( subtitle_t ) );;
                 current_tk->i_track_id = i_track_id;
 
                 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
@@ -483,18 +520,28 @@ static int ParseVobSubIDX( demux_t *p_demux )
              * loc is the hex location of the spu in the .sub file
              *
              */
-            unsigned int h, m, s, ms, loc;
-            int i_start, i_location = 0;
+            int h, m, s, ms, loc;
+            int64_t i_start, i_location = 0;
+            
+            vobsub_track_t *current_tk = &p_sys->track[p_sys->i_tracks - 1];
 
             if( sscanf( line, "timestamp: %d:%d:%d:%d, filepos: %x",
                         &h, &m, &s, &ms, &loc ) == 5 )
             {
-                i_start = ( (mtime_t)h * 3600*1000 +
-                            (mtime_t)m * 60*1000 +
-                            (mtime_t)s * 1000 +
-                            (mtime_t)ms ) * 1000;
+                subtitle_t *current_sub;
+                
+                i_start = (int64_t) ( h * 3600*1000 +
+                            m * 60*1000 +
+                            s * 1000 +
+                            ms ) * 1000;
                 i_location = loc;
-                break;
+                
+                current_tk->i_subtitles++;
+                current_tk->p_subtitles = (subtitle_t*)realloc( current_tk->p_subtitles, sizeof( subtitle_t ) * (current_tk->i_subtitles + 1 ) );
+                current_sub = &current_tk->p_subtitles[current_tk->i_subtitles - 1];
+                
+                current_sub->i_start = (int64_t) i_start;
+                current_sub->i_vobsub_location = i_location;
             }
         }
     }
@@ -527,7 +574,7 @@ static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
 
         if( p[3] != 0xbd )
         {
-            msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] );
+            /* msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] ); */
             p += i_size;
             continue;
         }
@@ -545,27 +592,26 @@ static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
             continue;
         }
         i_spu = i_id&0x1f;
-        msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size );
+        /* msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size ); */
 
-        /* FIXME i_spu == determines which of the spu tracks we will show. */
         for( i = 0; i < p_sys->i_tracks; i++ )
         {
 #define tk p_sys->track[i]
             p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
             p_pkt->i_length = 0;
             
-            if( tk.p_es && tk.i_track_id == i_id )
+            if( tk.p_es && tk.i_track_id == i_spu )
             {
                 es_out_Send( p_demux->out, tk.p_es, p_pkt );
-                p_bk->i_pts = 0;    /* only first packet has a pts */
+                p_bk->i_pts = 0;     /*only first packet has a pts */
+                break;
             }
-            else
+            else if( i == p_sys->i_tracks - 1 )
             {
                 block_Release( p_pkt );
-                continue;
             }
+#undef tk
         }
-#undef tk        
     }
 
     return VLC_SUCCESS;