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 picture_t
*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 */
108 /* Set output properties */
109 p_dec
->fmt_out
.i_cat
= VIDEO_ES
;
110 p_dec
->fmt_out
.i_codec
= VLC_CODEC_BGRA
;
113 p_dec
->pf_decode_video
= DecodeBlock
;
118 /****************************************************************************
119 * DecodeBlock: the whole thing
120 ****************************************************************************
121 * This function must be fed with a complete image.
122 ****************************************************************************/
123 static picture_t
*DecodeBlock( decoder_t
*p_dec
, block_t
**pp_block
)
125 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
;
134 if( !pp_block
|| !*pp_block
) return NULL
;
139 if( p_block
->i_flags
& BLOCK_FLAG_DISCONTINUITY
)
141 block_Release( p_block
);
145 rsvg
= rsvg_handle_new_from_data( p_block
->p_buffer
, p_block
->i_buffer
, NULL
);
149 RsvgDimensionData dim
;
150 rsvg_handle_get_dimensions( rsvg
, &dim
);
152 if( p_sys
->f_scale
> 0.0 )
154 i_width
= (int32_t)(p_sys
->f_scale
* dim
.width
);
155 i_height
= (int32_t)(p_sys
->f_scale
* dim
.height
);
160 if( p_sys
->i_width
< 0 && p_sys
->i_height
> 0 )
162 i_width
= dim
.width
* p_sys
->i_height
/ dim
.height
;
163 i_height
= p_sys
->i_height
;
165 else if( p_sys
->i_width
> 0 && p_sys
->i_height
< 0 )
167 i_width
= p_sys
->i_width
;
168 i_height
= dim
.height
* p_sys
->i_width
/ dim
.height
;
170 else if( p_sys
->i_width
> 0 && p_sys
->i_height
> 0 )
172 i_width
= dim
.width
* p_sys
->i_height
/ dim
.height
;
173 i_height
= p_sys
->i_height
;
178 i_height
= dim
.height
;
182 p_dec
->fmt_out
.i_codec
=
183 p_dec
->fmt_out
.video
.i_chroma
= VLC_CODEC_BGRA
;
184 p_dec
->fmt_out
.video
.i_width
= i_width
;
185 p_dec
->fmt_out
.video
.i_height
= i_height
;
186 p_dec
->fmt_out
.video
.i_visible_width
= i_width
;
187 p_dec
->fmt_out
.video
.i_visible_height
= i_height
;
188 p_dec
->fmt_out
.video
.i_sar_num
= 1;
189 p_dec
->fmt_out
.video
.i_sar_den
= 1;
190 p_dec
->fmt_out
.video
.i_rmask
= 0x80800000; /* Since librsvg v1.0 */
191 p_dec
->fmt_out
.video
.i_gmask
= 0x0000ff00;
192 p_dec
->fmt_out
.video
.i_bmask
= 0x000000ff;
193 video_format_FixRgb(&p_dec
->fmt_out
.video
);
195 /* Get a new picture */
196 p_pic
= decoder_NewPicture( p_dec
);
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
,
208 p_pic
->p
[0].i_pitch
);
211 picture_Release( p_pic
);
217 cr
= cairo_create( surface
);
220 picture_Release( p_pic
);
225 if ( i_width
!= dim
.width
|| i_height
!= dim
.height
)
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
;
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
);
247 p_pic
->date
= p_block
->i_pts
> VLC_TS_INVALID
? p_block
->i_pts
: p_block
->i_dts
;
251 g_object_unref( G_OBJECT( rsvg
) );
255 cairo_surface_destroy( surface
);
257 block_Release( p_block
);
261 /*****************************************************************************
262 * CloseDecoder: png decoder destruction
263 *****************************************************************************/
264 static void CloseDecoder( vlc_object_t
*p_this
)
266 VLC_UNUSED( p_this
);