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 *)((IPinImpl *)iface)->pinInfo.pFilter;
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 = &pTransformFilter->pmt;
71 if (IsEqualIID(&pmt->majortype, &outpmt->majortype)
72 && (IsEqualIID(&pmt->subtype, &outpmt->subtype) || IsEqualIID(&outpmt->subtype, &GUID_NULL)))
78 static inline TransformFilterImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
80 return (TransformFilterImpl *)((char*)iface - FIELD_OFFSET(TransformFilterImpl, mediaSeeking.lpVtbl));
83 static HRESULT WINAPI TransformFilter_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
85 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
87 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
90 static ULONG WINAPI TransformFilter_Seeking_AddRef(IMediaSeeking * iface)
92 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
94 return IUnknown_AddRef((IUnknown *)This);
97 static ULONG WINAPI TransformFilter_Seeking_Release(IMediaSeeking * iface)
99 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
101 return IUnknown_Release((IUnknown *)This);
104 static const IMediaSeekingVtbl TransformFilter_Seeking_Vtbl =
106 TransformFilter_Seeking_QueryInterface,
107 TransformFilter_Seeking_AddRef,
108 TransformFilter_Seeking_Release,
109 MediaSeekingImpl_GetCapabilities,
110 MediaSeekingImpl_CheckCapabilities,
111 MediaSeekingImpl_IsFormatSupported,
112 MediaSeekingImpl_QueryPreferredFormat,
113 MediaSeekingImpl_GetTimeFormat,
114 MediaSeekingImpl_IsUsingTimeFormat,
115 MediaSeekingImpl_SetTimeFormat,
116 MediaSeekingImpl_GetDuration,
117 MediaSeekingImpl_GetStopPosition,
118 MediaSeekingImpl_GetCurrentPosition,
119 MediaSeekingImpl_ConvertTimeFormat,
120 MediaSeekingImpl_SetPositions,
121 MediaSeekingImpl_GetPositions,
122 MediaSeekingImpl_GetAvailable,
123 MediaSeekingImpl_SetRate,
124 MediaSeekingImpl_GetRate,
125 MediaSeekingImpl_GetPreroll
128 /* These shouldn't be implemented by default.
129 * Usually only source filters should implement these
130 * and even it's not needed all of the time
132 static HRESULT TransformFilter_ChangeCurrent(IBaseFilter *iface)
134 TRACE("(%p) filter hasn't implemented current position change!\n", iface);
138 static HRESULT TransformFilter_ChangeStop(IBaseFilter *iface)
140 TRACE("(%p) filter hasn't implemented stop position change!\n", iface);
144 static HRESULT TransformFilter_ChangeRate(IBaseFilter *iface)
146 TRACE("(%p) filter hasn't implemented rate change!\n", iface);
150 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable, CHANGEPROC stop, CHANGEPROC current, CHANGEPROC rate)
156 /* pTransformFilter is already allocated */
157 pTransformFilter->clsid = *pClsid;
158 pTransformFilter->pFuncsTable = pFuncsTable;
160 pTransformFilter->lpVtbl = &TransformFilter_Vtbl;
162 pTransformFilter->refCount = 1;
163 InitializeCriticalSection(&pTransformFilter->csFilter);
164 pTransformFilter->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TransformFilterImpl.csFilter");
165 pTransformFilter->state = State_Stopped;
166 pTransformFilter->pClock = NULL;
167 ZeroMemory(&pTransformFilter->filterInfo, sizeof(FILTER_INFO));
168 ZeroMemory(&pTransformFilter->pmt, sizeof(pTransformFilter->pmt));
169 pTransformFilter->npins = 2;
171 pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
173 /* construct input pin */
174 piInput.dir = PINDIR_INPUT;
175 piInput.pFilter = (IBaseFilter *)pTransformFilter;
176 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
177 piOutput.dir = PINDIR_OUTPUT;
178 piOutput.pFilter = (IBaseFilter *)pTransformFilter;
179 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
181 hr = InputPin_Construct(&TransformFilter_InputPin_Vtbl, &piInput, (SAMPLEPROC_PUSH)pFuncsTable->pfnProcessSampleData, NULL, TransformFilter_Input_QueryAccept, NULL, &pTransformFilter->csFilter, NULL, &pTransformFilter->ppPins[0]);
185 ALLOCATOR_PROPERTIES props;
188 props.cbBuffer = 0; /* Will be updated at connection time */
191 ((InputPin *)pTransformFilter->ppPins[0])->pin.pUserData = pTransformFilter->ppPins[0];
193 hr = OutputPin_Construct(&TransformFilter_OutputPin_Vtbl, sizeof(OutputPin), &piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
196 ERR("Cannot create output pin (%x)\n", hr);
200 stop = TransformFilter_ChangeStop;
202 current = TransformFilter_ChangeCurrent;
204 rate = TransformFilter_ChangeRate;
206 MediaSeekingImpl_Init((IBaseFilter*)pTransformFilter, stop, current, rate, &pTransformFilter->mediaSeeking, &pTransformFilter->csFilter);
207 pTransformFilter->mediaSeeking.lpVtbl = &TransformFilter_Seeking_Vtbl;
212 CoTaskMemFree(pTransformFilter->ppPins);
213 pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
214 DeleteCriticalSection(&pTransformFilter->csFilter);
215 CoTaskMemFree(pTransformFilter);
221 static HRESULT WINAPI TransformFilter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
223 TransformFilterImpl *This = (TransformFilterImpl *)iface;
224 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
228 if (IsEqualIID(riid, &IID_IUnknown))
230 else if (IsEqualIID(riid, &IID_IPersist))
232 else if (IsEqualIID(riid, &IID_IMediaFilter))
234 else if (IsEqualIID(riid, &IID_IBaseFilter))
236 else if (IsEqualIID(riid, &IID_IMediaSeeking))
237 *ppv = &This->mediaSeeking;
241 IUnknown_AddRef((IUnknown *)(*ppv));
245 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
246 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
248 return E_NOINTERFACE;
251 static ULONG WINAPI TransformFilter_AddRef(IBaseFilter * iface)
253 TransformFilterImpl *This = (TransformFilterImpl *)iface;
254 ULONG refCount = InterlockedIncrement(&This->refCount);
256 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
261 static ULONG WINAPI TransformFilter_Release(IBaseFilter * iface)
263 TransformFilterImpl *This = (TransformFilterImpl *)iface;
264 ULONG refCount = InterlockedDecrement(&This->refCount);
266 TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
273 IReferenceClock_Release(This->pClock);
275 for (i = 0; i < This->npins; i++)
279 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
281 IPin_Disconnect(pConnectedTo);
282 IPin_Release(pConnectedTo);
284 IPin_Disconnect(This->ppPins[i]);
286 IPin_Release(This->ppPins[i]);
289 CoTaskMemFree(This->ppPins);
292 This->csFilter.DebugInfo->Spare[0] = 0;
293 DeleteCriticalSection(&This->csFilter);
295 TRACE("Destroying transform filter\n");
296 FreeMediaType(&This->pmt);
305 /** IPersist methods **/
307 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
309 TransformFilterImpl *This = (TransformFilterImpl *)iface;
311 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
313 *pClsid = This->clsid;
318 /** IMediaFilter methods **/
320 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
322 TransformFilterImpl *This = (TransformFilterImpl *)iface;
325 TRACE("(%p/%p)\n", This, iface);
327 EnterCriticalSection(&This->csFilter);
329 This->state = State_Stopped;
330 if (This->pFuncsTable->pfnProcessEnd)
331 hr = This->pFuncsTable->pfnProcessEnd(This);
333 LeaveCriticalSection(&This->csFilter);
338 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
340 TransformFilterImpl *This = (TransformFilterImpl *)iface;
343 TRACE("(%p/%p)->()\n", This, iface);
345 EnterCriticalSection(&This->csFilter);
347 if (This->state == State_Stopped)
348 hr = IBaseFilter_Run(iface, -1);
353 This->state = State_Paused;
355 LeaveCriticalSection(&This->csFilter);
360 static HRESULT WINAPI TransformFilter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
363 TransformFilterImpl *This = (TransformFilterImpl *)iface;
365 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
367 EnterCriticalSection(&This->csFilter);
369 if (This->state == State_Stopped)
371 ((InputPin *)This->ppPins[0])->end_of_stream = 0;
372 if (This->pFuncsTable->pfnProcessBegin)
373 hr = This->pFuncsTable->pfnProcessBegin(This);
375 hr = OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
380 This->rtStreamStart = tStart;
381 This->state = State_Running;
384 LeaveCriticalSection(&This->csFilter);
389 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
391 TransformFilterImpl *This = (TransformFilterImpl *)iface;
393 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
395 EnterCriticalSection(&This->csFilter);
397 *pState = This->state;
399 LeaveCriticalSection(&This->csFilter);
404 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
406 TransformFilterImpl *This = (TransformFilterImpl *)iface;
408 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
410 EnterCriticalSection(&This->csFilter);
413 IReferenceClock_Release(This->pClock);
414 This->pClock = pClock;
416 IReferenceClock_AddRef(This->pClock);
418 LeaveCriticalSection(&This->csFilter);
423 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
425 TransformFilterImpl *This = (TransformFilterImpl *)iface;
427 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
429 EnterCriticalSection(&This->csFilter);
431 *ppClock = This->pClock;
433 IReferenceClock_AddRef(This->pClock);
435 LeaveCriticalSection(&This->csFilter);
440 /** IBaseFilter implementation **/
442 static HRESULT TransformFilter_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
444 TransformFilterImpl *This = (TransformFilterImpl *)iface;
446 /* Our pins are static, not changing so setting static tick count is ok */
449 if (pos >= This->npins)
452 *pin = This->ppPins[pos];
457 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
459 TransformFilterImpl *This = (TransformFilterImpl *)iface;
461 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
463 return IEnumPinsImpl_Construct(ppEnum, TransformFilter_GetPin, iface);
466 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
468 TransformFilterImpl *This = (TransformFilterImpl *)iface;
470 TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
475 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
477 TransformFilterImpl *This = (TransformFilterImpl *)iface;
479 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
481 strcpyW(pInfo->achName, This->filterInfo.achName);
482 pInfo->pGraph = This->filterInfo.pGraph;
485 IFilterGraph_AddRef(pInfo->pGraph);
490 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
493 TransformFilterImpl *This = (TransformFilterImpl *)iface;
495 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
497 EnterCriticalSection(&This->csFilter);
500 strcpyW(This->filterInfo.achName, pName);
502 *This->filterInfo.achName = '\0';
503 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
505 LeaveCriticalSection(&This->csFilter);
510 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
512 TransformFilterImpl *This = (TransformFilterImpl *)iface;
513 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
517 static const IBaseFilterVtbl TransformFilter_Vtbl =
519 TransformFilter_QueryInterface,
520 TransformFilter_AddRef,
521 TransformFilter_Release,
522 TransformFilter_GetClassID,
523 TransformFilter_Stop,
524 TransformFilter_Pause,
526 TransformFilter_GetState,
527 TransformFilter_SetSyncSource,
528 TransformFilter_GetSyncSource,
529 TransformFilter_EnumPins,
530 TransformFilter_FindPin,
531 TransformFilter_QueryFilterInfo,
532 TransformFilter_JoinFilterGraph,
533 TransformFilter_QueryVendorInfo
536 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
538 InputPin* This = (InputPin*) iface;
539 TransformFilterImpl* pTransform;
543 TRACE("(%p)->()\n", iface);
545 /* Since we process samples synchronously, just forward notification downstream */
546 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
550 hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
553 hr = IPin_EndOfStream(ppin);
562 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
564 InputPin* This = (InputPin*) iface;
565 TransformFilterImpl* pTransform;
568 TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
570 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
572 hr = pTransform->pFuncsTable->pfnConnectInput(This, pmt);
575 hr = InputPin_ReceiveConnection(iface, pReceivePin, pmt);
577 pTransform->pFuncsTable->pfnCleanup(This);
583 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
585 InputPin* This = (InputPin*) iface;
586 TransformFilterImpl* pTransform;
588 TRACE("(%p)->()\n", iface);
590 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
591 pTransform->pFuncsTable->pfnCleanup(This);
593 return IPinImpl_Disconnect(iface);
596 static const IPinVtbl TransformFilter_InputPin_Vtbl =
598 InputPin_QueryInterface,
602 TransformFilter_InputPin_ReceiveConnection,
603 TransformFilter_InputPin_Disconnect,
604 IPinImpl_ConnectedTo,
605 IPinImpl_ConnectionMediaType,
606 IPinImpl_QueryPinInfo,
607 IPinImpl_QueryDirection,
609 IPinImpl_QueryAccept,
610 IPinImpl_EnumMediaTypes,
611 IPinImpl_QueryInternalConnections,
612 TransformFilter_InputPin_EndOfStream,
618 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
620 IPinImpl *This = (IPinImpl *)iface;
621 TransformFilterImpl *pTransform = (TransformFilterImpl *)This->pinInfo.pFilter;
622 ENUMMEDIADETAILS emd;
624 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
627 emd.pMediaTypes = &pTransform->pmt;
629 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
632 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
634 OutputPin_QueryInterface,
638 OutputPin_ReceiveConnection,
639 OutputPin_Disconnect,
640 IPinImpl_ConnectedTo,
641 IPinImpl_ConnectionMediaType,
642 IPinImpl_QueryPinInfo,
643 IPinImpl_QueryDirection,
645 IPinImpl_QueryAccept,
646 TransformFilter_Output_EnumMediaTypes,
647 IPinImpl_QueryInternalConnections,
648 OutputPin_EndOfStream,
649 OutputPin_BeginFlush,
654 static const IMemInputPinVtbl MemInputPin_Vtbl =
656 MemInputPin_QueryInterface,
659 MemInputPin_GetAllocator,
660 MemInputPin_NotifyAllocator,
661 MemInputPin_GetAllocatorRequirements,
663 MemInputPin_ReceiveMultiple,
664 MemInputPin_ReceiveCanBlock