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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 -v videotestsrc pattern=1 ! smpte name=s border=20000 type=234 duration=2000000000 ! ffmpegcolorspace ! 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.
47 #include <gst/video/video.h>
50 GST_DEBUG_CATEGORY_STATIC (gst_smpte_debug);
51 #define GST_CAT_DEFAULT gst_smpte_debug
53 static GstStaticPadTemplate gst_smpte_src_template =
54 GST_STATIC_PAD_TEMPLATE ("src",
57 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420")
61 static GstStaticPadTemplate gst_smpte_sink1_template =
62 GST_STATIC_PAD_TEMPLATE ("sink1",
65 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420")
69 static GstStaticPadTemplate gst_smpte_sink2_template =
70 GST_STATIC_PAD_TEMPLATE ("sink2",
73 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420")
78 /* SMPTE signals and args */
85 #define DEFAULT_PROP_TYPE 1
86 #define DEFAULT_PROP_BORDER 0
87 #define DEFAULT_PROP_DEPTH 16
88 #define DEFAULT_PROP_FPS 0.
89 #define DEFAULT_PROP_DURATION GST_SECOND
90 #define DEFAULT_PROP_INVERT FALSE
104 #define I420_Y_ROWSTRIDE(width) (GST_ROUND_UP_4(width))
105 #define I420_U_ROWSTRIDE(width) (GST_ROUND_UP_8(width)/2)
106 #define I420_V_ROWSTRIDE(width) ((GST_ROUND_UP_8(I420_Y_ROWSTRIDE(width)))/2)
108 #define I420_Y_OFFSET(w,h) (0)
109 #define I420_U_OFFSET(w,h) (I420_Y_OFFSET(w,h)+(I420_Y_ROWSTRIDE(w)*GST_ROUND_UP_2(h)))
110 #define I420_V_OFFSET(w,h) (I420_U_OFFSET(w,h)+(I420_U_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
112 #define I420_SIZE(w,h) (I420_V_OFFSET(w,h)+(I420_V_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
115 #define GST_TYPE_SMPTE_TRANSITION_TYPE (gst_smpte_transition_type_get_type())
117 gst_smpte_transition_type_get_type (void)
119 static GType smpte_transition_type = 0;
120 GEnumValue *smpte_transitions;
122 if (!smpte_transition_type) {
123 const GList *definitions;
126 definitions = gst_mask_get_definitions ();
128 g_new0 (GEnumValue, g_list_length ((GList *) definitions) + 1);
130 while (definitions) {
131 GstMaskDefinition *definition = (GstMaskDefinition *) definitions->data;
133 definitions = g_list_next (definitions);
135 smpte_transitions[i].value = definition->type;
136 /* older GLib versions have the two fields as non-const, hence the cast */
137 smpte_transitions[i].value_nick = (gchar *) definition->short_name;
138 smpte_transitions[i].value_name = (gchar *) definition->long_name;
143 smpte_transition_type =
144 g_enum_register_static ("GstSMPTETransitionType", smpte_transitions);
146 return smpte_transition_type;
150 static void gst_smpte_class_init (GstSMPTEClass * klass);
151 static void gst_smpte_base_init (GstSMPTEClass * klass);
152 static void gst_smpte_init (GstSMPTE * smpte);
153 static void gst_smpte_finalize (GstSMPTE * smpte);
155 static GstFlowReturn gst_smpte_collected (GstCollectPads2 * pads,
158 static void gst_smpte_set_property (GObject * object, guint prop_id,
159 const GValue * value, GParamSpec * pspec);
160 static void gst_smpte_get_property (GObject * object, guint prop_id,
161 GValue * value, GParamSpec * pspec);
163 static GstStateChangeReturn gst_smpte_change_state (GstElement * element,
164 GstStateChange transition);
166 static GstElementClass *parent_class = NULL;
168 /*static guint gst_smpte_signals[LAST_SIGNAL] = { 0 }; */
171 gst_smpte_get_type (void)
173 static GType smpte_type = 0;
176 static const GTypeInfo smpte_info = {
177 sizeof (GstSMPTEClass),
178 (GBaseInitFunc) gst_smpte_base_init,
180 (GClassInitFunc) gst_smpte_class_init,
185 (GInstanceInitFunc) gst_smpte_init,
189 g_type_register_static (GST_TYPE_ELEMENT, "GstSMPTE", &smpte_info, 0);
195 gst_smpte_base_init (GstSMPTEClass * klass)
197 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
199 gst_element_class_add_pad_template (element_class,
200 gst_static_pad_template_get (&gst_smpte_sink1_template));
201 gst_element_class_add_pad_template (element_class,
202 gst_static_pad_template_get (&gst_smpte_sink2_template));
203 gst_element_class_add_pad_template (element_class,
204 gst_static_pad_template_get (&gst_smpte_src_template));
205 gst_element_class_set_details_simple (element_class, "SMPTE transitions",
206 "Filter/Editor/Video",
207 "Apply the standard SMPTE transitions on video images",
208 "Wim Taymans <wim.taymans@chello.be>");
212 gst_smpte_class_init (GstSMPTEClass * klass)
214 GObjectClass *gobject_class;
215 GstElementClass *gstelement_class;
217 gobject_class = (GObjectClass *) klass;
218 gstelement_class = (GstElementClass *) klass;
220 parent_class = g_type_class_peek_parent (klass);
222 gobject_class->set_property = gst_smpte_set_property;
223 gobject_class->get_property = gst_smpte_get_property;
224 gobject_class->finalize = (GObjectFinalizeFunc) gst_smpte_finalize;
228 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TYPE,
229 g_param_spec_enum ("type", "Type", "The type of transition to use",
230 GST_TYPE_SMPTE_TRANSITION_TYPE, DEFAULT_PROP_TYPE,
231 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FPS,
233 g_param_spec_float ("fps", "FPS",
234 "Frames per second if no input files are given (deprecated)", 0.,
235 G_MAXFLOAT, DEFAULT_PROP_FPS,
236 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BORDER,
238 g_param_spec_int ("border", "Border",
239 "The border width of the transition", 0, G_MAXINT,
240 DEFAULT_PROP_BORDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DEPTH,
242 g_param_spec_int ("depth", "Depth", "Depth of the mask in bits", 1, 24,
243 DEFAULT_PROP_DEPTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DURATION,
245 g_param_spec_uint64 ("duration", "Duration",
246 "Duration of the transition effect in nanoseconds", 0, G_MAXUINT64,
247 DEFAULT_PROP_DURATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_INVERT,
249 g_param_spec_boolean ("invert", "Invert",
250 "Invert transition mask", DEFAULT_PROP_INVERT,
251 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
253 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_smpte_change_state);
256 /* wht yel cya grn mag red blu blk -I Q */
257 static const int y_colors[] = { 255, 226, 179, 150, 105, 76, 29, 16, 16, 0 };
258 static const int u_colors[] = { 128, 0, 170, 46, 212, 85, 255, 128, 0, 128 };
259 static const int v_colors[] = { 128, 155, 0, 21, 235, 255, 107, 128, 128, 255 };
262 fill_i420 (guint8 * data, gint width, gint height, gint color)
264 gint size = I420_Y_ROWSTRIDE (width) * GST_ROUND_UP_2 (height);
265 gint size4 = size >> 2;
267 guint8 *up = data + I420_U_OFFSET (width, height);
268 guint8 *vp = data + I420_V_OFFSET (width, height);
270 memset (yp, y_colors[color], size);
271 memset (up, u_colors[color], size4);
272 memset (vp, v_colors[color], size4);
276 gst_smpte_update_mask (GstSMPTE * smpte, gint type, gboolean invert,
277 gint depth, gint width, gint height)
282 if (smpte->type == type &&
283 smpte->invert == invert &&
284 smpte->depth == depth &&
285 smpte->width == width && smpte->height == height)
289 newmask = gst_mask_factory_new (type, invert, depth, width, height);
292 gst_mask_destroy (smpte->mask);
294 smpte->mask = newmask;
296 smpte->invert = invert;
297 smpte->depth = depth;
298 smpte->width = width;
299 smpte->height = height;
307 gst_smpte_setcaps (GstPad * pad, GstCaps * caps)
310 GstStructure *structure;
313 smpte = GST_SMPTE (GST_PAD_PARENT (pad));
315 structure = gst_caps_get_structure (caps, 0);
317 ret = gst_structure_get_int (structure, "width", &smpte->width);
318 ret &= gst_structure_get_int (structure, "height", &smpte->height);
319 ret &= gst_structure_get_fraction (structure, "framerate",
320 &smpte->fps_num, &smpte->fps_denom);
324 /* for backward compat, we store these here */
325 smpte->fps = ((gdouble) smpte->fps_num) / smpte->fps_denom;
327 /* figure out the duration in frames */
328 smpte->end_position = gst_util_uint64_scale (smpte->duration,
329 smpte->fps_num, GST_SECOND * smpte->fps_denom);
331 GST_DEBUG_OBJECT (smpte, "duration: %d frames", smpte->end_position);
334 gst_smpte_update_mask (smpte, smpte->type, smpte->invert, smpte->depth,
335 smpte->width, smpte->height);
341 gst_smpte_init (GstSMPTE * smpte)
344 gst_pad_new_from_static_template (&gst_smpte_sink1_template, "sink1");
345 gst_pad_set_setcaps_function (smpte->sinkpad1,
346 GST_DEBUG_FUNCPTR (gst_smpte_setcaps));
347 gst_pad_set_getcaps_function (smpte->sinkpad1,
348 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
349 gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad1);
352 gst_pad_new_from_static_template (&gst_smpte_sink2_template, "sink2");
353 gst_pad_set_setcaps_function (smpte->sinkpad2,
354 GST_DEBUG_FUNCPTR (gst_smpte_setcaps));
355 gst_pad_set_getcaps_function (smpte->sinkpad2,
356 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
357 gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad2);
360 gst_pad_new_from_static_template (&gst_smpte_src_template, "src");
361 gst_element_add_pad (GST_ELEMENT (smpte), smpte->srcpad);
363 smpte->collect = gst_collect_pads2_new ();
364 gst_collect_pads2_set_function (smpte->collect,
365 (GstCollectPads2Function) GST_DEBUG_FUNCPTR (gst_smpte_collected), smpte);
366 gst_collect_pads2_start (smpte->collect);
368 gst_collect_pads2_add_pad (smpte->collect, smpte->sinkpad1,
369 sizeof (GstCollectData2));
370 gst_collect_pads2_add_pad (smpte->collect, smpte->sinkpad2,
371 sizeof (GstCollectData2));
373 smpte->fps = DEFAULT_PROP_FPS;
374 smpte->type = DEFAULT_PROP_TYPE;
375 smpte->border = DEFAULT_PROP_BORDER;
376 smpte->depth = DEFAULT_PROP_DEPTH;
377 smpte->duration = DEFAULT_PROP_DURATION;
378 smpte->invert = DEFAULT_PROP_INVERT;
380 smpte->fps_denom = 1;
384 gst_smpte_finalize (GstSMPTE * smpte)
386 if (smpte->collect) {
387 gst_object_unref (smpte->collect);
390 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) smpte);
394 gst_smpte_reset (GstSMPTE * smpte)
399 smpte->end_position = 0;
403 gst_smpte_blend_i420 (guint8 * in1, guint8 * in2, guint8 * out, GstMask * mask,
404 gint width, gint height, gint border, gint pos)
410 guint8 *in1u, *in1v, *in2u, *in2v, *outu, *outv;
411 gint lumsize = width * height;
412 gint chromsize = lumsize >> 2;
420 in1u = in1 + lumsize;
421 in1v = in1u + chromsize;
422 in2u = in2 + lumsize;
423 in2v = in2u + chromsize;
424 outu = out + lumsize;
425 outv = outu + chromsize;
429 for (i = 0; i < height; i++) {
430 for (j = 0; j < width; j++) {
432 value = ((CLAMP (value, min, max) - min) << 8) / border;
434 *out++ = ((*in1++ * value) + (*in2++ * (256 - value))) >> 8;
435 if (!(i & 1) && !(j & 1)) {
436 *outu++ = ((*in1u++ * value) + (*in2u++ * (256 - value))) >> 8;
437 *outv++ = ((*in1v++ * value) + (*in2v++ * (256 - value))) >> 8;
444 gst_smpte_collected (GstCollectPads2 * pads, GstSMPTE * smpte)
448 GstBuffer *in1 = NULL, *in2 = NULL;
451 if (G_UNLIKELY (smpte->fps_num == 0))
454 if (!GST_PAD_CAPS (smpte->sinkpad1) || !GST_PAD_CAPS (smpte->sinkpad2))
457 ts = gst_util_uint64_scale_int (smpte->position * GST_SECOND,
458 smpte->fps_denom, smpte->fps_num);
460 for (collected = pads->data; collected; collected = g_slist_next (collected)) {
461 GstCollectData2 *data;
463 data = (GstCollectData2 *) collected->data;
465 if (data->pad == smpte->sinkpad1)
466 in1 = gst_collect_pads2_pop (pads, data);
467 else if (data->pad == smpte->sinkpad2)
468 in2 = gst_collect_pads2_pop (pads, data);
472 /* if no input, make picture black */
473 in1 = gst_buffer_new_and_alloc (I420_SIZE (smpte->width, smpte->height));
474 fill_i420 (GST_BUFFER_DATA (in1), smpte->width, smpte->height, 7);
477 /* if no input, make picture white */
478 in2 = gst_buffer_new_and_alloc (I420_SIZE (smpte->width, smpte->height));
479 fill_i420 (GST_BUFFER_DATA (in2), smpte->width, smpte->height, 0);
482 if (GST_BUFFER_SIZE (in1) != GST_BUFFER_SIZE (in2))
483 goto input_formats_do_not_match;
485 if (smpte->position < smpte->end_position) {
486 outbuf = gst_buffer_new_and_alloc (I420_SIZE (smpte->width, smpte->height));
488 /* set caps if not done yet */
489 if (!GST_PAD_CAPS (smpte->srcpad)) {
493 gst_caps_copy (gst_static_caps_get
494 (&gst_smpte_src_template.static_caps));
495 gst_caps_set_simple (caps, "width", G_TYPE_INT, smpte->width, "height",
496 G_TYPE_INT, smpte->height, "framerate", GST_TYPE_FRACTION,
497 smpte->fps_num, smpte->fps_denom, NULL);
499 gst_pad_set_caps (smpte->srcpad, caps);
501 gst_pad_push_event (smpte->srcpad,
502 gst_event_new_new_segment_full (FALSE,
503 1.0, 1.0, GST_FORMAT_TIME, 0, -1, 0));
506 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (smpte->srcpad));
508 gst_smpte_blend_i420 (GST_BUFFER_DATA (in1),
509 GST_BUFFER_DATA (in2),
510 GST_BUFFER_DATA (outbuf),
511 smpte->mask, smpte->width, smpte->height,
513 ((1 << smpte->depth) + smpte->border) *
514 smpte->position / smpte->end_position);
518 gst_buffer_ref (in2);
524 gst_buffer_unref (in1);
526 gst_buffer_unref (in2);
528 GST_BUFFER_TIMESTAMP (outbuf) = ts;
530 return gst_pad_push (smpte->srcpad, outbuf);
535 GST_ELEMENT_ERROR (smpte, CORE, NEGOTIATION, (NULL),
536 ("No input format negotiated"));
537 return GST_FLOW_NOT_NEGOTIATED;
539 input_formats_do_not_match:
541 GST_ELEMENT_ERROR (smpte, CORE, NEGOTIATION, (NULL),
542 ("input formats don't match: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
543 GST_PAD_CAPS (smpte->sinkpad1), GST_PAD_CAPS (smpte->sinkpad2)));
544 return GST_FLOW_ERROR;
549 gst_smpte_set_property (GObject * object, guint prop_id,
550 const GValue * value, GParamSpec * pspec)
554 smpte = GST_SMPTE (object);
558 smpte->type = g_value_get_enum (value);
561 smpte->border = g_value_get_int (value);
564 smpte->fps = g_value_get_float (value);
567 smpte->depth = g_value_get_int (value);
570 smpte->duration = g_value_get_uint64 (value);
573 smpte->invert = g_value_get_boolean (value);
576 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
582 gst_smpte_get_property (GObject * object, guint prop_id,
583 GValue * value, GParamSpec * pspec)
587 smpte = GST_SMPTE (object);
591 g_value_set_enum (value, smpte->type);
594 g_value_set_float (value, smpte->fps);
597 g_value_set_int (value, smpte->border);
600 g_value_set_int (value, smpte->depth);
603 g_value_set_uint64 (value, smpte->duration);
606 g_value_set_boolean (value, smpte->invert);
609 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
614 static GstStateChangeReturn
615 gst_smpte_change_state (GstElement * element, GstStateChange transition)
617 GstStateChangeReturn ret;
620 smpte = GST_SMPTE (element);
622 switch (transition) {
623 case GST_STATE_CHANGE_READY_TO_PAUSED:
624 gst_smpte_reset (smpte);
625 GST_LOG_OBJECT (smpte, "starting collectpads");
626 gst_collect_pads2_start (smpte->collect);
628 case GST_STATE_CHANGE_PAUSED_TO_READY:
629 GST_LOG_OBJECT (smpte, "stopping collectpads");
630 gst_collect_pads2_stop (smpte->collect);
636 ret = parent_class->change_state (element, transition);
638 switch (transition) {
639 case GST_STATE_CHANGE_PAUSED_TO_READY:
640 gst_smpte_reset (smpte);
649 gst_smpte_plugin_init (GstPlugin * plugin)
651 GST_DEBUG_CATEGORY_INIT (gst_smpte_debug, "smpte", 0,
652 "SMPTE transition effect");
654 return gst_element_register (plugin, "smpte", GST_RANK_NONE, GST_TYPE_SMPTE);