1 /*****************************************************************************
2 * svg.c : Put SVG on the video
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 the VideoLAN team
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 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
37 #include <sys/types.h>
41 #elif defined( WIN32 ) && !defined( UNDER_CE )
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 /*****************************************************************************
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 static char *svg_GetTemplate( vlc_object_t
*p_this
);
61 /*****************************************************************************
63 *****************************************************************************/
65 #define TEMPLATE_TEXT N_( "SVG template file" )
66 #define TEMPLATE_LONGTEXT N_( "Location of a file holding a SVG template "\
67 "for automatic string conversion" )
70 set_category( CAT_INPUT
)
71 set_category( SUBCAT_INPUT_SCODEC
)
72 set_capability( "text renderer", 99 )
74 add_string( "svg-template-file", "", TEMPLATE_TEXT
, TEMPLATE_LONGTEXT
, true )
75 set_callbacks( Create
, Destroy
)
79 Describes a SVG string to be displayed on the video
81 struct svg_rendition_t
86 /** The SVG source associated with this subpicture */
88 /* The rendered SVG, as a GdkPixbuf */
89 GdkPixbuf
*p_rendition
;
92 static int Render( filter_t
*, subpicture_region_t
*, svg_rendition_t
*, int, int);
93 static char *svg_GetTemplate ();
94 static void svg_set_size( filter_t
*p_filter
, int width
, int height
);
95 static void svg_SizeCallback ( int *width
, int *height
, gpointer data
);
96 static void svg_RenderPicture ( filter_t
*p_filter
,
97 svg_rendition_t
*p_svg
);
98 static void FreeString( svg_rendition_t
* );
100 /*****************************************************************************
101 * filter_sys_t: svg local data
102 *****************************************************************************
103 * This structure is part of the filter thread descriptor.
104 * It describes the svg specific properties of an output thread.
105 *****************************************************************************/
108 /* The SVG template used to convert strings */
110 /* Default size for rendering. Initialized to the output size. */
115 /*****************************************************************************
116 * Create: allocates svg video thread output method
117 *****************************************************************************
118 * This function allocates and initializes a vout method.
119 *****************************************************************************/
120 static int Create( vlc_object_t
*p_this
)
122 filter_t
*p_filter
= ( filter_t
* )p_this
;
125 /* Allocate structure */
126 p_sys
= malloc( sizeof( filter_sys_t
) );
130 /* Initialize psz_template */
131 p_sys
->psz_template
= svg_GetTemplate( p_this
);
132 if( !p_sys
->psz_template
)
138 p_sys
->i_width
= p_filter
->fmt_out
.video
.i_width
;
139 p_sys
->i_height
= p_filter
->fmt_out
.video
.i_height
;
141 p_filter
->pf_render_text
= RenderText
;
142 p_filter
->pf_render_html
= NULL
;
143 p_filter
->p_sys
= p_sys
;
145 /* MUST call this before any RSVG funcs */
151 static char *svg_GetTemplate( vlc_object_t
*p_this
)
153 filter_t
*p_filter
= ( filter_t
* )p_this
;
158 psz_filename
= var_InheritString( p_filter
, "svg-template-file" );
159 if( !psz_filename
|| (psz_filename
[0] == 0) )
161 /* No filename. Use a default value. */
166 /* Read the template */
167 file
= vlc_fopen( psz_filename
, "rt" );
170 msg_Warn( p_this
, "SVG template file %s does not exist.",
178 if( fstat( fileno( file
), &s
) )
180 /* Problem accessing file information. Should not
181 happen as we could open it. */
185 if( ((signed)s
.st_size
) < 0 )
187 msg_Err( p_this
, "SVG template too big" );
192 msg_Dbg( p_this
, "reading %ld bytes from template %s",
193 (unsigned long)s
.st_size
, psz_filename
);
195 psz_template
= calloc( 1, s
.st_size
+ 42 );
199 free( psz_filename
);
202 if(! fread( psz_template
, s
.st_size
, 1, file
) )
204 msg_Dbg( p_this
, "No data read from template." );
210 free( psz_filename
);
213 /* Either there was no file, or there was an error.
214 Use the default value */
215 psz_template
= strdup( "<?xml version='1.0' encoding='UTF-8' standalone='no'?> \
216 <svg version='1' preserveAspectRatio='xMinYMin meet' viewBox='0 0 800 600'> \
217 <text x='10' y='560' fill='white' font-size='32' \
218 font-family='sans-serif'>%s</text></svg>" );
224 /*****************************************************************************
225 * Destroy: destroy Clone video thread output method
226 *****************************************************************************
227 * Clean up all data and library connections
228 *****************************************************************************/
229 static void Destroy( vlc_object_t
*p_this
)
231 filter_t
*p_filter
= ( filter_t
* )p_this
;
232 filter_sys_t
*p_sys
= p_filter
->p_sys
;
234 free( p_sys
->psz_template
);
239 /*****************************************************************************
240 * Render: render SVG in picture
241 *****************************************************************************/
242 static int Render( filter_t
*p_filter
, subpicture_region_t
*p_region
,
243 svg_rendition_t
*p_svg
, int i_width
, int i_height
)
246 uint8_t *p_y
, *p_u
, *p_v
, *p_a
;
247 int x
, y
, i_pitch
, i_u_pitch
;
248 guchar
*pixels_in
= NULL
;
254 if ( p_filter
->p_sys
->i_width
!= i_width
||
255 p_filter
->p_sys
->i_height
!= i_height
)
257 svg_set_size( p_filter
, i_width
, i_height
);
258 p_svg
->p_rendition
= NULL
;
261 if( p_svg
->p_rendition
== NULL
) {
262 svg_RenderPicture( p_filter
, p_svg
);
263 if( ! p_svg
->p_rendition
)
265 msg_Err( p_filter
, "Cannot render SVG" );
269 i_width
= gdk_pixbuf_get_width( p_svg
->p_rendition
);
270 i_height
= gdk_pixbuf_get_height( p_svg
->p_rendition
);
272 /* Create a new subpicture region */
273 memset( &fmt
, 0, sizeof( video_format_t
) );
274 fmt
.i_chroma
= VLC_CODEC_YUVA
;
275 fmt
.i_width
= fmt
.i_visible_width
= i_width
;
276 fmt
.i_height
= fmt
.i_visible_height
= i_height
;
277 fmt
.i_x_offset
= fmt
.i_y_offset
= 0;
281 p_region
->p_picture
= picture_NewFromFormat( &fmt
);
282 if( !p_region
->p_picture
)
286 p_region
->i_x
= p_region
->i_y
= 0;
287 p_y
= p_region
->p_picture
->Y_PIXELS
;
288 p_u
= p_region
->p_picture
->U_PIXELS
;
289 p_v
= p_region
->p_picture
->V_PIXELS
;
290 p_a
= p_region
->p_picture
->A_PIXELS
;
292 i_pitch
= p_region
->p_picture
->Y_PITCH
;
293 i_u_pitch
= p_region
->p_picture
->U_PITCH
;
295 /* Initialize the region pixels (only the alpha will be changed later) */
296 memset( p_y
, 0x00, i_pitch
* p_region
->fmt
.i_height
);
297 memset( p_u
, 0x80, i_u_pitch
* p_region
->fmt
.i_height
);
298 memset( p_v
, 0x80, i_u_pitch
* p_region
->fmt
.i_height
);
300 p_pic
= p_region
->p_picture
;
304 /* This rendering code is in no way optimized. If someone has some time to
305 make it work faster or better, please do.
309 p_pixbuf->get_rowstride() is the number of bytes in a line.
310 p_pixbuf->get_height() is the number of lines.
312 The number of bytes of p_pixbuf->p_pixels is get_rowstride * get_height
315 alpha = pixels [ n_channels * ( y*rowstride + x ) + 3 ];
317 red = pixels [ n_channels * ( y*rowstride ) + x ) ];
318 green = pixels [ n_channels * ( y*rowstride ) + x ) + 1 ];
319 blue = pixels [ n_channels * ( y*rowstride ) + x ) + 2 ];
322 pixels_in
= gdk_pixbuf_get_pixels( p_svg
->p_rendition
);
323 rowstride_in
= gdk_pixbuf_get_rowstride( p_svg
->p_rendition
);
324 channels_in
= gdk_pixbuf_get_n_channels( p_svg
->p_rendition
);
325 alpha
= gdk_pixbuf_get_has_alpha( p_svg
->p_rendition
);
328 This crashes the plugin (if !alpha). As there is always an alpha value,
329 it does not matter for the moment :
332 memset( p_a, 0xFF, i_pitch * p_region->fmt.i_height );
335 #define INDEX_IN( x, y ) ( y * rowstride_in + x * channels_in )
336 #define INDEX_OUT( x, y ) ( y * i_pitch + x * p_pic->p[Y_PLANE].i_pixel_pitch )
338 for( y
= 0; y
< i_height
; y
++ )
340 for( x
= 0; x
< i_width
; x
++ )
345 p_in
= &pixels_in
[INDEX_IN( x
, y
)];
347 #define R( pixel ) *pixel
348 #define G( pixel ) *( pixel+1 )
349 #define B( pixel ) *( pixel+2 )
350 #define ALPHA( pixel ) *( pixel+3 )
352 /* From http://www.geocrawler.com/archives/3/8263/2001/6/0/6020594/ :
353 Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
354 U = -0.1687 * r - 0.3313 * g + 0.5 * b + 128
355 V = 0.5 * r - 0.4187 * g - 0.0813 * b + 128
358 i_out
= INDEX_OUT( x
, y
);
360 p_pic
->Y_PIXELS
[i_out
] = .299 * R( p_in
) + .587 * G( p_in
) + .114 * B( p_in
);
362 p_pic
->U_PIXELS
[i_out
] = -.1687 * R( p_in
) - .3313 * G( p_in
) + .5 * B( p_in
) + 128;
363 p_pic
->V_PIXELS
[i_out
] = .5 * R( p_in
) - .4187 * G( p_in
) - .0813 * B( p_in
) + 128;
365 p_pic
->A_PIXELS
[i_out
] = ALPHA( p_in
);
373 static void svg_set_size( filter_t
*p_filter
, int width
, int height
)
375 p_filter
->p_sys
->i_width
= width
;
376 p_filter
->p_sys
->i_height
= height
;
379 static void svg_SizeCallback( int *width
, int *height
, gpointer data
)
381 filter_t
*p_filter
= data
;
383 *width
= p_filter
->p_sys
->i_width
;
384 *height
= p_filter
->p_sys
->i_height
;
388 static void svg_RenderPicture( filter_t
*p_filter
,
389 svg_rendition_t
*p_svg
)
391 /* Render the SVG string p_string->psz_text into a new picture_t
392 p_string->p_rendition with dimensions ( ->i_width, ->i_height ) */
393 RsvgHandle
*p_handle
;
394 GError
*error
= NULL
;
396 p_svg
->p_rendition
= NULL
;
398 p_handle
= rsvg_handle_new();
402 msg_Err( p_filter
, "Error creating SVG reader" );
406 rsvg_handle_set_size_callback( p_handle
, svg_SizeCallback
, p_filter
, NULL
);
408 if( ! rsvg_handle_write( p_handle
,
409 ( guchar
* )p_svg
->psz_text
, strlen( p_svg
->psz_text
),
412 msg_Err( p_filter
, "error while rendering SVG: %s", error
->message
);
413 g_object_unref( G_OBJECT( p_handle
) );
417 if( ! rsvg_handle_close( p_handle
, &error
) )
419 msg_Err( p_filter
, "error while rendering SVG (close): %s", error
->message
);
420 g_object_unref( G_OBJECT( p_handle
) );
424 p_svg
->p_rendition
= rsvg_handle_get_pixbuf( p_handle
);
426 g_object_unref( G_OBJECT( p_handle
) );
430 static int RenderText( filter_t
*p_filter
, subpicture_region_t
*p_region_out
,
431 subpicture_region_t
*p_region_in
)
433 filter_sys_t
*p_sys
= p_filter
->p_sys
;
434 svg_rendition_t
*p_svg
= NULL
;
438 if( !p_region_in
|| !p_region_out
) return VLC_EGENERIC
;
439 psz_string
= p_region_in
->psz_text
;
440 if( !psz_string
|| !*psz_string
) return VLC_EGENERIC
;
442 p_svg
= malloc( sizeof( svg_rendition_t
) );
446 p_region_out
->i_x
= p_region_in
->i_x
;
447 p_region_out
->i_y
= p_region_in
->i_y
;
449 /* Check if the data is SVG or pure text. In the latter case,
450 convert the text to SVG. FIXME: find a better test */
451 if( strstr( psz_string
, "<svg" ))
453 /* Data is SVG: duplicate */
454 p_svg
->psz_text
= strdup( psz_string
);
455 if( !p_svg
->psz_text
)
463 /* Data is text. Convert to SVG */
464 /* FIXME: handle p_style attributes */
466 char* psz_template
= p_sys
->psz_template
;
467 length
= strlen( psz_string
) + strlen( psz_template
) + 42;
468 p_svg
->psz_text
= calloc( 1, length
+ 1 );
469 if( !p_svg
->psz_text
)
474 snprintf( p_svg
->psz_text
, length
, psz_template
, psz_string
);
476 p_svg
->i_width
= p_sys
->i_width
;
477 p_svg
->i_height
= p_sys
->i_height
;
478 p_svg
->i_chroma
= VLC_CODEC_YUVA
;
481 The input data is stored in the p_string structure,
482 and the function updates the p_rendition attribute. */
483 svg_RenderPicture( p_filter
, p_svg
);
485 Render( p_filter
, p_region_out
, p_svg
, p_svg
->i_width
, p_svg
->i_height
);
491 static void FreeString( svg_rendition_t
*p_svg
)
493 free( p_svg
->psz_text
);
494 /* p_svg->p_rendition is a GdkPixbuf, and its allocation is
495 managed through ref. counting */
496 if( p_svg
->p_rendition
)
497 g_object_unref( p_svg
->p_rendition
);