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