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