first batch : remove ',' at end of enums as they could confuse older gcc, foreign...
[platform/upstream/gst-plugins-good.git] / gst / smpte / gstsmpte.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <string.h>
24 #include <gstsmpte.h>
25 #include <gst/video/video.h>
26 #include "paint.h"
27
28 /* elementfactory information */
29 static GstElementDetails smpte_details = {
30   "SMPTE transitions",
31   "Filter/Editor/Video",
32   "Apply the standard SMPTE transitions on video images",
33   "Wim Taymans <wim.taymans@chello.be>"
34 };
35
36 static GstStaticPadTemplate gst_smpte_src_template =
37 GST_STATIC_PAD_TEMPLATE ("src",
38     GST_PAD_SRC,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420")
41     )
42     );
43
44 static GstStaticPadTemplate gst_smpte_sink1_template =
45 GST_STATIC_PAD_TEMPLATE ("sink1",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420")
49     )
50     );
51
52 static GstStaticPadTemplate gst_smpte_sink2_template =
53 GST_STATIC_PAD_TEMPLATE ("sink2",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420")
57     )
58     );
59
60
61 /* SMPTE signals and args */
62 enum
63 {
64   /* FILL ME */
65   LAST_SIGNAL
66 };
67
68 enum
69 {
70   ARG_0,
71   ARG_TYPE,
72   ARG_BORDER,
73   ARG_DEPTH,
74   ARG_FPS
75 };
76
77 #define GST_TYPE_SMPTE_TRANSITION_TYPE (gst_smpte_transition_type_get_type())
78 static GType
79 gst_smpte_transition_type_get_type (void)
80 {
81   static GType smpte_transition_type = 0;
82   GEnumValue *smpte_transitions;
83
84   if (!smpte_transition_type) {
85     const GList *definitions;
86     gint i = 0;
87
88     definitions = gst_mask_get_definitions ();
89     smpte_transitions =
90         g_new0 (GEnumValue, g_list_length ((GList *) definitions) + 1);
91
92     while (definitions) {
93       GstMaskDefinition *definition = (GstMaskDefinition *) definitions->data;
94
95       definitions = g_list_next (definitions);
96
97       smpte_transitions[i].value = definition->type;
98       smpte_transitions[i].value_name = definition->short_name;
99       smpte_transitions[i].value_nick = definition->long_name;
100
101       i++;
102     }
103
104     smpte_transition_type =
105         g_enum_register_static ("GstSMPTETransitionType", smpte_transitions);
106   }
107   return smpte_transition_type;
108 }
109
110
111 static void gst_smpte_class_init (GstSMPTEClass * klass);
112 static void gst_smpte_base_init (GstSMPTEClass * klass);
113 static void gst_smpte_init (GstSMPTE * smpte);
114
115 static void gst_smpte_loop (GstElement * element);
116
117 static void gst_smpte_set_property (GObject * object, guint prop_id,
118     const GValue * value, GParamSpec * pspec);
119 static void gst_smpte_get_property (GObject * object, guint prop_id,
120     GValue * value, GParamSpec * pspec);
121
122 static GstElementClass *parent_class = NULL;
123
124 /*static guint gst_smpte_signals[LAST_SIGNAL] = { 0 }; */
125
126 static GType
127 gst_smpte_get_type (void)
128 {
129   static GType smpte_type = 0;
130
131   if (!smpte_type) {
132     static const GTypeInfo smpte_info = {
133       sizeof (GstSMPTEClass),
134       (GBaseInitFunc) gst_smpte_base_init,
135       NULL,
136       (GClassInitFunc) gst_smpte_class_init,
137       NULL,
138       NULL,
139       sizeof (GstSMPTE),
140       0,
141       (GInstanceInitFunc) gst_smpte_init,
142     };
143
144     smpte_type =
145         g_type_register_static (GST_TYPE_ELEMENT, "GstSMPTE", &smpte_info, 0);
146   }
147   return smpte_type;
148 }
149
150 static void
151 gst_smpte_base_init (GstSMPTEClass * klass)
152 {
153   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
154
155   gst_element_class_add_pad_template (element_class,
156       gst_static_pad_template_get (&gst_smpte_sink1_template));
157   gst_element_class_add_pad_template (element_class,
158       gst_static_pad_template_get (&gst_smpte_sink2_template));
159   gst_element_class_add_pad_template (element_class,
160       gst_static_pad_template_get (&gst_smpte_src_template));
161   gst_element_class_set_details (element_class, &smpte_details);
162 }
163
164 static void
165 gst_smpte_class_init (GstSMPTEClass * klass)
166 {
167   GObjectClass *gobject_class;
168   GstElementClass *gstelement_class;
169
170   gobject_class = (GObjectClass *) klass;
171   gstelement_class = (GstElementClass *) klass;
172
173   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
174
175   gobject_class->set_property = gst_smpte_set_property;
176   gobject_class->get_property = gst_smpte_get_property;
177
178   _gst_mask_init ();
179
180   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TYPE,
181       g_param_spec_enum ("type", "Type", "The type of transition to use",
182           GST_TYPE_SMPTE_TRANSITION_TYPE, 1, G_PARAM_READWRITE));
183   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FPS,
184       g_param_spec_float ("fps", "FPS",
185           "Frames per second if no input files are given", 0., G_MAXFLOAT, 25.,
186           G_PARAM_READWRITE));
187   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BORDER,
188       g_param_spec_int ("border", "Border",
189           "The border width of the transition", 0, G_MAXINT, 0,
190           G_PARAM_READWRITE));
191   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEPTH,
192       g_param_spec_int ("depth", "Depth", "Depth of the mask in bits", 1, 24,
193           16, G_PARAM_READWRITE));
194 }
195
196 /*                        wht  yel  cya  grn  mag  red  blu  blk   -I    Q */
197 static int y_colors[] = { 255, 226, 179, 150, 105, 76, 29, 16, 16, 0 };
198 static int u_colors[] = { 128, 0, 170, 46, 212, 85, 255, 128, 0, 128 };
199 static int v_colors[] = { 128, 155, 0, 21, 235, 255, 107, 128, 128, 255 };
200
201 static void
202 fill_i420 (guint8 * data, gint width, gint height, gint color)
203 {
204   gint size = width * height, size4 = size >> 2;
205   guint8 *yp = data;
206   guint8 *up = data + size;
207   guint8 *vp = data + size + size4;
208
209   memset (yp, y_colors[color], size);
210   memset (up, u_colors[color], size4);
211   memset (vp, v_colors[color], size4);
212 }
213
214 static gboolean
215 gst_smpte_update_mask (GstSMPTE * smpte, gint type, gint depth, gint width,
216     gint height)
217 {
218   GstMask *newmask;
219
220   newmask = gst_mask_factory_new (type, depth, width, height);
221   if (newmask) {
222     if (smpte->mask) {
223       gst_mask_destroy (smpte->mask);
224     }
225     smpte->mask = newmask;
226     smpte->type = type;
227     smpte->depth = depth;
228     smpte->width = width;
229     smpte->height = height;
230
231     return TRUE;
232   }
233   return FALSE;
234 }
235
236 static gboolean
237 gst_smpte_sinkconnect (GstPad * pad, const GstCaps * caps)
238 {
239   GstSMPTE *smpte;
240   GstStructure *structure;
241   gboolean ret;
242
243   smpte = GST_SMPTE (gst_pad_get_parent (pad));
244
245   structure = gst_caps_get_structure (caps, 0);
246
247   ret = gst_structure_get_int (structure, "width", &smpte->width);
248   ret &= gst_structure_get_int (structure, "height", &smpte->height);
249   ret &= gst_structure_get_double (structure, "framerate", &smpte->fps);
250   if (!ret)
251     return GST_PAD_LINK_REFUSED;
252
253   gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width,
254       smpte->height);
255
256   /* forward to the next plugin */
257   return gst_pad_try_set_caps (smpte->srcpad, caps);
258 }
259
260 static void
261 gst_smpte_init (GstSMPTE * smpte)
262 {
263   smpte->sinkpad1 =
264       gst_pad_new_from_template (gst_static_pad_template_get
265       (&gst_smpte_sink1_template), "sink1");
266   gst_pad_set_link_function (smpte->sinkpad1, gst_smpte_sinkconnect);
267   gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad1);
268
269   smpte->sinkpad2 =
270       gst_pad_new_from_template (gst_static_pad_template_get
271       (&gst_smpte_sink2_template), "sink2");
272   gst_pad_set_link_function (smpte->sinkpad2, gst_smpte_sinkconnect);
273   gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad2);
274
275   smpte->srcpad =
276       gst_pad_new_from_template (gst_static_pad_template_get
277       (&gst_smpte_src_template), "src");
278   gst_element_add_pad (GST_ELEMENT (smpte), smpte->srcpad);
279
280   gst_element_set_loop_function (GST_ELEMENT (smpte), gst_smpte_loop);
281
282   smpte->width = 320;
283   smpte->height = 200;
284   smpte->fps = 25.;
285   smpte->duration = 64;
286   smpte->position = 0;
287   smpte->type = 1;
288   smpte->border = 0;
289   smpte->depth = 16;
290   gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width,
291       smpte->height);
292 }
293
294 static void
295 gst_smpte_blend_i420 (guint8 * in1, guint8 * in2, guint8 * out, GstMask * mask,
296     gint width, gint height, gint border, gint pos)
297 {
298   guint32 *maskp;
299   gint value;
300   gint i, j;
301   gint min, max;
302   guint8 *in1u, *in1v, *in2u, *in2v, *outu, *outv;
303   gint lumsize = width * height;
304   gint chromsize = lumsize >> 2;
305
306   if (border == 0)
307     border++;
308
309   min = pos - border;
310   max = pos;
311
312   in1u = in1 + lumsize;
313   in1v = in1u + chromsize;
314   in2u = in2 + lumsize;
315   in2v = in2u + chromsize;
316   outu = out + lumsize;
317   outv = outu + chromsize;
318
319   maskp = mask->data;
320
321   for (i = 0; i < height; i++) {
322     for (j = 0; j < width; j++) {
323       value = *maskp++;
324       value = ((CLAMP (value, min, max) - min) << 8) / border;
325
326       *out++ = ((*in1++ * value) + (*in2++ * (256 - value))) >> 8;
327       if (!(i & 1) && !(j & 1)) {
328         *outu++ = ((*in1u++ * value) + (*in2u++ * (256 - value))) >> 8;
329         *outv++ = ((*in1v++ * value) + (*in2v++ * (256 - value))) >> 8;
330       }
331     }
332   }
333 }
334
335 static void
336 gst_smpte_loop (GstElement * element)
337 {
338   GstSMPTE *smpte;
339   GstBuffer *outbuf;
340   GstClockTime ts;
341   GstBuffer *in1 = NULL, *in2 = NULL;
342
343   smpte = GST_SMPTE (element);
344
345   ts = smpte->position * GST_SECOND / smpte->fps;
346
347   while (GST_PAD_IS_USABLE (smpte->sinkpad1) && in1 == NULL) {
348     in1 = GST_BUFFER (gst_pad_pull (smpte->sinkpad1));
349     if (GST_IS_EVENT (in1)) {
350       gst_pad_push (smpte->srcpad, GST_DATA (in1));
351       in1 = NULL;
352     } else
353       ts = GST_BUFFER_TIMESTAMP (in1);
354   }
355   if (GST_PAD_IS_USABLE (smpte->sinkpad2) && in2 == NULL) {
356     in2 = GST_BUFFER (gst_pad_pull (smpte->sinkpad2));
357     if (GST_IS_EVENT (in2)) {
358       gst_pad_push (smpte->srcpad, GST_DATA (in2));
359       in2 = NULL;
360     } else
361       ts = GST_BUFFER_TIMESTAMP (in2);
362   }
363
364   if (in1 == NULL) {
365     in1 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
366     fill_i420 (GST_BUFFER_DATA (in1), smpte->width, smpte->height, 7);
367   }
368   if (in2 == NULL) {
369     in2 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
370     fill_i420 (GST_BUFFER_DATA (in2), smpte->width, smpte->height, 0);
371   }
372
373   if (smpte->position < smpte->duration) {
374     outbuf = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
375
376     if (!GST_PAD_CAPS (smpte->srcpad)) {
377       GstCaps *caps;
378
379       caps =
380           gst_caps_copy (gst_static_caps_get (&gst_smpte_src_template.
381               static_caps));
382       gst_caps_set_simple (caps, "width", G_TYPE_INT, smpte->width, "height",
383           G_TYPE_INT, smpte->height, "framerate", G_TYPE_DOUBLE, smpte->fps,
384           NULL);
385
386       if (!gst_pad_try_set_caps (smpte->srcpad, caps)) {
387         GST_ELEMENT_ERROR (smpte, CORE, NEGOTIATION, (NULL), (NULL));
388         return;
389       }
390     }
391
392     gst_smpte_blend_i420 (GST_BUFFER_DATA (in1),
393         GST_BUFFER_DATA (in2),
394         GST_BUFFER_DATA (outbuf),
395         smpte->mask, smpte->width, smpte->height,
396         smpte->border,
397         ((1 << smpte->depth) + smpte->border) *
398         smpte->position / smpte->duration);
399   } else {
400     outbuf = in2;
401     gst_buffer_ref (in2);
402   }
403
404   smpte->position++;
405
406   if (in1)
407     gst_buffer_unref (in1);
408   if (in2)
409     gst_buffer_unref (in2);
410
411   GST_BUFFER_TIMESTAMP (outbuf) = ts;
412   gst_pad_push (smpte->srcpad, GST_DATA (outbuf));
413 }
414
415 static void
416 gst_smpte_set_property (GObject * object, guint prop_id,
417     const GValue * value, GParamSpec * pspec)
418 {
419   GstSMPTE *smpte;
420
421   smpte = GST_SMPTE (object);
422
423   switch (prop_id) {
424     case ARG_TYPE:
425     {
426       gint type = g_value_get_enum (value);
427
428       gst_smpte_update_mask (smpte, type, smpte->depth,
429           smpte->width, smpte->height);
430       break;
431     }
432     case ARG_BORDER:
433       smpte->border = g_value_get_int (value);
434       break;
435     case ARG_FPS:
436       smpte->fps = g_value_get_float (value);
437       break;
438     case ARG_DEPTH:
439     {
440       gint depth = g_value_get_int (value);
441
442       gst_smpte_update_mask (smpte, smpte->type, depth,
443           smpte->width, smpte->height);
444       break;
445     }
446     default:
447       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
448       break;
449   }
450 }
451
452 static void
453 gst_smpte_get_property (GObject * object, guint prop_id,
454     GValue * value, GParamSpec * pspec)
455 {
456   GstSMPTE *smpte;
457
458   smpte = GST_SMPTE (object);
459
460   switch (prop_id) {
461     case ARG_TYPE:
462       if (smpte->mask) {
463         g_value_set_enum (value, smpte->mask->type);
464       }
465       break;
466     case ARG_FPS:
467       g_value_set_float (value, smpte->fps);
468       break;
469     case ARG_BORDER:
470       g_value_set_int (value, smpte->border);
471       break;
472     case ARG_DEPTH:
473       g_value_set_int (value, smpte->depth);
474       break;
475     default:
476       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
477       break;
478   }
479 }
480
481
482 static gboolean
483 plugin_init (GstPlugin * plugin)
484 {
485   return gst_element_register (plugin, "smpte", GST_RANK_NONE, GST_TYPE_SMPTE);
486 }
487
488 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
489     GST_VERSION_MINOR,
490     "smpte",
491     "Apply the standard SMPTE transitions on video images",
492     plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)