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