videomixer.c 39.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* Generic video mixer plugin
 * Copyright (C) 2004 Wim Taymans <wim@fluendo.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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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
/**
 * SECTION:element-videomixer
 * @short_description: Takes several AYUV video streams as input and mixes them
 * together.
 *
 * <refsect2>
 * <para>
 * Videomixer can only accept AYUV video streams. For each of the requested
 * sink pads it will compare the incoming geometry and framerate to define the
 * output parameters. Indeed output video frames will have the geometry of the
 * biggest incoming video stream and the framerate of the fastest incoming one.
 * </para>
 * <title>Sample pipelines</title>
 * <para>
 * Here is a pipeline to demonstrate videomixer used together with videobox :
 * <programlisting>
 * gst-launch videotestsrc pattern=1 ! video/x-raw-yuv, framerate=\(fraction\)10/1, width=100, height=100 ! videobox border-alpha=0 alpha=0.5 top=-70 bottom=-70 right=-220 ! videomixer name=mix ! ffmpegcolorspace ! xvimagesink videotestsrc ! video/x-raw-yuv, framerate=\(fraction\)5/1, width=320, height=240 ! alpha alpha=0.7 ! mix.
 * </programlisting>
 * This should show a 320x240 pixels video test source with some transparency
 * showing the background checker pattern. Another video test source with just
 * the snow pattern of 100x100 pixels is overlayed on top of the first one on
 * the left vertically centered with a small transparency showing the first
 * video test source behind and the checker pattern under it. Note that the
 * framerate of the output video is 10 frames per second.
 * </para>
 * </refsect2>
 */

48
49
50
51
52
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>
53
#include <gst/base/gstcollectpads.h>
54
#include <gst/controller/gstcontroller.h>
55

56
#include <stdlib.h>
57
58
#include <string.h>

59
60
#include "videomixer.h"

61
62
63
GST_DEBUG_CATEGORY_STATIC (gst_videomixer_debug);
#define GST_CAT_DEFAULT gst_videomixer_debug

64
65
66
67
68
69
70
#define GST_VIDEO_MIXER_GET_STATE_LOCK(mix) \
  (GST_VIDEO_MIXER(mix)->state_lock)
#define GST_VIDEO_MIXER_STATE_LOCK(mix) \
  (g_mutex_lock(GST_VIDEO_MIXER_GET_STATE_LOCK (mix)))
#define GST_VIDEO_MIXER_STATE_UNLOCK(mix) \
  (g_mutex_unlock(GST_VIDEO_MIXER_GET_STATE_LOCK (mix)))

71
72
static GType gst_videomixer_get_type (void);

73

74
75
76
77
78
79
80
static void gst_videomixer_pad_class_init (GstVideoMixerPadClass * klass);
static void gst_videomixer_pad_init (GstVideoMixerPad * mixerpad);
static void gst_videomixer_pad_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
static void gst_videomixer_pad_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);

81
82
static void gst_videomixer_sort_pads (GstVideoMixer * mix);

83
84
85
86
87
88
89
90
91
92
#define DEFAULT_PAD_ZORDER 0
#define DEFAULT_PAD_XPOS   0
#define DEFAULT_PAD_YPOS   0
#define DEFAULT_PAD_ALPHA  1.0
enum
{
  ARG_PAD_0,
  ARG_PAD_ZORDER,
  ARG_PAD_XPOS,
  ARG_PAD_YPOS,
93
  ARG_PAD_ALPHA
94
95
};

96
static GType
97
98
99
100
101
102
103
gst_videomixer_pad_get_type (void)
{
  static GType videomixer_pad_type = 0;

  if (!videomixer_pad_type) {
    static const GTypeInfo videomixer_pad_info = {
      sizeof (GstVideoMixerPadClass),
104
      NULL,
105
106
107
108
109
110
111
112
113
      NULL,
      (GClassInitFunc) gst_videomixer_pad_class_init,
      NULL,
      NULL,
      sizeof (GstVideoMixerPad),
      0,
      (GInstanceInitFunc) gst_videomixer_pad_init,
    };

114
    videomixer_pad_type = g_type_register_static (GST_TYPE_PAD,
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        "GstVideoMixerPad", &videomixer_pad_info, 0);
  }
  return videomixer_pad_type;
}

static void
gst_videomixer_pad_class_init (GstVideoMixerPadClass * klass)
{
  GObjectClass *gobject_class;

  gobject_class = (GObjectClass *) klass;

  gobject_class->set_property =
      GST_DEBUG_FUNCPTR (gst_videomixer_pad_set_property);
  gobject_class->get_property =
      GST_DEBUG_FUNCPTR (gst_videomixer_pad_get_property);

132
  g_object_class_install_property (gobject_class, ARG_PAD_ZORDER,
133
      g_param_spec_uint ("zorder", "Z-Order", "Z Order of the picture",
134
135
          0, 10000, DEFAULT_PAD_ZORDER,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
136
  g_object_class_install_property (gobject_class, ARG_PAD_XPOS,
137
      g_param_spec_int ("xpos", "X Position", "X Position of the picture",
138
139
          G_MININT, G_MAXINT, DEFAULT_PAD_XPOS,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
140
  g_object_class_install_property (gobject_class, ARG_PAD_YPOS,
141
      g_param_spec_int ("ypos", "Y Position", "Y Position of the picture",
142
143
          G_MININT, G_MAXINT, DEFAULT_PAD_YPOS,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
144
  g_object_class_install_property (gobject_class, ARG_PAD_ALPHA,
145
146
      g_param_spec_double ("alpha", "Alpha", "Alpha of the picture", 0.0, 1.0,
          DEFAULT_PAD_ALPHA, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
147
148
149
150
151
152
}

static void
gst_videomixer_pad_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
153
  GstVideoMixerPad *pad = GST_VIDEO_MIXER_PAD (object);
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

  switch (prop_id) {
    case ARG_PAD_ZORDER:
      g_value_set_uint (value, pad->zorder);
      break;
    case ARG_PAD_XPOS:
      g_value_set_int (value, pad->xpos);
      break;
    case ARG_PAD_YPOS:
      g_value_set_int (value, pad->ypos);
      break;
    case ARG_PAD_ALPHA:
      g_value_set_double (value, pad->alpha);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_videomixer_pad_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstVideoMixerPad *pad;
179
  GstVideoMixer *mix;
180
181

  pad = GST_VIDEO_MIXER_PAD (object);
182
  mix = GST_VIDEO_MIXER (gst_pad_get_parent (GST_PAD (pad)));
183
184
185

  switch (prop_id) {
    case ARG_PAD_ZORDER:
186
      GST_VIDEO_MIXER_STATE_LOCK (mix);
187
      pad->zorder = g_value_get_uint (value);
188
      gst_videomixer_sort_pads (mix);
189
      GST_VIDEO_MIXER_STATE_UNLOCK (mix);
190
191
192
193
194
195
196
197
198
199
200
201
202
203
      break;
    case ARG_PAD_XPOS:
      pad->xpos = g_value_get_int (value);
      break;
    case ARG_PAD_YPOS:
      pad->ypos = g_value_get_int (value);
      break;
    case ARG_PAD_ALPHA:
      pad->alpha = g_value_get_double (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
204
205

  gst_object_unref (mix);
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
static void
gst_videomixer_set_master_geometry (GstVideoMixer * mix)
{
  GSList *walk;
  gint width = 0, height = 0, fps_n = 0, fps_d = 0;
  GstVideoMixerPad *master = NULL;

  walk = mix->sinkpads;
  while (walk) {
    GstVideoMixerPad *mixpad = GST_VIDEO_MIXER_PAD (walk->data);

    walk = g_slist_next (walk);

    /* Biggest input geometry will be our output geometry */
    width = MAX (width, mixpad->in_width);
    height = MAX (height, mixpad->in_height);

    /* If mix framerate < mixpad framerate, using fractions */
    GST_DEBUG_OBJECT (mix, "comparing framerate %d/%d to mixpad's %d/%d",
        fps_n, fps_d, mixpad->fps_n, mixpad->fps_d);
    if ((!fps_n && !fps_d) ||
        ((gint64) fps_n * mixpad->fps_d < (gint64) mixpad->fps_n * fps_d)) {
      fps_n = mixpad->fps_n;
      fps_d = mixpad->fps_d;
      GST_DEBUG_OBJECT (mixpad, "becomes the master pad");
      master = mixpad;
    }
  }

  /* set results */
  if (mix->master != master || mix->in_width != width
      || mix->in_height != height || mix->fps_n != fps_n
      || mix->fps_d != fps_d) {
    mix->setcaps = TRUE;
242
    mix->sendseg = TRUE;
243
244
245
246
247
248
249
250
    mix->master = master;
    mix->in_width = width;
    mix->in_height = height;
    mix->fps_n = fps_n;
    mix->fps_d = fps_d;
  }
}

251
252
static gboolean
gst_videomixer_pad_sink_setcaps (GstPad * pad, GstCaps * vscaps)
253
254
255
256
{
  GstVideoMixer *mix;
  GstVideoMixerPad *mixpad;
  GstStructure *structure;
257
  gint in_width, in_height;
258
  gboolean ret = FALSE;
259
  const GValue *framerate;
260
261
262
263

  mix = GST_VIDEO_MIXER (gst_pad_get_parent (pad));
  mixpad = GST_VIDEO_MIXER_PAD (pad);

264
  if (!mixpad)
265
266
267
    goto beach;

  GST_DEBUG_OBJECT (mixpad, "setcaps triggered");
268
269
270

  structure = gst_caps_get_structure (vscaps, 0);

271
272
  if (!gst_structure_get_int (structure, "width", &in_width)
      || !gst_structure_get_int (structure, "height", &in_height)
273
      || (framerate = gst_structure_get_value (structure, "framerate")) == NULL)
274
    goto beach;
275

276
  GST_VIDEO_MIXER_STATE_LOCK (mix);
277
278
  mixpad->fps_n = gst_value_get_fraction_numerator (framerate);
  mixpad->fps_d = gst_value_get_fraction_denominator (framerate);
279

280
281
  mixpad->in_width = in_width;
  mixpad->in_height = in_height;
282

283
  gst_videomixer_set_master_geometry (mix);
284
  GST_VIDEO_MIXER_STATE_UNLOCK (mix);
285

286
287
288
289
290
291
  ret = TRUE;

beach:
  gst_object_unref (mix);

  return ret;
292
293
294
295
296
}

static void
gst_videomixer_pad_link (GstPad * pad, GstPad * peer, gpointer data)
{
297
  GST_DEBUG_OBJECT (pad, "connected");
298
299
300
301
302
}

static void
gst_videomixer_pad_unlink (GstPad * pad, GstPad * peer, gpointer data)
{
303
  GST_DEBUG_OBJECT (pad, "unlinked");
304
305
306
307
308
309
}

static void
gst_videomixer_pad_init (GstVideoMixerPad * mixerpad)
{
  g_signal_connect (mixerpad, "linked",
310
      G_CALLBACK (gst_videomixer_pad_link), mixerpad);
311
  g_signal_connect (mixerpad, "unlinked",
312
      G_CALLBACK (gst_videomixer_pad_unlink), mixerpad);
313
314

  /* setup some pad functions */
315
316
  gst_pad_set_setcaps_function (GST_PAD (mixerpad),
      gst_videomixer_pad_sink_setcaps);
317
318
319
320
321
322
323
324
325

  mixerpad->zorder = DEFAULT_PAD_ZORDER;
  mixerpad->xpos = DEFAULT_PAD_XPOS;
  mixerpad->ypos = DEFAULT_PAD_YPOS;
  mixerpad->alpha = DEFAULT_PAD_ALPHA;
}


/* elementfactory information */
326
static const GstElementDetails gst_videomixer_details =
Wim Taymans's avatar
Wim Taymans committed
327
GST_ELEMENT_DETAILS ("Video mixer",
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    "Filter/Editor/Video",
    "Mix multiple video streams",
    "Wim Taymans <wim@fluendo.com>");

/* VideoMixer signals and args */
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

#define DEFAULT_BACKGROUND VIDEO_MIXER_BACKGROUND_CHECKER
enum
{
  ARG_0,
343
  ARG_BACKGROUND
344
345
346
347
348
349
350
};

#define GST_TYPE_VIDEO_MIXER_BACKGROUND (gst_video_mixer_background_get_type())
static GType
gst_video_mixer_background_get_type (void)
{
  static GType video_mixer_background_type = 0;
351
  static const GEnumValue video_mixer_background[] = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
352
353
354
    {VIDEO_MIXER_BACKGROUND_CHECKER, "Checker pattern", "checker"},
    {VIDEO_MIXER_BACKGROUND_BLACK, "Black", "black"},
    {VIDEO_MIXER_BACKGROUND_WHITE, "White", "white"},
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
    {0, NULL, NULL},
  };

  if (!video_mixer_background_type) {
    video_mixer_background_type =
        g_enum_register_static ("GstVideoMixerBackground",
        video_mixer_background);
  }
  return video_mixer_background_type;
}

static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw-yuv,"
370
        "format = (fourcc) AYUV,"
371
        "width = (int) [ 1, max ],"
372
        "height = (int) [ 1, max ]," "framerate = (fraction) [ 0/1, MAX ]")
373
374
375
376
377
378
379
    );

static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS ("video/x-raw-yuv,"
        "format = (fourcc) AYUV,"
380
        "width = (int) [ 1, max ],"
381
        "height = (int) [ 1, max ]," "framerate = (fraction) [ 0/1, MAX ]")
382
383
    );

384
static void gst_videomixer_finalize (GObject * object);
385

386
387
static GstCaps *gst_videomixer_getcaps (GstPad * pad);

388
389
static GstFlowReturn gst_videomixer_collected (GstCollectPads * pads,
    GstVideoMixer * mix);
390
391
static GstPad *gst_videomixer_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name);
392
static void gst_videomixer_release_pad (GstElement * element, GstPad * pad);
393
394
395
396
static void gst_videomixer_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_videomixer_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
397
398
static GstStateChangeReturn gst_videomixer_change_state (GstElement * element,
    GstStateChange transition);
399
400
401

/*static guint gst_videomixer_signals[LAST_SIGNAL] = { 0 }; */

402
403
404
405
406
407
408
409
410
static void gst_videomixer_child_proxy_init (gpointer g_iface,
    gpointer iface_data);
static void _do_init (GType object_type);

GST_BOILERPLATE_FULL (GstVideoMixer, gst_videomixer, GstElement,
    GST_TYPE_ELEMENT, _do_init);

static void
_do_init (GType object_type)
411
{
412
413
414
415
416
417
418
419
420
  const GInterfaceInfo child_proxy_info = {
    (GInterfaceInitFunc) gst_videomixer_child_proxy_init,
    NULL,
    NULL
  };
  g_type_add_interface_static (object_type, GST_TYPE_CHILD_PROXY,
      &child_proxy_info);
  GST_INFO ("GstChildProxy interface registered");
}
421

422
423
424
425
426
427
static GstObject *
gst_videomixer_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
    guint index)
{
  GstVideoMixer *mix = GST_VIDEO_MIXER (child_proxy);
  GstObject *obj;
428

429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
  GST_VIDEO_MIXER_STATE_LOCK (mix);
  if ((obj = g_slist_nth_data (mix->sinkpads, index)))
    gst_object_ref (obj);
  GST_VIDEO_MIXER_STATE_UNLOCK (mix);
  return obj;
}

static guint
gst_videomixer_child_proxy_get_children_count (GstChildProxy * child_proxy)
{
  guint count = 0;
  GstVideoMixer *mix = GST_VIDEO_MIXER (child_proxy);

  GST_VIDEO_MIXER_STATE_LOCK (mix);
  count = mix->numpads;
  GST_VIDEO_MIXER_STATE_UNLOCK (mix);
  GST_INFO ("Children Count: %d", count);
  return count;
}

static void
gst_videomixer_child_proxy_init (gpointer g_iface, gpointer iface_data)
{
  GstChildProxyInterface *iface = g_iface;

  GST_INFO ("intializing child proxy interface");
  iface->get_child_by_index = gst_videomixer_child_proxy_get_child_by_index;
  iface->get_children_count = gst_videomixer_child_proxy_get_children_count;
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
}

static void
gst_videomixer_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&src_factory));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_factory));

  gst_element_class_set_details (element_class, &gst_videomixer_details);
}

static void
gst_videomixer_class_init (GstVideoMixerClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

481
  parent_class = g_type_class_peek_parent (klass);
482

483
484
  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_videomixer_finalize);

485
486
487
488
  gobject_class->get_property = gst_videomixer_get_property;
  gobject_class->set_property = gst_videomixer_set_property;

  g_object_class_install_property (gobject_class, ARG_BACKGROUND,
489
490
491
492
      g_param_spec_enum ("background", "Background", "Background type",
          GST_TYPE_VIDEO_MIXER_BACKGROUND,
          DEFAULT_BACKGROUND, G_PARAM_READWRITE));

493
494
495
496
497
498
499
500
501
  gstelement_class->request_new_pad =
      GST_DEBUG_FUNCPTR (gst_videomixer_request_new_pad);
  gstelement_class->release_pad =
      GST_DEBUG_FUNCPTR (gst_videomixer_release_pad);
  gstelement_class->change_state =
      GST_DEBUG_FUNCPTR (gst_videomixer_change_state);
}

static void
502
gst_videomixer_collect_free (GstVideoMixerCollect * mixcol)
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
{
  if (mixcol->buffer) {
    gst_buffer_unref (mixcol->buffer);
    mixcol->buffer = NULL;
  }
}

static void
gst_videomixer_reset (GstVideoMixer * mix)
{
  GSList *walk;

  mix->in_width = 0;
  mix->in_height = 0;
  mix->out_width = 0;
  mix->out_height = 0;
  mix->fps_n = mix->fps_d = 0;
  mix->setcaps = FALSE;
521
  mix->sendseg = FALSE;
522
523
524
525

  /* clean up collect data */
  walk = mix->collect->data;
  while (walk) {
526
    GstVideoMixerCollect *data = (GstVideoMixerCollect *) walk->data;
527
528
529
530

    gst_videomixer_collect_free (data);
    walk = g_slist_next (walk);
  }
531
532
533
}

static void
534
gst_videomixer_init (GstVideoMixer * mix, GstVideoMixerClass * g_class)
535
536
537
538
539
540
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (mix);

  mix->srcpad =
      gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
          "src"), "src");
541
542
  gst_pad_set_getcaps_function (GST_PAD (mix->srcpad),
      GST_DEBUG_FUNCPTR (gst_videomixer_getcaps));
543
544
  gst_element_add_pad (GST_ELEMENT (mix), mix->srcpad);

545
  mix->collect = gst_collect_pads_new ();
546
547
  mix->background = DEFAULT_BACKGROUND;

548
  gst_collect_pads_set_function (mix->collect,
549
550
      (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_videomixer_collected),
      mix);
551

552
  mix->state_lock = g_mutex_new ();
553
554
555
556
557
558
559
560
561
562
  /* initialize variables */
  gst_videomixer_reset (mix);
}

static void
gst_videomixer_finalize (GObject * object)
{
  GstVideoMixer *mix = GST_VIDEO_MIXER (object);

  gst_object_unref (mix->collect);
563
  g_mutex_free (mix->state_lock);
564
565

  G_OBJECT_CLASS (parent_class)->finalize (object);
566
567
}

568
569
570
571
572
573
574
575
static GstCaps *
gst_videomixer_getcaps (GstPad * pad)
{
  GstVideoMixer *mix;
  GstCaps *caps;
  GstStructure *structure;

  mix = GST_VIDEO_MIXER (gst_pad_get_parent (pad));
576
  caps = gst_caps_copy (gst_pad_get_pad_template_caps (mix->srcpad));
577
578
579
580
581
582
583
584
585

  structure = gst_caps_get_structure (caps, 0);

  if (mix->out_width != 0) {
    gst_structure_set (structure, "width", G_TYPE_INT, mix->out_width, NULL);
  }
  if (mix->out_height != 0) {
    gst_structure_set (structure, "height", G_TYPE_INT, mix->out_height, NULL);
  }
586
  if (mix->fps_d != 0) {
587
    gst_structure_set (structure,
588
        "framerate", GST_TYPE_FRACTION, mix->fps_n, mix->fps_d, NULL);
589
590
  }

591
592
  gst_object_unref (mix);

593
594
595
  return caps;
}

596
597
598
599
static GstPad *
gst_videomixer_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * req_name)
{
600
601
  GstVideoMixer *mix = NULL;
  GstVideoMixerPad *mixpad = NULL;
602
603
604
605
606
607
608
609
610
611
612
613
614
615
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);

  g_return_val_if_fail (templ != NULL, NULL);

  if (templ->direction != GST_PAD_SINK) {
    g_warning ("videomixer: request pad that is not a SINK pad\n");
    return NULL;
  }

  g_return_val_if_fail (GST_IS_VIDEO_MIXER (element), NULL);

  mix = GST_VIDEO_MIXER (element);

  if (templ == gst_element_class_get_pad_template (klass, "sink_%d")) {
616
617
618
    gint serial = 0;
    gchar *name = NULL;
    GstVideoMixerCollect *mixcol = NULL;
619

620
621
622
623
624
625
626
    if (req_name == NULL || strlen (req_name) < 6) {
      /* no name given when requesting the pad, use random serial number */
      serial = rand ();
    } else {
      /* parse serial number from requested padname */
      serial = atoi (&req_name[5]);
    }
627
    /* create new pad with the name */
628
629
630
    name = g_strdup_printf ("sink_%d", serial);
    mixpad = g_object_new (GST_TYPE_VIDEO_MIXER_PAD, "name", name, "direction",
        templ->direction, "template", templ, NULL);
631
632
    g_free (name);

633
    GST_VIDEO_MIXER_STATE_LOCK (mix);
634
    mixpad->zorder = mix->numpads;
635
636
637
638
639
    mixpad->xpos = DEFAULT_PAD_XPOS;
    mixpad->ypos = DEFAULT_PAD_YPOS;
    mixpad->alpha = DEFAULT_PAD_ALPHA;

    mixcol = (GstVideoMixerCollect *)
640
        gst_collect_pads_add_pad (mix->collect, GST_PAD (mixpad),
641
642
        sizeof (GstVideoMixerCollect));

643
    /* Keep track of each other */
644
645
646
647
648
    mixcol->mixpad = mixpad;
    mixpad->mixcol = mixcol;

    /* Keep an internal list of mixpads for zordering */
    mix->sinkpads = g_slist_append (mix->sinkpads, mixpad);
649
    mix->numpads++;
650
    GST_VIDEO_MIXER_STATE_UNLOCK (mix);
651
652
653
654
655
  } else {
    g_warning ("videomixer: this is not our template!\n");
    return NULL;
  }

656
  /* add the pad to the element */
657
  gst_element_add_pad (element, GST_PAD (mixpad));
658
  gst_child_proxy_child_added (GST_OBJECT (mix), GST_OBJECT (mixpad));
659

660
  return GST_PAD (mixpad);
661
662
}

663
664
665
666
static void
gst_videomixer_release_pad (GstElement * element, GstPad * pad)
{
  GstVideoMixer *mix = NULL;
667
  GstVideoMixerPad *mixpad;
668
669

  mix = GST_VIDEO_MIXER (element);
670
671
672
673
674
  GST_VIDEO_MIXER_STATE_LOCK (mix);
  if (G_UNLIKELY (g_slist_find (mix->sinkpads, pad) == NULL)) {
    g_warning ("Unknown pad %s", GST_PAD_NAME (pad));
    goto error;
  }
675

676
  mixpad = GST_VIDEO_MIXER_PAD (pad);
677

678
679
680
  mix->sinkpads = g_slist_remove (mix->sinkpads, pad);
  gst_videomixer_collect_free (mixpad->mixcol);
  gst_collect_pads_remove_pad (mix->collect, pad);
681
  gst_child_proxy_child_removed (GST_OBJECT (mix), GST_OBJECT (mixpad));
682
683
684
685
  /* determine possibly new geometry and master */
  gst_videomixer_set_master_geometry (mix);
  mix->numpads--;
  GST_VIDEO_MIXER_STATE_UNLOCK (mix);
686

687
688
689
690
  gst_element_remove_pad (element, pad);
  return;
error:
  GST_VIDEO_MIXER_STATE_UNLOCK (mix);
691
692
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
#define BLEND_NORMAL(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V)     \
        Y = ((Y1*(255-alpha))+(Y2*alpha))>>8;           \
        U = ((U1*(255-alpha))+(U2*alpha))>>8;           \
        V = ((V1*(255-alpha))+(V2*alpha))>>8;

#define BLEND_ADD(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V)                \
        Y = Y1+((Y2*alpha)>>8);                                 \
        U = U1+(((127*(255-alpha)+(U2*alpha)))>>8)-127;         \
        V = V1+(((127*(255-alpha)+(V2*alpha)))>>8)-127;         \
        if (Y>255) {                                            \
          gint mult = MAX (0, 288-Y);                           \
          U = ((U*mult) + (127*(32-mult)))>>5;                  \
          V = ((V*mult) + (127*(32-mult)))>>5;                  \
          Y = 255;                                              \
        }                                                       \
        U = MIN (U,255);                                        \
        V = MIN (V,255);

#define BLEND_SUBTRACT(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V)           \
        Y = Y1-((Y2*alpha)>>8);                                 \
        U = U1+(((127*(255-alpha)+(U2*alpha)))>>8)-127;         \
        V = V1+(((127*(255-alpha)+(V2*alpha)))>>8)-127;         \
        if (Y<0) {                                              \
          gint mult = MIN (32, -Y);                             \
          U = ((U*(32-mult)) + (127*mult))>>5;                  \
          V = ((V*(32-mult)) + (127*mult))>>5;                  \
          Y = 0;                                                \
        }

#define BLEND_DARKEN(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V)     \
        if (Y1 < Y2) {                                  \
          Y = Y1; U = U1; V = V1;                       \
        }                                               \
        else {                                          \
          Y = ((Y1*(255-alpha))+(Y2*alpha))>>8;         \
          U = ((U1*(255-alpha))+(U2*alpha))>>8;         \
          V = ((V1*(255-alpha))+(V2*alpha))>>8;         \
        }

#define BLEND_LIGHTEN(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V)    \
        if (Y1 > Y2) {                                  \
          Y = Y1; U = U1; V = V1;                       \
        }                                               \
        else {                                          \
          Y = ((Y1*(255-alpha))+(Y2*alpha))>>8;         \
          U = ((U1*(255-alpha))+(U2*alpha))>>8;         \
          V = ((V1*(255-alpha))+(V2*alpha))>>8;         \
        }

#define BLEND_MULTIPLY(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V)                   \
        Y = (Y1*(256*(255-alpha) +(Y2*alpha)))>>16;                     \
        U = ((U1*(255-alpha)*256)+(alpha*(U1*Y2+128*(256-Y2))))>>16;    \
        V = ((V1*(255-alpha)*256)+(alpha*(V1*Y2+128*(256-Y2))))>>16;

#define BLEND_DIFFERENCE(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V)         \
        Y = ABS((gint)Y1-(gint)Y2)+127;                         \
        U = ABS((gint)U1-(gint)U2)+127;                         \
        V = ABS((gint)V1-(gint)V2)+127;                         \
        Y = ((Y*alpha)+(Y1*(255-alpha)))>>8;                    \
        U = ((U*alpha)+(U1*(255-alpha)))>>8;                    \
        V = ((V*alpha)+(V1*(255-alpha)))>>8;                    \
        if (Y>255) {                                            \
          gint mult = MAX (0, 288-Y);                           \
          U = ((U*mult) + (127*(32-mult)))>>5;                  \
          V = ((V*mult) + (127*(32-mult)))>>5;                  \
          Y = 255;                                              \
        } else if (Y<0) {                                       \
          gint mult = MIN (32, -Y);                             \
          U = ((U*(32-mult)) + (127*mult))>>5;                  \
          V = ((V*(32-mult)) + (127*mult))>>5;                  \
          Y = 0;                                                \
        }                                                       \
        U = CLAMP(U, 0, 255);                                   \
766
767
        V = CLAMP(V, 0, 255);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
#define BLEND_EXCLUSION(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V)          \
        Y = ((gint)(Y1^0xff)*Y2+(gint)(Y2^0xff)*Y1)>>8;         \
        U = ((gint)(U1^0xff)*Y2+(gint)(Y2^0xff)*U1)>>8;         \
        V = ((gint)(V1^0xff)*Y2+(gint)(Y2^0xff)*V1)>>8;         \
        Y = ((Y*alpha)+(Y1*(255-alpha)))>>8;                    \
        U = ((U*alpha)+(U1*(255-alpha)))>>8;                    \
        V = ((V*alpha)+(V1*(255-alpha)))>>8;                    \
        if (Y>255) {                                            \
          gint mult = MAX (0, 288-Y);                           \
          U = ((U*mult) + (127*(32-mult)))>>5;                  \
          V = ((V*mult) + (127*(32-mult)))>>5;                  \
          Y = 255;                                              \
        } else if (Y<0) {                                       \
          gint mult = MIN (32, -Y);                             \
          U = ((U*(32-mult)) + (127*mult))>>5;                  \
          V = ((V*(32-mult)) + (127*mult))>>5;                  \
          Y = 0;                                                \
        }                                                       \
        U = CLAMP(U, 0, 255);                                   \
787
788
        V = CLAMP(V, 0, 255);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
#define BLEND_SOFTLIGHT(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V)          \
        Y = (gint)Y1+(gint)Y2 - 127;                            \
        U = (gint)U1+(gint)U2 - 127;                            \
        V = (gint)V1+(gint)V2 - 127;                            \
        Y = ((Y*alpha)+(Y1*(255-alpha)))>>8;                    \
        U = ((U*alpha)+(U1*(255-alpha)))>>8;                    \
        V = ((V*alpha)+(V1*(255-alpha)))>>8;                    \
        if (Y>255) {                                            \
          gint mult = MAX (0, 288-Y);                           \
          U = ((U*mult) + (127*(32-mult)))>>5;                  \
          V = ((V*mult) + (127*(32-mult)))>>5;                  \
          Y = 255;                                              \
        } else if (Y<0) {                                       \
          gint mult = MIN (32, -Y);                             \
          U = ((U*(32-mult)) + (127*mult))>>5;                  \
          V = ((V*(32-mult)) + (127*mult))>>5;                  \
          Y = 0;                                                \
        }                                                       \

#define BLEND_HARDLIGHT(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V)          \
        Y = (gint)Y1+(gint)Y2*2 - 255;                          \
        U = (gint)U1+(gint)U2 - 127;                            \
        V = (gint)V1+(gint)V2 - 127;                            \
        Y = ((Y*alpha)+(Y1*(255-alpha)))>>8;                    \
        U = ((U*alpha)+(U1*(255-alpha)))>>8;                    \
        V = ((V*alpha)+(V1*(255-alpha)))>>8;                    \
        if (Y>255) {                                            \
          gint mult = MAX (0, 288-Y);                           \
          U = ((U*mult) + (127*(32-mult)))>>5;                  \
          V = ((V*mult) + (127*(32-mult)))>>5;                  \
          Y = 255;                                              \
        } else if (Y<0) {                                       \
          gint mult = MIN (32, -Y);                             \
          U = ((U*(32-mult)) + (127*mult))>>5;                  \
          V = ((V*(32-mult)) + (127*mult))>>5;                  \
          Y = 0;                                                \
        }                                                       \
826
827
828

#define BLEND_MODE BLEND_NORMAL
#if 0
829
#define BLEND_MODE BLEND_NORMAL
830
831
832
#define BLEND_MODE BLEND_ADD
#define BLEND_MODE BLEND_SUBTRACT
#define BLEND_MODE BLEND_LIGHTEN
833
#define BLEND_MODE BLEND_DARKEN
834
835
836
837
838
839
840
841
842
843
#define BLEND_MODE BLEND_MULTIPLY
#define BLEND_MODE BLEND_DIFFERENCE
#define BLEND_MODE BLEND_EXCLUSION
#define BLEND_MODE BLEND_SOFTLIGHT
#define BLEND_MODE BLEND_HARDLIGHT
#endif

/* note that this function does packing conversion and blending at the
 * same time */
static void
844
gst_videomixer_blend_ayuv_ayuv (guint8 * src, gint xpos, gint ypos,
845
846
847
848
849
    gint src_width, gint src_height, gdouble src_alpha,
    guint8 * dest, gint dest_width, gint dest_height)
{
  gint alpha, b_alpha;
  gint i, j;
850
851
  gint src_stride, dest_stride;
  gint src_add, dest_add;
852
853
  gint Y, U, V;

854
855
  src_stride = src_width * 4;
  dest_stride = dest_width * 4;
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877

  b_alpha = (gint) (src_alpha * 255);

  /* adjust src pointers for negative sizes */
  if (xpos < 0) {
    src += -xpos * 4;
    src_width -= -xpos;
    xpos = 0;
  }
  if (ypos < 0) {
    src += -ypos * src_stride;
    src_height -= -ypos;
    ypos = 0;
  }
  /* adjust width/height if the src is bigger than dest */
  if (xpos + src_width > dest_width) {
    src_width = dest_width - xpos;
  }
  if (ypos + src_height > dest_height) {
    src_height = dest_height - ypos;
  }

878
879
  src_add = src_stride - (4 * src_width);
  dest_add = dest_stride - (4 * src_width);
880

881
  dest = dest + 4 * xpos + (ypos * dest_stride);
882
883

  /* we convert a square of 2x2 samples to generate 4 Luma and 2 chroma samples */
884
885
  for (i = 0; i < src_height; i++) {
    for (j = 0; j < src_width; j++) {
886
887
      alpha = (src[0] * b_alpha) >> 8;
      BLEND_MODE (dest[1], dest[2], dest[3], src[1], src[2], src[3],
888
          alpha, Y, U, V);
889
890
891
892
893
894
895
      dest[0] = 0xff;
      dest[1] = Y;
      dest[2] = U;
      dest[3] = V;

      src += 4;
      dest += 4;
896
    }
897
898
    src += src_add;
    dest += dest_add;
899
900
901
902
903
  }
}

#undef BLEND_MODE

904
905
906
907
908
909
910
911
912
913
914
915
916
917
static int
pad_zorder_compare (const GstVideoMixerPad * pad1,
    const GstVideoMixerPad * pad2)
{
  return pad1->zorder - pad2->zorder;
}

static void
gst_videomixer_sort_pads (GstVideoMixer * mix)
{
  mix->sinkpads = g_slist_sort (mix->sinkpads,
      (GCompareFunc) pad_zorder_compare);
}

918
919
920
921
922
923
924
925
/* fill a buffer with a checkerboard pattern */
static void
gst_videomixer_fill_checker (guint8 * dest, gint width, gint height)
{
  gint i, j;
  static int tab[] = { 80, 160, 80, 160 };

  for (i = 0; i < height; i++) {
926
    for (j = 0; j < width; j++) {
927
      *dest++ = 0xff;
928
      *dest++ = tab[((i & 0x8) >> 3) + ((j & 0x8) >> 3)];
929
930
      *dest++ = 128;
      *dest++ = 128;
931
932
933
934
935
936
937
938
    }
  }
}

static void
gst_videomixer_fill_color (guint8 * dest, gint width, gint height,
    gint colY, gint colU, gint colV)
{
939
  gint i, j;
940

941
  for (i = 0; i < height; i++) {
942
    for (j = 0; j < width; j++) {
943
944
945
946
947
948
      *dest++ = 0xff;
      *dest++ = colY;
      *dest++ = colU;
      *dest++ = colV;
    }
  }
949
950
951
952
953
954
955
}

/* try to get a buffer on all pads. As long as the queued value is
 * negative, we skip buffers */
static gboolean
gst_videomixer_fill_queues (GstVideoMixer * mix)
{
956
  GSList *walk = NULL;
957
958
  gboolean eos = TRUE;

959
960
961
962
  g_return_val_if_fail (GST_IS_VIDEO_MIXER (mix), FALSE);

  /* try to make sure we have a buffer from each usable pad first */
  walk = mix->collect->data;
963
  while (walk) {
964
965
966
    GstCollectData *data = (GstCollectData *) walk->data;
    GstVideoMixerCollect *mixcol = (GstVideoMixerCollect *) data;
    GstVideoMixerPad *mixpad = mixcol->mixpad;
967
968
969

    walk = g_slist_next (walk);

970
971
    if (mixcol->buffer == NULL) {
      GstBuffer *buf = NULL;
972

973
      GST_LOG ("we need a new buffer");
974

975
      buf = gst_collect_pads_pop (mix->collect, data);
976
977
978
979
980
981
982
983
984
985

      if (buf) {
        guint64 duration;

        GST_LOG ("we have a buffer !");

        mixcol->buffer = buf;
        duration = GST_BUFFER_DURATION (mixcol->buffer);
        /* no duration on the buffer, use the framerate */
        if (!GST_CLOCK_TIME_IS_VALID (duration)) {
986
          if (mixpad->fps_n == 0) {
987
            duration = GST_CLOCK_TIME_NONE;
988
          } else {
989
            duration = GST_SECOND * mixpad->fps_d / mixpad->fps_n;
990
991
          }
        }
992
993
994
995
996
997
        if (GST_CLOCK_TIME_IS_VALID (duration))
          mixpad->queued += duration;
        else if (!mixpad->queued)
          mixpad->queued = GST_CLOCK_TIME_NONE;
      } else {
        GST_LOG ("pop returned a NULL buffer");
998
999
      }
    }
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
    if (mix->sendseg && (mixpad == mix->master)) {
      GstEvent *event;
      GstSegment *segment = &data->segment;

      GST_INFO ("_sending play segment");
      event = gst_event_new_new_segment (FALSE, segment->rate, segment->format,
          segment->start, segment->stop, segment->time);
      gst_pad_push_event (mix->srcpad, event);
      mix->sendseg = FALSE;
    }

1011
    if (mixcol->buffer != NULL && GST_CLOCK_TIME_IS_VALID (mixpad->queued)) {
1012
      /* got a buffer somewhere so we're not eos */
1013
1014
1015
      eos = FALSE;
    }
  }
1016

1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
  return eos;
}

/* blend all buffers present on the pads */
static void
gst_videomixer_blend_buffers (GstVideoMixer * mix, GstBuffer * outbuf)
{
  GSList *walk;

  walk = mix->sinkpads;
1027
  while (walk) {                /* We walk with this list because it's ordered */
1028
    GstVideoMixerPad *pad = GST_VIDEO_MIXER_PAD (walk->data);
1029
    GstVideoMixerCollect *mixcol = pad->mixcol;
1030
1031
1032

    walk = g_slist_next (walk);

1033
    if (mixcol->buffer != NULL) {
1034
1035
1036
      if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (mixcol->buffer)))
        gst_object_sync_values (G_OBJECT (pad),
            GST_BUFFER_TIMESTAMP (mixcol->buffer));
1037
      gst_videomixer_blend_ayuv_ayuv (GST_BUFFER_DATA (mixcol->buffer),
1038
          pad->xpos, pad->ypos, pad->in_width, pad->in_height, pad->alpha,
1039
          GST_BUFFER_DATA (outbuf), mix->out_width, mix->out_height);
1040
      if (pad == mix->master) {
1041
1042
        GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (mixcol->buffer);
        GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (mixcol->buffer);
1043
      }
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
    }
  }
}

/* remove buffers from the queue that were expired in the
 * interval of the master, we also prepare the queued value
 * in the pad so that we can skip and fill buffers later on */
static void
gst_videomixer_update_queues (GstVideoMixer * mix)
{
  GSList *walk;
  guint64 interval;

  interval = mix->master->queued;
  if (interval <= 0) {
1059
    if (mix->fps_n == 0) {
1060
1061
      interval = G_MAXINT64;
    } else {
1062
      interval = GST_SECOND * mix->fps_d / mix->fps_n;
1063
    }
1064
1065
1066
1067
1068
  }

  walk = mix->sinkpads;
  while (walk) {
    GstVideoMixerPad *pad = GST_VIDEO_MIXER_PAD (walk->data);
1069
    GstVideoMixerCollect *mixcol = pad->mixcol;
1070
1071
1072

    walk = g_slist_next (walk);

1073
    if (mixcol->buffer != NULL && GST_CLOCK_TIME_IS_VALID (pad->queued)) {
1074
      pad->queued -= interval;
1075
      GST_DEBUG_OBJECT (pad, "queued now %lld", pad->queued);
1076
      if (pad->queued <= 0) {
1077
1078
1079
        GST_DEBUG ("unreffing buffer");
        gst_buffer_unref (mixcol->buffer);
        mixcol->buffer = NULL;
1080
1081
1082
1083
1084
      }
    }
  }
}

1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
static GstFlowReturn
gst_videomixer_collected (GstCollectPads * pads, GstVideoMixer * mix)
{
  GstFlowReturn ret = GST_FLOW_OK;
  GstBuffer *outbuf = NULL;
  size_t outsize = 0;
  gboolean eos = FALSE;

  g_return_val_if_fail (GST_IS_VIDEO_MIXER (mix), GST_FLOW_ERROR);

  GST_LOG ("all pads are collected");
1096
  GST_VIDEO_MIXER_STATE_LOCK (mix);
1097
1098
1099
1100
1101
1102
1103
1104

  eos = gst_videomixer_fill_queues (mix);

  if (eos) {
    /* Push EOS downstream */
    GST_LOG ("all our sinkpads are EOS, pushing downstream");
    gst_pad_push_event (mix->srcpad, gst_event_new_eos ());
    ret = GST_FLOW_WRONG_STATE;
1105
    goto error;
1106
1107
1108
  }

  /* Calculating out buffer size from input size */
1109
  outsize = 4 * mix->in_width * GST_ROUND_UP_2 (mix->in_height);
1110
1111

  /* If geometry has changed we need to set new caps on the buffer */
1112
1113
  if (mix->in_width != mix->out_width || mix->in_height != mix->out_height
      || mix->setcaps) {
1114
1115
    GstCaps *newcaps = NULL;

1116
1117
    newcaps = gst_caps_make_writable
        (gst_pad_get_negotiated_caps (GST_PAD (mix->master)));
1118
1119
1120
1121
1122
1123
1124
    gst_caps_set_simple (newcaps,
        "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("AYUV"),
        "width", G_TYPE_INT, mix->in_width,
        "height", G_TYPE_INT, mix->in_height, NULL);

    mix->out_width = mix->in_width;
    mix->out_height = mix->in_height;
1125
    mix->setcaps = FALSE;
1126

1127
1128
1129
    ret =
        gst_pad_alloc_buffer_and_set_caps (mix->srcpad, GST_BUFFER_OFFSET_NONE,
        outsize, newcaps, &outbuf);
1130
1131
    gst_caps_unref (newcaps);
  } else {                      /* Otherwise we just allocate a buffer from current caps */
1132
1133
1134
    ret =
        gst_pad_alloc_buffer_and_set_caps (mix->srcpad, GST_BUFFER_OFFSET_NONE,
        outsize, GST_PAD_CAPS (mix->srcpad), &outbuf);
1135
1136
1137
  }

  if (ret != GST_FLOW_OK) {
1138
    goto error;
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
  }

  switch (mix->background) {
    case VIDEO_MIXER_BACKGROUND_CHECKER:
      gst_videomixer_fill_checker (GST_BUFFER_DATA (outbuf),
          mix->out_width, mix->out_height);
      break;
    case VIDEO_MIXER_BACKGROUND_BLACK:
      gst_videomixer_fill_color (GST_BUFFER_DATA (outbuf),
          mix->out_width, mix->out_height, 16, 128, 128);
      break;
    case VIDEO_MIXER_BACKGROUND_WHITE:
      gst_videomixer_fill_color (GST_BUFFER_DATA (outbuf),
          mix->out_width, mix->out_height, 240, 128, 128);
      break;
  }

  gst_videomixer_blend_buffers (mix, outbuf);

  gst_videomixer_update_queues (mix);
1159
  GST_VIDEO_MIXER_STATE_UNLOCK (mix);
1160
1161
1162
1163
1164

  ret = gst_pad_push (mix->srcpad, outbuf);

beach:
  return ret;
1165
1166
1167
1168
1169
1170
1171

  /* ERRORS */
error:
  {
    GST_VIDEO_MIXER_STATE_UNLOCK (mix);
    goto beach;
  }
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
}

static void
gst_videomixer_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec)
{
  GstVideoMixer *mix = GST_VIDEO_MIXER (object);

  switch (prop_id) {
    case ARG_BACKGROUND:
      g_value_set_enum (value, mix->background);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_videomixer_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec)
{
  GstVideoMixer *mix = GST_VIDEO_MIXER (object);

  switch (prop_id) {
    case ARG_BACKGROUND:
      mix->background = g_value_get_enum (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

1206
1207
static GstStateChangeReturn
gst_videomixer_change_state (GstElement * element, GstStateChange transition)
1208
1209
{
  GstVideoMixer *mix;
1210
  GstStateChangeReturn ret;
1211

1212
  g_return_val_if_fail (GST_IS_VIDEO_MIXER (element), GST_STATE_CHANGE_FAILURE);
1213
1214
1215
1216

  mix = GST_VIDEO_MIXER (element);

  switch (transition) {
1217
    case GST_STATE_CHANGE_READY_TO_PAUSED:
1218
      GST_LOG ("starting collectpads");
1219
      gst_collect_pads_start (mix->collect);
1220
      break;
1221
    case GST_STATE_CHANGE_PAUSED_TO_READY:
1222
      GST_LOG ("stopping collectpads");
1223
      gst_collect_pads_stop (mix->collect);
1224
1225
      break;
    default:
1226
1227
1228
      break;
  }

1229
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1230

1231
1232
1233
1234
1235
1236
1237
1238
  switch (transition) {
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      gst_videomixer_reset (mix);
      break;
    default:
      break;
  }

1239
  return ret;
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
}

static gboolean
plugin_init (GstPlugin * plugin)
{
  GST_DEBUG_CATEGORY_INIT (gst_videomixer_debug, "videomixer", 0,
      "video mixer");

  return gst_element_register (plugin, "videomixer", GST_RANK_PRIMARY,
      GST_TYPE_VIDEO_MIXER);
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "videomixer",
1255
1256
    "Video mixer", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
    GST_PACKAGE_ORIGIN)