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 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.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 /*****************************************************************************
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 /*****************************************************************************
59 *****************************************************************************/
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
)
69 add_integer_with_range( "svg-width", -1, 1, 65535,
70 TEXT_WIDTH
, LONG_TEXT_WIDTH
, false )
72 add_integer_with_range( "svg-height", -1, 1, 65535,
73 TEXT_HEIGHT
, LONG_TEXT_HEIGHT
, false )
76 add_float( "svg-scale", -1.0, TEXT_SCALE
, LONG_TEXT_SCALE
, false )
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
)
96 decoder_sys_t
*p_sys
= malloc( sizeof(decoder_sys_t
) );
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)
110 /* Set output properties */
111 p_dec
->fmt_out
.i_cat
= VIDEO_ES
;
112 p_dec
->fmt_out
.i_codec
= VLC_CODEC_BGRA
;
115 p_dec
->pf_decode
= DecodeBlock
;
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
;
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
);
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
);
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
;
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
) )
197 p_pic
= decoder_NewPicture( p_dec
);
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
,
209 p_pic
->p
[0].i_pitch
);
212 picture_Release( p_pic
);
218 cr
= cairo_create( surface
);
221 picture_Release( p_pic
);
226 if ( i_width
!= dim
.width
|| i_height
!= dim
.height
)
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
;
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
);
248 p_pic
->date
= p_block
->i_pts
> VLC_TS_INVALID
? p_block
->i_pts
: p_block
->i_dts
;
252 g_object_unref( G_OBJECT( rsvg
) );
256 cairo_surface_destroy( surface
);
258 block_Release( p_block
);
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)