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 GST_PAD_TEMPLATE_FACTORY (smpte_src_factory,
43 GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
44 GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
48 GST_PAD_TEMPLATE_FACTORY (smpte_sink1_factory,
55 GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
56 GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
60 GST_PAD_TEMPLATE_FACTORY (smpte_sink2_factory,
67 GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
68 GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
73 /* SMPTE signals and args */
87 #define GST_TYPE_SMPTE_TRANSITION_TYPE (gst_smpte_transition_type_get_type())
89 gst_smpte_transition_type_get_type (void)
91 static GType smpte_transition_type = 0;
92 GEnumValue *smpte_transitions;
94 if (!smpte_transition_type) {
95 const GList *definitions;
98 definitions = gst_mask_get_definitions ();
99 smpte_transitions = g_new0 (GEnumValue, g_list_length ((GList *)definitions)+1);
101 while (definitions) {
102 GstMaskDefinition *definition = (GstMaskDefinition *) definitions->data;
103 definitions = g_list_next (definitions);
105 smpte_transitions[i].value = definition->type;
106 smpte_transitions[i].value_name = definition->short_name;
107 smpte_transitions[i].value_nick = definition->long_name;
112 smpte_transition_type =
113 g_enum_register_static ("GstSMPTETransitionType", smpte_transitions);
115 return smpte_transition_type;
119 static void gst_smpte_class_init (GstSMPTEClass *klass);
120 static void gst_smpte_base_init (GstSMPTEClass *klass);
121 static void gst_smpte_init (GstSMPTE *smpte);
123 static void gst_smpte_loop (GstElement *element);
125 static void gst_smpte_set_property (GObject *object, guint prop_id,
126 const GValue *value, GParamSpec *pspec);
127 static void gst_smpte_get_property (GObject *object, guint prop_id,
128 GValue *value, GParamSpec *pspec);
130 static GstElementClass *parent_class = NULL;
131 /*static guint gst_smpte_signals[LAST_SIGNAL] = { 0 }; */
134 gst_smpte_get_type (void)
136 static GType smpte_type = 0;
139 static const GTypeInfo smpte_info = {
140 sizeof(GstSMPTEClass),
141 (GBaseInitFunc)gst_smpte_base_init,
143 (GClassInitFunc)gst_smpte_class_init,
148 (GInstanceInitFunc)gst_smpte_init,
150 smpte_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSMPTE", &smpte_info, 0);
156 gst_smpte_base_init (GstSMPTEClass *klass)
158 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
160 gst_element_class_add_pad_template (element_class,
161 GST_PAD_TEMPLATE_GET (smpte_sink1_factory));
162 gst_element_class_add_pad_template (element_class,
163 GST_PAD_TEMPLATE_GET (smpte_sink2_factory));
164 gst_element_class_add_pad_template (element_class,
165 GST_PAD_TEMPLATE_GET (smpte_src_factory));
166 gst_element_class_set_details (element_class, &smpte_details);
170 gst_smpte_class_init (GstSMPTEClass *klass)
172 GObjectClass *gobject_class;
173 GstElementClass *gstelement_class;
175 gobject_class = (GObjectClass*)klass;
176 gstelement_class = (GstElementClass*)klass;
178 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
180 gobject_class->set_property = gst_smpte_set_property;
181 gobject_class->get_property = gst_smpte_get_property;
185 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TYPE,
186 g_param_spec_enum ("type", "Type", "The type of transition to use",
187 GST_TYPE_SMPTE_TRANSITION_TYPE, 1, G_PARAM_READWRITE));
188 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FPS,
189 g_param_spec_float ("fps", "FPS", "Frames per second if no input files are given",
190 0., G_MAXFLOAT, 25., G_PARAM_READWRITE));
191 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BORDER,
192 g_param_spec_int ("border", "Border", "The border width of the transition",
193 0, G_MAXINT, 0, G_PARAM_READWRITE));
194 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEPTH,
195 g_param_spec_int ("depth", "Depth", "Depth of the mask in bits",
196 1, 24, 16, G_PARAM_READWRITE));
199 /* wht yel cya grn mag red blu blk -I Q */
200 static int y_colors[] = { 255, 226, 179, 150, 105, 76, 29, 16, 16, 0 };
201 static int u_colors[] = { 128, 0, 170, 46, 212, 85, 255, 128, 0, 128 };
202 static int v_colors[] = { 128, 155, 0, 21, 235, 255, 107, 128, 128, 255 };
205 fill_i420 (guint8 *data, gint width, gint height, gint color)
207 gint size = width * height, size4 = size >> 2;
209 guint8 *up = data + size;
210 guint8 *vp = data + size + size4;
212 memset (yp, y_colors[color], size);
213 memset (up, u_colors[color], size4);
214 memset (vp, v_colors[color], size4);
218 gst_smpte_update_mask (GstSMPTE *smpte, gint type, gint depth, gint width, gint height)
222 newmask = gst_mask_factory_new (type, depth, width, height);
225 gst_mask_destroy (smpte->mask);
227 smpte->mask = newmask;
229 smpte->depth = depth;
230 smpte->width = width;
231 smpte->height = height;
239 gst_smpte_sinkconnect (GstPad *pad, GstCaps *caps)
243 smpte = GST_SMPTE (gst_pad_get_parent (pad));
245 if (!GST_CAPS_IS_FIXED (caps))
246 return GST_PAD_LINK_DELAYED;
248 gst_caps_get_int (caps, "width", &smpte->width);
249 gst_caps_get_int (caps, "height", &smpte->height);
250 gst_caps_get_float (caps, "framerate", &smpte->fps);
252 gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width, smpte->height);
254 /* forward to the next plugin */
255 return gst_pad_try_set_caps(smpte->srcpad, gst_caps_copy_1(caps));
259 gst_smpte_init (GstSMPTE *smpte)
261 smpte->sinkpad1 = gst_pad_new_from_template (
262 GST_PAD_TEMPLATE_GET (smpte_sink1_factory), "sink1");
263 gst_pad_set_link_function (smpte->sinkpad1, gst_smpte_sinkconnect);
264 gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad1);
266 smpte->sinkpad2 = gst_pad_new_from_template (
267 GST_PAD_TEMPLATE_GET (smpte_sink2_factory), "sink2");
268 gst_pad_set_link_function (smpte->sinkpad2, gst_smpte_sinkconnect);
269 gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad2);
271 smpte->srcpad = gst_pad_new_from_template (
272 GST_PAD_TEMPLATE_GET (smpte_src_factory), "src");
273 gst_element_add_pad (GST_ELEMENT (smpte), smpte->srcpad);
275 gst_element_set_loop_function (GST_ELEMENT (smpte), gst_smpte_loop);
280 smpte->duration = 64;
285 gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width, smpte->height);
289 gst_smpte_blend_i420 (guint8 *in1, guint8 *in2, guint8 *out, GstMask *mask,
290 gint width, gint height, gint border, gint pos)
296 guint8 *in1u, *in1v, *in2u, *in2v, *outu, *outv;
297 gint lumsize = width * height;
298 gint chromsize = lumsize >> 2;
300 if (border == 0) border++;
305 in1u = in1 + lumsize; in1v = in1u + chromsize;
306 in2u = in2 + lumsize; in2v = in2u + chromsize;
307 outu = out + lumsize; outv = outu + chromsize;
311 for (i = 0; i < height; i++) {
312 for (j = 0; j < width; j++) {
314 value = ((CLAMP (value, min, max) - min) << 8) / border;
316 *out++ = ((*in1++ * value) + (*in2++ * (256 - value))) >> 8;
317 if (!(i & 1) && !(j & 1)) {
318 *outu++ = ((*in1u++ * value) + (*in2u++ * (256 - value))) >> 8;
319 *outv++ = ((*in1v++ * value) + (*in2v++ * (256 - value))) >> 8;
326 gst_smpte_loop (GstElement *element)
331 GstBuffer *in1 = NULL, *in2 = NULL;
333 smpte = GST_SMPTE (element);
335 ts = smpte->position * GST_SECOND / smpte->fps;
337 while (GST_PAD_IS_USABLE (smpte->sinkpad1) && in1 == NULL) {
338 in1 = GST_BUFFER (gst_pad_pull (smpte->sinkpad1));
339 if (GST_IS_EVENT (in1)) {
340 gst_pad_push (smpte->srcpad, GST_DATA (in1));
344 ts = GST_BUFFER_TIMESTAMP (in1);
346 if (GST_PAD_IS_USABLE (smpte->sinkpad2) && in2 == NULL) {
347 in2 = GST_BUFFER (gst_pad_pull (smpte->sinkpad2));
348 if (GST_IS_EVENT (in2)) {
349 gst_pad_push (smpte->srcpad, GST_DATA (in2));
353 ts = GST_BUFFER_TIMESTAMP (in2);
357 in1 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
358 fill_i420 (GST_BUFFER_DATA (in1), smpte->width, smpte->height, 7);
361 in2 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
362 fill_i420 (GST_BUFFER_DATA (in2), smpte->width, smpte->height, 0);
365 if (smpte->position < smpte->duration) {
366 outbuf = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
368 if (!GST_PAD_CAPS (smpte->srcpad)) {
369 if (!gst_pad_try_set_caps (smpte->srcpad,
373 "format", GST_PROPS_FOURCC (GST_MAKE_FOURCC ('I','4','2','0')),
374 "width", GST_PROPS_INT (smpte->width),
375 "height", GST_PROPS_INT (smpte->height),
376 "framerate", GST_PROPS_FLOAT (smpte->fps)
379 gst_element_error (element, "cannot set caps");
384 gst_smpte_blend_i420 (GST_BUFFER_DATA (in1),
385 GST_BUFFER_DATA (in2),
386 GST_BUFFER_DATA (outbuf),
387 smpte->mask, smpte->width, smpte->height,
389 ((1 << smpte->depth) + smpte->border) *
390 smpte->position / smpte->duration);
394 gst_buffer_ref (in2);
400 gst_buffer_unref (in1);
402 gst_buffer_unref (in2);
404 GST_BUFFER_TIMESTAMP (outbuf) = ts;
405 gst_pad_push (smpte->srcpad, GST_DATA (outbuf));
409 gst_smpte_set_property (GObject *object, guint prop_id,
410 const GValue *value, GParamSpec *pspec)
414 smpte = GST_SMPTE(object);
419 gint type = g_value_get_enum (value);
421 gst_smpte_update_mask (smpte, type, smpte->depth,
422 smpte->width, smpte->height);
426 smpte->border = g_value_get_int (value);
429 smpte->fps = g_value_get_float (value);
433 gint depth = g_value_get_int (value);
435 gst_smpte_update_mask (smpte, smpte->type, depth,
436 smpte->width, smpte->height);
440 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
446 gst_smpte_get_property (GObject *object, guint prop_id,
447 GValue *value, GParamSpec *pspec)
451 smpte = GST_SMPTE(object);
456 g_value_set_enum (value, smpte->mask->type);
460 g_value_set_float (value, smpte->fps);
463 g_value_set_int (value, smpte->border);
466 g_value_set_int (value, smpte->depth);
469 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
476 plugin_init (GstPlugin *plugin)
478 return gst_element_register(plugin, "smpte",
479 GST_RANK_NONE, GST_TYPE_SMPTE);
486 "Apply the standard SMPTE transitions on video images",