1 /*****************************************************************************
2 * filter.c : DirectShow access module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: filter.cpp,v 1.3 2003/08/26 19:14:10 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 CLSID_AudioInputDeviceCategory = {0x33d9a762, 0x90c8, 0x11d0, {0xbd, 0x43, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
61 const GUID IID_IPropertyBag = {0x55272A00, 0x42CB, 0x11CE, {0x81, 0x35, 0x00, 0xAA, 0x00, 0x4B, 0xB8, 0x51}};
62 const GUID IID_ICreateDevEnum = {0x29840822, 0x5b84, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
63 const GUID IID_IFilterGraph = {0x56a8689f, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
64 const GUID IID_IMediaControl = {0x56a868b1, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
65 const GUID CLSID_FilterGraph = {0xe436ebb3, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
67 const GUID IID_IUnknown = {0x00000000, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
68 const GUID IID_IPersist = {0x0000010c, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
69 const GUID IID_IMediaFilter = {0x56a86899, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
70 const GUID IID_IBaseFilter = {0x56a86895, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
71 const GUID IID_IPin = {0x56a86891, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
72 const GUID IID_IMemInputPin = {0x56a8689d, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
74 const GUID IID_IEnumPins = {0x56a86892, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
75 const GUID IID_IEnumMediaTypes = {0x89c31040, 0x846b, 0x11ce, {0x97,0xd3, 0x00,0xaa,0x00,0x55,0x59,0x5a}};
78 * MEDIATYPEs and MEDIASUBTYPEs
80 const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
82 const GUID MEDIASUBTYPE_RGB8 = {0xe436eb7a, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
83 const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
84 const GUID MEDIASUBTYPE_RGB555 = {0xe436eb7c, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
85 const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
86 const GUID MEDIASUBTYPE_RGB32 = {0xe436eb7e, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
87 const GUID MEDIASUBTYPE_ARGB32 = {0x773c9ac0, 0x3274, 0x11d0, {0xb7, 0x24, 0x0, 0xaa, 0x0, 0x6c, 0x1a, 0x1}};
89 const GUID MEDIASUBTYPE_YUYV = {0x56595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
90 const GUID MEDIASUBTYPE_Y411 = {0x31313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
91 const GUID MEDIASUBTYPE_Y41P = {0x50313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
92 const GUID MEDIASUBTYPE_YUY2 = {0x32595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
93 const GUID MEDIASUBTYPE_YVYU = {0x55595659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
94 const GUID MEDIASUBTYPE_UYVY = {0x59565955, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
95 const GUID MEDIASUBTYPE_Y211 = {0x31313259, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
96 const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
98 const GUID MEDIATYPE_Audio = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
99 const GUID FORMAT_WaveFormatEx = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
100 const GUID MEDIASUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
102 void WINAPI FreeMediaType( AM_MEDIA_TYPE& mt )
104 if( mt.cbFormat != 0 )
106 CoTaskMemFree( (PVOID)mt.pbFormat );
110 if( mt.pUnk != NULL )
117 HRESULT WINAPI CopyMediaType( AM_MEDIA_TYPE *pmtTarget,
118 const AM_MEDIA_TYPE *pmtSource )
120 *pmtTarget = *pmtSource;
121 if( pmtSource->cbFormat != 0 )
123 pmtTarget->pbFormat = (PBYTE)CoTaskMemAlloc( pmtSource->cbFormat );
124 if( pmtTarget->pbFormat == NULL )
126 pmtTarget->cbFormat = 0;
127 return E_OUTOFMEMORY;
131 CopyMemory( (PVOID)pmtTarget->pbFormat, (PVOID)pmtSource->pbFormat,
132 pmtTarget->cbFormat );
135 if( pmtTarget->pUnk != NULL )
137 pmtTarget->pUnk->AddRef();
143 /****************************************************************************
144 * Implementation of our dummy directshow filter pin class
145 ****************************************************************************/
147 CapturePin::CapturePin( input_thread_t * _p_input, CaptureFilter *_p_filter )
148 : p_input( _p_input ), p_filter( _p_filter ), p_connected_pin( NULL ),
153 CapturePin::~CapturePin()
157 HRESULT CapturePin::CustomGetSample( VLCMediaSample *vlc_sample )
159 if( samples_queue.size() )
161 *vlc_sample = samples_queue.back();
162 samples_queue.pop_back();
168 AM_MEDIA_TYPE CapturePin::CustomGetMediaType()
173 /* IUnknown methods */
174 STDMETHODIMP CapturePin::QueryInterface(REFIID riid, void **ppv)
177 msg_Dbg( p_input, "CapturePin::QueryInterface" );
180 if( riid == IID_IUnknown ||
187 if( riid == IID_IMemInputPin )
190 *ppv = (IMemInputPin *)this;
196 return E_NOINTERFACE;
199 STDMETHODIMP_(ULONG) CapturePin::AddRef()
202 msg_Dbg( p_input, "CapturePin::AddRef" );
207 STDMETHODIMP_(ULONG) CapturePin::Release()
210 msg_Dbg( p_input, "CapturePin::Release" );
214 if( !i_ref ) delete this;
220 STDMETHODIMP CapturePin::Connect( IPin * pReceivePin,
221 const AM_MEDIA_TYPE *pmt )
224 msg_Dbg( p_input, "CapturePin::Connect" );
228 STDMETHODIMP CapturePin::ReceiveConnection( IPin * pConnector,
229 const AM_MEDIA_TYPE *pmt )
232 msg_Dbg( p_input, "CapturePin::ReceiveConnection" );
235 p_connected_pin = pConnector;
236 p_connected_pin->AddRef();
237 return CopyMediaType( &media_type, pmt );
239 STDMETHODIMP CapturePin::Disconnect()
242 msg_Dbg( p_input, "CapturePin::Disconnect" );
245 p_connected_pin->Release();
246 p_connected_pin = NULL;
247 FreeMediaType( media_type );
250 STDMETHODIMP CapturePin::ConnectedTo( IPin **pPin )
253 msg_Dbg( p_input, "CapturePin::ConnectedTo" );
256 if( !p_connected_pin ) return VFW_E_NOT_CONNECTED;
258 p_connected_pin->AddRef();
259 *pPin = p_connected_pin;
263 STDMETHODIMP CapturePin::ConnectionMediaType( AM_MEDIA_TYPE *pmt )
266 msg_Dbg( p_input, "CapturePin::ConnectionMediaType" );
269 return CopyMediaType( pmt, &media_type );
271 STDMETHODIMP CapturePin::QueryPinInfo( PIN_INFO * pInfo )
274 msg_Dbg( p_input, "CapturePin::QueryPinInfo" );
277 pInfo->pFilter = p_filter;
278 if( p_filter ) p_filter->AddRef();
280 pInfo->achName[0] = L'\0';
282 pInfo->dir = PINDIR_INPUT;
286 STDMETHODIMP CapturePin::QueryDirection( PIN_DIRECTION * pPinDir )
289 msg_Dbg( p_input, "CapturePin::QueryDirection" );
292 *pPinDir = PINDIR_INPUT;
295 STDMETHODIMP CapturePin::QueryId( LPWSTR * Id )
298 msg_Dbg( p_input, "CapturePin::QueryId" );
302 STDMETHODIMP CapturePin::QueryAccept( const AM_MEDIA_TYPE *pmt )
305 msg_Dbg( p_input, "CapturePin::QueryAccept" );
309 STDMETHODIMP CapturePin::EnumMediaTypes( IEnumMediaTypes **ppEnum )
312 msg_Dbg( p_input, "CapturePin::EnumMediaTypes" );
315 *ppEnum = new CaptureEnumMediaTypes( p_input, this, NULL );
317 if( *ppEnum == NULL ) return E_OUTOFMEMORY;
321 STDMETHODIMP CapturePin::QueryInternalConnections( IPin* *apPin, ULONG *nPin )
324 msg_Dbg( p_input, "CapturePin::QueryInternalConnections" );
328 STDMETHODIMP CapturePin::EndOfStream( void )
331 msg_Dbg( p_input, "CapturePin::EndOfStream" );
335 STDMETHODIMP CapturePin::BeginFlush( void )
338 msg_Dbg( p_input, "CapturePin::BeginFlush" );
342 STDMETHODIMP CapturePin::EndFlush( void )
345 msg_Dbg( p_input, "CapturePin::EndFlush" );
349 STDMETHODIMP CapturePin::NewSegment( REFERENCE_TIME tStart,
350 REFERENCE_TIME tStop,
354 msg_Dbg( p_input, "CapturePin::NewSegment" );
359 /* IMemInputPin methods */
360 STDMETHODIMP CapturePin::GetAllocator( IMemAllocator **ppAllocator )
363 msg_Dbg( p_input, "CapturePin::GetAllocator" );
366 return VFW_E_NO_ALLOCATOR;
368 STDMETHODIMP CapturePin::NotifyAllocator( IMemAllocator *pAllocator,
372 msg_Dbg( p_input, "CapturePin::NotifyAllocator" );
377 STDMETHODIMP CapturePin::GetAllocatorRequirements( ALLOCATOR_PROPERTIES *pProps )
380 msg_Dbg( p_input, "CapturePin::GetAllocatorRequirements" );
384 STDMETHODIMP CapturePin::Receive( IMediaSample *pSample )
387 //msg_Dbg( p_input, "CapturePin::Receive" );
391 mtime_t i_timestamp = mdate() * 10;
392 VLCMediaSample vlc_sample = {pSample, i_timestamp};
393 samples_queue.push_front( vlc_sample );
395 /* Make sure we don't cache too many samples */
396 if( samples_queue.size() > 10 )
398 vlc_sample = samples_queue.back();
399 samples_queue.pop_back();
400 msg_Dbg( p_input, "CapturePin::Receive trashing late input sample" );
401 // vlc_sample.p_sample->Release();
406 STDMETHODIMP CapturePin::ReceiveMultiple( IMediaSample **pSamples,
408 long *nSamplesProcessed )
411 msg_Dbg( p_input, "CapturePin::ReceiveMultiple" );
416 *nSamplesProcessed = 0;
417 while( nSamples-- > 0 )
419 hr = Receive( pSamples[*nSamplesProcessed] );
420 if( hr != S_OK ) break;
421 (*nSamplesProcessed)++;
425 STDMETHODIMP CapturePin::ReceiveCanBlock( void )
428 msg_Dbg( p_input, "CapturePin::ReceiveCanBlock" );
431 return S_FALSE; /* Thou shalt not block */
434 /****************************************************************************
435 * Implementation of our dummy directshow filter class
436 ****************************************************************************/
438 CaptureFilter::CaptureFilter( input_thread_t * _p_input )
439 : p_input( _p_input ), p_pin( new CapturePin( _p_input, this ) ),
444 CaptureFilter::~CaptureFilter()
449 /* IUnknown methods */
450 STDMETHODIMP CaptureFilter::QueryInterface( REFIID riid, void **ppv )
453 msg_Dbg( p_input, "CaptureFilter::QueryInterface" );
456 if( riid == IID_IUnknown )
459 *ppv = (IUnknown *)this;
462 if( riid == IID_IPersist )
465 *ppv = (IPersist *)this;
468 if( riid == IID_IMediaFilter )
471 *ppv = (IMediaFilter *)this;
474 if( riid == IID_IBaseFilter )
477 *ppv = (IBaseFilter *)this;
483 return E_NOINTERFACE;
486 STDMETHODIMP_(ULONG) CaptureFilter::AddRef()
489 msg_Dbg( p_input, "CaptureFilter::AddRef" );
494 STDMETHODIMP_(ULONG) CaptureFilter::Release()
497 msg_Dbg( p_input, "CaptureFilter::Release" );
501 if( !i_ref ) delete this;
506 /* IPersist method */
507 STDMETHODIMP CaptureFilter::GetClassID(CLSID *pClsID)
510 msg_Dbg( p_input, "CaptureFilter::GetClassID" );
515 /* IMediaFilter methods */
516 STDMETHODIMP CaptureFilter::GetState(DWORD dwMSecs, FILTER_STATE *State)
519 msg_Dbg( p_input, "CaptureFilter::GetStat" );
523 STDMETHODIMP CaptureFilter::SetSyncSource(IReferenceClock *pClock)
526 msg_Dbg( p_input, "CaptureFilter::SetSyncSource" );
531 STDMETHODIMP CaptureFilter::GetSyncSource(IReferenceClock **pClock)
534 msg_Dbg( p_input, "CaptureFilter::GetSyncSource" );
538 STDMETHODIMP CaptureFilter::Stop()
541 msg_Dbg( p_input, "CaptureFilter::Stop" );
545 STDMETHODIMP CaptureFilter::Pause()
548 msg_Dbg( p_input, "CaptureFilter::Pause" );
552 STDMETHODIMP CaptureFilter::Run(REFERENCE_TIME tStart)
555 msg_Dbg( p_input, "CaptureFilter::Run" );
560 /* IBaseFilter methods */
561 STDMETHODIMP CaptureFilter::EnumPins( IEnumPins ** ppEnum )
564 msg_Dbg( p_input, "CaptureFilter::EnumPins" );
567 /* Create a new ref counted enumerator */
568 *ppEnum = new CaptureEnumPins( p_input, this, NULL );
569 return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR;
571 STDMETHODIMP CaptureFilter::FindPin( LPCWSTR Id, IPin ** ppPin )
574 msg_Dbg( p_input, "CaptureFilter::FindPin" );
578 STDMETHODIMP CaptureFilter::QueryFilterInfo( FILTER_INFO * pInfo )
581 msg_Dbg( p_input, "CaptureFilter::QueryFilterInfo" );
584 pInfo->achName[0] = L'\0';
586 pInfo->pGraph = p_graph;
587 if( p_graph ) p_graph->AddRef();
591 STDMETHODIMP CaptureFilter::JoinFilterGraph( IFilterGraph * pGraph,
595 msg_Dbg( p_input, "CaptureFilter::JoinFilterGraph" );
602 STDMETHODIMP CaptureFilter::QueryVendorInfo( LPWSTR* pVendorInfo )
605 msg_Dbg( p_input, "CaptureFilter::QueryVendorInfo" );
611 CapturePin *CaptureFilter::CustomGetPin()
616 /****************************************************************************
617 * Implementation of our dummy directshow enumpins class
618 ****************************************************************************/
620 CaptureEnumPins::CaptureEnumPins( input_thread_t * _p_input,
621 CaptureFilter *_p_filter,
622 CaptureEnumPins *pEnumPins )
623 : p_input( _p_input ), p_filter( _p_filter ), i_ref( 1 )
625 /* Hold a reference count on our filter */
628 /* Are we creating a new enumerator */
630 if( pEnumPins == NULL )
636 i_position = pEnumPins->i_position;
640 CaptureEnumPins::~CaptureEnumPins()
645 /* IUnknown methods */
646 STDMETHODIMP CaptureEnumPins::QueryInterface( REFIID riid, void **ppv )
649 msg_Dbg( p_input, "CaptureEnumPins::QueryInterface" );
652 if( riid == IID_IUnknown ||
653 riid == IID_IEnumPins )
656 *ppv = (IEnumPins *)this;
662 return E_NOINTERFACE;
665 STDMETHODIMP_(ULONG) CaptureEnumPins::AddRef()
668 msg_Dbg( p_input, "CaptureEnumPins::AddRef" );
673 STDMETHODIMP_(ULONG) CaptureEnumPins::Release()
676 msg_Dbg( p_input, "CaptureEnumPins::Release" );
680 if( !i_ref ) delete this;
686 STDMETHODIMP CaptureEnumPins::Next( ULONG cPins, IPin ** ppPins,
690 msg_Dbg( p_input, "CaptureEnumPins::Next" );
695 if( i_position < 1 && cPins > 0 )
697 IPin *pPin = p_filter->CustomGetPin();
704 return *pcFetched == cPins ? NOERROR : S_FALSE;
706 STDMETHODIMP CaptureEnumPins::Skip( ULONG cPins )
709 msg_Dbg( p_input, "CaptureEnumPins::Skip" );
720 STDMETHODIMP CaptureEnumPins::Reset()
723 msg_Dbg( p_input, "CaptureEnumPins::Reset" );
729 STDMETHODIMP CaptureEnumPins::Clone( IEnumPins **ppEnum )
732 msg_Dbg( p_input, "CaptureEnumPins::Clone" );
735 *ppEnum = new CaptureEnumPins( p_input, p_filter, this );
736 if( *ppEnum == NULL ) return E_OUTOFMEMORY;
741 /****************************************************************************
742 * Implementation of our dummy directshow enummediatypes class
743 ****************************************************************************/
745 CaptureEnumMediaTypes::CaptureEnumMediaTypes( input_thread_t * _p_input,
747 CaptureEnumMediaTypes *pEnumMediaTypes )
748 : p_input( _p_input ), p_pin( _p_pin ), i_ref( 1 )
750 /* Hold a reference count on our filter */
753 /* Are we creating a new enumerator */
755 if( pEnumMediaTypes == NULL )
761 i_position = pEnumMediaTypes->i_position;
765 CaptureEnumMediaTypes::~CaptureEnumMediaTypes()
770 /* IUnknown methods */
771 STDMETHODIMP CaptureEnumMediaTypes::QueryInterface( REFIID riid, void **ppv )
774 msg_Dbg( p_input, "CaptureEnumMediaTypes::QueryInterface" );
777 if( riid == IID_IUnknown ||
778 riid == IID_IEnumMediaTypes )
781 *ppv = (IEnumMediaTypes *)this;
787 return E_NOINTERFACE;
790 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::AddRef()
793 msg_Dbg( p_input, "CaptureEnumMediaTypes::AddRef" );
798 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::Release()
801 msg_Dbg( p_input, "CaptureEnumMediaTypes::Release" );
805 if( !i_ref ) delete this;
810 /* IEnumMediaTypes */
811 STDMETHODIMP CaptureEnumMediaTypes::Next( ULONG cMediaTypes,
812 AM_MEDIA_TYPE ** ppMediaTypes,
816 msg_Dbg( p_input, "CaptureEnumMediaTypes::Next" );
822 if( i_position < 1 && cMediaTypes > 0 )
824 IPin *pPin = p_pin->CustomGetPin();
825 *ppMediaTypes = pPin;
835 STDMETHODIMP CaptureEnumMediaTypes::Skip( ULONG cMediaTypes )
838 msg_Dbg( p_input, "CaptureEnumMediaTypes::Skip" );
841 if( cMediaTypes > 0 )
846 i_position += cMediaTypes;
849 STDMETHODIMP CaptureEnumMediaTypes::Reset()
852 msg_Dbg( p_input, "CaptureEnumMediaTypes::Reset" );
858 STDMETHODIMP CaptureEnumMediaTypes::Clone( IEnumMediaTypes **ppEnum )
861 msg_Dbg( p_input, "CaptureEnumMediaTypes::Clone" );
864 *ppEnum = new CaptureEnumMediaTypes( p_input, p_pin, this );
865 if( *ppEnum == NULL ) return E_OUTOFMEMORY;