]> git.sesse.net Git - vlc/blobdiff - modules/demux/util/sub.c
* ALL: use p_block->i_length for text subtitles duration (instead of the i_dts hack).
[vlc] / modules / demux / util / sub.c
index 4e4a93a443164425d1c6a9fd9b35d0a13e8a650d..a06175b8cdd9774143d5216956ffac0523d5489a 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
- * sub.c
+ * sub.c: subtitle demux for external subtitle files
  *****************************************************************************
- * Copyright (C) 1999-2003 VideoLAN
- * $Id: sub.c,v 1.40 2004/01/06 14:35:16 hartman Exp $
+ * Copyright (C) 1999-2004 VideoLAN
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
+ *          Derk-Jan Hartman <hartman at videolan dot org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #    include <dirent.h>
 #endif
 
+#define DVD_VIDEO_LB_LEN 2048
+
 static int  Open ( vlc_object_t *p_this );
 
 static int  sub_open ( subtitle_demux_t *p_sub,
                        input_thread_t  *p_input,
                        char  *psz_name,
-                       mtime_t i_microsecperframe,
-                       int i_track_id );
+                       mtime_t i_microsecperframe );
 static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate );
 static int  sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date );
 static void sub_close( subtitle_demux_t *p_sub );
 
 static void sub_fix( subtitle_demux_t *p_sub );
 
-static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "ssa1",
+static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "subviewer", "ssa1",
   "ssa2-4", "vplayer", "sami", "vobsub" };
 
 /*****************************************************************************
@@ -71,16 +73,15 @@ static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "ssa1",
 vlc_module_begin();
     set_description( _("Text subtitles demux") );
     set_capability( "subtitle demux", 12 );
-    add_category_hint( "Subtitles", NULL, VLC_TRUE );
-        add_float( "sub-fps", 25.0, NULL,
-                   "Frames per second",
-                   SUB_FPS_LONGTEXT, VLC_TRUE );
-        add_integer( "sub-delay", 0, NULL,
-                     "Delay subtitles (in 1/10s)",
-                     SUB_DELAY_LONGTEXT, VLC_TRUE );
-        add_string( "sub-type", "auto", NULL, "subtitles type",
-                    SUB_TYPE_LONGTEXT, VLC_TRUE );
-            change_string_list( ppsz_sub_type, 0, 0 );
+    add_float( "sub-fps", 0.0, NULL,
+               N_("Frames per second"),
+               SUB_FPS_LONGTEXT, VLC_TRUE );
+    add_integer( "sub-delay", 0, NULL,
+                 N_("Delay subtitles (in 1/10s)"),
+                 SUB_DELAY_LONGTEXT, VLC_TRUE );
+    add_string( "sub-type", "auto", NULL, "Subtitles fileformat",
+                SUB_TYPE_LONGTEXT, VLC_TRUE );
+        change_string_list( ppsz_sub_type, 0, 0 );
     set_callbacks( Open, NULL );
 vlc_module_end();
 
@@ -206,10 +207,13 @@ static void text_rewind( text_t *txt )
 
 static int  sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
 static int  sub_SubRipRead  ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
+static int  sub_SubViewer   ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
 static int  sub_SSARead     ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
 static int  sub_Vplayer     ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
 static int  sub_Sami        ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
-static int  sub_VobSub      ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
+static int  sub_VobSubIDX   ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
+
+static int  DemuxVobSub     ( subtitle_demux_t *, block_t * );
 
 static struct
 {
@@ -221,11 +225,12 @@ static struct
 {
     { "microdvd",   SUB_TYPE_MICRODVD,  "MicroDVD", sub_MicroDvdRead },
     { "subrip",     SUB_TYPE_SUBRIP,    "SubRIP",   sub_SubRipRead },
+    { "subviewer",  SUB_TYPE_SUBVIEWER, "SubViewer",sub_SubViewer },
     { "ssa1",       SUB_TYPE_SSA1,      "SSA-1",    sub_SSARead },
     { "ssa2-4",     SUB_TYPE_SSA2_4,    "SSA-2/3/4",sub_SSARead },
     { "vplayer",    SUB_TYPE_VPLAYER,   "VPlayer",  sub_Vplayer },
     { "sami",       SUB_TYPE_SAMI,      "SAMI",     sub_Sami },
-    { "vobsub",     SUB_TYPE_VOBSUB,    "VobSub",   sub_VobSub },
+    { "vobsub",     SUB_TYPE_VOBSUB,    "VobSub",   sub_VobSubIDX },
     { NULL,         SUB_TYPE_UNKNOWN,   "Unknown",  NULL }
 };
 
@@ -256,31 +261,27 @@ static char * local_stristr( char *psz_big, char *psz_little)
 /*****************************************************************************
  * sub_open: Open a subtitle file and add subtitle ES
  *****************************************************************************/
-static int  sub_open ( subtitle_demux_t *p_sub,
-                       input_thread_t  *p_input,
-                       char     *psz_name,
-                       mtime_t i_microsecperframe,
-                       int i_track_id )
+static int sub_open( subtitle_demux_t *p_sub, input_thread_t  *p_input,
+                     char *psz_name, mtime_t i_microsecperframe )
 {
     text_t  txt;
     vlc_value_t val;
-    es_format_t  fmt;
-
-    int     i;
-    int     i_sub_type;
-    int     i_max;
-    int (*pf_read_subtitle)( subtitle_demux_t *, text_t *, subtitle_t *, mtime_t ) = NULL;
+    es_format_t fmt;
+    int i, i_sub_type, i_max;
+    int (*pf_read_subtitle)( subtitle_demux_t *, text_t *, subtitle_t *,
+                             mtime_t ) = NULL;
 
     p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
     p_sub->p_es = NULL;
     p_sub->i_subtitles = 0;
     p_sub->subtitle = NULL;
+    p_sub->p_vobsub_file = 0;
+    p_sub->i_original_mspf = i_microsecperframe;
     p_sub->p_input = p_input;
 
     if( !psz_name || !*psz_name )
     {
         msg_Err( p_sub, "no subtitle file specified" );
-        if( psz_name ) free( psz_name );
         return VLC_EGENERIC;
     }
 
@@ -288,20 +289,25 @@ static int  sub_open ( subtitle_demux_t *p_sub,
     if( text_load( &txt, psz_name ) )
     {
         msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
-        free( psz_name );
         return VLC_EGENERIC;
     }
+
     msg_Dbg( p_sub, "opened `%s'", psz_name );
-    free( psz_name );
 
     var_Get( p_sub, "sub-fps", &val );
     if( val.i_int >= 1.0 )
     {
         i_microsecperframe = (mtime_t)( (float)1000000 / val.f_float );
     }
+    else if( val.f_float == 0 )
+    {
+        /* No value given */
+        i_microsecperframe = 0;
+    }
     else if( val.f_float <= 0 )
     {
-        i_microsecperframe = 40000; /* default: 25fps */
+        /* invalid value, default = 25fps */
+        i_microsecperframe = 40000;
     }
 
     var_Get( p_sub, "sub-type", &val);
@@ -389,6 +395,11 @@ static int  sub_open ( subtitle_demux_t *p_sub,
                 i_sub_type = SUB_TYPE_SSA2_4; /* could be wrong */
                 break;
             }
+            else if( local_stristr( s, "[INFORMATION]" ) )
+            {
+                i_sub_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
+                break;
+            }
             else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
                      sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
             {
@@ -430,22 +441,11 @@ static int  sub_open ( subtitle_demux_t *p_sub,
         if( p_sub->i_subtitles >= i_max )
         {
             i_max += 128;
-            if( p_sub->subtitle )
+            if( !( p_sub->subtitle = realloc( p_sub->subtitle,
+                                              sizeof(subtitle_t) * i_max ) ) )
             {
-                if( !( p_sub->subtitle = realloc( p_sub->subtitle,
-                                           sizeof( subtitle_t ) * i_max ) ) )
-                {
-                    msg_Err( p_sub, "out of memory");
-                    return VLC_ENOMEM;
-                }
-            }
-            else
-            {
-                if( !(  p_sub->subtitle = malloc( sizeof( subtitle_t ) * i_max ) ) )
-                {
-                    msg_Err( p_sub, "out of memory");
-                    return VLC_ENOMEM;
-                }
+                msg_Err( p_sub, "out of memory");
+                return VLC_ENOMEM;
             }
         }
         if( pf_read_subtitle( p_sub, &txt,
@@ -463,6 +463,7 @@ static int  sub_open ( subtitle_demux_t *p_sub,
 
     /* *** fix subtitle (order and time) *** */
     p_sub->i_subtitle = 0;  /* will be modified by sub_fix */
+    
     if( p_sub->i_sub_type != SUB_TYPE_VOBSUB )
     {
         sub_fix( p_sub );
@@ -471,6 +472,18 @@ static int  sub_open ( subtitle_demux_t *p_sub,
     /* *** add subtitle ES *** */
     if( p_sub->i_sub_type == SUB_TYPE_VOBSUB )
     {
+        int i_len = strlen( psz_name );
+        char *psz_vobname = strdup(psz_name);
+
+        strcpy( psz_vobname + i_len - 4, ".sub" );
+
+        /* open file */
+        if( !( p_sub->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
+        {
+            msg_Err( p_sub, "couldn't open .sub Vobsub file: %s", psz_vobname );
+        }
+        free( psz_vobname );
+
         es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
     }
     else if( p_sub->i_sub_type == SUB_TYPE_SSA1 ||
@@ -488,8 +501,8 @@ static int  sub_open ( subtitle_demux_t *p_sub,
         fmt.p_extra = strdup( p_sub->psz_header );
     }
     p_sub->p_es = es_out_Add( p_input->p_es_out, &fmt );
-
     p_sub->i_previously_selected = 0;
+
     return VLC_SUCCESS;
 }
 
@@ -500,6 +513,8 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
 {
     input_thread_t *p_input = p_sub->p_input;
     vlc_bool_t     b;
+    vlc_value_t    val;
+    mtime_t i_delay;
 
     es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_sub->p_es, &b );
     if( b && !p_sub->i_previously_selected )
@@ -514,59 +529,111 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
         return VLC_SUCCESS;
     }
 
-    while( p_sub->i_subtitle < p_sub->i_subtitles &&
-           p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
+    if( p_sub->i_sub_type != SUB_TYPE_VOBSUB )
     {
-        block_t *p_block;
-        int i_len;
+        var_Get( p_sub, "sub-delay", &val );
+        i_delay = (mtime_t) val.i_int * 100000;
+        while( p_sub->i_subtitle < p_sub->i_subtitles &&
+               p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate - i_delay )
+        {
+            block_t *p_block;
+            int i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
 
-        i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
+            if( i_len <= 1 )
+            {
+                /* empty subtitle */
+                p_sub->i_subtitle++;
+                continue;
+            }
 
-        if( i_len <= 1 )
-        {
-            /* empty subtitle */
-            p_sub->i_subtitle++;
-            continue;
-        }
+            if( ( p_block = block_New( p_sub->p_input, i_len ) ) == NULL )
+            {
+                p_sub->i_subtitle++;
+                continue;
+            }
 
-        if( !( p_block = block_New( p_sub->p_input, i_len ) ) )
-        {
-            p_sub->i_subtitle++;
-            continue;
-        }
+            /* XXX we should convert all demuxers to use es_out_Control to set              * pcr and then remove that */
+            if( i_delay != 0 )
+            {
+                p_sub->subtitle[p_sub->i_subtitle].i_start += i_delay;
+                p_sub->subtitle[p_sub->i_subtitle].i_stop += i_delay;
+            }
 
-        p_block->i_pts =
-            input_ClockGetTS( p_sub->p_input,
-                              p_sub->p_input->stream.p_selected_program,
-                              p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
-        if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
-        {
-            /* FIXME kludge ...
-             * i_dts means end of display...
-             */
-            p_block->i_dts =
-                input_ClockGetTS( p_sub->p_input,
-                              p_sub->p_input->stream.p_selected_program,
-                              p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
-        }
-        else
-        {
-            p_block->i_dts = 0;
-        }
+            if( p_sub->subtitle[p_sub->i_subtitle].i_start < 0 )
+            {
+                p_sub->i_subtitle++;
+                continue;
+            }
 
-        memcpy( p_block->p_buffer,
-                p_sub->subtitle[p_sub->i_subtitle].psz_text, i_len );
+            p_block->i_pts = p_sub->subtitle[p_sub->i_subtitle].i_start;
+            p_block->i_dts = p_block->i_pts;
+            if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
+            {
+                p_block->i_length =
+                    p_sub->subtitle[p_sub->i_subtitle].i_stop - p_block->i_pts;
+            }
 
-        if( p_block->i_pts > 0 )
-        {
-            es_out_Send( p_input->p_es_out, p_sub->p_es, p_block );
+            memcpy( p_block->p_buffer,
+                    p_sub->subtitle[p_sub->i_subtitle].psz_text, i_len );
+            if( p_block->i_pts > 0 )
+            {
+                es_out_Send( p_input->p_es_out, p_sub->p_es, p_block );
+            }
+            else
+            {
+                block_Release( p_block );
+            }
+            p_sub->i_subtitle++;
         }
-        else
+    }
+    else
+    {
+        while( p_sub->i_subtitle < p_sub->i_subtitles &&
+               p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
         {
-            block_Release( p_block );
-        }
+            int i_pos = p_sub->subtitle[p_sub->i_subtitle].i_vobsub_location;
+            block_t *p_block;
+            int i_size = 0;
 
-        p_sub->i_subtitle++;
+            /* first compute SPU size */
+            if( p_sub->i_subtitle + 1 < p_sub->i_subtitles )
+            {
+                i_size = p_sub->subtitle[p_sub->i_subtitle+1].i_vobsub_location - i_pos;
+            }
+            if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
+
+            /* Seek at the right place (could be avoid if sub_seek is fixed to do his job) */
+            if( fseek( p_sub->p_vobsub_file, i_pos, SEEK_SET ) )
+            {
+                msg_Warn( p_sub, "cannot seek at right vobsub location %d", i_pos );
+                p_sub->i_subtitle++;
+                continue;
+            }
+
+            /* allocate a packet */
+            if( ( p_block = block_New( p_sub, i_size ) ) == NULL )
+            {
+                p_sub->i_subtitle++;
+                continue;
+            }
+
+            /* read data */
+            p_block->i_buffer = fread( p_block->p_buffer, 1, i_size, p_sub->p_vobsub_file );
+            if( p_block->i_buffer <= 6 )
+            {
+                block_Release( p_block );
+                p_sub->i_subtitle++;
+                continue;
+            }
+
+            /* pts */
+            p_block->i_pts = p_sub->subtitle[p_sub->i_subtitle].i_start;
+
+            /* demux this block */
+            DemuxVobSub( p_sub, p_block );
+
+            p_sub->i_subtitle++;
+        }
     }
     return VLC_SUCCESS;
 }
@@ -603,6 +670,10 @@ static void sub_close( subtitle_demux_t *p_sub )
         }
         free( p_sub->subtitle );
     }
+    if( p_sub->p_vobsub_file )
+    {
+        fclose( p_sub->p_vobsub_file );
+    }
 }
 
 /*****************************************************************************
@@ -610,11 +681,8 @@ static void sub_close( subtitle_demux_t *p_sub )
  *****************************************************************************/
 static void  sub_fix( subtitle_demux_t *p_sub )
 {
-    int     i;
-    mtime_t i_delay;
     int     i_index;
     int     i_done;
-    vlc_value_t val;
 
     /* *** fix order (to be sure...) *** */
     /* We suppose that there are near in order and this durty bubble sort
@@ -642,7 +710,8 @@ static void  sub_fix( subtitle_demux_t *p_sub )
             }
         }
     } while( !i_done );
-
+#if 0
+    /* We do not do this here anymore */
     /* *** and at the end add delay *** */
     var_Get( p_sub, "sub-delay", &val );
     i_delay = (mtime_t) val.i_int * 100000;
@@ -658,6 +727,7 @@ static void  sub_fix( subtitle_demux_t *p_sub )
             }
         }
     }
+#endif
 }
 
 
@@ -676,9 +746,14 @@ static int  sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *
     char *s;
 
     char buffer_text[MAX_LINE + 1];
-    uint32_t    i_start;
-    uint32_t    i_stop;
+    unsigned int    i_start;
+    unsigned int    i_stop;
     unsigned int i;
+    
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
 
     for( ;; )
     {
@@ -704,6 +779,10 @@ static int  sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *
             buffer_text[i] = '\n';
         }
     }
+    if( i_microsecperframe == 0)
+    {
+        i_microsecperframe = 40000;
+    }
     p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
     p_subtitle->i_stop  = (mtime_t)i_stop  * (mtime_t)i_microsecperframe;
     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
@@ -727,6 +806,11 @@ static int  sub_SubRipRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_
     mtime_t     i_start;
     mtime_t     i_stop;
 
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
+
     for( ;; )
     {
         int h1, m1, s1, d1, h2, m2, s2, d2;
@@ -766,6 +850,108 @@ static int  sub_SubRipRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_
                     p_subtitle->i_start = i_start;
                     p_subtitle->i_stop = i_stop;
                     p_subtitle->psz_text = strdup( buffer_text );
+                    /* If framerate is available, use sub-fps */
+                    if( i_microsecperframe != 0 && p_sub->i_original_mspf != 0)
+                    {
+                        p_subtitle->i_start = (mtime_t)i_start *
+                                              (mtime_t)p_sub->i_original_mspf /
+                                              (mtime_t)i_microsecperframe;
+                        p_subtitle->i_stop  = (mtime_t)i_stop  *
+                                              (mtime_t)p_sub->i_original_mspf /
+                                              (mtime_t)i_microsecperframe;
+                    }
+                    return( 0 );
+                }
+                else
+                {
+                    if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
+                    {
+                        memcpy( buffer_text + i_buffer_text,
+                                s,
+                                i_len );
+                        i_buffer_text += i_len;
+
+                        buffer_text[i_buffer_text] = '\n';
+                        i_buffer_text++;
+                    }
+                }
+            }
+        }
+    }
+}
+
+static int  sub_SubViewer( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
+{
+    /*
+     * h1:m1:s1.d1,h2:m2:s2.d2
+     * Line1[br]Line2
+     * Line3
+     * ...
+     * [empty line]
+     * ( works with subviewer and subviewer v2 )
+     */
+    char *s;
+    char buffer_text[ 10 * MAX_LINE];
+    int  i_buffer_text;
+    mtime_t     i_start;
+    mtime_t     i_stop;
+
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
+
+    for( ;; )
+    {
+        int h1, m1, s1, d1, h2, m2, s2, d2;
+        if( ( s = text_get_line( txt ) ) == NULL )
+        {
+            return( VLC_EGENERIC );
+        }
+        if( sscanf( s,
+                    "%d:%d:%d.%d,%d:%d:%d.%d",
+                    &h1, &m1, &s1, &d1,
+                    &h2, &m2, &s2, &d2 ) == 8 )
+        {
+            i_start = ( (mtime_t)h1 * 3600*1000 +
+                        (mtime_t)m1 * 60*1000 +
+                        (mtime_t)s1 * 1000 +
+                        (mtime_t)d1 ) * 1000;
+
+            i_stop  = ( (mtime_t)h2 * 3600*1000 +
+                        (mtime_t)m2 * 60*1000 +
+                        (mtime_t)s2 * 1000 +
+                        (mtime_t)d2 ) * 1000;
+
+            /* Now read text until an empty line */
+            for( i_buffer_text = 0;; )
+            {
+                int i_len, i;
+                if( ( s = text_get_line( txt ) ) == NULL )
+                {
+                    return( VLC_EGENERIC );
+                }
+
+                i_len = strlen( s );
+                if( i_len <= 1 )
+                {
+                    /* empty line -> end of this subtitle */
+                    buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
+                    p_subtitle->i_start = i_start;
+                    p_subtitle->i_stop = i_stop;
+
+                    /* replace [br] by \n */
+                    for( i = 0; i < strlen( buffer_text ) - 3; i++ )
+                    {
+                        if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
+                            buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
+                        {
+                            char *temp = buffer_text + i + 1;
+                            buffer_text[i] = '\n';
+                            memmove( temp, temp+3, strlen( temp-3 ));
+                        }
+                    }
+                    p_subtitle->psz_text = strdup( buffer_text );
                     return( 0 );
                 }
                 else
@@ -794,6 +980,11 @@ static int  sub_SSARead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_sub
     mtime_t     i_start;
     mtime_t     i_stop;
 
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
+
     for( ;; )
     {
         int h1, m1, s1, c1, h2, m2, s2, c2;
@@ -870,13 +1061,17 @@ static int  sub_Vplayer( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_sub
      *  h:m:s:Line1|Line2|Line3....
      *  or
      *  h:m:s Line1|Line2|Line3....
-     * where n1 and n2 are the video frame number...
      *
      */
     char *p;
     char buffer_text[MAX_LINE + 1];
     mtime_t    i_start;
     unsigned int i;
+    
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
 
     for( ;; )
     {
@@ -953,6 +1148,12 @@ static int  sub_Sami( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtit
 
     int  i_text;
     char buffer_text[10*MAX_LINE + 1];
+
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
+
 #define ADDC( c ) \
     if( i_text < 10*MAX_LINE )      \
     {                               \
@@ -1035,7 +1236,7 @@ static int  sub_Sami( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtit
 #undef ADDC
 }
 
-static int  sub_VobSub( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
+static int  sub_VobSubIDX( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
 {
     /*
      * Parse the idx file. Each line:
@@ -1044,9 +1245,13 @@ static int  sub_VobSub( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subt
      *
      */
     char *p;
-
     char buffer_text[MAX_LINE + 1];
-    uint32_t    i_start, i_location;
+    unsigned int    i_start, i_location;
+
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
 
     for( ;; )
     {
@@ -1072,9 +1277,71 @@ static int  sub_VobSub( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subt
     }
     p_subtitle->i_start = (mtime_t)i_start;
     p_subtitle->i_stop  = 0;
+    p_subtitle->psz_text = NULL;
     p_subtitle->i_vobsub_location = i_location;
     fprintf( stderr, "time: %x, location: %x\n", i_start, i_location );
     return( 0 );
 }
 
+static int  DemuxVobSub( subtitle_demux_t *p_demux, block_t *p_bk )
+{
+    uint8_t     *p = p_bk->p_buffer;
+    uint8_t     *p_end = &p_bk->p_buffer[p_bk->i_buffer];
+
+    while( p < p_end )
+    {
+        int i_size = ps_pkt_size( p, p_end - p );
+        block_t *p_pkt;
+        int      i_id;
+        int      i_spu;
+
+        if( i_size <= 0 )
+        {
+            break;
+        }
+        if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
+        {
+            msg_Warn( p_demux, "invalid PES" );
+            break;
+        }
+
+        if( p[3] != 0xbd )
+        {
+            msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] );
+            p += i_size;
+            continue;
+        }
+
+        /* Create a block */
+        p_pkt = block_New( p_demux, i_size );
+        memcpy( p_pkt->p_buffer, p, i_size);
+        p += i_size;
+
+        i_id = ps_pkt_id( p_pkt );
+        if( (i_id&0xffe0) != 0xbd20 ||
+            ps_pkt_parse_pes( p_pkt, 1 ) )
+        {
+            block_Release( p_pkt );
+            continue;
+        }
+        i_spu = i_id&0x1f;
+        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. */
+        if( p_demux->p_es && i_spu == 0 )
+        {
+            p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
+            p_pkt->i_length = 0;
+            es_out_Send( p_demux->p_input->p_es_out, p_demux->p_es, p_pkt );
 
+            p_bk->i_pts = 0;    /* only first packet has a pts */
+        }
+        else
+        {
+            block_Release( p_pkt );
+            continue;
+        }
+    }
+
+    return VLC_SUCCESS;
+}