]> git.sesse.net Git - vlc/blob - modules/access/dshow/filter.cpp
- capture filter supports more than one media types
[vlc] / modules / access / dshow / filter.cpp
1 /*****************************************************************************
2  * filter.c : DirectShow access module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id$
6  *
7  * Author: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/vout.h>
34
35 #ifndef _MSC_VER
36     /* Work-around a bug in w32api-2.5 */
37 #   define QACONTAINERFLAGS QACONTAINERFLAGS_SOMETHINGELSE
38 #endif
39
40 #include "filter.h"
41
42 #define DEBUG_DSHOW 1
43
44 struct access_sys_t
45 {
46     vlc_mutex_t lock;
47     vlc_cond_t  wait;
48 };
49
50
51 #define FILTER_NAME  L"VideoLAN Capture Filter"
52 #define PIN_NAME     L"Capture"
53
54 /*****************************************************************************
55  * DirectShow GUIDs.
56  * Easier to define them hear as mingw doesn't provide them all.
57  *****************************************************************************/
58 const GUID CLSID_SystemDeviceEnum = {0x62be5d10, 0x60eb, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
59 const GUID CLSID_VideoInputDeviceCategory = {0x860BB310,0x5D01,0x11d0,{0xBD,0x3B,0x00,0xA0,0xC9,0x11,0xCE,0x86}};
60 const GUID CLSID_AudioInputDeviceCategory = {0x33d9a762, 0x90c8, 0x11d0, {0xbd, 0x43, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
61 const GUID IID_IPropertyBag = {0x55272A00, 0x42CB, 0x11CE, {0x81, 0x35, 0x00, 0xAA, 0x00, 0x4B, 0xB8, 0x51}};
62 const GUID IID_ICreateDevEnum = {0x29840822, 0x5b84, 0x11d0, {0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86}};
63 const GUID IID_IFilterGraph = {0x56a8689f, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
64 const GUID IID_IMediaControl = {0x56a868b1, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
65 const GUID CLSID_FilterGraph = {0xe436ebb3, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
66
67 const GUID IID_IUnknown = {0x00000000, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
68 const GUID IID_IPersist = {0x0000010c, 0x0000, 0x0000, {0xc0,0x00, 0x00,0x00,0x00,0x00,0x00,0x46}};
69 const GUID IID_IMediaFilter = {0x56a86899, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
70 const GUID IID_IBaseFilter = {0x56a86895, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
71 const GUID IID_IPin = {0x56a86891, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
72 const GUID IID_IMemInputPin = {0x56a8689d, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
73
74 const GUID IID_IEnumPins = {0x56a86892, 0x0ad4, 0x11ce, {0xb0,0x3a, 0x00,0x20,0xaf,0x0b,0xa7,0x70}};
75 const GUID IID_IEnumMediaTypes = {0x89c31040, 0x846b, 0x11ce, {0x97,0xd3, 0x00,0xaa,0x00,0x55,0x59,0x5a}};
76
77 const GUID IID_IAMBufferNegotiation = {0x56ed71a0, 0xaf5f, 0x11d0, {0xb3, 0xf0, 0x00, 0xaa, 0x00, 0x37, 0x61, 0xc5}};
78
79 const GUID IID_ISpecifyPropertyPages = {0xb196b28b, 0xbab4, 0x101a, {0xb6, 0x9c, 0x00, 0xaa, 0x00, 0x34, 0x1d, 0x07}};
80
81 const GUID IID_IQualityControl = {0x56a868a5, 0x0ad4, 0x11ce, {0xb, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
82
83 const GUID CLSID_CaptureGraphBuilder2 = {0xBF87B6E1, 0x8C27, 0x11d0, {0xB3, 0xF0, 0x0, 0xAA, 0x00, 0x37, 0x61, 0xC5}};
84
85 const GUID IID_IGraphBuilder = {0x56a868a9, 0x0ad4, 0x11ce, {0xb0, 0x3a, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}};
86
87 const GUID IID_ICaptureGraphBuilder2 = {0x93E5A4E0, 0x2D50, 0x11d2, {0xAB, 0xFA, 0x00, 0xA0, 0xC9, 0xC6, 0xE3, 0x8D}};
88
89 const GUID IID_IAMTVAudio = {0x83EC1C30, 0x23D1, 0x11d1, {0x99, 0xE6, 0x00, 0xA0, 0xC9, 0x56, 0x02, 0x66}};
90 const GUID IID_IAMStreamConfig = {0xC6E13340, 0x30AC, 0x11d0, {0xA1, 0x8C, 0x00, 0xA0, 0xC9, 0x11, 0x89, 0x56}};
91 const GUID IID_IAMCrossbar = {0xC6E13380, 0x30AC, 0x11d0, {0xA1, 0x8C, 0x00, 0xA0, 0xC9, 0x11, 0x89, 0x56}};
92 const GUID IID_IAMTVTuner = {0x211A8766, 0x03AC, 0x11d1, {0x8D, 0x13, 0x00, 0xAA, 0x00, 0xBD, 0x83, 0x39}};
93
94 const GUID IID_IKsPropertySet = {0x31EFAC30, 0x515C, 0x11d0, {0xA9, 0xAA, 0x00, 0xAA, 0x00, 0x61, 0xBE, 0x93}};
95
96
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 /* Analog Video */
147 const GUID FORMAT_AnalogVideo = {0x482dde0, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
148
149 const GUID MEDIATYPE_AnalogVideo = {0x482dde1, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xab, 0x0, 0x6e, 0xcb, 0x65}};
150
151 const GUID MEDIASUBTYPE_AnalogVideo_NTSC_M = {0x482dde2, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
152 const GUID MEDIASUBTYPE_AnalogVideo_PAL_B = {0x482dde5, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
153 const GUID MEDIASUBTYPE_AnalogVideo_PAL_D = {0x482dde6, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
154 const GUID MEDIASUBTYPE_AnalogVideo_PAL_G = {0x482dde7, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
155 const GUID MEDIASUBTYPE_AnalogVideo_PAL_H = {0x482dde8, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
156 const GUID MEDIASUBTYPE_AnalogVideo_PAL_I = {0x482dde9, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
157 const GUID MEDIASUBTYPE_AnalogVideo_PAL_M = {0x482ddea, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
158 const GUID MEDIASUBTYPE_AnalogVideo_PAL_N = {0x482ddeb, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
159 const GUID MEDIASUBTYPE_AnalogVideo_PAL_N_COMBO = {0x482ddec, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
160 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_B = {0x482ddf0, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
161 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_D = {0x482ddf1, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
162 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_G = {0x482ddf2, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
163 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_H = {0x482ddf3, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
164 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_K = {0x482ddf4, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
165 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_K1 = {0x482ddf5, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
166 const GUID MEDIASUBTYPE_AnalogVideo_SECAM_L = {0x482ddf6, 0x7817, 0x11cf, {0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65}};
167
168 const GUID AMPROPSETID_Pin= {0x9b00f101, 0x1567, 0x11d1, {0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc5}};
169 const GUID PIN_CATEGORY_ANALOGVIDEOIN= {0xfb6c4283, 0x0353, 0x11d1, {0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba}};
170 const GUID PIN_CATEGORY_CAPTURE= {0xfb6c4281, 0x0353, 0x11d1, {0x90, 0x5f, 0x0, 0x0, 0xc0, 0xcc, 0x16, 0xba}};
171 const GUID LOOK_UPSTREAM_ONLY= {0xac798be0, 0x98e3, 0x11d1, {0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc}};
172
173 const GUID GUID_NULL = {0x0000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
174
175 void WINAPI FreeMediaType( AM_MEDIA_TYPE& mt )
176 {
177     if( mt.cbFormat != 0 )
178     {
179         CoTaskMemFree( (PVOID)mt.pbFormat );
180         mt.cbFormat = 0;
181         mt.pbFormat = NULL;
182     }
183     if( mt.pUnk != NULL )
184     {
185         mt.pUnk->Release();
186         mt.pUnk = NULL;
187     }
188 }
189
190 HRESULT WINAPI CopyMediaType( AM_MEDIA_TYPE *pmtTarget,
191                               const AM_MEDIA_TYPE *pmtSource )
192 {
193     *pmtTarget = *pmtSource;
194     if( pmtSource->cbFormat != 0 )
195     {
196         pmtTarget->pbFormat = (PBYTE)CoTaskMemAlloc( pmtSource->cbFormat );
197         if( pmtTarget->pbFormat == NULL )
198         {
199             pmtTarget->cbFormat = 0;
200             return E_OUTOFMEMORY;
201         }
202         else
203         {
204             CopyMemory( (PVOID)pmtTarget->pbFormat, (PVOID)pmtSource->pbFormat,
205                         pmtTarget->cbFormat );
206         }
207     }
208     if( pmtTarget->pUnk != NULL )
209     {
210         pmtTarget->pUnk->AddRef();
211     }
212
213     return S_OK;
214 }
215
216 int GetFourCCFromMediaType(const AM_MEDIA_TYPE &media_type)
217 {
218     int i_fourcc = 0;
219
220     if( media_type.majortype == MEDIATYPE_Video )
221     {
222         /* Packed RGB formats */
223         if( media_type.subtype == MEDIASUBTYPE_RGB1 )
224            i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '1' );
225         else if( media_type.subtype == MEDIASUBTYPE_RGB4 )
226            i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '4' );
227         else if( media_type.subtype == MEDIASUBTYPE_RGB8 )
228            i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '8' );
229         else if( media_type.subtype == MEDIASUBTYPE_RGB555 )
230            i_fourcc = VLC_FOURCC( 'R', 'V', '1', '5' );
231         else if( media_type.subtype == MEDIASUBTYPE_RGB565 )
232            i_fourcc = VLC_FOURCC( 'R', 'V', '1', '6' );
233         else if( media_type.subtype == MEDIASUBTYPE_RGB24 )
234            i_fourcc = VLC_FOURCC( 'R', 'V', '2', '4' );
235         else if( media_type.subtype == MEDIASUBTYPE_RGB32 )
236            i_fourcc = VLC_FOURCC( 'R', 'V', '3', '2' );
237         else if( media_type.subtype == MEDIASUBTYPE_ARGB32 )
238            i_fourcc = VLC_FOURCC( 'R', 'G', 'B', 'A' );
239
240         /* Planar YUV formats */
241         else if( media_type.subtype == MEDIASUBTYPE_I420 )
242            i_fourcc = VLC_FOURCC( 'I', '4', '2', '0' );
243         else if( media_type.subtype == MEDIASUBTYPE_Y41P )
244            i_fourcc = VLC_FOURCC( 'I', '4', '1', '1' );
245         else if( media_type.subtype == MEDIASUBTYPE_YV12 )
246            i_fourcc = VLC_FOURCC( 'Y', 'V', '1', '2' );
247         else if( media_type.subtype == MEDIASUBTYPE_IYUV )
248            i_fourcc = VLC_FOURCC( 'Y', 'V', '1', '2' );
249         else if( media_type.subtype == MEDIASUBTYPE_YVU9 )
250            i_fourcc = VLC_FOURCC( 'Y', 'V', 'U', '9' );
251
252         /* Packed YUV formats */
253         else if( media_type.subtype == MEDIASUBTYPE_YVYU )
254            i_fourcc = VLC_FOURCC( 'Y', 'V', 'Y', 'U' );
255         else if( media_type.subtype == MEDIASUBTYPE_YUYV )
256            i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', '2' );
257         else if( media_type.subtype == MEDIASUBTYPE_Y411 )
258            i_fourcc = VLC_FOURCC( 'I', '4', '1', 'N' );
259         else if( media_type.subtype == MEDIASUBTYPE_Y211 )
260            i_fourcc = VLC_FOURCC( 'Y', '2', '1', '1' );
261         else if( media_type.subtype == MEDIASUBTYPE_YUY2 )
262            i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', '2' );
263         else if( media_type.subtype == MEDIASUBTYPE_UYVY )
264            i_fourcc = VLC_FOURCC( 'U', 'Y', 'V', 'Y' );
265
266         /* MPEG2 video elementary stream */
267         else if( media_type.subtype == MEDIASUBTYPE_MPEG2_VIDEO )
268            i_fourcc = VLC_FOURCC( 'm', 'p', '2', 'v' );
269
270         /* DV formats */
271         else if( media_type.subtype == MEDIASUBTYPE_dvsl )
272            i_fourcc = VLC_FOURCC( 'd', 'v', 's', 'l' );
273         else if( media_type.subtype == MEDIASUBTYPE_dvsd )
274            i_fourcc = VLC_FOURCC( 'd', 'v', 's', 'd' );
275         else if( media_type.subtype == MEDIASUBTYPE_dvhd )
276            i_fourcc = VLC_FOURCC( 'd', 'v', 'h', 'd' );
277     }
278     else if( media_type.majortype == MEDIATYPE_Audio )
279     {
280         if( media_type.formattype == FORMAT_WaveFormatEx )
281         {
282             if( media_type.subtype == MEDIASUBTYPE_PCM )
283                 i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
284             else if( media_type.subtype == MEDIASUBTYPE_IEEE_FLOAT )
285                 i_fourcc = VLC_FOURCC( 'f', 'l', '3', '2' );
286         }
287     }
288     else if( media_type.majortype == MEDIATYPE_Stream )
289         {
290         if( media_type.subtype == MEDIASUBTYPE_MPEG2_PROGRAM )
291                    i_fourcc = VLC_FOURCC( 'm', 'p', '2', 'p' );
292         else if( media_type.subtype == MEDIASUBTYPE_MPEG2_TRANSPORT )
293                    i_fourcc = VLC_FOURCC( 'm', 'p', '2', 't' );
294     }
295     return i_fourcc;
296 }
297
298 /****************************************************************************
299  * Implementation of our dummy directshow filter pin class
300  ****************************************************************************/
301
302 CapturePin::CapturePin( input_thread_t * _p_input, CaptureFilter *_p_filter,
303                         AM_MEDIA_TYPE *mt, size_t mt_count )
304   : p_input( _p_input ), p_filter( _p_filter ), p_connected_pin( NULL ),
305     media_types(mt), media_type_count(mt_count), i_ref( 1 )
306 {
307     cx_media_type.majortype = mt[0].majortype;
308     cx_media_type.subtype   = GUID_NULL;
309     cx_media_type.pbFormat  = NULL;
310     cx_media_type.pUnk      = NULL;
311 }
312
313 CapturePin::~CapturePin()
314 {
315 #ifdef DEBUG_DSHOW
316     msg_Dbg( p_input, "CapturePin::~CapturePin" );
317 #endif
318     for( size_t c=0; c<media_type_count; c++ )
319     {
320         FreeMediaType(media_types[c]);
321     }
322     FreeMediaType(cx_media_type);
323 }
324
325 HRESULT CapturePin::CustomGetSample( VLCMediaSample *vlc_sample )
326 {
327 #if 0 //def DEBUG_DSHOW
328     msg_Dbg( p_input, "CapturePin::CustomGetSample" );
329 #endif
330
331     access_sys_t *p_sys = p_input->p_access_data;
332     vlc_mutex_lock( &p_sys->lock );
333     if( samples_queue.size() )
334     {
335         *vlc_sample = samples_queue.back();
336         samples_queue.pop_back();
337         vlc_mutex_unlock( &p_sys->lock );
338         return S_OK;
339     }
340     vlc_mutex_unlock( &p_sys->lock );
341     return S_FALSE;
342 }
343
344 AM_MEDIA_TYPE &CapturePin::CustomGetMediaType()
345 {
346     return cx_media_type;
347 }
348
349 /* IUnknown methods */
350 STDMETHODIMP CapturePin::QueryInterface(REFIID riid, void **ppv)
351 {
352 #ifdef DEBUG_DSHOW
353     msg_Dbg( p_input, "CapturePin::QueryInterface" );
354 #endif
355
356     if( riid == IID_IUnknown ||
357         riid == IID_IPin )
358     {
359         AddRef();
360         *ppv = (IPin *)this;
361         return NOERROR;
362     }
363     if( riid == IID_IMemInputPin )
364     {
365         AddRef();
366         *ppv = (IMemInputPin *)this;
367         return NOERROR;
368     }
369     else
370     {
371 #ifdef DEBUG_DSHOW
372         msg_Dbg( p_input, "CapturePin::QueryInterface() failed for: "
373                  "%04X-%02X-%02X-%02X%02X%02X%02X%02X%02X%02X%02X",
374                  (int)riid.Data1, (int)riid.Data2, (int)riid.Data3,
375                  static_cast<int>(riid.Data4[0]), (int)riid.Data4[1],
376                  (int)riid.Data4[2], (int)riid.Data4[3],
377                  (int)riid.Data4[4], (int)riid.Data4[5],
378                  (int)riid.Data4[6], (int)riid.Data4[7] );
379 #endif
380         *ppv = NULL;
381         return E_NOINTERFACE;
382     }
383 }
384
385 STDMETHODIMP_(ULONG) CapturePin::AddRef()
386 {
387 #ifdef DEBUG_DSHOW_L1
388     msg_Dbg( p_input, "CapturePin::AddRef (ref: %i)", i_ref );
389 #endif
390
391     return i_ref++;
392 };
393 STDMETHODIMP_(ULONG) CapturePin::Release()
394 {
395 #ifdef DEBUG_DSHOW_L1
396     msg_Dbg( p_input, "CapturePin::Release (ref: %i)", i_ref );
397 #endif
398
399     if( !InterlockedDecrement(&i_ref) ) delete this;
400
401     return 0;
402 };
403
404 /* IPin methods */
405 STDMETHODIMP CapturePin::Connect( IPin * pReceivePin,
406                                   const AM_MEDIA_TYPE *pmt )
407 {
408 #ifdef DEBUG_DSHOW
409     msg_Dbg( p_input, "CapturePin::Connect" );
410 #endif
411     if( State_Stopped == p_filter->state )
412     {
413         if( ! p_connected_pin )
414         {
415             if( ! pmt )
416                 return S_OK;
417                 
418             if( (GUID_NULL != pmt->majortype) && (media_types[0].majortype != pmt->majortype) )
419                 return S_FALSE;
420
421             if( (GUID_NULL != pmt->subtype) && (! GetFourCCFromMediaType(*pmt)) )
422                 return S_FALSE;
423
424             if( pmt->pbFormat )
425             {
426                 if( pmt->majortype == MEDIATYPE_Video )
427                 {
428                     if( (((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight == 0) )
429                         return S_FALSE;
430                     if( (((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth == 0) )
431                         return S_FALSE;
432
433                 }
434             }
435             return S_OK;
436         }
437         return VFW_E_ALREADY_CONNECTED;
438     }
439     return VFW_E_NOT_STOPPED;
440 }
441 STDMETHODIMP CapturePin::ReceiveConnection( IPin * pConnector,
442                                             const AM_MEDIA_TYPE *pmt )
443 {
444 #ifdef DEBUG_DSHOW
445     msg_Dbg( p_input, "CapturePin::ReceiveConnection" );
446 #endif
447
448     if( State_Stopped != p_filter->state )
449         return VFW_E_NOT_STOPPED;
450
451     if( ! pmt )
452         return E_POINTER;
453
454     if( p_connected_pin )
455         return VFW_E_ALREADY_CONNECTED;
456
457     if( S_OK != QueryAccept(pmt) )
458         return VFW_E_TYPE_NOT_ACCEPTED;
459
460     p_connected_pin = pConnector;
461     p_connected_pin->AddRef();
462
463     FreeMediaType( cx_media_type );
464     return CopyMediaType( &cx_media_type, pmt );
465 }
466 STDMETHODIMP CapturePin::Disconnect()
467 {
468 #ifdef DEBUG_DSHOW
469     msg_Dbg( p_input, "CapturePin::Disconnect" );
470 #endif
471
472     if( ! p_connected_pin ) return S_FALSE;
473
474 #if 0 // FIXME: This does seem to create crashes sometimes
475     VLCMediaSample vlc_sample;
476     access_sys_t *p_sys = p_input->p_access_data;
477
478     vlc_mutex_lock( &p_sys->lock );
479     while( samples_queue.size() )
480     {
481         vlc_sample = samples_queue.back();
482         samples_queue.pop_back();
483         vlc_sample.p_sample->Release();
484     }
485     vlc_mutex_unlock( &p_sys->lock );
486 #endif
487
488     p_connected_pin->Release();
489     p_connected_pin = NULL;
490     FreeMediaType( cx_media_type );
491
492     return S_OK;
493 }
494 STDMETHODIMP CapturePin::ConnectedTo( IPin **pPin )
495 {
496 #ifdef DEBUG_DSHOW
497     msg_Dbg( p_input, "CapturePin::ConnectedTo" );
498 #endif
499
500     if( !p_connected_pin ) return VFW_E_NOT_CONNECTED;
501
502     p_connected_pin->AddRef();
503     *pPin = p_connected_pin;
504
505     return S_OK;
506 }
507 STDMETHODIMP CapturePin::ConnectionMediaType( AM_MEDIA_TYPE *pmt )
508 {
509 #ifdef DEBUG_DSHOW
510     msg_Dbg( p_input, "CapturePin::ConnectionMediaType" );
511 #endif
512
513     if( !p_connected_pin ) return VFW_E_NOT_CONNECTED;
514
515     return CopyMediaType( pmt, &cx_media_type );
516 }
517 STDMETHODIMP CapturePin::QueryPinInfo( PIN_INFO * pInfo )
518 {
519 #ifdef DEBUG_DSHOW
520     msg_Dbg( p_input, "CapturePin::QueryPinInfo" );
521 #endif
522
523     pInfo->pFilter = p_filter;
524     if( p_filter ) p_filter->AddRef();
525
526     memcpy(pInfo->achName, PIN_NAME, sizeof(PIN_NAME));
527     pInfo->dir = PINDIR_INPUT;
528
529     return NOERROR;
530 }
531 STDMETHODIMP CapturePin::QueryDirection( PIN_DIRECTION * pPinDir )
532 {
533 #ifdef DEBUG_DSHOW
534     msg_Dbg( p_input, "CapturePin::QueryDirection" );
535 #endif
536
537     *pPinDir = PINDIR_INPUT;
538     return NOERROR;
539 }
540 STDMETHODIMP CapturePin::QueryId( LPWSTR * Id )
541 {
542 #ifdef DEBUG_DSHOW
543     msg_Dbg( p_input, "CapturePin::QueryId" );
544 #endif
545
546     *Id = L"VideoLAN Capture Pin";
547
548     return S_OK;
549 }
550 STDMETHODIMP CapturePin::QueryAccept( const AM_MEDIA_TYPE *pmt )
551 {
552 #ifdef DEBUG_DSHOW
553     msg_Dbg( p_input, "CapturePin::QueryAccept" );
554 #endif
555     if( State_Stopped == p_filter->state )
556     {
557         if( media_types[0].majortype == pmt->majortype )
558         {
559             if( GetFourCCFromMediaType(*pmt) )
560             {
561                 if( pmt->majortype == MEDIATYPE_Video )
562                     if( pmt->pbFormat &&
563                         ((((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biHeight == 0) ||
564                          (((VIDEOINFOHEADER *)pmt->pbFormat)->bmiHeader.biWidth == 0)) )
565                         return S_FALSE;
566
567                 if( !p_connected_pin )
568                     return S_OK;
569
570                 FreeMediaType( cx_media_type );
571                 return CopyMediaType( &cx_media_type, pmt );
572             }
573         }
574     }
575     return S_FALSE;
576 }
577 STDMETHODIMP CapturePin::EnumMediaTypes( IEnumMediaTypes **ppEnum )
578 {
579 #ifdef DEBUG_DSHOW
580     msg_Dbg( p_input, "CapturePin::EnumMediaTypes" );
581 #endif
582
583     *ppEnum = new CaptureEnumMediaTypes( p_input, this, NULL );
584
585     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
586
587     return NOERROR;
588 }
589 STDMETHODIMP CapturePin::QueryInternalConnections( IPin* *apPin, ULONG *nPin )
590 {
591 #ifdef DEBUG_DSHOW
592     msg_Dbg( p_input, "CapturePin::QueryInternalConnections" );
593 #endif
594     return E_NOTIMPL;
595 }
596 STDMETHODIMP CapturePin::EndOfStream( void )
597 {
598 #ifdef DEBUG_DSHOW
599     msg_Dbg( p_input, "CapturePin::EndOfStream" );
600 #endif
601     return S_OK;
602 }
603 STDMETHODIMP CapturePin::BeginFlush( void )
604 {
605 #ifdef DEBUG_DSHOW
606     msg_Dbg( p_input, "CapturePin::BeginFlush" );
607 #endif
608     return S_OK;
609 }
610 STDMETHODIMP CapturePin::EndFlush( void )
611 {
612 #ifdef DEBUG_DSHOW
613     msg_Dbg( p_input, "CapturePin::EndFlush" );
614 #endif
615     return S_OK;
616 }
617 STDMETHODIMP CapturePin::NewSegment( REFERENCE_TIME tStart,
618                                      REFERENCE_TIME tStop,
619                                      double dRate )
620 {
621 #ifdef DEBUG_DSHOW
622     msg_Dbg( p_input, "CapturePin::NewSegment" );
623 #endif
624     return S_OK;
625 }
626
627 /* IMemInputPin methods */
628 STDMETHODIMP CapturePin::GetAllocator( IMemAllocator **ppAllocator )
629 {
630 #ifdef DEBUG_DSHOW
631     msg_Dbg( p_input, "CapturePin::GetAllocator" );
632 #endif
633
634     return VFW_E_NO_ALLOCATOR;
635 }
636 STDMETHODIMP CapturePin::NotifyAllocator( IMemAllocator *pAllocator,
637                                           BOOL bReadOnly )
638 {
639 #ifdef DEBUG_DSHOW
640     msg_Dbg( p_input, "CapturePin::NotifyAllocator" );
641 #endif
642
643     return S_OK;
644 }
645 STDMETHODIMP CapturePin::GetAllocatorRequirements( ALLOCATOR_PROPERTIES *pProps )
646 {
647 #ifdef DEBUG_DSHOW
648     msg_Dbg( p_input, "CapturePin::GetAllocatorRequirements" );
649 #endif
650
651     return E_NOTIMPL;
652 }
653 STDMETHODIMP CapturePin::Receive( IMediaSample *pSample )
654 {
655 #if 0 //def DEBUG_DSHOW
656     msg_Dbg( p_input, "CapturePin::Receive" );
657 #endif
658
659     pSample->AddRef();
660     mtime_t i_timestamp = mdate() * 10;
661     VLCMediaSample vlc_sample = {pSample, i_timestamp};
662
663     access_sys_t *p_sys = p_input->p_access_data;
664     vlc_mutex_lock( &p_sys->lock );
665     samples_queue.push_front( vlc_sample );
666
667     /* Make sure we don't cache too many samples */
668     if( samples_queue.size() > 10 )
669     {
670         vlc_sample = samples_queue.back();
671         samples_queue.pop_back();
672         msg_Dbg( p_input, "CapturePin::Receive trashing late input sample" );
673         vlc_sample.p_sample->Release();
674     }
675
676     vlc_cond_signal( &p_sys->wait );
677     vlc_mutex_unlock( &p_sys->lock );
678
679     return S_OK;
680 }
681 STDMETHODIMP CapturePin::ReceiveMultiple( IMediaSample **pSamples,
682                                           long nSamples,
683                                           long *nSamplesProcessed )
684 {
685     HRESULT hr = S_OK;
686
687     *nSamplesProcessed = 0;
688     while( nSamples-- > 0 )
689     {
690          hr = Receive( pSamples[*nSamplesProcessed] );
691          if( hr != S_OK ) break;
692          (*nSamplesProcessed)++;
693     }
694     return hr;
695 }
696 STDMETHODIMP CapturePin::ReceiveCanBlock( void )
697 {
698 #ifdef DEBUG_DSHOW
699     msg_Dbg( p_input, "CapturePin::ReceiveCanBlock" );
700 #endif
701
702     return S_FALSE; /* Thou shalt not block */
703 }
704
705 /****************************************************************************
706  * Implementation of our dummy directshow filter class
707  ****************************************************************************/
708
709 CaptureFilter::CaptureFilter( input_thread_t * _p_input, AM_MEDIA_TYPE *mt, size_t mt_count )
710   : p_input( _p_input ), p_pin( new CapturePin( _p_input, this, mt, mt_count ) ),
711     state( State_Stopped ), i_ref( 1 )
712 {
713 }
714
715 CaptureFilter::~CaptureFilter()
716 {
717 #ifdef DEBUG_DSHOW
718     msg_Dbg( p_input, "CaptureFilter::~CaptureFilter" );
719 #endif
720     p_pin->Release();
721 }
722
723 /* IUnknown methods */
724 STDMETHODIMP CaptureFilter::QueryInterface( REFIID riid, void **ppv )
725 {
726 #ifdef DEBUG_DSHOW
727     msg_Dbg( p_input, "CaptureFilter::QueryInterface" );
728 #endif
729
730     if( riid == IID_IUnknown )
731     {
732         AddRef();
733         *ppv = (IUnknown *)this;
734         return NOERROR;
735     }
736     if( riid == IID_IPersist )
737     {
738         AddRef();
739         *ppv = (IPersist *)this;
740         return NOERROR;
741     }
742     if( riid == IID_IMediaFilter )
743     {
744         AddRef();
745         *ppv = (IMediaFilter *)this;
746         return NOERROR;
747     }
748     if( riid == IID_IBaseFilter )
749     {
750         AddRef();
751         *ppv = (IBaseFilter *)this;
752         return NOERROR;
753     }
754     else
755     {
756 #ifdef DEBUG_DSHOW
757         msg_Dbg( p_input, "CaptureFilter::QueryInterface() failed for: "
758                  "%04X-%02X-%02X-%02X%02X%02X%02X%02X%02X%02X%02X",
759                  (int)riid.Data1, (int)riid.Data2, (int)riid.Data3,
760                  static_cast<int>(riid.Data4[0]), (int)riid.Data4[1],
761                  (int)riid.Data4[2], (int)riid.Data4[3],
762                  (int)riid.Data4[4], (int)riid.Data4[5],
763                  (int)riid.Data4[6], (int)riid.Data4[7] );
764 #endif
765         *ppv = NULL;
766         return E_NOINTERFACE;
767     }
768 };
769 STDMETHODIMP_(ULONG) CaptureFilter::AddRef()
770 {
771 #ifdef DEBUG_DSHOW_L1
772     msg_Dbg( p_input, "CaptureFilter::AddRef (ref: %i)", i_ref );
773 #endif
774
775     return i_ref++;
776 };
777 STDMETHODIMP_(ULONG) CaptureFilter::Release()
778 {
779 #ifdef DEBUG_DSHOW_L1
780     msg_Dbg( p_input, "CaptureFilter::Release (ref: %i)", i_ref );
781 #endif
782
783     if( !InterlockedDecrement(&i_ref) ) delete this;
784
785     return 0;
786 };
787
788 /* IPersist method */
789 STDMETHODIMP CaptureFilter::GetClassID(CLSID *pClsID)
790 {
791 #ifdef DEBUG_DSHOW
792     msg_Dbg( p_input, "CaptureFilter::GetClassID" );
793 #endif
794     return E_NOTIMPL;
795 };
796
797 /* IMediaFilter methods */
798 STDMETHODIMP CaptureFilter::GetState(DWORD dwMSecs, FILTER_STATE *State)
799 {
800 #ifdef DEBUG_DSHOW
801     msg_Dbg( p_input, "CaptureFilter::GetState %i", state );
802 #endif
803
804     *State = state;
805     return S_OK;
806 };
807 STDMETHODIMP CaptureFilter::SetSyncSource(IReferenceClock *pClock)
808 {
809 #ifdef DEBUG_DSHOW
810     msg_Dbg( p_input, "CaptureFilter::SetSyncSource" );
811 #endif
812
813     return S_OK;
814 };
815 STDMETHODIMP CaptureFilter::GetSyncSource(IReferenceClock **pClock)
816 {
817 #ifdef DEBUG_DSHOW
818     msg_Dbg( p_input, "CaptureFilter::GetSyncSource" );
819 #endif
820
821     *pClock = NULL;
822     return NOERROR;
823 };
824 STDMETHODIMP CaptureFilter::Stop()
825 {
826 #ifdef DEBUG_DSHOW
827     msg_Dbg( p_input, "CaptureFilter::Stop" );
828 #endif
829
830     state = State_Stopped;
831     return S_OK;
832 };
833 STDMETHODIMP CaptureFilter::Pause()
834 {
835 #ifdef DEBUG_DSHOW
836     msg_Dbg( p_input, "CaptureFilter::Pause" );
837 #endif
838
839     state = State_Paused;
840     return S_OK;
841 };
842 STDMETHODIMP CaptureFilter::Run(REFERENCE_TIME tStart)
843 {
844 #ifdef DEBUG_DSHOW
845     msg_Dbg( p_input, "CaptureFilter::Run" );
846 #endif
847
848     state = State_Running;
849     return S_OK;
850 };
851
852 /* IBaseFilter methods */
853 STDMETHODIMP CaptureFilter::EnumPins( IEnumPins ** ppEnum )
854 {
855 #ifdef DEBUG_DSHOW
856     msg_Dbg( p_input, "CaptureFilter::EnumPins" );
857 #endif
858
859     /* Create a new ref counted enumerator */
860     *ppEnum = new CaptureEnumPins( p_input, this, NULL );
861     return *ppEnum == NULL ? E_OUTOFMEMORY : NOERROR;
862 };
863 STDMETHODIMP CaptureFilter::FindPin( LPCWSTR Id, IPin ** ppPin )
864 {
865 #ifdef DEBUG_DSHOW
866     msg_Dbg( p_input, "CaptureFilter::FindPin" );
867 #endif
868     return E_NOTIMPL;
869 };
870 STDMETHODIMP CaptureFilter::QueryFilterInfo( FILTER_INFO * pInfo )
871 {
872 #ifdef DEBUG_DSHOW
873     msg_Dbg( p_input, "CaptureFilter::QueryFilterInfo" );
874 #endif
875
876     memcpy(pInfo->achName, FILTER_NAME, sizeof(FILTER_NAME));
877
878     pInfo->pGraph = p_graph;
879     if( p_graph ) p_graph->AddRef();
880
881     return NOERROR;
882 };
883 STDMETHODIMP CaptureFilter::JoinFilterGraph( IFilterGraph * pGraph,
884                                              LPCWSTR pName )
885 {
886 #ifdef DEBUG_DSHOW
887     msg_Dbg( p_input, "CaptureFilter::JoinFilterGraph" );
888 #endif
889
890     p_graph = pGraph;
891
892     return NOERROR;
893 };
894 STDMETHODIMP CaptureFilter::QueryVendorInfo( LPWSTR* pVendorInfo )
895 {
896 #ifdef DEBUG_DSHOW
897     msg_Dbg( p_input, "CaptureFilter::QueryVendorInfo" );
898 #endif
899     return E_NOTIMPL;
900 };
901
902 /* Custom methods */
903 CapturePin *CaptureFilter::CustomGetPin()
904 {
905     return p_pin;
906 }
907
908 /****************************************************************************
909  * Implementation of our dummy directshow enumpins class
910  ****************************************************************************/
911
912 CaptureEnumPins::CaptureEnumPins( input_thread_t * _p_input,
913                                   CaptureFilter *_p_filter,
914                                   CaptureEnumPins *pEnumPins )
915   : p_input( _p_input ), p_filter( _p_filter ), i_ref( 1 )
916 {
917     /* Hold a reference count on our filter */
918     p_filter->AddRef();
919
920     /* Are we creating a new enumerator */
921
922     if( pEnumPins == NULL )
923     {
924         i_position = 0;
925     }
926     else
927     {
928         i_position = pEnumPins->i_position;
929     }
930 }
931
932 CaptureEnumPins::~CaptureEnumPins()
933 {
934 #ifdef DEBUG_DSHOW
935     msg_Dbg( p_input, "CaptureEnumPins::~CaptureEnumPins" );
936 #endif
937     p_filter->Release();
938 }
939
940 /* IUnknown methods */
941 STDMETHODIMP CaptureEnumPins::QueryInterface( REFIID riid, void **ppv )
942 {
943 #ifdef DEBUG_DSHOW
944     msg_Dbg( p_input, "CaptureEnumPins::QueryInterface" );
945 #endif
946
947     if( riid == IID_IUnknown ||
948         riid == IID_IEnumPins )
949     {
950         AddRef();
951         *ppv = (IEnumPins *)this;
952         return NOERROR;
953     }
954     else
955     {
956         *ppv = NULL;
957         return E_NOINTERFACE;
958     }
959 };
960 STDMETHODIMP_(ULONG) CaptureEnumPins::AddRef()
961 {
962 #ifdef DEBUG_DSHOW_L1
963     msg_Dbg( p_input, "CaptureEnumPins::AddRef (ref: %i)", i_ref );
964 #endif
965
966     return i_ref++;
967 };
968 STDMETHODIMP_(ULONG) CaptureEnumPins::Release()
969 {
970 #ifdef DEBUG_DSHOW_L1
971     msg_Dbg( p_input, "CaptureEnumPins::Release (ref: %i)", i_ref );
972 #endif
973
974     if( !InterlockedDecrement(&i_ref) ) delete this;
975
976     return 0;
977 };
978
979 /* IEnumPins */
980 STDMETHODIMP CaptureEnumPins::Next( ULONG cPins, IPin ** ppPins,
981                                     ULONG * pcFetched )
982 {
983 #ifdef DEBUG_DSHOW
984     msg_Dbg( p_input, "CaptureEnumPins::Next" );
985 #endif
986
987     unsigned int i_fetched = 0;
988
989     if( i_position < 1 && cPins > 0 )
990     {
991         IPin *pPin = p_filter->CustomGetPin();
992         *ppPins = pPin;
993         pPin->AddRef();
994         i_fetched = 1;
995         i_position++;
996     }
997
998     if( pcFetched ) *pcFetched = i_fetched;
999
1000     return (i_fetched == cPins) ? S_OK : S_FALSE;
1001 };
1002 STDMETHODIMP CaptureEnumPins::Skip( ULONG cPins )
1003 {
1004 #ifdef DEBUG_DSHOW
1005     msg_Dbg( p_input, "CaptureEnumPins::Skip" );
1006 #endif
1007
1008     i_position += cPins;
1009
1010     if( i_position > 1 )
1011     {
1012         return S_FALSE;
1013     }
1014
1015     return S_OK;
1016 };
1017 STDMETHODIMP CaptureEnumPins::Reset()
1018 {
1019 #ifdef DEBUG_DSHOW
1020     msg_Dbg( p_input, "CaptureEnumPins::Reset" );
1021 #endif
1022
1023     i_position = 0;
1024     return S_OK;
1025 };
1026 STDMETHODIMP CaptureEnumPins::Clone( IEnumPins **ppEnum )
1027 {
1028 #ifdef DEBUG_DSHOW
1029     msg_Dbg( p_input, "CaptureEnumPins::Clone" );
1030 #endif
1031
1032     *ppEnum = new CaptureEnumPins( p_input, p_filter, this );
1033     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
1034
1035     return NOERROR;
1036 };
1037
1038 /****************************************************************************
1039  * Implementation of our dummy directshow enummediatypes class
1040  ****************************************************************************/
1041
1042 CaptureEnumMediaTypes::CaptureEnumMediaTypes( input_thread_t * _p_input,
1043                                   CapturePin *_p_pin,
1044                                   CaptureEnumMediaTypes *pEnumMediaTypes )
1045   : p_input( _p_input ), p_pin( _p_pin ), i_ref( 1 )
1046 {
1047     /* Hold a reference count on our filter */
1048     p_pin->AddRef();
1049
1050     /* Are we creating a new enumerator */
1051     if( pEnumMediaTypes == NULL )
1052     {
1053         i_position = 0;
1054     }
1055     else
1056     {
1057         i_position = pEnumMediaTypes->i_position;
1058     }
1059 }
1060
1061 CaptureEnumMediaTypes::~CaptureEnumMediaTypes()
1062 {
1063 #ifdef DEBUG_DSHOW
1064     msg_Dbg( p_input, "CaptureEnumMediaTypes::~CaptureEnumMediaTypes" );
1065 #endif
1066     p_pin->Release();
1067 }
1068
1069 /* IUnknown methods */
1070 STDMETHODIMP CaptureEnumMediaTypes::QueryInterface( REFIID riid, void **ppv )
1071 {
1072 #ifdef DEBUG_DSHOW
1073     msg_Dbg( p_input, "CaptureEnumMediaTypes::QueryInterface" );
1074 #endif
1075
1076     if( riid == IID_IUnknown ||
1077         riid == IID_IEnumMediaTypes )
1078     {
1079         AddRef();
1080         *ppv = (IEnumMediaTypes *)this;
1081         return NOERROR;
1082     }
1083     else
1084     {
1085         *ppv = NULL;
1086         return E_NOINTERFACE;
1087     }
1088 };
1089 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::AddRef()
1090 {
1091 #ifdef DEBUG_DSHOW_L1
1092     msg_Dbg( p_input, "CaptureEnumMediaTypes::AddRef (ref: %i)", i_ref );
1093 #endif
1094
1095     return i_ref++;
1096 };
1097 STDMETHODIMP_(ULONG) CaptureEnumMediaTypes::Release()
1098 {
1099 #ifdef DEBUG_DSHOW_L1
1100     msg_Dbg( p_input, "CaptureEnumMediaTypes::Release (ref: %i)", i_ref );
1101 #endif
1102
1103     if( !InterlockedDecrement(&i_ref) ) delete this;
1104
1105     return 0;
1106 };
1107
1108 /* IEnumMediaTypes */
1109 STDMETHODIMP CaptureEnumMediaTypes::Next( ULONG cMediaTypes,
1110                                           AM_MEDIA_TYPE ** ppMediaTypes,
1111                                           ULONG * pcFetched )
1112 {
1113 #ifdef DEBUG_DSHOW
1114     msg_Dbg( p_input, "CaptureEnumMediaTypes::Next" );
1115 #endif
1116     ULONG count;
1117
1118     if( ! ppMediaTypes ) 
1119         return E_POINTER;
1120
1121     if( (! pcFetched)  && (cMediaTypes > 1) )
1122        return E_POINTER;
1123
1124     count = 0;
1125
1126     while( (count < cMediaTypes) && (i_position < p_pin->media_type_count)  )
1127     {
1128         ppMediaTypes[count] = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1129         if( CopyMediaType(ppMediaTypes[count], &p_pin->media_types[i_position]) != S_OK )
1130             return E_OUTOFMEMORY;
1131
1132         count++;
1133         i_position++; 
1134     }
1135
1136     if( pcFetched ) 
1137     {
1138         *pcFetched = count;
1139     }
1140
1141     return (count == cMediaTypes) ? S_OK : S_FALSE;
1142 };
1143 STDMETHODIMP CaptureEnumMediaTypes::Skip( ULONG cMediaTypes )
1144 {
1145 #ifdef DEBUG_DSHOW
1146     msg_Dbg( p_input, "CaptureEnumMediaTypes::Skip" );
1147 #endif
1148
1149     i_position += cMediaTypes;
1150     return (i_position < p_pin->media_type_count) ? S_OK : S_FALSE;
1151 };
1152 STDMETHODIMP CaptureEnumMediaTypes::Reset()
1153 {
1154 #ifdef DEBUG_DSHOW
1155     msg_Dbg( p_input, "CaptureEnumMediaTypes::Reset" );
1156 #endif
1157
1158     i_position = 0;
1159     return S_OK;
1160 };
1161 STDMETHODIMP CaptureEnumMediaTypes::Clone( IEnumMediaTypes **ppEnum )
1162 {
1163 #ifdef DEBUG_DSHOW
1164     msg_Dbg( p_input, "CaptureEnumMediaTypes::Clone" );
1165 #endif
1166
1167     *ppEnum = new CaptureEnumMediaTypes( p_input, p_pin, this );
1168     if( *ppEnum == NULL ) return E_OUTOFMEMORY;
1169
1170     return NOERROR;
1171 };