svg.c 6.08 KB
Newer Older
Adam Leggett's avatar
Adam Leggett committed
1 2 3 4 5 6
/*****************************************************************************
 * svg.c: svg decoder module making use of librsvg2.
 *****************************************************************************
 * Copyright (C) 2014 VLC authors and VideoLAN
 *
 * Authors: Adam Leggett <adamvleggett@gmail.com>
Jean-Paul Saman's avatar
Jean-Paul Saman committed
7
 *          Jean-Paul Saman <jpsaman@videolan.org>
Adam Leggett's avatar
Adam Leggett committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_codec.h>

#include <glib.h>
#include <glib/gstdio.h>
#include <glib-object.h>                                  /* g_object_unref( ) */

#include <librsvg/rsvg.h>
#include <cairo/cairo.h>

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  OpenDecoder   ( vlc_object_t * );
static void CloseDecoder  ( vlc_object_t * );

static picture_t *DecodeBlock  ( decoder_t *, block_t ** );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin ()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_VCODEC )
    set_description( N_("SVG video decoder") )
    set_capability( "decoder", 100 )
    set_callbacks( OpenDecoder, CloseDecoder )
    add_shortcut( "svg" )
vlc_module_end ()

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;

    if( p_dec->fmt_in.i_codec != VLC_CODEC_SVG )
        return VLC_EGENERIC;

    /* Initialize library */
    rsvg_init();

    /* Set output properties */
    p_dec->fmt_out.i_cat = VIDEO_ES;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
77
    p_dec->fmt_out.i_codec = VLC_CODEC_RGB32;
Adam Leggett's avatar
Adam Leggett committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    /* Set callbacks */
    p_dec->pf_decode_video = DecodeBlock;

    return VLC_SUCCESS;
}

/****************************************************************************
 * DecodeBlock: the whole thing
 ****************************************************************************
 * This function must be fed with a complete image.
 ****************************************************************************/
static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
{
    block_t *p_block;
    picture_t *p_pic = NULL;

    RsvgHandle *rsvg = NULL;
    cairo_surface_t *surface = NULL;
    cairo_t *cr = NULL;

    if( !pp_block || !*pp_block ) return NULL;

    p_block = *pp_block;
    *pp_block = NULL;

    if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
    {
        block_Release( p_block );
        return NULL;
    }

Jean-Paul Saman's avatar
Jean-Paul Saman committed
110
    rsvg = rsvg_handle_new_from_data( p_block->p_buffer, p_block->i_buffer, NULL );
Adam Leggett's avatar
Adam Leggett committed
111 112 113 114
    if( !rsvg )
        goto done;

    RsvgDimensionData dim;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
115
    rsvg_handle_get_dimensions( rsvg, &dim );
Adam Leggett's avatar
Adam Leggett committed
116

Jean-Paul Saman's avatar
Jean-Paul Saman committed
117 118
    p_dec->fmt_out.video.i_chroma = VLC_CODEC_RGB32;
    p_dec->fmt_out.video.i_width  = dim.width;
Adam Leggett's avatar
Adam Leggett committed
119
    p_dec->fmt_out.video.i_height = dim.height;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
120 121
    p_dec->fmt_out.video.i_visible_width  = dim.width;
    p_dec->fmt_out.video.i_visible_height = dim.height;
Adam Leggett's avatar
Adam Leggett committed
122 123
    p_dec->fmt_out.video.i_sar_num = 1;
    p_dec->fmt_out.video.i_sar_den = 1;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
124
    p_dec->fmt_out.video.i_rmask = 0x80800000; /* Since librsvg v1.0 */
Adam Leggett's avatar
Adam Leggett committed
125
    p_dec->fmt_out.video.i_gmask = 0x0000ff00;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
126 127
    p_dec->fmt_out.video.i_bmask = 0x000000ff;
    video_format_FixRgb(&p_dec->fmt_out.video);
Adam Leggett's avatar
Adam Leggett committed
128 129 130 131 132 133

    /* Get a new picture */
    p_pic = decoder_NewPicture( p_dec );
    if( !p_pic )
        goto done;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
134 135 136 137 138 139 140 141
    /* NOTE: Do not use the stride calculation from cairo, because it is wrong:
     * stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, dim.width);
     * Use the stride from VLC its picture_t::p[0].i_pitch, which is correct.
     */
    surface = cairo_image_surface_create_for_data( p_pic->p->p_pixels,
                                                   CAIRO_FORMAT_ARGB32,
                                                   dim.width, dim.height,
                                                   p_pic->p[0].i_pitch );
Adam Leggett's avatar
Adam Leggett committed
142 143
    if( !surface )
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
144
        picture_Release( p_pic );
Adam Leggett's avatar
Adam Leggett committed
145 146 147 148 149
        p_pic = NULL;
        goto done;
    }

    /* Decode picture */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
150
    cr = cairo_create( surface );
Adam Leggett's avatar
Adam Leggett committed
151 152
    if( !cr )
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
153
        picture_Release( p_pic );
Adam Leggett's avatar
Adam Leggett committed
154 155 156 157
        p_pic = NULL;
        goto done;
    }

Jean-Paul Saman's avatar
Jean-Paul Saman committed
158 159 160 161 162 163
    if( !rsvg_handle_render_cairo( rsvg, cr ) )
    {
        picture_Release( p_pic );
        p_pic = NULL;
        goto done;
    }
Adam Leggett's avatar
Adam Leggett committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

    p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;

done:
    if( rsvg )
        g_object_unref( G_OBJECT( rsvg ) );
    if( cr )
        cairo_destroy( cr );
    if( surface )
        cairo_surface_destroy( surface );

    block_Release( p_block );
    return p_pic;
}

/*****************************************************************************
 * CloseDecoder: png decoder destruction
 *****************************************************************************/
static void CloseDecoder( vlc_object_t *p_this )
{
    VLC_UNUSED( p_this );
    rsvg_term();
}