2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:element-smpte
23 * smpte can accept I420 video streams with the same width, height and
24 * framerate. The two incomming buffers are blended together using an effect
25 * specific alpha mask.
27 * The #GstSmpte:depth property defines the presision in bits of the mask. A
28 * higher presision will create a mask with smoother gradients in order to avoid
32 * <title>Sample pipelines</title>
34 * gst-launch-1.0 -v videotestsrc pattern=1 ! smpte name=s border=20000 type=234 duration=2000000000 ! videoconvert ! ximagesink videotestsrc ! s.
35 * ]| A pipeline to demonstrate the smpte transition.
36 * It shows a pinwheel transition a from a snow videotestsrc to an smpte
37 * pattern videotestsrc. The transition will take 2 seconds to complete. The
38 * edges of the transition are smoothed with a 20000 big border.
49 GST_DEBUG_CATEGORY_STATIC (gst_smpte_debug);
50 #define GST_CAT_DEFAULT gst_smpte_debug
52 static GstStaticPadTemplate gst_smpte_src_template =
53 GST_STATIC_PAD_TEMPLATE ("src",
56 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420")
60 static GstStaticPadTemplate gst_smpte_sink1_template =
61 GST_STATIC_PAD_TEMPLATE ("sink1",
64 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420")
68 static GstStaticPadTemplate gst_smpte_sink2_template =
69 GST_STATIC_PAD_TEMPLATE ("sink2",
72 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420")
77 /* SMPTE signals and args */
84 #define DEFAULT_PROP_TYPE 1
85 #define DEFAULT_PROP_BORDER 0
86 #define DEFAULT_PROP_DEPTH 16
87 #define DEFAULT_PROP_DURATION GST_SECOND
88 #define DEFAULT_PROP_INVERT FALSE
101 /* FIXME: should use video meta etc. */
102 #define I420_Y_ROWSTRIDE(width) (GST_ROUND_UP_4(width))
103 #define I420_U_ROWSTRIDE(width) (GST_ROUND_UP_8(width)/2)
104 #define I420_V_ROWSTRIDE(width) ((GST_ROUND_UP_8(I420_Y_ROWSTRIDE(width)))/2)
106 #define I420_Y_OFFSET(w,h) (0)
107 #define I420_U_OFFSET(w,h) (I420_Y_OFFSET(w,h)+(I420_Y_ROWSTRIDE(w)*GST_ROUND_UP_2(h)))
108 #define I420_V_OFFSET(w,h) (I420_U_OFFSET(w,h)+(I420_U_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
110 #define I420_SIZE(w,h) (I420_V_OFFSET(w,h)+(I420_V_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
113 #define GST_TYPE_SMPTE_TRANSITION_TYPE (gst_smpte_transition_type_get_type())
115 gst_smpte_transition_type_get_type (void)
117 static GType smpte_transition_type = 0;
118 GEnumValue *smpte_transitions;
120 if (!smpte_transition_type) {
121 const GList *definitions;
124 definitions = gst_mask_get_definitions ();
126 g_new0 (GEnumValue, g_list_length ((GList *) definitions) + 1);
128 while (definitions) {
129 GstMaskDefinition *definition = (GstMaskDefinition *) definitions->data;
131 definitions = g_list_next (definitions);
133 smpte_transitions[i].value = definition->type;
134 /* older GLib versions have the two fields as non-const, hence the cast */
135 smpte_transitions[i].value_nick = (gchar *) definition->short_name;
136 smpte_transitions[i].value_name = (gchar *) definition->long_name;
141 smpte_transition_type =
142 g_enum_register_static ("GstSMPTETransitionType", smpte_transitions);
144 return smpte_transition_type;
148 static void gst_smpte_finalize (GstSMPTE * smpte);
150 static GstFlowReturn gst_smpte_collected (GstCollectPads * pads,
153 static void gst_smpte_set_property (GObject * object, guint prop_id,
154 const GValue * value, GParamSpec * pspec);
155 static void gst_smpte_get_property (GObject * object, guint prop_id,
156 GValue * value, GParamSpec * pspec);
158 static GstStateChangeReturn gst_smpte_change_state (GstElement * element,
159 GstStateChange transition);
161 /*static guint gst_smpte_signals[LAST_SIGNAL] = { 0 }; */
163 #define gst_smpte_parent_class parent_class
164 G_DEFINE_TYPE (GstSMPTE, gst_smpte, GST_TYPE_ELEMENT);
167 gst_smpte_class_init (GstSMPTEClass * klass)
169 GObjectClass *gobject_class;
170 GstElementClass *gstelement_class;
172 gobject_class = (GObjectClass *) klass;
173 gstelement_class = (GstElementClass *) klass;
175 parent_class = g_type_class_peek_parent (klass);
177 gobject_class->set_property = gst_smpte_set_property;
178 gobject_class->get_property = gst_smpte_get_property;
179 gobject_class->finalize = (GObjectFinalizeFunc) gst_smpte_finalize;
183 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TYPE,
184 g_param_spec_enum ("type", "Type", "The type of transition to use",
185 GST_TYPE_SMPTE_TRANSITION_TYPE, DEFAULT_PROP_TYPE,
186 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BORDER,
188 g_param_spec_int ("border", "Border",
189 "The border width of the transition", 0, G_MAXINT,
190 DEFAULT_PROP_BORDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DEPTH,
192 g_param_spec_int ("depth", "Depth", "Depth of the mask in bits", 1, 24,
193 DEFAULT_PROP_DEPTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DURATION,
195 g_param_spec_uint64 ("duration", "Duration",
196 "Duration of the transition effect in nanoseconds", 0, G_MAXUINT64,
197 DEFAULT_PROP_DURATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_INVERT,
199 g_param_spec_boolean ("invert", "Invert",
200 "Invert transition mask", DEFAULT_PROP_INVERT,
201 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_smpte_change_state);
205 gst_element_class_add_pad_template (gstelement_class,
206 gst_static_pad_template_get (&gst_smpte_sink1_template));
207 gst_element_class_add_pad_template (gstelement_class,
208 gst_static_pad_template_get (&gst_smpte_sink2_template));
209 gst_element_class_add_pad_template (gstelement_class,
210 gst_static_pad_template_get (&gst_smpte_src_template));
211 gst_element_class_set_static_metadata (gstelement_class, "SMPTE transitions",
212 "Filter/Editor/Video",
213 "Apply the standard SMPTE transitions on video images",
214 "Wim Taymans <wim.taymans@chello.be>");
217 /* wht yel cya grn mag red blu blk -I Q */
218 static const int y_colors[] = { 255, 226, 179, 150, 105, 76, 29, 16, 16, 0 };
219 static const int u_colors[] = { 128, 0, 170, 46, 212, 85, 255, 128, 0, 128 };
220 static const int v_colors[] = { 128, 155, 0, 21, 235, 255, 107, 128, 128, 255 };
223 fill_i420 (guint8 * data, gint width, gint height, gint color)
225 gint size = I420_Y_ROWSTRIDE (width) * GST_ROUND_UP_2 (height);
226 gint size4 = size >> 2;
228 guint8 *up = data + I420_U_OFFSET (width, height);
229 guint8 *vp = data + I420_V_OFFSET (width, height);
231 memset (yp, y_colors[color], size);
232 memset (up, u_colors[color], size4);
233 memset (vp, v_colors[color], size4);
237 gst_smpte_update_mask (GstSMPTE * smpte, gint type, gboolean invert,
238 gint depth, gint width, gint height)
243 if (smpte->type == type &&
244 smpte->invert == invert &&
245 smpte->depth == depth &&
246 smpte->width == width && smpte->height == height)
250 newmask = gst_mask_factory_new (type, invert, depth, width, height);
253 gst_mask_destroy (smpte->mask);
255 smpte->mask = newmask;
257 smpte->invert = invert;
258 smpte->depth = depth;
259 smpte->width = width;
260 smpte->height = height;
268 gst_smpte_setcaps (GstPad * pad, GstCaps * caps)
274 smpte = GST_SMPTE (GST_PAD_PARENT (pad));
276 gst_video_info_init (&vinfo);
277 if (!gst_video_info_from_caps (&vinfo, caps))
280 smpte->width = GST_VIDEO_INFO_WIDTH (&vinfo);
281 smpte->height = GST_VIDEO_INFO_HEIGHT (&vinfo);
282 smpte->fps_num = GST_VIDEO_INFO_FPS_N (&vinfo);
283 smpte->fps_denom = GST_VIDEO_INFO_FPS_D (&vinfo);
285 /* figure out the duration in frames */
286 smpte->end_position = gst_util_uint64_scale (smpte->duration,
287 smpte->fps_num, GST_SECOND * smpte->fps_denom);
289 GST_DEBUG_OBJECT (smpte, "duration: %d frames", smpte->end_position);
292 gst_smpte_update_mask (smpte, smpte->type, smpte->invert, smpte->depth,
293 smpte->width, smpte->height);
295 if (pad == smpte->sinkpad1) {
296 GST_DEBUG_OBJECT (smpte, "setting pad1 info");
297 smpte->vinfo1 = vinfo;
299 GST_DEBUG_OBJECT (smpte, "setting pad2 info");
300 smpte->vinfo2 = vinfo;
307 gst_smpte_sink_event (GstCollectPads * pads,
308 GstCollectData * data, GstEvent * event, gpointer user_data)
311 gboolean ret = FALSE;
315 switch (GST_EVENT_TYPE (event)) {
320 gst_event_parse_caps (event, &caps);
321 ret = gst_smpte_setcaps (pad, caps);
322 gst_event_unref (event);
331 return gst_collect_pads_event_default (pads, data, event, FALSE);
337 gst_smpte_init (GstSMPTE * smpte)
340 gst_pad_new_from_static_template (&gst_smpte_sink1_template, "sink1");
341 GST_PAD_SET_PROXY_CAPS (smpte->sinkpad1);
342 gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad1);
345 gst_pad_new_from_static_template (&gst_smpte_sink2_template, "sink2");
346 GST_PAD_SET_PROXY_CAPS (smpte->sinkpad2);
347 gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad2);
350 gst_pad_new_from_static_template (&gst_smpte_src_template, "src");
351 gst_element_add_pad (GST_ELEMENT (smpte), smpte->srcpad);
353 smpte->collect = gst_collect_pads_new ();
354 gst_collect_pads_set_function (smpte->collect,
355 (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_smpte_collected), smpte);
356 gst_collect_pads_set_event_function (smpte->collect,
357 GST_DEBUG_FUNCPTR (gst_smpte_sink_event), smpte);
359 gst_collect_pads_add_pad (smpte->collect, smpte->sinkpad1,
360 sizeof (GstCollectData), NULL, TRUE);
361 gst_collect_pads_add_pad (smpte->collect, smpte->sinkpad2,
362 sizeof (GstCollectData), NULL, TRUE);
364 smpte->type = DEFAULT_PROP_TYPE;
365 smpte->border = DEFAULT_PROP_BORDER;
366 smpte->depth = DEFAULT_PROP_DEPTH;
367 smpte->duration = DEFAULT_PROP_DURATION;
368 smpte->invert = DEFAULT_PROP_INVERT;
370 smpte->fps_denom = 1;
374 gst_smpte_finalize (GstSMPTE * smpte)
376 if (smpte->collect) {
377 gst_object_unref (smpte->collect);
380 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) smpte);
384 gst_smpte_reset (GstSMPTE * smpte)
389 smpte->end_position = 0;
390 smpte->send_stream_start = TRUE;
394 gst_smpte_blend_i420 (GstVideoFrame * frame1, GstVideoFrame * frame2,
395 GstVideoFrame * oframe, GstMask * mask, gint border, gint pos)
401 guint8 *in1, *in2, *out, *in1u, *in1v, *in2u, *in2v, *outu, *outv;
410 width = GST_VIDEO_FRAME_WIDTH (frame1);
411 height = GST_VIDEO_FRAME_HEIGHT (frame1);
413 in1 = GST_VIDEO_FRAME_COMP_DATA (frame1, 0);
414 in2 = GST_VIDEO_FRAME_COMP_DATA (frame2, 0);
415 out = GST_VIDEO_FRAME_COMP_DATA (oframe, 0);
417 in1u = GST_VIDEO_FRAME_COMP_DATA (frame1, 1);
418 in1v = GST_VIDEO_FRAME_COMP_DATA (frame1, 2);
419 in2u = GST_VIDEO_FRAME_COMP_DATA (frame2, 1);
420 in2v = GST_VIDEO_FRAME_COMP_DATA (frame2, 2);
421 outu = GST_VIDEO_FRAME_COMP_DATA (oframe, 1);
422 outv = GST_VIDEO_FRAME_COMP_DATA (oframe, 2);
426 for (i = 0; i < height; i++) {
427 for (j = 0; j < width; j++) {
429 value = ((CLAMP (value, min, max) - min) << 8) / border;
431 out[j] = ((in1[j] * value) + (in2[j] * (256 - value))) >> 8;
432 if (!(i & 1) && !(j & 1)) {
434 ((in1u[j / 2] * value) + (in2u[j / 2] * (256 - value))) >> 8;
436 ((in1v[j / 2] * value) + (in2v[j / 2] * (256 - value))) >> 8;
440 in1 += GST_VIDEO_FRAME_COMP_STRIDE (frame1, 0);
441 in2 += GST_VIDEO_FRAME_COMP_STRIDE (frame2, 0);
442 out += GST_VIDEO_FRAME_COMP_STRIDE (oframe, 0);
445 in1u += GST_VIDEO_FRAME_COMP_STRIDE (frame1, 1);
446 in2u += GST_VIDEO_FRAME_COMP_STRIDE (frame2, 1);
447 in1v += GST_VIDEO_FRAME_COMP_STRIDE (frame1, 2);
448 in2v += GST_VIDEO_FRAME_COMP_STRIDE (frame1, 2);
449 outu += GST_VIDEO_FRAME_COMP_STRIDE (oframe, 1);
450 outv += GST_VIDEO_FRAME_COMP_STRIDE (oframe, 2);
456 gst_smpte_collected (GstCollectPads * pads, GstSMPTE * smpte)
460 GstBuffer *in1 = NULL, *in2 = NULL;
463 GstVideoFrame frame1, frame2, oframe;
465 if (G_UNLIKELY (smpte->fps_num == 0))
468 if (!gst_pad_has_current_caps (smpte->sinkpad1) ||
469 !gst_pad_has_current_caps (smpte->sinkpad2))
472 if (smpte->send_stream_start) {
475 /* stream-start (FIXME: create id based on input ids) */
476 g_snprintf (s_id, sizeof (s_id), "smpte-%08x", g_random_int ());
477 gst_pad_push_event (smpte->srcpad, gst_event_new_stream_start (s_id));
478 smpte->send_stream_start = FALSE;
481 ts = gst_util_uint64_scale_int (smpte->position * GST_SECOND,
482 smpte->fps_denom, smpte->fps_num);
484 for (collected = pads->data; collected; collected = g_slist_next (collected)) {
485 GstCollectData *data;
487 data = (GstCollectData *) collected->data;
489 if (data->pad == smpte->sinkpad1)
490 in1 = gst_collect_pads_pop (pads, data);
491 else if (data->pad == smpte->sinkpad2)
492 in2 = gst_collect_pads_pop (pads, data);
496 /* if no input, make picture black */
497 in1 = gst_buffer_new_and_alloc (I420_SIZE (smpte->width, smpte->height));
498 gst_buffer_map (in1, &map, GST_MAP_WRITE);
499 fill_i420 (map.data, smpte->width, smpte->height, 7);
500 gst_buffer_unmap (in1, &map);
503 /* if no input, make picture white */
504 in2 = gst_buffer_new_and_alloc (I420_SIZE (smpte->width, smpte->height));
505 gst_buffer_map (in2, &map, GST_MAP_WRITE);
506 fill_i420 (map.data, smpte->width, smpte->height, 0);
507 gst_buffer_unmap (in1, &map);
510 if (GST_VIDEO_INFO_WIDTH (&smpte->vinfo1) !=
511 GST_VIDEO_INFO_WIDTH (&smpte->vinfo2) ||
512 GST_VIDEO_INFO_HEIGHT (&smpte->vinfo1) !=
513 GST_VIDEO_INFO_HEIGHT (&smpte->vinfo2))
514 goto input_formats_do_not_match;
516 if (smpte->position < smpte->end_position) {
517 outbuf = gst_buffer_new_and_alloc (I420_SIZE (smpte->width, smpte->height));
519 /* set caps if not done yet */
520 if (!gst_pad_has_current_caps (smpte->srcpad)) {
525 gst_caps_make_writable (gst_static_caps_get
526 (&gst_smpte_src_template.static_caps));
527 gst_caps_set_simple (caps, "width", G_TYPE_INT, smpte->width, "height",
528 G_TYPE_INT, smpte->height, "framerate", GST_TYPE_FRACTION,
529 smpte->fps_num, smpte->fps_denom, NULL);
531 gst_pad_set_caps (smpte->srcpad, caps);
532 gst_caps_unref (caps);
534 gst_segment_init (&segment, GST_FORMAT_TIME);
535 gst_pad_push_event (smpte->srcpad, gst_event_new_segment (&segment));
538 gst_video_frame_map (&frame1, &smpte->vinfo1, in1, GST_MAP_READ);
539 gst_video_frame_map (&frame2, &smpte->vinfo2, in2, GST_MAP_READ);
540 /* re-use either info, now know they are essentially identical */
541 gst_video_frame_map (&oframe, &smpte->vinfo1, outbuf, GST_MAP_WRITE);
542 gst_smpte_blend_i420 (&frame1, &frame2, &oframe, smpte->mask, smpte->border,
543 ((1 << smpte->depth) + smpte->border) *
544 smpte->position / smpte->end_position);
545 gst_video_frame_unmap (&frame1);
546 gst_video_frame_unmap (&frame2);
547 gst_video_frame_unmap (&oframe);
550 gst_buffer_ref (in2);
556 gst_buffer_unref (in1);
558 gst_buffer_unref (in2);
560 GST_BUFFER_TIMESTAMP (outbuf) = ts;
562 return gst_pad_push (smpte->srcpad, outbuf);
567 GST_ELEMENT_ERROR (smpte, CORE, NEGOTIATION, (NULL),
568 ("No input format negotiated"));
569 return GST_FLOW_NOT_NEGOTIATED;
571 input_formats_do_not_match:
573 GstCaps *caps1, *caps2;
575 caps1 = gst_pad_get_current_caps (smpte->sinkpad1);
576 caps2 = gst_pad_get_current_caps (smpte->sinkpad2);
577 GST_ELEMENT_ERROR (smpte, CORE, NEGOTIATION, (NULL),
578 ("input formats don't match: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
580 gst_caps_unref (caps1);
581 gst_caps_unref (caps2);
582 return GST_FLOW_ERROR;
587 gst_smpte_set_property (GObject * object, guint prop_id,
588 const GValue * value, GParamSpec * pspec)
592 smpte = GST_SMPTE (object);
596 smpte->type = g_value_get_enum (value);
599 smpte->border = g_value_get_int (value);
602 smpte->depth = g_value_get_int (value);
605 smpte->duration = g_value_get_uint64 (value);
608 smpte->invert = g_value_get_boolean (value);
611 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
617 gst_smpte_get_property (GObject * object, guint prop_id,
618 GValue * value, GParamSpec * pspec)
622 smpte = GST_SMPTE (object);
626 g_value_set_enum (value, smpte->type);
629 g_value_set_int (value, smpte->border);
632 g_value_set_int (value, smpte->depth);
635 g_value_set_uint64 (value, smpte->duration);
638 g_value_set_boolean (value, smpte->invert);
641 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
646 static GstStateChangeReturn
647 gst_smpte_change_state (GstElement * element, GstStateChange transition)
649 GstStateChangeReturn ret;
652 smpte = GST_SMPTE (element);
654 switch (transition) {
655 case GST_STATE_CHANGE_READY_TO_PAUSED:
656 gst_smpte_reset (smpte);
657 GST_LOG_OBJECT (smpte, "starting collectpads");
658 gst_collect_pads_start (smpte->collect);
660 case GST_STATE_CHANGE_PAUSED_TO_READY:
661 GST_LOG_OBJECT (smpte, "stopping collectpads");
662 gst_collect_pads_stop (smpte->collect);
668 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
670 switch (transition) {
671 case GST_STATE_CHANGE_PAUSED_TO_READY:
672 gst_smpte_reset (smpte);
681 gst_smpte_plugin_init (GstPlugin * plugin)
683 GST_DEBUG_CATEGORY_INIT (gst_smpte_debug, "smpte", 0,
684 "SMPTE transition effect");
686 return gst_element_register (plugin, "smpte", GST_RANK_NONE, GST_TYPE_SMPTE);