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