gsttimeoverlay.c 9.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 * Copyright (C) <2003> David Schleef <ds@schleef.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

21 22 23 24 25
/**
 * SECTION:element-cairotimeoverlay
 *
 * cairotimeoverlay renders the buffer timestamp for each frame on top of
 * the frame.
26 27
 *
 * <refsect2>
28
 * <title>Example launch line</title>
29 30 31
 * |[
 * gst-launch videotestsrc ! cairotimeoverlay ! autovideosink
 * ]|
32
 * </refsect2>
33 34 35 36 37 38
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

39 40
#include <gst/math-compat.h>

41
#include <gsttimeoverlay.h>
Julien Moutte's avatar
Julien Moutte committed
42

43 44 45 46
#include <string.h>

#include <cairo.h>

Julien Moutte's avatar
Julien Moutte committed
47
#include <gst/video/video.h>
48

49
static GstStaticPadTemplate gst_cairo_time_overlay_src_template =
Julien Moutte's avatar
Julien Moutte committed
50 51 52 53 54
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
    );
55

56
static GstStaticPadTemplate gst_cairo_time_overlay_sink_template =
Julien Moutte's avatar
Julien Moutte committed
57 58 59 60 61
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
    );
62

63
static GstBaseTransformClass *parent_class = NULL;
64 65

static void
66
gst_cairo_time_overlay_update_font_height (GstCairoTimeOverlay * timeoverlay)
67
{
68 69 70
  gint width, height;
  cairo_surface_t *font_surface;
  cairo_t *font_cairo;
71 72
  cairo_font_extents_t font_extents;

Julien Moutte's avatar
Julien Moutte committed
73 74
  width = timeoverlay->width;
  height = timeoverlay->height;
75

76 77 78 79 80
  font_surface =
      cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
  font_cairo = cairo_create (font_surface);
  cairo_surface_destroy (font_surface);
  font_surface = NULL;
81

82 83 84 85
  cairo_select_font_face (font_cairo, "monospace", 0, 0);
  cairo_set_font_size (font_cairo, 20);
  cairo_font_extents (font_cairo, &font_extents);
  timeoverlay->text_height = font_extents.height;
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
86
  GST_DEBUG_OBJECT (timeoverlay, "font height is %f", font_extents.height);
87 88 89
  cairo_destroy (font_cairo);
  font_cairo = NULL;
}
90

Julien Moutte's avatar
Julien Moutte committed
91
static gboolean
92
gst_cairo_time_overlay_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
Julien Moutte's avatar
Julien Moutte committed
93
    GstCaps * outcaps)
94
{
95
  GstCairoTimeOverlay *filter = GST_CAIRO_TIME_OVERLAY (btrans);
Julien Moutte's avatar
Julien Moutte committed
96 97 98 99 100 101 102
  GstStructure *structure;
  gboolean ret = FALSE;

  structure = gst_caps_get_structure (incaps, 0);

  if (gst_structure_get_int (structure, "width", &filter->width) &&
      gst_structure_get_int (structure, "height", &filter->height)) {
103
    gst_cairo_time_overlay_update_font_height (filter);
Julien Moutte's avatar
Julien Moutte committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    ret = TRUE;
  }

  return ret;
}

/* Useful macros */
#define GST_VIDEO_I420_Y_ROWSTRIDE(width) (GST_ROUND_UP_4(width))
#define GST_VIDEO_I420_U_ROWSTRIDE(width) (GST_ROUND_UP_8(width)/2)
#define GST_VIDEO_I420_V_ROWSTRIDE(width) ((GST_ROUND_UP_8(GST_VIDEO_I420_Y_ROWSTRIDE(width)))/2)

#define GST_VIDEO_I420_Y_OFFSET(w,h) (0)
#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)))
#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))

#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))

static gboolean
122
gst_cairo_time_overlay_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
Julien Moutte's avatar
Julien Moutte committed
123 124
    guint * size)
{
125
  GstCairoTimeOverlay *filter;
Julien Moutte's avatar
Julien Moutte committed
126 127 128 129
  GstStructure *structure;
  gboolean ret = FALSE;
  gint width, height;

130
  filter = GST_CAIRO_TIME_OVERLAY (btrans);
Julien Moutte's avatar
Julien Moutte committed
131 132

  structure = gst_caps_get_structure (caps, 0);
133

Julien Moutte's avatar
Julien Moutte committed
134 135 136 137 138 139 140
  if (gst_structure_get_int (structure, "width", &width) &&
      gst_structure_get_int (structure, "height", &height)) {
    *size = GST_VIDEO_I420_SIZE (width, height);
    ret = TRUE;
    GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
        width, height);
  }
141

Julien Moutte's avatar
Julien Moutte committed
142
  return ret;
143 144 145
}

static char *
146
gst_cairo_time_overlay_print_smpte_time (guint64 time)
147 148 149 150 151 152 153
{
  int hours;
  int minutes;
  int seconds;
  int ms;
  double x;

154
  x = rint (gst_util_guint64_to_gdouble (time + 500000) * 1e-6);
155 156 157 158 159 160 161 162 163 164 165 166

  hours = floor (x / (60 * 60 * 1000));
  x -= hours * 60 * 60 * 1000;
  minutes = floor (x / (60 * 1000));
  x -= minutes * 60 * 1000;
  seconds = floor (x / (1000));
  x -= seconds * 1000;
  ms = rint (x);

  return g_strdup_printf ("%02d:%02d:%02d.%03d", hours, minutes, seconds, ms);
}

167

Julien Moutte's avatar
Julien Moutte committed
168
static GstFlowReturn
169
gst_cairo_time_overlay_transform (GstBaseTransform * trans, GstBuffer * in,
Julien Moutte's avatar
Julien Moutte committed
170
    GstBuffer * out)
171
{
172
  GstCairoTimeOverlay *timeoverlay;
173 174 175
  int width;
  int height;
  int b_width;
176
  int stride_y, stride_u, stride_v;
177 178
  char *string;
  int i, j;
179
  unsigned char *image;
180
  cairo_text_extents_t extents;
181
  guint8 *dest, *src;
182 183
  cairo_surface_t *font_surface;
  cairo_t *text_cairo;
Julien Moutte's avatar
Julien Moutte committed
184 185
  GstFlowReturn ret = GST_FLOW_OK;

186
  timeoverlay = GST_CAIRO_TIME_OVERLAY (trans);
Julien Moutte's avatar
Julien Moutte committed
187

188
  gst_buffer_copy_metadata (out, in, GST_BUFFER_COPY_TIMESTAMPS);
189

Julien Moutte's avatar
Julien Moutte committed
190 191
  src = GST_BUFFER_DATA (in);
  dest = GST_BUFFER_DATA (out);
192

Julien Moutte's avatar
Julien Moutte committed
193 194
  width = timeoverlay->width;
  height = timeoverlay->height;
195

196 197 198
  /* create surface for font rendering */
  /* FIXME: preparation of the surface could also be done once when settings
   * change */
199 200
  image = g_malloc (4 * width * timeoverlay->text_height);

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  font_surface =
      cairo_image_surface_create_for_data (image, CAIRO_FORMAT_ARGB32, width,
      timeoverlay->text_height, width * 4);
  text_cairo = cairo_create (font_surface);
  cairo_surface_destroy (font_surface);
  font_surface = NULL;

  /* we draw a rectangle because the compositing on the buffer below
   * doesn't do alpha */
  cairo_save (text_cairo);
  cairo_rectangle (text_cairo, 0, 0, width, timeoverlay->text_height);
  cairo_set_source_rgba (text_cairo, 0, 0, 0, 1);
  cairo_set_operator (text_cairo, CAIRO_OPERATOR_SOURCE);
  cairo_fill (text_cairo);
  cairo_restore (text_cairo);
216

217
  string = gst_cairo_time_overlay_print_smpte_time (GST_BUFFER_TIMESTAMP (in));
218 219 220 221 222 223 224
  cairo_save (text_cairo);
  cairo_select_font_face (text_cairo, "monospace", 0, 0);
  cairo_set_font_size (text_cairo, 20);
  cairo_text_extents (text_cairo, string, &extents);
  cairo_set_source_rgb (text_cairo, 1, 1, 1);
  cairo_move_to (text_cairo, 0, timeoverlay->text_height - 2);
  cairo_show_text (text_cairo, string);
225 226
  g_free (string);

227
  cairo_restore (text_cairo);
228

229
  /* blend width; should retain a max text width so it doesn't jitter */
230 231 232 233
  b_width = extents.width;
  if (b_width > width)
    b_width = width;

234 235 236 237
  stride_y = GST_VIDEO_I420_Y_ROWSTRIDE (width);
  stride_u = GST_VIDEO_I420_U_ROWSTRIDE (width);
  stride_v = GST_VIDEO_I420_V_ROWSTRIDE (width);

Julien Moutte's avatar
Julien Moutte committed
238
  memcpy (dest, src, GST_BUFFER_SIZE (in));
239 240
  for (i = 0; i < timeoverlay->text_height; i++) {
    for (j = 0; j < b_width; j++) {
241 242
      ((unsigned char *) dest)[i * stride_y + j] =
          image[(i * width + j) * 4 + 0];
243 244 245
    }
  }
  for (i = 0; i < timeoverlay->text_height / 2; i++) {
246 247 248 249
    memset (dest + GST_VIDEO_I420_U_OFFSET (width, height) + i * stride_u, 128,
        b_width / 2);
    memset (dest + GST_VIDEO_I420_V_OFFSET (width, height) + i * stride_v, 128,
        b_width / 2);
250 251
  }

252 253
  cairo_destroy (text_cairo);
  text_cairo = NULL;
254
  g_free (image);
Julien Moutte's avatar
Julien Moutte committed
255 256 257 258 259

  return ret;
}

static void
260
gst_cairo_time_overlay_base_init (gpointer g_class)
Julien Moutte's avatar
Julien Moutte committed
261 262 263
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

264
  gst_element_class_set_static_metadata (element_class, "Time overlay",
265 266
      "Filter/Editor/Video",
      "Overlays the time on a video stream", "David Schleef <ds@schleef.org>");
Julien Moutte's avatar
Julien Moutte committed
267 268

  gst_element_class_add_pad_template (element_class,
269
      gst_static_pad_template_get (&gst_cairo_time_overlay_sink_template));
Julien Moutte's avatar
Julien Moutte committed
270
  gst_element_class_add_pad_template (element_class,
271
      gst_static_pad_template_get (&gst_cairo_time_overlay_src_template));
Julien Moutte's avatar
Julien Moutte committed
272 273 274
}

static void
275
gst_cairo_time_overlay_class_init (gpointer klass, gpointer class_data)
Julien Moutte's avatar
Julien Moutte committed
276 277 278 279 280 281 282
{
  GstBaseTransformClass *trans_class;

  trans_class = (GstBaseTransformClass *) klass;

  parent_class = g_type_class_peek_parent (klass);

283
  trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_cairo_time_overlay_set_caps);
Julien Moutte's avatar
Julien Moutte committed
284
  trans_class->get_unit_size =
285 286
      GST_DEBUG_FUNCPTR (gst_cairo_time_overlay_get_unit_size);
  trans_class->transform = GST_DEBUG_FUNCPTR (gst_cairo_time_overlay_transform);
Julien Moutte's avatar
Julien Moutte committed
287 288 289
}

static void
290
gst_cairo_time_overlay_init (GTypeInstance * instance, gpointer g_class)
Julien Moutte's avatar
Julien Moutte committed
291 292 293 294
{
}

GType
295
gst_cairo_time_overlay_get_type (void)
Julien Moutte's avatar
Julien Moutte committed
296
{
297
  static GType cairo_time_overlay_type = 0;
Julien Moutte's avatar
Julien Moutte committed
298

299 300 301 302
  if (!cairo_time_overlay_type) {
    static const GTypeInfo cairo_time_overlay_info = {
      sizeof (GstCairoTimeOverlayClass),
      gst_cairo_time_overlay_base_init,
Julien Moutte's avatar
Julien Moutte committed
303
      NULL,
304
      gst_cairo_time_overlay_class_init,
Julien Moutte's avatar
Julien Moutte committed
305 306
      NULL,
      NULL,
307
      sizeof (GstCairoTimeOverlay),
Julien Moutte's avatar
Julien Moutte committed
308
      0,
309
      gst_cairo_time_overlay_init,
Julien Moutte's avatar
Julien Moutte committed
310 311
    };

312 313
    cairo_time_overlay_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
        "GstCairoTimeOverlay", &cairo_time_overlay_info, 0);
Julien Moutte's avatar
Julien Moutte committed
314
  }
315
  return cairo_time_overlay_type;
316
}