FFmpeg
librsvgdec.c
Go to the documentation of this file.
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 "codec_internal.h"
24 #include "internal.h"
25 #include "libavutil/opt.h"
26 #include "librsvg-2.0/librsvg/rsvg.h"
27 
28 typedef struct LibRSVGContext {
29  AVClass *class;
30 
31  int width;
32  int height;
33  int keep_ar;
35 
37  int *got_frame, AVPacket *pkt)
38 {
39  int ret;
40  LibRSVGContext *s = avctx->priv_data;
41 
42  RsvgHandle *handle;
43  RsvgDimensionData unscaled_dimensions, dimensions;
44  cairo_surface_t *image;
45  cairo_t *crender = NULL;
46  GError *error = NULL;
47 
48  *got_frame = 0;
49 
50  handle = rsvg_handle_new_from_data(pkt->data, pkt->size, &error);
51  if (error) {
52  av_log(avctx, AV_LOG_ERROR, "Error parsing svg!\n");
53  g_error_free(error);
54  return AVERROR_INVALIDDATA;
55  }
56 
57  rsvg_handle_get_dimensions(handle, &dimensions);
58  rsvg_handle_get_dimensions(handle, &unscaled_dimensions);
59  dimensions.width = s->width ? s->width : dimensions.width;
60  dimensions.height = s->height ? s->height : dimensions.height;
61  if (s->keep_ar && (s->width || s->height)) {
62  double default_ar = unscaled_dimensions.width/(double)unscaled_dimensions.height;
63  if (!s->width)
64  dimensions.width = lrintf(dimensions.height * default_ar);
65  else
66  dimensions.height = lrintf(dimensions.width / default_ar);
67  }
68 
69  if ((ret = ff_set_dimensions(avctx, dimensions.width, dimensions.height)))
70  return ret;
71  avctx->pix_fmt = AV_PIX_FMT_RGB32;
72 
73  if ((ret = ff_get_buffer(avctx, frame, 0)))
74  return ret;
75  frame->pict_type = AV_PICTURE_TYPE_I;
76  frame->key_frame = 1;
77 
78  image = cairo_image_surface_create_for_data(frame->data[0], CAIRO_FORMAT_ARGB32,
79  frame->width, frame->height,
80  frame->linesize[0]);
81  if (cairo_surface_status(image) != CAIRO_STATUS_SUCCESS)
82  return AVERROR_INVALIDDATA;
83 
84  crender = cairo_create(image);
85 
86  cairo_save(crender);
87  cairo_set_operator(crender, CAIRO_OPERATOR_CLEAR);
88  cairo_paint(crender);
89  cairo_restore(crender);
90 
91  cairo_scale(crender, dimensions.width / (double)unscaled_dimensions.width,
92  dimensions.height / (double)unscaled_dimensions.height);
93 
94  rsvg_handle_render_cairo(handle, crender);
95 
96  cairo_destroy(crender);
97  cairo_surface_destroy(image);
98  g_object_unref(handle);
99 
100  *got_frame = 1;
101 
102  return 0;
103 }
104 
105 #define OFFSET(x) offsetof(LibRSVGContext, x)
106 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
107 static const AVOption options[] = {
108  { "width", "Width to render to (0 for default)", OFFSET(width), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
109  { "height", "Height to render to (0 for default)", OFFSET(height), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
110  { "keep_ar", "Keep aspect ratio with custom width/height", OFFSET(keep_ar), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, DEC },
111  { NULL },
112 };
113 
115  .class_name = "Librsvg",
116  .item_name = av_default_item_name,
117  .option = options,
118  .version = LIBAVUTIL_VERSION_INT,
119 };
120 
122  .p.name = "librsvg",
123  .p.long_name = NULL_IF_CONFIG_SMALL("Librsvg rasterizer"),
124  .p.priv_class = &librsvg_decoder_class,
125  .p.type = AVMEDIA_TYPE_VIDEO,
126  .p.id = AV_CODEC_ID_SVG,
127  .p.capabilities = AV_CODEC_CAP_DR1,
128  .p.wrapper_name = "librsvg",
130  .priv_data_size = sizeof(LibRSVGContext),
131 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
opt.h
LibRSVGContext::width
int width
Definition: librsvgdec.c:31
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
FFCodec
Definition: codec_internal.h:112
options
static const AVOption options[]
Definition: librsvgdec.c:107
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
librsvg_decoder_class
static const AVClass librsvg_decoder_class
Definition: librsvgdec.c:114
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
s
#define s(width, name)
Definition: cbs_vp9.c:256
LibRSVGContext
Definition: librsvgdec.c:28
ff_librsvg_decoder
const FFCodec ff_librsvg_decoder
Definition: librsvgdec.c:121
librsvg_decode_frame
static int librsvg_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: librsvgdec.c:36
if
if(ret)
Definition: filter_design.txt:179
LibRSVGContext::height
int height
Definition: librsvgdec.c:32
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
double
double
Definition: af_crystalizer.c:132
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1403
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
codec_internal.h
height
#define height
AV_PIX_FMT_RGB32
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:379
AV_CODEC_ID_SVG
@ AV_CODEC_ID_SVG
Definition: codec_id.h:283
lrintf
#define lrintf(x)
Definition: libm_mips.h:72
OFFSET
#define OFFSET(x)
Definition: librsvgdec.c:105
LibRSVGContext::keep_ar
int keep_ar
Definition: librsvgdec.c:33
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:599
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:389
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_set_dimensions
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:90
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:562
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
DEC
#define DEC
Definition: librsvgdec.c:106