1 /*****************************************************************************
2 * svg.c : Put SVG on the video
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 the VideoLAN team
7 * Authors: Olivier Aubert <oaubert@lisi.univ-lyon1.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_charset.h>
37 #include <vlc_block.h>
38 #include <vlc_filter.h>
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
46 #elif defined( WIN32 ) && !defined( UNDER_CE )
51 #include <glib/gstdio.h>
52 #include <glib-object.h> /* g_object_unref( ) */
53 #include <librsvg-2/librsvg/rsvg.h>
55 typedef struct svg_rendition_t svg_rendition_t;
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Create ( vlc_object_t * );
61 static void Destroy ( vlc_object_t * );
62 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
63 subpicture_region_t *p_region_in );
64 static char *svg_GetTemplate( vlc_object_t *p_this );
66 /*****************************************************************************
68 *****************************************************************************/
70 #define TEMPLATE_TEXT N_( "SVG template file" )
71 #define TEMPLATE_LONGTEXT N_( "Location of a file holding a SVG template "\
72 "for automatic string conversion" )
75 set_category( CAT_INPUT )
76 set_category( SUBCAT_INPUT_SCODEC )
77 set_capability( "text renderer", 99 )
79 add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, true )
80 set_callbacks( Create, Destroy )
84 Describes a SVG string to be displayed on the video
86 struct svg_rendition_t
91 /** The SVG source associated with this subpicture */
93 /* The rendered SVG, as a GdkPixbuf */
94 GdkPixbuf *p_rendition;
97 static int Render( filter_t *, subpicture_region_t *, svg_rendition_t *, int, int);
98 static char *svg_GetTemplate ();
99 static void svg_set_size( filter_t *p_filter, int width, int height );
100 static void svg_SizeCallback ( int *width, int *height, gpointer data );
101 static void svg_RenderPicture ( filter_t *p_filter,
102 svg_rendition_t *p_svg );
103 static void FreeString( svg_rendition_t * );
105 /*****************************************************************************
106 * filter_sys_t: svg local data
107 *****************************************************************************
108 * This structure is part of the filter thread descriptor.
109 * It describes the svg specific properties of an output thread.
110 *****************************************************************************/
113 /* The SVG template used to convert strings */
115 /* Default size for rendering. Initialized to the output size. */
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 )
127 filter_t *p_filter = ( filter_t * )p_this;
130 /* Allocate structure */
131 p_sys = malloc( sizeof( filter_sys_t ) );
135 /* Initialize psz_template */
136 p_sys->psz_template = svg_GetTemplate( p_this );
137 if( !p_sys->psz_template )
143 p_sys->i_width = p_filter->fmt_out.video.i_width;
144 p_sys->i_height = p_filter->fmt_out.video.i_height;
146 p_filter->pf_render_text = RenderText;
147 p_filter->pf_render_html = NULL;
148 p_filter->p_sys = p_sys;
150 /* MUST call this before any RSVG funcs */
156 static char *svg_GetTemplate( vlc_object_t *p_this )
158 filter_t *p_filter = ( filter_t * )p_this;
163 psz_filename = config_GetPsz( p_filter, "svg-template-file" );
164 if( !psz_filename || (psz_filename[0] == 0) )
166 /* No filename. Use a default value. */
171 /* Read the template */
172 file = utf8_fopen( psz_filename, "rt" );
175 msg_Warn( p_this, "SVG template file %s does not exist.",
183 if( fstat( fileno( file ), &s ) )
185 /* Problem accessing file information. Should not
186 happen as we could open it. */
190 if( ((signed)s.st_size) < 0 )
192 msg_Err( p_this, "SVG template too big" );
197 msg_Dbg( p_this, "reading %ld bytes from template %s",
198 (unsigned long)s.st_size, psz_filename );
200 psz_template = malloc( s.st_size + 42 );
204 free( psz_filename );
207 memset( psz_template, 0, s.st_size + 1 );
208 if(! fread( psz_template, s.st_size, 1, file ) )
210 msg_Dbg( p_this, "No data read from template." );
216 free( psz_filename );
219 /* Either there was no file, or there was an error.
220 Use the default value */
221 psz_template = strdup( "<?xml version='1.0' encoding='UTF-8' standalone='no'?> \
222 <svg version='1' preserveAspectRatio='xMinYMin meet' viewBox='0 0 800 600'> \
223 <text x='10' y='560' fill='white' font-size='32' \
224 font-family='sans-serif'>%s</text></svg>" );
230 /*****************************************************************************
231 * Destroy: destroy Clone video thread output method
232 *****************************************************************************
233 * Clean up all data and library connections
234 *****************************************************************************/
235 static void Destroy( vlc_object_t *p_this )
237 filter_t *p_filter = ( filter_t * )p_this;
238 filter_sys_t *p_sys = p_filter->p_sys;
240 free( p_sys->psz_template );
245 /*****************************************************************************
246 * Render: render SVG in picture
247 *****************************************************************************/
248 static int Render( filter_t *p_filter, subpicture_region_t *p_region,
249 svg_rendition_t *p_svg, int i_width, int i_height )
252 uint8_t *p_y, *p_u, *p_v, *p_a;
253 int x, y, i_pitch, i_u_pitch;
254 guchar *pixels_in = NULL;
260 if ( p_filter->p_sys->i_width != i_width ||
261 p_filter->p_sys->i_height != i_height )
263 svg_set_size( p_filter, i_width, i_height );
264 p_svg->p_rendition = NULL;
267 if( p_svg->p_rendition == NULL ) {
268 svg_RenderPicture( p_filter, p_svg );
269 if( ! p_svg->p_rendition )
271 msg_Err( p_filter, "Cannot render SVG" );
275 i_width = gdk_pixbuf_get_width( p_svg->p_rendition );
276 i_height = gdk_pixbuf_get_height( p_svg->p_rendition );
278 /* Create a new subpicture region */
279 memset( &fmt, 0, sizeof( video_format_t ) );
280 fmt.i_chroma = VLC_CODEC_YUVA;
281 fmt.i_aspect = VOUT_ASPECT_FACTOR;
282 fmt.i_width = fmt.i_visible_width = i_width;
283 fmt.i_height = fmt.i_visible_height = i_height;
284 fmt.i_x_offset = fmt.i_y_offset = 0;
286 p_region->p_picture = picture_New( fmt.i_chroma, fmt.i_width, fmt.i_height, fmt.i_aspect );
287 if( !p_region->p_picture )
291 p_region->i_x = p_region->i_y = 0;
292 p_y = p_region->p_picture->Y_PIXELS;
293 p_u = p_region->p_picture->U_PIXELS;
294 p_v = p_region->p_picture->V_PIXELS;
295 p_a = p_region->p_picture->A_PIXELS;
297 i_pitch = p_region->p_picture->Y_PITCH;
298 i_u_pitch = p_region->p_picture->U_PITCH;
300 /* Initialize the region pixels (only the alpha will be changed later) */
301 memset( p_y, 0x00, i_pitch * p_region->fmt.i_height );
302 memset( p_u, 0x80, i_u_pitch * p_region->fmt.i_height );
303 memset( p_v, 0x80, i_u_pitch * p_region->fmt.i_height );
305 p_pic = p_region->p_picture;
309 /* This rendering code is in no way optimized. If someone has some time to
310 make it work faster or better, please do.
314 p_pixbuf->get_rowstride() is the number of bytes in a line.
315 p_pixbuf->get_height() is the number of lines.
317 The number of bytes of p_pixbuf->p_pixels is get_rowstride * get_height
320 alpha = pixels [ n_channels * ( y*rowstride + x ) + 3 ];
322 red = pixels [ n_channels * ( y*rowstride ) + x ) ];
323 green = pixels [ n_channels * ( y*rowstride ) + x ) + 1 ];
324 blue = pixels [ n_channels * ( y*rowstride ) + x ) + 2 ];
327 pixels_in = gdk_pixbuf_get_pixels( p_svg->p_rendition );
328 rowstride_in = gdk_pixbuf_get_rowstride( p_svg->p_rendition );
329 channels_in = gdk_pixbuf_get_n_channels( p_svg->p_rendition );
330 alpha = gdk_pixbuf_get_has_alpha( p_svg->p_rendition );
333 This crashes the plugin (if !alpha). As there is always an alpha value,
334 it does not matter for the moment :
337 memset( p_a, 0xFF, i_pitch * p_region->fmt.i_height );
340 #define INDEX_IN( x, y ) ( y * rowstride_in + x * channels_in )
341 #define INDEX_OUT( x, y ) ( y * i_pitch + x * p_pic->p[Y_PLANE].i_pixel_pitch )
343 for( y = 0; y < i_height; y++ )
345 for( x = 0; x < i_width; x++ )
350 p_in = &pixels_in[INDEX_IN( x, y )];
352 #define R( pixel ) *pixel
353 #define G( pixel ) *( pixel+1 )
354 #define B( pixel ) *( pixel+2 )
355 #define ALPHA( pixel ) *( pixel+3 )
357 /* From http://www.geocrawler.com/archives/3/8263/2001/6/0/6020594/ :
358 Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
359 U = -0.1687 * r - 0.3313 * g + 0.5 * b + 128
360 V = 0.5 * r - 0.4187 * g - 0.0813 * b + 128
363 i_out = INDEX_OUT( x, y );
365 p_pic->Y_PIXELS[i_out] = .299 * R( p_in ) + .587 * G( p_in ) + .114 * B( p_in );
367 p_pic->U_PIXELS[i_out] = -.1687 * R( p_in ) - .3313 * G( p_in ) + .5 * B( p_in ) + 128;
368 p_pic->V_PIXELS[i_out] = .5 * R( p_in ) - .4187 * G( p_in ) - .0813 * B( p_in ) + 128;
370 p_pic->A_PIXELS[i_out] = ALPHA( p_in );
378 static void svg_set_size( filter_t *p_filter, int width, int height )
380 p_filter->p_sys->i_width = width;
381 p_filter->p_sys->i_height = height;
384 static void svg_SizeCallback( int *width, int *height, gpointer data )
386 filter_t *p_filter = data;
388 *width = p_filter->p_sys->i_width;
389 *height = p_filter->p_sys->i_height;
393 static void svg_RenderPicture( filter_t *p_filter,
394 svg_rendition_t *p_svg )
396 /* Render the SVG string p_string->psz_text into a new picture_t
397 p_string->p_rendition with dimensions ( ->i_width, ->i_height ) */
398 RsvgHandle *p_handle;
399 GError *error = NULL;
401 p_svg->p_rendition = NULL;
403 p_handle = rsvg_handle_new();
407 msg_Err( p_filter, "Error creating SVG reader: %s", error->message );
411 rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_filter, NULL );
413 if( ! rsvg_handle_write( p_handle,
414 ( guchar* )p_svg->psz_text, strlen( p_svg->psz_text ),
417 msg_Err( p_filter, "error while rendering SVG: %s", error->message );
418 g_object_unref( G_OBJECT( p_handle ) );
422 if( ! rsvg_handle_close( p_handle, &error ) )
424 msg_Err( p_filter, "error while rendering SVG (close): %s", error->message );
425 g_object_unref( G_OBJECT( p_handle ) );
429 p_svg->p_rendition = rsvg_handle_get_pixbuf( p_handle );
431 g_object_unref( G_OBJECT( p_handle ) );
435 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
436 subpicture_region_t *p_region_in )
438 filter_sys_t *p_sys = p_filter->p_sys;
439 svg_rendition_t *p_svg = NULL;
443 if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
444 psz_string = p_region_in->psz_text;
445 if( !psz_string || !*psz_string ) return VLC_EGENERIC;
447 p_svg = malloc( sizeof( svg_rendition_t ) );
451 p_region_out->i_x = p_region_in->i_x;
452 p_region_out->i_y = p_region_in->i_y;
454 /* Check if the data is SVG or pure text. In the latter case,
455 convert the text to SVG. FIXME: find a better test */
456 if( strstr( psz_string, "<svg" ))
458 /* Data is SVG: duplicate */
459 p_svg->psz_text = strdup( psz_string );
460 if( !p_svg->psz_text )
468 /* Data is text. Convert to SVG */
469 /* FIXME: handle p_style attributes */
471 char* psz_template = p_sys->psz_template;
472 length = strlen( psz_string ) + strlen( psz_template ) + 42;
473 p_svg->psz_text = malloc( length + 1 );
474 if( !p_svg->psz_text )
479 memset( p_svg->psz_text, 0, length + 1 );
480 snprintf( p_svg->psz_text, length, psz_template, psz_string );
482 p_svg->i_width = p_sys->i_width;
483 p_svg->i_height = p_sys->i_height;
484 p_svg->i_chroma = VLC_CODEC_YUVA;
487 The input data is stored in the p_string structure,
488 and the function updates the p_rendition attribute. */
489 svg_RenderPicture( p_filter, p_svg );
491 Render( p_filter, p_region_out, p_svg, p_svg->i_width, p_svg->i_height );
497 static void FreeString( svg_rendition_t *p_svg )
499 free( p_svg->psz_text );
500 /* p_svg->p_rendition is a GdkPixbuf, and its allocation is
501 managed through ref. counting */
502 if( p_svg->p_rendition )
503 g_object_unref( p_svg->p_rendition );