]> git.sesse.net Git - vlc/blobdiff - modules/demux/subtitle.c
android/surface: remove single_instance
[vlc] / modules / demux / subtitle.c
index 9a1580e3b152938056f0e73af335b043b58cdff2..1b22c7db4756a629a91d8ed93f3f478e209d342a 100644 (file)
@@ -1,25 +1,26 @@
 /*****************************************************************************
  * subtitle.c: Demux for subtitle text files.
  *****************************************************************************
- * Copyright (C) 1999-2007 the VideoLAN team
+ * Copyright (C) 1999-2007 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Derk-Jan Hartman <hartman at videolan dot org>
+ *          Jean-Baptiste Kempf <jb@videolan.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
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_input.h>
+#include <vlc_memory.h>
 
-#include <errno.h>
-#ifdef HAVE_SYS_TYPES_H
-#   include <sys/types.h>
-#endif
 #include <ctype.h>
+#include <math.h>
 
 #include <vlc_demux.h>
 #include <vlc_charset.h>
@@ -55,36 +54,39 @@ static void Close( vlc_object_t *p_this );
     N_("Override the normal frames per second settings. " \
     "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
 #define SUB_TYPE_LONGTEXT \
-    N_("Force the subtiles format. Valid values are : \"microdvd\", " \
-    "\"subrip\",  \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
-    "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\" and \"auto\" (meaning autodetection, this " \
-    "should always work).")
-static const char *ppsz_sub_type[] =
+    N_("Force the subtiles format. Selecting \"auto\" means autodetection and should always work.")
+#define SUB_DESCRIPTION_LONGTEXT \
+    N_("Override the default track description.")
+
+static const char *const ppsz_sub_type[] =
 {
     "auto", "microdvd", "subrip", "subviewer", "ssa1",
     "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
-    "aqt", "pjs"
+    "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
+    "subviewer1","vtt"
 };
 
-vlc_module_begin();
-    set_shortname( _("Subtitles"));
-    set_description( _("Text subtitles parser") );
-    set_capability( "demux", 0 );
-    set_category( CAT_INPUT );
-    set_subcategory( SUBCAT_INPUT_DEMUX );
-    add_float( "sub-fps", 0.0, NULL,
-               N_("Frames per second"),
-               SUB_FPS_LONGTEXT, true );
-    add_integer( "sub-delay", 0, NULL,
-               N_("Subtitles delay"),
-               SUB_DELAY_LONGTEXT, true );
-    add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
-                SUB_TYPE_LONGTEXT, true );
-        change_string_list( ppsz_sub_type, NULL, NULL );
-    set_callbacks( Open, Close );
-
-    add_shortcut( "subtitle" );
-vlc_module_end();
+vlc_module_begin ()
+    set_shortname( N_("Subtitles"))
+    set_description( N_("Text subtitle parser") )
+    set_capability( "demux", 0 )
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_DEMUX )
+    add_float( "sub-fps", 0.0,
+               N_("Frames per Second"),
+               SUB_FPS_LONGTEXT, true )
+    add_integer( "sub-delay", 0,
+               N_("Subtitle delay"),
+               SUB_DELAY_LONGTEXT, true )
+    add_string( "sub-type", "auto", N_("Subtitle format"),
+                SUB_TYPE_LONGTEXT, true )
+        change_string_list( ppsz_sub_type, ppsz_sub_type )
+    add_string( "sub-description", NULL, N_("Subtitle description"),
+                SUB_DESCRIPTION_LONGTEXT, true )
+    set_callbacks( Open, Close )
+
+    add_shortcut( "subtitle" )
+vlc_module_end ()
 
 /*****************************************************************************
  * Prototypes:
@@ -99,13 +101,19 @@ enum
     SUB_TYPE_ASS,
     SUB_TYPE_VPLAYER,
     SUB_TYPE_SAMI,
-    SUB_TYPE_SUBVIEWER, //SUBVIEWER 2!
-    SUB_TYPE_DVDSUBTITLE,
+    SUB_TYPE_SUBVIEWER, /* SUBVIEWER 2 */
+    SUB_TYPE_DVDSUBTITLE, /* Mplayer calls it subviewer2 */
     SUB_TYPE_MPL2,
     SUB_TYPE_AQT,
     SUB_TYPE_PJS,
     SUB_TYPE_MPSUB,
-    SUB_TYPE_JACOSUB
+    SUB_TYPE_JACOSUB,
+    SUB_TYPE_PSB,
+    SUB_TYPE_RT,
+    SUB_TYPE_DKS,
+    SUB_TYPE_SUBVIEW1, /* SUBVIEWER 1 - mplayer calls it subrip09,
+                         and Gnome subtitles SubViewer 1.0 */
+    SUB_TYPE_VTT
 };
 
 typedef struct
@@ -142,6 +150,23 @@ struct demux_sys_t
     subtitle_t  *subtitle;
 
     int64_t     i_length;
+
+    /* */
+    struct
+    {
+        bool b_inited;
+
+        int i_comment;
+        int i_time_resolution;
+        int i_time_shift;
+    } jss;
+    struct
+    {
+        bool  b_inited;
+
+        float f_total;
+        float f_factor;
+    } mpsub;
 };
 
 static int  ParseMicroDvd   ( demux_t *, subtitle_t *, int );
@@ -156,8 +181,13 @@ static int  ParseAQT        ( demux_t *, subtitle_t *, int );
 static int  ParsePJS        ( demux_t *, subtitle_t *, int );
 static int  ParseMPSub      ( demux_t *, subtitle_t *, int );
 static int  ParseJSS        ( demux_t *, subtitle_t *, int );
+static int  ParsePSB        ( demux_t *, subtitle_t *, int );
+static int  ParseRealText   ( demux_t *, subtitle_t *, int );
+static int  ParseDKS        ( demux_t *, subtitle_t *, int );
+static int  ParseSubViewer1 ( demux_t *, subtitle_t *, int );
+static int  ParseVTT        ( demux_t *, subtitle_t *, int );
 
-static struct
+static const struct
 {
     const char *psz_type_name;
     int  i_type;
@@ -179,21 +209,22 @@ static struct
     { "pjs",        SUB_TYPE_PJS,         "PhoenixSub",  ParsePJS },
     { "mpsub",      SUB_TYPE_MPSUB,       "MPSub",       ParseMPSub },
     { "jacosub",    SUB_TYPE_JACOSUB,     "JacoSub",     ParseJSS },
+    { "psb",        SUB_TYPE_PSB,         "PowerDivx",   ParsePSB },
+    { "realtext",   SUB_TYPE_RT,          "RealText",    ParseRealText },
+    { "dks",        SUB_TYPE_DKS,         "DKS",         ParseDKS },
+    { "subviewer1", SUB_TYPE_SUBVIEW1,    "Subviewer 1", ParseSubViewer1 },
+    { "text/vtt",   SUB_TYPE_VTT,         "WebVTT",      ParseVTT },
     { NULL,         SUB_TYPE_UNKNOWN,     "Unknown",     NULL }
 };
-
-/* Missing Detect
-    SubViewer 1
-    JSS
-    RealText
-    Subrip09
-   */
-
+/* When adding support for more formats, be sure to add their file extension
+ * to src/input/subtitles.c to enable auto-detection.
+ */
 
 static int Demux( demux_t * );
 static int Control( demux_t *, int, va_list );
 
-/*static void Fix( demux_t * );*/
+static void Fix( demux_t * );
+static char * get_language_from_filename( const char * );
 
 /*****************************************************************************
  * Module initializer
@@ -217,31 +248,37 @@ static int Open ( vlc_object_t *p_this )
     p_demux->pf_demux = Demux;
     p_demux->pf_control = Control;
     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
+    if( p_sys == NULL )
+        return VLC_ENOMEM;
+
     p_sys->psz_header         = NULL;
     p_sys->i_subtitle         = 0;
     p_sys->i_subtitles        = 0;
     p_sys->subtitle           = NULL;
     p_sys->i_microsecperframe = 40000;
 
+    p_sys->jss.b_inited       = false;
+    p_sys->mpsub.b_inited     = false;
+
     /* Get the FPS */
-    f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
-    if( f_fps >= 1.0 )
-        p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
+    f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" ); /* FIXME */
+    if( f_fps >= 1.f )
+        p_sys->i_microsecperframe = llroundf( 1000000.f / f_fps );
 
-    msg_Dbg( p_demux, "Movie fps: %f", f_fps );
+    msg_Dbg( p_demux, "Movie fps: %f", (double) f_fps );
 
     /* Check for override of the fps */
     f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
-    if( f_fps >= 1.0 )
+    if( f_fps >= 1.f )
     {
-        p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
-        msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
+        p_sys->i_microsecperframe = llroundf( 1000000.f / f_fps );
+        msg_Dbg( p_demux, "Override subtitle fps %f", (double) f_fps );
     }
 
     /* Get or probe the type */
     p_sys->i_type = SUB_TYPE_UNKNOWN;
     psz_type = var_CreateGetString( p_demux, "sub-type" );
-    if( *psz_type )
+    if( psz_type && *psz_type )
     {
         int i;
 
@@ -260,6 +297,17 @@ static int Open ( vlc_object_t *p_this )
     }
     free( psz_type );
 
+    /* Detect Unicode while skipping the UTF-8 Byte Order Mark */
+    bool unicode = false;
+    const uint8_t *p_data;
+    if( stream_Peek( p_demux->s, &p_data, 3 ) >= 3
+     && !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
+    {
+        unicode = true;
+        stream_Seek( p_demux->s, 3 ); /* skip BOM */
+        msg_Dbg( p_demux, "detected Unicode Byte Order Mark" );
+    }
+
     /* Probe if unknown type */
     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
     {
@@ -286,10 +334,27 @@ static int Open ( vlc_object_t *p_this )
                 p_sys->i_type = SUB_TYPE_MICRODVD;
                 break;
             }
-            else if( sscanf( s,
-                             "%d:%d:%d,%d --> %d:%d:%d,%d",
+            else if( sscanf( s, "%d:%d:%d,%d --> %d:%d:%d,%d",
+                             &i_dummy,&i_dummy,&i_dummy,&i_dummy,
+                             &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 ||
+                     sscanf( s, "%d:%d:%d --> %d:%d:%d,%d",
+                             &i_dummy,&i_dummy,&i_dummy,&i_dummy,
+                             &i_dummy,&i_dummy,&i_dummy ) == 7 ||
+                     sscanf( s, "%d:%d:%d,%d --> %d:%d:%d",
+                             &i_dummy,&i_dummy,&i_dummy,&i_dummy,
+                             &i_dummy,&i_dummy,&i_dummy ) == 7 ||
+                     sscanf( s, "%d:%d:%d.%d --> %d:%d:%d.%d",
                              &i_dummy,&i_dummy,&i_dummy,&i_dummy,
-                             &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
+                             &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 ||
+                     sscanf( s, "%d:%d:%d --> %d:%d:%d.%d",
+                             &i_dummy,&i_dummy,&i_dummy,&i_dummy,
+                             &i_dummy,&i_dummy,&i_dummy ) == 7 ||
+                     sscanf( s, "%d:%d:%d.%d --> %d:%d:%d",
+                             &i_dummy,&i_dummy,&i_dummy,&i_dummy,
+                             &i_dummy,&i_dummy,&i_dummy ) == 7 ||
+                     sscanf( s, "%d:%d:%d --> %d:%d:%d",
+                             &i_dummy,&i_dummy,&i_dummy,
+                             &i_dummy,&i_dummy,&i_dummy ) == 6 )
             {
                 p_sys->i_type = SUB_TYPE_SUBRIP;
                 break;
@@ -324,10 +389,13 @@ static int Open ( vlc_object_t *p_this )
                 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
                 break;
             }
-            else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d", &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy ) == 7 ||
-                    sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
+            else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d",
+                                 &i_dummy, &i_dummy, &i_dummy, &i_dummy,
+                                 &i_dummy, &i_dummy, &i_dummy ) == 7 ||
+                     sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
             {
                 p_sys->i_type = SUB_TYPE_JACOSUB;
+                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 )
@@ -341,6 +409,17 @@ static int Open ( vlc_object_t *p_this )
                 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
                 break;
             }
+            else if( sscanf( s, "[%d:%d:%d]%c",
+                     &i_dummy, &i_dummy, &i_dummy, &p_dummy ) == 4 )
+            {
+                p_sys->i_type = SUB_TYPE_DKS;
+                break;
+            }
+            else if( strstr( s, "*** START SCRIPT" ) )
+            {
+                p_sys->i_type = SUB_TYPE_SUBVIEW1;
+                break;
+            }
             else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
                      sscanf( s, "[%d][]", &i_dummy ) == 1)
             {
@@ -352,14 +431,33 @@ static int Open ( vlc_object_t *p_this )
                        && p_dummy =='E' ) )
             {
                 p_sys->i_type = SUB_TYPE_MPSUB;
+                break;
             }
             else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
             {
                 p_sys->i_type = SUB_TYPE_AQT;
+                break;
             }
             else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
             {
                 p_sys->i_type = SUB_TYPE_PJS;
+                break;
+            }
+            else if( sscanf( s, "{%d:%d:%d}",
+                                &i_dummy, &i_dummy, &i_dummy ) == 3 )
+            {
+                p_sys->i_type = SUB_TYPE_PSB;
+                break;
+            }
+            else if( strcasestr( s, "<time" ) )
+            {
+                p_sys->i_type = SUB_TYPE_RT;
+                break;
+            }
+            else if( !strncasecmp( s, "WEBVTT",6 ) )
+            {
+                p_sys->i_type = SUB_TYPE_VTT;
+                break;
             }
 
             free( s );
@@ -370,14 +468,15 @@ static int Open ( vlc_object_t *p_this )
 
         /* It will nearly always work even for non seekable stream thanks the
          * caching system, and if it fails we lose just a few sub */
-        if( stream_Seek( p_demux->s, 0 ) )
-        {
+        if( stream_Seek( p_demux->s, unicode ? 3 : 0 ) )
             msg_Warn( p_demux, "failed to rewind" );
-        }
     }
+
+    /* Quit on unknown subtitles */
     if( p_sys->i_type == SUB_TYPE_UNKNOWN )
     {
-        msg_Err( p_demux, "failed to recognize subtitle type" );
+        stream_Seek( p_demux->s, 0 );
+        msg_Warn( p_demux, "failed to recognize subtitle type" );
         free( p_sys );
         return VLC_EGENERIC;
     }
@@ -404,11 +503,9 @@ static int Open ( vlc_object_t *p_this )
         if( p_sys->i_subtitles >= i_max )
         {
             i_max += 500;
-            if( !( p_sys->subtitle = realloc( p_sys->subtitle,
+            if( !( p_sys->subtitle = realloc_or_free( p_sys->subtitle,
                                               sizeof(subtitle_t) * i_max ) ) )
             {
-                msg_Err( p_demux, "out of memory");
-                free( p_sys->subtitle );
                 TextUnload( &p_sys->txt );
                 free( p_sys );
                 return VLC_ENOMEM;
@@ -442,18 +539,36 @@ static int Open ( vlc_object_t *p_this )
              p_sys->i_type == SUB_TYPE_SSA2_4 ||
              p_sys->i_type == SUB_TYPE_ASS )
     {
-        es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
+        Fix( p_demux );
+        es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
     }
     else
+        es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
+
+    /* Stupid language detection in the filename */
+    char * psz_language = get_language_from_filename( p_demux->psz_file );
+
+    if( psz_language )
     {
-        es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
+        fmt.psz_language = psz_language;
+        msg_Dbg( p_demux, "detected language %s of subtitle: %s", psz_language,
+                 p_demux->psz_location );
     }
+
+    if( unicode )
+        fmt.subs.psz_encoding = strdup( "UTF-8" );
+    char *psz_description = var_InheritString( p_demux, "sub-description" );
+    if( psz_description && *psz_description )
+        fmt.psz_description = psz_description;
+    else
+        free( psz_description );
     if( p_sys->psz_header != NULL )
     {
         fmt.i_extra = strlen( p_sys->psz_header ) + 1;
         fmt.p_extra = strdup( p_sys->psz_header );
     }
     p_sys->es = es_out_Add( p_demux->out, &fmt );
+    es_format_Clean( &fmt );
 
     return VLC_SUCCESS;
 }
@@ -470,6 +585,7 @@ static void Close( vlc_object_t *p_this )
     for( i = 0; i < p_sys->i_subtitles; i++ )
         free( p_sys->subtitle[i].psz_text );
     free( p_sys->subtitle );
+    free( p_sys->psz_header );
 
     free( p_sys );
 }
@@ -502,9 +618,15 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         case DEMUX_SET_TIME:
             i64 = (int64_t)va_arg( args, int64_t );
             p_sys->i_subtitle = 0;
-            while( p_sys->i_subtitle < p_sys->i_subtitles &&
-                   p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
+            while( p_sys->i_subtitle < p_sys->i_subtitles )
             {
+                const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
+
+                if( p_subtitle->i_start > i64 )
+                    break;
+                if( p_subtitle->i_stop > p_subtitle->i_start && p_subtitle->i_stop > i64 )
+                    break;
+
                 p_sys->i_subtitle++;
             }
 
@@ -547,11 +669,13 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
             return VLC_SUCCESS;
 
+        case DEMUX_GET_PTS_DELAY:
         case DEMUX_GET_FPS:
         case DEMUX_GET_META:
         case DEMUX_GET_ATTACHMENTS:
         case DEMUX_GET_TITLE_INFO:
         case DEMUX_HAS_UNSUPPORTED_META:
+        case DEMUX_CAN_RECORD:
             return VLC_EGENERIC;
 
         default:
@@ -581,46 +705,32 @@ static int Demux( demux_t *p_demux )
     while( p_sys->i_subtitle < p_sys->i_subtitles &&
            p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
     {
+        const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
+
         block_t *p_block;
-        int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
+        int i_len = strlen( p_subtitle->psz_text ) + 1;
 
-        if( i_len <= 1 )
+        if( i_len <= 1 || p_subtitle->i_start < 0 )
         {
-            /* empty subtitle */
             p_sys->i_subtitle++;
             continue;
         }
 
-        if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
+        if( ( p_block = block_Alloc( i_len ) ) == NULL )
         {
             p_sys->i_subtitle++;
             continue;
         }
 
-        if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
-        {
-            p_sys->i_subtitle++;
-            continue;
-        }
+        p_block->i_dts =
+        p_block->i_pts = VLC_TS_0 + p_subtitle->i_start;
+        if( p_subtitle->i_stop >= 0 && p_subtitle->i_stop >= p_subtitle->i_start )
+            p_block->i_length = p_subtitle->i_stop - p_subtitle->i_start;
 
-        p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
-        p_block->i_dts = p_block->i_pts;
-        if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
-        {
-            p_block->i_length =
-                p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
-        }
+        memcpy( p_block->p_buffer, p_subtitle->psz_text, i_len );
+
+        es_out_Send( p_demux->out, p_sys->es, p_block );
 
-        memcpy( p_block->p_buffer,
-                p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
-        if( p_block->i_pts > 0 )
-        {
-            es_out_Send( p_demux->out, p_sys->es, p_block );
-        }
-        else
-        {
-            block_Release( p_block );
-        }
         p_sys->i_subtitle++;
     }
 
@@ -633,24 +743,22 @@ static int Demux( demux_t *p_demux )
 /*****************************************************************************
  * Fix: fix time stamp and order of subtitle
  *****************************************************************************/
-#ifdef USE_THIS_UNUSED_PIECE_OF_CODE
 static void Fix( demux_t *p_demux )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
     bool b_done;
-    int     i_index;
 
     /* *** fix order (to be sure...) *** */
     /* We suppose that there are near in order and this durty bubble sort
-     * wont take too much time
+     * would not take too much time
      */
     do
     {
         b_done = true;
-        for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
+        for( int i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
         {
             if( p_sys->subtitle[i_index].i_start <
-                    p_sys->subtitle[i_index - 1].i_start )
+                p_sys->subtitle[i_index - 1].i_start )
             {
                 subtitle_t sub_xch;
                 memcpy( &sub_xch,
@@ -667,7 +775,6 @@ static void Fix( demux_t *p_demux )
         }
     } while( !b_done );
 }
-#endif
 
 static int TextLoad( text_t *txt, stream_t *s )
 {
@@ -678,6 +785,8 @@ static int TextLoad( text_t *txt, stream_t *s )
     txt->i_line_count   = 0;
     txt->i_line         = 0;
     txt->line           = calloc( i_line_max, sizeof( char * ) );
+    if( !txt->line )
+        return VLC_ENOMEM;
 
     /* load the complete file */
     for( ;; )
@@ -691,7 +800,9 @@ static int TextLoad( text_t *txt, stream_t *s )
         if( txt->i_line_count >= i_line_max )
         {
             i_line_max += 100;
-            txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
+            txt->line = realloc_or_free( txt->line, i_line_max * sizeof( char * ) );
+            if( !txt->line )
+                return VLC_ENOMEM;
         }
     }
 
@@ -740,6 +851,7 @@ static void TextPreviousLine( text_t *txt )
 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
                           int i_idx )
 {
+    VLC_UNUSED( i_idx );
     demux_sys_t *p_sys = p_demux->p_sys;
     text_t      *txt = &p_sys->txt;
     char *psz_text;
@@ -758,19 +870,18 @@ static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
             return VLC_ENOMEM;
 
         i_start = 0;
-        i_stop  = 0;
+        i_stop  = -1;
         if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
             sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
         {
-            float f_fps;
             if( i_start != 1 || i_stop != 1 )
                 break;
 
             /* We found a possible setting of the framerate "{1}{1}23.976" */
             /* Check if it's usable, and if the sub-fps is not set */
-            f_fps = us_strtod( psz_text, NULL );
-            if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
-                p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
+            float f_fps = us_strtof( psz_text, NULL );
+            if( f_fps > 0.f && var_GetFloat( p_demux, "sub-fps" ) <= 0.f )
+                p_sys->i_microsecperframe = llroundf(1000000.f / f_fps);
         }
         free( psz_text );
     }
@@ -784,7 +895,7 @@ static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
 
     /* */
     p_subtitle->i_start  = i_start * p_sys->i_microsecperframe;
-    p_subtitle->i_stop   = i_stop  * p_sys->i_microsecperframe;
+    p_subtitle->i_stop   = i_stop >= 0 ? (i_stop  * p_sys->i_microsecperframe) : -1;
     p_subtitle->psz_text = psz_text;
     return VLC_SUCCESS;
 }
@@ -806,7 +917,7 @@ static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
  *  We ignore line number for SubRip
  */
 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
-                                 const char *psz_fmt,
+                                 int (* pf_parse_timing)(subtitle_t *, const char *),
                                  bool b_replace_br )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
@@ -816,24 +927,13 @@ static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
     for( ;; )
     {
         const char *s = TextGetLine( txt );
-        int h1, m1, s1, d1, h2, m2, s2, d2;
 
         if( !s )
             return VLC_EGENERIC;
 
-        if( sscanf( s, psz_fmt,
-                    &h1, &m1, &s1, &d1,
-                    &h2, &m2, &s2, &d2 ) == 8 )
+        if( pf_parse_timing( p_subtitle, s) == VLC_SUCCESS &&
+            p_subtitle->i_start < p_subtitle->i_stop )
         {
-            p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
-                                    (int64_t)m1 * 60*1000 +
-                                    (int64_t)s1 * 1000 +
-                                    (int64_t)d1 ) * 1000;
-
-            p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
-                                    (int64_t)m2 * 60*1000 +
-                                    (int64_t)s2 * 1000 +
-                                    (int64_t)d2 ) * 1000;
             break;
         }
     }
@@ -842,19 +942,14 @@ static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
     psz_text = strdup("");
     if( !psz_text )
         return VLC_ENOMEM;
+
     for( ;; )
     {
         const char *s = TextGetLine( txt );
         int i_len;
         int i_old;
 
-        if( !s )
-        {
-            free( psz_text );
-            return VLC_EGENERIC;
-        }
-
-        i_len = strlen( s );
+        i_len = s ? strlen( s ) : 0;
         if( i_len <= 0 )
         {
             p_subtitle->psz_text = psz_text;
@@ -862,9 +957,11 @@ static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
         }
 
         i_old = strlen( psz_text );
-        psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
+        psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
         if( !psz_text )
+        {
             return VLC_ENOMEM;
+        }
         strcat( psz_text, s );
         strcat( psz_text, "\n" );
 
@@ -872,7 +969,7 @@ static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
         if( b_replace_br )
         {
             char *p;
+
             while( ( p = strstr( psz_text, "[br]" ) ) )
             {
                 *p++ = '\n';
@@ -881,22 +978,101 @@ static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
         }
     }
 }
+
+/* subtitle_ParseSubRipTimingValue
+ * Parses SubRip timing value.
+ */
+static int subtitle_ParseSubRipTimingValue(int64_t *timing_value,
+                                           const char *s)
+{
+    int h1, m1, s1, d1 = 0;
+
+    if ( sscanf( s, "%d:%d:%d,%d",
+                 &h1, &m1, &s1, &d1 ) == 4 ||
+         sscanf( s, "%d:%d:%d.%d",
+                 &h1, &m1, &s1, &d1 ) == 4 ||
+         sscanf( s, "%d:%d:%d",
+                 &h1, &m1, &s1) == 3 )
+    {
+        (*timing_value) = ( (int64_t)h1 * 3600 * 1000 +
+                            (int64_t)m1 * 60 * 1000 +
+                            (int64_t)s1 * 1000 +
+                            (int64_t)d1 ) * 1000;
+
+        return VLC_SUCCESS;
+    }
+
+    return VLC_EGENERIC;
+}
+
+/* subtitle_ParseSubRipTiming
+ * Parses SubRip timing.
+ */
+static int subtitle_ParseSubRipTiming( subtitle_t *p_subtitle,
+                                       const char *s )
+{
+    int i_result = VLC_EGENERIC;
+    char *psz_start, *psz_stop;
+    psz_start = malloc( strlen(s) + 1 );
+    psz_stop = malloc( strlen(s) + 1 );
+
+    if( sscanf( s, "%s --> %s", psz_start, psz_stop) == 2 &&
+        subtitle_ParseSubRipTimingValue( &p_subtitle->i_start, psz_start ) == VLC_SUCCESS &&
+        subtitle_ParseSubRipTimingValue( &p_subtitle->i_stop,  psz_stop ) == VLC_SUCCESS )
+    {
+        i_result = VLC_SUCCESS;
+    }
+
+    free(psz_start);
+    free(psz_stop);
+
+    return i_result;
+}
 /* ParseSubRip
  */
 static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
                          int i_idx )
 {
+    VLC_UNUSED( i_idx );
     return ParseSubRipSubViewer( p_demux, p_subtitle,
-                                 "%d:%d:%d,%d --> %d:%d:%d,%d",
+                                 &subtitle_ParseSubRipTiming,
                                  false );
 }
+
+/* subtitle_ParseSubViewerTiming
+ * Parses SubViewer timing.
+ */
+static int subtitle_ParseSubViewerTiming( subtitle_t *p_subtitle,
+                                   const char *s )
+{
+    int h1, m1, s1, d1, h2, m2, s2, d2;
+
+    if( sscanf( s, "%d:%d:%d.%d,%d:%d:%d.%d",
+                &h1, &m1, &s1, &d1, &h2, &m2, &s2, &d2) == 8 )
+    {
+        p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
+                                (int64_t)m1 * 60*1000 +
+                                (int64_t)s1 * 1000 +
+                                (int64_t)d1 ) * 1000;
+
+        p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
+                                (int64_t)m2 * 60*1000 +
+                                (int64_t)s2 * 1000 +
+                                (int64_t)d2 ) * 1000;
+        return VLC_SUCCESS;
+    }
+    return VLC_EGENERIC;
+}
+
 /* ParseSubViewer
  */
 static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
                             int i_idx )
 {
+    VLC_UNUSED( i_idx );
+
     return ParseSubRipSubViewer( p_demux, p_subtitle,
-                                 "%d:%d:%d.%d,%d:%d:%d.%d",
+                                 &subtitle_ParseSubViewerTiming,
                                  true );
 }
 
@@ -907,12 +1083,13 @@ static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
 {
     demux_sys_t *p_sys = p_demux->p_sys;
     text_t      *txt = &p_sys->txt;
+    size_t header_len = 0;
 
     for( ;; )
     {
         const char *s = TextGetLine( txt );
         int h1, m1, s1, c1, h2, m2, s2, c2;
-        char *psz_text;
+        char *psz_text, *psz_temp;
         char temp[16];
 
         if( !s )
@@ -955,9 +1132,14 @@ static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
                 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
 
                 /* ReadOrder, Layer, %s(rest of fields) */
-                snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
-                memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
-                memcpy( psz_text, temp, strlen(temp) );
+                if( asprintf( &psz_temp, "%d,%d,%s", i_idx, i_layer, psz_text ) == -1 )
+                {
+                    free( psz_text );
+                    return VLC_ENOMEM;
+                }
+
+                free( psz_text );
+                psz_text = psz_temp;
             }
 
             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
@@ -974,16 +1156,12 @@ static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
         free( psz_text );
 
         /* All the other stuff we add to the header field */
-        if( !p_sys->psz_header )
-            p_sys->psz_header = strdup( "" );
+        size_t s_len = strlen( s );
+        p_sys->psz_header = realloc_or_free( p_sys->psz_header, header_len + s_len + 2 );
         if( !p_sys->psz_header )
             return VLC_ENOMEM;
-
-        p_sys->psz_header =
-            realloc( p_sys->psz_header,
-                     strlen( p_sys->psz_header ) + strlen( s ) + 2 );
-        strcat( p_sys->psz_header,  s );
-        strcat( p_sys->psz_header, "\n" );
+        snprintf( p_sys->psz_header + header_len, s_len + 2, "%s\n", s );
+        header_len += s_len + 1;
     }
 }
 
@@ -993,9 +1171,11 @@ static int  ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
  *  or
  *      h:m:s Line1|Line2|Line3....
  */
-static int  ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
+static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
                           int i_idx )
 {
+    VLC_UNUSED( i_idx );
+
     demux_sys_t *p_sys = p_demux->p_sys;
     text_t      *txt = &p_sys->txt;
     char *psz_text;
@@ -1019,7 +1199,7 @@ static int  ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
             p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
                                     (int64_t)m1 * 60*1000 +
                                     (int64_t)s1 * 1000 ) * 1000;
-            p_subtitle->i_stop  = 0;
+            p_subtitle->i_stop  = -1;
             break;
         }
         free( psz_text );
@@ -1061,6 +1241,7 @@ static char *ParseSamiSearch( text_t *txt,
 }
 static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 {
+    VLC_UNUSED( i_idx );
     demux_sys_t *p_sys = p_demux->p_sys;
     text_t      *txt = &p_sys->txt;
 
@@ -1133,7 +1314,7 @@ static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
     }
 
     p_subtitle->i_start = i_start * 1000;
-    p_subtitle->i_stop  = 0;
+    p_subtitle->i_stop  = -1;
     p_subtitle->psz_text = strdup( text );
 
     return VLC_SUCCESS;
@@ -1159,6 +1340,8 @@ static int  ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
                              int i_idx )
 {
+    VLC_UNUSED( i_idx );
+
     demux_sys_t *p_sys = p_demux->p_sys;
     text_t      *txt = &p_sys->txt;
     char *psz_text;
@@ -1179,7 +1362,7 @@ static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
                                     (int64_t)m1 * 60*1000 +
                                     (int64_t)s1 * 1000 +
                                     (int64_t)c1 * 10) * 1000;
-            p_subtitle->i_stop = 0;
+            p_subtitle->i_stop = -1;
             break;
         }
     }
@@ -1208,7 +1391,7 @@ static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
         }
 
         i_old = strlen( psz_text );
-        psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
+        psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
         if( !psz_text )
             return VLC_ENOMEM;
         strcat( psz_text, s );
@@ -1223,6 +1406,8 @@ static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
  */
 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 {
+    VLC_UNUSED( i_idx );
+
     demux_sys_t *p_sys = p_demux->p_sys;
     text_t      *txt = &p_sys->txt;
     char *psz_text;
@@ -1242,12 +1427,12 @@ static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
             return VLC_ENOMEM;
 
         i_start = 0;
-        i_stop  = 0;
+        i_stop  = -1;
         if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
             sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
         {
             p_subtitle->i_start = (int64_t)i_start * 100000;
-            p_subtitle->i_stop  = (int64_t)i_stop  * 100000;
+            p_subtitle->i_stop  = i_stop >= 0 ? ((int64_t)i_stop  * 100000) : -1;
             break;
         }
         free( psz_text );
@@ -1271,6 +1456,8 @@ static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 
 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 {
+    VLC_UNUSED( i_idx );
+
     demux_sys_t *p_sys = p_demux->p_sys;
     text_t      *txt = &p_sys->txt;
     char *psz_text = strdup( "" );
@@ -1284,13 +1471,16 @@ static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
         const char *s = TextGetLine( txt );
 
         if( !s )
+        {
+            free( psz_text );
             return VLC_EGENERIC;
+        }
 
         /* Data Lines */
         if( sscanf (s, "-->> %d", &t) == 1)
         {
             p_subtitle->i_start = (int64_t)t; /* * FPS*/
-            p_subtitle->i_stop  = 0;
+            p_subtitle->i_stop  = -1;
 
             /* Starting of a subtitle */
             if( i_firstline )
@@ -1300,7 +1490,7 @@ static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
             /* We have been too far: end of the subtitle, begin of next */
             else
             {
-                txt->i_line--;
+                TextPreviousLine( txt );
                 break;
             }
         }
@@ -1308,7 +1498,7 @@ static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
         else
         {
             i_old = strlen( psz_text ) + 1;
-            psz_text = realloc( psz_text, i_old + strlen( s ) + 1 );
+            psz_text = realloc_or_free( psz_text, i_old + strlen( s ) + 1 );
             if( !psz_text )
                  return VLC_ENOMEM;
             strcat( psz_text, s );
@@ -1323,9 +1513,12 @@ static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 
 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 {
+    VLC_UNUSED( i_idx );
+
     demux_sys_t *p_sys = p_demux->p_sys;
     text_t      *txt = &p_sys->txt;
     char *psz_text;
+    int i;
 
     for( ;; )
     {
@@ -1336,6 +1529,8 @@ static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
             return VLC_EGENERIC;
 
         psz_text = malloc( strlen(s) + 1 );
+        if( !psz_text )
+            return VLC_ENOMEM;
 
         /* Data Lines */
         if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
@@ -1350,58 +1545,85 @@ static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
         }
         free( psz_text );
     }
+
+    /* replace | by \n */
+    for( i = 0; psz_text[i] != '\0'; i++ )
+    {
+        if( psz_text[i] == '|' )
+            psz_text[i] = '\n';
+    }
+
     p_subtitle->psz_text = psz_text;
     msg_Dbg( p_demux, "%s", psz_text );
     return VLC_SUCCESS;
 }
 
-static float mpsub_total = 0.0;
-static float mpsub_factor = 0.0;
-
 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 {
+    VLC_UNUSED( i_idx );
+
     demux_sys_t *p_sys = p_demux->p_sys;
     text_t      *txt = &p_sys->txt;
     char *psz_text = strdup( "" );
 
+    if( !p_sys->mpsub.b_inited )
+    {
+        p_sys->mpsub.f_total = 0.0;
+        p_sys->mpsub.f_factor = 0.0;
+
+        p_sys->mpsub.b_inited = true;
+    }
+
     for( ;; )
     {
-        const char *s = TextGetLine( txt );
-        float f1, f2;
         char p_dummy;
         char *psz_temp;
 
+        const char *s = TextGetLine( txt );
         if( !s )
+        {
+            free( psz_text );
             return VLC_EGENERIC;
+        }
+
         if( strstr( s, "FORMAT" ) )
         {
             if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
             {
-                mpsub_factor = 100.0;
+                p_sys->mpsub.f_factor = 100.0;
                 break;
             }
 
             psz_temp = malloc( strlen(s) );
+            if( !psz_temp )
+            {
+                free( psz_text );
+                return VLC_ENOMEM;
+            }
+
             if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
             {
-                float f_fps;
-                f_fps = us_strtod( psz_temp, NULL );
-                if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
+                float f_fps = us_strtof( psz_temp, NULL );
+
+                if( f_fps > 0.f && var_GetFloat( p_demux, "sub-fps" ) <= 0.f )
                     var_SetFloat( p_demux, "sub-fps", f_fps );
 
-                mpsub_factor = 1.0;
+                p_sys->mpsub.f_factor = 1.f;
                 free( psz_temp );
                 break;
             }
             free( psz_temp );
         }
+
         /* Data Lines */
-        if( sscanf (s, "%f %f", &f1, &f2 ) == 2 )
+        float f1 = us_strtof( s, &psz_temp );
+        if( *psz_temp )
         {
-            mpsub_total += f1 * mpsub_factor;
-            p_subtitle->i_start = (int64_t)(10000.0 * mpsub_total);
-            mpsub_total += f2 * mpsub_factor;
-            p_subtitle->i_stop = (int64_t)(10000.0 * mpsub_total);
+            float f2 = us_strtof( psz_temp, NULL );
+            p_sys->mpsub.f_total += f1 * p_sys->mpsub.f_factor;
+            p_subtitle->i_start = llroundf(10000.f * p_sys->mpsub.f_total);
+            p_sys->mpsub.f_total += f2 * p_sys->mpsub.f_factor;
+            p_subtitle->i_stop = llroundf(10000.f * p_sys->mpsub.f_total);
             break;
         }
     }
@@ -1411,7 +1633,10 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
         const char *s = TextGetLine( txt );
 
         if( !s )
+        {
+            free( psz_text );
             return VLC_EGENERIC;
+        }
 
         int i_len = strlen( s );
         if( i_len == 0 )
@@ -1419,7 +1644,7 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 
         int i_old = strlen( psz_text );
 
-        psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
+        psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
         if( !psz_text )
              return VLC_ENOMEM;
 
@@ -1433,15 +1658,22 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 
 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 {
+    VLC_UNUSED( i_idx );
+
     demux_sys_t  *p_sys = p_demux->p_sys;
     text_t       *txt = &p_sys->txt;
     char         *psz_text, *psz_orig;
     char         *psz_text2, *psz_orig2;
     int h1, h2, m1, m2, s1, s2, f1, f2;
-    static int i_comment = 0;
 
-    static int jss_time_resolution = 30;
-    static int jss_time_shift = 0;
+    if( !p_sys->jss.b_inited )
+    {
+        p_sys->jss.i_comment = 0;
+        p_sys->jss.i_time_resolution = 30;
+        p_sys->jss.i_time_shift = 0;
+
+        p_sys->jss.b_inited = true;
+    }
 
     /* Parse the main lines */
     for( ;; )
@@ -1450,26 +1682,34 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
         if( !s )
             return VLC_EGENERIC;
 
-        psz_text = malloc( strlen( s ) + 1 );
-        psz_orig = psz_text;
+        psz_orig = malloc( strlen( s ) + 1 );
+        if( !psz_orig )
+            return VLC_ENOMEM;
+        psz_text = psz_orig;
 
+        /* Complete time lines */
         if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
                     &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
         {
             p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
-                (int64_t)( ( f1 +  jss_time_shift ) /  jss_time_resolution ) )
+                (int64_t)( ( f1 +  p_sys->jss.i_time_shift ) /  p_sys->jss.i_time_resolution ) )
                 * 1000000;
             p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
-                (int64_t)( ( f2 +  jss_time_shift ) /  jss_time_resolution ) )
+                (int64_t)( ( f2 +  p_sys->jss.i_time_shift ) /  p_sys->jss.i_time_resolution ) )
                 * 1000000;
+            break;
         }
+        /* Short time lines */
         else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
         {
             p_subtitle->i_start = (int64_t)(
-                    ( f1 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
+                    ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
             p_subtitle->i_stop = (int64_t)(
-                    ( f2 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
+                    ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
+            break;
         }
+        /* General Directive lines */
+        /* Only TIME and SHIFT are supported so far */
         else if( s[0] == '#' )
         {
             int h = 0, m =0, sec = 1, f = 1;
@@ -1478,162 +1718,540 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 
             strcpy( psz_text, s );
 
-            switch( toupper( psz_text[1] ) )
+            switch( toupper( (unsigned char)psz_text[1] ) )
             {
-                case 'S':
-                     shift = isalpha( psz_text[2] ) ? 6 : 2 ;
+            case 'S':
+                 shift = isalpha( (unsigned char)psz_text[2] ) ? 6 : 2 ;
 
-                     if( sscanf( &psz_text[shift], "%d", &h ) )
+                 if( sscanf( &psz_text[shift], "%d", &h ) )
+                 {
+                     /* Negative shifting */
+                     if( h < 0 )
                      {
-                         /* Negative shifting */
-                         if( h < 0 )
-                         {
-                             h *= -1;
-                             inv = -1;
-                         }
+                         h *= -1;
+                         inv = -1;
+                     }
 
-                         if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
+                     if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
+                     {
+                         if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
                          {
-                             if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
-                             {
-                                 sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
-                             }
-                             else
-                             {
-                                 h = 0;
-                                 sscanf( &psz_text[shift], "%d:%d.%d", &m, &sec, &f );
-                                 m *= inv;
-                             }
+                             sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
                          }
                          else
                          {
-                             h = m = 0;
-                             sscanf( &psz_text[shift], "%d.%d", &sec, &f);
-                             sec *= inv;
+                             h = 0;
+                             sscanf( &psz_text[shift], "%d:%d.%d",
+                                     &m, &sec, &f );
+                             m *= inv;
                          }
-                         jss_time_shift = ( ( h * 3600 + m * 60 + sec )
-                             * jss_time_resolution + f ) * inv;
                      }
-                     break;
+                     else
+                     {
+                         h = m = 0;
+                         sscanf( &psz_text[shift], "%d.%d", &sec, &f);
+                         sec *= inv;
+                     }
+                     p_sys->jss.i_time_shift = ( ( h * 3600 + m * 60 + sec )
+                         * p_sys->jss.i_time_resolution + f ) * inv;
+                 }
+                 break;
 
-                case 'T':
-                    shift = isalpha( psz_text[2] ) ? 8 : 2 ;
+            case 'T':
+                shift = isalpha( (unsigned char)psz_text[2] ) ? 8 : 2 ;
 
-                    sscanf( &psz_text[shift], "%d", &jss_time_resolution );
-                    break;
+                sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution );
+                break;
             }
-            free( psz_text );
+            free( psz_orig );
             continue;
         }
         else
-            /* Unkown line */
+            /* Unkown type line, probably a comment */
         {
-            free( psz_text );
+            free( psz_orig );
             continue;
         }
+    }
 
-        /* Skip the blanks */
-        while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
+    while( psz_text[ strlen( psz_text ) - 1 ] == '\\' )
+    {
+        const char *s2 = TextGetLine( txt );
 
-        /* Parse the directives */
-        if( isalpha( *psz_text ) || *psz_text == '[' )
+        if( !s2 )
         {
-            while( *psz_text != ' ' )
-            { psz_text++ ;};
-
-            /* Directives are NOT parsed yet */
-            /* directive = malloc( strlen( psz_text ) + 1 );
-            if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
+            free( psz_orig );
+            return VLC_EGENERIC;
         }
 
-        /* Skip the blanks after directives */
-        while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
+        int i_len = strlen( s2 );
+        if( i_len == 0 )
+            break;
+
+        int i_old = strlen( psz_text );
+
+        psz_text = realloc_or_free( psz_text, i_old + i_len + 1 );
+        if( !psz_text )
+             return VLC_ENOMEM;
+
+        psz_orig = psz_text;
+        strcat( psz_text, s2 );
+    }
+
+    /* Skip the blanks */
+    while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
+
+    /* Parse the directives */
+    if( isalpha( (unsigned char)*psz_text ) || *psz_text == '[' )
+    {
+        while( *psz_text != ' ' )
+        { psz_text++ ;};
+
+        /* Directives are NOT parsed yet */
+        /* This has probably a better place in a decoder ? */
+        /* directive = malloc( strlen( psz_text ) + 1 );
+           if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
+    }
+
+    /* Skip the blanks after directives */
+    while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
 
-        psz_text2 = calloc( strlen( psz_text) + 1, 1 );
-        psz_orig2 = psz_text2;
+    /* Clean all the lines from inline comments and other stuffs */
+    psz_orig2 = calloc( strlen( psz_text) + 1, 1 );
+    psz_text2 = psz_orig2;
 
-        for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
+    for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
+    {
+        switch( *psz_text )
         {
-            switch( *psz_text )
+        case '{':
+            p_sys->jss.i_comment++;
+            break;
+        case '}':
+            if( p_sys->jss.i_comment )
+            {
+                p_sys->jss.i_comment = 0;
+                if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
+            }
+            break;
+        case '~':
+            if( !p_sys->jss.i_comment )
             {
-            case '{':
-                i_comment++;
+                *psz_text2 = ' ';
+                psz_text2++;
+            }
+            break;
+        case ' ':
+        case '\t':
+            if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
                 break;
-            case '}':
-                if( i_comment )
-                {
-                    i_comment = 0;
-                    if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
-                }
+            if( !p_sys->jss.i_comment )
+            {
+                *psz_text2 = ' ';
+                psz_text2++;
+            }
+            break;
+        case '\\':
+            if( (*(psz_text + 1 ) ) == 'n' )
+            {
+                *psz_text2 = '\n';
+                psz_text++;
+                psz_text2++;
                 break;
-            case '~':
-                if( !i_comment )
-                {
-                    *psz_text2 = ' ';
-                    psz_text2++;
-                }
+            }
+            if( ( toupper((unsigned char)*(psz_text + 1 ) ) == 'C' ) ||
+                    ( toupper((unsigned char)*(psz_text + 1 ) ) == 'F' ) )
+            {
+                psz_text++; psz_text++;
                 break;
-            case ' ':
-            case '\t':
-                if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
-                    break;
-                if( !i_comment )
-                {
-                    *psz_text2 = ' ';
-                    psz_text2++;
-                }
+            }
+            if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
+                (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
+                (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
+                (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
+            {
+                psz_text++;
                 break;
-            case '\\':
-                if( (*(psz_text + 1 ) ) == 'n' )
-                {
-                    *psz_text2 = '\n';
-                    psz_text++;
-                    psz_text2++;
-                    break;
-                }
-                if( ( toupper(*(psz_text + 1 ) ) == 'C' ) ||
-                    ( toupper(*(psz_text + 1 ) ) == 'F' ) )
-                {
-                    psz_text++; psz_text++;
-                    break;
-                }
-                if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
-                    (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
-                    (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
-                    (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
-                {
-                    psz_text++;
-                    break;
-                }
-                if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
-                    (*(psz_text + 1 ) ) == '\\' )
-                    psz_text++;
-                else if( *(psz_text + 1 ) == '\r' ||  *(psz_text + 1 ) == '\n'
-                         ||  *(psz_text + 1 ) == '\0' )
-                {
-                    char *s2 = TextGetLine( txt );
-                    if( !s2 )
-                        return VLC_EGENERIC;
+            }
+            if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
+                (*(psz_text + 1 ) ) == '\\' )
+                psz_text++;
+            else if( *(psz_text + 1 ) == '\r' ||  *(psz_text + 1 ) == '\n' ||
+                     *(psz_text + 1 ) == '\0' )
+            {
+                psz_text++;
+            }
+            break;
+        default:
+            if( !p_sys->jss.i_comment )
+            {
+                *psz_text2 = *psz_text;
+                psz_text2++;
+            }
+        }
+        psz_text++;
+    }
 
-                    while ( *s2 == ' ' ) s2++;
+    p_subtitle->psz_text = psz_orig2;
+    msg_Dbg( p_demux, "%s", p_subtitle->psz_text );
+    free( psz_orig );
+    return VLC_SUCCESS;
+}
 
-                    /* Here to parse the second line, we should add s2 to
-                       psz_text and go on the for( ) line 1556 in order to
-                       parse the next line.
-                    */
-                }
-            default:
-                if( !i_comment )
-                {
-                    *psz_text2 = *psz_text;
-                    psz_text2++;
-                }
+static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
+{
+    VLC_UNUSED( i_idx );
+
+    demux_sys_t *p_sys = p_demux->p_sys;
+    text_t      *txt = &p_sys->txt;
+    char *psz_text;
+    int i;
+
+    for( ;; )
+    {
+        int h1, m1, s1;
+        int h2, m2, s2;
+        const char *s = TextGetLine( txt );
+
+        if( !s )
+            return VLC_EGENERIC;
+
+        psz_text = malloc( strlen( s ) + 1 );
+        if( !psz_text )
+            return VLC_ENOMEM;
+
+        if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
+                    &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
+        {
+            p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
+                                    (int64_t)m1 * 60*1000 +
+                                    (int64_t)s1 * 1000 ) * 1000;
+            p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
+                                    (int64_t)m2 * 60*1000 +
+                                    (int64_t)s2 * 1000 ) * 1000;
+            break;
+        }
+        free( psz_text );
+    }
+
+    /* replace | by \n */
+    for( i = 0; psz_text[i] != '\0'; i++ )
+    {
+        if( psz_text[i] == '|' )
+            psz_text[i] = '\n';
+    }
+    p_subtitle->psz_text = psz_text;
+    return VLC_SUCCESS;
+}
+
+static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f )
+{
+    if( *psz == '\0' ) return 0;
+    if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 ||
+            sscanf( psz, "%d:%d.%d", m, s, f ) == 3 ||
+            sscanf( psz, "%d.%d", s, f ) == 2 ||
+            sscanf( psz, "%d:%d", m, s ) == 2 ||
+            sscanf( psz, "%d", s ) == 1 )
+    {
+        return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000
+               + (int64_t)*f * 10 * 1000;
+    }
+    else return VLC_EGENERIC;
+}
+
+static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
+{
+    VLC_UNUSED( i_idx );
+    demux_sys_t *p_sys = p_demux->p_sys;
+    text_t      *txt = &p_sys->txt;
+    char *psz_text = NULL;
+
+    for( ;; )
+    {
+        int h1 = 0, m1 = 0, s1 = 0, f1 = 0;
+        int h2 = 0, m2 = 0, s2 = 0, f2 = 0;
+        const char *s = TextGetLine( txt );
+        free( psz_text );
+
+        if( !s )
+            return VLC_EGENERIC;
+
+        psz_text = malloc( strlen( s ) + 1 );
+        if( !psz_text )
+            return VLC_ENOMEM;
+
+        /* Find the good begining. This removes extra spaces at the beginning
+           of the line.*/
+        char *psz_temp = strcasestr( s, "<time");
+        if( psz_temp != NULL )
+        {
+            char psz_end[12], psz_begin[12];
+            /* Line has begin and end */
+            if( ( sscanf( psz_temp,
+                  "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\" %*[e|E]nd=\"%11[^\"]%*[^>]%[^\n\r]",
+                            psz_begin, psz_end, psz_text) != 3 ) &&
+                    /* Line has begin and no end */
+                    ( sscanf( psz_temp,
+                              "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\"%*[^>]%[^\n\r]",
+                              psz_begin, psz_text ) != 2) )
+                /* Line is not recognized */
+            {
+                continue;
             }
-            psz_text++;
+
+            /* Get the times */
+            int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 );
+            p_subtitle->i_start = i_time >= 0 ? i_time : 0;
+
+            i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 );
+            p_subtitle->i_stop = i_time >= 0 ? i_time : -1;
+            break;
+        }
+    }
+
+    /* Get the following Lines */
+    for( ;; )
+    {
+        const char *s = TextGetLine( txt );
+
+        if( !s )
+        {
+            free( psz_text );
+            return VLC_EGENERIC;
         }
 
-        p_subtitle->psz_text = psz_orig2;
-        free( psz_orig );
-        return VLC_SUCCESS;
+        int i_len = strlen( s );
+        if( i_len == 0 ) break;
+
+        if( strcasestr( s, "<time" ) ||
+            strcasestr( s, "<clear/") )
+        {
+            TextPreviousLine( txt );
+            break;
+        }
+
+        int i_old = strlen( psz_text );
+
+        psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
+        if( !psz_text )
+            return VLC_ENOMEM;
+
+        strcat( psz_text, s );
+        strcat( psz_text, "\n" );
+    }
+
+    /* Remove the starting ">" that remained after the sscanf */
+    memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
+
+    p_subtitle->psz_text = psz_text;
+
+    return VLC_SUCCESS;
+}
+
+static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
+{
+    VLC_UNUSED( i_idx );
+
+    demux_sys_t *p_sys = p_demux->p_sys;
+    text_t      *txt = &p_sys->txt;
+    char *psz_text;
+
+    for( ;; )
+    {
+        int h1, m1, s1;
+        int h2, m2, s2;
+        char *s = TextGetLine( txt );
+
+        if( !s )
+            return VLC_EGENERIC;
+
+        psz_text = malloc( strlen( s ) + 1 );
+        if( !psz_text )
+            return VLC_ENOMEM;
+
+        if( sscanf( s, "[%d:%d:%d]%[^\r\n]",
+                    &h1, &m1, &s1, psz_text ) == 4 )
+        {
+            p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
+                                    (int64_t)m1 * 60*1000 +
+                                    (int64_t)s1 * 1000 ) * 1000;
+
+            char *s = TextGetLine( txt );
+            if( !s )
+            {
+                free( psz_text );
+                return VLC_EGENERIC;
+            }
+
+            if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
+                p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
+                                        (int64_t)m2 * 60*1000 +
+                                        (int64_t)s2 * 1000 ) * 1000;
+            else
+                p_subtitle->i_stop  = -1;
+            break;
+        }
+        free( psz_text );
+    }
+
+    /* replace [br] by \n */
+    char *p;
+    while( ( p = strstr( psz_text, "[br]" ) ) )
+    {
+        *p++ = '\n';
+        memmove( p, &p[3], strlen(&p[3])+1 );
+    }
+
+    p_subtitle->psz_text = psz_text;
+    return VLC_SUCCESS;
+}
+
+static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
+{
+    VLC_UNUSED( i_idx );
+
+    demux_sys_t *p_sys = p_demux->p_sys;
+    text_t      *txt = &p_sys->txt;
+    char *psz_text;
+
+    for( ;; )
+    {
+        int h1, m1, s1;
+        int h2, m2, s2;
+        char *s = TextGetLine( txt );
+
+        if( !s )
+            return VLC_EGENERIC;
+
+        if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 )
+        {
+            p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
+                                    (int64_t)m1 * 60*1000 +
+                                    (int64_t)s1 * 1000 ) * 1000;
+
+            char *s = TextGetLine( txt );
+            if( !s )
+                return VLC_EGENERIC;
+
+            psz_text = strdup( s );
+            if( !psz_text )
+                return VLC_ENOMEM;
+
+            s = TextGetLine( txt );
+            if( !s )
+            {
+                free( psz_text );
+                return VLC_EGENERIC;
+            }
+
+            if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
+                p_subtitle->i_stop  = ( (int64_t)h2 * 3600*1000 +
+                                        (int64_t)m2 * 60*1000 +
+                                        (int64_t)s2 * 1000 ) * 1000;
+            else
+                p_subtitle->i_stop  = -1;
+
+            break;
+        }
+    }
+
+    p_subtitle->psz_text = psz_text;
+
+    return VLC_SUCCESS;
+}
+/*Parsing WebVTT */
+static int  ParseVTT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
+{
+    VLC_UNUSED( i_idx );
+
+    demux_sys_t *p_sys = p_demux->p_sys;
+    text_t      *txt = &p_sys->txt;
+    char        *psz_text;
+
+    for( ;; )
+    {
+        const char *s = TextGetLine( txt );
+        int h1 = 0, m1 = 0, s1 = 0, d1 = 0;
+        int h2 = 0, m2 = 0, s2 = 0, d2 = 0;
+
+        if( !s )
+            return VLC_EGENERIC;
+
+        if( sscanf( s,"%d:%d:%d.%d --> %d:%d:%d.%d",
+                    &h1, &m1, &s1, &d1,
+                    &h2, &m2, &s2, &d2 ) == 8 ||
+            sscanf( s,"%d:%d:%d.%d --> %d:%d.%d",
+                    &h1, &m1, &s1, &d1,
+                         &m2, &s2, &d2 ) == 7 ||
+            sscanf( s,"%d:%d.%d --> %d:%d:%d.%d",
+                         &m1, &s1, &d1,
+                    &h2, &m2, &s2, &d2 ) == 7 ||
+            sscanf( s,"%d:%d.%d --> %d:%d.%d",
+                         &m1, &s1, &d1,
+                         &m2, &s2, &d2 ) == 6 )
+        {
+            p_subtitle->i_start = ( (int64_t)h1 * 3600 * 1000 +
+                                    (int64_t)m1 * 60 * 1000 +
+                                    (int64_t)s1 * 1000 +
+                                    (int64_t)d1 ) * 1000;
+
+            p_subtitle->i_stop  = ( (int64_t)h2 * 3600 * 1000 +
+                                    (int64_t)m2 * 60 * 1000 +
+                                    (int64_t)s2 * 1000 +
+                                    (int64_t)d2 ) * 1000;
+            if( p_subtitle->i_start < p_subtitle->i_stop )
+                break;
+        }
+    }
+
+    /* Now read text until an empty line */
+    psz_text = strdup("");
+    if( !psz_text )
+        return VLC_ENOMEM;
+
+    for( ;; )
+    {
+        const char *s = TextGetLine( txt );
+        int i_len;
+        int i_old;
+
+        i_len = s ? strlen( s ) : 0;
+        if( i_len <= 0 )
+        {
+            p_subtitle->psz_text = psz_text;
+            return VLC_SUCCESS;
+        }
+
+        i_old = strlen( psz_text );
+        psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
+        if( !psz_text )
+            return VLC_ENOMEM;
+
+        strcat( psz_text, s );
+        strcat( psz_text, "\n" );
     }
 }
+
+/* Matches filename.xx.srt */
+static char * get_language_from_filename( const char * psz_sub_file )
+{
+    char *psz_ret = NULL;
+    char *psz_tmp, *psz_language_begin;
+
+    if( !psz_sub_file ) return NULL;
+    char *psz_work = strdup( psz_sub_file );
+
+    /* Removing extension, but leaving the dot */
+    psz_tmp = strrchr( psz_work, '.' );
+    if( psz_tmp )
+    {
+        psz_tmp[0] = '\0';
+        psz_language_begin = strrchr( psz_work, '.' );
+        if( psz_language_begin )
+            psz_ret = strdup(++psz_language_begin);
+        psz_tmp[0] = '.';
+    }
+
+    free( psz_work );
+    return psz_ret;
+}