]> 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 bb26bbd7532d209e5203d1d2b2ed2fc9f75ba597..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.29 2003/10/12 21:53:58 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 <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
+#include <ctype.h>
 
 #include <vlc/vlc.h>
 #include <vlc/input.h>
 #include "vlc_video.h"
+#include <codecs.h>
 
 #include "sub.h"
 
 #    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", "ssa2-4", "vplayer", "sami", NULL };
-
+static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "subviewer", "ssa1",
+  "ssa2-4", "vplayer", "sami", "vobsub" };
 
 /*****************************************************************************
  * Module descriptor
@@ -69,16 +73,15 @@ static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "ssa1", "ssa2-4",
 vlc_module_begin();
     set_description( _("Text subtitles demux") );
     set_capability( "subtitle demux", 12 );
-    add_category_hint( "Subtitles", NULL, VLC_TRUE );
-        add_float( "sub-fps", 0.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_from_list( "sub-type", "auto", ppsz_sub_type, NULL,
-                              "subtitles type",
-                              SUB_TYPE_LONGTEXT, VLC_TRUE );
+    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();
 
@@ -202,79 +205,109 @@ static void text_rewind( text_t *txt )
     txt->i_line = 0;
 }
 
-static int  sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
-static int  sub_SubRipRead  ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
-static int  sub_SSA1Read    ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
-static int  sub_SSA2_4Read  ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
-static int  sub_Vplayer     ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
-static int  sub_Sami        ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
+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_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
 {
     char *psz_type_name;
     int  i_type;
     char *psz_name;
-    int  (*pf_read_subtitle)    ( text_t *, subtitle_t*, mtime_t );
+    int  (*pf_read_subtitle)    ( subtitle_demux_t *, text_t *, subtitle_t*, mtime_t );
 } sub_read_subtitle_function [] =
 {
     { "microdvd",   SUB_TYPE_MICRODVD,  "MicroDVD", sub_MicroDvdRead },
     { "subrip",     SUB_TYPE_SUBRIP,    "SubRIP",   sub_SubRipRead },
-    { "ssa1",       SUB_TYPE_SSA1,      "SSA-1",    sub_SSA1Read },
-    { "ssa2-4",     SUB_TYPE_SSA2_4,    "SSA-2/3/4",sub_SSA2_4Read },
+    { "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 },
-    { NULL,         SUB_TYPE_UNKNOWN,   "Unknow",   NULL }
+    { "vobsub",     SUB_TYPE_VOBSUB,    "VobSub",   sub_VobSubIDX },
+    { NULL,         SUB_TYPE_UNKNOWN,   "Unknown",  NULL }
 };
 
+static char * local_stristr( char *psz_big, char *psz_little)
+{
+    char *p_pos = psz_big;
+
+    if (!psz_big || !psz_little || !*psz_little) return psz_big;
+
+    while (*p_pos)
+    {
+        if (toupper(*p_pos) == toupper(*psz_little))
+        {
+            char * psz_cur1 = p_pos + 1;
+            char * psz_cur2 = psz_little + 1;
+            while (*psz_cur1 && *psz_cur2 && toupper(*psz_cur1) == toupper(*psz_cur2))
+            {
+                psz_cur1++;
+                psz_cur2++;
+            }
+            if (!*psz_cur2) return p_pos;
+        }
+        p_pos++;
+    }
+    return NULL;
+}
+
 /*****************************************************************************
  * 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;
-
-    int     i;
-    int     i_sub_type;
-    int     i_max;
-    int (*pf_read_subtitle)( 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;
     }
-    
+
     /* *** load the file *** */
     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 )
     {
-        var_Get( p_sub, "sub-fps", &val );
         i_microsecperframe = (mtime_t)( (float)1000000 / val.f_float );
     }
-    else if( i_microsecperframe <= 0 )
+    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);
@@ -319,7 +352,7 @@ static int  sub_open ( subtitle_demux_t *p_sub,
                 break;
             }
 
-            if( strstr( s, "<SAMI>" ) )
+            if( local_stristr( s, "<SAMI>" ) )
             {
                 i_sub_type = SUB_TYPE_SAMI;
                 break;
@@ -348,17 +381,23 @@ static int  sub_open ( subtitle_demux_t *p_sub,
                 }
                 else
                 {
-                    i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
+                    i_sub_type = SUB_TYPE_SSA2_4; /* I hope this will work */
                 }
                 break;
             }
-            else if( strstr( s, "This is a Sub Station Alpha v4 script" ) )
+            else if( local_stristr( s, "This is a Sub Station Alpha v4 script" ) )
             {
-                i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
+                i_sub_type = SUB_TYPE_SSA2_4; /* I hope this will work */
+                break;
             }
-            else if( !strncmp( s, "Dialogue: Marked", 16  ) )
+            else if( !strncasecmp( s, "Dialogue: Marked", 16  ) )
             {
-                i_sub_type = SUB_TYPE_SSA2_4; // could be wrong
+                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 ||
@@ -367,6 +406,11 @@ static int  sub_open ( subtitle_demux_t *p_sub,
                 i_sub_type = SUB_TYPE_VPLAYER;
                 break;
             }
+            else if( local_stristr( s, "# VobSub index file" ) )
+            {
+                i_sub_type = SUB_TYPE_VOBSUB;
+                break;
+            }
         }
 
         text_rewind( &txt );
@@ -386,6 +430,7 @@ static int  sub_open ( subtitle_demux_t *p_sub,
         {
             msg_Dbg( p_input, "detected %s format",
                     sub_read_subtitle_function[i].psz_name );
+            p_sub->i_sub_type = i_sub_type;
             pf_read_subtitle = sub_read_subtitle_function[i].pf_read_subtitle;
             break;
         }
@@ -396,17 +441,14 @@ 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 ) ) )
             {
-                p_sub->subtitle = realloc( p_sub->subtitle,
-                                           sizeof( subtitle_t ) * i_max );
-            }
-            else
-            {
-                p_sub->subtitle = malloc( sizeof( subtitle_t ) * i_max );
+                msg_Err( p_sub, "out of memory");
+                return VLC_ENOMEM;
             }
         }
-        if( pf_read_subtitle( &txt,
+        if( pf_read_subtitle( p_sub, &txt,
                               p_sub->subtitle + p_sub->i_subtitles,
                               i_microsecperframe ) < 0 )
         {
@@ -420,20 +462,47 @@ static int  sub_open ( subtitle_demux_t *p_sub,
     text_unload( &txt );
 
     /* *** fix subtitle (order and time) *** */
-    p_sub->i_subtitle = 0;  // will be modified by sub_fix
-    sub_fix( p_sub );
+    p_sub->i_subtitle = 0;  /* will be modified by sub_fix */
+    
+    if( p_sub->i_sub_type != SUB_TYPE_VOBSUB )
+    {
+        sub_fix( p_sub );
+    }
 
     /* *** add subtitle ES *** */
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    p_sub->p_es = input_AddES( p_input, p_input->stream.p_selected_program,
-                               0xff - i_track_id,    /* FIXME */
-                               SPU_ES, NULL, 0 );
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
+    if( p_sub->i_sub_type == SUB_TYPE_VOBSUB )
+    {
+        int i_len = strlen( psz_name );
+        char *psz_vobname = strdup(psz_name);
 
-    p_sub->p_es->i_stream_id = 0xff - i_track_id;    /* FIXME */
-    p_sub->p_es->i_fourcc    = VLC_FOURCC( 's','u','b','t' );
+        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 ||
+             p_sub->i_sub_type == SUB_TYPE_SSA2_4 )
+    {
+        es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
+    }
+    else
+    {
+        es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
+    }
+    if( p_sub->psz_header != NULL )
+    {
+        fmt.i_extra = strlen( p_sub->psz_header ) + 1;
+        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;
 }
 
@@ -442,89 +511,131 @@ static int  sub_open ( subtitle_demux_t *p_sub,
  *****************************************************************************/
 static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
 {
-    if( p_sub->p_es->p_decoder_fifo && !p_sub->i_previously_selected )
+    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 )
     {
         p_sub->i_previously_selected = 1;
         p_sub->pf_seek( p_sub, i_maxdate );
         return VLC_SUCCESS;
     }
-    else if( !p_sub->p_es->p_decoder_fifo && p_sub->i_previously_selected )
+    else if( !b && p_sub->i_previously_selected )
     {
         p_sub->i_previously_selected = 0;
         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 )
     {
-        pes_packet_t    *p_pes;
-        data_packet_t   *p_data;
+        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;
+
+            if( i_len <= 1 )
+            {
+                /* empty subtitle */
+                p_sub->i_subtitle++;
+                continue;
+            }
 
-        int i_len;
+            if( ( p_block = block_New( p_sub->p_input, i_len ) ) == NULL )
+            {
+                p_sub->i_subtitle++;
+                continue;
+            }
 
-        i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
+            /* 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;
+            }
 
-        if( i_len <= 1 )
-        {
-            /* empty subtitle */
+            if( p_sub->subtitle[p_sub->i_subtitle].i_start < 0 )
+            {
+                p_sub->i_subtitle++;
+                continue;
+            }
+
+            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;
+            }
+
+            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++;
-            continue;
         }
-        if( !( p_pes = input_NewPES( p_sub->p_input->p_method_data ) ) )
+    }
+    else
+    {
+        while( p_sub->i_subtitle < p_sub->i_subtitles &&
+               p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
         {
-            p_sub->i_subtitle++;
-            continue;
-        }
+            int i_pos = p_sub->subtitle[p_sub->i_subtitle].i_vobsub_location;
+            block_t *p_block;
+            int i_size = 0;
 
-        if( !( p_data = input_NewPacket( p_sub->p_input->p_method_data,
-                                         i_len ) ) )
-        {
-            input_DeletePES( p_sub->p_input->p_method_data, p_pes );
-            p_sub->i_subtitle++;
-            continue;
-        }
-        p_data->p_payload_end = p_data->p_payload_start + i_len;
+            /* 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 */
 
-        p_pes->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_pes->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_pes->i_dts = 0;
-        }
+            /* 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;
+            }
 
-        p_pes->i_nb_data = 1;
-        p_pes->p_first =
-            p_pes->p_last = p_data;
-        p_pes->i_pes_size = i_len;
+            /* allocate a packet */
+            if( ( p_block = block_New( p_sub, i_size ) ) == NULL )
+            {
+                p_sub->i_subtitle++;
+                continue;
+            }
 
-        memcpy( p_data->p_payload_start,
-                p_sub->subtitle[p_sub->i_subtitle].psz_text,
-                i_len );
-        if( p_sub->p_es->p_decoder_fifo && p_pes->i_pts > 0 )
-        {
+            /* 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;
+            }
 
-            input_DecodePES( p_sub->p_es->p_decoder_fifo, p_pes );
-        }
-        else
-        {
-            input_DeletePES( p_sub->p_input->p_method_data, p_pes );
-        }
+            /* pts */
+            p_block->i_pts = p_sub->subtitle[p_sub->i_subtitle].i_start;
 
-        p_sub->i_subtitle++;
+            /* demux this block */
+            DemuxVobSub( p_sub, p_block );
+
+            p_sub->i_subtitle++;
+        }
     }
-    return( 0 );
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
@@ -539,7 +650,6 @@ static int  sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
     {
         p_sub->i_subtitle++;
     }
-
     return( 0 );
 }
 
@@ -560,18 +670,19 @@ 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 );
+    }
 }
+
 /*****************************************************************************
- *
  * sub_fix: fix time stamp and order of subtitle
  *****************************************************************************/
 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
@@ -599,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;
@@ -615,6 +727,7 @@ static void  sub_fix( subtitle_demux_t *p_sub )
             }
         }
     }
+#endif
 }
 
 
@@ -622,7 +735,7 @@ static void  sub_fix( subtitle_demux_t *p_sub )
 /*****************************************************************************
  * Specific Subtitle function
  *****************************************************************************/
-static int  sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
+static int  sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
 {
     /*
      * each line:
@@ -633,9 +746,14 @@ static int  sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_mic
     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( ;; )
     {
@@ -661,13 +779,17 @@ static int  sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_mic
             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 );
     return( 0 );
 }
 
-static int  sub_SubRipRead( 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 )
 {
     /*
      * n
@@ -684,6 +806,11 @@ static int  sub_SubRipRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_micro
     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;
@@ -718,11 +845,21 @@ static int  sub_SubRipRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_micro
                 i_len = strlen( s );
                 if( i_len <= 1 )
                 {
-                    // empty line -> end of this subtitle
+                    /* 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;
                     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
@@ -743,16 +880,110 @@ static int  sub_SubRipRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_micro
     }
 }
 
+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;
 
-static int  sub_SSARead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe, int i_comma_count )
+    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
+                {
+                    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_SSARead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
 {
     char buffer_text[ 10 * MAX_LINE];
     char *s;
-    char *p_buffer_text;
     mtime_t     i_start;
     mtime_t     i_stop;
-    int         i_comma;
-    int         i_text;
+
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
 
     for( ;; )
     {
@@ -763,8 +994,10 @@ static int  sub_SSARead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsec
         {
             return( VLC_EGENERIC );
         }
+        p_subtitle->psz_text = malloc( strlen( s ) );
+
         if( sscanf( s,
-                    "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
+                    "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
                     &i_dummy,
                     &h1, &m1, &s1, &c1,
                     &h2, &m2, &s2, &c2,
@@ -780,72 +1013,65 @@ static int  sub_SSARead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsec
                         (mtime_t)s2 * 1000 +
                         (mtime_t)c2 * 10 ) * 1000;
 
-            p_buffer_text = buffer_text;
-            i_comma = 3;
-            while( i_comma < i_comma_count &&
-                   *p_buffer_text != '\0' )
+            /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
+            if( p_sub->i_sub_type == SUB_TYPE_SSA1 )
             {
-                if( *p_buffer_text == ',' )
-                {
-                    i_comma++;
-                }
-                p_buffer_text++;
+                sprintf( p_subtitle->psz_text, ",%d%s", i_dummy, strdup( buffer_text) );
+            }
+            else
+            {
+                sprintf( p_subtitle->psz_text, ",%d,%s", i_dummy, strdup( buffer_text) );
             }
-            p_subtitle->psz_text = malloc( strlen( p_buffer_text ) + 1);
-            i_text = 0;
-            while( *p_buffer_text )
+            p_subtitle->i_start = i_start;
+            p_subtitle->i_stop = i_stop;
+            return( 0 );
+        }
+        else
+        {
+            /* All the other stuff we add to the header field */
+            if( p_sub->psz_header != NULL )
             {
-                if( *p_buffer_text == '\\' && ( *p_buffer_text =='n' || *p_buffer_text =='N' ) )
-                {
-                    p_subtitle->psz_text[i_text] = '\n';
-                    i_text++;
-                    p_buffer_text += 2;
-                }
-                else if( *p_buffer_text == '{' && *p_buffer_text == '\\')
+                if( !( p_sub->psz_header = realloc( p_sub->psz_header,
+                          strlen( p_sub->psz_header ) + strlen( s ) + 2 ) ) )
                 {
-                    while( *p_buffer_text && *p_buffer_text != '}' )
-                    {
-                        p_buffer_text++;
-                    }
+                    msg_Err( p_sub, "out of memory");
+                    return VLC_ENOMEM;
                 }
-                else
+                p_sub->psz_header = strcat( p_sub->psz_header, strdup( s ) );
+                p_sub->psz_header = strcat( p_sub->psz_header, "\n" );
+            }
+            else
+            {
+                if( !( p_sub->psz_header = malloc( strlen( s ) + 2 ) ) )
                 {
-                    p_subtitle->psz_text[i_text] = *p_buffer_text;
-                    i_text++;
-                    p_buffer_text++;
+                    msg_Err( p_sub, "out of memory");
+                    return VLC_ENOMEM;
                 }
+                p_sub->psz_header = strdup( s );
+                p_sub->psz_header = strcat( p_sub->psz_header, "\n" );
             }
-            p_subtitle->psz_text[i_text] = '\0';
-            p_subtitle->i_start = i_start;
-            p_subtitle->i_stop = i_stop;
-            return( 0 );
         }
     }
 }
 
-static int  sub_SSA1Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
-{
-    return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 8 ) );
-}
-static int  sub_SSA2_4Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
-{
-    return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 9 ) );
-}
-
-static int  sub_Vplayer( 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)
 {
     /*
      * each line:
      *  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( ;; )
     {
@@ -888,9 +1114,9 @@ static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
 {
     if( psz_start )
     {
-        if( strstr( psz_start, psz_str ) )
+        if( local_stristr( psz_start, psz_str ) )
         {
-            char *s = strstr( psz_start, psz_str );
+            char *s = local_stristr( psz_start, psz_str );
 
             s += strlen( psz_str );
 
@@ -904,9 +1130,9 @@ static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
         {
             return NULL;
         }
-        if( strstr( p, psz_str ) )
+        if( local_stristr( p, psz_str ) )
         {
-            char *s = strstr( p, psz_str );
+            char *s = local_stristr( p, psz_str );
 
             s += strlen( psz_str );
 
@@ -915,13 +1141,19 @@ static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
     }
 }
 
-static int  sub_Sami( 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 )
 {
     char *p;
     int i_start;
 
     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 )      \
     {                               \
@@ -958,11 +1190,11 @@ static int  sub_Sami( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecper
         {
             if( *p == '<' )
             {
-                if( !strncmp( p, "<br", 3 ) || !strncmp( p, "<BR", 3 ) )
+                if( !strncasecmp( p, "<br", 3 ) )
                 {
                     ADDC( '\n' );
                 }
-                else if( strstr( p, "Start=" ) )
+                else if( local_stristr( p, "Start=" ) )
                 {
                     text_previous_line( txt );
                     break;
@@ -1004,3 +1236,112 @@ static int  sub_Sami( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecper
 #undef ADDC
 }
 
+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:
+     * timestamp: hh:mm:ss:mss, filepos: loc
+     * hexint is the hex location of the vobsub in the .sub file
+     *
+     */
+    char *p;
+    char buffer_text[MAX_LINE + 1];
+    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( ;; )
+    {
+        unsigned int h, m, s, ms, loc;
+
+        if( ( p = text_get_line( txt ) ) == NULL )
+        {
+            return( VLC_EGENERIC );
+        }
+        i_start = 0;
+
+        memset( buffer_text, '\0', MAX_LINE );
+        if( sscanf( p, "timestamp: %d:%d:%d:%d, filepos: %x%[^\r\n]",
+                    &h, &m, &s, &ms, &loc, buffer_text ) == 5 )
+        {
+            i_start = ( (mtime_t)h * 3600*1000 +
+                        (mtime_t)m * 60*1000 +
+                        (mtime_t)s * 1000 +
+                        (mtime_t)ms ) * 1000;
+            i_location = loc;
+            break;
+        }
+    }
+    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;
+}