2 * Transform Filter (Base for decoders, etc...)
4 * Copyright 2005 Christian Costa
5 * Copyright 2010 Aric Stewart, CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "wine/strmbase.h"
38 #include "strmbase_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(strmbase
);
42 static const WCHAR wcsInputPinName
[] = {'i','n','p','u','t',' ','p','i','n',0};
43 static const WCHAR wcsOutputPinName
[] = {'o','u','t','p','u','t',' ','p','i','n',0};
45 static const IPinVtbl TransformFilter_InputPin_Vtbl
;
46 static const IPinVtbl TransformFilter_OutputPin_Vtbl
;
47 static const IQualityControlVtbl TransformFilter_QualityControl_Vtbl
;
49 static inline BaseInputPin
*impl_BaseInputPin_from_BasePin( BasePin
*iface
)
51 return CONTAINING_RECORD(iface
, BaseInputPin
, pin
);
54 static inline BasePin
*impl_BasePin_from_IPin( IPin
*iface
)
56 return CONTAINING_RECORD(iface
, BasePin
, IPin_iface
);
59 static inline BaseInputPin
*impl_BaseInputPin_from_IPin( IPin
*iface
)
61 return CONTAINING_RECORD(iface
, BaseInputPin
, pin
.IPin_iface
);
64 static inline BaseOutputPin
*impl_BaseOutputPin_from_IPin( IPin
*iface
)
66 return CONTAINING_RECORD(iface
, BaseOutputPin
, pin
.IPin_iface
);
69 static inline TransformFilter
*impl_from_IBaseFilter( IBaseFilter
*iface
)
71 return CONTAINING_RECORD(iface
, TransformFilter
, filter
.IBaseFilter_iface
);
74 static inline TransformFilter
*impl_from_BaseFilter( BaseFilter
*iface
)
76 return CONTAINING_RECORD(iface
, TransformFilter
, filter
);
79 static HRESULT WINAPI
TransformFilter_Input_CheckMediaType(BasePin
*iface
, const AM_MEDIA_TYPE
* pmt
)
81 BaseInputPin
* This
= impl_BaseInputPin_from_BasePin(iface
);
82 TransformFilter
* pTransform
;
85 pTransform
= impl_from_IBaseFilter(This
->pin
.pinInfo
.pFilter
);
87 if (pTransform
->pFuncsTable
->pfnCheckInputType
)
88 return pTransform
->pFuncsTable
->pfnCheckInputType(pTransform
, pmt
);
89 /* Assume OK if there's no query method (the connection will fail if
94 static HRESULT WINAPI
TransformFilter_Input_Receive(BaseInputPin
*This
, IMediaSample
*pInSample
)
97 TransformFilter
* pTransform
;
99 pTransform
= impl_from_IBaseFilter(This
->pin
.pinInfo
.pFilter
);
101 EnterCriticalSection(&pTransform
->csReceive
);
102 if (pTransform
->filter
.state
== State_Stopped
)
104 LeaveCriticalSection(&pTransform
->csReceive
);
105 return VFW_E_WRONG_STATE
;
108 if (This
->end_of_stream
|| This
->flushing
)
110 LeaveCriticalSection(&pTransform
->csReceive
);
114 LeaveCriticalSection(&pTransform
->csReceive
);
115 if (pTransform
->pFuncsTable
->pfnReceive
)
116 hr
= pTransform
->pFuncsTable
->pfnReceive(pTransform
, pInSample
);
123 static HRESULT WINAPI
TransformFilter_Output_CheckMediaType(BasePin
*This
, const AM_MEDIA_TYPE
*pmt
)
125 TransformFilter
*pTransformFilter
= impl_from_IBaseFilter(This
->pinInfo
.pFilter
);
126 AM_MEDIA_TYPE
* outpmt
= &pTransformFilter
->pmt
;
128 if (IsEqualIID(&pmt
->majortype
, &outpmt
->majortype
)
129 && (IsEqualIID(&pmt
->subtype
, &outpmt
->subtype
) || IsEqualIID(&outpmt
->subtype
, &GUID_NULL
)))
134 static HRESULT WINAPI
TransformFilter_Output_DecideBufferSize(BaseOutputPin
*This
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
136 TransformFilter
*pTransformFilter
= impl_from_IBaseFilter(This
->pin
.pinInfo
.pFilter
);
137 return pTransformFilter
->pFuncsTable
->pfnDecideBufferSize(pTransformFilter
, pAlloc
, ppropInputRequest
);
140 static HRESULT WINAPI
TransformFilter_Output_GetMediaType(BasePin
*This
, int iPosition
, AM_MEDIA_TYPE
*pmt
)
142 TransformFilter
*pTransform
= impl_from_IBaseFilter(This
->pinInfo
.pFilter
);
147 return VFW_S_NO_MORE_ITEMS
;
148 CopyMediaType(pmt
, &pTransform
->pmt
);
152 static IPin
* WINAPI
TransformFilter_GetPin(BaseFilter
*iface
, int pos
)
154 TransformFilter
*This
= impl_from_BaseFilter(iface
);
156 if (pos
>= This
->npins
|| pos
< 0)
159 IPin_AddRef(This
->ppPins
[pos
]);
160 return This
->ppPins
[pos
];
163 static LONG WINAPI
TransformFilter_GetPinCount(BaseFilter
*iface
)
165 TransformFilter
*This
= impl_from_BaseFilter(iface
);
167 return (This
->npins
+1);
170 static const BaseFilterFuncTable tfBaseFuncTable
= {
171 TransformFilter_GetPin
,
172 TransformFilter_GetPinCount
175 static const BaseInputPinFuncTable tf_input_BaseInputFuncTable
= {
177 TransformFilter_Input_CheckMediaType
,
179 BasePinImpl_GetMediaTypeVersion
,
180 BasePinImpl_GetMediaType
182 TransformFilter_Input_Receive
185 static const BaseOutputPinFuncTable tf_output_BaseOutputFuncTable
= {
187 TransformFilter_Output_CheckMediaType
,
188 BaseOutputPinImpl_AttemptConnection
,
189 BasePinImpl_GetMediaTypeVersion
,
190 TransformFilter_Output_GetMediaType
192 TransformFilter_Output_DecideBufferSize
,
193 BaseOutputPinImpl_DecideAllocator
,
194 BaseOutputPinImpl_BreakConnect
197 static HRESULT
TransformFilter_Init(const IBaseFilterVtbl
*pVtbl
, const CLSID
* pClsid
, const TransformFilterFuncTable
* pFuncsTable
, TransformFilter
* pTransformFilter
)
203 BaseFilter_Init(&pTransformFilter
->filter
, pVtbl
, pClsid
, (DWORD_PTR
)(__FILE__
": TransformFilter.csFilter"), &tfBaseFuncTable
);
205 InitializeCriticalSection(&pTransformFilter
->csReceive
);
206 pTransformFilter
->csReceive
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TransformFilter.csReceive");
208 /* pTransformFilter is already allocated */
209 pTransformFilter
->pFuncsTable
= pFuncsTable
;
210 ZeroMemory(&pTransformFilter
->pmt
, sizeof(pTransformFilter
->pmt
));
211 pTransformFilter
->npins
= 2;
213 pTransformFilter
->ppPins
= CoTaskMemAlloc(2 * sizeof(IPin
*));
215 /* construct input pin */
216 piInput
.dir
= PINDIR_INPUT
;
217 piInput
.pFilter
= &pTransformFilter
->filter
.IBaseFilter_iface
;
218 lstrcpynW(piInput
.achName
, wcsInputPinName
, sizeof(piInput
.achName
) / sizeof(piInput
.achName
[0]));
219 piOutput
.dir
= PINDIR_OUTPUT
;
220 piOutput
.pFilter
= &pTransformFilter
->filter
.IBaseFilter_iface
;
221 lstrcpynW(piOutput
.achName
, wcsOutputPinName
, sizeof(piOutput
.achName
) / sizeof(piOutput
.achName
[0]));
223 hr
= BaseInputPin_Construct(&TransformFilter_InputPin_Vtbl
, sizeof(BaseInputPin
), &piInput
,
224 &tf_input_BaseInputFuncTable
, &pTransformFilter
->filter
.csFilter
, NULL
, &pTransformFilter
->ppPins
[0]);
228 hr
= BaseOutputPin_Construct(&TransformFilter_OutputPin_Vtbl
, sizeof(BaseOutputPin
), &piOutput
, &tf_output_BaseOutputFuncTable
, &pTransformFilter
->filter
.csFilter
, &pTransformFilter
->ppPins
[1]);
231 ERR("Cannot create output pin (%x)\n", hr
);
233 QualityControlImpl_Create( pTransformFilter
->ppPins
[0], &pTransformFilter
->filter
.IBaseFilter_iface
, &pTransformFilter
->qcimpl
);
234 pTransformFilter
->qcimpl
->IQualityControl_iface
.lpVtbl
= &TransformFilter_QualityControl_Vtbl
;
240 ISeekingPassThru
*passthru
;
241 pTransformFilter
->seekthru_unk
= NULL
;
242 hr
= CoCreateInstance(&CLSID_SeekingPassThru
, (IUnknown
*)&pTransformFilter
->filter
.IBaseFilter_iface
, CLSCTX_INPROC_SERVER
,
243 &IID_IUnknown
, (void **)&pTransformFilter
->seekthru_unk
);
246 IUnknown_QueryInterface(pTransformFilter
->seekthru_unk
, &IID_ISeekingPassThru
, (void**)&passthru
);
247 ISeekingPassThru_Init(passthru
, FALSE
, pTransformFilter
->ppPins
[0]);
248 ISeekingPassThru_Release(passthru
);
254 CoTaskMemFree(pTransformFilter
->ppPins
);
255 BaseFilterImpl_Release(&pTransformFilter
->filter
.IBaseFilter_iface
);
261 HRESULT
TransformFilter_Construct(const IBaseFilterVtbl
*pVtbl
, LONG filter_size
, const CLSID
* pClsid
, const TransformFilterFuncTable
* pFuncsTable
, IBaseFilter
** ppTransformFilter
)
263 TransformFilter
* pTf
;
265 *ppTransformFilter
= NULL
;
267 assert(filter_size
>= sizeof(TransformFilter
));
269 pTf
= CoTaskMemAlloc(filter_size
);
272 return E_OUTOFMEMORY
;
274 ZeroMemory(pTf
, filter_size
);
276 if (SUCCEEDED(TransformFilter_Init(pVtbl
, pClsid
, pFuncsTable
, pTf
)))
278 *ppTransformFilter
= &pTf
->filter
.IBaseFilter_iface
;
286 HRESULT WINAPI
TransformFilterImpl_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
289 TransformFilter
*This
= impl_from_IBaseFilter(iface
);
290 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, debugstr_guid(riid
), ppv
);
292 if (IsEqualIID(riid
, &IID_IQualityControl
)) {
293 *ppv
= (IQualityControl
*)This
->qcimpl
;
294 IUnknown_AddRef((IUnknown
*)*ppv
);
297 else if (IsEqualIID(riid
, &IID_IMediaSeeking
) ||
298 IsEqualIID(riid
, &IID_IMediaPosition
))
300 return IUnknown_QueryInterface(This
->seekthru_unk
, riid
, ppv
);
302 hr
= BaseFilterImpl_QueryInterface(iface
, riid
, ppv
);
304 if (FAILED(hr
) && !IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IVideoWindow
) &&
305 !IsEqualIID(riid
, &IID_IAMFilterMiscFlags
))
306 FIXME("No interface for %s!\n", debugstr_guid(riid
));
311 ULONG WINAPI
TransformFilterImpl_Release(IBaseFilter
* iface
)
313 TransformFilter
*This
= impl_from_IBaseFilter(iface
);
314 ULONG refCount
= InterlockedDecrement(&This
->filter
.refCount
);
316 TRACE("(%p/%p)->() Release from %d\n", This
, iface
, refCount
+ 1);
322 for (i
= 0; i
< This
->npins
; i
++)
326 if (SUCCEEDED(IPin_ConnectedTo(This
->ppPins
[i
], &pConnectedTo
)))
328 IPin_Disconnect(pConnectedTo
);
329 IPin_Release(pConnectedTo
);
331 IPin_Disconnect(This
->ppPins
[i
]);
333 IPin_Release(This
->ppPins
[i
]);
336 CoTaskMemFree(This
->ppPins
);
338 TRACE("Destroying transform filter\n");
339 This
->csReceive
.DebugInfo
->Spare
[0] = 0;
340 DeleteCriticalSection(&This
->csReceive
);
341 FreeMediaType(&This
->pmt
);
342 QualityControlImpl_Destroy(This
->qcimpl
);
343 IUnknown_Release(This
->seekthru_unk
);
344 BaseFilter_Destroy(&This
->filter
);
350 /** IMediaFilter methods **/
352 HRESULT WINAPI
TransformFilterImpl_Stop(IBaseFilter
* iface
)
354 TransformFilter
*This
= impl_from_IBaseFilter(iface
);
357 TRACE("(%p/%p)\n", This
, iface
);
359 EnterCriticalSection(&This
->csReceive
);
361 This
->filter
.state
= State_Stopped
;
362 if (This
->pFuncsTable
->pfnStopStreaming
)
363 hr
= This
->pFuncsTable
->pfnStopStreaming(This
);
365 hr
= BaseOutputPinImpl_Inactive(impl_BaseOutputPin_from_IPin(This
->ppPins
[1]));
367 LeaveCriticalSection(&This
->csReceive
);
372 HRESULT WINAPI
TransformFilterImpl_Pause(IBaseFilter
* iface
)
374 TransformFilter
*This
= impl_from_IBaseFilter(iface
);
377 TRACE("(%p/%p)->()\n", This
, iface
);
379 EnterCriticalSection(&This
->csReceive
);
381 if (This
->filter
.state
== State_Stopped
)
382 hr
= IBaseFilter_Run(iface
, -1);
387 This
->filter
.state
= State_Paused
;
389 LeaveCriticalSection(&This
->csReceive
);
394 HRESULT WINAPI
TransformFilterImpl_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
397 TransformFilter
*This
= impl_from_IBaseFilter(iface
);
399 TRACE("(%p/%p)->(%s)\n", This
, iface
, wine_dbgstr_longlong(tStart
));
401 EnterCriticalSection(&This
->csReceive
);
403 if (This
->filter
.state
== State_Stopped
)
405 impl_BaseInputPin_from_IPin(This
->ppPins
[0])->end_of_stream
= FALSE
;
406 if (This
->pFuncsTable
->pfnStartStreaming
)
407 hr
= This
->pFuncsTable
->pfnStartStreaming(This
);
409 hr
= BaseOutputPinImpl_Active(impl_BaseOutputPin_from_IPin(This
->ppPins
[1]));
414 This
->filter
.rtStreamStart
= tStart
;
415 This
->filter
.state
= State_Running
;
418 LeaveCriticalSection(&This
->csReceive
);
423 HRESULT WINAPI
TransformFilterImpl_Notify(TransformFilter
*iface
, IBaseFilter
*sender
, Quality qm
)
425 return QualityControlImpl_Notify((IQualityControl
*)iface
->qcimpl
, sender
, qm
);
428 /** IBaseFilter implementation **/
430 static HRESULT WINAPI
TransformFilter_InputPin_EndOfStream(IPin
* iface
)
432 BaseInputPin
* This
= impl_BaseInputPin_from_IPin(iface
);
433 TransformFilter
* pTransform
;
437 TRACE("(%p)->()\n", iface
);
439 /* Since we process samples synchronously, just forward notification downstream */
440 pTransform
= impl_from_IBaseFilter(This
->pin
.pinInfo
.pFilter
);
444 hr
= IPin_ConnectedTo(pTransform
->ppPins
[1], &ppin
);
447 hr
= IPin_EndOfStream(ppin
);
456 static HRESULT WINAPI
TransformFilter_InputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
458 BaseInputPin
* This
= impl_BaseInputPin_from_IPin(iface
);
459 TransformFilter
* pTransform
;
462 TRACE("(%p)->(%p, %p)\n", iface
, pReceivePin
, pmt
);
464 pTransform
= impl_from_IBaseFilter(This
->pin
.pinInfo
.pFilter
);
466 if (pTransform
->pFuncsTable
->pfnSetMediaType
)
467 hr
= pTransform
->pFuncsTable
->pfnSetMediaType(pTransform
, PINDIR_INPUT
, pmt
);
469 if (SUCCEEDED(hr
) && pTransform
->pFuncsTable
->pfnCompleteConnect
)
470 hr
= pTransform
->pFuncsTable
->pfnCompleteConnect(pTransform
, PINDIR_INPUT
, pReceivePin
);
474 hr
= BaseInputPinImpl_ReceiveConnection(iface
, pReceivePin
, pmt
);
475 if (FAILED(hr
) && pTransform
->pFuncsTable
->pfnBreakConnect
)
476 pTransform
->pFuncsTable
->pfnBreakConnect(pTransform
, PINDIR_INPUT
);
482 static HRESULT WINAPI
TransformFilter_InputPin_Disconnect(IPin
* iface
)
484 BaseInputPin
* This
= impl_BaseInputPin_from_IPin(iface
);
485 TransformFilter
* pTransform
;
487 TRACE("(%p)->()\n", iface
);
489 pTransform
= impl_from_IBaseFilter(This
->pin
.pinInfo
.pFilter
);
490 if (pTransform
->pFuncsTable
->pfnBreakConnect
)
491 pTransform
->pFuncsTable
->pfnBreakConnect(pTransform
, PINDIR_INPUT
);
493 return BasePinImpl_Disconnect(iface
);
496 static HRESULT WINAPI
TransformFilter_InputPin_BeginFlush(IPin
* iface
)
498 BaseInputPin
* This
= impl_BaseInputPin_from_IPin(iface
);
499 TransformFilter
* pTransform
;
502 TRACE("(%p)->()\n", iface
);
504 pTransform
= impl_from_IBaseFilter(This
->pin
.pinInfo
.pFilter
);
505 EnterCriticalSection(&pTransform
->filter
.csFilter
);
506 if (pTransform
->pFuncsTable
->pfnBeginFlush
)
507 hr
= pTransform
->pFuncsTable
->pfnBeginFlush(pTransform
);
509 hr
= BaseInputPinImpl_BeginFlush(iface
);
510 LeaveCriticalSection(&pTransform
->filter
.csFilter
);
514 static HRESULT WINAPI
TransformFilter_InputPin_EndFlush(IPin
* iface
)
516 BaseInputPin
* This
= impl_BaseInputPin_from_IPin(iface
);
517 TransformFilter
* pTransform
;
520 TRACE("(%p)->()\n", iface
);
522 pTransform
= impl_from_IBaseFilter(This
->pin
.pinInfo
.pFilter
);
523 EnterCriticalSection(&pTransform
->filter
.csFilter
);
524 if (pTransform
->pFuncsTable
->pfnEndFlush
)
525 hr
= pTransform
->pFuncsTable
->pfnEndFlush(pTransform
);
527 hr
= BaseInputPinImpl_EndFlush(iface
);
528 LeaveCriticalSection(&pTransform
->filter
.csFilter
);
532 static HRESULT WINAPI
TransformFilter_InputPin_NewSegment(IPin
* iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
)
534 BaseInputPin
* This
= impl_BaseInputPin_from_IPin(iface
);
535 TransformFilter
* pTransform
;
538 TRACE("(%p)->(%s %s %e)\n", iface
, wine_dbgstr_longlong(tStart
), wine_dbgstr_longlong(tStop
), dRate
);
540 pTransform
= impl_from_IBaseFilter(This
->pin
.pinInfo
.pFilter
);
541 EnterCriticalSection(&pTransform
->filter
.csFilter
);
542 if (pTransform
->pFuncsTable
->pfnNewSegment
)
543 hr
= pTransform
->pFuncsTable
->pfnNewSegment(pTransform
, tStart
, tStop
, dRate
);
545 hr
= BaseInputPinImpl_NewSegment(iface
, tStart
, tStop
, dRate
);
546 LeaveCriticalSection(&pTransform
->filter
.csFilter
);
550 static const IPinVtbl TransformFilter_InputPin_Vtbl
=
552 BaseInputPinImpl_QueryInterface
,
554 BaseInputPinImpl_Release
,
555 BaseInputPinImpl_Connect
,
556 TransformFilter_InputPin_ReceiveConnection
,
557 TransformFilter_InputPin_Disconnect
,
558 BasePinImpl_ConnectedTo
,
559 BasePinImpl_ConnectionMediaType
,
560 BasePinImpl_QueryPinInfo
,
561 BasePinImpl_QueryDirection
,
563 BasePinImpl_QueryAccept
,
564 BasePinImpl_EnumMediaTypes
,
565 BasePinImpl_QueryInternalConnections
,
566 TransformFilter_InputPin_EndOfStream
,
567 TransformFilter_InputPin_BeginFlush
,
568 TransformFilter_InputPin_EndFlush
,
569 TransformFilter_InputPin_NewSegment
572 static const IPinVtbl TransformFilter_OutputPin_Vtbl
=
574 BaseOutputPinImpl_QueryInterface
,
576 BaseOutputPinImpl_Release
,
577 BaseOutputPinImpl_Connect
,
578 BaseOutputPinImpl_ReceiveConnection
,
579 BaseOutputPinImpl_Disconnect
,
580 BasePinImpl_ConnectedTo
,
581 BasePinImpl_ConnectionMediaType
,
582 BasePinImpl_QueryPinInfo
,
583 BasePinImpl_QueryDirection
,
585 BasePinImpl_QueryAccept
,
586 BasePinImpl_EnumMediaTypes
,
587 BasePinImpl_QueryInternalConnections
,
588 BaseOutputPinImpl_EndOfStream
,
589 BaseOutputPinImpl_BeginFlush
,
590 BaseOutputPinImpl_EndFlush
,
591 BasePinImpl_NewSegment
594 static HRESULT WINAPI
TransformFilter_QualityControlImpl_Notify(IQualityControl
*iface
, IBaseFilter
*sender
, Quality qm
) {
595 QualityControlImpl
*qc
= (QualityControlImpl
*)iface
;
596 TransformFilter
*This
= impl_from_IBaseFilter(qc
->self
);
598 if (This
->pFuncsTable
->pfnNotify
)
599 return This
->pFuncsTable
->pfnNotify(This
, sender
, qm
);
601 return TransformFilterImpl_Notify(This
, sender
, qm
);
604 static const IQualityControlVtbl TransformFilter_QualityControl_Vtbl
= {
605 QualityControlImpl_QueryInterface
,
606 QualityControlImpl_AddRef
,
607 QualityControlImpl_Release
,
608 TransformFilter_QualityControlImpl_Notify
,
609 QualityControlImpl_SetSink