Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 1 | /* GStreamer |
| 2 | * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu> |
| 3 | * Copyright (C) 2007 Wim Taymans <wim.taymans@collabora.co.uk> |
| 4 | * Copyright (C) 2007 Edward Hervey <edward.hervey@collabora.co.uk> |
| 5 | * Copyright (C) 2007 Jan Schmidt <thaytan@noraisin.net> |
| 6 | * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Library General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Library General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Library General Public |
| 19 | * License along with this library; if not, write to the |
Tim-Philipp Müller | 9e1b75f | 2012-11-03 20:38:00 +0000 | [diff] [blame] | 20 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 21 | * Boston, MA 02110-1301, USA. |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * SECTION:element-chromahold |
Thibault Saunier | 78022a6 | 2017-03-08 15:01:13 -0300 | [diff] [blame] | 26 | * @title: chromahold |
| 27 | * |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 28 | * The chromahold element will remove all color information for |
| 29 | * all colors except a single one and converts them to grayscale. |
| 30 | * |
| 31 | * Sample pipeline: |
| 32 | * |[ |
Vineeth TM | 7c42ba9 | 2015-12-14 11:09:46 +0900 | [diff] [blame] | 33 | * gst-launch-1.0 videotestsrc pattern=smpte75 ! \ |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 34 | * chromahold target-r=0 target-g=0 target-b=255 ! \ |
Wim Taymans | b8f9135 | 2012-09-14 16:45:34 +0200 | [diff] [blame] | 35 | * videoconvert ! autovideosink \ |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 36 | * ]| This pipeline only keeps the red color. |
| 37 | */ |
| 38 | |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 39 | #ifdef HAVE_CONFIG_H |
| 40 | #include "config.h" |
| 41 | #endif |
| 42 | |
| 43 | #include "gstchromahold.h" |
| 44 | |
| 45 | #include <stdlib.h> |
| 46 | #include <string.h> |
| 47 | #include <math.h> |
| 48 | |
| 49 | GST_DEBUG_CATEGORY_STATIC (gst_chroma_hold_debug); |
| 50 | #define GST_CAT_DEFAULT gst_chroma_hold_debug |
| 51 | |
| 52 | #define DEFAULT_TARGET_R 255 |
| 53 | #define DEFAULT_TARGET_G 0 |
| 54 | #define DEFAULT_TARGET_B 0 |
Sebastian Dröge | 59720fd | 2010-10-06 16:54:16 +0200 | [diff] [blame] | 55 | #define DEFAULT_TOLERANCE 30 |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 56 | |
| 57 | enum |
| 58 | { |
| 59 | PROP_0, |
| 60 | PROP_TARGET_R, |
| 61 | PROP_TARGET_G, |
| 62 | PROP_TARGET_B, |
Luis de Bethencourt | c944093 | 2015-04-24 16:48:23 +0100 | [diff] [blame] | 63 | PROP_TOLERANCE |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static GstStaticPadTemplate gst_chroma_hold_src_template = |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 67 | GST_STATIC_PAD_TEMPLATE ("src", |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 68 | GST_PAD_SRC, |
| 69 | GST_PAD_ALWAYS, |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 70 | GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE |
| 71 | ("{ ARGB, BGRA, ABGR, RGBA, xRGB, BGRx, xBGR, RGBx}")) |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 72 | ); |
| 73 | |
| 74 | static GstStaticPadTemplate gst_chroma_hold_sink_template = |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 75 | GST_STATIC_PAD_TEMPLATE ("sink", |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 76 | GST_PAD_SINK, |
| 77 | GST_PAD_ALWAYS, |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 78 | GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE |
| 79 | ("{ ARGB, BGRA, ABGR, RGBA, xRGB, BGRx, xBGR, RGBx}")) |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 80 | ); |
| 81 | |
| 82 | #define GST_CHROMA_HOLD_LOCK(self) G_STMT_START { \ |
| 83 | GST_LOG_OBJECT (self, "Locking chromahold from thread %p", g_thread_self ()); \ |
Marc Leeman | f7b16f6 | 2013-02-07 11:54:06 +0100 | [diff] [blame] | 84 | g_mutex_lock (&self->lock); \ |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 85 | GST_LOG_OBJECT (self, "Locked chromahold from thread %p", g_thread_self ()); \ |
| 86 | } G_STMT_END |
| 87 | |
| 88 | #define GST_CHROMA_HOLD_UNLOCK(self) G_STMT_START { \ |
Luis de Bethencourt | 5435ae8 | 2011-04-19 19:09:30 +0200 | [diff] [blame] | 89 | GST_LOG_OBJECT (self, "Unlocking chromahold from thread %p", \ |
| 90 | g_thread_self ()); \ |
Marc Leeman | f7b16f6 | 2013-02-07 11:54:06 +0100 | [diff] [blame] | 91 | g_mutex_unlock (&self->lock); \ |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 92 | } G_STMT_END |
| 93 | |
| 94 | static gboolean gst_chroma_hold_start (GstBaseTransform * trans); |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 95 | static gboolean gst_chroma_hold_set_info (GstVideoFilter * vfilter, |
| 96 | GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps, |
| 97 | GstVideoInfo * out_info); |
| 98 | static GstFlowReturn gst_chroma_hold_transform_frame_ip (GstVideoFilter * |
| 99 | vfilter, GstVideoFrame * frame); |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 100 | static void gst_chroma_hold_before_transform (GstBaseTransform * btrans, |
| 101 | GstBuffer * buf); |
| 102 | |
| 103 | static void gst_chroma_hold_init_params (GstChromaHold * self); |
| 104 | static gboolean gst_chroma_hold_set_process_function (GstChromaHold * self); |
| 105 | |
| 106 | static void gst_chroma_hold_set_property (GObject * object, guint prop_id, |
| 107 | const GValue * value, GParamSpec * pspec); |
| 108 | static void gst_chroma_hold_get_property (GObject * object, guint prop_id, |
| 109 | GValue * value, GParamSpec * pspec); |
| 110 | static void gst_chroma_hold_finalize (GObject * object); |
| 111 | |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 112 | #define gst_chroma_hold_parent_class parent_class |
| 113 | G_DEFINE_TYPE (GstChromaHold, gst_chroma_hold, GST_TYPE_VIDEO_FILTER); |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 114 | |
| 115 | static void |
| 116 | gst_chroma_hold_class_init (GstChromaHoldClass * klass) |
| 117 | { |
| 118 | GObjectClass *gobject_class = (GObjectClass *) klass; |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 119 | GstElementClass *gstelement_class = (GstElementClass *) klass; |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 120 | GstBaseTransformClass *btrans_class = (GstBaseTransformClass *) klass; |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 121 | GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass; |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 122 | |
| 123 | gobject_class->set_property = gst_chroma_hold_set_property; |
| 124 | gobject_class->get_property = gst_chroma_hold_get_property; |
| 125 | gobject_class->finalize = gst_chroma_hold_finalize; |
| 126 | |
| 127 | g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TARGET_R, |
| 128 | g_param_spec_uint ("target-r", "Target Red", "The Red target", 0, 255, |
| 129 | DEFAULT_TARGET_R, |
| 130 | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)); |
| 131 | g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TARGET_G, |
| 132 | g_param_spec_uint ("target-g", "Target Green", "The Green target", 0, 255, |
| 133 | DEFAULT_TARGET_G, |
| 134 | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)); |
| 135 | g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TARGET_B, |
| 136 | g_param_spec_uint ("target-b", "Target Blue", "The Blue target", 0, 255, |
| 137 | DEFAULT_TARGET_B, |
| 138 | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)); |
| 139 | g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TOLERANCE, |
| 140 | g_param_spec_uint ("tolerance", "Tolerance", |
| 141 | "Tolerance for the target color", 0, 180, DEFAULT_TOLERANCE, |
| 142 | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS)); |
| 143 | |
| 144 | btrans_class->start = GST_DEBUG_FUNCPTR (gst_chroma_hold_start); |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 145 | btrans_class->before_transform = |
| 146 | GST_DEBUG_FUNCPTR (gst_chroma_hold_before_transform); |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 147 | |
| 148 | vfilter_class->transform_frame_ip = |
| 149 | GST_DEBUG_FUNCPTR (gst_chroma_hold_transform_frame_ip); |
| 150 | vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_chroma_hold_set_info); |
| 151 | |
Tim-Philipp Müller | 32ba17c | 2012-10-17 17:34:26 +0100 | [diff] [blame] | 152 | gst_element_class_set_static_metadata (gstelement_class, "Chroma hold filter", |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 153 | "Filter/Effect/Video", |
| 154 | "Removes all color information except for one color", |
| 155 | "Sebastian Dröge <sebastian.droege@collabora.co.uk>"); |
| 156 | |
Vineeth TM | 8cdfb13 | 2016-03-04 15:50:26 +0900 | [diff] [blame] | 157 | gst_element_class_add_static_pad_template (gstelement_class, |
| 158 | &gst_chroma_hold_sink_template); |
| 159 | gst_element_class_add_static_pad_template (gstelement_class, |
| 160 | &gst_chroma_hold_src_template); |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 161 | |
| 162 | GST_DEBUG_CATEGORY_INIT (gst_chroma_hold_debug, "chromahold", 0, |
| 163 | "chromahold - Removes all color information except for one color"); |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static void |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 167 | gst_chroma_hold_init (GstChromaHold * self) |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 168 | { |
| 169 | self->target_r = DEFAULT_TARGET_R; |
| 170 | self->target_g = DEFAULT_TARGET_G; |
| 171 | self->target_b = DEFAULT_TARGET_B; |
| 172 | self->tolerance = DEFAULT_TOLERANCE; |
| 173 | |
Marc Leeman | f7b16f6 | 2013-02-07 11:54:06 +0100 | [diff] [blame] | 174 | g_mutex_init (&self->lock); |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void |
| 178 | gst_chroma_hold_finalize (GObject * object) |
| 179 | { |
| 180 | GstChromaHold *self = GST_CHROMA_HOLD (object); |
| 181 | |
Marc Leeman | f7b16f6 | 2013-02-07 11:54:06 +0100 | [diff] [blame] | 182 | g_mutex_clear (&self->lock); |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 183 | |
| 184 | G_OBJECT_CLASS (parent_class)->finalize (object); |
| 185 | } |
| 186 | |
| 187 | static void |
| 188 | gst_chroma_hold_set_property (GObject * object, guint prop_id, |
| 189 | const GValue * value, GParamSpec * pspec) |
| 190 | { |
| 191 | GstChromaHold *self = GST_CHROMA_HOLD (object); |
| 192 | |
| 193 | GST_CHROMA_HOLD_LOCK (self); |
| 194 | switch (prop_id) { |
| 195 | case PROP_TARGET_R: |
| 196 | self->target_r = g_value_get_uint (value); |
| 197 | gst_chroma_hold_init_params (self); |
| 198 | break; |
| 199 | case PROP_TARGET_G: |
| 200 | self->target_g = g_value_get_uint (value); |
| 201 | gst_chroma_hold_init_params (self); |
| 202 | break; |
| 203 | case PROP_TARGET_B: |
| 204 | self->target_b = g_value_get_uint (value); |
| 205 | gst_chroma_hold_init_params (self); |
| 206 | break; |
| 207 | case PROP_TOLERANCE: |
| 208 | self->tolerance = g_value_get_uint (value); |
| 209 | break; |
| 210 | default: |
| 211 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | GST_CHROMA_HOLD_UNLOCK (self); |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | gst_chroma_hold_get_property (GObject * object, guint prop_id, GValue * value, |
| 220 | GParamSpec * pspec) |
| 221 | { |
| 222 | GstChromaHold *self = GST_CHROMA_HOLD (object); |
| 223 | |
| 224 | switch (prop_id) { |
| 225 | case PROP_TARGET_R: |
| 226 | g_value_set_uint (value, self->target_r); |
| 227 | break; |
| 228 | case PROP_TARGET_G: |
| 229 | g_value_set_uint (value, self->target_g); |
| 230 | break; |
| 231 | case PROP_TARGET_B: |
| 232 | g_value_set_uint (value, self->target_b); |
| 233 | break; |
| 234 | case PROP_TOLERANCE: |
| 235 | g_value_set_uint (value, self->tolerance); |
| 236 | break; |
| 237 | default: |
| 238 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static gboolean |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 244 | gst_chroma_hold_set_info (GstVideoFilter * vfilter, GstCaps * incaps, |
| 245 | GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info) |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 246 | { |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 247 | GstChromaHold *self = GST_CHROMA_HOLD (vfilter); |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 248 | |
| 249 | GST_CHROMA_HOLD_LOCK (self); |
| 250 | |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 251 | GST_DEBUG_OBJECT (self, |
| 252 | "Setting caps %" GST_PTR_FORMAT " -> %" GST_PTR_FORMAT, incaps, outcaps); |
| 253 | |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 254 | self->format = GST_VIDEO_INFO_FORMAT (in_info); |
| 255 | self->width = GST_VIDEO_INFO_WIDTH (in_info); |
| 256 | self->height = GST_VIDEO_INFO_HEIGHT (in_info); |
| 257 | |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 258 | if (!gst_chroma_hold_set_process_function (self)) { |
| 259 | GST_WARNING_OBJECT (self, "No processing function for this caps"); |
| 260 | GST_CHROMA_HOLD_UNLOCK (self); |
| 261 | return FALSE; |
| 262 | } |
| 263 | |
| 264 | GST_CHROMA_HOLD_UNLOCK (self); |
| 265 | |
| 266 | return TRUE; |
| 267 | } |
| 268 | |
| 269 | static inline gint |
| 270 | rgb_to_hue (gint r, gint g, gint b) |
| 271 | { |
Sebastian Dröge | 59720fd | 2010-10-06 16:54:16 +0200 | [diff] [blame] | 272 | gint m, M, C, C2, h; |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 273 | |
| 274 | m = MIN (MIN (r, g), b); |
| 275 | M = MAX (MAX (r, g), b); |
| 276 | C = M - m; |
Sebastian Dröge | 59720fd | 2010-10-06 16:54:16 +0200 | [diff] [blame] | 277 | C2 = C >> 1; |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 278 | |
| 279 | if (C == 0) { |
Sebastian Dröge | 10e0187 | 2010-10-06 15:21:09 +0200 | [diff] [blame] | 280 | return G_MAXUINT; |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 281 | } else if (M == r) { |
Sebastian Dröge | 59720fd | 2010-10-06 16:54:16 +0200 | [diff] [blame] | 282 | h = ((256 * 60 * (g - b) + C2) / C); |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 283 | } else if (M == g) { |
Sebastian Dröge | 59720fd | 2010-10-06 16:54:16 +0200 | [diff] [blame] | 284 | h = ((256 * 60 * (b - r) + C2) / C) + 120 * 256; |
| 285 | } else { |
| 286 | /* if (M == b) */ |
| 287 | h = ((256 * 60 * (r - g) + C2) / C) + 240 * 256; |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 288 | } |
Sebastian Dröge | 59720fd | 2010-10-06 16:54:16 +0200 | [diff] [blame] | 289 | h >>= 8; |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 290 | |
| 291 | if (h >= 360) |
| 292 | h -= 360; |
| 293 | else if (h < 0) |
| 294 | h += 360; |
| 295 | |
| 296 | return h; |
| 297 | } |
| 298 | |
| 299 | static inline gint |
| 300 | hue_dist (gint h1, gint h2) |
| 301 | { |
| 302 | gint d1, d2; |
| 303 | |
| 304 | d1 = h1 - h2; |
| 305 | d2 = h2 - h1; |
| 306 | |
| 307 | if (d1 < 0) |
| 308 | d1 += 360; |
| 309 | if (d2 < 0) |
| 310 | d2 += 360; |
| 311 | |
| 312 | return MIN (d1, d2); |
| 313 | } |
| 314 | |
| 315 | static void |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 316 | gst_chroma_hold_process_xrgb (GstVideoFrame * frame, gint width, |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 317 | gint height, GstChromaHold * self) |
| 318 | { |
| 319 | gint i, j; |
| 320 | gint r, g, b; |
| 321 | gint grey; |
| 322 | gint h1, h2; |
| 323 | gint tolerance = self->tolerance; |
| 324 | gint p[4]; |
| 325 | gint diff; |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 326 | gint row_wrap; |
| 327 | guint8 *dest; |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 328 | |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 329 | dest = GST_VIDEO_FRAME_PLANE_DATA (frame, 0); |
| 330 | p[0] = GST_VIDEO_FRAME_COMP_POFFSET (frame, 3); |
| 331 | p[1] = GST_VIDEO_FRAME_COMP_POFFSET (frame, 0); |
| 332 | p[2] = GST_VIDEO_FRAME_COMP_POFFSET (frame, 1); |
| 333 | p[3] = GST_VIDEO_FRAME_COMP_POFFSET (frame, 2); |
| 334 | row_wrap = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0) - 4 * width; |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 335 | |
| 336 | h1 = self->hue; |
| 337 | |
| 338 | for (i = 0; i < height; i++) { |
| 339 | for (j = 0; j < width; j++) { |
| 340 | r = dest[p[1]]; |
| 341 | g = dest[p[2]]; |
| 342 | b = dest[p[3]]; |
| 343 | |
| 344 | h2 = rgb_to_hue (r, g, b); |
| 345 | diff = hue_dist (h1, h2); |
Sebastian Dröge | 10e0187 | 2010-10-06 15:21:09 +0200 | [diff] [blame] | 346 | if (h1 == G_MAXUINT || diff > tolerance) { |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 347 | grey = (13938 * r + 46869 * g + 4730 * b) >> 16; |
| 348 | grey = CLAMP (grey, 0, 255); |
| 349 | dest[p[1]] = grey; |
| 350 | dest[p[2]] = grey; |
| 351 | dest[p[3]] = grey; |
| 352 | } |
| 353 | |
| 354 | dest += 4; |
| 355 | } |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 356 | dest += row_wrap; |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | /* Protected with the chroma hold lock */ |
| 361 | static void |
| 362 | gst_chroma_hold_init_params (GstChromaHold * self) |
| 363 | { |
| 364 | self->hue = rgb_to_hue (self->target_r, self->target_g, self->target_b); |
| 365 | } |
| 366 | |
| 367 | /* Protected with the chroma hold lock */ |
| 368 | static gboolean |
| 369 | gst_chroma_hold_set_process_function (GstChromaHold * self) |
| 370 | { |
| 371 | self->process = NULL; |
| 372 | |
| 373 | switch (self->format) { |
| 374 | case GST_VIDEO_FORMAT_ARGB: |
| 375 | case GST_VIDEO_FORMAT_ABGR: |
| 376 | case GST_VIDEO_FORMAT_RGBA: |
| 377 | case GST_VIDEO_FORMAT_BGRA: |
| 378 | case GST_VIDEO_FORMAT_xRGB: |
| 379 | case GST_VIDEO_FORMAT_xBGR: |
| 380 | case GST_VIDEO_FORMAT_RGBx: |
| 381 | case GST_VIDEO_FORMAT_BGRx: |
| 382 | self->process = gst_chroma_hold_process_xrgb; |
| 383 | break; |
| 384 | default: |
| 385 | break; |
| 386 | } |
| 387 | return self->process != NULL; |
| 388 | } |
| 389 | |
| 390 | static gboolean |
| 391 | gst_chroma_hold_start (GstBaseTransform * btrans) |
| 392 | { |
| 393 | GstChromaHold *self = GST_CHROMA_HOLD (btrans); |
| 394 | |
| 395 | GST_CHROMA_HOLD_LOCK (self); |
| 396 | gst_chroma_hold_init_params (self); |
| 397 | GST_CHROMA_HOLD_UNLOCK (self); |
| 398 | |
| 399 | return TRUE; |
| 400 | } |
| 401 | |
| 402 | static void |
| 403 | gst_chroma_hold_before_transform (GstBaseTransform * btrans, GstBuffer * buf) |
| 404 | { |
| 405 | GstChromaHold *self = GST_CHROMA_HOLD (btrans); |
| 406 | GstClockTime timestamp; |
| 407 | |
| 408 | timestamp = gst_segment_to_stream_time (&btrans->segment, GST_FORMAT_TIME, |
| 409 | GST_BUFFER_TIMESTAMP (buf)); |
| 410 | GST_LOG ("Got stream time of %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp)); |
| 411 | if (GST_CLOCK_TIME_IS_VALID (timestamp)) |
Stefan Sauer | 9f73890 | 2011-11-04 18:52:35 +0100 | [diff] [blame] | 412 | gst_object_sync_values (GST_OBJECT (self), timestamp); |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static GstFlowReturn |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 416 | gst_chroma_hold_transform_frame_ip (GstVideoFilter * vfilter, |
| 417 | GstVideoFrame * frame) |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 418 | { |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 419 | GstChromaHold *self = GST_CHROMA_HOLD (vfilter); |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 420 | |
| 421 | GST_CHROMA_HOLD_LOCK (self); |
| 422 | |
| 423 | if (G_UNLIKELY (!self->process)) { |
| 424 | GST_ERROR_OBJECT (self, "Not negotiated yet"); |
| 425 | GST_CHROMA_HOLD_UNLOCK (self); |
| 426 | return GST_FLOW_NOT_NEGOTIATED; |
| 427 | } |
| 428 | |
Mark Nauwelaerts | 4d1bd12 | 2012-04-24 17:00:57 +0200 | [diff] [blame] | 429 | self->process (frame, self->width, self->height, self); |
Sebastian Dröge | fa2a4af | 2010-10-06 11:55:34 +0200 | [diff] [blame] | 430 | |
| 431 | GST_CHROMA_HOLD_UNLOCK (self); |
| 432 | |
| 433 | return GST_FLOW_OK; |
| 434 | } |