tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / ext / cairo / gsttimeoverlay.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2003> David Schleef <ds@schleef.org>
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-cairotimeoverlay
23  *
24  * cairotimeoverlay renders the buffer timestamp for each frame on top of
25  * the frame.
26  *
27  * <refsect2>
28  * <title>Example launch line</title>
29  * |[
30  * gst-launch videotestsrc ! cairotimeoverlay ! autovideosink
31  * ]|
32  * </refsect2>
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <gst/math-compat.h>
40
41 #include <gsttimeoverlay.h>
42
43 #include <string.h>
44
45 #include <cairo.h>
46
47 #include <gst/video/video.h>
48
49 static GstStaticPadTemplate gst_cairo_time_overlay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
54     );
55
56 static GstStaticPadTemplate gst_cairo_time_overlay_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
58     GST_PAD_SINK,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
61     );
62
63 static GstBaseTransformClass *parent_class = NULL;
64
65 static void
66 gst_cairo_time_overlay_update_font_height (GstCairoTimeOverlay * timeoverlay)
67 {
68   gint width, height;
69   cairo_surface_t *font_surface;
70   cairo_t *font_cairo;
71   cairo_font_extents_t font_extents;
72
73   width = timeoverlay->width;
74   height = timeoverlay->height;
75
76   font_surface =
77       cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
78   font_cairo = cairo_create (font_surface);
79   cairo_surface_destroy (font_surface);
80   font_surface = NULL;
81
82   cairo_select_font_face (font_cairo, "monospace", 0, 0);
83   cairo_set_font_size (font_cairo, 20);
84   cairo_font_extents (font_cairo, &font_extents);
85   timeoverlay->text_height = font_extents.height;
86   GST_DEBUG_OBJECT (timeoverlay, "font height is %f", font_extents.height);
87   cairo_destroy (font_cairo);
88   font_cairo = NULL;
89 }
90
91 static gboolean
92 gst_cairo_time_overlay_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
93     GstCaps * outcaps)
94 {
95   GstCairoTimeOverlay *filter = GST_CAIRO_TIME_OVERLAY (btrans);
96   GstStructure *structure;
97   gboolean ret = FALSE;
98
99   structure = gst_caps_get_structure (incaps, 0);
100
101   if (gst_structure_get_int (structure, "width", &filter->width) &&
102       gst_structure_get_int (structure, "height", &filter->height)) {
103     gst_cairo_time_overlay_update_font_height (filter);
104     ret = TRUE;
105   }
106
107   return ret;
108 }
109
110 /* Useful macros */
111 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (GST_ROUND_UP_4(width))
112 #define GST_VIDEO_I420_U_ROWSTRIDE(width) (GST_ROUND_UP_8(width)/2)
113 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((GST_ROUND_UP_8(GST_VIDEO_I420_Y_ROWSTRIDE(width)))/2)
114
115 #define GST_VIDEO_I420_Y_OFFSET(w,h) (0)
116 #define GST_VIDEO_I420_U_OFFSET(w,h) (GST_VIDEO_I420_Y_OFFSET(w,h)+(GST_VIDEO_I420_Y_ROWSTRIDE(w)*GST_ROUND_UP_2(h)))
117 #define GST_VIDEO_I420_V_OFFSET(w,h) (GST_VIDEO_I420_U_OFFSET(w,h)+(GST_VIDEO_I420_U_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
118
119 #define GST_VIDEO_I420_SIZE(w,h)     (GST_VIDEO_I420_V_OFFSET(w,h)+(GST_VIDEO_I420_V_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
120
121 static gboolean
122 gst_cairo_time_overlay_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
123     guint * size)
124 {
125   GstCairoTimeOverlay *filter;
126   GstStructure *structure;
127   gboolean ret = FALSE;
128   gint width, height;
129
130   filter = GST_CAIRO_TIME_OVERLAY (btrans);
131
132   structure = gst_caps_get_structure (caps, 0);
133
134   if (gst_structure_get_int (structure, "width", &width) &&
135       gst_structure_get_int (structure, "height", &height)) {
136     *size = GST_VIDEO_I420_SIZE (width, height);
137     ret = TRUE;
138     GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
139         width, height);
140   }
141
142   return ret;
143 }
144
145 static char *
146 gst_cairo_time_overlay_print_smpte_time (guint64 time)
147 {
148   int hours;
149   int minutes;
150   int seconds;
151   int ms;
152   double x;
153
154   x = rint (gst_util_guint64_to_gdouble (time + 500000) * 1e-6);
155
156   hours = floor (x / (60 * 60 * 1000));
157   x -= hours * 60 * 60 * 1000;
158   minutes = floor (x / (60 * 1000));
159   x -= minutes * 60 * 1000;
160   seconds = floor (x / (1000));
161   x -= seconds * 1000;
162   ms = rint (x);
163
164   return g_strdup_printf ("%02d:%02d:%02d.%03d", hours, minutes, seconds, ms);
165 }
166
167
168 static GstFlowReturn
169 gst_cairo_time_overlay_transform (GstBaseTransform * trans, GstBuffer * in,
170     GstBuffer * out)
171 {
172   GstCairoTimeOverlay *timeoverlay;
173   int width;
174   int height;
175   int b_width;
176   int stride_y, stride_u, stride_v;
177   char *string;
178   int i, j;
179   unsigned char *image;
180   cairo_text_extents_t extents;
181   guint8 *dest, *src;
182   cairo_surface_t *font_surface;
183   cairo_t *text_cairo;
184   GstFlowReturn ret = GST_FLOW_OK;
185
186   timeoverlay = GST_CAIRO_TIME_OVERLAY (trans);
187
188   gst_buffer_copy_metadata (out, in, GST_BUFFER_COPY_TIMESTAMPS);
189
190   src = GST_BUFFER_DATA (in);
191   dest = GST_BUFFER_DATA (out);
192
193   width = timeoverlay->width;
194   height = timeoverlay->height;
195
196   /* create surface for font rendering */
197   /* FIXME: preparation of the surface could also be done once when settings
198    * change */
199   image = g_malloc (4 * width * timeoverlay->text_height);
200
201   font_surface =
202       cairo_image_surface_create_for_data (image, CAIRO_FORMAT_ARGB32, width,
203       timeoverlay->text_height, width * 4);
204   text_cairo = cairo_create (font_surface);
205   cairo_surface_destroy (font_surface);
206   font_surface = NULL;
207
208   /* we draw a rectangle because the compositing on the buffer below
209    * doesn't do alpha */
210   cairo_save (text_cairo);
211   cairo_rectangle (text_cairo, 0, 0, width, timeoverlay->text_height);
212   cairo_set_source_rgba (text_cairo, 0, 0, 0, 1);
213   cairo_set_operator (text_cairo, CAIRO_OPERATOR_SOURCE);
214   cairo_fill (text_cairo);
215   cairo_restore (text_cairo);
216
217   string = gst_cairo_time_overlay_print_smpte_time (GST_BUFFER_TIMESTAMP (in));
218   cairo_save (text_cairo);
219   cairo_select_font_face (text_cairo, "monospace", 0, 0);
220   cairo_set_font_size (text_cairo, 20);
221   cairo_text_extents (text_cairo, string, &extents);
222   cairo_set_source_rgb (text_cairo, 1, 1, 1);
223   cairo_move_to (text_cairo, 0, timeoverlay->text_height - 2);
224   cairo_show_text (text_cairo, string);
225   g_free (string);
226
227   cairo_restore (text_cairo);
228
229   /* blend width; should retain a max text width so it doesn't jitter */
230   b_width = extents.width;
231   if (b_width > width)
232     b_width = width;
233
234   stride_y = GST_VIDEO_I420_Y_ROWSTRIDE (width);
235   stride_u = GST_VIDEO_I420_U_ROWSTRIDE (width);
236   stride_v = GST_VIDEO_I420_V_ROWSTRIDE (width);
237
238   memcpy (dest, src, GST_BUFFER_SIZE (in));
239   for (i = 0; i < timeoverlay->text_height; i++) {
240     for (j = 0; j < b_width; j++) {
241       ((unsigned char *) dest)[i * stride_y + j] =
242           image[(i * width + j) * 4 + 0];
243     }
244   }
245   for (i = 0; i < timeoverlay->text_height / 2; i++) {
246     memset (dest + GST_VIDEO_I420_U_OFFSET (width, height) + i * stride_u, 128,
247         b_width / 2);
248     memset (dest + GST_VIDEO_I420_V_OFFSET (width, height) + i * stride_v, 128,
249         b_width / 2);
250   }
251
252   cairo_destroy (text_cairo);
253   text_cairo = NULL;
254   g_free (image);
255
256   return ret;
257 }
258
259 static void
260 gst_cairo_time_overlay_base_init (gpointer g_class)
261 {
262   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
263
264   gst_element_class_set_details_simple (element_class, "Time overlay",
265       "Filter/Editor/Video",
266       "Overlays the time on a video stream", "David Schleef <ds@schleef.org>");
267
268   gst_element_class_add_static_pad_template (element_class,
269       &gst_cairo_time_overlay_sink_template);
270   gst_element_class_add_static_pad_template (element_class,
271       &gst_cairo_time_overlay_src_template);
272 }
273
274 static void
275 gst_cairo_time_overlay_class_init (gpointer klass, gpointer class_data)
276 {
277   GstBaseTransformClass *trans_class;
278
279   trans_class = (GstBaseTransformClass *) klass;
280
281   parent_class = g_type_class_peek_parent (klass);
282
283   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_cairo_time_overlay_set_caps);
284   trans_class->get_unit_size =
285       GST_DEBUG_FUNCPTR (gst_cairo_time_overlay_get_unit_size);
286   trans_class->transform = GST_DEBUG_FUNCPTR (gst_cairo_time_overlay_transform);
287 }
288
289 static void
290 gst_cairo_time_overlay_init (GTypeInstance * instance, gpointer g_class)
291 {
292 }
293
294 GType
295 gst_cairo_time_overlay_get_type (void)
296 {
297   static GType cairo_time_overlay_type = 0;
298
299   if (!cairo_time_overlay_type) {
300     static const GTypeInfo cairo_time_overlay_info = {
301       sizeof (GstCairoTimeOverlayClass),
302       gst_cairo_time_overlay_base_init,
303       NULL,
304       gst_cairo_time_overlay_class_init,
305       NULL,
306       NULL,
307       sizeof (GstCairoTimeOverlay),
308       0,
309       gst_cairo_time_overlay_init,
310     };
311
312     cairo_time_overlay_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
313         "GstCairoTimeOverlay", &cairo_time_overlay_info, 0);
314   }
315   return cairo_time_overlay_type;
316 }