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