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);
113 static HRESULT TransformFilter_OutputPin_Construct(const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES *props,
114 LPVOID pUserData, QUERYACCEPTPROC pQueryAccept,
115 LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
117 OutputPin * pPinImpl;
121 if (pPinInfo->dir != PINDIR_OUTPUT)
123 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
127 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
130 return E_OUTOFMEMORY;
132 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
134 pPinImpl->pin.lpVtbl = &TransformFilter_OutputPin_Vtbl;
136 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
142 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable)
148 /* pTransformFilter is already allocated */
149 pTransformFilter->clsid = *pClsid;
150 pTransformFilter->pFuncsTable = pFuncsTable;
152 pTransformFilter->lpVtbl = &TransformFilter_Vtbl;
154 pTransformFilter->refCount = 1;
155 InitializeCriticalSection(&pTransformFilter->csFilter);
156 pTransformFilter->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TransformFilterImpl.csFilter");
157 pTransformFilter->state = State_Stopped;
158 pTransformFilter->pClock = NULL;
159 ZeroMemory(&pTransformFilter->filterInfo, sizeof(FILTER_INFO));
161 pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
163 /* construct input pin */
164 piInput.dir = PINDIR_INPUT;
165 piInput.pFilter = (IBaseFilter *)pTransformFilter;
166 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
167 piOutput.dir = PINDIR_OUTPUT;
168 piOutput.pFilter = (IBaseFilter *)pTransformFilter;
169 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
171 hr = TransformFilter_InputPin_Construct(&piInput, TransformFilter_Sample, pTransformFilter, TransformFilter_Input_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[0]);
175 ALLOCATOR_PROPERTIES props;
178 props.cbBuffer = 0; /* Will be updated at connection time */
181 hr = TransformFilter_OutputPin_Construct(&piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
184 ERR("Cannot create output pin (%x)\n", hr);
188 CoTaskMemFree(pTransformFilter->ppPins);
189 pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
190 DeleteCriticalSection(&pTransformFilter->csFilter);
191 CoTaskMemFree(pTransformFilter);
197 static HRESULT WINAPI TransformFilter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
199 TransformFilterImpl *This = (TransformFilterImpl *)iface;
200 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
204 if (IsEqualIID(riid, &IID_IUnknown))
206 else if (IsEqualIID(riid, &IID_IPersist))
208 else if (IsEqualIID(riid, &IID_IMediaFilter))
210 else if (IsEqualIID(riid, &IID_IBaseFilter))
215 IUnknown_AddRef((IUnknown *)(*ppv));
219 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
221 return E_NOINTERFACE;
224 static ULONG WINAPI TransformFilter_AddRef(IBaseFilter * iface)
226 TransformFilterImpl *This = (TransformFilterImpl *)iface;
227 ULONG refCount = InterlockedIncrement(&This->refCount);
229 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
234 static ULONG WINAPI TransformFilter_Release(IBaseFilter * iface)
236 TransformFilterImpl *This = (TransformFilterImpl *)iface;
237 ULONG refCount = InterlockedDecrement(&This->refCount);
239 TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
246 IReferenceClock_Release(This->pClock);
248 for (i = 0; i < 2; i++)
252 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
254 IPin_Disconnect(pConnectedTo);
255 IPin_Release(pConnectedTo);
257 IPin_Disconnect(This->ppPins[i]);
259 IPin_Release(This->ppPins[i]);
262 CoTaskMemFree(This->ppPins);
265 This->csFilter.DebugInfo->Spare[0] = 0;
266 DeleteCriticalSection(&This->csFilter);
268 TRACE("Destroying transform filter\n");
277 /** IPersist methods **/
279 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
281 TransformFilterImpl *This = (TransformFilterImpl *)iface;
283 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
285 *pClsid = This->clsid;
290 /** IMediaFilter methods **/
292 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
294 TransformFilterImpl *This = (TransformFilterImpl *)iface;
296 TRACE("(%p/%p)\n", This, iface);
298 EnterCriticalSection(&This->csFilter);
300 This->state = State_Stopped;
301 if (This->pFuncsTable->pfnProcessEnd)
302 This->pFuncsTable->pfnProcessEnd(This);
304 LeaveCriticalSection(&This->csFilter);
309 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
311 TransformFilterImpl *This = (TransformFilterImpl *)iface;
313 TRACE("(%p/%p)->()\n", This, iface);
315 EnterCriticalSection(&This->csFilter);
317 This->state = State_Paused;
319 LeaveCriticalSection(&This->csFilter);
324 static HRESULT WINAPI TransformFilter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
327 TransformFilterImpl *This = (TransformFilterImpl *)iface;
329 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
331 EnterCriticalSection(&This->csFilter);
333 This->rtStreamStart = tStart;
334 This->state = State_Running;
335 OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
336 if (This->pFuncsTable->pfnProcessBegin)
337 This->pFuncsTable->pfnProcessBegin(This);
339 LeaveCriticalSection(&This->csFilter);
344 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
346 TransformFilterImpl *This = (TransformFilterImpl *)iface;
348 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
350 EnterCriticalSection(&This->csFilter);
352 *pState = This->state;
354 LeaveCriticalSection(&This->csFilter);
359 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
361 TransformFilterImpl *This = (TransformFilterImpl *)iface;
363 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
365 EnterCriticalSection(&This->csFilter);
368 IReferenceClock_Release(This->pClock);
369 This->pClock = pClock;
371 IReferenceClock_AddRef(This->pClock);
373 LeaveCriticalSection(&This->csFilter);
378 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
380 TransformFilterImpl *This = (TransformFilterImpl *)iface;
382 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
384 EnterCriticalSection(&This->csFilter);
386 *ppClock = This->pClock;
387 IReferenceClock_AddRef(This->pClock);
389 LeaveCriticalSection(&This->csFilter);
394 /** IBaseFilter implementation **/
396 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
399 TransformFilterImpl *This = (TransformFilterImpl *)iface;
401 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
403 epd.cPins = 2; /* input and output pins */
404 epd.ppPins = This->ppPins;
405 return IEnumPinsImpl_Construct(&epd, ppEnum);
408 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
410 TransformFilterImpl *This = (TransformFilterImpl *)iface;
412 TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
417 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
419 TransformFilterImpl *This = (TransformFilterImpl *)iface;
421 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
423 strcpyW(pInfo->achName, This->filterInfo.achName);
424 pInfo->pGraph = This->filterInfo.pGraph;
427 IFilterGraph_AddRef(pInfo->pGraph);
432 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
435 TransformFilterImpl *This = (TransformFilterImpl *)iface;
437 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
439 EnterCriticalSection(&This->csFilter);
442 strcpyW(This->filterInfo.achName, pName);
444 *This->filterInfo.achName = '\0';
445 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
447 LeaveCriticalSection(&This->csFilter);
452 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
454 TransformFilterImpl *This = (TransformFilterImpl *)iface;
455 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
459 static const IBaseFilterVtbl TransformFilter_Vtbl =
461 TransformFilter_QueryInterface,
462 TransformFilter_AddRef,
463 TransformFilter_Release,
464 TransformFilter_GetClassID,
465 TransformFilter_Stop,
466 TransformFilter_Pause,
468 TransformFilter_GetState,
469 TransformFilter_SetSyncSource,
470 TransformFilter_GetSyncSource,
471 TransformFilter_EnumPins,
472 TransformFilter_FindPin,
473 TransformFilter_QueryFilterInfo,
474 TransformFilter_JoinFilterGraph,
475 TransformFilter_QueryVendorInfo
478 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
480 InputPin* This = (InputPin*) iface;
481 TransformFilterImpl* pTransform;
485 TRACE("(%p)->()\n", iface);
487 /* Since we process samples synchronously, just forward notification downstream */
488 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
492 hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
495 hr = IPin_EndOfStream(ppin);
504 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
506 InputPin* This = (InputPin*) iface;
507 TransformFilterImpl* pTransform;
510 TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
512 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
514 hr = pTransform->pFuncsTable->pfnConnectInput(pTransform, pmt);
517 hr = InputPin_ReceiveConnection(iface, pReceivePin, pmt);
519 pTransform->pFuncsTable->pfnCleanup(pTransform);
525 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
527 InputPin* This = (InputPin*) iface;
528 TransformFilterImpl* pTransform;
530 TRACE("(%p)->()\n", iface);
532 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
533 pTransform->pFuncsTable->pfnCleanup(pTransform);
535 return IPinImpl_Disconnect(iface);
538 static const IPinVtbl TransformFilter_InputPin_Vtbl =
540 InputPin_QueryInterface,
544 TransformFilter_InputPin_ReceiveConnection,
545 TransformFilter_InputPin_Disconnect,
546 IPinImpl_ConnectedTo,
547 IPinImpl_ConnectionMediaType,
548 IPinImpl_QueryPinInfo,
549 IPinImpl_QueryDirection,
551 IPinImpl_QueryAccept,
552 IPinImpl_EnumMediaTypes,
553 IPinImpl_QueryInternalConnections,
554 TransformFilter_InputPin_EndOfStream,
560 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
562 IPinImpl *This = (IPinImpl *)iface;
563 ENUMMEDIADETAILS emd;
565 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
568 emd.pMediaTypes = &This->mtCurrent;
570 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
573 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
575 OutputPin_QueryInterface,
579 OutputPin_ReceiveConnection,
580 OutputPin_Disconnect,
581 IPinImpl_ConnectedTo,
582 IPinImpl_ConnectionMediaType,
583 IPinImpl_QueryPinInfo,
584 IPinImpl_QueryDirection,
586 IPinImpl_QueryAccept,
587 TransformFilter_Output_EnumMediaTypes,
588 IPinImpl_QueryInternalConnections,
589 OutputPin_EndOfStream,
590 OutputPin_BeginFlush,
595 static const IMemInputPinVtbl MemInputPin_Vtbl =
597 MemInputPin_QueryInterface,
600 MemInputPin_GetAllocator,
601 MemInputPin_NotifyAllocator,
602 MemInputPin_GetAllocatorRequirements,
604 MemInputPin_ReceiveMultiple,
605 MemInputPin_ReceiveCanBlock