- reimplemented using organic masks, rendered with gouraud shaded triangles
[platform/upstream/gstreamer.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 #include <string.h>
21 #include <gstsmpte.h>
22 #include "paint.h"
23
24 /* elementfactory information */
25 static GstElementDetails smpte_details = {
26   "SMPTE transitions",
27   "Filter/Video",
28   "LGPL",
29   "Apply the standard SMPTE transitions on video images",
30   VERSION,
31   "Wim Taymans <wim.taymans@chello.be>",
32   "(C) 2002",
33 };
34
35 GST_PAD_TEMPLATE_FACTORY (smpte_src_factory,
36   "src",
37   GST_PAD_SRC,
38   GST_PAD_ALWAYS,
39   GST_CAPS_NEW (
40    "smpte_src",
41    "video/raw",
42      "format",   GST_PROPS_FOURCC (GST_STR_FOURCC ("I420"))
43   )
44 )
45
46 GST_PAD_TEMPLATE_FACTORY (smpte_sink1_factory,
47   "sink1",
48   GST_PAD_SINK,
49   GST_PAD_ALWAYS,
50   GST_CAPS_NEW (
51    "smpte_sink1",
52    "video/raw",
53      "format",   GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")),
54      "width",    GST_PROPS_INT_RANGE (0, 4096),
55      "height",   GST_PROPS_INT_RANGE (0, 4096) 
56   )
57 )
58
59 GST_PAD_TEMPLATE_FACTORY (smpte_sink2_factory,
60   "sink2",
61   GST_PAD_SINK,
62   GST_PAD_ALWAYS,
63   GST_CAPS_NEW (
64    "smpte_sink2",
65    "video/raw",
66      "format",   GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")),
67      "width",    GST_PROPS_INT_RANGE (0, 4096),
68      "height",   GST_PROPS_INT_RANGE (0, 4096)
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_init                  (GstSMPTE *smpte);
121
122 static void     gst_smpte_loop                  (GstElement *element);
123
124 static void     gst_smpte_set_property          (GObject *object, guint prop_id, 
125                                                  const GValue *value, GParamSpec *pspec);
126 static void     gst_smpte_get_property          (GObject *object, guint prop_id, 
127                                                  GValue *value, GParamSpec *pspec);
128
129 static GstElementClass *parent_class = NULL;
130 /*static guint gst_smpte_signals[LAST_SIGNAL] = { 0 }; */
131
132 static GType
133 gst_smpte_get_type (void)
134 {
135   static GType smpte_type = 0;
136
137   if (!smpte_type) {
138     static const GTypeInfo smpte_info = {
139       sizeof(GstSMPTEClass),      
140       NULL,
141       NULL,
142       (GClassInitFunc)gst_smpte_class_init,
143       NULL,
144       NULL,
145       sizeof(GstSMPTE),
146       0,
147       (GInstanceInitFunc)gst_smpte_init,
148     };
149     smpte_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSMPTE", &smpte_info, 0);
150   }
151   return smpte_type;
152 }
153
154 static void
155 gst_smpte_class_init (GstSMPTEClass *klass)
156 {
157   GObjectClass *gobject_class;
158   GstElementClass *gstelement_class;
159
160   gobject_class = (GObjectClass*)klass;
161   gstelement_class = (GstElementClass*)klass;
162
163   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
164
165   gobject_class->set_property = gst_smpte_set_property;
166   gobject_class->get_property = gst_smpte_get_property;
167
168   _gst_mask_init ();
169
170   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TYPE,
171     g_param_spec_enum ("type", "Type", "The type of transition to use",
172                        GST_TYPE_SMPTE_TRANSITION_TYPE, 1, G_PARAM_READWRITE));
173   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FPS,
174     g_param_spec_int ("fps", "FPS", "Frames per second if no input files are given",
175                       1, 255, 1, G_PARAM_READWRITE));
176   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BORDER,
177     g_param_spec_int ("border", "Border", "The border width of the transition",
178                       0, G_MAXINT, 0, G_PARAM_READWRITE));
179   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEPTH,
180     g_param_spec_int ("depth", "Depth", "Depth of the mask in bits",
181                       1, 24, 16, G_PARAM_READWRITE));
182 }
183
184 /*                        wht  yel  cya  grn  mag  red  blu  blk   -I    Q */
185 static int y_colors[] = { 255, 226, 179, 150, 105,  76,  29,  16,  16,   0 };
186 static int u_colors[] = { 128,   0, 170,  46, 212,  85, 255, 128,   0, 128 };
187 static int v_colors[] = { 128, 155,   0,  21, 235, 255, 107, 128, 128, 255 };
188
189 static void
190 fill_i420 (guint8 *data, gint width, gint height, gint color)
191 {
192   gint size = width * height, size4 = size >> 2;
193   guint8 *yp = data;
194   guint8 *up = data + size;
195   guint8 *vp = data + size + size4;
196   
197   memset (yp, y_colors[color], size);
198   memset (up, u_colors[color], size4);
199   memset (vp, v_colors[color], size4);
200 }
201
202 static gboolean
203 gst_smpte_update_mask (GstSMPTE *smpte, gint type, gint depth, gint width, gint height)
204 {
205   GstMask *newmask;
206
207   newmask = gst_mask_factory_new (type, depth, width, height);
208   if (newmask) {
209     if (smpte->mask) {
210       gst_mask_destroy (smpte->mask);
211     }
212     smpte->mask = newmask;
213     smpte->type = type;
214     smpte->depth = depth;
215     smpte->width = width;
216     smpte->height = height;
217
218     return TRUE;
219   }
220   return FALSE;
221 }
222
223 static gboolean
224 gst_smpte_sinkconnect (GstPad *pad, GstCaps *caps)
225 {
226   GstSMPTE *smpte;
227
228   smpte = GST_SMPTE (gst_pad_get_parent (pad));
229
230   if (!GST_CAPS_IS_FIXED (caps))
231     return GST_PAD_CONNECT_DELAYED;
232
233   gst_caps_get_int (caps, "width", &smpte->width);
234   gst_caps_get_int (caps, "height", &smpte->height);
235
236   gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width, smpte->height);
237
238   /* forward to the next plugin */
239   return gst_pad_try_set_caps(smpte->srcpad, gst_caps_copy_1(caps));
240 }
241
242 static void 
243 gst_smpte_init (GstSMPTE *smpte)
244 {
245   smpte->sinkpad1 = gst_pad_new_from_template (
246                   GST_PAD_TEMPLATE_GET (smpte_sink1_factory), "sink1");
247   gst_pad_set_connect_function (smpte->sinkpad1, gst_smpte_sinkconnect);
248   gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad1);
249
250   smpte->sinkpad2 = gst_pad_new_from_template (
251                   GST_PAD_TEMPLATE_GET (smpte_sink2_factory), "sink2");
252   gst_pad_set_connect_function (smpte->sinkpad2, gst_smpte_sinkconnect);
253   gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad2);
254
255   smpte->srcpad = gst_pad_new_from_template (
256                   GST_PAD_TEMPLATE_GET (smpte_src_factory), "src");
257   gst_element_add_pad (GST_ELEMENT (smpte), smpte->srcpad);
258
259   gst_element_set_loop_function (GST_ELEMENT (smpte), gst_smpte_loop);
260
261   smpte->width = 320;
262   smpte->height = 200;
263   smpte->duration = 64;
264   smpte->position = 0;
265   smpte->type = 1;
266   smpte->fps = 1;
267   smpte->border = 0;
268   smpte->depth = 16;
269   gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width, smpte->height);
270 }
271
272 static void
273 gst_smpte_blend_i420 (guint8 *in1, guint8 *in2, guint8 *out, GstMask *mask,
274                       gint width, gint height, gint border, gint pos)
275 {
276   guint32 *maskp;
277   gint value;
278   gint i;
279   gint min, max;
280   guint8 *in1u, *in1v, *in2u, *in2v, *outu, *outv; 
281   gint lumsize = width * height;
282   gint chromsize = lumsize >> 2;
283
284   if (border == 0) border++;
285
286   min = pos - border; 
287   max = pos;
288
289   in1u = in1 + lumsize; in1v = in1 + chromsize;
290   in2u = in2 + lumsize; in2v = in2 + chromsize;
291   outu = out + lumsize; outv = out + chromsize;
292   
293   maskp = mask->data;
294
295   for (i = lumsize; i; i--) {
296     value = *maskp++;
297     value = ((CLAMP (value, min, max) - min) << 8) / border;
298     
299     *out++ = ((*in1++ * value) + (*in2++ * (255 - value))) >> 8;
300     if (i % 4) {
301       *outu++ = ((*in1u++ * value) + (*in2u++ * (255 - value))) >> 8;
302       *outv++ = ((*in1v++ * value) + (*in2v++ * (255 - value))) >> 8;
303     }
304   }
305 }
306
307 static void
308 gst_smpte_loop (GstElement *element)
309 {
310   GstSMPTE *smpte;
311   GstBuffer *outbuf;
312   GstClockTime ts;
313   GstBuffer *in1 = NULL, *in2 = NULL;
314
315   smpte = GST_SMPTE (element);
316
317   ts = smpte->position * GST_SECOND / smpte->fps;
318
319   if (GST_PAD_IS_USABLE (smpte->sinkpad1)) {
320     in1 = gst_pad_pull (smpte->sinkpad1);
321     ts = GST_BUFFER_TIMESTAMP (in1);
322   }
323   else {
324     in1 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
325     fill_i420 (GST_BUFFER_DATA (in1), smpte->width, smpte->height, 7);
326   }
327     
328   if (GST_PAD_IS_USABLE (smpte->sinkpad2)) {
329     in2 = gst_pad_pull (smpte->sinkpad2);
330     ts = GST_BUFFER_TIMESTAMP (in2);
331   }
332   else {
333     in2 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
334     fill_i420 (GST_BUFFER_DATA (in2), smpte->width, smpte->height, 0);
335   }
336
337   if (smpte->position < smpte->duration) { 
338     outbuf = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
339
340     if (!GST_PAD_CAPS (smpte->srcpad)) {
341       if (!gst_pad_try_set_caps (smpte->srcpad,
342             GST_CAPS_NEW (
343                     "smpte_srccaps",
344                     "video/raw",
345                       "format",   GST_PROPS_FOURCC (GST_MAKE_FOURCC ('I','4','2','0')),
346                       "width",    GST_PROPS_INT (smpte->width),
347                       "height",   GST_PROPS_INT (smpte->height)
348                     )))
349       {
350         gst_element_error (element, "cannot set caps");
351         return;
352       }
353     }
354
355     gst_smpte_blend_i420 (GST_BUFFER_DATA (in1), 
356                           GST_BUFFER_DATA (in2), 
357                           GST_BUFFER_DATA (outbuf),
358                           smpte->mask, smpte->width, smpte->height, 
359                           smpte->border,
360                           ((1 << smpte->depth) + smpte->border) * 
361                             smpte->position / smpte->duration);
362   }
363   else {
364     outbuf = in2;
365     gst_buffer_ref (in2);
366   }
367
368   smpte->position++;
369
370   if (in1)
371     gst_buffer_unref (in1);
372   if (in2)
373     gst_buffer_unref (in2);
374
375   GST_BUFFER_TIMESTAMP (outbuf) = ts;
376   gst_pad_push (smpte->srcpad, outbuf);
377 }
378
379 static void
380 gst_smpte_set_property (GObject *object, guint prop_id, 
381                         const GValue *value, GParamSpec *pspec)
382 {
383   GstSMPTE *smpte;
384
385   smpte = GST_SMPTE(object);
386
387   switch (prop_id) {
388     case ARG_TYPE:
389     {
390       gint type = g_value_get_enum (value);
391
392       gst_smpte_update_mask (smpte, type, smpte->depth, 
393                              smpte->width, smpte->height);
394       break;
395     }
396     case ARG_FPS:
397       smpte->fps = g_value_get_int (value);
398       break;
399     case ARG_BORDER:
400       smpte->border = g_value_get_int (value);
401       break;
402     case ARG_DEPTH:
403     {
404       gint depth = g_value_get_int (value);
405
406       gst_smpte_update_mask (smpte, smpte->type, depth, 
407                              smpte->width, smpte->height);
408       break;
409     }
410     default:
411       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
412       break;
413   }
414 }
415
416 static void
417 gst_smpte_get_property (GObject *object, guint prop_id, 
418                         GValue *value, GParamSpec *pspec)
419 {
420   GstSMPTE *smpte;
421
422   smpte = GST_SMPTE(object);
423
424   switch (prop_id) {
425     case ARG_TYPE:
426       if (smpte->mask) {
427         g_value_set_enum (value, smpte->mask->type);
428       }
429       break;
430     case ARG_FPS:
431       g_value_set_int (value, smpte->fps);
432       break;
433     case ARG_BORDER:
434       g_value_set_int (value, smpte->border);
435       break;
436     case ARG_DEPTH:
437       g_value_set_int (value, smpte->depth);
438       break;
439     default:
440       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
441       break;
442   }
443 }
444
445
446 static gboolean
447 plugin_init (GModule *module, GstPlugin *plugin)
448 {
449   GstElementFactory *factory;
450
451   factory = gst_element_factory_new("smpte",GST_TYPE_SMPTE,
452                                    &smpte_details);
453   g_return_val_if_fail(factory != NULL, FALSE);
454
455   gst_element_factory_add_pad_template (factory, 
456                   GST_PAD_TEMPLATE_GET (smpte_sink1_factory));
457   gst_element_factory_add_pad_template (factory, 
458                   GST_PAD_TEMPLATE_GET (smpte_sink2_factory));
459   gst_element_factory_add_pad_template (factory, 
460                   GST_PAD_TEMPLATE_GET (smpte_src_factory));
461
462   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
463
464   return TRUE;
465 }
466
467 GstPluginDesc plugin_desc = {
468   GST_VERSION_MAJOR,
469   GST_VERSION_MINOR,
470   "smpte",
471   plugin_init
472 };
473