quartz: Pass the media sample to the individual transform filter callbacks.
[wine/gsoc_dplay.git] / dlls / quartz / transform.c
blob13bd308f87282921db0799b7c7552d2dfa4c151f
1 /*
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
21 #include "config.h"
23 #include "quartz_private.h"
24 #include "control_private.h"
25 #include "pin.h"
27 #include "uuids.h"
28 #include "aviriff.h"
29 #include "mmreg.h"
30 #include "vfwmsgs.h"
31 #include "amvideo.h"
32 #include "windef.h"
33 #include "winbase.h"
34 #include "dshow.h"
35 #include "strmif.h"
36 #include "vfwmsgs.h"
37 #include "evcode.h"
38 #include "vfw.h"
40 #include <assert.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;
61 TRACE("%p %p\n", iface, pSample);
63 return This->pFuncsTable->pfnProcessSampleData(This, pSample);
66 static HRESULT TransformFilter_Input_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
68 TransformFilterImpl* This = (TransformFilterImpl*)iface;
69 TRACE("%p\n", iface);
70 dump_AM_MEDIA_TYPE(pmt);
72 if (This->pFuncsTable->pfnQueryConnect)
73 return This->pFuncsTable->pfnQueryConnect(This, pmt);
74 /* Assume OK if there's no query method (the connection will fail if
75 needed) */
76 return S_OK;
80 static HRESULT TransformFilter_Output_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
82 TransformFilterImpl* pTransformFilter = (TransformFilterImpl*)iface;
83 AM_MEDIA_TYPE* outpmt = &((OutputPin*)pTransformFilter->ppPins[1])->pin.mtCurrent;
84 TRACE("%p\n", iface);
86 if (IsEqualIID(&pmt->majortype, &outpmt->majortype) && IsEqualIID(&pmt->subtype, &outpmt->subtype))
87 return S_OK;
88 return S_FALSE;
91 static HRESULT TransformFilter_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
93 InputPin * pPinImpl;
95 *ppPin = NULL;
97 if (pPinInfo->dir != PINDIR_INPUT)
99 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
100 return E_INVALIDARG;
103 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
105 if (!pPinImpl)
106 return E_OUTOFMEMORY;
108 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
110 pPinImpl->pin.lpVtbl = &TransformFilter_InputPin_Vtbl;
111 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
113 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
114 return S_OK;
116 return E_FAIL;
119 static HRESULT TransformFilter_OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
121 OutputPin * pPinImpl;
123 *ppPin = NULL;
125 if (pPinInfo->dir != PINDIR_OUTPUT)
127 ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
128 return E_INVALIDARG;
131 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
133 if (!pPinImpl)
134 return E_OUTOFMEMORY;
136 if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
138 pPinImpl->pin.lpVtbl = &TransformFilter_OutputPin_Vtbl;
140 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
141 return S_OK;
143 return E_FAIL;
146 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable)
148 HRESULT hr;
149 PIN_INFO piInput;
150 PIN_INFO piOutput;
152 /* pTransformFilter is already allocated */
153 pTransformFilter->clsid = *pClsid;
154 pTransformFilter->pFuncsTable = pFuncsTable;
156 pTransformFilter->lpVtbl = &TransformFilter_Vtbl;
158 pTransformFilter->refCount = 1;
159 InitializeCriticalSection(&pTransformFilter->csFilter);
160 pTransformFilter->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TransformFilterImpl.csFilter");
161 pTransformFilter->state = State_Stopped;
162 pTransformFilter->pClock = NULL;
163 ZeroMemory(&pTransformFilter->filterInfo, sizeof(FILTER_INFO));
165 pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
167 /* construct input pin */
168 piInput.dir = PINDIR_INPUT;
169 piInput.pFilter = (IBaseFilter *)pTransformFilter;
170 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
171 piOutput.dir = PINDIR_OUTPUT;
172 piOutput.pFilter = (IBaseFilter *)pTransformFilter;
173 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
175 hr = TransformFilter_InputPin_Construct(&piInput, TransformFilter_Sample, pTransformFilter, TransformFilter_Input_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[0]);
177 if (SUCCEEDED(hr))
179 ALLOCATOR_PROPERTIES props;
180 props.cbAlign = 1;
181 props.cbPrefix = 0;
182 props.cbBuffer = 0; /* Will be updated at connection time */
183 props.cBuffers = 2;
185 hr = TransformFilter_OutputPin_Construct(&piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
187 if (FAILED(hr))
188 ERR("Cannot create output pin (%x)\n", hr);
190 else
192 CoTaskMemFree(pTransformFilter->ppPins);
193 pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
194 DeleteCriticalSection(&pTransformFilter->csFilter);
195 CoTaskMemFree(pTransformFilter);
198 return hr;
201 static HRESULT WINAPI TransformFilter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
203 TransformFilterImpl *This = (TransformFilterImpl *)iface;
204 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
206 *ppv = NULL;
208 if (IsEqualIID(riid, &IID_IUnknown))
209 *ppv = (LPVOID)This;
210 else if (IsEqualIID(riid, &IID_IPersist))
211 *ppv = (LPVOID)This;
212 else if (IsEqualIID(riid, &IID_IMediaFilter))
213 *ppv = (LPVOID)This;
214 else if (IsEqualIID(riid, &IID_IBaseFilter))
215 *ppv = (LPVOID)This;
217 if (*ppv)
219 IUnknown_AddRef((IUnknown *)(*ppv));
220 return S_OK;
223 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
225 return E_NOINTERFACE;
228 static ULONG WINAPI TransformFilter_AddRef(IBaseFilter * iface)
230 TransformFilterImpl *This = (TransformFilterImpl *)iface;
231 ULONG refCount = InterlockedIncrement(&This->refCount);
233 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
235 return refCount;
238 static ULONG WINAPI TransformFilter_Release(IBaseFilter * iface)
240 TransformFilterImpl *This = (TransformFilterImpl *)iface;
241 ULONG refCount = InterlockedDecrement(&This->refCount);
243 TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
245 if (!refCount)
247 ULONG i;
249 if (This->pClock)
250 IReferenceClock_Release(This->pClock);
252 for (i = 0; i < 2; i++)
254 IPin *pConnectedTo;
256 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
258 IPin_Disconnect(pConnectedTo);
259 IPin_Release(pConnectedTo);
261 IPin_Disconnect(This->ppPins[i]);
263 IPin_Release(This->ppPins[i]);
266 CoTaskMemFree(This->ppPins);
267 This->lpVtbl = NULL;
269 This->csFilter.DebugInfo->Spare[0] = 0;
270 DeleteCriticalSection(&This->csFilter);
272 TRACE("Destroying transform filter\n");
273 CoTaskMemFree(This);
275 return 0;
277 else
278 return refCount;
281 /** IPersist methods **/
283 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
285 TransformFilterImpl *This = (TransformFilterImpl *)iface;
287 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
289 *pClsid = This->clsid;
291 return S_OK;
294 /** IMediaFilter methods **/
296 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
298 TransformFilterImpl *This = (TransformFilterImpl *)iface;
300 TRACE("(%p/%p)\n", This, iface);
302 EnterCriticalSection(&This->csFilter);
304 This->state = State_Stopped;
305 if (This->pFuncsTable->pfnProcessEnd)
306 This->pFuncsTable->pfnProcessEnd(This);
308 LeaveCriticalSection(&This->csFilter);
310 return S_OK;
313 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
315 TransformFilterImpl *This = (TransformFilterImpl *)iface;
317 TRACE("(%p/%p)->()\n", This, iface);
319 EnterCriticalSection(&This->csFilter);
321 This->state = State_Paused;
323 LeaveCriticalSection(&This->csFilter);
325 return S_OK;
328 static HRESULT WINAPI TransformFilter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
330 HRESULT hr = S_OK;
331 TransformFilterImpl *This = (TransformFilterImpl *)iface;
333 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
335 EnterCriticalSection(&This->csFilter);
337 This->rtStreamStart = tStart;
338 This->state = State_Running;
339 OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
340 if (This->pFuncsTable->pfnProcessBegin)
341 This->pFuncsTable->pfnProcessBegin(This);
343 LeaveCriticalSection(&This->csFilter);
345 return hr;
348 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
350 TransformFilterImpl *This = (TransformFilterImpl *)iface;
352 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
354 EnterCriticalSection(&This->csFilter);
356 *pState = This->state;
358 LeaveCriticalSection(&This->csFilter);
360 return S_OK;
363 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
365 TransformFilterImpl *This = (TransformFilterImpl *)iface;
367 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
369 EnterCriticalSection(&This->csFilter);
371 if (This->pClock)
372 IReferenceClock_Release(This->pClock);
373 This->pClock = pClock;
374 if (This->pClock)
375 IReferenceClock_AddRef(This->pClock);
377 LeaveCriticalSection(&This->csFilter);
379 return S_OK;
382 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
384 TransformFilterImpl *This = (TransformFilterImpl *)iface;
386 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
388 EnterCriticalSection(&This->csFilter);
390 *ppClock = This->pClock;
391 IReferenceClock_AddRef(This->pClock);
393 LeaveCriticalSection(&This->csFilter);
395 return S_OK;
398 /** IBaseFilter implementation **/
400 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
402 ENUMPINDETAILS epd;
403 TransformFilterImpl *This = (TransformFilterImpl *)iface;
405 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
407 epd.cPins = 2; /* input and output pins */
408 epd.ppPins = This->ppPins;
409 return IEnumPinsImpl_Construct(&epd, ppEnum);
412 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
414 TransformFilterImpl *This = (TransformFilterImpl *)iface;
416 TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
418 return E_NOTIMPL;
421 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
423 TransformFilterImpl *This = (TransformFilterImpl *)iface;
425 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
427 strcpyW(pInfo->achName, This->filterInfo.achName);
428 pInfo->pGraph = This->filterInfo.pGraph;
430 if (pInfo->pGraph)
431 IFilterGraph_AddRef(pInfo->pGraph);
433 return S_OK;
436 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
438 HRESULT hr = S_OK;
439 TransformFilterImpl *This = (TransformFilterImpl *)iface;
441 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
443 EnterCriticalSection(&This->csFilter);
445 if (pName)
446 strcpyW(This->filterInfo.achName, pName);
447 else
448 *This->filterInfo.achName = '\0';
449 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
451 LeaveCriticalSection(&This->csFilter);
453 return hr;
456 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
458 TransformFilterImpl *This = (TransformFilterImpl *)iface;
459 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
460 return E_NOTIMPL;
463 static const IBaseFilterVtbl TransformFilter_Vtbl =
465 TransformFilter_QueryInterface,
466 TransformFilter_AddRef,
467 TransformFilter_Release,
468 TransformFilter_GetClassID,
469 TransformFilter_Stop,
470 TransformFilter_Pause,
471 TransformFilter_Run,
472 TransformFilter_GetState,
473 TransformFilter_SetSyncSource,
474 TransformFilter_GetSyncSource,
475 TransformFilter_EnumPins,
476 TransformFilter_FindPin,
477 TransformFilter_QueryFilterInfo,
478 TransformFilter_JoinFilterGraph,
479 TransformFilter_QueryVendorInfo
482 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
484 InputPin* This = (InputPin*) iface;
485 TransformFilterImpl* pTransform;
486 IPin* ppin;
487 HRESULT hr;
489 TRACE("(%p)->()\n", iface);
491 /* Since we process samples synchronously, just forward notification downstream */
492 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
493 if (!pTransform)
494 hr = E_FAIL;
495 else
496 hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
497 if (SUCCEEDED(hr))
499 hr = IPin_EndOfStream(ppin);
500 IPin_Release(ppin);
503 if (FAILED(hr))
504 ERR("%x\n", hr);
505 return hr;
508 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
510 InputPin* This = (InputPin*) iface;
511 TransformFilterImpl* pTransform;
512 HRESULT hr;
514 TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
516 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
518 hr = pTransform->pFuncsTable->pfnConnectInput(pTransform, pmt);
519 if (SUCCEEDED(hr))
521 hr = InputPin_ReceiveConnection(iface, pReceivePin, pmt);
522 if (FAILED(hr))
523 pTransform->pFuncsTable->pfnCleanup(pTransform);
526 return hr;
529 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
531 InputPin* This = (InputPin*) iface;
532 TransformFilterImpl* pTransform;
534 TRACE("(%p)->()\n", iface);
536 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
537 pTransform->pFuncsTable->pfnCleanup(pTransform);
539 return IPinImpl_Disconnect(iface);
542 static const IPinVtbl TransformFilter_InputPin_Vtbl =
544 InputPin_QueryInterface,
545 IPinImpl_AddRef,
546 InputPin_Release,
547 InputPin_Connect,
548 TransformFilter_InputPin_ReceiveConnection,
549 TransformFilter_InputPin_Disconnect,
550 IPinImpl_ConnectedTo,
551 IPinImpl_ConnectionMediaType,
552 IPinImpl_QueryPinInfo,
553 IPinImpl_QueryDirection,
554 IPinImpl_QueryId,
555 IPinImpl_QueryAccept,
556 IPinImpl_EnumMediaTypes,
557 IPinImpl_QueryInternalConnections,
558 TransformFilter_InputPin_EndOfStream,
559 InputPin_BeginFlush,
560 InputPin_EndFlush,
561 InputPin_NewSegment
564 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
566 IPinImpl *This = (IPinImpl *)iface;
567 ENUMMEDIADETAILS emd;
569 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
571 emd.cMediaTypes = 1;
572 emd.pMediaTypes = &This->mtCurrent;
574 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
577 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
579 OutputPin_QueryInterface,
580 IPinImpl_AddRef,
581 OutputPin_Release,
582 OutputPin_Connect,
583 OutputPin_ReceiveConnection,
584 OutputPin_Disconnect,
585 IPinImpl_ConnectedTo,
586 IPinImpl_ConnectionMediaType,
587 IPinImpl_QueryPinInfo,
588 IPinImpl_QueryDirection,
589 IPinImpl_QueryId,
590 IPinImpl_QueryAccept,
591 TransformFilter_Output_EnumMediaTypes,
592 IPinImpl_QueryInternalConnections,
593 OutputPin_EndOfStream,
594 OutputPin_BeginFlush,
595 OutputPin_EndFlush,
596 OutputPin_NewSegment
599 static const IMemInputPinVtbl MemInputPin_Vtbl =
601 MemInputPin_QueryInterface,
602 MemInputPin_AddRef,
603 MemInputPin_Release,
604 MemInputPin_GetAllocator,
605 MemInputPin_NotifyAllocator,
606 MemInputPin_GetAllocatorRequirements,
607 MemInputPin_Receive,
608 MemInputPin_ReceiveMultiple,
609 MemInputPin_ReceiveCanBlock