Qt, prefs: correctly set the tooltip on Font Selector
[vlc.git] / modules / text_renderer / svg.c
blob5e7de1be55684262afbc7c013ab2935068dc9ee4
1 /*****************************************************************************
2 * svg.c : Put SVG on the video
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_fs.h>
35 #include <vlc_filter.h>
37 #include <sys/types.h>
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #elif defined( WIN32 ) && !defined( UNDER_CE )
42 # include <io.h>
43 #endif
45 #include <glib.h>
46 #include <glib/gstdio.h>
47 #include <glib-object.h> /* g_object_unref( ) */
48 #include <librsvg/rsvg.h>
50 typedef struct svg_rendition_t svg_rendition_t;
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 );
62 /*****************************************************************************
63 * Module descriptor
64 *****************************************************************************/
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" )
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 ()
79 /**
80 Describes a SVG string to be displayed on the video
82 struct svg_rendition_t
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;
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 * );
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
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;
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 )
123 filter_t *p_filter = ( filter_t * )p_this;
124 filter_sys_t *p_sys;
126 /* Allocate structure */
127 p_sys = malloc( sizeof( filter_sys_t ) );
128 if( !p_sys )
129 return VLC_ENOMEM;
131 /* Initialize psz_template */
132 p_sys->psz_template = svg_GetTemplate( p_this );
133 if( !p_sys->psz_template )
135 free( p_sys );
136 return VLC_ENOMEM;
139 p_sys->i_width = p_filter->fmt_out.video.i_width;
140 p_sys->i_height = p_filter->fmt_out.video.i_height;
142 p_filter->pf_render_text = RenderText;
143 p_filter->pf_render_html = NULL;
144 p_filter->p_sys = p_sys;
146 /* MUST call this before any RSVG funcs */
147 rsvg_init( );
149 return VLC_SUCCESS;
152 static char *svg_GetTemplate( vlc_object_t *p_this )
154 filter_t *p_filter = ( filter_t * )p_this;
155 char *psz_filename;
156 char *psz_template;
157 FILE *file;
159 psz_filename = var_InheritString( p_filter, "svg-template-file" );
160 if( !psz_filename || (psz_filename[0] == 0) )
162 /* No filename. Use a default value. */
163 psz_template = NULL;
165 else
167 /* Read the template */
168 file = vlc_fopen( psz_filename, "rt" );
169 if( !file )
171 msg_Warn( p_this, "SVG template file %s does not exist.",
172 psz_filename );
173 psz_template = NULL;
175 else
177 struct stat s;
179 if( fstat( fileno( file ), &s ) )
181 /* Problem accessing file information. Should not
182 happen as we could open it. */
183 psz_template = NULL;
185 else
186 if( ((signed)s.st_size) < 0 )
188 msg_Err( p_this, "SVG template too big" );
189 psz_template = NULL;
191 else
193 msg_Dbg( p_this, "reading %ld bytes from template %s",
194 (unsigned long)s.st_size, psz_filename );
196 psz_template = calloc( 1, s.st_size + 42 );
197 if( !psz_template )
199 fclose( file );
200 free( psz_filename );
201 return NULL;
203 if(! fread( psz_template, s.st_size, 1, file ) )
205 msg_Dbg( p_this, "No data read from template." );
208 fclose( file );
211 free( psz_filename );
212 if( !psz_template )
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>" );
222 return psz_template;
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 )
232 filter_t *p_filter = ( filter_t * )p_this;
233 filter_sys_t *p_sys = p_filter->p_sys;
235 free( p_sys->psz_template );
236 free( p_sys );
237 rsvg_term( );
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 )
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;
255 if ( p_filter->p_sys->i_width != i_width ||
256 p_filter->p_sys->i_height != i_height )
258 svg_set_size( p_filter, i_width, i_height );
259 p_svg->p_rendition = NULL;
262 if( p_svg->p_rendition == NULL ) {
263 svg_RenderPicture( p_filter, p_svg );
264 if( ! p_svg->p_rendition )
266 msg_Err( p_filter, "Cannot render SVG" );
267 return VLC_EGENERIC;
270 i_width = gdk_pixbuf_get_width( p_svg->p_rendition );
271 i_height = gdk_pixbuf_get_height( p_svg->p_rendition );
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;
282 p_region->p_picture = picture_NewFromFormat( &fmt );
283 if( !p_region->p_picture )
284 return VLC_EGENERIC;
285 p_region->fmt = fmt;
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;
293 i_pitch = p_region->p_picture->Y_PITCH;
294 i_u_pitch = p_region->p_picture->U_PITCH;
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 );
301 p_pic = p_region->p_picture;
303 /* Copy the data */
305 /* This rendering code is in no way optimized. If someone has some time to
306 make it work faster or better, please do.
310 p_pixbuf->get_rowstride() is the number of bytes in a line.
311 p_pixbuf->get_height() is the number of lines.
313 The number of bytes of p_pixbuf->p_pixels is get_rowstride * get_height
315 if( has_alpha() ) {
316 alpha = pixels [ n_channels * ( y*rowstride + x ) + 3 ];
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 ];
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 );
329 This crashes the plugin (if !alpha). As there is always an alpha value,
330 it does not matter for the moment :
332 if( !alpha )
333 memset( p_a, 0xFF, i_pitch * p_region->fmt.i_height );
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 )
339 for( y = 0; y < i_height; y++ )
341 for( x = 0; x < i_width; x++ )
343 guchar *p_in;
344 int i_out;
346 p_in = &pixels_in[INDEX_IN( x, y )];
348 #define R( pixel ) *pixel
349 #define G( pixel ) *( pixel+1 )
350 #define B( pixel ) *( pixel+2 )
351 #define ALPHA( pixel ) *( pixel+3 )
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
358 if ( alpha ) {
359 i_out = INDEX_OUT( x, y );
361 p_pic->Y_PIXELS[i_out] = .299 * R( p_in ) + .587 * G( p_in ) + .114 * B( p_in );
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;
366 p_pic->A_PIXELS[i_out] = ALPHA( p_in );
371 return VLC_SUCCESS;
374 static void svg_set_size( filter_t *p_filter, int width, int height )
376 p_filter->p_sys->i_width = width;
377 p_filter->p_sys->i_height = height;
380 static void svg_SizeCallback( int *width, int *height, gpointer data )
382 filter_t *p_filter = data;
384 *width = p_filter->p_sys->i_width;
385 *height = p_filter->p_sys->i_height;
386 return;
389 static void svg_RenderPicture( filter_t *p_filter,
390 svg_rendition_t *p_svg )
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;
397 p_svg->p_rendition = NULL;
399 p_handle = rsvg_handle_new();
401 if( !p_handle )
403 msg_Err( p_filter, "Error creating SVG reader" );
404 return;
407 rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_filter, NULL );
409 if( ! rsvg_handle_write( p_handle,
410 ( guchar* )p_svg->psz_text, strlen( p_svg->psz_text ),
411 &error ) )
413 msg_Err( p_filter, "error while rendering SVG: %s", error->message );
414 g_object_unref( G_OBJECT( p_handle ) );
415 return;
418 if( ! rsvg_handle_close( p_handle, &error ) )
420 msg_Err( p_filter, "error while rendering SVG (close): %s", error->message );
421 g_object_unref( G_OBJECT( p_handle ) );
422 return;
425 p_svg->p_rendition = rsvg_handle_get_pixbuf( p_handle );
427 g_object_unref( G_OBJECT( p_handle ) );
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 )
435 filter_sys_t *p_sys = p_filter->p_sys;
436 svg_rendition_t *p_svg = NULL;
437 char *psz_string;
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;
444 p_svg = malloc( sizeof( svg_rendition_t ) );
445 if( !p_svg )
446 return VLC_ENOMEM;
448 p_region_out->i_x = p_region_in->i_x;
449 p_region_out->i_y = p_region_in->i_y;
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" ))
455 /* Data is SVG: duplicate */
456 p_svg->psz_text = strdup( psz_string );
457 if( !p_svg->psz_text )
459 free( p_svg );
460 return VLC_ENOMEM;
463 else
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 )
473 free( p_svg );
474 return VLC_ENOMEM;
476 snprintf( p_svg->psz_text, length, psz_template, psz_string );
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;
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 );
487 Render( p_filter, p_region_out, p_svg, p_svg->i_width, p_svg->i_height );
488 FreeString( p_svg );
490 return VLC_SUCCESS;
493 static void FreeString( svg_rendition_t *p_svg )
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 );