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