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