Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / ext / cairo / gstcairooverlay.c
1 /* GStreamer
2  * Copyright (C) <2011> Jon Nordby <jononor@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-cairooverlay
22  *
23  * cairooverlay renders an overlay using a application provided render function.
24  *
25  * The full example can be found in tests/examples/cairo/cairo_overlay.c
26  * <refsect2>
27  * <title>Example code</title>
28  * |[
29  *
30  * #include &lt;gst/gst.h&gt;
31  * #include &lt;gst/video/video.h&gt;
32  *
33  * ...
34  *
35  * typedef struct {
36  *   gboolean valid;
37  *   int width;
38  *   int height;
39  * } CairoOverlayState;
40  * 
41  * ...
42  *
43  * static void
44  * prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
45  * {
46  *   CairoOverlayState *state = (CairoOverlayState *)user_data;
47  *
48  *   gst_video_format_parse_caps (caps, NULL, &amp;state-&gt;width, &amp;state-&gt;height);
49  *   state-&gt;valid = TRUE;
50  * }
51  *
52  * static void
53  * draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp, 
54  *   guint64 duration, gpointer user_data)
55  * {
56  *   CairoOverlayState *s = (CairoOverlayState *)user_data;
57  *   double scale;
58  *
59  *   if (!s-&gt;valid)
60  *     return;
61  *
62  *   scale = 2*(((timestamp/(int)1e7) % 70)+30)/100.0;
63  *   cairo_translate(cr, s-&gt;width/2, (s-&gt;height/2)-30);
64  *   cairo_scale (cr, scale, scale);
65  *
66  *   cairo_move_to (cr, 0, 0);
67  *   cairo_curve_to (cr, 0,-30, -50,-30, -50,0);
68  *   cairo_curve_to (cr, -50,30, 0,35, 0,60 );
69  *   cairo_curve_to (cr, 0,35, 50,30, 50,0 ); *  
70  *   cairo_curve_to (cr, 50,-30, 0,-30, 0,0 );
71  *   cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
72  *   cairo_fill (cr);
73  * }
74  *
75  * ...
76  *
77  * cairo_overlay = gst_element_factory_make (&quot;cairooverlay&quot;, &quot;overlay&quot;);
78  *
79  * g_signal_connect (cairo_overlay, &quot;draw&quot;, G_CALLBACK (draw_overlay),
80  *   overlay_state);
81  * g_signal_connect (cairo_overlay, &quot;caps-changed&quot;, 
82  *   G_CALLBACK (prepare_overlay), overlay_state);
83  * ...
84  *
85  * ]|
86  * </refsect2>
87  */
88
89 #ifdef HAVE_CONFIG_H
90 #include "config.h"
91 #endif
92
93 #include "gstcairooverlay.h"
94 #include "gstcairo-marshal.h"
95
96 #include <gst/video/video.h>
97
98 #include <cairo.h>
99
100 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
101 #define TEMPLATE_CAPS GST_VIDEO_CAPS_BGRx " ; " GST_VIDEO_CAPS_BGRA " ; "
102 #else
103 #define TEMPLATE_CAPS GST_VIDEO_CAPS_xRGB " ; " GST_VIDEO_CAPS_ARGB " ; "
104
105 #endif
106
107 static GstStaticPadTemplate gst_cairo_overlay_src_template =
108 GST_STATIC_PAD_TEMPLATE ("src",
109     GST_PAD_SRC,
110     GST_PAD_ALWAYS,
111     GST_STATIC_CAPS (TEMPLATE_CAPS)
112     );
113
114 static GstStaticPadTemplate gst_cairo_overlay_sink_template =
115 GST_STATIC_PAD_TEMPLATE ("sink",
116     GST_PAD_SINK,
117     GST_PAD_ALWAYS,
118     GST_STATIC_CAPS (TEMPLATE_CAPS)
119     );
120
121
122 GST_BOILERPLATE (GstCairoOverlay, gst_cairo_overlay, GstVideoFilter,
123     GST_TYPE_VIDEO_FILTER);
124
125 enum
126 {
127   SIGNAL_DRAW,
128   SIGNAL_CAPS_CHANGED,
129   N_SIGNALS
130 };
131
132 static guint gst_cairo_overlay_signals[N_SIGNALS];
133
134 static gboolean
135 gst_cairo_overlay_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
136     GstCaps * outcaps)
137 {
138   GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (btrans);
139   gboolean ret;
140
141   ret =
142       gst_video_format_parse_caps (incaps, &overlay->format, &overlay->width,
143       &overlay->height);
144   if (G_UNLIKELY (!ret))
145     return FALSE;
146
147   g_signal_emit (overlay, gst_cairo_overlay_signals[SIGNAL_CAPS_CHANGED], 0,
148       incaps, NULL);
149
150   return ret;
151 }
152
153 static GstFlowReturn
154 gst_cairo_overlay_transform_ip (GstBaseTransform * btrans, GstBuffer * buf)
155 {
156
157   GstCairoOverlay *overlay = GST_CAIRO_OVERLAY (btrans);
158   cairo_surface_t *surface;
159   cairo_t *cr;
160   cairo_format_t format;
161
162   format = (overlay->format == GST_VIDEO_FORMAT_ARGB
163       || overlay->format == GST_VIDEO_FORMAT_BGRA) ?
164       CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24;
165
166   surface =
167       cairo_image_surface_create_for_data (GST_BUFFER_DATA (buf), format,
168       overlay->width, overlay->height, overlay->width * 4);
169   if (G_UNLIKELY (!surface))
170     return GST_FLOW_ERROR;
171
172   cr = cairo_create (surface);
173   if (G_UNLIKELY (!cr)) {
174     cairo_surface_destroy (surface);
175     return GST_FLOW_ERROR;
176   }
177
178   g_signal_emit (overlay, gst_cairo_overlay_signals[SIGNAL_DRAW], 0,
179       cr, GST_BUFFER_TIMESTAMP (buf), GST_BUFFER_DURATION (buf), NULL);
180
181   cairo_destroy (cr);
182   cairo_surface_destroy (surface);
183
184   return GST_FLOW_OK;
185 }
186
187 static void
188 gst_cairo_overlay_base_init (gpointer g_class)
189 {
190   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
191
192   gst_element_class_set_details_simple (element_class, "Cairo overlay",
193       "Filter/Editor/Video",
194       "Render overlay on a video stream using Cairo",
195       "Jon Nordby <jononor@gmail.com>");
196
197   gst_element_class_add_static_pad_template (element_class,
198       &gst_cairo_overlay_sink_template);
199   gst_element_class_add_static_pad_template (element_class,
200       &gst_cairo_overlay_src_template);
201 }
202
203 static void
204 gst_cairo_overlay_class_init (GstCairoOverlayClass * klass)
205 {
206   GstBaseTransformClass *trans_class;
207
208   trans_class = (GstBaseTransformClass *) klass;
209
210   trans_class->set_caps = gst_cairo_overlay_set_caps;
211   trans_class->transform_ip = gst_cairo_overlay_transform_ip;
212
213   /**
214    * GstCairoOverlay::draw:
215    * @overlay: Overlay element emitting the signal.
216    * @cr: Cairo context to draw to.
217    * @timestamp: Timestamp (see #GstClockTime) of the current buffer.
218    * @duration: Duration (see #GstClockTime) of the current buffer.
219    * 
220    * This signal is emitted when the overlay should be drawn.
221    */
222   gst_cairo_overlay_signals[SIGNAL_DRAW] =
223       g_signal_new ("draw",
224       G_TYPE_FROM_CLASS (klass),
225       0,
226       0,
227       NULL,
228       NULL,
229       gst_cairo_marshal_VOID__BOXED_UINT64_UINT64,
230       G_TYPE_NONE, 3, CAIRO_GOBJECT_TYPE_CONTEXT, G_TYPE_UINT64, G_TYPE_UINT64);
231
232   /**
233    * GstCairoOverlay::caps-changed:
234    * @overlay: Overlay element emitting the signal.
235    * @caps: The #GstCaps of the element.
236    * 
237    * This signal is emitted when the caps of the element has changed.
238    */
239   gst_cairo_overlay_signals[SIGNAL_CAPS_CHANGED] =
240       g_signal_new ("caps-changed",
241       G_TYPE_FROM_CLASS (klass),
242       0,
243       0,
244       NULL, NULL, gst_cairo_marshal_VOID__BOXED, G_TYPE_NONE, 1, GST_TYPE_CAPS);
245 }
246
247 static void
248 gst_cairo_overlay_init (GstCairoOverlay * overlay, GstCairoOverlayClass * klass)
249 {
250 }