da9a76b9549008d0d574c63dbf7cd96bac1da826
[platform/upstream/gstreamer.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  * This file was (probably) generated from gstvideoflip.c,
23  * gstvideoflip.c,v 1.7 2003/11/08 02:48:59 dschleef Exp 
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 /*#define DEBUG_ENABLED */
31 #include "gstvideoflip.h"
32 #include <string.h>
33
34 /* GstVideoflip signals and args */
35 enum
36 {
37   /* FILL ME */
38   LAST_SIGNAL
39 };
40
41 enum
42 {
43   ARG_0,
44   ARG_METHOD
45       /* FILL ME */
46 };
47
48 GST_DEBUG_CATEGORY_STATIC (gst_videoflip_debug);
49 #define GST_CAT_DEFAULT gst_videoflip_debug
50
51 static void gst_videoflip_base_init (gpointer g_class);
52 static void gst_videoflip_class_init (gpointer g_class, gpointer class_data);
53 static void gst_videoflip_init (GTypeInstance * instance, gpointer g_class);
54
55 static void gst_videoflip_set_property (GObject * object, guint prop_id,
56     const GValue * value, GParamSpec * pspec);
57 static void gst_videoflip_get_property (GObject * object, guint prop_id,
58     GValue * value, GParamSpec * pspec);
59
60 static void gst_videoflip_planar411 (GstVideofilter * videofilter, void *dest,
61     void *src);
62 static void gst_videoflip_setup (GstVideofilter * videofilter);
63
64 #define GST_TYPE_VIDEOFLIP_METHOD (gst_videoflip_method_get_type())
65
66 static GType
67 gst_videoflip_method_get_type (void)
68 {
69   static GType videoflip_method_type = 0;
70   static GEnumValue videoflip_methods[] = {
71     {GST_VIDEOFLIP_METHOD_IDENTITY, "Identity (no rotation)", "none"},
72     {GST_VIDEOFLIP_METHOD_90R, "Rotate clockwise 90 degrees", "clockwise"},
73     {GST_VIDEOFLIP_METHOD_180, "Rotate 180 degrees", "rotate-180"},
74     {GST_VIDEOFLIP_METHOD_90L, "Rotate counter-clockwise 90 degrees",
75           "counterclockwise"},
76     {GST_VIDEOFLIP_METHOD_HORIZ, "Flip horizontally", "horizontal-flip"},
77     {GST_VIDEOFLIP_METHOD_VERT, "Flip vertically", "vertical-flip"},
78     {GST_VIDEOFLIP_METHOD_TRANS,
79         "Flip across upper left/lower right diagonal", "upper-left-diagonal"},
80     {GST_VIDEOFLIP_METHOD_OTHER,
81         "Flip across upper right/lower left diagonal", "upper-right-diagonal"},
82     {0, NULL, NULL},
83   };
84
85   if (!videoflip_method_type) {
86     videoflip_method_type = g_enum_register_static ("GstVideoflipMethod",
87         videoflip_methods);
88   }
89   return videoflip_method_type;
90 }
91
92 GType
93 gst_videoflip_get_type (void)
94 {
95   static GType videoflip_type = 0;
96
97   if (!videoflip_type) {
98     static const GTypeInfo videoflip_info = {
99       sizeof (GstVideoflipClass),
100       gst_videoflip_base_init,
101       NULL,
102       gst_videoflip_class_init,
103       NULL,
104       NULL,
105       sizeof (GstVideoflip),
106       0,
107       gst_videoflip_init,
108     };
109
110     videoflip_type = g_type_register_static (GST_TYPE_VIDEOFILTER,
111         "GstVideoflip", &videoflip_info, 0);
112   }
113   return videoflip_type;
114 }
115
116 static GstVideofilterFormat gst_videoflip_formats[] = {
117   /* planar */
118   {"YV12", 12, gst_videoflip_planar411,},
119   {"I420", 12, gst_videoflip_planar411,},
120   {"IYUV", 12, gst_videoflip_planar411,},
121 };
122
123 static void
124 gst_videoflip_base_init (gpointer g_class)
125 {
126   static GstElementDetails videoflip_details =
127       GST_ELEMENT_DETAILS ("Video Flipper",
128       "Filter/Effect/Video",
129       "Flips and rotates video",
130       "David Schleef <ds@schleef.org>");
131   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
132   GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
133   int i;
134
135   gst_element_class_set_details (element_class, &videoflip_details);
136
137   for (i = 0; i < G_N_ELEMENTS (gst_videoflip_formats); i++) {
138     gst_videofilter_class_add_format (videofilter_class,
139         gst_videoflip_formats + i);
140   }
141
142   gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
143 }
144
145 static void
146 gst_videoflip_class_init (gpointer g_class, gpointer class_data)
147 {
148   GObjectClass *gobject_class;
149   GstVideofilterClass *videofilter_class;
150
151   gobject_class = G_OBJECT_CLASS (g_class);
152   videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
153
154   gobject_class->set_property = gst_videoflip_set_property;
155   gobject_class->get_property = gst_videoflip_get_property;
156
157   g_object_class_install_property (gobject_class, ARG_METHOD,
158       g_param_spec_enum ("method", "method", "method",
159           GST_TYPE_VIDEOFLIP_METHOD, GST_VIDEOFLIP_METHOD_90R,
160           G_PARAM_READWRITE));
161
162   videofilter_class->setup = gst_videoflip_setup;
163 }
164
165 static void
166 gst_videoflip_init (GTypeInstance * instance, gpointer g_class)
167 {
168   GstVideoflip *videoflip = GST_VIDEOFLIP (instance);
169   GstVideofilter *videofilter;
170
171   GST_DEBUG_OBJECT (videoflip, "gst_videoflip_init");
172
173   videofilter = GST_VIDEOFILTER (videoflip);
174
175   /* do stuff */
176 }
177
178 static void
179 gst_videoflip_set_property (GObject * object, guint prop_id,
180     const GValue * value, GParamSpec * pspec)
181 {
182   GstVideoflip *videoflip;
183   GstVideofilter *videofilter;
184
185   g_return_if_fail (GST_IS_VIDEOFLIP (object));
186   videoflip = GST_VIDEOFLIP (object);
187   videofilter = GST_VIDEOFILTER (object);
188
189   GST_DEBUG_OBJECT (videoflip, "gst_videoflip_set_property");
190   switch (prop_id) {
191     case ARG_METHOD:
192       videoflip->method = g_value_get_enum (value);
193       if (videofilter->inited) {
194         GST_DEBUG_OBJECT (videoflip, "setting up videoflip again");
195         gst_videofilter_setup (videofilter);
196       }
197       break;
198     default:
199       break;
200   }
201 }
202
203 static void
204 gst_videoflip_get_property (GObject * object, guint prop_id, GValue * value,
205     GParamSpec * pspec)
206 {
207   GstVideoflip *videoflip;
208
209   g_return_if_fail (GST_IS_VIDEOFLIP (object));
210   videoflip = GST_VIDEOFLIP (object);
211
212   switch (prop_id) {
213     case ARG_METHOD:
214       g_value_set_enum (value, videoflip->method);
215       break;
216     default:
217       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218       break;
219   }
220 }
221
222 static gboolean
223 plugin_init (GstPlugin * plugin)
224 {
225   GST_DEBUG_CATEGORY_INIT (gst_videoflip_debug, "videoflip", 0, "videoflip");
226
227   return gst_element_register (plugin, "videoflip", GST_RANK_NONE,
228       GST_TYPE_VIDEOFLIP);
229 }
230
231 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
232     GST_VERSION_MINOR,
233     "videoflip",
234     "Flips and rotates video",
235     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
236
237 static void gst_videoflip_flip (GstVideoflip * videoflip,
238     unsigned char *dest, unsigned char *src, int sw, int sh, int dw, int dh);
239
240
241 static void
242 gst_videoflip_setup (GstVideofilter * videofilter)
243 {
244   int from_width, from_height;
245   GstVideoflip *videoflip;
246
247   videoflip = GST_VIDEOFLIP (videofilter);
248   GST_DEBUG_OBJECT (videoflip, "gst_videoflip_setup");
249
250   from_width = gst_videofilter_get_input_width (videofilter);
251   from_height = gst_videofilter_get_input_height (videofilter);
252
253   if (from_width == 0 || from_height == 0) {
254     return;
255   }
256
257   switch (videoflip->method) {
258     case GST_VIDEOFLIP_METHOD_90R:
259     case GST_VIDEOFLIP_METHOD_90L:
260     case GST_VIDEOFLIP_METHOD_TRANS:
261     case GST_VIDEOFLIP_METHOD_OTHER:
262       gst_videofilter_set_output_size (videofilter, from_height, from_width);
263       break;
264     case GST_VIDEOFLIP_METHOD_IDENTITY:
265     case GST_VIDEOFLIP_METHOD_180:
266     case GST_VIDEOFLIP_METHOD_HORIZ:
267     case GST_VIDEOFLIP_METHOD_VERT:
268       gst_videofilter_set_output_size (videofilter, from_width, from_height);
269       break;
270     default:
271       g_assert_not_reached ();
272       break;
273   }
274
275   GST_DEBUG_OBJECT (videoflip, "format=%p \"%s\" from %dx%d to %dx%d",
276       videofilter->format, videofilter->format->fourcc,
277       from_width, from_height, videofilter->to_width, videofilter->to_height);
278
279   if (videoflip->method == GST_VIDEOFLIP_METHOD_IDENTITY) {
280     GST_DEBUG_OBJECT (videoflip, "videoflip: using passthru");
281     videofilter->passthru = TRUE;
282   } else {
283     videofilter->passthru = FALSE;
284   }
285 }
286
287 static void
288 gst_videoflip_planar411 (GstVideofilter * videofilter, void *dest, void *src)
289 {
290   GstVideoflip *videoflip;
291   int sw;
292   int sh;
293   int dw;
294   int dh;
295
296   g_return_if_fail (GST_IS_VIDEOFLIP (videofilter));
297   videoflip = GST_VIDEOFLIP (videofilter);
298
299   sw = videofilter->from_width;
300   sh = videofilter->from_height;
301   dw = videofilter->to_width;
302   dh = videofilter->to_height;
303
304   GST_LOG_OBJECT (videoflip, "videoflip: scaling planar 4:1:1 %dx%d to %dx%d",
305       sw, sh, dw, dh);
306
307   gst_videoflip_flip (videoflip, dest, src, sw, sh, dw, dh);
308
309   src += sw * sh;
310   dest += dw * dh;
311
312   dh = dh >> 1;
313   dw = dw >> 1;
314   sh = sh >> 1;
315   sw = sw >> 1;
316
317   gst_videoflip_flip (videoflip, dest, src, sw, sh, dw, dh);
318
319   src += sw * sh;
320   dest += dw * dh;
321
322   gst_videoflip_flip (videoflip, dest, src, sw, sh, dw, dh);
323 }
324
325 static void
326 gst_videoflip_flip (GstVideoflip * videoflip, unsigned char *dest,
327     unsigned char *src, int sw, int sh, int dw, int dh)
328 {
329   int x, y;
330
331   switch (videoflip->method) {
332     case GST_VIDEOFLIP_METHOD_90R:
333       for (y = 0; y < dh; y++) {
334         for (x = 0; x < dw; x++) {
335           dest[y * dw + x] = src[(sh - 1 - x) * sw + y];
336         }
337       }
338       break;
339     case GST_VIDEOFLIP_METHOD_90L:
340       for (y = 0; y < dh; y++) {
341         for (x = 0; x < dw; x++) {
342           dest[y * dw + x] = src[x * sw + (sw - 1 - y)];
343         }
344       }
345       break;
346     case GST_VIDEOFLIP_METHOD_180:
347       for (y = 0; y < dh; y++) {
348         for (x = 0; x < dw; x++) {
349           dest[y * dw + x] = src[(sh - 1 - y) * sw + (sw - 1 - x)];
350         }
351       }
352       break;
353     case GST_VIDEOFLIP_METHOD_HORIZ:
354       for (y = 0; y < dh; y++) {
355         for (x = 0; x < dw; x++) {
356           dest[y * dw + x] = src[y * sw + (sw - 1 - x)];
357         }
358       }
359       break;
360     case GST_VIDEOFLIP_METHOD_VERT:
361       for (y = 0; y < dh; y++) {
362         for (x = 0; x < dw; x++) {
363           dest[y * dw + x] = src[(sh - 1 - y) * sw + x];
364         }
365       }
366       break;
367     case GST_VIDEOFLIP_METHOD_TRANS:
368       for (y = 0; y < dh; y++) {
369         for (x = 0; x < dw; x++) {
370           dest[y * dw + x] = src[x * sw + y];
371         }
372       }
373       break;
374     case GST_VIDEOFLIP_METHOD_OTHER:
375       for (y = 0; y < dh; y++) {
376         for (x = 0; x < dw; x++) {
377           dest[y * dw + x] = src[(sh - 1 - x) * sw + (sw - 1 - y)];
378         }
379       }
380       break;
381     default:
382       /* FIXME */
383       break;
384   }
385 }