user32: Implement semi-stub of GetCursorFrameInfo.
[wine] / dlls / strmbase / transform.c
1 /*
2  * Transform Filter (Base for decoders, etc...)
3  *
4  * Copyright 2005 Christian Costa
5  * Copyright 2010 Aric Stewart, CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21 #include "config.h"
22 #include <stdarg.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "dshow.h"
29 #include "amvideo.h"
30 #include "strmif.h"
31 #include "vfw.h"
32
33 #include <assert.h>
34
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "wine/strmbase.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
40
41 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
42 static const WCHAR wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};
43
44 static const IBaseFilterVtbl TransformFilter_Vtbl;
45 static const IPinVtbl TransformFilter_InputPin_Vtbl;
46 static const IPinVtbl TransformFilter_OutputPin_Vtbl;
47 static const IQualityControlVtbl TransformFilter_QualityControl_Vtbl;
48
49 static HRESULT WINAPI TransformFilter_Input_CheckMediaType(BasePin *iface, const AM_MEDIA_TYPE * pmt)
50 {
51     BaseInputPin* This = (BaseInputPin*) iface;
52     TransformFilter * pTransform;
53
54     TRACE("%p\n", iface);
55     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
56
57     if (pTransform->pFuncsTable->pfnCheckInputType)
58         return pTransform->pFuncsTable->pfnCheckInputType(pTransform, pmt);
59     /* Assume OK if there's no query method (the connection will fail if
60        needed) */
61     return S_OK;
62 }
63
64 static HRESULT WINAPI TransformFilter_Input_Receive(BaseInputPin *This, IMediaSample *pInSample)
65 {
66     HRESULT hr = S_FALSE;
67     TransformFilter * pTransform;
68     TRACE("%p\n", This);
69     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
70
71     EnterCriticalSection(&pTransform->filter.csFilter);
72     if (pTransform->filter.state == State_Stopped)
73     {
74         LeaveCriticalSection(&pTransform->filter.csFilter);
75         return VFW_E_WRONG_STATE;
76     }
77
78     if (This->end_of_stream || This->flushing)
79     {
80         LeaveCriticalSection(&pTransform->filter.csFilter);
81         return S_FALSE;
82     }
83     LeaveCriticalSection(&pTransform->filter.csFilter);
84
85     if (pTransform->pFuncsTable->pfnReceive)
86         hr = pTransform->pFuncsTable->pfnReceive(pTransform, pInSample);
87     else
88         hr = S_FALSE;
89
90     return hr;
91 }
92
93 static HRESULT WINAPI TransformFilter_Output_QueryAccept(IPin *iface, const AM_MEDIA_TYPE * pmt)
94 {
95     BasePin *This = (BasePin *)iface;
96     TransformFilter *pTransformFilter = (TransformFilter *)This->pinInfo.pFilter;
97     AM_MEDIA_TYPE* outpmt = &pTransformFilter->pmt;
98     TRACE("%p\n", iface);
99
100     if (IsEqualIID(&pmt->majortype, &outpmt->majortype)
101         && (IsEqualIID(&pmt->subtype, &outpmt->subtype) || IsEqualIID(&outpmt->subtype, &GUID_NULL)))
102         return S_OK;
103     return S_FALSE;
104 }
105
106 static HRESULT WINAPI TransformFilter_Output_DecideBufferSize(BaseOutputPin *This, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
107 {
108     TransformFilter *pTransformFilter = (TransformFilter *)This->pin.pinInfo.pFilter;
109     return pTransformFilter->pFuncsTable->pfnDecideBufferSize(pTransformFilter, pAlloc, ppropInputRequest);
110 }
111
112 static HRESULT WINAPI TransformFilter_Output_GetMediaType(BasePin *This, int iPosition, AM_MEDIA_TYPE *pmt)
113 {
114     TransformFilter *pTransform = (TransformFilter *)This->pinInfo.pFilter;
115
116     if (iPosition < 0)
117         return E_INVALIDARG;
118     if (iPosition > 0)
119         return VFW_S_NO_MORE_ITEMS;
120     CopyMediaType(pmt, &pTransform->pmt);
121     return S_OK;
122 }
123
124 static IPin* WINAPI TransformFilter_GetPin(BaseFilter *iface, int pos)
125 {
126     TransformFilter *This = (TransformFilter *)iface;
127
128     if (pos >= This->npins || pos < 0)
129         return NULL;
130
131     IPin_AddRef(This->ppPins[pos]);
132     return This->ppPins[pos];
133 }
134
135 static LONG WINAPI TransformFilter_GetPinCount(BaseFilter *iface)
136 {
137     TransformFilter *This = (TransformFilter *)iface;
138
139     return (This->npins+1);
140 }
141
142 static const BaseFilterFuncTable tfBaseFuncTable = {
143     TransformFilter_GetPin,
144     TransformFilter_GetPinCount
145 };
146
147 static const  BasePinFuncTable tf_input_BaseFuncTable = {
148     TransformFilter_Input_CheckMediaType,
149     NULL,
150     BasePinImpl_GetMediaTypeVersion,
151     BasePinImpl_GetMediaType
152 };
153
154 static const BaseInputPinFuncTable tf_input_BaseInputFuncTable = {
155     TransformFilter_Input_Receive
156 };
157
158 static const  BasePinFuncTable tf_output_BaseFuncTable = {
159     NULL,
160     BaseOutputPinImpl_AttemptConnection,
161     BasePinImpl_GetMediaTypeVersion,
162     TransformFilter_Output_GetMediaType
163 };
164
165 static const BaseOutputPinFuncTable tf_output_BaseOutputFuncTable = {
166     TransformFilter_Output_DecideBufferSize,
167     BaseOutputPinImpl_DecideAllocator,
168     BaseOutputPinImpl_BreakConnect
169 };
170
171 static HRESULT TransformFilter_Init(const IBaseFilterVtbl *pVtbl, const CLSID* pClsid, const TransformFilterFuncTable* pFuncsTable, TransformFilter* pTransformFilter)
172 {
173     HRESULT hr;
174     PIN_INFO piInput;
175     PIN_INFO piOutput;
176
177     BaseFilter_Init(&pTransformFilter->filter, pVtbl, pClsid, (DWORD_PTR)(__FILE__ ": TransformFilter.csFilter"), &tfBaseFuncTable);
178
179     /* pTransformFilter is already allocated */
180     pTransformFilter->pFuncsTable = pFuncsTable;
181     ZeroMemory(&pTransformFilter->pmt, sizeof(pTransformFilter->pmt));
182     pTransformFilter->npins = 2;
183
184     pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
185
186     /* construct input pin */
187     piInput.dir = PINDIR_INPUT;
188     piInput.pFilter = (IBaseFilter *)pTransformFilter;
189     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
190     piOutput.dir = PINDIR_OUTPUT;
191     piOutput.pFilter = (IBaseFilter *)pTransformFilter;
192     lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
193
194     hr = BaseInputPin_Construct(&TransformFilter_InputPin_Vtbl, &piInput, &tf_input_BaseFuncTable, &tf_input_BaseInputFuncTable, &pTransformFilter->filter.csFilter, NULL, &pTransformFilter->ppPins[0]);
195
196     if (SUCCEEDED(hr))
197     {
198         hr = BaseOutputPin_Construct(&TransformFilter_OutputPin_Vtbl, sizeof(BaseOutputPin), &piOutput, &tf_output_BaseFuncTable, &tf_output_BaseOutputFuncTable, &pTransformFilter->filter.csFilter, &pTransformFilter->ppPins[1]);
199
200         if (FAILED(hr))
201             ERR("Cannot create output pin (%x)\n", hr);
202         else {
203             QualityControlImpl_init(&pTransformFilter->qcimpl, (IPin*)pTransformFilter->ppPins[0], (IBaseFilter*)pTransformFilter);
204             pTransformFilter->qcimpl.lpVtbl = &TransformFilter_QualityControl_Vtbl;
205         }
206     }
207     if (FAILED(hr))
208     {
209         CoTaskMemFree(pTransformFilter->ppPins);
210         BaseFilterImpl_Release((IBaseFilter*)pTransformFilter);
211     }
212
213     return hr;
214 }
215
216 HRESULT TransformFilter_Construct(const IBaseFilterVtbl *pVtbl, LONG filter_size, const CLSID* pClsid, const TransformFilterFuncTable* pFuncsTable, IBaseFilter ** ppTransformFilter)
217 {
218     TransformFilter* pTf;
219
220     *ppTransformFilter = NULL;
221
222     assert(filter_size >= sizeof(TransformFilter));
223
224     pTf = CoTaskMemAlloc(filter_size);
225     ZeroMemory(pTf, filter_size);
226
227     if (!pTf)
228         return E_OUTOFMEMORY;
229
230     if (SUCCEEDED(TransformFilter_Init(pVtbl, pClsid, pFuncsTable, pTf)))
231     {
232         *ppTransformFilter = (IBaseFilter*)(&pTf->filter.lpVtbl);
233         return S_OK;
234     }
235
236     CoTaskMemFree(pTf);
237     return E_FAIL;
238 }
239
240 HRESULT WINAPI TransformFilterImpl_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
241 {
242     HRESULT hr;
243     TransformFilter *This = (TransformFilter *)iface;
244     TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
245
246     if (IsEqualIID(riid, &IID_IQualityControl))  {
247         *ppv = (IQualityControl*)&This->qcimpl;
248         IUnknown_AddRef((IUnknown*)*ppv);
249         return S_OK;
250     }
251     hr = BaseFilterImpl_QueryInterface(iface, riid, ppv);
252
253     if (FAILED(hr) && !IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow) &&
254         !IsEqualIID(riid, &IID_IAMFilterMiscFlags))
255         FIXME("No interface for %s!\n", debugstr_guid(riid));
256
257     return hr;
258 }
259
260 ULONG WINAPI TransformFilterImpl_Release(IBaseFilter * iface)
261 {
262     TransformFilter *This = (TransformFilter *)iface;
263     ULONG refCount = BaseFilterImpl_Release(iface);
264
265     TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
266
267     if (!refCount)
268     {
269         ULONG i;
270
271         for (i = 0; i < This->npins; i++)
272         {
273             IPin *pConnectedTo;
274
275             if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
276             {
277                 IPin_Disconnect(pConnectedTo);
278                 IPin_Release(pConnectedTo);
279             }
280             IPin_Disconnect(This->ppPins[i]);
281
282             IPin_Release(This->ppPins[i]);
283         }
284
285         CoTaskMemFree(This->ppPins);
286
287         TRACE("Destroying transform filter\n");
288         FreeMediaType(&This->pmt);
289         CoTaskMemFree(This);
290
291         return 0;
292     }
293     else
294         return refCount;
295 }
296
297 /** IMediaFilter methods **/
298
299 HRESULT WINAPI TransformFilterImpl_Stop(IBaseFilter * iface)
300 {
301     TransformFilter *This = (TransformFilter *)iface;
302     HRESULT hr = S_OK;
303
304     TRACE("(%p/%p)\n", This, iface);
305
306     EnterCriticalSection(&This->filter.csFilter);
307     {
308         This->filter.state = State_Stopped;
309         if (This->pFuncsTable->pfnStopStreaming)
310             hr = This->pFuncsTable->pfnStopStreaming(This);
311     }
312     LeaveCriticalSection(&This->filter.csFilter);
313
314     return hr;
315 }
316
317 HRESULT WINAPI TransformFilterImpl_Pause(IBaseFilter * iface)
318 {
319     TransformFilter *This = (TransformFilter *)iface;
320     HRESULT hr;
321
322     TRACE("(%p/%p)->()\n", This, iface);
323
324     EnterCriticalSection(&This->filter.csFilter);
325     {
326         if (This->filter.state == State_Stopped)
327             hr = IBaseFilter_Run(iface, -1);
328         else
329             hr = S_OK;
330
331         if (SUCCEEDED(hr))
332             This->filter.state = State_Paused;
333     }
334     LeaveCriticalSection(&This->filter.csFilter);
335
336     return hr;
337 }
338
339 HRESULT WINAPI TransformFilterImpl_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
340 {
341     HRESULT hr = S_OK;
342     TransformFilter *This = (TransformFilter *)iface;
343
344     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
345
346     EnterCriticalSection(&This->filter.csFilter);
347     {
348         if (This->filter.state == State_Stopped)
349         {
350             ((BaseInputPin *)This->ppPins[0])->end_of_stream = 0;
351             if (This->pFuncsTable->pfnStartStreaming)
352                 hr = This->pFuncsTable->pfnStartStreaming(This);
353             if (SUCCEEDED(hr))
354                 hr = BaseOutputPinImpl_Active((BaseOutputPin *)This->ppPins[1]);
355         }
356
357         if (SUCCEEDED(hr))
358         {
359             This->filter.rtStreamStart = tStart;
360             This->filter.state = State_Running;
361         }
362     }
363     LeaveCriticalSection(&This->filter.csFilter);
364
365     return hr;
366 }
367
368 /** IBaseFilter implementation **/
369
370 HRESULT WINAPI TransformFilterImpl_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
371 {
372     TransformFilter *This = (TransformFilter *)iface;
373
374     TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
375
376     return E_NOTIMPL;
377 }
378
379 static const IBaseFilterVtbl TransformFilter_Vtbl =
380 {
381     TransformFilterImpl_QueryInterface,
382     BaseFilterImpl_AddRef,
383     TransformFilterImpl_Release,
384     BaseFilterImpl_GetClassID,
385     TransformFilterImpl_Stop,
386     TransformFilterImpl_Pause,
387     TransformFilterImpl_Run,
388     BaseFilterImpl_GetState,
389     BaseFilterImpl_SetSyncSource,
390     BaseFilterImpl_GetSyncSource,
391     BaseFilterImpl_EnumPins,
392     TransformFilterImpl_FindPin,
393     BaseFilterImpl_QueryFilterInfo,
394     BaseFilterImpl_JoinFilterGraph,
395     BaseFilterImpl_QueryVendorInfo
396 };
397
398 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
399 {
400     BaseInputPin* This = (BaseInputPin*) iface;
401     TransformFilter* pTransform;
402     IPin* ppin;
403     HRESULT hr;
404
405     TRACE("(%p)->()\n", iface);
406
407     /* Since we process samples synchronously, just forward notification downstream */
408     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
409     if (!pTransform)
410         hr = E_FAIL;
411     else
412         hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
413     if (SUCCEEDED(hr))
414     {
415         hr = IPin_EndOfStream(ppin);
416         IPin_Release(ppin);
417     }
418
419     if (FAILED(hr))
420         ERR("%x\n", hr);
421     return hr;
422 }
423
424 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
425 {
426     BaseInputPin* This = (BaseInputPin*) iface;
427     TransformFilter* pTransform;
428     HRESULT hr = S_OK;
429
430     TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
431
432     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
433
434     if (pTransform->pFuncsTable->pfnSetMediaType)
435         hr = pTransform->pFuncsTable->pfnSetMediaType(pTransform, PINDIR_INPUT, pmt);
436
437     if (SUCCEEDED(hr) && pTransform->pFuncsTable->pfnCompleteConnect)
438         hr = pTransform->pFuncsTable->pfnCompleteConnect(pTransform, PINDIR_INPUT, pReceivePin);
439
440     if (SUCCEEDED(hr))
441     {
442         hr = BaseInputPinImpl_ReceiveConnection(iface, pReceivePin, pmt);
443         if (FAILED(hr) && pTransform->pFuncsTable->pfnBreakConnect)
444             pTransform->pFuncsTable->pfnBreakConnect(pTransform, PINDIR_INPUT);
445     }
446
447     return hr;
448 }
449
450 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
451 {
452     BaseInputPin* This = (BaseInputPin*) iface;
453     TransformFilter* pTransform;
454
455     TRACE("(%p)->()\n", iface);
456
457     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
458     if (pTransform->pFuncsTable->pfnBreakConnect)
459         pTransform->pFuncsTable->pfnBreakConnect(pTransform, PINDIR_INPUT);
460
461     return BasePinImpl_Disconnect(iface);
462 }
463
464 static HRESULT WINAPI TransformFilter_InputPin_BeginFlush(IPin * iface)
465 {
466     BaseInputPin* This = (BaseInputPin*) iface;
467     TransformFilter* pTransform;
468     HRESULT hr = S_OK;
469
470     TRACE("(%p)->()\n", iface);
471
472     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
473     EnterCriticalSection(&pTransform->filter.csFilter);
474     if (pTransform->pFuncsTable->pfnBeginFlush)
475         hr = pTransform->pFuncsTable->pfnBeginFlush(pTransform);
476     if (SUCCEEDED(hr))
477         hr = BaseInputPinImpl_BeginFlush(iface);
478     LeaveCriticalSection(&pTransform->filter.csFilter);
479     return hr;
480 }
481
482 static HRESULT WINAPI TransformFilter_InputPin_EndFlush(IPin * iface)
483 {
484     BaseInputPin* This = (BaseInputPin*) iface;
485     TransformFilter* pTransform;
486     HRESULT hr = S_OK;
487
488     TRACE("(%p)->()\n", iface);
489
490     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
491     EnterCriticalSection(&pTransform->filter.csFilter);
492     if (pTransform->pFuncsTable->pfnEndFlush)
493         hr = pTransform->pFuncsTable->pfnEndFlush(pTransform);
494     if (SUCCEEDED(hr))
495         hr = BaseInputPinImpl_EndFlush(iface);
496     LeaveCriticalSection(&pTransform->filter.csFilter);
497     return hr;
498 }
499
500 static HRESULT WINAPI TransformFilter_InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
501 {
502     BaseInputPin* This = (BaseInputPin*) iface;
503     TransformFilter* pTransform;
504     HRESULT hr = S_OK;
505
506     TRACE("(%p)->()\n", iface);
507
508     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
509     EnterCriticalSection(&pTransform->filter.csFilter);
510     if (pTransform->pFuncsTable->pfnNewSegment)
511         hr = pTransform->pFuncsTable->pfnNewSegment(pTransform, tStart, tStop, dRate);
512     if (SUCCEEDED(hr))
513         hr = BaseInputPinImpl_NewSegment(iface, tStart, tStop, dRate);
514     LeaveCriticalSection(&pTransform->filter.csFilter);
515     return hr;
516 }
517
518 static const IPinVtbl TransformFilter_InputPin_Vtbl =
519 {
520     BaseInputPinImpl_QueryInterface,
521     BasePinImpl_AddRef,
522     BaseInputPinImpl_Release,
523     BaseInputPinImpl_Connect,
524     TransformFilter_InputPin_ReceiveConnection,
525     TransformFilter_InputPin_Disconnect,
526     BasePinImpl_ConnectedTo,
527     BasePinImpl_ConnectionMediaType,
528     BasePinImpl_QueryPinInfo,
529     BasePinImpl_QueryDirection,
530     BasePinImpl_QueryId,
531     BaseInputPinImpl_QueryAccept,
532     BasePinImpl_EnumMediaTypes,
533     BasePinImpl_QueryInternalConnections,
534     TransformFilter_InputPin_EndOfStream,
535     TransformFilter_InputPin_BeginFlush,
536     TransformFilter_InputPin_EndFlush,
537     TransformFilter_InputPin_NewSegment
538 };
539
540 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
541 {
542     BaseOutputPinImpl_QueryInterface,
543     BasePinImpl_AddRef,
544     BaseOutputPinImpl_Release,
545     BaseOutputPinImpl_Connect,
546     BaseOutputPinImpl_ReceiveConnection,
547     BaseOutputPinImpl_Disconnect,
548     BasePinImpl_ConnectedTo,
549     BasePinImpl_ConnectionMediaType,
550     BasePinImpl_QueryPinInfo,
551     BasePinImpl_QueryDirection,
552     BasePinImpl_QueryId,
553     TransformFilter_Output_QueryAccept,
554     BasePinImpl_EnumMediaTypes,
555     BasePinImpl_QueryInternalConnections,
556     BaseOutputPinImpl_EndOfStream,
557     BaseOutputPinImpl_BeginFlush,
558     BaseOutputPinImpl_EndFlush,
559     BasePinImpl_NewSegment
560 };
561
562 HRESULT WINAPI TransformFilter_QualityControlImpl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm) {
563     QualityControlImpl *qc = (QualityControlImpl*)iface;
564     TransformFilter *This = (TransformFilter *)qc->self;
565
566     if (This->pFuncsTable->pfnNotify)
567         return This->pFuncsTable->pfnNotify(This, sender, qm);
568     else
569         return QualityControlImpl_Notify(iface, sender, qm);
570 }
571
572 static const IQualityControlVtbl TransformFilter_QualityControl_Vtbl = {
573     QualityControlImpl_QueryInterface,
574     QualityControlImpl_AddRef,
575     QualityControlImpl_Release,
576     TransformFilter_QualityControlImpl_Notify,
577     QualityControlImpl_SetSink
578 };