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