push 319afc849e77c7d120112c7718dd0b4fc9a636fe
[wine/hacks.git] / dlls / quartz / transform.c
blob9df01f2e7f080ba52237c92d27df7d0bdc649ad2
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_Input_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
53 TransformFilterImpl* This = (TransformFilterImpl*)iface;
54 TRACE("%p\n", 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
60 needed) */
61 return S_OK;
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;
69 TRACE("%p\n", iface);
71 if (IsEqualIID(&pmt->majortype, &outpmt->majortype) && IsEqualIID(&pmt->subtype, &outpmt->subtype))
72 return S_OK;
73 return S_FALSE;
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);
134 return S_OK;
137 static HRESULT TransformFilter_ChangeStop(IBaseFilter *iface)
139 TRACE("(%p) filter hasn't implemented stop position change!\n", iface);
140 return S_OK;
143 static HRESULT TransformFilter_ChangeRate(IBaseFilter *iface)
145 TRACE("(%p) filter hasn't implemented rate change!\n", iface);
146 return S_OK;
149 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable, CHANGEPROC stop, CHANGEPROC current, CHANGEPROC rate)
151 HRESULT hr;
152 PIN_INFO piInput;
153 PIN_INFO piOutput;
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));
168 pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
170 /* construct input pin */
171 piInput.dir = PINDIR_INPUT;
172 piInput.pFilter = (IBaseFilter *)pTransformFilter;
173 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
174 piOutput.dir = PINDIR_OUTPUT;
175 piOutput.pFilter = (IBaseFilter *)pTransformFilter;
176 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
178 hr = InputPin_Construct(&TransformFilter_InputPin_Vtbl, &piInput, (SAMPLEPROC_PUSH)pFuncsTable->pfnProcessSampleData, pTransformFilter, TransformFilter_Input_QueryAccept, NULL, &pTransformFilter->csFilter, NULL, &pTransformFilter->ppPins[0]);
180 if (SUCCEEDED(hr))
182 ALLOCATOR_PROPERTIES props;
183 props.cbAlign = 1;
184 props.cbPrefix = 0;
185 props.cbBuffer = 0; /* Will be updated at connection time */
186 props.cBuffers = 2;
188 hr = OutputPin_Construct(&TransformFilter_OutputPin_Vtbl, sizeof(OutputPin), &piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
190 if (FAILED(hr))
191 ERR("Cannot create output pin (%x)\n", hr);
192 else
194 if (!stop)
195 stop = TransformFilter_ChangeStop;
196 if (!current)
197 current = TransformFilter_ChangeCurrent;
198 if (!rate)
199 rate = TransformFilter_ChangeRate;
201 MediaSeekingImpl_Init((IBaseFilter*)pTransformFilter, stop, current, rate, &pTransformFilter->mediaSeeking, &pTransformFilter->csFilter);
202 pTransformFilter->mediaSeeking.lpVtbl = &TransformFilter_Seeking_Vtbl;
205 else
207 CoTaskMemFree(pTransformFilter->ppPins);
208 pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
209 DeleteCriticalSection(&pTransformFilter->csFilter);
210 CoTaskMemFree(pTransformFilter);
213 return hr;
216 static HRESULT WINAPI TransformFilter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
218 TransformFilterImpl *This = (TransformFilterImpl *)iface;
219 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
221 *ppv = NULL;
223 if (IsEqualIID(riid, &IID_IUnknown))
224 *ppv = (LPVOID)This;
225 else if (IsEqualIID(riid, &IID_IPersist))
226 *ppv = (LPVOID)This;
227 else if (IsEqualIID(riid, &IID_IMediaFilter))
228 *ppv = (LPVOID)This;
229 else if (IsEqualIID(riid, &IID_IBaseFilter))
230 *ppv = (LPVOID)This;
231 else if (IsEqualIID(riid, &IID_IMediaSeeking))
232 *ppv = &This->mediaSeeking;
234 if (*ppv)
236 IUnknown_AddRef((IUnknown *)(*ppv));
237 return S_OK;
240 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
241 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
243 return E_NOINTERFACE;
246 static ULONG WINAPI TransformFilter_AddRef(IBaseFilter * iface)
248 TransformFilterImpl *This = (TransformFilterImpl *)iface;
249 ULONG refCount = InterlockedIncrement(&This->refCount);
251 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
253 return refCount;
256 static ULONG WINAPI TransformFilter_Release(IBaseFilter * iface)
258 TransformFilterImpl *This = (TransformFilterImpl *)iface;
259 ULONG refCount = InterlockedDecrement(&This->refCount);
261 TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
263 if (!refCount)
265 ULONG i;
267 if (This->pClock)
268 IReferenceClock_Release(This->pClock);
270 for (i = 0; i < 2; i++)
272 IPin *pConnectedTo;
274 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
276 IPin_Disconnect(pConnectedTo);
277 IPin_Release(pConnectedTo);
279 IPin_Disconnect(This->ppPins[i]);
281 IPin_Release(This->ppPins[i]);
284 CoTaskMemFree(This->ppPins);
285 This->lpVtbl = NULL;
287 This->csFilter.DebugInfo->Spare[0] = 0;
288 DeleteCriticalSection(&This->csFilter);
290 TRACE("Destroying transform filter\n");
291 CoTaskMemFree(This);
293 return 0;
295 else
296 return refCount;
299 /** IPersist methods **/
301 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
303 TransformFilterImpl *This = (TransformFilterImpl *)iface;
305 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
307 *pClsid = This->clsid;
309 return S_OK;
312 /** IMediaFilter methods **/
314 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
316 TransformFilterImpl *This = (TransformFilterImpl *)iface;
318 TRACE("(%p/%p)\n", This, iface);
320 EnterCriticalSection(&This->csFilter);
322 This->state = State_Stopped;
323 if (This->pFuncsTable->pfnProcessEnd)
324 This->pFuncsTable->pfnProcessEnd(This);
326 LeaveCriticalSection(&This->csFilter);
328 return S_OK;
331 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
333 TransformFilterImpl *This = (TransformFilterImpl *)iface;
335 TRACE("(%p/%p)->()\n", This, iface);
337 EnterCriticalSection(&This->csFilter);
339 if (This->state == State_Stopped)
340 IBaseFilter_Run(iface, -1);
342 This->state = State_Paused;
344 LeaveCriticalSection(&This->csFilter);
346 return S_OK;
349 static HRESULT WINAPI TransformFilter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
351 HRESULT hr = S_OK;
352 TransformFilterImpl *This = (TransformFilterImpl *)iface;
354 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
356 EnterCriticalSection(&This->csFilter);
358 if (This->state == State_Stopped)
360 ((InputPin *)This->ppPins[0])->end_of_stream = 0;
361 if (This->pFuncsTable->pfnProcessBegin)
362 This->pFuncsTable->pfnProcessBegin(This);
363 OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
366 This->rtStreamStart = tStart;
367 This->state = State_Running;
369 LeaveCriticalSection(&This->csFilter);
371 return hr;
374 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
376 TransformFilterImpl *This = (TransformFilterImpl *)iface;
378 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
380 EnterCriticalSection(&This->csFilter);
382 *pState = This->state;
384 LeaveCriticalSection(&This->csFilter);
386 return S_OK;
389 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
391 TransformFilterImpl *This = (TransformFilterImpl *)iface;
393 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
395 EnterCriticalSection(&This->csFilter);
397 if (This->pClock)
398 IReferenceClock_Release(This->pClock);
399 This->pClock = pClock;
400 if (This->pClock)
401 IReferenceClock_AddRef(This->pClock);
403 LeaveCriticalSection(&This->csFilter);
405 return S_OK;
408 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
410 TransformFilterImpl *This = (TransformFilterImpl *)iface;
412 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
414 EnterCriticalSection(&This->csFilter);
416 *ppClock = This->pClock;
417 if (This->pClock)
418 IReferenceClock_AddRef(This->pClock);
420 LeaveCriticalSection(&This->csFilter);
422 return S_OK;
425 /** IBaseFilter implementation **/
427 static HRESULT TransformFilter_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
429 TransformFilterImpl *This = (TransformFilterImpl *)iface;
431 /* Our pins are static, not changing so setting static tick count is ok */
432 *lastsynctick = 0;
434 if (pos >= 2)
435 return S_FALSE;
437 *pin = This->ppPins[pos];
438 IPin_AddRef(*pin);
439 return S_OK;
442 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
444 TransformFilterImpl *This = (TransformFilterImpl *)iface;
446 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
448 return IEnumPinsImpl_Construct(ppEnum, TransformFilter_GetPin, iface);
451 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
453 TransformFilterImpl *This = (TransformFilterImpl *)iface;
455 TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
457 return E_NOTIMPL;
460 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
462 TransformFilterImpl *This = (TransformFilterImpl *)iface;
464 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
466 strcpyW(pInfo->achName, This->filterInfo.achName);
467 pInfo->pGraph = This->filterInfo.pGraph;
469 if (pInfo->pGraph)
470 IFilterGraph_AddRef(pInfo->pGraph);
472 return S_OK;
475 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
477 HRESULT hr = S_OK;
478 TransformFilterImpl *This = (TransformFilterImpl *)iface;
480 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
482 EnterCriticalSection(&This->csFilter);
484 if (pName)
485 strcpyW(This->filterInfo.achName, pName);
486 else
487 *This->filterInfo.achName = '\0';
488 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
490 LeaveCriticalSection(&This->csFilter);
492 return hr;
495 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
497 TransformFilterImpl *This = (TransformFilterImpl *)iface;
498 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
499 return E_NOTIMPL;
502 static const IBaseFilterVtbl TransformFilter_Vtbl =
504 TransformFilter_QueryInterface,
505 TransformFilter_AddRef,
506 TransformFilter_Release,
507 TransformFilter_GetClassID,
508 TransformFilter_Stop,
509 TransformFilter_Pause,
510 TransformFilter_Run,
511 TransformFilter_GetState,
512 TransformFilter_SetSyncSource,
513 TransformFilter_GetSyncSource,
514 TransformFilter_EnumPins,
515 TransformFilter_FindPin,
516 TransformFilter_QueryFilterInfo,
517 TransformFilter_JoinFilterGraph,
518 TransformFilter_QueryVendorInfo
521 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
523 InputPin* This = (InputPin*) iface;
524 TransformFilterImpl* pTransform;
525 IPin* ppin;
526 HRESULT hr;
528 TRACE("(%p)->()\n", iface);
530 /* Since we process samples synchronously, just forward notification downstream */
531 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
532 if (!pTransform)
533 hr = E_FAIL;
534 else
535 hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
536 if (SUCCEEDED(hr))
538 hr = IPin_EndOfStream(ppin);
539 IPin_Release(ppin);
542 if (FAILED(hr))
543 ERR("%x\n", hr);
544 return hr;
547 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
549 InputPin* This = (InputPin*) iface;
550 TransformFilterImpl* pTransform;
551 HRESULT hr;
553 TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
555 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
557 hr = pTransform->pFuncsTable->pfnConnectInput(pTransform, pmt);
558 if (SUCCEEDED(hr))
560 hr = InputPin_ReceiveConnection(iface, pReceivePin, pmt);
561 if (FAILED(hr))
562 pTransform->pFuncsTable->pfnCleanup(pTransform);
565 return hr;
568 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
570 InputPin* This = (InputPin*) iface;
571 TransformFilterImpl* pTransform;
573 TRACE("(%p)->()\n", iface);
575 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
576 pTransform->pFuncsTable->pfnCleanup(pTransform);
578 return IPinImpl_Disconnect(iface);
581 static const IPinVtbl TransformFilter_InputPin_Vtbl =
583 InputPin_QueryInterface,
584 IPinImpl_AddRef,
585 InputPin_Release,
586 InputPin_Connect,
587 TransformFilter_InputPin_ReceiveConnection,
588 TransformFilter_InputPin_Disconnect,
589 IPinImpl_ConnectedTo,
590 IPinImpl_ConnectionMediaType,
591 IPinImpl_QueryPinInfo,
592 IPinImpl_QueryDirection,
593 IPinImpl_QueryId,
594 IPinImpl_QueryAccept,
595 IPinImpl_EnumMediaTypes,
596 IPinImpl_QueryInternalConnections,
597 TransformFilter_InputPin_EndOfStream,
598 InputPin_BeginFlush,
599 InputPin_EndFlush,
600 InputPin_NewSegment
603 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
605 IPinImpl *This = (IPinImpl *)iface;
606 ENUMMEDIADETAILS emd;
608 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
610 emd.cMediaTypes = 1;
611 emd.pMediaTypes = &This->mtCurrent;
613 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
616 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
618 OutputPin_QueryInterface,
619 IPinImpl_AddRef,
620 OutputPin_Release,
621 OutputPin_Connect,
622 OutputPin_ReceiveConnection,
623 OutputPin_Disconnect,
624 IPinImpl_ConnectedTo,
625 IPinImpl_ConnectionMediaType,
626 IPinImpl_QueryPinInfo,
627 IPinImpl_QueryDirection,
628 IPinImpl_QueryId,
629 IPinImpl_QueryAccept,
630 TransformFilter_Output_EnumMediaTypes,
631 IPinImpl_QueryInternalConnections,
632 OutputPin_EndOfStream,
633 OutputPin_BeginFlush,
634 OutputPin_EndFlush,
635 OutputPin_NewSegment
638 static const IMemInputPinVtbl MemInputPin_Vtbl =
640 MemInputPin_QueryInterface,
641 MemInputPin_AddRef,
642 MemInputPin_Release,
643 MemInputPin_GetAllocator,
644 MemInputPin_NotifyAllocator,
645 MemInputPin_GetAllocatorRequirements,
646 MemInputPin_Receive,
647 MemInputPin_ReceiveMultiple,
648 MemInputPin_ReceiveCanBlock