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.
22 * SECTION:element-cairorender
24 * cairorender encodes a video stream into PDF, SVG, PNG or Postscript
27 * <title>Example launch line</title>
29 * gst-launch videotestsrc num-buffers=3 ! cairorender ! "application/pdf" ! filesink location=test.pdf
34 #include "gstcairorender.h"
37 #include <cairo-features.h>
38 #ifdef CAIRO_HAS_PS_SURFACE
41 #ifdef CAIRO_HAS_PDF_SURFACE
42 #include <cairo-pdf.h>
44 #ifdef CAIRO_HAS_SVG_SURFACE
45 #include <cairo-svg.h>
48 #include <gst/video/video.h>
52 GST_DEBUG_CATEGORY_STATIC (cairo_render_debug);
53 #define GST_CAT_DEFAULT cairo_render_debug
56 gst_cairo_render_event (GstPad * pad, GstEvent * e)
58 GstCairoRender *c = GST_CAIRO_RENDER (GST_PAD_PARENT (pad));
60 switch (GST_EVENT_TYPE (e)) {
63 cairo_surface_finish (c->surface);
68 return gst_pad_event_default (pad, e);
72 write_func (void *closure, const unsigned char *data, unsigned int length)
74 GstCairoRender *c = GST_CAIRO_RENDER (closure);
78 buf = gst_buffer_new ();
79 gst_buffer_set_data (buf, (guint8 *) data, length);
80 gst_buffer_set_caps (buf, GST_PAD_CAPS (c->src));
81 if ((r = gst_pad_push (c->src, buf)) != GST_FLOW_OK) {
82 GST_DEBUG_OBJECT (c, "Could not pass on buffer: %s.",
83 gst_flow_get_name (r));
84 return CAIRO_STATUS_WRITE_ERROR;
86 return CAIRO_STATUS_SUCCESS;
90 read_buffer (void *closure, unsigned char *data, unsigned int length)
92 GstBuffer *buf = GST_BUFFER (closure);
94 if (GST_BUFFER_OFFSET (buf) + length > GST_BUFFER_SIZE (buf))
95 return CAIRO_STATUS_READ_ERROR;
96 memcpy (data, GST_BUFFER_DATA (buf) + GST_BUFFER_OFFSET (buf), length);
97 GST_BUFFER_OFFSET (buf) += length;
98 return CAIRO_STATUS_SUCCESS;
102 gst_cairo_render_push_surface (GstCairoRender * c, cairo_surface_t * surface)
104 cairo_status_t s = 0;
108 s = cairo_surface_write_to_png_stream (surface, write_func, c);
109 cairo_surface_destroy (surface);
110 if (s != CAIRO_STATUS_SUCCESS) {
111 GST_DEBUG_OBJECT (c, "Could not create PNG stream: %s.",
112 cairo_status_to_string (s));
118 cr = cairo_create (c->surface);
119 cairo_set_source_surface (cr, surface, 0, 0);
121 cairo_show_page (cr);
123 cairo_surface_destroy (surface);
128 gst_cairo_render_chain (GstPad * pad, GstBuffer * buf)
130 GstCairoRender *c = GST_CAIRO_RENDER (GST_PAD_PARENT (pad));
134 if (G_UNLIKELY (c->width <= 0 || c->height <= 0 || c->stride <= 0))
135 return GST_FLOW_NOT_NEGOTIATED;
138 GST_BUFFER_OFFSET (buf) = 0;
139 s = cairo_image_surface_create_from_png_stream (read_buffer, buf);
141 s = cairo_image_surface_create_for_data (GST_BUFFER_DATA (buf),
142 c->format, c->width, c->height, c->stride);
143 success = gst_cairo_render_push_surface (c, s);
144 gst_buffer_unref (buf);
145 return success ? GST_FLOW_OK : GST_FLOW_ERROR;
149 gst_cairo_render_setcaps_sink (GstPad * pad, GstCaps * caps)
151 GstCairoRender *c = GST_CAIRO_RENDER (GST_PAD_PARENT (pad));
152 GstStructure *s = gst_caps_get_structure (caps, 0);
153 const gchar *mime = gst_structure_get_name (s);
154 gint fps_n = 0, fps_d = 1;
157 GST_DEBUG_OBJECT (c, "Got caps (%s).", mime);
158 if ((c->png = !strcmp (mime, "image/png")))
161 /* Width and height */
162 if (!gst_structure_get_int (s, "width", &c->width) ||
163 !gst_structure_get_int (s, "height", &c->height)) {
164 GST_ERROR_OBJECT (c, "Invalid caps");
169 if (!strcmp (mime, "video/x-raw-yuv") || !strcmp (mime, "video/x-raw-grey")) {
170 c->format = CAIRO_FORMAT_A8;
171 c->stride = GST_ROUND_UP_4 (c->width);
172 } else if (!strcmp (mime, "video/x-raw-rgb")) {
173 c->format = CAIRO_FORMAT_RGB24;
174 c->stride = 4 * c->width;
176 GST_DEBUG_OBJECT (c, "Unknown mime type '%s'.", mime);
181 gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d);
183 /* Create output caps */
184 caps = gst_pad_get_allowed_caps (c->src);
185 caps = gst_caps_make_writable (caps);
186 gst_caps_truncate (caps);
187 s = gst_caps_get_structure (caps, 0);
188 mime = gst_structure_get_name (s);
189 gst_structure_set (s, "height", G_TYPE_INT, c->height, "width", G_TYPE_INT,
190 c->width, "framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
193 cairo_surface_destroy (c->surface);
200 GST_DEBUG_OBJECT (c, "Setting src caps %" GST_PTR_FORMAT, caps);
201 gst_pad_set_caps (c->src, caps);
203 #if CAIRO_HAS_PS_SURFACE
204 if (!strcmp (mime, "application/postscript")) {
205 c->surface = cairo_ps_surface_create_for_stream (write_func, c, w, h);
208 #if CAIRO_HAS_PDF_SURFACE
209 if (!strcmp (mime, "application/pdf")) {
210 c->surface = cairo_pdf_surface_create_for_stream (write_func, c, w, h);
213 #if CAIRO_HAS_SVG_SURFACE
214 if (!strcmp (mime, "image/svg+xml")) {
215 c->surface = cairo_svg_surface_create_for_stream (write_func, c, w, h);
219 gst_caps_unref (caps);
223 gst_caps_unref (caps);
228 static GstStaticPadTemplate t_src = GST_STATIC_PAD_TEMPLATE ("src",
229 GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS (
230 #if CAIRO_HAS_PDF_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_PS_SURFACE
238 "application/postscript, "
239 "width = (int) [ 1, MAX], " "height = (int) [ 1, MAX] "
241 #if (CAIRO_HAS_PDF_SURFACE || CAIRO_HAS_PS_SURFACE) && (CAIRO_HAS_SVG_SURFACE || CAIRO_HAS_PNG_FUNCTIONS)
244 #if CAIRO_HAS_SVG_SURFACE
246 "width = (int) [ 1, MAX], " "height = (int) [ 1, MAX] "
248 #if (CAIRO_HAS_PDF_SURFACE || CAIRO_HAS_PS_SURFACE || CAIRO_HAS_SVG_SURFACE) && CAIRO_HAS_PNG_FUNCTIONS
251 #if CAIRO_HAS_PNG_FUNCTIONS
252 "image/png, " "width = (int) [ 1, MAX], " "height = (int) [ 1, MAX] "
255 static GstStaticPadTemplate t_snk = GST_STATIC_PAD_TEMPLATE ("sink",
256 GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS (
257 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
258 GST_VIDEO_CAPS_BGRx " ; "
260 GST_VIDEO_CAPS_xRGB " ; "
262 GST_VIDEO_CAPS_YUV ("Y800") " ; "
266 "width = " GST_VIDEO_SIZE_RANGE ", "
267 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = " GST_VIDEO_FPS_RANGE
269 #if CAIRO_HAS_PNG_FUNCTIONS
271 "width = " GST_VIDEO_SIZE_RANGE ", " "height = " GST_VIDEO_SIZE_RANGE
275 GST_BOILERPLATE (GstCairoRender, gst_cairo_render, GstElement,
279 gst_cairo_render_init (GstCairoRender * c, GstCairoRenderClass * klass)
283 gst_pad_new_from_template (gst_static_pad_template_get (&t_snk), "sink");
284 gst_pad_set_event_function (c->snk, gst_cairo_render_event);
285 gst_pad_set_chain_function (c->snk, gst_cairo_render_chain);
286 gst_pad_set_setcaps_function (c->snk, gst_cairo_render_setcaps_sink);
287 gst_pad_use_fixed_caps (c->snk);
288 gst_element_add_pad (GST_ELEMENT (c), c->snk);
292 gst_pad_new_from_template (gst_static_pad_template_get (&t_src), "src");
293 gst_pad_use_fixed_caps (c->src);
294 gst_element_add_pad (GST_ELEMENT (c), c->src);
302 gst_cairo_render_base_init (gpointer g_class)
304 GstElementClass *ec = GST_ELEMENT_CLASS (g_class);
306 gst_element_class_set_details_simple (ec, "Cairo encoder",
307 "Codec/Encoder", "Encodes streams using Cairo",
308 "Lutz Mueller <lutz@topfrose.de>");
309 gst_element_class_add_pad_template (ec, gst_static_pad_template_get (&t_snk));
310 gst_element_class_add_pad_template (ec, gst_static_pad_template_get (&t_src));
314 gst_cairo_render_finalize (GObject * object)
316 GstCairoRender *c = GST_CAIRO_RENDER (object);
319 cairo_surface_destroy (c->surface);
323 G_OBJECT_CLASS (parent_class)->finalize (object);
327 gst_cairo_render_class_init (GstCairoRenderClass * klass)
329 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
331 gobject_class->finalize = gst_cairo_render_finalize;
333 GST_DEBUG_CATEGORY_INIT (cairo_render_debug, "cairo_render", 0,