2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David Schleef <ds@schleef.org>
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.
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.
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.
22 /*#define DEBUG_ENABLED */
26 #include <gstvideoflip.h>
34 /* elementfactory information */
35 static GstElementDetails videoflip_details = {
39 "Flips and rotates video",
41 "David Schleef <ds@schleef.org>",
45 /* GstVideoflip signals and args */
57 static void gst_videoflip_class_init (GstVideoflipClass *klass);
58 static void gst_videoflip_init (GstVideoflip *videoflip);
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);
63 static void gst_videoflip_planar411(GstVideofilter *videofilter, void *dest, void *src);
64 static void gst_videoflip_setup(GstVideofilter *videofilter);
66 static GstVideoflipClass *this_class = NULL;
67 static GstVideofilterClass *parent_class = NULL;
68 static GstElementClass *element_class = NULL;
70 #define GST_TYPE_VIDEOFLIP_METHOD (gst_videoflip_method_get_type())
73 gst_videoflip_method_get_type(void)
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" },
87 if(!videoflip_method_type){
88 videoflip_method_type = g_enum_register_static("GstVideoflipMethod",
91 return videoflip_method_type;
95 gst_videoflip_get_type (void)
97 static GType videoflip_type = 0;
99 if (!videoflip_type) {
100 static const GTypeInfo videoflip_info = {
101 sizeof(GstVideoflipClass), NULL,
103 (GClassInitFunc)gst_videoflip_class_init,
106 sizeof(GstVideoflip),
108 (GInstanceInitFunc)gst_videoflip_init,
110 videoflip_type = g_type_register_static(GST_TYPE_VIDEOFILTER, "GstVideoflip", &videoflip_info, 0);
112 return videoflip_type;
115 static GstVideofilterFormat gst_videoflip_formats[] = {
117 { "YV12", 12, gst_videoflip_planar411, },
118 { "I420", 12, gst_videoflip_planar411, },
119 { "IYUV", 12, gst_videoflip_planar411, },
123 gst_videoflip_class_init (GstVideoflipClass *klass)
125 GObjectClass *gobject_class;
126 GstElementClass *gstelement_class;
127 GstVideofilterClass *gstvideofilter_class;
130 gobject_class = (GObjectClass*)klass;
131 gstelement_class = (GstElementClass*)klass;
132 gstvideofilter_class = (GstVideofilterClass *)klass;
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,
140 parent_class = g_type_class_ref(GST_TYPE_VIDEOFILTER);
141 element_class = g_type_class_ref(GST_TYPE_ELEMENT);
143 gobject_class->set_property = gst_videoflip_set_property;
144 gobject_class->get_property = gst_videoflip_get_property;
146 gstvideofilter_class->setup = gst_videoflip_setup;
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);
154 static GstCaps *gst_videoflip_get_capslist(void)
156 GstVideofilterClass *klass;
158 klass = g_type_class_ref(GST_TYPE_VIDEOFILTER);
160 return gst_videofilter_class_get_capslist(klass);
163 static GstPadTemplate *
164 gst_videoflip_src_template_factory(void)
166 static GstPadTemplate *templ = NULL;
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));
173 caps = gst_caps_intersect(caps, gst_videoflip_get_capslist ());
175 templ = GST_PAD_TEMPLATE_NEW("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps);
180 static GstPadTemplate *
181 gst_videoflip_sink_template_factory(void)
183 static GstPadTemplate *templ = NULL;
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));
190 caps = gst_caps_intersect(caps, gst_videoflip_get_capslist ());
192 templ = GST_PAD_TEMPLATE_NEW("src", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
198 gst_videoflip_init (GstVideoflip *videoflip)
200 GstVideofilter *videofilter;
202 GST_DEBUG ("gst_videoflip_init");
204 videofilter = GST_VIDEOFILTER(videoflip);
206 videofilter->sinkpad = gst_pad_new_from_template (
207 GST_PAD_TEMPLATE_GET (gst_videoflip_sink_template_factory),
210 videofilter->srcpad = gst_pad_new_from_template (
211 GST_PAD_TEMPLATE_GET (gst_videoflip_src_template_factory),
214 gst_videofilter_postinit(GST_VIDEOFILTER(videoflip));
218 gst_videoflip_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
220 GstVideoflip *videoflip;
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);
226 GST_DEBUG ("gst_videoflip_set_property");
229 videoflip->method = g_value_get_enum (value);
230 /* FIXME is this ok? (threading issues) */
231 gst_videoflip_setup(GST_VIDEOFILTER(videoflip));
239 gst_videoflip_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
241 GstVideoflip *videoflip;
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);
249 g_value_set_enum (value, videoflip->method);
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257 static gboolean plugin_init (GModule *module, GstPlugin *plugin)
259 GstElementFactory *factory;
261 if(!gst_library_load("gstvideofilter"))
264 /* create an elementfactory for the videoflip element */
265 factory = gst_element_factory_new("videoflip",GST_TYPE_VIDEOFLIP,
267 g_return_val_if_fail(factory != NULL, FALSE);
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));
272 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
277 GstPluginDesc plugin_desc = {
285 static void gst_videoflip_flip(GstVideoflip *videoflip, unsigned char *dest,
286 unsigned char *src, int sw, int sh, int dw, int dh);
289 static void gst_videoflip_setup (GstVideofilter *videofilter)
291 int from_width, from_height;
292 GstVideoflip *videoflip;
294 GST_DEBUG("gst_videoflip_setup");
296 videoflip = GST_VIDEOFLIP(videofilter);
298 from_width = gst_videofilter_get_input_width(videofilter);
299 from_height = gst_videofilter_get_input_height(videofilter);
301 if(from_width==0 || from_height==0){
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);
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);
319 g_assert_not_reached();
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);
328 if(videoflip->method == GST_VIDEOFLIP_METHOD_IDENTITY){
329 GST_DEBUG ("videoflip: using passthru");
330 videofilter->passthru = TRUE;
332 videofilter->passthru = FALSE;
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;
340 videofilter->inited = TRUE;
343 static void gst_videoflip_planar411(GstVideofilter *videofilter,
344 void *dest, void *src)
346 GstVideoflip *videoflip;
352 g_return_if_fail(GST_IS_VIDEOFLIP(videofilter));
353 videoflip = GST_VIDEOFLIP(videofilter);
355 sw = videofilter->from_width;
356 sh = videofilter->from_height;
357 dw = videofilter->to_width;
358 dh = videofilter->to_height;
360 GST_DEBUG ("videoflip: scaling planar 4:1:1 %dx%d to %dx%d", sw, sh, dw, dh);
362 gst_videoflip_flip(videoflip, dest, src, sw, sh, dw, dh);
372 gst_videoflip_flip(videoflip, dest, src, sw, sh, dw, dh);
377 gst_videoflip_flip(videoflip, dest, src, sw, sh, dw, dh);
381 gst_videoflip_flip(GstVideoflip *videoflip, unsigned char *dest,
382 unsigned char *src, int sw, int sh, int dw, int dh)
386 switch(videoflip->method){
387 case GST_VIDEOFLIP_METHOD_90R:
390 dest[y*dw + x] = src[(sh - 1 - x)*sw + y];
394 case GST_VIDEOFLIP_METHOD_90L:
397 dest[y*dw + x] = src[x*sw + (sw - 1 - y)];
401 case GST_VIDEOFLIP_METHOD_180:
404 dest[y*dw + x] = src[(sh - 1 - y)*sw + (sw - 1 - x)];
408 case GST_VIDEOFLIP_METHOD_HORIZ:
411 dest[y*dw + x] = src[y*sw + (sw - 1 - x)];
415 case GST_VIDEOFLIP_METHOD_VERT:
418 dest[y*dw + x] = src[(sh - 1 - y)*sw + x];
422 case GST_VIDEOFLIP_METHOD_TRANS:
425 dest[y*dw + x] = src[x*sw + y];
429 case GST_VIDEOFLIP_METHOD_OTHER:
432 dest[y*dw + x] = src[(sh - 1 - x)*sw + (sw - 1 - y)];