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