include/msvcrt: Define more CPU control word flags.
[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
31 #include "uuids.h"
32 #include "wine/debug.h"
33
34 #include <assert.h>
35
36 WINE_DEFAULT_DEBUG_CHANNEL(strmbase_qc);
37
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;
43 }
44
45 HRESULT WINAPI QualityControlImpl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv) {
46     QualityControlImpl *This = (QualityControlImpl*)iface;
47     return IUnknown_QueryInterface(This->self, riid, ppv);
48 }
49
50 ULONG WINAPI QualityControlImpl_AddRef(IQualityControl *iface) {
51     QualityControlImpl *This = (QualityControlImpl*)iface;
52     return IUnknown_AddRef(This->self);
53 }
54
55 ULONG WINAPI QualityControlImpl_Release(IQualityControl *iface) {
56     QualityControlImpl *This = (QualityControlImpl*)iface;
57     return IUnknown_Release(This->self);
58 }
59
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);
74             }
75             IPin_Release(to);
76         }
77     }
78     return hr;
79 }
80
81 HRESULT WINAPI QualityControlImpl_SetSink(IQualityControl *iface, IQualityControl *tonotify) {
82     QualityControlImpl *This = (QualityControlImpl*)iface;
83     This->tonotify = tonotify;
84     return S_OK;
85 }
86
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))
89
90 /* generic running average, this has a neutral window size */
91 #define UPDATE_RUNNING_AVG(avg,val)   DO_RUNNING_AVG(avg,val,8)
92
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)
98
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 */
105 }
106
107
108 void QualityControlRender_SetClock(QualityControlImpl *This, IReferenceClock *clock) {
109     This->clock = clock;
110 }
111
112 static BOOL QualityControlRender_IsLate(QualityControlImpl *This, REFERENCE_TIME jitter,
113                                         REFERENCE_TIME start, REFERENCE_TIME stop)
114 {
115     REFERENCE_TIME max_lateness = 200000;
116
117     /* we can add a valid stop time */
118     if (stop >= start)
119         max_lateness += stop;
120     else
121         max_lateness += start;
122
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");
133     }
134     This->last_in_time = start;
135     return FALSE;
136 }
137
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;
144
145     if (start >= 0) {
146         REFERENCE_TIME now;
147         IReferenceClock_GetTime(This->clock, &now);
148         now -= This->clockstart;
149
150         jitter = now - start;
151 #if 0
152         /* AdviseTime is bugged, so don't use it at all */
153         if (jitter < -200000) {
154             DWORD_PTR cookie;
155             IReferenceClock_AdviseTime(This->clock, clockstart, start, ev, &cookie);
156             WaitForSingleObject(ev, INFINITE);
157             IReferenceClock_Unadvise(This->clock,  cookie);
158         } else
159 #endif
160         if (jitter < 0) {
161             TRACE("Sleeping for %i ms\n", (int)-jitter/10000);
162             WaitForSingleObject(ev, -jitter/10000);
163         }
164     }
165     else
166         start = stop = -1;
167     This->current_rstart = start;
168     This->current_rstop = stop > start ? stop : start;
169     This->current_jitter = jitter;
170     This->is_dropped = QualityControlRender_IsLate(This, jitter, start, stop);
171     TRACE("Dropped: %i %i %i %i\n", This->is_dropped, (int)(start/10000), (int)(stop/10000), (int)(jitter / 10000));
172     if (This->is_dropped) {
173         This->dropped++;
174         if (!This->qos_handled)
175             return S_FALSE;
176     } else
177         This->rendered++;
178     return S_OK;
179 }
180
181 void QualityControlRender_DoQOS(QualityControlImpl *priv)
182 {
183     REFERENCE_TIME start, stop, jitter, pt, entered, left, duration;
184     double rate;
185
186     if (!priv->clock)
187         return;
188
189     start = priv->current_rstart;
190     stop = priv->current_rstop;
191     jitter = priv->current_jitter;
192
193     if (jitter < 0) {
194         /* this is the time the buffer entered the sink */
195         if (start < -jitter)
196             entered = 0;
197         else
198             entered = start + jitter;
199         left = start;
200     } else {
201         /* this is the time the buffer entered the sink */
202         entered = start + jitter;
203         /* this is the time the buffer left the sink */
204         left = start + jitter;
205     }
206
207     /* calculate duration of the buffer */
208     if (stop >= start)
209         duration = stop - start;
210     else
211         duration = 0;
212
213     /* if we have the time when the last buffer left us, calculate
214      * processing time */
215     if (priv->last_left >= 0) {
216         if (entered > priv->last_left) {
217             pt = entered - priv->last_left;
218         } else {
219             pt = 0;
220         }
221     } else {
222         pt = priv->avg_pt;
223     }
224
225 #define XTIME(u) (int)(u/10000000), (int)((u / 10000)%1000)
226     TRACE("start: %u.%03u, entered %u.%03u, left %u.%03u, pt: %u.%03u, "
227           "duration %u.%03u, jitter %u.%03u\n", XTIME(start), XTIME(entered),
228           XTIME(left), XTIME(pt), XTIME(duration), XTIME(jitter));
229
230     TRACE("avg_duration: %u.%03u, avg_pt: %u.%03u, avg_rate: %g\n",
231       XTIME(priv->avg_duration), XTIME(priv->avg_pt), priv->avg_rate);
232 #undef XTIME
233
234     /* collect running averages. for first observations, we copy the
235     * values */
236     if (priv->avg_duration < 0)
237         priv->avg_duration = duration;
238     else
239         priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
240
241     if (priv->avg_pt < 0)
242         priv->avg_pt = pt;
243     else
244         priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
245
246     if (priv->avg_duration != 0)
247         rate =
248             (double)priv->avg_pt /
249             (double)priv->avg_duration;
250     else
251         rate = 0.0;
252
253     if (priv->last_left >= 0) {
254         if (priv->is_dropped || priv->avg_rate < 0.0) {
255             priv->avg_rate = rate;
256         } else {
257             if (rate > 1.0)
258                 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
259             else
260                 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
261         }
262     }
263
264     if (priv->avg_rate >= 0.0) {
265         HRESULT hr;
266         Quality q;
267         /* if we have a valid rate, start sending QoS messages */
268         if (priv->current_jitter < 0) {
269             /* make sure we never go below 0 when adding the jitter to the
270              * timestamp. */
271             if (priv->current_rstart < -priv->current_jitter)
272                 priv->current_jitter = -priv->current_rstart;
273         }
274         else
275             priv->current_jitter += (priv->current_rstop - priv->current_rstart);
276         q.Type = (jitter > 0 ? Famine : Flood);
277         q.Proportion = (LONG)(1000. / priv->avg_rate);
278         if (q.Proportion < 500)
279             q.Proportion = 500;
280         else if (q.Proportion > 2000)
281             q.Proportion = 2000;
282         q.Late = priv->current_jitter;
283         q.TimeStamp = priv->current_rstart;
284         TRACE("Late: %i from %i, rate: %g\n", (int)(q.Late/10000), (int)(q.TimeStamp/10000), 1./priv->avg_rate);
285         hr = IQualityControl_Notify((IQualityControl *)priv, priv->self, q);
286         priv->qos_handled = hr == S_OK;
287     }
288
289     /* record when this buffer will leave us */
290     priv->last_left = left;
291 }
292
293
294 void QualityControlRender_BeginRender(QualityControlImpl *This) {
295     This->start = -1;
296     if (!This->clock)
297         return;
298     IReferenceClock_GetTime(This->clock, &This->start);
299 }
300
301 void QualityControlRender_EndRender(QualityControlImpl *This) {
302     REFERENCE_TIME elapsed;
303     if (!This->clock || This->start < 0 || FAILED(IReferenceClock_GetTime(This->clock, &This->stop)))
304         return;
305
306     elapsed = This->start - This->stop;
307     if (elapsed < 0)
308         return;
309     if (This->avg_render < 0)
310         This->avg_render = elapsed;
311     else
312         This->avg_render = UPDATE_RUNNING_AVG (This->avg_render, elapsed);
313 }