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 "vlc_block.h"
38 #include "vlc_filter.h"
40 #include <librsvg-2/librsvg/rsvg.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Create ( vlc_object_t * );
46 static void Destroy ( vlc_object_t * );
47 static subpicture_t *RenderText( filter_t *, block_t * );
49 /*****************************************************************************
51 *****************************************************************************/
53 #define TEMPLATE_TEXT N_( "SVG template file" )
54 #define TEMPLATE_LONGTEXT N_( "Location of a file holding a SVG template for automatic string conversion" )
57 set_capability( "text renderer", 101 );
58 add_shortcut( "svg" );
59 add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, VLC_TRUE );
60 set_callbacks( Create, Destroy );
64 Describes a SVG string to be displayed on the video
66 typedef struct subpicture_data_t
71 /** The SVG source associated with this subpicture */
73 /* The rendered SVG, as a GdkPixbuf */
74 GdkPixbuf *p_rendition;
77 static void Render ( filter_t *, subpicture_t *, subpicture_data_t * );
78 static byte_t *svg_GetTemplate ();
79 static void svg_SizeCallback (int *width, int *height, gpointer data );
80 static void svg_RenderPicture (filter_t *p_filter,
81 subpicture_data_t *p_string );
82 static void FreeString( subpicture_data_t * );
84 /*****************************************************************************
85 * filter_sys_t: svg local data
86 *****************************************************************************
87 * This structure is part of the filter thread descriptor.
88 * It describes the svg specific properties of an output thread.
89 *****************************************************************************/
92 /* The SVG template used to convert strings */
94 /* Default size for rendering. Initialized to the output size. */
100 /*****************************************************************************
101 * Create: allocates svg video thread output method
102 *****************************************************************************
103 * This function allocates and initializes a vout method.
104 *****************************************************************************/
105 static int Create( vlc_object_t *p_this )
107 filter_t *p_filter = (filter_t *)p_this;
110 /* Allocate structure */
111 p_sys = malloc( sizeof( filter_sys_t ) );
114 msg_Err( p_filter, "Out of memory" );
118 /* Initialize psz_template */
119 p_sys->psz_template = svg_GetTemplate( p_this );
120 if( !p_sys->psz_template )
122 msg_Err( p_filter, "Out of memory" );
126 p_sys->i_width = p_filter->fmt_out.video.i_width;
127 p_sys->i_height = p_filter->fmt_out.video.i_height;
129 p_filter->pf_render_string = RenderText;
130 p_filter->p_sys = p_sys;
132 /* MUST call this before any RSVG funcs */
138 static byte_t *svg_GetTemplate( vlc_object_t *p_this )
140 filter_t *p_filter = (filter_t *)p_this;
145 psz_filename = config_GetPsz( p_filter, "svg-template-file" );
146 if( !psz_filename || psz_filename[0] == 0 )
148 /* No filename. Use a default value. */
153 /* Read the template */
154 file = fopen( psz_filename, "rt" );
157 msg_Warn( p_this, "SVG template file %s does not exist.", psz_filename );
165 i_ret = lstat( psz_filename, &s );
168 /* Problem accessing file information. Should not
169 happen as we could open it. */
174 msg_Dbg( p_this, "Reading %ld bytes from template %s\n", (long)s.st_size, psz_filename );
176 psz_template = malloc( s.st_size + 42 );
179 msg_Err( p_filter, "Out of memory" );
182 memset( psz_template, 0, s.st_size + 1 );
183 fread( psz_template, s.st_size, 1, file );
190 /* Either there was no file, or there was an error.
191 Use the default value */
192 psz_template = strdup( "<?xml version='1.0' encoding='UTF-8' standalone='no'?> \
193 <svg version='1' preserveAspectRatio='xMinYMin meet' viewBox='0 0 800 600'> \
194 <text x='10' y='560' fill='white' font-size='32' \
195 font-family='sans-serif'>%s</text></svg>" );
201 /*****************************************************************************
202 * Destroy: destroy Clone video thread output method
203 *****************************************************************************
204 * Clean up all data and library connections
205 *****************************************************************************/
206 static void Destroy( vlc_object_t *p_this )
208 filter_t *p_filter = (filter_t *)p_this;
209 filter_sys_t *p_sys = p_filter->p_sys;
211 free( p_sys->psz_template );
215 /*****************************************************************************
216 * Render: render SVG in picture
217 *****************************************************************************/
218 static void Render( filter_t *p_filter, subpicture_t *p_spu,
219 subpicture_data_t *p_string )
221 int i_width, i_height;
223 uint8_t *p_y, *p_u, *p_v, *p_a;
224 int x, y, i_pitch, i_u_pitch;
225 guchar *pixels_in = NULL;
231 if( p_string->p_rendition == NULL ) {
232 svg_RenderPicture( p_filter, p_string );
234 i_width = gdk_pixbuf_get_width( p_string->p_rendition );
235 i_height = gdk_pixbuf_get_height( p_string->p_rendition );
237 /* Create a new subpicture region */
238 memset( &fmt, 0, sizeof(video_format_t) );
239 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
240 fmt.i_aspect = VOUT_ASPECT_FACTOR;
241 fmt.i_width = fmt.i_visible_width = i_width;
242 fmt.i_height = fmt.i_visible_height = i_height;
243 fmt.i_x_offset = fmt.i_y_offset = 0;
244 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
245 if( !p_spu->p_region )
247 msg_Err( p_filter, "cannot allocate SPU region" );
251 p_spu->p_region->i_x = p_spu->p_region->i_y = 0;
252 p_y = p_spu->p_region->picture.Y_PIXELS;
253 p_u = p_spu->p_region->picture.U_PIXELS;
254 p_v = p_spu->p_region->picture.V_PIXELS;
255 p_a = p_spu->p_region->picture.A_PIXELS;
257 i_pitch = p_spu->p_region->picture.Y_PITCH;
258 i_u_pitch = p_spu->p_region->picture.U_PITCH;
260 /* Initialize the region pixels (only the alpha will be changed later) */
261 memset( p_y, 0x00, i_pitch * p_spu->p_region->fmt.i_height );
262 memset( p_u, 0x80, i_u_pitch * p_spu->p_region->fmt.i_height );
263 memset( p_v, 0x80, i_u_pitch * p_spu->p_region->fmt.i_height );
265 p_pic = &(p_spu->p_region->picture);
269 /* This rendering code is in no way optimized. If someone has some time to
270 make it work faster or better, please do.
274 p_pixbuf->get_rowstride() is the number of bytes in a line.
275 p_pixbuf->get_height() is the number of lines.
277 The number of bytes of p_pixbuf->p_pixels is get_rowstride * get_height
280 alpha = pixels [ n_channels * ( y*rowstride + x ) + 3 ];
282 red = pixels [ n_channels * ( y*rowstride ) + x ) ];
283 green = pixels [ n_channels * ( y*rowstride ) + x ) + 1 ];
284 blue = pixels [ n_channels * ( y*rowstride ) + x ) + 2 ];
287 pixels_in = gdk_pixbuf_get_pixels( p_string->p_rendition );
288 rowstride_in = gdk_pixbuf_get_rowstride( p_string->p_rendition );
289 channels_in = gdk_pixbuf_get_n_channels( p_string->p_rendition );
290 alpha = gdk_pixbuf_get_has_alpha( p_string->p_rendition );
293 This crashes the plugin (if !alpha). As there is always an alpha value,
294 it does not matter for the moment :
297 memset( p_a, 0xFF, i_pitch * p_spu->p_region->fmt.i_height );
300 #define INDEX_IN( x, y ) ( y * rowstride_in + x * channels_in )
301 #define INDEX_OUT( x, y ) ( y * i_pitch + x * p_pic->p[Y_PLANE].i_pixel_pitch )
302 #define UV_INDEX_OUT( x, y ) ( ( y >> 1 ) * i_u_pitch + ( x >> 1) * p_pic->p[U_PLANE].i_pixel_pitch )
304 for( y = 0; y < i_height; y++ )
306 for( x = 0; x < i_width; x++ )
312 p_in = &pixels_in[INDEX_IN( x, y )];
314 #define R( pixel ) *pixel
315 #define G( pixel ) *( pixel+1 )
316 #define B( pixel ) *( pixel+2 )
317 #define ALPHA( pixel ) *( pixel+3 )
319 /* From http://www.geocrawler.com/archives/3/8263/2001/6/0/6020594/ :
320 Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
321 U = -0.1687 * r - 0.3313 * g + 0.5 * b + 128
322 V = 0.5 * r - 0.4187 * g - 0.0813 * b + 128
325 i_out = INDEX_OUT( x, y );
327 p_pic->Y_PIXELS[i_out] = .299 * R( p_in ) + .587 * G( p_in ) + .114 * B( p_in );
329 p_pic->A_PIXELS[i_out] = ALPHA( p_in );
331 if( ( x % 2 == 0 ) && ( y % 2 == 0 ) ) {
333 i_uv_out = UV_INDEX_OUT( x, y );
336 p_pic->U_PIXELS[i_uv_out] = -.1687 * R( p_in ) - .3313 * G( p_in ) + .5 * B( p_in ) + 128;
337 p_pic->V_PIXELS[i_uv_out] = .5 * R( p_in ) - .4187 * G( p_in ) - .0813 * B( p_in ) + 128;
344 static void svg_SizeCallback( int *width, int *height, gpointer data )
346 subpicture_data_t *p_string = data;
348 *width = p_string->i_width;
349 *height = p_string->i_height;
353 static void svg_RenderPicture( filter_t *p_filter,
354 subpicture_data_t *p_string )
356 /* Render the SVG string p_string->psz_text into a new picture_t
357 p_string->p_rendition with dimensions ( ->i_width, ->i_height ) */
358 RsvgHandle *p_handle;
359 GError *error = NULL;
361 p_handle = rsvg_handle_new();
363 rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_string, NULL );
365 rsvg_handle_write( p_handle,
366 p_string->psz_text, strlen( p_string->psz_text ) + 1,
370 msg_Err( p_filter, "Error in handle_write: %s\n", error->message );
373 rsvg_handle_close( p_handle, &error );
375 p_string->p_rendition = rsvg_handle_get_pixbuf( p_handle );
376 rsvg_handle_free( p_handle );
380 static subpicture_t *RenderText( filter_t *p_filter, block_t *p_block )
382 filter_sys_t *p_sys = p_filter->p_sys;
383 subpicture_t *p_subpic = NULL;
384 subpicture_data_t *p_string = NULL;
388 if( !p_block ) return NULL;
389 psz_string = p_block->p_buffer;
390 if( !psz_string || !*psz_string ) return NULL;
392 /* Create and initialize a subpicture */
393 p_subpic = p_filter->pf_sub_buffer_new( p_filter );
394 if( !p_subpic ) return NULL;
396 p_subpic->i_start = p_block->i_pts;
397 p_subpic->i_stop = p_block->i_pts + p_block->i_length;
398 /* Always replace rendered text when another is displayed */
399 p_subpic->b_ephemer = VLC_TRUE;
400 p_subpic->b_absolute = VLC_FALSE;
402 // msg_Dbg( p_filter, "adding string \"%s\" start_date "I64Fd
404 /* Create and initialize private data for the subpicture */
405 p_string = malloc( sizeof(subpicture_data_t) );
408 msg_Err( p_filter, "Out of memory" );
409 p_filter->pf_sub_buffer_del( p_filter, p_subpic );
412 /* Check if the data is SVG or pure text. In the latter case,
413 convert the text to SVG. FIXME: find a better test */
414 if( strstr( psz_string, "<svg" ))
416 /* Data is SVG: duplicate */
417 p_string->psz_text = strdup( psz_string );
418 if( !p_string->psz_text )
420 msg_Err( p_filter, "Out of memory" );
421 p_filter->pf_sub_buffer_del( p_filter, p_subpic );
428 /* Data is text. Convert to SVG */
430 byte_t* psz_template = p_sys->psz_template;
431 length = strlen( psz_string ) + strlen( psz_template ) + 42;
432 p_string->psz_text = malloc( length + 1 );
433 if( !p_string->psz_text )
435 msg_Err( p_filter, "Out of memory" );
436 p_filter->pf_sub_buffer_del( p_filter, p_subpic );
440 memset( p_string->psz_text, 0, length + 1 );
441 snprintf( p_string->psz_text, length, psz_template, psz_string );
443 p_string->i_width = p_sys->i_width;
444 p_string->i_height = p_sys->i_height;
445 p_string->i_chroma = VLC_FOURCC('Y','U','V','A');
448 The input data is stored in the p_string structure,
449 and the function updates the p_rendition attribute. */
450 svg_RenderPicture( p_filter, p_string );
452 Render( p_filter, p_subpic, p_string );
453 FreeString( p_string );
454 block_Release( p_block );
459 static void FreeString( subpicture_data_t *p_string )
461 free( p_string->psz_text );
462 free( p_string->p_rendition );