• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2018 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 the alpha element and converted to OpenGL
21  */
22 
23 /**
24  * SECTION:element-glalpha
25  * @title: glalpha
26  *
27  * The glalpha element adds an alpha channel to a video stream. The values
28  * of the alpha channel can be either be set to a constant or can be
29  * dynamically calculated via chroma keying, e.g. blue can be set as
30  * the transparent color.
31  *
32  * Sample pipeline:
33  * |[
34  * gst-launch-1.0 gltestsrc pattern=snow ! mixer.sink_0 \
35  *   gltestsrc pattern=smpte ! glalpha method=green ! mixer.sink_1 \
36  *   glvideomixer name=mixer sink_0::zorder=0 sink_1::zorder=1 ! \
37  *   glimagesink
38  * ]| This pipeline adds a alpha channel to the SMPTE color bars
39  * with green as the transparent color and overlays the output on
40  * top of a snow video stream.
41  *
42  */
43 
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47 
48 #include "gstglelements.h"
49 #include "gstglalpha.h"
50 #include <string.h>
51 #include <math.h>
52 #include <gst/gl/gstglfuncs.h>
53 
54 #ifndef M_PI
55 #define M_PI  3.14159265358979323846
56 #endif
57 
58 GST_DEBUG_CATEGORY_STATIC (glalpha_debug);
59 #define GST_CAT_DEFAULT glalpha_debug
60 
61 #define GST_TYPE_GL_ALPHA_METHOD (gst_gl_alpha_method_get_type())
62 static GType
gst_gl_alpha_method_get_type(void)63 gst_gl_alpha_method_get_type (void)
64 {
65   static GType alpha_method_type = 0;
66   static const GEnumValue alpha_method[] = {
67     {ALPHA_METHOD_SET, "Set/adjust alpha channel", "set"},
68     {ALPHA_METHOD_GREEN, "Chroma Key on pure green", "green"},
69     {ALPHA_METHOD_BLUE, "Chroma Key on pure blue", "blue"},
70     {ALPHA_METHOD_CUSTOM, "Chroma Key on custom RGB values", "custom"},
71     {0, NULL, NULL},
72   };
73 
74   if (!alpha_method_type) {
75     alpha_method_type =
76         g_enum_register_static ("GstGLAlphaMethod", alpha_method);
77   }
78   return alpha_method_type;
79 }
80 
81 /* GstGLAlpha properties */
82 #define DEFAULT_METHOD ALPHA_METHOD_SET
83 #define DEFAULT_ALPHA 1.0
84 #define DEFAULT_TARGET_R 0
85 #define DEFAULT_TARGET_G 255
86 #define DEFAULT_TARGET_B 0
87 #define DEFAULT_ANGLE 20.0
88 #define DEFAULT_NOISE_LEVEL 2.0
89 #define DEFAULT_BLACK_SENSITIVITY 100
90 #define DEFAULT_WHITE_SENSITIVITY 100
91 
92 enum
93 {
94   PROP_0,
95   PROP_METHOD,
96   PROP_ALPHA,
97   PROP_TARGET_R,
98   PROP_TARGET_G,
99   PROP_TARGET_B,
100   PROP_ANGLE,
101   PROP_NOISE_LEVEL,
102   PROP_BLACK_SENSITIVITY,
103   PROP_WHITE_SENSITIVITY,
104 };
105 
106 /* *INDENT-OFF* */
107 static const gchar *alpha_frag =
108   "varying vec2 v_texcoord;\n"
109   "uniform sampler2D tex;\n"
110   "uniform float alpha;\n"
111   "void main () {\n"
112   "  vec4 rgba = texture2D (tex, v_texcoord);\n"
113   "  rgba.a = alpha;\n"
114   "  gl_FragColor = rgba;\n"
115   "}\n";
116 
117 static const gchar *chroma_key_frag =
118   "varying vec2 v_texcoord;\n"
119   "uniform sampler2D tex;\n"
120   "uniform float cb;\n"
121   "uniform float cr;\n"
122   "uniform float kg;\n"
123   "uniform float accept_angle_tg;\n"
124   "uniform float accept_angle_ctg;\n"
125   "uniform float one_over_kc;\n"
126   "uniform float kfgy_scale;\n"
127   "uniform float noise_level2;\n"
128   "uniform float smin;\n"
129   "uniform float smax;\n"
130   /* these values are taken from the alpha element and divided by 256 to
131    * get the floating point numbers below.
132    * XXX: They are different from the values produced by videoconvert and used
133    * by glcolorconvert
134   298, 0, 409, -57068,
135   298, -100, -208, 34707,
136   298, 516, 0, -70870,
137   */
138   "#define from_yuv_bt601_offset vec3(-0.0625, -0.5, -0.5)\n"
139   "#define from_yuv_bt601_rcoeff vec3(1.1640625, 0.000, 1.787)\n"
140   "#define from_yuv_bt601_gcoeff vec3(1.1640625,-0.213,-0.531)\n"
141   "#define from_yuv_bt601_bcoeff vec3(1.1640625, 2.112, 0.000)\n"
142   /* these values are taken from the alpha element and divided by 256 to
143    * get the floating point numbers below
144    * XXX: They are different from the values produced by videoconvert and used
145    * by glcolorconvert
146   66, 129, 25, 4096,
147   -38, -74, 112, 32768,
148   112, -94, -18, 32768,
149   */
150   "#define from_rgb_bt601_offset vec3(0.0625, 0.5, 0.5)\n"
151   "#define from_rgb_bt601_ycoeff vec3( 0.2578125, 0.50390625, 0.09765625)\n"
152   "#define from_rgb_bt601_ucoeff vec3(-0.1484375,-0.28906250, 0.43750000)\n"
153   "#define from_rgb_bt601_vcoeff vec3( 0.4375000,-0.36718750,-0.07031250)\n"
154   "#define PI 3.14159265\n"
155   "\n"
156   "vec3 yuv_to_rgb (vec3 val) {\n"
157   "  vec3 rgb;\n"
158   "  val += from_yuv_bt601_offset;\n"
159   "  rgb.r = dot(val, from_yuv_bt601_rcoeff);\n"
160   "  rgb.g = dot(val, from_yuv_bt601_gcoeff);\n"
161   "  rgb.b = dot(val, from_yuv_bt601_bcoeff);\n"
162   "  return rgb;\n"
163   "}\n"
164   "vec3 rgb_to_yuv (vec3 val) {\n"
165   "  vec3 yuv;\n"
166   "  yuv.r = dot(val.rgb, from_rgb_bt601_ycoeff);\n"
167   "  yuv.g = dot(val.rgb, from_rgb_bt601_ucoeff);\n"
168   "  yuv.b = dot(val.rgb, from_rgb_bt601_vcoeff);\n"
169   "  yuv += from_rgb_bt601_offset;\n"
170   "  return yuv;\n"
171   "}\n"
172   "vec4 chroma_keying_yuv (vec4 yuva) {\n"
173   "  float y, u, v;\n"
174   "  y = yuva.x;\n"
175   "  u = yuva.y-0.5;\n"
176   "  v = yuva.z-0.5;\n"
177   "  if (y < smin || y > smax) {\n"
178   "    return yuva;\n"
179   "  }\n"
180   "  vec4 new;\n"
181   "  float tmp, tmp1;\n"
182   "  float x = clamp ((u * cb + v * cr) * 2.0, -0.5, 0.5);\n"
183   "  float z = clamp ((v * cb - u * cr) * 2.0, -0.5, 0.5);\n"
184   "  if (abs(z) > min (0.5, x * accept_angle_tg * 0.0625)) {\n"
185   "    return yuva;\n"
186   "  }\n"
187   "  float x1 = min (0.5, abs(z * accept_angle_ctg * 0.0625));\n"
188   "  float y1 = z;\n"
189   "  tmp1 = max (0.0, x-x1);\n"
190   "  float b_alpha = yuva.a * (1.0 - clamp (tmp1 * one_over_kc, 0.0, 1.0));\n"
191   "  tmp = min (1.0, tmp1 * kfgy_scale * 0.0625);\n"
192   "  new.x = max (y-tmp, 0.0);//y < tmp ? 0.0 : y - tmp;\n"
193   "  new.y = clamp ((x1 * cb - y1 * cr) * 2.0, -0.5, 0.5)+0.5;\n"
194   "  new.z = clamp ((y1 * cb + x1 * cr) * 2.0, -0.5, 0.5)+0.5;\n"
195   "  new.a = z * z + (x - kg) * (x - kg) < noise_level2 ? 0.0 : b_alpha;\n"
196   "  return new;\n"
197   "}\n"
198   "void main () {\n"
199   "  vec4 yuva;\n"
200   /* operations translated from alpha and tested with glvideomixer
201    * with one pad's parameters blend-equation-rgb={subtract,reverse-subtract},
202    * blend-function-src-rgb=src-color and blend-function-dst-rgb=dst-color */
203   "  vec4 rgba = texture2D (tex, v_texcoord);\n"
204   "  yuva.xyz = rgb_to_yuv (rgba.rgb);\n"
205   "  yuva.a = rgba.a;\n"
206   "  yuva = chroma_keying_yuv (yuva);\n"
207   "  rgba.rgb = yuv_to_rgb (yuva.xyz);\n"
208   "  rgba.a = yuva.a;\n"
209   "  gl_FragColor = rgba;\n"
210   "}\n";
211 /* *INDENT-ON* */
212 
213 static const gfloat cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
214   0.2578125, 0.50390625, 0.09765625, 0.0625f,
215   -0.1484375, -0.28906250, 0.43750000, 0.5f,
216   0.4375000, -0.36718750, -0.07031250, 0.5f
217 };
218 
219 static void gst_gl_alpha_set_property (GObject * object, guint prop_id,
220     const GValue * value, GParamSpec * pspec);
221 static void gst_gl_alpha_get_property (GObject * object, guint prop_id,
222     GValue * value, GParamSpec * pspec);
223 
224 #define gst_gl_alpha_parent_class parent_class
225 G_DEFINE_TYPE (GstGLAlpha, gst_gl_alpha, GST_TYPE_GL_FILTER);
226 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (glalpha, "glalpha",
227     GST_RANK_NONE, GST_TYPE_GL_ALPHA, gl_element_init (plugin));
228 
229 static gboolean
gst_gl_alpha_is_passthrough(GstGLAlpha * glalpha)230 gst_gl_alpha_is_passthrough (GstGLAlpha * glalpha)
231 {
232   return glalpha->method == ALPHA_METHOD_SET && glalpha->alpha == 1.0;
233 }
234 
235 /* move v to be inside [from, to] by some multiple of (to-from) */
236 static float
wrap(float v,float from,float to)237 wrap (float v, float from, float to)
238 {
239   float diff;
240 
241   if (from > to) {
242     float t = to;
243     to = from;
244     from = t;
245   }
246 
247   if (!isfinite (to) || !isfinite (from) || !isfinite (v))
248     return v;
249 
250   diff = to - from;
251 
252   while (v < from)
253     v += diff;
254   while (v > to)
255     v -= diff;
256 
257   return v;
258 }
259 
260 static void
gst_gl_alpha_update_properties(GstGLAlpha * glalpha)261 gst_gl_alpha_update_properties (GstGLAlpha * glalpha)
262 {
263   GstBaseTransform *base = GST_BASE_TRANSFORM (glalpha);
264   gboolean current_passthrough, passthrough;
265   gfloat kgl;
266   gfloat tmp;
267   gfloat target_r, target_g, target_b;
268   gfloat target_y, target_u, target_v;
269   const float *matrix = cog_rgb_to_ycbcr_matrix_8bit_sdtv;
270 
271   GST_OBJECT_LOCK (glalpha);
272   switch (glalpha->method) {
273     case ALPHA_METHOD_GREEN:
274       target_r = 0.0;
275       target_g = 1.0;
276       target_b = 0.0;
277       break;
278     case ALPHA_METHOD_BLUE:
279       target_r = 0.0;
280       target_g = 0.0;
281       target_b = 1.0;
282       break;
283     default:
284       target_r = (gfloat) glalpha->target_r / 255.0;
285       target_g = (gfloat) glalpha->target_g / 255.0;
286       target_b = (gfloat) glalpha->target_b / 255.0;
287       break;
288   }
289 
290   target_y =
291       matrix[0] * target_r + matrix[1] * target_g + matrix[2] * target_b +
292       matrix[3];
293   /* Cb,Cr without offset here because the chroma keying
294    * works with them being in range [-128,127]
295    */
296   target_u = matrix[4] * target_r + matrix[5] * target_g + matrix[6] * target_b;
297   target_v =
298       matrix[8] * target_r + matrix[9] * target_g + matrix[10] * target_b;
299 
300   tmp = target_u * target_u + target_v * target_v;
301   kgl = sqrt (tmp);
302   glalpha->cb = target_u / kgl * 0.5;
303   glalpha->cr = target_v / kgl * 0.5;
304 
305   tmp = 15 * tan (M_PI * glalpha->angle / 180);
306   tmp = MIN (tmp, 255);
307   glalpha->accept_angle_tg = tmp;
308   tmp = 15 / tan (M_PI * glalpha->angle / 180);
309   tmp = MIN (tmp, 255);
310   glalpha->accept_angle_ctg = tmp;
311   glalpha->one_over_kc = wrap (2 / kgl - 255, 0, 256);
312   tmp = 15 * target_y / kgl;
313   tmp = MIN (tmp, 255);
314   glalpha->kfgy_scale = tmp;
315   glalpha->kg = MIN (kgl, 0.5);
316 
317   glalpha->noise_level2 =
318       glalpha->noise_level / 256.0 * glalpha->noise_level / 256.0;
319 
320   GST_INFO_OBJECT (glalpha, "target yuv: %f, %f, %f, "
321       "kgl: %f, cb: %f, cr: %f, accept_angle_tg: %f, accept_angle_ctg: %f, "
322       "one_over_kc: %f, kgfy_scale: %f, kg: %f, noise level: %f",
323       (float) target_y, (float) target_u, (float) target_v, (float) kgl,
324       (float) glalpha->cb, (float) glalpha->cr,
325       (float) glalpha->accept_angle_tg, (float) glalpha->accept_angle_ctg,
326       (float) glalpha->one_over_kc, (float) glalpha->kfgy_scale,
327       (float) glalpha->kg, (float) glalpha->noise_level2);
328 
329   passthrough = gst_gl_alpha_is_passthrough (glalpha);
330   GST_OBJECT_UNLOCK (glalpha);
331   current_passthrough = gst_base_transform_is_passthrough (base);
332 
333   gst_base_transform_set_passthrough (base, passthrough);
334   if (current_passthrough != passthrough)
335     gst_base_transform_reconfigure_src (base);
336 }
337 
338 static gboolean
_create_shader(GstGLAlpha * alpha)339 _create_shader (GstGLAlpha * alpha)
340 {
341   GstGLBaseFilter *base_filter = GST_GL_BASE_FILTER (alpha);
342   GstGLFilter *filter = GST_GL_FILTER (alpha);
343   GError *error = NULL;
344   const gchar *frags[2];
345 
346   if (alpha->alpha_shader)
347     gst_object_unref (alpha->alpha_shader);
348 
349   frags[0] =
350       gst_gl_shader_string_get_highest_precision (base_filter->context,
351       GST_GLSL_VERSION_NONE,
352       GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY);
353   frags[1] = alpha_frag;
354 
355   if (!(alpha->alpha_shader =
356           gst_gl_shader_new_link_with_stages (base_filter->context, &error,
357               gst_glsl_stage_new_default_vertex (base_filter->context),
358               gst_glsl_stage_new_with_strings (base_filter->context,
359                   GL_FRAGMENT_SHADER, GST_GLSL_VERSION_NONE,
360                   GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY, 2,
361                   frags), NULL))) {
362     GST_ELEMENT_ERROR (alpha, RESOURCE, NOT_FOUND, ("%s",
363             "Failed to initialize alpha shader"), ("%s",
364             error ? error->message : "Unknown error"));
365     return FALSE;
366   }
367 
368   if (alpha->chroma_key_shader)
369     gst_object_unref (alpha->chroma_key_shader);
370 
371   frags[1] = chroma_key_frag;
372 
373   if (!(alpha->chroma_key_shader =
374           gst_gl_shader_new_link_with_stages (base_filter->context, &error,
375               gst_glsl_stage_new_default_vertex (base_filter->context),
376               gst_glsl_stage_new_with_strings (base_filter->context,
377                   GL_FRAGMENT_SHADER, GST_GLSL_VERSION_NONE,
378                   GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY, 2,
379                   frags), NULL))) {
380     GST_ELEMENT_ERROR (alpha, RESOURCE, NOT_FOUND, ("%s",
381             "Failed to initialize chroma key shader"), ("%s",
382             error ? error->message : "Unknown error"));
383     return FALSE;
384   }
385 
386   filter->draw_attr_position_loc =
387       gst_gl_shader_get_attribute_location (alpha->alpha_shader, "a_position");
388   filter->draw_attr_texture_loc =
389       gst_gl_shader_get_attribute_location (alpha->alpha_shader, "a_texcoord");
390 
391   return TRUE;
392 }
393 
394 static gboolean
gst_gl_alpha_gl_start(GstGLBaseFilter * base_filter)395 gst_gl_alpha_gl_start (GstGLBaseFilter * base_filter)
396 {
397   GstGLAlpha *alpha = GST_GL_ALPHA (base_filter);
398 
399   if (!_create_shader (alpha))
400     return FALSE;
401 
402   return GST_GL_BASE_FILTER_CLASS (parent_class)->gl_start (base_filter);
403 }
404 
405 static void
gst_gl_alpha_gl_stop(GstGLBaseFilter * base_filter)406 gst_gl_alpha_gl_stop (GstGLBaseFilter * base_filter)
407 {
408   GstGLAlpha *alpha = GST_GL_ALPHA (base_filter);
409 
410   if (alpha->alpha_shader)
411     gst_object_unref (alpha->alpha_shader);
412   alpha->alpha_shader = NULL;
413 
414   if (alpha->chroma_key_shader)
415     gst_object_unref (alpha->chroma_key_shader);
416   alpha->chroma_key_shader = NULL;
417 
418   GST_GL_BASE_FILTER_CLASS (parent_class)->gl_stop (base_filter);
419 }
420 
421 static void
gst_gl_alpha_before_transform(GstBaseTransform * base,GstBuffer * buf)422 gst_gl_alpha_before_transform (GstBaseTransform * base, GstBuffer * buf)
423 {
424   GstGLAlpha *alpha = GST_GL_ALPHA (base);
425   GstClockTime timestamp, stream_time;
426 
427   timestamp = GST_BUFFER_TIMESTAMP (buf);
428   stream_time =
429       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
430 
431   GST_DEBUG_OBJECT (alpha, "sync to %" GST_TIME_FORMAT,
432       GST_TIME_ARGS (timestamp));
433 
434   if (GST_CLOCK_TIME_IS_VALID (stream_time))
435     gst_object_sync_values (GST_OBJECT (alpha), stream_time);
436 }
437 
438 static gboolean
gst_gl_alpha_filter_texture(GstGLFilter * filter,GstGLMemory * in_tex,GstGLMemory * out_tex)439 gst_gl_alpha_filter_texture (GstGLFilter * filter, GstGLMemory * in_tex,
440     GstGLMemory * out_tex)
441 {
442   GstGLAlpha *alpha = GST_GL_ALPHA (filter);
443   GstGLShader *shader;
444 
445   if (!alpha->alpha_shader)
446     _create_shader (alpha);
447 
448   GST_OBJECT_LOCK (alpha);
449   if (alpha->method == ALPHA_METHOD_SET) {
450     shader = alpha->alpha_shader;
451     gst_gl_shader_use (shader);
452     gst_gl_shader_set_uniform_1f (shader, "alpha", alpha->alpha);
453   } else {
454     shader = alpha->chroma_key_shader;
455     gst_gl_shader_use (shader);
456 
457     gst_gl_shader_set_uniform_1f (shader, "cb", alpha->cb);
458     gst_gl_shader_set_uniform_1f (shader, "cr", alpha->cr);
459     gst_gl_shader_set_uniform_1f (shader, "kg", alpha->kg);
460     gst_gl_shader_set_uniform_1f (shader, "accept_angle_tg",
461         alpha->accept_angle_tg);
462     gst_gl_shader_set_uniform_1f (shader, "accept_angle_ctg",
463         alpha->accept_angle_ctg);
464     gst_gl_shader_set_uniform_1f (shader, "one_over_kc", alpha->one_over_kc);
465     gst_gl_shader_set_uniform_1f (shader, "kfgy_scale", alpha->kfgy_scale);
466     gst_gl_shader_set_uniform_1f (shader, "noise_level2", alpha->noise_level2);
467     gst_gl_shader_set_uniform_1f (shader, "smin",
468         0.5 - alpha->black_sensitivity / 255.0);
469     gst_gl_shader_set_uniform_1f (shader, "smax",
470         0.5 + alpha->white_sensitivity / 255.0);
471   }
472   GST_OBJECT_UNLOCK (alpha);
473 
474   gst_gl_filter_render_to_target_with_shader (filter, in_tex, out_tex, shader);
475 
476   return TRUE;
477 }
478 
479 static void
gst_gl_alpha_class_init(GstGLAlphaClass * klass)480 gst_gl_alpha_class_init (GstGLAlphaClass * klass)
481 {
482   GObjectClass *gobject_class = (GObjectClass *) klass;
483   GstElementClass *gstelement_class = (GstElementClass *) klass;
484   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
485   GstGLBaseFilterClass *base_filter_class = (GstGLBaseFilterClass *) klass;
486   GstGLFilterClass *filter_class = (GstGLFilterClass *) klass;
487 
488   GST_DEBUG_CATEGORY_INIT (glalpha_debug, "glalpha", 0, "glalpha");
489 
490   gst_gl_filter_add_rgba_pad_templates (GST_GL_FILTER_CLASS (klass));
491 
492   gobject_class->set_property = gst_gl_alpha_set_property;
493   gobject_class->get_property = gst_gl_alpha_get_property;
494 
495   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_METHOD,
496       g_param_spec_enum ("method", "Method",
497           "How the alpha channels should be created", GST_TYPE_GL_ALPHA_METHOD,
498           DEFAULT_METHOD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
499   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ALPHA,
500       g_param_spec_double ("alpha", "Alpha", "The value for the alpha channel",
501           0.0, 1.0, DEFAULT_ALPHA,
502           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
503   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TARGET_R,
504       g_param_spec_uint ("target-r", "Target Red",
505           "The red color value for custom RGB chroma keying", 0, 255,
506           DEFAULT_TARGET_R,
507           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
508   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TARGET_G,
509       g_param_spec_uint ("target-g", "Target Green",
510           "The green color value for custom RGB chroma keying", 0, 255,
511           DEFAULT_TARGET_G,
512           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
513   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TARGET_B,
514       g_param_spec_uint ("target-b", "Target Blue",
515           "The blue color value for custom RGB chroma keying", 0, 255,
516           DEFAULT_TARGET_B,
517           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
518   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ANGLE,
519       g_param_spec_float ("angle", "Angle", "Size of the colorcube to change",
520           0.0, 90.0, DEFAULT_ANGLE,
521           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
522   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_NOISE_LEVEL,
523       g_param_spec_float ("noise-level", "Noise Level", "Size of noise radius",
524           0.0, 64.0, DEFAULT_NOISE_LEVEL,
525           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
526   g_object_class_install_property (G_OBJECT_CLASS (klass),
527       PROP_BLACK_SENSITIVITY, g_param_spec_uint ("black-sensitivity",
528           "Black Sensitivity", "Sensitivity to dark colors", 0, 128,
529           DEFAULT_BLACK_SENSITIVITY,
530           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
531   g_object_class_install_property (G_OBJECT_CLASS (klass),
532       PROP_WHITE_SENSITIVITY, g_param_spec_uint ("white-sensitivity",
533           "White Sensitivity", "Sensitivity to bright colors", 0, 128,
534           DEFAULT_WHITE_SENSITIVITY,
535           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
536 
537   gst_element_class_set_static_metadata (gstelement_class,
538       "OpenGL Alpha Filter", "Filter/Effect/Video",
539       "Adds an alpha channel to video using OpenGL - uniform or chroma-keying",
540       "Matthew Waters <matthew@centricular.com>");
541 
542   trans_class->before_transform =
543       GST_DEBUG_FUNCPTR (gst_gl_alpha_before_transform);
544   trans_class->transform_ip_on_passthrough = FALSE;
545 
546   base_filter_class->gl_start = GST_DEBUG_FUNCPTR (gst_gl_alpha_gl_start);
547   base_filter_class->gl_stop = GST_DEBUG_FUNCPTR (gst_gl_alpha_gl_stop);
548 
549   filter_class->filter_texture =
550       GST_DEBUG_FUNCPTR (gst_gl_alpha_filter_texture);
551 
552   gst_type_mark_as_plugin_api (GST_TYPE_GL_ALPHA_METHOD, 0);
553 }
554 
555 static void
gst_gl_alpha_init(GstGLAlpha * alpha)556 gst_gl_alpha_init (GstGLAlpha * alpha)
557 {
558   alpha->alpha = DEFAULT_ALPHA;
559   alpha->method = DEFAULT_METHOD;
560   alpha->target_r = DEFAULT_TARGET_R;
561   alpha->target_g = DEFAULT_TARGET_G;
562   alpha->target_b = DEFAULT_TARGET_B;
563   alpha->angle = DEFAULT_ANGLE;
564   alpha->noise_level = DEFAULT_NOISE_LEVEL;
565   alpha->black_sensitivity = DEFAULT_BLACK_SENSITIVITY;
566   alpha->white_sensitivity = DEFAULT_WHITE_SENSITIVITY;
567 
568   gst_gl_alpha_update_properties (alpha);
569 }
570 
571 static void
gst_gl_alpha_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)572 gst_gl_alpha_set_property (GObject * object, guint prop_id,
573     const GValue * value, GParamSpec * pspec)
574 {
575   GstGLAlpha *alpha = GST_GL_ALPHA (object);
576 
577   GST_OBJECT_LOCK (alpha);
578   switch (prop_id) {
579     case PROP_METHOD:{
580       alpha->method = g_value_get_enum (value);
581       break;
582     }
583     case PROP_ALPHA:{
584       alpha->alpha = g_value_get_double (value);
585       break;
586     }
587     case PROP_TARGET_R:
588       alpha->target_r = g_value_get_uint (value);
589       break;
590     case PROP_TARGET_G:
591       alpha->target_g = g_value_get_uint (value);
592       break;
593     case PROP_TARGET_B:
594       alpha->target_b = g_value_get_uint (value);
595       break;
596     case PROP_ANGLE:
597       alpha->angle = g_value_get_float (value);
598       break;
599     case PROP_NOISE_LEVEL:
600       alpha->noise_level = g_value_get_float (value);
601       break;
602     case PROP_BLACK_SENSITIVITY:
603       alpha->black_sensitivity = g_value_get_uint (value);
604       break;
605     case PROP_WHITE_SENSITIVITY:
606       alpha->white_sensitivity = g_value_get_uint (value);
607       break;
608     default:
609       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
610       break;
611   }
612   GST_OBJECT_UNLOCK (alpha);
613   gst_gl_alpha_update_properties (alpha);
614 }
615 
616 static void
gst_gl_alpha_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)617 gst_gl_alpha_get_property (GObject * object, guint prop_id,
618     GValue * value, GParamSpec * pspec)
619 {
620   GstGLAlpha *alpha = GST_GL_ALPHA (object);
621 
622   switch (prop_id) {
623     case PROP_METHOD:
624       g_value_set_enum (value, alpha->method);
625       break;
626     case PROP_ALPHA:
627       g_value_set_double (value, alpha->alpha);
628       break;
629     case PROP_TARGET_R:
630       g_value_set_uint (value, alpha->target_r);
631       break;
632     case PROP_TARGET_G:
633       g_value_set_uint (value, alpha->target_g);
634       break;
635     case PROP_TARGET_B:
636       g_value_set_uint (value, alpha->target_b);
637       break;
638     case PROP_ANGLE:
639       g_value_set_float (value, alpha->angle);
640       break;
641     case PROP_NOISE_LEVEL:
642       g_value_set_float (value, alpha->noise_level);
643       break;
644     case PROP_BLACK_SENSITIVITY:
645       g_value_set_uint (value, alpha->black_sensitivity);
646       break;
647     case PROP_WHITE_SENSITIVITY:
648       g_value_set_uint (value, alpha->white_sensitivity);
649       break;
650     default:
651       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
652       break;
653   }
654 }
655