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