2 * Librsvg rasterization wrapper
3 * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/opt.h"
25 #include "librsvg-2.0/librsvg/rsvg.h"
27 typedef struct LibRSVGContext {
35 static int librsvg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
38 LibRSVGContext *s = avctx->priv_data;
39 AVFrame *frame = data;
42 RsvgDimensionData unscaled_dimensions, dimensions;
43 cairo_surface_t *image;
44 cairo_t *crender = NULL;
49 handle = rsvg_handle_new_from_data(pkt->data, pkt->size, &error);
51 av_log(avctx, AV_LOG_ERROR, "Error parsing svg!\n");
53 return AVERROR_INVALIDDATA;
56 rsvg_handle_get_dimensions(handle, &dimensions);
57 rsvg_handle_get_dimensions(handle, &unscaled_dimensions);
58 dimensions.width = s->width ? s->width : dimensions.width;
59 dimensions.height = s->height ? s->height : dimensions.height;
60 if (s->keep_ar && (s->width || s->height)) {
61 double default_ar = unscaled_dimensions.width/(double)unscaled_dimensions.height;
63 dimensions.width = lrintf(dimensions.height * default_ar);
65 dimensions.height = lrintf(dimensions.width / default_ar);
68 if ((ret = ff_set_dimensions(avctx, dimensions.width, dimensions.height)))
70 avctx->pix_fmt = AV_PIX_FMT_RGB32;
72 if ((ret = ff_get_buffer(avctx, frame, 0)))
74 frame->pict_type = AV_PICTURE_TYPE_I;
77 image = cairo_image_surface_create_for_data(frame->data[0], CAIRO_FORMAT_ARGB32,
78 frame->width, frame->height,
80 if (cairo_surface_status(image) != CAIRO_STATUS_SUCCESS)
81 return AVERROR_INVALIDDATA;
83 crender = cairo_create(image);
85 cairo_set_source_rgba(crender, 0.0, 0.0, 0.0, 1.0f);
86 cairo_paint_with_alpha(crender, 0.0f);
88 cairo_scale(crender, dimensions.width / (double)unscaled_dimensions.width,
89 dimensions.height / (double)unscaled_dimensions.height);
91 rsvg_handle_render_cairo(handle, crender);
93 cairo_destroy(crender);
94 cairo_surface_destroy(image);
95 g_object_unref(handle);
102 #define OFFSET(x) offsetof(LibRSVGContext, x)
103 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
104 static const AVOption options[] = {
105 { "width", "Width to render to (0 for default)", OFFSET(width), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
106 { "height", "Height to render to (0 for default)", OFFSET(height), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
107 { "keep_ar", "Keep aspect ratio with custom width/height", OFFSET(keep_ar), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, DEC },
111 static const AVClass librsvg_decoder_class = {
112 .class_name = "Librsvg",
113 .item_name = av_default_item_name,
115 .version = LIBAVUTIL_VERSION_INT,
118 AVCodec ff_librsvg_decoder = {
120 .long_name = NULL_IF_CONFIG_SMALL("Librsvg rasterizer"),
121 .priv_class = &librsvg_decoder_class,
122 .type = AVMEDIA_TYPE_VIDEO,
123 .id = AV_CODEC_ID_SVG,
124 .decode = librsvg_decode_frame,
125 .priv_data_size = sizeof(LibRSVGContext),
126 .capabilities = AV_CODEC_CAP_LOSSLESS | AV_CODEC_CAP_DR1,