quartz: Exclude unused headers.
[wine] / dlls / quartz / transform.c
1 /*
2  * Transform Filter (Base for decoders, etc...)
3  *
4  * Copyright 2005 Christian Costa
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include "quartz_private.h"
24 #include "control_private.h"
25 #include "pin.h"
26
27 #include "amvideo.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "dshow.h"
31 #include "strmif.h"
32 #include "vfw.h"
33
34 #include <assert.h>
35
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
38
39 #include "transform.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
42
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};
45
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;
50
51 static HRESULT TransformFilter_Sample(LPVOID iface, IMediaSample * pSample)
52 {
53     TransformFilterImpl *This = (TransformFilterImpl *)iface;
54
55     TRACE("%p %p\n", iface, pSample);
56
57     return This->pFuncsTable->pfnProcessSampleData(This, pSample);
58 }
59
60 static HRESULT TransformFilter_Input_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
61 {
62     TransformFilterImpl* This = (TransformFilterImpl*)iface;
63     TRACE("%p\n", iface);
64     dump_AM_MEDIA_TYPE(pmt);
65
66     if (This->pFuncsTable->pfnQueryConnect)
67         return This->pFuncsTable->pfnQueryConnect(This, pmt);
68     /* Assume OK if there's no query method (the connection will fail if
69        needed) */
70     return S_OK;
71 }
72
73
74 static HRESULT TransformFilter_Output_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
75 {
76     TransformFilterImpl* pTransformFilter = (TransformFilterImpl*)iface;
77     AM_MEDIA_TYPE* outpmt = &((OutputPin*)pTransformFilter->ppPins[1])->pin.mtCurrent;
78     TRACE("%p\n", iface);
79
80     if (IsEqualIID(&pmt->majortype, &outpmt->majortype) && IsEqualIID(&pmt->subtype, &outpmt->subtype))
81         return S_OK;
82     return S_FALSE;
83 }
84
85 static HRESULT TransformFilter_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
86 {
87     InputPin * pPinImpl;
88
89     *ppPin = NULL;
90
91     if (pPinInfo->dir != PINDIR_INPUT)
92     {
93         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
94         return E_INVALIDARG;
95     }
96
97     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
98
99     if (!pPinImpl)
100         return E_OUTOFMEMORY;
101
102     if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
103     {
104         pPinImpl->pin.lpVtbl = &TransformFilter_InputPin_Vtbl;
105         pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
106
107         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
108         return S_OK;
109     }
110     return E_FAIL;
111 }
112
113 static HRESULT TransformFilter_OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
114 {
115     OutputPin * pPinImpl;
116
117     *ppPin = NULL;
118
119     if (pPinInfo->dir != PINDIR_OUTPUT)
120     {
121         ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
122         return E_INVALIDARG;
123     }
124
125     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
126
127     if (!pPinImpl)
128         return E_OUTOFMEMORY;
129
130     if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
131     {
132         pPinImpl->pin.lpVtbl = &TransformFilter_OutputPin_Vtbl;
133
134         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
135         return S_OK;
136     }
137     return E_FAIL;
138 }
139
140 HRESULT TransformFilter_Create(TransformFilterImpl* pTransformFilter, const CLSID* pClsid, const TransformFuncsTable* pFuncsTable)
141 {
142     HRESULT hr;
143     PIN_INFO piInput;
144     PIN_INFO piOutput;
145
146     /* pTransformFilter is already allocated */
147     pTransformFilter->clsid = *pClsid;
148     pTransformFilter->pFuncsTable = pFuncsTable;
149
150     pTransformFilter->lpVtbl = &TransformFilter_Vtbl;
151
152     pTransformFilter->refCount = 1;
153     InitializeCriticalSection(&pTransformFilter->csFilter);
154     pTransformFilter->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TransformFilterImpl.csFilter");
155     pTransformFilter->state = State_Stopped;
156     pTransformFilter->pClock = NULL;
157     ZeroMemory(&pTransformFilter->filterInfo, sizeof(FILTER_INFO));
158
159     pTransformFilter->ppPins = CoTaskMemAlloc(2 * sizeof(IPin *));
160
161     /* construct input pin */
162     piInput.dir = PINDIR_INPUT;
163     piInput.pFilter = (IBaseFilter *)pTransformFilter;
164     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
165     piOutput.dir = PINDIR_OUTPUT;
166     piOutput.pFilter = (IBaseFilter *)pTransformFilter;
167     lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
168
169     hr = TransformFilter_InputPin_Construct(&piInput, TransformFilter_Sample, pTransformFilter, TransformFilter_Input_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[0]);
170
171     if (SUCCEEDED(hr))
172     {
173         ALLOCATOR_PROPERTIES props;
174         props.cbAlign = 1;
175         props.cbPrefix = 0;
176         props.cbBuffer = 0; /* Will be updated at connection time */
177         props.cBuffers = 2;
178
179         hr = TransformFilter_OutputPin_Construct(&piOutput, &props, pTransformFilter, TransformFilter_Output_QueryAccept, &pTransformFilter->csFilter, &pTransformFilter->ppPins[1]);
180
181         if (FAILED(hr))
182             ERR("Cannot create output pin (%x)\n", hr);
183     }
184     else
185     {
186         CoTaskMemFree(pTransformFilter->ppPins);
187         pTransformFilter->csFilter.DebugInfo->Spare[0] = 0;
188         DeleteCriticalSection(&pTransformFilter->csFilter);
189         CoTaskMemFree(pTransformFilter);
190     }
191
192     return hr;
193 }
194
195 static HRESULT WINAPI TransformFilter_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
196 {
197     TransformFilterImpl *This = (TransformFilterImpl *)iface;
198     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
199
200     *ppv = NULL;
201
202     if (IsEqualIID(riid, &IID_IUnknown))
203         *ppv = (LPVOID)This;
204     else if (IsEqualIID(riid, &IID_IPersist))
205         *ppv = (LPVOID)This;
206     else if (IsEqualIID(riid, &IID_IMediaFilter))
207         *ppv = (LPVOID)This;
208     else if (IsEqualIID(riid, &IID_IBaseFilter))
209         *ppv = (LPVOID)This;
210
211     if (*ppv)
212     {
213         IUnknown_AddRef((IUnknown *)(*ppv));
214         return S_OK;
215     }
216
217     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
218
219     return E_NOINTERFACE;
220 }
221
222 static ULONG WINAPI TransformFilter_AddRef(IBaseFilter * iface)
223 {
224     TransformFilterImpl *This = (TransformFilterImpl *)iface;
225     ULONG refCount = InterlockedIncrement(&This->refCount);
226
227     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
228
229     return refCount;
230 }
231
232 static ULONG WINAPI TransformFilter_Release(IBaseFilter * iface)
233 {
234     TransformFilterImpl *This = (TransformFilterImpl *)iface;
235     ULONG refCount = InterlockedDecrement(&This->refCount);
236
237     TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
238
239     if (!refCount)
240     {
241         ULONG i;
242
243         if (This->pClock)
244             IReferenceClock_Release(This->pClock);
245
246         for (i = 0; i < 2; i++)
247         {
248             IPin *pConnectedTo;
249
250             if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
251             {
252                 IPin_Disconnect(pConnectedTo);
253                 IPin_Release(pConnectedTo);
254             }
255             IPin_Disconnect(This->ppPins[i]);
256
257             IPin_Release(This->ppPins[i]);
258         }
259
260         CoTaskMemFree(This->ppPins);
261         This->lpVtbl = NULL;
262
263         This->csFilter.DebugInfo->Spare[0] = 0;
264         DeleteCriticalSection(&This->csFilter);
265
266         TRACE("Destroying transform filter\n");
267         CoTaskMemFree(This);
268
269         return 0;
270     }
271     else
272         return refCount;
273 }
274
275 /** IPersist methods **/
276
277 static HRESULT WINAPI TransformFilter_GetClassID(IBaseFilter * iface, CLSID * pClsid)
278 {
279     TransformFilterImpl *This = (TransformFilterImpl *)iface;
280
281     TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
282
283     *pClsid = This->clsid;
284
285     return S_OK;
286 }
287
288 /** IMediaFilter methods **/
289
290 static HRESULT WINAPI TransformFilter_Stop(IBaseFilter * iface)
291 {
292     TransformFilterImpl *This = (TransformFilterImpl *)iface;
293
294     TRACE("(%p/%p)\n", This, iface);
295
296     EnterCriticalSection(&This->csFilter);
297     {
298         This->state = State_Stopped;
299         if (This->pFuncsTable->pfnProcessEnd)
300             This->pFuncsTable->pfnProcessEnd(This);
301     }
302     LeaveCriticalSection(&This->csFilter);
303
304     return S_OK;
305 }
306
307 static HRESULT WINAPI TransformFilter_Pause(IBaseFilter * iface)
308 {
309     TransformFilterImpl *This = (TransformFilterImpl *)iface;
310
311     TRACE("(%p/%p)->()\n", This, iface);
312
313     EnterCriticalSection(&This->csFilter);
314     {
315         This->state = State_Paused;
316     }
317     LeaveCriticalSection(&This->csFilter);
318
319     return S_OK;
320 }
321
322 static HRESULT WINAPI TransformFilter_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
323 {
324     HRESULT hr = S_OK;
325     TransformFilterImpl *This = (TransformFilterImpl *)iface;
326
327     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
328
329     EnterCriticalSection(&This->csFilter);
330     {
331         This->rtStreamStart = tStart;
332         This->state = State_Running;
333         OutputPin_CommitAllocator((OutputPin *)This->ppPins[1]);
334         if (This->pFuncsTable->pfnProcessBegin)
335             This->pFuncsTable->pfnProcessBegin(This);
336     }
337     LeaveCriticalSection(&This->csFilter);
338
339     return hr;
340 }
341
342 static HRESULT WINAPI TransformFilter_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
343 {
344     TransformFilterImpl *This = (TransformFilterImpl *)iface;
345
346     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
347
348     EnterCriticalSection(&This->csFilter);
349     {
350         *pState = This->state;
351     }
352     LeaveCriticalSection(&This->csFilter);
353
354     return S_OK;
355 }
356
357 static HRESULT WINAPI TransformFilter_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
358 {
359     TransformFilterImpl *This = (TransformFilterImpl *)iface;
360
361     TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
362
363     EnterCriticalSection(&This->csFilter);
364     {
365         if (This->pClock)
366             IReferenceClock_Release(This->pClock);
367         This->pClock = pClock;
368         if (This->pClock)
369             IReferenceClock_AddRef(This->pClock);
370     }
371     LeaveCriticalSection(&This->csFilter);
372
373     return S_OK;
374 }
375
376 static HRESULT WINAPI TransformFilter_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
377 {
378     TransformFilterImpl *This = (TransformFilterImpl *)iface;
379
380     TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
381
382     EnterCriticalSection(&This->csFilter);
383     {
384         *ppClock = This->pClock;
385         IReferenceClock_AddRef(This->pClock);
386     }
387     LeaveCriticalSection(&This->csFilter);
388
389     return S_OK;
390 }
391
392 /** IBaseFilter implementation **/
393
394 static HRESULT WINAPI TransformFilter_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
395 {
396     ENUMPINDETAILS epd;
397     TransformFilterImpl *This = (TransformFilterImpl *)iface;
398
399     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
400
401     epd.cPins = 2; /* input and output pins */
402     epd.ppPins = This->ppPins;
403     return IEnumPinsImpl_Construct(&epd, ppEnum);
404 }
405
406 static HRESULT WINAPI TransformFilter_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
407 {
408     TransformFilterImpl *This = (TransformFilterImpl *)iface;
409
410     TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
411
412     return E_NOTIMPL;
413 }
414
415 static HRESULT WINAPI TransformFilter_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
416 {
417     TransformFilterImpl *This = (TransformFilterImpl *)iface;
418
419     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
420
421     strcpyW(pInfo->achName, This->filterInfo.achName);
422     pInfo->pGraph = This->filterInfo.pGraph;
423
424     if (pInfo->pGraph)
425         IFilterGraph_AddRef(pInfo->pGraph);
426
427     return S_OK;
428 }
429
430 static HRESULT WINAPI TransformFilter_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
431 {
432     HRESULT hr = S_OK;
433     TransformFilterImpl *This = (TransformFilterImpl *)iface;
434
435     TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
436
437     EnterCriticalSection(&This->csFilter);
438     {
439         if (pName)
440             strcpyW(This->filterInfo.achName, pName);
441         else
442             *This->filterInfo.achName = '\0';
443         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
444     }
445     LeaveCriticalSection(&This->csFilter);
446
447     return hr;
448 }
449
450 static HRESULT WINAPI TransformFilter_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
451 {
452     TransformFilterImpl *This = (TransformFilterImpl *)iface;
453     TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
454     return E_NOTIMPL;
455 }
456
457 static const IBaseFilterVtbl TransformFilter_Vtbl =
458 {
459     TransformFilter_QueryInterface,
460     TransformFilter_AddRef,
461     TransformFilter_Release,
462     TransformFilter_GetClassID,
463     TransformFilter_Stop,
464     TransformFilter_Pause,
465     TransformFilter_Run,
466     TransformFilter_GetState,
467     TransformFilter_SetSyncSource,
468     TransformFilter_GetSyncSource,
469     TransformFilter_EnumPins,
470     TransformFilter_FindPin,
471     TransformFilter_QueryFilterInfo,
472     TransformFilter_JoinFilterGraph,
473     TransformFilter_QueryVendorInfo
474 };
475
476 static HRESULT WINAPI TransformFilter_InputPin_EndOfStream(IPin * iface)
477 {
478     InputPin* This = (InputPin*) iface;
479     TransformFilterImpl* pTransform;
480     IPin* ppin;
481     HRESULT hr;
482     
483     TRACE("(%p)->()\n", iface);
484
485     /* Since we process samples synchronously, just forward notification downstream */
486     pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
487     if (!pTransform)
488         hr = E_FAIL;
489     else
490         hr = IPin_ConnectedTo(pTransform->ppPins[1], &ppin);
491     if (SUCCEEDED(hr))
492     {
493         hr = IPin_EndOfStream(ppin);
494         IPin_Release(ppin);
495     }
496
497     if (FAILED(hr))
498         ERR("%x\n", hr);
499     return hr;
500 }
501
502 static HRESULT WINAPI TransformFilter_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
503 {
504     InputPin* This = (InputPin*) iface;
505     TransformFilterImpl* pTransform;
506     HRESULT hr;
507
508     TRACE("(%p)->(%p, %p)\n", iface, pReceivePin, pmt);
509
510     pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
511
512     hr = pTransform->pFuncsTable->pfnConnectInput(pTransform, pmt);
513     if (SUCCEEDED(hr))
514     {
515         hr = InputPin_ReceiveConnection(iface, pReceivePin, pmt);
516         if (FAILED(hr))
517             pTransform->pFuncsTable->pfnCleanup(pTransform);
518     }
519
520     return hr;
521 }
522
523 static HRESULT WINAPI TransformFilter_InputPin_Disconnect(IPin * iface)
524 {
525     InputPin* This = (InputPin*) iface;
526     TransformFilterImpl* pTransform;
527
528     TRACE("(%p)->()\n", iface);
529
530     pTransform = (TransformFilterImpl*)This->pin.pinInfo.pFilter;
531     pTransform->pFuncsTable->pfnCleanup(pTransform);
532
533     return IPinImpl_Disconnect(iface);
534 }
535
536 static const IPinVtbl TransformFilter_InputPin_Vtbl = 
537 {
538     InputPin_QueryInterface,
539     IPinImpl_AddRef,
540     InputPin_Release,
541     InputPin_Connect,
542     TransformFilter_InputPin_ReceiveConnection,
543     TransformFilter_InputPin_Disconnect,
544     IPinImpl_ConnectedTo,
545     IPinImpl_ConnectionMediaType,
546     IPinImpl_QueryPinInfo,
547     IPinImpl_QueryDirection,
548     IPinImpl_QueryId,
549     IPinImpl_QueryAccept,
550     IPinImpl_EnumMediaTypes,
551     IPinImpl_QueryInternalConnections,
552     TransformFilter_InputPin_EndOfStream,
553     InputPin_BeginFlush,
554     InputPin_EndFlush,
555     InputPin_NewSegment
556 };
557
558 static HRESULT WINAPI TransformFilter_Output_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
559 {
560     IPinImpl *This = (IPinImpl *)iface;
561     ENUMMEDIADETAILS emd;
562
563     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
564
565     emd.cMediaTypes = 1;
566     emd.pMediaTypes = &This->mtCurrent;
567
568     return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
569 }
570
571 static const IPinVtbl TransformFilter_OutputPin_Vtbl =
572 {
573     OutputPin_QueryInterface,
574     IPinImpl_AddRef,
575     OutputPin_Release,
576     OutputPin_Connect,
577     OutputPin_ReceiveConnection,
578     OutputPin_Disconnect,
579     IPinImpl_ConnectedTo,
580     IPinImpl_ConnectionMediaType,
581     IPinImpl_QueryPinInfo,
582     IPinImpl_QueryDirection,
583     IPinImpl_QueryId,
584     IPinImpl_QueryAccept,
585     TransformFilter_Output_EnumMediaTypes,
586     IPinImpl_QueryInternalConnections,
587     OutputPin_EndOfStream,
588     OutputPin_BeginFlush,
589     OutputPin_EndFlush,
590     OutputPin_NewSegment
591 };
592
593 static const IMemInputPinVtbl MemInputPin_Vtbl = 
594 {
595     MemInputPin_QueryInterface,
596     MemInputPin_AddRef,
597     MemInputPin_Release,
598     MemInputPin_GetAllocator,
599     MemInputPin_NotifyAllocator,
600     MemInputPin_GetAllocatorRequirements,
601     MemInputPin_Receive,
602     MemInputPin_ReceiveMultiple,
603     MemInputPin_ReceiveCanBlock
604 };