1 /*****************************************************************************
2 * filter.c : DirectShow access module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: filter.cpp,v 1.1 2003/08/24 11:17:39 gbazin Exp $
7 * Author: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc/input.h>
44 # define _I64_MAX LONG_LONG_MAX
45 # define LONGLONG long long
54 /*****************************************************************************
56 * Easier to define them hear as mingw doesn't provide them all.
57 *****************************************************************************/
58 const GUID CLSID_SystemDeviceEnum = {0x62be5d10, 0x60eb, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
59 const GUID CLSID_VideoInputDeviceCategory = {0x860BB310,0x5D01,0x11d0,{0xBD,0x3B,0x00,0xA0,0xC9,0x11,0xCE,0x86}};
60 const GUID IID_IPropertyBag = {0x55272A00, 0x42CB, 0x11CE, {0x81, 0x35, 0x00, 0xAA, 0x00, 0x4B, 0xB8, 0x51}};
61 const GUID IID_ICreateDevEnum = {0x29840822, 0x5b84, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
62 const GUID IID_IFilterGraph = {0x56a8689f, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
63 const GUID IID_IMediaControl = {0x56a868b1, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
64 const GUID CLSID_FilterGraph = {0xe436ebb3, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
66 const GUID IID_IUnknown = {0x00000000, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
67 const GUID IID_IPersist = {0x0000010c, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
68 const GUID IID_IMediaFilter = {0x56a86899, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
69 const GUID IID_IBaseFilter = {0x56a86895, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
70 const GUID IID_IPin = {0x56a86891, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
71 const GUID IID_IMemInputPin = {0x56a8689d, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
73 const GUID IID_IEnumPins = {0x56a86892, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
74 const GUID IID_IEnumMediaTypes = {0x89c31040, 0x846b, 0x11ce, {0x97,0xd3, 0x00,0xaa,0x00,0x55,0x59,0x5a}};
77 * MEDIATYPEs and MEDIASUBTYPEs
79 const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
81 const GUID MEDIASUBTYPE_RGB8 = {0xe436eb7a, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
82 const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
83 const GUID MEDIASUBTYPE_RGB555 = {0xe436eb7c, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
84 const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
85 const GUID MEDIASUBTYPE_RGB32 = {0xe436eb7e, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
86 const GUID MEDIASUBTYPE_ARGB32 = {0x773c9ac0, 0x3274, 0x11d0, {0xb7, 0x24, 0x0, 0xaa, 0x0, 0x6c, 0x1a, 0x1}};
88 const GUID MEDIASUBTYPE_YUYV = {0x56595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
89 const GUID MEDIASUBTYPE_Y411 = {0x31313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
90 const GUID MEDIASUBTYPE_Y41P = {0x50313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
91 const GUID MEDIASUBTYPE_YUY2 = {0x32595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
92 const GUID MEDIASUBTYPE_YVYU = {0x55595659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
93 const GUID MEDIASUBTYPE_UYVY = {0x59565955, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
94 const GUID MEDIASUBTYPE_Y211 = {0x31313259, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
95 const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
97 void WINAPI FreeMediaType( AM_MEDIA_TYPE& mt )
99 if( mt.cbFormat != 0 )
101 CoTaskMemFree( (PVOID)mt.pbFormat );
105 if( mt.pUnk != NULL )
112 HRESULT WINAPI CopyMediaType( AM_MEDIA_TYPE *pmtTarget,
113 const AM_MEDIA_TYPE *pmtSource )
115 *pmtTarget = *pmtSource;
116 if( pmtSource->cbFormat != 0 )
118 pmtTarget->pbFormat = (PBYTE)CoTaskMemAlloc( pmtSource->cbFormat );
119 if( pmtTarget->pbFormat == NULL )
121 pmtTarget->cbFormat = 0;
122 return E_OUTOFMEMORY;
126 CopyMemory( (PVOID)pmtTarget->pbFormat, (PVOID)pmtSource->pbFormat,
127 pmtTarget->cbFormat );
130 if( pmtTarget->pUnk != NULL )
132 pmtTarget->pUnk->AddRef();
138 /****************************************************************************
139 * Implementation of our dummy directshow filter pin class
140 ****************************************************************************/
142 CapturePin::CapturePin( input_thread_t * _p_input, CaptureFilter *_p_filter )
143 : p_input( _p_input ), p_filter( _p_filter ), p_connected_pin( NULL ),
148 CapturePin::~CapturePin()
152 HRESULT CapturePin::CustomGetSample( VLCMediaSample *vlc_sample )
154 if( samples_queue.size() )
156 *vlc_sample = samples_queue.back();
157 samples_queue.pop_back();
163 AM_MEDIA_TYPE CapturePin::CustomGetMediaType()
168 /* IUnknown methods */
169 STDMETHODIMP CapturePin::QueryInterface(REFIID riid, void **ppv)
172 msg_Dbg( p_input, "CapturePin::QueryInterface" );
175 if( riid == IID_IUnknown ||
181 if( riid == IID_IMemInputPin )
183 *ppv = (IMemInputPin *)this;
189 return E_NOINTERFACE;
192 STDMETHODIMP_(ULONG) CapturePin::AddRef()
195 msg_Dbg( p_input, "CapturePin::AddRef" );
201 STDMETHODIMP_(ULONG) CapturePin::Release()
204 msg_Dbg( p_input, "CapturePin::Release" );
208 if( !i_ref ) delete this;
214 STDMETHODIMP CapturePin::Connect( IPin * pReceivePin,
215 const AM_MEDIA_TYPE *pmt )
218 msg_Dbg( p_input, "CapturePin::Connect" );
222 STDMETHODIMP CapturePin::ReceiveConnection( IPin * pConnector,
223 const AM_MEDIA_TYPE *pmt )
226 msg_Dbg( p_input, "CapturePin::ReceiveConnection" );
229 p_connected_pin = pConnector;
230 p_connected_pin->AddRef();
231 return CopyMediaType( &media_type, pmt );
233 STDMETHODIMP CapturePin::Disconnect()
236 msg_Dbg( p_input, "CapturePin::Disconnect" );
239 p_connected_pin->Release();
240 p_connected_pin = NULL;
241 FreeMediaType( media_type );
244 STDMETHODIMP CapturePin::ConnectedTo( IPin **pPin )
247 msg_Dbg( p_input, "CapturePin::ConnectedTo" );
250 p_connected_pin->AddRef();
251 *pPin = p_connected_pin;
255 STDMETHODIMP CapturePin::ConnectionMediaType( AM_MEDIA_TYPE *pmt )
258 msg_Dbg( p_input, "CapturePin::ConnectionMediaType" );
261 return CopyMediaType( pmt, &media_type );
263 STDMETHODIMP CapturePin::QueryPinInfo( PIN_INFO * pInfo )
266 msg_Dbg( p_input, "CapturePin::QueryPinInfo" );
269 pInfo->pFilter = p_filter;
270 if( p_filter ) p_filter->AddRef();
272 pInfo->achName[0] = L'\0';
274 pInfo->dir = PINDIR_INPUT;
278 STDMETHODIMP CapturePin::QueryDirection( PIN_DIRECTION * pPinDir )
281 msg_Dbg( p_input, "CapturePin::QueryDirection" );
284 *pPinDir = PINDIR_INPUT;
287 STDMETHODIMP CapturePin::QueryId( LPWSTR * Id )
290 msg_Dbg( p_input, "CapturePin::QueryId" );
294 STDMETHODIMP CapturePin::QueryAccept( const AM_MEDIA_TYPE *pmt )
297 msg_Dbg( p_input, "CapturePin::QueryAccept" );
301 STDMETHODIMP CapturePin::EnumMediaTypes( IEnumMediaTypes **ppEnum )
304 msg_Dbg( p_input, "CapturePin::EnumMediaTypes" );
307 *ppEnum = new CaptureEnumMediaTypes( p_input, this, NULL );
309 if( *ppEnum == NULL ) return E_OUTOFMEMORY;
313 STDMETHODIMP CapturePin::QueryInternalConnections( IPin* *apPin, ULONG *nPin )
316 msg_Dbg( p_input, "CapturePin::QueryInternalConnections" );
320 STDMETHODIMP CapturePin::EndOfStream( void )
323 msg_Dbg( p_input, "CapturePin::EndOfStream" );
327 STDMETHODIMP CapturePin::BeginFlush( void )
330 msg_Dbg( p_input, "CapturePin::BeginFlush" );
334 STDMETHODIMP CapturePin::EndFlush( void )
337 msg_Dbg( p_input, "CapturePin::EndFlush" );
341 STDMETHODIMP CapturePin::NewSegment( REFERENCE_TIME tStart,
342 REFERENCE_TIME tStop,
346 msg_Dbg( p_input, "CapturePin::NewSegment" );
351 /* IMemInputPin methods */
352 STDMETHODIMP CapturePin::GetAllocator( IMemAllocator **ppAllocator )
355 msg_Dbg( p_input, "CapturePin::GetAllocator" );
358 return VFW_E_NO_ALLOCATOR;
360 STDMETHODIMP CapturePin::NotifyAllocator( IMemAllocator *pAllocator,
364 msg_Dbg( p_input, "CapturePin::NotifyAllocator" );
369 STDMETHODIMP CapturePin::GetAllocatorRequirements( ALLOCATOR_PROPERTIES *pProps )
372 msg_Dbg( p_input, "CapturePin::GetAllocatorRequirements" );
376 STDMETHODIMP CapturePin::Receive( IMediaSample *pSample )
379 msg_Dbg( p_input, "CapturePin::Receive" );
383 mtime_t i_timestamp = mdate() * 10;
384 VLCMediaSample vlc_sample = {pSample, i_timestamp};
385 samples_queue.push_front( vlc_sample );
387 /* Make sure we don't cache too many samples */
388 if( samples_queue.size() > 10 )
390 vlc_sample = samples_queue.back();
391 samples_queue.pop_back();
392 msg_Dbg( p_input, "CapturePin::Receive trashing late input sample" );
393 // vlc_sample.p_sample->Release();
398 STDMETHODIMP CapturePin::ReceiveMultiple( IMediaSample **pSamples,
400 long *nSamplesProcessed )
403 msg_Dbg( p_input, "CapturePin::ReceiveMultiple" );
408 *nSamplesProcessed = 0;
409 while( nSamples-- > 0 )
411 hr = Receive( pSamples[*nSamplesProcessed] );
412 if( hr != S_OK ) break;
413 (*nSamplesProcessed)++;
417 STDMETHODIMP CapturePin::ReceiveCanBlock( void )
420 msg_Dbg( p_input, "CapturePin::ReceiveCanBlock" );
423 return S_FALSE; /* Thou shalt not block */
426 /****************************************************************************
427 * Implementation of our dummy directshow filter class
428 ****************************************************************************/
430 CaptureFilter::CaptureFilter( input_thread_t * _p_input )
431 : p_input( _p_input ), p_pin( new CapturePin( _p_input, this ) ),
436 CaptureFilter::~CaptureFilter()
441 /* IUnknown methods */
442 STDMETHODIMP CaptureFilter::QueryInterface( REFIID riid, void **ppv )
445 msg_Dbg( p_input, "CaptureFilter::QueryInterface" );
448 if( riid == IID_IUnknown )
450 *ppv = (IUnknown *)this;
453 if( riid == IID_IPersist )
455 *ppv = (IPersist *)this;
458 if( riid == IID_IMediaFilter )
460 *ppv = (IMediaFilter *)this;
463 if( riid == IID_IBaseFilter )
465 *ppv = (IBaseFilter *)this;
471 return E_NOINTERFACE;
474 STDMETHODIMP_(ULONG) CaptureFilter::AddRef()
477 msg_Dbg( p_input, "CaptureFilter::AddRef" );
483 STDMETHODIMP_(ULONG) CaptureFilter::Release()
486 msg_Dbg( p_input, "CaptureFilter::Release" );
490 if( !i_ref ) delete this;
495 /* IPersist method */
496 STDMETHODIMP CaptureFilter::GetClassID(CLSID *pClsID)
499 msg_Dbg( p_input, "CaptureFilter::GetClassID" );
504 /* IMediaFilter methods */
505 STDMETHODIMP CaptureFilter::GetState(DWORD dwMSecs, FILTER_STATE *State)
508 msg_Dbg( p_input, "CaptureFilter::GetStat" );
512 STDMETHODIMP CaptureFilter::SetSyncSource(IReferenceClock *pClock)
515 msg_Dbg( p_input, "CaptureFilter::SetSyncSource" );
520 STDMETHODIMP CaptureFilter::GetSyncSource(IReferenceClock **pClock)
523 msg_Dbg( p_input, "CaptureFilter::GetSyncSource" );
527 STDMETHODIMP CaptureFilter::Stop()
530 msg_Dbg( p_input, "CaptureFilter::Stop" );
534 STDMETHODIMP CaptureFilter::Pause()
537 msg_Dbg( p_input, "CaptureFilter::Pause" );
541 STDMETHODIMP CaptureFilter::Run(REFERENCE_TIME tStart)
544 msg_Dbg( p_input, "CaptureFilter::Run" );
549 /* IBaseFilter methods */
550 STDMETHODIMP CaptureFilter::EnumPins( IEnumPins ** ppEnum )
553 msg_Dbg( p_input, "CaptureFilter::EnumPins" );
556 /* Create a new ref counted enumerator */
557 *ppEnum = new CaptureEnumPins( p_input, this, NULL );
558 return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR;
560 STDMETHODIMP CaptureFilter::FindPin( LPCWSTR Id, IPin ** ppPin )
563 msg_Dbg( p_input, "CaptureFilter::FindPin" );
567 STDMETHODIMP CaptureFilter::QueryFilterInfo( FILTER_INFO * pInfo )
570 msg_Dbg( p_input, "CaptureFilter::QueryFilterInfo" );
573 pInfo->achName[0] = L'\0';
575 pInfo->pGraph = p_graph;
576 if( p_graph ) p_graph->AddRef();
580 STDMETHODIMP CaptureFilter::JoinFilterGraph( IFilterGraph * pGraph,
584 msg_Dbg( p_input, "CaptureFilter::JoinFilterGraph" );
591 STDMETHODIMP CaptureFilter::QueryVendorInfo( LPWSTR* pVendorInfo )
594 msg_Dbg( p_input, "CaptureFilter::QueryVendorInfo" );
600 CapturePin *CaptureFilter::CustomGetPin()
605 /****************************************************************************
606 * Implementation of our dummy directshow enumpins class
607 ****************************************************************************/
609 CaptureEnumPins::CaptureEnumPins( input_thread_t * _p_input,
610 CaptureFilter *_p_filter,
611 CaptureEnumPins *pEnumPins )
612 : p_input( _p_input ), p_filter( _p_filter ), i_ref( 1 )
614 /* Hold a reference count on our filter */
617 /* Are we creating a new enumerator */
619 if( pEnumPins == NULL )
625 i_position = pEnumPins->i_position;
629 CaptureEnumPins::~CaptureEnumPins()
634 /* IUnknown methods */
635 STDMETHODIMP CaptureEnumPins::QueryInterface( REFIID riid, void **ppv )
638 msg_Dbg( p_input, "CaptureEnumPins::QueryInterface" );
641 if( riid == IID_IUnknown ||
642 riid == IID_IEnumPins )
644 *ppv = (IEnumPins *)this;
650 return E_NOINTERFACE;
653 STDMETHODIMP_(ULONG) CaptureEnumPins::AddRef()
656 msg_Dbg( p_input, "CaptureEnumPins::AddRef" );
662 STDMETHODIMP_(ULONG) CaptureEnumPins::Release()
665 msg_Dbg( p_input, "CaptureEnumPins::Release" );
669 if( !i_ref ) delete this;
675 STDMETHODIMP CaptureEnumPins::Next( ULONG cPins, IPin ** ppPins,
679 msg_Dbg( p_input, "CaptureEnumPins::Next" );
684 if( i_position < 1 && cPins > 0 )
686 IPin *pPin = p_filter->CustomGetPin();
696 STDMETHODIMP CaptureEnumPins::Skip( ULONG cPins )
699 msg_Dbg( p_input, "CaptureEnumPins::Skip" );
710 STDMETHODIMP CaptureEnumPins::Reset()
713 msg_Dbg( p_input, "CaptureEnumPins::Reset" );
719 STDMETHODIMP CaptureEnumPins::Clone( IEnumPins **ppEnum )
722 msg_Dbg( p_input, "CaptureEnumPins::Clone" );
725 *ppEnum = new CaptureEnumPins( p_input, p_filter, this );
726 if( *ppEnum == NULL ) return E_OUTOFMEMORY;
731 /****************************************************************************
732 * Implementation of our dummy directshow enummediatypes class
733 ****************************************************************************/
735 CaptureEnumMediaTypes::CaptureEnumMediaTypes( input_thread_t * _p_input,
737 CaptureEnumMediaTypes *pEnumMediaTypes )
738 : p_input( _p_input ), p_pin( _p_pin ), i_ref( 1 )
740 /* Hold a reference count on our filter */
743 /* Are we creating a new enumerator */
745 if( pEnumMediaTypes == NULL )
751 i_position = pEnumMediaTypes->i_position;
755 CaptureEnumMediaTypes::~CaptureEnumMediaTypes()
760 /* IUnknown methods */
761 STDMETHODIMP CaptureEnumMediaTypes::QueryInterface( REFIID riid, void **ppv )
764 msg_Dbg( p_input, "CaptureEnumMediaTypes::QueryInterface" );
767 if( riid == IID_IUnknown ||
768 riid == IID_IEnumMediaTypes )
770 *ppv = (IEnumMediaTypes *)this;
776 return E_NOINTERFACE;
779 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::AddRef()
782 msg_Dbg( p_input, "CaptureEnumMediaTypes::AddRef" );
788 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::Release()
791 msg_Dbg( p_input, "CaptureEnumMediaTypes::Release" );
795 if( !i_ref ) delete this;
800 /* IEnumMediaTypes */
801 STDMETHODIMP CaptureEnumMediaTypes::Next( ULONG cMediaTypes,
802 AM_MEDIA_TYPE ** ppMediaTypes,
806 msg_Dbg( p_input, "CaptureEnumMediaTypes::Next" );
812 if( i_position < 1 && cMediaTypes > 0 )
814 IPin *pPin = p_pin->CustomGetPin();
815 *ppMediaTypes = pPin;
825 STDMETHODIMP CaptureEnumMediaTypes::Skip( ULONG cMediaTypes )
828 msg_Dbg( p_input, "CaptureEnumMediaTypes::Skip" );
831 if( cMediaTypes > 1 )
836 i_position += cMediaTypes;
839 STDMETHODIMP CaptureEnumMediaTypes::Reset()
842 msg_Dbg( p_input, "CaptureEnumMediaTypes::Reset" );
848 STDMETHODIMP CaptureEnumMediaTypes::Clone( IEnumMediaTypes **ppEnum )
851 msg_Dbg( p_input, "CaptureEnumMediaTypes::Clone" );
854 *ppEnum = new CaptureEnumMediaTypes( p_input, p_pin, this );
855 if( *ppEnum == NULL ) return E_OUTOFMEMORY;