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.
25 #include <gst/video/video.h>
28 /* elementfactory information */
29 static GstElementDetails smpte_details = {
31 "Filter/Editor/Video",
32 "Apply the standard SMPTE transitions on video images",
33 "Wim Taymans <wim.taymans@chello.be>"
36 static GstStaticPadTemplate gst_smpte_src_template =
37 GST_STATIC_PAD_TEMPLATE (
42 GST_VIDEO_CAPS_YUV("I420")
46 static GstStaticPadTemplate gst_smpte_sink1_template =
47 GST_STATIC_PAD_TEMPLATE (
52 GST_VIDEO_CAPS_YUV("I420")
56 static GstStaticPadTemplate gst_smpte_sink2_template =
57 GST_STATIC_PAD_TEMPLATE (
62 GST_VIDEO_CAPS_YUV("I420")
67 /* SMPTE signals and args */
81 #define GST_TYPE_SMPTE_TRANSITION_TYPE (gst_smpte_transition_type_get_type())
83 gst_smpte_transition_type_get_type (void)
85 static GType smpte_transition_type = 0;
86 GEnumValue *smpte_transitions;
88 if (!smpte_transition_type) {
89 const GList *definitions;
92 definitions = gst_mask_get_definitions ();
93 smpte_transitions = g_new0 (GEnumValue, g_list_length ((GList *)definitions)+1);
96 GstMaskDefinition *definition = (GstMaskDefinition *) definitions->data;
97 definitions = g_list_next (definitions);
99 smpte_transitions[i].value = definition->type;
100 smpte_transitions[i].value_name = definition->short_name;
101 smpte_transitions[i].value_nick = definition->long_name;
106 smpte_transition_type =
107 g_enum_register_static ("GstSMPTETransitionType", smpte_transitions);
109 return smpte_transition_type;
113 static void gst_smpte_class_init (GstSMPTEClass *klass);
114 static void gst_smpte_base_init (GstSMPTEClass *klass);
115 static void gst_smpte_init (GstSMPTE *smpte);
117 static void gst_smpte_loop (GstElement *element);
119 static void gst_smpte_set_property (GObject *object, guint prop_id,
120 const GValue *value, GParamSpec *pspec);
121 static void gst_smpte_get_property (GObject *object, guint prop_id,
122 GValue *value, GParamSpec *pspec);
124 static GstElementClass *parent_class = NULL;
125 /*static guint gst_smpte_signals[LAST_SIGNAL] = { 0 }; */
128 gst_smpte_get_type (void)
130 static GType smpte_type = 0;
133 static const GTypeInfo smpte_info = {
134 sizeof(GstSMPTEClass),
135 (GBaseInitFunc)gst_smpte_base_init,
137 (GClassInitFunc)gst_smpte_class_init,
142 (GInstanceInitFunc)gst_smpte_init,
144 smpte_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSMPTE", &smpte_info, 0);
150 gst_smpte_base_init (GstSMPTEClass *klass)
152 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
154 gst_element_class_add_pad_template (element_class,
155 gst_static_pad_template_get(&gst_smpte_sink1_template));
156 gst_element_class_add_pad_template (element_class,
157 gst_static_pad_template_get(&gst_smpte_sink2_template));
158 gst_element_class_add_pad_template (element_class,
159 gst_static_pad_template_get(&gst_smpte_src_template));
160 gst_element_class_set_details (element_class, &smpte_details);
164 gst_smpte_class_init (GstSMPTEClass *klass)
166 GObjectClass *gobject_class;
167 GstElementClass *gstelement_class;
169 gobject_class = (GObjectClass*)klass;
170 gstelement_class = (GstElementClass*)klass;
172 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
174 gobject_class->set_property = gst_smpte_set_property;
175 gobject_class->get_property = gst_smpte_get_property;
179 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TYPE,
180 g_param_spec_enum ("type", "Type", "The type of transition to use",
181 GST_TYPE_SMPTE_TRANSITION_TYPE, 1, G_PARAM_READWRITE));
182 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FPS,
183 g_param_spec_float ("fps", "FPS", "Frames per second if no input files are given",
184 0., G_MAXFLOAT, 25., G_PARAM_READWRITE));
185 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BORDER,
186 g_param_spec_int ("border", "Border", "The border width of the transition",
187 0, G_MAXINT, 0, G_PARAM_READWRITE));
188 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEPTH,
189 g_param_spec_int ("depth", "Depth", "Depth of the mask in bits",
190 1, 24, 16, G_PARAM_READWRITE));
193 /* wht yel cya grn mag red blu blk -I Q */
194 static int y_colors[] = { 255, 226, 179, 150, 105, 76, 29, 16, 16, 0 };
195 static int u_colors[] = { 128, 0, 170, 46, 212, 85, 255, 128, 0, 128 };
196 static int v_colors[] = { 128, 155, 0, 21, 235, 255, 107, 128, 128, 255 };
199 fill_i420 (guint8 *data, gint width, gint height, gint color)
201 gint size = width * height, size4 = size >> 2;
203 guint8 *up = data + size;
204 guint8 *vp = data + size + size4;
206 memset (yp, y_colors[color], size);
207 memset (up, u_colors[color], size4);
208 memset (vp, v_colors[color], size4);
212 gst_smpte_update_mask (GstSMPTE *smpte, gint type, gint depth, gint width, gint height)
216 newmask = gst_mask_factory_new (type, depth, width, height);
219 gst_mask_destroy (smpte->mask);
221 smpte->mask = newmask;
223 smpte->depth = depth;
224 smpte->width = width;
225 smpte->height = height;
233 gst_smpte_sinkconnect (GstPad *pad, const GstCaps *caps)
236 GstStructure *structure;
239 smpte = GST_SMPTE (gst_pad_get_parent (pad));
241 structure = gst_caps_get_structure (caps, 0);
243 ret = gst_structure_get_int (structure, "width", &smpte->width);
244 ret &= gst_structure_get_int (structure, "height", &smpte->height);
245 ret &= gst_structure_get_double (structure, "framerate", &smpte->fps);
246 if (!ret) return GST_PAD_LINK_REFUSED;
248 gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width, smpte->height);
250 /* forward to the next plugin */
251 return gst_pad_try_set_caps(smpte->srcpad, caps);
255 gst_smpte_init (GstSMPTE *smpte)
257 smpte->sinkpad1 = gst_pad_new_from_template (
258 gst_static_pad_template_get(&gst_smpte_sink1_template), "sink1");
259 gst_pad_set_link_function (smpte->sinkpad1, gst_smpte_sinkconnect);
260 gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad1);
262 smpte->sinkpad2 = gst_pad_new_from_template (
263 gst_static_pad_template_get(&gst_smpte_sink2_template), "sink2");
264 gst_pad_set_link_function (smpte->sinkpad2, gst_smpte_sinkconnect);
265 gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad2);
267 smpte->srcpad = gst_pad_new_from_template (
268 gst_static_pad_template_get(&gst_smpte_src_template), "src");
269 gst_element_add_pad (GST_ELEMENT (smpte), smpte->srcpad);
271 gst_element_set_loop_function (GST_ELEMENT (smpte), gst_smpte_loop);
276 smpte->duration = 64;
281 gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width, smpte->height);
285 gst_smpte_blend_i420 (guint8 *in1, guint8 *in2, guint8 *out, GstMask *mask,
286 gint width, gint height, gint border, gint pos)
292 guint8 *in1u, *in1v, *in2u, *in2v, *outu, *outv;
293 gint lumsize = width * height;
294 gint chromsize = lumsize >> 2;
296 if (border == 0) border++;
301 in1u = in1 + lumsize; in1v = in1u + chromsize;
302 in2u = in2 + lumsize; in2v = in2u + chromsize;
303 outu = out + lumsize; outv = outu + chromsize;
307 for (i = 0; i < height; i++) {
308 for (j = 0; j < width; j++) {
310 value = ((CLAMP (value, min, max) - min) << 8) / border;
312 *out++ = ((*in1++ * value) + (*in2++ * (256 - value))) >> 8;
313 if (!(i & 1) && !(j & 1)) {
314 *outu++ = ((*in1u++ * value) + (*in2u++ * (256 - value))) >> 8;
315 *outv++ = ((*in1v++ * value) + (*in2v++ * (256 - value))) >> 8;
322 gst_smpte_loop (GstElement *element)
327 GstBuffer *in1 = NULL, *in2 = NULL;
329 smpte = GST_SMPTE (element);
331 ts = smpte->position * GST_SECOND / smpte->fps;
333 while (GST_PAD_IS_USABLE (smpte->sinkpad1) && in1 == NULL) {
334 in1 = GST_BUFFER (gst_pad_pull (smpte->sinkpad1));
335 if (GST_IS_EVENT (in1)) {
336 gst_pad_push (smpte->srcpad, GST_DATA (in1));
340 ts = GST_BUFFER_TIMESTAMP (in1);
342 if (GST_PAD_IS_USABLE (smpte->sinkpad2) && in2 == NULL) {
343 in2 = GST_BUFFER (gst_pad_pull (smpte->sinkpad2));
344 if (GST_IS_EVENT (in2)) {
345 gst_pad_push (smpte->srcpad, GST_DATA (in2));
349 ts = GST_BUFFER_TIMESTAMP (in2);
353 in1 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
354 fill_i420 (GST_BUFFER_DATA (in1), smpte->width, smpte->height, 7);
357 in2 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
358 fill_i420 (GST_BUFFER_DATA (in2), smpte->width, smpte->height, 0);
361 if (smpte->position < smpte->duration) {
362 outbuf = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
364 if (!GST_PAD_CAPS (smpte->srcpad)) {
366 caps = gst_caps_copy (gst_static_caps_get (
367 &gst_smpte_src_template.static_caps));
368 gst_caps_set_simple (caps,
369 "width", G_TYPE_INT, smpte->width,
370 "height", G_TYPE_INT, smpte->height,
371 "framerate", G_TYPE_DOUBLE, smpte->fps, NULL);
373 if (!gst_pad_try_set_caps (smpte->srcpad, caps)) {
374 GST_ELEMENT_ERROR (smpte, CORE, NEGOTIATION, (NULL), (NULL));
379 gst_smpte_blend_i420 (GST_BUFFER_DATA (in1),
380 GST_BUFFER_DATA (in2),
381 GST_BUFFER_DATA (outbuf),
382 smpte->mask, smpte->width, smpte->height,
384 ((1 << smpte->depth) + smpte->border) *
385 smpte->position / smpte->duration);
389 gst_buffer_ref (in2);
395 gst_buffer_unref (in1);
397 gst_buffer_unref (in2);
399 GST_BUFFER_TIMESTAMP (outbuf) = ts;
400 gst_pad_push (smpte->srcpad, GST_DATA (outbuf));
404 gst_smpte_set_property (GObject *object, guint prop_id,
405 const GValue *value, GParamSpec *pspec)
409 smpte = GST_SMPTE(object);
414 gint type = g_value_get_enum (value);
416 gst_smpte_update_mask (smpte, type, smpte->depth,
417 smpte->width, smpte->height);
421 smpte->border = g_value_get_int (value);
424 smpte->fps = g_value_get_float (value);
428 gint depth = g_value_get_int (value);
430 gst_smpte_update_mask (smpte, smpte->type, depth,
431 smpte->width, smpte->height);
435 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
441 gst_smpte_get_property (GObject *object, guint prop_id,
442 GValue *value, GParamSpec *pspec)
446 smpte = GST_SMPTE(object);
451 g_value_set_enum (value, smpte->mask->type);
455 g_value_set_float (value, smpte->fps);
458 g_value_set_int (value, smpte->border);
461 g_value_set_int (value, smpte->depth);
464 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
471 plugin_init (GstPlugin *plugin)
473 return gst_element_register(plugin, "smpte",
474 GST_RANK_NONE, GST_TYPE_SMPTE);
481 "Apply the standard SMPTE transitions on video images",