* Sync up to trunk HEAD (r62975).
[reactos.git] / lib / 3rdparty / strmbase / qualitycontrol.c
1 /*
2 * Quality Control Interfaces
3 *
4 * Copyright 2010 Maarten Lankhorst for CodeWeavers
5 *
6 * rendering qos functions based on, the original can be found at
7 * gstreamer/libs/gst/base/gstbasesink.c which has copyright notice:
8 *
9 * Copyright (C) 2005-2007 Wim Taymans <wim.taymans@gmail.com>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26 #include "strmbase_private.h"
27
28 HRESULT QualityControlImpl_Create(IPin *input, IBaseFilter *self, QualityControlImpl **ppv) {
29 QualityControlImpl *This;
30 *ppv = HeapAlloc(GetProcessHeap(),0,sizeof(QualityControlImpl));
31 if (!*ppv)
32 return E_OUTOFMEMORY;
33 This = *ppv;
34 This->input = input;
35 This->self = self;
36 This->tonotify = NULL;
37 This->clock = NULL;
38 return S_OK;
39 }
40
41 HRESULT QualityControlImpl_Destroy(QualityControlImpl *This)
42 {
43 return HeapFree(GetProcessHeap(),0,This);
44 }
45
46 HRESULT WINAPI QualityControlImpl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv) {
47 QualityControlImpl *This = (QualityControlImpl*)iface;
48 return IBaseFilter_QueryInterface(This->self, riid, ppv);
49 }
50
51 ULONG WINAPI QualityControlImpl_AddRef(IQualityControl *iface) {
52 QualityControlImpl *This = (QualityControlImpl*)iface;
53 return IBaseFilter_AddRef(This->self);
54 }
55
56 ULONG WINAPI QualityControlImpl_Release(IQualityControl *iface) {
57 QualityControlImpl *This = (QualityControlImpl*)iface;
58 return IBaseFilter_Release(This->self);
59 }
60
61 HRESULT WINAPI QualityControlImpl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm) {
62 HRESULT hr = S_FALSE;
63 QualityControlImpl *This = (QualityControlImpl*)iface;
64 if (This->tonotify)
65 return IQualityControl_Notify(This->tonotify, This->self, qm);
66 if (This->input) {
67 IPin *to = NULL;
68 IPin_ConnectedTo(This->input, &to);
69 if (to) {
70 IQualityControl *qc = NULL;
71 IPin_QueryInterface(to, &IID_IQualityControl, (void**)&qc);
72 if (qc) {
73 hr = IQualityControl_Notify(qc, This->self, qm);
74 IQualityControl_Release(qc);
75 }
76 IPin_Release(to);
77 }
78 }
79 return hr;
80 }
81
82 HRESULT WINAPI QualityControlImpl_SetSink(IQualityControl *iface, IQualityControl *tonotify) {
83 QualityControlImpl *This = (QualityControlImpl*)iface;
84 This->tonotify = tonotify;
85 return S_OK;
86 }
87
88 /* Macros copied from gstreamer, weighted average between old average and new ones */
89 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
90
91 /* generic running average, this has a neutral window size */
92 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
93
94 /* the windows for these running averages are experimentally obtained.
95 * possitive values get averaged more while negative values use a small
96 * window so we can react faster to badness. */
97 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
98 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
99
100 void QualityControlRender_Start(QualityControlImpl *This, REFERENCE_TIME tStart) {
101 This->avg_render = This->last_in_time = This->last_left = This->avg_duration = This->avg_pt = -1;
102 This->clockstart = tStart;
103 This->avg_rate = -1.0;
104 This->rendered = This->dropped = 0;
105 This->is_dropped = FALSE;
106 This->qos_handled = TRUE; /* Lie that will be corrected on first adjustment */
107 }
108
109
110 void QualityControlRender_SetClock(QualityControlImpl *This, IReferenceClock *clock) {
111 This->clock = clock;
112 }
113
114 static BOOL QualityControlRender_IsLate(QualityControlImpl *This, REFERENCE_TIME jitter,
115 REFERENCE_TIME start, REFERENCE_TIME stop)
116 {
117 REFERENCE_TIME max_lateness = 200000;
118
119 /* we can add a valid stop time */
120 if (stop >= start)
121 max_lateness += stop;
122 else
123 max_lateness += start;
124
125 /* if the jitter bigger than duration and lateness we are too late */
126 if (start + jitter > max_lateness) {
127 WARN("buffer is too late %i > %i\n", (int)((start + jitter)/10000), (int)(max_lateness/10000));
128 /* !!emergency!!, if we did not receive anything valid for more than a
129 * second, render it anyway so the user sees something */
130 if (This->last_in_time < 0 ||
131 start - This->last_in_time < 10000000)
132 return TRUE;
133 FIXME("A lot of buffers are being dropped.\n");
134 FIXME("There may be a timestamping problem, or this computer is too slow.\n");
135 }
136 This->last_in_time = start;
137 return FALSE;
138 }
139
140 HRESULT QualityControlRender_WaitFor(QualityControlImpl *This, IMediaSample *sample, HANDLE ev) {
141 REFERENCE_TIME start = -1, stop = -1, jitter = 0;
142 This->current_rstart = This->current_rstop = -1;
143 This->current_jitter = 0;
144 if (!This->clock || FAILED(IMediaSample_GetTime(sample, &start, &stop)))
145 return S_OK;
146
147 if (start >= 0) {
148 REFERENCE_TIME now;
149 IReferenceClock_GetTime(This->clock, &now);
150 now -= This->clockstart;
151
152 jitter = now - start;
153 if (jitter <= -10000) {
154 DWORD_PTR cookie;
155 IReferenceClock_AdviseTime(This->clock, This->clockstart, start, (HEVENT)ev, &cookie);
156 WaitForSingleObject(ev, INFINITE);
157 IReferenceClock_Unadvise(This->clock, cookie);
158 }
159 }
160 else
161 start = stop = -1;
162 This->current_rstart = start;
163 This->current_rstop = stop > start ? stop : start;
164 This->current_jitter = jitter;
165 This->is_dropped = QualityControlRender_IsLate(This, jitter, start, stop);
166 TRACE("Dropped: %i %i %i %i\n", This->is_dropped, (int)(start/10000), (int)(stop/10000), (int)(jitter / 10000));
167 if (This->is_dropped) {
168 This->dropped++;
169 if (!This->qos_handled)
170 return S_FALSE;
171 } else
172 This->rendered++;
173 return S_OK;
174 }
175
176 void QualityControlRender_DoQOS(QualityControlImpl *priv)
177 {
178 REFERENCE_TIME start, stop, jitter, pt, entered, left, duration;
179 double rate;
180
181 if (!priv->clock || priv->current_rstart < 0)
182 return;
183
184 start = priv->current_rstart;
185 stop = priv->current_rstop;
186 jitter = priv->current_jitter;
187
188 if (jitter < 0) {
189 /* this is the time the buffer entered the sink */
190 if (start < -jitter)
191 entered = 0;
192 else
193 entered = start + jitter;
194 left = start;
195 } else {
196 /* this is the time the buffer entered the sink */
197 entered = start + jitter;
198 /* this is the time the buffer left the sink */
199 left = start + jitter;
200 }
201
202 /* calculate duration of the buffer */
203 if (stop >= start)
204 duration = stop - start;
205 else
206 duration = 0;
207
208 /* if we have the time when the last buffer left us, calculate
209 * processing time */
210 if (priv->last_left >= 0) {
211 if (entered > priv->last_left) {
212 pt = entered - priv->last_left;
213 } else {
214 pt = 0;
215 }
216 } else {
217 pt = priv->avg_pt;
218 }
219
220 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
221 TRACE("start: %u.%03u, entered %u.%03u, left %u.%03u, pt: %u.%03u, "
222 "duration %u.%03u, jitter %u.%03u\n", XTIME(start), XTIME(entered),
223 XTIME(left), XTIME(pt), XTIME(duration), XTIME(jitter));
224
225 TRACE("avg_duration: %u.%03u, avg_pt: %u.%03u, avg_rate: %g\n",
226 XTIME(priv->avg_duration), XTIME(priv->avg_pt), priv->avg_rate);
227 #undef XTIME
228
229 /* collect running averages. for first observations, we copy the
230 * values */
231 if (priv->avg_duration < 0)
232 priv->avg_duration = duration;
233 else
234 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
235
236 if (priv->avg_pt < 0)
237 priv->avg_pt = pt;
238 else
239 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
240
241 if (priv->avg_duration != 0)
242 rate =
243 (double)priv->avg_pt /
244 (double)priv->avg_duration;
245 else
246 rate = 0.0;
247
248 if (priv->last_left >= 0) {
249 if (priv->is_dropped || priv->avg_rate < 0.0) {
250 priv->avg_rate = rate;
251 } else {
252 if (rate > 1.0)
253 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
254 else
255 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
256 }
257 }
258
259 if (priv->avg_rate >= 0.0) {
260 HRESULT hr;
261 Quality q;
262 /* if we have a valid rate, start sending QoS messages */
263 if (priv->current_jitter < 0) {
264 /* make sure we never go below 0 when adding the jitter to the
265 * timestamp. */
266 if (priv->current_rstart < -priv->current_jitter)
267 priv->current_jitter = -priv->current_rstart;
268 }
269 else
270 priv->current_jitter += (priv->current_rstop - priv->current_rstart);
271 q.Type = (jitter > 0 ? Famine : Flood);
272 q.Proportion = (LONG)(1000. / priv->avg_rate);
273 if (q.Proportion < 200)
274 q.Proportion = 200;
275 else if (q.Proportion > 5000)
276 q.Proportion = 5000;
277 q.Late = priv->current_jitter;
278 q.TimeStamp = priv->current_rstart;
279 TRACE("Late: %i from %i, rate: %g\n", (int)(q.Late/10000), (int)(q.TimeStamp/10000), 1./priv->avg_rate);
280 hr = IQualityControl_Notify((IQualityControl *)priv, priv->self, q);
281 priv->qos_handled = hr == S_OK;
282 }
283
284 /* record when this buffer will leave us */
285 priv->last_left = left;
286 }
287
288
289 void QualityControlRender_BeginRender(QualityControlImpl *This) {
290 This->start = -1;
291 if (!This->clock)
292 return;
293 IReferenceClock_GetTime(This->clock, &This->start);
294 }
295
296 void QualityControlRender_EndRender(QualityControlImpl *This) {
297 REFERENCE_TIME elapsed;
298 if (!This->clock || This->start < 0 || FAILED(IReferenceClock_GetTime(This->clock, &This->stop)))
299 return;
300
301 elapsed = This->start - This->stop;
302 if (elapsed < 0)
303 return;
304 if (This->avg_render < 0)
305 This->avg_render = elapsed;
306 else
307 This->avg_render = UPDATE_RUNNING_AVG (This->avg_render, elapsed);
308 }