]> git.sesse.net Git - vlc/blobdiff - modules/demux/subtitle.c
android/surface: remove single_instance
[vlc] / modules / demux / subtitle.c
index f864ecb119ed1d94a46e1bccabc5e71ece156077..1b22c7db4756a629a91d8ed93f3f478e209d342a 100644 (file)
@@ -1,26 +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.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -37,6 +37,7 @@
 #include <vlc_memory.h>
 
 #include <ctype.h>
+#include <math.h>
 
 #include <vlc_demux.h>
 #include <vlc_charset.h>
@@ -53,11 +54,7 @@ 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\", \"subviewer\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\", " \
-    "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\", "\
-    "\"mpsub\", \"jacosub\", \"psb\", \"realtext\", \"dks\", \"subviewer1\", " \
-    " and \"auto\" (meaning autodetection, this should always work).")
+    N_("Force the subtiles format. Selecting \"auto\" means autodetection and should always work.")
 #define SUB_DESCRIPTION_LONGTEXT \
     N_("Override the default track description.")
 
@@ -66,25 +63,25 @@ static const char *const ppsz_sub_type[] =
     "auto", "microdvd", "subrip", "subviewer", "ssa1",
     "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
     "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
-    "subviewer1"
+    "subviewer1","vtt"
 };
 
 vlc_module_begin ()
     set_shortname( N_("Subtitles"))
-    set_description( N_("Text subtitles parser") )
+    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"),
+               N_("Frames per Second"),
                SUB_FPS_LONGTEXT, true )
-    add_integer( "sub-delay", 0, NULL,
-               N_("Subtitles delay"),
+    add_integer( "sub-delay", 0,
+               N_("Subtitle delay"),
                SUB_DELAY_LONGTEXT, true )
-    add_string( "sub-type", "auto", N_("Subtitles format"),
+    add_string( "sub-type", "auto", N_("Subtitle format"),
                 SUB_TYPE_LONGTEXT, true )
-        change_string_list( ppsz_sub_type, NULL, NULL )
-    add_string( "sub-description", NULL, N_("Subtitles description"),
+        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 )
 
@@ -99,7 +96,6 @@ enum
     SUB_TYPE_UNKNOWN = -1,
     SUB_TYPE_MICRODVD,
     SUB_TYPE_SUBRIP,
-    SUB_TYPE_SUBRIP_DOT, /* Invalid SubRip file (dot instead of comma) */
     SUB_TYPE_SSA1,
     SUB_TYPE_SSA2_4,
     SUB_TYPE_ASS,
@@ -115,8 +111,9 @@ enum
     SUB_TYPE_PSB,
     SUB_TYPE_RT,
     SUB_TYPE_DKS,
-    SUB_TYPE_SUBVIEW1 /* SUBVIEWER 1 - mplayer calls it subrip09,
+    SUB_TYPE_SUBVIEW1, /* SUBVIEWER 1 - mplayer calls it subrip09,
                          and Gnome subtitles SubViewer 1.0 */
+    SUB_TYPE_VTT
 };
 
 typedef struct
@@ -174,7 +171,6 @@ struct demux_sys_t
 
 static int  ParseMicroDvd   ( demux_t *, subtitle_t *, int );
 static int  ParseSubRip     ( demux_t *, subtitle_t *, int );
-static int  ParseSubRipDot  ( demux_t *, subtitle_t *, int );
 static int  ParseSubViewer  ( demux_t *, subtitle_t *, int );
 static int  ParseSSA        ( demux_t *, subtitle_t *, int );
 static int  ParseVplayer    ( demux_t *, subtitle_t *, int );
@@ -189,6 +185,7 @@ 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 const struct
 {
@@ -200,7 +197,6 @@ static const struct
 {
     { "microdvd",   SUB_TYPE_MICRODVD,    "MicroDVD",    ParseMicroDvd },
     { "subrip",     SUB_TYPE_SUBRIP,      "SubRIP",      ParseSubRip },
-    { "subrip-dot", SUB_TYPE_SUBRIP_DOT,  "SubRIP(Dot)", ParseSubRipDot },
     { "subviewer",  SUB_TYPE_SUBVIEWER,   "SubViewer",   ParseSubViewer },
     { "ssa1",       SUB_TYPE_SSA1,        "SSA-1",       ParseSSA },
     { "ssa2-4",     SUB_TYPE_SSA2_4,      "SSA-2/3/4",   ParseSSA },
@@ -217,6 +213,7 @@ static const struct
     { "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 }
 };
 /* When adding support for more formats, be sure to add their file extension
@@ -227,6 +224,7 @@ static int Demux( demux_t * );
 static int Control( demux_t *, int, va_list );
 
 static void Fix( demux_t * );
+static char * get_language_from_filename( const char * );
 
 /*****************************************************************************
  * Module initializer
@@ -264,17 +262,17 @@ static int Open ( vlc_object_t *p_this )
 
     /* Get the FPS */
     f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" ); /* FIXME */
-    if( f_fps >= 1.0 )
-        p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
+    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 */
@@ -299,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 )
     {
@@ -325,21 +334,29 @@ 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 )
-            {
-                p_sys->i_type = SUB_TYPE_SUBRIP;
-                break;
-            }
-            else if( sscanf( s,
-                             "%d:%d:%d.%d --> %d:%d:%d.%d",
+                             &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,&i_dummy ) == 8 )
+                             &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 ||
+                     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 )
             {
-                msg_Err( p_demux, "Detected invalid SubRip file, playing anyway" );
-                p_sys->i_type = SUB_TYPE_SUBRIP_DOT;
+                p_sys->i_type = SUB_TYPE_SUBRIP;
                 break;
             }
             else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
@@ -437,6 +454,11 @@ static int Open ( vlc_object_t *p_this )
                 p_sys->i_type = SUB_TYPE_RT;
                 break;
             }
+            else if( !strncasecmp( s, "WEBVTT",6 ) )
+            {
+                p_sys->i_type = SUB_TYPE_VTT;
+                break;
+            }
 
             free( s );
             s = NULL;
@@ -446,15 +468,14 @@ 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 )
     {
+        stream_Seek( p_demux->s, 0 );
         msg_Warn( p_demux, "failed to recognize subtitle type" );
         free( p_sys );
         return VLC_EGENERIC;
@@ -522,9 +543,20 @@ static int Open ( vlc_object_t *p_this )
         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 )
+    {
+        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;
@@ -553,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 );
 }
@@ -636,6 +669,7 @@ 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:
@@ -682,7 +716,7 @@ static int Demux( demux_t *p_demux )
             continue;
         }
 
-        if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
+        if( ( p_block = block_Alloc( i_len ) ) == NULL )
         {
             p_sys->i_subtitle++;
             continue;
@@ -716,7 +750,7 @@ static void Fix( demux_t *p_demux )
 
     /* *** 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
     {
@@ -840,15 +874,14 @@ static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
         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 );
     }
@@ -884,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;
@@ -894,26 +927,14 @@ 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;
-            if( p_subtitle->i_start < p_subtitle->i_stop )
-                break;
+            break;
         }
     }
 
@@ -957,6 +978,56 @@ 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,
@@ -964,20 +1035,35 @@ static int  ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
 {
     VLC_UNUSED( i_idx );
     return ParseSubRipSubViewer( p_demux, p_subtitle,
-                                 "%d:%d:%d,%d --> %d:%d:%d,%d",
+                                 &subtitle_ParseSubRipTiming,
                                  false );
 }
-/* ParseSubRipDot
- * Special version for buggy file using '.' instead of ','
+
+/* subtitle_ParseSubViewerTiming
+ * Parses SubViewer timing.
  */
-static int  ParseSubRipDot( demux_t *p_demux, subtitle_t *p_subtitle,
-                            int i_idx )
+static int subtitle_ParseSubViewerTiming( subtitle_t *p_subtitle,
+                                   const char *s )
 {
-    VLC_UNUSED( i_idx );
-    return ParseSubRipSubViewer( p_demux, p_subtitle,
-                                 "%d:%d:%d.%d --> %d:%d:%d.%d",
-                                 false );
+    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,
@@ -986,7 +1072,7 @@ static int  ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
     VLC_UNUSED( i_idx );
 
     return ParseSubRipSubViewer( p_demux, p_subtitle,
-                                 "%d:%d:%d.%d,%d:%d:%d.%d",
+                                 &subtitle_ParseSubViewerTiming,
                                  true );
 }
 
@@ -997,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 )
@@ -1045,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 +
@@ -1064,11 +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 */
-        char *psz_header;
-        if( asprintf( &psz_header, "%s%s\n",
-                       p_sys->psz_header ? p_sys->psz_header : "", s ) == -1 )
+        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 = psz_header;
+        snprintf( p_sys->psz_header + header_len, s_len + 2, "%s\n", s );
+        header_len += s_len + 1;
     }
 }
 
@@ -1483,7 +1576,6 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 
     for( ;; )
     {
-        float f1, f2;
         char p_dummy;
         char *psz_temp;
 
@@ -1511,26 +1603,27 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 
             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 );
 
-                p_sys->mpsub.f_factor = 1.0;
+                p_sys->mpsub.f_factor = 1.f;
                 free( psz_temp );
                 break;
             }
             free( psz_temp );
         }
+
         /* Data Lines */
-        f1 = us_strtod( s, &psz_temp );
+        float f1 = us_strtof( s, &psz_temp );
         if( *psz_temp )
         {
-            f2 = us_strtod( psz_temp, NULL );
+            float f2 = us_strtof( psz_temp, NULL );
             p_sys->mpsub.f_total += f1 * p_sys->mpsub.f_factor;
-            p_subtitle->i_start = (int64_t)(10000.0 * p_sys->mpsub.f_total);
+            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 = (int64_t)(10000.0 * p_sys->mpsub.f_total);
+            p_subtitle->i_stop = llroundf(10000.f * p_sys->mpsub.f_total);
             break;
         }
     }
@@ -1625,10 +1718,10 @@ 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 ;
+                 shift = isalpha( (unsigned char)psz_text[2] ) ? 6 : 2 ;
 
                  if( sscanf( &psz_text[shift], "%d", &h ) )
                  {
@@ -1665,7 +1758,7 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
                  break;
 
             case 'T':
-                shift = isalpha( psz_text[2] ) ? 8 : 2 ;
+                shift = isalpha( (unsigned char)psz_text[2] ) ? 8 : 2 ;
 
                 sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution );
                 break;
@@ -1680,7 +1773,7 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
             continue;
         }
     }
-       
+
     while( psz_text[ strlen( psz_text ) - 1 ] == '\\' )
     {
         const char *s2 = TextGetLine( txt );
@@ -1701,7 +1794,7 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
         if( !psz_text )
              return VLC_ENOMEM;
 
-               psz_orig = psz_text;
+        psz_orig = psz_text;
         strcat( psz_text, s2 );
     }
 
@@ -1709,7 +1802,7 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
     while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
 
     /* Parse the directives */
-    if( isalpha( *psz_text ) || *psz_text == '[' )
+    if( isalpha( (unsigned char)*psz_text ) || *psz_text == '[' )
     {
         while( *psz_text != ' ' )
         { psz_text++ ;};
@@ -1766,8 +1859,8 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
                 psz_text2++;
                 break;
             }
-            if( ( toupper(*(psz_text + 1 ) ) == 'C' ) ||
-                    ( toupper(*(psz_text + 1 ) ) == 'F' ) )
+            if( ( toupper((unsigned char)*(psz_text + 1 ) ) == 'C' ) ||
+                    ( toupper((unsigned char)*(psz_text + 1 ) ) == 'F' ) )
             {
                 psz_text++; psz_text++;
                 break;
@@ -1786,7 +1879,7 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
             else if( *(psz_text + 1 ) == '\r' ||  *(psz_text + 1 ) == '\n' ||
                      *(psz_text + 1 ) == '\0' )
             {
-                               psz_text++;
+                psz_text++;
             }
             break;
         default:
@@ -2066,4 +2159,99 @@ static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx
 
     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;
+}