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