push 6fe5edf8439c19d3885814583531c2f2b1495177
[wine/hacks.git] / dlls / quartz / transform.c
blobb090e5bdcc64fd5165b86cae84e52abd959c8326
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 if (This->state == State_Stopped)
58 return S_FALSE;
60 return This->pFuncsTable->pfnProcessSampleData(This, pSample);
63 static HRESULT TransformFilter_Input_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
65 TransformFilterImpl* This = (TransformFilterImpl*)iface;
66 TRACE("%p\n", iface);
67 dump_AM_MEDIA_TYPE(pmt);
69 if (This->pFuncsTable->pfnQueryConnect)
70 return This->pFuncsTable->pfnQueryConnect(This, pmt);
71 /* Assume OK if there's no query method (the connection will fail if
72 needed) */
73 return S_OK;
77 static HRESULT TransformFilter_Output_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
79 TransformFilterImpl* pTransformFilter = (TransformFilterImpl*)iface;
80 AM_MEDIA_TYPE* outpmt = &((OutputPin*)pTransformFilter->ppPins[1])->pin.mtCurrent;
81 TRACE("%p\n", iface);
83 if (IsEqualIID(&pmt->majortype, &outpmt->majortype) && IsEqualIID(&pmt->subtype, &outpmt->subtype))
84 return S_OK;
85 return S_FALSE;
89 static inline TransformFilterImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
91 return (TransformFilterImpl *)((char*)iface - FIELD_OFFSET(TransformFilterImpl, mediaSeeking.lpVtbl));
94 static HRESULT WINAPI TransformFilter_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
96 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
98 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
101 static ULONG WINAPI TransformFilter_Seeking_AddRef(IMediaSeeking * iface)
103 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
105 return IUnknown_AddRef((IUnknown *)This);
108 static ULONG WINAPI TransformFilter_Seeking_Release(IMediaSeeking * iface)
110 TransformFilterImpl *This = impl_from_IMediaSeeking(iface);
112 return IUnknown_Release((IUnknown *)This);
115 static const IMediaSeekingVtbl TransformFilter_Seeking_Vtbl =
117 TransformFilter_Seeking_QueryInterface,
118 TransformFilter_Seeking_AddRef,
119 TransformFilter_Seeking_Release,
120 MediaSeekingImpl_GetCapabilities,
121 MediaSeekingImpl_CheckCapabilities,
122 MediaSeekingImpl_IsFormatSupported,
123 MediaSeekingImpl_QueryPreferredFormat,
124 MediaSeekingImpl_GetTimeFormat,
125 MediaSeekingImpl_IsUsingTimeFormat,
126 MediaSeekingImpl_SetTimeFormat,
127 MediaSeekingImpl_GetDuration,
128 MediaSeekingImpl_GetStopPosition,
129 MediaSeekingImpl_GetCurrentPosition,
130 MediaSeekingImpl_ConvertTimeFormat,
131 MediaSeekingImpl_SetPositions,
132 MediaSeekingImpl_GetPositions,
133 MediaSeekingImpl_GetAvailable,
134 MediaSeekingImpl_SetRate,
135 MediaSeekingImpl_GetRate,
136 MediaSeekingImpl_GetPreroll
139 /* These shouldn't be implemented by default.
140 * Usually only source filters should implement these
141 * and even it's not needed all of the time
143 static HRESULT TransformFilter_ChangeCurrent(IBaseFilter *iface)
145 TRACE("(%p) filter hasn't implemented current position change!\n", iface);
146 return S_OK;
149 static HRESULT TransformFilter_ChangeStop(IBaseFilter *iface)
151 TRACE("(%p) filter hasn't implemented stop position change!\n", iface);
152 return S_OK;
155 static HRESULT TransformFilter_ChangeRate(IBaseFilter *iface)
157 TRACE("(%p) filter hasn't implemented rate change!\n", iface);
158 return S_OK;
161 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable, CHANGEPROC stop, CHANGEPROC current, CHANGEPROC rate)
163 HRESULT hr;
164 PIN_INFO piInput;
165 PIN_INFO piOutput;
167 /* pTransformFilter is already allocated */
168 pTransformFilter->clsid = *pClsid;
169 pTransformFilter->pFuncsTable = pFuncsTable;
171 pTransformFilter->lpVtbl = &TransformFilter_Vtbl;
173 pTransformFilter->refCount = 1;
174 InitializeCriticalSection(&pTransformFilter->csFilter);
175 pTransformFilter->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TransformFilterImpl.csFilter");
176 pTransformFilter->state = State_Stopped;
177 pTransformFilter->pClock = NULL;
178 ZeroMemory(&pTransformFilter->filterInfo, sizeof(FILTER_INFO));
180 pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
182 /* construct input pin */
183 piInput.dir = PINDIR_INPUT;
184 piInput.pFilter = (IBaseFilter *)pTransformFilter;
185 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
186 piOutput.dir = PINDIR_OUTPUT;
187 piOutput.pFilter = (IBaseFilter *)pTransformFilter;
188 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
190 hr = InputPin_Construct(&TransformFilter_InputPin_Vtbl, &piInput, TransformFilter_Sample, pTransformFilter, TransformFilter_Input_QueryAccept, NULL, &pTransformFilter->csFilter, &pTransformFilter->ppPins[0]);
192 if (SUCCEEDED(hr))
194 ALLOCATOR_PROPERTIES props;
195 props.cbAlign = 1;
196 props.cbPrefix = 0;
197 props.cbBuffer = 0; /* Will be updated at connection time */
198 props.cBuffers = 2;
200 hr = OutputPin_Construct(&TransformFilter_OutputPin_Vtbl, sizeof(OutputPin), &piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
202 if (FAILED(hr))
203 ERR("Cannot create output pin (%x)\n", hr);
204 else
206 if (!stop)
207 stop = TransformFilter_ChangeStop;
208 if (!current)
209 current = TransformFilter_ChangeCurrent;
210 if (!rate)
211 rate = TransformFilter_ChangeRate;
213 MediaSeekingImpl_Init((IBaseFilter*)pTransformFilter, stop, current, rate, &pTransformFilter->mediaSeeking, &pTransformFilter->csFilter);
214 pTransformFilter->mediaSeeking.lpVtbl = &TransformFilter_Seeking_Vtbl;
217 else
219 CoTaskMemFree(pTransformFilter->ppPins);
220 pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
221 DeleteCriticalSection(&pTransformFilter->csFilter);
222 CoTaskMemFree(pTransformFilter);
225 return hr;
228 static HRESULT WINAPI TransformFilter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
230 TransformFilterImpl *This = (TransformFilterImpl *)iface;
231 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
233 *ppv = NULL;
235 if (IsEqualIID(riid, &IID_IUnknown))
236 *ppv = (LPVOID)This;
237 else if (IsEqualIID(riid, &IID_IPersist))
238 *ppv = (LPVOID)This;
239 else if (IsEqualIID(riid, &IID_IMediaFilter))
240 *ppv = (LPVOID)This;
241 else if (IsEqualIID(riid, &IID_IBaseFilter))
242 *ppv = (LPVOID)This;
243 else if (IsEqualIID(riid, &IID_IMediaSeeking))
244 *ppv = &This->mediaSeeking;
246 if (*ppv)
248 IUnknown_AddRef((IUnknown *)(*ppv));
249 return S_OK;
252 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
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);
265 return refCount;
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);
275 if (!refCount)
277 ULONG i;
279 if (This->pClock)
280 IReferenceClock_Release(This->pClock);
282 for (i = 0; i < 2; i++)
284 IPin *pConnectedTo;
286 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
288 IPin_Disconnect(pConnectedTo);
289 IPin_Release(pConnectedTo);
291 IPin_Disconnect(This->ppPins[i]);
293 IPin_Release(This->ppPins[i]);
296 CoTaskMemFree(This->ppPins);
297 This->lpVtbl = NULL;
299 This->csFilter.DebugInfo->Spare[0] = 0;
300 DeleteCriticalSection(&This->csFilter);
302 TRACE("Destroying transform filter\n");
303 CoTaskMemFree(This);
305 return 0;
307 else
308 return refCount;
311 /** IPersist methods **/
313 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
315 TransformFilterImpl *This = (TransformFilterImpl *)iface;
317 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
319 *pClsid = This->clsid;
321 return S_OK;
324 /** IMediaFilter methods **/
326 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
328 TransformFilterImpl *This = (TransformFilterImpl *)iface;
330 TRACE("(%p/%p)\n", This, iface);
332 EnterCriticalSection(&This->csFilter);
334 This->state = State_Stopped;
335 if (This->pFuncsTable->pfnProcessEnd)
336 This->pFuncsTable->pfnProcessEnd(This);
338 LeaveCriticalSection(&This->csFilter);
340 return S_OK;
343 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
345 TransformFilterImpl *This = (TransformFilterImpl *)iface;
347 TRACE("(%p/%p)->()\n", This, iface);
349 EnterCriticalSection(&This->csFilter);
351 if (This->state == State_Stopped)
352 ((InputPin *)This->ppPins[0])->end_of_stream = 0;
354 This->state = State_Paused;
356 LeaveCriticalSection(&This->csFilter);
358 return S_OK;
361 static HRESULT WINAPI TransformFilter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
363 HRESULT hr = S_OK;
364 TransformFilterImpl *This = (TransformFilterImpl *)iface;
366 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
368 EnterCriticalSection(&This->csFilter);
370 if (This->state == State_Stopped)
371 ((InputPin *)This->ppPins[0])->end_of_stream = 0;
373 This->rtStreamStart = tStart;
374 This->state = State_Running;
375 OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
376 if (This->pFuncsTable->pfnProcessBegin)
377 This->pFuncsTable->pfnProcessBegin(This);
379 LeaveCriticalSection(&This->csFilter);
381 return hr;
384 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
386 TransformFilterImpl *This = (TransformFilterImpl *)iface;
388 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
390 EnterCriticalSection(&This->csFilter);
392 *pState = This->state;
394 LeaveCriticalSection(&This->csFilter);
396 return S_OK;
399 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
401 TransformFilterImpl *This = (TransformFilterImpl *)iface;
403 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
405 EnterCriticalSection(&This->csFilter);
407 if (This->pClock)
408 IReferenceClock_Release(This->pClock);
409 This->pClock = pClock;
410 if (This->pClock)
411 IReferenceClock_AddRef(This->pClock);
413 LeaveCriticalSection(&This->csFilter);
415 return S_OK;
418 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
420 TransformFilterImpl *This = (TransformFilterImpl *)iface;
422 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
424 EnterCriticalSection(&This->csFilter);
426 *ppClock = This->pClock;
427 IReferenceClock_AddRef(This->pClock);
429 LeaveCriticalSection(&This->csFilter);
431 return S_OK;
434 /** IBaseFilter implementation **/
436 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
438 ENUMPINDETAILS epd;
439 TransformFilterImpl *This = (TransformFilterImpl *)iface;
441 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
443 epd.cPins = 2; /* input and output pins */
444 epd.ppPins = This->ppPins;
445 return IEnumPinsImpl_Construct(&epd, ppEnum);
448 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
450 TransformFilterImpl *This = (TransformFilterImpl *)iface;
452 TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
454 return E_NOTIMPL;
457 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
459 TransformFilterImpl *This = (TransformFilterImpl *)iface;
461 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
463 strcpyW(pInfo->achName, This->filterInfo.achName);
464 pInfo->pGraph = This->filterInfo.pGraph;
466 if (pInfo->pGraph)
467 IFilterGraph_AddRef(pInfo->pGraph);
469 return S_OK;
472 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
474 HRESULT hr = S_OK;
475 TransformFilterImpl *This = (TransformFilterImpl *)iface;
477 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
479 EnterCriticalSection(&This->csFilter);
481 if (pName)
482 strcpyW(This->filterInfo.achName, pName);
483 else
484 *This->filterInfo.achName = '\0';
485 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
487 LeaveCriticalSection(&This->csFilter);
489 return hr;
492 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
494 TransformFilterImpl *This = (TransformFilterImpl *)iface;
495 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
496 return E_NOTIMPL;
499 static const IBaseFilterVtbl TransformFilter_Vtbl =
501 TransformFilter_QueryInterface,
502 TransformFilter_AddRef,
503 TransformFilter_Release,
504 TransformFilter_GetClassID,
505 TransformFilter_Stop,
506 TransformFilter_Pause,
507 TransformFilter_Run,
508 TransformFilter_GetState,
509 TransformFilter_SetSyncSource,
510 TransformFilter_GetSyncSource,
511 TransformFilter_EnumPins,
512 TransformFilter_FindPin,
513 TransformFilter_QueryFilterInfo,
514 TransformFilter_JoinFilterGraph,
515 TransformFilter_QueryVendorInfo
518 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
520 InputPin* This = (InputPin*) iface;
521 TransformFilterImpl* pTransform;
522 IPin* ppin;
523 HRESULT hr;
525 TRACE("(%p)->()\n", iface);
527 /* Since we process samples synchronously, just forward notification downstream */
528 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
529 if (!pTransform)
530 hr = E_FAIL;
531 else
532 hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
533 if (SUCCEEDED(hr))
535 hr = IPin_EndOfStream(ppin);
536 IPin_Release(ppin);
539 if (FAILED(hr))
540 ERR("%x\n", hr);
541 return hr;
544 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
546 InputPin* This = (InputPin*) iface;
547 TransformFilterImpl* pTransform;
548 HRESULT hr;
550 TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
552 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
554 hr = pTransform->pFuncsTable->pfnConnectInput(pTransform, pmt);
555 if (SUCCEEDED(hr))
557 hr = InputPin_ReceiveConnection(iface, pReceivePin, pmt);
558 if (FAILED(hr))
559 pTransform->pFuncsTable->pfnCleanup(pTransform);
562 return hr;
565 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
567 InputPin* This = (InputPin*) iface;
568 TransformFilterImpl* pTransform;
570 TRACE("(%p)->()\n", iface);
572 pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
573 pTransform->pFuncsTable->pfnCleanup(pTransform);
575 return IPinImpl_Disconnect(iface);
578 static const IPinVtbl TransformFilter_InputPin_Vtbl =
580 InputPin_QueryInterface,
581 IPinImpl_AddRef,
582 InputPin_Release,
583 InputPin_Connect,
584 TransformFilter_InputPin_ReceiveConnection,
585 TransformFilter_InputPin_Disconnect,
586 IPinImpl_ConnectedTo,
587 IPinImpl_ConnectionMediaType,
588 IPinImpl_QueryPinInfo,
589 IPinImpl_QueryDirection,
590 IPinImpl_QueryId,
591 IPinImpl_QueryAccept,
592 IPinImpl_EnumMediaTypes,
593 IPinImpl_QueryInternalConnections,
594 TransformFilter_InputPin_EndOfStream,
595 InputPin_BeginFlush,
596 InputPin_EndFlush,
597 InputPin_NewSegment
600 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
602 IPinImpl *This = (IPinImpl *)iface;
603 ENUMMEDIADETAILS emd;
605 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
607 emd.cMediaTypes = 1;
608 emd.pMediaTypes = &This->mtCurrent;
610 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
613 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
615 OutputPin_QueryInterface,
616 IPinImpl_AddRef,
617 OutputPin_Release,
618 OutputPin_Connect,
619 OutputPin_ReceiveConnection,
620 OutputPin_Disconnect,
621 IPinImpl_ConnectedTo,
622 IPinImpl_ConnectionMediaType,
623 IPinImpl_QueryPinInfo,
624 IPinImpl_QueryDirection,
625 IPinImpl_QueryId,
626 IPinImpl_QueryAccept,
627 TransformFilter_Output_EnumMediaTypes,
628 IPinImpl_QueryInternalConnections,
629 OutputPin_EndOfStream,
630 OutputPin_BeginFlush,
631 OutputPin_EndFlush,
632 OutputPin_NewSegment
635 static const IMemInputPinVtbl MemInputPin_Vtbl =
637 MemInputPin_QueryInterface,
638 MemInputPin_AddRef,
639 MemInputPin_Release,
640 MemInputPin_GetAllocator,
641 MemInputPin_NotifyAllocator,
642 MemInputPin_GetAllocatorRequirements,
643 MemInputPin_Receive,
644 MemInputPin_ReceiveMultiple,
645 MemInputPin_ReceiveCanBlock