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_Input_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
53 TransformFilterImpl* This = (TransformFilterImpl*)iface;
55 dump_AM_MEDIA_TYPE(pmt);
57 if (This->pFuncsTable->pfnQueryConnect)
58 return This->pFuncsTable->pfnQueryConnect(This, pmt);
59 /* Assume OK if there's no query method (the connection will fail if
65 static HRESULT TransformFilter_Output_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
67 TransformFilterImpl* pTransformFilter = (TransformFilterImpl*)iface;
68 AM_MEDIA_TYPE* outpmt = &((OutputPin*)pTransformFilter->ppPins[1])->pin.mtCurrent;
71 if (IsEqualIID(&pmt->majortype, &outpmt->majortype) && IsEqualIID(&pmt->subtype, &outpmt->subtype))
77 static inline TransformFilterImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
79 return (TransformFilterImpl *)((char*)iface - FIELD_OFFSET(TransformFilterImpl, mediaSeeking.lpVtbl));
82 static HRESULT WINAPI TransformFilter_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
84 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
86 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
89 static ULONG WINAPI TransformFilter_Seeking_AddRef(IMediaSeeking * iface)
91 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
93 return IUnknown_AddRef((IUnknown *)This);
96 static ULONG WINAPI TransformFilter_Seeking_Release(IMediaSeeking * iface)
98 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
100 return IUnknown_Release((IUnknown *)This);
103 static const IMediaSeekingVtbl TransformFilter_Seeking_Vtbl =
105 TransformFilter_Seeking_QueryInterface,
106 TransformFilter_Seeking_AddRef,
107 TransformFilter_Seeking_Release,
108 MediaSeekingImpl_GetCapabilities,
109 MediaSeekingImpl_CheckCapabilities,
110 MediaSeekingImpl_IsFormatSupported,
111 MediaSeekingImpl_QueryPreferredFormat,
112 MediaSeekingImpl_GetTimeFormat,
113 MediaSeekingImpl_IsUsingTimeFormat,
114 MediaSeekingImpl_SetTimeFormat,
115 MediaSeekingImpl_GetDuration,
116 MediaSeekingImpl_GetStopPosition,
117 MediaSeekingImpl_GetCurrentPosition,
118 MediaSeekingImpl_ConvertTimeFormat,
119 MediaSeekingImpl_SetPositions,
120 MediaSeekingImpl_GetPositions,
121 MediaSeekingImpl_GetAvailable,
122 MediaSeekingImpl_SetRate,
123 MediaSeekingImpl_GetRate,
124 MediaSeekingImpl_GetPreroll
127 /* These shouldn't be implemented by default.
128 * Usually only source filters should implement these
129 * and even it's not needed all of the time
131 static HRESULT TransformFilter_ChangeCurrent(IBaseFilter *iface)
133 TRACE("(%p) filter hasn't implemented current position change!\n", iface);
137 static HRESULT TransformFilter_ChangeStop(IBaseFilter *iface)
139 TRACE("(%p) filter hasn't implemented stop position change!\n", iface);
143 static HRESULT TransformFilter_ChangeRate(IBaseFilter *iface)
145 TRACE("(%p) filter hasn't implemented rate change!\n", iface);
149 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable, CHANGEPROC stop, CHANGEPROC current, CHANGEPROC rate)
155 /* pTransformFilter is already allocated */
156 pTransformFilter->clsid = *pClsid;
157 pTransformFilter->pFuncsTable = pFuncsTable;
159 pTransformFilter->lpVtbl = &TransformFilter_Vtbl;
161 pTransformFilter->refCount = 1;
162 InitializeCriticalSection(&pTransformFilter->csFilter);
163 pTransformFilter->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TransformFilterImpl.csFilter");
164 pTransformFilter->state = State_Stopped;
165 pTransformFilter->pClock = NULL;
166 ZeroMemory(&pTransformFilter->filterInfo, sizeof(FILTER_INFO));
167 ZeroMemory(&pTransformFilter->pmt, sizeof(pTransformFilter->pmt));
169 pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
171 /* construct input pin */
172 piInput.dir = PINDIR_INPUT;
173 piInput.pFilter = (IBaseFilter *)pTransformFilter;
174 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
175 piOutput.dir = PINDIR_OUTPUT;
176 piOutput.pFilter = (IBaseFilter *)pTransformFilter;
177 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
179 hr = InputPin_Construct(&TransformFilter_InputPin_Vtbl, &piInput, (SAMPLEPROC_PUSH)pFuncsTable->pfnProcessSampleData, pTransformFilter, TransformFilter_Input_QueryAccept, NULL, &pTransformFilter->csFilter, NULL, &pTransformFilter->ppPins[0]);
183 ALLOCATOR_PROPERTIES props;
186 props.cbBuffer = 0; /* Will be updated at connection time */
189 hr = OutputPin_Construct(&TransformFilter_OutputPin_Vtbl, sizeof(OutputPin), &piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
192 ERR("Cannot create output pin (%x)\n", hr);
196 stop = TransformFilter_ChangeStop;
198 current = TransformFilter_ChangeCurrent;
200 rate = TransformFilter_ChangeRate;
202 MediaSeekingImpl_Init((IBaseFilter*)pTransformFilter, stop, current, rate, &pTransformFilter->mediaSeeking, &pTransformFilter->csFilter);
203 pTransformFilter->mediaSeeking.lpVtbl = &TransformFilter_Seeking_Vtbl;
208 CoTaskMemFree(pTransformFilter->ppPins);
209 pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
210 DeleteCriticalSection(&pTransformFilter->csFilter);
211 CoTaskMemFree(pTransformFilter);
217 static HRESULT WINAPI TransformFilter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
219 TransformFilterImpl *This = (TransformFilterImpl *)iface;
220 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
224 if (IsEqualIID(riid, &IID_IUnknown))
226 else if (IsEqualIID(riid, &IID_IPersist))
228 else if (IsEqualIID(riid, &IID_IMediaFilter))
230 else if (IsEqualIID(riid, &IID_IBaseFilter))
232 else if (IsEqualIID(riid, &IID_IMediaSeeking))
233 *ppv = &This->mediaSeeking;
237 IUnknown_AddRef((IUnknown *)(*ppv));
241 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
242 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
244 return E_NOINTERFACE;
247 static ULONG WINAPI TransformFilter_AddRef(IBaseFilter * iface)
249 TransformFilterImpl *This = (TransformFilterImpl *)iface;
250 ULONG refCount = InterlockedIncrement(&This->refCount);
252 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
257 static ULONG WINAPI TransformFilter_Release(IBaseFilter * iface)
259 TransformFilterImpl *This = (TransformFilterImpl *)iface;
260 ULONG refCount = InterlockedDecrement(&This->refCount);
262 TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
269 IReferenceClock_Release(This->pClock);
271 for (i = 0; i < 2; i++)
275 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
277 IPin_Disconnect(pConnectedTo);
278 IPin_Release(pConnectedTo);
280 IPin_Disconnect(This->ppPins[i]);
282 IPin_Release(This->ppPins[i]);
285 CoTaskMemFree(This->ppPins);
288 This->csFilter.DebugInfo->Spare[0] = 0;
289 DeleteCriticalSection(&This->csFilter);
291 TRACE("Destroying transform filter\n");
292 FreeMediaType(&This->pmt);
301 /** IPersist methods **/
303 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
305 TransformFilterImpl *This = (TransformFilterImpl *)iface;
307 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
309 *pClsid = This->clsid;
314 /** IMediaFilter methods **/
316 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
318 TransformFilterImpl *This = (TransformFilterImpl *)iface;
320 TRACE("(%p/%p)\n", This, iface);
322 EnterCriticalSection(&This->csFilter);
324 This->state = State_Stopped;
325 if (This->pFuncsTable->pfnProcessEnd)
326 This->pFuncsTable->pfnProcessEnd(This);
328 LeaveCriticalSection(&This->csFilter);
333 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
335 TransformFilterImpl *This = (TransformFilterImpl *)iface;
337 TRACE("(%p/%p)->()\n", This, iface);
339 EnterCriticalSection(&This->csFilter);
341 if (This->state == State_Stopped)
342 IBaseFilter_Run(iface, -1);
344 This->state = State_Paused;
346 LeaveCriticalSection(&This->csFilter);
351 static HRESULT WINAPI TransformFilter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
354 TransformFilterImpl *This = (TransformFilterImpl *)iface;
356 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
358 EnterCriticalSection(&This->csFilter);
360 if (This->state == State_Stopped)
362 ((InputPin *)This->ppPins[0])->end_of_stream = 0;
363 if (This->pFuncsTable->pfnProcessBegin)
364 This->pFuncsTable->pfnProcessBegin(This);
365 OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
368 This->rtStreamStart = tStart;
369 This->state = State_Running;
371 LeaveCriticalSection(&This->csFilter);
376 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
378 TransformFilterImpl *This = (TransformFilterImpl *)iface;
380 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
382 EnterCriticalSection(&This->csFilter);
384 *pState = This->state;
386 LeaveCriticalSection(&This->csFilter);
391 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
393 TransformFilterImpl *This = (TransformFilterImpl *)iface;
395 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
397 EnterCriticalSection(&This->csFilter);
400 IReferenceClock_Release(This->pClock);
401 This->pClock = pClock;
403 IReferenceClock_AddRef(This->pClock);
405 LeaveCriticalSection(&This->csFilter);
410 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
412 TransformFilterImpl *This = (TransformFilterImpl *)iface;
414 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
416 EnterCriticalSection(&This->csFilter);
418 *ppClock = This->pClock;
420 IReferenceClock_AddRef(This->pClock);
422 LeaveCriticalSection(&This->csFilter);
427 /** IBaseFilter implementation **/
429 static HRESULT TransformFilter_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
431 TransformFilterImpl *This = (TransformFilterImpl *)iface;
433 /* Our pins are static, not changing so setting static tick count is ok */
439 *pin = This->ppPins[pos];
444 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
446 TransformFilterImpl *This = (TransformFilterImpl *)iface;
448 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
450 return IEnumPinsImpl_Construct(ppEnum, TransformFilter_GetPin, iface);
453 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
455 TransformFilterImpl *This = (TransformFilterImpl *)iface;
457 TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
462 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
464 TransformFilterImpl *This = (TransformFilterImpl *)iface;
466 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
468 strcpyW(pInfo->achName, This->filterInfo.achName);
469 pInfo->pGraph = This->filterInfo.pGraph;
472 IFilterGraph_AddRef(pInfo->pGraph);
477 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
480 TransformFilterImpl *This = (TransformFilterImpl *)iface;
482 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
484 EnterCriticalSection(&This->csFilter);
487 strcpyW(This->filterInfo.achName, pName);
489 *This->filterInfo.achName = '\0';
490 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
492 LeaveCriticalSection(&This->csFilter);
497 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
499 TransformFilterImpl *This = (TransformFilterImpl *)iface;
500 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
504 static const IBaseFilterVtbl TransformFilter_Vtbl =
506 TransformFilter_QueryInterface,
507 TransformFilter_AddRef,
508 TransformFilter_Release,
509 TransformFilter_GetClassID,
510 TransformFilter_Stop,
511 TransformFilter_Pause,
513 TransformFilter_GetState,
514 TransformFilter_SetSyncSource,
515 TransformFilter_GetSyncSource,
516 TransformFilter_EnumPins,
517 TransformFilter_FindPin,
518 TransformFilter_QueryFilterInfo,
519 TransformFilter_JoinFilterGraph,
520 TransformFilter_QueryVendorInfo
523 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
525 InputPin* This = (InputPin*) iface;
526 TransformFilterImpl* pTransform;
530 TRACE("(%p)->()\n", iface);
532 /* Since we process samples synchronously, just forward notification downstream */
533 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
537 hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
540 hr = IPin_EndOfStream(ppin);
549 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
551 InputPin* This = (InputPin*) iface;
552 TransformFilterImpl* pTransform;
555 TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
557 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
559 hr = pTransform->pFuncsTable->pfnConnectInput(pTransform, pmt);
562 hr = InputPin_ReceiveConnection(iface, pReceivePin, pmt);
564 pTransform->pFuncsTable->pfnCleanup(pTransform);
570 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
572 InputPin* This = (InputPin*) iface;
573 TransformFilterImpl* pTransform;
575 TRACE("(%p)->()\n", iface);
577 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
578 pTransform->pFuncsTable->pfnCleanup(pTransform);
580 return IPinImpl_Disconnect(iface);
583 static const IPinVtbl TransformFilter_InputPin_Vtbl =
585 InputPin_QueryInterface,
589 TransformFilter_InputPin_ReceiveConnection,
590 TransformFilter_InputPin_Disconnect,
591 IPinImpl_ConnectedTo,
592 IPinImpl_ConnectionMediaType,
593 IPinImpl_QueryPinInfo,
594 IPinImpl_QueryDirection,
596 IPinImpl_QueryAccept,
597 IPinImpl_EnumMediaTypes,
598 IPinImpl_QueryInternalConnections,
599 TransformFilter_InputPin_EndOfStream,
605 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
607 IPinImpl *This = (IPinImpl *)iface;
608 TransformFilterImpl *pTransform = (TransformFilterImpl *)This->pinInfo.pFilter;
609 ENUMMEDIADETAILS emd;
611 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
614 emd.pMediaTypes = &pTransform->pmt;
616 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
619 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
621 OutputPin_QueryInterface,
625 OutputPin_ReceiveConnection,
626 OutputPin_Disconnect,
627 IPinImpl_ConnectedTo,
628 IPinImpl_ConnectionMediaType,
629 IPinImpl_QueryPinInfo,
630 IPinImpl_QueryDirection,
632 IPinImpl_QueryAccept,
633 TransformFilter_Output_EnumMediaTypes,
634 IPinImpl_QueryInternalConnections,
635 OutputPin_EndOfStream,
636 OutputPin_BeginFlush,
641 static const IMemInputPinVtbl MemInputPin_Vtbl =
643 MemInputPin_QueryInterface,
646 MemInputPin_GetAllocator,
647 MemInputPin_NotifyAllocator,
648 MemInputPin_GetAllocatorRequirements,
650 MemInputPin_ReceiveMultiple,
651 MemInputPin_ReceiveCanBlock