1 /* Generic video mixer plugin
2 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #include <gst/base/gstcollectpads.h>
29 GST_DEBUG_CATEGORY_STATIC (gst_videomixer_debug);
30 #define GST_CAT_DEFAULT gst_videomixer_debug
32 #define GST_TYPE_VIDEO_MIXER_PAD (gst_videomixer_pad_get_type())
33 #define GST_VIDEO_MIXER_PAD(obj) \
34 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_MIXER_PAD, GstVideoMixerPad))
35 #define GST_VIDEO_MIXER_PAD_CLASS(klass) \
36 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_MIXER_PAD, GstVideoMixerPadiClass))
37 #define GST_IS_VIDEO_MIXER_PAD(obj) \
38 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_MIXER_PAD))
39 #define GST_IS_VIDEO_MIXER_PAD_CLASS(obj) \
40 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_MIXER_PAD))
42 typedef struct _GstVideoMixerPad GstVideoMixerPad;
43 typedef struct _GstVideoMixerPadClass GstVideoMixerPadClass;
44 typedef struct _GstVideoMixerCollect GstVideoMixerCollect;
46 #define GST_TYPE_VIDEO_MIXER (gst_videomixer_get_type())
47 #define GST_VIDEO_MIXER(obj) \
48 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_MIXER, GstVideoMixer))
49 #define GST_VIDEO_MIXER_CLASS(klass) \
50 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_MIXER, GstVideoMixerClass))
51 #define GST_IS_VIDEO_MIXER(obj) \
52 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_MIXER))
53 #define GST_IS_VIDEO_MIXER_CLASS(obj) \
54 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_MIXER))
56 static GType gst_videomixer_get_type (void);
58 typedef struct _GstVideoMixer GstVideoMixer;
59 typedef struct _GstVideoMixerClass GstVideoMixerClass;
61 static void gst_videomixer_pad_base_init (gpointer g_class);
62 static void gst_videomixer_pad_class_init (GstVideoMixerPadClass * klass);
63 static void gst_videomixer_pad_init (GstVideoMixerPad * mixerpad);
65 static void gst_videomixer_pad_get_property (GObject * object, guint prop_id,
66 GValue * value, GParamSpec * pspec);
67 static void gst_videomixer_pad_set_property (GObject * object, guint prop_id,
68 const GValue * value, GParamSpec * pspec);
70 static void gst_videomixer_sort_pads (GstVideoMixer * mix);
72 #define DEFAULT_PAD_ZORDER 0
73 #define DEFAULT_PAD_XPOS 0
74 #define DEFAULT_PAD_YPOS 0
75 #define DEFAULT_PAD_ALPHA 1.0
85 struct _GstVideoMixerCollect
87 GstCollectData collect; /* we extend the CollectData */
89 GstBuffer *buffer; /* the queued buffer for this pad */
91 GstVideoMixerPad *mixpad;
94 /* all information needed for one video stream */
95 struct _GstVideoMixerPad
97 GstPad parent; /* subclass the pad */
101 guint in_width, in_height;
102 gint in_framerate_numerator;
103 gint in_framerate_denominator;
110 GstVideoMixerCollect *mixcol;
113 struct _GstVideoMixerPadClass
115 GstPadClass parent_class;
119 gst_videomixer_pad_get_type (void)
121 static GType videomixer_pad_type = 0;
123 if (!videomixer_pad_type) {
124 static const GTypeInfo videomixer_pad_info = {
125 sizeof (GstVideoMixerPadClass),
126 gst_videomixer_pad_base_init,
128 (GClassInitFunc) gst_videomixer_pad_class_init,
131 sizeof (GstVideoMixerPad),
133 (GInstanceInitFunc) gst_videomixer_pad_init,
136 videomixer_pad_type = g_type_register_static (GST_TYPE_PAD,
137 "GstVideoMixerPad", &videomixer_pad_info, 0);
139 return videomixer_pad_type;
143 gst_videomixer_pad_base_init (gpointer g_class)
148 gst_videomixer_pad_class_init (GstVideoMixerPadClass * klass)
150 GObjectClass *gobject_class;
152 gobject_class = (GObjectClass *) klass;
154 gobject_class->set_property =
155 GST_DEBUG_FUNCPTR (gst_videomixer_pad_set_property);
156 gobject_class->get_property =
157 GST_DEBUG_FUNCPTR (gst_videomixer_pad_get_property);
159 g_object_class_install_property (gobject_class, ARG_PAD_ZORDER,
160 g_param_spec_uint ("zorder", "Z-Order", "Z Order of the picture",
161 0, 10000, DEFAULT_PAD_ZORDER, G_PARAM_READWRITE));
162 g_object_class_install_property (gobject_class, ARG_PAD_XPOS,
163 g_param_spec_int ("xpos", "X Position", "X Position of the picture",
164 G_MININT, G_MAXINT, DEFAULT_PAD_XPOS, G_PARAM_READWRITE));
165 g_object_class_install_property (gobject_class, ARG_PAD_YPOS,
166 g_param_spec_int ("ypos", "Y Position", "Y Position of the picture",
167 G_MININT, G_MAXINT, DEFAULT_PAD_YPOS, G_PARAM_READWRITE));
168 g_object_class_install_property (gobject_class, ARG_PAD_ALPHA,
169 g_param_spec_double ("alpha", "Alpha", "Alpha of the picture",
170 0.0, 1.0, DEFAULT_PAD_ALPHA, G_PARAM_READWRITE));
174 gst_videomixer_pad_get_property (GObject * object, guint prop_id,
175 GValue * value, GParamSpec * pspec)
177 GstVideoMixerPad *pad = GST_VIDEO_MIXER_PAD (object);
181 g_value_set_uint (value, pad->zorder);
184 g_value_set_int (value, pad->xpos);
187 g_value_set_int (value, pad->ypos);
190 g_value_set_double (value, pad->alpha);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 gst_videomixer_pad_set_property (GObject * object, guint prop_id,
200 const GValue * value, GParamSpec * pspec)
202 GstVideoMixerPad *pad;
205 pad = GST_VIDEO_MIXER_PAD (object);
206 mix = GST_VIDEO_MIXER (gst_pad_get_parent (GST_PAD (pad)));
210 pad->zorder = g_value_get_uint (value);
211 gst_videomixer_sort_pads (mix);
214 pad->xpos = g_value_get_int (value);
217 pad->ypos = g_value_get_int (value);
220 pad->alpha = g_value_get_double (value);
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 gst_object_unref (mix);
232 VIDEO_MIXER_BACKGROUND_CHECKER,
233 VIDEO_MIXER_BACKGROUND_BLACK,
234 VIDEO_MIXER_BACKGROUND_WHITE,
236 GstVideoMixerBackground;
238 struct _GstVideoMixer
245 /* Sink pads using Collect Pads from core's base library */
246 GstCollectPads *collect;
247 /* sinkpads, a GSList of GstVideoMixerPads */
253 GstVideoMixerPad *master;
255 gint in_width, in_height;
256 gint out_width, out_height;
258 GstVideoMixerBackground background;
260 gint in_framerate_numerator;
261 gint in_framerate_denominator;
264 struct _GstVideoMixerClass
266 GstElementClass parent_class;
270 gst_videomixer_pad_sink_setcaps (GstPad * pad, GstCaps * vscaps)
273 GstVideoMixerPad *mixpad;
274 GstStructure *structure;
275 gint in_width, in_height;
276 gboolean ret = FALSE;
277 const GValue *framerate;
279 mix = GST_VIDEO_MIXER (gst_pad_get_parent (pad));
280 mixpad = GST_VIDEO_MIXER_PAD (pad);
282 GST_DEBUG ("videomixer: setcaps triggered on %s", gst_pad_get_name (pad));
284 structure = gst_caps_get_structure (vscaps, 0);
286 if (!gst_structure_get_int (structure, "width", &in_width)
287 || !gst_structure_get_int (structure, "height", &in_height)
288 || (framerate = gst_structure_get_value (structure, "framerate")) != NULL)
291 mixpad->in_framerate_numerator = gst_value_get_fraction_numerator (framerate);
292 mixpad->in_framerate_denominator =
293 gst_value_get_fraction_denominator (framerate);
295 mixpad->in_width = in_width;
296 mixpad->in_height = in_height;
301 mix->in_width = MAX (mix->in_width, mixpad->in_width);
302 mix->in_height = MAX (mix->in_height, mixpad->in_height);
304 /* If mix framerate < mixpad framerate, using fractions */
305 if ((gint64) mix->in_framerate_numerator * mixpad->in_framerate_denominator <
306 (gint64) mixpad->in_framerate_numerator * mix->in_framerate_denominator) {
307 mix->in_framerate_numerator = mixpad->in_framerate_numerator;
308 mix->in_framerate_denominator = mixpad->in_framerate_denominator;
309 mix->master = mixpad;
315 gst_object_unref (mix);
321 gst_videomixer_pad_link (GstPad * pad, GstPad * peer, gpointer data)
323 GST_DEBUG_OBJECT (pad, "connected");
327 gst_videomixer_pad_unlink (GstPad * pad, GstPad * peer, gpointer data)
329 GST_DEBUG_OBJECT (pad, "unlinked");
333 gst_videomixer_pad_init (GstVideoMixerPad * mixerpad)
335 g_signal_connect (mixerpad, "linked",
336 G_CALLBACK (gst_videomixer_pad_link), mixerpad);
337 g_signal_connect (mixerpad, "unlinked",
338 G_CALLBACK (gst_videomixer_pad_unlink), mixerpad);
340 /* setup some pad functions */
341 gst_pad_set_setcaps_function (GST_PAD (mixerpad),
342 gst_videomixer_pad_sink_setcaps);
344 mixerpad->zorder = DEFAULT_PAD_ZORDER;
345 mixerpad->xpos = DEFAULT_PAD_XPOS;
346 mixerpad->ypos = DEFAULT_PAD_YPOS;
347 mixerpad->alpha = DEFAULT_PAD_ALPHA;
351 /* elementfactory information */
352 static GstElementDetails gst_videomixer_details =
353 GST_ELEMENT_DETAILS ("video mixer",
354 "Filter/Editor/Video",
355 "Mix multiple video streams",
356 "Wim Taymans <wim@fluendo.com>");
358 /* VideoMixer signals and args */
365 #define DEFAULT_BACKGROUND VIDEO_MIXER_BACKGROUND_CHECKER
372 #define GST_TYPE_VIDEO_MIXER_BACKGROUND (gst_video_mixer_background_get_type())
374 gst_video_mixer_background_get_type (void)
376 static GType video_mixer_background_type = 0;
377 static GEnumValue video_mixer_background[] = {
378 {VIDEO_MIXER_BACKGROUND_CHECKER, "0", "Checker pattern"},
379 {VIDEO_MIXER_BACKGROUND_BLACK, "1", "Black"},
380 {VIDEO_MIXER_BACKGROUND_WHITE, "2", "White"},
384 if (!video_mixer_background_type) {
385 video_mixer_background_type =
386 g_enum_register_static ("GstVideoMixerBackground",
387 video_mixer_background);
389 return video_mixer_background_type;
392 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
395 GST_STATIC_CAPS ("video/x-raw-yuv,"
396 "format = (fourcc) AYUV,"
397 "width = (int) [ 1, max ],"
398 "height = (int) [ 1, max ]," "framerate = (fraction) [ 0/1, MAX ]")
401 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
404 GST_STATIC_CAPS ("video/x-raw-yuv,"
405 "format = (fourcc) AYUV,"
406 "width = (int) [ 1, max ],"
407 "height = (int) [ 1, max ]," "framerate = (fraction) [ 0/1, MAX ]")
410 static void gst_videomixer_base_init (gpointer g_class);
411 static void gst_videomixer_class_init (GstVideoMixerClass * klass);
412 static void gst_videomixer_init (GstVideoMixer * videomixer);
414 static GstCaps *gst_videomixer_getcaps (GstPad * pad);
416 static GstFlowReturn gst_videomixer_collected (GstCollectPads * pads,
417 GstVideoMixer * mix);
418 static GstPad *gst_videomixer_request_new_pad (GstElement * element,
419 GstPadTemplate * templ, const gchar * name);
420 static void gst_videomixer_set_property (GObject * object, guint prop_id,
421 const GValue * value, GParamSpec * pspec);
422 static void gst_videomixer_get_property (GObject * object, guint prop_id,
423 GValue * value, GParamSpec * pspec);
424 static GstStateChangeReturn gst_videomixer_change_state (GstElement * element,
425 GstStateChange transition);
427 static GstElementClass *parent_class = NULL;
429 /*static guint gst_videomixer_signals[LAST_SIGNAL] = { 0 }; */
432 gst_videomixer_get_type (void)
434 static GType videomixer_type = 0;
436 if (!videomixer_type) {
437 static const GTypeInfo videomixer_info = {
438 sizeof (GstVideoMixerClass),
439 gst_videomixer_base_init,
441 (GClassInitFunc) gst_videomixer_class_init,
444 sizeof (GstVideoMixer),
446 (GInstanceInitFunc) gst_videomixer_init,
450 g_type_register_static (GST_TYPE_ELEMENT, "GstVideoMixer",
451 &videomixer_info, 0);
453 return videomixer_type;
457 gst_videomixer_base_init (gpointer g_class)
459 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
461 gst_element_class_add_pad_template (element_class,
462 gst_static_pad_template_get (&src_factory));
463 gst_element_class_add_pad_template (element_class,
464 gst_static_pad_template_get (&sink_factory));
466 gst_element_class_set_details (element_class, &gst_videomixer_details);
470 gst_videomixer_class_init (GstVideoMixerClass * klass)
472 GObjectClass *gobject_class;
473 GstElementClass *gstelement_class;
475 gobject_class = (GObjectClass *) klass;
476 gstelement_class = (GstElementClass *) klass;
478 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
480 gobject_class->get_property = gst_videomixer_get_property;
481 gobject_class->set_property = gst_videomixer_set_property;
483 g_object_class_install_property (gobject_class, ARG_BACKGROUND,
484 g_param_spec_enum ("background", "Background", "Background type",
485 GST_TYPE_VIDEO_MIXER_BACKGROUND,
486 DEFAULT_BACKGROUND, G_PARAM_READWRITE));
488 gstelement_class->request_new_pad = gst_videomixer_request_new_pad;
489 gstelement_class->change_state = gst_videomixer_change_state;
493 gst_videomixer_init (GstVideoMixer * mix)
495 GstElementClass *klass = GST_ELEMENT_GET_CLASS (mix);
498 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
500 gst_pad_set_getcaps_function (GST_PAD (mix->srcpad), gst_videomixer_getcaps);
501 gst_element_add_pad (GST_ELEMENT (mix), mix->srcpad);
503 mix->collect = gst_collect_pads_new ();
504 mix->background = DEFAULT_BACKGROUND;
510 gst_collect_pads_set_function (mix->collect,
511 (GstCollectPadsFunction) gst_videomixer_collected, mix);
515 gst_videomixer_getcaps (GstPad * pad)
519 GstStructure *structure;
521 mix = GST_VIDEO_MIXER (gst_pad_get_parent (pad));
522 caps = gst_caps_copy (gst_pad_get_pad_template_caps (mix->srcpad));
524 structure = gst_caps_get_structure (caps, 0);
526 if (mix->out_width != 0) {
527 gst_structure_set (structure, "width", G_TYPE_INT, mix->out_width, NULL);
529 if (mix->out_height != 0) {
530 gst_structure_set (structure, "height", G_TYPE_INT, mix->out_height, NULL);
532 if (mix->in_framerate_denominator != 0) {
533 gst_structure_set (structure,
534 "framerate", GST_TYPE_FRACTION, mix->in_framerate_numerator,
535 mix->in_framerate_denominator, NULL);
538 gst_object_unref (mix);
544 gst_videomixer_request_new_pad (GstElement * element,
545 GstPadTemplate * templ, const gchar * req_name)
547 GstVideoMixer *mix = NULL;
548 GstVideoMixerPad *mixpad = NULL;
549 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
551 g_return_val_if_fail (templ != NULL, NULL);
553 if (templ->direction != GST_PAD_SINK) {
554 g_warning ("videomixer: request pad that is not a SINK pad\n");
558 g_return_val_if_fail (GST_IS_VIDEO_MIXER (element), NULL);
560 mix = GST_VIDEO_MIXER (element);
562 if (templ == gst_element_class_get_pad_template (klass, "sink_%d")) {
565 GstVideoMixerCollect *mixcol = NULL;
567 if (req_name == NULL || strlen (req_name) < 6) {
568 /* no name given when requesting the pad, use random serial number */
571 /* parse serial number from requested padname */
572 serial = atoi (&req_name[5]);
574 /* create new pad with the name */
575 name = g_strdup_printf ("sink_%d", serial);
576 mixpad = g_object_new (GST_TYPE_VIDEO_MIXER_PAD, "name", name, "direction",
577 templ->direction, "template", templ, NULL);
580 mixpad->zorder = mix->numpads;
581 mixpad->xpos = DEFAULT_PAD_XPOS;
582 mixpad->ypos = DEFAULT_PAD_YPOS;
583 mixpad->alpha = DEFAULT_PAD_ALPHA;
585 mixcol = (GstVideoMixerCollect *)
586 gst_collect_pads_add_pad (mix->collect, GST_PAD (mixpad),
587 sizeof (GstVideoMixerCollect));
589 /* Keep track of eachother */
590 mixcol->mixpad = mixpad;
591 mixpad->mixcol = mixcol;
593 /* Keep an internal list of mixpads for zordering */
594 mix->sinkpads = g_slist_append (mix->sinkpads, mixpad);
597 g_warning ("videomixer: this is not our template!\n");
601 /* dd the pad to the element */
602 gst_element_add_pad (element, GST_PAD (mixpad));
604 return GST_PAD (mixpad);
607 #define BLEND_NORMAL(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V) \
608 Y = ((Y1*(255-alpha))+(Y2*alpha))>>8; \
609 U = ((U1*(255-alpha))+(U2*alpha))>>8; \
610 V = ((V1*(255-alpha))+(V2*alpha))>>8;
612 #define BLEND_ADD(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V) \
613 Y = Y1+((Y2*alpha)>>8); \
614 U = U1+(((127*(255-alpha)+(U2*alpha)))>>8)-127; \
615 V = V1+(((127*(255-alpha)+(V2*alpha)))>>8)-127; \
617 gint mult = MAX (0, 288-Y); \
618 U = ((U*mult) + (127*(32-mult)))>>5; \
619 V = ((V*mult) + (127*(32-mult)))>>5; \
625 #define BLEND_SUBTRACT(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V) \
626 Y = Y1-((Y2*alpha)>>8); \
627 U = U1+(((127*(255-alpha)+(U2*alpha)))>>8)-127; \
628 V = V1+(((127*(255-alpha)+(V2*alpha)))>>8)-127; \
630 gint mult = MIN (32, -Y); \
631 U = ((U*(32-mult)) + (127*mult))>>5; \
632 V = ((V*(32-mult)) + (127*mult))>>5; \
636 #define BLEND_DARKEN(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V) \
638 Y = Y1; U = U1; V = V1; \
641 Y = ((Y1*(255-alpha))+(Y2*alpha))>>8; \
642 U = ((U1*(255-alpha))+(U2*alpha))>>8; \
643 V = ((V1*(255-alpha))+(V2*alpha))>>8; \
646 #define BLEND_LIGHTEN(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V) \
648 Y = Y1; U = U1; V = V1; \
651 Y = ((Y1*(255-alpha))+(Y2*alpha))>>8; \
652 U = ((U1*(255-alpha))+(U2*alpha))>>8; \
653 V = ((V1*(255-alpha))+(V2*alpha))>>8; \
656 #define BLEND_MULTIPLY(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V) \
657 Y = (Y1*(256*(255-alpha) +(Y2*alpha)))>>16; \
658 U = ((U1*(255-alpha)*256)+(alpha*(U1*Y2+128*(256-Y2))))>>16; \
659 V = ((V1*(255-alpha)*256)+(alpha*(V1*Y2+128*(256-Y2))))>>16;
661 #define BLEND_DIFFERENCE(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V) \
662 Y = ABS((gint)Y1-(gint)Y2)+127; \
663 U = ABS((gint)U1-(gint)U2)+127; \
664 V = ABS((gint)V1-(gint)V2)+127; \
665 Y = ((Y*alpha)+(Y1*(255-alpha)))>>8; \
666 U = ((U*alpha)+(U1*(255-alpha)))>>8; \
667 V = ((V*alpha)+(V1*(255-alpha)))>>8; \
669 gint mult = MAX (0, 288-Y); \
670 U = ((U*mult) + (127*(32-mult)))>>5; \
671 V = ((V*mult) + (127*(32-mult)))>>5; \
674 gint mult = MIN (32, -Y); \
675 U = ((U*(32-mult)) + (127*mult))>>5; \
676 V = ((V*(32-mult)) + (127*mult))>>5; \
679 U = CLAMP(U, 0, 255); \
680 V = CLAMP(V, 0, 255);
682 #define BLEND_EXCLUSION(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V) \
683 Y = ((gint)(Y1^0xff)*Y2+(gint)(Y2^0xff)*Y1)>>8; \
684 U = ((gint)(U1^0xff)*Y2+(gint)(Y2^0xff)*U1)>>8; \
685 V = ((gint)(V1^0xff)*Y2+(gint)(Y2^0xff)*V1)>>8; \
686 Y = ((Y*alpha)+(Y1*(255-alpha)))>>8; \
687 U = ((U*alpha)+(U1*(255-alpha)))>>8; \
688 V = ((V*alpha)+(V1*(255-alpha)))>>8; \
690 gint mult = MAX (0, 288-Y); \
691 U = ((U*mult) + (127*(32-mult)))>>5; \
692 V = ((V*mult) + (127*(32-mult)))>>5; \
695 gint mult = MIN (32, -Y); \
696 U = ((U*(32-mult)) + (127*mult))>>5; \
697 V = ((V*(32-mult)) + (127*mult))>>5; \
700 U = CLAMP(U, 0, 255); \
701 V = CLAMP(V, 0, 255);
703 #define BLEND_SOFTLIGHT(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V) \
704 Y = (gint)Y1+(gint)Y2 - 127; \
705 U = (gint)U1+(gint)U2 - 127; \
706 V = (gint)V1+(gint)V2 - 127; \
707 Y = ((Y*alpha)+(Y1*(255-alpha)))>>8; \
708 U = ((U*alpha)+(U1*(255-alpha)))>>8; \
709 V = ((V*alpha)+(V1*(255-alpha)))>>8; \
711 gint mult = MAX (0, 288-Y); \
712 U = ((U*mult) + (127*(32-mult)))>>5; \
713 V = ((V*mult) + (127*(32-mult)))>>5; \
716 gint mult = MIN (32, -Y); \
717 U = ((U*(32-mult)) + (127*mult))>>5; \
718 V = ((V*(32-mult)) + (127*mult))>>5; \
722 #define BLEND_HARDLIGHT(Y1,U1,V1,Y2,U2,V2,alpha,Y,U,V) \
723 Y = (gint)Y1+(gint)Y2*2 - 255; \
724 U = (gint)U1+(gint)U2 - 127; \
725 V = (gint)V1+(gint)V2 - 127; \
726 Y = ((Y*alpha)+(Y1*(255-alpha)))>>8; \
727 U = ((U*alpha)+(U1*(255-alpha)))>>8; \
728 V = ((V*alpha)+(V1*(255-alpha)))>>8; \
730 gint mult = MAX (0, 288-Y); \
731 U = ((U*mult) + (127*(32-mult)))>>5; \
732 V = ((V*mult) + (127*(32-mult)))>>5; \
735 gint mult = MIN (32, -Y); \
736 U = ((U*(32-mult)) + (127*mult))>>5; \
737 V = ((V*(32-mult)) + (127*mult))>>5; \
741 #define BLEND_MODE BLEND_NORMAL
743 #define BLEND_MODE BLEND_NORMAL
744 #define BLEND_MODE BLEND_ADD
745 #define BLEND_MODE BLEND_SUBTRACT
746 #define BLEND_MODE BLEND_LIGHTEN
747 #define BLEND_MODE BLEND_DARKEN
748 #define BLEND_MODE BLEND_MULTIPLY
749 #define BLEND_MODE BLEND_DIFFERENCE
750 #define BLEND_MODE BLEND_EXCLUSION
751 #define BLEND_MODE BLEND_SOFTLIGHT
752 #define BLEND_MODE BLEND_HARDLIGHT
755 #define ROUND_UP_2(x) (((x) + 1) & ~1)
756 #define ROUND_UP_4(x) (((x) + 3) & ~3)
757 #define ROUND_UP_8(x) (((x) + 7) & ~7)
759 /* note that this function does packing conversion and blending at the
762 gst_videomixer_blend_ayuv_ayuv (guint8 * src, gint xpos, gint ypos,
763 gint src_width, gint src_height, gdouble src_alpha,
764 guint8 * dest, gint dest_width, gint dest_height)
768 gint src_stride, dest_stride;
769 gint src_add, dest_add;
772 src_stride = ROUND_UP_2 (src_width) * 4;
773 dest_stride = ROUND_UP_2 (dest_width) * 4;
775 b_alpha = (gint) (src_alpha * 255);
777 /* adjust src pointers for negative sizes */
784 src += -ypos * src_stride;
788 /* adjust width/height if the src is bigger than dest */
789 if (xpos + src_width > dest_width) {
790 src_width = dest_width - xpos;
792 if (ypos + src_height > dest_height) {
793 src_height = dest_height - ypos;
796 src_add = src_stride - (4 * ROUND_UP_2 (src_width));
797 dest_add = dest_stride - (4 * ROUND_UP_2 (src_width));
799 dest = dest + 4 * xpos + (ypos * dest_stride);
801 /* we convert a square of 2x2 samples to generate 4 Luma and 2 chroma samples */
802 for (i = 0; i < ROUND_UP_2 (src_height); i++) {
803 for (j = 0; j < ROUND_UP_2 (src_width); j++) {
804 alpha = (src[0] * b_alpha) >> 8;
805 BLEND_MODE (dest[1], dest[2], dest[3], src[1], src[2], src[3],
823 pad_zorder_compare (const GstVideoMixerPad * pad1,
824 const GstVideoMixerPad * pad2)
826 return pad1->zorder - pad2->zorder;
830 gst_videomixer_sort_pads (GstVideoMixer * mix)
832 mix->sinkpads = g_slist_sort (mix->sinkpads,
833 (GCompareFunc) pad_zorder_compare);
836 /* fill a buffer with a checkerboard pattern */
838 gst_videomixer_fill_checker (guint8 * dest, gint width, gint height)
842 static int tab[] = { 80, 160, 80, 160 };
844 stride = ROUND_UP_2 (width);
846 for (i = 0; i < height; i++) {
847 for (j = 0; j < stride; j++) {
849 *dest++ = tab[((i & 0x8) >> 3) + ((j & 0x8) >> 3)];
857 gst_videomixer_fill_color (guint8 * dest, gint width, gint height,
858 gint colY, gint colU, gint colV)
863 stride = ROUND_UP_2 (width);
865 for (i = 0; i < height; i++) {
866 for (j = 0; j < stride; j++) {
875 /* try to get a buffer on all pads. As long as the queued value is
876 * negative, we skip buffers */
878 gst_videomixer_fill_queues (GstVideoMixer * mix)
883 g_return_val_if_fail (GST_IS_VIDEO_MIXER (mix), FALSE);
885 /* try to make sure we have a buffer from each usable pad first */
886 walk = mix->collect->data;
888 GstCollectData *data = (GstCollectData *) walk->data;
889 GstVideoMixerCollect *mixcol = (GstVideoMixerCollect *) data;
890 GstVideoMixerPad *mixpad = mixcol->mixpad;
892 walk = g_slist_next (walk);
894 if (mixcol->buffer == NULL) {
895 GstBuffer *buf = NULL;
897 GST_LOG ("we need a new buffer");
899 buf = gst_collect_pads_pop (mix->collect, data);
904 GST_LOG ("we have a buffer !");
906 mixcol->buffer = buf;
907 duration = GST_BUFFER_DURATION (mixcol->buffer);
908 /* no duration on the buffer, use the framerate */
909 if (!GST_CLOCK_TIME_IS_VALID (duration)) {
910 if (mixpad->in_framerate_numerator == 0) {
911 duration = GST_CLOCK_TIME_NONE;
913 duration = GST_SECOND * mixpad->in_framerate_denominator /
914 mixpad->in_framerate_numerator;
917 if (GST_CLOCK_TIME_IS_VALID (duration))
918 mixpad->queued += duration;
919 else if (!mixpad->queued)
920 mixpad->queued = GST_CLOCK_TIME_NONE;
922 GST_LOG ("pop returned a NULL buffer");
925 if (mixcol->buffer !=
926 NULL /* && GST_CLOCK_TIME_IS_VALID (mixpad->queued) */ ) {
927 /* got a buffer somewhere so we're not eos */
935 /* blend all buffers present on the pads */
937 gst_videomixer_blend_buffers (GstVideoMixer * mix, GstBuffer * outbuf)
941 walk = mix->sinkpads;
942 while (walk) { /* We walk with this list because it's ordered */
943 GstVideoMixerPad *pad = GST_VIDEO_MIXER_PAD (walk->data);
944 GstVideoMixerCollect *mixcol = pad->mixcol;
946 walk = g_slist_next (walk);
948 if (mixcol->buffer != NULL) {
949 gst_videomixer_blend_ayuv_ayuv (GST_BUFFER_DATA (mixcol->buffer),
950 pad->xpos, pad->ypos,
951 pad->in_width, pad->in_height,
953 GST_BUFFER_DATA (outbuf), mix->out_width, mix->out_height);
954 if (pad == mix->master) {
955 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (mixcol->buffer);
956 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (mixcol->buffer);
962 /* remove buffers from the queue that were expired in the
963 * interval of the master, we also prepare the queued value
964 * in the pad so that we can skip and fill buffers later on */
966 gst_videomixer_update_queues (GstVideoMixer * mix)
971 interval = mix->master->queued;
973 if (mix->in_framerate_numerator == 0) {
974 interval = G_MAXINT64;
976 interval = GST_SECOND * mix->in_framerate_denominator /
977 mix->in_framerate_numerator;
981 walk = mix->sinkpads;
983 GstVideoMixerPad *pad = GST_VIDEO_MIXER_PAD (walk->data);
984 GstVideoMixerCollect *mixcol = pad->mixcol;
986 walk = g_slist_next (walk);
988 if (mixcol->buffer != NULL && GST_CLOCK_TIME_IS_VALID (pad->queued)) {
989 pad->queued -= interval;
990 GST_DEBUG_OBJECT (pad, "queued now %lld", pad->queued);
991 if (pad->queued == 0) {
992 GST_DEBUG ("unreffing buffer");
993 gst_buffer_unref (mixcol->buffer);
994 mixcol->buffer = NULL;
1001 * The basic idea is to get a buffer on all pads and mix them together.
1002 * Based on the framerate, buffers are removed from the queues to make room
1006 gst_videomixer_loop (GstElement * element)
1011 gint new_width, new_height;
1014 mix = GST_VIDEO_MIXER (element);
1016 eos = gst_videomixer_fill_queues (mix);
1018 gst_pad_push (mix->srcpad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
1019 gst_element_set_eos (GST_ELEMENT (mix));
1023 new_width = mix->in_width;
1024 new_height = mix->in_height;
1026 if (new_width != mix->out_width ||
1027 new_height != mix->out_height || !GST_PAD_CAPS (mix->srcpad)) {
1030 newcaps = gst_caps_make_writable (
1031 gst_pad_get_negotiated_caps (GST_PAD (mix->master)));
1032 gst_caps_set_simple (newcaps, "format", GST_TYPE_FOURCC,
1033 GST_STR_FOURCC ("AYUV"), "width", G_TYPE_INT, new_width, "height",
1034 G_TYPE_INT, new_height, NULL);
1036 if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (mix->srcpad, newcaps))) {
1037 GST_ELEMENT_ERROR (mix, CORE, NEGOTIATION, (NULL), (NULL));
1041 mix->out_width = new_width;
1042 mix->out_height = new_height;
1045 outsize = ROUND_UP_2 (mix->out_width) * ROUND_UP_2 (mix->out_height) * 4;
1047 outbuf = gst_pad_alloc_buffer (mix->srcpad, GST_BUFFER_OFFSET_NONE, outsize);
1048 switch (mix->background) {
1049 case VIDEO_MIXER_BACKGROUND_CHECKER:
1050 gst_videomixer_fill_checker (GST_BUFFER_DATA (outbuf),
1051 new_width, new_height);
1053 case VIDEO_MIXER_BACKGROUND_BLACK:
1054 gst_videomixer_fill_color (GST_BUFFER_DATA (outbuf),
1055 new_width, new_height, 16, 128, 128);
1057 case VIDEO_MIXER_BACKGROUND_WHITE:
1058 gst_videomixer_fill_color (GST_BUFFER_DATA (outbuf),
1059 new_width, new_height, 240, 128, 128);
1063 gst_videomixer_blend_buffers (mix, outbuf);
1065 gst_videomixer_update_queues (mix);
1067 gst_pad_push (mix->srcpad, GST_DATA (outbuf));
1070 static GstFlowReturn
1071 gst_videomixer_collected (GstCollectPads * pads, GstVideoMixer * mix)
1073 GstFlowReturn ret = GST_FLOW_OK;
1074 GstBuffer *outbuf = NULL;
1076 gboolean eos = FALSE;
1078 g_return_val_if_fail (GST_IS_VIDEO_MIXER (mix), GST_FLOW_ERROR);
1080 GST_LOG ("all pads are collected");
1082 eos = gst_videomixer_fill_queues (mix);
1085 /* Push EOS downstream */
1086 GST_LOG ("all our sinkpads are EOS, pushing downstream");
1087 gst_pad_push_event (mix->srcpad, gst_event_new_eos ());
1088 ret = GST_FLOW_WRONG_STATE;
1092 /* Calculating out buffer size from input size */
1093 outsize = ROUND_UP_2 (mix->in_width) * ROUND_UP_2 (mix->in_height) * 4;
1095 /* If geometry has changed we need to set new caps on the buffer */
1096 if (mix->in_width != mix->out_width || mix->in_height != mix->out_height) {
1097 GstCaps *newcaps = NULL;
1099 newcaps = gst_caps_make_writable
1100 (gst_pad_get_negotiated_caps (GST_PAD (mix->master)));
1101 gst_caps_set_simple (newcaps,
1102 "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("AYUV"),
1103 "width", G_TYPE_INT, mix->in_width,
1104 "height", G_TYPE_INT, mix->in_height, NULL);
1106 mix->out_width = mix->in_width;
1107 mix->out_height = mix->in_height;
1109 ret = gst_pad_alloc_buffer (mix->srcpad, GST_BUFFER_OFFSET_NONE, outsize,
1111 gst_caps_unref (newcaps);
1112 } else { /* Otherwise we just allocate a buffer from current caps */
1113 ret = gst_pad_alloc_buffer (mix->srcpad, GST_BUFFER_OFFSET_NONE, outsize,
1114 GST_PAD_CAPS (mix->srcpad), &outbuf);
1117 if (ret != GST_FLOW_OK) {
1121 switch (mix->background) {
1122 case VIDEO_MIXER_BACKGROUND_CHECKER:
1123 gst_videomixer_fill_checker (GST_BUFFER_DATA (outbuf),
1124 mix->out_width, mix->out_height);
1126 case VIDEO_MIXER_BACKGROUND_BLACK:
1127 gst_videomixer_fill_color (GST_BUFFER_DATA (outbuf),
1128 mix->out_width, mix->out_height, 16, 128, 128);
1130 case VIDEO_MIXER_BACKGROUND_WHITE:
1131 gst_videomixer_fill_color (GST_BUFFER_DATA (outbuf),
1132 mix->out_width, mix->out_height, 240, 128, 128);
1136 gst_videomixer_blend_buffers (mix, outbuf);
1138 gst_videomixer_update_queues (mix);
1140 ret = gst_pad_push (mix->srcpad, outbuf);
1147 gst_videomixer_get_property (GObject * object,
1148 guint prop_id, GValue * value, GParamSpec * pspec)
1150 GstVideoMixer *mix = GST_VIDEO_MIXER (object);
1153 case ARG_BACKGROUND:
1154 g_value_set_enum (value, mix->background);
1157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1163 gst_videomixer_set_property (GObject * object,
1164 guint prop_id, const GValue * value, GParamSpec * pspec)
1166 GstVideoMixer *mix = GST_VIDEO_MIXER (object);
1169 case ARG_BACKGROUND:
1170 mix->background = g_value_get_enum (value);
1173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1178 static GstStateChangeReturn
1179 gst_videomixer_change_state (GstElement * element, GstStateChange transition)
1182 GstStateChangeReturn ret;
1184 g_return_val_if_fail (GST_IS_VIDEO_MIXER (element), GST_STATE_CHANGE_FAILURE);
1186 mix = GST_VIDEO_MIXER (element);
1188 switch (transition) {
1189 case GST_STATE_CHANGE_READY_TO_PAUSED:
1190 GST_LOG ("starting collectpads");
1191 gst_collect_pads_start (mix->collect);
1193 case GST_STATE_CHANGE_PAUSED_TO_READY:
1194 GST_LOG ("stopping collectpads");
1195 gst_collect_pads_stop (mix->collect);
1201 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1207 plugin_init (GstPlugin * plugin)
1209 GST_DEBUG_CATEGORY_INIT (gst_videomixer_debug, "videomixer", 0,
1212 return gst_element_register (plugin, "videomixer", GST_RANK_PRIMARY,
1213 GST_TYPE_VIDEO_MIXER);
1216 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1219 "Video mixer", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,