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