]> git.sesse.net Git - vlc/blob - modules/misc/svg.c
0a659fcf75efcca4473505bc6fb75b38260403d5
[vlc] / modules / misc / svg.c
1 /*****************************************************************************
2  * svg.c : Put SVG on the video
3  *****************************************************************************
4  * Copyright (C) 2002, 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Aubert <oaubert@lisi.univ-lyon1.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc( ), free( ) */
28 #include <string.h>
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33
34 #include <vlc/vlc.h>
35 #include <vlc/vout.h>
36 #include "vlc_osd.h"
37 #include "vlc_block.h"
38 #include "vlc_filter.h"
39
40 #include <librsvg-2/librsvg/rsvg.h>
41
42 typedef struct svg_rendition_t svg_rendition_t;
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int  Create    ( vlc_object_t * );
48 static void Destroy   ( vlc_object_t * );
49 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
50                        subpicture_region_t *p_region_in );
51
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56
57 #define TEMPLATE_TEXT N_( "SVG template file" )
58 #define TEMPLATE_LONGTEXT N_( "Location of a file holding a SVG template for automatic string conversion" )
59
60 vlc_module_begin();
61  set_category( CAT_INPUT);
62  set_category( SUBCAT_INPUT_SCODEC );
63  set_capability( "text renderer", 101 );
64  add_shortcut( "svg" );
65  add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, VLC_TRUE );
66  set_callbacks( Create, Destroy );
67 vlc_module_end();
68
69 /**
70    Describes a SVG string to be displayed on the video
71 */
72 struct svg_rendition_t
73 {
74     int            i_width;
75     int            i_height;
76     int            i_chroma;
77     /** The SVG source associated with this subpicture */
78     byte_t        *psz_text;
79     /* The rendered SVG, as a GdkPixbuf */
80     GdkPixbuf      *p_rendition;
81 };
82
83 static int Render( filter_t *, subpicture_region_t *, svg_rendition_t *, int, int);
84 static byte_t *svg_GetTemplate ();
85 static void svg_set_size( filter_t *p_filter, int width, int height );
86 static void svg_SizeCallback  ( int *width, int *height, gpointer data );
87 static void svg_RenderPicture ( filter_t *p_filter,
88                                 svg_rendition_t *p_svg );
89 static void FreeString( svg_rendition_t * );
90
91 /*****************************************************************************
92  * filter_sys_t: svg local data
93  *****************************************************************************
94  * This structure is part of the filter thread descriptor.
95  * It describes the svg specific properties of an output thread.
96  *****************************************************************************/
97 struct filter_sys_t
98 {
99     /* The SVG template used to convert strings */
100     byte_t        *psz_template;
101     /* Default size for rendering. Initialized to the output size. */
102     int            i_width;
103     int            i_height;
104     vlc_mutex_t   *lock;
105 };
106
107 /*****************************************************************************
108  * Create: allocates svg video thread output method
109  *****************************************************************************
110  * This function allocates and initializes a  vout method.
111  *****************************************************************************/
112 static int Create( vlc_object_t *p_this )
113 {
114     filter_t *p_filter = (filter_t *)p_this;
115     filter_sys_t *p_sys;
116
117     /* Allocate structure */
118     p_sys = malloc( sizeof( filter_sys_t ) );
119     if( !p_sys )
120     {
121         msg_Err( p_filter, "Out of memory" );
122         return VLC_ENOMEM;
123     }
124
125     /* Initialize psz_template */
126     p_sys->psz_template = svg_GetTemplate( p_this );
127     if( !p_sys->psz_template )
128     {
129         msg_Err( p_filter, "Out of memory" );
130         return VLC_ENOMEM;
131     }
132
133     p_sys->i_width = p_filter->fmt_out.video.i_width;
134     p_sys->i_height = p_filter->fmt_out.video.i_height;
135
136     p_filter->pf_render_text = RenderText;
137     p_filter->p_sys = p_sys;
138
139     /* MUST call this before any RSVG funcs */
140     g_type_init ();
141
142     return VLC_SUCCESS;
143 }
144
145 static byte_t *svg_GetTemplate( vlc_object_t *p_this )
146 {
147     filter_t *p_filter = (filter_t *)p_this;
148     char *psz_filename;
149     char *psz_template;
150     FILE *file;
151
152     psz_filename = config_GetPsz( p_filter, "svg-template-file" );
153     if( !psz_filename || psz_filename[0] == 0 )
154     {
155         /* No filename. Use a default value. */
156         psz_template = NULL;
157     }
158     else
159     {
160         /* Read the template */
161         file = fopen( psz_filename, "rt" );
162         if( !file )
163         {
164             msg_Warn( p_this, "SVG template file %s does not exist.", psz_filename );
165             psz_template = NULL;
166         }
167         else
168         {
169             struct stat s;
170             int i_ret;
171
172             i_ret = lstat( psz_filename, &s );
173             if( i_ret )
174             {
175                 /* Problem accessing file information. Should not
176                    happen as we could open it. */
177                 psz_template = NULL;
178             }
179             else
180             {
181                 msg_Dbg( p_this, "Reading %ld bytes from template %s\n", (long)s.st_size, psz_filename );
182
183                 psz_template = malloc( s.st_size + 42 );
184                 if( !psz_template )
185                 {
186                     msg_Err( p_filter, "Out of memory" );
187                     return NULL;
188                 }
189                 memset( psz_template, 0, s.st_size + 1 );
190                 fread( psz_template, s.st_size, 1, file );
191                 fclose( file );
192             }
193         }
194     }
195     if( !psz_template )
196     {
197         /* Either there was no file, or there was an error.
198            Use the default value */
199         psz_template = strdup( "<?xml version='1.0' encoding='UTF-8' standalone='no'?> \
200 <svg version='1' preserveAspectRatio='xMinYMin meet' viewBox='0 0 800 600'> \
201   <text x='10' y='560' fill='white' font-size='32'  \
202         font-family='sans-serif'>%s</text></svg>" );
203     }
204
205     return psz_template;
206 }
207
208 /*****************************************************************************
209  * Destroy: destroy Clone video thread output method
210  *****************************************************************************
211  * Clean up all data and library connections
212  *****************************************************************************/
213 static void Destroy( vlc_object_t *p_this )
214 {
215     filter_t *p_filter = (filter_t *)p_this;
216     filter_sys_t *p_sys = p_filter->p_sys;
217
218     free( p_sys->psz_template );
219     free( p_sys );
220 }
221
222 /*****************************************************************************
223  * Render: render SVG in picture
224  *****************************************************************************/
225 static int Render( filter_t *p_filter, subpicture_region_t *p_region,
226                    svg_rendition_t *p_svg, int i_width, int i_height )
227 {
228     video_format_t fmt;
229     uint8_t *p_y, *p_u, *p_v, *p_a;
230     int x, y, i_pitch, i_u_pitch;
231     guchar *pixels_in = NULL;
232     int rowstride_in;
233     int channels_in;
234     int alpha;
235     picture_t *p_pic;
236     subpicture_region_t *p_region_tmp;
237
238     if ( p_filter->p_sys->i_width != i_width ||
239          p_filter->p_sys->i_height != i_height )
240     {
241         svg_set_size( p_filter, i_width, i_height );
242         p_svg->p_rendition = NULL;
243     }
244
245     if( p_svg->p_rendition == NULL ) {
246         svg_RenderPicture( p_filter, p_svg );
247         /* FIXME: should do a check here to ensure that
248            the rendition went OK */
249     }
250     i_width = gdk_pixbuf_get_width( p_svg->p_rendition );
251     i_height = gdk_pixbuf_get_height( p_svg->p_rendition );
252
253     /* Create a new subpicture region */
254     memset( &fmt, 0, sizeof(video_format_t) );
255     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
256     fmt.i_aspect = VOUT_ASPECT_FACTOR;
257     fmt.i_width = fmt.i_visible_width = i_width;
258     fmt.i_height = fmt.i_visible_height = i_height;
259     fmt.i_x_offset = fmt.i_y_offset = 0;
260     p_region_tmp = spu_CreateRegion( p_filter, &fmt );
261     if( !p_region_tmp )
262     {
263         msg_Err( p_filter, "cannot allocate SPU region" );
264         return VLC_EGENERIC;
265     }
266     p_region->fmt = p_region_tmp->fmt;
267     p_region->picture = p_region_tmp->picture;
268     free( p_region_tmp );
269
270     p_region->i_x = p_region->i_y = 0;
271     p_y = p_region->picture.Y_PIXELS;
272     p_u = p_region->picture.U_PIXELS;
273     p_v = p_region->picture.V_PIXELS;
274     p_a = p_region->picture.A_PIXELS;
275
276     i_pitch = p_region->picture.Y_PITCH;
277     i_u_pitch = p_region->picture.U_PITCH;
278
279     /* Initialize the region pixels (only the alpha will be changed later) */
280     memset( p_y, 0x00, i_pitch * p_region->fmt.i_height );
281     memset( p_u, 0x80, i_u_pitch * p_region->fmt.i_height );
282     memset( p_v, 0x80, i_u_pitch * p_region->fmt.i_height );
283
284     p_pic = &(p_region->picture);
285
286     /* Copy the data */
287
288     /* This rendering code is in no way optimized. If someone has some time to
289        make it work faster or better, please do.
290     */
291
292     /*
293       p_pixbuf->get_rowstride() is the number of bytes in a line.
294       p_pixbuf->get_height() is the number of lines.
295
296       The number of bytes of p_pixbuf->p_pixels is get_rowstride * get_height
297
298       if( has_alpha() ) {
299       alpha = pixels [ n_channels * ( y*rowstride + x ) + 3 ];
300       }
301       red   = pixels [ n_channels * ( y*rowstride ) + x ) ];
302       green = pixels [ n_channels * ( y*rowstride ) + x ) + 1 ];
303       blue  = pixels [ n_channels * ( y*rowstride ) + x ) + 2 ];
304     */
305
306     pixels_in = gdk_pixbuf_get_pixels( p_svg->p_rendition );
307     rowstride_in = gdk_pixbuf_get_rowstride( p_svg->p_rendition );
308     channels_in = gdk_pixbuf_get_n_channels( p_svg->p_rendition );
309     alpha = gdk_pixbuf_get_has_alpha( p_svg->p_rendition );
310
311     /*
312       This crashes the plugin (if !alpha). As there is always an alpha value,
313       it does not matter for the moment :
314
315     if( !alpha )
316       memset( p_a, 0xFF, i_pitch * p_region->fmt.i_height );
317     */
318  
319 #define INDEX_IN( x, y ) ( y * rowstride_in + x * channels_in )
320 #define INDEX_OUT( x, y ) ( y * i_pitch + x * p_pic->p[Y_PLANE].i_pixel_pitch )
321   
322     for( y = 0; y < i_height; y++ )
323     {
324         for( x = 0; x < i_width; x++ )
325         {
326             guchar *p_in;
327             int i_out;
328  
329             p_in = &pixels_in[INDEX_IN( x, y )];
330             
331 #define R( pixel ) *pixel
332 #define G( pixel ) *( pixel+1 )
333 #define B( pixel ) *( pixel+2 )
334 #define ALPHA( pixel ) *( pixel+3 )
335  
336             /* From http://www.geocrawler.com/archives/3/8263/2001/6/0/6020594/ :
337                Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
338                U = -0.1687 * r  - 0.3313 * g + 0.5 * b + 128
339                V = 0.5   * r - 0.4187 * g - 0.0813 * b + 128
340             */
341             if ( alpha ) {
342                 i_out = INDEX_OUT( x, y );
343                 
344                 p_pic->Y_PIXELS[i_out] = .299 * R( p_in ) + .587 * G( p_in ) + .114 * B( p_in );
345                 
346                 p_pic->U_PIXELS[i_out] = -.1687 * R( p_in ) - .3313 * G( p_in ) + .5 * B( p_in ) + 128;
347                 p_pic->V_PIXELS[i_out] = .5 * R( p_in ) - .4187 * G( p_in ) - .0813 * B( p_in ) + 128;
348
349                 p_pic->A_PIXELS[i_out] = ALPHA( p_in );
350             }
351         }
352     }
353
354     return VLC_SUCCESS;
355 }
356
357 static void svg_set_size( filter_t *p_filter, int width, int height )
358 {
359   p_filter->p_sys->i_width = width;
360   p_filter->p_sys->i_height = height;
361 }
362
363 static void svg_SizeCallback( int *width, int *height, gpointer data )
364 {
365     filter_t *p_filter = data;
366
367     *width = p_filter->p_sys->i_width;
368     *height = p_filter->p_sys->i_height;
369     return;
370 }
371
372 static void svg_RenderPicture( filter_t *p_filter,
373                                svg_rendition_t *p_svg )
374 {
375     /* Render the SVG string p_string->psz_text into a new picture_t
376        p_string->p_rendition with dimensions ( ->i_width, ->i_height ) */
377     RsvgHandle *p_handle;
378     GError *error = NULL;
379
380     p_handle = rsvg_handle_new();
381
382     rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_filter, NULL );
383
384     rsvg_handle_write( p_handle,
385                        p_svg->psz_text, strlen( p_svg->psz_text ) + 1,
386                        &error );
387     if( error != NULL )
388     {
389         msg_Err( p_filter, "Error in handle_write: %s\n", error->message );
390         return;
391     }
392     rsvg_handle_close( p_handle, &error );
393
394     p_svg->p_rendition = rsvg_handle_get_pixbuf( p_handle );
395     rsvg_handle_free( p_handle );
396 }
397
398
399 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
400                        subpicture_region_t *p_region_in )
401 {
402     filter_sys_t *p_sys = p_filter->p_sys;
403     svg_rendition_t *p_svg = NULL;
404     char *psz_string;
405
406     /* Sanity check */
407     if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
408     psz_string = p_region_in->psz_text;
409     if( !psz_string || !*psz_string ) return VLC_EGENERIC;
410
411     p_svg = ( svg_rendition_t * )malloc( sizeof( svg_rendition_t ) );
412     if( !p_svg )
413     {
414         msg_Err( p_filter, "Out of memory" );
415         return VLC_ENOMEM;
416     }
417
418     p_region_out->i_x = p_region_in->i_x;
419     p_region_out->i_y = p_region_in->i_y;
420
421     /* Check if the data is SVG or pure text. In the latter case,
422        convert the text to SVG. FIXME: find a better test */
423     if( strstr( psz_string, "<svg" ))
424     {
425         /* Data is SVG: duplicate */
426         p_svg->psz_text = strdup( psz_string );
427         if( !p_svg->psz_text )
428         {
429             msg_Err( p_filter, "Out of memory" );
430             free( p_svg );
431             return VLC_ENOMEM;
432         }
433     }
434     else
435     {
436         /* Data is text. Convert to SVG */
437         int length;
438         byte_t* psz_template = p_sys->psz_template;
439         length = strlen( psz_string ) + strlen( psz_template ) + 42;
440         p_svg->psz_text = malloc( length + 1 );
441         if( !p_svg->psz_text )
442         {
443             msg_Err( p_filter, "Out of memory" );
444             free( p_svg );
445             return VLC_ENOMEM;
446         }
447         memset( p_svg->psz_text, 0, length + 1 );
448         snprintf( p_svg->psz_text, length, psz_template, psz_string );
449     }
450     p_svg->i_width = p_sys->i_width;
451     p_svg->i_height = p_sys->i_height;
452     p_svg->i_chroma = VLC_FOURCC('Y','U','V','A');
453
454     /* Render the SVG.
455        The input data is stored in the p_string structure,
456        and the function updates the p_rendition attribute. */
457     svg_RenderPicture( p_filter, p_svg );
458
459     Render( p_filter, p_region_out, p_svg, p_svg->i_width, p_svg->i_height );
460     FreeString( p_svg );
461
462     return VLC_SUCCESS;
463 }
464
465 static void FreeString( svg_rendition_t *p_svg )
466 {
467     free( p_svg->psz_text );
468     free( p_svg->p_rendition );
469     free( p_svg );
470 }