History

lavc: add a librsvg rasterization library wrapper

Enables rendering of SVG images. This is possible since SVG images still contain and specify the dimensions in pixels to which they've been drawn to and thus enable browsers to display them without any external data. Users can still override and generate images with arbitrary resolutions.

Rostislav Pehlivanov committed 9 years ago

Commit 5fd4cff1 parent f68ea925 files · +134 −0Browse files

Unified · Split

5 files
  1. Changelog +1 −0
  2. configure +4 −0
  3. libavcodec/Makefile +1 −0
  4. libavcodec/allcodecs.c +1 −0
  5. libavcodec/librsvgdec.c +127 −0
Changelog+1−0
@@ -11,6 +11,7 @@ version <next>: · 10 unmodified lines
11- update cuvid/nvenc headers to Video Codec SDK 8.0.1411- update cuvid/nvenc headers to Video Codec SDK 8.0.14
12- afir audio filter12- afir audio filter
13- scale_cuda CUDA based video scale filter13- scale_cuda CUDA based video scale filter
14- librsvg support for svg rasterization
14 15
15version 3.3:16version 3.3:
16- CrystalHD decoder moved to new decode API17- CrystalHD decoder moved to new decode API
configure+4−0
@@ -245,6 +245,7 @@ External library support: · 244 unmodified lines
245 --enable-libopenmpt enable decoding tracked files via libopenmpt [no]245 --enable-libopenmpt enable decoding tracked files via libopenmpt [no]
246 --enable-libopus enable Opus de/encoding via libopus [no]246 --enable-libopus enable Opus de/encoding via libopus [no]
247 --enable-libpulse enable Pulseaudio input via libpulse [no]247 --enable-libpulse enable Pulseaudio input via libpulse [no]
248 --enable-librsvg enable SVG rasterization via librsvg [no]
248 --enable-librubberband enable rubberband needed for rubberband filter [no]249 --enable-librubberband enable rubberband needed for rubberband filter [no]
249 --enable-librtmp enable RTMP[E] support via librtmp [no]250 --enable-librtmp enable RTMP[E] support via librtmp [no]
250 --enable-libschroedinger enable Dirac de/encoding via libschroedinger [no]251 --enable-libschroedinger enable Dirac de/encoding via libschroedinger [no]
@@ -1560,6 +1561,7 @@ EXTERNAL_LIBRARY_LIST=" · 1309 unmodified lines
1560 libopenmpt1561 libopenmpt
1561 libopus1562 libopus
1562 libpulse1563 libpulse
1564 librsvg
1563 librtmp1565 librtmp
1564 libschroedinger1566 libschroedinger
1565 libshine1567 libshine
@@ -2875,6 +2877,7 @@ libopenmpt_demuxer_deps="libopenmpt" · 1309 unmodified lines
2875libopus_decoder_deps="libopus"2877libopus_decoder_deps="libopus"
2876libopus_encoder_deps="libopus"2878libopus_encoder_deps="libopus"
2877libopus_encoder_select="audio_frame_queue"2879libopus_encoder_select="audio_frame_queue"
2880librsvg_decoder_deps="librsvg"
2878libschroedinger_decoder_deps="libschroedinger"2881libschroedinger_decoder_deps="libschroedinger"
2879libschroedinger_encoder_deps="libschroedinger"2882libschroedinger_encoder_deps="libschroedinger"
2880libshine_encoder_deps="libshine"2883libshine_encoder_deps="libshine"
@@ -5837,6 +5840,7 @@ enabled libopus && { · 2956 unmodified lines
5837 }5840 }
5838}5841}
5839enabled libpulse && require_pkg_config libpulse pulse/pulseaudio.h pa_context_new5842enabled libpulse && require_pkg_config libpulse pulse/pulseaudio.h pa_context_new
5843enabled librsvg && require_pkg_config librsvg-2.0 librsvg-2.0/librsvg/rsvg.h rsvg_handle_render_cairo
5840enabled librtmp && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket5844enabled librtmp && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket
5841enabled librubberband && require_pkg_config "rubberband >= 1.8.1" rubberband/rubberband-c.h rubberband_new5845enabled librubberband && require_pkg_config "rubberband >= 1.8.1" rubberband/rubberband-c.h rubberband_new
5842enabled libschroedinger && require_pkg_config schroedinger-1.0 schroedinger/schro.h schro_init5846enabled libschroedinger && require_pkg_config schroedinger-1.0 schroedinger/schro.h schro_init
libavcodec/Makefile+1−0
@@ -551,6 +551,7 @@ OBJS-$(CONFIG_SUBVIEWER1_DECODER) += textdec.o ass.o · 550 unmodified lines
551OBJS-$(CONFIG_SUBVIEWER_DECODER) += subviewerdec.o ass.o551OBJS-$(CONFIG_SUBVIEWER_DECODER) += subviewerdec.o ass.o
552OBJS-$(CONFIG_SUNRAST_DECODER) += sunrast.o552OBJS-$(CONFIG_SUNRAST_DECODER) += sunrast.o
553OBJS-$(CONFIG_SUNRAST_ENCODER) += sunrastenc.o553OBJS-$(CONFIG_SUNRAST_ENCODER) += sunrastenc.o
554OBJS-$(CONFIG_LIBRSVG_DECODER) += librsvgdec.o
554OBJS-$(CONFIG_SVQ1_DECODER) += svq1dec.o svq1.o svq13.o h263data.o555OBJS-$(CONFIG_SVQ1_DECODER) += svq1dec.o svq1.o svq13.o h263data.o
555OBJS-$(CONFIG_SVQ1_ENCODER) += svq1enc.o svq1.o h263data.o \556OBJS-$(CONFIG_SVQ1_ENCODER) += svq1enc.o svq1.o h263data.o \
556 h263.o ituh263enc.o557 h263.o ituh263enc.o
libavcodec/allcodecs.c+1−0
@@ -319,6 +319,7 @@ static void register_all(void) · 318 unmodified lines
319 REGISTER_DECODER(SPEEDHQ, speedhq);319 REGISTER_DECODER(SPEEDHQ, speedhq);
320 REGISTER_DECODER(SRGC, srgc);320 REGISTER_DECODER(SRGC, srgc);
321 REGISTER_ENCDEC (SUNRAST, sunrast);321 REGISTER_ENCDEC (SUNRAST, sunrast);
322 REGISTER_DECODER(LIBRSVG, librsvg);
322 REGISTER_ENCDEC (SVQ1, svq1);323 REGISTER_ENCDEC (SVQ1, svq1);
323 REGISTER_DECODER(SVQ3, svq3);324 REGISTER_DECODER(SVQ3, svq3);
324 REGISTER_ENCDEC (TARGA, targa);325 REGISTER_ENCDEC (TARGA, targa);
libavcodec/librsvgdec.c+127−0
@@ -0,0 +1,127 @@
1/*
2 * Librsvg rasterization wrapper
3 * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "avcodec.h"
23#include "internal.h"
24#include "libavutil/opt.h"
25#include "librsvg-2.0/librsvg/rsvg.h"
26
27typedef struct LibRSVGContext {
28 AVClass *class;
29
30 int width;
31 int height;
32 int keep_ar;
33} LibRSVGContext;
34
35static int librsvg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
36{
37 int ret;
38 LibRSVGContext *s = avctx->priv_data;
39 AVFrame *frame = data;
40
41 RsvgHandle *handle;
42 RsvgDimensionData unscaled_dimensions, dimensions;
43 cairo_surface_t *image;
44 cairo_t *crender = NULL;
45 GError *error = NULL;
46
47 *got_frame = 0;
48
49 handle = rsvg_handle_new_from_data(pkt->data, pkt->size, &error);
50 if (error) {
51 av_log(avctx, AV_LOG_ERROR, "Error parsing svg!\n");
52 g_error_free(error);
53 return AVERROR_INVALIDDATA;
54 }
55
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;
62 if (!s->width)
63 dimensions.width = lrintf(dimensions.height * default_ar);
64 else
65 dimensions.height = lrintf(dimensions.width / default_ar);
66 }
67
68 if ((ret = ff_set_dimensions(avctx, dimensions.width, dimensions.height)))
69 return ret;
70 avctx->pix_fmt = AV_PIX_FMT_BGRA;
71
72 if ((ret = ff_get_buffer(avctx, frame, 0)))
73 return ret;
74 frame->pict_type = AV_PICTURE_TYPE_I;
75 frame->key_frame = 1;
76
77 image = cairo_image_surface_create_for_data(frame->data[0], CAIRO_FORMAT_ARGB32,
78 frame->width, frame->height,
79 frame->linesize[0]);
80 if (cairo_surface_status(image) != CAIRO_STATUS_SUCCESS)
81 return AVERROR_INVALIDDATA;
82
83 crender = cairo_create(image);
84
85 cairo_set_source_rgba(crender, 0.0, 0.0, 0.0, 1.0f);
86 cairo_paint_with_alpha(crender, 0.0f);
87
88 cairo_scale(crender, dimensions.width / (double)unscaled_dimensions.width,
89 dimensions.height / (double)unscaled_dimensions.height);
90
91 rsvg_handle_render_cairo(handle, crender);
92
93 cairo_destroy(crender);
94 cairo_surface_destroy(image);
95 g_object_unref(handle);
96
97 *got_frame = 1;
98
99 return 0;
100}
101
102#define OFFSET(x) offsetof(LibRSVGContext, x)
103#define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
104static 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 },
108 { NULL },
109};
110
111static const AVClass librsvg_decoder_class = {
112 .class_name = "Librsvg",
113 .item_name = av_default_item_name,
114 .option = options,
115 .version = LIBAVUTIL_VERSION_INT,
116};
117
118AVCodec ff_librsvg_decoder = {
119 .name = "librsvg",
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,
127};

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t