add formats to base class, not the actual videofilter class
[platform/upstream/gst-plugins-good.git] / gst / videofilter / gstvideoflip.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2003> David Schleef <ds@schleef.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21
22 /*#define DEBUG_ENABLED */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <gstvideoflip.h>
27
28 #include <stdlib.h>
29 #include <math.h>
30 #include <string.h>
31
32
33
34 /* elementfactory information */
35 static GstElementDetails videoflip_details = {
36   "Video flipper",
37   "Filter/Video",
38   "LGPL",
39   "Flips and rotates video",
40   VERSION,
41   "David Schleef <ds@schleef.org>",
42   "(C) 2003",
43 };
44
45 /* GstVideoflip signals and args */
46 enum {
47   /* FILL ME */
48   LAST_SIGNAL
49 };
50
51 enum {
52   ARG_0,
53   ARG_METHOD,
54   /* FILL ME */
55 };
56
57 static void     gst_videoflip_class_init        (GstVideoflipClass *klass);
58 static void     gst_videoflip_init              (GstVideoflip *videoflip);
59
60 static void     gst_videoflip_set_property              (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
61 static void     gst_videoflip_get_property              (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
62
63 static void gst_videoflip_planar411(GstVideofilter *videofilter, void *dest, void *src);
64 static void gst_videoflip_setup(GstVideofilter *videofilter);
65
66 static GstVideoflipClass *this_class = NULL;
67 static GstVideofilterClass *parent_class = NULL;
68 static GstElementClass *element_class = NULL;
69
70 #define GST_TYPE_VIDEOFLIP_METHOD (gst_videoflip_method_get_type())
71
72 static GType
73 gst_videoflip_method_get_type(void)
74 {
75   static GType videoflip_method_type = 0;
76   static GEnumValue videoflip_methods[] = {
77     { GST_VIDEOFLIP_METHOD_IDENTITY,    "0", "Identity (no rotation)" },
78     { GST_VIDEOFLIP_METHOD_90R,         "1", "Rotate clockwise 90 degrees" },
79     { GST_VIDEOFLIP_METHOD_180,         "2", "Rotate 180 degrees" },
80     { GST_VIDEOFLIP_METHOD_90L,         "3", "Rotate counter-clockwise 90 degrees" },
81     { GST_VIDEOFLIP_METHOD_HORIZ,       "4", "Flip horizontally" },
82     { GST_VIDEOFLIP_METHOD_VERT,        "5", "Flip vertically" },
83     { GST_VIDEOFLIP_METHOD_TRANS,       "6", "Flip across upper left/lower right diagonal" },
84     { GST_VIDEOFLIP_METHOD_OTHER,       "7", "Flip across upper right/lower left diagonal" },
85     { 0, NULL, NULL },
86   };
87   if(!videoflip_method_type){
88     videoflip_method_type = g_enum_register_static("GstVideoflipMethod",
89         videoflip_methods);
90   }
91   return videoflip_method_type;
92 }
93
94 GType
95 gst_videoflip_get_type (void)
96 {
97   static GType videoflip_type = 0;
98
99   if (!videoflip_type) {
100     static const GTypeInfo videoflip_info = {
101       sizeof(GstVideoflipClass),      NULL,
102       NULL,
103       (GClassInitFunc)gst_videoflip_class_init,
104       NULL,
105       NULL,
106       sizeof(GstVideoflip),
107       0,
108       (GInstanceInitFunc)gst_videoflip_init,
109     };
110     videoflip_type = g_type_register_static(GST_TYPE_VIDEOFILTER, "GstVideoflip", &videoflip_info, 0);
111   }
112   return videoflip_type;
113 }
114
115 static GstVideofilterFormat gst_videoflip_formats[] = {
116   /* planar */
117   { "YV12", 12, gst_videoflip_planar411, },
118   { "I420", 12, gst_videoflip_planar411, },
119   { "IYUV", 12, gst_videoflip_planar411, },
120 };
121
122 static void
123 gst_videoflip_class_init (GstVideoflipClass *klass)
124 {
125   GObjectClass *gobject_class;
126   GstElementClass *gstelement_class;
127   GstVideofilterClass *gstvideofilter_class;
128   int i;
129
130   gobject_class = (GObjectClass*)klass;
131   gstelement_class = (GstElementClass*)klass;
132   gstvideofilter_class = (GstVideofilterClass *)klass;
133
134   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_METHOD,
135       g_param_spec_enum("method","method","method",
136       GST_TYPE_VIDEOFLIP_METHOD, GST_VIDEOFLIP_METHOD_90R,
137       G_PARAM_READWRITE));
138
139   this_class = klass;
140   parent_class = g_type_class_ref(GST_TYPE_VIDEOFILTER);
141   element_class = g_type_class_ref(GST_TYPE_ELEMENT);
142
143   gobject_class->set_property = gst_videoflip_set_property;
144   gobject_class->get_property = gst_videoflip_get_property;
145
146   gstvideofilter_class->setup = gst_videoflip_setup;
147
148   for(i=0;i<G_N_ELEMENTS(gst_videoflip_formats);i++){
149     gst_videofilter_class_add_format(gstvideofilter_class,
150         gst_videoflip_formats + i);
151   }
152 }
153
154 static GstCaps *gst_videoflip_get_capslist(void)
155 {
156   GstVideofilterClass *klass;
157
158   klass = g_type_class_ref(GST_TYPE_VIDEOFILTER);
159
160   return gst_videofilter_class_get_capslist(klass);
161 }
162
163 static GstPadTemplate *
164 gst_videoflip_src_template_factory(void)
165 {
166   static GstPadTemplate *templ = NULL;
167
168   if(!templ){
169     GstCaps *caps = GST_CAPS_NEW("src","video/raw",
170                 "width", GST_PROPS_INT_RANGE (0, G_MAXINT),
171                 "height", GST_PROPS_INT_RANGE (0, G_MAXINT));
172
173     caps = gst_caps_intersect(caps, gst_videoflip_get_capslist ());
174
175     templ = GST_PAD_TEMPLATE_NEW("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps);
176   }
177   return templ;
178 }
179
180 static GstPadTemplate *
181 gst_videoflip_sink_template_factory(void)
182 {
183   static GstPadTemplate *templ = NULL;
184
185   if(!templ){
186     GstCaps *caps = GST_CAPS_NEW("sink","video/raw",
187                 "width", GST_PROPS_INT_RANGE (0, G_MAXINT),
188                 "height", GST_PROPS_INT_RANGE (0, G_MAXINT));
189
190     caps = gst_caps_intersect(caps, gst_videoflip_get_capslist ());
191
192     templ = GST_PAD_TEMPLATE_NEW("src", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
193   }
194   return templ;
195 }
196
197 static void
198 gst_videoflip_init (GstVideoflip *videoflip)
199 {
200   GstVideofilter *videofilter;
201
202   GST_DEBUG ("gst_videoflip_init");
203
204   videofilter = GST_VIDEOFILTER(videoflip);
205
206   videofilter->sinkpad = gst_pad_new_from_template (
207                   GST_PAD_TEMPLATE_GET (gst_videoflip_sink_template_factory),
208                   "sink");
209
210   videofilter->srcpad = gst_pad_new_from_template (
211                   GST_PAD_TEMPLATE_GET (gst_videoflip_src_template_factory),
212                   "src");
213
214   gst_videofilter_postinit(GST_VIDEOFILTER(videoflip));
215 }
216
217 static void
218 gst_videoflip_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
219 {
220   GstVideoflip *videoflip;
221
222   /* it's not null if we got it, but it might not be ours */
223   g_return_if_fail(GST_IS_VIDEOFLIP(object));
224   videoflip = GST_VIDEOFLIP(object);
225
226   GST_DEBUG ("gst_videoflip_set_property");
227   switch (prop_id) {
228     case ARG_METHOD:
229       videoflip->method = g_value_get_enum (value);
230       /* FIXME is this ok? (threading issues) */
231       gst_videoflip_setup(GST_VIDEOFILTER(videoflip));
232       break;
233     default:
234       break;
235   }
236 }
237
238 static void
239 gst_videoflip_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
240 {
241   GstVideoflip *videoflip;
242
243   /* it's not null if we got it, but it might not be ours */
244   g_return_if_fail(GST_IS_VIDEOFLIP(object));
245   videoflip = GST_VIDEOFLIP(object);
246
247   switch (prop_id) {
248     case ARG_METHOD:
249       g_value_set_enum (value, videoflip->method);
250       break;
251     default:
252       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
253       break;
254   }
255 }
256
257 static gboolean plugin_init (GModule *module, GstPlugin *plugin)
258 {
259   GstElementFactory *factory;
260
261   if(!gst_library_load("gstvideofilter"))
262     return FALSE;
263
264   /* create an elementfactory for the videoflip element */
265   factory = gst_element_factory_new("videoflip",GST_TYPE_VIDEOFLIP,
266                                    &videoflip_details);
267   g_return_val_if_fail(factory != NULL, FALSE);
268
269   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (gst_videoflip_sink_template_factory));
270   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (gst_videoflip_src_template_factory));
271
272   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
273
274   return TRUE;
275 }
276
277 GstPluginDesc plugin_desc = {
278   GST_VERSION_MAJOR,
279   GST_VERSION_MINOR,
280   "videoflip",
281   plugin_init
282 };
283
284
285 static void gst_videoflip_flip(GstVideoflip *videoflip, unsigned char *dest,
286     unsigned char *src, int sw, int sh, int dw, int dh);
287
288
289 static void gst_videoflip_setup (GstVideofilter *videofilter)
290 {
291   int from_width, from_height;
292   GstVideoflip *videoflip;
293
294   GST_DEBUG("gst_videoflip_setup");
295
296   videoflip = GST_VIDEOFLIP(videofilter);
297
298   from_width = gst_videofilter_get_input_width(videofilter);
299   from_height = gst_videofilter_get_input_height(videofilter);
300
301   if(from_width==0 || from_height==0){
302     return;
303   }
304
305   switch(videoflip->method){
306     case GST_VIDEOFLIP_METHOD_90R:
307     case GST_VIDEOFLIP_METHOD_90L:
308     case GST_VIDEOFLIP_METHOD_TRANS:
309     case GST_VIDEOFLIP_METHOD_OTHER:
310       gst_videofilter_set_output_size(videofilter, from_height, from_width);
311       break;
312     case GST_VIDEOFLIP_METHOD_IDENTITY:
313     case GST_VIDEOFLIP_METHOD_180:
314     case GST_VIDEOFLIP_METHOD_HORIZ:
315     case GST_VIDEOFLIP_METHOD_VERT:
316       gst_videofilter_set_output_size(videofilter, from_width, from_height);
317       break;
318     default:
319       g_assert_not_reached();
320       break;
321   }
322
323   GST_DEBUG ("format=%p \"%s\" from %dx%d to %dx%d",
324                 videofilter->format, videofilter->format->fourcc,
325                 from_width, from_height,
326                 videofilter->to_width, videofilter->to_height);
327
328   if(videoflip->method == GST_VIDEOFLIP_METHOD_IDENTITY){
329     GST_DEBUG ("videoflip: using passthru");
330     videofilter->passthru = TRUE;
331   }else{
332     videofilter->passthru = FALSE;
333   }
334
335   videofilter->from_buf_size = (videofilter->from_width * videofilter->from_height
336                   * videofilter->format->depth) / 8;
337   videofilter->to_buf_size = (videofilter->to_width * videofilter->to_height
338                   * videofilter->format->depth) / 8;
339
340   videofilter->inited = TRUE;
341 }
342
343 static void gst_videoflip_planar411(GstVideofilter *videofilter,
344     void *dest, void *src)
345 {
346   GstVideoflip *videoflip;
347   int sw;
348   int sh;
349   int dw;
350   int dh;
351
352   g_return_if_fail(GST_IS_VIDEOFLIP(videofilter));
353   videoflip = GST_VIDEOFLIP(videofilter);
354
355   sw = videofilter->from_width;
356   sh = videofilter->from_height;
357   dw = videofilter->to_width;
358   dh = videofilter->to_height;
359
360   GST_DEBUG ("videoflip: scaling planar 4:1:1 %dx%d to %dx%d", sw, sh, dw, dh);
361
362   gst_videoflip_flip(videoflip, dest, src, sw, sh, dw, dh);
363
364   src += sw*sh;
365   dest += dw*dh;
366
367   dh = dh>>1;
368   dw = dw>>1;
369   sh = sh>>1;
370   sw = sw>>1;
371
372   gst_videoflip_flip(videoflip, dest, src, sw, sh, dw, dh);
373
374   src += sw*sh;
375   dest += dw*dh;
376
377   gst_videoflip_flip(videoflip, dest, src, sw, sh, dw, dh);
378 }
379
380 static void
381 gst_videoflip_flip(GstVideoflip *videoflip, unsigned char *dest,
382     unsigned char *src, int sw, int sh, int dw, int dh)
383 {
384   int x,y;
385
386   switch(videoflip->method){
387     case GST_VIDEOFLIP_METHOD_90R:
388       for(y=0;y<dh;y++){
389         for(x=0;x<dw;x++){
390           dest[y*dw + x] = src[(sh - 1 - x)*sw + y];
391         }
392       }
393       break;
394     case GST_VIDEOFLIP_METHOD_90L:
395       for(y=0;y<dh;y++){
396         for(x=0;x<dw;x++){
397           dest[y*dw + x] = src[x*sw + (sw - 1 - y)];
398         }
399       }
400       break;
401     case GST_VIDEOFLIP_METHOD_180:
402       for(y=0;y<dh;y++){
403         for(x=0;x<dw;x++){
404           dest[y*dw + x] = src[(sh - 1 - y)*sw + (sw - 1 - x)];
405         }
406       }
407       break;
408     case GST_VIDEOFLIP_METHOD_HORIZ:
409       for(y=0;y<dh;y++){
410         for(x=0;x<dw;x++){
411           dest[y*dw + x] = src[y*sw + (sw - 1 - x)];
412         }
413       }
414       break;
415     case GST_VIDEOFLIP_METHOD_VERT:
416       for(y=0;y<dh;y++){
417         for(x=0;x<dw;x++){
418           dest[y*dw + x] = src[(sh - 1 - y)*sw + x];
419         }
420       }
421       break;
422     case GST_VIDEOFLIP_METHOD_TRANS:
423       for(y=0;y<dh;y++){
424         for(x=0;x<dw;x++){
425           dest[y*dw + x] = src[x*sw + y];
426         }
427       }
428       break;
429     case GST_VIDEOFLIP_METHOD_OTHER:
430       for(y=0;y<dh;y++){
431         for(x=0;x<dw;x++){
432           dest[y*dw + x] = src[(sh - 1 - x)*sw + (sw - 1 - y)];
433         }
434       }
435       break;
436     default:
437       /* FIXME */
438       break;
439   }
440 }
441
442