wined3d: Get rid of the local_constant typedef.
[wine] / dlls / quartz / videorenderer.c
1 /*
2  * Video Renderer (Fullscreen and Windowed using Direct Draw)
3  *
4  * Copyright 2004 Christian Costa
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #define NONAMELESSSTRUCT
24 #define NONAMELESSUNION
25 #include "quartz_private.h"
26 #include "control_private.h"
27 #include "pin.h"
28
29 #include "uuids.h"
30 #include "vfwmsgs.h"
31 #include "amvideo.h"
32 #include "windef.h"
33 #include "winbase.h"
34 #include "dshow.h"
35 #include "evcode.h"
36 #include "strmif.h"
37 #include "ddraw.h"
38 #include "dvdmedia.h"
39
40 #include <assert.h>
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
45
46 static BOOL wnd_class_registered = FALSE;
47
48 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
49
50 static const IBaseFilterVtbl VideoRenderer_Vtbl;
51 static const IUnknownVtbl IInner_VTable;
52 static const IBasicVideoVtbl IBasicVideo_VTable;
53 static const IVideoWindowVtbl IVideoWindow_VTable;
54 static const IPinVtbl VideoRenderer_InputPin_Vtbl;
55 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl;
56 static const IQualityControlVtbl VideoRenderer_QualityControl_Vtbl = {
57     QualityControlImpl_QueryInterface,
58     QualityControlImpl_AddRef,
59     QualityControlImpl_Release,
60     QualityControlImpl_Notify,
61     QualityControlImpl_SetSink
62 };
63
64 typedef struct VideoRendererImpl
65 {
66     BaseFilter filter;
67     const IBasicVideoVtbl * IBasicVideo_vtbl;
68     const IVideoWindowVtbl * IVideoWindow_vtbl;
69     const IUnknownVtbl * IInner_vtbl;
70     const IAMFilterMiscFlagsVtbl *IAMFilterMiscFlags_vtbl;
71     IUnknown *seekthru_unk;
72     QualityControlImpl qcimpl;
73
74     BaseInputPin *pInputPin;
75
76     BOOL init;
77     HANDLE hThread;
78     HANDLE blocked;
79
80     DWORD ThreadID;
81     HANDLE hEvent;
82     BOOL ThreadResult;
83     HWND hWnd;
84     HWND hWndMsgDrain;
85     HWND hWndOwner;
86     BOOL AutoShow;
87     RECT SourceRect;
88     RECT DestRect;
89     RECT WindowPos;
90     LONG VideoWidth;
91     LONG VideoHeight;
92     IUnknown * pUnkOuter;
93     BOOL bUnkOuterValid;
94     BOOL bAggregatable;
95     LONG WindowStyle;
96
97     /* During pause we can hold a single sample, for use in GetCurrentImage */
98     IMediaSample *sample_held;
99 } VideoRendererImpl;
100
101 static LRESULT CALLBACK VideoWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
102 {
103     VideoRendererImpl* pVideoRenderer = (VideoRendererImpl*)GetWindowLongPtrW(hwnd, 0);
104     LPRECT lprect = (LPRECT)lParam;
105
106     if (pVideoRenderer && pVideoRenderer->hWndMsgDrain)
107     {
108         switch(uMsg)
109         {
110             case WM_KEYDOWN:
111             case WM_KEYUP:
112             case WM_LBUTTONDBLCLK:
113             case WM_LBUTTONDOWN:
114             case WM_LBUTTONUP:
115             case WM_MBUTTONDBLCLK:
116             case WM_MBUTTONDOWN:
117             case WM_MBUTTONUP:
118             case WM_MOUSEACTIVATE:
119             case WM_MOUSEMOVE:
120             case WM_NCLBUTTONDBLCLK:
121             case WM_NCLBUTTONDOWN:
122             case WM_NCLBUTTONUP:
123             case WM_NCMBUTTONDBLCLK:
124             case WM_NCMBUTTONDOWN:
125             case WM_NCMBUTTONUP:
126             case WM_NCMOUSEMOVE:
127             case WM_NCRBUTTONDBLCLK:
128             case WM_NCRBUTTONDOWN:
129             case WM_NCRBUTTONUP:
130             case WM_RBUTTONDBLCLK:
131             case WM_RBUTTONDOWN:
132             case WM_RBUTTONUP:
133                 PostMessageW(pVideoRenderer->hWndMsgDrain, uMsg, wParam, lParam);
134                 break;
135             default:
136                 break;
137         }
138     }
139
140     switch(uMsg)
141     {
142         case WM_SIZING:
143             /* TRACE("WM_SIZING %d %d %d %d\n", lprect->left, lprect->top, lprect->right, lprect->bottom); */
144             SetWindowPos(hwnd, NULL, lprect->left, lprect->top, lprect->right - lprect->left, lprect->bottom - lprect->top, SWP_NOZORDER);
145             GetClientRect(hwnd, &pVideoRenderer->DestRect);
146             TRACE("WM_SIZING: DestRect=(%d,%d),(%d,%d)\n",
147                 pVideoRenderer->DestRect.left,
148                 pVideoRenderer->DestRect.top,
149                 pVideoRenderer->DestRect.right - pVideoRenderer->DestRect.left,
150                 pVideoRenderer->DestRect.bottom - pVideoRenderer->DestRect.top);
151             return TRUE;
152         case WM_SIZE:
153             TRACE("WM_SIZE %d %d\n", LOWORD(lParam), HIWORD(lParam));
154             GetClientRect(hwnd, &pVideoRenderer->DestRect);
155             TRACE("WM_SIZING: DestRect=(%d,%d),(%d,%d)\n",
156                 pVideoRenderer->DestRect.left,
157                 pVideoRenderer->DestRect.top,
158                 pVideoRenderer->DestRect.right - pVideoRenderer->DestRect.left,
159                 pVideoRenderer->DestRect.bottom - pVideoRenderer->DestRect.top);
160             return TRUE;
161         default:
162             return DefWindowProcW(hwnd, uMsg, wParam, lParam);
163     }
164     return 0;
165 }
166
167 static const WCHAR classnameW[] = { 'W','i','n','e',' ','A','c','t','i','v','e','M','o','v','i','e',' ','C','l','a','s','s',0 };
168 static const WCHAR windownameW[] = { 'A','c','t','i','v','e','M','o','v','i','e',' ','W','i','n','d','o','w',0 };
169
170 static BOOL video_register_windowclass(void) {
171     WNDCLASSW winclass;
172     if (wnd_class_registered)
173         return 1;
174
175     winclass.style = 0;
176     winclass.lpfnWndProc = VideoWndProcW;
177     winclass.cbClsExtra = 0;
178     winclass.cbWndExtra = sizeof(VideoRendererImpl*);
179     winclass.hInstance = NULL;
180     winclass.hIcon = NULL;
181     winclass.hCursor = NULL;
182     winclass.hbrBackground = GetStockObject(BLACK_BRUSH);
183     winclass.lpszMenuName = NULL;
184     winclass.lpszClassName = classnameW;
185     if (!RegisterClassW(&winclass))
186     {
187         ERR("Unable to register window class: %u\n", GetLastError());
188         return FALSE;
189     }
190     wnd_class_registered = 1;
191     return 1;
192 }
193
194 void video_unregister_windowclass(void) {
195     if (!wnd_class_registered)
196         return;
197     UnregisterClassW(classnameW, NULL);
198 }
199
200 static BOOL CreateRenderingWindow(VideoRendererImpl* This)
201 {
202     TRACE("(%p)->()\n", This);
203     if (!video_register_windowclass())
204         return FALSE;
205     This->hWnd = CreateWindowExW(0, classnameW, windownameW, WS_SIZEBOX,
206                                  CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
207                                  NULL, NULL, NULL);
208
209     if (!This->hWnd)
210     {
211         ERR("Unable to create window\n");
212         return FALSE;
213     }
214
215     SetWindowLongPtrW(This->hWnd, 0, (LONG_PTR)This);
216
217     return TRUE;
218 }
219
220 static DWORD WINAPI MessageLoop(LPVOID lpParameter)
221 {
222     VideoRendererImpl* This = lpParameter;
223     MSG msg; 
224     BOOL fGotMessage;
225
226     TRACE("Starting message loop\n");
227
228     if (!CreateRenderingWindow(This))
229     {
230         This->ThreadResult = FALSE;
231         SetEvent(This->hEvent);
232         return 0;
233     }
234
235     This->ThreadResult = TRUE;
236     SetEvent(This->hEvent);
237
238     while ((fGotMessage = GetMessageW(&msg, NULL, 0, 0)) != 0 && fGotMessage != -1)
239     {
240         TranslateMessage(&msg); 
241         DispatchMessageW(&msg);
242     }
243
244     TRACE("End of message loop\n");
245
246     return msg.wParam;
247 }
248
249 static BOOL CreateRenderingSubsystem(VideoRendererImpl* This)
250 {
251     This->hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
252     if (!This->hEvent)
253         return FALSE;
254
255     This->hThread = CreateThread(NULL, 0, MessageLoop, This, 0, &This->ThreadID);
256     if (!This->hThread)
257     {
258         CloseHandle(This->hEvent);
259         return FALSE;
260     }
261
262     WaitForSingleObject(This->hEvent, INFINITE);
263
264     if (!This->ThreadResult)
265     {
266         CloseHandle(This->hEvent);
267         CloseHandle(This->hThread);
268         return FALSE;
269     }
270
271     return TRUE;
272 }
273
274 static void VideoRenderer_AutoShowWindow(VideoRendererImpl *This) {
275     if (!This->init && (!This->WindowPos.right || !This->WindowPos.top))
276     {
277         DWORD style = GetWindowLongW(This->hWnd, GWL_STYLE);
278         DWORD style_ex = GetWindowLongW(This->hWnd, GWL_EXSTYLE);
279
280         if (!This->WindowPos.right)
281         {
282             This->WindowPos.left = This->SourceRect.left;
283             This->WindowPos.right = This->SourceRect.right;
284         }
285         if (!This->WindowPos.bottom)
286         {
287             This->WindowPos.top = This->SourceRect.top;
288             This->WindowPos.bottom = This->SourceRect.bottom;
289         }
290
291         AdjustWindowRectEx(&This->WindowPos, style, TRUE, style_ex);
292
293         TRACE("WindowPos: %d %d %d %d\n", This->WindowPos.left, This->WindowPos.top, This->WindowPos.right, This->WindowPos.bottom);
294         SetWindowPos(This->hWnd, NULL,
295             This->WindowPos.left,
296             This->WindowPos.top,
297             This->WindowPos.right - This->WindowPos.left,
298             This->WindowPos.bottom - This->WindowPos.top,
299             SWP_NOZORDER|SWP_NOMOVE|SWP_DEFERERASE);
300
301         GetClientRect(This->hWnd, &This->DestRect);
302     }
303     else if (!This->init)
304         This->DestRect = This->WindowPos;
305     This->init = TRUE;
306     if (This->AutoShow)
307         ShowWindow(This->hWnd, SW_SHOW);
308 }
309
310 static DWORD VideoRenderer_SendSampleData(VideoRendererImpl* This, LPBYTE data, DWORD size)
311 {
312     AM_MEDIA_TYPE amt;
313     HRESULT hr = S_OK;
314     DDSURFACEDESC sdesc;
315     HDC hDC;
316     BITMAPINFOHEADER *bmiHeader;
317
318     TRACE("(%p)->(%p, %d)\n", This, data, size);
319
320     sdesc.dwSize = sizeof(sdesc);
321     hr = IPin_ConnectionMediaType((IPin *)This->pInputPin, &amt);
322     if (FAILED(hr)) {
323         ERR("Unable to retrieve media type\n");
324         return hr;
325     }
326
327     if (IsEqualIID(&amt.formattype, &FORMAT_VideoInfo))
328     {
329         bmiHeader = &((VIDEOINFOHEADER *)amt.pbFormat)->bmiHeader;
330     }
331     else if (IsEqualIID(&amt.formattype, &FORMAT_VideoInfo2))
332     {
333         bmiHeader = &((VIDEOINFOHEADER2 *)amt.pbFormat)->bmiHeader;
334     }
335     else
336     {
337         FIXME("Unknown type %s\n", debugstr_guid(&amt.subtype));
338         return VFW_E_RUNTIME_ERROR;
339     }
340
341     TRACE("biSize = %d\n", bmiHeader->biSize);
342     TRACE("biWidth = %d\n", bmiHeader->biWidth);
343     TRACE("biHeight = %d\n", bmiHeader->biHeight);
344     TRACE("biPlanes = %d\n", bmiHeader->biPlanes);
345     TRACE("biBitCount = %d\n", bmiHeader->biBitCount);
346     TRACE("biCompression = %s\n", debugstr_an((LPSTR)&(bmiHeader->biCompression), 4));
347     TRACE("biSizeImage = %d\n", bmiHeader->biSizeImage);
348
349     hDC = GetDC(This->hWnd);
350
351     if (!hDC) {
352         ERR("Cannot get DC from window!\n");
353         return E_FAIL;
354     }
355
356     TRACE("Src Rect: %d %d %d %d\n", This->SourceRect.left, This->SourceRect.top, This->SourceRect.right, This->SourceRect.bottom);
357     TRACE("Dst Rect: %d %d %d %d\n", This->DestRect.left, This->DestRect.top, This->DestRect.right, This->DestRect.bottom);
358
359     StretchDIBits(hDC, This->DestRect.left, This->DestRect.top, This->DestRect.right -This->DestRect.left,
360                   This->DestRect.bottom - This->DestRect.top, This->SourceRect.left, This->SourceRect.top,
361                   This->SourceRect.right - This->SourceRect.left, This->SourceRect.bottom - This->SourceRect.top,
362                   data, (BITMAPINFO *)bmiHeader, DIB_RGB_COLORS, SRCCOPY);
363
364     ReleaseDC(This->hWnd, hDC);
365
366     return S_OK;
367 }
368
369 static HRESULT WINAPI VideoRenderer_Receive(BaseInputPin* pin, IMediaSample * pSample)
370 {
371     VideoRendererImpl *This = (VideoRendererImpl *)pin->pin.pinInfo.pFilter;
372     LPBYTE pbSrcStream = NULL;
373     LONG cbSrcStream = 0;
374     REFERENCE_TIME tStart, tStop;
375     HRESULT hr;
376
377     TRACE("(%p)->(%p)\n", pin, pSample);
378
379     EnterCriticalSection(&This->filter.csFilter);
380
381     if (This->pInputPin->flushing || This->pInputPin->end_of_stream)
382     {
383         LeaveCriticalSection(&This->filter.csFilter);
384         return S_FALSE;
385     }
386
387     if (This->filter.state == State_Stopped)
388     {
389         LeaveCriticalSection(&This->filter.csFilter);
390         return VFW_E_WRONG_STATE;
391     }
392
393     if (IMediaSample_GetMediaTime(pSample, &tStart, &tStop) == S_OK)
394         MediaSeekingPassThru_RegisterMediaTime(This->seekthru_unk, tStart);
395
396     /* Preroll means the sample isn't shown, this is used for key frames and things like that */
397     if (IMediaSample_IsPreroll(pSample) == S_OK) {
398         LeaveCriticalSection(&This->filter.csFilter);
399         return S_OK;
400     }
401
402     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
403     if (FAILED(hr))
404     {
405         ERR("Cannot get pointer to sample data (%x)\n", hr);
406         LeaveCriticalSection(&This->filter.csFilter);
407         return hr;
408     }
409
410     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
411
412     TRACE("val %p %d\n", pbSrcStream, cbSrcStream);
413
414 #if 0 /* For debugging purpose */
415     {
416         int i;
417         for(i = 0; i < cbSrcStream; i++)
418         {
419             if ((i!=0) && !(i%16))
420                 TRACE("\n");
421                 TRACE("%02x ", pbSrcStream[i]);
422         }
423         TRACE("\n");
424     }
425 #endif
426
427     SetEvent(This->hEvent);
428     if (This->filter.state == State_Paused)
429     {
430         VideoRenderer_SendSampleData(This, pbSrcStream, cbSrcStream);
431         This->sample_held = pSample;
432         LeaveCriticalSection(&This->filter.csFilter);
433         WaitForSingleObject(This->blocked, INFINITE);
434         EnterCriticalSection(&This->filter.csFilter);
435         SetEvent(This->hEvent);
436         This->sample_held = NULL;
437         if (This->filter.state == State_Paused)
438         {
439             /* Flushing */
440             LeaveCriticalSection(&This->filter.csFilter);
441             return S_OK;
442         }
443         if (This->filter.state == State_Stopped)
444         {
445             LeaveCriticalSection(&This->filter.csFilter);
446             return VFW_E_WRONG_STATE;
447         }
448     } else {
449         hr = QualityControlRender_WaitFor(&This->qcimpl, pSample, This->blocked);
450         if (hr == S_OK) {
451             QualityControlRender_BeginRender(&This->qcimpl);
452             VideoRenderer_SendSampleData(This, pbSrcStream, cbSrcStream);
453             QualityControlRender_EndRender(&This->qcimpl);
454         }
455         QualityControlRender_DoQOS(&This->qcimpl);
456     }
457     LeaveCriticalSection(&This->filter.csFilter);
458     return S_OK;
459 }
460
461 static HRESULT WINAPI VideoRenderer_CheckMediaType(BasePin *iface, const AM_MEDIA_TYPE * pmt)
462 {
463     BaseInputPin* pin = (BaseInputPin*)iface;
464     VideoRendererImpl *This = (VideoRendererImpl *)pin->pin.pinInfo.pFilter;
465
466     if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Video))
467         return S_FALSE;
468
469     if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_RGB32) ||
470         IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_RGB24) ||
471         IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_RGB565) ||
472         IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_RGB8))
473     {
474         LONG height;
475
476         if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
477         {
478             VIDEOINFOHEADER *format = (VIDEOINFOHEADER *)pmt->pbFormat;
479             This->SourceRect.left = 0;
480             This->SourceRect.top = 0;
481             This->SourceRect.right = This->VideoWidth = format->bmiHeader.biWidth;
482             height = format->bmiHeader.biHeight;
483             if (height < 0)
484                 This->SourceRect.bottom = This->VideoHeight = -height;
485             else
486                 This->SourceRect.bottom = This->VideoHeight = height;
487         }
488         else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
489         {
490             VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)pmt->pbFormat;
491
492             This->SourceRect.left = 0;
493             This->SourceRect.top = 0;
494             This->SourceRect.right = This->VideoWidth = format2->bmiHeader.biWidth;
495             height = format2->bmiHeader.biHeight;
496             if (height < 0)
497                 This->SourceRect.bottom = This->VideoHeight = -height;
498             else
499                 This->SourceRect.bottom = This->VideoHeight = height;
500         }
501         else
502         {
503             WARN("Format type %s not supported\n", debugstr_guid(&pmt->formattype));
504             return S_FALSE;
505         }
506         return S_OK;
507     }
508     return S_FALSE;
509 }
510
511 static IPin* WINAPI VideoRenderer_GetPin(BaseFilter *iface, int pos)
512 {
513     VideoRendererImpl *This = (VideoRendererImpl *)iface;
514
515     if (pos >= 1 || pos < 0)
516         return NULL;
517
518     IPin_AddRef((IPin *)This->pInputPin);
519     return (IPin *)This->pInputPin;
520 }
521
522 static LONG WINAPI VideoRenderer_GetPinCount(BaseFilter *iface)
523 {
524     return 1;
525 }
526
527 static const BaseFilterFuncTable BaseFuncTable = {
528     VideoRenderer_GetPin,
529     VideoRenderer_GetPinCount
530 };
531
532 static const  BasePinFuncTable input_BaseFuncTable = {
533     VideoRenderer_CheckMediaType,
534     NULL,
535     BasePinImpl_GetMediaTypeVersion,
536     BasePinImpl_GetMediaType
537 };
538
539 static const BaseInputPinFuncTable input_BaseInputFuncTable = {
540     VideoRenderer_Receive
541 };
542
543 HRESULT VideoRenderer_create(IUnknown * pUnkOuter, LPVOID * ppv)
544 {
545     HRESULT hr;
546     PIN_INFO piInput;
547     VideoRendererImpl * pVideoRenderer;
548     ISeekingPassThru *passthru;
549
550     TRACE("(%p, %p)\n", pUnkOuter, ppv);
551
552     *ppv = NULL;
553
554     pVideoRenderer = CoTaskMemAlloc(sizeof(VideoRendererImpl));
555     pVideoRenderer->pUnkOuter = pUnkOuter;
556     pVideoRenderer->bUnkOuterValid = FALSE;
557     pVideoRenderer->bAggregatable = FALSE;
558     pVideoRenderer->IInner_vtbl = &IInner_VTable;
559     pVideoRenderer->IAMFilterMiscFlags_vtbl = &IAMFilterMiscFlags_Vtbl;
560
561     BaseFilter_Init(&pVideoRenderer->filter, &VideoRenderer_Vtbl, &CLSID_VideoRenderer, (DWORD_PTR)(__FILE__ ": VideoRendererImpl.csFilter"), &BaseFuncTable);
562
563     pVideoRenderer->IBasicVideo_vtbl = &IBasicVideo_VTable;
564     pVideoRenderer->IVideoWindow_vtbl = &IVideoWindow_VTable;
565
566     pVideoRenderer->init = 0;
567     pVideoRenderer->AutoShow = 1;
568     ZeroMemory(&pVideoRenderer->SourceRect, sizeof(RECT));
569     ZeroMemory(&pVideoRenderer->DestRect, sizeof(RECT));
570     ZeroMemory(&pVideoRenderer->WindowPos, sizeof(RECT));
571     pVideoRenderer->hWndMsgDrain = pVideoRenderer->hWndOwner = NULL;
572     pVideoRenderer->WindowStyle = WS_OVERLAPPED;
573
574     /* construct input pin */
575     piInput.dir = PINDIR_INPUT;
576     piInput.pFilter = (IBaseFilter *)pVideoRenderer;
577     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
578
579     hr = BaseInputPin_Construct(&VideoRenderer_InputPin_Vtbl, &piInput, &input_BaseFuncTable, &input_BaseInputFuncTable, &pVideoRenderer->filter.csFilter, NULL, (IPin **)&pVideoRenderer->pInputPin);
580
581     if (SUCCEEDED(hr))
582     {
583         hr = CoCreateInstance(&CLSID_SeekingPassThru, pUnkOuter ? pUnkOuter : (IUnknown*)&pVideoRenderer->IInner_vtbl, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&pVideoRenderer->seekthru_unk);
584         if (FAILED(hr)) {
585             IPin_Release((IPin*)pVideoRenderer->pInputPin);
586             goto fail;
587         }
588         IUnknown_QueryInterface(pVideoRenderer->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
589         ISeekingPassThru_Init(passthru, TRUE, (IPin*)pVideoRenderer->pInputPin);
590         ISeekingPassThru_Release(passthru);
591         pVideoRenderer->sample_held = NULL;
592         *ppv = pVideoRenderer;
593     }
594     if (FAILED(hr))
595         goto fail;
596
597     QualityControlImpl_init(&pVideoRenderer->qcimpl, (IPin*)pVideoRenderer->pInputPin, (IBaseFilter*)pVideoRenderer);
598     pVideoRenderer->qcimpl.lpVtbl = &VideoRenderer_QualityControl_Vtbl;
599
600     if (!CreateRenderingSubsystem(pVideoRenderer))
601         return E_FAIL;
602
603     pVideoRenderer->blocked = CreateEventW(NULL, FALSE, FALSE, NULL);
604     if (!pVideoRenderer->blocked)
605     {
606         hr = HRESULT_FROM_WIN32(GetLastError());
607         IUnknown_Release((IUnknown *)pVideoRenderer);
608     }
609
610     return hr;
611 fail:
612     BaseFilterImpl_Release((IBaseFilter*)pVideoRenderer);
613     CoTaskMemFree(pVideoRenderer);
614     return hr;
615 }
616
617 HRESULT VideoRendererDefault_create(IUnknown * pUnkOuter, LPVOID * ppv)
618 {
619     /* TODO: Attempt to use the VMR-7 renderer instead when possible */
620     return VideoRenderer_create(pUnkOuter, ppv);
621 }
622
623 static HRESULT WINAPI VideoRendererInner_QueryInterface(IUnknown * iface, REFIID riid, LPVOID * ppv)
624 {
625     ICOM_THIS_MULTI(VideoRendererImpl, IInner_vtbl, iface);
626     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
627
628     if (This->bAggregatable)
629         This->bUnkOuterValid = TRUE;
630
631     *ppv = NULL;
632
633     if (IsEqualIID(riid, &IID_IUnknown))
634         *ppv = &This->IInner_vtbl;
635     else if (IsEqualIID(riid, &IID_IPersist))
636         *ppv = This;
637     else if (IsEqualIID(riid, &IID_IMediaFilter))
638         *ppv = This;
639     else if (IsEqualIID(riid, &IID_IBaseFilter))
640         *ppv = This;
641     else if (IsEqualIID(riid, &IID_IBasicVideo))
642         *ppv = &This->IBasicVideo_vtbl;
643     else if (IsEqualIID(riid, &IID_IVideoWindow))
644         *ppv = &This->IVideoWindow_vtbl;
645     else if (IsEqualIID(riid, &IID_IMediaSeeking))
646         return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
647     else if (IsEqualIID(riid, &IID_IAMFilterMiscFlags))
648         *ppv = &This->IAMFilterMiscFlags_vtbl;
649     else if (IsEqualIID(riid, &IID_IQualityControl))
650         *ppv = &This->qcimpl;
651
652     if (*ppv)
653     {
654         IUnknown_AddRef((IUnknown *)(*ppv));
655         return S_OK;
656     }
657
658     if (!IsEqualIID(riid, &IID_IPin))
659         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
660
661     return E_NOINTERFACE;
662 }
663
664 static ULONG WINAPI VideoRendererInner_AddRef(IUnknown * iface)
665 {
666     ICOM_THIS_MULTI(VideoRendererImpl, IInner_vtbl, iface);
667     ULONG refCount = InterlockedIncrement(&This->filter.refCount);
668
669     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
670
671     return refCount;
672 }
673
674 static ULONG WINAPI VideoRendererInner_Release(IUnknown * iface)
675 {
676     ICOM_THIS_MULTI(VideoRendererImpl, IInner_vtbl, iface);
677     ULONG refCount = InterlockedDecrement(&This->filter.refCount);
678
679     TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
680
681     if (!refCount)
682     {
683         IPin *pConnectedTo;
684
685         if (This->hWnd)
686             SendMessageW(This->hWnd, WM_CLOSE, 0, 0);
687         PostThreadMessageW(This->ThreadID, WM_QUIT, 0, 0);
688         WaitForSingleObject(This->hThread, INFINITE);
689         CloseHandle(This->hThread);
690         CloseHandle(This->hEvent);
691
692         if (SUCCEEDED(IPin_ConnectedTo((IPin *)This->pInputPin, &pConnectedTo)))
693         {
694             IPin_Disconnect(pConnectedTo);
695             IPin_Release(pConnectedTo);
696         }
697         IPin_Disconnect((IPin *)This->pInputPin);
698
699         IPin_Release((IPin *)This->pInputPin);
700
701         This->filter.lpVtbl = NULL;
702         IUnknown_Release(This->seekthru_unk);
703         This->filter.csFilter.DebugInfo->Spare[0] = 0;
704         DeleteCriticalSection(&This->filter.csFilter);
705
706         TRACE("Destroying Video Renderer\n");
707         CoTaskMemFree(This);
708         
709         return 0;
710     }
711     else
712         return refCount;
713 }
714
715 static const IUnknownVtbl IInner_VTable =
716 {
717     VideoRendererInner_QueryInterface,
718     VideoRendererInner_AddRef,
719     VideoRendererInner_Release
720 };
721
722 static HRESULT WINAPI VideoRenderer_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
723 {
724     VideoRendererImpl *This = (VideoRendererImpl *)iface;
725
726     if (This->bAggregatable)
727         This->bUnkOuterValid = TRUE;
728
729     if (This->pUnkOuter)
730     {
731         if (This->bAggregatable)
732             return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
733
734         if (IsEqualIID(riid, &IID_IUnknown))
735         {
736             HRESULT hr;
737
738             IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
739             hr = IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
740             IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
741             This->bAggregatable = TRUE;
742             return hr;
743         }
744
745         *ppv = NULL;
746         return E_NOINTERFACE;
747     }
748
749     return IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
750 }
751
752 static ULONG WINAPI VideoRenderer_AddRef(IBaseFilter * iface)
753 {
754     VideoRendererImpl *This = (VideoRendererImpl *)iface;
755
756     if (This->pUnkOuter && This->bUnkOuterValid)
757         return IUnknown_AddRef(This->pUnkOuter);
758     return IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
759 }
760
761 static ULONG WINAPI VideoRenderer_Release(IBaseFilter * iface)
762 {
763     VideoRendererImpl *This = (VideoRendererImpl *)iface;
764
765     if (This->pUnkOuter && This->bUnkOuterValid)
766         return IUnknown_Release(This->pUnkOuter);
767     return IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
768 }
769
770 /** IMediaFilter methods **/
771
772 static HRESULT WINAPI VideoRenderer_Stop(IBaseFilter * iface)
773 {
774     VideoRendererImpl *This = (VideoRendererImpl *)iface;
775
776     TRACE("(%p/%p)->()\n", This, iface);
777
778     EnterCriticalSection(&This->filter.csFilter);
779     {
780         This->filter.state = State_Stopped;
781         SetEvent(This->hEvent);
782         SetEvent(This->blocked);
783         MediaSeekingPassThru_ResetMediaTime(This->seekthru_unk);
784         if (This->AutoShow)
785             /* Black it out */
786             RedrawWindow(This->hWnd, NULL, NULL, RDW_INVALIDATE|RDW_ERASE);
787     }
788     LeaveCriticalSection(&This->filter.csFilter);
789
790     return S_OK;
791 }
792
793 static HRESULT WINAPI VideoRenderer_Pause(IBaseFilter * iface)
794 {
795     VideoRendererImpl *This = (VideoRendererImpl *)iface;
796     
797     TRACE("(%p/%p)->()\n", This, iface);
798
799     EnterCriticalSection(&This->filter.csFilter);
800     if (This->filter.state != State_Paused)
801     {
802         if (This->filter.state == State_Stopped)
803         {
804             This->pInputPin->end_of_stream = 0;
805             ResetEvent(This->hEvent);
806             VideoRenderer_AutoShowWindow(This);
807         }
808
809         This->filter.state = State_Paused;
810         ResetEvent(This->blocked);
811     }
812     LeaveCriticalSection(&This->filter.csFilter);
813
814     return S_OK;
815 }
816
817 static HRESULT WINAPI VideoRenderer_SetSyncSource(IBaseFilter *iface, IReferenceClock *clock) {
818     VideoRendererImpl *This = (VideoRendererImpl *)iface;
819     HRESULT hr;
820
821     EnterCriticalSection(&This->filter.csFilter);
822     QualityControlRender_SetClock(&This->qcimpl, clock);
823     hr = BaseFilterImpl_SetSyncSource(iface, clock);
824     LeaveCriticalSection(&This->filter.csFilter);
825     return hr;
826 }
827
828 static HRESULT WINAPI VideoRenderer_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
829 {
830     HRESULT hr = S_OK;
831     VideoRendererImpl *This = (VideoRendererImpl *)iface;
832
833     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
834
835     EnterCriticalSection(&This->filter.csFilter);
836     This->filter.rtStreamStart = tStart;
837     if (This->filter.state == State_Running)
838         goto out;
839     QualityControlRender_Start(&This->qcimpl, tStart);
840     if (This->pInputPin->pin.pConnectedTo && (This->filter.state == State_Stopped || !This->pInputPin->end_of_stream))
841     {
842         if (This->filter.state == State_Stopped)
843         {
844             ResetEvent(This->hEvent);
845             VideoRenderer_AutoShowWindow(This);
846             This->pInputPin->end_of_stream = 0;
847         }
848         SetEvent(This->blocked);
849     } else if (This->filter.filterInfo.pGraph) {
850         IMediaEventSink *pEventSink;
851         hr = IFilterGraph_QueryInterface(This->filter.filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
852         if (SUCCEEDED(hr))
853         {
854             hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, (LONG_PTR)This);
855             IMediaEventSink_Release(pEventSink);
856         }
857         hr = S_OK;
858     }
859     if (SUCCEEDED(hr))
860         This->filter.state = State_Running;
861 out:
862     LeaveCriticalSection(&This->filter.csFilter);
863
864     return hr;
865 }
866
867 static HRESULT WINAPI VideoRenderer_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
868 {
869     VideoRendererImpl *This = (VideoRendererImpl *)iface;
870     HRESULT hr;
871
872     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
873
874     if (WaitForSingleObject(This->hEvent, dwMilliSecsTimeout) == WAIT_TIMEOUT)
875         hr = VFW_S_STATE_INTERMEDIATE;
876     else
877         hr = S_OK;
878
879     BaseFilterImpl_GetState(iface, dwMilliSecsTimeout, pState);
880
881     return hr;
882 }
883
884 /** IBaseFilter implementation **/
885
886 static HRESULT WINAPI VideoRenderer_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
887 {
888     VideoRendererImpl *This = (VideoRendererImpl *)iface;
889
890     FIXME("(%p/%p)->(%p,%p): stub !!!\n", This, iface, debugstr_w(Id), ppPin);
891
892     /* FIXME: critical section */
893
894     return E_NOTIMPL;
895 }
896
897 static const IBaseFilterVtbl VideoRenderer_Vtbl =
898 {
899     VideoRenderer_QueryInterface,
900     VideoRenderer_AddRef,
901     VideoRenderer_Release,
902     BaseFilterImpl_GetClassID,
903     VideoRenderer_Stop,
904     VideoRenderer_Pause,
905     VideoRenderer_Run,
906     VideoRenderer_GetState,
907     VideoRenderer_SetSyncSource,
908     BaseFilterImpl_GetSyncSource,
909     BaseFilterImpl_EnumPins,
910     VideoRenderer_FindPin,
911     BaseFilterImpl_QueryFilterInfo,
912     BaseFilterImpl_JoinFilterGraph,
913     BaseFilterImpl_QueryVendorInfo
914 };
915
916 static HRESULT WINAPI VideoRenderer_InputPin_EndOfStream(IPin * iface)
917 {
918     BaseInputPin* This = (BaseInputPin*)iface;
919     VideoRendererImpl *pFilter;
920     IMediaEventSink* pEventSink;
921     HRESULT hr = S_OK;
922
923     TRACE("(%p/%p)->()\n", This, iface);
924
925     EnterCriticalSection(This->pin.pCritSec);
926     pFilter = (VideoRendererImpl*)This->pin.pinInfo.pFilter;
927     if (This->flushing || This->end_of_stream)
928         goto out;
929     hr = IFilterGraph_QueryInterface(pFilter->filter.filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
930     if (SUCCEEDED(hr))
931     {
932         hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, (LONG_PTR)pFilter);
933         IMediaEventSink_Release(pEventSink);
934     }
935     MediaSeekingPassThru_EOS(pFilter->seekthru_unk);
936     This->end_of_stream = 1;
937 out:
938     LeaveCriticalSection(This->pin.pCritSec);
939
940     return hr;
941 }
942
943 static HRESULT WINAPI VideoRenderer_InputPin_BeginFlush(IPin * iface)
944 {
945     BaseInputPin* This = (BaseInputPin*)iface;
946     VideoRendererImpl *pVideoRenderer = (VideoRendererImpl *)This->pin.pinInfo.pFilter;
947
948     TRACE("(%p/%p)->()\n", This, iface);
949
950     SetEvent(pVideoRenderer->blocked);
951     return BaseInputPinImpl_BeginFlush(iface);
952 }
953
954 static HRESULT WINAPI VideoRenderer_InputPin_EndFlush(IPin * iface)
955 {
956     BaseInputPin* This = (BaseInputPin*)iface;
957     VideoRendererImpl *pVideoRenderer = (VideoRendererImpl *)This->pin.pinInfo.pFilter;
958     HRESULT hr;
959
960     TRACE("(%p/%p)->()\n", This, iface);
961
962     EnterCriticalSection(This->pin.pCritSec);
963
964     if (pVideoRenderer->sample_held) {
965         SetEvent(pVideoRenderer->blocked);
966         ResetEvent(pVideoRenderer->hEvent);
967         LeaveCriticalSection(This->pin.pCritSec);
968         WaitForSingleObject(pVideoRenderer->hEvent, INFINITE);
969         EnterCriticalSection(This->pin.pCritSec);
970         ResetEvent(pVideoRenderer->blocked);
971     }
972     if (pVideoRenderer->filter.state == State_Paused) {
973         ResetEvent(pVideoRenderer->blocked);
974         ResetEvent(pVideoRenderer->hEvent);
975     }
976
977     QualityControlRender_Start(&pVideoRenderer->qcimpl, pVideoRenderer->filter.rtStreamStart);
978     hr = BaseInputPinImpl_EndFlush(iface);
979     LeaveCriticalSection(This->pin.pCritSec);
980     MediaSeekingPassThru_ResetMediaTime(pVideoRenderer->seekthru_unk);
981
982     return hr;
983 }
984
985 static const IPinVtbl VideoRenderer_InputPin_Vtbl = 
986 {
987     BaseInputPinImpl_QueryInterface,
988     BasePinImpl_AddRef,
989     BaseInputPinImpl_Release,
990     BaseInputPinImpl_Connect,
991     BaseInputPinImpl_ReceiveConnection,
992     BasePinImpl_Disconnect,
993     BasePinImpl_ConnectedTo,
994     BasePinImpl_ConnectionMediaType,
995     BasePinImpl_QueryPinInfo,
996     BasePinImpl_QueryDirection,
997     BasePinImpl_QueryId,
998     BaseInputPinImpl_QueryAccept,
999     BasePinImpl_EnumMediaTypes,
1000     BasePinImpl_QueryInternalConnections,
1001     VideoRenderer_InputPin_EndOfStream,
1002     VideoRenderer_InputPin_BeginFlush,
1003     VideoRenderer_InputPin_EndFlush,
1004     BaseInputPinImpl_NewSegment
1005 };
1006
1007 /*** IUnknown methods ***/
1008 static HRESULT WINAPI Basicvideo_QueryInterface(IBasicVideo *iface,
1009                                                 REFIID riid,
1010                                                 LPVOID*ppvObj) {
1011     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1012
1013     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1014
1015     return VideoRenderer_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1016 }
1017
1018 static ULONG WINAPI Basicvideo_AddRef(IBasicVideo *iface) {
1019     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1020
1021     TRACE("(%p/%p)->()\n", This, iface);
1022
1023     return VideoRenderer_AddRef((IBaseFilter*)This);
1024 }
1025
1026 static ULONG WINAPI Basicvideo_Release(IBasicVideo *iface) {
1027     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1028
1029     TRACE("(%p/%p)->()\n", This, iface);
1030
1031     return VideoRenderer_Release((IBaseFilter*)This);
1032 }
1033
1034 /*** IDispatch methods ***/
1035 static HRESULT WINAPI Basicvideo_GetTypeInfoCount(IBasicVideo *iface,
1036                                                   UINT*pctinfo) {
1037     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1038
1039     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
1040
1041     return S_OK;
1042 }
1043
1044 static HRESULT WINAPI Basicvideo_GetTypeInfo(IBasicVideo *iface,
1045                                              UINT iTInfo,
1046                                              LCID lcid,
1047                                              ITypeInfo**ppTInfo) {
1048     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1049
1050     FIXME("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
1051
1052     return S_OK;
1053 }
1054
1055 static HRESULT WINAPI Basicvideo_GetIDsOfNames(IBasicVideo *iface,
1056                                                REFIID riid,
1057                                                LPOLESTR*rgszNames,
1058                                                UINT cNames,
1059                                                LCID lcid,
1060                                                DISPID*rgDispId) {
1061     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1062
1063     FIXME("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
1064
1065     return S_OK;
1066 }
1067
1068 static HRESULT WINAPI Basicvideo_Invoke(IBasicVideo *iface,
1069                                         DISPID dispIdMember,
1070                                         REFIID riid,
1071                                         LCID lcid,
1072                                         WORD wFlags,
1073                                         DISPPARAMS*pDispParams,
1074                                         VARIANT*pVarResult,
1075                                         EXCEPINFO*pExepInfo,
1076                                         UINT*puArgErr) {
1077     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1078
1079     FIXME("(%p/%p)->(%d, %s (%p), %d, %04x, %p, %p, %p, %p): stub !!!\n", This, iface, dispIdMember, debugstr_guid(riid), riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
1080
1081     return S_OK;
1082 }
1083
1084 /*** IBasicVideo methods ***/
1085 static HRESULT WINAPI Basicvideo_get_AvgTimePerFrame(IBasicVideo *iface,
1086                                                      REFTIME *pAvgTimePerFrame) {
1087     AM_MEDIA_TYPE *pmt;
1088     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1089
1090     if (!This->pInputPin->pin.pConnectedTo)
1091         return VFW_E_NOT_CONNECTED;
1092
1093     TRACE("(%p/%p)->(%p)\n", This, iface, pAvgTimePerFrame);
1094
1095     pmt = &This->pInputPin->pin.mtCurrent;
1096     if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo)) {
1097         VIDEOINFOHEADER *vih = (VIDEOINFOHEADER*)pmt->pbFormat;
1098         *pAvgTimePerFrame = vih->AvgTimePerFrame;
1099     } else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2)) {
1100         VIDEOINFOHEADER2 *vih = (VIDEOINFOHEADER2*)pmt->pbFormat;
1101         *pAvgTimePerFrame = vih->AvgTimePerFrame;
1102     } else {
1103         ERR("Unknown format type %s\n", qzdebugstr_guid(&pmt->formattype));
1104         *pAvgTimePerFrame = 0;
1105     }
1106     return S_OK;
1107 }
1108
1109 static HRESULT WINAPI Basicvideo_get_BitRate(IBasicVideo *iface,
1110                                              LONG *pBitRate) {
1111     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1112
1113     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, pBitRate);
1114
1115     return S_OK;
1116 }
1117
1118 static HRESULT WINAPI Basicvideo_get_BitErrorRate(IBasicVideo *iface,
1119                                                   LONG *pBitErrorRate) {
1120     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1121
1122     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, pBitErrorRate);
1123
1124     return S_OK;
1125 }
1126
1127 static HRESULT WINAPI Basicvideo_get_VideoWidth(IBasicVideo *iface,
1128                                                 LONG *pVideoWidth) {
1129     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1130
1131     TRACE("(%p/%p)->(%p)\n", This, iface, pVideoWidth);
1132
1133     *pVideoWidth = This->VideoWidth;
1134
1135     return S_OK;
1136 }
1137
1138 static HRESULT WINAPI Basicvideo_get_VideoHeight(IBasicVideo *iface,
1139                                                  LONG *pVideoHeight) {
1140     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1141
1142     TRACE("(%p/%p)->(%p)\n", This, iface, pVideoHeight);
1143
1144     *pVideoHeight = This->VideoHeight;
1145
1146     return S_OK;
1147 }
1148
1149 static HRESULT WINAPI Basicvideo_put_SourceLeft(IBasicVideo *iface,
1150                                                 LONG SourceLeft) {
1151     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1152
1153     TRACE("(%p/%p)->(%d)\n", This, iface, SourceLeft);
1154
1155     This->SourceRect.left = SourceLeft;
1156
1157     return S_OK;
1158 }
1159
1160 static HRESULT WINAPI Basicvideo_get_SourceLeft(IBasicVideo *iface,
1161                                                 LONG *pSourceLeft) {
1162     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1163
1164     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceLeft);
1165
1166     *pSourceLeft = This->SourceRect.left;
1167
1168     return S_OK;
1169 }
1170
1171 static HRESULT WINAPI Basicvideo_put_SourceWidth(IBasicVideo *iface,
1172                                                  LONG SourceWidth) {
1173     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1174
1175     TRACE("(%p/%p)->(%d)\n", This, iface, SourceWidth);
1176
1177     This->SourceRect.right = This->SourceRect.left + SourceWidth;
1178
1179     return S_OK;
1180 }
1181
1182 static HRESULT WINAPI Basicvideo_get_SourceWidth(IBasicVideo *iface,
1183                                                  LONG *pSourceWidth) {
1184     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1185
1186     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceWidth);
1187
1188     *pSourceWidth = This->SourceRect.right - This->SourceRect.left;
1189     
1190     return S_OK;
1191 }
1192
1193 static HRESULT WINAPI Basicvideo_put_SourceTop(IBasicVideo *iface,
1194                                                LONG SourceTop) {
1195     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1196
1197     TRACE("(%p/%p)->(%d)\n", This, iface, SourceTop);
1198
1199     This->SourceRect.top = SourceTop;
1200
1201     return S_OK;
1202 }
1203
1204 static HRESULT WINAPI Basicvideo_get_SourceTop(IBasicVideo *iface,
1205                                                LONG *pSourceTop) {
1206     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1207
1208     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceTop);
1209
1210     *pSourceTop = This->SourceRect.top;
1211
1212     return S_OK;
1213 }
1214
1215 static HRESULT WINAPI Basicvideo_put_SourceHeight(IBasicVideo *iface,
1216                                                   LONG SourceHeight) {
1217     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1218
1219     TRACE("(%p/%p)->(%d)\n", This, iface, SourceHeight);
1220
1221     This->SourceRect.bottom = This->SourceRect.top + SourceHeight;
1222
1223     return S_OK;
1224 }
1225
1226 static HRESULT WINAPI Basicvideo_get_SourceHeight(IBasicVideo *iface,
1227                                                   LONG *pSourceHeight) {
1228     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1229
1230     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceHeight);
1231
1232     *pSourceHeight = This->SourceRect.bottom - This->SourceRect.top;
1233
1234     return S_OK;
1235 }
1236
1237 static HRESULT WINAPI Basicvideo_put_DestinationLeft(IBasicVideo *iface,
1238                                                      LONG DestinationLeft) {
1239     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1240
1241     TRACE("(%p/%p)->(%d)\n", This, iface, DestinationLeft);
1242
1243     This->DestRect.left = DestinationLeft;
1244
1245     return S_OK;
1246 }
1247
1248 static HRESULT WINAPI Basicvideo_get_DestinationLeft(IBasicVideo *iface,
1249                                                      LONG *pDestinationLeft) {
1250     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1251
1252     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationLeft);
1253
1254     *pDestinationLeft = This->DestRect.left;
1255
1256     return S_OK;
1257 }
1258
1259 static HRESULT WINAPI Basicvideo_put_DestinationWidth(IBasicVideo *iface,
1260                                                       LONG DestinationWidth) {
1261     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1262
1263     TRACE("(%p/%p)->(%d)\n", This, iface, DestinationWidth);
1264
1265     This->DestRect.right = This->DestRect.left + DestinationWidth;
1266
1267     return S_OK;
1268 }
1269
1270 static HRESULT WINAPI Basicvideo_get_DestinationWidth(IBasicVideo *iface,
1271                                                       LONG *pDestinationWidth) {
1272     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1273
1274     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationWidth);
1275
1276     *pDestinationWidth = This->DestRect.right - This->DestRect.left;
1277
1278     return S_OK;
1279 }
1280
1281 static HRESULT WINAPI Basicvideo_put_DestinationTop(IBasicVideo *iface,
1282                                                     LONG DestinationTop) {
1283     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1284
1285     TRACE("(%p/%p)->(%d)\n", This, iface, DestinationTop);
1286
1287     This->DestRect.top = DestinationTop;
1288     
1289     return S_OK;
1290 }
1291
1292 static HRESULT WINAPI Basicvideo_get_DestinationTop(IBasicVideo *iface,
1293                                                     LONG *pDestinationTop) {
1294     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1295
1296     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationTop);
1297
1298     *pDestinationTop = This->DestRect.top;
1299
1300     return S_OK;
1301 }
1302
1303 static HRESULT WINAPI Basicvideo_put_DestinationHeight(IBasicVideo *iface,
1304                                                        LONG DestinationHeight) {
1305     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1306
1307     TRACE("(%p/%p)->(%d)\n", This, iface, DestinationHeight);
1308
1309     This->DestRect.right = This->DestRect.left + DestinationHeight;
1310
1311     return S_OK;
1312 }
1313
1314 static HRESULT WINAPI Basicvideo_get_DestinationHeight(IBasicVideo *iface,
1315                                                        LONG *pDestinationHeight) {
1316     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1317
1318     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationHeight);
1319
1320     *pDestinationHeight = This->DestRect.right - This->DestRect.left;
1321
1322     return S_OK;
1323 }
1324
1325 static HRESULT WINAPI Basicvideo_SetSourcePosition(IBasicVideo *iface,
1326                                                    LONG Left,
1327                                                    LONG Top,
1328                                                    LONG Width,
1329                                                    LONG Height) {
1330     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1331
1332     TRACE("(%p/%p)->(%d, %d, %d, %d)\n", This, iface, Left, Top, Width, Height);
1333
1334     This->SourceRect.left = Left;
1335     This->SourceRect.top = Top;
1336     This->SourceRect.right = Left + Width;
1337     This->SourceRect.bottom = Top + Height;
1338     
1339     return S_OK;
1340 }
1341
1342 static HRESULT WINAPI Basicvideo_GetSourcePosition(IBasicVideo *iface,
1343                                                    LONG *pLeft,
1344                                                    LONG *pTop,
1345                                                    LONG *pWidth,
1346                                                    LONG *pHeight) {
1347     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1348
1349     TRACE("(%p/%p)->(%p, %p, %p, %p)\n", This, iface, pLeft, pTop, pWidth, pHeight);
1350
1351     *pLeft = This->SourceRect.left;
1352     *pTop = This->SourceRect.top;
1353     *pWidth = This->SourceRect.right - This->SourceRect.left;
1354     *pHeight = This->SourceRect.bottom - This->SourceRect.top;
1355     
1356     return S_OK;
1357 }
1358
1359 static HRESULT WINAPI Basicvideo_SetDefaultSourcePosition(IBasicVideo *iface) {
1360     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1361
1362     TRACE("(%p/%p)->()\n", This, iface);
1363
1364     This->SourceRect.left = 0;
1365     This->SourceRect.top = 0;
1366     This->SourceRect.right = This->VideoWidth;
1367     This->SourceRect.bottom = This->VideoHeight;
1368
1369     return S_OK;
1370 }
1371
1372 static HRESULT WINAPI Basicvideo_SetDestinationPosition(IBasicVideo *iface,
1373                                                         LONG Left,
1374                                                         LONG Top,
1375                                                         LONG Width,
1376                                                         LONG Height) {
1377     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1378
1379     TRACE("(%p/%p)->(%d, %d, %d, %d)\n", This, iface, Left, Top, Width, Height);
1380
1381     This->DestRect.left = Left;
1382     This->DestRect.top = Top;
1383     This->DestRect.right = Left + Width;
1384     This->DestRect.bottom = Top + Height;
1385
1386     return S_OK;
1387 }
1388
1389 static HRESULT WINAPI Basicvideo_GetDestinationPosition(IBasicVideo *iface,
1390                                                         LONG *pLeft,
1391                                                         LONG *pTop,
1392                                                         LONG *pWidth,
1393                                                         LONG *pHeight) {
1394     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1395
1396     TRACE("(%p/%p)->(%p, %p, %p, %p)\n", This, iface, pLeft, pTop, pWidth, pHeight);
1397
1398     *pLeft = This->DestRect.left;
1399     *pTop = This->DestRect.top;
1400     *pWidth = This->DestRect.right - This->DestRect.left;
1401     *pHeight = This->DestRect.bottom - This->DestRect.top;
1402
1403     return S_OK;
1404 }
1405
1406 static HRESULT WINAPI Basicvideo_SetDefaultDestinationPosition(IBasicVideo *iface) {
1407     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1408     RECT rect;
1409
1410     TRACE("(%p/%p)->()\n", This, iface);
1411
1412     if (!GetClientRect(This->hWnd, &rect))
1413         return E_FAIL;
1414     
1415     This->SourceRect.left = 0;
1416     This->SourceRect.top = 0;
1417     This->SourceRect.right = rect.right;
1418     This->SourceRect.bottom = rect.bottom;
1419     
1420     return S_OK;
1421 }
1422
1423 static HRESULT WINAPI Basicvideo_GetVideoSize(IBasicVideo *iface,
1424                                               LONG *pWidth,
1425                                               LONG *pHeight) {
1426     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1427
1428     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pWidth, pHeight);
1429
1430     *pWidth = This->VideoWidth;
1431     *pHeight = This->VideoHeight;
1432     
1433     return S_OK;
1434 }
1435
1436 static HRESULT WINAPI Basicvideo_GetVideoPaletteEntries(IBasicVideo *iface,
1437                                                         LONG StartIndex,
1438                                                         LONG Entries,
1439                                                         LONG *pRetrieved,
1440                                                         LONG *pPalette) {
1441     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1442
1443     TRACE("(%p/%p)->(%d, %d, %p, %p)\n", This, iface, StartIndex, Entries, pRetrieved, pPalette);
1444
1445     if (pRetrieved)
1446         *pRetrieved = 0;
1447     return VFW_E_NO_PALETTE_AVAILABLE;
1448 }
1449
1450 static HRESULT WINAPI Basicvideo_GetCurrentImage(IBasicVideo *iface,
1451                                                  LONG *pBufferSize,
1452                                                  LONG *pDIBImage) {
1453     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1454     BITMAPINFOHEADER *bmiHeader;
1455     LONG needed_size;
1456     AM_MEDIA_TYPE *amt = &This->pInputPin->pin.mtCurrent;
1457     char *ptr;
1458
1459     FIXME("(%p/%p)->(%p, %p): partial stub\n", This, iface, pBufferSize, pDIBImage);
1460
1461     EnterCriticalSection(&This->filter.csFilter);
1462
1463     if (!This->sample_held)
1464     {
1465          LeaveCriticalSection(&This->filter.csFilter);
1466          return (This->filter.state == State_Paused ? E_UNEXPECTED : VFW_E_NOT_PAUSED);
1467     }
1468
1469     if (IsEqualIID(&amt->formattype, &FORMAT_VideoInfo))
1470     {
1471         bmiHeader = &((VIDEOINFOHEADER *)amt->pbFormat)->bmiHeader;
1472     }
1473     else if (IsEqualIID(&amt->formattype, &FORMAT_VideoInfo2))
1474     {
1475         bmiHeader = &((VIDEOINFOHEADER2 *)amt->pbFormat)->bmiHeader;
1476     }
1477     else
1478     {
1479         FIXME("Unknown type %s\n", debugstr_guid(&amt->subtype));
1480         LeaveCriticalSection(&This->filter.csFilter);
1481         return VFW_E_RUNTIME_ERROR;
1482     }
1483
1484     needed_size = bmiHeader->biSize;
1485     needed_size += IMediaSample_GetActualDataLength(This->sample_held);
1486
1487     if (!pDIBImage)
1488     {
1489         *pBufferSize = needed_size;
1490         LeaveCriticalSection(&This->filter.csFilter);
1491         return S_OK;
1492     }
1493
1494     if (needed_size < *pBufferSize)
1495     {
1496         ERR("Buffer too small %u/%u\n", needed_size, *pBufferSize);
1497         LeaveCriticalSection(&This->filter.csFilter);
1498         return E_FAIL;
1499     }
1500     *pBufferSize = needed_size;
1501
1502     memcpy(pDIBImage, bmiHeader, bmiHeader->biSize);
1503     IMediaSample_GetPointer(This->sample_held, (BYTE **)&ptr);
1504     memcpy((char *)pDIBImage + bmiHeader->biSize, ptr, IMediaSample_GetActualDataLength(This->sample_held));
1505
1506     LeaveCriticalSection(&This->filter.csFilter);
1507
1508     return S_OK;
1509 }
1510
1511 static HRESULT WINAPI Basicvideo_IsUsingDefaultSource(IBasicVideo *iface) {
1512     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1513
1514     FIXME("(%p/%p)->(): stub !!!\n", This, iface);
1515
1516     return S_OK;
1517 }
1518
1519 static HRESULT WINAPI Basicvideo_IsUsingDefaultDestination(IBasicVideo *iface) {
1520     ICOM_THIS_MULTI(VideoRendererImpl, IBasicVideo_vtbl, iface);
1521
1522     FIXME("(%p/%p)->(): stub !!!\n", This, iface);
1523
1524     return S_OK;
1525 }
1526
1527
1528 static const IBasicVideoVtbl IBasicVideo_VTable =
1529 {
1530     Basicvideo_QueryInterface,
1531     Basicvideo_AddRef,
1532     Basicvideo_Release,
1533     Basicvideo_GetTypeInfoCount,
1534     Basicvideo_GetTypeInfo,
1535     Basicvideo_GetIDsOfNames,
1536     Basicvideo_Invoke,
1537     Basicvideo_get_AvgTimePerFrame,
1538     Basicvideo_get_BitRate,
1539     Basicvideo_get_BitErrorRate,
1540     Basicvideo_get_VideoWidth,
1541     Basicvideo_get_VideoHeight,
1542     Basicvideo_put_SourceLeft,
1543     Basicvideo_get_SourceLeft,
1544     Basicvideo_put_SourceWidth,
1545     Basicvideo_get_SourceWidth,
1546     Basicvideo_put_SourceTop,
1547     Basicvideo_get_SourceTop,
1548     Basicvideo_put_SourceHeight,
1549     Basicvideo_get_SourceHeight,
1550     Basicvideo_put_DestinationLeft,
1551     Basicvideo_get_DestinationLeft,
1552     Basicvideo_put_DestinationWidth,
1553     Basicvideo_get_DestinationWidth,
1554     Basicvideo_put_DestinationTop,
1555     Basicvideo_get_DestinationTop,
1556     Basicvideo_put_DestinationHeight,
1557     Basicvideo_get_DestinationHeight,
1558     Basicvideo_SetSourcePosition,
1559     Basicvideo_GetSourcePosition,
1560     Basicvideo_SetDefaultSourcePosition,
1561     Basicvideo_SetDestinationPosition,
1562     Basicvideo_GetDestinationPosition,
1563     Basicvideo_SetDefaultDestinationPosition,
1564     Basicvideo_GetVideoSize,
1565     Basicvideo_GetVideoPaletteEntries,
1566     Basicvideo_GetCurrentImage,
1567     Basicvideo_IsUsingDefaultSource,
1568     Basicvideo_IsUsingDefaultDestination
1569 };
1570
1571
1572 /*** IUnknown methods ***/
1573 static HRESULT WINAPI Videowindow_QueryInterface(IVideoWindow *iface,
1574                                                  REFIID riid,
1575                                                  LPVOID*ppvObj) {
1576     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1577
1578     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1579
1580     return VideoRenderer_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1581 }
1582
1583 static ULONG WINAPI Videowindow_AddRef(IVideoWindow *iface) {
1584     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1585
1586     TRACE("(%p/%p)->()\n", This, iface);
1587
1588     return VideoRenderer_AddRef((IBaseFilter*)This);
1589 }
1590
1591 static ULONG WINAPI Videowindow_Release(IVideoWindow *iface) {
1592     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1593
1594     TRACE("(%p/%p)->()\n", This, iface);
1595
1596     return VideoRenderer_Release((IBaseFilter*)This);
1597 }
1598
1599 /*** IDispatch methods ***/
1600 static HRESULT WINAPI Videowindow_GetTypeInfoCount(IVideoWindow *iface,
1601                                                    UINT*pctinfo) {
1602     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1603
1604     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
1605
1606     return S_OK;
1607 }
1608
1609 static HRESULT WINAPI Videowindow_GetTypeInfo(IVideoWindow *iface,
1610                                               UINT iTInfo,
1611                                               LCID lcid,
1612                                               ITypeInfo**ppTInfo) {
1613     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1614
1615     FIXME("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
1616
1617     return S_OK;
1618 }
1619
1620 static HRESULT WINAPI Videowindow_GetIDsOfNames(IVideoWindow *iface,
1621                                                 REFIID riid,
1622                                                 LPOLESTR*rgszNames,
1623                                                 UINT cNames,
1624                                                 LCID lcid,
1625                                                 DISPID*rgDispId) {
1626     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1627
1628     FIXME("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
1629
1630     return S_OK;
1631 }
1632
1633 static HRESULT WINAPI Videowindow_Invoke(IVideoWindow *iface,
1634                                          DISPID dispIdMember,
1635                                          REFIID riid,
1636                                          LCID lcid,
1637                                          WORD wFlags,
1638                                          DISPPARAMS*pDispParams,
1639                                          VARIANT*pVarResult,
1640                                          EXCEPINFO*pExepInfo,
1641                                          UINT*puArgErr) {
1642     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1643
1644     FIXME("(%p/%p)->(%d, %s (%p), %d, %04x, %p, %p, %p, %p): stub !!!\n", This, iface, dispIdMember, debugstr_guid(riid), riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
1645
1646     return S_OK;
1647 }
1648
1649 /*** IVideoWindow methods ***/
1650 static HRESULT WINAPI Videowindow_put_Caption(IVideoWindow *iface,
1651                                               BSTR strCaption) {
1652     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1653
1654     TRACE("(%p/%p)->(%s (%p))\n", This, iface, debugstr_w(strCaption), strCaption);
1655
1656     if (!SetWindowTextW(This->hWnd, strCaption))
1657         return E_FAIL;
1658
1659     return S_OK;
1660 }
1661
1662 static HRESULT WINAPI Videowindow_get_Caption(IVideoWindow *iface,
1663                                               BSTR *strCaption) {
1664     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1665
1666     TRACE("(%p/%p)->(%p)\n", This, iface, strCaption);
1667
1668     GetWindowTextW(This->hWnd, (LPWSTR)strCaption, 100);
1669
1670     return S_OK;
1671 }
1672
1673 static HRESULT WINAPI Videowindow_put_WindowStyle(IVideoWindow *iface,
1674                                                   LONG WindowStyle) {
1675     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1676     LONG old;
1677
1678     old = GetWindowLongW(This->hWnd, GWL_STYLE);
1679
1680     TRACE("(%p/%p)->(%x -> %x)\n", This, iface, old, WindowStyle);
1681
1682     if (WindowStyle & (WS_DISABLED|WS_HSCROLL|WS_ICONIC|WS_MAXIMIZE|WS_MINIMIZE|WS_VSCROLL))
1683         return E_INVALIDARG;
1684
1685     SetWindowLongW(This->hWnd, GWL_STYLE, WindowStyle);
1686     SetWindowPos(This->hWnd,0,0,0,0,0,SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOZORDER);
1687     This->WindowStyle = WindowStyle;
1688
1689     return S_OK;
1690 }
1691
1692 static HRESULT WINAPI Videowindow_get_WindowStyle(IVideoWindow *iface,
1693                                                   LONG *WindowStyle) {
1694     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1695
1696     TRACE("(%p/%p)->(%p)\n", This, iface, WindowStyle);
1697
1698     *WindowStyle = This->WindowStyle;
1699
1700     return S_OK;
1701 }
1702
1703 static HRESULT WINAPI Videowindow_put_WindowStyleEx(IVideoWindow *iface,
1704                                                     LONG WindowStyleEx) {
1705     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1706
1707     TRACE("(%p/%p)->(%d)\n", This, iface, WindowStyleEx);
1708
1709     if (!SetWindowLongW(This->hWnd, GWL_EXSTYLE, WindowStyleEx))
1710         return E_FAIL;
1711
1712     return S_OK;
1713 }
1714
1715 static HRESULT WINAPI Videowindow_get_WindowStyleEx(IVideoWindow *iface,
1716                                                     LONG *WindowStyleEx) {
1717     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1718
1719     TRACE("(%p/%p)->(%p)\n", This, iface, WindowStyleEx);
1720
1721     *WindowStyleEx = GetWindowLongW(This->hWnd, GWL_EXSTYLE);
1722
1723     return S_OK;
1724 }
1725
1726 static HRESULT WINAPI Videowindow_put_AutoShow(IVideoWindow *iface,
1727                                                LONG AutoShow) {
1728     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1729
1730     TRACE("(%p/%p)->(%d)\n", This, iface, AutoShow);
1731
1732     This->AutoShow = AutoShow;
1733
1734     return S_OK;
1735 }
1736
1737 static HRESULT WINAPI Videowindow_get_AutoShow(IVideoWindow *iface,
1738                                                LONG *AutoShow) {
1739     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1740
1741     TRACE("(%p/%p)->(%p)\n", This, iface, AutoShow);
1742
1743     *AutoShow = This->AutoShow;
1744
1745     return S_OK;
1746 }
1747
1748 static HRESULT WINAPI Videowindow_put_WindowState(IVideoWindow *iface,
1749                                                   LONG WindowState) {
1750     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1751
1752     TRACE("(%p/%p)->(%d)\n", This, iface, WindowState);
1753     ShowWindow(This->hWnd, WindowState);
1754     return S_OK;
1755 }
1756
1757 static HRESULT WINAPI Videowindow_get_WindowState(IVideoWindow *iface,
1758                                                   LONG *WindowState) {
1759     WINDOWPLACEMENT place;
1760     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1761
1762     place.length = sizeof(place);
1763     GetWindowPlacement(This->hWnd, &place);
1764     TRACE("(%p/%p)->(%p)\n", This, iface, WindowState);
1765     *WindowState = place.showCmd;
1766
1767     return S_OK;
1768 }
1769
1770 static HRESULT WINAPI Videowindow_put_BackgroundPalette(IVideoWindow *iface,
1771                                                         LONG BackgroundPalette) {
1772     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1773
1774     FIXME("(%p/%p)->(%d): stub !!!\n", This, iface, BackgroundPalette);
1775
1776     return S_OK;
1777 }
1778
1779 static HRESULT WINAPI Videowindow_get_BackgroundPalette(IVideoWindow *iface,
1780                                                         LONG *pBackgroundPalette) {
1781     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1782
1783     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, pBackgroundPalette);
1784
1785     return S_OK;
1786 }
1787
1788 static HRESULT WINAPI Videowindow_put_Visible(IVideoWindow *iface,
1789                                               LONG Visible) {
1790     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1791
1792     TRACE("(%p/%p)->(%d)\n", This, iface, Visible);
1793
1794     ShowWindow(This->hWnd, Visible ? SW_SHOW : SW_HIDE);
1795
1796     return S_OK;
1797 }
1798
1799 static HRESULT WINAPI Videowindow_get_Visible(IVideoWindow *iface,
1800                                               LONG *pVisible) {
1801     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1802
1803     TRACE("(%p/%p)->(%p)\n", This, iface, pVisible);
1804
1805     *pVisible = IsWindowVisible(This->hWnd);
1806
1807     return S_OK;
1808 }
1809
1810 static HRESULT WINAPI Videowindow_put_Left(IVideoWindow *iface,
1811                                            LONG Left) {
1812     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1813
1814     TRACE("(%p/%p)->(%d)\n", This, iface, Left);
1815
1816     if (!SetWindowPos(This->hWnd, NULL, Left, This->WindowPos.top, 0, 0, SWP_NOZORDER|SWP_NOSIZE))
1817         return E_FAIL;
1818
1819     This->WindowPos.left = Left;
1820
1821     return S_OK;
1822 }
1823
1824 static HRESULT WINAPI Videowindow_get_Left(IVideoWindow *iface,
1825                                            LONG *pLeft) {
1826     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1827
1828     TRACE("(%p/%p)->(%p)\n", This, iface, pLeft);
1829
1830     *pLeft = This->WindowPos.left;
1831
1832     return S_OK;
1833 }
1834
1835 static HRESULT WINAPI Videowindow_put_Width(IVideoWindow *iface,
1836                                             LONG Width) {
1837     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1838
1839     TRACE("(%p/%p)->(%d)\n", This, iface, Width);
1840
1841     if (!SetWindowPos(This->hWnd, NULL, 0, 0, Width, This->WindowPos.bottom-This->WindowPos.top, SWP_NOZORDER|SWP_NOMOVE))
1842         return E_FAIL;
1843
1844     This->WindowPos.right = This->WindowPos.left + Width;
1845
1846     return S_OK;
1847 }
1848
1849 static HRESULT WINAPI Videowindow_get_Width(IVideoWindow *iface,
1850                                             LONG *pWidth) {
1851     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1852
1853     TRACE("(%p/%p)->(%p)\n", This, iface, pWidth);
1854
1855     *pWidth = This->WindowPos.right - This->WindowPos.left;
1856
1857     return S_OK;
1858 }
1859
1860 static HRESULT WINAPI Videowindow_put_Top(IVideoWindow *iface,
1861                                           LONG Top) {
1862     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1863
1864     TRACE("(%p/%p)->(%d)\n", This, iface, Top);
1865
1866     if (!SetWindowPos(This->hWnd, NULL, This->WindowPos.left, Top, 0, 0, SWP_NOZORDER|SWP_NOSIZE))
1867         return E_FAIL;
1868
1869     This->WindowPos.top = Top;
1870
1871     return S_OK;
1872 }
1873
1874 static HRESULT WINAPI Videowindow_get_Top(IVideoWindow *iface,
1875                                           LONG *pTop) {
1876     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1877
1878     TRACE("(%p/%p)->(%p)\n", This, iface, pTop);
1879
1880     *pTop = This->WindowPos.top;
1881
1882     return S_OK;
1883 }
1884
1885 static HRESULT WINAPI Videowindow_put_Height(IVideoWindow *iface,
1886                                              LONG Height) {
1887     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1888
1889     TRACE("(%p/%p)->(%d)\n", This, iface, Height);
1890
1891     if (!SetWindowPos(This->hWnd, NULL, 0, 0, This->WindowPos.right-This->WindowPos.left, Height, SWP_NOZORDER|SWP_NOMOVE))
1892         return E_FAIL;
1893
1894     This->WindowPos.bottom = This->WindowPos.top + Height;
1895
1896     return S_OK;
1897 }
1898
1899 static HRESULT WINAPI Videowindow_get_Height(IVideoWindow *iface,
1900                                              LONG *pHeight) {
1901     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1902
1903     TRACE("(%p/%p)->(%p)\n", This, iface, pHeight);
1904
1905     *pHeight = This->WindowPos.bottom - This->WindowPos.top;
1906
1907     return S_OK;
1908 }
1909
1910 static HRESULT WINAPI Videowindow_put_Owner(IVideoWindow *iface,
1911                                             OAHWND Owner) {
1912     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1913
1914     TRACE("(%p/%p)->(%08x)\n", This, iface, (DWORD) Owner);
1915
1916     This->hWndOwner = (HWND)Owner;
1917     SetParent(This->hWnd, This->hWndOwner);
1918     if (This->WindowStyle & WS_CHILD)
1919     {
1920         LONG old = GetWindowLongW(This->hWnd, GWL_STYLE);
1921         if (old != This->WindowStyle)
1922         {
1923             SetWindowLongW(This->hWnd, GWL_STYLE, This->WindowStyle);
1924             SetWindowPos(This->hWnd,0,0,0,0,0,SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOZORDER);
1925         }
1926     }
1927
1928     return S_OK;
1929 }
1930
1931 static HRESULT WINAPI Videowindow_get_Owner(IVideoWindow *iface,
1932                                             OAHWND *Owner) {
1933     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1934
1935     TRACE("(%p/%p)->(%p)\n", This, iface, Owner);
1936
1937     *(HWND*)Owner = This->hWndOwner;
1938
1939     return S_OK;
1940 }
1941
1942 static HRESULT WINAPI Videowindow_put_MessageDrain(IVideoWindow *iface,
1943                                                    OAHWND Drain) {
1944     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1945
1946     TRACE("(%p/%p)->(%08x)\n", This, iface, (DWORD) Drain);
1947
1948     This->hWndMsgDrain = (HWND)Drain;
1949
1950     return S_OK;
1951 }
1952
1953 static HRESULT WINAPI Videowindow_get_MessageDrain(IVideoWindow *iface,
1954                                                    OAHWND *Drain) {
1955     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1956
1957     TRACE("(%p/%p)->(%p)\n", This, iface, Drain);
1958
1959     *Drain = (OAHWND)This->hWndMsgDrain;
1960
1961     return S_OK;
1962 }
1963
1964 static HRESULT WINAPI Videowindow_get_BorderColor(IVideoWindow *iface,
1965                                                   LONG *Color) {
1966     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1967
1968     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, Color);
1969
1970     return S_OK;
1971 }
1972
1973 static HRESULT WINAPI Videowindow_put_BorderColor(IVideoWindow *iface,
1974                                                   LONG Color) {
1975     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1976
1977     FIXME("(%p/%p)->(%d): stub !!!\n", This, iface, Color);
1978
1979     return S_OK;
1980 }
1981
1982 static HRESULT WINAPI Videowindow_get_FullScreenMode(IVideoWindow *iface,
1983                                                      LONG *FullScreenMode) {
1984     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1985
1986     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, FullScreenMode);
1987
1988     return S_OK;
1989 }
1990
1991 static HRESULT WINAPI Videowindow_put_FullScreenMode(IVideoWindow *iface,
1992                                                      LONG FullScreenMode) {
1993     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
1994
1995     FIXME("(%p/%p)->(%d): stub !!!\n", This, iface, FullScreenMode);
1996
1997     if (FullScreenMode) {
1998         ShowWindow(This->hWnd, SW_HIDE);
1999         SetParent(This->hWnd, 0);
2000         SetWindowLongW(This->hWnd, GWL_STYLE, WS_POPUP);
2001         SetWindowPos(This->hWnd,HWND_TOP,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),SWP_SHOWWINDOW);
2002         GetWindowRect(This->hWnd, &This->DestRect);
2003         This->WindowPos = This->DestRect;
2004     } else {
2005         ShowWindow(This->hWnd, SW_HIDE);
2006         SetParent(This->hWnd, This->hWndOwner);
2007         SetWindowLongW(This->hWnd, GWL_STYLE, This->WindowStyle);
2008         GetClientRect(This->hWnd, &This->DestRect);
2009         SetWindowPos(This->hWnd,0,This->DestRect.left,This->DestRect.top,This->DestRect.right,This->DestRect.bottom,SWP_NOZORDER|SWP_SHOWWINDOW);
2010         This->WindowPos = This->DestRect;
2011     }
2012
2013     return S_OK;
2014 }
2015
2016 static HRESULT WINAPI Videowindow_SetWindowForeground(IVideoWindow *iface,
2017                                                       LONG Focus) {
2018     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
2019     BOOL ret;
2020     IPin* pPin;
2021     HRESULT hr;
2022
2023     TRACE("(%p/%p)->(%d)\n", This, iface, Focus);
2024
2025     if ((Focus != FALSE) && (Focus != TRUE))
2026         return E_INVALIDARG;
2027
2028     hr = IPin_ConnectedTo((IPin *)This->pInputPin, &pPin);
2029     if ((hr != S_OK) || !pPin)
2030         return VFW_E_NOT_CONNECTED;
2031
2032     if (Focus)
2033         ret = SetForegroundWindow(This->hWnd);
2034     else
2035         ret = SetWindowPos(This->hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
2036
2037     if (!ret)
2038         return E_FAIL;
2039
2040     return S_OK;
2041 }
2042
2043 static HRESULT WINAPI Videowindow_NotifyOwnerMessage(IVideoWindow *iface,
2044                                                      OAHWND hwnd,
2045                                                      LONG uMsg,
2046                                                      LONG_PTR wParam,
2047                                                      LONG_PTR lParam) {
2048     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
2049
2050     TRACE("(%p/%p)->(%08lx, %d, %08lx, %08lx)\n", This, iface, hwnd, uMsg, wParam, lParam);
2051
2052     if (!PostMessageW(This->hWnd, uMsg, wParam, lParam))
2053         return E_FAIL;
2054     
2055     return S_OK;
2056 }
2057
2058 static HRESULT WINAPI Videowindow_SetWindowPosition(IVideoWindow *iface,
2059                                                     LONG Left,
2060                                                     LONG Top,
2061                                                     LONG Width,
2062                                                     LONG Height) {
2063     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
2064
2065     TRACE("(%p/%p)->(%d, %d, %d, %d)\n", This, iface, Left, Top, Width, Height);
2066
2067     if (!SetWindowPos(This->hWnd, NULL, Left, Top, Width, Height, SWP_NOZORDER))
2068         return E_FAIL;
2069
2070     This->WindowPos.left = Left;
2071     This->WindowPos.top = Top;
2072     This->WindowPos.right = Left + Width;
2073     This->WindowPos.bottom = Top + Height;
2074     
2075     return S_OK;
2076 }
2077
2078 static HRESULT WINAPI Videowindow_GetWindowPosition(IVideoWindow *iface,
2079                                                     LONG *pLeft,
2080                                                     LONG *pTop,
2081                                                     LONG *pWidth,
2082                                                     LONG *pHeight) {
2083     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
2084
2085     TRACE("(%p/%p)->(%p, %p, %p, %p)\n", This, iface, pLeft, pTop, pWidth, pHeight);
2086
2087     *pLeft = This->WindowPos.left;
2088     *pTop = This->WindowPos.top;
2089     *pWidth = This->WindowPos.right - This->WindowPos.left;
2090     *pHeight = This->WindowPos.bottom - This->WindowPos.top;
2091
2092     return S_OK;
2093 }
2094
2095 static HRESULT WINAPI Videowindow_GetMinIdealImageSize(IVideoWindow *iface,
2096                                                        LONG *pWidth,
2097                                                        LONG *pHeight) {
2098     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
2099
2100     FIXME("(%p/%p)->(%p, %p): semi stub !!!\n", This, iface, pWidth, pHeight);
2101
2102     *pWidth = This->VideoWidth;
2103     *pHeight = This->VideoHeight;
2104
2105     return S_OK;
2106 }
2107
2108 static HRESULT WINAPI Videowindow_GetMaxIdealImageSize(IVideoWindow *iface,
2109                                                        LONG *pWidth,
2110                                                        LONG *pHeight) {
2111     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
2112
2113     FIXME("(%p/%p)->(%p, %p): semi stub !!!\n", This, iface, pWidth, pHeight);
2114
2115     *pWidth = This->VideoWidth;
2116     *pHeight = This->VideoHeight;
2117
2118     return S_OK;
2119 }
2120
2121 static HRESULT WINAPI Videowindow_GetRestorePosition(IVideoWindow *iface,
2122                                                      LONG *pLeft,
2123                                                      LONG *pTop,
2124                                                      LONG *pWidth,
2125                                                      LONG *pHeight) {
2126     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
2127
2128     FIXME("(%p/%p)->(%p, %p, %p, %p): stub !!!\n", This, iface, pLeft, pTop, pWidth, pHeight);
2129
2130     return S_OK;
2131 }
2132
2133 static HRESULT WINAPI Videowindow_HideCursor(IVideoWindow *iface,
2134                                              LONG HideCursor) {
2135     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
2136
2137     FIXME("(%p/%p)->(%d): stub !!!\n", This, iface, HideCursor);
2138
2139     return S_OK;
2140 }
2141
2142 static HRESULT WINAPI Videowindow_IsCursorHidden(IVideoWindow *iface,
2143                                                  LONG *CursorHidden) {
2144     ICOM_THIS_MULTI(VideoRendererImpl, IVideoWindow_vtbl, iface);
2145
2146     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, CursorHidden);
2147
2148     return S_OK;
2149 }
2150
2151 static const IVideoWindowVtbl IVideoWindow_VTable =
2152 {
2153     Videowindow_QueryInterface,
2154     Videowindow_AddRef,
2155     Videowindow_Release,
2156     Videowindow_GetTypeInfoCount,
2157     Videowindow_GetTypeInfo,
2158     Videowindow_GetIDsOfNames,
2159     Videowindow_Invoke,
2160     Videowindow_put_Caption,
2161     Videowindow_get_Caption,
2162     Videowindow_put_WindowStyle,
2163     Videowindow_get_WindowStyle,
2164     Videowindow_put_WindowStyleEx,
2165     Videowindow_get_WindowStyleEx,
2166     Videowindow_put_AutoShow,
2167     Videowindow_get_AutoShow,
2168     Videowindow_put_WindowState,
2169     Videowindow_get_WindowState,
2170     Videowindow_put_BackgroundPalette,
2171     Videowindow_get_BackgroundPalette,
2172     Videowindow_put_Visible,
2173     Videowindow_get_Visible,
2174     Videowindow_put_Left,
2175     Videowindow_get_Left,
2176     Videowindow_put_Width,
2177     Videowindow_get_Width,
2178     Videowindow_put_Top,
2179     Videowindow_get_Top,
2180     Videowindow_put_Height,
2181     Videowindow_get_Height,
2182     Videowindow_put_Owner,
2183     Videowindow_get_Owner,
2184     Videowindow_put_MessageDrain,
2185     Videowindow_get_MessageDrain,
2186     Videowindow_get_BorderColor,
2187     Videowindow_put_BorderColor,
2188     Videowindow_get_FullScreenMode,
2189     Videowindow_put_FullScreenMode,
2190     Videowindow_SetWindowForeground,
2191     Videowindow_NotifyOwnerMessage,
2192     Videowindow_SetWindowPosition,
2193     Videowindow_GetWindowPosition,
2194     Videowindow_GetMinIdealImageSize,
2195     Videowindow_GetMaxIdealImageSize,
2196     Videowindow_GetRestorePosition,
2197     Videowindow_HideCursor,
2198     Videowindow_IsCursorHidden
2199 };
2200
2201 static VideoRendererImpl *from_IAMFilterMiscFlags(IAMFilterMiscFlags *iface) {
2202     return (VideoRendererImpl*)((char*)iface - offsetof(VideoRendererImpl, IAMFilterMiscFlags_vtbl));
2203 }
2204
2205 static HRESULT WINAPI AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags *iface, REFIID riid, void **ppv) {
2206     VideoRendererImpl *This = from_IAMFilterMiscFlags(iface);
2207     return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
2208 }
2209
2210 static ULONG WINAPI AMFilterMiscFlags_AddRef(IAMFilterMiscFlags *iface) {
2211     VideoRendererImpl *This = from_IAMFilterMiscFlags(iface);
2212     return IUnknown_AddRef((IUnknown*)This);
2213 }
2214
2215 static ULONG WINAPI AMFilterMiscFlags_Release(IAMFilterMiscFlags *iface) {
2216     VideoRendererImpl *This = from_IAMFilterMiscFlags(iface);
2217     return IUnknown_Release((IUnknown*)This);
2218 }
2219
2220 static ULONG WINAPI AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags *iface) {
2221     return AM_FILTER_MISC_FLAGS_IS_RENDERER;
2222 }
2223
2224 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl = {
2225     AMFilterMiscFlags_QueryInterface,
2226     AMFilterMiscFlags_AddRef,
2227     AMFilterMiscFlags_Release,
2228     AMFilterMiscFlags_GetMiscFlags
2229 };