]> git.sesse.net Git - vlc/commitdiff
Support for both types of MPSub
authorJean-Baptiste Kempf <jb@videolan.org>
Mon, 12 May 2008 14:29:34 +0000 (07:29 -0700)
committerJean-Baptiste Kempf <jb@videolan.org>
Mon, 12 May 2008 14:29:58 +0000 (07:29 -0700)
Basic parsing and support of JacoSub (the parsing doesn't work yet with multi-lines and the directives are ignored but the text works)

modules/demux/subtitle.c

index 56476cb136e5e3992992bacfefca9d3bd3d39c34..8b23ed6f08691e7615c3eed1d18ea8b37b21fd25 100644 (file)
@@ -25,6 +25,7 @@
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
+
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
@@ -33,7 +34,6 @@
 #include <vlc_plugin.h>
 #include <vlc_input.h>
 
-
 #include <errno.h>
 #ifdef HAVE_SYS_TYPES_H
 #   include <sys/types.h>
@@ -99,12 +99,13 @@ enum
     SUB_TYPE_ASS,
     SUB_TYPE_VPLAYER,
     SUB_TYPE_SAMI,
-    SUB_TYPE_SUBVIEWER,
+    SUB_TYPE_SUBVIEWER, //SUBVIEWER 2!
     SUB_TYPE_DVDSUBTITLE,
     SUB_TYPE_MPL2,
     SUB_TYPE_AQT,
     SUB_TYPE_PJS,
-    SUB_TYPE_MPSUB
+    SUB_TYPE_MPSUB,
+    SUB_TYPE_JACOSUB
 };
 
 typedef struct
@@ -113,6 +114,7 @@ typedef struct
     int     i_line;
     char    **line;
 } text_t;
+
 static int  TextLoad( text_t *, stream_t *s );
 static void TextUnload( text_t * );
 
@@ -152,7 +154,8 @@ static int  ParseDVDSubtitle( demux_t *, subtitle_t *, int );
 static int  ParseMPL2       ( demux_t *, subtitle_t *, int );
 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  ParseMPSub      ( demux_t *, subtitle_t *, int );
+static int  ParseJSS        ( demux_t *, subtitle_t *, int );
 
 static struct
 {
@@ -175,9 +178,18 @@ static struct
     { "aqt",        SUB_TYPE_AQT,         "AQTitle",     ParseAQT },
     { "pjs",        SUB_TYPE_PJS,         "PhoenixSub",  ParsePJS },
     { "mpsub",      SUB_TYPE_MPSUB,       "MPSub",       ParseMPSub },
+    { "jacosub",    SUB_TYPE_JACOSUB,     "JacoSub",     ParseJSS },
     { NULL,         SUB_TYPE_UNKNOWN,     "Unknown",     NULL }
 };
 
+/* Missing Detect
+    SubViewer 1
+    JSS
+    RealText
+    Subrip09
+   */
+
+
 static int Demux( demux_t * );
 static int Control( demux_t *, int, va_list );
 
@@ -258,7 +270,7 @@ static int Open ( vlc_object_t *p_this )
         for( i_try = 0; i_try < 256; i_try++ )
         {
             int i_dummy;
-            float f_dummy;
+            char p_dummy;
 
             if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
                 break;
@@ -312,6 +324,11 @@ 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)
+            {
+                p_sys->i_type = SUB_TYPE_JACOSUB;
+            }
             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 )
             {
@@ -330,7 +347,9 @@ static int Open ( vlc_object_t *p_this )
                 p_sys->i_type = SUB_TYPE_MPL2;
                 break;
             }
-            else if( sscanf( s, "%f %f", &f_dummy, &f_dummy ) == 2 )
+            else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
+                     ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
+                       && p_dummy =='E' ) )
             {
                 p_sys->i_type = SUB_TYPE_MPSUB;
             }
@@ -1325,7 +1344,7 @@ static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
             p_subtitle->i_start = 10 * t1;
             p_subtitle->i_stop = 10 * t2;
             /* Remove latest " */
-            psz_text[ strlen(psz_text) - 1 ] = '\0 ';
+            psz_text[ strlen(psz_text) - 1 ] = '\0';
 
             break;
         }
@@ -1336,7 +1355,8 @@ static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
     return VLC_SUCCESS;
 }
 
-static float mpsub_total = 0;
+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 )
 {
@@ -1348,17 +1368,39 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
     {
         const char *s = TextGetLine( txt );
         float f1, f2;
+        char p_dummy;
+        char *psz_temp;
 
         if( !s )
             return VLC_EGENERIC;
 
+        if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
+        {
+            mpsub_factor = 100.0;
+            break;
+        }
+
+        psz_temp = malloc( strlen(s) - 6 );
+        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 )
+                var_SetFloat( p_demux, "sub-fps", f_fps );
+
+            mpsub_factor = 1.0;
+            free( psz_temp );
+            break;
+        }
+        free( psz_temp );
+
         /* Data Lines */
         if( sscanf (s, "%f %f", &f1, &f2 ) == 2 )
         {
-            mpsub_total += f1;
-            p_subtitle->i_start = (int64_t)(1000000.0 * mpsub_total);
-            mpsub_total += f2;
-            p_subtitle->i_stop = (int64_t)(1000000.0 * mpsub_total);
+            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);
             break;
         }
     }
@@ -1388,3 +1430,212 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
     return VLC_SUCCESS;
 }
 
+static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int 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;
+
+    /* Parse the main lines */
+    for( ;; )
+    {
+        const char *s = TextGetLine( txt );
+        if( !s )
+            return VLC_EGENERIC;
+
+        psz_text = malloc( strlen( s ) + 1 );
+        psz_orig = psz_text;
+
+        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 ) )
+                * 1000000;
+            p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
+                (int64_t)( ( f2 +  jss_time_shift ) /  jss_time_resolution ) )
+                * 1000000;
+        }
+        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 );
+            p_subtitle->i_stop = (int64_t)(
+                    ( f2 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
+        }
+        else if( s[0] == '#' )
+        {
+            int h = 0, m =0, sec = 1, f = 1;
+            unsigned shift = 1;
+            int inv = 1;
+
+            strcpy( psz_text, s );
+
+            switch( toupper( psz_text[1] ) )
+            {
+                case 'S':
+                     shift = isalpha( psz_text[2] ) ? 6 : 2 ;
+
+                     if( sscanf( &psz_text[shift], "%d", &h ) )
+                     {
+                         /* Negative shifting */
+                         if( h < 0 )
+                         {
+                             h *= -1;
+                             inv = -1;
+                         }
+
+                         if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
+                         {
+                             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;
+                             }
+                         }
+                         else
+                         {
+                             h = m = 0;
+                             sscanf( &psz_text[shift], "%d.%d", &sec, &f);
+                             sec *= inv;
+                         }
+                         jss_time_shift = ( ( h * 3600 + m * 60 + sec )
+                             * jss_time_resolution + f ) * inv;
+                     }
+                     break;
+
+                case 'T':
+                    shift = isalpha( psz_text[2] ) ? 8 : 2 ;
+
+                    sscanf( &psz_text[shift], "%d", &jss_time_resolution );
+                    break;
+            }
+            free( psz_text );
+            continue;
+        }
+        else
+            /* Unkown line */
+        {
+            free( psz_text );
+            continue;
+        }
+
+        /* Skip the blanks */
+        while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
+
+        /* Parse the directives */
+        if( isalpha( *psz_text ) || *psz_text == '[' )
+        {
+            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 )*/
+        }
+
+        /* 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;
+
+        for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
+        {
+            switch( *psz_text )
+            {
+            case '{':
+                i_comment++;
+                break;
+            case '}':
+                if( i_comment )
+                {
+                    i_comment = 0;
+                    if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
+                }
+                break;
+            case '~':
+                if( !i_comment )
+                {
+                    *psz_text2 = ' ';
+                    psz_text2++;
+                }
+                break;
+            case ' ':
+            case '\t':
+                if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
+                    break;
+                if( !i_comment )
+                {
+                    *psz_text2 = ' ';
+                    psz_text2++;
+                }
+                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;
+
+                    while ( *s2 == ' ' ) s2++;
+
+/*                    int i_len = strlen( psz_orig2 );
+
+
+                    psz_orig2 = realloc( psz_orig2, strlen( s2 ) +  i_len + 1 );
+
+
+                    */
+                }
+            default:
+                if( !i_comment )
+                {
+                    *psz_text2 = *psz_text;
+                    psz_text2++;
+                }
+            }
+            psz_text++;
+        }
+
+        p_subtitle->psz_text = psz_orig2;
+        free( psz_orig );
+        return VLC_SUCCESS;
+    }
+}