ntdll: Use list_empty() instead of list_count() == 0.
[wine/multimedia.git] / dlls / strmbase / qualitycontrol.c
blob8bcaaa998f5848bbf9b57f7bce6c7908267b108a
1 /*
2 * Quality Control Interfaces
4 * Copyright 2010 Maarten Lankhorst for CodeWeavers
6 * rendering qos functions based on, the original can be found at
7 * gstreamer/libs/gst/base/gstbasesink.c which has copyright notice:
9 * Copyright (C) 2005-2007 Wim Taymans <wim.taymans@gmail.com>
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.
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.
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
26 #define COBJMACROS
28 #include "dshow.h"
29 #include "wine/strmbase.h"
31 #include "uuids.h"
32 #include "wine/debug.h"
34 #include <assert.h>
36 WINE_DEFAULT_DEBUG_CHANNEL(strmbase_qc);
38 void QualityControlImpl_init(QualityControlImpl *This, IPin *input, IBaseFilter *self) {
39 This->input = input;
40 This->self = self;
41 This->tonotify = NULL;
42 This->clock = NULL;
45 HRESULT WINAPI QualityControlImpl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv) {
46 QualityControlImpl *This = (QualityControlImpl*)iface;
47 return IUnknown_QueryInterface(This->self, riid, ppv);
50 ULONG WINAPI QualityControlImpl_AddRef(IQualityControl *iface) {
51 QualityControlImpl *This = (QualityControlImpl*)iface;
52 return IUnknown_AddRef(This->self);
55 ULONG WINAPI QualityControlImpl_Release(IQualityControl *iface) {
56 QualityControlImpl *This = (QualityControlImpl*)iface;
57 return IUnknown_Release(This->self);
60 HRESULT WINAPI QualityControlImpl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm) {
61 HRESULT hr = S_FALSE;
62 QualityControlImpl *This = (QualityControlImpl*)iface;
63 if (This->tonotify)
64 return IQualityControl_Notify(This->tonotify, This->self, qm);
65 if (This->input) {
66 IPin *to = NULL;
67 IPin_ConnectedTo(This->input, &to);
68 if (to) {
69 IQualityControl *qc = NULL;
70 IUnknown_QueryInterface(to, &IID_IQualityControl, (void**)&qc);
71 if (qc) {
72 hr = IQualityControl_Notify(qc, This->self, qm);
73 IQualityControl_Release(qc);
75 IPin_Release(to);
78 return hr;
81 HRESULT WINAPI QualityControlImpl_SetSink(IQualityControl *iface, IQualityControl *tonotify) {
82 QualityControlImpl *This = (QualityControlImpl*)iface;
83 This->tonotify = tonotify;
84 return S_OK;
87 /* Macros copied from gstreamer, weighted average between old average and new ones */
88 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
90 /* generic running average, this has a neutral window size */
91 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
93 /* the windows for these running averages are experimentally obtained.
94 * possitive values get averaged more while negative values use a small
95 * window so we can react faster to badness. */
96 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
97 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
99 void QualityControlRender_Start(QualityControlImpl *This, REFERENCE_TIME tStart) {
100 This->avg_render = This->last_in_time = This->last_left = This->avg_duration = This->avg_pt = -1;
101 This->clockstart = tStart;
102 This->avg_rate = -1.0;
103 This->rendered = This->dropped = This->is_dropped = 0;
104 This->qos_handled = 1; /* Lie that will be corrected on first adjustment */
108 void QualityControlRender_SetClock(QualityControlImpl *This, IReferenceClock *clock) {
109 This->clock = clock;
112 static BOOL QualityControlRender_IsLate(QualityControlImpl *This, REFERENCE_TIME jitter,
113 REFERENCE_TIME start, REFERENCE_TIME stop)
115 REFERENCE_TIME max_lateness = 200000;
117 /* we can add a valid stop time */
118 if (stop >= start)
119 max_lateness += stop;
120 else
121 max_lateness += start;
123 /* if the jitter bigger than duration and lateness we are too late */
124 if (start + jitter > max_lateness) {
125 WARN("buffer is too late %i > %i\n", (int)((start + jitter)/10000), (int)(max_lateness/10000));
126 /* !!emergency!!, if we did not receive anything valid for more than a
127 * second, render it anyway so the user sees something */
128 if (This->last_in_time < 0 ||
129 start - This->last_in_time < 10000000)
130 return TRUE;
131 FIXME("A lot of buffers are being dropped.\n");
132 FIXME("There may be a timestamping problem, or this computer is too slow.\n");
134 This->last_in_time = start;
135 return FALSE;
138 HRESULT QualityControlRender_WaitFor(QualityControlImpl *This, IMediaSample *sample, HANDLE ev) {
139 REFERENCE_TIME start = -1, stop = -1, jitter = 0;
140 This->current_rstart = This->current_rstop = -1;
141 This->current_jitter = 0;
142 if (!This->clock || FAILED(IMediaSample_GetTime(sample, &start, &stop)))
143 return S_OK;
145 if (start >= 0) {
146 REFERENCE_TIME now;
147 IReferenceClock_GetTime(This->clock, &now);
148 now -= This->clockstart;
150 jitter = now - start;
151 if (jitter <= -10000) {
152 DWORD_PTR cookie;
153 IReferenceClock_AdviseTime(This->clock, This->clockstart, start, (HEVENT)ev, &cookie);
154 WaitForSingleObject(ev, INFINITE);
155 IReferenceClock_Unadvise(This->clock, cookie);
158 else
159 start = stop = -1;
160 This->current_rstart = start;
161 This->current_rstop = stop > start ? stop : start;
162 This->current_jitter = jitter;
163 This->is_dropped = QualityControlRender_IsLate(This, jitter, start, stop);
164 TRACE("Dropped: %i %i %i %i\n", This->is_dropped, (int)(start/10000), (int)(stop/10000), (int)(jitter / 10000));
165 if (This->is_dropped) {
166 This->dropped++;
167 if (!This->qos_handled)
168 return S_FALSE;
169 } else
170 This->rendered++;
171 return S_OK;
174 void QualityControlRender_DoQOS(QualityControlImpl *priv)
176 REFERENCE_TIME start, stop, jitter, pt, entered, left, duration;
177 double rate;
179 if (!priv->clock || priv->current_rstart < 0)
180 return;
182 start = priv->current_rstart;
183 stop = priv->current_rstop;
184 jitter = priv->current_jitter;
186 if (jitter < 0) {
187 /* this is the time the buffer entered the sink */
188 if (start < -jitter)
189 entered = 0;
190 else
191 entered = start + jitter;
192 left = start;
193 } else {
194 /* this is the time the buffer entered the sink */
195 entered = start + jitter;
196 /* this is the time the buffer left the sink */
197 left = start + jitter;
200 /* calculate duration of the buffer */
201 if (stop >= start)
202 duration = stop - start;
203 else
204 duration = 0;
206 /* if we have the time when the last buffer left us, calculate
207 * processing time */
208 if (priv->last_left >= 0) {
209 if (entered > priv->last_left) {
210 pt = entered - priv->last_left;
211 } else {
212 pt = 0;
214 } else {
215 pt = priv->avg_pt;
218 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
219 TRACE("start: %u.%03u, entered %u.%03u, left %u.%03u, pt: %u.%03u, "
220 "duration %u.%03u, jitter %u.%03u\n", XTIME(start), XTIME(entered),
221 XTIME(left), XTIME(pt), XTIME(duration), XTIME(jitter));
223 TRACE("avg_duration: %u.%03u, avg_pt: %u.%03u, avg_rate: %g\n",
224 XTIME(priv->avg_duration), XTIME(priv->avg_pt), priv->avg_rate);
225 #undef XTIME
227 /* collect running averages. for first observations, we copy the
228 * values */
229 if (priv->avg_duration < 0)
230 priv->avg_duration = duration;
231 else
232 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
234 if (priv->avg_pt < 0)
235 priv->avg_pt = pt;
236 else
237 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
239 if (priv->avg_duration != 0)
240 rate =
241 (double)priv->avg_pt /
242 (double)priv->avg_duration;
243 else
244 rate = 0.0;
246 if (priv->last_left >= 0) {
247 if (priv->is_dropped || priv->avg_rate < 0.0) {
248 priv->avg_rate = rate;
249 } else {
250 if (rate > 1.0)
251 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
252 else
253 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
257 if (priv->avg_rate >= 0.0) {
258 HRESULT hr;
259 Quality q;
260 /* if we have a valid rate, start sending QoS messages */
261 if (priv->current_jitter < 0) {
262 /* make sure we never go below 0 when adding the jitter to the
263 * timestamp. */
264 if (priv->current_rstart < -priv->current_jitter)
265 priv->current_jitter = -priv->current_rstart;
267 else
268 priv->current_jitter += (priv->current_rstop - priv->current_rstart);
269 q.Type = (jitter > 0 ? Famine : Flood);
270 q.Proportion = (LONG)(1000. / priv->avg_rate);
271 if (q.Proportion < 200)
272 q.Proportion = 200;
273 else if (q.Proportion > 5000)
274 q.Proportion = 5000;
275 q.Late = priv->current_jitter;
276 q.TimeStamp = priv->current_rstart;
277 TRACE("Late: %i from %i, rate: %g\n", (int)(q.Late/10000), (int)(q.TimeStamp/10000), 1./priv->avg_rate);
278 hr = IQualityControl_Notify((IQualityControl *)priv, priv->self, q);
279 priv->qos_handled = hr == S_OK;
282 /* record when this buffer will leave us */
283 priv->last_left = left;
287 void QualityControlRender_BeginRender(QualityControlImpl *This) {
288 This->start = -1;
289 if (!This->clock)
290 return;
291 IReferenceClock_GetTime(This->clock, &This->start);
294 void QualityControlRender_EndRender(QualityControlImpl *This) {
295 REFERENCE_TIME elapsed;
296 if (!This->clock || This->start < 0 || FAILED(IReferenceClock_GetTime(This->clock, &This->stop)))
297 return;
299 elapsed = This->start - This->stop;
300 if (elapsed < 0)
301 return;
302 if (This->avg_render < 0)
303 This->avg_render = elapsed;
304 else
305 This->avg_render = UPDATE_RUNNING_AVG (This->avg_render, elapsed);