Fix compilation
[vlc.git] / modules / codec / subtitles / subsdec.c
index 1d64f98..82d1e66 100644 (file)
@@ -27,6 +27,9 @@
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
 #include "subsdec.h"
 
@@ -139,23 +142,28 @@ static int OpenDecoder( vlc_object_t *p_this )
     decoder_sys_t *p_sys;
     vlc_value_t    val;
 
-    if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
-        p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
+    switch( p_dec->fmt_in.i_codec )
     {
-        return VLC_EGENERIC;
+        case VLC_FOURCC('s','u','b','t'):
+        case VLC_FOURCC('s','s','a',' '):
+        case VLC_FOURCC('t','1','4','0'):
+            break;
+        default:
+            return VLC_EGENERIC;
     }
 
     p_dec->pf_decode_sub = DecodeBlock;
 
     /* Allocate the memory needed to store the decoder's structure */
-    if( ( p_dec->p_sys = p_sys =
-          (decoder_sys_t *)calloc(1, sizeof(decoder_sys_t)) ) == NULL )
+    p_dec->p_sys = p_sys = malloc( sizeof( *p_sys ) );
+    if( p_sys == NULL )
     {
         msg_Err( p_dec, "out of memory" );
         return VLC_ENOMEM;
     }
 
     /* init of p_sys */
+    memset( p_sys, 0, sizeof( *p_sys ) );
     p_sys->i_align = 0;
     p_sys->iconv_handle = (vlc_iconv_t)-1;
     p_sys->b_autodetect_utf8 = VLC_FALSE;
@@ -166,7 +174,11 @@ static int OpenDecoder( vlc_object_t *p_this )
     TAB_INIT( p_sys->i_images, p_sys->pp_images );
 
     char *psz_charset = NULL;
+
     /* First try demux-specified encoding */
+    if( p_dec->fmt_in.i_codec == VLC_FOURCC('t','1','4','0') )
+        psz_charset = strdup( "UTF-8" ); /* IUT T.140 is always using UTF-8 */
+    else
     if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
     {
         psz_charset = strdup (p_dec->fmt_in.subs.psz_encoding);
@@ -202,25 +214,20 @@ static int OpenDecoder( vlc_object_t *p_this )
         }
     }
 
+    /* Forth, don't do character decoding, i.e. assume UTF-8 */
     if (psz_charset == NULL)
     {
         psz_charset = strdup ("UTF-8");
-        msg_Dbg (p_dec, "trying hard-coded character encoding: %s",
-                 psz_charset ?: "error");
-    }
-
-    if (psz_charset == NULL)
-    {
-        free (p_sys);
-        return VLC_ENOMEM;
+        msg_Dbg (p_dec, "using UTF-8 character encoding" );
     }
 
-    if (strcasecmp (psz_charset, "UTF-8") && strcasecmp (psz_charset, "utf8"))
+    if ((psz_charset != NULL)
+     && strcasecmp (psz_charset, "UTF-8")
+     && strcasecmp (psz_charset, "utf8"))
     {
         p_sys->iconv_handle = vlc_iconv_open ("UTF-8", psz_charset);
         if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
-            msg_Err (p_dec, "cannot convert from %s: %s", psz_charset,
-                     strerror (errno));
+            msg_Err (p_dec, "cannot convert from %s: %m", psz_charset);
     }
     free (psz_charset);
 
@@ -228,7 +235,8 @@ static int OpenDecoder( vlc_object_t *p_this )
     var_Get( p_dec, "subsdec-align", &val );
     p_sys->i_align = val.i_int;
 
-    if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
+    if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ')
+     && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
     {
         if( p_dec->fmt_in.i_extra > 0 )
             ParseSSAHeader( p_dec );
@@ -275,12 +283,9 @@ static void CloseDecoder( vlc_object_t *p_this )
             if( !p_sys->pp_ssa_styles[i] )
                 continue;
 
-            if( p_sys->pp_ssa_styles[i]->psz_stylename )
-                free( p_sys->pp_ssa_styles[i]->psz_stylename );
-            if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname )
-                free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
-            if( p_sys->pp_ssa_styles[i] )
-                free( p_sys->pp_ssa_styles[i] );
+            free( p_sys->pp_ssa_styles[i]->psz_stylename );
+            free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
+            free( p_sys->pp_ssa_styles[i] );
         }
         TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
     }
@@ -294,8 +299,7 @@ static void CloseDecoder( vlc_object_t *p_this )
 
             if( p_sys->pp_images[i]->p_pic )
                 p_sys->pp_images[i]->p_pic->pf_release( p_sys->pp_images[i]->p_pic );
-            if( p_sys->pp_images[i]->psz_filename )
-                free( p_sys->pp_images[i]->psz_filename );
+            free( p_sys->pp_images[i]->psz_filename );
 
             free( p_sys->pp_images[i] );
         }
@@ -393,7 +397,7 @@ static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
     if( !p_spu )
     {
         msg_Warn( p_dec, "can't get spu buffer" );
-        if( psz_subtitle ) free( psz_subtitle );
+        free( psz_subtitle );
         return NULL;
     }
 
@@ -409,7 +413,7 @@ static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
     if( !p_spu->p_region )
     {
         msg_Err( p_dec, "cannot allocate SPU region" );
-        if( psz_subtitle ) free( psz_subtitle );
+        free( psz_subtitle );
         p_dec->pf_spu_buffer_del( p_dec, p_spu );
         return NULL;
     }
@@ -448,7 +452,7 @@ static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
         p_spu->i_original_picture_width = p_sys->i_original_width;
         p_spu->i_original_picture_height = p_sys->i_original_height;
     }
-    if( psz_subtitle ) free( psz_subtitle );
+    free( psz_subtitle );
 
     return p_spu;
 }
@@ -471,7 +475,7 @@ char* GotoNextLine( char *psz_text )
     return p_newline;
 }
 
-/* Function now handles tags which has attribute values, and tries
+/* Function now handles tags with attribute values, and tries
  * to deal with &' commands too. It no longer modifies the string
  * in place, so that the original text can be reused
  */
@@ -568,11 +572,12 @@ static char *StripTags( char *psz_subtitle )
  */
 static char *CreateHtmlSubtitle( char *psz_subtitle )
 {
-    char    psz_tagStack[ 100 ];
+    char   *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
+    if( !psz_tag ) return NULL;
     size_t  i_buf_size     = strlen( psz_subtitle ) + 100;
     char   *psz_html_start = malloc( i_buf_size );
 
-    psz_tagStack[ 0 ] = '\0';
+    psz_tag[ 0 ] = '\0';
 
     if( psz_html_start != NULL )
     {
@@ -600,21 +605,21 @@ static char *CreateHtmlSubtitle( char *psz_subtitle )
                 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
                 {
                     strcpy( psz_html, "<b>" );
-                    strcat( psz_tagStack, "b" );
+                    strcat( psz_tag, "b" );
                     psz_html += 3;
                     psz_subtitle += 3;
                 }
                 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
                 {
                     strcpy( psz_html, "<i>" );
-                    strcat( psz_tagStack, "i" );
+                    strcat( psz_tag, "i" );
                     psz_html += 3;
                     psz_subtitle += 3;
                 }
                 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
                 {
                     strcpy( psz_html, "<u>" );
-                    strcat( psz_tagStack, "u" );
+                    strcat( psz_tag, "u" );
                     psz_html += 3;
                     psz_subtitle += 3;
                 }
@@ -626,7 +631,7 @@ static char *CreateHtmlSubtitle( char *psz_subtitle )
                             "alpha=\"", NULL };
 
                     strcpy( psz_html, "<font " );
-                    strcat( psz_tagStack, "f" );
+                    strcat( psz_tag, "f" );
                     psz_html += 6;
                     psz_subtitle += 6;
 
@@ -664,12 +669,12 @@ static char *CreateHtmlSubtitle( char *psz_subtitle )
                 else if( !strncmp( psz_subtitle, "</", 2 ))
                 {
                     vlc_bool_t  b_match     = VLC_FALSE;
-                    int         i_len       = strlen( psz_tagStack ) - 1;
+                    int         i_len       = strlen( psz_tag ) - 1;
                     char       *psz_lastTag = NULL;
 
                     if( i_len >= 0 )
                     {
-                        psz_lastTag = psz_tagStack + i_len;
+                        psz_lastTag = psz_tag + i_len;
                         i_len = 0;
 
                         switch( *psz_lastTag )
@@ -768,7 +773,7 @@ static char *CreateHtmlSubtitle( char *psz_subtitle )
         strcpy( psz_html, "</text>" );
         psz_html += 7;
 
-        if( psz_tagStack[ 0 ] != '\0' )
+        if( psz_tag[ 0 ] != '\0' )
         {
             /* Not well formed -- kill everything */
             free( psz_html_start );
@@ -780,5 +785,6 @@ static char *CreateHtmlSubtitle( char *psz_subtitle )
             psz_html_start = realloc( psz_html_start,  psz_html - psz_html_start + 1 );
         }
     }
+    free( psz_tag );
     return psz_html_start;
 }