]> git.sesse.net Git - vlc/blob - modules/codec/svg.c
upnp: change item b_net and i_type
[vlc] / modules / codec / svg.c
1 /*****************************************************************************
2  * svg.c: svg decoder module making use of librsvg2.
3  *****************************************************************************
4  * Copyright (C) 2014 VLC authors and VideoLAN
5  *
6  * Authors: Adam Leggett <adamvleggett@gmail.com>
7  *          Jean-Paul Saman <jpsaman@videolan.org>
8  *
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.
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 Lesser General Public License for more details.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
34
35 #include <glib.h>
36 #include <glib/gstdio.h>
37 #include <glib-object.h>                                  /* g_object_unref( ) */
38
39 #include <librsvg/rsvg.h>
40 #include <cairo/cairo.h>
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  OpenDecoder   ( vlc_object_t * );
46 static void CloseDecoder  ( vlc_object_t * );
47
48 static picture_t *DecodeBlock  ( decoder_t *, block_t ** );
49
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")
56
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" )
67
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()
75
76     add_float( "svg-scale", -1.0, TEXT_SCALE, LONG_TEXT_SCALE, false )
77 vlc_module_end ()
78
79 struct decoder_sys_t
80 {
81     int32_t i_width;
82     int32_t i_height;
83     double  f_scale;
84 };
85
86 /*****************************************************************************
87  * OpenDecoder: probe the decoder and return score
88  *****************************************************************************/
89 static int OpenDecoder( vlc_object_t *p_this )
90 {
91     decoder_t *p_dec = (decoder_t*)p_this;
92
93     if( p_dec->fmt_in.i_codec != VLC_CODEC_SVG )
94         return VLC_EGENERIC;
95
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;
100
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" );
104
105     /* Initialize library */
106     rsvg_init();
107
108     /* Set output properties */
109     p_dec->fmt_out.i_cat = VIDEO_ES;
110     p_dec->fmt_out.i_codec = VLC_CODEC_BGRA;
111
112     /* Set callbacks */
113     p_dec->pf_decode_video = DecodeBlock;
114
115     return VLC_SUCCESS;
116 }
117
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 )
124 {
125     decoder_sys_t *p_sys  = (decoder_sys_t *) p_dec->p_sys;
126     block_t *p_block;
127     picture_t *p_pic = NULL;
128     int32_t i_width, i_height;
129
130     RsvgHandle *rsvg = NULL;
131     cairo_surface_t *surface = NULL;
132     cairo_t *cr = NULL;
133
134     if( !pp_block || !*pp_block ) return NULL;
135
136     p_block = *pp_block;
137     *pp_block = NULL;
138
139     if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
140     {
141         block_Release( p_block );
142         return NULL;
143     }
144
145     rsvg = rsvg_handle_new_from_data( p_block->p_buffer, p_block->i_buffer, NULL );
146     if( !rsvg )
147         goto done;
148
149     RsvgDimensionData dim;
150     rsvg_handle_get_dimensions( rsvg, &dim );
151
152     if( p_sys->f_scale > 0.0 )
153     {
154         i_width  = (int32_t)(p_sys->f_scale * dim.width);
155         i_height = (int32_t)(p_sys->f_scale * dim.height);
156     }
157     else
158     {
159         /* Keep aspect */
160         if( p_sys->i_width < 0 && p_sys->i_height > 0 )
161         {
162             i_width  = dim.width * p_sys->i_height / dim.height;
163             i_height = p_sys->i_height;
164         }
165         else if( p_sys->i_width > 0 && p_sys->i_height < 0 )
166         {
167             i_width  = p_sys->i_width;
168             i_height = dim.height * p_sys->i_width / dim.height;
169         }
170         else if( p_sys->i_width > 0 && p_sys->i_height > 0 )
171         {
172             i_width  = dim.width * p_sys->i_height / dim.height;
173             i_height = p_sys->i_height;
174         }
175         else
176         {
177             i_width  = dim.width;
178             i_height = dim.height;
179         }
180     }
181
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);
194
195     /* Get a new picture */
196     p_pic = decoder_NewPicture( p_dec );
197     if( !p_pic )
198         goto done;
199
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.
203      */
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 )
210     {
211         picture_Release( p_pic );
212         p_pic = NULL;
213         goto done;
214     }
215
216     /* Decode picture */
217     cr = cairo_create( surface );
218     if( !cr )
219     {
220         picture_Release( p_pic );
221         p_pic = NULL;
222         goto done;
223     }
224
225     if ( i_width != dim.width || i_height != dim.height )
226     {
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
231         {
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;
236         }
237         cairo_scale(cr, sw, sh);
238     }
239
240     if( !rsvg_handle_render_cairo( rsvg, cr ) )
241     {
242         picture_Release( p_pic );
243         p_pic = NULL;
244         goto done;
245     }
246
247     p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
248
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 );
256
257     block_Release( p_block );
258     return p_pic;
259 }
260
261 /*****************************************************************************
262  * CloseDecoder: png decoder destruction
263  *****************************************************************************/
264 static void CloseDecoder( vlc_object_t *p_this )
265 {
266     VLC_UNUSED( p_this );
267     rsvg_term();
268 }