5899a9a16f89c0708f5b7cd98c4b33b8db7f0433
[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 (
38   "src",
39   GST_PAD_SRC,
40   GST_PAD_ALWAYS,
41   GST_STATIC_CAPS (
42      GST_VIDEO_CAPS_YUV("I420")
43   )
44 );
45
46 static GstStaticPadTemplate gst_smpte_sink1_template =
47 GST_STATIC_PAD_TEMPLATE (
48   "sink1",
49   GST_PAD_SINK,
50   GST_PAD_ALWAYS,
51   GST_STATIC_CAPS (
52      GST_VIDEO_CAPS_YUV("I420")
53   )
54 );
55
56 static GstStaticPadTemplate gst_smpte_sink2_template =
57 GST_STATIC_PAD_TEMPLATE (
58   "sink2",
59   GST_PAD_SINK,
60   GST_PAD_ALWAYS,
61   GST_STATIC_CAPS (
62      GST_VIDEO_CAPS_YUV("I420")
63   )
64 );
65
66
67 /* SMPTE signals and args */
68 enum {
69   /* FILL ME */
70   LAST_SIGNAL
71 };
72
73 enum {
74   ARG_0,
75   ARG_TYPE,
76   ARG_BORDER,
77   ARG_DEPTH,
78   ARG_FPS,
79 };
80
81 #define GST_TYPE_SMPTE_TRANSITION_TYPE (gst_smpte_transition_type_get_type())
82 static GType
83 gst_smpte_transition_type_get_type (void) 
84 {
85   static GType smpte_transition_type = 0;
86   GEnumValue *smpte_transitions;
87
88   if (!smpte_transition_type) {
89     const GList *definitions;
90     gint i=0;
91
92     definitions = gst_mask_get_definitions ();
93     smpte_transitions = g_new0 (GEnumValue, g_list_length ((GList *)definitions)+1);
94
95     while (definitions) {
96       GstMaskDefinition *definition = (GstMaskDefinition *) definitions->data;
97       definitions = g_list_next (definitions);
98
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;
102       
103       i++;
104     }
105
106     smpte_transition_type = 
107             g_enum_register_static ("GstSMPTETransitionType", smpte_transitions);
108   }
109   return smpte_transition_type;
110 }   
111
112
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);
116
117 static void     gst_smpte_loop                  (GstElement *element);
118
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);
123
124 static GstElementClass *parent_class = NULL;
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     smpte_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSMPTE", &smpte_info, 0);
145   }
146   return smpte_type;
147 }
148
149 static void
150 gst_smpte_base_init (GstSMPTEClass *klass)
151 {
152   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
153
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);
161 }
162
163 static void
164 gst_smpte_class_init (GstSMPTEClass *klass)
165 {
166   GObjectClass *gobject_class;
167   GstElementClass *gstelement_class;
168
169   gobject_class = (GObjectClass*)klass;
170   gstelement_class = (GstElementClass*)klass;
171
172   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
173
174   gobject_class->set_property = gst_smpte_set_property;
175   gobject_class->get_property = gst_smpte_get_property;
176
177   _gst_mask_init ();
178
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));
191 }
192
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 };
197
198 static void
199 fill_i420 (guint8 *data, gint width, gint height, gint color)
200 {
201   gint size = width * height, size4 = size >> 2;
202   guint8 *yp = data;
203   guint8 *up = data + size;
204   guint8 *vp = data + size + size4;
205   
206   memset (yp, y_colors[color], size);
207   memset (up, u_colors[color], size4);
208   memset (vp, v_colors[color], size4);
209 }
210
211 static gboolean
212 gst_smpte_update_mask (GstSMPTE *smpte, gint type, gint depth, gint width, gint height)
213 {
214   GstMask *newmask;
215
216   newmask = gst_mask_factory_new (type, depth, width, height);
217   if (newmask) {
218     if (smpte->mask) {
219       gst_mask_destroy (smpte->mask);
220     }
221     smpte->mask = newmask;
222     smpte->type = type;
223     smpte->depth = depth;
224     smpte->width = width;
225     smpte->height = height;
226
227     return TRUE;
228   }
229   return FALSE;
230 }
231
232 static gboolean
233 gst_smpte_sinkconnect (GstPad *pad, const GstCaps *caps)
234 {
235   GstSMPTE *smpte;
236   GstStructure *structure;
237   gboolean ret;
238
239   smpte = GST_SMPTE (gst_pad_get_parent (pad));
240
241   structure = gst_caps_get_structure (caps, 0);
242
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;
247
248   gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width, smpte->height);
249
250   /* forward to the next plugin */
251   return gst_pad_try_set_caps(smpte->srcpad, caps);
252 }
253
254 static void 
255 gst_smpte_init (GstSMPTE *smpte)
256 {
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);
261
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);
266
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);
270
271   gst_element_set_loop_function (GST_ELEMENT (smpte), gst_smpte_loop);
272
273   smpte->width = 320;
274   smpte->height = 200;
275   smpte->fps = 25.;
276   smpte->duration = 64;
277   smpte->position = 0;
278   smpte->type = 1;
279   smpte->border = 0;
280   smpte->depth = 16;
281   gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width, smpte->height);
282 }
283
284 static void
285 gst_smpte_blend_i420 (guint8 *in1, guint8 *in2, guint8 *out, GstMask *mask,
286                       gint width, gint height, gint border, gint pos)
287 {
288   guint32 *maskp;
289   gint value;
290   gint i, j;
291   gint min, max;
292   guint8 *in1u, *in1v, *in2u, *in2v, *outu, *outv; 
293   gint lumsize = width * height;
294   gint chromsize = lumsize >> 2;
295
296   if (border == 0) border++;
297
298   min = pos - border; 
299   max = pos;
300
301   in1u = in1 + lumsize; in1v = in1u + chromsize;
302   in2u = in2 + lumsize; in2v = in2u + chromsize;
303   outu = out + lumsize; outv = outu + chromsize;
304   
305   maskp = mask->data;
306
307   for (i = 0; i < height; i++) {
308     for (j = 0; j < width; j++) {
309       value = *maskp++;
310       value = ((CLAMP (value, min, max) - min) << 8) / border;
311     
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;
316       }
317     }
318   }
319 }
320
321 static void
322 gst_smpte_loop (GstElement *element)
323 {
324   GstSMPTE *smpte;
325   GstBuffer *outbuf;
326   GstClockTime ts;
327   GstBuffer *in1 = NULL, *in2 = NULL;
328
329   smpte = GST_SMPTE (element);
330
331   ts = smpte->position * GST_SECOND / smpte->fps;
332
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));
337       in1 = NULL;
338     }
339     else 
340       ts = GST_BUFFER_TIMESTAMP (in1);
341   }
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));
346       in2 = NULL;
347     }
348     else 
349       ts = GST_BUFFER_TIMESTAMP (in2);
350   }
351
352   if (in1 == NULL) {
353     in1 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
354     fill_i420 (GST_BUFFER_DATA (in1), smpte->width, smpte->height, 7);
355   }
356   if (in2 == NULL) {
357     in2 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
358     fill_i420 (GST_BUFFER_DATA (in2), smpte->width, smpte->height, 0);
359   }
360
361   if (smpte->position < smpte->duration) { 
362     outbuf = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
363
364     if (!GST_PAD_CAPS (smpte->srcpad)) {
365       GstCaps *caps;
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);
372
373       if (!gst_pad_try_set_caps (smpte->srcpad, caps)) {
374         GST_ELEMENT_ERROR (smpte, CORE, NEGOTIATION, (NULL), (NULL));
375         return;
376       }
377     }
378
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, 
383                           smpte->border,
384                           ((1 << smpte->depth) + smpte->border) * 
385                             smpte->position / smpte->duration);
386   }
387   else {
388     outbuf = in2;
389     gst_buffer_ref (in2);
390   }
391
392   smpte->position++;
393
394   if (in1)
395     gst_buffer_unref (in1);
396   if (in2)
397     gst_buffer_unref (in2);
398
399   GST_BUFFER_TIMESTAMP (outbuf) = ts;
400   gst_pad_push (smpte->srcpad, GST_DATA (outbuf));
401 }
402
403 static void
404 gst_smpte_set_property (GObject *object, guint prop_id, 
405                         const GValue *value, GParamSpec *pspec)
406 {
407   GstSMPTE *smpte;
408
409   smpte = GST_SMPTE(object);
410
411   switch (prop_id) {
412     case ARG_TYPE:
413     {
414       gint type = g_value_get_enum (value);
415
416       gst_smpte_update_mask (smpte, type, smpte->depth, 
417                              smpte->width, smpte->height);
418       break;
419     }
420     case ARG_BORDER:
421       smpte->border = g_value_get_int (value);
422       break;
423     case ARG_FPS:
424       smpte->fps = g_value_get_float (value);
425       break;
426     case ARG_DEPTH:
427     {
428       gint depth = g_value_get_int (value);
429
430       gst_smpte_update_mask (smpte, smpte->type, depth, 
431                              smpte->width, smpte->height);
432       break;
433     }
434     default:
435       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
436       break;
437   }
438 }
439
440 static void
441 gst_smpte_get_property (GObject *object, guint prop_id, 
442                         GValue *value, GParamSpec *pspec)
443 {
444   GstSMPTE *smpte;
445
446   smpte = GST_SMPTE(object);
447
448   switch (prop_id) {
449     case ARG_TYPE:
450       if (smpte->mask) {
451         g_value_set_enum (value, smpte->mask->type);
452       }
453       break;
454     case ARG_FPS:
455       g_value_set_float (value, smpte->fps);
456       break;
457     case ARG_BORDER:
458       g_value_set_int (value, smpte->border);
459       break;
460     case ARG_DEPTH:
461       g_value_set_int (value, smpte->depth);
462       break;
463     default:
464       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
465       break;
466   }
467 }
468
469
470 static gboolean
471 plugin_init (GstPlugin *plugin)
472 {
473   return gst_element_register(plugin, "smpte",
474                               GST_RANK_NONE, GST_TYPE_SMPTE);
475 }
476
477 GST_PLUGIN_DEFINE (
478   GST_VERSION_MAJOR,
479   GST_VERSION_MINOR,
480   "smpte",
481   "Apply the standard SMPTE transitions on video images",
482   plugin_init,
483   VERSION,
484   "LGPL",
485   GST_PACKAGE,
486   GST_ORIGIN
487 )