Way, way, way too many files: Remove crack comment from the 2000 era.
[platform/upstream/gst-plugins-good.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_setup (GstVideofilter * videofilter)
194 {
195   GstTimeoverlay *timeoverlay;
196   cairo_font_extents_t font_extents;
197
198   g_return_if_fail (GST_IS_TIMEOVERLAY (videofilter));
199   timeoverlay = GST_TIMEOVERLAY (videofilter);
200
201   /* if any setup needs to be done, do it here */
202
203   timeoverlay->cr = cairo_create ();
204
205   cairo_set_rgb_color (timeoverlay->cr, 0, 0, 0);
206
207   cairo_select_font (timeoverlay->cr, "monospace", 0, 0);
208   cairo_scale_font (timeoverlay->cr, 20);
209
210   cairo_current_font_extents (timeoverlay->cr, &font_extents);
211   timeoverlay->text_height = font_extents.height;
212
213 }
214
215 static char *
216 gst_timeoverlay_print_smpte_time (guint64 time)
217 {
218   int hours;
219   int minutes;
220   int seconds;
221   int ms;
222   double x;
223
224   x = rint ((time + 500000) * 1e-6);
225
226   hours = floor (x / (60 * 60 * 1000));
227   x -= hours * 60 * 60 * 1000;
228   minutes = floor (x / (60 * 1000));
229   x -= minutes * 60 * 1000;
230   seconds = floor (x / (1000));
231   x -= seconds * 1000;
232   ms = rint (x);
233
234   return g_strdup_printf ("%02d:%02d:%02d.%03d", hours, minutes, seconds, ms);
235 }
236
237 static void
238 gst_timeoverlay_planar411 (GstVideofilter * videofilter, void *dest, void *src)
239 {
240   GstTimeoverlay *timeoverlay;
241   int width;
242   int height;
243   int b_width;
244   char *string;
245   int i, j;
246   uint8_t *image;
247   cairo_text_extents_t extents;
248
249   g_return_if_fail (GST_IS_TIMEOVERLAY (videofilter));
250   timeoverlay = GST_TIMEOVERLAY (videofilter);
251
252   width = gst_videofilter_get_input_width (videofilter);
253   height = gst_videofilter_get_input_height (videofilter);
254
255   string =
256       gst_timeoverlay_print_smpte_time (GST_BUFFER_TIMESTAMP (videofilter->
257           in_buf));
258
259   image = g_malloc (4 * width * timeoverlay->text_height);
260
261   cairo_set_target_image (timeoverlay->cr, image, CAIRO_FORMAT_ARGB32,
262       width, timeoverlay->text_height, width * 4);
263
264   cairo_save (timeoverlay->cr);
265   cairo_rectangle (timeoverlay->cr, 0, 0, width, timeoverlay->text_height);
266   cairo_set_alpha (timeoverlay->cr, 0);
267   cairo_set_operator (timeoverlay->cr, CAIRO_OPERATOR_SRC);
268   cairo_fill (timeoverlay->cr);
269   cairo_restore (timeoverlay->cr);
270
271   cairo_save (timeoverlay->cr);
272   cairo_text_extents (timeoverlay->cr, string, &extents);
273
274   cairo_set_rgb_color (timeoverlay->cr, 1, 1, 1);
275   cairo_move_to (timeoverlay->cr, 0, timeoverlay->text_height - 2);
276   cairo_show_text (timeoverlay->cr, string);
277   g_free (string);
278 #if 0
279   cairo_text_path (timeoverlay->cr, string);
280   cairo_set_rgb_color (timeoverlay->cr, 1, 1, 1);
281   cairo_set_line_width (timeoverlay->cr, 1.0);
282   cairo_stroke (timeoverlay->cr);
283 #endif
284
285   cairo_restore (timeoverlay->cr);
286
287   b_width = extents.width;
288   if (b_width > width)
289     b_width = width;
290
291   memcpy (dest, src, videofilter->from_buf_size);
292   for (i = 0; i < timeoverlay->text_height; i++) {
293     for (j = 0; j < b_width; j++) {
294       ((uint8_t *) dest)[i * width + j] = image[(i * width + j) * 4 + 0];
295     }
296   }
297   for (i = 0; i < timeoverlay->text_height / 2; i++) {
298     memset (dest + width * height + i * (width / 2), 128, b_width / 2);
299     memset (dest + width * height + (width / 2) * (height / 2) +
300         i * (width / 2), 128, b_width / 2);
301   }
302
303   g_free (image);
304 }