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