]> git.sesse.net Git - vlc/blob - modules/access/dshow/filter.cpp
* modules/access/dshow/filter.cpp: re-enabled code to properly do the cleanup on...
[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 "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 hear 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 /* Analog Video */
148 const GUID FORMAT_AnalogVideo = {0x482dde0, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
149
150 const GUID MEDIATYPE_AnalogVideo = {0x482dde1, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xab, 0x0, 0x6e, 0xcb, 0x65}};
151
152 const GUID MEDIASUBTYPE_AnalogVideo_NTSC_M = {0x482dde2, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
153 const GUID MEDIASUBTYPE_AnalogVideo_PAL_B = {0x482dde5, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
154 const GUID MEDIASUBTYPE_AnalogVideo_PAL_D = {0x482dde6, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
155 const GUID MEDIASUBTYPE_AnalogVideo_PAL_G = {0x482dde7, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
156 const GUID MEDIASUBTYPE_AnalogVideo_PAL_H = {0x482dde8, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
157 const GUID MEDIASUBTYPE_AnalogVideo_PAL_I = {0x482dde9, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
158 const GUID MEDIASUBTYPE_AnalogVideo_PAL_M = {0x482ddea, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
159 const GUID MEDIASUBTYPE_AnalogVideo_PAL_N = {0x482ddeb, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
160 const GUID MEDIASUBTYPE_AnalogVideo_PAL_N_COMBO = {0x482ddec, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
161 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_B = {0x482ddf0, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
162 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_D = {0x482ddf1, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
163 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_G = {0x482ddf2, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
164 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_H = {0x482ddf3, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
165 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_K = {0x482ddf4, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
166 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_K1 = {0x482ddf5, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
167 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_L = {0x482ddf6, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
168
169 const GUID AMPROPSETID_Pin= {0x9b00f101, 0x1567, 0x11d1, {0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc5}};
170 const GUID PIN_CATEGORY_ANALOGVIDEOIN= {0xfb6c4283, 0x0353, 0x11d1, {0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba}};
171 const GUID PIN_CATEGORY_CAPTURE= {0xfb6c4281, 0x0353, 0x11d1, {0x90, 0x5f, 0x0, 0x0, 0xc0, 0xcc, 0x16, 0xba}};
172 const GUID LOOK_UPSTREAM_ONLY= {0xac798be0, 0x98e3, 0x11d1, {0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc}};
173
174 //const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
175 extern const GUID GUID_NULL;
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( 1 /* 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
306     return i_fourcc;
307 }
308
309 /****************************************************************************
310  * Implementation of our dummy directshow filter pin class
311  ****************************************************************************/
312
313 CapturePin::CapturePin( vlc_object_t *_p_input, access_sys_t *_p_sys,
314                         CaptureFilter *_p_filter,
315                         AM_MEDIA_TYPE *mt, size_t mt_count )
316   : p_input( _p_input ), p_sys( _p_sys ), p_filter( _p_filter ),
317     p_connected_pin( NULL ),  media_types(mt), media_type_count(mt_count),
318     i_ref( 1 )
319 {
320     cx_media_type.majortype = mt[0].majortype;
321     cx_media_type.subtype   = GUID_NULL;
322     cx_media_type.pbFormat  = NULL;
323     cx_media_type.cbFormat  = 0;
324     cx_media_type.pUnk      = NULL;
325 }
326
327 CapturePin::~CapturePin()
328 {
329 #ifdef DEBUG_DSHOW
330     msg_Dbg( p_input, "CapturePin::~CapturePin" );
331 #endif
332     for( size_t c=0; c<media_type_count; c++ )
333     {
334         FreeMediaType(media_types[c]);
335     }
336     FreeMediaType(cx_media_type);
337 }
338
339 HRESULT CapturePin::CustomGetSample( VLCMediaSample *vlc_sample )
340 {
341 #if 0 //def DEBUG_DSHOW
342     msg_Dbg( p_input, "CapturePin::CustomGetSample" );
343 #endif
344
345     vlc_mutex_lock( &p_sys->lock );
346     if( samples_queue.size() )
347     {
348         *vlc_sample = samples_queue.back();
349         samples_queue.pop_back();
350         vlc_mutex_unlock( &p_sys->lock );
351         return S_OK;
352     }
353     vlc_mutex_unlock( &p_sys->lock );
354     return S_FALSE;
355 }
356
357 AM_MEDIA_TYPE &CapturePin::CustomGetMediaType()
358 {
359     return cx_media_type;
360 }
361
362 /* IUnknown methods */
363 STDMETHODIMP CapturePin::QueryInterface(REFIID riid, void **ppv)
364 {
365 #ifdef DEBUG_DSHOW_L1
366     msg_Dbg( p_input, "CapturePin::QueryInterface" );
367 #endif
368
369     if( riid == IID_IUnknown ||
370         riid == IID_IPin )
371     {
372         AddRef();
373         *ppv = (IPin *)this;
374         return NOERROR;
375     }
376     if( riid == IID_IMemInputPin )
377     {
378         AddRef();
379         *ppv = (IMemInputPin *)this;
380         return NOERROR;
381     }
382     else
383     {
384 #ifdef DEBUG_DSHOW_L1
385         msg_Dbg( p_input, "CapturePin::QueryInterface() failed for: "
386                  "%04X-%02X-%02X-%02X%02X%02X%02X%02X%02X%02X%02X",
387                  (int)riid.Data1, (int)riid.Data2, (int)riid.Data3,
388                  static_cast<int>(riid.Data4[0]), (int)riid.Data4[1],
389                  (int)riid.Data4[2], (int)riid.Data4[3],
390                  (int)riid.Data4[4], (int)riid.Data4[5],
391                  (int)riid.Data4[6], (int)riid.Data4[7] );
392 #endif
393         *ppv = NULL;
394         return E_NOINTERFACE;
395     }
396 }
397
398 STDMETHODIMP_(ULONG) CapturePin::AddRef()
399 {
400 #ifdef DEBUG_DSHOW_L1
401     msg_Dbg( p_input, "CapturePin::AddRef (ref: %i)", i_ref );
402 #endif
403
404     return i_ref++;
405 };
406 STDMETHODIMP_(ULONG) CapturePin::Release()
407 {
408 #ifdef DEBUG_DSHOW_L1
409     msg_Dbg( p_input, "CapturePin::Release (ref: %i)", i_ref );
410 #endif
411
412     if( !InterlockedDecrement(&i_ref) ) delete this;
413
414     return 0;
415 };
416
417 /* IPin methods */
418 STDMETHODIMP CapturePin::Connect( IPin * pReceivePin,
419                                   const AM_MEDIA_TYPE *pmt )
420 {
421     if( State_Running == p_filter->state )
422     {
423         msg_Dbg( p_input, "CapturePin::Connect [not stopped]" );
424         return VFW_E_NOT_STOPPED;
425     }
426
427     if( p_connected_pin )
428     {
429         msg_Dbg( p_input, "CapturePin::Connect [already connected]" );
430         return VFW_E_ALREADY_CONNECTED;
431     }
432
433     if( !pmt ) return S_OK;
434                 
435     if( GUID_NULL != pmt->majortype &&
436         media_types[0].majortype != pmt->majortype )
437     {
438         msg_Dbg( p_input, "CapturePin::Connect [media major type mismatch]" );
439         return S_FALSE;
440     }
441
442     if( GUID_NULL != pmt->subtype && !GetFourCCFromMediaType(*pmt) )
443     {
444         msg_Dbg( p_input, "CapturePin::Connect [media subtype type "
445                  "not supported]" );
446         return S_FALSE;
447     }
448
449     if( pmt->pbFormat && pmt->majortype == MEDIATYPE_Video  )
450     {
451         if( !((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight ||
452             !((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth )
453         {
454             msg_Dbg( p_input, "CapturePin::Connect "
455                      "[video width/height == 0 ]" );
456             return S_FALSE;
457         }
458     }
459
460     msg_Dbg( p_input, "CapturePin::Connect [OK]" );
461     return S_OK;
462 }
463 STDMETHODIMP CapturePin::ReceiveConnection( IPin * pConnector,
464                                             const AM_MEDIA_TYPE *pmt )
465 {
466     if( State_Stopped != p_filter->state )
467     {
468         msg_Dbg( p_input, "CapturePin::ReceiveConnection [not stopped]" );
469         return VFW_E_NOT_STOPPED;
470     }
471
472     if( !pConnector || !pmt )
473     {
474         msg_Dbg( p_input, "CapturePin::ReceiveConnection [null pointer]" );
475         return E_POINTER;
476     }
477
478     if( p_connected_pin )
479     {
480         msg_Dbg( p_input, "CapturePin::ReceiveConnection [already connected]");
481         return VFW_E_ALREADY_CONNECTED;
482     }
483
484     if( S_OK != QueryAccept(pmt) )
485     {
486         msg_Dbg( p_input, "CapturePin::ReceiveConnection "
487                  "[media type not accepted]" );
488         return VFW_E_TYPE_NOT_ACCEPTED;
489     }
490
491     msg_Dbg( p_input, "CapturePin::ReceiveConnection [OK]" );
492
493     p_connected_pin = pConnector;
494     p_connected_pin->AddRef();
495
496     FreeMediaType( cx_media_type );
497     return CopyMediaType( &cx_media_type, pmt );
498 }
499 STDMETHODIMP CapturePin::Disconnect()
500 {
501     if( ! p_connected_pin )
502     {
503         msg_Dbg( p_input, "CapturePin::Disconnect [not connected]" );
504         return S_FALSE;
505     }
506
507     msg_Dbg( p_input, "CapturePin::Disconnect [OK]" );
508
509 #if 1 // This used to create crashes sometimes. Is the problem fixed ?
510     VLCMediaSample vlc_sample;
511
512     vlc_mutex_lock( &p_sys->lock );
513     while( samples_queue.size() )
514     {
515         vlc_sample = samples_queue.back();
516         samples_queue.pop_back();
517         vlc_sample.p_sample->Release();
518     }
519     vlc_mutex_unlock( &p_sys->lock );
520 #endif
521
522     p_connected_pin->Release();
523     p_connected_pin = NULL;
524     //FreeMediaType( cx_media_type );
525     //cx_media_type.subtype = GUID_NULL;
526
527     return S_OK;
528 }
529 STDMETHODIMP CapturePin::ConnectedTo( IPin **pPin )
530 {
531     if( !p_connected_pin )
532     {
533         msg_Dbg( p_input, "CapturePin::ConnectedTo [not connected]" );
534         return VFW_E_NOT_CONNECTED;
535     }
536
537     p_connected_pin->AddRef();
538     *pPin = p_connected_pin;
539
540     msg_Dbg( p_input, "CapturePin::ConnectedTo [OK]" );
541
542     return S_OK;
543 }
544 STDMETHODIMP CapturePin::ConnectionMediaType( AM_MEDIA_TYPE *pmt )
545 {
546     if( !p_connected_pin )
547     {
548         msg_Dbg( p_input, "CapturePin::ConnectionMediaType [not connected]" );
549         return VFW_E_NOT_CONNECTED;
550     }
551
552     return CopyMediaType( pmt, &cx_media_type );
553 }
554 STDMETHODIMP CapturePin::QueryPinInfo( PIN_INFO * pInfo )
555 {
556 #ifdef DEBUG_DSHOW
557     msg_Dbg( p_input, "CapturePin::QueryPinInfo" );
558 #endif
559
560     pInfo->pFilter = p_filter;
561     if( p_filter ) p_filter->AddRef();
562
563     memcpy(pInfo->achName, PIN_NAME, sizeof(PIN_NAME));
564     pInfo->dir = PINDIR_INPUT;
565
566     return NOERROR;
567 }
568 STDMETHODIMP CapturePin::QueryDirection( PIN_DIRECTION * pPinDir )
569 {
570 #ifdef DEBUG_DSHOW
571     msg_Dbg( p_input, "CapturePin::QueryDirection" );
572 #endif
573
574     *pPinDir = PINDIR_INPUT;
575     return NOERROR;
576 }
577 STDMETHODIMP CapturePin::QueryId( LPWSTR * Id )
578 {
579 #ifdef DEBUG_DSHOW
580     msg_Dbg( p_input, "CapturePin::QueryId" );
581 #endif
582
583     *Id = L"VideoLAN Capture Pin";
584
585     return S_OK;
586 }
587 STDMETHODIMP CapturePin::QueryAccept( const AM_MEDIA_TYPE *pmt )
588 {
589     if( State_Stopped != p_filter->state )
590     {
591         msg_Dbg( p_input, "CapturePin::QueryAccept [not stopped]" );
592         return S_FALSE;
593     }
594
595     if( media_types[0].majortype != pmt->majortype )
596     {
597         msg_Dbg( p_input, "CapturePin::QueryAccept [media type mismatch]" );
598         return S_FALSE;
599     }
600
601     int i_fourcc = GetFourCCFromMediaType(*pmt);
602     if( !i_fourcc )
603     {   
604         msg_Dbg( p_input, "CapturePin::QueryAccept "
605                  "[media type not supported]" );
606         return S_FALSE;
607     }
608
609     if( pmt->majortype == MEDIATYPE_Video )
610     {
611         if( pmt->pbFormat &&
612             ( (((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight == 0) ||
613               (((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth == 0) ) )
614         {
615             msg_Dbg( p_input, "CapturePin::QueryAccept [video size wxh == 0]");
616             return S_FALSE;
617         }
618
619         msg_Dbg( p_input, "CapturePin::QueryAccept [OK] "
620                  "(width=%ld, height=%ld, chroma=%4.4s)",
621                  ((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth,
622                  ((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight,
623                  (char *)&i_fourcc );
624     }
625     else if( pmt->majortype == MEDIATYPE_Audio )
626     {
627         msg_Dbg( p_input, "CapturePin::QueryAccept [OK] (channels=%d, "
628                  "samples/sec=%lu, bits/samples=%d, format=%4.4s)",
629                  ((WAVEFORMATEX *)pmt->pbFormat)->nChannels,
630                  ((WAVEFORMATEX *)pmt->pbFormat)->nSamplesPerSec,
631                  ((WAVEFORMATEX *)pmt->pbFormat)->wBitsPerSample,
632                  (char *)&i_fourcc );
633     }   
634     else
635     {
636         msg_Dbg( p_input, "CapturePin::QueryAccept [OK] (stream format=%4.4s)",
637                  (char *)&i_fourcc );
638     }
639
640     if( p_connected_pin )
641     {
642         FreeMediaType( cx_media_type );
643         CopyMediaType( &cx_media_type, pmt );
644     }
645
646     return S_OK;
647 }
648 STDMETHODIMP CapturePin::EnumMediaTypes( IEnumMediaTypes **ppEnum )
649 {
650 #ifdef DEBUG_DSHOW_L1
651     msg_Dbg( p_input, "CapturePin::EnumMediaTypes" );
652 #endif
653
654     *ppEnum = new CaptureEnumMediaTypes( p_input, this, NULL );
655
656     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
657
658     return NOERROR;
659 }
660 STDMETHODIMP CapturePin::QueryInternalConnections( IPin* *apPin, ULONG *nPin )
661 {
662 #ifdef DEBUG_DSHOW_L1
663     msg_Dbg( p_input, "CapturePin::QueryInternalConnections" );
664 #endif
665     return E_NOTIMPL;
666 }
667 STDMETHODIMP CapturePin::EndOfStream( void )
668 {
669 #ifdef DEBUG_DSHOW
670     msg_Dbg( p_input, "CapturePin::EndOfStream" );
671 #endif
672     return S_OK;
673 }
674 STDMETHODIMP CapturePin::BeginFlush( void )
675 {
676 #ifdef DEBUG_DSHOW
677     msg_Dbg( p_input, "CapturePin::BeginFlush" );
678 #endif
679     return S_OK;
680 }
681 STDMETHODIMP CapturePin::EndFlush( void )
682 {
683 #ifdef DEBUG_DSHOW
684     msg_Dbg( p_input, "CapturePin::EndFlush" );
685 #endif
686     return S_OK;
687 }
688 STDMETHODIMP CapturePin::NewSegment( REFERENCE_TIME tStart,
689                                      REFERENCE_TIME tStop,
690                                      double dRate )
691 {
692 #ifdef DEBUG_DSHOW
693     msg_Dbg( p_input, "CapturePin::NewSegment" );
694 #endif
695     return S_OK;
696 }
697
698 /* IMemInputPin methods */
699 STDMETHODIMP CapturePin::GetAllocator( IMemAllocator **ppAllocator )
700 {
701 #ifdef DEBUG_DSHOW
702     msg_Dbg( p_input, "CapturePin::GetAllocator" );
703 #endif
704
705     return VFW_E_NO_ALLOCATOR;
706 }
707 STDMETHODIMP CapturePin::NotifyAllocator( IMemAllocator *pAllocator,
708                                           BOOL bReadOnly )
709 {
710 #ifdef DEBUG_DSHOW
711     msg_Dbg( p_input, "CapturePin::NotifyAllocator" );
712 #endif
713
714     return S_OK;
715 }
716 STDMETHODIMP CapturePin::GetAllocatorRequirements( ALLOCATOR_PROPERTIES *pProps )
717 {
718 #ifdef DEBUG_DSHOW
719     msg_Dbg( p_input, "CapturePin::GetAllocatorRequirements" );
720 #endif
721
722     return E_NOTIMPL;
723 }
724 STDMETHODIMP CapturePin::Receive( IMediaSample *pSample )
725 {
726 #if 0 //def DEBUG_DSHOW
727     msg_Dbg( p_input, "CapturePin::Receive" );
728 #endif
729
730     pSample->AddRef();
731     mtime_t i_timestamp = mdate() * 10;
732     VLCMediaSample vlc_sample = {pSample, i_timestamp};
733
734     vlc_mutex_lock( &p_sys->lock );
735     samples_queue.push_front( vlc_sample );
736
737     /* Make sure we don't cache too many samples */
738     if( samples_queue.size() > 10 )
739     {
740         vlc_sample = samples_queue.back();
741         samples_queue.pop_back();
742         msg_Dbg( p_input, "CapturePin::Receive trashing late input sample" );
743         vlc_sample.p_sample->Release();
744     }
745
746     vlc_cond_signal( &p_sys->wait );
747     vlc_mutex_unlock( &p_sys->lock );
748
749     return S_OK;
750 }
751 STDMETHODIMP CapturePin::ReceiveMultiple( IMediaSample **pSamples,
752                                           long nSamples,
753                                           long *nSamplesProcessed )
754 {
755     HRESULT hr = S_OK;
756
757     *nSamplesProcessed = 0;
758     while( nSamples-- > 0 )
759     {
760          hr = Receive( pSamples[*nSamplesProcessed] );
761          if( hr != S_OK ) break;
762          (*nSamplesProcessed)++;
763     }
764     return hr;
765 }
766 STDMETHODIMP CapturePin::ReceiveCanBlock( void )
767 {
768 #ifdef DEBUG_DSHOW
769     msg_Dbg( p_input, "CapturePin::ReceiveCanBlock" );
770 #endif
771
772     return S_FALSE; /* Thou shalt not block */
773 }
774
775 /****************************************************************************
776  * Implementation of our dummy directshow filter class
777  ****************************************************************************/
778 CaptureFilter::CaptureFilter( vlc_object_t *_p_input, access_sys_t *p_sys,
779                               AM_MEDIA_TYPE *mt, size_t mt_count )
780   : p_input( _p_input ),
781     p_pin( new CapturePin( _p_input, p_sys, this, mt, mt_count ) ),
782     state( State_Stopped ), i_ref( 1 ) 
783 {
784 }
785
786 CaptureFilter::~CaptureFilter()
787 {
788 #ifdef DEBUG_DSHOW
789     msg_Dbg( p_input, "CaptureFilter::~CaptureFilter" );
790 #endif
791     p_pin->Release();
792 }
793
794 /* IUnknown methods */
795 STDMETHODIMP CaptureFilter::QueryInterface( REFIID riid, void **ppv )
796 {
797 #ifdef DEBUG_DSHOW_L1
798     msg_Dbg( p_input, "CaptureFilter::QueryInterface" );
799 #endif
800
801     if( riid == IID_IUnknown )
802     {
803         AddRef();
804         *ppv = (IUnknown *)this;
805         return NOERROR;
806     }
807     if( riid == IID_IPersist )
808     {
809         AddRef();
810         *ppv = (IPersist *)this;
811         return NOERROR;
812     }
813     if( riid == IID_IMediaFilter )
814     {
815         AddRef();
816         *ppv = (IMediaFilter *)this;
817         return NOERROR;
818     }
819     if( riid == IID_IBaseFilter )
820     {
821         AddRef();
822         *ppv = (IBaseFilter *)this;
823         return NOERROR;
824     }
825     else
826     {
827 #ifdef DEBUG_DSHOW_L1
828         msg_Dbg( p_input, "CaptureFilter::QueryInterface() failed for: "
829                  "%04X-%02X-%02X-%02X%02X%02X%02X%02X%02X%02X%02X",
830                  (int)riid.Data1, (int)riid.Data2, (int)riid.Data3,
831                  static_cast<int>(riid.Data4[0]), (int)riid.Data4[1],
832                  (int)riid.Data4[2], (int)riid.Data4[3],
833                  (int)riid.Data4[4], (int)riid.Data4[5],
834                  (int)riid.Data4[6], (int)riid.Data4[7] );
835 #endif
836         *ppv = NULL;
837         return E_NOINTERFACE;
838     }
839 };
840 STDMETHODIMP_(ULONG) CaptureFilter::AddRef()
841 {
842 #ifdef DEBUG_DSHOW_L1
843     msg_Dbg( p_input, "CaptureFilter::AddRef (ref: %i)", i_ref );
844 #endif
845
846     return i_ref++;
847 };
848 STDMETHODIMP_(ULONG) CaptureFilter::Release()
849 {
850 #ifdef DEBUG_DSHOW_L1
851     msg_Dbg( p_input, "CaptureFilter::Release (ref: %i)", i_ref );
852 #endif
853
854     if( !InterlockedDecrement(&i_ref) ) delete this;
855
856     return 0;
857 };
858
859 /* IPersist method */
860 STDMETHODIMP CaptureFilter::GetClassID(CLSID *pClsID)
861 {
862 #ifdef DEBUG_DSHOW
863     msg_Dbg( p_input, "CaptureFilter::GetClassID" );
864 #endif
865     return E_NOTIMPL;
866 };
867
868 /* IMediaFilter methods */
869 STDMETHODIMP CaptureFilter::GetState(DWORD dwMSecs, FILTER_STATE *State)
870 {
871 #ifdef DEBUG_DSHOW
872     msg_Dbg( p_input, "CaptureFilter::GetState %i", state );
873 #endif
874
875     *State = state;
876     return S_OK;
877 };
878 STDMETHODIMP CaptureFilter::SetSyncSource(IReferenceClock *pClock)
879 {
880 #ifdef DEBUG_DSHOW
881     msg_Dbg( p_input, "CaptureFilter::SetSyncSource" );
882 #endif
883
884     return S_OK;
885 };
886 STDMETHODIMP CaptureFilter::GetSyncSource(IReferenceClock **pClock)
887 {
888 #ifdef DEBUG_DSHOW
889     msg_Dbg( p_input, "CaptureFilter::GetSyncSource" );
890 #endif
891
892     *pClock = NULL;
893     return NOERROR;
894 };
895 STDMETHODIMP CaptureFilter::Stop()
896 {
897 #ifdef DEBUG_DSHOW
898     msg_Dbg( p_input, "CaptureFilter::Stop" );
899 #endif
900
901     state = State_Stopped;
902     return S_OK;
903 };
904 STDMETHODIMP CaptureFilter::Pause()
905 {
906 #ifdef DEBUG_DSHOW
907     msg_Dbg( p_input, "CaptureFilter::Pause" );
908 #endif
909
910     state = State_Paused;
911     return S_OK;
912 };
913 STDMETHODIMP CaptureFilter::Run(REFERENCE_TIME tStart)
914 {
915 #ifdef DEBUG_DSHOW
916     msg_Dbg( p_input, "CaptureFilter::Run" );
917 #endif
918
919     state = State_Running;
920     return S_OK;
921 };
922
923 /* IBaseFilter methods */
924 STDMETHODIMP CaptureFilter::EnumPins( IEnumPins ** ppEnum )
925 {
926 #ifdef DEBUG_DSHOW
927     msg_Dbg( p_input, "CaptureFilter::EnumPins" );
928 #endif
929
930     /* Create a new ref counted enumerator */
931     *ppEnum = new CaptureEnumPins( p_input, this, NULL );
932     return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR;
933 };
934 STDMETHODIMP CaptureFilter::FindPin( LPCWSTR Id, IPin ** ppPin )
935 {
936 #ifdef DEBUG_DSHOW
937     msg_Dbg( p_input, "CaptureFilter::FindPin" );
938 #endif
939     return E_NOTIMPL;
940 };
941 STDMETHODIMP CaptureFilter::QueryFilterInfo( FILTER_INFO * pInfo )
942 {
943 #ifdef DEBUG_DSHOW
944     msg_Dbg( p_input, "CaptureFilter::QueryFilterInfo" );
945 #endif
946
947     memcpy(pInfo->achName, FILTER_NAME, sizeof(FILTER_NAME));
948
949     pInfo->pGraph = p_graph;
950     if( p_graph ) p_graph->AddRef();
951
952     return NOERROR;
953 };
954 STDMETHODIMP CaptureFilter::JoinFilterGraph( IFilterGraph * pGraph,
955                                              LPCWSTR pName )
956 {
957 #ifdef DEBUG_DSHOW
958     msg_Dbg( p_input, "CaptureFilter::JoinFilterGraph" );
959 #endif
960
961     p_graph = pGraph;
962
963     return NOERROR;
964 };
965 STDMETHODIMP CaptureFilter::QueryVendorInfo( LPWSTR* pVendorInfo )
966 {
967 #ifdef DEBUG_DSHOW
968     msg_Dbg( p_input, "CaptureFilter::QueryVendorInfo" );
969 #endif
970     return E_NOTIMPL;
971 };
972
973 /* Custom methods */
974 CapturePin *CaptureFilter::CustomGetPin()
975 {
976     return p_pin;
977 }
978
979 /****************************************************************************
980  * Implementation of our dummy directshow enumpins class
981  ****************************************************************************/
982
983 CaptureEnumPins::CaptureEnumPins( vlc_object_t *_p_input,
984                                   CaptureFilter *_p_filter,
985                                   CaptureEnumPins *pEnumPins )
986   : p_input( _p_input ), p_filter( _p_filter ), i_ref( 1 )
987 {
988     /* Hold a reference count on our filter */
989     p_filter->AddRef();
990
991     /* Are we creating a new enumerator */
992
993     if( pEnumPins == NULL )
994     {
995         i_position = 0;
996     }
997     else
998     {
999         i_position = pEnumPins->i_position;
1000     }
1001 }
1002
1003 CaptureEnumPins::~CaptureEnumPins()
1004 {
1005 #ifdef DEBUG_DSHOW_L1
1006     msg_Dbg( p_input, "CaptureEnumPins::~CaptureEnumPins" );
1007 #endif
1008     p_filter->Release();
1009 }
1010
1011 /* IUnknown methods */
1012 STDMETHODIMP CaptureEnumPins::QueryInterface( REFIID riid, void **ppv )
1013 {
1014 #ifdef DEBUG_DSHOW_L1
1015     msg_Dbg( p_input, "CaptureEnumPins::QueryInterface" );
1016 #endif
1017
1018     if( riid == IID_IUnknown ||
1019         riid == IID_IEnumPins )
1020     {
1021         AddRef();
1022         *ppv = (IEnumPins *)this;
1023         return NOERROR;
1024     }
1025     else
1026     {
1027         *ppv = NULL;
1028         return E_NOINTERFACE;
1029     }
1030 };
1031 STDMETHODIMP_(ULONG) CaptureEnumPins::AddRef()
1032 {
1033 #ifdef DEBUG_DSHOW_L1
1034     msg_Dbg( p_input, "CaptureEnumPins::AddRef (ref: %i)", i_ref );
1035 #endif
1036
1037     return i_ref++;
1038 };
1039 STDMETHODIMP_(ULONG) CaptureEnumPins::Release()
1040 {
1041 #ifdef DEBUG_DSHOW_L1
1042     msg_Dbg( p_input, "CaptureEnumPins::Release (ref: %i)", i_ref );
1043 #endif
1044
1045     if( !InterlockedDecrement(&i_ref) ) delete this;
1046
1047     return 0;
1048 };
1049
1050 /* IEnumPins */
1051 STDMETHODIMP CaptureEnumPins::Next( ULONG cPins, IPin ** ppPins,
1052                                     ULONG * pcFetched )
1053 {
1054 #ifdef DEBUG_DSHOW_L1
1055     msg_Dbg( p_input, "CaptureEnumPins::Next" );
1056 #endif
1057
1058     unsigned int i_fetched = 0;
1059
1060     if( i_position < 1 && cPins > 0 )
1061     {
1062         IPin *pPin = p_filter->CustomGetPin();
1063         *ppPins = pPin;
1064         pPin->AddRef();
1065         i_fetched = 1;
1066         i_position++;
1067     }
1068
1069     if( pcFetched ) *pcFetched = i_fetched;
1070
1071     return (i_fetched == cPins) ? S_OK : S_FALSE;
1072 };
1073 STDMETHODIMP CaptureEnumPins::Skip( ULONG cPins )
1074 {
1075 #ifdef DEBUG_DSHOW_L1
1076     msg_Dbg( p_input, "CaptureEnumPins::Skip" );
1077 #endif
1078
1079     i_position += cPins;
1080
1081     if( i_position > 1 )
1082     {
1083         return S_FALSE;
1084     }
1085
1086     return S_OK;
1087 };
1088 STDMETHODIMP CaptureEnumPins::Reset()
1089 {
1090 #ifdef DEBUG_DSHOW_L1
1091     msg_Dbg( p_input, "CaptureEnumPins::Reset" );
1092 #endif
1093
1094     i_position = 0;
1095     return S_OK;
1096 };
1097 STDMETHODIMP CaptureEnumPins::Clone( IEnumPins **ppEnum )
1098 {
1099 #ifdef DEBUG_DSHOW_L1
1100     msg_Dbg( p_input, "CaptureEnumPins::Clone" );
1101 #endif
1102
1103     *ppEnum = new CaptureEnumPins( p_input, p_filter, this );
1104     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
1105
1106     return NOERROR;
1107 };
1108
1109 /****************************************************************************
1110  * Implementation of our dummy directshow enummediatypes class
1111  ****************************************************************************/
1112 CaptureEnumMediaTypes::CaptureEnumMediaTypes( vlc_object_t *_p_input,
1113     CapturePin *_p_pin, CaptureEnumMediaTypes *pEnumMediaTypes )
1114   : p_input( _p_input ), p_pin( _p_pin ), i_ref( 1 )
1115 {
1116     /* Hold a reference count on our filter */
1117     p_pin->AddRef();
1118
1119     /* Are we creating a new enumerator */
1120     if( pEnumMediaTypes == NULL )
1121     {
1122         CopyMediaType(&cx_media_type, &p_pin->cx_media_type); 
1123         i_position = 0;
1124     }
1125     else
1126     {
1127         CopyMediaType(&cx_media_type, &pEnumMediaTypes->cx_media_type); 
1128         i_position = pEnumMediaTypes->i_position;
1129     }
1130 }
1131
1132 CaptureEnumMediaTypes::~CaptureEnumMediaTypes()
1133 {
1134 #ifdef DEBUG_DSHOW_L1
1135     msg_Dbg( p_input, "CaptureEnumMediaTypes::~CaptureEnumMediaTypes" );
1136 #endif
1137     FreeMediaType(cx_media_type);
1138     p_pin->Release();
1139 }
1140
1141 /* IUnknown methods */
1142 STDMETHODIMP CaptureEnumMediaTypes::QueryInterface( REFIID riid, void **ppv )
1143 {
1144 #ifdef DEBUG_DSHOW_L1
1145     msg_Dbg( p_input, "CaptureEnumMediaTypes::QueryInterface" );
1146 #endif
1147
1148     if( riid == IID_IUnknown ||
1149         riid == IID_IEnumMediaTypes )
1150     {
1151         AddRef();
1152         *ppv = (IEnumMediaTypes *)this;
1153         return NOERROR;
1154     }
1155     else
1156     {
1157         *ppv = NULL;
1158         return E_NOINTERFACE;
1159     }
1160 };
1161 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::AddRef()
1162 {
1163 #ifdef DEBUG_DSHOW_L1
1164     msg_Dbg( p_input, "CaptureEnumMediaTypes::AddRef (ref: %i)", i_ref );
1165 #endif
1166
1167     return i_ref++;
1168 };
1169 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::Release()
1170 {
1171 #ifdef DEBUG_DSHOW_L1
1172     msg_Dbg( p_input, "CaptureEnumMediaTypes::Release (ref: %i)", i_ref );
1173 #endif
1174
1175     if( !InterlockedDecrement(&i_ref) ) delete this;
1176
1177     return 0;
1178 };
1179
1180 /* IEnumMediaTypes */
1181 STDMETHODIMP CaptureEnumMediaTypes::Next( ULONG cMediaTypes,
1182                                           AM_MEDIA_TYPE ** ppMediaTypes,
1183                                           ULONG * pcFetched )
1184 {
1185 #ifdef DEBUG_DSHOW_L1
1186     msg_Dbg( p_input, "CaptureEnumMediaTypes::Next " );
1187 #endif
1188     ULONG copied = 0;
1189     ULONG offset = 0;
1190     ULONG max = p_pin->media_type_count;
1191
1192     if( ! ppMediaTypes ) 
1193         return E_POINTER;
1194
1195     if( (! pcFetched)  && (cMediaTypes > 1) )
1196        return E_POINTER;
1197
1198     /*
1199     ** use connection media type as first entry in iterator if it exists
1200     */
1201     copied = 0;
1202     if( cx_media_type.subtype != GUID_NULL )
1203     {
1204         ++max;
1205         if( i_position == 0 )
1206         {
1207             ppMediaTypes[copied] =
1208                 (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1209             if( CopyMediaType(ppMediaTypes[copied], &cx_media_type) != S_OK )
1210                 return E_OUTOFMEMORY;
1211             ++i_position; 
1212             ++copied;
1213         }
1214     }
1215
1216     while( (copied < cMediaTypes) && (i_position < max)  )
1217     {
1218         ppMediaTypes[copied] =
1219             (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1220         if( CopyMediaType( ppMediaTypes[copied],
1221                            &p_pin->media_types[i_position-offset]) != S_OK )
1222             return E_OUTOFMEMORY;
1223
1224         ++copied;
1225         ++i_position; 
1226     }
1227
1228     if( pcFetched )  *pcFetched = copied;
1229
1230     return (copied == cMediaTypes) ? S_OK : S_FALSE;
1231 };
1232 STDMETHODIMP CaptureEnumMediaTypes::Skip( ULONG cMediaTypes )
1233 {
1234     ULONG max =  p_pin->media_type_count;
1235     if( cx_media_type.subtype != GUID_NULL )
1236     {
1237         max = 1;
1238     }
1239 #ifdef DEBUG_DSHOW_L1
1240     msg_Dbg( p_input, "CaptureEnumMediaTypes::Skip" );
1241 #endif
1242
1243     i_position += cMediaTypes;
1244     return (i_position < max) ? S_OK : S_FALSE;
1245 };
1246 STDMETHODIMP CaptureEnumMediaTypes::Reset()
1247 {
1248 #ifdef DEBUG_DSHOW_L1
1249     msg_Dbg( p_input, "CaptureEnumMediaTypes::Reset" );
1250 #endif
1251
1252     FreeMediaType(cx_media_type);
1253     CopyMediaType(&cx_media_type, &p_pin->cx_media_type); 
1254     i_position = 0;
1255     return S_OK;
1256 };
1257 STDMETHODIMP CaptureEnumMediaTypes::Clone( IEnumMediaTypes **ppEnum )
1258 {
1259 #ifdef DEBUG_DSHOW_L1
1260     msg_Dbg( p_input, "CaptureEnumMediaTypes::Clone" );
1261 #endif
1262
1263     *ppEnum = new CaptureEnumMediaTypes( p_input, p_pin, this );
1264     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
1265
1266     return NOERROR;
1267 };