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