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