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