]> git.sesse.net Git - vlc/blob - modules/misc/svg.c
9f25f303b05738ed6d4aaa8f0875e1aa5e563477
[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
37 #include <librsvg-2/librsvg/rsvg.h>
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  Create    ( vlc_object_t * );
43 static void Destroy   ( vlc_object_t * );
44 static void Render    ( vout_thread_t *, picture_t *,
45                         const subpicture_t * );
46 static subpicture_t *AddText ( vout_thread_t *p_vout, int i_channel,
47                                char *psz_string, text_style_t *p_style, int i_flags,
48                                int i_hmargin, int i_vmargin, mtime_t i_start,
49                                mtime_t i_stop );
50 static byte_t *svg_GetTemplate ();
51 static void svg_SizeCallback  (int *width, int *height, gpointer data );
52 static void svg_RenderPicture (vout_thread_t *p_vout,
53                                subpicture_sys_t *p_string );
54 static void FreeString( subpicture_t * );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59
60 #define TEMPLATE_TEXT N_( "SVG template file" )
61 #define TEMPLATE_LONGTEXT N_( "Location of a file holding a SVG template for automatic string conversion" )
62
63 vlc_module_begin();
64  set_capability( "text renderer", 100 );
65  add_shortcut( "svg" );
66  add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, VLC_TRUE );
67  set_callbacks( Create, Destroy );
68 vlc_module_end();
69
70 /**
71    Describes a SVG string to be displayed on the video
72 */
73 struct subpicture_sys_t
74 {
75     int            i_width;
76     int            i_height;
77     int            i_chroma;
78     /** The SVG source associated with this subpicture */
79     byte_t        *psz_text;
80     /* The rendered SVG, as a GdkPixbuf */
81     GdkPixbuf      *p_rendition;
82 };
83
84 /*****************************************************************************
85  * vout_sys_t: svg local data
86  *****************************************************************************
87  * This structure is part of the video output thread descriptor.
88  * It describes the svg specific properties of an output thread.
89  *****************************************************************************/
90 struct text_renderer_sys_t
91 {
92     /* The SVG template used to convert strings */
93     byte_t        *psz_template;
94     vlc_mutex_t   *lock;
95 };
96
97 /*****************************************************************************
98  * Create: allocates svg video thread output method
99  *****************************************************************************
100  * This function allocates and initializes a  vout method.
101  *****************************************************************************/
102 static int Create( vlc_object_t *p_this )
103 {
104     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
105
106     /* Allocate structure */
107     p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) );
108     if( p_vout->p_text_renderer_data == NULL )
109     {
110         msg_Err( p_vout, "Out of memory" );
111         return VLC_ENOMEM;
112     }
113     p_vout->pf_add_string = AddText;
114
115     /* Initialize psz_template */
116     p_vout->p_text_renderer_data->psz_template = svg_GetTemplate( p_this );
117     if( !p_vout->p_text_renderer_data->psz_template )
118     {
119         msg_Err( p_vout, "Out of memory" );
120         return VLC_ENOMEM;
121     }
122
123     return VLC_SUCCESS;
124 }
125
126 static byte_t *svg_GetTemplate( vlc_object_t *p_this )
127 {
128     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
129     char *psz_filename;
130     char *psz_template;
131     FILE *file;
132
133     psz_filename = config_GetPsz( p_vout, "svg-template-file" );
134     if( !psz_filename || psz_filename[0] == 0 )
135     {
136         /* No filename. Use a default value. */
137         psz_template = NULL;
138     }
139     else
140     {
141         /* Read the template */
142         file = fopen( psz_filename, "rt" );
143         if( !file )
144         {
145             msg_Warn( p_this, "SVG template file %s does not exist.", psz_filename );
146             psz_template = NULL;
147         }
148         else
149         {
150             struct stat s;
151             int i_ret;
152
153             i_ret = lstat( psz_filename, &s );
154             if( i_ret )
155             {
156                 /* Problem accessing file information. Should not
157                    happen as we could open it. */
158                 psz_template = NULL;
159             }
160             else
161             {
162                 fprintf( stderr, "Reading %ld bytes from %s\n", (long)s.st_size, psz_filename );
163
164                 psz_template = malloc( s.st_size + 42 );
165                 if( !psz_template )
166                 {
167                     msg_Err( p_vout, "Out of memory" );
168                     return NULL;
169                 }
170                 fread( psz_template, s.st_size, 1, file );
171                 fclose( file );
172             }
173         }
174     }
175     if( !psz_template )
176     {
177         /* Either there was no file, or there was an error.
178            Use the default value */
179         psz_template = strdup( "<?xml version='1.0' encoding='UTF-8' standalone='no'?> \
180 <svg version='1' preserveAspectRatio='xMinYMin meet' viewBox='0 0 800 600'> \
181   <text x='10' y='560' fill='white' font-size='32'  \
182         font-family='sans-serif'>%s</text></svg>" );
183     }
184
185     return psz_template;
186 }
187
188 /*****************************************************************************
189  * Destroy: destroy Clone video thread output method
190  *****************************************************************************
191  * Clean up all data and library connections
192  *****************************************************************************/
193 static void Destroy( vlc_object_t *p_this )
194 {
195     vout_thread_t *p_vout = ( vout_thread_t * )p_this;
196     free( p_vout->p_text_renderer_data->psz_template );
197     free( p_vout->p_text_renderer_data );
198 }
199
200 /*****************************************************************************
201  * Render: render SVG in picture
202  *****************************************************************************
203  * This function merges the previously rendered SVG subpicture into a picture
204  *****************************************************************************/
205 static void Render( vout_thread_t *p_vout, picture_t *p_pic,
206                     const subpicture_t *p_subpic )
207 {
208     subpicture_sys_t *p_string = p_subpic->p_sys;
209     guchar *pixels_in = NULL;
210     guchar *pixels_out = NULL;
211     int rowstride_in, rowstride_out;
212     int channels_in, channels_out;
213     int x, y;
214     int i_width, i_height;
215     int alpha;
216
217     if( p_string->p_rendition == NULL ) {
218         /* Something changed ( presumably the dimensions ). Get the new
219            dimensions and update the pixbuf */
220         p_string->i_width = p_vout->output.i_width;
221         p_string->i_height = p_vout->output.i_height;
222         svg_RenderPicture( p_vout, p_string );
223     }
224
225     /* This rendering code is in no way optimized. If someone has some
226        time to lose to make it work faster, please do.
227     */
228
229     /* FIXME: The alpha value is not taken into account. */
230
231     /*
232       p_pixbuf->get_rowstride() is the number of bytes in a line.
233       p_pixbuf->get_height() is the number of lines.
234
235       The number of bytes of p_pixbuf->p_pixels is get_rowstride * get_height
236
237       if( has_alpha() ) {
238       alpha = pixels [ n_channels * ( y*rowstride + x ) + 3 ];
239       }
240       red   = pixels [ n_channels * ( y*rowstride ) + x ) ];
241       green = pixels [ n_channels * ( y*rowstride ) + x ) + 1 ];
242       blue  = pixels [ n_channels * ( y*rowstride ) + x ) + 2 ];
243     */
244
245     pixels_in = gdk_pixbuf_get_pixels( p_string->p_rendition );
246     pixels_out = p_pic->p->p_pixels;
247
248     rowstride_in = gdk_pixbuf_get_rowstride( p_string->p_rendition );
249     rowstride_out = p_pic->p->i_pitch;
250
251     channels_in = gdk_pixbuf_get_n_channels( p_string->p_rendition );
252     channels_out = p_pic->p->i_pixel_pitch;
253
254     alpha = gdk_pixbuf_get_has_alpha( p_string->p_rendition );
255
256 #define INDEX_IN( x, y ) ( y * rowstride_in + x * channels_in )
257 #define INDEX_OUT( x, y ) ( y * rowstride_out + x * channels_out )
258 #define UV_INDEX_OUT( x, y ) ( y * p_pic->p[U_PLANE].i_pitch / 2 + x * p_pic->p[U_PLANE].i_pixel_pitch / 2 )
259
260     i_width = gdk_pixbuf_get_width( p_string->p_rendition );
261     i_height = gdk_pixbuf_get_height( p_string->p_rendition );
262
263     switch( p_vout->output.i_chroma )
264     {
265         /* I420 target, no scaling */
266     case VLC_FOURCC( 'I','4','2','0' ):
267     case VLC_FOURCC( 'I','Y','U','V' ):
268     case VLC_FOURCC( 'Y','V','1','2' ):
269         for( y = 0; y < i_height; y++ )
270         {
271             for( x = 0; x < i_width; x++ )
272             {
273                 guchar *p_in;
274                 int i_out;
275                 int i_uv_out;
276
277                 p_in = &pixels_in[INDEX_IN( x, y )];
278
279 #define R( pixel ) *pixel
280 #define G( pixel ) *( pixel+1 )
281 #define B( pixel ) *( pixel+2 )
282 #define ALPHA( pixel ) *( pixel+3 )
283
284                 /* From http://www.geocrawler.com/archives/3/8263/2001/6/0/6020594/ :
285                    Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
286                    U = -0.1687 * r  - 0.3313 * g + 0.5 * b + 128
287                    V = 0.5   * r - 0.4187 * g - 0.0813 * b + 128
288                 */
289                 if( (alpha && ALPHA( p_in ) > 10 ) || ( ! alpha )) {
290                     i_out = INDEX_OUT( x, y );
291
292                     p_pic->p[Y_PLANE].p_pixels[i_out] = .299 * R( p_in ) + .587 * G( p_in ) + .114 * B( p_in );
293
294                     if( ( x % 2 == 0 ) && ( y % 2 == 0 ) ) {
295                         i_uv_out = UV_INDEX_OUT( x, y );
296
297                         p_pic->p[U_PLANE].p_pixels[i_uv_out] = -.1687 * R( p_in ) - .3313 * G( p_in ) + .5 * B( p_in ) + 128;
298                         p_pic->p[V_PLANE].p_pixels[i_uv_out] = .5 * R( p_in ) - .4187 * G( p_in ) - .0813 * B( p_in ) + 128;
299                     }
300                 }
301             }
302         }
303         break;
304
305         /* RV32 target, scaling */
306     case VLC_FOURCC( 'R','V','2','4' ):
307     case VLC_FOURCC( 'R','V','3','2' ):
308         for( y = 0; y < i_height; y++ )
309         {
310             for( x = 0; x < i_width; x++ )
311             {
312                 guchar *p_in;
313                 guchar *p_out;
314
315                 p_in = &pixels_in[INDEX_IN( x, y )];
316                 p_out = &pixels_out[INDEX_OUT( x, y )];
317
318                 *p_out = *p_in;
319                 *( p_out+1 ) = *( p_in+1 );
320                 *( p_out+2 ) = *( p_in+2 );
321             }
322         }
323         break;
324
325     default:
326         msg_Err( p_vout, "unknown chroma, can't render SVG" );
327         break;
328     }
329 }
330
331 static void svg_SizeCallback( int *width, int *height, gpointer data )
332 {
333     subpicture_sys_t *p_string = data;
334
335     *width = p_string->i_width;
336     *height = p_string->i_height;
337     return;
338 }
339
340 static void svg_RenderPicture( vout_thread_t *p_vout,
341                                subpicture_sys_t *p_string )
342 {
343     /* Render the SVG string p_string->psz_text into a new picture_t
344        p_string->p_rendition with dimensions ( ->i_width, ->i_height ) */
345     RsvgHandle *p_handle;
346     GError *error;
347
348     p_handle = rsvg_handle_new();
349
350     rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_string, NULL );
351
352     rsvg_handle_write( p_handle,
353                        p_string->psz_text, strlen( p_string->psz_text ) + 1,
354                        &error );
355     rsvg_handle_close( p_handle, &error );
356
357     p_string->p_rendition = rsvg_handle_get_pixbuf( p_handle );
358     rsvg_handle_free( p_handle );
359 }
360
361
362 /**
363  * This function receives a SVG string and creates a subpicture for it.
364  * It is used as pf_add_string callback in the vout method by this module.
365  */
366 static subpicture_t *AddText ( vout_thread_t *p_vout, int i_channel,
367                                char *psz_string, text_style_t *p_style, int i_flags,
368                                int i_hmargin, int i_vmargin, mtime_t i_start,
369                                mtime_t i_stop )
370 {
371     subpicture_sys_t *p_string;
372     subpicture_t *p_subpic;
373
374     msg_Dbg( p_vout, "adding string \"%s\" start_date "I64Fd
375              " end_date" I64Fd, psz_string, i_start, i_stop );
376
377     /* Create and initialize a subpicture */
378     p_subpic = vout_CreateSubPicture( p_vout, i_channel, GRAPH_CONTENT,
379                                       MEMORY_SUBPICTURE );
380     if( p_subpic == NULL )
381     {
382         return NULL;
383     }
384
385     p_subpic->pf_render = Render;
386     p_subpic->pf_destroy = FreeString;
387     p_subpic->i_start = i_start;
388     p_subpic->i_stop = i_stop;
389     if( i_stop == 0 )
390     {
391         p_subpic->b_ephemer = VLC_TRUE;
392     }
393     else
394     {
395         p_subpic->b_ephemer = VLC_FALSE;
396     }
397
398     /* Create and initialize private data for the subpicture */
399     p_string = malloc( sizeof( subpicture_sys_t ) );
400     if( p_string == NULL )
401     {
402         vout_DestroySubPicture( p_vout, p_subpic );
403         return NULL;
404     }
405     p_subpic->p_sys = p_string;
406
407     /* Check if the data is SVG or pure text. In the latter case,
408        convert the text to SVG. FIXME: find a better test */
409     if( strstr( psz_string, "<svg" ))
410     {
411         /* Data is SVG: duplicate */
412         p_string->psz_text = strdup( psz_string );
413     }
414     else
415     {
416         /* Data is text. Convert to SVG */
417         int length;
418         byte_t* psz_template = p_vout->p_text_renderer_data->psz_template;
419         length = strlen( psz_string ) + strlen( psz_template ) + 42;
420         p_string->psz_text = malloc( length + 1 );
421         if( p_string->psz_text == NULL )
422         {
423             return NULL;
424         }
425         snprintf( p_string->psz_text, length, psz_template, psz_string );
426     }
427
428     p_string->i_width = p_vout->output.i_width;
429     p_string->i_height = p_vout->output.i_height;
430     p_string->i_chroma = p_vout->output.i_chroma;
431
432     /* Render the SVG.
433        The input data is stored in the p_string structure,
434        and the function updates the p_rendition attribute. */
435     svg_RenderPicture( p_vout, p_string );
436
437     vout_DisplaySubPicture( p_vout, p_subpic );
438     return p_subpic;
439 }
440
441 static void FreeString( subpicture_t *p_subpic )
442 {
443     subpicture_sys_t *p_string = p_subpic->p_sys;
444
445     free( p_string->psz_text );
446     free( p_string->p_rendition );
447     free( p_string );
448 }