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