3 * Copyright (C) 2006-2009 Lutz Mueller <lutz@topfrose.de>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 #include "gstcairorender.h"
23 #include <cairo-features.h>
24 #ifdef CAIRO_HAS_PS_SURFACE
27 #ifdef CAIRO_HAS_PDF_SURFACE
28 #include <cairo-pdf.h>
30 #ifdef CAIRO_HAS_SVG_SURFACE
31 #include <cairo-svg.h>
34 #include <gst/video/video.h>
38 GST_DEBUG_CATEGORY_STATIC (cairo_render_debug);
39 #define GST_CAT_DEFAULT cairo_render_debug
42 gst_cairo_render_event (GstPad * pad, GstEvent * e)
44 GstCairoRender *c = GST_CAIRO_RENDER (GST_PAD_PARENT (pad));
46 switch (GST_EVENT_TYPE (e)) {
49 cairo_surface_finish (c->surface);
54 return gst_pad_event_default (pad, e);
58 write_func (void *closure, const unsigned char *data, unsigned int length)
60 GstCairoRender *c = GST_CAIRO_RENDER (closure);
64 buf = gst_buffer_new ();
65 gst_buffer_set_data (buf, (guint8 *) data, length);
66 gst_buffer_set_caps (buf, GST_PAD_CAPS (c->src));
67 if ((r = gst_pad_push (c->src, buf)) != GST_FLOW_OK) {
68 GST_DEBUG_OBJECT (c, "Could not pass on buffer: %s.",
69 gst_flow_get_name (r));
70 return CAIRO_STATUS_WRITE_ERROR;
72 return CAIRO_STATUS_SUCCESS;
76 read_buffer (void *closure, unsigned char *data, unsigned int length)
78 GstBuffer *buf = GST_BUFFER (closure);
80 if (GST_BUFFER_OFFSET (buf) + length > GST_BUFFER_SIZE (buf))
81 return CAIRO_STATUS_READ_ERROR;
82 memcpy (data, GST_BUFFER_DATA (buf) + GST_BUFFER_OFFSET (buf), length);
83 GST_BUFFER_OFFSET (buf) += length;
84 return CAIRO_STATUS_SUCCESS;
88 gst_cairo_render_push_surface (GstCairoRender * c, cairo_surface_t * surface)
94 s = cairo_surface_write_to_png_stream (surface, write_func, c);
95 cairo_surface_destroy (surface);
96 if (s != CAIRO_STATUS_SUCCESS) {
97 GST_DEBUG_OBJECT (c, "Could not create PNG stream: %s.",
98 cairo_status_to_string (s));
104 cr = cairo_create (c->surface);
105 cairo_set_source_surface (cr, surface, 0, 0);
107 cairo_show_page (cr);
109 cairo_surface_destroy (surface);
114 gst_cairo_render_chain (GstPad * pad, GstBuffer * buf)
116 GstCairoRender *c = GST_CAIRO_RENDER (GST_PAD_PARENT (pad));
120 if (G_UNLIKELY (c->width <= 0 || c->height <= 0 || c->stride <= 0))
121 return GST_FLOW_NOT_NEGOTIATED;
124 GST_BUFFER_OFFSET (buf) = 0;
125 s = cairo_image_surface_create_from_png_stream (read_buffer, buf);
127 s = cairo_image_surface_create_for_data (GST_BUFFER_DATA (buf),
128 c->format, c->width, c->height, c->stride);
129 success = gst_cairo_render_push_surface (c, s);
130 gst_buffer_unref (buf);
131 return success ? GST_FLOW_OK : GST_FLOW_ERROR;
135 gst_cairo_render_setcaps_sink (GstPad * pad, GstCaps * caps)
137 GstCairoRender *c = GST_CAIRO_RENDER (GST_PAD_PARENT (pad));
138 GstStructure *s = gst_caps_get_structure (caps, 0);
139 const gchar *mime = gst_structure_get_name (s);
140 gint fps_n = 0, fps_d = 1;
143 GST_DEBUG_OBJECT (c, "Got caps (%s).", mime);
144 if ((c->png = !strcmp (mime, "image/png")))
147 /* Width and height */
148 if (!gst_structure_get_int (s, "width", &c->width) ||
149 !gst_structure_get_int (s, "height", &c->height)) {
150 GST_ERROR_OBJECT (c, "Invalid caps");
155 if (!strcmp (mime, "video/x-raw-yuv") || !strcmp (mime, "video/x-raw-grey")) {
156 c->format = CAIRO_FORMAT_A8;
157 c->stride = GST_ROUND_UP_4 (c->width);
158 } else if (!strcmp (mime, "video/x-raw-rgb")) {
159 c->format = CAIRO_FORMAT_RGB24;
160 c->stride = 4 * c->width;
162 GST_DEBUG_OBJECT (c, "Unknown mime type '%s'.", mime);
167 gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d);
169 /* Create output caps */
170 caps = gst_pad_get_allowed_caps (c->src);
171 caps = gst_caps_make_writable (caps);
172 gst_caps_truncate (caps);
173 s = gst_caps_get_structure (caps, 0);
174 mime = gst_structure_get_name (s);
175 gst_structure_set (s, "height", G_TYPE_INT, c->height, "width", G_TYPE_INT,
176 c->width, "framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
179 cairo_surface_destroy (c->surface);
186 GST_DEBUG_OBJECT (c, "Setting src caps %" GST_PTR_FORMAT, caps);
187 gst_pad_set_caps (c->src, caps);
189 #if CAIRO_HAS_PS_SURFACE
190 if (!strcmp (mime, "application/postscript")) {
191 c->surface = cairo_ps_surface_create_for_stream (write_func, c, w, h);
194 #if CAIRO_HAS_PDF_SURFACE
195 if (!strcmp (mime, "application/pdf")) {
196 c->surface = cairo_pdf_surface_create_for_stream (write_func, c, w, h);
199 #if CAIRO_HAS_SVG_SURFACE
200 if (!strcmp (mime, "image/svg+xml")) {
201 c->surface = cairo_svg_surface_create_for_stream (write_func, c, w, h);
205 gst_caps_unref (caps);
209 gst_caps_unref (caps);
214 static GstStaticPadTemplate t_src = GST_STATIC_PAD_TEMPLATE ("src",
215 GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS (
216 #if CAIRO_HAS_PDF_SURFACE
218 "width = (int) [ 1, MAX], " "height = (int) [ 1, MAX] "
220 #if CAIRO_HAS_PDF_SURFACE && (CAIRO_HAS_PS_SURFACE || CAIRO_HAS_SVG_SURFACE || CAIRO_HAS_PNG_FUNCTIONS)
223 #if CAIRO_HAS_PS_SURFACE
224 "application/postscript, "
225 "width = (int) [ 1, MAX], " "height = (int) [ 1, MAX] "
227 #if (CAIRO_HAS_PDF_SURFACE || CAIRO_HAS_PS_SURFACE) && (CAIRO_HAS_SVG_SURFACE || CAIRO_HAS_PNG_FUNCTIONS)
230 #if CAIRO_HAS_SVG_SURFACE
232 "width = (int) [ 1, MAX], " "height = (int) [ 1, MAX] "
234 #if (CAIRO_HAS_PDF_SURFACE || CAIRO_HAS_PS_SURFACE || CAIRO_HAS_SVG_SURFACE) && CAIRO_HAS_PNG_FUNCTIONS
237 #if CAIRO_HAS_PNG_FUNCTIONS
238 "image/png, " "width = (int) [ 1, MAX], " "height = (int) [ 1, MAX] "
241 static GstStaticPadTemplate t_snk = GST_STATIC_PAD_TEMPLATE ("sink",
242 GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS (
243 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
244 GST_VIDEO_CAPS_BGRx " ; "
246 GST_VIDEO_CAPS_xRGB " ; "
248 GST_VIDEO_CAPS_YUV ("Y800") " ; "
252 "width = " GST_VIDEO_SIZE_RANGE ", "
253 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = " GST_VIDEO_FPS_RANGE
255 #if CAIRO_HAS_PNG_FUNCTIONS
257 "width = " GST_VIDEO_SIZE_RANGE ", " "height = " GST_VIDEO_SIZE_RANGE
261 GST_BOILERPLATE (GstCairoRender, gst_cairo_render, GstElement,
265 gst_cairo_render_init (GstCairoRender * c, GstCairoRenderClass * klass)
269 gst_pad_new_from_template (gst_static_pad_template_get (&t_snk), "sink");
270 gst_pad_set_event_function (c->snk, gst_cairo_render_event);
271 gst_pad_set_chain_function (c->snk, gst_cairo_render_chain);
272 gst_pad_set_setcaps_function (c->snk, gst_cairo_render_setcaps_sink);
273 gst_pad_use_fixed_caps (c->snk);
274 gst_element_add_pad (GST_ELEMENT (c), c->snk);
278 gst_pad_new_from_template (gst_static_pad_template_get (&t_src), "src");
279 gst_pad_use_fixed_caps (c->src);
280 gst_element_add_pad (GST_ELEMENT (c), c->src);
288 gst_cairo_render_base_init (gpointer g_class)
290 GstElementClass *ec = GST_ELEMENT_CLASS (g_class);
292 gst_element_class_set_details_simple (ec, "Cairo encoder",
293 "Codec/Encoder", "Encodes streams using Cairo",
294 "Lutz Mueller <lutz@topfrose.de>");
295 gst_element_class_add_pad_template (ec, gst_static_pad_template_get (&t_snk));
296 gst_element_class_add_pad_template (ec, gst_static_pad_template_get (&t_src));
300 gst_cairo_render_finalize (GObject * object)
302 GstCairoRender *c = GST_CAIRO_RENDER (object);
305 cairo_surface_destroy (c->surface);
309 G_OBJECT_CLASS (parent_class)->finalize (object);
313 gst_cairo_render_class_init (GstCairoRenderClass * klass)
315 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
317 gobject_class->finalize = gst_cairo_render_finalize;
319 GST_DEBUG_CATEGORY_INIT (cairo_render_debug, "cairo_render", 0,