|
|
|
|
1 |
/* GStreamer |
| 2 |
* Copyright (C) <2010> Filippo Argiolas <filippo.argiolas@gmail.com> |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Library General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Library General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Library General Public |
| 15 |
* License along with this library; if not, write to the |
| 16 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 |
* Boston, MA 02111-1307, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
/** |
| 21 |
* SECTION:element-squeeze |
| 22 |
* |
| 23 |
* Squeeze applies a transform that diverges at the center of the |
| 24 |
* video giving the impression that the video surface is being pinched |
| 25 |
* |
| 26 |
* <refsect2> |
| 27 |
* <title>Example launch line</title> |
| 28 |
* |[ |
| 29 |
* gst-launch -v videotestsrc ! cheesesqueeze ! ffmpegcolorspace ! autovideosink |
| 30 |
* ]| This pipeline shows the effect of cheesesqueeze on a test stream. |
| 31 |
* </refsect2> |
| 32 |
*/ |
| 33 |
|
| 34 |
#ifdef HAVE_CONFIG_H |
| 35 |
#include "config.h" |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <gst/video/video.h> |
| 39 |
#include "gstsqueeze.h" |
| 40 |
|
| 41 |
enum |
| 42 |
{ |
| 43 |
PROP_0, |
| 44 |
PROP_METHOD |
| 45 |
}; |
| 46 |
|
| 47 |
static void gst_cheese_squeeze_set_property (GObject * object, guint prop_id, |
| 48 |
const GValue * value, GParamSpec * pspec); |
| 49 |
static void gst_cheese_squeeze_get_property (GObject * object, guint prop_id, |
| 50 |
GValue * value, GParamSpec * pspec); |
| 51 |
|
| 52 |
|
| 53 |
GST_BOILERPLATE (GstCheeseSqueeze, gst_cheese_squeeze, GstVideoFilter, |
| 54 |
GST_TYPE_VIDEO_FILTER); |
| 55 |
|
| 56 |
static void init_distortion_map (GstCheeseSqueeze * filter); |
| 57 |
|
| 58 |
static GstStaticPadTemplate gst_cheese_squeeze_src_template = |
| 59 |
GST_STATIC_PAD_TEMPLATE ("src", |
| 60 |
GST_PAD_SRC, |
| 61 |
GST_PAD_ALWAYS, |
| 62 |
GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";" |
| 63 |
GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR ";" |
| 64 |
GST_VIDEO_CAPS_YUV ("AYUV")) |
| 65 |
); |
| 66 |
|
| 67 |
static GstStaticPadTemplate gst_cheese_squeeze_sink_template = |
| 68 |
GST_STATIC_PAD_TEMPLATE ("sink", |
| 69 |
GST_PAD_SINK, |
| 70 |
GST_PAD_ALWAYS, |
| 71 |
GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";" |
| 72 |
GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR ";" |
| 73 |
GST_VIDEO_CAPS_YUV ("AYUV")) |
| 74 |
); |
| 75 |
|
| 76 |
static gboolean |
| 77 |
gst_cheese_squeeze_set_caps (GstBaseTransform * btrans, GstCaps * incaps, |
| 78 |
GstCaps * outcaps) |
| 79 |
{ |
| 80 |
GstCheeseSqueeze *filter = GST_CHEESE_SQUEEZE (btrans); |
| 81 |
GstStructure *structure; |
| 82 |
gboolean ret = FALSE; |
| 83 |
|
| 84 |
structure = gst_caps_get_structure (incaps, 0); |
| 85 |
GST_OBJECT_LOCK (filter); |
| 86 |
if (gst_structure_get_int (structure, "width", &filter->width) && |
| 87 |
gst_structure_get_int (structure, "height", &filter->height)) { |
| 88 |
|
| 89 |
g_free (filter->map); |
| 90 |
|
| 91 |
filter->map = |
| 92 |
g_malloc (2 * filter->width * filter->height * sizeof (guint32)); |
| 93 |
|
| 94 |
init_distortion_map (filter); |
| 95 |
|
| 96 |
ret = TRUE; |
| 97 |
} |
| 98 |
GST_OBJECT_UNLOCK (filter); |
| 99 |
|
| 100 |
return ret; |
| 101 |
} |
| 102 |
|
| 103 |
static GstFlowReturn |
| 104 |
gst_cheese_squeeze_transform (GstBaseTransform * trans, GstBuffer * in, |
| 105 |
GstBuffer * out) |
| 106 |
{ |
| 107 |
GstCheeseSqueeze *filter = GST_CHEESE_SQUEEZE (trans); |
| 108 |
guint32 *src = (guint32 *) GST_BUFFER_DATA (in); |
| 109 |
guint32 *dest = (guint32 *) GST_BUFFER_DATA (out); |
| 110 |
gint width, height; |
| 111 |
gint x, y; |
| 112 |
guint32 newx, newy; |
| 113 |
guint32 u, v; |
| 114 |
guint32 idx; |
| 115 |
guint32 tlidx; |
| 116 |
guint32 tridx; |
| 117 |
guint32 blidx; |
| 118 |
guint32 bridx; |
| 119 |
|
| 120 |
GstFlowReturn ret = GST_FLOW_OK; |
| 121 |
|
| 122 |
GST_OBJECT_LOCK (filter); |
| 123 |
width = filter->width; |
| 124 |
height = filter->height; |
| 125 |
|
| 126 |
for (y = 0; y < height; y++) { |
| 127 |
for (x = 0; x < width; x++) { |
| 128 |
/* may be worth to precalculate offsets in an outer loop like |
| 129 |
warptv does */ |
| 130 |
idx = 2 * (x + y * width); |
| 131 |
/* u, v, fixed point coordinates */ |
| 132 |
u = filter->map[idx]; |
| 133 |
v = filter->map[idx + 1]; |
| 134 |
|
| 135 |
switch (filter->method) { |
| 136 |
case GST_CHEESE_INTERP_NEAREST: |
| 137 |
newx = (u + 128) >> 8; /* nearbyint (newx) */ |
| 138 |
newy = (v + 128) >> 8; /* nearbyint (newy) */ |
| 139 |
|
| 140 |
dest[x + y * width] = src[newx + newy * width]; |
| 141 |
break; |
| 142 |
case GST_CHEESE_INTERP_BILINEAR: |
| 143 |
newx = u >> 8; /* floor (newx) */ |
| 144 |
newy = v >> 8; /* floor (newy) */ |
| 145 |
|
| 146 |
/* do not try to interpolate with out of bound pixels */ |
| 147 |
/* is there a better way to do this? */ |
| 148 |
tlidx = newx + newy * width; |
| 149 |
tridx = (CLAMP (newx + 1, 0, width - 1)) + newy * width; |
| 150 |
blidx = newx + (CLAMP (newy + 1, 0, height - 1)) * width; |
| 151 |
bridx = |
| 152 |
(CLAMP (newx + 1, 0, width - 1)) + (CLAMP (newy + 1, 0, |
| 153 |
height - 1)) * width; |
| 154 |
|
| 155 |
dest[x + y * width] = bilerp (src[tlidx], src[tridx], |
| 156 |
src[blidx], src[bridx], u, v); |
| 157 |
break; |
| 158 |
default: |
| 159 |
g_assert_not_reached (); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
GST_OBJECT_UNLOCK (filter); |
| 165 |
|
| 166 |
return ret; |
| 167 |
} |
| 168 |
|
| 169 |
static void |
| 170 |
gst_cheese_squeeze_set_property (GObject * object, guint prop_id, |
| 171 |
const GValue * value, GParamSpec * pspec) |
| 172 |
{ |
| 173 |
GstCheeseSqueeze *filter = GST_CHEESE_SQUEEZE (object); |
| 174 |
|
| 175 |
switch (prop_id) { |
| 176 |
case PROP_METHOD: |
| 177 |
GST_OBJECT_LOCK (filter); |
| 178 |
filter->method = g_value_get_enum (value); |
| 179 |
GST_OBJECT_UNLOCK (filter); |
| 180 |
break; |
| 181 |
default: |
| 182 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 183 |
break; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
static void |
| 188 |
gst_cheese_squeeze_get_property (GObject * object, guint prop_id, |
| 189 |
GValue * value, GParamSpec * pspec) |
| 190 |
{ |
| 191 |
GstCheeseSqueeze *filter = GST_CHEESE_SQUEEZE (object); |
| 192 |
|
| 193 |
switch (prop_id) { |
| 194 |
case PROP_METHOD: |
| 195 |
GST_OBJECT_LOCK (filter); |
| 196 |
g_value_set_enum (value, filter->method); |
| 197 |
GST_OBJECT_UNLOCK (filter); |
| 198 |
break; |
| 199 |
default: |
| 200 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
static void |
| 206 |
gst_cheese_squeeze_finalize (GObject * object) |
| 207 |
{ |
| 208 |
GstCheeseSqueeze *filter = GST_CHEESE_SQUEEZE (object); |
| 209 |
|
| 210 |
g_free (filter->map); |
| 211 |
|
| 212 |
G_OBJECT_CLASS (parent_class)->finalize (object); |
| 213 |
} |
| 214 |
|
| 215 |
static void |
| 216 |
gst_cheese_squeeze_base_init (gpointer g_class) |
| 217 |
{ |
| 218 |
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); |
| 219 |
|
| 220 |
gst_element_class_set_details_simple (element_class, "Squeeze effect", |
| 221 |
"Filter/Effect/Video", |
| 222 |
"Squeeze the video input", |
| 223 |
"Filippo Argiolas <filippo.argiolas@gmail.com>"); |
| 224 |
|
| 225 |
gst_element_class_add_pad_template (element_class, |
| 226 |
gst_static_pad_template_get (&gst_cheese_squeeze_sink_template)); |
| 227 |
gst_element_class_add_pad_template (element_class, |
| 228 |
gst_static_pad_template_get (&gst_cheese_squeeze_src_template)); |
| 229 |
} |
| 230 |
|
| 231 |
static void |
| 232 |
gst_cheese_squeeze_class_init (GstCheeseSqueezeClass * klass) |
| 233 |
{ |
| 234 |
GObjectClass *gobject_class = (GObjectClass *) klass; |
| 235 |
GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass; |
| 236 |
|
| 237 |
gobject_class->finalize = gst_cheese_squeeze_finalize; |
| 238 |
gobject_class->set_property = gst_cheese_squeeze_set_property; |
| 239 |
gobject_class->get_property = gst_cheese_squeeze_get_property; |
| 240 |
|
| 241 |
g_object_class_install_property (gobject_class, PROP_METHOD, |
| 242 |
g_param_spec_enum ("interp-method", "interp-method", |
| 243 |
"How to calculate the color of transformed fractional coordinates", |
| 244 |
GST_TYPE_CHEESE_INTERP_METHOD, DEFAULT_INTERP_METHOD, |
| 245 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 246 |
|
| 247 |
trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_cheese_squeeze_set_caps); |
| 248 |
trans_class->transform = GST_DEBUG_FUNCPTR (gst_cheese_squeeze_transform); |
| 249 |
} |
| 250 |
|
| 251 |
static void |
| 252 |
gst_cheese_squeeze_init (GstCheeseSqueeze * filter, |
| 253 |
GstCheeseSqueezeClass * klass) |
| 254 |
{ |
| 255 |
filter->map = NULL; |
| 256 |
gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (filter)); |
| 257 |
gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (filter)); |
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
init_distortion_map (GstCheeseSqueeze * filter) |
| 262 |
{ |
| 263 |
gint x, y; |
| 264 |
gdouble newx, newy; |
| 265 |
gdouble norm_x; |
| 266 |
gdouble norm_y; |
| 267 |
gdouble r; |
| 268 |
|
| 269 |
gint width = filter->width; |
| 270 |
gint height = filter->height; |
| 271 |
|
| 272 |
gdouble hw = (width - 1.0) / 2.0; |
| 273 |
gdouble hh = (height - 1.0) / 2.0; |
| 274 |
|
| 275 |
/* this is run once at set_caps so it's optimized for readability |
| 276 |
* and math clarity, not for performance */ |
| 277 |
|
| 278 |
for (y = 0; y < height; y++) { |
| 279 |
for (x = 0; x < width; x++) { |
| 280 |
/* normalize in ((-1.0, -1.0), (1.0, 1.0) */ |
| 281 |
norm_x = x / hw - 1.0; |
| 282 |
norm_y = y / hh - 1.0; |
| 283 |
|
| 284 |
/* work on r to have circular simmetry around center */ |
| 285 |
r = length (norm_x, norm_y); |
| 286 |
|
| 287 |
/* transform */ |
| 288 |
/* the power of r is the amount of squeezing, the lower it is |
| 289 |
* the smoother will be the squeeze. */ |
| 290 |
/* the multiplicative factor is the zooming amount (given the |
| 291 |
* squeezing moves everything to the center a little zoom can be |
| 292 |
* nice) */ |
| 293 |
if (r > 0) |
| 294 |
norm_x /= pow (r, 0.4) * 1.2; |
| 295 |
|
| 296 |
/* ensure transform doesn't give us out of bound coordinates */ |
| 297 |
norm_x = CLAMP (norm_x, -1.0, 1.0); |
| 298 |
norm_y = CLAMP (norm_y, -1.0, 1.0); |
| 299 |
|
| 300 |
/* unnormalize */ |
| 301 |
newx = (norm_x + 1.0) * hw; |
| 302 |
newy = (norm_y + 1.0) * hh; |
| 303 |
|
| 304 |
/* store new coordinates in fixed point (32 bits with 8 bits for |
| 305 |
the fractional part) */ |
| 306 |
/* transformed x and y are stored in the map next to each other */ |
| 307 |
filter->map[(2 * x) + y * 2 * width] = newx * 256; |
| 308 |
filter->map[(2 * x + 1) + y * 2 * width] = newy * 256; |
| 309 |
} |
| 310 |
} |
| 311 |
} |