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