ieframe: Added ITargetFramePriv2 interface to HlinkFrame object.
[wine] / dlls / strmbase / transform.c
1 /*
2  * Transform Filter (Base for decoders, etc...)
3  *
4  * Copyright 2005 Christian Costa
5  * Copyright 2010 Aric Stewart, CodeWeavers
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "config.h"
22 #include <stdarg.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "dshow.h"
29 #include "amvideo.h"
30 #include "strmif.h"
31 #include "vfw.h"
32
33 #include <assert.h>
34
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "wine/strmbase.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
40
41 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
42 static const WCHAR wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};
43
44 static const IBaseFilterVtbl TransformFilter_Vtbl;
45 static const IPinVtbl TransformFilter_InputPin_Vtbl;
46 static const IPinVtbl TransformFilter_OutputPin_Vtbl;
47 static const IQualityControlVtbl TransformFilter_QualityControl_Vtbl;
48
49 static HRESULT WINAPI TransformFilter_Input_CheckMediaType(BasePin *iface, const AM_MEDIA_TYPE * pmt)
50 {
51     BaseInputPin* This = (BaseInputPin*) iface;
52     TransformFilter * pTransform;
53
54     TRACE("%p\n", iface);
55     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
56
57     if (pTransform->pFuncsTable->pfnCheckInputType)
58         return pTransform->pFuncsTable->pfnCheckInputType(pTransform, pmt);
59     /* Assume OK if there's no query method (the connection will fail if
60        needed) */
61     return S_OK;
62 }
63
64 static HRESULT WINAPI TransformFilter_Input_Receive(BaseInputPin *This, IMediaSample *pInSample)
65 {
66     HRESULT hr = S_FALSE;
67     TransformFilter * pTransform;
68     TRACE("%p\n", This);
69     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
70
71     EnterCriticalSection(&pTransform->filter.csFilter);
72     if (pTransform->filter.state == State_Stopped)
73     {
74         LeaveCriticalSection(&pTransform->filter.csFilter);
75         return VFW_E_WRONG_STATE;
76     }
77
78     if (This->end_of_stream || This->flushing)
79     {
80         LeaveCriticalSection(&pTransform->filter.csFilter);
81         return S_FALSE;
82     }
83     LeaveCriticalSection(&pTransform->filter.csFilter);
84
85     if (pTransform->pFuncsTable->pfnReceive)
86         hr = pTransform->pFuncsTable->pfnReceive(pTransform, pInSample);
87     else
88         hr = S_FALSE;
89
90     return hr;
91 }
92
93 static HRESULT WINAPI TransformFilter_Output_QueryAccept(IPin *iface, const AM_MEDIA_TYPE * pmt)
94 {
95     BasePin *This = (BasePin *)iface;
96     TransformFilter *pTransformFilter = (TransformFilter *)This->pinInfo.pFilter;
97     AM_MEDIA_TYPE* outpmt = &pTransformFilter->pmt;
98     TRACE("%p\n", iface);
99
100     if (IsEqualIID(&pmt->majortype, &outpmt->majortype)
101         && (IsEqualIID(&pmt->subtype, &outpmt->subtype) || IsEqualIID(&outpmt->subtype, &GUID_NULL)))
102         return S_OK;
103     return S_FALSE;
104 }
105
106 static HRESULT WINAPI TransformFilter_Output_DecideBufferSize(BaseOutputPin *This, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
107 {
108     TransformFilter *pTransformFilter = (TransformFilter *)This->pin.pinInfo.pFilter;
109     return pTransformFilter->pFuncsTable->pfnDecideBufferSize(pTransformFilter, pAlloc, ppropInputRequest);
110 }
111
112 static HRESULT WINAPI TransformFilter_Output_GetMediaType(BasePin *This, int iPosition, AM_MEDIA_TYPE *pmt)
113 {
114     TransformFilter *pTransform = (TransformFilter *)This->pinInfo.pFilter;
115
116     if (iPosition < 0)
117         return E_INVALIDARG;
118     if (iPosition > 0)
119         return VFW_S_NO_MORE_ITEMS;
120     CopyMediaType(pmt, &pTransform->pmt);
121     return S_OK;
122 }
123
124 static IPin* WINAPI TransformFilter_GetPin(BaseFilter *iface, int pos)
125 {
126     TransformFilter *This = (TransformFilter *)iface;
127
128     if (pos >= This->npins || pos < 0)
129         return NULL;
130
131     IPin_AddRef(This->ppPins[pos]);
132     return This->ppPins[pos];
133 }
134
135 static LONG WINAPI TransformFilter_GetPinCount(BaseFilter *iface)
136 {
137     TransformFilter *This = (TransformFilter *)iface;
138
139     return (This->npins+1);
140 }
141
142 static const BaseFilterFuncTable tfBaseFuncTable = {
143     TransformFilter_GetPin,
144     TransformFilter_GetPinCount
145 };
146
147 static const  BasePinFuncTable tf_input_BaseFuncTable = {
148     TransformFilter_Input_CheckMediaType,
149     NULL,
150     BasePinImpl_GetMediaTypeVersion,
151     BasePinImpl_GetMediaType
152 };
153
154 static const BaseInputPinFuncTable tf_input_BaseInputFuncTable = {
155     TransformFilter_Input_Receive
156 };
157
158 static const  BasePinFuncTable tf_output_BaseFuncTable = {
159     NULL,
160     BaseOutputPinImpl_AttemptConnection,
161     BasePinImpl_GetMediaTypeVersion,
162     TransformFilter_Output_GetMediaType
163 };
164
165 static const BaseOutputPinFuncTable tf_output_BaseOutputFuncTable = {
166     TransformFilter_Output_DecideBufferSize,
167     BaseOutputPinImpl_DecideAllocator,
168     BaseOutputPinImpl_BreakConnect
169 };
170
171 static HRESULT TransformFilter_Init(const IBaseFilterVtbl *pVtbl, const CLSID* pClsid, const TransformFilterFuncTable* pFuncsTable, TransformFilter* pTransformFilter)
172 {
173     HRESULT hr;
174     PIN_INFO piInput;
175     PIN_INFO piOutput;
176
177     BaseFilter_Init(&pTransformFilter->filter, pVtbl, pClsid, (DWORD_PTR)(__FILE__ ": TransformFilter.csFilter"), &tfBaseFuncTable);
178
179     /* pTransformFilter is already allocated */
180     pTransformFilter->pFuncsTable = pFuncsTable;
181     ZeroMemory(&pTransformFilter->pmt, sizeof(pTransformFilter->pmt));
182     pTransformFilter->npins = 2;
183
184     pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
185
186     /* construct input pin */
187     piInput.dir = PINDIR_INPUT;
188     piInput.pFilter = (IBaseFilter *)pTransformFilter;
189     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
190     piOutput.dir = PINDIR_OUTPUT;
191     piOutput.pFilter = (IBaseFilter *)pTransformFilter;
192     lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
193
194     hr = BaseInputPin_Construct(&TransformFilter_InputPin_Vtbl, &piInput, &tf_input_BaseFuncTable, &tf_input_BaseInputFuncTable, &pTransformFilter->filter.csFilter, NULL, &pTransformFilter->ppPins[0]);
195
196     if (SUCCEEDED(hr))
197     {
198         hr = BaseOutputPin_Construct(&TransformFilter_OutputPin_Vtbl, sizeof(BaseOutputPin), &piOutput, &tf_output_BaseFuncTable, &tf_output_BaseOutputFuncTable, &pTransformFilter->filter.csFilter, &pTransformFilter->ppPins[1]);
199
200         if (FAILED(hr))
201             ERR("Cannot create output pin (%x)\n", hr);
202         else {
203             QualityControlImpl_init(&pTransformFilter->qcimpl, (IPin*)pTransformFilter->ppPins[0], (IBaseFilter*)pTransformFilter);
204             pTransformFilter->qcimpl.lpVtbl = &TransformFilter_QualityControl_Vtbl;
205         }
206     }
207     if (FAILED(hr))
208     {
209         CoTaskMemFree(pTransformFilter->ppPins);
210         BaseFilterImpl_Release((IBaseFilter*)pTransformFilter);
211     }
212
213     return hr;
214 }
215
216 HRESULT TransformFilter_Construct(const IBaseFilterVtbl *pVtbl, LONG filter_size, const CLSID* pClsid, const TransformFilterFuncTable* pFuncsTable, IBaseFilter ** ppTransformFilter)
217 {
218     TransformFilter* pTf;
219
220     *ppTransformFilter = NULL;
221
222     assert(filter_size >= sizeof(TransformFilter));
223
224     pTf = CoTaskMemAlloc(filter_size);
225
226     if (!pTf)
227         return E_OUTOFMEMORY;
228
229     ZeroMemory(pTf, filter_size);
230
231     if (SUCCEEDED(TransformFilter_Init(pVtbl, pClsid, pFuncsTable, pTf)))
232     {
233         *ppTransformFilter = (IBaseFilter*)(&pTf->filter.lpVtbl);
234         return S_OK;
235     }
236
237     CoTaskMemFree(pTf);
238     return E_FAIL;
239 }
240
241 HRESULT WINAPI TransformFilterImpl_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
242 {
243     HRESULT hr;
244     TransformFilter *This = (TransformFilter *)iface;
245     TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
246
247     if (IsEqualIID(riid, &IID_IQualityControl))  {
248         *ppv = (IQualityControl*)&This->qcimpl;
249         IUnknown_AddRef((IUnknown*)*ppv);
250         return S_OK;
251     }
252     hr = BaseFilterImpl_QueryInterface(iface, riid, ppv);
253
254     if (FAILED(hr) && !IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow) &&
255         !IsEqualIID(riid, &IID_IAMFilterMiscFlags))
256         FIXME("No interface for %s!\n", debugstr_guid(riid));
257
258     return hr;
259 }
260
261 ULONG WINAPI TransformFilterImpl_Release(IBaseFilter * iface)
262 {
263     TransformFilter *This = (TransformFilter *)iface;
264     ULONG refCount = BaseFilterImpl_Release(iface);
265
266     TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
267
268     if (!refCount)
269     {
270         ULONG i;
271
272         for (i = 0; i < This->npins; i++)
273         {
274             IPin *pConnectedTo;
275
276             if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
277             {
278                 IPin_Disconnect(pConnectedTo);
279                 IPin_Release(pConnectedTo);
280             }
281             IPin_Disconnect(This->ppPins[i]);
282
283             IPin_Release(This->ppPins[i]);
284         }
285
286         CoTaskMemFree(This->ppPins);
287
288         TRACE("Destroying transform filter\n");
289         FreeMediaType(&This->pmt);
290         CoTaskMemFree(This);
291
292         return 0;
293     }
294     else
295         return refCount;
296 }
297
298 /** IMediaFilter methods **/
299
300 HRESULT WINAPI TransformFilterImpl_Stop(IBaseFilter * iface)
301 {
302     TransformFilter *This = (TransformFilter *)iface;
303     HRESULT hr = S_OK;
304
305     TRACE("(%p/%p)\n", This, iface);
306
307     EnterCriticalSection(&This->filter.csFilter);
308     {
309         This->filter.state = State_Stopped;
310         if (This->pFuncsTable->pfnStopStreaming)
311             hr = This->pFuncsTable->pfnStopStreaming(This);
312     }
313     LeaveCriticalSection(&This->filter.csFilter);
314
315     return hr;
316 }
317
318 HRESULT WINAPI TransformFilterImpl_Pause(IBaseFilter * iface)
319 {
320     TransformFilter *This = (TransformFilter *)iface;
321     HRESULT hr;
322
323     TRACE("(%p/%p)->()\n", This, iface);
324
325     EnterCriticalSection(&This->filter.csFilter);
326     {
327         if (This->filter.state == State_Stopped)
328             hr = IBaseFilter_Run(iface, -1);
329         else
330             hr = S_OK;
331
332         if (SUCCEEDED(hr))
333             This->filter.state = State_Paused;
334     }
335     LeaveCriticalSection(&This->filter.csFilter);
336
337     return hr;
338 }
339
340 HRESULT WINAPI TransformFilterImpl_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
341 {
342     HRESULT hr = S_OK;
343     TransformFilter *This = (TransformFilter *)iface;
344
345     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
346
347     EnterCriticalSection(&This->filter.csFilter);
348     {
349         if (This->filter.state == State_Stopped)
350         {
351             ((BaseInputPin *)This->ppPins[0])->end_of_stream = 0;
352             if (This->pFuncsTable->pfnStartStreaming)
353                 hr = This->pFuncsTable->pfnStartStreaming(This);
354             if (SUCCEEDED(hr))
355                 hr = BaseOutputPinImpl_Active((BaseOutputPin *)This->ppPins[1]);
356         }
357
358         if (SUCCEEDED(hr))
359         {
360             This->filter.rtStreamStart = tStart;
361             This->filter.state = State_Running;
362         }
363     }
364     LeaveCriticalSection(&This->filter.csFilter);
365
366     return hr;
367 }
368
369 /** IBaseFilter implementation **/
370
371 HRESULT WINAPI TransformFilterImpl_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
372 {
373     TransformFilter *This = (TransformFilter *)iface;
374
375     TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
376
377     return E_NOTIMPL;
378 }
379
380 static const IBaseFilterVtbl TransformFilter_Vtbl =
381 {
382     TransformFilterImpl_QueryInterface,
383     BaseFilterImpl_AddRef,
384     TransformFilterImpl_Release,
385     BaseFilterImpl_GetClassID,
386     TransformFilterImpl_Stop,
387     TransformFilterImpl_Pause,
388     TransformFilterImpl_Run,
389     BaseFilterImpl_GetState,
390     BaseFilterImpl_SetSyncSource,
391     BaseFilterImpl_GetSyncSource,
392     BaseFilterImpl_EnumPins,
393     TransformFilterImpl_FindPin,
394     BaseFilterImpl_QueryFilterInfo,
395     BaseFilterImpl_JoinFilterGraph,
396     BaseFilterImpl_QueryVendorInfo
397 };
398
399 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
400 {
401     BaseInputPin* This = (BaseInputPin*) iface;
402     TransformFilter* pTransform;
403     IPin* ppin;
404     HRESULT hr;
405
406     TRACE("(%p)->()\n", iface);
407
408     /* Since we process samples synchronously, just forward notification downstream */
409     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
410     if (!pTransform)
411         hr = E_FAIL;
412     else
413         hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
414     if (SUCCEEDED(hr))
415     {
416         hr = IPin_EndOfStream(ppin);
417         IPin_Release(ppin);
418     }
419
420     if (FAILED(hr))
421         ERR("%x\n", hr);
422     return hr;
423 }
424
425 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
426 {
427     BaseInputPin* This = (BaseInputPin*) iface;
428     TransformFilter* pTransform;
429     HRESULT hr = S_OK;
430
431     TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
432
433     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
434
435     if (pTransform->pFuncsTable->pfnSetMediaType)
436         hr = pTransform->pFuncsTable->pfnSetMediaType(pTransform, PINDIR_INPUT, pmt);
437
438     if (SUCCEEDED(hr) && pTransform->pFuncsTable->pfnCompleteConnect)
439         hr = pTransform->pFuncsTable->pfnCompleteConnect(pTransform, PINDIR_INPUT, pReceivePin);
440
441     if (SUCCEEDED(hr))
442     {
443         hr = BaseInputPinImpl_ReceiveConnection(iface, pReceivePin, pmt);
444         if (FAILED(hr) && pTransform->pFuncsTable->pfnBreakConnect)
445             pTransform->pFuncsTable->pfnBreakConnect(pTransform, PINDIR_INPUT);
446     }
447
448     return hr;
449 }
450
451 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
452 {
453     BaseInputPin* This = (BaseInputPin*) iface;
454     TransformFilter* pTransform;
455
456     TRACE("(%p)->()\n", iface);
457
458     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
459     if (pTransform->pFuncsTable->pfnBreakConnect)
460         pTransform->pFuncsTable->pfnBreakConnect(pTransform, PINDIR_INPUT);
461
462     return BasePinImpl_Disconnect(iface);
463 }
464
465 static HRESULT WINAPI TransformFilter_InputPin_BeginFlush(IPin * iface)
466 {
467     BaseInputPin* This = (BaseInputPin*) iface;
468     TransformFilter* pTransform;
469     HRESULT hr = S_OK;
470
471     TRACE("(%p)->()\n", iface);
472
473     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
474     EnterCriticalSection(&pTransform->filter.csFilter);
475     if (pTransform->pFuncsTable->pfnBeginFlush)
476         hr = pTransform->pFuncsTable->pfnBeginFlush(pTransform);
477     if (SUCCEEDED(hr))
478         hr = BaseInputPinImpl_BeginFlush(iface);
479     LeaveCriticalSection(&pTransform->filter.csFilter);
480     return hr;
481 }
482
483 static HRESULT WINAPI TransformFilter_InputPin_EndFlush(IPin * iface)
484 {
485     BaseInputPin* This = (BaseInputPin*) iface;
486     TransformFilter* pTransform;
487     HRESULT hr = S_OK;
488
489     TRACE("(%p)->()\n", iface);
490
491     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
492     EnterCriticalSection(&pTransform->filter.csFilter);
493     if (pTransform->pFuncsTable->pfnEndFlush)
494         hr = pTransform->pFuncsTable->pfnEndFlush(pTransform);
495     if (SUCCEEDED(hr))
496         hr = BaseInputPinImpl_EndFlush(iface);
497     LeaveCriticalSection(&pTransform->filter.csFilter);
498     return hr;
499 }
500
501 static HRESULT WINAPI TransformFilter_InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
502 {
503     BaseInputPin* This = (BaseInputPin*) iface;
504     TransformFilter* pTransform;
505     HRESULT hr = S_OK;
506
507     TRACE("(%p)->()\n", iface);
508
509     pTransform = (TransformFilter*)This->pin.pinInfo.pFilter;
510     EnterCriticalSection(&pTransform->filter.csFilter);
511     if (pTransform->pFuncsTable->pfnNewSegment)
512         hr = pTransform->pFuncsTable->pfnNewSegment(pTransform, tStart, tStop, dRate);
513     if (SUCCEEDED(hr))
514         hr = BaseInputPinImpl_NewSegment(iface, tStart, tStop, dRate);
515     LeaveCriticalSection(&pTransform->filter.csFilter);
516     return hr;
517 }
518
519 static const IPinVtbl TransformFilter_InputPin_Vtbl =
520 {
521     BaseInputPinImpl_QueryInterface,
522     BasePinImpl_AddRef,
523     BaseInputPinImpl_Release,
524     BaseInputPinImpl_Connect,
525     TransformFilter_InputPin_ReceiveConnection,
526     TransformFilter_InputPin_Disconnect,
527     BasePinImpl_ConnectedTo,
528     BasePinImpl_ConnectionMediaType,
529     BasePinImpl_QueryPinInfo,
530     BasePinImpl_QueryDirection,
531     BasePinImpl_QueryId,
532     BaseInputPinImpl_QueryAccept,
533     BasePinImpl_EnumMediaTypes,
534     BasePinImpl_QueryInternalConnections,
535     TransformFilter_InputPin_EndOfStream,
536     TransformFilter_InputPin_BeginFlush,
537     TransformFilter_InputPin_EndFlush,
538     TransformFilter_InputPin_NewSegment
539 };
540
541 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
542 {
543     BaseOutputPinImpl_QueryInterface,
544     BasePinImpl_AddRef,
545     BaseOutputPinImpl_Release,
546     BaseOutputPinImpl_Connect,
547     BaseOutputPinImpl_ReceiveConnection,
548     BaseOutputPinImpl_Disconnect,
549     BasePinImpl_ConnectedTo,
550     BasePinImpl_ConnectionMediaType,
551     BasePinImpl_QueryPinInfo,
552     BasePinImpl_QueryDirection,
553     BasePinImpl_QueryId,
554     TransformFilter_Output_QueryAccept,
555     BasePinImpl_EnumMediaTypes,
556     BasePinImpl_QueryInternalConnections,
557     BaseOutputPinImpl_EndOfStream,
558     BaseOutputPinImpl_BeginFlush,
559     BaseOutputPinImpl_EndFlush,
560     BasePinImpl_NewSegment
561 };
562
563 static HRESULT WINAPI TransformFilter_QualityControlImpl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm) {
564     QualityControlImpl *qc = (QualityControlImpl*)iface;
565     TransformFilter *This = (TransformFilter *)qc->self;
566
567     if (This->pFuncsTable->pfnNotify)
568         return This->pFuncsTable->pfnNotify(This, sender, qm);
569     else
570         return QualityControlImpl_Notify(iface, sender, qm);
571 }
572
573 static const IQualityControlVtbl TransformFilter_QualityControl_Vtbl = {
574     QualityControlImpl_QueryInterface,
575     QualityControlImpl_AddRef,
576     QualityControlImpl_Release,
577     TransformFilter_QualityControlImpl_Notify,
578     QualityControlImpl_SetSink
579 };