]> git.sesse.net Git - vlc/blobdiff - modules/demux/util/sub.c
* ALL: use p_block->i_length for text subtitles duration (instead of the i_dts hack).
[vlc] / modules / demux / util / sub.c
index 7b22c5616df8beeca239fd2c96947e29f3df1de0..a06175b8cdd9774143d5216956ffac0523d5489a 100644 (file)
@@ -2,7 +2,7 @@
  * sub.c: subtitle demux for external subtitle files
  *****************************************************************************
  * Copyright (C) 1999-2004 VideoLAN
- * $Id: sub.c,v 1.48 2004/01/27 12:46:46 fenrir Exp $
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Derk-Jan Hartman <hartman at videolan dot org>
@@ -55,7 +55,7 @@ static void sub_close( subtitle_demux_t *p_sub );
 
 static void sub_fix( subtitle_demux_t *p_sub );
 
-static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "ssa1",
+static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "subviewer", "ssa1",
   "ssa2-4", "vplayer", "sami", "vobsub" };
 
 /*****************************************************************************
@@ -73,7 +73,7 @@ static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "ssa1",
 vlc_module_begin();
     set_description( _("Text subtitles demux") );
     set_capability( "subtitle demux", 12 );
-    add_float( "sub-fps", 25.0, NULL,
+    add_float( "sub-fps", 0.0, NULL,
                N_("Frames per second"),
                SUB_FPS_LONGTEXT, VLC_TRUE );
     add_integer( "sub-delay", 0, NULL,
@@ -96,7 +96,7 @@ static int Open ( vlc_object_t *p_this )
     p_sub->pf_demux = sub_demux;
     p_sub->pf_seek  = sub_seek;
     p_sub->pf_close = sub_close;
-    
+
     /* Initialize the variables */
     var_Create( p_this, "sub-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
     var_Create( p_this, "sub-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
@@ -207,10 +207,11 @@ static void text_rewind( text_t *txt )
 
 static int  sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
 static int  sub_SubRipRead  ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
+static int  sub_SubViewer   ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
 static int  sub_SSARead     ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
 static int  sub_Vplayer     ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
 static int  sub_Sami        ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
-static int  sub_VobSub      ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
+static int  sub_VobSubIDX   ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
 
 static int  DemuxVobSub     ( subtitle_demux_t *, block_t * );
 
@@ -224,11 +225,12 @@ static struct
 {
     { "microdvd",   SUB_TYPE_MICRODVD,  "MicroDVD", sub_MicroDvdRead },
     { "subrip",     SUB_TYPE_SUBRIP,    "SubRIP",   sub_SubRipRead },
+    { "subviewer",  SUB_TYPE_SUBVIEWER, "SubViewer",sub_SubViewer },
     { "ssa1",       SUB_TYPE_SSA1,      "SSA-1",    sub_SSARead },
     { "ssa2-4",     SUB_TYPE_SSA2_4,    "SSA-2/3/4",sub_SSARead },
     { "vplayer",    SUB_TYPE_VPLAYER,   "VPlayer",  sub_Vplayer },
     { "sami",       SUB_TYPE_SAMI,      "SAMI",     sub_Sami },
-    { "vobsub",     SUB_TYPE_VOBSUB,    "VobSub",   sub_VobSub },
+    { "vobsub",     SUB_TYPE_VOBSUB,    "VobSub",   sub_VobSubIDX },
     { NULL,         SUB_TYPE_UNKNOWN,   "Unknown",  NULL }
 };
 
@@ -274,6 +276,7 @@ static int sub_open( subtitle_demux_t *p_sub, input_thread_t  *p_input,
     p_sub->i_subtitles = 0;
     p_sub->subtitle = NULL;
     p_sub->p_vobsub_file = 0;
+    p_sub->i_original_mspf = i_microsecperframe;
     p_sub->p_input = p_input;
 
     if( !psz_name || !*psz_name )
@@ -296,9 +299,15 @@ static int sub_open( subtitle_demux_t *p_sub, input_thread_t  *p_input,
     {
         i_microsecperframe = (mtime_t)( (float)1000000 / val.f_float );
     }
+    else if( val.f_float == 0 )
+    {
+        /* No value given */
+        i_microsecperframe = 0;
+    }
     else if( val.f_float <= 0 )
     {
-        i_microsecperframe = 40000; /* default: 25fps */
+        /* invalid value, default = 25fps */
+        i_microsecperframe = 40000;
     }
 
     var_Get( p_sub, "sub-type", &val);
@@ -386,6 +395,11 @@ static int sub_open( subtitle_demux_t *p_sub, input_thread_t  *p_input,
                 i_sub_type = SUB_TYPE_SSA2_4; /* could be wrong */
                 break;
             }
+            else if( local_stristr( s, "[INFORMATION]" ) )
+            {
+                i_sub_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
+                break;
+            }
             else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
                      sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
             {
@@ -395,7 +409,6 @@ static int sub_open( subtitle_demux_t *p_sub, input_thread_t  *p_input,
             else if( local_stristr( s, "# VobSub index file" ) )
             {
                 i_sub_type = SUB_TYPE_VOBSUB;
-                ps_track_init( p_sub->tk );
                 break;
             }
         }
@@ -450,6 +463,7 @@ static int sub_open( subtitle_demux_t *p_sub, input_thread_t  *p_input,
 
     /* *** fix subtitle (order and time) *** */
     p_sub->i_subtitle = 0;  /* will be modified by sub_fix */
+    
     if( p_sub->i_sub_type != SUB_TYPE_VOBSUB )
     {
         sub_fix( p_sub );
@@ -499,6 +513,8 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
 {
     input_thread_t *p_input = p_sub->p_input;
     vlc_bool_t     b;
+    vlc_value_t    val;
+    mtime_t i_delay;
 
     es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_sub->p_es, &b );
     if( b && !p_sub->i_previously_selected )
@@ -515,8 +531,10 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
 
     if( p_sub->i_sub_type != SUB_TYPE_VOBSUB )
     {
+        var_Get( p_sub, "sub-delay", &val );
+        i_delay = (mtime_t) val.i_int * 100000;
         while( p_sub->i_subtitle < p_sub->i_subtitles &&
-               p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
+               p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate - i_delay )
         {
             block_t *p_block;
             int i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
@@ -534,24 +552,29 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
                 continue;
             }
 
-            /* XXX we should convert all demuxers to use es_out_Control to set pcr and
-             * then remove that */
-            p_block->i_pts =
-                input_ClockGetTS( p_sub->p_input,
-                                  p_sub->p_input->stream.p_selected_program,
-                                  p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
-            p_block->i_dts = 0;
-            if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
+            /* XXX we should convert all demuxers to use es_out_Control to set              * pcr and then remove that */
+            if( i_delay != 0 )
             {
-                /* FIXME kludge i_dts means end of display... */
-                p_block->i_dts =
-                    input_ClockGetTS( p_sub->p_input,
-                                  p_sub->p_input->stream.p_selected_program,
-                                  p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
+                p_sub->subtitle[p_sub->i_subtitle].i_start += i_delay;
+                p_sub->subtitle[p_sub->i_subtitle].i_stop += i_delay;
+            }
+
+            if( p_sub->subtitle[p_sub->i_subtitle].i_start < 0 )
+            {
+                p_sub->i_subtitle++;
+                continue;
             }
 
-            memcpy( p_block->p_buffer, p_sub->subtitle[p_sub->i_subtitle].psz_text, i_len );
+            p_block->i_pts = p_sub->subtitle[p_sub->i_subtitle].i_start;
+            p_block->i_dts = p_block->i_pts;
+            if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
+            {
+                p_block->i_length =
+                    p_sub->subtitle[p_sub->i_subtitle].i_stop - p_block->i_pts;
+            }
 
+            memcpy( p_block->p_buffer,
+                    p_sub->subtitle[p_sub->i_subtitle].psz_text, i_len );
             if( p_block->i_pts > 0 )
             {
                 es_out_Send( p_input->p_es_out, p_sub->p_es, p_block );
@@ -580,7 +603,7 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
             if( i_size <= 0 ) i_size = 65535;   /* Invalid or EOF */
 
             /* Seek at the right place (could be avoid if sub_seek is fixed to do his job) */
-            if( fseek( p_sub->p_vobsub_file, i_pos, SEEK_CUR ) )
+            if( fseek( p_sub->p_vobsub_file, i_pos, SEEK_SET ) )
             {
                 msg_Warn( p_sub, "cannot seek at right vobsub location %d", i_pos );
                 p_sub->i_subtitle++;
@@ -604,10 +627,7 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
             }
 
             /* pts */
-            p_block->i_pts =
-                input_ClockGetTS( p_sub->p_input,
-                                  p_sub->p_input->stream.p_selected_program,
-                                  p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
+            p_block->i_pts = p_sub->subtitle[p_sub->i_subtitle].i_start;
 
             /* demux this block */
             DemuxVobSub( p_sub, p_block );
@@ -661,11 +681,8 @@ static void sub_close( subtitle_demux_t *p_sub )
  *****************************************************************************/
 static void  sub_fix( subtitle_demux_t *p_sub )
 {
-    int     i;
-    mtime_t i_delay;
     int     i_index;
     int     i_done;
-    vlc_value_t val;
 
     /* *** fix order (to be sure...) *** */
     /* We suppose that there are near in order and this durty bubble sort
@@ -693,7 +710,8 @@ static void  sub_fix( subtitle_demux_t *p_sub )
             }
         }
     } while( !i_done );
-
+#if 0
+    /* We do not do this here anymore */
     /* *** and at the end add delay *** */
     var_Get( p_sub, "sub-delay", &val );
     i_delay = (mtime_t) val.i_int * 100000;
@@ -709,6 +727,7 @@ static void  sub_fix( subtitle_demux_t *p_sub )
             }
         }
     }
+#endif
 }
 
 
@@ -730,6 +749,11 @@ static int  sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *
     unsigned int    i_start;
     unsigned int    i_stop;
     unsigned int i;
+    
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
 
     for( ;; )
     {
@@ -755,6 +779,10 @@ static int  sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *
             buffer_text[i] = '\n';
         }
     }
+    if( i_microsecperframe == 0)
+    {
+        i_microsecperframe = 40000;
+    }
     p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
     p_subtitle->i_stop  = (mtime_t)i_stop  * (mtime_t)i_microsecperframe;
     p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
@@ -778,6 +806,11 @@ static int  sub_SubRipRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_
     mtime_t     i_start;
     mtime_t     i_stop;
 
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
+
     for( ;; )
     {
         int h1, m1, s1, d1, h2, m2, s2, d2;
@@ -817,6 +850,108 @@ static int  sub_SubRipRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_
                     p_subtitle->i_start = i_start;
                     p_subtitle->i_stop = i_stop;
                     p_subtitle->psz_text = strdup( buffer_text );
+                    /* If framerate is available, use sub-fps */
+                    if( i_microsecperframe != 0 && p_sub->i_original_mspf != 0)
+                    {
+                        p_subtitle->i_start = (mtime_t)i_start *
+                                              (mtime_t)p_sub->i_original_mspf /
+                                              (mtime_t)i_microsecperframe;
+                        p_subtitle->i_stop  = (mtime_t)i_stop  *
+                                              (mtime_t)p_sub->i_original_mspf /
+                                              (mtime_t)i_microsecperframe;
+                    }
+                    return( 0 );
+                }
+                else
+                {
+                    if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
+                    {
+                        memcpy( buffer_text + i_buffer_text,
+                                s,
+                                i_len );
+                        i_buffer_text += i_len;
+
+                        buffer_text[i_buffer_text] = '\n';
+                        i_buffer_text++;
+                    }
+                }
+            }
+        }
+    }
+}
+
+static int  sub_SubViewer( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
+{
+    /*
+     * h1:m1:s1.d1,h2:m2:s2.d2
+     * Line1[br]Line2
+     * Line3
+     * ...
+     * [empty line]
+     * ( works with subviewer and subviewer v2 )
+     */
+    char *s;
+    char buffer_text[ 10 * MAX_LINE];
+    int  i_buffer_text;
+    mtime_t     i_start;
+    mtime_t     i_stop;
+
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
+
+    for( ;; )
+    {
+        int h1, m1, s1, d1, h2, m2, s2, d2;
+        if( ( s = text_get_line( txt ) ) == NULL )
+        {
+            return( VLC_EGENERIC );
+        }
+        if( sscanf( s,
+                    "%d:%d:%d.%d,%d:%d:%d.%d",
+                    &h1, &m1, &s1, &d1,
+                    &h2, &m2, &s2, &d2 ) == 8 )
+        {
+            i_start = ( (mtime_t)h1 * 3600*1000 +
+                        (mtime_t)m1 * 60*1000 +
+                        (mtime_t)s1 * 1000 +
+                        (mtime_t)d1 ) * 1000;
+
+            i_stop  = ( (mtime_t)h2 * 3600*1000 +
+                        (mtime_t)m2 * 60*1000 +
+                        (mtime_t)s2 * 1000 +
+                        (mtime_t)d2 ) * 1000;
+
+            /* Now read text until an empty line */
+            for( i_buffer_text = 0;; )
+            {
+                int i_len, i;
+                if( ( s = text_get_line( txt ) ) == NULL )
+                {
+                    return( VLC_EGENERIC );
+                }
+
+                i_len = strlen( s );
+                if( i_len <= 1 )
+                {
+                    /* empty line -> end of this subtitle */
+                    buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
+                    p_subtitle->i_start = i_start;
+                    p_subtitle->i_stop = i_stop;
+
+                    /* replace [br] by \n */
+                    for( i = 0; i < strlen( buffer_text ) - 3; i++ )
+                    {
+                        if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
+                            buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
+                        {
+                            char *temp = buffer_text + i + 1;
+                            buffer_text[i] = '\n';
+                            memmove( temp, temp+3, strlen( temp-3 ));
+                        }
+                    }
+                    p_subtitle->psz_text = strdup( buffer_text );
                     return( 0 );
                 }
                 else
@@ -845,6 +980,11 @@ static int  sub_SSARead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_sub
     mtime_t     i_start;
     mtime_t     i_stop;
 
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
+
     for( ;; )
     {
         int h1, m1, s1, c1, h2, m2, s2, c2;
@@ -921,13 +1061,17 @@ static int  sub_Vplayer( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_sub
      *  h:m:s:Line1|Line2|Line3....
      *  or
      *  h:m:s Line1|Line2|Line3....
-     * where n1 and n2 are the video frame number...
      *
      */
     char *p;
     char buffer_text[MAX_LINE + 1];
     mtime_t    i_start;
     unsigned int i;
+    
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
 
     for( ;; )
     {
@@ -1004,6 +1148,12 @@ static int  sub_Sami( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtit
 
     int  i_text;
     char buffer_text[10*MAX_LINE + 1];
+
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
+
 #define ADDC( c ) \
     if( i_text < 10*MAX_LINE )      \
     {                               \
@@ -1086,7 +1236,7 @@ static int  sub_Sami( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtit
 #undef ADDC
 }
 
-static int  sub_VobSub( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
+static int  sub_VobSubIDX( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
 {
     /*
      * Parse the idx file. Each line:
@@ -1095,10 +1245,14 @@ static int  sub_VobSub( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subt
      *
      */
     char *p;
-
     char buffer_text[MAX_LINE + 1];
     unsigned int    i_start, i_location;
 
+    p_subtitle->i_start = 0;
+    p_subtitle->i_stop  = 0;
+    p_subtitle->i_vobsub_location  = 0;
+    p_subtitle->psz_text = NULL;
+
     for( ;; )
     {
         unsigned int h, m, s, ms, loc;
@@ -1123,6 +1277,7 @@ static int  sub_VobSub( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subt
     }
     p_subtitle->i_start = (mtime_t)i_start;
     p_subtitle->i_stop  = 0;
+    p_subtitle->psz_text = NULL;
     p_subtitle->i_vobsub_location = i_location;
     fprintf( stderr, "time: %x, location: %x\n", i_start, i_location );
     return( 0 );
@@ -1172,13 +1327,20 @@ static int  DemuxVobSub( subtitle_demux_t *p_demux, block_t *p_bk )
         i_spu = i_id&0x1f;
         msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size );
 
-        if( p_demux->p_es )
+        /* FIXME i_spu == determines which of the spu tracks we will show. */
+        if( p_demux->p_es && i_spu == 0 )
         {
-            p_pkt->i_pts = p_bk->i_pts;
+            p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
+            p_pkt->i_length = 0;
             es_out_Send( p_demux->p_input->p_es_out, p_demux->p_es, p_pkt );
 
             p_bk->i_pts = 0;    /* only first packet has a pts */
         }
+        else
+        {
+            block_Release( p_pkt );
+            continue;
+        }
     }
 
     return VLC_SUCCESS;