]> git.sesse.net Git - vlc/blob - modules/misc/svg.c
Fix memleak.
[vlc] / modules / misc / svg.c
1 /*****************************************************************************
2  * svg.c : Put SVG on the video
3  *****************************************************************************
4  * Copyright (C) 2002, 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Aubert <oaubert@lisi.univ-lyon1.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option ) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc( ), free( ) */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc_charset.h>
32 #include <vlc_vout.h>
33 #include <vlc_osd.h>
34 #include <vlc_block.h>
35 #include <vlc_filter.h>
36
37 #ifdef HAVE_SYS_TYPES_H
38 #   include <sys/types.h>
39 #endif
40
41 #ifdef HAVE_UNISTD_H
42 #    include <unistd.h>
43 #elif defined( WIN32 ) && !defined( UNDER_CE )
44 #   include <io.h>
45 #endif
46
47 #include <glib.h>
48 #include <glib/gstdio.h>
49 #include <glib-object.h>                                  /* g_object_unref( ) */
50 #include <librsvg-2/librsvg/rsvg.h>
51
52 typedef struct svg_rendition_t svg_rendition_t;
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 static int  Create    ( vlc_object_t * );
58 static void Destroy   ( vlc_object_t * );
59 static int  RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
60                         subpicture_region_t *p_region_in );
61 static char *svg_GetTemplate( vlc_object_t *p_this );
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66
67 #define TEMPLATE_TEXT N_( "SVG template file" )
68 #define TEMPLATE_LONGTEXT N_( "Location of a file holding a SVG template "\
69         "for automatic string conversion" )
70
71 vlc_module_begin();
72  set_category( CAT_INPUT);
73  set_category( SUBCAT_INPUT_SCODEC );
74  set_capability( "text renderer", 99 );
75  add_shortcut( "svg" );
76  add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, VLC_TRUE );
77  set_callbacks( Create, Destroy );
78 vlc_module_end();
79
80 /**
81    Describes a SVG string to be displayed on the video
82 */
83 struct svg_rendition_t
84 {
85     int            i_width;
86     int            i_height;
87     int            i_chroma;
88     /** The SVG source associated with this subpicture */
89     char           *psz_text;
90     /* The rendered SVG, as a GdkPixbuf */
91     GdkPixbuf      *p_rendition;
92 };
93
94 static int Render( filter_t *, subpicture_region_t *, svg_rendition_t *, int, int);
95 static char *svg_GetTemplate ();
96 static void svg_set_size( filter_t *p_filter, int width, int height );
97 static void svg_SizeCallback  ( int *width, int *height, gpointer data );
98 static void svg_RenderPicture ( filter_t *p_filter,
99                                 svg_rendition_t *p_svg );
100 static void FreeString( svg_rendition_t * );
101
102 /*****************************************************************************
103  * filter_sys_t: svg local data
104  *****************************************************************************
105  * This structure is part of the filter thread descriptor.
106  * It describes the svg specific properties of an output thread.
107  *****************************************************************************/
108 struct filter_sys_t
109 {
110     /* The SVG template used to convert strings */
111     char          *psz_template;
112     /* Default size for rendering. Initialized to the output size. */
113     int            i_width;
114     int            i_height;
115     vlc_mutex_t   *lock;
116 };
117
118 /*****************************************************************************
119  * Create: allocates svg video thread output method
120  *****************************************************************************
121  * This function allocates and initializes a  vout method.
122  *****************************************************************************/
123 static int Create( vlc_object_t *p_this )
124 {
125     filter_t *p_filter = ( filter_t * )p_this;
126     filter_sys_t *p_sys;
127
128     /* Allocate structure */
129     p_sys = malloc( sizeof( filter_sys_t ) );
130     if( !p_sys )
131     {
132         msg_Err( p_filter, "out of memory" );
133         return VLC_ENOMEM;
134     }
135
136     /* Initialize psz_template */
137     p_sys->psz_template = svg_GetTemplate( p_this );
138     if( !p_sys->psz_template )
139     {
140         msg_Err( p_filter, "out of memory" );
141         free( p_sys );
142         return VLC_ENOMEM;
143     }
144
145     p_sys->i_width = p_filter->fmt_out.video.i_width;
146     p_sys->i_height = p_filter->fmt_out.video.i_height;
147
148     p_filter->pf_render_text = RenderText;
149     p_filter->pf_render_html = NULL;
150     p_filter->p_sys = p_sys;
151
152     /* MUST call this before any RSVG funcs */
153     rsvg_init( );
154
155     return VLC_SUCCESS;
156 }
157
158 static char *svg_GetTemplate( vlc_object_t *p_this )
159 {
160     filter_t *p_filter = ( filter_t * )p_this;
161     char *psz_filename;
162     char *psz_template;
163     FILE *file;
164
165     psz_filename = config_GetPsz( p_filter, "svg-template-file" );
166     if( !psz_filename || (psz_filename[0] == 0) )
167     {
168         /* No filename. Use a default value. */
169         psz_template = NULL;
170     }
171     else
172     {
173         /* Read the template */
174         file = utf8_fopen( psz_filename, "rt" );
175         if( !file )
176         {
177             msg_Warn( p_this, "SVG template file %s does not exist.",
178                                          psz_filename );
179             psz_template = NULL;
180         }
181         else
182         {
183             struct stat s;
184             int i_ret;
185
186             i_ret = utf8_stat( psz_filename, &s );
187             if( i_ret )
188             {
189                 /* Problem accessing file information. Should not
190                    happen as we could open it. */
191                 psz_template = NULL;
192             }
193             else
194             if( ((signed)s.st_size) < 0 )
195             {
196                 msg_Err( p_this, "SVG template too big" );
197                 psz_template = NULL;
198             }
199             else
200             {
201                 msg_Dbg( p_this, "reading %ld bytes from template %s",
202                          (unsigned long)s.st_size, psz_filename );
203
204                 psz_template = malloc( ( s.st_size + 42 ) * sizeof( char ) );
205                 if( !psz_template )
206                 {
207                     msg_Err( p_filter, "out of memory" );
208                     return NULL;
209                 }
210                 memset( psz_template, 0, s.st_size + 1 );
211                 fread( psz_template, s.st_size, 1, file );
212                 fclose( file );
213             }
214         }
215     }
216     if( !psz_template )
217     {
218         /* Either there was no file, or there was an error.
219            Use the default value */
220         psz_template = strdup( "<?xml version='1.0' encoding='UTF-8' standalone='no'?> \
221 <svg version='1' preserveAspectRatio='xMinYMin meet' viewBox='0 0 800 600'> \
222   <text x='10' y='560' fill='white' font-size='32'  \
223         font-family='sans-serif'>%s</text></svg>" );
224     }
225
226     return psz_template;
227 }
228
229 /*****************************************************************************
230  * Destroy: destroy Clone video thread output method
231  *****************************************************************************
232  * Clean up all data and library connections
233  *****************************************************************************/
234 static void Destroy( vlc_object_t *p_this )
235 {
236     filter_t *p_filter = ( filter_t * )p_this;
237     filter_sys_t *p_sys = p_filter->p_sys;
238
239     free( p_sys->psz_template );
240     free( p_sys );
241     rsvg_term( );
242 }
243
244 /*****************************************************************************
245  * Render: render SVG in picture
246  *****************************************************************************/
247 static int Render( filter_t *p_filter, subpicture_region_t *p_region,
248                    svg_rendition_t *p_svg, int i_width, int i_height )
249 {
250     video_format_t fmt;
251     uint8_t *p_y, *p_u, *p_v, *p_a;
252     int x, y, i_pitch, i_u_pitch;
253     guchar *pixels_in = NULL;
254     int rowstride_in;
255     int channels_in;
256     int alpha;
257     picture_t *p_pic;
258     subpicture_region_t *p_region_tmp;
259
260     if ( p_filter->p_sys->i_width != i_width ||
261          p_filter->p_sys->i_height != i_height )
262     {
263         svg_set_size( p_filter, i_width, i_height );
264         p_svg->p_rendition = NULL;
265     }
266
267     if( p_svg->p_rendition == NULL ) {
268         svg_RenderPicture( p_filter, p_svg );
269         if( ! p_svg->p_rendition )
270         {
271             msg_Err( p_filter, "Cannot render SVG" );
272             return VLC_EGENERIC;
273         }
274     }
275     i_width = gdk_pixbuf_get_width( p_svg->p_rendition );
276     i_height = gdk_pixbuf_get_height( p_svg->p_rendition );
277
278     /* Create a new subpicture region */
279     memset( &fmt, 0, sizeof( video_format_t ) );
280     fmt.i_chroma = VLC_FOURCC( 'Y','U','V','A' );
281     fmt.i_aspect = VOUT_ASPECT_FACTOR;
282     fmt.i_width = fmt.i_visible_width = i_width;
283     fmt.i_height = fmt.i_visible_height = i_height;
284     fmt.i_x_offset = fmt.i_y_offset = 0;
285     p_region_tmp = spu_CreateRegion( p_filter, &fmt );
286     if( !p_region_tmp )
287     {
288         msg_Err( p_filter, "cannot allocate SPU region" );
289         return VLC_EGENERIC;
290     }
291     p_region->fmt = p_region_tmp->fmt;
292     p_region->picture = p_region_tmp->picture;
293     free( p_region_tmp );
294
295     p_region->i_x = p_region->i_y = 0;
296     p_y = p_region->picture.Y_PIXELS;
297     p_u = p_region->picture.U_PIXELS;
298     p_v = p_region->picture.V_PIXELS;
299     p_a = p_region->picture.A_PIXELS;
300
301     i_pitch = p_region->picture.Y_PITCH;
302     i_u_pitch = p_region->picture.U_PITCH;
303
304     /* Initialize the region pixels (only the alpha will be changed later) */
305     memset( p_y, 0x00, i_pitch * p_region->fmt.i_height );
306     memset( p_u, 0x80, i_u_pitch * p_region->fmt.i_height );
307     memset( p_v, 0x80, i_u_pitch * p_region->fmt.i_height );
308
309     p_pic = &p_region->picture;
310
311     /* Copy the data */
312
313     /* This rendering code is in no way optimized. If someone has some time to
314        make it work faster or better, please do.
315     */
316
317     /*
318       p_pixbuf->get_rowstride() is the number of bytes in a line.
319       p_pixbuf->get_height() is the number of lines.
320
321       The number of bytes of p_pixbuf->p_pixels is get_rowstride * get_height
322
323       if( has_alpha() ) {
324       alpha = pixels [ n_channels * ( y*rowstride + x ) + 3 ];
325       }
326       red   = pixels [ n_channels * ( y*rowstride ) + x ) ];
327       green = pixels [ n_channels * ( y*rowstride ) + x ) + 1 ];
328       blue  = pixels [ n_channels * ( y*rowstride ) + x ) + 2 ];
329     */
330
331     pixels_in = gdk_pixbuf_get_pixels( p_svg->p_rendition );
332     rowstride_in = gdk_pixbuf_get_rowstride( p_svg->p_rendition );
333     channels_in = gdk_pixbuf_get_n_channels( p_svg->p_rendition );
334     alpha = gdk_pixbuf_get_has_alpha( p_svg->p_rendition );
335
336     /*
337       This crashes the plugin (if !alpha). As there is always an alpha value,
338       it does not matter for the moment :
339
340     if( !alpha )
341       memset( p_a, 0xFF, i_pitch * p_region->fmt.i_height );
342     */
343
344 #define INDEX_IN( x, y ) ( y * rowstride_in + x * channels_in )
345 #define INDEX_OUT( x, y ) ( y * i_pitch + x * p_pic->p[Y_PLANE].i_pixel_pitch )
346
347     for( y = 0; y < i_height; y++ )
348     {
349         for( x = 0; x < i_width; x++ )
350         {
351             guchar *p_in;
352             int i_out;
353
354             p_in = &pixels_in[INDEX_IN( x, y )];
355
356 #define R( pixel ) *pixel
357 #define G( pixel ) *( pixel+1 )
358 #define B( pixel ) *( pixel+2 )
359 #define ALPHA( pixel ) *( pixel+3 )
360
361             /* From http://www.geocrawler.com/archives/3/8263/2001/6/0/6020594/ :
362                Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
363                U = -0.1687 * r  - 0.3313 * g + 0.5 * b + 128
364                V = 0.5   * r - 0.4187 * g - 0.0813 * b + 128
365             */
366             if ( alpha ) {
367                 i_out = INDEX_OUT( x, y );
368
369                 p_pic->Y_PIXELS[i_out] = .299 * R( p_in ) + .587 * G( p_in ) + .114 * B( p_in );
370
371                 p_pic->U_PIXELS[i_out] = -.1687 * R( p_in ) - .3313 * G( p_in ) + .5 * B( p_in ) + 128;
372                 p_pic->V_PIXELS[i_out] = .5 * R( p_in ) - .4187 * G( p_in ) - .0813 * B( p_in ) + 128;
373
374                 p_pic->A_PIXELS[i_out] = ALPHA( p_in );
375             }
376         }
377     }
378
379     return VLC_SUCCESS;
380 }
381
382 static void svg_set_size( filter_t *p_filter, int width, int height )
383 {
384   p_filter->p_sys->i_width = width;
385   p_filter->p_sys->i_height = height;
386 }
387
388 static void svg_SizeCallback( int *width, int *height, gpointer data )
389 {
390     filter_t *p_filter = data;
391
392     *width = p_filter->p_sys->i_width;
393     *height = p_filter->p_sys->i_height;
394     return;
395 }
396
397 static void svg_RenderPicture( filter_t *p_filter,
398                                svg_rendition_t *p_svg )
399 {
400     /* Render the SVG string p_string->psz_text into a new picture_t
401        p_string->p_rendition with dimensions ( ->i_width, ->i_height ) */
402     RsvgHandle *p_handle;
403     GError *error = NULL;
404
405     p_svg->p_rendition = NULL;
406
407     p_handle = rsvg_handle_new();
408
409     if( !p_handle )
410     {
411         msg_Err( p_filter, "Error creating SVG reader: %s", error->message );
412         return;
413     }
414
415     rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_filter, NULL );
416
417     if( ! rsvg_handle_write( p_handle,
418                              ( guchar* )p_svg->psz_text, strlen( p_svg->psz_text ),
419                              &error ) ) 
420     {
421         msg_Err( p_filter, "error while rendering SVG: %s\n", error->message );
422         g_object_unref( G_OBJECT( p_handle ) );
423         return;
424     }
425
426     if( ! rsvg_handle_close( p_handle, &error ) )
427     {
428         msg_Err( p_filter, "error while rendering SVG (close): %s\n", error->message );
429         g_object_unref( G_OBJECT( p_handle ) );
430         return;
431     }
432
433     p_svg->p_rendition = rsvg_handle_get_pixbuf( p_handle );
434
435     g_object_unref( G_OBJECT( p_handle ) );
436 }
437
438
439 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
440                        subpicture_region_t *p_region_in )
441 {
442     filter_sys_t *p_sys = p_filter->p_sys;
443     svg_rendition_t *p_svg = NULL;
444     char *psz_string;
445
446     /* Sanity check */
447     if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
448     psz_string = p_region_in->psz_text;
449     if( !psz_string || !*psz_string ) return VLC_EGENERIC;
450
451     p_svg = ( svg_rendition_t * )malloc( sizeof( svg_rendition_t ) );
452     if( !p_svg )
453     {
454         msg_Err( p_filter, "out of memory" );
455         return VLC_ENOMEM;
456     }
457
458     p_region_out->i_x = p_region_in->i_x;
459     p_region_out->i_y = p_region_in->i_y;
460
461     /* Check if the data is SVG or pure text. In the latter case,
462        convert the text to SVG. FIXME: find a better test */
463     if( strstr( psz_string, "<svg" ))
464     {
465         /* Data is SVG: duplicate */
466         p_svg->psz_text = strdup( psz_string );
467         if( !p_svg->psz_text )
468         {
469             msg_Err( p_filter, "out of memory" );
470             free( p_svg );
471             return VLC_ENOMEM;
472         }
473     }
474     else
475     {
476         /* Data is text. Convert to SVG */
477         /* FIXME: handle p_style attributes */
478         int length;
479         char* psz_template = p_sys->psz_template;
480         length = strlen( psz_string ) + strlen( psz_template ) + 42;
481         p_svg->psz_text = malloc( ( length + 1 ) * sizeof( char ) );
482         if( !p_svg->psz_text )
483         {
484             msg_Err( p_filter, "out of memory" );
485             free( p_svg );
486             return VLC_ENOMEM;
487         }
488         memset( p_svg->psz_text, 0, length + 1 );
489         snprintf( p_svg->psz_text, length, psz_template, psz_string );
490     }
491     p_svg->i_width = p_sys->i_width;
492     p_svg->i_height = p_sys->i_height;
493     p_svg->i_chroma = VLC_FOURCC( 'Y','U','V','A' );
494
495     /* Render the SVG.
496        The input data is stored in the p_string structure,
497        and the function updates the p_rendition attribute. */
498     svg_RenderPicture( p_filter, p_svg );
499
500     Render( p_filter, p_region_out, p_svg, p_svg->i_width, p_svg->i_height );
501     FreeString( p_svg );
502
503     return VLC_SUCCESS;
504 }
505
506 static void FreeString( svg_rendition_t *p_svg )
507 {
508     if( p_svg->psz_text )
509         free( p_svg->psz_text );
510     /* p_svg->p_rendition is a GdkPixbuf, and its allocation is
511        managed through ref. counting */
512     if( p_svg->p_rendition )
513         g_object_unref( p_svg->p_rendition );
514     free( p_svg );
515 }