configure: Improve detection of ibtool
[vlc.git] / modules / codec / svg.c
blob3da62ad9ae1132051381b4daeaff015bc4f0d717
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( "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 struct decoder_sys_t
81 int32_t i_width;
82 int32_t i_height;
83 double f_scale;
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_cat = VIDEO_ES;
112 p_dec->fmt_out.i_codec = VLC_CODEC_BGRA;
114 /* Set callbacks */
115 p_dec->pf_decode = DecodeBlock;
117 return VLC_SUCCESS;
120 /****************************************************************************
121 * DecodeBlock: the whole thing
122 ****************************************************************************
123 * This function must be fed with a complete image.
124 ****************************************************************************/
125 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
127 decoder_sys_t *p_sys = (decoder_sys_t *) p_dec->p_sys;
128 picture_t *p_pic = NULL;
129 int32_t i_width, i_height;
131 RsvgHandle *rsvg = NULL;
132 cairo_surface_t *surface = NULL;
133 cairo_t *cr = NULL;
135 if( p_block == NULL ) /* No Drain */
136 return VLCDEC_SUCCESS;
138 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED)
140 block_Release( p_block );
141 return VLCDEC_SUCCESS;
144 rsvg = rsvg_handle_new_from_data( p_block->p_buffer, p_block->i_buffer, NULL );
145 if( !rsvg )
146 goto done;
148 RsvgDimensionData dim;
149 rsvg_handle_get_dimensions( rsvg, &dim );
151 if( p_sys->f_scale > 0.0 )
153 i_width = (int32_t)(p_sys->f_scale * dim.width);
154 i_height = (int32_t)(p_sys->f_scale * dim.height);
156 else
158 /* Keep aspect */
159 if( p_sys->i_width < 0 && p_sys->i_height > 0 )
161 i_width = dim.width * p_sys->i_height / dim.height;
162 i_height = p_sys->i_height;
164 else if( p_sys->i_width > 0 && p_sys->i_height < 0 )
166 i_width = p_sys->i_width;
167 i_height = dim.height * p_sys->i_width / dim.height;
169 else if( p_sys->i_width > 0 && p_sys->i_height > 0 )
171 i_width = dim.width * p_sys->i_height / dim.height;
172 i_height = p_sys->i_height;
174 else
176 i_width = dim.width;
177 i_height = dim.height;
181 p_dec->fmt_out.i_codec =
182 p_dec->fmt_out.video.i_chroma = VLC_CODEC_BGRA;
183 p_dec->fmt_out.video.i_width = i_width;
184 p_dec->fmt_out.video.i_height = i_height;
185 p_dec->fmt_out.video.i_visible_width = i_width;
186 p_dec->fmt_out.video.i_visible_height = i_height;
187 p_dec->fmt_out.video.i_sar_num = 1;
188 p_dec->fmt_out.video.i_sar_den = 1;
189 p_dec->fmt_out.video.i_rmask = 0x80800000; /* Since librsvg v1.0 */
190 p_dec->fmt_out.video.i_gmask = 0x0000ff00;
191 p_dec->fmt_out.video.i_bmask = 0x000000ff;
192 video_format_FixRgb(&p_dec->fmt_out.video);
194 /* Get a new picture */
195 if( decoder_UpdateVideoFormat( p_dec ) )
196 goto done;
197 p_pic = decoder_NewPicture( p_dec );
198 if( !p_pic )
199 goto done;
201 /* NOTE: Do not use the stride calculation from cairo, because it is wrong:
202 * stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, dim.width);
203 * Use the stride from VLC its picture_t::p[0].i_pitch, which is correct.
205 memset(p_pic->p[0].p_pixels, 0, p_pic->p[0].i_pitch * p_pic->p[0].i_lines);
206 surface = cairo_image_surface_create_for_data( p_pic->p->p_pixels,
207 CAIRO_FORMAT_ARGB32,
208 i_width, i_height,
209 p_pic->p[0].i_pitch );
210 if( !surface )
212 picture_Release( p_pic );
213 p_pic = NULL;
214 goto done;
217 /* Decode picture */
218 cr = cairo_create( surface );
219 if( !cr )
221 picture_Release( p_pic );
222 p_pic = NULL;
223 goto done;
226 if ( i_width != dim.width || i_height != dim.height )
228 double sw, sh;
229 if ( p_sys->f_scale > 0.0 && !(p_sys->i_width > 0 || p_sys->i_height > 0) )
230 sw = sh = p_sys->f_scale;
231 else
233 double aspect = (double) (dim.width * p_dec->fmt_out.video.i_sar_num) /
234 (dim.height * p_dec->fmt_out.video.i_sar_den);
235 sw = aspect * i_width / dim.width;
236 sh = aspect * i_height / dim.height;
238 cairo_scale(cr, sw, sh);
241 if( !rsvg_handle_render_cairo( rsvg, cr ) )
243 picture_Release( p_pic );
244 p_pic = NULL;
245 goto done;
248 p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
250 done:
251 if( rsvg )
252 g_object_unref( G_OBJECT( rsvg ) );
253 if( cr )
254 cairo_destroy( cr );
255 if( surface )
256 cairo_surface_destroy( surface );
258 block_Release( p_block );
259 if( p_pic != NULL )
260 decoder_QueueVideo( p_dec, p_pic );
261 return VLCDEC_SUCCESS;
264 /*****************************************************************************
265 * CloseDecoder: png decoder destruction
266 *****************************************************************************/
267 static void CloseDecoder( vlc_object_t *p_this )
269 VLC_UNUSED( p_this );
270 #if (GLIB_MAJOR_VERSION < 2 || GLIB_MINOR_VERSION < 36)
271 rsvg_term();
272 #endif