quartz: Reset EcCompleteCount before starting filters.
[wine/wine64.git] / dlls / quartz / transform.c
blob5928ba39995a5f88aa83d57136f4f945375dbf93
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 "amvideo.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "dshow.h"
31 #include "strmif.h"
32 #include "vfw.h"
34 #include <assert.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;
63 TRACE("%p\n", 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
69 needed) */
70 return S_OK;
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;
78 TRACE("%p\n", iface);
80 if (IsEqualIID(&pmt->majortype, &outpmt->majortype) && IsEqualIID(&pmt->subtype, &outpmt->subtype))
81 return S_OK;
82 return S_FALSE;
86 static inline TransformFilterImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
88 return (TransformFilterImpl *)((char*)iface - FIELD_OFFSET(TransformFilterImpl, mediaSeeking.lpVtbl));
91 static HRESULT WINAPI TransformFilter_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
93 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
95 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
98 static ULONG WINAPI TransformFilter_Seeking_AddRef(IMediaSeeking * iface)
100 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
102 return IUnknown_AddRef((IUnknown *)This);
105 static ULONG WINAPI TransformFilter_Seeking_Release(IMediaSeeking * iface)
107 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
109 return IUnknown_Release((IUnknown *)This);
112 static const IMediaSeekingVtbl TransformFilter_Seeking_Vtbl =
114 TransformFilter_Seeking_QueryInterface,
115 TransformFilter_Seeking_AddRef,
116 TransformFilter_Seeking_Release,
117 MediaSeekingImpl_GetCapabilities,
118 MediaSeekingImpl_CheckCapabilities,
119 MediaSeekingImpl_IsFormatSupported,
120 MediaSeekingImpl_QueryPreferredFormat,
121 MediaSeekingImpl_GetTimeFormat,
122 MediaSeekingImpl_IsUsingTimeFormat,
123 MediaSeekingImpl_SetTimeFormat,
124 MediaSeekingImpl_GetDuration,
125 MediaSeekingImpl_GetStopPosition,
126 MediaSeekingImpl_GetCurrentPosition,
127 MediaSeekingImpl_ConvertTimeFormat,
128 MediaSeekingImpl_SetPositions,
129 MediaSeekingImpl_GetPositions,
130 MediaSeekingImpl_GetAvailable,
131 MediaSeekingImpl_SetRate,
132 MediaSeekingImpl_GetRate,
133 MediaSeekingImpl_GetPreroll
136 static HRESULT TransformFilter_ChangeCurrent(IBaseFilter *iface)
138 FIXME("(%p) filter hasn't implemented current position change!\n", iface);
139 return S_OK;
142 static HRESULT TransformFilter_ChangeStop(IBaseFilter *iface)
144 FIXME("(%p) filter hasn't implemented stop position change!\n", iface);
145 return S_OK;
148 static HRESULT TransformFilter_ChangeRate(IBaseFilter *iface)
150 FIXME("(%p) filter hasn't implemented rate change!\n", iface);
151 return S_OK;
154 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable, CHANGEPROC stop, CHANGEPROC current, CHANGEPROC rate)
156 HRESULT hr;
157 PIN_INFO piInput;
158 PIN_INFO piOutput;
160 /* pTransformFilter is already allocated */
161 pTransformFilter->clsid = *pClsid;
162 pTransformFilter->pFuncsTable = pFuncsTable;
164 pTransformFilter->lpVtbl = &TransformFilter_Vtbl;
166 pTransformFilter->refCount = 1;
167 InitializeCriticalSection(&pTransformFilter->csFilter);
168 pTransformFilter->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TransformFilterImpl.csFilter");
169 pTransformFilter->state = State_Stopped;
170 pTransformFilter->pClock = NULL;
171 ZeroMemory(&pTransformFilter->filterInfo, sizeof(FILTER_INFO));
173 pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
175 /* construct input pin */
176 piInput.dir = PINDIR_INPUT;
177 piInput.pFilter = (IBaseFilter *)pTransformFilter;
178 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
179 piOutput.dir = PINDIR_OUTPUT;
180 piOutput.pFilter = (IBaseFilter *)pTransformFilter;
181 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
183 hr = InputPin_Construct(&TransformFilter_InputPin_Vtbl, &piInput, TransformFilter_Sample, pTransformFilter, TransformFilter_Input_QueryAccept, NULL, &pTransformFilter->csFilter, &pTransformFilter->ppPins[0]);
185 if (SUCCEEDED(hr))
187 ALLOCATOR_PROPERTIES props;
188 props.cbAlign = 1;
189 props.cbPrefix = 0;
190 props.cbBuffer = 0; /* Will be updated at connection time */
191 props.cBuffers = 2;
193 hr = OutputPin_Construct(&TransformFilter_OutputPin_Vtbl, sizeof(OutputPin), &piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
195 if (FAILED(hr))
196 ERR("Cannot create output pin (%x)\n", hr);
197 else
199 if (!stop)
200 stop = TransformFilter_ChangeStop;
201 if (!current)
202 current = TransformFilter_ChangeCurrent;
203 if (!rate)
204 rate = TransformFilter_ChangeRate;
206 MediaSeekingImpl_Init((IBaseFilter*)pTransformFilter, stop, current, rate, &pTransformFilter->mediaSeeking, &pTransformFilter->csFilter);
207 pTransformFilter->mediaSeeking.lpVtbl = &TransformFilter_Seeking_Vtbl;
210 else
212 CoTaskMemFree(pTransformFilter->ppPins);
213 pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
214 DeleteCriticalSection(&pTransformFilter->csFilter);
215 CoTaskMemFree(pTransformFilter);
218 return hr;
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);
226 *ppv = NULL;
228 if (IsEqualIID(riid, &IID_IUnknown))
229 *ppv = (LPVOID)This;
230 else if (IsEqualIID(riid, &IID_IPersist))
231 *ppv = (LPVOID)This;
232 else if (IsEqualIID(riid, &IID_IMediaFilter))
233 *ppv = (LPVOID)This;
234 else if (IsEqualIID(riid, &IID_IBaseFilter))
235 *ppv = (LPVOID)This;
236 else if (IsEqualIID(riid, &IID_IMediaSeeking))
237 *ppv = &This->mediaSeeking;
239 if (*ppv)
241 IUnknown_AddRef((IUnknown *)(*ppv));
242 return S_OK;
245 if (!IsEqualIID(riid, &IID_IPin))
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);
258 return refCount;
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);
268 if (!refCount)
270 ULONG i;
272 if (This->pClock)
273 IReferenceClock_Release(This->pClock);
275 for (i = 0; i < 2; i++)
277 IPin *pConnectedTo;
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);
290 This->lpVtbl = NULL;
292 This->csFilter.DebugInfo->Spare[0] = 0;
293 DeleteCriticalSection(&This->csFilter);
295 TRACE("Destroying transform filter\n");
296 CoTaskMemFree(This);
298 return 0;
300 else
301 return refCount;
304 /** IPersist methods **/
306 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
308 TransformFilterImpl *This = (TransformFilterImpl *)iface;
310 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
312 *pClsid = This->clsid;
314 return S_OK;
317 /** IMediaFilter methods **/
319 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
321 TransformFilterImpl *This = (TransformFilterImpl *)iface;
323 TRACE("(%p/%p)\n", This, iface);
325 EnterCriticalSection(&This->csFilter);
327 This->state = State_Stopped;
328 if (This->pFuncsTable->pfnProcessEnd)
329 This->pFuncsTable->pfnProcessEnd(This);
331 LeaveCriticalSection(&This->csFilter);
333 return S_OK;
336 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
338 TransformFilterImpl *This = (TransformFilterImpl *)iface;
340 TRACE("(%p/%p)->()\n", This, iface);
342 EnterCriticalSection(&This->csFilter);
344 This->state = State_Paused;
346 LeaveCriticalSection(&This->csFilter);
348 return S_OK;
351 static HRESULT WINAPI TransformFilter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
353 HRESULT hr = S_OK;
354 TransformFilterImpl *This = (TransformFilterImpl *)iface;
356 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
358 EnterCriticalSection(&This->csFilter);
360 This->rtStreamStart = tStart;
361 This->state = State_Running;
362 OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
363 if (This->pFuncsTable->pfnProcessBegin)
364 This->pFuncsTable->pfnProcessBegin(This);
366 LeaveCriticalSection(&This->csFilter);
368 return hr;
371 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
373 TransformFilterImpl *This = (TransformFilterImpl *)iface;
375 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
377 EnterCriticalSection(&This->csFilter);
379 *pState = This->state;
381 LeaveCriticalSection(&This->csFilter);
383 return S_OK;
386 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
388 TransformFilterImpl *This = (TransformFilterImpl *)iface;
390 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
392 EnterCriticalSection(&This->csFilter);
394 if (This->pClock)
395 IReferenceClock_Release(This->pClock);
396 This->pClock = pClock;
397 if (This->pClock)
398 IReferenceClock_AddRef(This->pClock);
400 LeaveCriticalSection(&This->csFilter);
402 return S_OK;
405 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
407 TransformFilterImpl *This = (TransformFilterImpl *)iface;
409 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
411 EnterCriticalSection(&This->csFilter);
413 *ppClock = This->pClock;
414 IReferenceClock_AddRef(This->pClock);
416 LeaveCriticalSection(&This->csFilter);
418 return S_OK;
421 /** IBaseFilter implementation **/
423 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
425 ENUMPINDETAILS epd;
426 TransformFilterImpl *This = (TransformFilterImpl *)iface;
428 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
430 epd.cPins = 2; /* input and output pins */
431 epd.ppPins = This->ppPins;
432 return IEnumPinsImpl_Construct(&epd, ppEnum);
435 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
437 TransformFilterImpl *This = (TransformFilterImpl *)iface;
439 TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
441 return E_NOTIMPL;
444 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
446 TransformFilterImpl *This = (TransformFilterImpl *)iface;
448 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
450 strcpyW(pInfo->achName, This->filterInfo.achName);
451 pInfo->pGraph = This->filterInfo.pGraph;
453 if (pInfo->pGraph)
454 IFilterGraph_AddRef(pInfo->pGraph);
456 return S_OK;
459 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
461 HRESULT hr = S_OK;
462 TransformFilterImpl *This = (TransformFilterImpl *)iface;
464 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
466 EnterCriticalSection(&This->csFilter);
468 if (pName)
469 strcpyW(This->filterInfo.achName, pName);
470 else
471 *This->filterInfo.achName = '\0';
472 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
474 LeaveCriticalSection(&This->csFilter);
476 return hr;
479 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
481 TransformFilterImpl *This = (TransformFilterImpl *)iface;
482 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
483 return E_NOTIMPL;
486 static const IBaseFilterVtbl TransformFilter_Vtbl =
488 TransformFilter_QueryInterface,
489 TransformFilter_AddRef,
490 TransformFilter_Release,
491 TransformFilter_GetClassID,
492 TransformFilter_Stop,
493 TransformFilter_Pause,
494 TransformFilter_Run,
495 TransformFilter_GetState,
496 TransformFilter_SetSyncSource,
497 TransformFilter_GetSyncSource,
498 TransformFilter_EnumPins,
499 TransformFilter_FindPin,
500 TransformFilter_QueryFilterInfo,
501 TransformFilter_JoinFilterGraph,
502 TransformFilter_QueryVendorInfo
505 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
507 InputPin* This = (InputPin*) iface;
508 TransformFilterImpl* pTransform;
509 IPin* ppin;
510 HRESULT hr;
512 TRACE("(%p)->()\n", iface);
514 /* Since we process samples synchronously, just forward notification downstream */
515 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
516 if (!pTransform)
517 hr = E_FAIL;
518 else
519 hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
520 if (SUCCEEDED(hr))
522 hr = IPin_EndOfStream(ppin);
523 IPin_Release(ppin);
526 if (FAILED(hr))
527 ERR("%x\n", hr);
528 return hr;
531 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
533 InputPin* This = (InputPin*) iface;
534 TransformFilterImpl* pTransform;
535 HRESULT hr;
537 TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
539 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
541 hr = pTransform->pFuncsTable->pfnConnectInput(pTransform, pmt);
542 if (SUCCEEDED(hr))
544 hr = InputPin_ReceiveConnection(iface, pReceivePin, pmt);
545 if (FAILED(hr))
546 pTransform->pFuncsTable->pfnCleanup(pTransform);
549 return hr;
552 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
554 InputPin* This = (InputPin*) iface;
555 TransformFilterImpl* pTransform;
557 TRACE("(%p)->()\n", iface);
559 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
560 pTransform->pFuncsTable->pfnCleanup(pTransform);
562 return IPinImpl_Disconnect(iface);
565 static const IPinVtbl TransformFilter_InputPin_Vtbl =
567 InputPin_QueryInterface,
568 IPinImpl_AddRef,
569 InputPin_Release,
570 InputPin_Connect,
571 TransformFilter_InputPin_ReceiveConnection,
572 TransformFilter_InputPin_Disconnect,
573 IPinImpl_ConnectedTo,
574 IPinImpl_ConnectionMediaType,
575 IPinImpl_QueryPinInfo,
576 IPinImpl_QueryDirection,
577 IPinImpl_QueryId,
578 IPinImpl_QueryAccept,
579 IPinImpl_EnumMediaTypes,
580 IPinImpl_QueryInternalConnections,
581 TransformFilter_InputPin_EndOfStream,
582 InputPin_BeginFlush,
583 InputPin_EndFlush,
584 InputPin_NewSegment
587 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
589 IPinImpl *This = (IPinImpl *)iface;
590 ENUMMEDIADETAILS emd;
592 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
594 emd.cMediaTypes = 1;
595 emd.pMediaTypes = &This->mtCurrent;
597 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
600 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
602 OutputPin_QueryInterface,
603 IPinImpl_AddRef,
604 OutputPin_Release,
605 OutputPin_Connect,
606 OutputPin_ReceiveConnection,
607 OutputPin_Disconnect,
608 IPinImpl_ConnectedTo,
609 IPinImpl_ConnectionMediaType,
610 IPinImpl_QueryPinInfo,
611 IPinImpl_QueryDirection,
612 IPinImpl_QueryId,
613 IPinImpl_QueryAccept,
614 TransformFilter_Output_EnumMediaTypes,
615 IPinImpl_QueryInternalConnections,
616 OutputPin_EndOfStream,
617 OutputPin_BeginFlush,
618 OutputPin_EndFlush,
619 OutputPin_NewSegment
622 static const IMemInputPinVtbl MemInputPin_Vtbl =
624 MemInputPin_QueryInterface,
625 MemInputPin_AddRef,
626 MemInputPin_Release,
627 MemInputPin_GetAllocator,
628 MemInputPin_NotifyAllocator,
629 MemInputPin_GetAllocatorRequirements,
630 MemInputPin_Receive,
631 MemInputPin_ReceiveMultiple,
632 MemInputPin_ReceiveCanBlock