gstframepositioner.c 15.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* GStreamer
 * Copyright (C) 2013 Mathieu Duponchelle <mduponchelle1@gmail.com>
 *
 * 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., 51 Franklin Street, Suite 500,
 * Boston, MA 02110-1335, USA.
 */

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

#include <gst/gst.h>
25
#include <gst/video/video.h>
26

27
#include "gstframepositioner.h"
28

29
30
31
32
/* We  need to define a max number of pixel so we can interpolate them */
#define MAX_PIXELS 100000
#define MIN_PIXELS -100000

33
static void gst_frame_positioner_set_property (GObject * object,
34
    guint property_id, const GValue * value, GParamSpec * pspec);
35
static void gst_frame_positioner_get_property (GObject * object,
36
    guint property_id, GValue * value, GParamSpec * pspec);
37
static GstFlowReturn gst_frame_positioner_transform_ip (GstBaseTransform *
38
39
40
    trans, GstBuffer * buf);

static gboolean
41
gst_frame_positioner_meta_init (GstMeta * meta, gpointer params,
42
    GstBuffer * buffer);
43
static gboolean gst_frame_positioner_meta_transform (GstBuffer * dest,
44
    GstMeta * meta, GstBuffer * buffer, GQuark type, gpointer data);
45
46
47
48
49
50
51

enum
{
  PROP_0,
  PROP_ALPHA,
  PROP_POSX,
  PROP_POSY,
52
53
54
  PROP_ZORDER,
  PROP_WIDTH,
  PROP_HEIGHT
55
56
};

57
static GstStaticPadTemplate gst_frame_positioner_src_template =
58
59
60
61
62
63
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw")
    );

64
static GstStaticPadTemplate gst_frame_positioner_sink_template =
65
66
67
68
69
70
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw")
    );

71
G_DEFINE_TYPE (GstFramePositioner, gst_frame_positioner,
72
73
    GST_TYPE_BASE_TRANSFORM);

74
static void
75
_weak_notify_cb (GstFramePositioner * pos, GObject * old)
76
77
78
79
80
{
  pos->current_track = NULL;
}

static void
81
gst_frame_positioner_update_properties (GstFramePositioner * pos,
82
    gboolean track_mixing, gint old_track_width, gint old_track_height)
83
{
84
  GstCaps *caps;
85
86
87
88

  if (pos->capsfilter == NULL)
    return;

89
90
  if (pos->track_width && pos->track_height &&
      (!track_mixing || !pos->scale_in_compositor)) {
91
    caps =
92
        gst_caps_new_simple ("video/x-raw", "width", G_TYPE_INT,
93
94
95
96
        pos->track_width, "height", G_TYPE_INT, pos->track_height, NULL);
  } else {
    caps = gst_caps_new_empty_simple ("video/x-raw");
  }
97

98
99
100
101
  if (pos->fps_n != -1)
    gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, pos->fps_n,
        pos->fps_d, NULL);

102
103
104
105
  if (pos->par_n != -1)
    gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
        pos->par_n, pos->par_d, NULL);

106
  if (old_track_width && pos->width == old_track_width &&
107
      old_track_height && pos->height == old_track_height &&
108
      pos->track_height && pos->track_width &&
109
110
      ((float) old_track_width / (float) old_track_height) ==
      ((float) pos->track_width / (float) pos->track_height)) {
111

112
    GST_DEBUG_OBJECT (pos, "Following track size width old_track: %d -- pos: %d"
113
114
115
116
117
118
119
        " || height, old_track %d -- pos: %d",
        old_track_width, pos->width, old_track_height, pos->height);

    pos->width = pos->track_width;
    pos->height = pos->track_height;
  }

120
  GST_DEBUG_OBJECT (caps, "setting caps");
121
122

  g_object_set (pos->capsfilter, "caps", caps, NULL);
123

124
  gst_caps_unref (caps);
125
126
127
}

static void
128
sync_properties_from_track (GstFramePositioner * pos, GESTrack * track)
129
130
{
  gint width, height;
131
  gint old_track_width, old_track_height;
132
133
134
  GstCaps *caps;

  g_object_get (track, "restriction-caps", &caps, NULL);
135
136
137
138
139
140
141
142
143
144

  width = height = 0;

  if (caps && gst_caps_get_size (caps) > 0) {
    GstStructure *structure;

    structure = gst_caps_get_structure (caps, 0);
    if (!gst_structure_get_int (structure, "width", &width))
      width = 0;
    if (!gst_structure_get_int (structure, "height", &height))
145
      height = 0;
146
147
148
    if (!gst_structure_get_fraction (structure, "framerate", &(pos->fps_n),
            &(pos->fps_d)))
      pos->fps_n = -1;
149
150
151
152

    if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
            &(pos->par_n), &(pos->par_d)))
      pos->par_n = -1;
153
154
  }

155
156
157
  old_track_width = pos->track_width;
  old_track_height = pos->track_height;

158
159
160
  pos->track_width = width;
  pos->track_height = height;

161
162
  GST_DEBUG_OBJECT (pos, "syncing framerate from caps : %d/%d", pos->fps_n,
      pos->fps_d);
163

164
165
  gst_frame_positioner_update_properties (pos, ges_track_get_mixing (track),
      old_track_width, old_track_height);
166
167
168
169
}

static void
_track_restriction_changed_cb (GESTrack * track, GParamSpec * arg G_GNUC_UNUSED,
170
    GstFramePositioner * pos)
171
{
172
  sync_properties_from_track (pos, track);
173
174
175
}

static void
176
set_track (GstFramePositioner * pos)
177
178
179
180
181
182
183
184
185
186
{
  GESTrack *new_track;

  if (pos->current_track) {
    g_signal_handlers_disconnect_by_func (pos->current_track,
        (GCallback) _track_restriction_changed_cb, pos);
    g_object_weak_unref (G_OBJECT (pos->current_track),
        (GWeakNotify) _weak_notify_cb, pos);
  }

187
  new_track = ges_track_element_get_track (pos->track_source);
188
189
190
191
192
  if (new_track) {
    pos->current_track = new_track;
    g_object_weak_ref (G_OBJECT (new_track), (GWeakNotify) _weak_notify_cb,
        pos);
    GST_DEBUG_OBJECT (pos, "connecting to track : %p", pos->current_track);
193

194
195
    g_signal_connect (pos->current_track, "notify::restriction-caps",
        (GCallback) _track_restriction_changed_cb, pos);
196
    sync_properties_from_track (pos, pos->current_track);
197
  } else {
198
    pos->current_track = NULL;
199
200
201
202
203
  }
}

static void
_track_changed_cb (GESTrackElement * trksrc, GParamSpec * arg G_GNUC_UNUSED,
204
    GstFramePositioner * pos)
205
206
{
  set_track (pos);
207
208
}

209
210
211
212
static void
_trk_element_weak_notify_cb (GstFramePositioner * pos, GObject * old)
{
  pos->track_source = NULL;
213
  gst_object_unref (pos);
214
215
}

216
void
217
ges_frame_positioner_set_source_and_filter (GstFramePositioner * pos,
218
219
220
221
222
    GESTrackElement * trksrc, GstElement * capsfilter)
{
  pos->track_source = trksrc;
  pos->capsfilter = capsfilter;
  pos->current_track = ges_track_element_get_track (trksrc);
223

224
  g_object_weak_ref (G_OBJECT (trksrc),
225
      (GWeakNotify) _trk_element_weak_notify_cb, gst_object_ref (pos));
226
227
  g_signal_connect (trksrc, "notify::track", (GCallback) _track_changed_cb,
      pos);
228
  set_track (pos);
229
230
231
}

static void
232
gst_frame_positioner_dispose (GObject * object)
233
{
234
  GstFramePositioner *pos = GST_FRAME_POSITIONNER (object);
235
236
237
238
239
240
241
242
243
244
245
246
247
248

  if (pos->track_source) {
    g_signal_handlers_disconnect_by_func (pos->track_source, _track_changed_cb,
        pos);
    pos->track_source = NULL;
  }

  if (pos->current_track) {
    g_signal_handlers_disconnect_by_func (pos->current_track,
        _track_restriction_changed_cb, pos);
    g_object_weak_unref (G_OBJECT (pos->current_track),
        (GWeakNotify) _weak_notify_cb, pos);
    pos->current_track = NULL;
  }
249

250
  G_OBJECT_CLASS (gst_frame_positioner_parent_class)->dispose (object);
251
252
}

253
static void
254
gst_frame_positioner_class_init (GstFramePositionerClass * klass)
255
256
257
258
259
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GstBaseTransformClass *base_transform_class =
      GST_BASE_TRANSFORM_CLASS (klass);

260
  gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
261
      &gst_frame_positioner_src_template);
262
  gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
263
      &gst_frame_positioner_sink_template);
264

265
266
267
  gobject_class->set_property = gst_frame_positioner_set_property;
  gobject_class->get_property = gst_frame_positioner_get_property;
  gobject_class->dispose = gst_frame_positioner_dispose;
268
  base_transform_class->transform_ip =
269
      GST_DEBUG_FUNCPTR (gst_frame_positioner_transform_ip);
270
271

  /**
272
   * gstframepositioner:alpha:
273
274
275
276
277
278
279
280
   *
   * The desired alpha for the stream.
   */
  g_object_class_install_property (gobject_class, PROP_ALPHA,
      g_param_spec_double ("alpha", "alpha", "alpha of the stream",
          0.0, 1.0, 1.0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));

  /**
281
   * gstframepositioner:posx:
282
283
284
   *
   * The desired x position for the stream.
   */
285
  g_object_class_install_property (gobject_class, PROP_POSX,
286
      g_param_spec_int ("posx", "posx", "x position of the stream",
287
288
289
          MIN_PIXELS, MAX_PIXELS, 0,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));

290
291

  /**
292
   * gstframepositioner:posy:
293
294
295
   *
   * The desired y position for the stream.
   */
296
  g_object_class_install_property (gobject_class, PROP_POSY,
297
      g_param_spec_int ("posy", "posy", "y position of the stream",
298
299
          MIN_PIXELS, MAX_PIXELS, 0,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
300
301

  /**
302
   * gstframepositioner:zorder:
303
304
305
306
307
   *
   * The desired z order for the stream.
   */
  g_object_class_install_property (gobject_class, PROP_ZORDER,
      g_param_spec_uint ("zorder", "zorder", "z order of the stream",
308
          0, G_MAXUINT, 0, G_PARAM_READWRITE));
309

310
  /**
311
   * gesframepositioner:width:
312
313
314
315
316
317
   *
   * The desired width for that source.
   * Set to 0 if size is not mandatory, will be set to width of the current track.
   */
  g_object_class_install_property (gobject_class, PROP_WIDTH,
      g_param_spec_int ("width", "width", "width of the source",
318
          0, MAX_PIXELS, 0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
319
320

  /**
321
   * gesframepositioner:height:
322
323
324
325
326
327
   *
   * The desired height for that source.
   * Set to 0 if size is not mandatory, will be set to height of the current track.
   */
  g_object_class_install_property (gobject_class, PROP_HEIGHT,
      g_param_spec_int ("height", "height", "height of the source",
328
          0, MAX_PIXELS, 0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
329

330
  gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
331
      "frame positioner", "Metadata",
332
333
334
335
336
      "This element provides with tagging facilities",
      "mduponchelle1@gmail.com");
}

static void
337
gst_frame_positioner_init (GstFramePositioner * framepositioner)
338
{
339
340
341
342
343
344
345
346
347
348
349
350
351
  framepositioner->alpha = 1.0;
  framepositioner->posx = 0.0;
  framepositioner->posy = 0.0;
  framepositioner->zorder = 0;
  framepositioner->width = 0;
  framepositioner->height = 0;
  framepositioner->fps_n = -1;
  framepositioner->fps_d = -1;
  framepositioner->track_width = 0;
  framepositioner->track_height = 0;
  framepositioner->capsfilter = NULL;
  framepositioner->track_source = NULL;
  framepositioner->current_track = NULL;
352
  framepositioner->scale_in_compositor = TRUE;
353
354
355

  framepositioner->par_n = -1;
  framepositioner->par_d = 1;
356
357
358
}

void
359
gst_frame_positioner_set_property (GObject * object, guint property_id,
360
361
    const GValue * value, GParamSpec * pspec)
{
362
  GstFramePositioner *framepositioner = GST_FRAME_POSITIONNER (object);
363
364
365
366
  gboolean track_mixing = TRUE;

  if (framepositioner->current_track)
    track_mixing = ges_track_get_mixing (framepositioner->current_track);
367
368


369
  GST_OBJECT_LOCK (framepositioner);
370
371
  switch (property_id) {
    case PROP_ALPHA:
372
      framepositioner->alpha = g_value_get_double (value);
373
374
      break;
    case PROP_POSX:
375
      framepositioner->posx = g_value_get_int (value);
376
377
      break;
    case PROP_POSY:
378
      framepositioner->posy = g_value_get_int (value);
379
380
      break;
    case PROP_ZORDER:
381
      framepositioner->zorder = g_value_get_uint (value);
382
      break;
383
    case PROP_WIDTH:
384
      framepositioner->width = g_value_get_int (value);
385
386
      gst_frame_positioner_update_properties (framepositioner, track_mixing,
          0, 0);
387
388
      break;
    case PROP_HEIGHT:
389
      framepositioner->height = g_value_get_int (value);
390
391
      gst_frame_positioner_update_properties (framepositioner, track_mixing,
          0, 0);
392
      break;
393
394
395
396
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
397
  GST_OBJECT_UNLOCK (framepositioner);
398
399
400
}

void
401
gst_frame_positioner_get_property (GObject * object, guint property_id,
402
403
    GValue * value, GParamSpec * pspec)
{
404
  GstFramePositioner *pos = GST_FRAME_POSITIONNER (object);
405
  gint real_width, real_height;
406

407
  GST_DEBUG_OBJECT (pos, "get_property");
408
409
410

  switch (property_id) {
    case PROP_ALPHA:
411
      g_value_set_double (value, pos->alpha);
412
413
      break;
    case PROP_POSX:
414
      g_value_set_int (value, pos->posx);
415
416
      break;
    case PROP_POSY:
417
      g_value_set_int (value, pos->posy);
418
419
      break;
    case PROP_ZORDER:
420
421
422
423
424
425
426
427
428
      g_value_set_uint (value, pos->zorder);
      break;
    case PROP_WIDTH:
      real_width = (pos->width > 0) ? pos->width : pos->track_width;
      g_value_set_int (value, real_width);
      break;
    case PROP_HEIGHT:
      real_height = (pos->height > 0) ? pos->height : pos->track_height;
      g_value_set_int (value, real_height);
429
430
431
432
433
434
435
436
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}

GType
437
gst_frame_positioner_meta_api_get_type (void)
438
439
{
  static volatile GType type;
440
  static const gchar *tags[] = { "video", NULL };
441
442

  if (g_once_init_enter (&type)) {
443
    GType _type = gst_meta_api_type_register ("GstFramePositionerApi", tags);
444
445
446
447
448
449
    g_once_init_leave (&type, _type);
  }
  return type;
}

static const GstMetaInfo *
450
gst_frame_positioner_get_info (void)
451
452
453
{
  static const GstMetaInfo *meta_info = NULL;

454
  if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
455
    const GstMetaInfo *meta =
456
457
458
        gst_meta_register (gst_frame_positioner_meta_api_get_type (),
        "GstFramePositionerMeta",
        sizeof (GstFramePositionerMeta), gst_frame_positioner_meta_init,
459
        NULL,
460
        gst_frame_positioner_meta_transform);
461
    g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) meta);
462
463
464
465
  }
  return meta_info;
}

466
static gboolean
467
gst_frame_positioner_meta_init (GstMeta * meta, gpointer params,
468
469
    GstBuffer * buffer)
{
470
  GstFramePositionerMeta *smeta;
471

472
  smeta = (GstFramePositionerMeta *) meta;
473
474
475
476
477
478
479
480

  smeta->alpha = 0.0;
  smeta->posx = smeta->posy = smeta->height = smeta->width = 0;
  smeta->zorder = 0;

  return TRUE;
}

481
static gboolean
482
gst_frame_positioner_meta_transform (GstBuffer * dest, GstMeta * meta,
483
484
    GstBuffer * buffer, GQuark type, gpointer data)
{
485
  GstFramePositionerMeta *dmeta, *smeta;
486

487
  smeta = (GstFramePositionerMeta *) meta;
488
489
490
491

  if (GST_META_TRANSFORM_IS_COPY (type)) {
    /* only copy if the complete data is copied as well */
    dmeta =
492
493
        (GstFramePositionerMeta *) gst_buffer_add_meta (dest,
        gst_frame_positioner_get_info (), NULL);
494
495
496
    dmeta->alpha = smeta->alpha;
    dmeta->posx = smeta->posx;
    dmeta->posy = smeta->posy;
497
498
    dmeta->width = smeta->width;
    dmeta->height = smeta->height;
499
500
501
502
503
504
505
    dmeta->zorder = smeta->zorder;
  }

  return TRUE;
}

static GstFlowReturn
506
gst_frame_positioner_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
507
{
508
509
  GstFramePositionerMeta *meta;
  GstFramePositioner *framepositioner = GST_FRAME_POSITIONNER (trans);
510
  GstClockTime timestamp = GST_BUFFER_PTS (buf);
511
512
513
514
515
516

  if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
    gst_object_sync_values (GST_OBJECT (trans), timestamp);
  }

  meta =
517
518
519
520
521
522
523
524
525
526
527
      (GstFramePositionerMeta *) gst_buffer_add_meta (buf,
      gst_frame_positioner_get_info (), NULL);

  GST_OBJECT_LOCK (framepositioner);
  meta->alpha = framepositioner->alpha;
  meta->posx = framepositioner->posx;
  meta->posy = framepositioner->posy;
  meta->width = framepositioner->width;
  meta->height = framepositioner->height;
  meta->zorder = framepositioner->zorder;
  GST_OBJECT_UNLOCK (framepositioner);
528
529
530

  return GST_FLOW_OK;
}