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