2 * Transform Filter (Base for decoders, etc...)
4 * Copyright 2005 Christian Costa
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.
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.
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
23 #include "quartz_private.h"
24 #include "control_private.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 #include "transform.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
43 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
44 static const WCHAR wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};
46 static const IBaseFilterVtbl TransformFilter_Vtbl;
47 static const IPinVtbl TransformFilter_InputPin_Vtbl;
48 static const IMemInputPinVtbl MemInputPin_Vtbl;
49 static const IPinVtbl TransformFilter_OutputPin_Vtbl;
51 static HRESULT TransformFilter_Sample(LPVOID iface, IMediaSample * pSample)
53 TransformFilterImpl *This = (TransformFilterImpl *)iface;
55 TRACE("%p %p\n", iface, pSample);
57 return This->pFuncsTable->pfnProcessSampleData(This, pSample);
60 static HRESULT TransformFilter_Input_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
62 TransformFilterImpl* This = (TransformFilterImpl*)iface;
64 dump_AM_MEDIA_TYPE(pmt);
66 if (This->pFuncsTable->pfnQueryConnect)
67 return This->pFuncsTable->pfnQueryConnect(This, pmt);
68 /* Assume OK if there's no query method (the connection will fail if
74 static HRESULT TransformFilter_Output_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
76 TransformFilterImpl* pTransformFilter = (TransformFilterImpl*)iface;
77 AM_MEDIA_TYPE* outpmt = &((OutputPin*)pTransformFilter->ppPins[1])->pin.mtCurrent;
80 if (IsEqualIID(&pmt->majortype, &outpmt->majortype) && IsEqualIID(&pmt->subtype, &outpmt->subtype))
85 static HRESULT TransformFilter_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
91 if (pPinInfo->dir != PINDIR_INPUT)
93 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
97 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
100 return E_OUTOFMEMORY;
102 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
104 pPinImpl->pin.lpVtbl = &TransformFilter_InputPin_Vtbl;
105 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
107 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
111 CoTaskMemFree(pPinImpl);
115 static HRESULT TransformFilter_OutputPin_Construct(const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES *props,
116 LPVOID pUserData, QUERYACCEPTPROC pQueryAccept,
117 LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
119 OutputPin * pPinImpl;
123 if (pPinInfo->dir != PINDIR_OUTPUT)
125 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
129 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
132 return E_OUTOFMEMORY;
134 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
136 pPinImpl->pin.lpVtbl = &TransformFilter_OutputPin_Vtbl;
138 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
142 CoTaskMemFree(pPinImpl);
146 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable)
152 /* pTransformFilter is already allocated */
153 pTransformFilter->clsid = *pClsid;
154 pTransformFilter->pFuncsTable = pFuncsTable;
156 pTransformFilter->lpVtbl = &TransformFilter_Vtbl;
158 pTransformFilter->refCount = 1;
159 InitializeCriticalSection(&pTransformFilter->csFilter);
160 pTransformFilter->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TransformFilterImpl.csFilter");
161 pTransformFilter->state = State_Stopped;
162 pTransformFilter->pClock = NULL;
163 ZeroMemory(&pTransformFilter->filterInfo, sizeof(FILTER_INFO));
165 pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
167 /* construct input pin */
168 piInput.dir = PINDIR_INPUT;
169 piInput.pFilter = (IBaseFilter *)pTransformFilter;
170 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
171 piOutput.dir = PINDIR_OUTPUT;
172 piOutput.pFilter = (IBaseFilter *)pTransformFilter;
173 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
175 hr = TransformFilter_InputPin_Construct(&piInput, TransformFilter_Sample, pTransformFilter, TransformFilter_Input_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[0]);
179 ALLOCATOR_PROPERTIES props;
182 props.cbBuffer = 0; /* Will be updated at connection time */
185 hr = TransformFilter_OutputPin_Construct(&piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
188 ERR("Cannot create output pin (%x)\n", hr);
192 CoTaskMemFree(pTransformFilter->ppPins);
193 pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
194 DeleteCriticalSection(&pTransformFilter->csFilter);
195 CoTaskMemFree(pTransformFilter);
201 static HRESULT WINAPI TransformFilter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
203 TransformFilterImpl *This = (TransformFilterImpl *)iface;
204 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
208 if (IsEqualIID(riid, &IID_IUnknown))
210 else if (IsEqualIID(riid, &IID_IPersist))
212 else if (IsEqualIID(riid, &IID_IMediaFilter))
214 else if (IsEqualIID(riid, &IID_IBaseFilter))
219 IUnknown_AddRef((IUnknown *)(*ppv));
223 if (!IsEqualIID(riid, &IID_IPin))
224 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
226 return E_NOINTERFACE;
229 static ULONG WINAPI TransformFilter_AddRef(IBaseFilter * iface)
231 TransformFilterImpl *This = (TransformFilterImpl *)iface;
232 ULONG refCount = InterlockedIncrement(&This->refCount);
234 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
239 static ULONG WINAPI TransformFilter_Release(IBaseFilter * iface)
241 TransformFilterImpl *This = (TransformFilterImpl *)iface;
242 ULONG refCount = InterlockedDecrement(&This->refCount);
244 TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
251 IReferenceClock_Release(This->pClock);
253 for (i = 0; i < 2; i++)
257 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
259 IPin_Disconnect(pConnectedTo);
260 IPin_Release(pConnectedTo);
262 IPin_Disconnect(This->ppPins[i]);
264 IPin_Release(This->ppPins[i]);
267 CoTaskMemFree(This->ppPins);
270 This->csFilter.DebugInfo->Spare[0] = 0;
271 DeleteCriticalSection(&This->csFilter);
273 TRACE("Destroying transform filter\n");
282 /** IPersist methods **/
284 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
286 TransformFilterImpl *This = (TransformFilterImpl *)iface;
288 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
290 *pClsid = This->clsid;
295 /** IMediaFilter methods **/
297 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
299 TransformFilterImpl *This = (TransformFilterImpl *)iface;
301 TRACE("(%p/%p)\n", This, iface);
303 EnterCriticalSection(&This->csFilter);
305 This->state = State_Stopped;
306 if (This->pFuncsTable->pfnProcessEnd)
307 This->pFuncsTable->pfnProcessEnd(This);
309 LeaveCriticalSection(&This->csFilter);
314 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
316 TransformFilterImpl *This = (TransformFilterImpl *)iface;
318 TRACE("(%p/%p)->()\n", This, iface);
320 EnterCriticalSection(&This->csFilter);
322 This->state = State_Paused;
324 LeaveCriticalSection(&This->csFilter);
329 static HRESULT WINAPI TransformFilter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
332 TransformFilterImpl *This = (TransformFilterImpl *)iface;
334 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
336 EnterCriticalSection(&This->csFilter);
338 This->rtStreamStart = tStart;
339 This->state = State_Running;
340 OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
341 if (This->pFuncsTable->pfnProcessBegin)
342 This->pFuncsTable->pfnProcessBegin(This);
344 LeaveCriticalSection(&This->csFilter);
349 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
351 TransformFilterImpl *This = (TransformFilterImpl *)iface;
353 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
355 EnterCriticalSection(&This->csFilter);
357 *pState = This->state;
359 LeaveCriticalSection(&This->csFilter);
364 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
366 TransformFilterImpl *This = (TransformFilterImpl *)iface;
368 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
370 EnterCriticalSection(&This->csFilter);
373 IReferenceClock_Release(This->pClock);
374 This->pClock = pClock;
376 IReferenceClock_AddRef(This->pClock);
378 LeaveCriticalSection(&This->csFilter);
383 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
385 TransformFilterImpl *This = (TransformFilterImpl *)iface;
387 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
389 EnterCriticalSection(&This->csFilter);
391 *ppClock = This->pClock;
392 IReferenceClock_AddRef(This->pClock);
394 LeaveCriticalSection(&This->csFilter);
399 /** IBaseFilter implementation **/
401 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
404 TransformFilterImpl *This = (TransformFilterImpl *)iface;
406 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
408 epd.cPins = 2; /* input and output pins */
409 epd.ppPins = This->ppPins;
410 return IEnumPinsImpl_Construct(&epd, ppEnum);
413 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
415 TransformFilterImpl *This = (TransformFilterImpl *)iface;
417 TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
422 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
424 TransformFilterImpl *This = (TransformFilterImpl *)iface;
426 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
428 strcpyW(pInfo->achName, This->filterInfo.achName);
429 pInfo->pGraph = This->filterInfo.pGraph;
432 IFilterGraph_AddRef(pInfo->pGraph);
437 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
440 TransformFilterImpl *This = (TransformFilterImpl *)iface;
442 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
444 EnterCriticalSection(&This->csFilter);
447 strcpyW(This->filterInfo.achName, pName);
449 *This->filterInfo.achName = '\0';
450 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
452 LeaveCriticalSection(&This->csFilter);
457 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
459 TransformFilterImpl *This = (TransformFilterImpl *)iface;
460 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
464 static const IBaseFilterVtbl TransformFilter_Vtbl =
466 TransformFilter_QueryInterface,
467 TransformFilter_AddRef,
468 TransformFilter_Release,
469 TransformFilter_GetClassID,
470 TransformFilter_Stop,
471 TransformFilter_Pause,
473 TransformFilter_GetState,
474 TransformFilter_SetSyncSource,
475 TransformFilter_GetSyncSource,
476 TransformFilter_EnumPins,
477 TransformFilter_FindPin,
478 TransformFilter_QueryFilterInfo,
479 TransformFilter_JoinFilterGraph,
480 TransformFilter_QueryVendorInfo
483 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
485 InputPin* This = (InputPin*) iface;
486 TransformFilterImpl* pTransform;
490 TRACE("(%p)->()\n", iface);
492 /* Since we process samples synchronously, just forward notification downstream */
493 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
497 hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
500 hr = IPin_EndOfStream(ppin);
509 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
511 InputPin* This = (InputPin*) iface;
512 TransformFilterImpl* pTransform;
515 TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
517 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
519 hr = pTransform->pFuncsTable->pfnConnectInput(pTransform, pmt);
522 hr = InputPin_ReceiveConnection(iface, pReceivePin, pmt);
524 pTransform->pFuncsTable->pfnCleanup(pTransform);
530 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
532 InputPin* This = (InputPin*) iface;
533 TransformFilterImpl* pTransform;
535 TRACE("(%p)->()\n", iface);
537 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
538 pTransform->pFuncsTable->pfnCleanup(pTransform);
540 return IPinImpl_Disconnect(iface);
543 static const IPinVtbl TransformFilter_InputPin_Vtbl =
545 InputPin_QueryInterface,
549 TransformFilter_InputPin_ReceiveConnection,
550 TransformFilter_InputPin_Disconnect,
551 IPinImpl_ConnectedTo,
552 IPinImpl_ConnectionMediaType,
553 IPinImpl_QueryPinInfo,
554 IPinImpl_QueryDirection,
556 IPinImpl_QueryAccept,
557 IPinImpl_EnumMediaTypes,
558 IPinImpl_QueryInternalConnections,
559 TransformFilter_InputPin_EndOfStream,
565 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
567 IPinImpl *This = (IPinImpl *)iface;
568 ENUMMEDIADETAILS emd;
570 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
573 emd.pMediaTypes = &This->mtCurrent;
575 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
578 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
580 OutputPin_QueryInterface,
584 OutputPin_ReceiveConnection,
585 OutputPin_Disconnect,
586 IPinImpl_ConnectedTo,
587 IPinImpl_ConnectionMediaType,
588 IPinImpl_QueryPinInfo,
589 IPinImpl_QueryDirection,
591 IPinImpl_QueryAccept,
592 TransformFilter_Output_EnumMediaTypes,
593 IPinImpl_QueryInternalConnections,
594 OutputPin_EndOfStream,
595 OutputPin_BeginFlush,
600 static const IMemInputPinVtbl MemInputPin_Vtbl =
602 MemInputPin_QueryInterface,
605 MemInputPin_GetAllocator,
606 MemInputPin_NotifyAllocator,
607 MemInputPin_GetAllocatorRequirements,
609 MemInputPin_ReceiveMultiple,
610 MemInputPin_ReceiveCanBlock