Added the base framework for the SMPTE transitions and wipes.
[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 };
83
84 #define GST_TYPE_SMPTE_TRANSITION_TYPE (gst_smpte_transition_type_get_type())
85 static GType
86 gst_smpte_transition_type_get_type (void) 
87 {
88   static GType smpte_transition_type = 0;
89   GEnumValue *smpte_transitions;
90
91   if (!smpte_transition_type) {
92     const GList *definitions;
93     gint i=0;
94
95     definitions = gst_mask_get_definitions ();
96     smpte_transitions = g_new0 (GEnumValue, g_list_length ((GList *)definitions)+1);
97
98     while (definitions) {
99       GstMaskDefinition *definition = (GstMaskDefinition *) definitions->data;
100       definitions = g_list_next (definitions);
101
102       smpte_transitions[i].value = definition->type;
103       smpte_transitions[i].value_name = definition->short_name;
104       smpte_transitions[i].value_nick = definition->long_name;
105       
106       i++;
107     }
108
109     smpte_transition_type = g_enum_register_static ("GstSMPTETransitionType", smpte_transitions);
110   }
111   return smpte_transition_type;
112 }   
113
114
115 static void     gst_smpte_class_init            (GstSMPTEClass *klass);
116 static void     gst_smpte_init                  (GstSMPTE *smpte);
117
118 static void     gst_smpte_loop                  (GstElement *element);
119
120 static void     gst_smpte_set_property          (GObject *object, guint prop_id, 
121                                                  const GValue *value, GParamSpec *pspec);
122 static void     gst_smpte_get_property          (GObject *object, guint prop_id, 
123                                                  GValue *value, GParamSpec *pspec);
124
125 static GstElementClass *parent_class = NULL;
126 /*static guint gst_smpte_signals[LAST_SIGNAL] = { 0 }; */
127
128 static GType
129 gst_smpte_get_type (void)
130 {
131   static GType smpte_type = 0;
132
133   if (!smpte_type) {
134     static const GTypeInfo smpte_info = {
135       sizeof(GstSMPTEClass),      
136       NULL,
137       NULL,
138       (GClassInitFunc)gst_smpte_class_init,
139       NULL,
140       NULL,
141       sizeof(GstSMPTE),
142       0,
143       (GInstanceInitFunc)gst_smpte_init,
144     };
145     smpte_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSMPTE", &smpte_info, 0);
146   }
147   return smpte_type;
148 }
149
150 static void
151 gst_smpte_class_init (GstSMPTEClass *klass)
152 {
153   GObjectClass *gobject_class;
154   GstElementClass *gstelement_class;
155
156   gobject_class = (GObjectClass*)klass;
157   gstelement_class = (GstElementClass*)klass;
158
159   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
160
161   gobject_class->set_property = gst_smpte_set_property;
162   gobject_class->get_property = gst_smpte_get_property;
163
164   _gst_mask_init ();
165
166   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TYPE,
167     g_param_spec_enum ("type", "Type", "The type of transition to use",
168                        GST_TYPE_SMPTE_TRANSITION_TYPE, 1, G_PARAM_READWRITE));
169
170 }
171
172 /*                        wht  yel  cya  grn  mag  red  blu  blk   -I    Q */
173 static int y_colors[] = { 255, 226, 179, 150, 105,  76,  29,  16,  16,   0 };
174 static int u_colors[] = { 128,   0, 170,  46, 212,  85, 255, 128,   0, 128 };
175 static int v_colors[] = { 128, 155,   0,  21, 235, 255, 107, 128, 128, 255 };
176
177 static void
178 fill_i420 (guint8 *data, gint width, gint height, gint color)
179 {
180   guint8 *yp = data;
181   guint8 *up = data + width * height;
182   guint8 *vp = data + width * height + (width * height) / 4;
183   
184   gst_smpte_paint_rect (yp, width,   0, 0, width,   height,   y_colors[color]);
185   gst_smpte_paint_rect (up, width/2, 0, 0, width/2, height/2, u_colors[color]);
186   gst_smpte_paint_rect (vp, width/2, 0, 0, width/2, height/2, v_colors[color]);
187 }
188
189 static gboolean
190 gst_smpte_sinkconnect (GstPad *pad, GstCaps *caps)
191 {
192   GstSMPTE *smpte;
193
194   smpte = GST_SMPTE (gst_pad_get_parent (pad));
195
196   if (!GST_CAPS_IS_FIXED (caps))
197     return GST_PAD_CONNECT_DELAYED;
198
199   gst_caps_get_int (caps, "width", &smpte->width);
200   gst_caps_get_int (caps, "height", &smpte->height);
201
202   smpte->mask = gst_mask_factory_new (smpte->type, 8, smpte->width, smpte->height);
203
204   /* forward to the next plugin */
205   return gst_pad_try_set_caps(smpte->srcpad, gst_caps_copy_1(caps));
206 }
207
208 static void 
209 gst_smpte_init (GstSMPTE *smpte)
210 {
211   smpte->sinkpad1 = gst_pad_new_from_template (
212                   GST_PAD_TEMPLATE_GET (smpte_sink1_factory), "sink1");
213   gst_pad_set_connect_function (smpte->sinkpad1, gst_smpte_sinkconnect);
214   gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad1);
215
216   smpte->sinkpad2 = gst_pad_new_from_template (
217                   GST_PAD_TEMPLATE_GET (smpte_sink2_factory), "sink2");
218   gst_pad_set_connect_function (smpte->sinkpad2, gst_smpte_sinkconnect);
219   gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad2);
220
221   smpte->srcpad = gst_pad_new_from_template (
222                   GST_PAD_TEMPLATE_GET (smpte_src_factory), "src");
223   gst_element_add_pad (GST_ELEMENT (smpte), smpte->srcpad);
224
225   gst_element_set_loop_function (GST_ELEMENT (smpte), gst_smpte_loop);
226
227   smpte->width = 320;
228   smpte->height = 200;
229   smpte->duration = 64;
230   smpte->position = 0;
231   smpte->type = 1;
232   smpte->mask = gst_mask_factory_new (smpte->type, 8, smpte->width, smpte->height);
233 }
234
235 static void
236 gst_smpte_blend_i420 (guint8 *in1, guint8 *in2, guint8 *out, GstMask *mask,
237                       gint width, gint height)
238 {
239   gint i, j;
240   guint8 *maskporig, *maskp;
241   guint8 *in1u, *in1v, *in2u, *in2v, *outu, *outv; 
242   guint8 value;
243   gint chromsize = (width * height) >> 2;
244
245   maskp = maskporig = mask->data;
246   
247   for (i = width * height; i; i--) {
248     value = *maskp++;
249     *out++ = ((*in2++ * value) + (*in1++ * (255 - value))) >> 8;
250   }
251   in1u = in1; in1v = in1 + chromsize;
252   in2u = in2; in2v = in2 + chromsize;
253   outu = out; outv = out + chromsize;
254
255   maskp = maskporig;
256   for (i = height/2; i; i--, maskp += width) {
257     for (j = width/2; j; j--, maskp += 2) {
258       value = *maskp;
259       *outu++ = ((*in2u++ * value) + (*in1u++ * (255 - value))) >> 8;
260       *outv++ = ((*in2v++ * value) + (*in1v++ * (255 - value))) >> 8;
261     }
262   }
263 }
264
265 static void
266 gst_smpte_loop (GstElement *element)
267 {
268   GstSMPTE *smpte;
269   GstBuffer *outbuf;
270   GstClockTime ts;
271   GstBuffer *in1 = NULL, *in2 = NULL;
272
273   smpte = GST_SMPTE (element);
274
275   ts = smpte->position * GST_SECOND;
276
277   if (GST_PAD_IS_USABLE (smpte->sinkpad1)) {
278     in1 = gst_pad_pull (smpte->sinkpad1);
279     ts = GST_BUFFER_TIMESTAMP (in1);
280   }
281   else {
282     in1 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
283     fill_i420 (GST_BUFFER_DATA (in1), smpte->width, smpte->height, 7);
284   }
285     
286   if (GST_PAD_IS_USABLE (smpte->sinkpad2)) {
287     in2 = gst_pad_pull (smpte->sinkpad2);
288     ts = GST_BUFFER_TIMESTAMP (in2);
289   }
290   else {
291     in2 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
292     fill_i420 (GST_BUFFER_DATA (in2), smpte->width, smpte->height, 0);
293   }
294
295   if (smpte->position < smpte->duration) { 
296     outbuf = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
297
298     if (!GST_PAD_CAPS (smpte->srcpad)) {
299       if (!gst_pad_try_set_caps (smpte->srcpad,
300                             GST_CAPS_NEW (
301                                     "smpte_srccaps",
302                                     "video/raw",
303                                       "format",   GST_PROPS_FOURCC (GST_MAKE_FOURCC ('I','4','2','0')),
304                                       "width",    GST_PROPS_INT (smpte->width),
305                                       "height",   GST_PROPS_INT (smpte->height)
306                                     )))
307       {
308         gst_element_error (element, "cannot set caps");
309         return;
310       }
311     }
312
313     gst_mask_update (smpte->mask, smpte->position, smpte->duration);
314
315     gst_smpte_blend_i420 (GST_BUFFER_DATA (in1), GST_BUFFER_DATA (in2), GST_BUFFER_DATA (outbuf),
316                           smpte->mask, smpte->width, smpte->height);
317   }
318   else {
319     outbuf = in2;
320     gst_buffer_ref (in2);
321   }
322
323   smpte->position++;
324
325   if (in1)
326     gst_buffer_unref (in1);
327   if (in2)
328     gst_buffer_unref (in2);
329
330   GST_BUFFER_TIMESTAMP (outbuf) = ts;
331   gst_pad_push (smpte->srcpad, outbuf);
332 }
333
334 static void
335 gst_smpte_set_property (GObject *object, guint prop_id, 
336                         const GValue *value, GParamSpec *pspec)
337 {
338   GstSMPTE *smpte;
339
340   smpte = GST_SMPTE(object);
341
342   switch (prop_id) {
343     case ARG_TYPE:
344     {
345       GstMask *newmask;
346       gint type = g_value_get_enum (value);
347
348       newmask = gst_mask_factory_new (type, 8, smpte->width, smpte->height);
349       if (newmask) {
350         if (smpte->mask) {
351           gst_mask_destroy (smpte->mask);
352         }
353         smpte->mask = newmask;
354         smpte->type = type;
355       }
356       break;
357     }
358     default:
359       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360       break;
361   }
362 }
363
364 static void
365 gst_smpte_get_property (GObject *object, guint prop_id, 
366                         GValue *value, GParamSpec *pspec)
367 {
368   GstSMPTE *smpte;
369
370   smpte = GST_SMPTE(object);
371
372   switch (prop_id) {
373     case ARG_TYPE:
374       if (smpte->mask) {
375         g_value_set_enum (value, smpte->mask->type);
376       }
377       break;
378     default:
379       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
380       break;
381   }
382 }
383
384
385 static gboolean
386 plugin_init (GModule *module, GstPlugin *plugin)
387 {
388   GstElementFactory *factory;
389
390   factory = gst_element_factory_new("smpte",GST_TYPE_SMPTE,
391                                    &smpte_details);
392   g_return_val_if_fail(factory != NULL, FALSE);
393
394   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (smpte_sink1_factory));
395   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (smpte_sink2_factory));
396   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (smpte_src_factory));
397
398   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
399
400   return TRUE;
401 }
402
403 GstPluginDesc plugin_desc = {
404   GST_VERSION_MAJOR,
405   GST_VERSION_MINOR,
406   "smpte",
407   plugin_init
408 };
409