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