6016d941f30ce1ee4c79996f180033c4dcbf547d
[platform/upstream/gstreamer.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  * This file was (probably) generated from gsttimeoverlay.c,
23  * gsttimeoverlay.c,v 1.7 2003/11/08 02:48:59 dschleef Exp 
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 /*#define DEBUG_ENABLED */
31 #include <gsttimeoverlay.h>
32 #include <string.h>
33 #include <math.h>
34
35 #include <cairo.h>
36
37
38 /* GstTimeoverlay signals and args */
39 enum
40 {
41   /* FILL ME */
42   LAST_SIGNAL
43 };
44
45 enum
46 {
47   ARG_0
48       /* FILL ME */
49 };
50
51 static void gst_timeoverlay_base_init (gpointer g_class);
52 static void gst_timeoverlay_class_init (gpointer g_class, gpointer class_data);
53 static void gst_timeoverlay_init (GTypeInstance * instance, gpointer g_class);
54
55 static void gst_timeoverlay_set_property (GObject * object, guint prop_id,
56     const GValue * value, GParamSpec * pspec);
57 static void gst_timeoverlay_get_property (GObject * object, guint prop_id,
58     GValue * value, GParamSpec * pspec);
59
60 static void gst_timeoverlay_planar411 (GstVideofilter * videofilter, void *dest,
61     void *src);
62 static void gst_timeoverlay_setup (GstVideofilter * videofilter);
63
64 GType
65 gst_timeoverlay_get_type (void)
66 {
67   static GType timeoverlay_type = 0;
68
69   if (!timeoverlay_type) {
70     static const GTypeInfo timeoverlay_info = {
71       sizeof (GstTimeoverlayClass),
72       gst_timeoverlay_base_init,
73       NULL,
74       gst_timeoverlay_class_init,
75       NULL,
76       NULL,
77       sizeof (GstTimeoverlay),
78       0,
79       gst_timeoverlay_init,
80     };
81
82     timeoverlay_type = g_type_register_static (GST_TYPE_VIDEOFILTER,
83         "GstTimeoverlay", &timeoverlay_info, 0);
84   }
85   return timeoverlay_type;
86 }
87
88 static GstVideofilterFormat gst_timeoverlay_formats[] = {
89   {"I420", 12, gst_timeoverlay_planar411,},
90 };
91
92
93 static void
94 gst_timeoverlay_base_init (gpointer g_class)
95 {
96   static GstElementDetails timeoverlay_details =
97       GST_ELEMENT_DETAILS ("Time Overlay",
98       "Filter/Editor/Video",
99       "Overlays the time on a video stream",
100       "David Schleef <ds@schleef.org>");
101   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
102   GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
103   int i;
104
105   gst_element_class_set_details (element_class, &timeoverlay_details);
106
107   for (i = 0; i < G_N_ELEMENTS (gst_timeoverlay_formats); i++) {
108     gst_videofilter_class_add_format (videofilter_class,
109         gst_timeoverlay_formats + i);
110   }
111
112   gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
113 }
114
115 static void
116 gst_timeoverlay_class_init (gpointer g_class, gpointer class_data)
117 {
118   GObjectClass *gobject_class;
119   GstVideofilterClass *videofilter_class;
120
121   gobject_class = G_OBJECT_CLASS (g_class);
122   videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
123
124 #if 0
125   g_object_class_install_property (gobject_class, ARG_METHOD,
126       g_param_spec_enum ("method", "method", "method",
127           GST_TYPE_TIMEOVERLAY_METHOD, GST_TIMEOVERLAY_METHOD_1,
128           G_PARAM_READWRITE));
129 #endif
130
131   gobject_class->set_property = gst_timeoverlay_set_property;
132   gobject_class->get_property = gst_timeoverlay_get_property;
133
134   videofilter_class->setup = gst_timeoverlay_setup;
135 }
136
137 static void
138 gst_timeoverlay_init (GTypeInstance * instance, gpointer g_class)
139 {
140   GstTimeoverlay *timeoverlay = GST_TIMEOVERLAY (instance);
141   GstVideofilter *videofilter;
142
143   GST_DEBUG ("gst_timeoverlay_init");
144
145   videofilter = GST_VIDEOFILTER (timeoverlay);
146
147   /* do stuff */
148 }
149
150 static void
151 gst_timeoverlay_set_property (GObject * object, guint prop_id,
152     const GValue * value, GParamSpec * pspec)
153 {
154   GstTimeoverlay *src;
155
156   g_return_if_fail (GST_IS_TIMEOVERLAY (object));
157   src = GST_TIMEOVERLAY (object);
158
159   GST_DEBUG ("gst_timeoverlay_set_property");
160   switch (prop_id) {
161 #if 0
162     case ARG_METHOD:
163       src->method = g_value_get_enum (value);
164       break;
165 #endif
166     default:
167       break;
168   }
169 }
170
171 static void
172 gst_timeoverlay_get_property (GObject * object, guint prop_id, GValue * value,
173     GParamSpec * pspec)
174 {
175   GstTimeoverlay *src;
176
177   g_return_if_fail (GST_IS_TIMEOVERLAY (object));
178   src = GST_TIMEOVERLAY (object);
179
180   switch (prop_id) {
181 #if 0
182     case ARG_METHOD:
183       g_value_set_enum (value, src->method);
184       break;
185 #endif
186     default:
187       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188       break;
189   }
190 }
191
192 static void
193 gst_timeoverlay_update_font_height (GstVideofilter * videofilter)
194 {
195   GstTimeoverlay *timeoverlay = GST_TIMEOVERLAY (videofilter);
196   gint width, height;
197   cairo_surface_t *font_surface;
198   cairo_t *font_cairo;
199   cairo_font_extents_t font_extents;
200
201   width = gst_videofilter_get_input_width (videofilter);
202   height = gst_videofilter_get_input_height (videofilter);
203
204   font_surface =
205       cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
206   font_cairo = cairo_create (font_surface);
207   cairo_surface_destroy (font_surface);
208   font_surface = NULL;
209
210   cairo_select_font_face (font_cairo, "monospace", 0, 0);
211   cairo_set_font_size (font_cairo, 20);
212   cairo_font_extents (font_cairo, &font_extents);
213   timeoverlay->text_height = font_extents.height;
214   GST_DEBUG_OBJECT (timeoverlay, "font height is %d", font_extents.height);
215   cairo_destroy (font_cairo);
216   font_cairo = NULL;
217 }
218
219 static void
220 gst_timeoverlay_setup (GstVideofilter * videofilter)
221 {
222   GstTimeoverlay *timeoverlay;
223
224   g_return_if_fail (GST_IS_TIMEOVERLAY (videofilter));
225   timeoverlay = GST_TIMEOVERLAY (videofilter);
226
227   gst_timeoverlay_update_font_height (videofilter);
228 }
229
230 static char *
231 gst_timeoverlay_print_smpte_time (guint64 time)
232 {
233   int hours;
234   int minutes;
235   int seconds;
236   int ms;
237   double x;
238
239   x = rint ((time + 500000) * 1e-6);
240
241   hours = floor (x / (60 * 60 * 1000));
242   x -= hours * 60 * 60 * 1000;
243   minutes = floor (x / (60 * 1000));
244   x -= minutes * 60 * 1000;
245   seconds = floor (x / (1000));
246   x -= seconds * 1000;
247   ms = rint (x);
248
249   return g_strdup_printf ("%02d:%02d:%02d.%03d", hours, minutes, seconds, ms);
250 }
251
252
253 static void
254 gst_timeoverlay_planar411 (GstVideofilter * videofilter, void *dest, void *src)
255 {
256   GstTimeoverlay *timeoverlay;
257   int width;
258   int height;
259   int b_width;
260   char *string;
261   int i, j;
262   unsigned char *image;
263   cairo_text_extents_t extents;
264
265   cairo_surface_t *font_surface;
266   cairo_t *text_cairo;
267
268   g_return_if_fail (GST_IS_TIMEOVERLAY (videofilter));
269   timeoverlay = GST_TIMEOVERLAY (videofilter);
270
271   width = gst_videofilter_get_input_width (videofilter);
272   height = gst_videofilter_get_input_height (videofilter);
273
274   /* create surface for font rendering */
275   /* FIXME: preparation of the surface could also be done once when settings
276    * change */
277   image = g_malloc (4 * width * timeoverlay->text_height);
278
279   font_surface =
280       cairo_image_surface_create_for_data (image, CAIRO_FORMAT_ARGB32, width,
281       timeoverlay->text_height, width * 4);
282   text_cairo = cairo_create (font_surface);
283   cairo_surface_destroy (font_surface);
284   font_surface = NULL;
285
286   /* we draw a rectangle because the compositing on the buffer below
287    * doesn't do alpha */
288   cairo_save (text_cairo);
289   cairo_rectangle (text_cairo, 0, 0, width, timeoverlay->text_height);
290   cairo_set_source_rgba (text_cairo, 0, 0, 0, 1);
291   cairo_set_operator (text_cairo, CAIRO_OPERATOR_SOURCE);
292   cairo_fill (text_cairo);
293   cairo_restore (text_cairo);
294
295   string =
296       gst_timeoverlay_print_smpte_time (GST_BUFFER_TIMESTAMP (videofilter->
297           in_buf));
298   cairo_save (text_cairo);
299   cairo_select_font_face (text_cairo, "monospace", 0, 0);
300   cairo_set_font_size (text_cairo, 20);
301   cairo_text_extents (text_cairo, string, &extents);
302   cairo_set_source_rgb (text_cairo, 1, 1, 1);
303   cairo_move_to (text_cairo, 0, timeoverlay->text_height - 2);
304   cairo_show_text (text_cairo, string);
305   g_free (string);
306 #if 0
307   cairo_text_path (timeoverlay->cr, string);
308   cairo_set_rgb_color (timeoverlay->cr, 1, 1, 1);
309   cairo_set_line_width (timeoverlay->cr, 1.0);
310   cairo_stroke (timeoverlay->cr);
311 #endif
312
313   cairo_restore (text_cairo);
314
315   /* blend width; should retain a max text width so it doesn't jitter */
316   b_width = extents.width;
317   if (b_width > width)
318     b_width = width;
319
320   memcpy (dest, src, videofilter->from_buf_size);
321   for (i = 0; i < timeoverlay->text_height; i++) {
322     for (j = 0; j < b_width; j++) {
323       ((unsigned char *) dest)[i * width + j] = image[(i * width + j) * 4 + 0];
324     }
325   }
326   for (i = 0; i < timeoverlay->text_height / 2; i++) {
327     memset (dest + width * height + i * (width / 2), 128, b_width / 2);
328     memset (dest + width * height + (width / 2) * (height / 2) +
329         i * (width / 2), 128, b_width / 2);
330   }
331
332   cairo_destroy (text_cairo);
333   text_cairo = NULL;
334   g_free (image);
335 }