de485105ba1a96ec6862baa90af6b429f0c79d9a
[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 GST_PAD_TEMPLATE_FACTORY (smpte_src_factory,
37   "src",
38   GST_PAD_SRC,
39   GST_PAD_ALWAYS,
40   gst_caps_new (
41    "smpte_src",
42    "video/x-raw-yuv",
43      GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
44              GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
45   )
46 )
47
48 GST_PAD_TEMPLATE_FACTORY (smpte_sink1_factory,
49   "sink1",
50   GST_PAD_SINK,
51   GST_PAD_ALWAYS,
52   gst_caps_new (
53    "smpte_sink1",
54    "video/x-raw-yuv",
55      GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
56              GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
57   )
58 )
59
60 GST_PAD_TEMPLATE_FACTORY (smpte_sink2_factory,
61   "sink2",
62   GST_PAD_SINK,
63   GST_PAD_ALWAYS,
64   gst_caps_new (
65    "smpte_sink2",
66    "video/x-raw-yuv",
67      GST_VIDEO_YUV_PAD_TEMPLATE_PROPS(
68              GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")))
69   )
70 )
71
72
73 /* SMPTE signals and args */
74 enum {
75   /* FILL ME */
76   LAST_SIGNAL
77 };
78
79 enum {
80   ARG_0,
81   ARG_TYPE,
82   ARG_BORDER,
83   ARG_DEPTH,
84   ARG_FPS,
85 };
86
87 #define GST_TYPE_SMPTE_TRANSITION_TYPE (gst_smpte_transition_type_get_type())
88 static GType
89 gst_smpte_transition_type_get_type (void) 
90 {
91   static GType smpte_transition_type = 0;
92   GEnumValue *smpte_transitions;
93
94   if (!smpte_transition_type) {
95     const GList *definitions;
96     gint i=0;
97
98     definitions = gst_mask_get_definitions ();
99     smpte_transitions = g_new0 (GEnumValue, g_list_length ((GList *)definitions)+1);
100
101     while (definitions) {
102       GstMaskDefinition *definition = (GstMaskDefinition *) definitions->data;
103       definitions = g_list_next (definitions);
104
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;
108       
109       i++;
110     }
111
112     smpte_transition_type = 
113             g_enum_register_static ("GstSMPTETransitionType", smpte_transitions);
114   }
115   return smpte_transition_type;
116 }   
117
118
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);
122
123 static void     gst_smpte_loop                  (GstElement *element);
124
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);
129
130 static GstElementClass *parent_class = NULL;
131 /*static guint gst_smpte_signals[LAST_SIGNAL] = { 0 }; */
132
133 static GType
134 gst_smpte_get_type (void)
135 {
136   static GType smpte_type = 0;
137
138   if (!smpte_type) {
139     static const GTypeInfo smpte_info = {
140       sizeof(GstSMPTEClass),      
141       (GBaseInitFunc)gst_smpte_base_init,
142       NULL,
143       (GClassInitFunc)gst_smpte_class_init,
144       NULL,
145       NULL,
146       sizeof(GstSMPTE),
147       0,
148       (GInstanceInitFunc)gst_smpte_init,
149     };
150     smpte_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSMPTE", &smpte_info, 0);
151   }
152   return smpte_type;
153 }
154
155 static void
156 gst_smpte_base_init (GstSMPTEClass *klass)
157 {
158   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
159
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);
167 }
168
169 static void
170 gst_smpte_class_init (GstSMPTEClass *klass)
171 {
172   GObjectClass *gobject_class;
173   GstElementClass *gstelement_class;
174
175   gobject_class = (GObjectClass*)klass;
176   gstelement_class = (GstElementClass*)klass;
177
178   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
179
180   gobject_class->set_property = gst_smpte_set_property;
181   gobject_class->get_property = gst_smpte_get_property;
182
183   _gst_mask_init ();
184
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));
197 }
198
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 };
203
204 static void
205 fill_i420 (guint8 *data, gint width, gint height, gint color)
206 {
207   gint size = width * height, size4 = size >> 2;
208   guint8 *yp = data;
209   guint8 *up = data + size;
210   guint8 *vp = data + size + size4;
211   
212   memset (yp, y_colors[color], size);
213   memset (up, u_colors[color], size4);
214   memset (vp, v_colors[color], size4);
215 }
216
217 static gboolean
218 gst_smpte_update_mask (GstSMPTE *smpte, gint type, gint depth, gint width, gint height)
219 {
220   GstMask *newmask;
221
222   newmask = gst_mask_factory_new (type, depth, width, height);
223   if (newmask) {
224     if (smpte->mask) {
225       gst_mask_destroy (smpte->mask);
226     }
227     smpte->mask = newmask;
228     smpte->type = type;
229     smpte->depth = depth;
230     smpte->width = width;
231     smpte->height = height;
232
233     return TRUE;
234   }
235   return FALSE;
236 }
237
238 static gboolean
239 gst_smpte_sinkconnect (GstPad *pad, GstCaps *caps)
240 {
241   GstSMPTE *smpte;
242
243   smpte = GST_SMPTE (gst_pad_get_parent (pad));
244
245   if (!GST_CAPS_IS_FIXED (caps))
246     return GST_PAD_LINK_DELAYED;
247
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);
251
252   gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width, smpte->height);
253
254   /* forward to the next plugin */
255   return gst_pad_try_set_caps(smpte->srcpad, gst_caps_copy_1(caps));
256 }
257
258 static void 
259 gst_smpte_init (GstSMPTE *smpte)
260 {
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);
265
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);
270
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);
274
275   gst_element_set_loop_function (GST_ELEMENT (smpte), gst_smpte_loop);
276
277   smpte->width = 320;
278   smpte->height = 200;
279   smpte->fps = 25.;
280   smpte->duration = 64;
281   smpte->position = 0;
282   smpte->type = 1;
283   smpte->border = 0;
284   smpte->depth = 16;
285   gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width, smpte->height);
286 }
287
288 static void
289 gst_smpte_blend_i420 (guint8 *in1, guint8 *in2, guint8 *out, GstMask *mask,
290                       gint width, gint height, gint border, gint pos)
291 {
292   guint32 *maskp;
293   gint value;
294   gint i, j;
295   gint min, max;
296   guint8 *in1u, *in1v, *in2u, *in2v, *outu, *outv; 
297   gint lumsize = width * height;
298   gint chromsize = lumsize >> 2;
299
300   if (border == 0) border++;
301
302   min = pos - border; 
303   max = pos;
304
305   in1u = in1 + lumsize; in1v = in1u + chromsize;
306   in2u = in2 + lumsize; in2v = in2u + chromsize;
307   outu = out + lumsize; outv = outu + chromsize;
308   
309   maskp = mask->data;
310
311   for (i = 0; i < height; i++) {
312     for (j = 0; j < width; j++) {
313       value = *maskp++;
314       value = ((CLAMP (value, min, max) - min) << 8) / border;
315     
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;
320       }
321     }
322   }
323 }
324
325 static void
326 gst_smpte_loop (GstElement *element)
327 {
328   GstSMPTE *smpte;
329   GstBuffer *outbuf;
330   GstClockTime ts;
331   GstBuffer *in1 = NULL, *in2 = NULL;
332
333   smpte = GST_SMPTE (element);
334
335   ts = smpte->position * GST_SECOND / smpte->fps;
336
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));
341       in1 = NULL;
342     }
343     else 
344       ts = GST_BUFFER_TIMESTAMP (in1);
345   }
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));
350       in2 = NULL;
351     }
352     else 
353       ts = GST_BUFFER_TIMESTAMP (in2);
354   }
355
356   if (in1 == NULL) {
357     in1 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
358     fill_i420 (GST_BUFFER_DATA (in1), smpte->width, smpte->height, 7);
359   }
360   if (in2 == NULL) {
361     in2 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
362     fill_i420 (GST_BUFFER_DATA (in2), smpte->width, smpte->height, 0);
363   }
364
365   if (smpte->position < smpte->duration) { 
366     outbuf = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
367
368     if (!GST_PAD_CAPS (smpte->srcpad)) {
369       if (!gst_pad_try_set_caps (smpte->srcpad,
370             GST_CAPS_NEW (
371                     "smpte_srccaps",
372                     "video/raw",
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)
377                     )))
378       {
379         gst_element_error (element, "cannot set caps");
380         return;
381       }
382     }
383
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, 
388                           smpte->border,
389                           ((1 << smpte->depth) + smpte->border) * 
390                             smpte->position / smpte->duration);
391   }
392   else {
393     outbuf = in2;
394     gst_buffer_ref (in2);
395   }
396
397   smpte->position++;
398
399   if (in1)
400     gst_buffer_unref (in1);
401   if (in2)
402     gst_buffer_unref (in2);
403
404   GST_BUFFER_TIMESTAMP (outbuf) = ts;
405   gst_pad_push (smpte->srcpad, GST_DATA (outbuf));
406 }
407
408 static void
409 gst_smpte_set_property (GObject *object, guint prop_id, 
410                         const GValue *value, GParamSpec *pspec)
411 {
412   GstSMPTE *smpte;
413
414   smpte = GST_SMPTE(object);
415
416   switch (prop_id) {
417     case ARG_TYPE:
418     {
419       gint type = g_value_get_enum (value);
420
421       gst_smpte_update_mask (smpte, type, smpte->depth, 
422                              smpte->width, smpte->height);
423       break;
424     }
425     case ARG_BORDER:
426       smpte->border = g_value_get_int (value);
427       break;
428     case ARG_FPS:
429       smpte->fps = g_value_get_float (value);
430       break;
431     case ARG_DEPTH:
432     {
433       gint depth = g_value_get_int (value);
434
435       gst_smpte_update_mask (smpte, smpte->type, depth, 
436                              smpte->width, smpte->height);
437       break;
438     }
439     default:
440       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
441       break;
442   }
443 }
444
445 static void
446 gst_smpte_get_property (GObject *object, guint prop_id, 
447                         GValue *value, GParamSpec *pspec)
448 {
449   GstSMPTE *smpte;
450
451   smpte = GST_SMPTE(object);
452
453   switch (prop_id) {
454     case ARG_TYPE:
455       if (smpte->mask) {
456         g_value_set_enum (value, smpte->mask->type);
457       }
458       break;
459     case ARG_FPS:
460       g_value_set_float (value, smpte->fps);
461       break;
462     case ARG_BORDER:
463       g_value_set_int (value, smpte->border);
464       break;
465     case ARG_DEPTH:
466       g_value_set_int (value, smpte->depth);
467       break;
468     default:
469       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
470       break;
471   }
472 }
473
474
475 static gboolean
476 plugin_init (GstPlugin *plugin)
477 {
478   return gst_element_register(plugin, "smpte",
479                               GST_RANK_NONE, GST_TYPE_SMPTE);
480 }
481
482 GST_PLUGIN_DEFINE (
483   GST_VERSION_MAJOR,
484   GST_VERSION_MINOR,
485   "smpte",
486   "Apply the standard SMPTE transitions on video images",
487   plugin_init,
488   VERSION,
489   "LGPL",
490   GST_COPYRIGHT,
491   GST_PACKAGE,
492   GST_ORIGIN
493 )