lavu/opt: allow to set sample and pixel format with av_opt_set_int()
[ffmpeg.git] / libavdevice / dshow_capture.h
1 /*
2  * DirectShow capture interface
3  * Copyright (c) 2010 Ramiro Polla
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #ifndef AVDEVICE_DSHOW_H
23 #define AVDEVICE_DSHOW_H
24
25 #define DSHOWDEBUG 0
26
27 #include "avdevice.h"
28
29 #define COBJMACROS
30 #include <windows.h>
31 #define NO_DSHOW_STRSAFE
32 #include <dshow.h>
33 #include <dvdmedia.h>
34
35 long ff_copy_dshow_media_type(AM_MEDIA_TYPE *dst, const AM_MEDIA_TYPE *src);
36 void ff_print_VIDEO_STREAM_CONFIG_CAPS(const VIDEO_STREAM_CONFIG_CAPS *caps);
37 void ff_print_AUDIO_STREAM_CONFIG_CAPS(const AUDIO_STREAM_CONFIG_CAPS *caps);
38 void ff_print_AM_MEDIA_TYPE(const AM_MEDIA_TYPE *type);
39 void ff_printGUID(const GUID *g);
40
41 #if DSHOWDEBUG
42 extern const AVClass *ff_dshow_context_class_ptr;
43 #define dshowdebug(...) av_log(&ff_dshow_context_class_ptr, AV_LOG_DEBUG, __VA_ARGS__)
44 #else
45 #define dshowdebug(...)
46 #endif
47
48 static inline void nothing(void *foo)
49 {
50 }
51
52 struct GUIDoffset {
53     const GUID *iid;
54     int offset;
55 };
56
57 enum dshowDeviceType {
58     VideoDevice = 0,
59     AudioDevice = 1,
60 };
61
62 #define DECLARE_QUERYINTERFACE(class, ...)                                   \
63 long WINAPI                                                                  \
64 class##_QueryInterface(class *this, const GUID *riid, void **ppvObject)      \
65 {                                                                            \
66     struct GUIDoffset ifaces[] = __VA_ARGS__;                                \
67     int i;                                                                   \
68     dshowdebug(AV_STRINGIFY(class)"_QueryInterface(%p, %p, %p)\n", this, riid, ppvObject); \
69     ff_printGUID(riid);                                                      \
70     if (!ppvObject)                                                          \
71         return E_POINTER;                                                    \
72     for (i = 0; i < sizeof(ifaces)/sizeof(ifaces[0]); i++) {                 \
73         if (IsEqualGUID(riid, ifaces[i].iid)) {                              \
74             void *obj = (void *) ((uint8_t *) this + ifaces[i].offset);      \
75             class##_AddRef(this);                                            \
76             dshowdebug("\tfound %d with offset %d\n", i, ifaces[i].offset);  \
77             *ppvObject = (void *) obj;                                       \
78             return S_OK;                                                     \
79         }                                                                    \
80     }                                                                        \
81     dshowdebug("\tE_NOINTERFACE\n");                                         \
82     *ppvObject = NULL;                                                       \
83     return E_NOINTERFACE;                                                    \
84 }
85 #define DECLARE_ADDREF(class)                                                \
86 unsigned long WINAPI                                                         \
87 class##_AddRef(class *this)                                                  \
88 {                                                                            \
89     dshowdebug(AV_STRINGIFY(class)"_AddRef(%p)\t%ld\n", this, this->ref+1);  \
90     return InterlockedIncrement(&this->ref);                                 \
91 }
92 #define DECLARE_RELEASE(class)                                               \
93 unsigned long WINAPI                                                         \
94 class##_Release(class *this)                                                 \
95 {                                                                            \
96     long ref = InterlockedDecrement(&this->ref);                             \
97     dshowdebug(AV_STRINGIFY(class)"_Release(%p)\t%ld\n", this, ref);         \
98     if (!ref)                                                                \
99         class##_Destroy(this);                                               \
100     return ref;                                                              \
101 }
102
103 #define DECLARE_DESTROY(class, func)                                         \
104 void class##_Destroy(class *this)                                            \
105 {                                                                            \
106     dshowdebug(AV_STRINGIFY(class)"_Destroy(%p)\n", this);                   \
107     func(this);                                                              \
108     if (this) {                                                              \
109         if (this->vtbl)                                                      \
110             CoTaskMemFree(this->vtbl);                                       \
111         CoTaskMemFree(this);                                                 \
112     }                                                                        \
113 }
114 #define DECLARE_CREATE(class, setup, ...)                                    \
115 class *class##_Create(__VA_ARGS__)                                           \
116 {                                                                            \
117     class *this = CoTaskMemAlloc(sizeof(class));                             \
118     void  *vtbl = CoTaskMemAlloc(sizeof(*this->vtbl));                       \
119     dshowdebug(AV_STRINGIFY(class)"_Create(%p)\n", this);                    \
120     if (!this || !vtbl)                                                      \
121         goto fail;                                                           \
122     ZeroMemory(this, sizeof(class));                                         \
123     ZeroMemory(vtbl, sizeof(*this->vtbl));                                   \
124     this->ref  = 1;                                                          \
125     this->vtbl = vtbl;                                                       \
126     if (!setup)                                                              \
127         goto fail;                                                           \
128     dshowdebug("created "AV_STRINGIFY(class)" %p\n", this);                  \
129     return this;                                                             \
130 fail:                                                                        \
131     class##_Destroy(this);                                                   \
132     dshowdebug("could not create "AV_STRINGIFY(class)"\n");                  \
133     return NULL;                                                             \
134 }
135
136 #define SETVTBL(vtbl, class, fn) \
137     do { (vtbl)->fn = (void *) class##_##fn; } while(0)
138
139 /*****************************************************************************
140  * Forward Declarations
141  ****************************************************************************/
142 typedef struct libAVPin libAVPin;
143 typedef struct libAVMemInputPin libAVMemInputPin;
144 typedef struct libAVEnumPins libAVEnumPins;
145 typedef struct libAVEnumMediaTypes libAVEnumMediaTypes;
146 typedef struct libAVFilter libAVFilter;
147
148 /*****************************************************************************
149  * libAVPin
150  ****************************************************************************/
151 struct libAVPin {
152     IPinVtbl *vtbl;
153     long ref;
154     libAVFilter *filter;
155     IPin *connectedto;
156     AM_MEDIA_TYPE type;
157     IMemInputPinVtbl *imemvtbl;
158 };
159
160 long          WINAPI libAVPin_QueryInterface          (libAVPin *, const GUID *, void **);
161 unsigned long WINAPI libAVPin_AddRef                  (libAVPin *);
162 unsigned long WINAPI libAVPin_Release                 (libAVPin *);
163 long          WINAPI libAVPin_Connect                 (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
164 long          WINAPI libAVPin_ReceiveConnection       (libAVPin *, IPin *, const AM_MEDIA_TYPE *);
165 long          WINAPI libAVPin_Disconnect              (libAVPin *);
166 long          WINAPI libAVPin_ConnectedTo             (libAVPin *, IPin **);
167 long          WINAPI libAVPin_ConnectionMediaType     (libAVPin *, AM_MEDIA_TYPE *);
168 long          WINAPI libAVPin_QueryPinInfo            (libAVPin *, PIN_INFO *);
169 long          WINAPI libAVPin_QueryDirection          (libAVPin *, PIN_DIRECTION *);
170 long          WINAPI libAVPin_QueryId                 (libAVPin *, wchar_t **);
171 long          WINAPI libAVPin_QueryAccept             (libAVPin *, const AM_MEDIA_TYPE *);
172 long          WINAPI libAVPin_EnumMediaTypes          (libAVPin *, IEnumMediaTypes **);
173 long          WINAPI libAVPin_QueryInternalConnections(libAVPin *, IPin **, unsigned long *);
174 long          WINAPI libAVPin_EndOfStream             (libAVPin *);
175 long          WINAPI libAVPin_BeginFlush              (libAVPin *);
176 long          WINAPI libAVPin_EndFlush                (libAVPin *);
177 long          WINAPI libAVPin_NewSegment              (libAVPin *, REFERENCE_TIME, REFERENCE_TIME, double);
178
179 long          WINAPI libAVMemInputPin_QueryInterface          (libAVMemInputPin *, const GUID *, void **);
180 unsigned long WINAPI libAVMemInputPin_AddRef                  (libAVMemInputPin *);
181 unsigned long WINAPI libAVMemInputPin_Release                 (libAVMemInputPin *);
182 long          WINAPI libAVMemInputPin_GetAllocator            (libAVMemInputPin *, IMemAllocator **);
183 long          WINAPI libAVMemInputPin_NotifyAllocator         (libAVMemInputPin *, IMemAllocator *, BOOL);
184 long          WINAPI libAVMemInputPin_GetAllocatorRequirements(libAVMemInputPin *, ALLOCATOR_PROPERTIES *);
185 long          WINAPI libAVMemInputPin_Receive                 (libAVMemInputPin *, IMediaSample *);
186 long          WINAPI libAVMemInputPin_ReceiveMultiple         (libAVMemInputPin *, IMediaSample **, long, long *);
187 long          WINAPI libAVMemInputPin_ReceiveCanBlock         (libAVMemInputPin *);
188
189 void                 libAVPin_Destroy(libAVPin *);
190 libAVPin            *libAVPin_Create (libAVFilter *filter);
191
192 void                 libAVMemInputPin_Destroy(libAVMemInputPin *);
193
194 /*****************************************************************************
195  * libAVEnumPins
196  ****************************************************************************/
197 struct libAVEnumPins {
198     IEnumPinsVtbl *vtbl;
199     long ref;
200     int pos;
201     libAVPin *pin;
202     libAVFilter *filter;
203 };
204
205 long          WINAPI libAVEnumPins_QueryInterface(libAVEnumPins *, const GUID *, void **);
206 unsigned long WINAPI libAVEnumPins_AddRef        (libAVEnumPins *);
207 unsigned long WINAPI libAVEnumPins_Release       (libAVEnumPins *);
208 long          WINAPI libAVEnumPins_Next          (libAVEnumPins *, unsigned long, IPin **, unsigned long *);
209 long          WINAPI libAVEnumPins_Skip          (libAVEnumPins *, unsigned long);
210 long          WINAPI libAVEnumPins_Reset         (libAVEnumPins *);
211 long          WINAPI libAVEnumPins_Clone         (libAVEnumPins *, libAVEnumPins **);
212
213 void                 libAVEnumPins_Destroy(libAVEnumPins *);
214 libAVEnumPins       *libAVEnumPins_Create (libAVPin *pin, libAVFilter *filter);
215
216 /*****************************************************************************
217  * libAVEnumMediaTypes
218  ****************************************************************************/
219 struct libAVEnumMediaTypes {
220     IEnumPinsVtbl *vtbl;
221     long ref;
222     int pos;
223     AM_MEDIA_TYPE type;
224 };
225
226 long          WINAPI libAVEnumMediaTypes_QueryInterface(libAVEnumMediaTypes *, const GUID *, void **);
227 unsigned long WINAPI libAVEnumMediaTypes_AddRef        (libAVEnumMediaTypes *);
228 unsigned long WINAPI libAVEnumMediaTypes_Release       (libAVEnumMediaTypes *);
229 long          WINAPI libAVEnumMediaTypes_Next          (libAVEnumMediaTypes *, unsigned long, AM_MEDIA_TYPE **, unsigned long *);
230 long          WINAPI libAVEnumMediaTypes_Skip          (libAVEnumMediaTypes *, unsigned long);
231 long          WINAPI libAVEnumMediaTypes_Reset         (libAVEnumMediaTypes *);
232 long          WINAPI libAVEnumMediaTypes_Clone         (libAVEnumMediaTypes *, libAVEnumMediaTypes **);
233
234 void                 libAVEnumMediaTypes_Destroy(libAVEnumMediaTypes *);
235 libAVEnumMediaTypes *libAVEnumMediaTypes_Create(const AM_MEDIA_TYPE *type);
236
237 /*****************************************************************************
238  * libAVFilter
239  ****************************************************************************/
240 struct libAVFilter {
241     IBaseFilterVtbl *vtbl;
242     long ref;
243     const wchar_t *name;
244     libAVPin *pin;
245     FILTER_INFO info;
246     FILTER_STATE state;
247     IReferenceClock *clock;
248     enum dshowDeviceType type;
249     void *priv_data;
250     int stream_index;
251     int64_t start_time;
252     void (*callback)(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time);
253 };
254
255 long          WINAPI libAVFilter_QueryInterface (libAVFilter *, const GUID *, void **);
256 unsigned long WINAPI libAVFilter_AddRef         (libAVFilter *);
257 unsigned long WINAPI libAVFilter_Release        (libAVFilter *);
258 long          WINAPI libAVFilter_GetClassID     (libAVFilter *, CLSID *);
259 long          WINAPI libAVFilter_Stop           (libAVFilter *);
260 long          WINAPI libAVFilter_Pause          (libAVFilter *);
261 long          WINAPI libAVFilter_Run            (libAVFilter *, REFERENCE_TIME);
262 long          WINAPI libAVFilter_GetState       (libAVFilter *, DWORD, FILTER_STATE *);
263 long          WINAPI libAVFilter_SetSyncSource  (libAVFilter *, IReferenceClock *);
264 long          WINAPI libAVFilter_GetSyncSource  (libAVFilter *, IReferenceClock **);
265 long          WINAPI libAVFilter_EnumPins       (libAVFilter *, IEnumPins **);
266 long          WINAPI libAVFilter_FindPin        (libAVFilter *, const wchar_t *, IPin **);
267 long          WINAPI libAVFilter_QueryFilterInfo(libAVFilter *, FILTER_INFO *);
268 long          WINAPI libAVFilter_JoinFilterGraph(libAVFilter *, IFilterGraph *, const wchar_t *);
269 long          WINAPI libAVFilter_QueryVendorInfo(libAVFilter *, wchar_t **);
270
271 void                 libAVFilter_Destroy(libAVFilter *);
272 libAVFilter         *libAVFilter_Create (void *, void *, enum dshowDeviceType);
273
274 #endif /* AVDEVICE_DSHOW_H */