* sub.c : begun clean up
authorLaurent Aimar <fenrir@videolan.org>
Sun, 9 Feb 2003 13:25:42 +0000 (13:25 +0000)
committerLaurent Aimar <fenrir@videolan.org>
Sun, 9 Feb 2003 13:25:42 +0000 (13:25 +0000)
modules/demux/util/sub.c

index 61b03f0..1d62103 100644 (file)
@@ -2,7 +2,7 @@
  * sub.c
  *****************************************************************************
  * Copyright (C) 1999-2001 VideoLAN
- * $Id: sub.c,v 1.4 2003/02/08 19:10:21 massiot Exp $
+ * $Id: sub.c,v 1.5 2003/02/09 13:25:42 fenrir Exp $
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
@@ -10,7 +10,7 @@
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 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
@@ -39,7 +39,7 @@
 
 static int  Open ( vlc_object_t *p_this );
 
-static int  sub_open ( subtitle_demux_t *p_sub, 
+static int  sub_open ( subtitle_demux_t *p_sub,
                        input_thread_t  *p_input,
                        char  *psz_name,
                        mtime_t i_microsecperframe );
@@ -72,14 +72,14 @@ vlc_module_begin();
     add_category_hint( "subtitle", NULL );
         add_string( "sub-file", NULL, NULL,
                     "subtitle file name", "subtitle file name" );
-        add_float( "sub-fps", 0.0, NULL, 
+        add_float( "sub-fps", 0.0, NULL,
                    "override frames per second",
                    SUB_FPS_LONGTEXT );
         add_integer( "sub-delay", 0, NULL,
-                     "delay subtitles (in 1/10s)", 
+                     "delay subtitles (in 1/10s)",
                      "delay subtitles (in 1/10s)" );
         add_string_from_list( "sub-type", NULL, ppsz_sub_type, NULL,
-                              "subtitle type", 
+                              "subtitle type",
                               SUB_TYPE_LONGTEXT );
     set_callbacks( Open, NULL );
 vlc_module_end();
@@ -95,7 +95,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;
-    
+
     return VLC_SUCCESS;
 }
 #define MAX_TRY     256
@@ -103,7 +103,7 @@ static int Open ( vlc_object_t *p_this )
 /*****************************************************************************
  * sub_open: Open a subtitle file and add subtitle ES
  *****************************************************************************/
-static int  sub_open ( subtitle_demux_t *p_sub, 
+static int  sub_open ( subtitle_demux_t *p_sub,
                        input_thread_t  *p_input,
                        char     *psz_name,
                        mtime_t i_microsecperframe )
@@ -115,8 +115,7 @@ static int  sub_open ( subtitle_demux_t *p_sub,
     int     i_sub_type;
     int     i_max;
     int (*pf_read_subtitle)( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe ) = NULL;
-   
-        
+
     p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
     p_sub->p_es = NULL;
     p_sub->i_subtitles = 0;
@@ -128,13 +127,17 @@ static int  sub_open ( subtitle_demux_t *p_sub,
         psz_name = config_GetPsz( p_sub, "sub-file" );
         if( !psz_name || !*psz_name )
         {
-            return( -1 );
+            return VLC_EGENERIC;
         }
     }
+    else
+    {
+        psz_name = strdup( psz_name );
+    }
 
     if(  config_GetFloat( p_sub, "sub-fps" ) >= 1.0 )
     {
-        i_microsecperframe = (mtime_t)( (float)1000000 / 
+        i_microsecperframe = (mtime_t)( (float)1000000 /
                                         config_GetFloat( p_sub, "sub-fps" ) );
     }
     else if( i_microsecperframe <= 0 )
@@ -146,11 +149,13 @@ static int  sub_open ( subtitle_demux_t *p_sub,
     if( !( p_file = fopen( psz_name, "rb" ) ) )
     {
         msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
-
+        free( psz_name );
+        return VLC_EGENERIC;
     }
     else
     {
         msg_Dbg( p_sub, "opened `%s'", psz_name );
+        free( psz_name );
     }
 
     psz_file_type = config_GetPsz( p_sub, "sub-type" );
@@ -181,7 +186,7 @@ static int  sub_open ( subtitle_demux_t *p_sub,
     {
         i_sub_type = SUB_TYPE_UNKNOWN;
     }
-   
+
     /* *** Now try to autodetect subtitle format *** */
     if( i_sub_type == SUB_TYPE_UNKNOWN )
     {
@@ -200,15 +205,15 @@ static int  sub_open ( subtitle_demux_t *p_sub,
                 i_sub_type = SUB_TYPE_MICRODVD;
                 break;
             }
-            else if( sscanf( buffer, 
+            else if( sscanf( buffer,
                              "%d:%d:%d,%d --> %d:%d:%d,%d",
-                             &i_dummy,&i_dummy,&i_dummy,&i_dummy, 
+                             &i_dummy,&i_dummy,&i_dummy,&i_dummy,
                              &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
             {
                 i_sub_type = SUB_TYPE_SUBRIP;
                 break;
             }
-            else if( sscanf( buffer, 
+            else if( sscanf( buffer,
                              "!: This is a Sub Station Alpha v%d.x script.",
                              &i_dummy ) == 1)
             {
@@ -226,8 +231,6 @@ static int  sub_open ( subtitle_demux_t *p_sub,
             {
                 i_sub_type = SUB_TYPE_SSA2_4; // could be wrong
             }
-                              
-            
         }
     }
 
@@ -253,23 +256,23 @@ static int  sub_open ( subtitle_demux_t *p_sub,
         default:
             msg_Err( p_sub, "unknown subtitile file" );
             fclose( p_file );
-            return( -1 );
+            return VLC_EGENERIC;
     }
-    
+
     if( fseek( p_file, 0L, SEEK_SET ) < 0 )
     {
         msg_Err( p_input, "cannot read file from begining" );
         fclose( p_file );
-        return( -1 );
+        return VLC_EGENERIC;
     }
     for( i_max = 0;; )
-    {   
-        if( p_sub->i_subtitles <= i_max )
+    {
+        if( p_sub->i_subtitles >= i_max )
         {
             i_max += 128;
             if( p_sub->subtitle )
             {
-                p_sub->subtitle = realloc( p_sub->subtitle, 
+                p_sub->subtitle = realloc( p_sub->subtitle,
                                            sizeof( subtitle_t ) * i_max );
             }
             else
@@ -277,8 +280,8 @@ static int  sub_open ( subtitle_demux_t *p_sub,
                 p_sub->subtitle = malloc( sizeof( subtitle_t ) * i_max );
             }
         }
-        if( pf_read_subtitle( p_file, 
-                              p_sub->subtitle + p_sub->i_subtitles, 
+        if( pf_read_subtitle( p_file,
+                              p_sub->subtitle + p_sub->i_subtitles,
                               i_microsecperframe ) < 0 )
         {
             break;
@@ -287,14 +290,13 @@ static int  sub_open ( subtitle_demux_t *p_sub,
     }
     msg_Dbg( p_sub, "loaded %d subtitles", p_sub->i_subtitles );
 
-    
     /* *** Close the file *** */
     fclose( p_file );
 
     /* *** fix subtitle (order and time) *** */
     p_sub->i_subtitle = 0;  // will be modified by sub_fix
     sub_fix( p_sub );
-    
+
     /* *** add subtitle ES *** */
     vlc_mutex_lock( &p_input->stream.stream_lock );
     p_sub->p_es = input_AddES( p_input,
@@ -308,7 +310,7 @@ static int  sub_open ( subtitle_demux_t *p_sub,
     p_sub->p_es->i_cat       = SPU_ES;
 
     p_sub->i_previously_selected = 0;
-    return( 0 );
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
@@ -321,12 +323,12 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
     {
         p_sub->i_previously_selected = 1;
         p_sub->pf_seek( p_sub, i_maxdate );
-        return( 0 );
+        return VLC_SUCCESS;
     }
     else if( !p_sub->p_es->p_decoder_fifo && p_sub->i_previously_selected )
     {
         p_sub->i_previously_selected = 0;
-        return( 0 );
+        return VLC_SUCCESS;
     }
 
     while( p_sub->i_subtitle < p_sub->i_subtitles &&
@@ -334,11 +336,11 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
     {
         pes_packet_t    *p_pes;
         data_packet_t   *p_data;
-        
+
         int i_len;
-        
+
         i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
-        
+
         if( i_len <= 1 )
         {
             /* empty subtitle */
@@ -350,26 +352,26 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
             p_sub->i_subtitle++;
             continue;
         }
-        
-        if( !( p_data = input_NewPacket( p_sub->p_input->p_method_data, 
+
+        if( !( p_data = input_NewPacket( p_sub->p_input->p_method_data,
                                          i_len ) ) )
         {
             input_DeletePES( p_sub->p_input->p_method_data, p_pes );
             p_sub->i_subtitle++;
             continue;
         }
-        
-        p_pes->i_pts = 
-            input_ClockGetTS( p_sub->p_input, 
+
+        p_pes->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);
         if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
         {
-            /* FIXME kludge ... 
+            /* FIXME kludge ...
              * i_dts means end of display...
              */
-            p_pes->i_dts = 
-                input_ClockGetTS( p_sub->p_input, 
+            p_pes->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);
         }
@@ -378,14 +380,14 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
             p_pes->i_dts = 0;
         }
         p_pes->i_nb_data = 1;
-        p_pes->p_first = 
+        p_pes->p_first =
             p_pes->p_last = p_data;
         p_pes->i_pes_size = i_len;
 
-        memcpy( p_data->p_payload_start, 
-                p_sub->subtitle[p_sub->i_subtitle].psz_text, 
+        memcpy( p_data->p_payload_start,
+                p_sub->subtitle[p_sub->i_subtitle].psz_text,
                 i_len );
-        if( p_sub->p_es->p_decoder_fifo )
+        if( p_sub->p_es->p_decoder_fifo && p_pes->i_pts > 0 )
         {
 
             input_DecodePES( p_sub->p_es->p_decoder_fifo, p_pes );
@@ -394,8 +396,7 @@ static int  sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
         {
             input_DeletePES( p_sub->p_input->p_method_data, p_pes );
         }
-        
-        
+
         p_sub->i_subtitle++;
     }
     return( 0 );
@@ -459,11 +460,11 @@ static void  sub_fix( subtitle_demux_t *p_sub )
                     p_sub->subtitle[i_index - 1].i_start )
             {
                 subtitle_t sub_xch;
-                memcpy( &sub_xch, 
-                        p_sub->subtitle + i_index - 1, 
+                memcpy( &sub_xch,
+                        p_sub->subtitle + i_index - 1,
                         sizeof( subtitle_t ) );
-                memcpy( p_sub->subtitle + i_index - 1, 
-                        p_sub->subtitle + i_index, 
+                memcpy( p_sub->subtitle + i_index - 1,
+                        p_sub->subtitle + i_index,
                         sizeof( subtitle_t ) );
                 memcpy( p_sub->subtitle + i_index,
                         &sub_xch,
@@ -472,8 +473,7 @@ static void  sub_fix( subtitle_demux_t *p_sub )
             }
         }
     } while( !i_done );
-        
-    
+
     /* *** and at the end add delay *** */
     i_delay = (mtime_t)config_GetInt( p_sub, "sub-delay" ) * 100000;
     if( i_delay != 0 )
@@ -493,7 +493,7 @@ static void  sub_fix( subtitle_demux_t *p_sub )
 
 
 /*****************************************************************************
- * Specific Subtitle function 
+ * Specific Subtitle function
  *****************************************************************************/
 static int  sub_MicroDvdRead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
 {
@@ -508,15 +508,18 @@ static int  sub_MicroDvdRead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_mi
     uint32_t    i_start;
     uint32_t    i_stop;
     int         i;
-    
+
     for( ;; )
     {
-        if( fgets( buffer, MAX_LINE, p_file ) <= 0) 
+        if( fgets( buffer, MAX_LINE, p_file ) <= 0)
         {
             return( -1 );
         }
         i_start = 0;
         i_stop  = 0;
+
+        buffer[MAX_LINE] = '\0';
+        memset( buffer_text, '\0', MAX_LINE );
         if( sscanf( buffer, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
             sscanf( buffer, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
         {
@@ -524,7 +527,7 @@ static int  sub_MicroDvdRead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_mi
         }
     }
     /* replace | by \n */
-    for( i = 0; i < MAX_LINE; i++ )
+    for( i = 0; i < strlen( buffer_text ); i++ )
     {
         if( buffer_text[i] == '|' )
         {
@@ -546,7 +549,7 @@ static int  sub_SubRipRead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_micr
      * Line2
      * ...
      * [empty line]
-     * 
+     *
      */
     char buffer[MAX_LINE + 1];
     char buffer_text[ 10 * MAX_LINE];
@@ -566,21 +569,21 @@ static int  sub_SubRipRead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_micr
                     &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 + 
+            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 + 
+            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;
-                if( fgets( buffer, MAX_LINE, p_file ) <= 0
+                if( fgets( buffer, MAX_LINE, p_file ) <= 0 )
                 {
                     return( -1 );
                 }
@@ -611,7 +614,6 @@ static int  sub_SubRipRead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_micr
             }
         }
     }
-   
 }
 
 
@@ -624,37 +626,37 @@ static int  sub_SSARead( FILE *p_file, subtitle_t *p_subtitle, mtime_t i_microse
     mtime_t     i_stop;
     int         i_comma;
     int         i_text;
-    
+
     for( ;; )
     {
         int h1, m1, s1, c1, h2, m2, s2, c2;
         int i_dummy;
-        if( fgets( buffer, MAX_LINE, p_file ) <= 0
+        if( fgets( buffer, MAX_LINE, p_file ) <= 0 )
         {
             return( -1 );
         }
-        if( sscanf( buffer, 
+        if( sscanf( buffer,
                     "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
-                    &i_dummy, 
+                    &i_dummy,
                     &h1, &m1, &s1, &c1,
                     &h2, &m2, &s2, &c2,
                     buffer_text ) == 10 )
         {
-            i_start = ( (mtime_t)h1 * 3600*1000 + 
-                        (mtime_t)m1 * 60*1000 + 
-                        (mtime_t)s1 * 1000 + 
+            i_start = ( (mtime_t)h1 * 3600*1000 +
+                        (mtime_t)m1 * 60*1000 +
+                        (mtime_t)s1 * 1000 +
                         (mtime_t)c1 * 10 ) * 1000;
 
-            i_stop  = ( (mtime_t)h2 * 3600*1000 + 
-                        (mtime_t)m2 * 60*1000 + 
-                        (mtime_t)s2 * 1000 + 
+            i_stop  = ( (mtime_t)h2 * 3600*1000 +
+                        (mtime_t)m2 * 60*1000 +
+                        (mtime_t)s2 * 1000 +
                         (mtime_t)c2 * 10 ) * 1000;
-            
+
             p_buffer_text = buffer_text;
             i_comma = 3;
-            while( i_comma < i_comma_count && 
+            while( i_comma < i_comma_count &&
                    *p_buffer_text != '\0' )
-            {   
+            {
                 if( *p_buffer_text == ',' )
                 {
                     i_comma++;