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