static GstFlowReturn gst_rsvg_decode_image (GstRsvgDec * rsvg, const guint8 * data, guint size, GstBuffer ** buffer) { GstFlowReturn ret = GST_FLOW_OK; cairo_t *cr; cairo_surface_t *surface; RsvgHandle *handle; GError *error = NULL; RsvgDimensionData dimension; gdouble scalex, scaley; GST_LOG_OBJECT (rsvg, "parsing svg"); handle = rsvg_handle_new_from_data (data, size, &error); if (!handle) { GST_ERROR_OBJECT (rsvg, "Failed to parse SVG image: %s", error->message); g_error_free (error); return GST_FLOW_ERROR; } rsvg_handle_get_dimensions (handle, &dimension); if (rsvg->width != dimension.width || rsvg->height != dimension.height) { GstCaps *caps1, *caps2, *caps3; GstStructure *s; GST_LOG_OBJECT (rsvg, "resolution changed, updating caps"); caps1 = gst_caps_copy (gst_pad_get_pad_template_caps (rsvg->srcpad)); caps2 = gst_pad_peer_get_caps (rsvg->srcpad); if (caps2) { caps3 = gst_caps_intersect (caps1, caps2); gst_caps_unref (caps1); gst_caps_unref (caps2); caps1 = caps3; caps3 = NULL; } if (gst_caps_is_empty (caps1)) { GST_ERROR_OBJECT (rsvg, "Unable to negotiate a format"); gst_caps_unref (caps1); g_object_unref (handle); return GST_FLOW_NOT_NEGOTIATED; } caps2 = gst_caps_copy (gst_pad_get_pad_template_caps (rsvg->srcpad)); s = gst_caps_get_structure (caps2, 0); gst_structure_set (s, "width", G_TYPE_INT, dimension.width, "height", G_TYPE_INT, dimension.height, "framerate", GST_TYPE_FRACTION, 0, 1, NULL); caps3 = gst_caps_intersect (caps1, caps2); if (!gst_caps_is_empty (caps3)) { gst_caps_truncate (caps3); gst_pad_set_caps (rsvg->srcpad, caps3); gst_caps_unref (caps1); gst_caps_unref (caps2); gst_caps_unref (caps3); rsvg->width = dimension.width; rsvg->height = dimension.height; } else { gst_caps_unref (caps2); gst_caps_unref (caps3); gst_caps_truncate (caps1); s = gst_caps_get_structure (caps1, 0); gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL); if (!gst_caps_is_fixed (caps1) && (!gst_structure_fixate_field_nearest_int (s, "width", dimension.width) || !gst_structure_fixate_field_nearest_int (s, "height", dimension.height))) { g_object_unref (handle); GST_ERROR_OBJECT (rsvg, "Failed to fixate caps"); return GST_FLOW_NOT_NEGOTIATED; } gst_pad_set_caps (rsvg->srcpad, caps1); gst_structure_get_int (s, "width", &rsvg->width); gst_structure_get_int (s, "height", &rsvg->height); gst_caps_unref (caps1); } } if ((ret = gst_pad_alloc_buffer_and_set_caps (rsvg->srcpad, GST_BUFFER_OFFSET_NONE, rsvg->width * rsvg->height * 4, GST_PAD_CAPS (rsvg->srcpad), buffer)) != GST_FLOW_OK) { g_object_unref (handle); GST_ERROR_OBJECT (rsvg, "Buffer allocation failed %s", gst_flow_get_name (ret)); return ret; } GST_LOG_OBJECT (rsvg, "render image at %d x %d", rsvg->height, rsvg->width); surface = cairo_image_surface_create_for_data (GST_BUFFER_DATA (*buffer), CAIRO_FORMAT_ARGB32, rsvg->width, rsvg->height, rsvg->width * 4); cr = cairo_create (surface); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); cairo_paint (cr); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0); scalex = scaley = 1.0; if (rsvg->width != dimension.width) { scalex = ((gdouble) rsvg->width) / ((gdouble) dimension.width); } if (rsvg->height != dimension.height) { scaley = ((gdouble) rsvg->height) / ((gdouble) dimension.height); } cairo_scale (cr, scalex, scaley); rsvg_handle_render_cairo (handle, cr); g_object_unref (handle); cairo_destroy (cr); cairo_surface_destroy (surface); /* Now unpremultiply Cairo's ARGB to match GStreamer's */ gst_rsvg_decode_unpremultiply (GST_BUFFER_DATA (*buffer), rsvg->width, rsvg->height); return ret; }
static GstFlowReturn gst_rsvg_decode_image (GstRsvgDec * rsvg, GstBuffer * buffer, GstVideoCodecFrame * frame) { GstVideoDecoder *decoder = GST_VIDEO_DECODER (rsvg); GstFlowReturn ret = GST_FLOW_OK; cairo_t *cr; cairo_surface_t *surface; RsvgHandle *handle; GError *error = NULL; RsvgDimensionData dimension; gdouble scalex, scaley; GstMapInfo minfo; GstVideoFrame vframe; GstVideoCodecState *output_state; GST_LOG_OBJECT (rsvg, "parsing svg"); if (!gst_buffer_map (buffer, &minfo, GST_MAP_READ)) { GST_ERROR_OBJECT (rsvg, "Failed to get SVG image"); return GST_FLOW_ERROR; } handle = rsvg_handle_new_from_data (minfo.data, minfo.size, &error); if (!handle) { GST_ERROR_OBJECT (rsvg, "Failed to parse SVG image: %s", error->message); g_error_free (error); return GST_FLOW_ERROR; } rsvg_handle_get_dimensions (handle, &dimension); output_state = gst_video_decoder_get_output_state (decoder); if ((output_state == NULL) || GST_VIDEO_INFO_WIDTH (&output_state->info) != dimension.width || GST_VIDEO_INFO_HEIGHT (&output_state->info) != dimension.height) { /* Create the output state */ if (output_state) gst_video_codec_state_unref (output_state); output_state = gst_video_decoder_set_output_state (decoder, GST_RSVG_VIDEO_FORMAT, dimension.width, dimension.height, rsvg->input_state); } ret = gst_video_decoder_allocate_output_frame (decoder, frame); if (ret != GST_FLOW_OK) { g_object_unref (handle); gst_video_codec_state_unref (output_state); GST_ERROR_OBJECT (rsvg, "Buffer allocation failed %s", gst_flow_get_name (ret)); return ret; } GST_LOG_OBJECT (rsvg, "render image at %d x %d", GST_VIDEO_INFO_HEIGHT (&output_state->info), GST_VIDEO_INFO_WIDTH (&output_state->info)); if (!gst_video_frame_map (&vframe, &output_state->info, frame->output_buffer, GST_MAP_READWRITE)) { GST_ERROR_OBJECT (rsvg, "Failed to get SVG image"); g_object_unref (handle); gst_video_codec_state_unref (output_state); return GST_FLOW_ERROR; } surface = cairo_image_surface_create_for_data (GST_VIDEO_FRAME_PLANE_DATA (&vframe, 0), CAIRO_FORMAT_ARGB32, GST_VIDEO_FRAME_WIDTH (&vframe), GST_VIDEO_FRAME_HEIGHT (&vframe), GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, 0)); cr = cairo_create (surface); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); cairo_paint (cr); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0); scalex = scaley = 1.0; if (GST_VIDEO_INFO_WIDTH (&output_state->info) != dimension.width) { scalex = ((gdouble) GST_VIDEO_INFO_WIDTH (&output_state->info)) / ((gdouble) dimension.width); } if (GST_VIDEO_INFO_HEIGHT (&output_state->info) != dimension.height) { scaley = ((gdouble) GST_VIDEO_INFO_HEIGHT (&output_state->info)) / ((gdouble) dimension.height); } cairo_scale (cr, scalex, scaley); rsvg_handle_render_cairo (handle, cr); g_object_unref (handle); cairo_destroy (cr); cairo_surface_destroy (surface); /* Now unpremultiply Cairo's ARGB to match GStreamer's */ gst_rsvg_decode_unpremultiply (GST_VIDEO_FRAME_PLANE_DATA (&vframe, 0), GST_VIDEO_FRAME_WIDTH (&vframe), GST_VIDEO_FRAME_HEIGHT (&vframe)); gst_video_codec_state_unref (output_state); gst_buffer_unmap (buffer, &minfo); gst_video_frame_unmap (&vframe); return ret; }