1 /*****************************************************************************
2 * svg.c : Put SVG on the video
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VLC authors and VideoLAN
7 * Authors: Olivier Aubert <oaubert@lisi.univ-lyon1.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
37 #include <sys/types.h>
41 #include <glib/gstdio.h>
42 #include <glib-object.h> /* g_object_unref( ) */
43 #include <librsvg/rsvg.h>
45 typedef struct svg_rendition_t svg_rendition_t;
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Create ( vlc_object_t * );
51 static void Destroy ( vlc_object_t * );
52 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
53 subpicture_region_t *p_region_in,
54 const vlc_fourcc_t * );
55 static char *svg_GetTemplate( vlc_object_t *p_this );
57 /*****************************************************************************
59 *****************************************************************************/
61 #define TEMPLATE_TEXT N_( "SVG template file" )
62 #define TEMPLATE_LONGTEXT N_( "Location of a file holding a SVG template "\
63 "for automatic string conversion" )
66 set_category( CAT_INPUT )
67 set_category( SUBCAT_INPUT_SCODEC )
68 set_capability( "text renderer", 99 )
70 add_string( "svg-template-file", "", TEMPLATE_TEXT, TEMPLATE_LONGTEXT, true )
71 set_callbacks( Create, Destroy )
75 Describes a SVG string to be displayed on the video
77 struct svg_rendition_t
82 /** The SVG source associated with this subpicture */
84 /* The rendered SVG, as a GdkPixbuf */
85 GdkPixbuf *p_rendition;
88 static int Render( filter_t *, subpicture_region_t *, svg_rendition_t *, int, int);
89 static char *svg_GetTemplate ();
90 static void svg_set_size( filter_t *p_filter, int width, int height );
91 static void svg_SizeCallback ( int *width, int *height, gpointer data );
92 static void svg_RenderPicture ( filter_t *p_filter,
93 svg_rendition_t *p_svg );
94 static void FreeString( svg_rendition_t * );
96 /*****************************************************************************
97 * filter_sys_t: svg local data
98 *****************************************************************************
99 * This structure is part of the filter thread descriptor.
100 * It describes the svg specific properties of an output thread.
101 *****************************************************************************/
104 /* The SVG template used to convert strings */
106 /* Default size for rendering. Initialized to the output size. */
111 /*****************************************************************************
112 * Create: allocates svg video thread output method
113 *****************************************************************************
114 * This function allocates and initializes a vout method.
115 *****************************************************************************/
116 static int Create( vlc_object_t *p_this )
118 filter_t *p_filter = ( filter_t * )p_this;
121 /* Allocate structure */
122 p_sys = malloc( sizeof( filter_sys_t ) );
126 /* Initialize psz_template */
127 p_sys->psz_template = svg_GetTemplate( p_this );
128 if( !p_sys->psz_template )
134 p_sys->i_width = p_filter->fmt_out.video.i_width;
135 p_sys->i_height = p_filter->fmt_out.video.i_height;
137 p_filter->pf_render_text = RenderText;
138 p_filter->pf_render_html = NULL;
139 p_filter->p_sys = p_sys;
141 /* MUST call this before any RSVG funcs */
147 static char *svg_GetTemplate( vlc_object_t *p_this )
149 filter_t *p_filter = ( filter_t * )p_this;
154 psz_filename = var_InheritString( p_filter, "svg-template-file" );
155 if( !psz_filename || (psz_filename[0] == 0) )
157 /* No filename. Use a default value. */
162 /* Read the template */
163 file = vlc_fopen( psz_filename, "rt" );
166 msg_Warn( p_this, "SVG template file %s does not exist.",
174 if( fstat( fileno( file ), &s ) )
176 /* Problem accessing file information. Should not
177 happen as we could open it. */
181 if( ((signed)s.st_size) < 0 )
183 msg_Err( p_this, "SVG template too big" );
188 msg_Dbg( p_this, "reading %ld bytes from template %s",
189 (unsigned long)s.st_size, psz_filename );
191 psz_template = calloc( 1, s.st_size + 42 );
195 free( psz_filename );
198 if(! fread( psz_template, s.st_size, 1, file ) )
200 msg_Dbg( p_this, "No data read from template." );
206 free( psz_filename );
209 /* Either there was no file, or there was an error.
210 Use the default value */
211 psz_template = strdup( "<?xml version='1.0' encoding='UTF-8' standalone='no'?> \
212 <svg version='1' preserveAspectRatio='xMinYMin meet' viewBox='0 0 800 600'> \
213 <text x='10' y='560' fill='white' font-size='32' \
214 font-family='sans-serif'>%s</text></svg>" );
220 /*****************************************************************************
221 * Destroy: destroy Clone video thread output method
222 *****************************************************************************
223 * Clean up all data and library connections
224 *****************************************************************************/
225 static void Destroy( vlc_object_t *p_this )
227 filter_t *p_filter = ( filter_t * )p_this;
228 filter_sys_t *p_sys = p_filter->p_sys;
230 free( p_sys->psz_template );
235 /*****************************************************************************
236 * Render: render SVG in picture
237 *****************************************************************************/
238 static int Render( filter_t *p_filter, subpicture_region_t *p_region,
239 svg_rendition_t *p_svg, int i_width, int i_height )
242 uint8_t *p_y, *p_u, *p_v, *p_a;
243 int x, y, i_pitch, i_u_pitch;
244 guchar *pixels_in = NULL;
250 if ( p_filter->p_sys->i_width != i_width ||
251 p_filter->p_sys->i_height != i_height )
253 svg_set_size( p_filter, i_width, i_height );
254 p_svg->p_rendition = NULL;
257 if( p_svg->p_rendition == NULL ) {
258 svg_RenderPicture( p_filter, p_svg );
259 if( ! p_svg->p_rendition )
261 msg_Err( p_filter, "Cannot render SVG" );
265 i_width = gdk_pixbuf_get_width( p_svg->p_rendition );
266 i_height = gdk_pixbuf_get_height( p_svg->p_rendition );
268 /* Create a new subpicture region */
269 memset( &fmt, 0, sizeof( video_format_t ) );
270 fmt.i_chroma = VLC_CODEC_YUVA;
271 fmt.i_width = fmt.i_visible_width = i_width;
272 fmt.i_height = fmt.i_visible_height = i_height;
273 fmt.i_x_offset = fmt.i_y_offset = 0;
277 p_region->p_picture = picture_NewFromFormat( &fmt );
278 if( !p_region->p_picture )
282 p_region->i_x = p_region->i_y = 0;
283 p_y = p_region->p_picture->Y_PIXELS;
284 p_u = p_region->p_picture->U_PIXELS;
285 p_v = p_region->p_picture->V_PIXELS;
286 p_a = p_region->p_picture->A_PIXELS;
288 i_pitch = p_region->p_picture->Y_PITCH;
289 i_u_pitch = p_region->p_picture->U_PITCH;
291 /* Initialize the region pixels (only the alpha will be changed later) */
292 memset( p_y, 0x00, i_pitch * p_region->fmt.i_height );
293 memset( p_u, 0x80, i_u_pitch * p_region->fmt.i_height );
294 memset( p_v, 0x80, i_u_pitch * p_region->fmt.i_height );
296 p_pic = p_region->p_picture;
300 /* This rendering code is in no way optimized. If someone has some time to
301 make it work faster or better, please do.
305 p_pixbuf->get_rowstride() is the number of bytes in a line.
306 p_pixbuf->get_height() is the number of lines.
308 The number of bytes of p_pixbuf->p_pixels is get_rowstride * get_height
311 alpha = pixels [ n_channels * ( y*rowstride + x ) + 3 ];
313 red = pixels [ n_channels * ( y*rowstride ) + x ) ];
314 green = pixels [ n_channels * ( y*rowstride ) + x ) + 1 ];
315 blue = pixels [ n_channels * ( y*rowstride ) + x ) + 2 ];
318 pixels_in = gdk_pixbuf_get_pixels( p_svg->p_rendition );
319 rowstride_in = gdk_pixbuf_get_rowstride( p_svg->p_rendition );
320 channels_in = gdk_pixbuf_get_n_channels( p_svg->p_rendition );
321 alpha = gdk_pixbuf_get_has_alpha( p_svg->p_rendition );
324 This crashes the plugin (if !alpha). As there is always an alpha value,
325 it does not matter for the moment :
328 memset( p_a, 0xFF, i_pitch * p_region->fmt.i_height );
331 #define INDEX_IN( x, y ) ( y * rowstride_in + x * channels_in )
332 #define INDEX_OUT( x, y ) ( y * i_pitch + x * p_pic->p[Y_PLANE].i_pixel_pitch )
334 for( y = 0; y < i_height; y++ )
336 for( x = 0; x < i_width; x++ )
341 p_in = &pixels_in[INDEX_IN( x, y )];
343 #define R( pixel ) *pixel
344 #define G( pixel ) *( pixel+1 )
345 #define B( pixel ) *( pixel+2 )
346 #define ALPHA( pixel ) *( pixel+3 )
348 /* From http://www.geocrawler.com/archives/3/8263/2001/6/0/6020594/ :
349 Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
350 U = -0.1687 * r - 0.3313 * g + 0.5 * b + 128
351 V = 0.5 * r - 0.4187 * g - 0.0813 * b + 128
354 i_out = INDEX_OUT( x, y );
356 p_pic->Y_PIXELS[i_out] = .299 * R( p_in ) + .587 * G( p_in ) + .114 * B( p_in );
358 p_pic->U_PIXELS[i_out] = -.1687 * R( p_in ) - .3313 * G( p_in ) + .5 * B( p_in ) + 128;
359 p_pic->V_PIXELS[i_out] = .5 * R( p_in ) - .4187 * G( p_in ) - .0813 * B( p_in ) + 128;
361 p_pic->A_PIXELS[i_out] = ALPHA( p_in );
369 static void svg_set_size( filter_t *p_filter, int width, int height )
371 p_filter->p_sys->i_width = width;
372 p_filter->p_sys->i_height = height;
375 static void svg_SizeCallback( int *width, int *height, gpointer data )
377 filter_t *p_filter = data;
379 *width = p_filter->p_sys->i_width;
380 *height = p_filter->p_sys->i_height;
384 static void svg_RenderPicture( filter_t *p_filter,
385 svg_rendition_t *p_svg )
387 /* Render the SVG string p_string->psz_text into a new picture_t
388 p_string->p_rendition with dimensions ( ->i_width, ->i_height ) */
389 RsvgHandle *p_handle;
390 GError *error = NULL;
392 p_svg->p_rendition = NULL;
394 p_handle = rsvg_handle_new();
398 msg_Err( p_filter, "Error creating SVG reader" );
402 rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_filter, NULL );
404 if( ! rsvg_handle_write( p_handle,
405 ( guchar* )p_svg->psz_text, strlen( p_svg->psz_text ),
408 msg_Err( p_filter, "error while rendering SVG: %s", error->message );
409 g_object_unref( G_OBJECT( p_handle ) );
413 if( ! rsvg_handle_close( p_handle, &error ) )
415 msg_Err( p_filter, "error while rendering SVG (close): %s", error->message );
416 g_object_unref( G_OBJECT( p_handle ) );
420 p_svg->p_rendition = rsvg_handle_get_pixbuf( p_handle );
422 g_object_unref( G_OBJECT( p_handle ) );
426 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
427 subpicture_region_t *p_region_in,
428 const vlc_fourcc_t *p_chroma_list )
430 VLC_UNUSED(p_chroma_list);
432 filter_sys_t *p_sys = p_filter->p_sys;
433 svg_rendition_t *p_svg = NULL;
437 if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
438 psz_string = p_region_in->psz_text;
439 if( !psz_string || !*psz_string ) return VLC_EGENERIC;
441 p_svg = malloc( sizeof( svg_rendition_t ) );
445 p_region_out->i_x = p_region_in->i_x;
446 p_region_out->i_y = p_region_in->i_y;
448 /* Check if the data is SVG or pure text. In the latter case,
449 convert the text to SVG. FIXME: find a better test */
450 if( strstr( psz_string, "<svg" ))
452 /* Data is SVG: duplicate */
453 p_svg->psz_text = strdup( psz_string );
454 if( !p_svg->psz_text )
462 /* Data is text. Convert to SVG */
463 /* FIXME: handle p_style attributes */
465 char* psz_template = p_sys->psz_template;
466 length = strlen( psz_string ) + strlen( psz_template ) + 42;
467 p_svg->psz_text = calloc( 1, length + 1 );
468 if( !p_svg->psz_text )
473 snprintf( p_svg->psz_text, length, psz_template, psz_string );
475 p_svg->i_width = p_sys->i_width;
476 p_svg->i_height = p_sys->i_height;
477 p_svg->i_chroma = VLC_CODEC_YUVA;
480 The input data is stored in the p_string structure,
481 and the function updates the p_rendition attribute. */
482 svg_RenderPicture( p_filter, p_svg );
484 Render( p_filter, p_region_out, p_svg, p_svg->i_width, p_svg->i_height );
490 static void FreeString( svg_rendition_t *p_svg )
492 free( p_svg->psz_text );
493 /* p_svg->p_rendition is a GdkPixbuf, and its allocation is
494 managed through ref. counting */
495 if( p_svg->p_rendition )
496 g_object_unref( p_svg->p_rendition );