summaryrefslogtreecommitdiff
path: root/ext/rsvg/gstrsvgoverlay.c
blob: d5c707824cea0015fe1d2129f134e89b3621fd15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* GStreamer
 * Copyright (C) 2010 Olivier Aubert <olivier.aubert@liris.cnrs.fr>
 *
 * 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.
 */

/**
 * SECTION:element-rsvgoverlay
 *
 * This elements overlays SVG graphics over the video. SVG data can
 * either be specified through properties, or fed through the
 * data-sink pad.
 *
 * Note: setting the x or y parameter to a non-zero value will implicitly disable the fit-to-frame behaviour.
 *
 * <refsect2>
 * 
 * <title>Example launch lines</title>
 * |[
 * gst-launch -v videotestsrc ! ffmpegcolorspace ! rsvgoverlay location=foo.svg ! ffmpegcolorspace ! autovideosink
 * ]| specifies the SVG location through the filename property. 
 * |[
 * gst-launch -v videotestsrc ! ffmpegcolorspace ! rsvgoverlay name=overlay ! ffmpegcolorspace ! autovideosink filesrc location=foo.svg ! image/svg ! overlay.data_sink
 * ]| does the same by feeding data through the data_sink pad. You can also specify the SVG data itself as parameter:
 * |[
 * gst-launch -v videotestsrc ! ffmpegcolorspace ! rsvgoverlay data='<svg viewBox="0 0 800 600"><image x="80%" y="80%" width="10%" height="10%" xlink:href="foo.jpg" /></svg>' ! ffmpegcolorspace ! autovideosink
 * ]|
 * </refsect2>
 */

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

#include "gstrsvgoverlay.h"
#include <string.h>

#include <gst/video/video.h>

#include <librsvg/rsvg.h>
#include <librsvg/rsvg-cairo.h>

enum
{
  PROP_0,
  PROP_DATA,
  PROP_FILENAME,
  PROP_FIT_TO_FRAME,
  PROP_X,
  PROP_Y,
};

#define GST_RSVG_LOCK(overlay) G_STMT_START { \
  GST_LOG_OBJECT (overlay, "Locking rsvgoverlay from thread %p", g_thread_self ()); \
  g_static_mutex_lock (&overlay->rsvg_lock); \
  GST_LOG_OBJECT (overlay, "Locked rsvgoverlay from thread %p", g_thread_self ()); \
} G_STMT_END

#define GST_RSVG_UNLOCK(overlay) G_STMT_START { \
  GST_LOG_OBJECT (overlay, "Unlocking rsvgoverlay from thread %p", g_thread_self ()); \
  g_static_mutex_unlock (&overlay->rsvg_lock); \
} G_STMT_END

#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#define GST_RSVG_VIDEO_CAPS GST_VIDEO_CAPS_BGRA
#else
#define GST_RSVG_VIDEO_CAPS GST_VIDEO_CAPS_ARGB
#endif

static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (GST_RSVG_VIDEO_CAPS)
    );

static GstStaticPadTemplate video_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (GST_RSVG_VIDEO_CAPS)
    );

static GstStaticPadTemplate data_sink_template =
    GST_STATIC_PAD_TEMPLATE ("data_sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("image/svg+xml; image/svg; text/plain"));

GST_BOILERPLATE (GstRsvgOverlay, gst_rsvg_overlay, GstVideoFilter,
    GST_TYPE_VIDEO_FILTER);

static void
gst_rsvg_overlay_set_svg_data (GstRsvgOverlay * overlay, const gchar * data,
    gboolean consider_as_filename)
{
  GstBaseTransform *btrans = GST_BASE_TRANSFORM (overlay);
  gsize size;
  GError *error = NULL;

  if (overlay->handle) {
    g_object_unref (overlay->handle);
    overlay->handle = NULL;
    gst_base_transform_set_passthrough (btrans, TRUE);
  }

  /* data may be NULL */
  if (data) {
    size = strlen (data);
    if (size) {
      /* Read data either from string or from file */
      if (consider_as_filename)
        overlay->handle = rsvg_handle_new_from_file (data, &error);
      else
        overlay->handle =
            rsvg_handle_new_from_data ((guint8 *) data, size, &error);
      if (error) {
        GST_ERROR_OBJECT (overlay, "Cannot read SVG data: %s\n%s",
            error->message, data);
        g_error_free (error);
      } else {
        /* Get SVG dimension. */
        RsvgDimensionData svg_dimension;
        rsvg_handle_get_dimensions (overlay->handle, &svg_dimension);
        overlay->width = svg_dimension.width;
        overlay->height = svg_dimension.height;
        gst_base_transform_set_passthrough (btrans, FALSE);
      }
    }
  }
}

static void
gst_rsvg_overlay_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstRsvgOverlay *overlay = GST_RSVG_OVERLAY (object);

  GST_RSVG_LOCK (overlay);

  switch (prop_id) {
    case PROP_DATA:
    {
      gst_rsvg_overlay_set_svg_data (overlay, g_value_get_string (value),
          FALSE);
      break;
    }
    case PROP_FILENAME:
    {
      gst_rsvg_overlay_set_svg_data (overlay, g_value_get_string (value), TRUE);
      break;
    }
    case PROP_FIT_TO_FRAME:
    {
      overlay->fit_to_frame = g_value_get_boolean (value);
      break;
    }
    case PROP_X:
    {
      overlay->x_offset = g_value_get_int (value);
      break;
    }
    case PROP_Y:
    {
      overlay->y_offset = g_value_get_int (value);
      break;
    }

    default:
    {
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
  }

  GST_RSVG_UNLOCK (overlay);
}

static void
gst_rsvg_overlay_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstRsvgOverlay *overlay = GST_RSVG_OVERLAY (object);

  switch (prop_id) {
    case PROP_X:
      g_value_set_uint (value, overlay->x_offset);
      break;
    case PROP_Y:
      g_value_set_uint (value, overlay->y_offset);
      break;
    case PROP_FIT_TO_FRAME:
      g_value_set_boolean (value, overlay->fit_to_frame);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static GstFlowReturn
gst_rsvg_overlay_data_sink_chain (GstPad * pad, GstBuffer * buffer)
{
  GstRsvgOverlay *overlay = GST_RSVG_OVERLAY (GST_PAD_PARENT (pad));

  gst_adapter_push (overlay->adapter, buffer);
  return GST_FLOW_OK;
}

static gboolean
gst_rsvg_overlay_data_sink_event (GstPad * pad, GstEvent * event)
{
  GstRsvgOverlay *overlay = GST_RSVG_OVERLAY (GST_PAD_PARENT (pad));

  GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));

  switch (GST_EVENT_TYPE (event)) {

    case GST_EVENT_EOS:{
      guint data_size;

      GST_RSVG_LOCK (overlay);
      /* FIXME: rsvgdec looks for </svg> in data to determine the end
         of SVG code. Should we really do the same here? IOW, could
         data be sent and _pushed() before the EOS gets processed? */
      data_size = gst_adapter_available (overlay->adapter);
      if (data_size) {
        gst_rsvg_overlay_set_svg_data (overlay,
            (const gchar *) gst_adapter_take (overlay->adapter, data_size),
            FALSE);
        gst_adapter_clear (overlay->adapter);
      }
      GST_RSVG_UNLOCK (overlay);
    }

    case GST_EVENT_FLUSH_START:
      gst_adapter_clear (overlay->adapter);
      break;

    default:
      break;
  }

  /* Dropping all events here */
  gst_event_unref (event);
  return TRUE;
}

static gboolean
gst_rsvg_overlay_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
    GstCaps * outcaps)
{
  GstRsvgOverlay *overlay = GST_RSVG_OVERLAY (btrans);

  return G_LIKELY (gst_video_format_parse_caps (incaps,
          &overlay->caps_format, &overlay->caps_width, &overlay->caps_height));
}

static GstFlowReturn
gst_rsvg_overlay_transform_ip (GstBaseTransform * btrans, GstBuffer * buf)
{
  GstRsvgOverlay *overlay = GST_RSVG_OVERLAY (btrans);
  cairo_surface_t *surface;
  cairo_t *cr;

  GST_RSVG_LOCK (overlay);
  if (!overlay->handle) {
    GST_RSVG_UNLOCK (overlay);
    return GST_FLOW_OK;
  }

  surface =
      cairo_image_surface_create_for_data (GST_BUFFER_DATA (buf),
      CAIRO_FORMAT_ARGB32, overlay->caps_width, overlay->caps_height,
      overlay->caps_width * 4);
  if (G_UNLIKELY (!surface))
    return GST_FLOW_ERROR;

  cr = cairo_create (surface);
  if (G_UNLIKELY (!cr)) {
    cairo_surface_destroy (surface);
    return GST_FLOW_ERROR;
  }

  /* If x or y offset is specified, do not fit-to-frame. */
  if (overlay->x_offset || overlay->y_offset)
    cairo_translate (cr, (double) overlay->x_offset,
        (double) overlay->y_offset);
  else if (overlay->fit_to_frame && overlay->width && overlay->height)
    cairo_scale (cr, (float) overlay->caps_width / overlay->width,
        (float) overlay->caps_height / overlay->height);

  rsvg_handle_render_cairo (overlay->handle, cr);
  GST_RSVG_UNLOCK (overlay);

  cairo_destroy (cr);
  cairo_surface_destroy (surface);

  return GST_FLOW_OK;
}

static gboolean
gst_rsvg_overlay_stop (GstBaseTransform * btrans)
{
  GstRsvgOverlay *overlay = GST_RSVG_OVERLAY (btrans);

  if (overlay->handle) {
    g_object_unref (overlay->handle);
    g_object_unref (overlay->adapter);
    overlay->handle = NULL;
  }

  return TRUE;
}

static void
gst_rsvg_overlay_base_init (gpointer klass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&src_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&video_sink_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&data_sink_template));

  gst_element_class_set_details_simple (element_class, "RSVG overlay",
      "Filter/Editor/Video",
      "Overlays SVG graphics over a video stream",
      "Olivier Aubert <olivier.aubert@liris.cnrs.fr>");
}

static void
gst_rsvg_overlay_class_init (GstRsvgOverlayClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstBaseTransformClass *basetransform_class = GST_BASE_TRANSFORM_CLASS (klass);

  gobject_class->set_property = gst_rsvg_overlay_set_property;
  gobject_class->get_property = gst_rsvg_overlay_get_property;

  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DATA,
      g_param_spec_string ("data", "data", "SVG data.", "",
          G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILENAME,
      g_param_spec_string ("location", "location", "SVG file location.", "",
          G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FIT_TO_FRAME,
      g_param_spec_boolean ("fit-to-frame", "fit to frame",
          "Fit the SVG to fill the whole frame.", TRUE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_X,
      g_param_spec_int ("x", "x offset",
          "Specify an x offset.", 0, G_MAXINT, 0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_Y,
      g_param_spec_int ("y", "y offset",
          "Specify a y offset.", 0, G_MAXINT, 0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  basetransform_class->set_caps = gst_rsvg_overlay_set_caps;
  basetransform_class->transform_ip = gst_rsvg_overlay_transform_ip;
  basetransform_class->stop = gst_rsvg_overlay_stop;
  basetransform_class->passthrough_on_same_caps = FALSE;
}

static void
gst_rsvg_overlay_init (GstRsvgOverlay * overlay, GstRsvgOverlayClass * klass)
{
  overlay->fit_to_frame = 1;
  overlay->adapter = gst_adapter_new ();

  /* data sink */
  overlay->data_sinkpad =
      gst_pad_new_from_static_template (&data_sink_template, "data_sink");
  gst_pad_set_chain_function (overlay->data_sinkpad,
      GST_DEBUG_FUNCPTR (gst_rsvg_overlay_data_sink_chain));
  gst_pad_set_event_function (overlay->data_sinkpad,
      GST_DEBUG_FUNCPTR (gst_rsvg_overlay_data_sink_event));
  gst_element_add_pad (GST_ELEMENT (overlay), overlay->data_sinkpad);
}