1 /*****************************************************************************
2 * svg.c : Put SVG on the video
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
5 * $Id: svg.c,v 1.2 2003/07/23 17:26:56 oaubert Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc( ), free( ) */
30 #include <sys/types.h>
37 #include <librsvg-2/librsvg/rsvg.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
44 static void Render ( vout_thread_t *, picture_t *,
45 const subpicture_t * );
46 static subpicture_t *AddText ( vout_thread_t *p_vout, int i_channel,
47 char *psz_string, text_style_t *p_style, int i_flags,
48 int i_hmargin, int i_vmargin, mtime_t i_start,
50 static byte_t *svg_GetTemplate ();
51 static void svg_SizeCallback (int *width, int *height, gpointer data );
52 static void svg_RenderPicture (vout_thread_t *p_vout,
53 subpicture_sys_t *p_string );
54 static void FreeString( subpicture_t * );
56 /*****************************************************************************
58 *****************************************************************************/
60 #define TEMPLATE_TEXT N_( "SVG template file" )
61 #define TEMPLATE_LONGTEXT N_( "Location of a file holding a SVG template for automatic string conversion" )
64 set_category( CAT_VIDEO );
65 set_subcategory( SUBCAT_VIDEO_TEXT );
66 set_capability( "text renderer", 100 );
67 add_shortcut( "svg" );
68 add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, VLC_TRUE );
69 set_callbacks( Create, Destroy );
73 Describes a SVG string to be displayed on the video
75 struct subpicture_sys_t
80 /** The SVG source associated with this subpicture */
82 /* The rendered SVG, as a GdkPixbuf */
83 GdkPixbuf *p_rendition;
86 /*****************************************************************************
87 * vout_sys_t: svg local data
88 *****************************************************************************
89 * This structure is part of the video output thread descriptor.
90 * It describes the svg specific properties of an output thread.
91 *****************************************************************************/
92 struct text_renderer_sys_t
94 /* The SVG template used to convert strings */
99 /*****************************************************************************
100 * Create: allocates svg video thread output method
101 *****************************************************************************
102 * This function allocates and initializes a vout method.
103 *****************************************************************************/
104 static int Create( vlc_object_t *p_this )
106 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
108 /* Allocate structure */
109 p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) );
110 if( p_vout->p_text_renderer_data == NULL )
112 msg_Err( p_vout, "Out of memory" );
115 p_vout->pf_add_string = AddText;
117 /* Initialize psz_template */
118 p_vout->p_text_renderer_data->psz_template = svg_GetTemplate( p_this );
119 if( !p_vout->p_text_renderer_data->psz_template )
121 msg_Err( p_vout, "Out of memory" );
128 static byte_t *svg_GetTemplate( vlc_object_t *p_this )
130 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
135 psz_filename = config_GetPsz( p_vout, "svg-template-file" );
136 if( !psz_filename || psz_filename[0] == 0 )
138 /* No filename. Use a default value. */
143 /* Read the template */
144 file = fopen( psz_filename, "rt" );
147 msg_Warn( p_this, "SVG template file %s does not exist.", psz_filename );
155 i_ret = lstat( psz_filename, &s );
158 /* Problem accessing file information. Should not
159 happen as we could open it. */
164 fprintf( stderr, "Reading %ld bytes from %s\n", (long)s.st_size, psz_filename );
166 psz_template = malloc( s.st_size + 42 );
169 msg_Err( p_vout, "Out of memory" );
172 fread( psz_template, s.st_size, 1, file );
179 /* Either there was no file, or there was an error.
180 Use the default value */
181 psz_template = strdup( "<?xml version='1.0' encoding='UTF-8' standalone='no'?> \
182 <svg version='1' preserveAspectRatio='xMinYMin meet' viewBox='0 0 800 600'> \
183 <text x='10' y='560' fill='white' font-size='32' \
184 font-family='sans-serif'>%s</text></svg>" );
190 /*****************************************************************************
191 * Destroy: destroy Clone video thread output method
192 *****************************************************************************
193 * Clean up all data and library connections
194 *****************************************************************************/
195 static void Destroy( vlc_object_t *p_this )
197 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
198 free( p_vout->p_text_renderer_data->psz_template );
199 free( p_vout->p_text_renderer_data );
202 /*****************************************************************************
203 * Render: render SVG in picture
204 *****************************************************************************
205 * This function merges the previously rendered SVG subpicture into a picture
206 *****************************************************************************/
207 static void Render( vout_thread_t *p_vout, picture_t *p_pic,
208 const subpicture_t *p_subpic )
210 subpicture_sys_t *p_string = p_subpic->p_sys;
211 guchar *pixels_in = NULL;
212 guchar *pixels_out = NULL;
213 int rowstride_in, rowstride_out;
214 int channels_in, channels_out;
216 int i_width, i_height;
219 if( p_string->p_rendition == NULL ) {
220 /* Something changed ( presumably the dimensions ). Get the new
221 dimensions and update the pixbuf */
222 p_string->i_width = p_vout->output.i_width;
223 p_string->i_height = p_vout->output.i_height;
224 svg_RenderPicture( p_vout, p_string );
227 /* This rendering code is in no way optimized. If someone has some
228 time to lose to make it work faster, please do.
231 /* FIXME: The alpha value is not taken into account. */
234 p_pixbuf->get_rowstride() is the number of bytes in a line.
235 p_pixbuf->get_height() is the number of lines.
237 The number of bytes of p_pixbuf->p_pixels is get_rowstride * get_height
240 alpha = pixels [ n_channels * ( y*rowstride + x ) + 3 ];
242 red = pixels [ n_channels * ( y*rowstride ) + x ) ];
243 green = pixels [ n_channels * ( y*rowstride ) + x ) + 1 ];
244 blue = pixels [ n_channels * ( y*rowstride ) + x ) + 2 ];
247 pixels_in = gdk_pixbuf_get_pixels( p_string->p_rendition );
248 pixels_out = p_pic->p->p_pixels;
250 rowstride_in = gdk_pixbuf_get_rowstride( p_string->p_rendition );
251 rowstride_out = p_pic->p->i_pitch;
253 channels_in = gdk_pixbuf_get_n_channels( p_string->p_rendition );
254 channels_out = p_pic->p->i_pixel_pitch;
256 alpha = gdk_pixbuf_get_has_alpha( p_string->p_rendition );
258 #define INDEX_IN( x, y ) ( y * rowstride_in + x * channels_in )
259 #define INDEX_OUT( x, y ) ( y * rowstride_out + x * channels_out )
260 #define UV_INDEX_OUT( x, y ) ( y * p_pic->p[U_PLANE].i_pitch / 2 + x * p_pic->p[U_PLANE].i_pixel_pitch / 2 )
262 i_width = gdk_pixbuf_get_width( p_string->p_rendition );
263 i_height = gdk_pixbuf_get_height( p_string->p_rendition );
265 switch( p_vout->output.i_chroma )
267 /* I420 target, no scaling */
268 case VLC_FOURCC( 'I','4','2','0' ):
269 case VLC_FOURCC( 'I','Y','U','V' ):
270 case VLC_FOURCC( 'Y','V','1','2' ):
271 for( y = 0; y < i_height; y++ )
273 for( x = 0; x < i_width; x++ )
279 p_in = &pixels_in[INDEX_IN( x, y )];
281 #define R( pixel ) *pixel
282 #define G( pixel ) *( pixel+1 )
283 #define B( pixel ) *( pixel+2 )
284 #define ALPHA( pixel ) *( pixel+3 )
286 /* From http://www.geocrawler.com/archives/3/8263/2001/6/0/6020594/ :
287 Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
288 U = -0.1687 * r - 0.3313 * g + 0.5 * b + 128
289 V = 0.5 * r - 0.4187 * g - 0.0813 * b + 128
291 if( (alpha && ALPHA( p_in ) > 10 ) || ( ! alpha )) {
292 i_out = INDEX_OUT( x, y );
294 p_pic->p[Y_PLANE].p_pixels[i_out] = .299 * R( p_in ) + .587 * G( p_in ) + .114 * B( p_in );
296 if( ( x % 2 == 0 ) && ( y % 2 == 0 ) ) {
297 i_uv_out = UV_INDEX_OUT( x, y );
299 p_pic->p[U_PLANE].p_pixels[i_uv_out] = -.1687 * R( p_in ) - .3313 * G( p_in ) + .5 * B( p_in ) + 128;
300 p_pic->p[V_PLANE].p_pixels[i_uv_out] = .5 * R( p_in ) - .4187 * G( p_in ) - .0813 * B( p_in ) + 128;
307 /* RV32 target, scaling */
308 case VLC_FOURCC( 'R','V','2','4' ):
309 case VLC_FOURCC( 'R','V','3','2' ):
310 for( y = 0; y < i_height; y++ )
312 for( x = 0; x < i_width; x++ )
317 p_in = &pixels_in[INDEX_IN( x, y )];
318 p_out = &pixels_out[INDEX_OUT( x, y )];
321 *( p_out+1 ) = *( p_in+1 );
322 *( p_out+2 ) = *( p_in+2 );
328 msg_Err( p_vout, "unknown chroma, can't render SVG" );
333 static void svg_SizeCallback( int *width, int *height, gpointer data )
335 subpicture_sys_t *p_string = data;
337 *width = p_string->i_width;
338 *height = p_string->i_height;
342 static void svg_RenderPicture( vout_thread_t *p_vout,
343 subpicture_sys_t *p_string )
345 /* Render the SVG string p_string->psz_text into a new picture_t
346 p_string->p_rendition with dimensions ( ->i_width, ->i_height ) */
347 RsvgHandle *p_handle;
350 p_handle = rsvg_handle_new();
352 rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_string, NULL );
354 rsvg_handle_write( p_handle,
355 p_string->psz_text, strlen( p_string->psz_text ) + 1,
357 rsvg_handle_close( p_handle, &error );
359 p_string->p_rendition = rsvg_handle_get_pixbuf( p_handle );
360 rsvg_handle_free( p_handle );
365 * This function receives a SVG string and creates a subpicture for it.
366 * It is used as pf_add_string callback in the vout method by this module.
368 static subpicture_t *AddText ( vout_thread_t *p_vout, int i_channel,
369 char *psz_string, text_style_t *p_style, int i_flags,
370 int i_hmargin, int i_vmargin, mtime_t i_start,
373 subpicture_sys_t *p_string;
374 subpicture_t *p_subpic;
376 msg_Dbg( p_vout, "adding string \"%s\" start_date "I64Fd
377 " end_date" I64Fd, psz_string, i_start, i_stop );
379 /* Create and initialize a subpicture */
380 p_subpic = vout_CreateSubPicture( p_vout, i_channel, GRAPH_CONTENT,
382 if( p_subpic == NULL )
387 p_subpic->pf_render = Render;
388 p_subpic->pf_destroy = FreeString;
389 p_subpic->i_start = i_start;
390 p_subpic->i_stop = i_stop;
393 p_subpic->b_ephemer = VLC_TRUE;
397 p_subpic->b_ephemer = VLC_FALSE;
400 /* Create and initialize private data for the subpicture */
401 p_string = malloc( sizeof( subpicture_sys_t ) );
402 if( p_string == NULL )
404 vout_DestroySubPicture( p_vout, p_subpic );
407 p_subpic->p_sys = p_string;
409 /* Check if the data is SVG or pure text. In the latter case,
410 convert the text to SVG. FIXME: find a better test */
411 if( strstr( psz_string, "<svg" ))
413 /* Data is SVG: duplicate */
414 p_string->psz_text = strdup( psz_string );
418 /* Data is text. Convert to SVG */
420 byte_t* psz_template = p_vout->p_text_renderer_data->psz_template;
421 length = strlen( psz_string ) + strlen( psz_template ) + 42;
422 p_string->psz_text = malloc( length + 1 );
423 if( p_string->psz_text == NULL )
427 snprintf( p_string->psz_text, length, psz_template, psz_string );
430 p_string->i_width = p_vout->output.i_width;
431 p_string->i_height = p_vout->output.i_height;
432 p_string->i_chroma = p_vout->output.i_chroma;
435 The input data is stored in the p_string structure,
436 and the function updates the p_rendition attribute. */
437 svg_RenderPicture( p_vout, p_string );
439 vout_DisplaySubPicture( p_vout, p_subpic );
443 static void FreeString( subpicture_t *p_subpic )
445 subpicture_sys_t *p_string = p_subpic->p_sys;
447 free( p_string->psz_text );
448 free( p_string->p_rendition );