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