tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.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
21 /**
22  * SECTION:element-cairorender
23  *
24  * cairorender encodes a video stream into PDF, SVG, PNG or Postscript
25  *
26  * <refsect2>
27  * <title>Example launch line</title>
28  * |[
29  * gst-launch videotestsrc num-buffers=3 ! cairorender ! "application/pdf" ! filesink location=test.pdf
30  * ]|
31  * </refsect2>
32  */
33
34 #include "gstcairorender.h"
35
36 #include <cairo.h>
37 #include <cairo-features.h>
38 #ifdef CAIRO_HAS_PS_SURFACE
39 #include <cairo-ps.h>
40 #endif
41 #ifdef CAIRO_HAS_PDF_SURFACE
42 #include <cairo-pdf.h>
43 #endif
44 #ifdef CAIRO_HAS_SVG_SURFACE
45 #include <cairo-svg.h>
46 #endif
47
48 #include <gst/video/video.h>
49
50 #include <string.h>
51
52 GST_DEBUG_CATEGORY_STATIC (cairo_render_debug);
53 #define GST_CAT_DEFAULT cairo_render_debug
54
55 static gboolean
56 gst_cairo_render_event (GstPad * pad, GstEvent * e)
57 {
58   GstCairoRender *c = GST_CAIRO_RENDER (GST_PAD_PARENT (pad));
59
60   switch (GST_EVENT_TYPE (e)) {
61     case GST_EVENT_EOS:
62       if (c->surface)
63         cairo_surface_finish (c->surface);
64       break;
65     default:
66       break;
67   }
68   return gst_pad_event_default (pad, e);
69 }
70
71 static cairo_status_t
72 write_func (void *closure, const unsigned char *data, unsigned int length)
73 {
74   GstCairoRender *c = GST_CAIRO_RENDER (closure);
75   GstBuffer *buf;
76   GstFlowReturn r;
77
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;
85   }
86   return CAIRO_STATUS_SUCCESS;
87 }
88
89 static cairo_status_t
90 read_buffer (void *closure, unsigned char *data, unsigned int length)
91 {
92   GstBuffer *buf = GST_BUFFER (closure);
93
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;
99 }
100
101 static gboolean
102 gst_cairo_render_push_surface (GstCairoRender * c, cairo_surface_t * surface)
103 {
104   cairo_status_t s = 0;
105   cairo_t *cr;
106
107   if (!c->surface) {
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));
113       return FALSE;
114     }
115     return TRUE;
116   }
117
118   cr = cairo_create (c->surface);
119   cairo_set_source_surface (cr, surface, 0, 0);
120   cairo_paint (cr);
121   cairo_show_page (cr);
122   cairo_destroy (cr);
123   cairo_surface_destroy (surface);
124   return (TRUE);
125 }
126
127 static GstFlowReturn
128 gst_cairo_render_chain (GstPad * pad, GstBuffer * buf)
129 {
130   GstCairoRender *c = GST_CAIRO_RENDER (GST_PAD_PARENT (pad));
131   cairo_surface_t *s;
132   gboolean success;
133
134   if (G_UNLIKELY (c->width <= 0 || c->height <= 0 || c->stride <= 0))
135     return GST_FLOW_NOT_NEGOTIATED;
136
137   if (c->png) {
138     GST_BUFFER_OFFSET (buf) = 0;
139     s = cairo_image_surface_create_from_png_stream (read_buffer, buf);
140   } else {
141     if (c->format == CAIRO_FORMAT_ARGB32) {
142       guint i, j;
143       guint8 *data = GST_BUFFER_DATA (buf);
144
145       buf = gst_buffer_make_writable (buf);
146
147       /* Cairo ARGB is pre-multiplied with the alpha
148        * value, i.e. 0x80008000 is half transparent
149        * green
150        */
151       for (i = 0; i < c->height; i++) {
152         for (j = 0; j < c->width; j++) {
153 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
154           guint8 alpha = data[3];
155
156           data[0] = (data[0] * alpha) >> 8;
157           data[1] = (data[1] * alpha) >> 8;
158           data[2] = (data[2] * alpha) >> 8;
159 #else
160           guint8 alpha = data[0];
161
162           data[1] = (data[1] * alpha) >> 8;
163           data[2] = (data[2] * alpha) >> 8;
164           data[3] = (data[3] * alpha) >> 8;
165 #endif
166           data += 4;
167         }
168       }
169     }
170
171     s = cairo_image_surface_create_for_data (GST_BUFFER_DATA (buf),
172         c->format, c->width, c->height, c->stride);
173   }
174
175   success = gst_cairo_render_push_surface (c, s);
176   gst_buffer_unref (buf);
177   return success ? GST_FLOW_OK : GST_FLOW_ERROR;
178 }
179
180 static gboolean
181 gst_cairo_render_setcaps_sink (GstPad * pad, GstCaps * caps)
182 {
183   GstCairoRender *c = GST_CAIRO_RENDER (GST_PAD_PARENT (pad));
184   GstStructure *s = gst_caps_get_structure (caps, 0);
185   const gchar *mime = gst_structure_get_name (s);
186   gint fps_n = 0, fps_d = 1;
187   gint w, h;
188
189   GST_DEBUG_OBJECT (c, "Got caps (%s).", mime);
190   if ((c->png = !strcmp (mime, "image/png")))
191     return TRUE;
192
193   /* Width and height */
194   if (!gst_structure_get_int (s, "width", &c->width) ||
195       !gst_structure_get_int (s, "height", &c->height)) {
196     GST_ERROR_OBJECT (c, "Invalid caps");
197     return FALSE;
198   }
199
200   /* Colorspace */
201   if (!strcmp (mime, "video/x-raw-yuv") || !strcmp (mime, "video/x-raw-grey")) {
202     c->format = CAIRO_FORMAT_A8;
203     c->stride = GST_ROUND_UP_4 (c->width);
204   } else if (!strcmp (mime, "video/x-raw-rgb")) {
205     gint bpp;
206
207     if (!gst_structure_get_int (s, "bpp", &bpp)) {
208       GST_ERROR_OBJECT (c, "Invalid caps");
209       return FALSE;
210     }
211
212     c->format = (bpp == 32) ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24;
213     c->stride = 4 * c->width;
214   } else {
215     GST_DEBUG_OBJECT (c, "Unknown mime type '%s'.", mime);
216     return FALSE;
217   }
218
219   /* Framerate */
220   gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d);
221
222   /* Create output caps */
223   caps = gst_pad_get_allowed_caps (c->src);
224   caps = gst_caps_make_writable (caps);
225   gst_caps_truncate (caps);
226   s = gst_caps_get_structure (caps, 0);
227   mime = gst_structure_get_name (s);
228   gst_structure_set (s, "height", G_TYPE_INT, c->height, "width", G_TYPE_INT,
229       c->width, "framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
230
231   if (c->surface) {
232     cairo_surface_destroy (c->surface);
233     c->surface = NULL;
234   }
235
236   w = c->width;
237   h = c->height;
238
239   GST_DEBUG_OBJECT (c, "Setting src caps %" GST_PTR_FORMAT, caps);
240   gst_pad_set_caps (c->src, caps);
241
242 #if CAIRO_HAS_PS_SURFACE
243   if (!strcmp (mime, "application/postscript")) {
244     c->surface = cairo_ps_surface_create_for_stream (write_func, c, w, h);
245   } else
246 #endif
247 #if CAIRO_HAS_PDF_SURFACE
248   if (!strcmp (mime, "application/pdf")) {
249     c->surface = cairo_pdf_surface_create_for_stream (write_func, c, w, h);
250   } else
251 #endif
252 #if CAIRO_HAS_SVG_SURFACE
253   if (!strcmp (mime, "image/svg+xml")) {
254     c->surface = cairo_svg_surface_create_for_stream (write_func, c, w, h);
255   } else
256 #endif
257   {
258     gst_caps_unref (caps);
259     return FALSE;
260   }
261
262   gst_caps_unref (caps);
263
264   return TRUE;
265 }
266
267
268 #define SIZE_CAPS "width = (int) [ 1, MAX], height = (int) [ 1, MAX] "
269 #if CAIRO_HAS_PDF_SURFACE
270 #define PDF_CAPS "application/pdf, " SIZE_CAPS
271 #else
272 #define PDF_CAPS
273 #endif
274 #if CAIRO_HAS_PDF_SURFACE && (CAIRO_HAS_PS_SURFACE || CAIRO_HAS_SVG_SURFACE || CAIRO_HAS_PNG_FUNCTIONS)
275 #define JOIN1 ";"
276 #else
277 #define JOIN1
278 #endif
279 #if CAIRO_HAS_PS_SURFACE
280 #define PS_CAPS "application/postscript, " SIZE_CAPS
281 #else
282 #define PS_CAPS
283 #endif
284 #if (CAIRO_HAS_PDF_SURFACE || CAIRO_HAS_PS_SURFACE) && (CAIRO_HAS_SVG_SURFACE || CAIRO_HAS_PNG_FUNCTIONS)
285 #define JOIN2 ";"
286 #else
287 #define JOIN2
288 #endif
289 #if CAIRO_HAS_SVG_SURFACE
290 #define SVG_CAPS "image/svg+xml, " SIZE_CAPS
291 #else
292 #define SVG_CAPS
293 #endif
294 #if (CAIRO_HAS_PDF_SURFACE || CAIRO_HAS_PS_SURFACE || CAIRO_HAS_SVG_SURFACE) && CAIRO_HAS_PNG_FUNCTIONS
295 #define JOIN3 ";"
296 #else
297 #define JOIN3
298 #endif
299 #if CAIRO_HAS_PNG_FUNCTIONS
300 #define PNG_CAPS "image/png, " SIZE_CAPS
301 #define PNG_CAPS2 "; image/png, " SIZE_CAPS
302 #else
303 #define PNG_CAPS
304 #define PNG_CAPS2
305 #endif
306
307 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
308 #define ARGB_CAPS GST_VIDEO_CAPS_BGRx " ; " GST_VIDEO_CAPS_BGRA " ; "
309 #else
310 #define ARGB_CAPS GST_VIDEO_CAPS_xRGB " ; " GST_VIDEO_CAPS_ARGB " ; "
311 #endif
312 static GstStaticPadTemplate t_src = GST_STATIC_PAD_TEMPLATE ("src",
313     GST_PAD_SRC, GST_PAD_ALWAYS,
314     GST_STATIC_CAPS (PDF_CAPS JOIN1 PS_CAPS JOIN2 SVG_CAPS JOIN3 PNG_CAPS));
315 static GstStaticPadTemplate t_snk = GST_STATIC_PAD_TEMPLATE ("sink",
316     GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS (ARGB_CAPS
317         GST_VIDEO_CAPS_YUV ("Y800") " ; "
318         "video/x-raw-gray, "
319         "bpp = 8, "
320         "depth = 8, "
321         "width = " GST_VIDEO_SIZE_RANGE ", "
322         "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = " GST_VIDEO_FPS_RANGE
323         PNG_CAPS2));
324
325 GST_BOILERPLATE (GstCairoRender, gst_cairo_render, GstElement,
326     GST_TYPE_ELEMENT);
327
328 static void
329 gst_cairo_render_init (GstCairoRender * c, GstCairoRenderClass * klass)
330 {
331   /* The sink */
332   c->snk = gst_pad_new_from_static_template (&t_snk, "sink");
333   gst_pad_set_event_function (c->snk, gst_cairo_render_event);
334   gst_pad_set_chain_function (c->snk, gst_cairo_render_chain);
335   gst_pad_set_setcaps_function (c->snk, gst_cairo_render_setcaps_sink);
336   gst_pad_use_fixed_caps (c->snk);
337   gst_element_add_pad (GST_ELEMENT (c), c->snk);
338
339   /* The source */
340   c->src = gst_pad_new_from_static_template (&t_src, "src");
341   gst_pad_use_fixed_caps (c->src);
342   gst_element_add_pad (GST_ELEMENT (c), c->src);
343
344   c->width = 0;
345   c->height = 0;
346   c->stride = 0;
347 }
348
349 static void
350 gst_cairo_render_base_init (gpointer g_class)
351 {
352   GstElementClass *ec = GST_ELEMENT_CLASS (g_class);
353
354   gst_element_class_set_details_simple (ec, "Cairo encoder",
355       "Codec/Encoder", "Encodes streams using Cairo",
356       "Lutz Mueller <lutz@topfrose.de>");
357   gst_element_class_add_static_pad_template (ec, &t_snk);
358   gst_element_class_add_static_pad_template (ec, &t_src);
359 }
360
361 static void
362 gst_cairo_render_finalize (GObject * object)
363 {
364   GstCairoRender *c = GST_CAIRO_RENDER (object);
365
366   if (c->surface) {
367     cairo_surface_destroy (c->surface);
368     c->surface = NULL;
369   }
370
371   G_OBJECT_CLASS (parent_class)->finalize (object);
372 }
373
374 static void
375 gst_cairo_render_class_init (GstCairoRenderClass * klass)
376 {
377   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
378
379   gobject_class->finalize = gst_cairo_render_finalize;
380
381   GST_DEBUG_CATEGORY_INIT (cairo_render_debug, "cairo_render", 0,
382       "Cairo encoder");
383 }