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