1 /* GStreamer
2  * Copyright (C) <2016> Matthew Waters <matthew@centricular.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /*
20  * This file was modified from videobalance and converted to OpenGL
21  */
22 
23 /**
24  * SECTION:element-glcolorbalance
25  * @title: glcolorbalance
26  *
27  * Adjusts brightness, contrast, hue, saturation on a video stream.
28  *
29  * ## Example launch line
30  * |[
31  * gst-launch-1.0 videotestsrc ! glupload ! glcolorbalance saturation=0.0 ! glcolorconvert ! gldownload ! ximagesink
32  * ]| This pipeline converts the image to black and white by setting the
33  * saturation to 0.0.
34  *
35  */
36 
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40 
41 #include <string.h>
42 #include <gst/gl/gstglfuncs.h>
43 #include <gst/math-compat.h>
44 #include <gst/video/colorbalance.h>
45 
46 #include "gstglcolorbalance.h"
47 
48 GST_DEBUG_CATEGORY_STATIC (glcolorbalance_debug);
49 #define GST_CAT_DEFAULT glcolorbalance_debug
50 
51 /* GstGLColorBalance properties */
52 #define DEFAULT_PROP_CONTRAST       1.0
53 #define DEFAULT_PROP_BRIGHTNESS	    0.0
54 #define DEFAULT_PROP_HUE            0.0
55 #define DEFAULT_PROP_SATURATION	    1.0
56 
57 #define GST_GL_COLOR_BALANCE_VIDEO_CAPS \
58     "video/x-raw(" GST_CAPS_FEATURE_MEMORY_GL_MEMORY "), "              \
59     "format = (string) RGBA, "              \
60     "width = " GST_VIDEO_SIZE_RANGE ", "                                \
61     "height = " GST_VIDEO_SIZE_RANGE ", "                               \
62     "framerate = " GST_VIDEO_FPS_RANGE ", "                             \
63     "texture-target = (string) { 2D, external-oes } "        \
64     " ; "                                                               \
65     "video/x-raw(" GST_CAPS_FEATURE_MEMORY_GL_MEMORY ","                \
66     GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION "), "           \
67     "format = (string) RGBA, "              \
68     "width = " GST_VIDEO_SIZE_RANGE ", "                                \
69     "height = " GST_VIDEO_SIZE_RANGE ", "                               \
70     "framerate = " GST_VIDEO_FPS_RANGE ", "                             \
71     "texture-target = (string) { 2D, external-oes }"
72 
73 static GstStaticPadTemplate gst_gl_color_balance_element_src_pad_template =
74 GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS (GST_GL_COLOR_BALANCE_VIDEO_CAPS));
78 
79 static GstStaticPadTemplate gst_gl_color_balance_element_sink_pad_template =
80 GST_STATIC_PAD_TEMPLATE ("sink",
81     GST_PAD_SINK,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS (GST_GL_COLOR_BALANCE_VIDEO_CAPS));
84 
85 /* *INDENT-OFF* */
86 static const gchar glsl_external_image_extension[] =
87     "#extension GL_OES_EGL_image_external : require\n";
88 
89 static const gchar glsl_external_image_sampler[] =
90     "uniform samplerExternalOES tex;\n";
91 
92 static const gchar glsl_2D_image_sampler[] =
93   "uniform sampler2D tex;\n";
94 
95 static const gchar color_balance_frag_templ[] =
96   "uniform float brightness;\n"
97   "uniform float contrast;\n"
98   "uniform float saturation;\n"
99   "uniform float hue;\n"
100   "varying vec2 v_texcoord;\n"
101   "#define from_yuv_bt601_offset vec3(-0.0625, -0.5, -0.5)\n"
102   "#define from_yuv_bt601_rcoeff vec3(1.164, 0.000, 1.596)\n"
103   "#define from_yuv_bt601_gcoeff vec3(1.164,-0.391,-0.813)\n"
104   "#define from_yuv_bt601_bcoeff vec3(1.164, 2.018, 0.000)\n"
105   "#define from_rgb_bt601_offset vec3(0.0625, 0.5, 0.5)\n"
106   "#define from_rgb_bt601_ycoeff vec3(0.256816, 0.504154, 0.0979137)\n"
107   "#define from_rgb_bt601_ucoeff vec3(-0.148246, -0.29102, 0.439266)\n"
108   "#define from_rgb_bt601_vcoeff vec3(0.439271, -0.367833, -0.071438)\n"
109   "#define PI 3.14159265\n"
110   "\n"
111   "vec3 yuv_to_rgb (vec3 val) {\n"
112   "  vec3 rgb;\n"
113   "  val += from_yuv_bt601_offset;\n"
114   "  rgb.r = dot(val, from_yuv_bt601_rcoeff);\n"
115   "  rgb.g = dot(val, from_yuv_bt601_gcoeff);\n"
116   "  rgb.b = dot(val, from_yuv_bt601_bcoeff);\n"
117   "  return rgb;\n"
118   "}\n"
119   "vec3 rgb_to_yuv (vec3 val) {\n"
120   "  vec3 yuv;\n"
121   "  yuv.r = dot(val.rgb, from_rgb_bt601_ycoeff);\n"
122   "  yuv.g = dot(val.rgb, from_rgb_bt601_ucoeff);\n"
123   "  yuv.b = dot(val.rgb, from_rgb_bt601_vcoeff);\n"
124   "  yuv += from_rgb_bt601_offset;\n"
125   "  return yuv;\n"
126   "}\n"
127   /* 224 = 256 - (256 - 240) - 16*/
128   "float luma_to_narrow (float luma) {\n"
129   "  return (luma + 16.0 / 256.0) * 219.0 / 256.0;"
130   "}\n"
131   "float luma_to_full (float luma) {\n"
132   "  return (luma * 256.0 / 219.0) - 16.0 / 256.0;"
133   "}\n"
134   "void main () {\n"
135   "  vec3 yuv;\n"
136   /* operations translated from videobalanceand tested with glvideomixer
137    * with one pad's paremeters blend-equation-rgb={subtract,reverse-subtract},
138    * blend-function-src-rgb=src-color and blend-function-dst-rgb=dst-color */
139   "  float hue_cos = cos (PI * hue);\n"
140   "  float hue_sin = sin (PI * hue);\n"
141   "  vec4 rgba = %s (tex, v_texcoord);\n" /* texture2D / texture2DOES */
142   "  yuv = rgb_to_yuv (rgba.rgb);\n"
143   "  yuv.x = clamp (luma_to_narrow (luma_to_full(yuv.x) * contrast) + brightness, 0.0, 1.0);\n"
144   "  vec2 uv = yuv.yz;\n"
145   "  yuv.y = clamp (0.5 + (((uv.x - 0.5) * hue_cos + (uv.y - 0.5) * hue_sin) * saturation), 0.0, 1.0);\n"
146   "  yuv.z = clamp (0.5 + (((0.5 - uv.x) * hue_sin + (uv.y - 0.5) * hue_cos) * saturation), 0.0, 1.0);\n"
147   "  rgba.rgb = yuv_to_rgb (yuv);\n"
148   "  gl_FragColor = rgba;\n"
149   "}\n";
150 /* *INDENT-ON* */
151 
152 enum
153 {
154   PROP_0,
155   PROP_CONTRAST,
156   PROP_BRIGHTNESS,
157   PROP_HUE,
158   PROP_SATURATION
159 };
160 
161 static void gst_gl_color_balance_colorbalance_init (GstColorBalanceInterface *
162     iface);
163 
164 static void gst_gl_color_balance_set_property (GObject * object, guint prop_id,
165     const GValue * value, GParamSpec * pspec);
166 static void gst_gl_color_balance_get_property (GObject * object, guint prop_id,
167     GValue * value, GParamSpec * pspec);
168 
169 #define gst_gl_color_balance_parent_class parent_class
170 G_DEFINE_TYPE_WITH_CODE (GstGLColorBalance, gst_gl_color_balance,
171     GST_TYPE_GL_FILTER,
172     G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
173         gst_gl_color_balance_colorbalance_init));
174 
175 static GstCaps *
gcb_transform_internal_caps(GstGLFilter * filter,GstPadDirection direction,GstCaps * caps,GstCaps * filter_caps)176 gcb_transform_internal_caps (GstGLFilter * filter,
177     GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps)
178 {
179   GstCaps *tmp = gst_caps_copy (caps);
180   gint i;
181   /* If we're not in passthrough mode, we can only output 2D textures,
182    * but can always receive any compatible texture.
183    * This function is not called in passthrough mode, so we can do the
184    * transform unconditionally */
185   for (i = 0; i < gst_caps_get_size (tmp); i++) {
186     GstStructure *outs = gst_caps_get_structure (tmp, i);
187     if (direction == GST_PAD_SINK) {
188       gst_structure_set (outs, "texture-target", G_TYPE_STRING,
189           gst_gl_texture_target_to_string (GST_GL_TEXTURE_TARGET_2D), NULL);
190     } else {
191       gst_structure_remove_field (outs, "texture-target");
192     }
193   }
194   return tmp;
195 }
196 
197 static gboolean
gst_gl_color_balance_is_passthrough(GstGLColorBalance * glcolorbalance)198 gst_gl_color_balance_is_passthrough (GstGLColorBalance * glcolorbalance)
199 {
200   return glcolorbalance->contrast == 1.0 &&
201       glcolorbalance->brightness == 0.0 &&
202       glcolorbalance->hue == 0.0 && glcolorbalance->saturation == 1.0;
203 }
204 
205 static void
gst_gl_color_balance_update_properties(GstGLColorBalance * glcolorbalance)206 gst_gl_color_balance_update_properties (GstGLColorBalance * glcolorbalance)
207 {
208   gboolean current_passthrough, passthrough;
209   GstBaseTransform *base = GST_BASE_TRANSFORM (glcolorbalance);
210 
211   GST_OBJECT_LOCK (glcolorbalance);
212   passthrough = gst_gl_color_balance_is_passthrough (glcolorbalance);
213   GST_OBJECT_UNLOCK (glcolorbalance);
214   current_passthrough = gst_base_transform_is_passthrough (base);
215 
216   gst_base_transform_set_passthrough (base, passthrough);
217   if (current_passthrough != passthrough)
218     gst_base_transform_reconfigure_src (base);
219 }
220 
221 static gboolean
_create_shader(GstGLColorBalance * balance)222 _create_shader (GstGLColorBalance * balance)
223 {
224   GstGLBaseFilter *base_filter = GST_GL_BASE_FILTER (balance);
225   GstGLFilter *filter = GST_GL_FILTER (balance);
226   GError *error = NULL;
227   gchar *frag_body;
228   const gchar *frags[4];
229   guint frag_i = 0;
230 
231   if (balance->shader)
232     gst_object_unref (balance->shader);
233 
234   if (filter->in_texture_target == GST_GL_TEXTURE_TARGET_EXTERNAL_OES)
235     frags[frag_i++] = glsl_external_image_extension;
236 
237   frags[frag_i++] =
238       gst_gl_shader_string_get_highest_precision (base_filter->context,
239       GST_GLSL_VERSION_NONE,
240       GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY);
241 
242   /* Can support rectangle textures in the future if needed */
243   if (filter->in_texture_target == GST_GL_TEXTURE_TARGET_2D) {
244     frags[frag_i++] = glsl_2D_image_sampler;
245     frags[frag_i++] = frag_body =
246         g_strdup_printf (color_balance_frag_templ, "texture2D");
247   } else {
248     frags[frag_i++] = glsl_external_image_sampler;
249     frags[frag_i++] = frag_body =
250         g_strdup_printf (color_balance_frag_templ, "texture2D");
251   }
252 
253   g_assert (frag_i <= G_N_ELEMENTS (frags));
254 
255   if (!(balance->shader =
256           gst_gl_shader_new_link_with_stages (base_filter->context, &error,
257               gst_glsl_stage_new_default_vertex (base_filter->context),
258               gst_glsl_stage_new_with_strings (base_filter->context,
259                   GL_FRAGMENT_SHADER, GST_GLSL_VERSION_NONE,
260                   GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY, frag_i,
261                   frags), NULL))) {
262     g_free (frag_body);
263     GST_ELEMENT_ERROR (balance, RESOURCE, NOT_FOUND, ("%s",
264             "Failed to initialize colorbalance shader"), ("%s",
265             error ? error->message : "Unknown error"));
266     return FALSE;
267   }
268   g_free (frag_body);
269 
270   filter->draw_attr_position_loc =
271       gst_gl_shader_get_attribute_location (balance->shader, "a_position");
272   filter->draw_attr_texture_loc =
273       gst_gl_shader_get_attribute_location (balance->shader, "a_texcoord");
274 
275   return TRUE;
276 }
277 
278 static gboolean
gst_gl_color_balance_gl_start(GstGLBaseFilter * base_filter)279 gst_gl_color_balance_gl_start (GstGLBaseFilter * base_filter)
280 {
281   GstGLColorBalance *balance = GST_GL_COLOR_BALANCE (base_filter);
282 
283   if (!_create_shader (balance))
284     return FALSE;
285 
286   return GST_GL_BASE_FILTER_CLASS (parent_class)->gl_start (base_filter);
287 }
288 
289 static void
gst_gl_color_balance_gl_stop(GstGLBaseFilter * base_filter)290 gst_gl_color_balance_gl_stop (GstGLBaseFilter * base_filter)
291 {
292   GstGLColorBalance *balance = GST_GL_COLOR_BALANCE (base_filter);
293 
294   if (balance->shader)
295     gst_object_unref (balance->shader);
296   balance->shader = NULL;
297 
298   GST_GL_BASE_FILTER_CLASS (parent_class)->gl_stop (base_filter);
299 }
300 
301 static void
gst_gl_color_balance_before_transform(GstBaseTransform * base,GstBuffer * buf)302 gst_gl_color_balance_before_transform (GstBaseTransform * base, GstBuffer * buf)
303 {
304   GstGLColorBalance *balance = GST_GL_COLOR_BALANCE (base);
305   GstClockTime timestamp, stream_time;
306 
307   timestamp = GST_BUFFER_TIMESTAMP (buf);
308   stream_time =
309       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
310 
311   GST_DEBUG_OBJECT (balance, "sync to %" GST_TIME_FORMAT,
312       GST_TIME_ARGS (timestamp));
313 
314   if (GST_CLOCK_TIME_IS_VALID (stream_time))
315     gst_object_sync_values (GST_OBJECT (balance), stream_time);
316 }
317 
318 static gboolean
gst_gl_color_balance_filter_texture(GstGLFilter * filter,GstGLMemory * in_tex,GstGLMemory * out_tex)319 gst_gl_color_balance_filter_texture (GstGLFilter * filter, GstGLMemory * in_tex,
320     GstGLMemory * out_tex)
321 {
322   GstGLColorBalance *balance = GST_GL_COLOR_BALANCE (filter);
323 
324   if (!balance->shader)
325     _create_shader (balance);
326 
327   gst_gl_shader_use (balance->shader);
328   GST_OBJECT_LOCK (balance);
329   gst_gl_shader_set_uniform_1f (balance->shader, "brightness",
330       balance->brightness);
331   gst_gl_shader_set_uniform_1f (balance->shader, "contrast", balance->contrast);
332   gst_gl_shader_set_uniform_1f (balance->shader, "saturation",
333       balance->saturation);
334   gst_gl_shader_set_uniform_1f (balance->shader, "hue", balance->hue);
335   GST_OBJECT_UNLOCK (balance);
336 
337   gst_gl_filter_render_to_target_with_shader (filter, in_tex, out_tex,
338       balance->shader);
339 
340   return TRUE;
341 }
342 
343 static void
gst_gl_color_balance_finalize(GObject * object)344 gst_gl_color_balance_finalize (GObject * object)
345 {
346   GstGLColorBalance *balance = GST_GL_COLOR_BALANCE (object);
347   GList *channels = NULL;
348 
349   channels = balance->channels;
350   while (channels) {
351     GstColorBalanceChannel *channel = channels->data;
352 
353     g_object_unref (channel);
354     channels->data = NULL;
355     channels = g_list_next (channels);
356   }
357 
358   if (balance->channels)
359     g_list_free (balance->channels);
360 
361   G_OBJECT_CLASS (parent_class)->finalize (object);
362 }
363 
364 static void
gst_gl_color_balance_class_init(GstGLColorBalanceClass * klass)365 gst_gl_color_balance_class_init (GstGLColorBalanceClass * klass)
366 {
367   GObjectClass *gobject_class = (GObjectClass *) klass;
368   GstElementClass *gstelement_class = (GstElementClass *) klass;
369   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
370   GstGLBaseFilterClass *base_filter_class = (GstGLBaseFilterClass *) klass;
371   GstGLFilterClass *filter_class = (GstGLFilterClass *) klass;
372 
373   GST_DEBUG_CATEGORY_INIT (glcolorbalance_debug, "glcolorbalance", 0,
374       "glcolorbalance");
375 
376   gst_element_class_add_static_pad_template (gstelement_class,
377       &gst_gl_color_balance_element_src_pad_template);
378   gst_element_class_add_static_pad_template (gstelement_class,
379       &gst_gl_color_balance_element_sink_pad_template);
380 
381   gobject_class->finalize = gst_gl_color_balance_finalize;
382   gobject_class->set_property = gst_gl_color_balance_set_property;
383   gobject_class->get_property = gst_gl_color_balance_get_property;
384 
385   g_object_class_install_property (gobject_class, PROP_CONTRAST,
386       g_param_spec_double ("contrast", "Contrast", "contrast",
387           0.0, 2.0, DEFAULT_PROP_CONTRAST,
388           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
389   g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
390       g_param_spec_double ("brightness", "Brightness", "brightness", -1.0, 1.0,
391           DEFAULT_PROP_BRIGHTNESS,
392           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
393   g_object_class_install_property (gobject_class, PROP_HUE,
394       g_param_spec_double ("hue", "Hue", "hue", -1.0, 1.0, DEFAULT_PROP_HUE,
395           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
396   g_object_class_install_property (gobject_class, PROP_SATURATION,
397       g_param_spec_double ("saturation", "Saturation", "saturation", 0.0, 2.0,
398           DEFAULT_PROP_SATURATION,
399           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400 
401   gst_element_class_set_static_metadata (gstelement_class, "Video balance",
402       "Filter/Effect/Video",
403       "Adjusts brightness, contrast, hue, saturation on a video stream",
404       "Matthew Waters <matthew@centricular.com>");
405 
406   trans_class->before_transform =
407       GST_DEBUG_FUNCPTR (gst_gl_color_balance_before_transform);
408   trans_class->transform_ip_on_passthrough = FALSE;
409 
410   base_filter_class->gl_start =
411       GST_DEBUG_FUNCPTR (gst_gl_color_balance_gl_start);
412   base_filter_class->gl_stop = GST_DEBUG_FUNCPTR (gst_gl_color_balance_gl_stop);
413 
414   filter_class->filter_texture =
415       GST_DEBUG_FUNCPTR (gst_gl_color_balance_filter_texture);
416   filter_class->transform_internal_caps = gcb_transform_internal_caps;
417 }
418 
419 static void
gst_gl_color_balance_init(GstGLColorBalance * glcolorbalance)420 gst_gl_color_balance_init (GstGLColorBalance * glcolorbalance)
421 {
422   const gchar *channels[4] = { "HUE", "SATURATION",
423     "BRIGHTNESS", "CONTRAST"
424   };
425   gint i;
426 
427   /* Initialize propertiews */
428   glcolorbalance->contrast = DEFAULT_PROP_CONTRAST;
429   glcolorbalance->brightness = DEFAULT_PROP_BRIGHTNESS;
430   glcolorbalance->hue = DEFAULT_PROP_HUE;
431   glcolorbalance->saturation = DEFAULT_PROP_SATURATION;
432 
433   gst_gl_color_balance_update_properties (glcolorbalance);
434 
435   /* Generate the channels list */
436   for (i = 0; i < G_N_ELEMENTS (channels); i++) {
437     GstColorBalanceChannel *channel;
438 
439     channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
440     channel->label = g_strdup (channels[i]);
441     channel->min_value = -1000;
442     channel->max_value = 1000;
443 
444     glcolorbalance->channels =
445         g_list_append (glcolorbalance->channels, channel);
446   }
447 }
448 
449 static const GList *
gst_gl_color_balance_colorbalance_list_channels(GstColorBalance * balance)450 gst_gl_color_balance_colorbalance_list_channels (GstColorBalance * balance)
451 {
452   GstGLColorBalance *glcolorbalance = GST_GL_COLOR_BALANCE (balance);
453 
454   g_return_val_if_fail (glcolorbalance != NULL, NULL);
455   g_return_val_if_fail (GST_IS_GL_COLOR_BALANCE (glcolorbalance), NULL);
456 
457   return glcolorbalance->channels;
458 }
459 
460 static void
gst_gl_color_balance_colorbalance_set_value(GstColorBalance * balance,GstColorBalanceChannel * channel,gint value)461 gst_gl_color_balance_colorbalance_set_value (GstColorBalance * balance,
462     GstColorBalanceChannel * channel, gint value)
463 {
464   GstGLColorBalance *vb = GST_GL_COLOR_BALANCE (balance);
465   gdouble new_val;
466   gboolean changed = FALSE;
467 
468   g_return_if_fail (vb != NULL);
469   g_return_if_fail (GST_IS_GL_COLOR_BALANCE (vb));
470   g_return_if_fail (channel->label != NULL);
471 
472   GST_OBJECT_LOCK (vb);
473   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
474     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
475     changed = new_val != vb->hue;
476     vb->hue = new_val;
477   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
478     new_val = (value + 1000.0) * 2.0 / 2000.0;
479     changed = new_val != vb->saturation;
480     vb->saturation = new_val;
481   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
482     new_val = (value + 1000.0) * 2.0 / 2000.0 - 1.0;
483     changed = new_val != vb->brightness;
484     vb->brightness = new_val;
485   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
486     new_val = (value + 1000.0) * 2.0 / 2000.0;
487     changed = new_val != vb->contrast;
488     vb->contrast = new_val;
489   }
490   GST_OBJECT_UNLOCK (vb);
491 
492   if (changed)
493     gst_gl_color_balance_update_properties (vb);
494 
495   if (changed) {
496     gst_color_balance_value_changed (balance, channel,
497         gst_color_balance_get_value (balance, channel));
498   }
499 }
500 
501 static gint
gst_gl_color_balance_colorbalance_get_value(GstColorBalance * balance,GstColorBalanceChannel * channel)502 gst_gl_color_balance_colorbalance_get_value (GstColorBalance * balance,
503     GstColorBalanceChannel * channel)
504 {
505   GstGLColorBalance *vb = GST_GL_COLOR_BALANCE (balance);
506   gint value = 0;
507 
508   g_return_val_if_fail (vb != NULL, 0);
509   g_return_val_if_fail (GST_IS_GL_COLOR_BALANCE (vb), 0);
510   g_return_val_if_fail (channel->label != NULL, 0);
511 
512   if (!g_ascii_strcasecmp (channel->label, "HUE")) {
513     value = (vb->hue + 1) * 2000.0 / 2.0 - 1000.0;
514   } else if (!g_ascii_strcasecmp (channel->label, "SATURATION")) {
515     value = vb->saturation * 2000.0 / 2.0 - 1000.0;
516   } else if (!g_ascii_strcasecmp (channel->label, "BRIGHTNESS")) {
517     value = (vb->brightness + 1) * 2000.0 / 2.0 - 1000.0;
518   } else if (!g_ascii_strcasecmp (channel->label, "CONTRAST")) {
519     value = vb->contrast * 2000.0 / 2.0 - 1000.0;
520   }
521 
522   return value;
523 }
524 
525 static GstColorBalanceType
gst_gl_color_balance_colorbalance_get_balance_type(GstColorBalance * balance)526 gst_gl_color_balance_colorbalance_get_balance_type (GstColorBalance * balance)
527 {
528   return GST_COLOR_BALANCE_HARDWARE;
529 }
530 
531 static void
gst_gl_color_balance_colorbalance_init(GstColorBalanceInterface * iface)532 gst_gl_color_balance_colorbalance_init (GstColorBalanceInterface * iface)
533 {
534   iface->list_channels = gst_gl_color_balance_colorbalance_list_channels;
535   iface->set_value = gst_gl_color_balance_colorbalance_set_value;
536   iface->get_value = gst_gl_color_balance_colorbalance_get_value;
537   iface->get_balance_type = gst_gl_color_balance_colorbalance_get_balance_type;
538 }
539 
540 static GstColorBalanceChannel *
gst_gl_color_balance_find_channel(GstGLColorBalance * balance,const gchar * label)541 gst_gl_color_balance_find_channel (GstGLColorBalance * balance,
542     const gchar * label)
543 {
544   GList *l;
545 
546   for (l = balance->channels; l; l = l->next) {
547     GstColorBalanceChannel *channel = l->data;
548 
549     if (g_ascii_strcasecmp (channel->label, label) == 0)
550       return channel;
551   }
552   return NULL;
553 }
554 
555 static void
gst_gl_color_balance_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)556 gst_gl_color_balance_set_property (GObject * object, guint prop_id,
557     const GValue * value, GParamSpec * pspec)
558 {
559   GstGLColorBalance *balance = GST_GL_COLOR_BALANCE (object);
560   gdouble d;
561   const gchar *label = NULL;
562 
563   GST_OBJECT_LOCK (balance);
564   switch (prop_id) {
565     case PROP_CONTRAST:
566       d = g_value_get_double (value);
567       GST_DEBUG_OBJECT (balance, "Changing contrast from %lf to %lf",
568           balance->contrast, d);
569       if (d != balance->contrast)
570         label = "CONTRAST";
571       balance->contrast = d;
572       break;
573     case PROP_BRIGHTNESS:
574       d = g_value_get_double (value);
575       GST_DEBUG_OBJECT (balance, "Changing brightness from %lf to %lf",
576           balance->brightness, d);
577       if (d != balance->brightness)
578         label = "BRIGHTNESS";
579       balance->brightness = d;
580       break;
581     case PROP_HUE:
582       d = g_value_get_double (value);
583       GST_DEBUG_OBJECT (balance, "Changing hue from %lf to %lf", balance->hue,
584           d);
585       if (d != balance->hue)
586         label = "HUE";
587       balance->hue = d;
588       break;
589     case PROP_SATURATION:
590       d = g_value_get_double (value);
591       GST_DEBUG_OBJECT (balance, "Changing saturation from %lf to %lf",
592           balance->saturation, d);
593       if (d != balance->saturation)
594         label = "SATURATION";
595       balance->saturation = d;
596       break;
597     default:
598       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
599       break;
600   }
601 
602   GST_OBJECT_UNLOCK (balance);
603   gst_gl_color_balance_update_properties (balance);
604 
605   if (label) {
606     GstColorBalanceChannel *channel =
607         gst_gl_color_balance_find_channel (balance, label);
608     gst_color_balance_value_changed (GST_COLOR_BALANCE (balance), channel,
609         gst_color_balance_get_value (GST_COLOR_BALANCE (balance), channel));
610   }
611 }
612 
613 static void
gst_gl_color_balance_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)614 gst_gl_color_balance_get_property (GObject * object, guint prop_id,
615     GValue * value, GParamSpec * pspec)
616 {
617   GstGLColorBalance *balance = GST_GL_COLOR_BALANCE (object);
618 
619   switch (prop_id) {
620     case PROP_CONTRAST:
621       g_value_set_double (value, balance->contrast);
622       break;
623     case PROP_BRIGHTNESS:
624       g_value_set_double (value, balance->brightness);
625       break;
626     case PROP_HUE:
627       g_value_set_double (value, balance->hue);
628       break;
629     case PROP_SATURATION:
630       g_value_set_double (value, balance->saturation);
631       break;
632     default:
633       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
634       break;
635   }
636 }
637