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