1 /*
2 * Copyright (c) 2015, Igalia S.L
3 * Author: Philippe Normand <philn@igalia.com>
4 * Licence: LGPL. (See COPYING.LGPL)
5 */
6
7 #include <glib.h>
8 #include <gst/gst.h>
9 #include <gst/video/colorbalance.h>
10
11 #define CONTROL_SATURATION 1
12 #define CONTROL_BRIGHTNESS 1
13 #define CONTROL_CONTRAST 1
14
15 #define PIPELINE "rpicamsrc name=src preview=0 fullscreen=0 ! h264parse ! omxh264dec ! glimagesink sync=0"
16
17 #define declare_value(name, value) \
18 static gint current_##name = value; \
19 static gboolean incrementing_##name = TRUE;
20
21 #if CONTROL_SATURATION
22 declare_value (SATURATION, 50);
23 #endif
24 #if CONTROL_BRIGHTNESS
25 declare_value (BRIGHTNESS, 50);
26 #endif
27 #if CONTROL_CONTRAST
28 declare_value (CONTRAST, 0);
29 #endif
30
31 #define update(name, channel, current_value) \
32 if (!g_strcmp0(channel->label, #name)) { \
33 if (current_value >= channel->max_value) \
34 incrementing_##name = FALSE; \
35 else if (current_value <= channel->min_value) \
36 incrementing_##name = TRUE; \
37 current_##name += incrementing_##name ? 10 : -10; \
38 g_print("new " #name ": %d\n", current_##name); \
39 return current_##name; \
40 }
41
42 static gint
compute_value(GstColorBalanceChannel * channel,gint current_value)43 compute_value (GstColorBalanceChannel * channel, gint current_value)
44 {
45 #if CONTROL_SATURATION
46 update (SATURATION, channel, current_value);
47 #endif
48 #if CONTROL_BRIGHTNESS
49 update (BRIGHTNESS, channel, current_value);
50 #endif
51 #if CONTROL_CONTRAST
52 update (CONTRAST, channel, current_value);
53 #endif
54 return current_value;
55 }
56
57 static gboolean
process(gpointer data)58 process (gpointer data)
59 {
60 GstColorBalance *balance = (GstColorBalance *) data;
61 const GList *controls;
62 GstColorBalanceChannel *channel;
63 const GList *item;
64 gint index, new_value, current_value;
65
66 controls = gst_color_balance_list_channels (balance);
67
68 if (controls == NULL) {
69 g_printerr ("There is no list of colorbalance controls\n");
70 return G_SOURCE_REMOVE;
71 }
72
73 for (item = controls, index = 0; item != NULL; item = item->next, ++index) {
74 channel = item->data;
75 current_value = gst_color_balance_get_value (balance, channel);
76 new_value = compute_value (channel, current_value);
77 gst_color_balance_set_value (balance, channel, new_value);
78 }
79
80 return G_SOURCE_CONTINUE;
81 }
82
83 int
main(int argc,char ** argv)84 main (int argc, char **argv)
85 {
86 GMainLoop *loop;
87 GstElement *pipeline;
88 GError *error = NULL;
89 GstElement *src;
90 GstColorBalance *balance;
91
92 gst_init (&argc, &argv);
93 loop = g_main_loop_new (NULL, FALSE);
94
95 pipeline = gst_parse_launch (PIPELINE, &error);
96 if (error != NULL) {
97 g_printerr ("Error parsing '%s': %s", PIPELINE, error->message);
98 g_error_free (error);
99 return -1;
100 }
101
102 gst_element_set_state (pipeline, GST_STATE_PLAYING);
103 src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
104 if (!src) {
105 g_printerr ("Source element not found\n");
106 return -2;
107 }
108
109 balance = GST_COLOR_BALANCE (src);
110 g_timeout_add_seconds (1, process, balance);
111 g_main_loop_run (loop);
112
113 gst_object_unref (src);
114 gst_element_set_state (pipeline, GST_STATE_NULL);
115 return 0;
116 }
117