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