avcodec/librsvgdec: fix memory leaks and deprecated functions

At various points through the function librsvg_decode_frame, errors are
returned from immediately without deallocating any allocated structs.
This patch both fixes those leaks, and also fixes the use of functions
that are deprecated since librsvg version 2.52.0. The older calls are
still used, guarded by #ifdefs while the newer replacements are used if
librsvg >= 2.52.0. One of the deprecated functions is used as a check
for the configure shell script, so it was replaced with a different
function.

Signed-off-by: Leo Izen <leo.izen@gmail.com>
This commit is contained in:
Leo Izen 2023-10-07 21:36:23 -04:00
parent a76b409dd0
commit 86ed68420d
No known key found for this signature in database
GPG Key ID: 5A71C331FD2FA19A
2 changed files with 64 additions and 21 deletions

2
configure vendored
View File

@ -6786,7 +6786,7 @@ enabled libpulse && require_pkg_config libpulse libpulse pulse/pulseaud
enabled librabbitmq && require_pkg_config librabbitmq "librabbitmq >= 0.7.1" amqp.h amqp_new_connection enabled librabbitmq && require_pkg_config librabbitmq "librabbitmq >= 0.7.1" amqp.h amqp_new_connection
enabled librav1e && require_pkg_config librav1e "rav1e >= 0.5.0" rav1e.h rav1e_context_new enabled librav1e && require_pkg_config librav1e "rav1e >= 0.5.0" rav1e.h rav1e_context_new
enabled librist && require_pkg_config librist "librist >= 0.2.7" librist/librist.h rist_receiver_create enabled librist && require_pkg_config librist "librist >= 0.2.7" librist/librist.h rist_receiver_create
enabled librsvg && require_pkg_config librsvg librsvg-2.0 librsvg-2.0/librsvg/rsvg.h rsvg_handle_render_cairo enabled librsvg && require_pkg_config librsvg librsvg-2.0 librsvg-2.0/librsvg/rsvg.h rsvg_handle_new_from_data
enabled librtmp && require_pkg_config librtmp librtmp librtmp/rtmp.h RTMP_Socket enabled librtmp && require_pkg_config librtmp librtmp librtmp/rtmp.h RTMP_Socket
enabled librubberband && require_pkg_config librubberband "rubberband >= 1.8.1" rubberband/rubberband-c.h rubberband_new -lstdc++ && append librubberband_extralibs "-lstdc++" enabled librubberband && require_pkg_config librubberband "rubberband >= 1.8.1" rubberband/rubberband-c.h rubberband_new -lstdc++ && append librubberband_extralibs "-lstdc++"
enabled libshaderc && require_pkg_config spirv_compiler "shaderc >= 2019.1" shaderc/shaderc.h shaderc_compiler_initialize enabled libshaderc && require_pkg_config spirv_compiler "shaderc >= 2019.1" shaderc/shaderc.h shaderc_compiler_initialize

View File

@ -38,48 +38,75 @@ static int librsvg_decode_frame(AVCodecContext *avctx, AVFrame *frame,
{ {
int ret; int ret;
LibRSVGContext *s = avctx->priv_data; LibRSVGContext *s = avctx->priv_data;
RsvgHandle *handle = NULL;
RsvgHandle *handle; RsvgDimensionData dimensions;
RsvgDimensionData unscaled_dimensions, dimensions; #if LIBRSVG_MAJOR_VERSION > 2 || LIBRSVG_MAJOR_VERSION == 2 && LIBRSVG_MINOR_VERSION >= 52
cairo_surface_t *image; RsvgRectangle viewport = { 0 };
#else
RsvgDimensionData unscaled_dimensions;
#endif
cairo_surface_t *image = NULL;
cairo_t *crender = NULL; cairo_t *crender = NULL;
GError *error = NULL; GError *error = NULL;
gboolean gret;
*got_frame = 0; *got_frame = 0;
handle = rsvg_handle_new_from_data(pkt->data, pkt->size, &error); handle = rsvg_handle_new_from_data(pkt->data, pkt->size, &error);
if (error) { if (error) {
av_log(avctx, AV_LOG_ERROR, "Error parsing svg!\n"); av_log(avctx, AV_LOG_ERROR, "Error parsing svg: %s\n", error->message);
g_error_free(error); ret = AVERROR_INVALIDDATA;
return AVERROR_INVALIDDATA; goto end;
} }
#if LIBRSVG_MAJOR_VERSION > 2 || LIBRSVG_MAJOR_VERSION == 2 && LIBRSVG_MINOR_VERSION >= 52
gret = rsvg_handle_get_intrinsic_size_in_pixels(handle, &viewport.width, &viewport.height);
if (!gret) {
viewport.width = s->width ? s->width : 100;
viewport.height = s->height ? s->height : 100;
}
dimensions.width = (int)viewport.width;
dimensions.height = (int)viewport.height;
#else
rsvg_handle_get_dimensions(handle, &dimensions); rsvg_handle_get_dimensions(handle, &dimensions);
rsvg_handle_get_dimensions(handle, &unscaled_dimensions); rsvg_handle_get_dimensions(handle, &unscaled_dimensions);
#endif
dimensions.width = s->width ? s->width : dimensions.width; dimensions.width = s->width ? s->width : dimensions.width;
dimensions.height = s->height ? s->height : dimensions.height; dimensions.height = s->height ? s->height : dimensions.height;
if (s->keep_ar && (s->width || s->height)) { if (s->keep_ar && (s->width || s->height)) {
#if LIBRSVG_MAJOR_VERSION > 2 || LIBRSVG_MAJOR_VERSION == 2 && LIBRSVG_MINOR_VERSION >= 52
double default_ar = viewport.width / viewport.height;
#else
double default_ar = unscaled_dimensions.width/(double)unscaled_dimensions.height; double default_ar = unscaled_dimensions.width/(double)unscaled_dimensions.height;
#endif
if (!s->width) if (!s->width)
dimensions.width = lrintf(dimensions.height * default_ar); dimensions.width = lrintf(dimensions.height * default_ar);
else else
dimensions.height = lrintf(dimensions.width / default_ar); dimensions.height = lrintf(dimensions.width / default_ar);
} }
if ((ret = ff_set_dimensions(avctx, dimensions.width, dimensions.height))) ret = ff_set_dimensions(avctx, dimensions.width, dimensions.height);
return ret; if (ret < 0)
avctx->pix_fmt = AV_PIX_FMT_RGB32; goto end;
avctx->pix_fmt = AV_PIX_FMT_RGB32;
viewport.width = dimensions.width;
viewport.height = dimensions.height;
ret = ff_get_buffer(avctx, frame, 0);
if (ret < 0)
goto end;
if ((ret = ff_get_buffer(avctx, frame, 0)))
return ret;
frame->pict_type = AV_PICTURE_TYPE_I; frame->pict_type = AV_PICTURE_TYPE_I;
frame->flags |= AV_FRAME_FLAG_KEY; frame->flags |= AV_FRAME_FLAG_KEY;
image = cairo_image_surface_create_for_data(frame->data[0], CAIRO_FORMAT_ARGB32, image = cairo_image_surface_create_for_data(frame->data[0], CAIRO_FORMAT_ARGB32,
frame->width, frame->height, frame->width, frame->height,
frame->linesize[0]); frame->linesize[0]);
if (cairo_surface_status(image) != CAIRO_STATUS_SUCCESS) if (cairo_surface_status(image) != CAIRO_STATUS_SUCCESS) {
return AVERROR_INVALIDDATA; ret = AVERROR_EXTERNAL;
goto end;
}
crender = cairo_create(image); crender = cairo_create(image);
@ -88,18 +115,34 @@ static int librsvg_decode_frame(AVCodecContext *avctx, AVFrame *frame,
cairo_paint(crender); cairo_paint(crender);
cairo_restore(crender); cairo_restore(crender);
#if LIBRSVG_MAJOR_VERSION > 2 || LIBRSVG_MAJOR_VERSION == 2 && LIBRSVG_MINOR_VERSION >= 52
gret = rsvg_handle_render_document(handle, crender, &viewport, &error);
#else
cairo_scale(crender, dimensions.width / (double)unscaled_dimensions.width, cairo_scale(crender, dimensions.width / (double)unscaled_dimensions.width,
dimensions.height / (double)unscaled_dimensions.height); dimensions.height / (double)unscaled_dimensions.height);
gret = rsvg_handle_render_cairo(handle, crender);
#endif
rsvg_handle_render_cairo(handle, crender); if (!gret) {
av_log(avctx, AV_LOG_ERROR, "Error rendering svg: %s\n", error ? error->message : "unknown error");
cairo_destroy(crender); ret = AVERROR_EXTERNAL;
cairo_surface_destroy(image); goto end;
g_object_unref(handle); }
*got_frame = 1; *got_frame = 1;
ret = 0;
return 0; end:
if (error)
g_error_free(error);
if (handle)
g_object_unref(handle);
if (crender)
cairo_destroy(crender);
if (image)
cairo_surface_destroy(image);
return ret;
} }
#define OFFSET(x) offsetof(LibRSVGContext, x) #define OFFSET(x) offsetof(LibRSVGContext, x)