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 * SECTION:element-cairotimeoverlay
24 * cairotimeoverlay renders the buffer timestamp for each frame on top of
28 * <title>Example launch line</title>
30 * gst-launch videotestsrc ! cairotimeoverlay ! autovideosink
39 #include <gst/math-compat.h>
41 #include <gsttimeoverlay.h>
47 #include <gst/video/video.h>
49 static GstStaticPadTemplate gst_cairo_time_overlay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
56 static GstStaticPadTemplate gst_cairo_time_overlay_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
63 static GstBaseTransformClass *parent_class = NULL;
66 gst_cairo_time_overlay_update_font_height (GstCairoTimeOverlay * timeoverlay)
69 cairo_surface_t *font_surface;
71 cairo_font_extents_t font_extents;
73 width = timeoverlay->width;
74 height = timeoverlay->height;
77 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
78 font_cairo = cairo_create (font_surface);
79 cairo_surface_destroy (font_surface);
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);
92 gst_cairo_time_overlay_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
95 GstCairoTimeOverlay *filter = GST_CAIRO_TIME_OVERLAY (btrans);
96 GstStructure *structure;
99 structure = gst_caps_get_structure (incaps, 0);
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);
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)
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))
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))
122 gst_cairo_time_overlay_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
125 GstCairoTimeOverlay *filter;
126 GstStructure *structure;
127 gboolean ret = FALSE;
130 filter = GST_CAIRO_TIME_OVERLAY (btrans);
132 structure = gst_caps_get_structure (caps, 0);
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);
138 GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
146 gst_cairo_time_overlay_print_smpte_time (guint64 time)
154 x = rint (gst_util_guint64_to_gdouble (time + 500000) * 1e-6);
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));
164 return g_strdup_printf ("%02d:%02d:%02d.%03d", hours, minutes, seconds, ms);
169 gst_cairo_time_overlay_transform (GstBaseTransform * trans, GstBuffer * in,
172 GstCairoTimeOverlay *timeoverlay;
176 int stride_y, stride_u, stride_v;
179 unsigned char *image;
180 cairo_text_extents_t extents;
182 cairo_surface_t *font_surface;
184 GstFlowReturn ret = GST_FLOW_OK;
186 timeoverlay = GST_CAIRO_TIME_OVERLAY (trans);
188 gst_buffer_copy_metadata (out, in, GST_BUFFER_COPY_TIMESTAMPS);
190 src = GST_BUFFER_DATA (in);
191 dest = GST_BUFFER_DATA (out);
193 width = timeoverlay->width;
194 height = timeoverlay->height;
196 /* create surface for font rendering */
197 /* FIXME: preparation of the surface could also be done once when settings
199 image = g_malloc (4 * width * timeoverlay->text_height);
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);
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);
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);
227 cairo_restore (text_cairo);
229 /* blend width; should retain a max text width so it doesn't jitter */
230 b_width = extents.width;
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);
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];
245 for (i = 0; i < timeoverlay->text_height / 2; i++) {
246 memset (dest + GST_VIDEO_I420_U_OFFSET (width, height) + i * stride_u, 128,
248 memset (dest + GST_VIDEO_I420_V_OFFSET (width, height) + i * stride_v, 128,
252 cairo_destroy (text_cairo);
260 gst_cairo_time_overlay_base_init (gpointer g_class)
262 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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>");
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);
275 gst_cairo_time_overlay_class_init (gpointer klass, gpointer class_data)
277 GstBaseTransformClass *trans_class;
279 trans_class = (GstBaseTransformClass *) klass;
281 parent_class = g_type_class_peek_parent (klass);
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);
290 gst_cairo_time_overlay_init (GTypeInstance * instance, gpointer g_class)
295 gst_cairo_time_overlay_get_type (void)
297 static GType cairo_time_overlay_type = 0;
299 if (!cairo_time_overlay_type) {
300 static const GTypeInfo cairo_time_overlay_info = {
301 sizeof (GstCairoTimeOverlayClass),
302 gst_cairo_time_overlay_base_init,
304 gst_cairo_time_overlay_class_init,
307 sizeof (GstCairoTimeOverlay),
309 gst_cairo_time_overlay_init,
312 cairo_time_overlay_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
313 "GstCairoTimeOverlay", &cairo_time_overlay_info, 0);
315 return cairo_time_overlay_type;