]> git.sesse.net Git - vlc/blobdiff - modules/demux/subtitle.c
Remove change_unsafe() no-op.
[vlc] / modules / demux / subtitle.c
index fa113a2f021c3bb5b9e943815f6b9be39883e00b..16ca09c1f0e3ba3ecbc7f2bad658b28dea823a24 100644 (file)
@@ -31,7 +31,7 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_input.h>
 
@@ -57,21 +57,22 @@ static void Close( vlc_object_t *p_this );
     "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\" "\
-    "\"mpsub\" \"jacosub\" \"psb\" \"realtext\" \"dks\" and \"auto\" " \
-    "(meaning autodetection, this should always work).")
+    "\"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).")
 
-static const char *ppsz_sub_type[] =
+static const char *const ppsz_sub_type[] =
 {
     "auto", "microdvd", "subrip", "subviewer", "ssa1",
     "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
-    "aqt", "pjs", "mpsub", "jacosub", "psb", "rt", "dks"
+    "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
+    "subviewer1"
 };
 
 vlc_module_begin();
-    set_shortname( _("Subtitles"));
-    set_description( _("Text subtitles parser") );
+    set_shortname( N_("Subtitles"));
+    set_description( N_("Text subtitles parser") );
     set_capability( "demux", 0 );
     set_category( CAT_INPUT );
     set_subcategory( SUBCAT_INPUT_DEMUX );
@@ -102,8 +103,8 @@ 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,
@@ -111,8 +112,9 @@ enum
     SUB_TYPE_JACOSUB,
     SUB_TYPE_PSB,
     SUB_TYPE_RT,
-    SUB_TYPE_SUBVIEW1,
-    SUB_TYPE_DKS
+    SUB_TYPE_DKS,
+    SUB_TYPE_SUBVIEW1 /* SUBVIEWER 1 - mplayer calls it subrip09,
+                         and Gnome subtitles SubViewer 1.0 */
 };
 
 typedef struct
@@ -149,6 +151,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 );
@@ -166,9 +185,9 @@ 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  ParseSub1       ( demux_t *, subtitle_t *, int );
+static int  ParseSubViewer1 ( demux_t *, subtitle_t *, int );
 
-static struct
+static const struct
 {
     const char *psz_type_name;
     int  i_type;
@@ -193,9 +212,12 @@ static struct
     { "psb",        SUB_TYPE_PSB,         "PowerDivx",   ParsePSB },
     { "realtext",   SUB_TYPE_RT,          "RealText",    ParseRealText },
     { "dks",        SUB_TYPE_DKS,         "DKS",         ParseDKS },
-    { "subviewer1", SUB_TYPE_SUBVIEW1,    "Subviewer 1", ParseSub1 },
+    { "subviewer1", SUB_TYPE_SUBVIEW1,    "Subviewer 1", ParseSubViewer1 },
     { NULL,         SUB_TYPE_UNKNOWN,     "Unknown",     NULL }
 };
+/* 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 );
@@ -233,6 +255,9 @@ static int Open ( vlc_object_t *p_this )
     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" ); /* FIXME */
     if( f_fps >= 1.0 )
@@ -251,7 +276,7 @@ static int Open ( vlc_object_t *p_this )
     /* 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;
 
@@ -447,7 +472,6 @@ static int Open ( vlc_object_t *p_this )
             if( !( p_sys->subtitle = realloc( 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 );
@@ -542,9 +566,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++;
             }
 
@@ -592,6 +622,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         case DEMUX_GET_ATTACHMENTS:
         case DEMUX_GET_TITLE_INFO:
         case DEMUX_HAS_UNSUPPORTED_META:
+        case DEMUX_CAN_RECORD:
             return VLC_EGENERIC;
 
         default:
@@ -621,12 +652,13 @@ 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;
         }
@@ -637,30 +669,15 @@ static int Demux( demux_t *p_demux )
             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 = 1 + 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++;
     }
 
@@ -883,19 +900,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;
@@ -905,7 +917,9 @@ 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 );
         if( !psz_text )
+        {
             return VLC_ENOMEM;
+        }
         strcat( psz_text, s );
         strcat( psz_text, "\n" );
 
@@ -913,7 +927,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';
@@ -1421,9 +1435,6 @@ static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
     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 );
@@ -1432,6 +1443,14 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
     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( ;; )
     {
         float f1, f2;
@@ -1446,7 +1465,7 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
         {
             if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
             {
-                mpsub_factor = 100.0;
+                p_sys->mpsub.f_factor = 100.0;
                 break;
             }
 
@@ -1461,19 +1480,21 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
                 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
                     var_SetFloat( p_demux, "sub-fps", f_fps );
 
-                mpsub_factor = 1.0;
+                p_sys->mpsub.f_factor = 1.0;
                 free( psz_temp );
                 break;
             }
             free( psz_temp );
         }
         /* Data Lines */
-        if( sscanf (s, "%f %f", &f1, &f2 ) == 2 )
+        f1 = us_strtod( 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);
+            f2 = us_strtod( 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_sys->mpsub.f_total += f2 * p_sys->mpsub.f_factor;
+            p_subtitle->i_stop = (int64_t)(10000.0 * p_sys->mpsub.f_total);
             break;
         }
     }
@@ -1513,10 +1534,14 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
     char         *psz_text2, *psz_orig2;
     int h1, h2, m1, m2, s1, s2, f1, f2;
 
-    static int i_comment = 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;
 
-    static int jss_time_resolution = 30;
-    static int jss_time_shift = 0;
+        p_sys->jss.b_inited = true;
+    }
 
     /* Parse the main lines */
     for( ;; )
@@ -1535,10 +1560,10 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
                     &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;
         }
@@ -1546,9 +1571,9 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
         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 */
@@ -1595,15 +1620,15 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
                          sscanf( &psz_text[shift], "%d.%d", &sec, &f);
                          sec *= inv;
                      }
-                     jss_time_shift = ( ( h * 3600 + m * 60 + sec )
-                         * jss_time_resolution + f ) * 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 ;
 
-                sscanf( &psz_text[shift], "%d", &jss_time_resolution );
+                sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution );
                 break;
             }
             free( psz_orig );
@@ -1665,17 +1690,17 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
         switch( *psz_text )
         {
         case '{':
-            i_comment++;
+            p_sys->jss.i_comment++;
             break;
         case '}':
-            if( i_comment )
+            if( p_sys->jss.i_comment )
             {
-                i_comment = 0;
+                p_sys->jss.i_comment = 0;
                 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
             }
             break;
         case '~':
-            if( !i_comment )
+            if( !p_sys->jss.i_comment )
             {
                 *psz_text2 = ' ';
                 psz_text2++;
@@ -1685,7 +1710,7 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
         case '\t':
             if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
                 break;
-            if( !i_comment )
+            if( !p_sys->jss.i_comment )
             {
                 *psz_text2 = ' ';
                 psz_text2++;
@@ -1723,7 +1748,7 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
             }
             break;
         default:
-            if( !i_comment )
+            if( !p_sys->jss.i_comment )
             {
                 *psz_text2 = *psz_text;
                 psz_text2++;
@@ -1947,7 +1972,7 @@ static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
     return VLC_SUCCESS;
 }
 
-static int ParseSub1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
+static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
 {
     VLC_UNUSED( i_idx );