2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David Schleef <ds@schleef.org>
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.
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.
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.
22 * This file was (probably) generated from gsttimeoverlay.c,
23 * gsttimeoverlay.c,v 1.7 2003/11/08 02:48:59 dschleef Exp
30 /*#define DEBUG_ENABLED */
31 #include <gsttimeoverlay.h>
38 /* GstTimeoverlay signals and args */
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);
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);
60 static void gst_timeoverlay_planar411 (GstVideofilter * videofilter, void *dest,
62 static void gst_timeoverlay_setup (GstVideofilter * videofilter);
65 gst_timeoverlay_get_type (void)
67 static GType timeoverlay_type = 0;
69 if (!timeoverlay_type) {
70 static const GTypeInfo timeoverlay_info = {
71 sizeof (GstTimeoverlayClass),
72 gst_timeoverlay_base_init,
74 gst_timeoverlay_class_init,
77 sizeof (GstTimeoverlay),
82 timeoverlay_type = g_type_register_static (GST_TYPE_VIDEOFILTER,
83 "GstTimeoverlay", &timeoverlay_info, 0);
85 return timeoverlay_type;
88 static GstVideofilterFormat gst_timeoverlay_formats[] = {
89 {"I420", 12, gst_timeoverlay_planar411,},
94 gst_timeoverlay_base_init (gpointer g_class)
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);
105 gst_element_class_set_details (element_class, &timeoverlay_details);
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);
112 gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
116 gst_timeoverlay_class_init (gpointer g_class, gpointer class_data)
118 GObjectClass *gobject_class;
119 GstVideofilterClass *videofilter_class;
121 gobject_class = G_OBJECT_CLASS (g_class);
122 videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
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,
131 gobject_class->set_property = gst_timeoverlay_set_property;
132 gobject_class->get_property = gst_timeoverlay_get_property;
134 videofilter_class->setup = gst_timeoverlay_setup;
138 gst_timeoverlay_init (GTypeInstance * instance, gpointer g_class)
140 GstTimeoverlay *timeoverlay = GST_TIMEOVERLAY (instance);
141 GstVideofilter *videofilter;
143 GST_DEBUG ("gst_timeoverlay_init");
145 videofilter = GST_VIDEOFILTER (timeoverlay);
151 gst_timeoverlay_set_property (GObject * object, guint prop_id,
152 const GValue * value, GParamSpec * pspec)
156 g_return_if_fail (GST_IS_TIMEOVERLAY (object));
157 src = GST_TIMEOVERLAY (object);
159 GST_DEBUG ("gst_timeoverlay_set_property");
163 src->method = g_value_get_enum (value);
172 gst_timeoverlay_get_property (GObject * object, guint prop_id, GValue * value,
177 g_return_if_fail (GST_IS_TIMEOVERLAY (object));
178 src = GST_TIMEOVERLAY (object);
183 g_value_set_enum (value, src->method);
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 gst_timeoverlay_update_font_height (GstVideofilter * videofilter)
195 GstTimeoverlay *timeoverlay = GST_TIMEOVERLAY (videofilter);
197 cairo_surface_t *font_surface;
199 cairo_font_extents_t font_extents;
201 width = gst_videofilter_get_input_width (videofilter);
202 height = gst_videofilter_get_input_height (videofilter);
205 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
206 font_cairo = cairo_create (font_surface);
207 cairo_surface_destroy (font_surface);
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);
220 gst_timeoverlay_setup (GstVideofilter * videofilter)
222 GstTimeoverlay *timeoverlay;
224 g_return_if_fail (GST_IS_TIMEOVERLAY (videofilter));
225 timeoverlay = GST_TIMEOVERLAY (videofilter);
227 gst_timeoverlay_update_font_height (videofilter);
231 gst_timeoverlay_print_smpte_time (guint64 time)
239 x = rint ((time + 500000) * 1e-6);
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));
249 return g_strdup_printf ("%02d:%02d:%02d.%03d", hours, minutes, seconds, ms);
254 gst_timeoverlay_planar411 (GstVideofilter * videofilter, void *dest, void *src)
256 GstTimeoverlay *timeoverlay;
262 unsigned char *image;
263 cairo_text_extents_t extents;
265 cairo_surface_t *font_surface;
268 g_return_if_fail (GST_IS_TIMEOVERLAY (videofilter));
269 timeoverlay = GST_TIMEOVERLAY (videofilter);
271 width = gst_videofilter_get_input_width (videofilter);
272 height = gst_videofilter_get_input_height (videofilter);
274 /* create surface for font rendering */
275 /* FIXME: preparation of the surface could also be done once when settings
277 image = g_malloc (4 * width * timeoverlay->text_height);
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);
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);
296 gst_timeoverlay_print_smpte_time (GST_BUFFER_TIMESTAMP (videofilter->
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);
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);
313 cairo_restore (text_cairo);
315 /* blend width; should retain a max text width so it doesn't jitter */
316 b_width = extents.width;
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];
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);
332 cairo_destroy (text_cairo);