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"
42 #include "wine/unicode.h"
43 #include "wine/debug.h"
45 #include "transform.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
49 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
50 static const WCHAR wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};
52 static const IBaseFilterVtbl TransformFilter_Vtbl;
53 static const IPinVtbl TransformFilter_InputPin_Vtbl;
54 static const IMemInputPinVtbl MemInputPin_Vtbl;
55 static const IPinVtbl TransformFilter_OutputPin_Vtbl;
57 static HRESULT TransformFilter_Sample(LPVOID iface, IMediaSample * pSample)
59 TransformFilterImpl *This = (TransformFilterImpl *)iface;
60 LPBYTE pbSrcStream = NULL;
62 REFERENCE_TIME tStart, tStop;
65 TRACE("%p %p\n", iface, pSample);
67 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
70 ERR("Cannot get pointer to sample data (%x)\n", hr);
74 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
76 ERR("Cannot get sample time (%x)\n", hr);
78 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
80 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
82 #if 0 /* For debugging purpose */
85 for(i = 0; i < cbSrcStream; i++)
87 if ((i!=0) && !(i%16))
89 TRACE("%02x ", pbSrcStream[i]);
95 This->pFuncsTable->pfnProcessSampleData(This, pbSrcStream, cbSrcStream);
100 static HRESULT TransformFilter_Input_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
102 TransformFilterImpl* This = (TransformFilterImpl*)iface;
103 TRACE("%p\n", iface);
104 dump_AM_MEDIA_TYPE(pmt);
106 return This->pFuncsTable->pfnConnectInput(This, pmt);
110 static HRESULT TransformFilter_Output_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
112 TransformFilterImpl* pTransformFilter = (TransformFilterImpl*)iface;
113 AM_MEDIA_TYPE* outpmt = &((OutputPin*)pTransformFilter->ppPins[1])->pin.mtCurrent;
114 TRACE("%p\n", iface);
116 if (IsEqualIID(&pmt->majortype, &outpmt->majortype) && IsEqualIID(&pmt->subtype, &outpmt->subtype))
121 static HRESULT TransformFilter_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
127 if (pPinInfo->dir != PINDIR_INPUT)
129 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
133 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
136 return E_OUTOFMEMORY;
138 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
140 pPinImpl->pin.lpVtbl = &TransformFilter_InputPin_Vtbl;
141 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
143 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
149 static HRESULT TransformFilter_OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
151 OutputPin * pPinImpl;
155 if (pPinInfo->dir != PINDIR_OUTPUT)
157 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
161 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
164 return E_OUTOFMEMORY;
166 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
168 pPinImpl->pin.lpVtbl = &TransformFilter_OutputPin_Vtbl;
170 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
176 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable)
182 /* pTransformFilter is already allocated */
183 pTransformFilter->clsid = *pClsid;
184 pTransformFilter->pFuncsTable = pFuncsTable;
186 pTransformFilter->lpVtbl = &TransformFilter_Vtbl;
188 pTransformFilter->refCount = 1;
189 InitializeCriticalSection(&pTransformFilter->csFilter);
190 pTransformFilter->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TransformFilterImpl.csFilter");
191 pTransformFilter->state = State_Stopped;
192 pTransformFilter->pClock = NULL;
193 ZeroMemory(&pTransformFilter->filterInfo, sizeof(FILTER_INFO));
195 pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
197 /* construct input pin */
198 piInput.dir = PINDIR_INPUT;
199 piInput.pFilter = (IBaseFilter *)pTransformFilter;
200 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
201 piOutput.dir = PINDIR_OUTPUT;
202 piOutput.pFilter = (IBaseFilter *)pTransformFilter;
203 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
205 hr = TransformFilter_InputPin_Construct(&piInput, TransformFilter_Sample, pTransformFilter, TransformFilter_Input_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[0]);
209 ALLOCATOR_PROPERTIES props;
212 props.cbBuffer = 0; /* Will be updated at connection time */
215 hr = TransformFilter_OutputPin_Construct(&piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
218 ERR("Cannot create output pin (%x)\n", hr);
222 CoTaskMemFree(pTransformFilter->ppPins);
223 pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
224 DeleteCriticalSection(&pTransformFilter->csFilter);
225 CoTaskMemFree(pTransformFilter);
231 static HRESULT WINAPI TransformFilter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
233 TransformFilterImpl *This = (TransformFilterImpl *)iface;
234 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
238 if (IsEqualIID(riid, &IID_IUnknown))
240 else if (IsEqualIID(riid, &IID_IPersist))
242 else if (IsEqualIID(riid, &IID_IMediaFilter))
244 else if (IsEqualIID(riid, &IID_IBaseFilter))
249 IUnknown_AddRef((IUnknown *)(*ppv));
253 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
255 return E_NOINTERFACE;
258 static ULONG WINAPI TransformFilter_AddRef(IBaseFilter * iface)
260 TransformFilterImpl *This = (TransformFilterImpl *)iface;
261 ULONG refCount = InterlockedIncrement(&This->refCount);
263 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
268 static ULONG WINAPI TransformFilter_Release(IBaseFilter * iface)
270 TransformFilterImpl *This = (TransformFilterImpl *)iface;
271 ULONG refCount = InterlockedDecrement(&This->refCount);
273 TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
279 This->csFilter.DebugInfo->Spare[0] = 0;
280 DeleteCriticalSection(&This->csFilter);
283 IReferenceClock_Release(This->pClock);
285 for (i = 0; i < 2; i++)
289 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
291 IPin_Disconnect(pConnectedTo);
292 IPin_Release(pConnectedTo);
294 IPin_Disconnect(This->ppPins[i]);
296 IPin_Release(This->ppPins[i]);
299 CoTaskMemFree(This->ppPins);
302 This->pFuncsTable->pfnCleanup(This);
304 TRACE("Destroying transform filter\n");
313 /** IPersist methods **/
315 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
317 TransformFilterImpl *This = (TransformFilterImpl *)iface;
319 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
321 *pClsid = This->clsid;
326 /** IMediaFilter methods **/
328 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
330 TransformFilterImpl *This = (TransformFilterImpl *)iface;
332 TRACE("(%p/%p)\n", This, iface);
334 EnterCriticalSection(&This->csFilter);
336 This->state = State_Stopped;
337 if (This->pFuncsTable->pfnProcessEnd)
338 This->pFuncsTable->pfnProcessEnd(This);
340 LeaveCriticalSection(&This->csFilter);
345 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
347 TransformFilterImpl *This = (TransformFilterImpl *)iface;
349 TRACE("(%p/%p)->()\n", This, iface);
351 EnterCriticalSection(&This->csFilter);
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 This->rtStreamStart = tStart;
370 This->state = State_Running;
371 OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
372 if (This->pFuncsTable->pfnProcessBegin)
373 This->pFuncsTable->pfnProcessBegin(This);
375 LeaveCriticalSection(&This->csFilter);
380 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
382 TransformFilterImpl *This = (TransformFilterImpl *)iface;
384 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
386 EnterCriticalSection(&This->csFilter);
388 *pState = This->state;
390 LeaveCriticalSection(&This->csFilter);
395 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
397 TransformFilterImpl *This = (TransformFilterImpl *)iface;
399 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
401 EnterCriticalSection(&This->csFilter);
404 IReferenceClock_Release(This->pClock);
405 This->pClock = pClock;
407 IReferenceClock_AddRef(This->pClock);
409 LeaveCriticalSection(&This->csFilter);
414 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
416 TransformFilterImpl *This = (TransformFilterImpl *)iface;
418 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
420 EnterCriticalSection(&This->csFilter);
422 *ppClock = This->pClock;
423 IReferenceClock_AddRef(This->pClock);
425 LeaveCriticalSection(&This->csFilter);
430 /** IBaseFilter implementation **/
432 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
435 TransformFilterImpl *This = (TransformFilterImpl *)iface;
437 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
439 epd.cPins = 2; /* input and output pins */
440 epd.ppPins = This->ppPins;
441 return IEnumPinsImpl_Construct(&epd, ppEnum);
444 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
446 TransformFilterImpl *This = (TransformFilterImpl *)iface;
448 TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
453 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
455 TransformFilterImpl *This = (TransformFilterImpl *)iface;
457 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
459 strcpyW(pInfo->achName, This->filterInfo.achName);
460 pInfo->pGraph = This->filterInfo.pGraph;
463 IFilterGraph_AddRef(pInfo->pGraph);
468 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
471 TransformFilterImpl *This = (TransformFilterImpl *)iface;
473 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
475 EnterCriticalSection(&This->csFilter);
478 strcpyW(This->filterInfo.achName, pName);
480 *This->filterInfo.achName = '\0';
481 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
483 LeaveCriticalSection(&This->csFilter);
488 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
490 TransformFilterImpl *This = (TransformFilterImpl *)iface;
491 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
495 static const IBaseFilterVtbl TransformFilter_Vtbl =
497 TransformFilter_QueryInterface,
498 TransformFilter_AddRef,
499 TransformFilter_Release,
500 TransformFilter_GetClassID,
501 TransformFilter_Stop,
502 TransformFilter_Pause,
504 TransformFilter_GetState,
505 TransformFilter_SetSyncSource,
506 TransformFilter_GetSyncSource,
507 TransformFilter_EnumPins,
508 TransformFilter_FindPin,
509 TransformFilter_QueryFilterInfo,
510 TransformFilter_JoinFilterGraph,
511 TransformFilter_QueryVendorInfo
514 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
516 InputPin* This = (InputPin*) iface;
517 TransformFilterImpl* pTransform;
521 TRACE("(%p)->()\n", iface);
523 /* Since we process samples synchronously, just forward notification downstream */
524 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
528 hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
531 hr = IPin_EndOfStream(ppin);
540 static const IPinVtbl TransformFilter_InputPin_Vtbl =
542 InputPin_QueryInterface,
546 InputPin_ReceiveConnection,
548 IPinImpl_ConnectedTo,
549 IPinImpl_ConnectionMediaType,
550 IPinImpl_QueryPinInfo,
551 IPinImpl_QueryDirection,
553 IPinImpl_QueryAccept,
554 IPinImpl_EnumMediaTypes,
555 IPinImpl_QueryInternalConnections,
556 TransformFilter_InputPin_EndOfStream,
562 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
564 IPinImpl *This = (IPinImpl *)iface;
565 ENUMMEDIADETAILS emd;
567 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
570 emd.pMediaTypes = &This->mtCurrent;
572 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
575 static HRESULT WINAPI TransformFilter_Output_Disconnect(IPin * iface)
577 OutputPin* This = (OutputPin*)iface;
579 TransformFilterImpl* pTransformFilter = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
581 TRACE("(%p/%p)->()\n", This, iface);
583 hr = OutputPin_Disconnect(iface);
587 pTransformFilter->pFuncsTable->pfnCleanup(pTransformFilter);
593 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
595 OutputPin_QueryInterface,
599 OutputPin_ReceiveConnection,
600 TransformFilter_Output_Disconnect,
601 IPinImpl_ConnectedTo,
602 IPinImpl_ConnectionMediaType,
603 IPinImpl_QueryPinInfo,
604 IPinImpl_QueryDirection,
606 IPinImpl_QueryAccept,
607 TransformFilter_Output_EnumMediaTypes,
608 IPinImpl_QueryInternalConnections,
609 OutputPin_EndOfStream,
610 OutputPin_BeginFlush,
615 static const IMemInputPinVtbl MemInputPin_Vtbl =
617 MemInputPin_QueryInterface,
620 MemInputPin_GetAllocator,
621 MemInputPin_NotifyAllocator,
622 MemInputPin_GetAllocatorRequirements,
624 MemInputPin_ReceiveMultiple,
625 MemInputPin_ReceiveCanBlock