]> git.sesse.net Git - vlc/blob - modules/misc/svg.c
svg: quick fix for out-of-tree compilation
[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 #else
31 # define N_(str) (str)
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_charset.h>
37 #include <vlc_vout.h>
38 #include <vlc_osd.h>
39 #include <vlc_block.h>
40 #include <vlc_filter.h>
41
42 #ifdef HAVE_SYS_TYPES_H
43 #   include <sys/types.h>
44 #endif
45
46 #ifdef HAVE_UNISTD_H
47 #    include <unistd.h>
48 #elif defined( WIN32 ) && !defined( UNDER_CE )
49 #   include <io.h>
50 #endif
51
52 #include <glib.h>
53 #include <glib/gstdio.h>
54 #include <glib-object.h>                                  /* g_object_unref( ) */
55 #include <librsvg-2/librsvg/rsvg.h>
56
57 typedef struct svg_rendition_t svg_rendition_t;
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static int  Create    ( vlc_object_t * );
63 static void Destroy   ( vlc_object_t * );
64 static int  RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
65                         subpicture_region_t *p_region_in );
66 static char *svg_GetTemplate( vlc_object_t *p_this );
67
68 /*****************************************************************************
69  * Module descriptor
70  *****************************************************************************/
71
72 #define TEMPLATE_TEXT N_( "SVG template file" )
73 #define TEMPLATE_LONGTEXT N_( "Location of a file holding a SVG template "\
74         "for automatic string conversion" )
75
76 vlc_module_begin ()
77     set_category( CAT_INPUT )
78     set_category( SUBCAT_INPUT_SCODEC )
79     set_capability( "text renderer", 99 )
80     add_shortcut( "svg" )
81     add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, true )
82     set_callbacks( Create, Destroy )
83 vlc_module_end ()
84
85 /**
86    Describes a SVG string to be displayed on the video
87 */
88 struct svg_rendition_t
89 {
90     int            i_width;
91     int            i_height;
92     int            i_chroma;
93     /** The SVG source associated with this subpicture */
94     char           *psz_text;
95     /* The rendered SVG, as a GdkPixbuf */
96     GdkPixbuf      *p_rendition;
97 };
98
99 static int Render( filter_t *, subpicture_region_t *, svg_rendition_t *, int, int);
100 static char *svg_GetTemplate ();
101 static void svg_set_size( filter_t *p_filter, int width, int height );
102 static void svg_SizeCallback  ( int *width, int *height, gpointer data );
103 static void svg_RenderPicture ( filter_t *p_filter,
104                                 svg_rendition_t *p_svg );
105 static void FreeString( svg_rendition_t * );
106
107 /*****************************************************************************
108  * filter_sys_t: svg local data
109  *****************************************************************************
110  * This structure is part of the filter thread descriptor.
111  * It describes the svg specific properties of an output thread.
112  *****************************************************************************/
113 struct filter_sys_t
114 {
115     /* The SVG template used to convert strings */
116     char          *psz_template;
117     /* Default size for rendering. Initialized to the output size. */
118     int            i_width;
119     int            i_height;
120 };
121
122 /*****************************************************************************
123  * Create: allocates svg video thread output method
124  *****************************************************************************
125  * This function allocates and initializes a  vout method.
126  *****************************************************************************/
127 static int Create( vlc_object_t *p_this )
128 {
129     filter_t *p_filter = ( filter_t * )p_this;
130     filter_sys_t *p_sys;
131
132     /* Allocate structure */
133     p_sys = malloc( sizeof( filter_sys_t ) );
134     if( !p_sys )
135         return VLC_ENOMEM;
136
137     /* Initialize psz_template */
138     p_sys->psz_template = svg_GetTemplate( p_this );
139     if( !p_sys->psz_template )
140     {
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
185             if( fstat( fileno( file ), &s ) )
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 );
203                 if( !psz_template )
204                 {
205                     fclose( file );
206                     free( psz_filename );
207                     return NULL;
208                 }
209                 memset( psz_template, 0, s.st_size + 1 );
210                 if(! fread( psz_template, s.st_size, 1, file ) )
211                 {
212                     msg_Dbg( p_this, "No data read from template." );
213                 }
214             }
215             fclose( file );
216         }
217     }
218     free( psz_filename );
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
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_CODEC_YUVA;
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
288     p_region->p_picture = picture_New( fmt.i_chroma, fmt.i_width, fmt.i_height, fmt.i_aspect );
289     if( !p_region->p_picture )
290         return VLC_EGENERIC;
291     p_region->fmt = fmt;
292
293     p_region->i_x = p_region->i_y = 0;
294     p_y = p_region->p_picture->Y_PIXELS;
295     p_u = p_region->p_picture->U_PIXELS;
296     p_v = p_region->p_picture->V_PIXELS;
297     p_a = p_region->p_picture->A_PIXELS;
298
299     i_pitch = p_region->p_picture->Y_PITCH;
300     i_u_pitch = p_region->p_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->p_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", 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", 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 = malloc( sizeof( svg_rendition_t ) );
450     if( !p_svg )
451         return VLC_ENOMEM;
452
453     p_region_out->i_x = p_region_in->i_x;
454     p_region_out->i_y = p_region_in->i_y;
455
456     /* Check if the data is SVG or pure text. In the latter case,
457        convert the text to SVG. FIXME: find a better test */
458     if( strstr( psz_string, "<svg" ))
459     {
460         /* Data is SVG: duplicate */
461         p_svg->psz_text = strdup( psz_string );
462         if( !p_svg->psz_text )
463         {
464             free( p_svg );
465             return VLC_ENOMEM;
466         }
467     }
468     else
469     {
470         /* Data is text. Convert to SVG */
471         /* FIXME: handle p_style attributes */
472         int length;
473         char* psz_template = p_sys->psz_template;
474         length = strlen( psz_string ) + strlen( psz_template ) + 42;
475         p_svg->psz_text = malloc( length + 1 );
476         if( !p_svg->psz_text )
477         {
478             free( p_svg );
479             return VLC_ENOMEM;
480         }
481         memset( p_svg->psz_text, 0, length + 1 );
482         snprintf( p_svg->psz_text, length, psz_template, psz_string );
483     }
484     p_svg->i_width = p_sys->i_width;
485     p_svg->i_height = p_sys->i_height;
486     p_svg->i_chroma = VLC_CODEC_YUVA;
487
488     /* Render the SVG.
489        The input data is stored in the p_string structure,
490        and the function updates the p_rendition attribute. */
491     svg_RenderPicture( p_filter, p_svg );
492
493     Render( p_filter, p_region_out, p_svg, p_svg->i_width, p_svg->i_height );
494     FreeString( p_svg );
495
496     return VLC_SUCCESS;
497 }
498
499 static void FreeString( svg_rendition_t *p_svg )
500 {
501     free( p_svg->psz_text );
502     /* p_svg->p_rendition is a GdkPixbuf, and its allocation is
503        managed through ref. counting */
504     if( p_svg->p_rendition )
505         g_object_unref( p_svg->p_rendition );
506     free( p_svg );
507 }