2e0607819b49e404003b373c3cc0573fa6a24a89
[platform/upstream/gstreamer.git] / libs / gst / control / dparam_smooth.c
1 /* GStreamer
2  * Copyright (C) 2001 Steve Baker <stevebaker_org@yahoo.co.uk>
3  *
4  * gstdparam_smooth.c: Realtime smoothed dynamic parameter
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <math.h>
23 #include <string.h>
24 #include <gst/gstinfo.h>
25
26 #include "dparam_smooth.h"
27 #include "dparammanager.h"
28
29 static void gst_dpsmooth_class_init (GstDParamSmoothClass *klass);
30 static void gst_dpsmooth_init (GstDParamSmooth *dparam);
31 static void gst_dpsmooth_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
32 static void gst_dpsmooth_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
33 static void gst_dpsmooth_do_update_float (GstDParam *dparam, gint64 timestamp, GValue *value, GstDParamUpdateInfo update_info);
34 static void gst_dpsmooth_value_changed_float (GstDParam *dparam);
35
36 enum {
37         ARG_0,
38         ARG_UPDATE_PERIOD,
39         ARG_SLOPE_TIME,
40         ARG_SLOPE_DELTA_FLOAT,
41         ARG_SLOPE_DELTA_INT,
42         ARG_SLOPE_DELTA_INT64,
43 };
44
45 GType 
46 gst_dpsmooth_get_type(void) {
47         static GType dpsmooth_type = 0;
48
49         if (!dpsmooth_type) {
50                 static const GTypeInfo dpsmooth_info = {
51                         sizeof(GstDParamSmoothClass),
52                         NULL,
53                         NULL,
54                         (GClassInitFunc)gst_dpsmooth_class_init,
55                         NULL,
56                         NULL,
57                         sizeof(GstDParamSmooth),
58                         0,
59                         (GInstanceInitFunc)gst_dpsmooth_init,
60                 };
61                 dpsmooth_type = g_type_register_static(GST_TYPE_DPARAM, "GstDParamSmooth", &dpsmooth_info, 0);
62         }
63         return dpsmooth_type;
64 }
65
66 static void
67 gst_dpsmooth_class_init (GstDParamSmoothClass *klass)
68 {
69         GObjectClass *gobject_class;
70         GstDParamSmoothClass *dpsmooth_class;
71         GstObjectClass *gstobject_class;
72
73         gobject_class = (GObjectClass*)klass;
74         dpsmooth_class = (GstDParamSmoothClass*)klass;
75         gstobject_class = (GstObjectClass*) klass;
76         
77         gobject_class->get_property = gst_dpsmooth_get_property;
78         gobject_class->set_property = gst_dpsmooth_set_property;
79         
80         g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_UPDATE_PERIOD,
81                 g_param_spec_int64("update_period", 
82                                    "Update Period (nanoseconds)", 
83                                    "Number of nanoseconds between updates",
84                                    0LL, G_MAXINT64, 2000000LL, G_PARAM_READWRITE));
85                                    
86         g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_SLOPE_TIME,
87                 g_param_spec_int64("slope_time", 
88                                    "Slope Time (nanoseconds)", 
89                                    "The time period to define slope_delta by",
90                                    0LL, G_MAXINT64, 10000000LL, G_PARAM_READWRITE));
91                                    
92         g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_SLOPE_DELTA_FLOAT,
93                 g_param_spec_float("slope_delta_float", 
94                                    "Slope Delta float", 
95                                    "The amount a float value can change for a given slope_time",
96                                    0.0F, G_MAXFLOAT, 0.2F, G_PARAM_READWRITE));
97         
98         /*gstobject_class->save_thyself = gst_dparam_save_thyself; */
99
100 }
101
102 static void
103 gst_dpsmooth_init (GstDParamSmooth *dpsmooth)
104 {
105         g_return_if_fail (dpsmooth != NULL);
106 }
107
108 /**
109  * gst_dpsmooth_new:
110  * @type: the type that this dparam will store
111  *
112  * Returns: a new instance of GstDParam
113  */
114 GstDParam* 
115 gst_dpsmooth_new (GType type)
116 {
117         GstDParam *dparam;
118         GstDParamSmooth *dpsmooth;
119         
120         dpsmooth =g_object_new (gst_dpsmooth_get_type (), NULL);
121         dparam = GST_DPARAM(dpsmooth);
122         
123         GST_DPARAM_TYPE(dparam) = type;
124
125         switch (type){
126                 case G_TYPE_FLOAT: {
127                         dparam->do_update_func = gst_dpsmooth_do_update_float;
128                         g_signal_connect (G_OBJECT (dpsmooth), "value_changed", G_CALLBACK (gst_dpsmooth_value_changed_float), NULL);
129                         break;
130                 }
131                 default:
132                         /* we don't support this type here */
133                         dparam->do_update_func = gst_dparam_do_update_default;
134                         break;
135         }
136         return dparam;
137 }
138
139 static void 
140 gst_dpsmooth_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
141 {
142         GstDParam *dparam;
143         GstDParamSmooth *dpsmooth;
144
145         g_return_if_fail(GST_IS_DPSMOOTH(object));
146         
147         dpsmooth = GST_DPSMOOTH(object);
148         dparam = GST_DPARAM(object);
149         
150         GST_DPARAM_LOCK(dparam);
151
152         switch (prop_id) {
153                 case ARG_UPDATE_PERIOD:
154                         dpsmooth->update_period = g_value_get_int64 (value);
155                         GST_DPARAM_READY_FOR_UPDATE(dparam) = TRUE;
156                         break;
157
158                 case ARG_SLOPE_TIME:
159                         dpsmooth->slope_time = g_value_get_int64 (value);
160                         GST_DEBUG(GST_CAT_PARAMS, "dpsmooth->slope_time:%lld",dpsmooth->slope_time);
161                         GST_DPARAM_READY_FOR_UPDATE(dparam) = TRUE;
162                         break;
163
164                 case ARG_SLOPE_DELTA_FLOAT:
165                         dpsmooth->slope_delta_float = g_value_get_float (value);
166                         GST_DPARAM_READY_FOR_UPDATE(dparam) = TRUE;
167                         break;
168                         
169                 default:
170                         break;
171         }
172         GST_DPARAM_UNLOCK(dparam);
173 }
174
175 static void 
176 gst_dpsmooth_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) 
177 {
178         GstDParam *dparam;
179         GstDParamSmooth *dpsmooth;
180
181         g_return_if_fail(GST_IS_DPSMOOTH(object));
182         
183         dpsmooth = GST_DPSMOOTH(object);
184         dparam = GST_DPARAM(object);
185         
186         switch (prop_id) {
187                 case ARG_UPDATE_PERIOD:
188                         g_value_set_int64(value, dpsmooth->update_period);
189                         break;
190                 case ARG_SLOPE_TIME:
191                         g_value_set_int64(value, dpsmooth->slope_time);
192                         break;
193                 case ARG_SLOPE_DELTA_FLOAT:
194                         g_value_set_float (value, dpsmooth->slope_delta_float);
195                         break;
196                 default:
197                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198                         break;
199         }
200 }
201
202 static void 
203 gst_dpsmooth_value_changed_float (GstDParam *dparam)
204 {
205         GstDParamSmooth *dpsmooth;
206         gfloat time_ratio;
207
208         g_return_if_fail(GST_IS_DPSMOOTH(dparam));
209         dpsmooth = GST_DPSMOOTH(dparam);
210
211         if (GST_DPARAM_IS_LOG(dparam)){
212                 dparam->value_float = log(dparam->value_float);
213         }
214         dpsmooth->start_float = dpsmooth->current_float;
215         dpsmooth->diff_float = dparam->value_float - dpsmooth->start_float;
216
217         time_ratio = ABS(dpsmooth->diff_float) / dpsmooth->slope_delta_float;
218         dpsmooth->duration_interp = (gint64)(time_ratio * (gfloat)dpsmooth->slope_time);
219
220         dpsmooth->need_interp_times = TRUE;
221
222         GST_DEBUG(GST_CAT_PARAMS, "%f to %f ratio:%f duration:%lld\n", 
223                   dpsmooth->start_float, dparam->value_float, time_ratio, dpsmooth->duration_interp);
224 }
225
226 static void
227 gst_dpsmooth_do_update_float (GstDParam *dparam, gint64 timestamp, GValue *value, GstDParamUpdateInfo update_info)
228 {
229         gfloat time_ratio;
230         GstDParamSmooth *dpsmooth = GST_DPSMOOTH(dparam);
231
232         GST_DPARAM_LOCK(dparam);
233
234         if (dpsmooth->need_interp_times){
235                 dpsmooth->start_interp = timestamp;
236                 dpsmooth->end_interp = timestamp + dpsmooth->duration_interp;
237                 dpsmooth->need_interp_times = FALSE;
238         }
239
240         if ((update_info == GST_DPARAM_UPDATE_FIRST) || (timestamp >= dpsmooth->end_interp)){
241                 if (GST_DPARAM_IS_LOG(dparam)){
242                         g_value_set_float(value, exp(dparam->value_float)); 
243                 }
244                 else {
245                         g_value_set_float(value, dparam->value_float); 
246                 }
247                 dpsmooth->current_float = dparam->value_float;
248                 
249                 GST_DEBUG(GST_CAT_PARAMS, "interp finished at %lld", timestamp); 
250
251                 GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam) = timestamp;  
252                 GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam) = timestamp;
253                 
254                 GST_DPARAM_READY_FOR_UPDATE(dparam) = FALSE;
255                 GST_DPARAM_UNLOCK(dparam);
256                 return;
257         }
258
259         if (timestamp <= dpsmooth->start_interp){
260                 if (GST_DPARAM_IS_LOG(dparam)){
261                         g_value_set_float(value, exp(dpsmooth->start_float)); 
262                 }
263                 else {
264                         g_value_set_float(value, dpsmooth->start_float); 
265                 }
266                 GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam) = timestamp;  
267                 GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam) = dpsmooth->start_interp + dpsmooth->update_period; 
268                 
269                 GST_DEBUG(GST_CAT_PARAMS, "interp started at %lld", timestamp); 
270
271                 GST_DPARAM_UNLOCK(dparam);
272                 return;
273                 
274         }
275
276         time_ratio = (gfloat)(timestamp - dpsmooth->start_interp) / (gfloat)dpsmooth->duration_interp;
277
278         GST_DEBUG(GST_CAT_PARAMS, "start:%lld current:%lld end:%lld ratio%f", dpsmooth->start_interp, timestamp, dpsmooth->end_interp, time_ratio); 
279         GST_DEBUG(GST_CAT_PARAMS, "pre  start:%f current:%f target:%f", dpsmooth->start_float, dpsmooth->current_float, dparam->value_float);
280                                    
281         dpsmooth->current_float = dpsmooth->start_float + (dpsmooth->diff_float * time_ratio);
282
283         GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam) = timestamp + dpsmooth->update_period; 
284         if (GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam) > dpsmooth->end_interp){
285                 GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam) = dpsmooth->end_interp;        
286         }
287
288         GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam) = timestamp;
289
290         if (GST_DPARAM_IS_LOG(dparam)){
291                 g_value_set_float(value, exp(dpsmooth->current_float)); 
292         }
293         else {
294                 g_value_set_float(value, dpsmooth->current_float); 
295         }
296
297         GST_DEBUG(GST_CAT_PARAMS, "post start:%f current:%f target:%f", dpsmooth->start_float, dpsmooth->current_float, dparam->value_float);
298
299         GST_DPARAM_UNLOCK(dparam);
300 }
301