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