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