player: osd: fix zoom
[vlc.git] / modules / codec / svg.c
bloba39200bbe65610a55a97276e420a6838cdf11797
1 /*****************************************************************************
2 * svg.c: svg decoder module making use of librsvg2.
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
6 * Authors: Adam Leggett <adamvleggett@gmail.com>
7 * Jean-Paul Saman <jpsaman@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
35 #include <glib.h>
36 #include <glib/gstdio.h>
37 #include <glib-object.h> /* g_object_unref( ) */
39 #include <librsvg/rsvg.h>
40 #include <cairo/cairo.h>
42 /*****************************************************************************
43 * Local prototypes
44 *****************************************************************************/
45 static int OpenDecoder ( vlc_object_t * );
46 static void CloseDecoder ( vlc_object_t * );
48 static int DecodeBlock ( decoder_t *, block_t * );
50 #define TEXT_WIDTH N_("Image width")
51 #define LONG_TEXT_WIDTH N_("Specify the width to decode the image too")
52 #define TEXT_HEIGHT N_("Image height")
53 #define LONG_TEXT_HEIGHT N_("Specify the height to decode the image too")
54 #define TEXT_SCALE N_("Scale factor")
55 #define LONG_TEXT_SCALE N_("Scale factor to apply to image")
57 /*****************************************************************************
58 * Module descriptor
59 *****************************************************************************/
60 vlc_module_begin ()
61 set_category( CAT_INPUT )
62 set_subcategory( SUBCAT_INPUT_VCODEC )
63 set_description( N_("SVG video decoder") )
64 set_capability( "video decoder", 100 )
65 set_callbacks( OpenDecoder, CloseDecoder )
66 add_shortcut( "svg" )
68 /* svg options */
69 add_integer_with_range( "svg-width", -1, 1, 65535,
70 TEXT_WIDTH, LONG_TEXT_WIDTH, false )
71 change_safe()
72 add_integer_with_range( "svg-height", -1, 1, 65535,
73 TEXT_HEIGHT, LONG_TEXT_HEIGHT, false )
74 change_safe()
76 add_float( "svg-scale", -1.0, TEXT_SCALE, LONG_TEXT_SCALE, false )
77 vlc_module_end ()
79 typedef struct
81 int32_t i_width;
82 int32_t i_height;
83 double f_scale;
84 } decoder_sys_t;
86 /*****************************************************************************
87 * OpenDecoder: probe the decoder and return score
88 *****************************************************************************/
89 static int OpenDecoder( vlc_object_t *p_this )
91 decoder_t *p_dec = (decoder_t*)p_this;
93 if( p_dec->fmt_in.i_codec != VLC_CODEC_SVG )
94 return VLC_EGENERIC;
96 decoder_sys_t *p_sys = malloc( sizeof(decoder_sys_t) );
97 if (!p_sys)
98 return VLC_ENOMEM;
99 p_dec->p_sys = p_sys;
101 p_sys->i_width = var_InheritInteger( p_this, "svg-width" );
102 p_sys->i_height = var_InheritInteger( p_this, "svg-height" );
103 p_sys->f_scale = var_InheritFloat( p_this, "svg-scale" );
105 /* Initialize library */
106 #if (GLIB_MAJOR_VERSION < 2 || GLIB_MINOR_VERSION < 36)
107 g_type_init();
108 #endif
110 /* Set output properties */
111 p_dec->fmt_out.i_codec = VLC_CODEC_BGRA;
113 /* Set callbacks */
114 p_dec->pf_decode = DecodeBlock;
116 return VLC_SUCCESS;
119 /****************************************************************************
120 * DecodeBlock: the whole thing
121 ****************************************************************************
122 * This function must be fed with a complete image.
123 ****************************************************************************/
124 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
126 decoder_sys_t *p_sys = (decoder_sys_t *) p_dec->p_sys;
127 picture_t *p_pic = NULL;
128 int32_t i_width, i_height;
130 RsvgHandle *rsvg = NULL;
131 cairo_surface_t *surface = NULL;
132 cairo_t *cr = NULL;
134 if( p_block == NULL ) /* No Drain */
135 return VLCDEC_SUCCESS;
137 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED)
139 block_Release( p_block );
140 return VLCDEC_SUCCESS;
143 rsvg = rsvg_handle_new_from_data( p_block->p_buffer, p_block->i_buffer, NULL );
144 if( !rsvg )
145 goto done;
147 RsvgDimensionData dim;
148 rsvg_handle_get_dimensions( rsvg, &dim );
150 if( p_sys->f_scale > 0.0 )
152 i_width = (int32_t)(p_sys->f_scale * dim.width);
153 i_height = (int32_t)(p_sys->f_scale * dim.height);
155 else
157 /* Keep aspect */
158 if( p_sys->i_width < 0 && p_sys->i_height > 0 )
160 i_width = dim.width * p_sys->i_height / dim.height;
161 i_height = p_sys->i_height;
163 else if( p_sys->i_width > 0 && p_sys->i_height < 0 )
165 i_width = p_sys->i_width;
166 i_height = dim.height * p_sys->i_width / dim.height;
168 else if( p_sys->i_width > 0 && p_sys->i_height > 0 )
170 i_width = dim.width * p_sys->i_height / dim.height;
171 i_height = p_sys->i_height;
173 else
175 i_width = dim.width;
176 i_height = dim.height;
180 p_dec->fmt_out.i_codec =
181 p_dec->fmt_out.video.i_chroma = VLC_CODEC_BGRA;
182 p_dec->fmt_out.video.i_width = i_width;
183 p_dec->fmt_out.video.i_height = i_height;
184 p_dec->fmt_out.video.i_visible_width = i_width;
185 p_dec->fmt_out.video.i_visible_height = i_height;
186 p_dec->fmt_out.video.i_sar_num = 1;
187 p_dec->fmt_out.video.i_sar_den = 1;
188 p_dec->fmt_out.video.i_rmask = 0x80800000; /* Since librsvg v1.0 */
189 p_dec->fmt_out.video.i_gmask = 0x0000ff00;
190 p_dec->fmt_out.video.i_bmask = 0x000000ff;
191 video_format_FixRgb(&p_dec->fmt_out.video);
193 /* Get a new picture */
194 if( decoder_UpdateVideoFormat( p_dec ) )
195 goto done;
196 p_pic = decoder_NewPicture( p_dec );
197 if( !p_pic )
198 goto done;
200 /* NOTE: Do not use the stride calculation from cairo, because it is wrong:
201 * stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, dim.width);
202 * Use the stride from VLC its picture_t::p[0].i_pitch, which is correct.
204 memset(p_pic->p[0].p_pixels, 0, p_pic->p[0].i_pitch * p_pic->p[0].i_lines);
205 surface = cairo_image_surface_create_for_data( p_pic->p->p_pixels,
206 CAIRO_FORMAT_ARGB32,
207 i_width, i_height,
208 p_pic->p[0].i_pitch );
209 if( !surface )
211 picture_Release( p_pic );
212 p_pic = NULL;
213 goto done;
216 /* Decode picture */
217 cr = cairo_create( surface );
218 if( !cr )
220 picture_Release( p_pic );
221 p_pic = NULL;
222 goto done;
225 if ( i_width != dim.width || i_height != dim.height )
227 double sw, sh;
228 if ( p_sys->f_scale > 0.0 && !(p_sys->i_width > 0 || p_sys->i_height > 0) )
229 sw = sh = p_sys->f_scale;
230 else
232 double aspect = (double) (dim.width * p_dec->fmt_out.video.i_sar_num) /
233 (dim.height * p_dec->fmt_out.video.i_sar_den);
234 sw = aspect * i_width / dim.width;
235 sh = aspect * i_height / dim.height;
237 cairo_scale(cr, sw, sh);
240 if( !rsvg_handle_render_cairo( rsvg, cr ) )
242 picture_Release( p_pic );
243 p_pic = NULL;
244 goto done;
247 p_pic->date = p_block->i_pts != VLC_TICK_INVALID ? p_block->i_pts : p_block->i_dts;
249 done:
250 if( rsvg )
251 g_object_unref( G_OBJECT( rsvg ) );
252 if( cr )
253 cairo_destroy( cr );
254 if( surface )
255 cairo_surface_destroy( surface );
257 block_Release( p_block );
258 if( p_pic != NULL )
259 decoder_QueueVideo( p_dec, p_pic );
260 return VLCDEC_SUCCESS;
263 /*****************************************************************************
264 * CloseDecoder: png decoder destruction
265 *****************************************************************************/
266 static void CloseDecoder( vlc_object_t *p_this )
268 VLC_UNUSED( p_this );
269 #if (GLIB_MAJOR_VERSION < 2 || GLIB_MINOR_VERSION < 36)
270 rsvg_term();
271 #endif