]> git.sesse.net Git - vlc/blob - modules/access/dshow/filter.cpp
* modules/access/dshow/*: fixes and optimizations. Also changed all the parameters...
[vlc] / modules / access / dshow / filter.cpp
1 /*****************************************************************************
2  * filter.c : DirectShow access module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: filter.cpp,v 1.6 2003/10/18 20:09:23 gbazin Exp $
6  *
7  * Author: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/vout.h>
34
35 #include "filter.h"
36
37 #define DEBUG_DSHOW 1
38
39 struct access_sys_t
40 {
41     vlc_mutex_t lock;
42     vlc_cond_t  wait;
43 };
44
45 /*****************************************************************************
46  * DirectShow GUIDs.
47  * Easier to define them hear as mingw doesn't provide them all.
48  *****************************************************************************/
49 const GUID CLSID_SystemDeviceEnum = {0x62be5d10, 0x60eb, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
50 const GUID CLSID_VideoInputDeviceCategory = {0x860BB310,0x5D01,0x11d0,{0xBD,0x3B,0x00,0xA0,0xC9,0x11,0xCE,0x86}};
51 const GUID CLSID_AudioInputDeviceCategory = {0x33d9a762, 0x90c8, 0x11d0, {0xbd, 0x43, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
52 const GUID IID_IPropertyBag = {0x55272A00, 0x42CB, 0x11CE, {0x81, 0x35, 0x00, 0xAA, 0x00, 0x4B, 0xB8, 0x51}};
53 const GUID IID_ICreateDevEnum = {0x29840822, 0x5b84, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
54 const GUID IID_IFilterGraph = {0x56a8689f, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
55 const GUID IID_IMediaControl = {0x56a868b1, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
56 const GUID CLSID_FilterGraph = {0xe436ebb3, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
57
58 const GUID IID_IUnknown = {0x00000000, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
59 const GUID IID_IPersist = {0x0000010c, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
60 const GUID IID_IMediaFilter = {0x56a86899, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
61 const GUID IID_IBaseFilter = {0x56a86895, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
62 const GUID IID_IPin = {0x56a86891, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
63 const GUID IID_IMemInputPin = {0x56a8689d, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
64
65 const GUID IID_IEnumPins = {0x56a86892, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
66 const GUID IID_IEnumMediaTypes = {0x89c31040, 0x846b, 0x11ce, {0x97,0xd3, 0x00,0xaa,0x00,0x55,0x59,0x5a}};
67
68 const GUID IID_IAMBufferNegotiation = {0x56ed71a0, 0xaf5f, 0x11d0, {0xb3, 0xf0, 0x00, 0xaa, 0x00, 0x37, 0x61, 0xc5}};
69
70 /*
71  * MEDIATYPEs and MEDIASUBTYPEs
72  */
73 const GUID MEDIATYPE_Video = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
74
75 /* Packed RGB formats */
76 const GUID MEDIASUBTYPE_RGB1 = {0xe436eb78, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
77 const GUID MEDIASUBTYPE_RGB4 = {0xe436eb79, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
78 const GUID MEDIASUBTYPE_RGB8 = {0xe436eb7a, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
79 const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
80 const GUID MEDIASUBTYPE_RGB555 = {0xe436eb7c, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
81 const GUID MEDIASUBTYPE_RGB24 = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
82 const GUID MEDIASUBTYPE_RGB32 = {0xe436eb7e, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
83 const GUID MEDIASUBTYPE_ARGB32 = {0x773c9ac0, 0x3274, 0x11d0, {0xb7, 0x24, 0x0, 0xaa, 0x0, 0x6c, 0x1a, 0x1}};
84
85 /* Packed YUV formats */
86 const GUID MEDIASUBTYPE_YUYV = {0x56595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
87 const GUID MEDIASUBTYPE_Y411 = {0x31313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
88 const GUID MEDIASUBTYPE_Y211 = {0x31313259, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
89 const GUID MEDIASUBTYPE_YUY2 = {0x32595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
90 const GUID MEDIASUBTYPE_YVYU = {0x55595659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
91 const GUID MEDIASUBTYPE_UYVY = {0x59565955, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
92
93 /* Planar YUV formats */
94 const GUID MEDIASUBTYPE_YVU9 = {0x39555659, 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}};
96 const GUID MEDIASUBTYPE_IYUV = {0x56555949, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}}; /* identical to YV12 */
97 const GUID MEDIASUBTYPE_Y41P = {0x50313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
98 const GUID MEDIASUBTYPE_I420 = {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
99
100 const GUID MEDIATYPE_Audio = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
101 const GUID FORMAT_WaveFormatEx = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
102 const GUID MEDIASUBTYPE_PCM = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
103 const GUID MEDIASUBTYPE_IEEE_FLOAT = {0x00000003, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
104
105 const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
106
107 void WINAPI FreeMediaType( AM_MEDIA_TYPE& mt )
108 {
109     if( mt.cbFormat != 0 )
110     {
111         CoTaskMemFree( (PVOID)mt.pbFormat );
112         mt.cbFormat = 0;
113         mt.pbFormat = NULL;
114     }
115     if( mt.pUnk != NULL )
116     {
117         mt.pUnk->Release();
118         mt.pUnk = NULL;
119     }
120 }
121
122 HRESULT WINAPI CopyMediaType( AM_MEDIA_TYPE *pmtTarget,
123                               const AM_MEDIA_TYPE *pmtSource )
124 {
125     *pmtTarget = *pmtSource;
126     if( pmtSource->cbFormat != 0 )
127     {
128         pmtTarget->pbFormat = (PBYTE)CoTaskMemAlloc( pmtSource->cbFormat );
129         if( pmtTarget->pbFormat == NULL )
130         {
131             pmtTarget->cbFormat = 0;
132             return E_OUTOFMEMORY;
133         }
134         else
135         {
136             CopyMemory( (PVOID)pmtTarget->pbFormat, (PVOID)pmtSource->pbFormat,
137                         pmtTarget->cbFormat );
138         }
139     }
140     if( pmtTarget->pUnk != NULL )
141     {
142         pmtTarget->pUnk->AddRef();
143     }
144
145     return S_OK;
146 }
147
148 /****************************************************************************
149  * Implementation of our dummy directshow filter pin class
150  ****************************************************************************/
151
152 CapturePin::CapturePin( input_thread_t * _p_input, CaptureFilter *_p_filter,
153                         AM_MEDIA_TYPE mt )
154   : p_input( _p_input ), p_filter( _p_filter ), p_connected_pin( NULL ),
155     media_type( mt ), i_ref( 1 )
156 {
157 }
158
159 CapturePin::~CapturePin()
160 {
161 }
162
163 HRESULT CapturePin::CustomGetSample( VLCMediaSample *vlc_sample )
164 {
165     if( samples_queue.size() )
166     {
167         *vlc_sample = samples_queue.back();
168         samples_queue.pop_back();
169         return S_OK;
170     }
171     return S_FALSE;
172 }
173
174 AM_MEDIA_TYPE CapturePin::CustomGetMediaType()
175 {
176     return media_type;
177 }
178
179 /* IUnknown methods */
180 STDMETHODIMP CapturePin::QueryInterface(REFIID riid, void **ppv)
181 {
182 #ifdef DEBUG_DSHOW
183     msg_Dbg( p_input, "CapturePin::QueryInterface" );
184 #endif
185
186     if( riid == IID_IUnknown ||
187         riid == IID_IPin )
188     {
189         AddRef();
190         *ppv = (IPin *)this;
191         return NOERROR;
192     }
193     if( riid == IID_IMemInputPin )
194     {
195         AddRef();
196         *ppv = (IMemInputPin *)this;
197         return NOERROR;
198     }
199     else
200     {
201         *ppv = NULL;
202         return E_NOINTERFACE;
203     }
204 }
205 STDMETHODIMP_(ULONG) CapturePin::AddRef()
206 {
207 #ifdef DEBUG_DSHOW
208     msg_Dbg( p_input, "CapturePin::AddRef (ref: %i)", i_ref );
209 #endif
210
211     return i_ref++;
212 };
213 STDMETHODIMP_(ULONG) CapturePin::Release()
214 {
215 #ifdef DEBUG_DSHOW
216     msg_Dbg( p_input, "CapturePin::Release (ref: %i)", i_ref );
217 #endif
218
219     if( !InterlockedDecrement(&i_ref) ) delete this;
220
221     return 0;
222 };
223
224 /* IPin methods */
225 STDMETHODIMP CapturePin::Connect( IPin * pReceivePin,
226                                   const AM_MEDIA_TYPE *pmt )
227 {
228 #ifdef DEBUG_DSHOW
229     msg_Dbg( p_input, "CapturePin::Connect" );
230 #endif
231     return E_NOTIMPL;
232 }
233 STDMETHODIMP CapturePin::ReceiveConnection( IPin * pConnector,
234                                             const AM_MEDIA_TYPE *pmt )
235 {
236 #ifdef DEBUG_DSHOW
237     msg_Dbg( p_input, "CapturePin::ReceiveConnection" );
238 #endif
239
240     if( pmt->majortype == MEDIATYPE_Video )
241     {
242         if( media_type.subtype != GUID_NULL &&
243             media_type.subtype != pmt->subtype )
244             return VFW_E_TYPE_NOT_ACCEPTED;
245
246         if( media_type.pbFormat &&
247             ((VIDEOINFOHEADER *)media_type.pbFormat)->bmiHeader.biHeight &&
248             ((VIDEOINFOHEADER *)media_type.pbFormat)->bmiHeader.biHeight !=
249             ((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight )
250             return VFW_E_TYPE_NOT_ACCEPTED;
251
252         if( media_type.pbFormat &&
253             ((VIDEOINFOHEADER *)media_type.pbFormat)->bmiHeader.biWidth &&
254             ((VIDEOINFOHEADER *)media_type.pbFormat)->bmiHeader.biWidth !=
255             ((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth )
256             return VFW_E_TYPE_NOT_ACCEPTED;
257     }
258
259     p_connected_pin = pConnector;
260     p_connected_pin->AddRef();
261
262     FreeMediaType( media_type );
263     return CopyMediaType( &media_type, pmt );
264 }
265 STDMETHODIMP CapturePin::Disconnect()
266 {
267 #ifdef DEBUG_DSHOW
268     msg_Dbg( p_input, "CapturePin::Disconnect" );
269 #endif
270
271     p_connected_pin->Release();
272     p_connected_pin = NULL;
273     FreeMediaType( media_type );
274     return S_OK;
275 }
276 STDMETHODIMP CapturePin::ConnectedTo( IPin **pPin )
277 {
278 #ifdef DEBUG_DSHOW
279     msg_Dbg( p_input, "CapturePin::ConnectedTo" );
280 #endif
281
282     if( !p_connected_pin ) return VFW_E_NOT_CONNECTED;
283
284     p_connected_pin->AddRef();
285     *pPin = p_connected_pin;
286
287     return S_OK;
288 }
289 STDMETHODIMP CapturePin::ConnectionMediaType( AM_MEDIA_TYPE *pmt )
290 {
291 #ifdef DEBUG_DSHOW
292     msg_Dbg( p_input, "CapturePin::ConnectionMediaType" );
293 #endif
294
295     return CopyMediaType( pmt, &media_type );
296 }
297 STDMETHODIMP CapturePin::QueryPinInfo( PIN_INFO * pInfo )
298 {
299 #ifdef DEBUG_DSHOW
300     msg_Dbg( p_input, "CapturePin::QueryPinInfo" );
301 #endif
302
303     pInfo->pFilter = p_filter;
304     if( p_filter ) p_filter->AddRef();
305
306     pInfo->achName[0] = L'\0';
307
308     pInfo->dir = PINDIR_INPUT;
309
310     return NOERROR;
311 }
312 STDMETHODIMP CapturePin::QueryDirection( PIN_DIRECTION * pPinDir )
313 {
314 #ifdef DEBUG_DSHOW
315     msg_Dbg( p_input, "CapturePin::QueryDirection" );
316 #endif
317
318     *pPinDir = PINDIR_INPUT;
319     return NOERROR;
320 }
321 STDMETHODIMP CapturePin::QueryId( LPWSTR * Id )
322 {
323 #ifdef DEBUG_DSHOW
324     msg_Dbg( p_input, "CapturePin::QueryId" );
325 #endif
326     return E_NOTIMPL;
327 }
328 STDMETHODIMP CapturePin::QueryAccept( const AM_MEDIA_TYPE *pmt )
329 {
330 #ifdef DEBUG_DSHOW
331     msg_Dbg( p_input, "CapturePin::QueryAccept" );
332 #endif
333     return E_NOTIMPL;
334 }
335 STDMETHODIMP CapturePin::EnumMediaTypes( IEnumMediaTypes **ppEnum )
336 {
337 #ifdef DEBUG_DSHOW
338     msg_Dbg( p_input, "CapturePin::EnumMediaTypes" );
339 #endif
340
341     *ppEnum = new CaptureEnumMediaTypes( p_input, this, NULL );
342
343     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
344
345     return NOERROR;
346 }
347 STDMETHODIMP CapturePin::QueryInternalConnections( IPin* *apPin, ULONG *nPin )
348 {
349 #ifdef DEBUG_DSHOW
350     msg_Dbg( p_input, "CapturePin::QueryInternalConnections" );
351 #endif
352     return E_NOTIMPL;
353 }
354 STDMETHODIMP CapturePin::EndOfStream( void )
355 {
356 #ifdef DEBUG_DSHOW
357     msg_Dbg( p_input, "CapturePin::EndOfStream" );
358 #endif
359     return E_NOTIMPL;
360 }
361 STDMETHODIMP CapturePin::BeginFlush( void )
362 {
363 #ifdef DEBUG_DSHOW
364     msg_Dbg( p_input, "CapturePin::BeginFlush" );
365 #endif
366     return E_NOTIMPL;
367 }
368 STDMETHODIMP CapturePin::EndFlush( void )
369 {
370 #ifdef DEBUG_DSHOW
371     msg_Dbg( p_input, "CapturePin::EndFlush" );
372 #endif
373     return E_NOTIMPL;
374 }
375 STDMETHODIMP CapturePin::NewSegment( REFERENCE_TIME tStart,
376                                      REFERENCE_TIME tStop,
377                                      double dRate )
378 {
379 #ifdef DEBUG_DSHOW
380     msg_Dbg( p_input, "CapturePin::NewSegment" );
381 #endif
382     return E_NOTIMPL;
383 }
384
385 /* IMemInputPin methods */
386 STDMETHODIMP CapturePin::GetAllocator( IMemAllocator **ppAllocator )
387 {
388 #ifdef DEBUG_DSHOW
389     msg_Dbg( p_input, "CapturePin::GetAllocator" );
390 #endif
391
392     return VFW_E_NO_ALLOCATOR;
393 }
394 STDMETHODIMP CapturePin::NotifyAllocator( IMemAllocator *pAllocator,
395                                           BOOL bReadOnly )
396 {
397 #ifdef DEBUG_DSHOW
398     msg_Dbg( p_input, "CapturePin::NotifyAllocator" );
399 #endif
400
401     return S_OK;
402 }
403 STDMETHODIMP CapturePin::GetAllocatorRequirements( ALLOCATOR_PROPERTIES *pProps )
404 {
405 #ifdef DEBUG_DSHOW
406     msg_Dbg( p_input, "CapturePin::GetAllocatorRequirements" );
407 #endif
408
409     return E_NOTIMPL;
410 }
411 STDMETHODIMP CapturePin::Receive( IMediaSample *pSample )
412 {
413 #ifdef DEBUG_DSHOW
414     //msg_Dbg( p_input, "CapturePin::Receive" );
415 #endif
416
417     pSample->AddRef();
418     mtime_t i_timestamp = mdate() * 10;
419     VLCMediaSample vlc_sample = {pSample, i_timestamp};
420
421     access_sys_t *p_sys = p_input->p_access_data;
422     vlc_mutex_lock( &p_sys->lock );
423     samples_queue.push_front( vlc_sample );
424
425     /* Make sure we don't cache too many samples */
426     if( samples_queue.size() > 10 )
427     {
428         vlc_sample = samples_queue.back();
429         samples_queue.pop_back();
430         msg_Dbg( p_input, "CapturePin::Receive trashing late input sample" );
431         vlc_sample.p_sample->Release();
432     }
433
434     vlc_cond_signal( &p_sys->wait );
435     vlc_mutex_unlock( &p_sys->lock );
436
437     return S_OK;
438 }
439 STDMETHODIMP CapturePin::ReceiveMultiple( IMediaSample **pSamples,
440                                           long nSamples,
441                                           long *nSamplesProcessed )
442 {
443 #ifdef DEBUG_DSHOW
444     msg_Dbg( p_input, "CapturePin::ReceiveMultiple" );
445 #endif
446
447     HRESULT hr = S_OK;
448
449     *nSamplesProcessed = 0;
450     while( nSamples-- > 0 )
451     {
452          hr = Receive( pSamples[*nSamplesProcessed] );
453          if( hr != S_OK ) break;
454          (*nSamplesProcessed)++;
455     }
456     return hr;
457 }
458 STDMETHODIMP CapturePin::ReceiveCanBlock( void )
459 {
460 #ifdef DEBUG_DSHOW
461     msg_Dbg( p_input, "CapturePin::ReceiveCanBlock" );
462 #endif
463
464     return S_FALSE; /* Thou shalt not block */
465 }
466
467 /****************************************************************************
468  * Implementation of our dummy directshow filter class
469  ****************************************************************************/
470
471 CaptureFilter::CaptureFilter( input_thread_t * _p_input, AM_MEDIA_TYPE mt )
472   : p_input( _p_input ), p_pin( new CapturePin( _p_input, this, mt ) ),
473     media_type( mt ), i_ref( 1 )
474 {
475 }
476
477 CaptureFilter::~CaptureFilter()
478 {
479     p_pin->Release();
480 }
481
482 /* IUnknown methods */
483 STDMETHODIMP CaptureFilter::QueryInterface( REFIID riid, void **ppv )
484 {
485 #ifdef DEBUG_DSHOW
486     msg_Dbg( p_input, "CaptureFilter::QueryInterface" );
487 #endif
488
489     if( riid == IID_IUnknown )
490     {
491         AddRef();
492         *ppv = (IUnknown *)this;
493         return NOERROR;
494     }
495     if( riid == IID_IPersist )
496     {
497         AddRef();
498         *ppv = (IPersist *)this;
499         return NOERROR;
500     }
501     if( riid == IID_IMediaFilter )
502     {
503         AddRef();
504         *ppv = (IMediaFilter *)this;
505         return NOERROR;
506     }
507     if( riid == IID_IBaseFilter )
508     {
509         AddRef();
510         *ppv = (IBaseFilter *)this;
511         return NOERROR;
512     }
513     else
514     {
515         *ppv = NULL;
516         return E_NOINTERFACE;
517     }
518 };
519 STDMETHODIMP_(ULONG) CaptureFilter::AddRef()
520 {
521 #ifdef DEBUG_DSHOW
522     msg_Dbg( p_input, "CaptureFilter::AddRef (ref: %i)", i_ref );
523 #endif
524
525     return i_ref++;
526 };
527 STDMETHODIMP_(ULONG) CaptureFilter::Release()
528 {
529 #ifdef DEBUG_DSHOW
530     msg_Dbg( p_input, "CaptureFilter::Release (ref: %i)", i_ref );
531 #endif
532
533     if( !InterlockedDecrement(&i_ref) ) delete this;
534
535     return 0;
536 };
537
538 /* IPersist method */
539 STDMETHODIMP CaptureFilter::GetClassID(CLSID *pClsID)
540 {
541 #ifdef DEBUG_DSHOW
542     msg_Dbg( p_input, "CaptureFilter::GetClassID" );
543 #endif
544     return E_NOTIMPL;
545 };
546
547 /* IMediaFilter methods */
548 STDMETHODIMP CaptureFilter::GetState(DWORD dwMSecs, FILTER_STATE *State)
549 {
550 #ifdef DEBUG_DSHOW
551     msg_Dbg( p_input, "CaptureFilter::GetStat" );
552 #endif
553     return E_NOTIMPL;
554 };
555 STDMETHODIMP CaptureFilter::SetSyncSource(IReferenceClock *pClock)
556 {
557 #ifdef DEBUG_DSHOW
558     msg_Dbg( p_input, "CaptureFilter::SetSyncSource" );
559 #endif
560
561     return NOERROR;
562 };
563 STDMETHODIMP CaptureFilter::GetSyncSource(IReferenceClock **pClock)
564 {
565 #ifdef DEBUG_DSHOW
566     msg_Dbg( p_input, "CaptureFilter::GetSyncSource" );
567 #endif
568     return E_NOTIMPL;
569 };
570 STDMETHODIMP CaptureFilter::Stop()
571 {
572 #ifdef DEBUG_DSHOW
573     msg_Dbg( p_input, "CaptureFilter::Stop" );
574 #endif
575     return E_NOTIMPL;
576 };
577 STDMETHODIMP CaptureFilter::Pause()
578 {
579 #ifdef DEBUG_DSHOW
580     msg_Dbg( p_input, "CaptureFilter::Pause" );
581 #endif
582     return E_NOTIMPL;
583 };
584 STDMETHODIMP CaptureFilter::Run(REFERENCE_TIME tStart)
585 {
586 #ifdef DEBUG_DSHOW
587     msg_Dbg( p_input, "CaptureFilter::Run" );
588 #endif
589     return E_NOTIMPL;
590 };
591
592 /* IBaseFilter methods */
593 STDMETHODIMP CaptureFilter::EnumPins( IEnumPins ** ppEnum )
594 {
595 #ifdef DEBUG_DSHOW
596     msg_Dbg( p_input, "CaptureFilter::EnumPins" );
597 #endif
598
599     /* Create a new ref counted enumerator */
600     *ppEnum = new CaptureEnumPins( p_input, this, NULL );
601     return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR;
602 };
603 STDMETHODIMP CaptureFilter::FindPin( LPCWSTR Id, IPin ** ppPin )
604 {
605 #ifdef DEBUG_DSHOW
606     msg_Dbg( p_input, "CaptureFilter::FindPin" );
607 #endif
608     return E_NOTIMPL;
609 };
610 STDMETHODIMP CaptureFilter::QueryFilterInfo( FILTER_INFO * pInfo )
611 {
612 #ifdef DEBUG_DSHOW
613     msg_Dbg( p_input, "CaptureFilter::QueryFilterInfo" );
614 #endif
615
616     pInfo->achName[0] = L'\0';
617
618     pInfo->pGraph = p_graph;
619     if( p_graph ) p_graph->AddRef();
620
621     return NOERROR;
622 };
623 STDMETHODIMP CaptureFilter::JoinFilterGraph( IFilterGraph * pGraph,
624                                              LPCWSTR pName )
625 {
626 #ifdef DEBUG_DSHOW
627     msg_Dbg( p_input, "CaptureFilter::JoinFilterGraph" );
628 #endif
629
630     p_graph = pGraph;
631
632     return NOERROR;
633 };
634 STDMETHODIMP CaptureFilter::QueryVendorInfo( LPWSTR* pVendorInfo )
635 {
636 #ifdef DEBUG_DSHOW
637     msg_Dbg( p_input, "CaptureFilter::QueryVendorInfo" );
638 #endif
639     return E_NOTIMPL;
640 };
641
642 /* Custom methods */
643 CapturePin *CaptureFilter::CustomGetPin()
644 {
645     return p_pin;
646 }
647
648 /****************************************************************************
649  * Implementation of our dummy directshow enumpins class
650  ****************************************************************************/
651
652 CaptureEnumPins::CaptureEnumPins( input_thread_t * _p_input,
653                                   CaptureFilter *_p_filter,
654                                   CaptureEnumPins *pEnumPins )
655   : p_input( _p_input ), p_filter( _p_filter ), i_ref( 1 )
656 {
657     /* Hold a reference count on our filter */
658     p_filter->AddRef();
659
660     /* Are we creating a new enumerator */
661
662     if( pEnumPins == NULL )
663     {
664         i_position = 0;
665     }
666     else
667     {
668         i_position = pEnumPins->i_position;
669     }
670 }
671
672 CaptureEnumPins::~CaptureEnumPins()
673 {
674     p_filter->Release();
675 }
676
677 /* IUnknown methods */
678 STDMETHODIMP CaptureEnumPins::QueryInterface( REFIID riid, void **ppv )
679 {
680 #ifdef DEBUG_DSHOW
681     msg_Dbg( p_input, "CaptureEnumPins::QueryInterface" );
682 #endif
683
684     if( riid == IID_IUnknown ||
685         riid == IID_IEnumPins )
686     {
687         AddRef();
688         *ppv = (IEnumPins *)this;
689         return NOERROR;
690     }
691     else
692     {
693         *ppv = NULL;
694         return E_NOINTERFACE;
695     }
696 };
697 STDMETHODIMP_(ULONG) CaptureEnumPins::AddRef()
698 {
699 #ifdef DEBUG_DSHOW
700     msg_Dbg( p_input, "CaptureEnumPins::AddRef (ref: %i)", i_ref );
701 #endif
702
703     return i_ref++;
704 };
705 STDMETHODIMP_(ULONG) CaptureEnumPins::Release()
706 {
707 #ifdef DEBUG_DSHOW
708     msg_Dbg( p_input, "CaptureEnumPins::Release (ref: %i)", i_ref );
709 #endif
710
711     if( !InterlockedDecrement(&i_ref) ) delete this;
712
713     return 0;
714 };
715
716 /* IEnumPins */
717 STDMETHODIMP CaptureEnumPins::Next( ULONG cPins, IPin ** ppPins,
718                                     ULONG * pcFetched )
719 {
720 #ifdef DEBUG_DSHOW
721     msg_Dbg( p_input, "CaptureEnumPins::Next" );
722 #endif
723
724     *pcFetched = 0;
725
726     if( i_position < 1 && cPins > 0 )
727     {
728         IPin *pPin = p_filter->CustomGetPin();
729         *ppPins = pPin;
730         pPin->AddRef();
731         *pcFetched = 1;
732         i_position++;
733     }
734
735     return *pcFetched == cPins ? NOERROR : S_FALSE;
736 };
737 STDMETHODIMP CaptureEnumPins::Skip( ULONG cPins )
738 {
739 #ifdef DEBUG_DSHOW
740     msg_Dbg( p_input, "CaptureEnumPins::Skip" );
741 #endif
742
743     if( cPins > 1 )
744     {
745         return S_FALSE;
746     }
747
748     i_position += cPins;
749     return NOERROR;
750 };
751 STDMETHODIMP CaptureEnumPins::Reset()
752 {
753 #ifdef DEBUG_DSHOW
754     msg_Dbg( p_input, "CaptureEnumPins::Reset" );
755 #endif
756
757     i_position = 0;
758     return S_OK;
759 };
760 STDMETHODIMP CaptureEnumPins::Clone( IEnumPins **ppEnum )
761 {
762 #ifdef DEBUG_DSHOW
763     msg_Dbg( p_input, "CaptureEnumPins::Clone" );
764 #endif
765
766     *ppEnum = new CaptureEnumPins( p_input, p_filter, this );
767     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
768
769     return NOERROR;
770 };
771
772 /****************************************************************************
773  * Implementation of our dummy directshow enummediatypes class
774  ****************************************************************************/
775
776 CaptureEnumMediaTypes::CaptureEnumMediaTypes( input_thread_t * _p_input,
777                                   CapturePin *_p_pin,
778                                   CaptureEnumMediaTypes *pEnumMediaTypes )
779   : p_input( _p_input ), p_pin( _p_pin ), i_ref( 1 )
780 {
781     /* Hold a reference count on our filter */
782     p_pin->AddRef();
783
784     /* Are we creating a new enumerator */
785     if( pEnumMediaTypes == NULL )
786     {
787         i_position = 0;
788     }
789     else
790     {
791         i_position = pEnumMediaTypes->i_position;
792     }
793 }
794
795 CaptureEnumMediaTypes::~CaptureEnumMediaTypes()
796 {
797     p_pin->Release();
798 }
799
800 /* IUnknown methods */
801 STDMETHODIMP CaptureEnumMediaTypes::QueryInterface( REFIID riid, void **ppv )
802 {
803 #ifdef DEBUG_DSHOW
804     msg_Dbg( p_input, "CaptureEnumMediaTypes::QueryInterface" );
805 #endif
806
807     if( riid == IID_IUnknown ||
808         riid == IID_IEnumMediaTypes )
809     {
810         AddRef();
811         *ppv = (IEnumMediaTypes *)this;
812         return NOERROR;
813     }
814     else
815     {
816         *ppv = NULL;
817         return E_NOINTERFACE;
818     }
819 };
820 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::AddRef()
821 {
822 #ifdef DEBUG_DSHOW
823     msg_Dbg( p_input, "CaptureEnumMediaTypes::AddRef (ref: %i)", i_ref );
824 #endif
825
826     return i_ref++;
827 };
828 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::Release()
829 {
830 #ifdef DEBUG_DSHOW
831     msg_Dbg( p_input, "CaptureEnumMediaTypes::Release (ref: %i)", i_ref );
832 #endif
833
834     if( !InterlockedDecrement(&i_ref) ) delete this;
835
836     return 0;
837 };
838
839 /* IEnumMediaTypes */
840 STDMETHODIMP CaptureEnumMediaTypes::Next( ULONG cMediaTypes,
841                                           AM_MEDIA_TYPE ** ppMediaTypes,
842                                           ULONG * pcFetched )
843 {
844 #ifdef DEBUG_DSHOW
845     msg_Dbg( p_input, "CaptureEnumMediaTypes::Next" );
846 #endif
847
848     if( pcFetched ) *pcFetched = 0;
849     return S_FALSE;
850 };
851 STDMETHODIMP CaptureEnumMediaTypes::Skip( ULONG cMediaTypes )
852 {
853 #ifdef DEBUG_DSHOW
854     msg_Dbg( p_input, "CaptureEnumMediaTypes::Skip" );
855 #endif
856
857     return S_FALSE;
858 };
859 STDMETHODIMP CaptureEnumMediaTypes::Reset()
860 {
861 #ifdef DEBUG_DSHOW
862     msg_Dbg( p_input, "CaptureEnumMediaTypes::Reset" );
863 #endif
864
865     i_position = 0;
866     return S_OK;
867 };
868 STDMETHODIMP CaptureEnumMediaTypes::Clone( IEnumMediaTypes **ppEnum )
869 {
870 #ifdef DEBUG_DSHOW
871     msg_Dbg( p_input, "CaptureEnumMediaTypes::Clone" );
872 #endif
873
874     *ppEnum = new CaptureEnumMediaTypes( p_input, p_pin, this );
875     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
876
877     return NOERROR;
878 };