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