move code from videoflip directory. Why? I don't really know.
[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(parent_class, gst_videoflip_formats + i);
150   }
151 }
152
153 static GstCaps *gst_videoflip_get_capslist(void)
154 {
155   GstVideofilterClass *klass;
156
157   klass = g_type_class_ref(GST_TYPE_VIDEOFILTER);
158
159   return gst_videofilter_class_get_capslist(klass);
160 }
161
162 static GstPadTemplate *
163 gst_videoflip_src_template_factory(void)
164 {
165   static GstPadTemplate *templ = NULL;
166
167   if(!templ){
168     GstCaps *caps = GST_CAPS_NEW("src","video/raw",
169                 "width", GST_PROPS_INT_RANGE (0, G_MAXINT),
170                 "height", GST_PROPS_INT_RANGE (0, G_MAXINT));
171
172     caps = gst_caps_intersect(caps, gst_videoflip_get_capslist ());
173
174     templ = GST_PAD_TEMPLATE_NEW("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps);
175   }
176   return templ;
177 }
178
179 static GstPadTemplate *
180 gst_videoflip_sink_template_factory(void)
181 {
182   static GstPadTemplate *templ = NULL;
183
184   if(!templ){
185     GstCaps *caps = GST_CAPS_NEW("sink","video/raw",
186                 "width", GST_PROPS_INT_RANGE (0, G_MAXINT),
187                 "height", GST_PROPS_INT_RANGE (0, G_MAXINT));
188
189     caps = gst_caps_intersect(caps, gst_videoflip_get_capslist ());
190
191     templ = GST_PAD_TEMPLATE_NEW("src", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
192   }
193   return templ;
194 }
195
196 static void
197 gst_videoflip_init (GstVideoflip *videoflip)
198 {
199   GstVideofilter *videofilter;
200
201   GST_DEBUG ("gst_videoflip_init");
202
203   videofilter = GST_VIDEOFILTER(videoflip);
204
205   videofilter->sinkpad = gst_pad_new_from_template (
206                   GST_PAD_TEMPLATE_GET (gst_videoflip_sink_template_factory),
207                   "sink");
208
209   videofilter->srcpad = gst_pad_new_from_template (
210                   GST_PAD_TEMPLATE_GET (gst_videoflip_src_template_factory),
211                   "src");
212
213   gst_videofilter_postinit(GST_VIDEOFILTER(videoflip));
214 }
215
216 static void
217 gst_videoflip_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
218 {
219   GstVideoflip *videoflip;
220
221   /* it's not null if we got it, but it might not be ours */
222   g_return_if_fail(GST_IS_VIDEOFLIP(object));
223   videoflip = GST_VIDEOFLIP(object);
224
225   GST_DEBUG ("gst_videoflip_set_property");
226   switch (prop_id) {
227     case ARG_METHOD:
228       videoflip->method = g_value_get_enum (value);
229       /* FIXME is this ok? (threading issues) */
230       gst_videoflip_setup(GST_VIDEOFILTER(videoflip));
231       break;
232     default:
233       break;
234   }
235 }
236
237 static void
238 gst_videoflip_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
239 {
240   GstVideoflip *videoflip;
241
242   /* it's not null if we got it, but it might not be ours */
243   g_return_if_fail(GST_IS_VIDEOFLIP(object));
244   videoflip = GST_VIDEOFLIP(object);
245
246   switch (prop_id) {
247     case ARG_METHOD:
248       g_value_set_enum (value, videoflip->method);
249       break;
250     default:
251       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252       break;
253   }
254 }
255
256 static gboolean plugin_init (GModule *module, GstPlugin *plugin)
257 {
258   GstElementFactory *factory;
259
260   if(!gst_library_load("gstvideofilter"))
261     return FALSE;
262
263   /* create an elementfactory for the videoflip element */
264   factory = gst_element_factory_new("videoflip",GST_TYPE_VIDEOFLIP,
265                                    &videoflip_details);
266   g_return_val_if_fail(factory != NULL, FALSE);
267
268   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (gst_videoflip_sink_template_factory));
269   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (gst_videoflip_src_template_factory));
270
271   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
272
273   return TRUE;
274 }
275
276 GstPluginDesc plugin_desc = {
277   GST_VERSION_MAJOR,
278   GST_VERSION_MINOR,
279   "videoflip",
280   plugin_init
281 };
282
283
284 static void gst_videoflip_flip(GstVideoflip *videoflip, unsigned char *dest,
285     unsigned char *src, int sw, int sh, int dw, int dh);
286
287
288 static void gst_videoflip_setup (GstVideofilter *videofilter)
289 {
290   int from_width, from_height;
291   GstVideoflip *videoflip;
292
293   GST_DEBUG("gst_videoflip_setup");
294
295   videoflip = GST_VIDEOFLIP(videofilter);
296
297   from_width = gst_videofilter_get_input_width(videofilter);
298   from_height = gst_videofilter_get_input_height(videofilter);
299
300   if(from_width==0 || from_height==0){
301     return;
302   }
303
304   switch(videoflip->method){
305     case GST_VIDEOFLIP_METHOD_90R:
306     case GST_VIDEOFLIP_METHOD_90L:
307     case GST_VIDEOFLIP_METHOD_TRANS:
308     case GST_VIDEOFLIP_METHOD_OTHER:
309       gst_videofilter_set_output_size(videofilter, from_height, from_width);
310       break;
311     case GST_VIDEOFLIP_METHOD_IDENTITY:
312     case GST_VIDEOFLIP_METHOD_180:
313     case GST_VIDEOFLIP_METHOD_HORIZ:
314     case GST_VIDEOFLIP_METHOD_VERT:
315       gst_videofilter_set_output_size(videofilter, from_width, from_height);
316       break;
317     default:
318       g_assert_not_reached();
319       break;
320   }
321
322   GST_DEBUG ("format=%p \"%s\" from %dx%d to %dx%d",
323                 videofilter->format, videofilter->format->fourcc,
324                 from_width, from_height,
325                 videofilter->to_width, videofilter->to_height);
326
327   if(videoflip->method == GST_VIDEOFLIP_METHOD_IDENTITY){
328     GST_DEBUG ("videoflip: using passthru");
329     videofilter->passthru = TRUE;
330   }else{
331     videofilter->passthru = FALSE;
332   }
333
334   videofilter->from_buf_size = (videofilter->from_width * videofilter->from_height
335                   * videofilter->format->depth) / 8;
336   videofilter->to_buf_size = (videofilter->to_width * videofilter->to_height
337                   * videofilter->format->depth) / 8;
338
339   videofilter->inited = TRUE;
340 }
341
342 static void gst_videoflip_planar411(GstVideofilter *videofilter,
343     void *dest, void *src)
344 {
345   GstVideoflip *videoflip;
346   int sw;
347   int sh;
348   int dw;
349   int dh;
350
351   g_return_if_fail(GST_IS_VIDEOFLIP(videofilter));
352   videoflip = GST_VIDEOFLIP(videofilter);
353
354   sw = videofilter->from_width;
355   sh = videofilter->from_height;
356   dw = videofilter->to_width;
357   dh = videofilter->to_height;
358
359   GST_DEBUG ("videoflip: scaling planar 4:1:1 %dx%d to %dx%d", sw, sh, dw, dh);
360
361   gst_videoflip_flip(videoflip, dest, src, sw, sh, dw, dh);
362
363   src += sw*sh;
364   dest += dw*dh;
365
366   dh = dh>>1;
367   dw = dw>>1;
368   sh = sh>>1;
369   sw = sw>>1;
370
371   gst_videoflip_flip(videoflip, dest, src, sw, sh, dw, dh);
372
373   src += sw*sh;
374   dest += dw*dh;
375
376   gst_videoflip_flip(videoflip, dest, src, sw, sh, dw, dh);
377 }
378
379 static void
380 gst_videoflip_flip(GstVideoflip *videoflip, unsigned char *dest,
381     unsigned char *src, int sw, int sh, int dw, int dh)
382 {
383   int x,y;
384
385   switch(videoflip->method){
386     case GST_VIDEOFLIP_METHOD_90R:
387       for(y=0;y<dh;y++){
388         for(x=0;x<dw;x++){
389           dest[y*dw + x] = src[(sh - 1 - x)*sw + y];
390         }
391       }
392       break;
393     case GST_VIDEOFLIP_METHOD_90L:
394       for(y=0;y<dh;y++){
395         for(x=0;x<dw;x++){
396           dest[y*dw + x] = src[x*sw + (sw - 1 - y)];
397         }
398       }
399       break;
400     case GST_VIDEOFLIP_METHOD_180:
401       for(y=0;y<dh;y++){
402         for(x=0;x<dw;x++){
403           dest[y*dw + x] = src[(sh - 1 - y)*sw + (sw - 1 - x)];
404         }
405       }
406       break;
407     case GST_VIDEOFLIP_METHOD_HORIZ:
408       for(y=0;y<dh;y++){
409         for(x=0;x<dw;x++){
410           dest[y*dw + x] = src[y*sw + (sw - 1 - x)];
411         }
412       }
413       break;
414     case GST_VIDEOFLIP_METHOD_VERT:
415       for(y=0;y<dh;y++){
416         for(x=0;x<dw;x++){
417           dest[y*dw + x] = src[(sh - 1 - y)*sw + x];
418         }
419       }
420       break;
421     case GST_VIDEOFLIP_METHOD_TRANS:
422       for(y=0;y<dh;y++){
423         for(x=0;x<dw;x++){
424           dest[y*dw + x] = src[x*sw + y];
425         }
426       }
427       break;
428     case GST_VIDEOFLIP_METHOD_OTHER:
429       for(y=0;y<dh;y++){
430         for(x=0;x<dw;x++){
431           dest[y*dw + x] = src[(sh - 1 - x)*sw + (sw - 1 - y)];
432         }
433       }
434       break;
435     default:
436       /* FIXME */
437       break;
438   }
439 }
440
441