cairorender: Return not-negotiated if we have no caps
[platform/upstream/gst-plugins-good.git] / ext / cairo / gstcairorender.c
1 /* GStreamer
2  *
3  * Copyright (C) 2006-2009 Lutz Mueller <lutz@topfrose.de>
4  *
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.
9  *
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.
14  *
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.
19  */
20 #include "gstcairorender.h"
21
22 #include <cairo.h>
23 #include <cairo-features.h>
24 #ifdef CAIRO_HAS_PS_SURFACE
25 #include <cairo-ps.h>
26 #endif
27 #ifdef CAIRO_HAS_PDF_SURFACE
28 #include <cairo-pdf.h>
29 #endif
30 #ifdef CAIRO_HAS_SVG_SURFACE
31 #include <cairo-svg.h>
32 #endif
33
34 #include <gst/video/video.h>
35
36 #include <string.h>
37
38 GST_DEBUG_CATEGORY_STATIC (cairo_render_debug);
39 #define GST_CAT_DEFAULT cairo_render_debug
40
41 static gboolean
42 gst_cairo_render_event (GstPad * pad, GstEvent * e)
43 {
44   GstCairoRender *c = GST_CAIRO_RENDER (GST_PAD_PARENT (pad));
45
46   switch (GST_EVENT_TYPE (e)) {
47     case GST_EVENT_EOS:
48       if (c->surface)
49         cairo_surface_finish (c->surface);
50       break;
51     default:
52       break;
53   }
54   return gst_pad_event_default (pad, e);
55 }
56
57 static cairo_status_t
58 write_func (void *closure, const unsigned char *data, unsigned int length)
59 {
60   GstCairoRender *c = GST_CAIRO_RENDER (closure);
61   GstBuffer *buf;
62   GstFlowReturn r;
63
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;
71   }
72   return CAIRO_STATUS_SUCCESS;
73 }
74
75 static cairo_status_t
76 read_buffer (void *closure, unsigned char *data, unsigned int length)
77 {
78   GstBuffer *buf = GST_BUFFER (closure);
79
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;
85 }
86
87 static gboolean
88 gst_cairo_render_push_surface (GstCairoRender * c, cairo_surface_t * surface)
89 {
90   cairo_status_t s = 0;
91   cairo_t *cr;
92
93   if (!c->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));
99       return FALSE;
100     }
101     return TRUE;
102   }
103
104   cr = cairo_create (c->surface);
105   cairo_set_source_surface (cr, surface, 0, 0);
106   cairo_paint (cr);
107   cairo_show_page (cr);
108   cairo_destroy (cr);
109   cairo_surface_destroy (surface);
110   return (TRUE);
111 }
112
113 static GstFlowReturn
114 gst_cairo_render_chain (GstPad * pad, GstBuffer * buf)
115 {
116   GstCairoRender *c = GST_CAIRO_RENDER (GST_PAD_PARENT (pad));
117   cairo_surface_t *s;
118   gboolean success;
119
120   if (G_UNLIKELY (c->width <= 0 || c->height <= 0 || c->stride <= 0))
121     return GST_FLOW_NOT_NEGOTIATED;
122
123   if (c->png) {
124     GST_BUFFER_OFFSET (buf) = 0;
125     s = cairo_image_surface_create_from_png_stream (read_buffer, buf);
126   } else
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;
132 }
133
134 static gboolean
135 gst_cairo_render_setcaps_sink (GstPad * pad, GstCaps * caps)
136 {
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;
141   gint w, h;
142
143   GST_DEBUG_OBJECT (c, "Got caps (%s).", mime);
144   if ((c->png = !strcmp (mime, "image/png")))
145     return TRUE;
146
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");
151     return FALSE;
152   }
153
154   /* Colorspace */
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;
161   } else {
162     GST_DEBUG_OBJECT (c, "Unknown mime type '%s'.", mime);
163     return FALSE;
164   }
165
166   /* Framerate */
167   gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d);
168
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);
177
178   if (c->surface) {
179     cairo_surface_destroy (c->surface);
180     c->surface = NULL;
181   }
182
183   w = c->width;
184   h = c->height;
185
186   GST_DEBUG_OBJECT (c, "Setting src caps %" GST_PTR_FORMAT, caps);
187   gst_pad_set_caps (c->src, caps);
188
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);
192   } else
193 #endif
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);
197   } else
198 #endif
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);
202   } else
203 #endif
204   {
205     gst_caps_unref (caps);
206     return FALSE;
207   }
208
209   gst_caps_unref (caps);
210
211   return TRUE;
212 }
213
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
217         "application/pdf, "
218         "width = (int) [ 1, MAX], " "height = (int) [ 1, MAX] "
219 #endif
220 #if CAIRO_HAS_PDF_SURFACE && (CAIRO_HAS_PS_SURFACE || CAIRO_HAS_SVG_SURFACE || CAIRO_HAS_PNG_FUNCTIONS)
221         ";"
222 #endif
223 #if CAIRO_HAS_PS_SURFACE
224         "application/postscript, "
225         "width = (int) [ 1, MAX], " "height = (int) [ 1, MAX] "
226 #endif
227 #if (CAIRO_HAS_PDF_SURFACE || CAIRO_HAS_PS_SURFACE) && (CAIRO_HAS_SVG_SURFACE || CAIRO_HAS_PNG_FUNCTIONS)
228         ";"
229 #endif
230 #if CAIRO_HAS_SVG_SURFACE
231         "image/svg+xml, "
232         "width = (int) [ 1, MAX], " "height = (int) [ 1, MAX] "
233 #endif
234 #if (CAIRO_HAS_PDF_SURFACE || CAIRO_HAS_PS_SURFACE || CAIRO_HAS_SVG_SURFACE) && CAIRO_HAS_PNG_FUNCTIONS
235         ";"
236 #endif
237 #if CAIRO_HAS_PNG_FUNCTIONS
238         "image/png, " "width = (int) [ 1, MAX], " "height = (int) [ 1, MAX] "
239 #endif
240     ));
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 " ; "
245 #else
246         GST_VIDEO_CAPS_xRGB " ; "
247 #endif
248         GST_VIDEO_CAPS_YUV ("Y800") " ; "
249         "video/x-raw-gray, "
250         "bpp = 8, "
251         "depth = 8, "
252         "width = " GST_VIDEO_SIZE_RANGE ", "
253         "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = " GST_VIDEO_FPS_RANGE
254         " ; "
255 #if CAIRO_HAS_PNG_FUNCTIONS
256         "image/png, "
257         "width = " GST_VIDEO_SIZE_RANGE ", " "height = " GST_VIDEO_SIZE_RANGE
258 #endif
259     ));
260
261 GST_BOILERPLATE (GstCairoRender, gst_cairo_render, GstElement,
262     GST_TYPE_ELEMENT);
263
264 static void
265 gst_cairo_render_init (GstCairoRender * c, GstCairoRenderClass * klass)
266 {
267   /* The sink */
268   c->snk =
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);
275
276   /* The source */
277   c->src =
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);
281
282   c->width = 0;
283   c->height = 0;
284   c->stride = 0;
285 }
286
287 static void
288 gst_cairo_render_base_init (gpointer g_class)
289 {
290   GstElementClass *ec = GST_ELEMENT_CLASS (g_class);
291
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));
297 }
298
299 static void
300 gst_cairo_render_finalize (GObject * object)
301 {
302   GstCairoRender *c = GST_CAIRO_RENDER (object);
303
304   if (c->surface) {
305     cairo_surface_destroy (c->surface);
306     c->surface = NULL;
307   }
308
309   G_OBJECT_CLASS (parent_class)->finalize (object);
310 }
311
312 static void
313 gst_cairo_render_class_init (GstCairoRenderClass * klass)
314 {
315   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
316
317   gobject_class->finalize = gst_cairo_render_finalize;
318
319   GST_DEBUG_CATEGORY_INIT (cairo_render_debug, "cairo_render", 0,
320       "Cairo encoder");
321 }