From b8f5a3587b8a0a5b8380793e66926cf1dea042ca Mon Sep 17 00:00:00 2001
From: basOS G <noxelia 4t gmail , com>
Date: Mon, 29 Dec 2008 23:11:05 +0200
Subject: [PATCH] Added textStyleInit. Cleaned modules

Added a method to initialize or copy text style struct
taking care for memmory management. Please mind that on
deletion you have to free the p_style->psz_fontname
before p_style itself.. The vout_ShowText* functions
will take a p_style and copy it internally so you have
to free in the same function. Other modules where p_style
was malloced there are now fixed to copy before calling
the sout* functions.
---
 include/vlc_osd.h                                  |    8 ++++-
 modules/codec/subtitles/subsass.c                  |    2 +-
 modules/codec/subtitles/subsusf.c                  |   10 ++++--
 modules/gui/fbosd.c                                |    5 ++-
 .../video_filter/dynamicoverlay/dynamicoverlay.c   |    4 +--
 .../video_filter/dynamicoverlay/dynamicoverlay.h   |    2 +-
 .../dynamicoverlay/dynamicoverlay_commands.c       |   16 +++++----
 modules/video_filter/marq.c                        |    8 +++--
 modules/video_filter/rss.c                         |   10 ++++--
 src/libvlccore.sym                                 |    1 +
 src/video_output/video_text.c                      |    4 +-
 src/video_output/vout_subpictures.c                |   32 +++++++++++++++++++-
 12 files changed, 74 insertions(+), 28 deletions(-)

diff --git a/include/vlc_osd.h b/include/vlc_osd.h
index df855f6..aef1f4b 100644
--- a/include/vlc_osd.h
+++ b/include/vlc_osd.h
@@ -265,8 +265,12 @@ struct text_style_t
 #define STYLE_UNDERLINE   32
 #define STYLE_STRIKEOUT   64
 
-static const text_style_t default_text_style = { NULL, 22, 0xffffff, 0xff, STYLE_OUTLINE,
-                0x000000, 0xff, 0x000000, 0xff, 0xffffff, 0x80, 0xffffff, 0xff, 1, 0, -1 };
+/**
+ * Initialize a text_style_t struct from another or default when arg is NULL
+ *
+ * Use this whenever a new text_style_t is wanted to properly manage memory
+ */
+VLC_EXPORT( text_style_t* , textStyleInit, ( text_style_t* ) );
 
 /**
  * OSD menu button states
diff --git a/modules/codec/subtitles/subsass.c b/modules/codec/subtitles/subsass.c
index e642673..b712988 100644
--- a/modules/codec/subtitles/subsass.c
+++ b/modules/codec/subtitles/subsass.c
@@ -132,7 +132,7 @@ void ParseSSAString( decoder_t *p_dec,
     else
     {
         msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename);
-        p_spu->p_region->p_style = &p_style->font_style;
+        p_spu->p_region->p_style = textStyleInit( &p_style->font_style ) ;
         p_spu->p_region->i_align = p_style->i_align;
         if( p_style->i_align & SUBPICTURE_ALIGN_LEFT )
         {
diff --git a/modules/codec/subtitles/subsusf.c b/modules/codec/subtitles/subsusf.c
index ab7756b..fceebd0 100644
--- a/modules/codec/subtitles/subsusf.c
+++ b/modules/codec/subtitles/subsusf.c
@@ -414,7 +414,11 @@ static subpicture_region_t *CreateTextRegion( decoder_t *p_dec,
         {
             msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename );
 
-            p_text_region->p_style = &p_style->font_style;
+            /* basos g: region->p_style is freed by the vout. Decoder may live shorter.
+               cmnt from spu_DestroyRegion :"fenrir plugin does not allocate the memory for it.
+               I think it might lead to segfault, video renderer can live longer than the decoder"
+            */
+            p_text_region->p_style = textStyleInit( &p_style->font_style ) ;
             p_text_region->i_align = p_style->i_align;
 
             /* TODO: Setup % based offsets properly, without adversely affecting
@@ -508,7 +512,7 @@ static int ParseImageAttachments( decoder_t *p_dec )
                     video_format_t     fmt_in;
                     video_format_t     fmt_out;
 
-                    memcpy( p_block->p_buffer, p_attach->p_data, p_attach->i_data );
+                    vlc_memcpy( p_block->p_buffer, p_attach->p_data, p_attach->i_data );
 
                     memset( &fmt_in,  0, sizeof( video_format_t));
                     memset( &fmt_out, 0, sizeof( video_format_t));
@@ -657,7 +661,7 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
                         {
                             ssa_style_t *p_default_style = p_sys->pp_ssa_styles[i];
 
-                            memcpy( p_style, p_default_style, sizeof( ssa_style_t ) );
+                            vlc_memcpy( p_style, p_default_style, sizeof( ssa_style_t ) );
                             p_style->font_style.psz_fontname = strdup( p_style->font_style.psz_fontname );
                             p_style->psz_stylename = NULL;
                         }
diff --git a/modules/gui/fbosd.c b/modules/gui/fbosd.c
index f64b4b1..f30848f 100644
--- a/modules/gui/fbosd.c
+++ b/modules/gui/fbosd.c
@@ -311,19 +311,19 @@ static int Create( vlc_object_t *p_this )
         return VLC_ENOMEM;
     memset( p_sys, 0, sizeof(intf_sys_t) );
 
-    p_sys->p_style = malloc( sizeof( text_style_t ) );
+    p_sys->p_style = textStyleInit( NULL );
     if( !p_sys->p_style )
     {
         free( p_intf->p_sys );
         return VLC_ENOMEM;
     }
-    vlc_memcpy( p_sys->p_style, &default_text_style, sizeof( text_style_t ) );
 
     p_intf->pf_run = Run;
 
     p_sys->p_image = image_HandlerCreate( p_this );
     if( !p_sys->p_image )
     {
+        free( p_intf->p_sys->p_style->psz_fontname ) ;
         free( p_intf->p_sys->p_style );
         free( p_intf->p_sys );
         return VLC_ENOMEM;
@@ -521,6 +521,7 @@ static void Destroy( vlc_object_t *p_this )
     if( p_sys->p_overlay )
         picture_Release( p_sys->p_overlay );
 
+    free( p_sys->p_style->psz_fontname ) ;
     free( p_sys->p_style );
     free( p_sys );
 }
diff --git a/modules/video_filter/dynamicoverlay/dynamicoverlay.c b/modules/video_filter/dynamicoverlay/dynamicoverlay.c
index be3327d..2dd799c 100644
--- a/modules/video_filter/dynamicoverlay/dynamicoverlay.c
+++ b/modules/video_filter/dynamicoverlay/dynamicoverlay.c
@@ -358,9 +358,7 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
         if( p_overlay->format.i_chroma == VLC_FOURCC('T','E','X','T') )
         {
             p_region->psz_text = strdup( p_overlay->data.p_text );
-            p_region->p_style = malloc( sizeof(struct text_style_t) );
-            if( p_region->p_style )
-                *p_region->p_style = p_overlay->fontstyle;
+            p_region->p_style = textStyleInit( p_overlay->p_fontstyle ) ;
         }
         else
         {
diff --git a/modules/video_filter/dynamicoverlay/dynamicoverlay.h b/modules/video_filter/dynamicoverlay/dynamicoverlay.h
index 8eb500a..86caa18 100644
--- a/modules/video_filter/dynamicoverlay/dynamicoverlay.h
+++ b/modules/video_filter/dynamicoverlay/dynamicoverlay.h
@@ -122,7 +122,7 @@ typedef struct overlay_t
     bool b_active;
 
     video_format_t format;
-    struct text_style_t fontstyle;
+    struct text_style_t* p_fontstyle;
     union {
         picture_t *p_pic;
         char *p_text;
diff --git a/modules/video_filter/dynamicoverlay/dynamicoverlay_commands.c b/modules/video_filter/dynamicoverlay/dynamicoverlay_commands.c
index 03e28b2..06a1aa6 100644
--- a/modules/video_filter/dynamicoverlay/dynamicoverlay_commands.c
+++ b/modules/video_filter/dynamicoverlay/dynamicoverlay_commands.c
@@ -58,7 +58,7 @@ overlay_t *OverlayCreate( void )
     p_ovl->b_active = false;
     vout_InitFormat( &p_ovl->format, VLC_FOURCC( '\0','\0','\0','\0') , 0, 0,
                      VOUT_ASPECT_FACTOR );
-    memcpy( &p_ovl->fontstyle, &default_text_style, sizeof(struct text_style_t) );
+    p_ovl->p_fontstyle = textStyleInit( NULL ) ;
     p_ovl->data.p_text = NULL;
 
     return p_ovl;
@@ -68,6 +68,8 @@ int OverlayDestroy( overlay_t *p_ovl )
 {
     if( p_ovl->data.p_text != NULL )
         free( p_ovl->data.p_text );
+    free( p_ovl->p_fontstyle->psz_fontname );
+    free( p_ovl->p_fontstyle ) ;
 
     return VLC_SUCCESS;
 }
@@ -636,7 +638,7 @@ static int exec_GetTextAlpha( filter_t *p_filter,
     if( p_ovl == NULL )
         return VLC_EGENERIC;
 
-    p_results->fontstyle.i_font_alpha = p_ovl->fontstyle.i_font_alpha;
+    p_results->fontstyle.i_font_alpha = p_ovl->p_fontstyle->i_font_alpha;
     return VLC_SUCCESS;
 }
 
@@ -649,7 +651,7 @@ static int exec_GetTextColor( filter_t *p_filter,
     if( p_ovl == NULL )
         return VLC_EGENERIC;
 
-    p_results->fontstyle.i_font_color = p_ovl->fontstyle.i_font_color;
+    p_results->fontstyle.i_font_color = p_ovl->p_fontstyle->i_font_color;
     return VLC_SUCCESS;
 }
 
@@ -662,7 +664,7 @@ static int exec_GetTextSize( filter_t *p_filter,
     if( p_ovl == NULL )
         return VLC_EGENERIC;
 
-    p_results->fontstyle.i_font_size = p_ovl->fontstyle.i_font_size;
+    p_results->fontstyle.i_font_size = p_ovl->p_fontstyle->i_font_size;
     return VLC_SUCCESS;
 }
 
@@ -725,7 +727,7 @@ static int exec_SetTextAlpha( filter_t *p_filter,
     if( p_ovl == NULL )
         return VLC_EGENERIC;
 
-    p_ovl->fontstyle.i_font_alpha = p_params->fontstyle.i_font_alpha;
+    p_ovl->p_fontstyle->i_font_alpha = p_params->fontstyle.i_font_alpha;
     p_sys->b_updated = p_ovl->b_active;
     return VLC_SUCCESS;
 }
@@ -741,7 +743,7 @@ static int exec_SetTextColor( filter_t *p_filter,
     if( p_ovl == NULL )
         return VLC_EGENERIC;
 
-    p_ovl->fontstyle.i_font_color = p_params->fontstyle.i_font_color;
+    p_ovl->p_fontstyle->i_font_color = p_params->fontstyle.i_font_color;
     p_sys->b_updated = p_ovl->b_active;
     return VLC_SUCCESS;
 }
@@ -757,7 +759,7 @@ static int exec_SetTextSize( filter_t *p_filter,
     if( p_ovl == NULL )
         return VLC_EGENERIC;
 
-    p_ovl->fontstyle.i_font_size = p_params->fontstyle.i_font_size;
+    p_ovl->p_fontstyle->i_font_size = p_params->fontstyle.i_font_size;
     p_sys->b_updated = p_ovl->b_active;
     return VLC_SUCCESS;
 }
diff --git a/modules/video_filter/marq.c b/modules/video_filter/marq.c
index c7528e4..64c99c1 100644
--- a/modules/video_filter/marq.c
+++ b/modules/video_filter/marq.c
@@ -201,8 +201,7 @@ static int CreateFilter( vlc_object_t *p_this )
     if( p_sys == NULL )
         return VLC_ENOMEM;
 
-    p_sys->p_style = malloc( sizeof( text_style_t ) );
-    memcpy( p_sys->p_style, &default_text_style, sizeof( text_style_t ) );
+    p_sys->p_style = textStyleInit( NULL ) ;
 
     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
                        p_filter->p_cfg );
@@ -239,6 +238,7 @@ static void DestroyFilter( vlc_object_t *p_this )
     filter_t *p_filter = (filter_t *)p_this;
     filter_sys_t *p_sys = p_filter->p_sys;
 
+    free( p_sys->p_style->psz_fontname ) ;
     free( p_sys->p_style );
     free( p_sys->psz_marquee );
 
@@ -320,7 +320,9 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
     p_spu->p_region->i_x = p_sys->i_xoff;
     p_spu->p_region->i_y = p_sys->i_yoff;
 
-    p_spu->p_region->p_style = p_sys->p_style;
+
+    //basos:: we should copy here as spu manages deletion from now and on
+    p_spu->p_region->p_style = textStyleInit( p_sys->p_style ) ;
 
 out:
     vlc_object_unlock( p_filter );
diff --git a/modules/video_filter/rss.c b/modules/video_filter/rss.c
index 516a28e..f4d4740 100644
--- a/modules/video_filter/rss.c
+++ b/modules/video_filter/rss.c
@@ -272,7 +272,7 @@ static int CreateFilter( vlc_object_t *p_this )
     }
     p_sys->psz_marquee[p_sys->i_length] = '\0';
 
-    p_sys->p_style = malloc( sizeof( text_style_t ));
+    p_sys->p_style = textStyleInit( NULL ) ;
     if( p_sys->p_style == NULL )
     {
         free( p_sys->psz_marquee );
@@ -282,7 +282,6 @@ static int CreateFilter( vlc_object_t *p_this )
         free( p_sys );
         return VLC_ENOMEM;
     }
-    memcpy( p_sys->p_style, &default_text_style, sizeof( text_style_t ));
 
     p_sys->i_xoff = var_CreateGetInteger( p_filter, CFG_PREFIX "x" );
     p_sys->i_yoff = var_CreateGetInteger( p_filter, CFG_PREFIX "y" );
@@ -299,6 +298,7 @@ static int CreateFilter( vlc_object_t *p_this )
     if( FetchRSS( p_filter ) )
     {
         msg_Err( p_filter, "failed while fetching RSS ... too bad" );
+        free( p_sys->p_style->psz_fontname ) ;
         free( p_sys->p_style );
         free( p_sys->psz_marquee );
         vlc_mutex_unlock( &p_sys->lock );
@@ -311,6 +311,7 @@ static int CreateFilter( vlc_object_t *p_this )
 
     if( p_sys->i_feeds == 0 )
     {
+        free( p_sys->p_style->psz_fontname ) ;
         free( p_sys->p_style );
         free( p_sys->psz_marquee );
         vlc_mutex_unlock( &p_sys->lock );
@@ -323,6 +324,7 @@ static int CreateFilter( vlc_object_t *p_this )
     {
         if( p_sys->p_feeds[i_feed].i_items == 0 )
         {
+            free( p_sys->p_style->psz_fontname ) ;
             free( p_sys->p_style );
             free( p_sys->psz_marquee );
             FreeRSS( p_filter );
@@ -351,6 +353,7 @@ static void DestroyFilter( vlc_object_t *p_this )
 
     vlc_mutex_lock( &p_sys->lock );
 
+    free( p_sys->p_style->psz_fontname ) ;
     free( p_sys->p_style );
     free( p_sys->psz_marquee );
     free( p_sys->psz_urls );
@@ -530,7 +533,8 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
         p_spu->b_absolute = false;
     }
 
-    p_spu->p_region->p_style = p_sys->p_style;
+    // basos:: we should copy here as style mem is managed by spu from now and on
+    p_spu->p_region->p_style = textStyleInit( p_sys->p_style );
 
     if( p_feed->p_pic )
     {
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 1e614e4..4d50bd0 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -547,3 +547,4 @@ vout_UnlinkPicture
 vout_vaControlDefault
 __xml_Create
 xml_Delete
+textStyleInit
diff --git a/src/video_output/video_text.c b/src/video_output/video_text.c
index d0e7a0a..b12b8ad 100644
--- a/src/video_output/video_text.c
+++ b/src/video_output/video_text.c
@@ -73,10 +73,8 @@ int vout_ShowTextAbsolute( vout_thread_t *p_vout, int i_channel,
                            int i_flags, int i_hmargin, int i_vmargin,
                            mtime_t i_start, mtime_t i_stop )
 {
-    (void)p_style;
     subpicture_t *p_spu;
     video_format_t fmt;
-    /* (void)p_style; FIXME: <-- why ask for this if it's unused?!? */
 
     if( !psz_string ) return VLC_EGENERIC;
 
@@ -110,6 +108,8 @@ int vout_ShowTextAbsolute( vout_thread_t *p_vout, int i_channel,
     p_spu->p_region->i_align = i_flags & SUBPICTURE_ALIGN_MASK;
     p_spu->p_region->i_x = i_hmargin;
     p_spu->p_region->i_y = i_vmargin;
+    if ( p_style )
+        p_spu->p_region->p_style = textStyleInit( p_style );
 
     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
 
diff --git a/src/video_output/vout_subpictures.c b/src/video_output/vout_subpictures.c
index d20d4e0..1c0285d 100644
--- a/src/video_output/vout_subpictures.c
+++ b/src/video_output/vout_subpictures.c
@@ -52,6 +52,12 @@
 #define VLC_FOURCC_RGBA VLC_FOURCC('R','G','B','A')
 #define VLC_FOURCC_TEXT VLC_FOURCC('T','E','X','T')
 
+/* Default text style
+   DONT memcpy. Use p_style = textStyleInit( NULL ) to allocate default text style
+   and free(p_style->psz_fontname); free(p_style) to deallocate */
+static const text_style_t default_text_style = { NULL, 22, 0xffffff, 0xff, STYLE_OUTLINE,
+                0x000000, 0xff, 0x000000, 0xff, 0xffffff, 0x80, 0xffffff, 0xff, 1, 0, -1 };
+
 /* */
 typedef struct
 {
@@ -330,6 +336,26 @@ void spu_Attach( spu_t *p_spu, vlc_object_t *p_this, bool b_attach )
 }
 
 /**
+ * Initialize a text style struct managing all the memory stuff
+ * when p_style_in is NULL initialize with the default text style
+ */
+text_style_t* textStyleInit( text_style_t* p_style_in )
+{
+  text_style_t* p_style_out = NULL ;
+  p_style_out = malloc( sizeof( text_style_t ) ) ;
+  if ( !p_style_out)
+      return NULL ;
+  if ( p_style_in )
+      vlc_memcpy( p_style_out, p_style_in, sizeof( text_style_t ) );
+  else
+      vlc_memcpy( p_style_out, &default_text_style, sizeof( text_style_t ) ) ;
+  if ( p_style_out->psz_fontname )
+      p_style_out->psz_fontname = strdup( p_style_out->psz_fontname ) ;
+
+  return p_style_out ;
+}
+
+/**
  * Display a subpicture
  *
  * Remove the reservation flag of a subpicture, which will cause it to be
@@ -776,7 +802,11 @@ void subpicture_region_Delete( subpicture_region_t *p_region )
 
     free( p_region->psz_text );
     free( p_region->psz_html );
-    //free( p_region->p_style ); FIXME --fenrir plugin does not allocate the memory for it. I think it might lead to segfault, video renderer can live longer than the decoder
+    // basos: p_region owns a copy of p_style (with textStyleInit)
+    if ( p_region->p_style ) {
+        free (p_region->p_style->psz_fontname ) ;
+        free( p_region->p_style );
+    }
     free( p_region );
 }
 
-- 
1.5.4.3

