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, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
115 OutputPin * pPinImpl;
119 if (pPinInfo->dir != PINDIR_OUTPUT)
121 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
125 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
128 return E_OUTOFMEMORY;
130 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
132 pPinImpl->pin.lpVtbl = &TransformFilter_OutputPin_Vtbl;
134 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
140 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable)
146 /* pTransformFilter is already allocated */
147 pTransformFilter->clsid = *pClsid;
148 pTransformFilter->pFuncsTable = pFuncsTable;
150 pTransformFilter->lpVtbl = &TransformFilter_Vtbl;
152 pTransformFilter->refCount = 1;
153 InitializeCriticalSection(&pTransformFilter->csFilter);
154 pTransformFilter->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TransformFilterImpl.csFilter");
155 pTransformFilter->state = State_Stopped;
156 pTransformFilter->pClock = NULL;
157 ZeroMemory(&pTransformFilter->filterInfo, sizeof(FILTER_INFO));
159 pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
161 /* construct input pin */
162 piInput.dir = PINDIR_INPUT;
163 piInput.pFilter = (IBaseFilter *)pTransformFilter;
164 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
165 piOutput.dir = PINDIR_OUTPUT;
166 piOutput.pFilter = (IBaseFilter *)pTransformFilter;
167 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
169 hr = TransformFilter_InputPin_Construct(&piInput, TransformFilter_Sample, pTransformFilter, TransformFilter_Input_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[0]);
173 ALLOCATOR_PROPERTIES props;
176 props.cbBuffer = 0; /* Will be updated at connection time */
179 hr = TransformFilter_OutputPin_Construct(&piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
182 ERR("Cannot create output pin (%x)\n", hr);
186 CoTaskMemFree(pTransformFilter->ppPins);
187 pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
188 DeleteCriticalSection(&pTransformFilter->csFilter);
189 CoTaskMemFree(pTransformFilter);
195 static HRESULT WINAPI TransformFilter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
197 TransformFilterImpl *This = (TransformFilterImpl *)iface;
198 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
202 if (IsEqualIID(riid, &IID_IUnknown))
204 else if (IsEqualIID(riid, &IID_IPersist))
206 else if (IsEqualIID(riid, &IID_IMediaFilter))
208 else if (IsEqualIID(riid, &IID_IBaseFilter))
213 IUnknown_AddRef((IUnknown *)(*ppv));
217 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
219 return E_NOINTERFACE;
222 static ULONG WINAPI TransformFilter_AddRef(IBaseFilter * iface)
224 TransformFilterImpl *This = (TransformFilterImpl *)iface;
225 ULONG refCount = InterlockedIncrement(&This->refCount);
227 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
232 static ULONG WINAPI TransformFilter_Release(IBaseFilter * iface)
234 TransformFilterImpl *This = (TransformFilterImpl *)iface;
235 ULONG refCount = InterlockedDecrement(&This->refCount);
237 TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
244 IReferenceClock_Release(This->pClock);
246 for (i = 0; i < 2; i++)
250 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
252 IPin_Disconnect(pConnectedTo);
253 IPin_Release(pConnectedTo);
255 IPin_Disconnect(This->ppPins[i]);
257 IPin_Release(This->ppPins[i]);
260 CoTaskMemFree(This->ppPins);
263 This->csFilter.DebugInfo->Spare[0] = 0;
264 DeleteCriticalSection(&This->csFilter);
266 TRACE("Destroying transform filter\n");
275 /** IPersist methods **/
277 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
279 TransformFilterImpl *This = (TransformFilterImpl *)iface;
281 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
283 *pClsid = This->clsid;
288 /** IMediaFilter methods **/
290 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
292 TransformFilterImpl *This = (TransformFilterImpl *)iface;
294 TRACE("(%p/%p)\n", This, iface);
296 EnterCriticalSection(&This->csFilter);
298 This->state = State_Stopped;
299 if (This->pFuncsTable->pfnProcessEnd)
300 This->pFuncsTable->pfnProcessEnd(This);
302 LeaveCriticalSection(&This->csFilter);
307 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
309 TransformFilterImpl *This = (TransformFilterImpl *)iface;
311 TRACE("(%p/%p)->()\n", This, iface);
313 EnterCriticalSection(&This->csFilter);
315 This->state = State_Paused;
317 LeaveCriticalSection(&This->csFilter);
322 static HRESULT WINAPI TransformFilter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
325 TransformFilterImpl *This = (TransformFilterImpl *)iface;
327 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
329 EnterCriticalSection(&This->csFilter);
331 This->rtStreamStart = tStart;
332 This->state = State_Running;
333 OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
334 if (This->pFuncsTable->pfnProcessBegin)
335 This->pFuncsTable->pfnProcessBegin(This);
337 LeaveCriticalSection(&This->csFilter);
342 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
344 TransformFilterImpl *This = (TransformFilterImpl *)iface;
346 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
348 EnterCriticalSection(&This->csFilter);
350 *pState = This->state;
352 LeaveCriticalSection(&This->csFilter);
357 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
359 TransformFilterImpl *This = (TransformFilterImpl *)iface;
361 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
363 EnterCriticalSection(&This->csFilter);
366 IReferenceClock_Release(This->pClock);
367 This->pClock = pClock;
369 IReferenceClock_AddRef(This->pClock);
371 LeaveCriticalSection(&This->csFilter);
376 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
378 TransformFilterImpl *This = (TransformFilterImpl *)iface;
380 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
382 EnterCriticalSection(&This->csFilter);
384 *ppClock = This->pClock;
385 IReferenceClock_AddRef(This->pClock);
387 LeaveCriticalSection(&This->csFilter);
392 /** IBaseFilter implementation **/
394 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
397 TransformFilterImpl *This = (TransformFilterImpl *)iface;
399 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
401 epd.cPins = 2; /* input and output pins */
402 epd.ppPins = This->ppPins;
403 return IEnumPinsImpl_Construct(&epd, ppEnum);
406 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
408 TransformFilterImpl *This = (TransformFilterImpl *)iface;
410 TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
415 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
417 TransformFilterImpl *This = (TransformFilterImpl *)iface;
419 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
421 strcpyW(pInfo->achName, This->filterInfo.achName);
422 pInfo->pGraph = This->filterInfo.pGraph;
425 IFilterGraph_AddRef(pInfo->pGraph);
430 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
433 TransformFilterImpl *This = (TransformFilterImpl *)iface;
435 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
437 EnterCriticalSection(&This->csFilter);
440 strcpyW(This->filterInfo.achName, pName);
442 *This->filterInfo.achName = '\0';
443 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
445 LeaveCriticalSection(&This->csFilter);
450 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
452 TransformFilterImpl *This = (TransformFilterImpl *)iface;
453 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
457 static const IBaseFilterVtbl TransformFilter_Vtbl =
459 TransformFilter_QueryInterface,
460 TransformFilter_AddRef,
461 TransformFilter_Release,
462 TransformFilter_GetClassID,
463 TransformFilter_Stop,
464 TransformFilter_Pause,
466 TransformFilter_GetState,
467 TransformFilter_SetSyncSource,
468 TransformFilter_GetSyncSource,
469 TransformFilter_EnumPins,
470 TransformFilter_FindPin,
471 TransformFilter_QueryFilterInfo,
472 TransformFilter_JoinFilterGraph,
473 TransformFilter_QueryVendorInfo
476 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
478 InputPin* This = (InputPin*) iface;
479 TransformFilterImpl* pTransform;
483 TRACE("(%p)->()\n", iface);
485 /* Since we process samples synchronously, just forward notification downstream */
486 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
490 hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
493 hr = IPin_EndOfStream(ppin);
502 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
504 InputPin* This = (InputPin*) iface;
505 TransformFilterImpl* pTransform;
508 TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
510 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
512 hr = pTransform->pFuncsTable->pfnConnectInput(pTransform, pmt);
515 hr = InputPin_ReceiveConnection(iface, pReceivePin, pmt);
517 pTransform->pFuncsTable->pfnCleanup(pTransform);
523 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
525 InputPin* This = (InputPin*) iface;
526 TransformFilterImpl* pTransform;
528 TRACE("(%p)->()\n", iface);
530 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
531 pTransform->pFuncsTable->pfnCleanup(pTransform);
533 return IPinImpl_Disconnect(iface);
536 static const IPinVtbl TransformFilter_InputPin_Vtbl =
538 InputPin_QueryInterface,
542 TransformFilter_InputPin_ReceiveConnection,
543 TransformFilter_InputPin_Disconnect,
544 IPinImpl_ConnectedTo,
545 IPinImpl_ConnectionMediaType,
546 IPinImpl_QueryPinInfo,
547 IPinImpl_QueryDirection,
549 IPinImpl_QueryAccept,
550 IPinImpl_EnumMediaTypes,
551 IPinImpl_QueryInternalConnections,
552 TransformFilter_InputPin_EndOfStream,
558 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
560 IPinImpl *This = (IPinImpl *)iface;
561 ENUMMEDIADETAILS emd;
563 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
566 emd.pMediaTypes = &This->mtCurrent;
568 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
571 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
573 OutputPin_QueryInterface,
577 OutputPin_ReceiveConnection,
578 OutputPin_Disconnect,
579 IPinImpl_ConnectedTo,
580 IPinImpl_ConnectionMediaType,
581 IPinImpl_QueryPinInfo,
582 IPinImpl_QueryDirection,
584 IPinImpl_QueryAccept,
585 TransformFilter_Output_EnumMediaTypes,
586 IPinImpl_QueryInternalConnections,
587 OutputPin_EndOfStream,
588 OutputPin_BeginFlush,
593 static const IMemInputPinVtbl MemInputPin_Vtbl =
595 MemInputPin_QueryInterface,
598 MemInputPin_GetAllocator,
599 MemInputPin_NotifyAllocator,
600 MemInputPin_GetAllocatorRequirements,
602 MemInputPin_ReceiveMultiple,
603 MemInputPin_ReceiveCanBlock