diff mbox series

[FFmpeg-devel] Adds base-uri parameter to librsvg, allowing e.g. embedded images in SVGs to be loaded

Message ID 20210803094534.1000-1-daniel.petri@tum.de
State New
Headers show
Series [FFmpeg-devel] Adds base-uri parameter to librsvg, allowing e.g. embedded images in SVGs to be loaded | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Daniel Petri Rocha Aug. 3, 2021, 9:45 a.m. UTC
Usage of the data URI scheme is currently needed to load external references in SVGs. This patch adds a
base_uri parameter so linked images can be rendered without encoding them in Base64.
 
<!-- Example usage: ffmpeg -base_uri "/path/to/base/dir" -i example.svg out.png -->
<!-- example.svg: -->
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <image xlink:href="file:///absolute/path/to/base/dir/image.png" height="200" width="200"/>
</svg>

Signed-off-by: Daniel Petri Rocha <daniel.petri@tum.de>
---
 libavcodec/librsvgdec.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/librsvgdec.c b/libavcodec/librsvgdec.c
index 2895bbf28b..bdef65b423 100644
--- a/libavcodec/librsvgdec.c
+++ b/libavcodec/librsvgdec.c
@@ -30,6 +30,8 @@  typedef struct LibRSVGContext {
     int width;
     int height;
     int keep_ar;
+    char *base_uri;
+    int flag;
 } LibRSVGContext;
 
 static int librsvg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
@@ -42,11 +44,22 @@  static int librsvg_decode_frame(AVCodecContext *avctx, void *data, int *got_fram
     RsvgDimensionData unscaled_dimensions, dimensions;
     cairo_surface_t *image;
     cairo_t *crender = NULL;
+    
     GError *error = NULL;
-
+    GInputStream *input_stream;
+    GFile *referenced_file;
+    
     *got_frame = 0;
 
-    handle = rsvg_handle_new_from_data(pkt->data, pkt->size, &error);
+    input_stream = g_memory_input_stream_new_from_data (pkt->data, pkt->size, NULL);
+    referenced_file = g_file_new_for_path(s->base_uri);
+
+    handle = rsvg_handle_new_from_stream_sync (input_stream,
+                                  referenced_file,
+                                  s->flag,
+                                  NULL,
+                                  &error);
+
     if (error) {
         av_log(avctx, AV_LOG_ERROR, "Error parsing svg!\n");
         g_error_free(error);
@@ -94,8 +107,10 @@  static int librsvg_decode_frame(AVCodecContext *avctx, void *data, int *got_fram
 
     cairo_destroy(crender);
     cairo_surface_destroy(image);
-    g_object_unref(handle);
 
+    g_object_unref(handle);
+    g_object_unref(referenced_file);
+    g_object_unref(input_stream);
     *got_frame = 1;
 
     return 0;
@@ -103,10 +118,13 @@  static int librsvg_decode_frame(AVCodecContext *avctx, void *data, int *got_fram
 
 #define OFFSET(x) offsetof(LibRSVGContext, x)
 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
+
 static const AVOption options[] = {
     { "width", "Width to render to (0 for default)", OFFSET(width), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
     { "height", "Height to render to (0 for default)", OFFSET(height), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
     { "keep_ar", "Keep aspect ratio with custom width/height", OFFSET(keep_ar), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, DEC },
+    { "base_uri", "Base URI to load referenced files", OFFSET(base_uri), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, DEC },
+    { "handle_flag", "Value of RsvgHandleFlags", OFFSET(flag), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, DEC},
     { NULL },
 };