summaryrefslogtreecommitdiff
path: root/libs/control/gstdparam.c
blob: 665b66d2a84d9b9827e1d56df64598d2da7a8894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* GStreamer
 * Copyright (C) 2001 Steve Baker <stevebaker_org@yahoo.co.uk>
 *
 * gstdparam.c: Dynamic Parameter functionality
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <math.h>
#include <string.h>
#include <gst/gstinfo.h>

#include "gstdparam.h"
#include "gstdparammanager.h"

static void gst_dparam_class_init (GstDParamClass *klass);
static void gst_dparam_init (GstDParam *dparam);

static void gst_dparam_do_update_realtime (GstDParam *dparam, gint64 timestamp);
static GValue** gst_dparam_get_point_realtime (GstDParam *dparam, gint64 timestamp);

GType 
gst_dparam_get_type(void) {
	static GType dparam_type = 0;

	if (!dparam_type) {
		static const GTypeInfo dparam_info = {
			sizeof(GstDParamClass),
			NULL,
			NULL,
			(GClassInitFunc)gst_dparam_class_init,
			NULL,
			NULL,
			sizeof(GstDParam),
			0,
			(GInstanceInitFunc)gst_dparam_init,
		};
		dparam_type = g_type_register_static(GST_TYPE_OBJECT, "GstDParam", &dparam_info, 0);
	}
	return dparam_type;
}

static void
gst_dparam_class_init (GstDParamClass *klass)
{
	GObjectClass *gobject_class;
	GstDParamClass *dparam_class;
	GstObjectClass *gstobject_class;

	gobject_class = (GObjectClass*)klass;
	dparam_class = (GstDParamClass*)klass;
	gstobject_class = (GstObjectClass*) klass;

	//gstobject_class->save_thyself = gst_dparam_save_thyself;

}

static void
gst_dparam_init (GstDParam *dparam)
{
	g_return_if_fail (dparam != NULL);
	GST_DPARAM_VALUE(dparam) = NULL;
	GST_DPARAM_TYPE(dparam) = 0;
	GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam)=0LL;
	GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam)=0LL;
	GST_DPARAM_READY_FOR_UPDATE(dparam)=FALSE;
	dparam->lock = g_mutex_new ();
}

/**
 * gst_dparam_new:
 * @type: the type that this dparam will store
 *
 * Returns: a new instance of GstDParam
 */
GstDParam* 
gst_dparam_new (GType type)
{
	GstDParam *dparam;

	dparam = g_object_new (gst_dparam_get_type (), NULL);
	dparam->do_update_func = gst_dparam_do_update_realtime;
	dparam->get_point_func = gst_dparam_get_point_realtime;
	
	dparam->point = gst_dparam_new_value_array(type, 0);	
	GST_DPARAM_TYPE(dparam) = type;
	
	return dparam;
}

/**
 * gst_dparam_attach
 * @dparam: GstDParam instance
 * @parent: the GstDParamManager that this dparam belongs to
 *
 */
void
gst_dparam_attach (GstDParam *dparam, GstObject *parent, GValue *value, GstDParamSpec *spec)
{
	
	g_return_if_fail (dparam != NULL);
	g_return_if_fail (GST_IS_DPARAM (dparam));
	g_return_if_fail (GST_DPARAM_PARENT (dparam) == NULL);
	g_return_if_fail (parent != NULL);
	g_return_if_fail (G_IS_OBJECT (parent));
	g_return_if_fail (GST_IS_DPMAN (parent));
	g_return_if_fail ((gpointer)dparam != (gpointer)parent);
	g_return_if_fail (value != NULL);
	g_return_if_fail (spec != NULL);
	g_return_if_fail (GST_DPARAM_TYPE(dparam) == G_VALUE_TYPE(value));
	
	GST_DPARAM_NAME(dparam) = spec->dparam_name;
	GST_DPARAM_VALUE(dparam) = value;
	GST_DPARAM_SPEC(dparam) = spec;
	gst_object_set_parent (GST_OBJECT (dparam), parent);
}

/**
 * gst_dparam_new_value_array
 * @type: the type of the first GValue in the array
 * @...: the type of other GValues in the array
 *
 * The list of types should be terminated with a 0.
 * If the type of a value is not yet known then use G_TYPE_NONE .
 *
 * Returns: an newly created array of GValues
 */
GValue**
gst_dparam_new_value_array(GType type, ...)
{
	GValue **point;
	GValue *value;
	guint x;
	guint values_length = 0;
	va_list var_args;
	GType each_type;

	va_start (var_args, type);
	each_type = type;
	while (each_type){
		values_length++;
		each_type = va_arg (var_args, GType);
	}
	va_end (var_args);
	
	point = g_new0(GValue*,values_length + 1);

	va_start (var_args, type);
	each_type = type;
	for (x=0 ; x < values_length ; x++){
		value = g_new0(GValue,1);
		if (each_type != G_TYPE_NONE){
			g_value_init(value, each_type);
		}
		point[x] = value;
		each_type = va_arg (var_args, GType);
	}
	point[values_length] = NULL;
	va_end (var_args);
	
	GST_DEBUG(GST_CAT_PARAMS, "array with %d values created\n", values_length);

	return point;
}

void
gst_dparam_set_value_from_string(GValue *value, const gchar *value_str)
{

	g_return_if_fail(value != NULL);
	g_return_if_fail(value_str != NULL);
	
	GST_DEBUG(GST_CAT_PARAMS, "parsing '%s' to type %s\n", value_str, g_type_name(G_VALUE_TYPE(value)));

	switch (G_VALUE_TYPE(value)) {
		case G_TYPE_STRING:
			g_value_set_string(value, g_strdup(value_str));
			break;
		case G_TYPE_ENUM: 
		case G_TYPE_INT: {
			gint i;
			sscanf (value_str, "%d", &i);
			g_value_set_int(value, i);
			break;
		}
		case G_TYPE_UINT: {
			guint i;
			sscanf (value_str, "%u", &i);
			g_value_set_uint(value, i);
			break;
		}
		case G_TYPE_LONG: {
			glong i;
			sscanf (value_str, "%ld", &i);
			g_value_set_long(value, i);
			break;
		}
		case G_TYPE_ULONG: {
			gulong i;
			sscanf (value_str, "%lu", &i);
			g_value_set_ulong(value, i);
			break;
		}
		case G_TYPE_BOOLEAN: {
			gboolean i = FALSE;
			if (!strncmp ("true", value_str, 4)) i = TRUE;
			g_value_set_boolean(value, i);
			break;
		}
		case G_TYPE_CHAR: {
			gchar i;
			sscanf (value_str, "%c", &i);
			g_value_set_char(value, i);
			break;
		}
		case G_TYPE_UCHAR: {
			guchar i;
			sscanf (value_str, "%c", &i);
			g_value_set_uchar(value, i);
			break;
		}
		case G_TYPE_FLOAT: {
			gfloat i;
			sscanf (value_str, "%f", &i);
			g_value_set_float(value, i);
			break;
		}
		case G_TYPE_DOUBLE: {
			gfloat i;
			sscanf (value_str, "%g", &i);
			g_value_set_double(value, (gdouble)i);
			break;
		}
		default:
	  		break;
	}
}

static void
gst_dparam_do_update_realtime (GstDParam *dparam, gint64 timestamp)
{
	GST_DPARAM_LOCK(dparam);
	GST_DPARAM_READY_FOR_UPDATE(dparam) = FALSE;
	GST_DEBUG(GST_CAT_PARAMS, "updating value for %s(%p)\n",GST_DPARAM_NAME (dparam),dparam);
	g_value_copy(dparam->point[0], GST_DPARAM_VALUE(dparam));
	GST_DPARAM_UNLOCK(dparam);
}

static GValue** 
gst_dparam_get_point_realtime (GstDParam *dparam, gint64 timestamp)
{
	GST_DEBUG(GST_CAT_PARAMS, "getting point for %s(%p)\n",GST_DPARAM_NAME (dparam),dparam);
	return dparam->point;
}

/**********************
 * GstDParamSmooth
 **********************/

static void gst_dparam_do_update_smooth (GstDParam *dparam, gint64 timestamp);
static GValue** gst_dparam_get_point_smooth (GstDParam *dparam, gint64 timestamp);

/**
 * gst_dparam_smooth_new:
 * @type: the type that this dparam will store
 *
 * Returns: a new instance of GstDParamSmooth
 */
GstDParam* 
gst_dparam_smooth_new (GType type)
{
	GstDParam *dparam;

	dparam = g_object_new (gst_dparam_get_type (), NULL);
	
	dparam->do_update_func = gst_dparam_do_update_smooth;
	dparam->get_point_func = gst_dparam_get_point_smooth;
	
	dparam->point = gst_dparam_new_value_array(type, type, G_TYPE_FLOAT, 0);	
	GST_DPARAM_TYPE(dparam) = type;
	
	return dparam;
}

static void
gst_dparam_do_update_smooth (GstDParam *dparam, gint64 timestamp)
{
	gint64 time_diff;
	gfloat time_ratio;
	
	time_diff = MIN(GST_DPARAM_DEFAULT_UPDATE_PERIOD(dparam), 
	                timestamp - GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam));
	                
	time_ratio = (gfloat)time_diff / g_value_get_float(dparam->point[2]);

	GST_DPARAM_LOCK(dparam);

	GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam) = GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam);  
	while(GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam) <= timestamp){
		GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam) += GST_DPARAM_DEFAULT_UPDATE_PERIOD(dparam);
	}
	GST_DEBUG(GST_CAT_PARAMS, "last:%lld current:%lld next:%lld\n",
	                           GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam), timestamp, GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam));

	
	switch (G_VALUE_TYPE(GST_DPARAM_VALUE(dparam))){
		case G_TYPE_FLOAT: {
			gfloat current, target, max_change, current_diff, final_val;
			
			target = g_value_get_float(dparam->point[0]);
			current = g_value_get_float(GST_DPARAM_VALUE(dparam));
			max_change = time_ratio * g_value_get_float(dparam->point[1]);

			GST_DEBUG(GST_CAT_PARAMS, "target:%f current:%f max_change:%f \n", 
			                           target, current, max_change);
			                           
			if (dparam->spec->is_log){
				gfloat current_log;
				current_log = log(current);
				current_diff = ABS(current_log - log(target));
				if (current_diff > max_change)
					final_val = (target < current) ? exp(current_log-max_change) : exp(current_log+max_change);
				else
					final_val = target;
			} 
			else {
				current_diff = ABS (current - target);
				if (current_diff > max_change)
					final_val = (target < current) ? current-max_change : current+max_change;
				else
					final_val = target;									
			}

			GST_DPARAM_READY_FOR_UPDATE(dparam) = (current_diff > max_change);
			g_value_set_float(GST_DPARAM_VALUE(dparam), final_val);
			
			break;
		}
		default:
			break;
	}

		                           
	//GST_DEBUG(GST_CAT_PARAMS, "smooth update for %s(%p): %f\n",
	//                           GST_DPARAM_NAME (dparam),dparam, g_value_get_float(GST_DPARAM_VALUE(dparam)));

	GST_DPARAM_UNLOCK(dparam);
}

static GValue** 
gst_dparam_get_point_smooth (GstDParam *dparam, gint64 timestamp)
{
	GST_DEBUG(GST_CAT_PARAMS, "getting point for %s(%p)\n",GST_DPARAM_NAME (dparam),dparam);
	return dparam->point;
}