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_capability( "text renderer", 100 );
65 add_shortcut( "svg" );
66 add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, VLC_TRUE );
67 set_callbacks( Create, Destroy );
71 Describes a SVG string to be displayed on the video
73 struct subpicture_sys_t
78 /** The SVG source associated with this subpicture */
80 /* The rendered SVG, as a GdkPixbuf */
81 GdkPixbuf *p_rendition;
84 /*****************************************************************************
85 * vout_sys_t: svg local data
86 *****************************************************************************
87 * This structure is part of the video output thread descriptor.
88 * It describes the svg specific properties of an output thread.
89 *****************************************************************************/
90 struct text_renderer_sys_t
92 /* The SVG template used to convert strings */
97 /*****************************************************************************
98 * Create: allocates svg video thread output method
99 *****************************************************************************
100 * This function allocates and initializes a vout method.
101 *****************************************************************************/
102 static int Create( vlc_object_t *p_this )
104 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
106 /* Allocate structure */
107 p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) );
108 if( p_vout->p_text_renderer_data == NULL )
110 msg_Err( p_vout, "Out of memory" );
113 p_vout->pf_add_string = AddText;
115 /* Initialize psz_template */
116 p_vout->p_text_renderer_data->psz_template = svg_GetTemplate( p_this );
117 if( !p_vout->p_text_renderer_data->psz_template )
119 msg_Err( p_vout, "Out of memory" );
126 static byte_t *svg_GetTemplate( vlc_object_t *p_this )
128 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
133 psz_filename = config_GetPsz( p_vout, "svg-template-file" );
134 if( !psz_filename || psz_filename[0] == 0 )
136 /* No filename. Use a default value. */
141 /* Read the template */
142 file = fopen( psz_filename, "rt" );
145 msg_Warn( p_this, "SVG template file %s does not exist.", psz_filename );
153 i_ret = lstat( psz_filename, &s );
156 /* Problem accessing file information. Should not
157 happen as we could open it. */
162 fprintf( stderr, "Reading %ld bytes from %s\n", (long)s.st_size, psz_filename );
164 psz_template = malloc( s.st_size + 42 );
167 msg_Err( p_vout, "Out of memory" );
170 fread( psz_template, s.st_size, 1, file );
177 /* Either there was no file, or there was an error.
178 Use the default value */
179 psz_template = strdup( "<?xml version='1.0' encoding='UTF-8' standalone='no'?> \
180 <svg version='1' preserveAspectRatio='xMinYMin meet' viewBox='0 0 800 600'> \
181 <text x='10' y='560' fill='white' font-size='32' \
182 font-family='sans-serif'>%s</text></svg>" );
188 /*****************************************************************************
189 * Destroy: destroy Clone video thread output method
190 *****************************************************************************
191 * Clean up all data and library connections
192 *****************************************************************************/
193 static void Destroy( vlc_object_t *p_this )
195 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
196 free( p_vout->p_text_renderer_data->psz_template );
197 free( p_vout->p_text_renderer_data );
200 /*****************************************************************************
201 * Render: render SVG in picture
202 *****************************************************************************
203 * This function merges the previously rendered SVG subpicture into a picture
204 *****************************************************************************/
205 static void Render( vout_thread_t *p_vout, picture_t *p_pic,
206 const subpicture_t *p_subpic )
208 subpicture_sys_t *p_string = p_subpic->p_sys;
209 guchar *pixels_in = NULL;
210 guchar *pixels_out = NULL;
211 int rowstride_in, rowstride_out;
212 int channels_in, channels_out;
214 int i_width, i_height;
217 if( p_string->p_rendition == NULL ) {
218 /* Something changed ( presumably the dimensions ). Get the new
219 dimensions and update the pixbuf */
220 p_string->i_width = p_vout->output.i_width;
221 p_string->i_height = p_vout->output.i_height;
222 svg_RenderPicture( p_vout, p_string );
225 /* This rendering code is in no way optimized. If someone has some
226 time to lose to make it work faster, please do.
229 /* FIXME: The alpha value is not taken into account. */
232 p_pixbuf->get_rowstride() is the number of bytes in a line.
233 p_pixbuf->get_height() is the number of lines.
235 The number of bytes of p_pixbuf->p_pixels is get_rowstride * get_height
238 alpha = pixels [ n_channels * ( y*rowstride + x ) + 3 ];
240 red = pixels [ n_channels * ( y*rowstride ) + x ) ];
241 green = pixels [ n_channels * ( y*rowstride ) + x ) + 1 ];
242 blue = pixels [ n_channels * ( y*rowstride ) + x ) + 2 ];
245 pixels_in = gdk_pixbuf_get_pixels( p_string->p_rendition );
246 pixels_out = p_pic->p->p_pixels;
248 rowstride_in = gdk_pixbuf_get_rowstride( p_string->p_rendition );
249 rowstride_out = p_pic->p->i_pitch;
251 channels_in = gdk_pixbuf_get_n_channels( p_string->p_rendition );
252 channels_out = p_pic->p->i_pixel_pitch;
254 alpha = gdk_pixbuf_get_has_alpha( p_string->p_rendition );
256 #define INDEX_IN( x, y ) ( y * rowstride_in + x * channels_in )
257 #define INDEX_OUT( x, y ) ( y * rowstride_out + x * channels_out )
258 #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 )
260 i_width = gdk_pixbuf_get_width( p_string->p_rendition );
261 i_height = gdk_pixbuf_get_height( p_string->p_rendition );
263 switch( p_vout->output.i_chroma )
265 /* I420 target, no scaling */
266 case VLC_FOURCC( 'I','4','2','0' ):
267 case VLC_FOURCC( 'I','Y','U','V' ):
268 case VLC_FOURCC( 'Y','V','1','2' ):
269 for( y = 0; y < i_height; y++ )
271 for( x = 0; x < i_width; x++ )
277 p_in = &pixels_in[INDEX_IN( x, y )];
279 #define R( pixel ) *pixel
280 #define G( pixel ) *( pixel+1 )
281 #define B( pixel ) *( pixel+2 )
282 #define ALPHA( pixel ) *( pixel+3 )
284 /* From http://www.geocrawler.com/archives/3/8263/2001/6/0/6020594/ :
285 Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
286 U = -0.1687 * r - 0.3313 * g + 0.5 * b + 128
287 V = 0.5 * r - 0.4187 * g - 0.0813 * b + 128
289 if( (alpha && ALPHA( p_in ) > 10 ) || ( ! alpha )) {
290 i_out = INDEX_OUT( x, y );
292 p_pic->p[Y_PLANE].p_pixels[i_out] = .299 * R( p_in ) + .587 * G( p_in ) + .114 * B( p_in );
294 if( ( x % 2 == 0 ) && ( y % 2 == 0 ) ) {
295 i_uv_out = UV_INDEX_OUT( x, y );
297 p_pic->p[U_PLANE].p_pixels[i_uv_out] = -.1687 * R( p_in ) - .3313 * G( p_in ) + .5 * B( p_in ) + 128;
298 p_pic->p[V_PLANE].p_pixels[i_uv_out] = .5 * R( p_in ) - .4187 * G( p_in ) - .0813 * B( p_in ) + 128;
305 /* RV32 target, scaling */
306 case VLC_FOURCC( 'R','V','2','4' ):
307 case VLC_FOURCC( 'R','V','3','2' ):
308 for( y = 0; y < i_height; y++ )
310 for( x = 0; x < i_width; x++ )
315 p_in = &pixels_in[INDEX_IN( x, y )];
316 p_out = &pixels_out[INDEX_OUT( x, y )];
319 *( p_out+1 ) = *( p_in+1 );
320 *( p_out+2 ) = *( p_in+2 );
326 msg_Err( p_vout, "unknown chroma, can't render SVG" );
331 static void svg_SizeCallback( int *width, int *height, gpointer data )
333 subpicture_sys_t *p_string = data;
335 *width = p_string->i_width;
336 *height = p_string->i_height;
340 static void svg_RenderPicture( vout_thread_t *p_vout,
341 subpicture_sys_t *p_string )
343 /* Render the SVG string p_string->psz_text into a new picture_t
344 p_string->p_rendition with dimensions ( ->i_width, ->i_height ) */
345 RsvgHandle *p_handle;
348 p_handle = rsvg_handle_new();
350 rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_string, NULL );
352 rsvg_handle_write( p_handle,
353 p_string->psz_text, strlen( p_string->psz_text ) + 1,
355 rsvg_handle_close( p_handle, &error );
357 p_string->p_rendition = rsvg_handle_get_pixbuf( p_handle );
358 rsvg_handle_free( p_handle );
363 * This function receives a SVG string and creates a subpicture for it.
364 * It is used as pf_add_string callback in the vout method by this module.
366 static subpicture_t *AddText ( vout_thread_t *p_vout, int i_channel,
367 char *psz_string, text_style_t *p_style, int i_flags,
368 int i_hmargin, int i_vmargin, mtime_t i_start,
371 subpicture_sys_t *p_string;
372 subpicture_t *p_subpic;
374 msg_Dbg( p_vout, "adding string \"%s\" start_date "I64Fd
375 " end_date" I64Fd, psz_string, i_start, i_stop );
377 /* Create and initialize a subpicture */
378 p_subpic = vout_CreateSubPicture( p_vout, i_channel, GRAPH_CONTENT,
380 if( p_subpic == NULL )
385 p_subpic->pf_render = Render;
386 p_subpic->pf_destroy = FreeString;
387 p_subpic->i_start = i_start;
388 p_subpic->i_stop = i_stop;
391 p_subpic->b_ephemer = VLC_TRUE;
395 p_subpic->b_ephemer = VLC_FALSE;
398 /* Create and initialize private data for the subpicture */
399 p_string = malloc( sizeof( subpicture_sys_t ) );
400 if( p_string == NULL )
402 vout_DestroySubPicture( p_vout, p_subpic );
405 p_subpic->p_sys = p_string;
407 /* Check if the data is SVG or pure text. In the latter case,
408 convert the text to SVG. FIXME: find a better test */
409 if( strstr( psz_string, "<svg" ))
411 /* Data is SVG: duplicate */
412 p_string->psz_text = strdup( psz_string );
416 /* Data is text. Convert to SVG */
418 byte_t* psz_template = p_vout->p_text_renderer_data->psz_template;
419 length = strlen( psz_string ) + strlen( psz_template ) + 42;
420 p_string->psz_text = malloc( length + 1 );
421 if( p_string->psz_text == NULL )
425 snprintf( p_string->psz_text, length, psz_template, psz_string );
428 p_string->i_width = p_vout->output.i_width;
429 p_string->i_height = p_vout->output.i_height;
430 p_string->i_chroma = p_vout->output.i_chroma;
433 The input data is stored in the p_string structure,
434 and the function updates the p_rendition attribute. */
435 svg_RenderPicture( p_vout, p_string );
437 vout_DisplaySubPicture( p_vout, p_subpic );
441 static void FreeString( subpicture_t *p_subpic )
443 subpicture_sys_t *p_string = p_subpic->p_sys;
445 free( p_string->psz_text );
446 free( p_string->p_rendition );