fix up more enums
[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_nick = definition->short_name;
99       smpte_transitions[i].value_name = 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 GstFlowReturn gst_smpte_collected (GstCollectPads * pads,
116     GstSMPTE * smpte);
117
118 static void gst_smpte_set_property (GObject * object, guint prop_id,
119     const GValue * value, GParamSpec * pspec);
120 static void gst_smpte_get_property (GObject * object, guint prop_id,
121     GValue * value, GParamSpec * pspec);
122
123 static GstElementClass *parent_class = NULL;
124
125 /*static guint gst_smpte_signals[LAST_SIGNAL] = { 0 }; */
126
127 static GType
128 gst_smpte_get_type (void)
129 {
130   static GType smpte_type = 0;
131
132   if (!smpte_type) {
133     static const GTypeInfo smpte_info = {
134       sizeof (GstSMPTEClass),
135       (GBaseInitFunc) gst_smpte_base_init,
136       NULL,
137       (GClassInitFunc) gst_smpte_class_init,
138       NULL,
139       NULL,
140       sizeof (GstSMPTE),
141       0,
142       (GInstanceInitFunc) gst_smpte_init,
143     };
144
145     smpte_type =
146         g_type_register_static (GST_TYPE_ELEMENT, "GstSMPTE", &smpte_info, 0);
147   }
148   return smpte_type;
149 }
150
151 static void
152 gst_smpte_base_init (GstSMPTEClass * klass)
153 {
154   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
155
156   gst_element_class_add_pad_template (element_class,
157       gst_static_pad_template_get (&gst_smpte_sink1_template));
158   gst_element_class_add_pad_template (element_class,
159       gst_static_pad_template_get (&gst_smpte_sink2_template));
160   gst_element_class_add_pad_template (element_class,
161       gst_static_pad_template_get (&gst_smpte_src_template));
162   gst_element_class_set_details (element_class, &smpte_details);
163 }
164
165 static void
166 gst_smpte_class_init (GstSMPTEClass * klass)
167 {
168   GObjectClass *gobject_class;
169   GstElementClass *gstelement_class;
170
171   gobject_class = (GObjectClass *) klass;
172   gstelement_class = (GstElementClass *) klass;
173
174   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
175
176   gobject_class->set_property = gst_smpte_set_property;
177   gobject_class->get_property = gst_smpte_get_property;
178
179   _gst_mask_init ();
180
181   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TYPE,
182       g_param_spec_enum ("type", "Type", "The type of transition to use",
183           GST_TYPE_SMPTE_TRANSITION_TYPE, 1, G_PARAM_READWRITE));
184   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FPS,
185       g_param_spec_float ("fps", "FPS",
186           "Frames per second if no input files are given", 0., G_MAXFLOAT, 25.,
187           G_PARAM_READWRITE));
188   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BORDER,
189       g_param_spec_int ("border", "Border",
190           "The border width of the transition", 0, G_MAXINT, 0,
191           G_PARAM_READWRITE));
192   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEPTH,
193       g_param_spec_int ("depth", "Depth", "Depth of the mask in bits", 1, 24,
194           16, G_PARAM_READWRITE));
195 }
196
197 /*                        wht  yel  cya  grn  mag  red  blu  blk   -I    Q */
198 static int y_colors[] = { 255, 226, 179, 150, 105, 76, 29, 16, 16, 0 };
199 static int u_colors[] = { 128, 0, 170, 46, 212, 85, 255, 128, 0, 128 };
200 static int v_colors[] = { 128, 155, 0, 21, 235, 255, 107, 128, 128, 255 };
201
202 static void
203 fill_i420 (guint8 * data, gint width, gint height, gint color)
204 {
205   gint size = width * height, size4 = size >> 2;
206   guint8 *yp = data;
207   guint8 *up = data + size;
208   guint8 *vp = data + size + size4;
209
210   memset (yp, y_colors[color], size);
211   memset (up, u_colors[color], size4);
212   memset (vp, v_colors[color], size4);
213 }
214
215 static gboolean
216 gst_smpte_update_mask (GstSMPTE * smpte, gint type, gint depth, gint width,
217     gint height)
218 {
219   GstMask *newmask;
220
221   newmask = gst_mask_factory_new (type, depth, width, height);
222   if (newmask) {
223     if (smpte->mask) {
224       gst_mask_destroy (smpte->mask);
225     }
226     smpte->mask = newmask;
227     smpte->type = type;
228     smpte->depth = depth;
229     smpte->width = width;
230     smpte->height = height;
231
232     return TRUE;
233   }
234   return FALSE;
235 }
236
237 static gboolean
238 gst_smpte_setcaps (GstPad * pad, GstCaps * caps)
239 {
240   GstSMPTE *smpte;
241   GstStructure *structure;
242   gboolean ret;
243
244   smpte = GST_SMPTE (GST_PAD_PARENT (pad));
245
246   structure = gst_caps_get_structure (caps, 0);
247
248   ret = gst_structure_get_int (structure, "width", &smpte->width);
249   ret &= gst_structure_get_int (structure, "height", &smpte->height);
250   ret &= gst_structure_get_double (structure, "framerate", &smpte->fps);
251   if (!ret)
252     return FALSE;
253
254   gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width,
255       smpte->height);
256
257   /* forward to the next plugin */
258   return TRUE;
259 }
260
261 static void
262 gst_smpte_init (GstSMPTE * smpte)
263 {
264   smpte->sinkpad1 =
265       gst_pad_new_from_template (gst_static_pad_template_get
266       (&gst_smpte_sink1_template), "sink1");
267   gst_pad_set_setcaps_function (smpte->sinkpad1, gst_smpte_setcaps);
268   gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad1);
269
270   smpte->sinkpad2 =
271       gst_pad_new_from_template (gst_static_pad_template_get
272       (&gst_smpte_sink2_template), "sink2");
273   gst_pad_set_setcaps_function (smpte->sinkpad2, gst_smpte_setcaps);
274   gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad2);
275
276   smpte->srcpad =
277       gst_pad_new_from_template (gst_static_pad_template_get
278       (&gst_smpte_src_template), "src");
279   gst_element_add_pad (GST_ELEMENT (smpte), smpte->srcpad);
280
281   smpte->collect = gst_collect_pads_new ();
282   gst_collect_pads_set_function (smpte->collect,
283       (GstCollectPadsFunction) gst_smpte_collected, smpte);
284   gst_collect_pads_start (smpte->collect);
285
286   gst_collect_pads_add_pad (smpte->collect, smpte->sinkpad1,
287       sizeof (GstCollectData));
288   gst_collect_pads_add_pad (smpte->collect, smpte->sinkpad2,
289       sizeof (GstCollectData));
290
291   smpte->width = 320;
292   smpte->height = 200;
293   smpte->fps = 25.;
294   smpte->duration = 512;
295   smpte->position = 0;
296   smpte->type = 1;
297   smpte->border = 0;
298   smpte->depth = 16;
299   gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width,
300       smpte->height);
301 }
302
303 static void
304 gst_smpte_blend_i420 (guint8 * in1, guint8 * in2, guint8 * out, GstMask * mask,
305     gint width, gint height, gint border, gint pos)
306 {
307   guint32 *maskp;
308   gint value;
309   gint i, j;
310   gint min, max;
311   guint8 *in1u, *in1v, *in2u, *in2v, *outu, *outv;
312   gint lumsize = width * height;
313   gint chromsize = lumsize >> 2;
314
315   if (border == 0)
316     border++;
317
318   min = pos - border;
319   max = pos;
320
321   in1u = in1 + lumsize;
322   in1v = in1u + chromsize;
323   in2u = in2 + lumsize;
324   in2v = in2u + chromsize;
325   outu = out + lumsize;
326   outv = outu + chromsize;
327
328   maskp = mask->data;
329
330   for (i = 0; i < height; i++) {
331     for (j = 0; j < width; j++) {
332       value = *maskp++;
333       value = ((CLAMP (value, min, max) - min) << 8) / border;
334
335       *out++ = ((*in1++ * value) + (*in2++ * (256 - value))) >> 8;
336       if (!(i & 1) && !(j & 1)) {
337         *outu++ = ((*in1u++ * value) + (*in2u++ * (256 - value))) >> 8;
338         *outv++ = ((*in1v++ * value) + (*in2v++ * (256 - value))) >> 8;
339       }
340     }
341   }
342 }
343
344 static GstFlowReturn
345 gst_smpte_collected (GstCollectPads * pads, GstSMPTE * smpte)
346 {
347   GstBuffer *outbuf;
348   GstClockTime ts;
349   GstBuffer *in1 = NULL, *in2 = NULL;
350   GSList *collected;
351
352   ts = smpte->position * GST_SECOND / smpte->fps;
353
354   for (collected = pads->data; collected; collected = g_slist_next (collected)) {
355     GstCollectData *data;
356
357     data = (GstCollectData *) collected->data;
358
359     if (data->pad == smpte->sinkpad1)
360       in1 = gst_collect_pads_pop (pads, data);
361     else if (data->pad == smpte->sinkpad2)
362       in2 = gst_collect_pads_pop (pads, data);
363   }
364
365   if (in1 == NULL) {
366     in1 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
367     fill_i420 (GST_BUFFER_DATA (in1), smpte->width, smpte->height, 7);
368   }
369   if (in2 == NULL) {
370     in2 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
371     fill_i420 (GST_BUFFER_DATA (in2), smpte->width, smpte->height, 0);
372   }
373
374   if (smpte->position < smpte->duration) {
375     outbuf = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
376
377     /* set caps if not done yet */
378     if (!GST_PAD_CAPS (smpte->srcpad)) {
379       GstCaps *caps;
380
381       caps =
382           gst_caps_copy (gst_static_caps_get (&gst_smpte_src_template.
383               static_caps));
384       gst_caps_set_simple (caps, "width", G_TYPE_INT, smpte->width, "height",
385           G_TYPE_INT, smpte->height, "framerate", G_TYPE_DOUBLE, smpte->fps,
386           NULL);
387
388       gst_pad_set_caps (smpte->srcpad, caps);
389     }
390     gst_buffer_set_caps (outbuf, GST_PAD_CAPS (smpte->srcpad));
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
400   } else {
401     outbuf = in2;
402     gst_buffer_ref (in2);
403   }
404
405   smpte->position++;
406
407   if (in1)
408     gst_buffer_unref (in1);
409   if (in2)
410     gst_buffer_unref (in2);
411
412   GST_BUFFER_TIMESTAMP (outbuf) = ts;
413
414   return gst_pad_push (smpte->srcpad, outbuf);
415 }
416
417 static void
418 gst_smpte_set_property (GObject * object, guint prop_id,
419     const GValue * value, GParamSpec * pspec)
420 {
421   GstSMPTE *smpte;
422
423   smpte = GST_SMPTE (object);
424
425   switch (prop_id) {
426     case ARG_TYPE:
427     {
428       gint type = g_value_get_enum (value);
429
430       gst_smpte_update_mask (smpte, type, smpte->depth,
431           smpte->width, smpte->height);
432       break;
433     }
434     case ARG_BORDER:
435       smpte->border = g_value_get_int (value);
436       break;
437     case ARG_FPS:
438       smpte->fps = g_value_get_float (value);
439       break;
440     case ARG_DEPTH:
441     {
442       gint depth = g_value_get_int (value);
443
444       gst_smpte_update_mask (smpte, smpte->type, depth,
445           smpte->width, smpte->height);
446       break;
447     }
448     default:
449       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
450       break;
451   }
452 }
453
454 static void
455 gst_smpte_get_property (GObject * object, guint prop_id,
456     GValue * value, GParamSpec * pspec)
457 {
458   GstSMPTE *smpte;
459
460   smpte = GST_SMPTE (object);
461
462   switch (prop_id) {
463     case ARG_TYPE:
464       if (smpte->mask) {
465         g_value_set_enum (value, smpte->mask->type);
466       }
467       break;
468     case ARG_FPS:
469       g_value_set_float (value, smpte->fps);
470       break;
471     case ARG_BORDER:
472       g_value_set_int (value, smpte->border);
473       break;
474     case ARG_DEPTH:
475       g_value_set_int (value, smpte->depth);
476       break;
477     default:
478       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
479       break;
480   }
481 }
482
483
484 static gboolean
485 plugin_init (GstPlugin * plugin)
486 {
487   return gst_element_register (plugin, "smpte", GST_RANK_NONE, GST_TYPE_SMPTE);
488 }
489
490 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
491     GST_VERSION_MINOR,
492     "smpte",
493     "Apply the standard SMPTE transitions on video images",
494     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)