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