include: Assorted spelling fixes.
[wine] / dlls / 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 #define COBJMACROS
27
28 #include "dshow.h"
29 #include "wine/strmbase.h"
30 #include "strmbase_private.h"
31
32 #include "uuids.h"
33 #include "wine/debug.h"
34
35 #include <assert.h>
36
37 WINE_DEFAULT_DEBUG_CHANNEL(strmbase_qc);
38
39 HRESULT QualityControlImpl_Create(IPin *input, IBaseFilter *self, QualityControlImpl **ppv) {
40     QualityControlImpl *This;
41     *ppv = HeapAlloc(GetProcessHeap(),0,sizeof(QualityControlImpl));
42     if (!*ppv)
43         return E_OUTOFMEMORY;
44     This = *ppv;
45     This->input = input;
46     This->self = self;
47     This->tonotify = NULL;
48     This->clock = NULL;
49     return S_OK;
50 }
51
52 HRESULT QualityControlImpl_Destroy(QualityControlImpl *This)
53 {
54     return HeapFree(GetProcessHeap(),0,This);
55 }
56
57 HRESULT WINAPI QualityControlImpl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv) {
58     QualityControlImpl *This = (QualityControlImpl*)iface;
59     return IBaseFilter_QueryInterface(This->self, riid, ppv);
60 }
61
62 ULONG WINAPI QualityControlImpl_AddRef(IQualityControl *iface) {
63     QualityControlImpl *This = (QualityControlImpl*)iface;
64     return IBaseFilter_AddRef(This->self);
65 }
66
67 ULONG WINAPI QualityControlImpl_Release(IQualityControl *iface) {
68     QualityControlImpl *This = (QualityControlImpl*)iface;
69     return IBaseFilter_Release(This->self);
70 }
71
72 HRESULT WINAPI QualityControlImpl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm) {
73     HRESULT hr = S_FALSE;
74     QualityControlImpl *This = (QualityControlImpl*)iface;
75     if (This->tonotify)
76         return IQualityControl_Notify(This->tonotify, This->self, qm);
77     if (This->input) {
78         IPin *to = NULL;
79         IPin_ConnectedTo(This->input, &to);
80         if (to) {
81             IQualityControl *qc = NULL;
82             IPin_QueryInterface(to, &IID_IQualityControl, (void**)&qc);
83             if (qc) {
84                 hr = IQualityControl_Notify(qc, This->self, qm);
85                 IQualityControl_Release(qc);
86             }
87             IPin_Release(to);
88         }
89     }
90     return hr;
91 }
92
93 HRESULT WINAPI QualityControlImpl_SetSink(IQualityControl *iface, IQualityControl *tonotify) {
94     QualityControlImpl *This = (QualityControlImpl*)iface;
95     This->tonotify = tonotify;
96     return S_OK;
97 }
98
99 /* Macros copied from gstreamer, weighted average between old average and new ones */
100 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
101
102 /* generic running average, this has a neutral window size */
103 #define UPDATE_RUNNING_AVG(avg,val)   DO_RUNNING_AVG(avg,val,8)
104
105 /* the windows for these running averages are experimentally obtained.
106  * possitive values get averaged more while negative values use a small
107  * window so we can react faster to badness. */
108 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
109 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
110
111 void QualityControlRender_Start(QualityControlImpl *This, REFERENCE_TIME tStart) {
112     This->avg_render = This->last_in_time = This->last_left = This->avg_duration = This->avg_pt = -1;
113     This->clockstart = tStart;
114     This->avg_rate = -1.0;
115     This->rendered = This->dropped = This->is_dropped = 0;
116     This->qos_handled = 1; /* Lie that will be corrected on first adjustment */
117 }
118
119
120 void QualityControlRender_SetClock(QualityControlImpl *This, IReferenceClock *clock) {
121     This->clock = clock;
122 }
123
124 static BOOL QualityControlRender_IsLate(QualityControlImpl *This, REFERENCE_TIME jitter,
125                                         REFERENCE_TIME start, REFERENCE_TIME stop)
126 {
127     REFERENCE_TIME max_lateness = 200000;
128
129     /* we can add a valid stop time */
130     if (stop >= start)
131         max_lateness += stop;
132     else
133         max_lateness += start;
134
135     /* if the jitter bigger than duration and lateness we are too late */
136     if (start + jitter > max_lateness) {
137         WARN("buffer is too late %i > %i\n", (int)((start + jitter)/10000),  (int)(max_lateness/10000));
138         /* !!emergency!!, if we did not receive anything valid for more than a
139          * second, render it anyway so the user sees something */
140         if (This->last_in_time < 0 ||
141             start - This->last_in_time < 10000000)
142             return TRUE;
143         FIXME("A lot of buffers are being dropped.\n");
144         FIXME("There may be a timestamping problem, or this computer is too slow.\n");
145     }
146     This->last_in_time = start;
147     return FALSE;
148 }
149
150 HRESULT QualityControlRender_WaitFor(QualityControlImpl *This, IMediaSample *sample, HANDLE ev) {
151     REFERENCE_TIME start = -1, stop = -1, jitter = 0;
152     This->current_rstart = This->current_rstop = -1;
153     This->current_jitter = 0;
154     if (!This->clock || FAILED(IMediaSample_GetTime(sample, &start, &stop)))
155         return S_OK;
156
157     if (start >= 0) {
158         REFERENCE_TIME now;
159         IReferenceClock_GetTime(This->clock, &now);
160         now -= This->clockstart;
161
162         jitter = now - start;
163         if (jitter <= -10000) {
164             DWORD_PTR cookie;
165             IReferenceClock_AdviseTime(This->clock, This->clockstart, start, (HEVENT)ev, &cookie);
166             WaitForSingleObject(ev, INFINITE);
167             IReferenceClock_Unadvise(This->clock, cookie);
168         }
169     }
170     else
171         start = stop = -1;
172     This->current_rstart = start;
173     This->current_rstop = stop > start ? stop : start;
174     This->current_jitter = jitter;
175     This->is_dropped = QualityControlRender_IsLate(This, jitter, start, stop);
176     TRACE("Dropped: %i %i %i %i\n", This->is_dropped, (int)(start/10000), (int)(stop/10000), (int)(jitter / 10000));
177     if (This->is_dropped) {
178         This->dropped++;
179         if (!This->qos_handled)
180             return S_FALSE;
181     } else
182         This->rendered++;
183     return S_OK;
184 }
185
186 void QualityControlRender_DoQOS(QualityControlImpl *priv)
187 {
188     REFERENCE_TIME start, stop, jitter, pt, entered, left, duration;
189     double rate;
190
191     if (!priv->clock || priv->current_rstart < 0)
192         return;
193
194     start = priv->current_rstart;
195     stop = priv->current_rstop;
196     jitter = priv->current_jitter;
197
198     if (jitter < 0) {
199         /* this is the time the buffer entered the sink */
200         if (start < -jitter)
201             entered = 0;
202         else
203             entered = start + jitter;
204         left = start;
205     } else {
206         /* this is the time the buffer entered the sink */
207         entered = start + jitter;
208         /* this is the time the buffer left the sink */
209         left = start + jitter;
210     }
211
212     /* calculate duration of the buffer */
213     if (stop >= start)
214         duration = stop - start;
215     else
216         duration = 0;
217
218     /* if we have the time when the last buffer left us, calculate
219      * processing time */
220     if (priv->last_left >= 0) {
221         if (entered > priv->last_left) {
222             pt = entered - priv->last_left;
223         } else {
224             pt = 0;
225         }
226     } else {
227         pt = priv->avg_pt;
228     }
229
230 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
231     TRACE("start: %u.%03u, entered %u.%03u, left %u.%03u, pt: %u.%03u, "
232           "duration %u.%03u, jitter %u.%03u\n", XTIME(start), XTIME(entered),
233           XTIME(left), XTIME(pt), XTIME(duration), XTIME(jitter));
234
235     TRACE("avg_duration: %u.%03u, avg_pt: %u.%03u, avg_rate: %g\n",
236       XTIME(priv->avg_duration), XTIME(priv->avg_pt), priv->avg_rate);
237 #undef XTIME
238
239     /* collect running averages. for first observations, we copy the
240     * values */
241     if (priv->avg_duration < 0)
242         priv->avg_duration = duration;
243     else
244         priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
245
246     if (priv->avg_pt < 0)
247         priv->avg_pt = pt;
248     else
249         priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
250
251     if (priv->avg_duration != 0)
252         rate =
253             (double)priv->avg_pt /
254             (double)priv->avg_duration;
255     else
256         rate = 0.0;
257
258     if (priv->last_left >= 0) {
259         if (priv->is_dropped || priv->avg_rate < 0.0) {
260             priv->avg_rate = rate;
261         } else {
262             if (rate > 1.0)
263                 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
264             else
265                 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
266         }
267     }
268
269     if (priv->avg_rate >= 0.0) {
270         HRESULT hr;
271         Quality q;
272         /* if we have a valid rate, start sending QoS messages */
273         if (priv->current_jitter < 0) {
274             /* make sure we never go below 0 when adding the jitter to the
275              * timestamp. */
276             if (priv->current_rstart < -priv->current_jitter)
277                 priv->current_jitter = -priv->current_rstart;
278         }
279         else
280             priv->current_jitter += (priv->current_rstop - priv->current_rstart);
281         q.Type = (jitter > 0 ? Famine : Flood);
282         q.Proportion = (LONG)(1000. / priv->avg_rate);
283         if (q.Proportion < 200)
284             q.Proportion = 200;
285         else if (q.Proportion > 5000)
286             q.Proportion = 5000;
287         q.Late = priv->current_jitter;
288         q.TimeStamp = priv->current_rstart;
289         TRACE("Late: %i from %i, rate: %g\n", (int)(q.Late/10000), (int)(q.TimeStamp/10000), 1./priv->avg_rate);
290         hr = IQualityControl_Notify((IQualityControl *)priv, priv->self, q);
291         priv->qos_handled = hr == S_OK;
292     }
293
294     /* record when this buffer will leave us */
295     priv->last_left = left;
296 }
297
298
299 void QualityControlRender_BeginRender(QualityControlImpl *This) {
300     This->start = -1;
301     if (!This->clock)
302         return;
303     IReferenceClock_GetTime(This->clock, &This->start);
304 }
305
306 void QualityControlRender_EndRender(QualityControlImpl *This) {
307     REFERENCE_TIME elapsed;
308     if (!This->clock || This->start < 0 || FAILED(IReferenceClock_GetTime(This->clock, &This->stop)))
309         return;
310
311     elapsed = This->start - This->stop;
312     if (elapsed < 0)
313         return;
314     if (This->avg_render < 0)
315         This->avg_render = elapsed;
316     else
317         This->avg_render = UPDATE_RUNNING_AVG (This->avg_render, elapsed);
318 }