]> git.sesse.net Git - vlc/blob - modules/access/dshow/filter.cpp
DShow: kill a warning
[vlc] / modules / access / dshow / filter.cpp
1 /*****************************************************************************
2  * filter.c : DirectShow access module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2010 the VideoLAN team
5  * $Id$
6  *
7  * Author: Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_fourcc.h>
34
35 #ifndef _MSC_VER
36     /* Work-around a bug in w32api-2.5 */
37 #   define QACONTAINERFLAGS QACONTAINERFLAGS_SOMETHINGELSE
38 #endif
39
40 #include "common.h"
41 #include "filter.h"
42
43 #define DEBUG_DSHOW 1
44
45 #define FILTER_NAME  L"VideoLAN Capture Filter"
46 #define PIN_NAME     L"Capture"
47
48 /*****************************************************************************
49  * DirectShow GUIDs.
50  * Easier to define them here as mingw doesn't provide them all.
51  *****************************************************************************/
52 const GUID CLSID_SystemDeviceEnum         = {0x62be5d10, 0x60eb, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
53 const GUID CLSID_VideoInputDeviceCategory = {0x860BB310, 0x5D01, 0x11d0, {0xBD, 0x3B, 0x00, 0xA0, 0xC9, 0x11, 0xCE, 0x86}};
54 const GUID CLSID_AudioInputDeviceCategory = {0x33d9a762, 0x90c8, 0x11d0, {0xbd, 0x43, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
55 //const GUID IID_IPropertyBag = {0x55272A00, 0x42CB, 0x11CE, {0x81, 0x35, 0x00, 0xAA, 0x00, 0x4B, 0xB8, 0x51}};
56 extern const GUID IID_IPropertyBag;
57 const GUID IID_ICreateDevEnum = {0x29840822, 0x5b84, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
58 const GUID IID_IFilterGraph   = {0x56a8689f, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
59 const GUID IID_IMediaControl  = {0x56a868b1, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
60 const GUID CLSID_FilterGraph  = {0xe436ebb3, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
61
62 //const GUID IID_IUnknown = {0x00000000, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
63 extern const GUID IID_IUnknown;
64 //const GUID IID_IPersist = {0x0000010c, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
65 extern const GUID IID_IPersist;
66 const GUID IID_IMediaFilter = {0x56a86899, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
67 const GUID IID_IBaseFilter  = {0x56a86895, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
68 const GUID IID_IPin         = {0x56a86891, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
69 const GUID IID_IMemInputPin = {0x56a8689d, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
70 extern const GUID IID_IMemInputPin;
71
72 const GUID IID_IEnumPins    = {0x56a86892, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
73 const GUID IID_IEnumMediaTypes = {0x89c31040, 0x846b, 0x11ce, {0x97,0xd3, 0x00,0xaa,0x00,0x55,0x59,0x5a}};
74
75 //const GUID IID_IAMBufferNegotiation = {0x56ed71a0, 0xaf5f, 0x11d0, {0xb3, 0xf0, 0x00, 0xaa, 0x00, 0x37, 0x61, 0xc5}};
76
77 //const GUID IID_ISpecifyPropertyPages = {0xb196b28b, 0xbab4, 0x101a, {0xb6, 0x9c, 0x00, 0xaa, 0x00, 0x34, 0x1d, 0x07}};
78 extern const GUID IID_ISpecifyPropertyPages;
79
80 const GUID IID_IQualityControl        = {0x56a868a5, 0x0ad4, 0x11ce, {0xb, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
81
82 const GUID CLSID_CaptureGraphBuilder2 = {0xBF87B6E1, 0x8C27, 0x11d0, {0xB3, 0xF0, 0x0, 0xAA, 0x00, 0x37, 0x61, 0xC5}};
83
84 const GUID IID_IGraphBuilder          = {0x56a868a9, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
85
86 const GUID IID_ICaptureGraphBuilder2  = {0x93E5A4E0, 0x2D50, 0x11d2, {0xAB, 0xFA, 0x00, 0xA0, 0xC9, 0xC6, 0xE3, 0x8D}};
87
88 const GUID IID_IKsPropertySet  = {0x31EFAC30, 0x515C, 0x11d0, {0xA9, 0xAA, 0x00, 0xAA, 0x00, 0x61, 0xBE, 0x93}};
89
90 /* Video Format */
91
92 const GUID FORMAT_VideoInfo    = {0x05589f80, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
93 /*
94  * MEDIATYPEs and MEDIASUBTYPEs
95  */
96 const GUID MEDIATYPE_Video            = {0x73646976, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
97 const GUID MEDIATYPE_Interleaved      = {0x73766169, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
98 const GUID MEDIATYPE_Stream           = {0xe436eb83, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
99 const GUID MEDIASUBTYPE_PREVIEW_VIDEO = {0x2859e1da, 0xb81f, 0x4fbd, {0x94, 0x3b, 0xe2, 0x37, 0x24, 0xa1, 0xab, 0xb3}};
100
101 /* Packed RGB formats */
102 const GUID MEDIASUBTYPE_RGB1   = {0xe436eb78, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
103 const GUID MEDIASUBTYPE_RGB4   = {0xe436eb79, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
104 const GUID MEDIASUBTYPE_RGB8   = {0xe436eb7a, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
105 const GUID MEDIASUBTYPE_RGB565 = {0xe436eb7b, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
106 const GUID MEDIASUBTYPE_RGB555 = {0xe436eb7c, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
107 const GUID MEDIASUBTYPE_RGB24  = {0xe436eb7d, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
108 const GUID MEDIASUBTYPE_RGB32  = {0xe436eb7e, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
109 const GUID MEDIASUBTYPE_ARGB32 = {0x773c9ac0, 0x3274, 0x11d0, {0xb7, 0x24, 0x0, 0xaa, 0x0, 0x6c, 0x1a, 0x1}};
110
111 /* Packed YUV formats */
112 const GUID MEDIASUBTYPE_YUYV = {0x56595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
113 const GUID MEDIASUBTYPE_Y411 = {0x31313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
114 const GUID MEDIASUBTYPE_Y211 = {0x31313259, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
115 const GUID MEDIASUBTYPE_YUY2 = {0x32595559, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
116 const GUID MEDIASUBTYPE_YVYU = {0x55595659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
117 const GUID MEDIASUBTYPE_UYVY = {0x59565955, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
118 const GUID MEDIASUBTYPE_HDYC = {0x43594448, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
119
120 /* Planar YUV formats */
121 const GUID MEDIASUBTYPE_YVU9 = {0x39555659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
122 const GUID MEDIASUBTYPE_YV12 = {0x32315659, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
123 const GUID MEDIASUBTYPE_IYUV = {0x56555949, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}}; /* identical to YV12 */
124 const GUID MEDIASUBTYPE_Y41P = {0x50313459, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
125 const GUID MEDIASUBTYPE_I420 = {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
126
127 const GUID MEDIATYPE_Audio         = {0x73647561, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
128 const GUID FORMAT_WaveFormatEx     = {0x05589f81, 0xc356, 0x11ce, {0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a}};
129 const GUID MEDIASUBTYPE_PCM        = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
130 const GUID MEDIASUBTYPE_IEEE_FLOAT = {0x00000003, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
131
132 /* DV formats */
133 const GUID MEDIASUBTYPE_dvsd = {0x64737664, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
134 const GUID MEDIASUBTYPE_dvhd = {0x64687664, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
135 const GUID MEDIASUBTYPE_dvsl = {0x6c737664, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
136 /* What about Pro formats, like 'dv25', 'dv50' and 'dvh1' */
137
138 /* MPEG2 formats */
139 const GUID MEDIASUBTYPE_MPEG2_VIDEO     = {0xe06d8026, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};
140 const GUID MEDIASUBTYPE_MPEG2_PROGRAM   = {0xe06d8022, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};
141 const GUID MEDIASUBTYPE_MPEG2_TRANSPORT = {0xe06d8023, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};
142 const GUID FORMAT_MPEG2Video            = {0xe06d80e3, 0xdb46, 0x11cf, {0xb4, 0xd1, 0x00, 0x80, 0x5f, 0x6c, 0xbb, 0xea}};
143
144 /* MJPG format */
145 const GUID MEDIASUBTYPE_MJPG = {0x47504A4D, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
146
147 /* DivX formats */
148 const GUID MEDIASUBTYPE_DIVX = {0x58564944, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
149
150 /* Analog Video */
151 const GUID FORMAT_AnalogVideo = {0x482dde0, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
152
153 const GUID MEDIATYPE_AnalogVideo = {0x482dde1, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xab, 0x0, 0x6e, 0xcb, 0x65}};
154
155 const GUID MEDIASUBTYPE_AnalogVideo_NTSC_M      = {0x482dde2, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
156 const GUID MEDIASUBTYPE_AnalogVideo_PAL_B       = {0x482dde5, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
157 const GUID MEDIASUBTYPE_AnalogVideo_PAL_D       = {0x482dde6, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
158 const GUID MEDIASUBTYPE_AnalogVideo_PAL_G       = {0x482dde7, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
159 const GUID MEDIASUBTYPE_AnalogVideo_PAL_H       = {0x482dde8, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
160 const GUID MEDIASUBTYPE_AnalogVideo_PAL_I       = {0x482dde9, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
161 const GUID MEDIASUBTYPE_AnalogVideo_PAL_M       = {0x482ddea, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
162 const GUID MEDIASUBTYPE_AnalogVideo_PAL_N       = {0x482ddeb, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
163 const GUID MEDIASUBTYPE_AnalogVideo_PAL_N_COMBO = {0x482ddec, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
164 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_B     = {0x482ddf0, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
165 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_D     = {0x482ddf1, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
166 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_G     = {0x482ddf2, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
167 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_H     = {0x482ddf3, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
168 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_K     = {0x482ddf4, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
169 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_K1    = {0x482ddf5, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
170 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_L     = {0x482ddf6, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
171
172 const GUID AMPROPSETID_Pin            = {0x9b00f101, 0x1567, 0x11d1, {0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc5}};
173 const GUID PIN_CATEGORY_ANALOGVIDEOIN = {0xfb6c4283, 0x0353, 0x11d1, {0x90, 0x5f, 0x0, 0x0, 0xc0, 0xcc, 0x16, 0xba}};
174 const GUID PIN_CATEGORY_CAPTURE       = {0xfb6c4281, 0x0353, 0x11d1, {0x90, 0x5f, 0x0, 0x0, 0xc0, 0xcc, 0x16, 0xba}};
175 const GUID LOOK_UPSTREAM_ONLY         = {0xac798be0, 0x98e3, 0x11d1, {0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc}};
176
177 //const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
178 extern const GUID GUID_NULL;
179
180 void WINAPI FreeMediaType( AM_MEDIA_TYPE& mt )
181 {
182     if( mt.cbFormat != 0 )
183     {
184         CoTaskMemFree( (PVOID)mt.pbFormat );
185         mt.cbFormat = 0;
186         mt.pbFormat = NULL;
187     }
188     if( mt.pUnk != NULL )
189     {
190         mt.pUnk->Release();
191         mt.pUnk = NULL;
192     }
193 }
194
195 HRESULT WINAPI CopyMediaType( AM_MEDIA_TYPE *pmtTarget,
196                               const AM_MEDIA_TYPE *pmtSource )
197 {
198     *pmtTarget = *pmtSource;
199
200     if( !pmtSource || !pmtTarget ) return S_FALSE;
201
202     if( pmtSource->cbFormat && pmtSource->pbFormat )
203     {
204         pmtTarget->pbFormat = (PBYTE)CoTaskMemAlloc( pmtSource->cbFormat );
205         if( pmtTarget->pbFormat == NULL )
206         {
207             pmtTarget->cbFormat = 0;
208             return E_OUTOFMEMORY;
209         }
210         else
211         {
212             CopyMemory( (PVOID)pmtTarget->pbFormat, (PVOID)pmtSource->pbFormat,
213                         pmtTarget->cbFormat );
214         }
215     }
216     if( pmtTarget->pUnk != NULL )
217     {
218         pmtTarget->pUnk->AddRef();
219     }
220
221     return S_OK;
222 }
223
224 int GetFourCCFromMediaType( const AM_MEDIA_TYPE &media_type )
225 {
226     int i_fourcc = 0;
227
228     if( media_type.majortype == MEDIATYPE_Video )
229     {
230         /* currently only support this type of video info format */
231         if( 1 /* media_type.formattype == FORMAT_VideoInfo */ )
232         {
233             /* Packed RGB formats */
234             if( media_type.subtype == MEDIASUBTYPE_RGB1 )
235                i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '1' );
236             else if( media_type.subtype == MEDIASUBTYPE_RGB4 )
237                i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '4' );
238             else if( media_type.subtype == MEDIASUBTYPE_RGB8 )
239                i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '8' );
240             else if( media_type.subtype == MEDIASUBTYPE_RGB555 )
241                i_fourcc = VLC_CODEC_RGB15;
242             else if( media_type.subtype == MEDIASUBTYPE_RGB565 )
243                i_fourcc = VLC_CODEC_RGB16;
244             else if( media_type.subtype == MEDIASUBTYPE_RGB24 )
245                i_fourcc = VLC_CODEC_RGB24;
246             else if( media_type.subtype == MEDIASUBTYPE_RGB32 )
247                i_fourcc = VLC_CODEC_RGB32;
248             else if( media_type.subtype == MEDIASUBTYPE_ARGB32 )
249                i_fourcc = VLC_CODEC_RGBA;
250
251             /* Planar YUV formats */
252             else if( media_type.subtype == MEDIASUBTYPE_I420 )
253                i_fourcc = VLC_CODEC_I420;
254             else if( media_type.subtype == MEDIASUBTYPE_Y41P )
255                i_fourcc = VLC_CODEC_I411;
256             else if( media_type.subtype == MEDIASUBTYPE_YV12 )
257                i_fourcc = VLC_CODEC_YV12;
258             else if( media_type.subtype == MEDIASUBTYPE_IYUV )
259                i_fourcc = VLC_CODEC_YV12;
260             else if( media_type.subtype == MEDIASUBTYPE_YVU9 )
261                i_fourcc = VLC_CODEC_I410;
262
263             /* Packed YUV formats */
264             else if( media_type.subtype == MEDIASUBTYPE_YVYU )
265                i_fourcc = VLC_CODEC_YVYU;
266             else if( media_type.subtype == MEDIASUBTYPE_YUYV )
267                i_fourcc = VLC_CODEC_YUYV;
268             else if( media_type.subtype == MEDIASUBTYPE_Y411 )
269                i_fourcc = VLC_FOURCC( 'I', '4', '1', 'N' );
270             else if( media_type.subtype == MEDIASUBTYPE_Y211 )
271                i_fourcc = VLC_CODEC_Y211;
272             else if( media_type.subtype == MEDIASUBTYPE_YUY2 )
273                i_fourcc = VLC_CODEC_YUYV;
274             else if( media_type.subtype == MEDIASUBTYPE_UYVY )
275                i_fourcc = VLC_CODEC_UYVY;
276             /* HDYC uses UYVY sample positions but Rec709 colourimetry */
277             /* FIXME: When VLC understands colourspace, something will need
278              * to be added / changed here. Until then, just make it behave
279              * like UYVY */
280             else if( media_type.subtype == MEDIASUBTYPE_HDYC )
281                 i_fourcc = VLC_CODEC_UYVY;
282
283             /* MPEG2 video elementary stream */
284             else if( media_type.subtype == MEDIASUBTYPE_MPEG2_VIDEO )
285                i_fourcc = VLC_CODEC_MPGV;
286
287             /* DivX video */
288             else if( media_type.subtype == MEDIASUBTYPE_DIVX )
289                i_fourcc = VLC_CODEC_MP4V;
290
291             /* DV formats */
292             else if( media_type.subtype == MEDIASUBTYPE_dvsl )
293                i_fourcc = VLC_CODEC_DV;
294             else if( media_type.subtype == MEDIASUBTYPE_dvsd )
295                i_fourcc = VLC_CODEC_DV;
296             else if( media_type.subtype == MEDIASUBTYPE_dvhd )
297                i_fourcc = VLC_CODEC_DV;
298
299             /* MJPEG format */
300             else if( media_type.subtype == MEDIASUBTYPE_MJPG )
301                 i_fourcc = VLC_CODEC_MJPG;
302
303         }
304     }
305     else if( media_type.majortype == MEDIATYPE_Audio )
306     {
307         /* currently only support this type of audio info format */
308         if( media_type.formattype == FORMAT_WaveFormatEx )
309         {
310             if( media_type.subtype == MEDIASUBTYPE_PCM )
311                 i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
312             else if( media_type.subtype == MEDIASUBTYPE_IEEE_FLOAT )
313                 i_fourcc = VLC_CODEC_FL32;
314         }
315     }
316     else if( media_type.majortype == MEDIATYPE_Stream )
317     {
318         if( media_type.subtype == MEDIASUBTYPE_MPEG2_PROGRAM )
319             i_fourcc = VLC_FOURCC( 'm', 'p', '2', 'p' );
320         else if( media_type.subtype == MEDIASUBTYPE_MPEG2_TRANSPORT )
321             i_fourcc = VLC_FOURCC( 'm', 'p', '2', 't' );
322     }
323
324     return i_fourcc;
325 }
326
327 /****************************************************************************
328  * Implementation of our dummy directshow filter pin class
329  ****************************************************************************/
330
331 CapturePin::CapturePin( vlc_object_t *_p_input, access_sys_t *_p_sys,
332                         CaptureFilter *_p_filter,
333                         AM_MEDIA_TYPE *mt, size_t mt_count )
334   : p_input( _p_input ), p_sys( _p_sys ), p_filter( _p_filter ),
335     p_connected_pin( NULL ),  media_types(mt), media_type_count(mt_count),
336     i_ref( 1 )
337 {
338     cx_media_type.majortype = mt[0].majortype;
339     cx_media_type.subtype   = GUID_NULL;
340     cx_media_type.pbFormat  = NULL;
341     cx_media_type.cbFormat  = 0;
342     cx_media_type.pUnk      = NULL;
343 }
344
345 CapturePin::~CapturePin()
346 {
347 #ifdef DEBUG_DSHOW
348     msg_Dbg( p_input, "CapturePin::~CapturePin" );
349 #endif
350     for( size_t c=0; c<media_type_count; c++ )
351     {
352         FreeMediaType(media_types[c]);
353     }
354     FreeMediaType(cx_media_type);
355 }
356
357 /**
358  * Returns the complete queue of samples that have been received so far.
359  * Lock the p_sys->lock before calling this function.
360  * @param samples_queue [out] Empty queue that will get all elements from
361  * the pin queue.
362  * @return S_OK if a sample was available, S_FALSE if no sample was
363  * available
364  */
365 HRESULT CapturePin::CustomGetSamples( deque<VLCMediaSample> &external_queue )
366 {
367 #if 0 //def DEBUG_DSHOW
368     msg_Dbg( p_input, "CapturePin::CustomGetSamples: %d samples in the queue", samples_queue.size());
369 #endif
370
371     if( !samples_queue.empty() )
372     {
373         external_queue.swap(samples_queue);
374         return S_OK;
375     }
376     return S_FALSE;
377 }
378
379 /**
380  * Returns a sample from its sample queue. Proper locking must be done prior
381  * to this call. Current dshow code protects the access to any sample queue
382  * (audio and video) with the p_sys->lock
383  * @param vlc_sample [out] Address of a sample if sucessfull. Undefined
384  * otherwise.
385  * @return S_OK if a sample was available, S_FALSE if no sample was
386  * available
387  */
388 HRESULT CapturePin::CustomGetSample( VLCMediaSample *vlc_sample )
389 {
390 #if 0 //def DEBUG_DSHOW
391     msg_Dbg( p_input, "CapturePin::CustomGetSample" );
392 #endif
393
394     if( samples_queue.size() )
395     {
396         *vlc_sample = samples_queue.back();
397         samples_queue.pop_back();
398         return S_OK;
399     }
400     return S_FALSE;
401 }
402
403 AM_MEDIA_TYPE &CapturePin::CustomGetMediaType()
404 {
405     return cx_media_type;
406 }
407
408 /* IUnknown methods */
409 STDMETHODIMP CapturePin::QueryInterface(REFIID riid, void **ppv)
410 {
411 #ifdef DEBUG_DSHOW_L1
412     msg_Dbg( p_input, "CapturePin::QueryInterface" );
413 #endif
414
415     if( riid == IID_IUnknown ||
416         riid == IID_IPin )
417     {
418         AddRef();
419         *ppv = (IPin *)this;
420         return NOERROR;
421     }
422     if( riid == IID_IMemInputPin )
423     {
424         AddRef();
425         *ppv = (IMemInputPin *)this;
426         return NOERROR;
427     }
428     else
429     {
430 #ifdef DEBUG_DSHOW_L1
431         msg_Dbg( p_input, "CapturePin::QueryInterface() failed for: "
432                  "%04X-%02X-%02X-%02X%02X%02X%02X%02X%02X%02X%02X",
433                  (int)riid.Data1, (int)riid.Data2, (int)riid.Data3,
434                  static_cast<int>(riid.Data4[0]), (int)riid.Data4[1],
435                  (int)riid.Data4[2], (int)riid.Data4[3],
436                  (int)riid.Data4[4], (int)riid.Data4[5],
437                  (int)riid.Data4[6], (int)riid.Data4[7] );
438 #endif
439         *ppv = NULL;
440         return E_NOINTERFACE;
441     }
442 }
443
444 STDMETHODIMP_(ULONG) CapturePin::AddRef()
445 {
446 #ifdef DEBUG_DSHOW_L1
447     msg_Dbg( p_input, "CapturePin::AddRef (ref: %i)", i_ref );
448 #endif
449
450     return i_ref++;
451 };
452 STDMETHODIMP_(ULONG) CapturePin::Release()
453 {
454 #ifdef DEBUG_DSHOW_L1
455     msg_Dbg( p_input, "CapturePin::Release (ref: %i)", i_ref );
456 #endif
457
458     if( !InterlockedDecrement(&i_ref) ) delete this;
459
460     return 0;
461 };
462
463 /* IPin methods */
464 STDMETHODIMP CapturePin::Connect( IPin * pReceivePin,
465                                   const AM_MEDIA_TYPE *pmt )
466 {
467     if( State_Running == p_filter->state )
468     {
469         msg_Dbg( p_input, "CapturePin::Connect [not stopped]" );
470         return VFW_E_NOT_STOPPED;
471     }
472
473     if( p_connected_pin )
474     {
475         msg_Dbg( p_input, "CapturePin::Connect [already connected]" );
476         return VFW_E_ALREADY_CONNECTED;
477     }
478
479     if( !pmt ) return S_OK;
480  
481     if( GUID_NULL != pmt->majortype &&
482         media_types[0].majortype != pmt->majortype )
483     {
484         msg_Dbg( p_input, "CapturePin::Connect [media major type mismatch]" );
485         return S_FALSE;
486     }
487
488     if( GUID_NULL != pmt->subtype && !GetFourCCFromMediaType(*pmt) )
489     {
490         msg_Dbg( p_input, "CapturePin::Connect [media subtype type "
491                  "not supported]" );
492         return S_FALSE;
493     }
494
495     if( pmt->pbFormat && pmt->majortype == MEDIATYPE_Video  )
496     {
497         if( !((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight ||
498             !((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth )
499         {
500             msg_Dbg( p_input, "CapturePin::Connect "
501                      "[video width/height == 0 ]" );
502             return S_FALSE;
503         }
504     }
505
506     msg_Dbg( p_input, "CapturePin::Connect [OK]" );
507     return S_OK;
508 }
509 STDMETHODIMP CapturePin::ReceiveConnection( IPin * pConnector,
510                                             const AM_MEDIA_TYPE *pmt )
511 {
512     if( State_Stopped != p_filter->state )
513     {
514         msg_Dbg( p_input, "CapturePin::ReceiveConnection [not stopped]" );
515         return VFW_E_NOT_STOPPED;
516     }
517
518     if( !pConnector || !pmt )
519     {
520         msg_Dbg( p_input, "CapturePin::ReceiveConnection [null pointer]" );
521         return E_POINTER;
522     }
523
524     if( p_connected_pin )
525     {
526         msg_Dbg( p_input, "CapturePin::ReceiveConnection [already connected]");
527         return VFW_E_ALREADY_CONNECTED;
528     }
529
530     if( S_OK != QueryAccept(pmt) )
531     {
532         msg_Dbg( p_input, "CapturePin::ReceiveConnection "
533                  "[media type not accepted]" );
534         return VFW_E_TYPE_NOT_ACCEPTED;
535     }
536
537     msg_Dbg( p_input, "CapturePin::ReceiveConnection [OK]" );
538
539     p_connected_pin = pConnector;
540     p_connected_pin->AddRef();
541
542     FreeMediaType( cx_media_type );
543     return CopyMediaType( &cx_media_type, pmt );
544 }
545 STDMETHODIMP CapturePin::Disconnect()
546 {
547     if( ! p_connected_pin )
548     {
549         msg_Dbg( p_input, "CapturePin::Disconnect [not connected]" );
550         return S_FALSE;
551     }
552
553     msg_Dbg( p_input, "CapturePin::Disconnect [OK]" );
554
555     /* samples_queue was already flushed in EndFlush() */
556
557     p_connected_pin->Release();
558     p_connected_pin = NULL;
559     //FreeMediaType( cx_media_type );
560     //cx_media_type.subtype = GUID_NULL;
561
562     return S_OK;
563 }
564 STDMETHODIMP CapturePin::ConnectedTo( IPin **pPin )
565 {
566     if( !p_connected_pin )
567     {
568         msg_Dbg( p_input, "CapturePin::ConnectedTo [not connected]" );
569         return VFW_E_NOT_CONNECTED;
570     }
571
572     p_connected_pin->AddRef();
573     *pPin = p_connected_pin;
574
575     msg_Dbg( p_input, "CapturePin::ConnectedTo [OK]" );
576
577     return S_OK;
578 }
579 STDMETHODIMP CapturePin::ConnectionMediaType( AM_MEDIA_TYPE *pmt )
580 {
581     if( !p_connected_pin )
582     {
583         msg_Dbg( p_input, "CapturePin::ConnectionMediaType [not connected]" );
584         return VFW_E_NOT_CONNECTED;
585     }
586
587     return CopyMediaType( pmt, &cx_media_type );
588 }
589 STDMETHODIMP CapturePin::QueryPinInfo( PIN_INFO * pInfo )
590 {
591 #ifdef DEBUG_DSHOW
592     msg_Dbg( p_input, "CapturePin::QueryPinInfo" );
593 #endif
594
595     pInfo->pFilter = p_filter;
596     if( p_filter ) p_filter->AddRef();
597
598     memcpy(pInfo->achName, PIN_NAME, sizeof(PIN_NAME));
599     pInfo->dir = PINDIR_INPUT;
600
601     return NOERROR;
602 }
603 STDMETHODIMP CapturePin::QueryDirection( PIN_DIRECTION * pPinDir )
604 {
605 #ifdef DEBUG_DSHOW
606     msg_Dbg( p_input, "CapturePin::QueryDirection" );
607 #endif
608
609     *pPinDir = PINDIR_INPUT;
610     return NOERROR;
611 }
612 STDMETHODIMP CapturePin::QueryId( LPWSTR * Id )
613 {
614 #ifdef DEBUG_DSHOW
615     msg_Dbg( p_input, "CapturePin::QueryId" );
616 #endif
617
618     *Id = (LPWSTR)L"VLC Capture Pin";
619
620     return S_OK;
621 }
622 STDMETHODIMP CapturePin::QueryAccept( const AM_MEDIA_TYPE *pmt )
623 {
624     if( State_Stopped != p_filter->state )
625     {
626         msg_Dbg( p_input, "CapturePin::QueryAccept [not stopped]" );
627         return S_FALSE;
628     }
629
630     if( media_types[0].majortype != pmt->majortype )
631     {
632         msg_Dbg( p_input, "CapturePin::QueryAccept [media type mismatch]" );
633         return S_FALSE;
634     }
635
636     int i_fourcc = GetFourCCFromMediaType(*pmt);
637     if( !i_fourcc )
638     {
639         msg_Dbg( p_input, "CapturePin::QueryAccept "
640                  "[media type not supported]" );
641         return S_FALSE;
642     }
643
644     if( pmt->majortype == MEDIATYPE_Video )
645     {
646         if( pmt->pbFormat &&
647             ( (((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight == 0) ||
648               (((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth == 0) ) )
649         {
650             msg_Dbg( p_input, "CapturePin::QueryAccept [video size wxh == 0]");
651             return S_FALSE;
652         }
653
654         msg_Dbg( p_input, "CapturePin::QueryAccept [OK] "
655                  "(width=%ld, height=%ld, chroma=%4.4s, fps=%f)",
656                  ((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth,
657                  ((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight,
658                  (char *)&i_fourcc,
659          10000000.0f/((float)((VIDEOINFOHEADER *)pmt->pbFormat)->AvgTimePerFrame) );
660     }
661     else if( pmt->majortype == MEDIATYPE_Audio )
662     {
663         msg_Dbg( p_input, "CapturePin::QueryAccept [OK] (channels=%d, "
664                  "samples/sec=%lu, bits/samples=%d, format=%4.4s)",
665                  ((WAVEFORMATEX *)pmt->pbFormat)->nChannels,
666                  ((WAVEFORMATEX *)pmt->pbFormat)->nSamplesPerSec,
667                  ((WAVEFORMATEX *)pmt->pbFormat)->wBitsPerSample,
668                  (char *)&i_fourcc );
669     }
670     else
671     {
672         msg_Dbg( p_input, "CapturePin::QueryAccept [OK] (stream format=%4.4s)",
673                  (char *)&i_fourcc );
674     }
675
676     if( p_connected_pin )
677     {
678         FreeMediaType( cx_media_type );
679         CopyMediaType( &cx_media_type, pmt );
680     }
681
682     return S_OK;
683 }
684 STDMETHODIMP CapturePin::EnumMediaTypes( IEnumMediaTypes **ppEnum )
685 {
686 #ifdef DEBUG_DSHOW_L1
687     msg_Dbg( p_input, "CapturePin::EnumMediaTypes" );
688 #endif
689
690     *ppEnum = new CaptureEnumMediaTypes( p_input, this, NULL );
691
692     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
693
694     return NOERROR;
695 }
696 STDMETHODIMP CapturePin::QueryInternalConnections( IPin* *apPin, ULONG *nPin )
697 {
698 #ifdef DEBUG_DSHOW_L1
699     msg_Dbg( p_input, "CapturePin::QueryInternalConnections" );
700 #endif
701     return E_NOTIMPL;
702 }
703 STDMETHODIMP CapturePin::EndOfStream( void )
704 {
705 #ifdef DEBUG_DSHOW
706     msg_Dbg( p_input, "CapturePin::EndOfStream" );
707 #endif
708     return S_OK;
709 }
710 STDMETHODIMP CapturePin::BeginFlush( void )
711 {
712 #ifdef DEBUG_DSHOW
713     msg_Dbg( p_input, "CapturePin::BeginFlush" );
714 #endif
715     return S_OK;
716 }
717 STDMETHODIMP CapturePin::EndFlush( void )
718 {
719 #ifdef DEBUG_DSHOW
720     msg_Dbg( p_input, "CapturePin::EndFlush" );
721 #endif
722
723     VLCMediaSample vlc_sample;
724
725     vlc_mutex_lock( &p_sys->lock );
726     while( samples_queue.size() )
727     {
728         vlc_sample = samples_queue.back();
729         samples_queue.pop_back();
730         vlc_sample.p_sample->Release();
731     }
732     vlc_mutex_unlock( &p_sys->lock );
733
734     return S_OK;
735 }
736 STDMETHODIMP CapturePin::NewSegment( REFERENCE_TIME tStart,
737                                      REFERENCE_TIME tStop,
738                                      double dRate )
739 {
740 #ifdef DEBUG_DSHOW
741     msg_Dbg( p_input, "CapturePin::NewSegment" );
742 #endif
743     return S_OK;
744 }
745
746 /* IMemInputPin methods */
747 STDMETHODIMP CapturePin::GetAllocator( IMemAllocator **ppAllocator )
748 {
749 #ifdef DEBUG_DSHOW
750     msg_Dbg( p_input, "CapturePin::GetAllocator" );
751 #endif
752
753     return VFW_E_NO_ALLOCATOR;
754 }
755 STDMETHODIMP CapturePin::NotifyAllocator( IMemAllocator *pAllocator,
756                                           BOOL bReadOnly )
757 {
758 #ifdef DEBUG_DSHOW
759     msg_Dbg( p_input, "CapturePin::NotifyAllocator" );
760 #endif
761
762     return S_OK;
763 }
764 STDMETHODIMP CapturePin::GetAllocatorRequirements( ALLOCATOR_PROPERTIES *pProps )
765 {
766 #ifdef DEBUG_DSHOW
767     msg_Dbg( p_input, "CapturePin::GetAllocatorRequirements" );
768 #endif
769
770     return E_NOTIMPL;
771 }
772 STDMETHODIMP CapturePin::Receive( IMediaSample *pSample )
773 {
774 #if 0 //def DEBUG_DSHOW
775     msg_Dbg( p_input, "CapturePin::Receive" );
776 #endif
777
778     pSample->AddRef();
779     mtime_t i_timestamp = mdate() * 10;
780     VLCMediaSample vlc_sample = {pSample, i_timestamp};
781
782     vlc_mutex_lock( &p_sys->lock );
783     samples_queue.push_front( vlc_sample );
784
785     /* Make sure we don't cache too many samples */
786     if( samples_queue.size() > 10 )
787     {
788         vlc_sample = samples_queue.back();
789         samples_queue.pop_back();
790         msg_Dbg( p_input, "CapturePin::Receive trashing late input sample" );
791         vlc_sample.p_sample->Release();
792     }
793
794     vlc_cond_signal( &p_sys->wait );
795     vlc_mutex_unlock( &p_sys->lock );
796
797     return S_OK;
798 }
799 STDMETHODIMP CapturePin::ReceiveMultiple( IMediaSample **pSamples,
800                                           long nSamples,
801                                           long *nSamplesProcessed )
802 {
803     HRESULT hr = S_OK;
804
805     *nSamplesProcessed = 0;
806     while( nSamples-- > 0 )
807     {
808          hr = Receive( pSamples[*nSamplesProcessed] );
809          if( hr != S_OK ) break;
810          (*nSamplesProcessed)++;
811     }
812     return hr;
813 }
814 STDMETHODIMP CapturePin::ReceiveCanBlock( void )
815 {
816 #ifdef DEBUG_DSHOW
817     msg_Dbg( p_input, "CapturePin::ReceiveCanBlock" );
818 #endif
819
820     return S_FALSE; /* Thou shalt not block */
821 }
822
823 /****************************************************************************
824  * Implementation of our dummy directshow filter class
825  ****************************************************************************/
826 CaptureFilter::CaptureFilter( vlc_object_t *_p_input, access_sys_t *p_sys,
827                               AM_MEDIA_TYPE *mt, size_t mt_count )
828   : p_input( _p_input ),
829     p_pin( new CapturePin( _p_input, p_sys, this, mt, mt_count ) ),
830     state( State_Stopped ), i_ref( 1 )
831 {
832 }
833
834 CaptureFilter::~CaptureFilter()
835 {
836 #ifdef DEBUG_DSHOW
837     msg_Dbg( p_input, "CaptureFilter::~CaptureFilter" );
838 #endif
839     p_pin->Release();
840 }
841
842 /* IUnknown methods */
843 STDMETHODIMP CaptureFilter::QueryInterface( REFIID riid, void **ppv )
844 {
845 #ifdef DEBUG_DSHOW_L1
846     msg_Dbg( p_input, "CaptureFilter::QueryInterface" );
847 #endif
848
849     if( riid == IID_IUnknown )
850     {
851         AddRef();
852         *ppv = (IUnknown *)this;
853         return NOERROR;
854     }
855     if( riid == IID_IPersist )
856     {
857         AddRef();
858         *ppv = (IPersist *)this;
859         return NOERROR;
860     }
861     if( riid == IID_IMediaFilter )
862     {
863         AddRef();
864         *ppv = (IMediaFilter *)this;
865         return NOERROR;
866     }
867     if( riid == IID_IBaseFilter )
868     {
869         AddRef();
870         *ppv = (IBaseFilter *)this;
871         return NOERROR;
872     }
873     else
874     {
875 #ifdef DEBUG_DSHOW_L1
876         msg_Dbg( p_input, "CaptureFilter::QueryInterface() failed for: "
877                  "%04X-%02X-%02X-%02X%02X%02X%02X%02X%02X%02X%02X",
878                  (int)riid.Data1, (int)riid.Data2, (int)riid.Data3,
879                  static_cast<int>(riid.Data4[0]), (int)riid.Data4[1],
880                  (int)riid.Data4[2], (int)riid.Data4[3],
881                  (int)riid.Data4[4], (int)riid.Data4[5],
882                  (int)riid.Data4[6], (int)riid.Data4[7] );
883 #endif
884         *ppv = NULL;
885         return E_NOINTERFACE;
886     }
887 };
888 STDMETHODIMP_(ULONG) CaptureFilter::AddRef()
889 {
890 #ifdef DEBUG_DSHOW_L1
891     msg_Dbg( p_input, "CaptureFilter::AddRef (ref: %i)", i_ref );
892 #endif
893
894     return i_ref++;
895 };
896 STDMETHODIMP_(ULONG) CaptureFilter::Release()
897 {
898 #ifdef DEBUG_DSHOW_L1
899     msg_Dbg( p_input, "CaptureFilter::Release (ref: %i)", i_ref );
900 #endif
901
902     if( !InterlockedDecrement(&i_ref) ) delete this;
903
904     return 0;
905 };
906
907 /* IPersist method */
908 STDMETHODIMP CaptureFilter::GetClassID(CLSID *pClsID)
909 {
910 #ifdef DEBUG_DSHOW
911     msg_Dbg( p_input, "CaptureFilter::GetClassID" );
912 #endif
913     return E_NOTIMPL;
914 };
915
916 /* IMediaFilter methods */
917 STDMETHODIMP CaptureFilter::GetState(DWORD dwMSecs, FILTER_STATE *State)
918 {
919 #ifdef DEBUG_DSHOW
920     msg_Dbg( p_input, "CaptureFilter::GetState %i", state );
921 #endif
922
923     *State = state;
924     return S_OK;
925 };
926 STDMETHODIMP CaptureFilter::SetSyncSource(IReferenceClock *pClock)
927 {
928 #ifdef DEBUG_DSHOW
929     msg_Dbg( p_input, "CaptureFilter::SetSyncSource" );
930 #endif
931
932     return S_OK;
933 };
934 STDMETHODIMP CaptureFilter::GetSyncSource(IReferenceClock **pClock)
935 {
936 #ifdef DEBUG_DSHOW
937     msg_Dbg( p_input, "CaptureFilter::GetSyncSource" );
938 #endif
939
940     *pClock = NULL;
941     return NOERROR;
942 };
943 STDMETHODIMP CaptureFilter::Stop()
944 {
945 #ifdef DEBUG_DSHOW
946     msg_Dbg( p_input, "CaptureFilter::Stop" );
947 #endif
948
949     p_pin->EndFlush();
950
951     state = State_Stopped;
952     return S_OK;
953 };
954 STDMETHODIMP CaptureFilter::Pause()
955 {
956 #ifdef DEBUG_DSHOW
957     msg_Dbg( p_input, "CaptureFilter::Pause" );
958 #endif
959
960     state = State_Paused;
961     return S_OK;
962 };
963 STDMETHODIMP CaptureFilter::Run(REFERENCE_TIME tStart)
964 {
965 #ifdef DEBUG_DSHOW
966     msg_Dbg( p_input, "CaptureFilter::Run" );
967 #endif
968
969     state = State_Running;
970     return S_OK;
971 };
972
973 /* IBaseFilter methods */
974 STDMETHODIMP CaptureFilter::EnumPins( IEnumPins ** ppEnum )
975 {
976 #ifdef DEBUG_DSHOW
977     msg_Dbg( p_input, "CaptureFilter::EnumPins" );
978 #endif
979
980     /* Create a new ref counted enumerator */
981     *ppEnum = new CaptureEnumPins( p_input, this, NULL );
982     return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR;
983 };
984 STDMETHODIMP CaptureFilter::FindPin( LPCWSTR Id, IPin ** ppPin )
985 {
986 #ifdef DEBUG_DSHOW
987     msg_Dbg( p_input, "CaptureFilter::FindPin" );
988 #endif
989     return E_NOTIMPL;
990 };
991 STDMETHODIMP CaptureFilter::QueryFilterInfo( FILTER_INFO * pInfo )
992 {
993 #ifdef DEBUG_DSHOW
994     msg_Dbg( p_input, "CaptureFilter::QueryFilterInfo" );
995 #endif
996
997     memcpy(pInfo->achName, FILTER_NAME, sizeof(FILTER_NAME));
998
999     pInfo->pGraph = p_graph;
1000     if( p_graph ) p_graph->AddRef();
1001
1002     return NOERROR;
1003 };
1004 STDMETHODIMP CaptureFilter::JoinFilterGraph( IFilterGraph * pGraph,
1005                                              LPCWSTR pName )
1006 {
1007 #ifdef DEBUG_DSHOW
1008     msg_Dbg( p_input, "CaptureFilter::JoinFilterGraph" );
1009 #endif
1010
1011     p_graph = pGraph;
1012
1013     return NOERROR;
1014 };
1015 STDMETHODIMP CaptureFilter::QueryVendorInfo( LPWSTR* pVendorInfo )
1016 {
1017 #ifdef DEBUG_DSHOW
1018     msg_Dbg( p_input, "CaptureFilter::QueryVendorInfo" );
1019 #endif
1020     return E_NOTIMPL;
1021 };
1022
1023 /* Custom methods */
1024 CapturePin *CaptureFilter::CustomGetPin()
1025 {
1026     return p_pin;
1027 }
1028
1029 /****************************************************************************
1030  * Implementation of our dummy directshow enumpins class
1031  ****************************************************************************/
1032
1033 CaptureEnumPins::CaptureEnumPins( vlc_object_t *_p_input,
1034                                   CaptureFilter *_p_filter,
1035                                   CaptureEnumPins *pEnumPins )
1036   : p_input( _p_input ), p_filter( _p_filter ), i_ref( 1 )
1037 {
1038     /* Hold a reference count on our filter */
1039     p_filter->AddRef();
1040
1041     /* Are we creating a new enumerator */
1042
1043     if( pEnumPins == NULL )
1044     {
1045         i_position = 0;
1046     }
1047     else
1048     {
1049         i_position = pEnumPins->i_position;
1050     }
1051 }
1052
1053 CaptureEnumPins::~CaptureEnumPins()
1054 {
1055 #ifdef DEBUG_DSHOW_L1
1056     msg_Dbg( p_input, "CaptureEnumPins::~CaptureEnumPins" );
1057 #endif
1058     p_filter->Release();
1059 }
1060
1061 /* IUnknown methods */
1062 STDMETHODIMP CaptureEnumPins::QueryInterface( REFIID riid, void **ppv )
1063 {
1064 #ifdef DEBUG_DSHOW_L1
1065     msg_Dbg( p_input, "CaptureEnumPins::QueryInterface" );
1066 #endif
1067
1068     if( riid == IID_IUnknown ||
1069         riid == IID_IEnumPins )
1070     {
1071         AddRef();
1072         *ppv = (IEnumPins *)this;
1073         return NOERROR;
1074     }
1075     else
1076     {
1077         *ppv = NULL;
1078         return E_NOINTERFACE;
1079     }
1080 };
1081 STDMETHODIMP_(ULONG) CaptureEnumPins::AddRef()
1082 {
1083 #ifdef DEBUG_DSHOW_L1
1084     msg_Dbg( p_input, "CaptureEnumPins::AddRef (ref: %i)", i_ref );
1085 #endif
1086
1087     return i_ref++;
1088 };
1089 STDMETHODIMP_(ULONG) CaptureEnumPins::Release()
1090 {
1091 #ifdef DEBUG_DSHOW_L1
1092     msg_Dbg( p_input, "CaptureEnumPins::Release (ref: %i)", i_ref );
1093 #endif
1094
1095     if( !InterlockedDecrement(&i_ref) ) delete this;
1096
1097     return 0;
1098 };
1099
1100 /* IEnumPins */
1101 STDMETHODIMP CaptureEnumPins::Next( ULONG cPins, IPin ** ppPins,
1102                                     ULONG * pcFetched )
1103 {
1104 #ifdef DEBUG_DSHOW_L1
1105     msg_Dbg( p_input, "CaptureEnumPins::Next" );
1106 #endif
1107
1108     unsigned int i_fetched = 0;
1109
1110     if( i_position < 1 && cPins > 0 )
1111     {
1112         IPin *pPin = p_filter->CustomGetPin();
1113         *ppPins = pPin;
1114         pPin->AddRef();
1115         i_fetched = 1;
1116         i_position++;
1117     }
1118
1119     if( pcFetched ) *pcFetched = i_fetched;
1120
1121     return (i_fetched == cPins) ? S_OK : S_FALSE;
1122 };
1123 STDMETHODIMP CaptureEnumPins::Skip( ULONG cPins )
1124 {
1125 #ifdef DEBUG_DSHOW_L1
1126     msg_Dbg( p_input, "CaptureEnumPins::Skip" );
1127 #endif
1128
1129     i_position += cPins;
1130
1131     if( i_position > 1 )
1132     {
1133         return S_FALSE;
1134     }
1135
1136     return S_OK;
1137 };
1138 STDMETHODIMP CaptureEnumPins::Reset()
1139 {
1140 #ifdef DEBUG_DSHOW_L1
1141     msg_Dbg( p_input, "CaptureEnumPins::Reset" );
1142 #endif
1143
1144     i_position = 0;
1145     return S_OK;
1146 };
1147 STDMETHODIMP CaptureEnumPins::Clone( IEnumPins **ppEnum )
1148 {
1149 #ifdef DEBUG_DSHOW_L1
1150     msg_Dbg( p_input, "CaptureEnumPins::Clone" );
1151 #endif
1152
1153     *ppEnum = new CaptureEnumPins( p_input, p_filter, this );
1154     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
1155
1156     return NOERROR;
1157 };
1158
1159 /****************************************************************************
1160  * Implementation of our dummy directshow enummediatypes class
1161  ****************************************************************************/
1162 CaptureEnumMediaTypes::CaptureEnumMediaTypes( vlc_object_t *_p_input,
1163     CapturePin *_p_pin, CaptureEnumMediaTypes *pEnumMediaTypes )
1164   : p_input( _p_input ), p_pin( _p_pin ), i_ref( 1 )
1165 {
1166     /* Hold a reference count on our filter */
1167     p_pin->AddRef();
1168
1169     /* Are we creating a new enumerator */
1170     if( pEnumMediaTypes == NULL )
1171     {
1172         CopyMediaType(&cx_media_type, &p_pin->cx_media_type);
1173         i_position = 0;
1174     }
1175     else
1176     {
1177         CopyMediaType(&cx_media_type, &pEnumMediaTypes->cx_media_type);
1178         i_position = pEnumMediaTypes->i_position;
1179     }
1180 }
1181
1182 CaptureEnumMediaTypes::~CaptureEnumMediaTypes()
1183 {
1184 #ifdef DEBUG_DSHOW_L1
1185     msg_Dbg( p_input, "CaptureEnumMediaTypes::~CaptureEnumMediaTypes" );
1186 #endif
1187     FreeMediaType(cx_media_type);
1188     p_pin->Release();
1189 }
1190
1191 /* IUnknown methods */
1192 STDMETHODIMP CaptureEnumMediaTypes::QueryInterface( REFIID riid, void **ppv )
1193 {
1194 #ifdef DEBUG_DSHOW_L1
1195     msg_Dbg( p_input, "CaptureEnumMediaTypes::QueryInterface" );
1196 #endif
1197
1198     if( riid == IID_IUnknown ||
1199         riid == IID_IEnumMediaTypes )
1200     {
1201         AddRef();
1202         *ppv = (IEnumMediaTypes *)this;
1203         return NOERROR;
1204     }
1205     else
1206     {
1207         *ppv = NULL;
1208         return E_NOINTERFACE;
1209     }
1210 };
1211 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::AddRef()
1212 {
1213 #ifdef DEBUG_DSHOW_L1
1214     msg_Dbg( p_input, "CaptureEnumMediaTypes::AddRef (ref: %i)", i_ref );
1215 #endif
1216
1217     return i_ref++;
1218 };
1219 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::Release()
1220 {
1221 #ifdef DEBUG_DSHOW_L1
1222     msg_Dbg( p_input, "CaptureEnumMediaTypes::Release (ref: %i)", i_ref );
1223 #endif
1224
1225     if( !InterlockedDecrement(&i_ref) ) delete this;
1226
1227     return 0;
1228 };
1229
1230 /* IEnumMediaTypes */
1231 STDMETHODIMP CaptureEnumMediaTypes::Next( ULONG cMediaTypes,
1232                                           AM_MEDIA_TYPE ** ppMediaTypes,
1233                                           ULONG * pcFetched )
1234 {
1235 #ifdef DEBUG_DSHOW_L1
1236     msg_Dbg( p_input, "CaptureEnumMediaTypes::Next " );
1237 #endif
1238     ULONG copied = 0;
1239     ULONG offset = 0;
1240     ULONG max = p_pin->media_type_count;
1241
1242     if( ! ppMediaTypes )
1243         return E_POINTER;
1244
1245     if( (! pcFetched)  && (cMediaTypes > 1) )
1246        return E_POINTER;
1247
1248     /*
1249     ** use connection media type as first entry in iterator if it exists
1250     */
1251     copied = 0;
1252     if( cx_media_type.subtype != GUID_NULL )
1253     {
1254         ++max;
1255         if( i_position == 0 )
1256         {
1257             ppMediaTypes[copied] =
1258                 (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1259             if( CopyMediaType(ppMediaTypes[copied], &cx_media_type) != S_OK )
1260                 return E_OUTOFMEMORY;
1261             ++i_position;
1262             ++copied;
1263         }
1264     }
1265
1266     while( (copied < cMediaTypes) && (i_position < max)  )
1267     {
1268         ppMediaTypes[copied] =
1269             (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1270         if( CopyMediaType( ppMediaTypes[copied],
1271                            &p_pin->media_types[i_position-offset]) != S_OK )
1272             return E_OUTOFMEMORY;
1273
1274         ++copied;
1275         ++i_position;
1276     }
1277
1278     if( pcFetched )  *pcFetched = copied;
1279
1280     return (copied == cMediaTypes) ? S_OK : S_FALSE;
1281 };
1282 STDMETHODIMP CaptureEnumMediaTypes::Skip( ULONG cMediaTypes )
1283 {
1284     ULONG max =  p_pin->media_type_count;
1285     if( cx_media_type.subtype != GUID_NULL )
1286     {
1287         max = 1;
1288     }
1289 #ifdef DEBUG_DSHOW_L1
1290     msg_Dbg( p_input, "CaptureEnumMediaTypes::Skip" );
1291 #endif
1292
1293     i_position += cMediaTypes;
1294     return (i_position < max) ? S_OK : S_FALSE;
1295 };
1296 STDMETHODIMP CaptureEnumMediaTypes::Reset()
1297 {
1298 #ifdef DEBUG_DSHOW_L1
1299     msg_Dbg( p_input, "CaptureEnumMediaTypes::Reset" );
1300 #endif
1301
1302     FreeMediaType(cx_media_type);
1303     CopyMediaType(&cx_media_type, &p_pin->cx_media_type);
1304     i_position = 0;
1305     return S_OK;
1306 };
1307 STDMETHODIMP CaptureEnumMediaTypes::Clone( IEnumMediaTypes **ppEnum )
1308 {
1309 #ifdef DEBUG_DSHOW_L1
1310     msg_Dbg( p_input, "CaptureEnumMediaTypes::Clone" );
1311 #endif
1312
1313     *ppEnum = new CaptureEnumMediaTypes( p_input, p_pin, this );
1314     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
1315
1316     return NOERROR;
1317 };