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