ReactOS  0.4.14-dev-358-gbef841c
qualitycontrol.c File Reference
#include "dshow.h"
#include "wine/strmbase.h"
#include "strmbase_private.h"
#include "uuids.h"
#include "wine/debug.h"
#include <assert.h>
Include dependency graph for qualitycontrol.c:

Go to the source code of this file.

Macros

#define COBJMACROS
 
#define XTIME_FMT   "%u.%03u"
 
#define XTIME(u)   (int)(u/10000000), (int)((u / 10000)%1000)
 
#define DO_RUNNING_AVG(avg, val, size)   (((val) + ((size)-1) * (avg)) / (size))
 
#define UPDATE_RUNNING_AVG(avg, val)   DO_RUNNING_AVG(avg,val,8)
 
#define UPDATE_RUNNING_AVG_P(avg, val)   DO_RUNNING_AVG(avg,val,16)
 
#define UPDATE_RUNNING_AVG_N(avg, val)   DO_RUNNING_AVG(avg,val,4)
 

Functions

 WINE_DEFAULT_DEBUG_CHANNEL (strmbase_qc)
 
HRESULT QualityControlImpl_Create (IPin *input, IBaseFilter *self, QualityControlImpl **ppv)
 
void QualityControlImpl_Destroy (QualityControlImpl *This)
 
static QualityControlImplimpl_from_IQualityControl (IQualityControl *iface)
 
HRESULT WINAPI QualityControlImpl_QueryInterface (IQualityControl *iface, REFIID riid, void **ppv)
 
ULONG WINAPI QualityControlImpl_AddRef (IQualityControl *iface)
 
ULONG WINAPI QualityControlImpl_Release (IQualityControl *iface)
 
HRESULT WINAPI QualityControlImpl_Notify (IQualityControl *iface, IBaseFilter *sender, Quality qm)
 
HRESULT WINAPI QualityControlImpl_SetSink (IQualityControl *iface, IQualityControl *tonotify)
 
void QualityControlRender_Start (QualityControlImpl *This, REFERENCE_TIME tStart)
 
void QualityControlRender_SetClock (QualityControlImpl *This, IReferenceClock *clock)
 
static BOOL QualityControlRender_IsLate (QualityControlImpl *This, REFERENCE_TIME jitter, REFERENCE_TIME start, REFERENCE_TIME stop)
 
HRESULT QualityControlRender_WaitFor (QualityControlImpl *This, IMediaSample *sample, HANDLE ev)
 
void QualityControlRender_DoQOS (QualityControlImpl *priv)
 
void QualityControlRender_BeginRender (QualityControlImpl *This)
 
void QualityControlRender_EndRender (QualityControlImpl *This)
 

Macro Definition Documentation

◆ COBJMACROS

#define COBJMACROS

Definition at line 26 of file qualitycontrol.c.

◆ DO_RUNNING_AVG

#define DO_RUNNING_AVG (   avg,
  val,
  size 
)    (((val) + ((size)-1) * (avg)) / (size))

Definition at line 125 of file qualitycontrol.c.

◆ UPDATE_RUNNING_AVG

#define UPDATE_RUNNING_AVG (   avg,
  val 
)    DO_RUNNING_AVG(avg,val,8)

Definition at line 128 of file qualitycontrol.c.

◆ UPDATE_RUNNING_AVG_N

#define UPDATE_RUNNING_AVG_N (   avg,
  val 
)    DO_RUNNING_AVG(avg,val,4)

Definition at line 134 of file qualitycontrol.c.

◆ UPDATE_RUNNING_AVG_P

#define UPDATE_RUNNING_AVG_P (   avg,
  val 
)    DO_RUNNING_AVG(avg,val,16)

Definition at line 133 of file qualitycontrol.c.

◆ XTIME

#define XTIME (   u)    (int)(u/10000000), (int)((u / 10000)%1000)

Definition at line 40 of file qualitycontrol.c.

◆ XTIME_FMT

#define XTIME_FMT   "%u.%03u"

Definition at line 39 of file qualitycontrol.c.

Function Documentation

◆ impl_from_IQualityControl()

static QualityControlImpl* impl_from_IQualityControl ( IQualityControl iface)
inlinestatic

Definition at line 64 of file qualitycontrol.c.

65 {
66  return CONTAINING_RECORD(iface, QualityControlImpl, IQualityControl_iface);
67 }
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560

Referenced by QualityControlImpl_AddRef(), QualityControlImpl_Notify(), QualityControlImpl_QueryInterface(), QualityControlImpl_Release(), and QualityControlImpl_SetSink().

◆ QualityControlImpl_AddRef()

ULONG WINAPI QualityControlImpl_AddRef ( IQualityControl iface)

Definition at line 75 of file qualitycontrol.c.

76 {
78  return IBaseFilter_AddRef(This->self);
79 }
static QualityControlImpl * impl_from_IQualityControl(IQualityControl *iface)

◆ QualityControlImpl_Create()

HRESULT QualityControlImpl_Create ( IPin input,
IBaseFilter self,
QualityControlImpl **  ppv 
)

Definition at line 42 of file qualitycontrol.c.

43 {
45  TRACE("%p, %p, %p\n", input, self, ppv);
47  if (!*ppv)
48  return E_OUTOFMEMORY;
49  This = *ppv;
50  This->input = input;
51  This->self = self;
52  This->tonotify = NULL;
53  This->clock = NULL;
54  This->current_rstart = This->current_rstop = -1;
55  TRACE("-> %p\n", This);
56  return S_OK;
57 }
REFIID LPVOID * ppv
Definition: atlbase.h:39
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
smooth NULL
Definition: ftsmooth.c:416
#define TRACE(s)
Definition: solgame.cpp:4
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define S_OK
Definition: intsafe.h:59
GLenum GLenum GLenum input
Definition: glext.h:9031
#define HEAP_ZERO_MEMORY
Definition: compat.h:123

Referenced by BaseRenderer_Init(), and TransformFilter_Init().

◆ QualityControlImpl_Destroy()

void QualityControlImpl_Destroy ( QualityControlImpl This)

Definition at line 59 of file qualitycontrol.c.

60 {
62 }
#define GetProcessHeap()
Definition: compat.h:403
#define HeapFree(x, y, z)
Definition: compat.h:402

Referenced by BaseRendererImpl_Release(), and TransformFilterImpl_Release().

◆ QualityControlImpl_Notify()

HRESULT WINAPI QualityControlImpl_Notify ( IQualityControl iface,
IBaseFilter sender,
Quality  qm 
)

Definition at line 87 of file qualitycontrol.c.

88 {
90  HRESULT hr = S_FALSE;
91 
92  TRACE("%p %p { 0x%x %u " XTIME_FMT " " XTIME_FMT " }\n",
93  This, sender, qm.Type, qm.Proportion,
94  XTIME(qm.Late), XTIME(qm.TimeStamp));
95 
96  if (This->tonotify)
97  return IQualityControl_Notify(This->tonotify, This->self, qm);
98 
99  if (This->input) {
100  IPin *to = NULL;
101  IPin_ConnectedTo(This->input, &to);
102  if (to) {
103  IQualityControl *qc = NULL;
104  IPin_QueryInterface(to, &IID_IQualityControl, (void**)&qc);
105  if (qc) {
106  hr = IQualityControl_Notify(qc, This->self, qm);
107  IQualityControl_Release(qc);
108  }
109  IPin_Release(to);
110  }
111  }
112 
113  return hr;
114 }
HRESULT hr
Definition: shlfolder.c:183
static QualityControlImpl * impl_from_IQualityControl(IQualityControl *iface)
#define S_FALSE
Definition: winerror.h:2357
smooth NULL
Definition: ftsmooth.c:416
LONG Proportion
Definition: axextend.idl:322
#define TRACE(s)
Definition: solgame.cpp:4
LONG HRESULT
Definition: typedefs.h:77
REFERENCE_TIME Late
Definition: axextend.idl:323
#define XTIME_FMT
Definition: axcore.idl:91
QualityMessageType Type
Definition: axextend.idl:321
REFERENCE_TIME TimeStamp
Definition: axextend.idl:324
#define XTIME(u)

Referenced by TransformFilterImpl_Notify().

◆ QualityControlImpl_QueryInterface()

HRESULT WINAPI QualityControlImpl_QueryInterface ( IQualityControl iface,
REFIID  riid,
void **  ppv 
)

Definition at line 69 of file qualitycontrol.c.

70 {
72  return IBaseFilter_QueryInterface(This->self, riid, ppv);
73 }
REFIID riid
Definition: precomp.h:44
REFIID LPVOID * ppv
Definition: atlbase.h:39
static QualityControlImpl * impl_from_IQualityControl(IQualityControl *iface)

◆ QualityControlImpl_Release()

ULONG WINAPI QualityControlImpl_Release ( IQualityControl iface)

Definition at line 81 of file qualitycontrol.c.

82 {
84  return IBaseFilter_Release(This->self);
85 }
static QualityControlImpl * impl_from_IQualityControl(IQualityControl *iface)

◆ QualityControlImpl_SetSink()

HRESULT WINAPI QualityControlImpl_SetSink ( IQualityControl iface,
IQualityControl tonotify 
)

Definition at line 116 of file qualitycontrol.c.

117 {
119  TRACE("%p %p\n", This, tonotify);
120  This->tonotify = tonotify;
121  return S_OK;
122 }
static QualityControlImpl * impl_from_IQualityControl(IQualityControl *iface)
#define TRACE(s)
Definition: solgame.cpp:4
#define S_OK
Definition: intsafe.h:59

◆ QualityControlRender_BeginRender()

void QualityControlRender_BeginRender ( QualityControlImpl This)

Definition at line 336 of file qualitycontrol.c.

337 {
338  TRACE("%p\n", This);
339 
340  This->start = -1;
341 
342  if (!This->clock)
343  return;
344 
345  IReferenceClock_GetTime(This->clock, &This->start);
346  TRACE("at: " XTIME_FMT "\n", XTIME(This->start));
347 }
#define TRACE(s)
Definition: solgame.cpp:4
#define IReferenceClock_GetTime(p, a)
Definition: dmusicc.h:754
#define XTIME_FMT
#define XTIME(u)

Referenced by BaseRendererImpl_Receive().

◆ QualityControlRender_DoQOS()

void QualityControlRender_DoQOS ( QualityControlImpl priv)

Definition at line 223 of file qualitycontrol.c.

224 {
225  REFERENCE_TIME start, stop, jitter, pt, entered, left, duration;
226  double rate;
227 
228  TRACE("%p\n", priv);
229 
230  if (!priv->clock || priv->current_rstart < 0)
231  return;
232 
233  start = priv->current_rstart;
234  stop = priv->current_rstop;
235  jitter = priv->current_jitter;
236 
237  if (jitter < 0) {
238  /* this is the time the buffer entered the sink */
239  if (start < -jitter)
240  entered = 0;
241  else
242  entered = start + jitter;
243  left = start;
244  } else {
245  /* this is the time the buffer entered the sink */
246  entered = start + jitter;
247  /* this is the time the buffer left the sink */
248  left = start + jitter;
249  }
250 
251  /* calculate duration of the buffer */
252  if (stop >= start)
253  duration = stop - start;
254  else
255  duration = 0;
256 
257  /* if we have the time when the last buffer left us, calculate
258  * processing time */
259  if (priv->last_left >= 0) {
260  if (entered > priv->last_left) {
261  pt = entered - priv->last_left;
262  } else {
263  pt = 0;
264  }
265  } else {
266  pt = priv->avg_pt;
267  }
268 
269  TRACE("start: " XTIME_FMT ", entered " XTIME_FMT ", left " XTIME_FMT ", pt: " XTIME_FMT ", "
270  "duration " XTIME_FMT ", jitter " XTIME_FMT "\n", XTIME(start), XTIME(entered),
271  XTIME(left), XTIME(pt), XTIME(duration), XTIME(jitter));
272 
273  TRACE("avg_duration: " XTIME_FMT ", avg_pt: " XTIME_FMT ", avg_rate: %g\n",
274  XTIME(priv->avg_duration), XTIME(priv->avg_pt), priv->avg_rate);
275 
276  /* collect running averages. for first observations, we copy the
277  * values */
278  if (priv->avg_duration < 0)
279  priv->avg_duration = duration;
280  else
281  priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
282 
283  if (priv->avg_pt < 0)
284  priv->avg_pt = pt;
285  else
286  priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
287 
288  if (priv->avg_duration != 0)
289  rate =
290  (double)priv->avg_pt /
291  (double)priv->avg_duration;
292  else
293  rate = 0.0;
294 
295  if (priv->last_left >= 0) {
296  if (priv->is_dropped || priv->avg_rate < 0.0) {
297  priv->avg_rate = rate;
298  } else {
299  if (rate > 1.0)
300  priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
301  else
302  priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
303  }
304  }
305 
306  if (priv->avg_rate >= 0.0) {
307  HRESULT hr;
308  Quality q;
309  /* if we have a valid rate, start sending QoS messages */
310  if (priv->current_jitter < 0) {
311  /* make sure we never go below 0 when adding the jitter to the
312  * timestamp. */
313  if (priv->current_rstart < -priv->current_jitter)
314  priv->current_jitter = -priv->current_rstart;
315  }
316  else
317  priv->current_jitter += (priv->current_rstop - priv->current_rstart);
318  q.Type = (jitter > 0 ? Famine : Flood);
319  q.Proportion = (LONG)(1000. / priv->avg_rate);
320  if (q.Proportion < 200)
321  q.Proportion = 200;
322  else if (q.Proportion > 5000)
323  q.Proportion = 5000;
324  q.Late = priv->current_jitter;
325  q.TimeStamp = priv->current_rstart;
326  TRACE("Late: %i from %i, rate: %g\n", (int)(q.Late/10000), (int)(q.TimeStamp/10000), 1./priv->avg_rate);
327  hr = IQualityControl_Notify(&priv->IQualityControl_iface, priv->self, q);
328  priv->qos_handled = hr == S_OK;
329  }
330 
331  /* record when this buffer will leave us */
332  priv->last_left = left;
333 }
REFERENCE_TIME avg_duration
#define UPDATE_RUNNING_AVG(avg, val)
REFERENCE_TIME last_left
REFERENCE_TIME avg_pt
HRESULT hr
Definition: shlfolder.c:183
#define pt(x, y)
Definition: drawing.c:79
REFERENCE_TIME current_rstart
REFERENCE_TIME current_jitter
int rate
Definition: pcmconverter.c:97
IQualityControl IQualityControl_iface
long LONG
Definition: pedump.c:60
#define UPDATE_RUNNING_AVG_P(avg, val)
static const char mbstate_t *static wchar_t const char mbstate_t *static const wchar_t int *static double
Definition: string.c:80
IBaseFilter * self
#define TRACE(s)
Definition: solgame.cpp:4
LONGLONG REFERENCE_TIME
Definition: dmusicks.h:9
LONG HRESULT
Definition: typedefs.h:77
IReferenceClock * clock
GLint left
Definition: glext.h:7726
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
REFERENCE_TIME current_rstop
#define UPDATE_RUNNING_AVG_N(avg, val)
#define XTIME_FMT
#define S_OK
Definition: intsafe.h:59
GLuint start
Definition: gl.h:1545
#define XTIME(u)

Referenced by BaseRendererImpl_Receive().

◆ QualityControlRender_EndRender()

void QualityControlRender_EndRender ( QualityControlImpl This)

Definition at line 349 of file qualitycontrol.c.

350 {
351  REFERENCE_TIME elapsed;
352 
353  TRACE("%p\n", This);
354 
355  if (!This->clock || This->start < 0 || FAILED(IReferenceClock_GetTime(This->clock, &This->stop)))
356  return;
357 
358  elapsed = This->start - This->stop;
359  if (elapsed < 0)
360  return;
361  if (This->avg_render < 0)
362  This->avg_render = elapsed;
363  else
364  This->avg_render = UPDATE_RUNNING_AVG (This->avg_render, elapsed);
365 }
#define UPDATE_RUNNING_AVG(avg, val)
#define TRACE(s)
Definition: solgame.cpp:4
LONGLONG REFERENCE_TIME
Definition: dmusicks.h:9
#define IReferenceClock_GetTime(p, a)
Definition: dmusicc.h:754

Referenced by BaseRendererImpl_Receive().

◆ QualityControlRender_IsLate()

static BOOL QualityControlRender_IsLate ( QualityControlImpl This,
REFERENCE_TIME  jitter,
REFERENCE_TIME  start,
REFERENCE_TIME  stop 
)
static

Definition at line 154 of file qualitycontrol.c.

156 {
157  REFERENCE_TIME max_lateness = 200000;
158 
159  TRACE("%p " XTIME_FMT " " XTIME_FMT " " XTIME_FMT "\n",
160  This, XTIME(jitter), XTIME(start), XTIME(stop));
161 
162  /* we can add a valid stop time */
163  if (stop >= start)
164  max_lateness += stop;
165  else
166  max_lateness += start;
167 
168  /* if the jitter bigger than duration and lateness we are too late */
169  if (start + jitter > max_lateness) {
170  WARN("buffer is too late %i > %i\n", (int)((start + jitter)/10000), (int)(max_lateness/10000));
171  /* !!emergency!!, if we did not receive anything valid for more than a
172  * second, render it anyway so the user sees something */
173  if (This->last_in_time < 0 ||
174  start - This->last_in_time < 10000000)
175  return TRUE;
176  FIXME("A lot of buffers are being dropped.\n");
177  FIXME("There may be a timestamping problem, or this computer is too slow.\n");
178  }
179  This->last_in_time = start;
180  return FALSE;
181 }
#define TRUE
Definition: types.h:120
#define WARN(fmt,...)
Definition: debug.h:111
#define FIXME(fmt,...)
Definition: debug.h:110
#define TRACE(s)
Definition: solgame.cpp:4
LONGLONG REFERENCE_TIME
Definition: dmusicks.h:9
#define XTIME_FMT
GLuint start
Definition: gl.h:1545
#define XTIME(u)

Referenced by QualityControlRender_WaitFor().

◆ QualityControlRender_SetClock()

void QualityControlRender_SetClock ( QualityControlImpl This,
IReferenceClock clock 
)

Definition at line 148 of file qualitycontrol.c.

149 {
150  TRACE("%p %p\n", This, clock);
151  This->clock = clock;
152 }
#define TRACE(s)
Definition: solgame.cpp:4
_Check_return_ _CRTIMP clock_t __cdecl clock(void)
Definition: clock.c:23

Referenced by BaseRendererImpl_SetSyncSource().

◆ QualityControlRender_Start()

void QualityControlRender_Start ( QualityControlImpl This,
REFERENCE_TIME  tStart 
)

Definition at line 136 of file qualitycontrol.c.

137 {
138  TRACE("%p " XTIME_FMT "\n", This, XTIME(tStart));
139  This->avg_render = This->last_in_time = This->last_left = This->avg_duration = This->avg_pt = -1;
140  This->clockstart = tStart;
141  This->avg_rate = -1.0;
142  This->rendered = This->dropped = 0;
143  This->is_dropped = FALSE;
144  This->qos_handled = TRUE; /* Lie that will be corrected on first adjustment */
145 }
#define TRUE
Definition: types.h:120
#define TRACE(s)
Definition: solgame.cpp:4
#define XTIME_FMT
#define XTIME(u)

Referenced by BaseRendererImpl_EndFlush(), and BaseRendererImpl_Run().

◆ QualityControlRender_WaitFor()

HRESULT QualityControlRender_WaitFor ( QualityControlImpl This,
IMediaSample sample,
HANDLE  ev 
)

Definition at line 183 of file qualitycontrol.c.

184 {
185  REFERENCE_TIME start = -1, stop = -1, jitter = 0;
186 
187  TRACE("%p %p %p\n", This, sample, ev);
188 
189  This->current_rstart = This->current_rstop = -1;
190  This->current_jitter = 0;
191  if (!This->clock || FAILED(IMediaSample_GetTime(sample, &start, &stop)))
192  return S_OK;
193 
194  if (start >= 0) {
196  IReferenceClock_GetTime(This->clock, &now);
197  now -= This->clockstart;
198 
199  jitter = now - start;
200  if (jitter <= -10000) {
201  DWORD_PTR cookie;
202  IReferenceClock_AdviseTime(This->clock, This->clockstart, start, (HEVENT)ev, &cookie);
204  IReferenceClock_Unadvise(This->clock, cookie);
205  }
206  }
207  else
208  start = stop = -1;
209  This->current_rstart = start;
210  This->current_rstop = stop > start ? stop : start;
211  This->current_jitter = jitter;
212  This->is_dropped = QualityControlRender_IsLate(This, jitter, start, stop);
213  TRACE("Dropped: %i %i %i %i\n", This->is_dropped, (int)(start/10000), (int)(stop/10000), (int)(jitter / 10000));
214  if (This->is_dropped) {
215  This->dropped++;
216  if (!This->qos_handled)
217  return S_FALSE;
218  } else
219  This->rendered++;
220  return S_OK;
221 }
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
static BOOL QualityControlRender_IsLate(QualityControlImpl *This, REFERENCE_TIME jitter, REFERENCE_TIME start, REFERENCE_TIME stop)
time_t now
Definition: finger.c:65
#define S_FALSE
Definition: winerror.h:2357
#define IReferenceClock_AdviseTime(p, a, b, c, d)
Definition: dmusicc.h:755
#define TRACE(s)
Definition: solgame.cpp:4
LONGLONG REFERENCE_TIME
Definition: dmusicks.h:9
#define IReferenceClock_GetTime(p, a)
Definition: dmusicc.h:754
uint32_t DWORD_PTR
Definition: typedefs.h:63
#define S_OK
Definition: intsafe.h:59
GLuint start
Definition: gl.h:1545
#define IReferenceClock_Unadvise(p, a)
Definition: dmusicc.h:757
#define INFINITE
Definition: serial.h:102
DWORD_PTR HEVENT
Definition: axcore.idl:61

Referenced by BaseRendererImpl_Receive().

◆ WINE_DEFAULT_DEBUG_CHANNEL()

WINE_DEFAULT_DEBUG_CHANNEL ( strmbase_qc  )