msvcr120: Add _dpcomp implementation.
[wine.git] / dlls / qedit / mediadet.c
blob9098223657ad02c05373f1dd47d66d3191a1ee40
1 /* DirectShow Media Detector object (QEDIT.DLL)
3 * Copyright 2008 Google (Lei Zhang, Dan Hipschman)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <assert.h>
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
30 #include "qedit_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(qedit);
35 typedef struct MediaDetImpl {
36 IUnknown IUnknown_inner;
37 IMediaDet IMediaDet_iface;
38 IUnknown *outer_unk;
39 LONG ref;
40 IGraphBuilder *graph;
41 IBaseFilter *source;
42 IBaseFilter *splitter;
43 LONG num_streams;
44 LONG cur_stream;
45 IPin *cur_pin;
46 } MediaDetImpl;
48 static inline MediaDetImpl *impl_from_IUnknown(IUnknown *iface)
50 return CONTAINING_RECORD(iface, MediaDetImpl, IUnknown_inner);
53 static inline MediaDetImpl *impl_from_IMediaDet(IMediaDet *iface)
55 return CONTAINING_RECORD(iface, MediaDetImpl, IMediaDet_iface);
58 static void MD_cleanup(MediaDetImpl *This)
60 if (This->cur_pin) IPin_Release(This->cur_pin);
61 This->cur_pin = NULL;
62 if (This->source) IBaseFilter_Release(This->source);
63 This->source = NULL;
64 if (This->splitter) IBaseFilter_Release(This->splitter);
65 This->splitter = NULL;
66 if (This->graph) IGraphBuilder_Release(This->graph);
67 This->graph = NULL;
68 This->num_streams = -1;
69 This->cur_stream = 0;
72 /* MediaDet inner IUnknown */
73 static HRESULT WINAPI MediaDet_inner_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
75 MediaDetImpl *This = impl_from_IUnknown(iface);
77 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
79 *ppv = NULL;
80 if (IsEqualIID(riid, &IID_IUnknown))
81 *ppv = &This->IUnknown_inner;
82 else if (IsEqualIID(riid, &IID_IMediaDet))
83 *ppv = &This->IMediaDet_iface;
84 else
85 WARN("(%p, %s,%p): not found\n", This, debugstr_guid(riid), ppv);
87 if (!*ppv)
88 return E_NOINTERFACE;
90 IUnknown_AddRef((IUnknown*)*ppv);
91 return S_OK;
94 static ULONG WINAPI MediaDet_inner_AddRef(IUnknown *iface)
96 MediaDetImpl *This = impl_from_IUnknown(iface);
97 ULONG ref = InterlockedIncrement(&This->ref);
99 TRACE("(%p) new ref = %u\n", This, ref);
101 return ref;
104 static ULONG WINAPI MediaDet_inner_Release(IUnknown *iface)
106 MediaDetImpl *This = impl_from_IUnknown(iface);
107 ULONG ref = InterlockedDecrement(&This->ref);
109 TRACE("(%p) new ref = %u\n", This, ref);
111 if (ref == 0)
113 MD_cleanup(This);
114 CoTaskMemFree(This);
115 return 0;
118 return ref;
121 static const IUnknownVtbl mediadet_vtbl =
123 MediaDet_inner_QueryInterface,
124 MediaDet_inner_AddRef,
125 MediaDet_inner_Release,
128 /* IMediaDet implementation */
129 static HRESULT WINAPI MediaDet_QueryInterface(IMediaDet *iface, REFIID riid, void **ppv)
131 MediaDetImpl *This = impl_from_IMediaDet(iface);
132 return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
135 static ULONG WINAPI MediaDet_AddRef(IMediaDet *iface)
137 MediaDetImpl *This = impl_from_IMediaDet(iface);
138 return IUnknown_AddRef(This->outer_unk);
141 static ULONG WINAPI MediaDet_Release(IMediaDet *iface)
143 MediaDetImpl *This = impl_from_IMediaDet(iface);
144 return IUnknown_Release(This->outer_unk);
147 static HRESULT WINAPI MediaDet_get_Filter(IMediaDet* iface, IUnknown **pVal)
149 MediaDetImpl *This = impl_from_IMediaDet(iface);
150 FIXME("(%p)->(%p): not implemented!\n", This, pVal);
151 return E_NOTIMPL;
154 static HRESULT WINAPI MediaDet_put_Filter(IMediaDet* iface, IUnknown *newVal)
156 MediaDetImpl *This = impl_from_IMediaDet(iface);
157 FIXME("(%p)->(%p): not implemented!\n", This, newVal);
158 return E_NOTIMPL;
161 static HRESULT WINAPI MediaDet_get_OutputStreams(IMediaDet* iface, LONG *pVal)
163 MediaDetImpl *This = impl_from_IMediaDet(iface);
164 IEnumPins *pins;
165 IPin *pin;
166 HRESULT hr;
168 TRACE("(%p)\n", This);
170 if (!This->splitter)
171 return E_INVALIDARG;
173 if (This->num_streams != -1)
175 *pVal = This->num_streams;
176 return S_OK;
179 *pVal = 0;
181 hr = IBaseFilter_EnumPins(This->splitter, &pins);
182 if (FAILED(hr))
183 return hr;
185 while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK)
187 PIN_DIRECTION dir;
188 hr = IPin_QueryDirection(pin, &dir);
189 IPin_Release(pin);
190 if (FAILED(hr))
192 IEnumPins_Release(pins);
193 return hr;
196 if (dir == PINDIR_OUTPUT)
197 ++*pVal;
199 IEnumPins_Release(pins);
201 This->num_streams = *pVal;
202 return S_OK;
205 static HRESULT WINAPI MediaDet_get_CurrentStream(IMediaDet* iface, LONG *pVal)
207 MediaDetImpl *This = impl_from_IMediaDet(iface);
208 TRACE("(%p)\n", This);
210 if (!pVal)
211 return E_POINTER;
213 *pVal = This->cur_stream;
214 return S_OK;
217 static HRESULT SetCurPin(MediaDetImpl *This, LONG strm)
219 IEnumPins *pins;
220 IPin *pin;
221 HRESULT hr;
223 assert(This->splitter);
224 assert(0 <= strm && strm < This->num_streams);
226 if (This->cur_pin)
228 IPin_Release(This->cur_pin);
229 This->cur_pin = NULL;
232 hr = IBaseFilter_EnumPins(This->splitter, &pins);
233 if (FAILED(hr))
234 return hr;
236 while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK && !This->cur_pin)
238 PIN_DIRECTION dir;
239 hr = IPin_QueryDirection(pin, &dir);
240 if (FAILED(hr))
242 IPin_Release(pin);
243 IEnumPins_Release(pins);
244 return hr;
247 if (dir == PINDIR_OUTPUT && strm-- == 0)
248 This->cur_pin = pin;
249 else
250 IPin_Release(pin);
252 IEnumPins_Release(pins);
254 assert(This->cur_pin);
255 return S_OK;
258 static HRESULT WINAPI MediaDet_put_CurrentStream(IMediaDet* iface, LONG newVal)
260 MediaDetImpl *This = impl_from_IMediaDet(iface);
261 HRESULT hr;
263 TRACE("(%p)->(%d)\n", This, newVal);
265 if (This->num_streams == -1)
267 LONG n;
268 hr = MediaDet_get_OutputStreams(iface, &n);
269 if (FAILED(hr))
270 return hr;
273 if (newVal < 0 || This->num_streams <= newVal)
274 return E_INVALIDARG;
276 hr = SetCurPin(This, newVal);
277 if (FAILED(hr))
278 return hr;
280 This->cur_stream = newVal;
281 return S_OK;
284 static HRESULT WINAPI MediaDet_get_StreamType(IMediaDet* iface, GUID *pVal)
286 MediaDetImpl *This = impl_from_IMediaDet(iface);
287 FIXME("(%p)->(%s): not implemented!\n", This, debugstr_guid(pVal));
288 return E_NOTIMPL;
291 static HRESULT WINAPI MediaDet_get_StreamTypeB(IMediaDet* iface, BSTR *pVal)
293 MediaDetImpl *This = impl_from_IMediaDet(iface);
294 FIXME("(%p)->(%p): not implemented!\n", This, pVal);
295 return E_NOTIMPL;
298 static HRESULT WINAPI MediaDet_get_StreamLength(IMediaDet* iface, double *pVal)
300 MediaDetImpl *This = impl_from_IMediaDet(iface);
301 FIXME("(%p): stub!\n", This);
302 return VFW_E_INVALIDMEDIATYPE;
305 static HRESULT WINAPI MediaDet_get_Filename(IMediaDet* iface, BSTR *pVal)
307 MediaDetImpl *This = impl_from_IMediaDet(iface);
308 IFileSourceFilter *file;
309 LPOLESTR name;
310 HRESULT hr;
312 TRACE("(%p)\n", This);
314 if (!pVal)
315 return E_POINTER;
317 *pVal = NULL;
318 /* MSDN says it should return E_FAIL if no file is open, but tests
319 show otherwise. */
320 if (!This->source)
321 return S_OK;
323 hr = IBaseFilter_QueryInterface(This->source, &IID_IFileSourceFilter,
324 (void **) &file);
325 if (FAILED(hr))
326 return hr;
328 hr = IFileSourceFilter_GetCurFile(file, &name, NULL);
329 IFileSourceFilter_Release(file);
330 if (FAILED(hr))
331 return hr;
333 *pVal = SysAllocString(name);
334 CoTaskMemFree(name);
335 if (!*pVal)
336 return E_OUTOFMEMORY;
338 return S_OK;
341 /* From quartz, 2008/04/07 */
342 static HRESULT GetFilterInfo(IMoniker *pMoniker, GUID *pclsid, VARIANT *pvar)
344 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
345 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
346 IPropertyBag *pPropBagCat = NULL;
347 HRESULT hr;
349 VariantInit(pvar);
350 V_VT(pvar) = VT_BSTR;
352 hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag,
353 (LPVOID *) &pPropBagCat);
355 if (SUCCEEDED(hr))
356 hr = IPropertyBag_Read(pPropBagCat, wszClsidName, pvar, NULL);
358 if (SUCCEEDED(hr))
360 hr = CLSIDFromString(V_BSTR(pvar), pclsid);
361 VariantClear(pvar);
362 V_VT(pvar) = VT_BSTR;
365 if (SUCCEEDED(hr))
366 hr = IPropertyBag_Read(pPropBagCat, wszFriendlyName, pvar, NULL);
368 if (SUCCEEDED(hr))
369 TRACE("Moniker = %s - %s\n", debugstr_guid(pclsid), debugstr_w(V_BSTR(pvar)));
371 if (pPropBagCat)
372 IPropertyBag_Release(pPropBagCat);
374 return hr;
377 static HRESULT GetSplitter(MediaDetImpl *This)
379 IFileSourceFilter *file;
380 LPOLESTR name;
381 AM_MEDIA_TYPE mt;
382 GUID type[2];
383 IFilterMapper2 *map;
384 IEnumMoniker *filters;
385 IMoniker *mon;
386 VARIANT var;
387 GUID clsid;
388 IBaseFilter *splitter;
389 IEnumPins *pins;
390 IPin *source_pin, *splitter_pin;
391 HRESULT hr;
393 hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
394 &IID_IFilterMapper2, (void **) &map);
395 if (FAILED(hr))
396 return hr;
398 hr = IBaseFilter_QueryInterface(This->source, &IID_IFileSourceFilter,
399 (void **) &file);
400 if (FAILED(hr))
402 IFilterMapper2_Release(map);
403 return hr;
406 hr = IFileSourceFilter_GetCurFile(file, &name, &mt);
407 IFileSourceFilter_Release(file);
408 CoTaskMemFree(name);
409 if (FAILED(hr))
411 IFilterMapper2_Release(map);
412 return hr;
414 type[0] = mt.majortype;
415 type[1] = mt.subtype;
416 CoTaskMemFree(mt.pbFormat);
418 hr = IFilterMapper2_EnumMatchingFilters(map, &filters, 0, TRUE,
419 MERIT_UNLIKELY, FALSE, 1, type,
420 NULL, NULL, FALSE, TRUE,
421 0, NULL, NULL, NULL);
422 IFilterMapper2_Release(map);
423 if (FAILED(hr))
424 return hr;
426 hr = E_NOINTERFACE;
427 while (IEnumMoniker_Next(filters, 1, &mon, NULL) == S_OK)
429 hr = GetFilterInfo(mon, &clsid, &var);
430 IMoniker_Release(mon);
431 if (FAILED(hr))
432 continue;
434 hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER,
435 &IID_IBaseFilter, (void **) &splitter);
436 if (FAILED(hr))
438 VariantClear(&var);
439 continue;
442 hr = IGraphBuilder_AddFilter(This->graph, splitter, V_BSTR(&var));
443 VariantClear(&var);
444 This->splitter = splitter;
445 if (FAILED(hr))
446 goto retry;
448 hr = IBaseFilter_EnumPins(This->source, &pins);
449 if (FAILED(hr))
450 goto retry;
451 IEnumPins_Next(pins, 1, &source_pin, NULL);
452 IEnumPins_Release(pins);
454 hr = IBaseFilter_EnumPins(splitter, &pins);
455 if (FAILED(hr))
457 IPin_Release(source_pin);
458 goto retry;
460 IEnumPins_Next(pins, 1, &splitter_pin, NULL);
461 IEnumPins_Release(pins);
463 hr = IPin_Connect(source_pin, splitter_pin, NULL);
464 IPin_Release(source_pin);
465 IPin_Release(splitter_pin);
466 if (SUCCEEDED(hr))
467 break;
469 retry:
470 IBaseFilter_Release(splitter);
471 This->splitter = NULL;
474 IEnumMoniker_Release(filters);
475 if (FAILED(hr))
476 return hr;
478 return S_OK;
481 static HRESULT WINAPI MediaDet_put_Filename(IMediaDet* iface, BSTR newVal)
483 static const WCHAR reader[] = {'R','e','a','d','e','r',0};
484 MediaDetImpl *This = impl_from_IMediaDet(iface);
485 IGraphBuilder *gb;
486 IBaseFilter *bf;
487 HRESULT hr;
489 TRACE("(%p)->(%s)\n", This, debugstr_w(newVal));
491 if (This->graph)
493 WARN("MSDN says not to call this method twice\n");
494 MD_cleanup(This);
497 hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
498 &IID_IGraphBuilder, (void **) &gb);
499 if (FAILED(hr))
500 return hr;
502 hr = IGraphBuilder_AddSourceFilter(gb, newVal, reader, &bf);
503 if (FAILED(hr))
505 IGraphBuilder_Release(gb);
506 return hr;
509 This->graph = gb;
510 This->source = bf;
511 hr = GetSplitter(This);
512 if (FAILED(hr))
513 return hr;
515 return MediaDet_put_CurrentStream(iface, 0);
518 static HRESULT WINAPI MediaDet_GetBitmapBits(IMediaDet* iface,
519 double StreamTime,
520 LONG *pBufferSize, char *pBuffer,
521 LONG Width, LONG Height)
523 MediaDetImpl *This = impl_from_IMediaDet(iface);
524 FIXME("(%p)->(%f %p %p %d %d): not implemented!\n", This, StreamTime, pBufferSize, pBuffer,
525 Width, Height);
526 return E_NOTIMPL;
529 static HRESULT WINAPI MediaDet_WriteBitmapBits(IMediaDet* iface,
530 double StreamTime, LONG Width,
531 LONG Height, BSTR Filename)
533 MediaDetImpl *This = impl_from_IMediaDet(iface);
534 FIXME("(%p)->(%f %d %d %p): not implemented!\n", This, StreamTime, Width, Height, Filename);
535 return E_NOTIMPL;
538 static HRESULT WINAPI MediaDet_get_StreamMediaType(IMediaDet* iface,
539 AM_MEDIA_TYPE *pVal)
541 MediaDetImpl *This = impl_from_IMediaDet(iface);
542 IEnumMediaTypes *types;
543 AM_MEDIA_TYPE *pmt;
544 HRESULT hr;
546 TRACE("(%p)\n", This);
548 if (!pVal)
549 return E_POINTER;
551 if (!This->cur_pin)
552 return E_INVALIDARG;
554 hr = IPin_EnumMediaTypes(This->cur_pin, &types);
555 if (SUCCEEDED(hr))
557 hr = (IEnumMediaTypes_Next(types, 1, &pmt, NULL) == S_OK
558 ? S_OK
559 : E_NOINTERFACE);
560 IEnumMediaTypes_Release(types);
563 if (SUCCEEDED(hr))
565 *pVal = *pmt;
566 CoTaskMemFree(pmt);
569 return hr;
572 static HRESULT WINAPI MediaDet_GetSampleGrabber(IMediaDet* iface,
573 ISampleGrabber **ppVal)
575 MediaDetImpl *This = impl_from_IMediaDet(iface);
576 FIXME("(%p)->(%p): not implemented!\n", This, ppVal);
577 return E_NOTIMPL;
580 static HRESULT WINAPI MediaDet_get_FrameRate(IMediaDet* iface, double *pVal)
582 MediaDetImpl *This = impl_from_IMediaDet(iface);
583 AM_MEDIA_TYPE mt;
584 VIDEOINFOHEADER *vh;
585 HRESULT hr;
587 TRACE("(%p)\n", This);
589 if (!pVal)
590 return E_POINTER;
592 hr = MediaDet_get_StreamMediaType(iface, &mt);
593 if (FAILED(hr))
594 return hr;
596 if (!IsEqualGUID(&mt.majortype, &MEDIATYPE_Video))
598 CoTaskMemFree(mt.pbFormat);
599 return VFW_E_INVALIDMEDIATYPE;
602 vh = (VIDEOINFOHEADER *) mt.pbFormat;
603 *pVal = 1.0e7 / (double) vh->AvgTimePerFrame;
605 CoTaskMemFree(mt.pbFormat);
606 return S_OK;
609 static HRESULT WINAPI MediaDet_EnterBitmapGrabMode(IMediaDet* iface,
610 double SeekTime)
612 MediaDetImpl *This = impl_from_IMediaDet(iface);
613 FIXME("(%p)->(%f): not implemented!\n", This, SeekTime);
614 return E_NOTIMPL;
617 static const IMediaDetVtbl IMediaDet_VTable =
619 MediaDet_QueryInterface,
620 MediaDet_AddRef,
621 MediaDet_Release,
622 MediaDet_get_Filter,
623 MediaDet_put_Filter,
624 MediaDet_get_OutputStreams,
625 MediaDet_get_CurrentStream,
626 MediaDet_put_CurrentStream,
627 MediaDet_get_StreamType,
628 MediaDet_get_StreamTypeB,
629 MediaDet_get_StreamLength,
630 MediaDet_get_Filename,
631 MediaDet_put_Filename,
632 MediaDet_GetBitmapBits,
633 MediaDet_WriteBitmapBits,
634 MediaDet_get_StreamMediaType,
635 MediaDet_GetSampleGrabber,
636 MediaDet_get_FrameRate,
637 MediaDet_EnterBitmapGrabMode,
640 HRESULT MediaDet_create(IUnknown * pUnkOuter, LPVOID * ppv) {
641 MediaDetImpl* obj = NULL;
643 TRACE("(%p,%p)\n", ppv, pUnkOuter);
645 obj = CoTaskMemAlloc(sizeof(MediaDetImpl));
646 if (NULL == obj) {
647 *ppv = NULL;
648 return E_OUTOFMEMORY;
650 ZeroMemory(obj, sizeof(MediaDetImpl));
652 obj->ref = 1;
653 obj->IUnknown_inner.lpVtbl = &mediadet_vtbl;
654 obj->IMediaDet_iface.lpVtbl = &IMediaDet_VTable;
655 obj->graph = NULL;
656 obj->source = NULL;
657 obj->splitter = NULL;
658 obj->cur_pin = NULL;
659 obj->num_streams = -1;
660 obj->cur_stream = 0;
662 if (pUnkOuter)
663 obj->outer_unk = pUnkOuter;
664 else
665 obj->outer_unk = &obj->IUnknown_inner;
667 *ppv = &obj->IUnknown_inner;
668 return S_OK;