setupapi: Add an internal helper for SetupDiSetDeviceRegistryProperty().
[wine.git] / dlls / strmbase / transform.c
blob8419b9093183dc0dcf38f2b21cec4cf583b62dc7
1 /*
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
21 #include "config.h"
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "dshow.h"
29 #include "amvideo.h"
30 #include "strmif.h"
31 #include "vfw.h"
33 #include <assert.h>
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 BaseInputPin *impl_BaseInputPin_from_IPin( IPin *iface )
56 return CONTAINING_RECORD(iface, BaseInputPin, pin.IPin_iface);
59 static inline BaseOutputPin *impl_BaseOutputPin_from_IPin( IPin *iface )
61 return CONTAINING_RECORD(iface, BaseOutputPin, pin.IPin_iface);
64 static inline TransformFilter *impl_from_IBaseFilter( IBaseFilter *iface )
66 return CONTAINING_RECORD(iface, TransformFilter, filter.IBaseFilter_iface);
69 static inline TransformFilter *impl_from_BaseFilter( BaseFilter *iface )
71 return CONTAINING_RECORD(iface, TransformFilter, filter);
74 static HRESULT WINAPI TransformFilter_Input_CheckMediaType(BasePin *iface, const AM_MEDIA_TYPE * pmt)
76 BaseInputPin* This = impl_BaseInputPin_from_BasePin(iface);
77 TransformFilter * pTransform;
79 TRACE("%p\n", iface);
80 pTransform = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
82 if (pTransform->pFuncsTable->pfnCheckInputType)
83 return pTransform->pFuncsTable->pfnCheckInputType(pTransform, pmt);
84 /* Assume OK if there's no query method (the connection will fail if
85 needed) */
86 return S_OK;
89 static HRESULT WINAPI TransformFilter_Input_Receive(BaseInputPin *This, IMediaSample *pInSample)
91 HRESULT hr;
92 TransformFilter * pTransform;
93 TRACE("%p\n", This);
94 pTransform = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
96 EnterCriticalSection(&pTransform->csReceive);
97 if (pTransform->filter.state == State_Stopped)
99 LeaveCriticalSection(&pTransform->csReceive);
100 return VFW_E_WRONG_STATE;
103 if (This->end_of_stream || This->flushing)
105 LeaveCriticalSection(&pTransform->csReceive);
106 return S_FALSE;
109 LeaveCriticalSection(&pTransform->csReceive);
110 if (pTransform->pFuncsTable->pfnReceive)
111 hr = pTransform->pFuncsTable->pfnReceive(pTransform, pInSample);
112 else
113 hr = S_FALSE;
115 return hr;
118 static HRESULT WINAPI TransformFilter_Output_CheckMediaType(BasePin *This, const AM_MEDIA_TYPE *pmt)
120 TransformFilter *pTransformFilter = impl_from_IBaseFilter(This->pinInfo.pFilter);
121 AM_MEDIA_TYPE* outpmt = &pTransformFilter->pmt;
123 if (IsEqualIID(&pmt->majortype, &outpmt->majortype)
124 && (IsEqualIID(&pmt->subtype, &outpmt->subtype) || IsEqualIID(&outpmt->subtype, &GUID_NULL)))
125 return S_OK;
126 return S_FALSE;
129 static HRESULT WINAPI TransformFilter_Output_DecideBufferSize(BaseOutputPin *This, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
131 TransformFilter *pTransformFilter = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
132 return pTransformFilter->pFuncsTable->pfnDecideBufferSize(pTransformFilter, pAlloc, ppropInputRequest);
135 static HRESULT WINAPI TransformFilter_Output_GetMediaType(BasePin *This, int iPosition, AM_MEDIA_TYPE *pmt)
137 TransformFilter *pTransform = impl_from_IBaseFilter(This->pinInfo.pFilter);
139 if (iPosition < 0)
140 return E_INVALIDARG;
141 if (iPosition > 0)
142 return VFW_S_NO_MORE_ITEMS;
143 CopyMediaType(pmt, &pTransform->pmt);
144 return S_OK;
147 static IPin* WINAPI TransformFilter_GetPin(BaseFilter *iface, int pos)
149 TransformFilter *This = impl_from_BaseFilter(iface);
151 if (pos >= This->npins || pos < 0)
152 return NULL;
154 IPin_AddRef(This->ppPins[pos]);
155 return This->ppPins[pos];
158 static LONG WINAPI TransformFilter_GetPinCount(BaseFilter *iface)
160 TransformFilter *This = impl_from_BaseFilter(iface);
162 return (This->npins+1);
165 static const BaseFilterFuncTable tfBaseFuncTable = {
166 TransformFilter_GetPin,
167 TransformFilter_GetPinCount
170 static const BaseInputPinFuncTable tf_input_BaseInputFuncTable = {
172 TransformFilter_Input_CheckMediaType,
173 NULL,
174 BasePinImpl_GetMediaTypeVersion,
175 BasePinImpl_GetMediaType
177 TransformFilter_Input_Receive
180 static const BaseOutputPinFuncTable tf_output_BaseOutputFuncTable = {
182 TransformFilter_Output_CheckMediaType,
183 BaseOutputPinImpl_AttemptConnection,
184 BasePinImpl_GetMediaTypeVersion,
185 TransformFilter_Output_GetMediaType
187 TransformFilter_Output_DecideBufferSize,
188 BaseOutputPinImpl_DecideAllocator,
189 BaseOutputPinImpl_BreakConnect
192 static HRESULT TransformFilter_Init(const IBaseFilterVtbl *pVtbl, const CLSID* pClsid, const TransformFilterFuncTable* pFuncsTable, TransformFilter* pTransformFilter)
194 HRESULT hr;
195 PIN_INFO piInput;
196 PIN_INFO piOutput;
198 BaseFilter_Init(&pTransformFilter->filter, pVtbl, pClsid, (DWORD_PTR)(__FILE__ ": TransformFilter.csFilter"), &tfBaseFuncTable);
200 InitializeCriticalSection(&pTransformFilter->csReceive);
201 pTransformFilter->csReceive.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__": TransformFilter.csReceive");
203 /* pTransformFilter is already allocated */
204 pTransformFilter->pFuncsTable = pFuncsTable;
205 ZeroMemory(&pTransformFilter->pmt, sizeof(pTransformFilter->pmt));
206 pTransformFilter->npins = 2;
208 pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
210 /* construct input pin */
211 piInput.dir = PINDIR_INPUT;
212 piInput.pFilter = &pTransformFilter->filter.IBaseFilter_iface;
213 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
214 piOutput.dir = PINDIR_OUTPUT;
215 piOutput.pFilter = &pTransformFilter->filter.IBaseFilter_iface;
216 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
218 hr = BaseInputPin_Construct(&TransformFilter_InputPin_Vtbl, sizeof(BaseInputPin), &piInput,
219 &tf_input_BaseInputFuncTable, &pTransformFilter->filter.csFilter, NULL, &pTransformFilter->ppPins[0]);
221 if (SUCCEEDED(hr))
223 hr = BaseOutputPin_Construct(&TransformFilter_OutputPin_Vtbl, sizeof(BaseOutputPin), &piOutput, &tf_output_BaseOutputFuncTable, &pTransformFilter->filter.csFilter, &pTransformFilter->ppPins[1]);
225 if (FAILED(hr))
226 ERR("Cannot create output pin (%x)\n", hr);
227 else {
228 QualityControlImpl_Create( pTransformFilter->ppPins[0], &pTransformFilter->filter.IBaseFilter_iface, &pTransformFilter->qcimpl);
229 pTransformFilter->qcimpl->IQualityControl_iface.lpVtbl = &TransformFilter_QualityControl_Vtbl;
233 if (SUCCEEDED(hr))
235 ISeekingPassThru *passthru;
236 pTransformFilter->seekthru_unk = NULL;
237 hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown *)&pTransformFilter->filter.IBaseFilter_iface, CLSCTX_INPROC_SERVER,
238 &IID_IUnknown, (void **)&pTransformFilter->seekthru_unk);
239 if (SUCCEEDED(hr))
241 IUnknown_QueryInterface(pTransformFilter->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
242 ISeekingPassThru_Init(passthru, FALSE, pTransformFilter->ppPins[0]);
243 ISeekingPassThru_Release(passthru);
247 if (FAILED(hr))
249 CoTaskMemFree(pTransformFilter->ppPins);
250 BaseFilterImpl_Release(&pTransformFilter->filter.IBaseFilter_iface);
253 return hr;
256 HRESULT TransformFilter_Construct(const IBaseFilterVtbl *pVtbl, LONG filter_size, const CLSID* pClsid, const TransformFilterFuncTable* pFuncsTable, IBaseFilter ** ppTransformFilter)
258 TransformFilter* pTf;
260 *ppTransformFilter = NULL;
262 assert(filter_size >= sizeof(TransformFilter));
264 pTf = CoTaskMemAlloc(filter_size);
266 if (!pTf)
267 return E_OUTOFMEMORY;
269 ZeroMemory(pTf, filter_size);
271 if (SUCCEEDED(TransformFilter_Init(pVtbl, pClsid, pFuncsTable, pTf)))
273 *ppTransformFilter = &pTf->filter.IBaseFilter_iface;
274 return S_OK;
277 CoTaskMemFree(pTf);
278 return E_FAIL;
281 HRESULT WINAPI TransformFilterImpl_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
283 HRESULT hr;
284 TransformFilter *This = impl_from_IBaseFilter(iface);
285 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
287 if (IsEqualIID(riid, &IID_IQualityControl)) {
288 *ppv = (IQualityControl*)This->qcimpl;
289 IUnknown_AddRef((IUnknown*)*ppv);
290 return S_OK;
292 else if (IsEqualIID(riid, &IID_IMediaSeeking) ||
293 IsEqualIID(riid, &IID_IMediaPosition))
295 return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
297 hr = BaseFilterImpl_QueryInterface(iface, riid, ppv);
299 if (FAILED(hr) && !IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow) &&
300 !IsEqualIID(riid, &IID_IAMFilterMiscFlags))
301 FIXME("No interface for %s!\n", debugstr_guid(riid));
303 return hr;
306 ULONG WINAPI TransformFilterImpl_Release(IBaseFilter * iface)
308 TransformFilter *This = impl_from_IBaseFilter(iface);
309 ULONG refCount = InterlockedDecrement(&This->filter.refCount);
311 TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
313 if (!refCount)
315 ULONG i;
317 for (i = 0; i < This->npins; i++)
319 IPin *pConnectedTo;
321 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
323 IPin_Disconnect(pConnectedTo);
324 IPin_Release(pConnectedTo);
326 IPin_Disconnect(This->ppPins[i]);
328 IPin_Release(This->ppPins[i]);
331 CoTaskMemFree(This->ppPins);
333 TRACE("Destroying transform filter\n");
334 This->csReceive.DebugInfo->Spare[0] = 0;
335 DeleteCriticalSection(&This->csReceive);
336 FreeMediaType(&This->pmt);
337 QualityControlImpl_Destroy(This->qcimpl);
338 IUnknown_Release(This->seekthru_unk);
339 BaseFilter_Destroy(&This->filter);
340 CoTaskMemFree(This);
342 return refCount;
345 /** IMediaFilter methods **/
347 HRESULT WINAPI TransformFilterImpl_Stop(IBaseFilter * iface)
349 TransformFilter *This = impl_from_IBaseFilter(iface);
350 HRESULT hr = S_OK;
352 TRACE("(%p/%p)\n", This, iface);
354 EnterCriticalSection(&This->csReceive);
356 This->filter.state = State_Stopped;
357 if (This->pFuncsTable->pfnStopStreaming)
358 hr = This->pFuncsTable->pfnStopStreaming(This);
359 if (SUCCEEDED(hr))
360 hr = BaseOutputPinImpl_Inactive(impl_BaseOutputPin_from_IPin(This->ppPins[1]));
362 LeaveCriticalSection(&This->csReceive);
364 return hr;
367 HRESULT WINAPI TransformFilterImpl_Pause(IBaseFilter * iface)
369 TransformFilter *This = impl_from_IBaseFilter(iface);
370 HRESULT hr;
372 TRACE("(%p/%p)->()\n", This, iface);
374 EnterCriticalSection(&This->csReceive);
376 if (This->filter.state == State_Stopped)
377 hr = IBaseFilter_Run(iface, -1);
378 else
379 hr = S_OK;
381 if (SUCCEEDED(hr))
382 This->filter.state = State_Paused;
384 LeaveCriticalSection(&This->csReceive);
386 return hr;
389 HRESULT WINAPI TransformFilterImpl_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
391 HRESULT hr = S_OK;
392 TransformFilter *This = impl_from_IBaseFilter(iface);
394 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
396 EnterCriticalSection(&This->csReceive);
398 if (This->filter.state == State_Stopped)
400 impl_BaseInputPin_from_IPin(This->ppPins[0])->end_of_stream = FALSE;
401 if (This->pFuncsTable->pfnStartStreaming)
402 hr = This->pFuncsTable->pfnStartStreaming(This);
403 if (SUCCEEDED(hr))
404 hr = BaseOutputPinImpl_Active(impl_BaseOutputPin_from_IPin(This->ppPins[1]));
407 if (SUCCEEDED(hr))
409 This->filter.rtStreamStart = tStart;
410 This->filter.state = State_Running;
413 LeaveCriticalSection(&This->csReceive);
415 return hr;
418 HRESULT WINAPI TransformFilterImpl_Notify(TransformFilter *iface, IBaseFilter *sender, Quality qm)
420 return QualityControlImpl_Notify((IQualityControl*)iface->qcimpl, sender, qm);
423 /** IBaseFilter implementation **/
425 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
427 BaseInputPin* This = impl_BaseInputPin_from_IPin(iface);
428 TransformFilter* pTransform;
429 IPin* ppin;
430 HRESULT hr;
432 TRACE("(%p)->()\n", iface);
434 /* Since we process samples synchronously, just forward notification downstream */
435 pTransform = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
436 if (!pTransform)
437 hr = E_FAIL;
438 else
439 hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
440 if (SUCCEEDED(hr))
442 hr = IPin_EndOfStream(ppin);
443 IPin_Release(ppin);
446 if (FAILED(hr))
447 ERR("%x\n", hr);
448 return hr;
451 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
453 BaseInputPin* This = impl_BaseInputPin_from_IPin(iface);
454 TransformFilter* pTransform;
455 HRESULT hr = S_OK;
457 TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
459 pTransform = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
461 if (pTransform->pFuncsTable->pfnSetMediaType)
462 hr = pTransform->pFuncsTable->pfnSetMediaType(pTransform, PINDIR_INPUT, pmt);
464 if (SUCCEEDED(hr) && pTransform->pFuncsTable->pfnCompleteConnect)
465 hr = pTransform->pFuncsTable->pfnCompleteConnect(pTransform, PINDIR_INPUT, pReceivePin);
467 if (SUCCEEDED(hr))
469 hr = BaseInputPinImpl_ReceiveConnection(iface, pReceivePin, pmt);
470 if (FAILED(hr) && pTransform->pFuncsTable->pfnBreakConnect)
471 pTransform->pFuncsTable->pfnBreakConnect(pTransform, PINDIR_INPUT);
474 return hr;
477 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
479 BaseInputPin* This = impl_BaseInputPin_from_IPin(iface);
480 TransformFilter* pTransform;
482 TRACE("(%p)->()\n", iface);
484 pTransform = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
485 if (pTransform->pFuncsTable->pfnBreakConnect)
486 pTransform->pFuncsTable->pfnBreakConnect(pTransform, PINDIR_INPUT);
488 return BasePinImpl_Disconnect(iface);
491 static HRESULT WINAPI TransformFilter_InputPin_BeginFlush(IPin * iface)
493 BaseInputPin* This = impl_BaseInputPin_from_IPin(iface);
494 TransformFilter* pTransform;
495 HRESULT hr = S_OK;
497 TRACE("(%p)->()\n", iface);
499 pTransform = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
500 EnterCriticalSection(&pTransform->filter.csFilter);
501 if (pTransform->pFuncsTable->pfnBeginFlush)
502 hr = pTransform->pFuncsTable->pfnBeginFlush(pTransform);
503 if (SUCCEEDED(hr))
504 hr = BaseInputPinImpl_BeginFlush(iface);
505 LeaveCriticalSection(&pTransform->filter.csFilter);
506 return hr;
509 static HRESULT WINAPI TransformFilter_InputPin_EndFlush(IPin * iface)
511 BaseInputPin* This = impl_BaseInputPin_from_IPin(iface);
512 TransformFilter* pTransform;
513 HRESULT hr = S_OK;
515 TRACE("(%p)->()\n", iface);
517 pTransform = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
518 EnterCriticalSection(&pTransform->filter.csFilter);
519 if (pTransform->pFuncsTable->pfnEndFlush)
520 hr = pTransform->pFuncsTable->pfnEndFlush(pTransform);
521 if (SUCCEEDED(hr))
522 hr = BaseInputPinImpl_EndFlush(iface);
523 LeaveCriticalSection(&pTransform->filter.csFilter);
524 return hr;
527 static HRESULT WINAPI TransformFilter_InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
529 BaseInputPin* This = impl_BaseInputPin_from_IPin(iface);
530 TransformFilter* pTransform;
531 HRESULT hr = S_OK;
533 TRACE("(%p)->(%s %s %e)\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
535 pTransform = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
536 EnterCriticalSection(&pTransform->filter.csFilter);
537 if (pTransform->pFuncsTable->pfnNewSegment)
538 hr = pTransform->pFuncsTable->pfnNewSegment(pTransform, tStart, tStop, dRate);
539 if (SUCCEEDED(hr))
540 hr = BaseInputPinImpl_NewSegment(iface, tStart, tStop, dRate);
541 LeaveCriticalSection(&pTransform->filter.csFilter);
542 return hr;
545 static const IPinVtbl TransformFilter_InputPin_Vtbl =
547 BaseInputPinImpl_QueryInterface,
548 BasePinImpl_AddRef,
549 BaseInputPinImpl_Release,
550 BaseInputPinImpl_Connect,
551 TransformFilter_InputPin_ReceiveConnection,
552 TransformFilter_InputPin_Disconnect,
553 BasePinImpl_ConnectedTo,
554 BasePinImpl_ConnectionMediaType,
555 BasePinImpl_QueryPinInfo,
556 BasePinImpl_QueryDirection,
557 BasePinImpl_QueryId,
558 BasePinImpl_QueryAccept,
559 BasePinImpl_EnumMediaTypes,
560 BasePinImpl_QueryInternalConnections,
561 TransformFilter_InputPin_EndOfStream,
562 TransformFilter_InputPin_BeginFlush,
563 TransformFilter_InputPin_EndFlush,
564 TransformFilter_InputPin_NewSegment
567 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
569 BaseOutputPinImpl_QueryInterface,
570 BasePinImpl_AddRef,
571 BaseOutputPinImpl_Release,
572 BaseOutputPinImpl_Connect,
573 BaseOutputPinImpl_ReceiveConnection,
574 BaseOutputPinImpl_Disconnect,
575 BasePinImpl_ConnectedTo,
576 BasePinImpl_ConnectionMediaType,
577 BasePinImpl_QueryPinInfo,
578 BasePinImpl_QueryDirection,
579 BasePinImpl_QueryId,
580 BasePinImpl_QueryAccept,
581 BasePinImpl_EnumMediaTypes,
582 BasePinImpl_QueryInternalConnections,
583 BaseOutputPinImpl_EndOfStream,
584 BaseOutputPinImpl_BeginFlush,
585 BaseOutputPinImpl_EndFlush,
586 BasePinImpl_NewSegment
589 static HRESULT WINAPI TransformFilter_QualityControlImpl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm) {
590 QualityControlImpl *qc = (QualityControlImpl*)iface;
591 TransformFilter *This = impl_from_IBaseFilter(qc->self);
593 if (This->pFuncsTable->pfnNotify)
594 return This->pFuncsTable->pfnNotify(This, sender, qm);
595 else
596 return TransformFilterImpl_Notify(This, sender, qm);
599 static const IQualityControlVtbl TransformFilter_QualityControl_Vtbl = {
600 QualityControlImpl_QueryInterface,
601 QualityControlImpl_AddRef,
602 QualityControlImpl_Release,
603 TransformFilter_QualityControlImpl_Notify,
604 QualityControlImpl_SetSink