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
23
24 #include <stddef.h>
25 #define imemoffset offsetof(DShowPin, imemvtbl)
26
28 { {&IID_IUnknown,0}, {&IID_IPin,0}, {&IID_IMemInputPin,
imemoffset} })
31
33 {
35 /* Input pins receive connections. */
36 return S_FALSE;
37 }
39 const AM_MEDIA_TYPE *
type)
40 {
42 dshowdebug(
"ff_dshow_pin_ReceiveConnection(%p)\n",
this);
43
44 if (!pin)
45 return E_POINTER;
46 if (this->connectedto)
47 return VFW_E_ALREADY_CONNECTED;
48
51 if (!IsEqualGUID(&
type->majortype, &MEDIATYPE_Video))
52 return VFW_E_TYPE_NOT_ACCEPTED;
53 } else {
54 if (!IsEqualGUID(&
type->majortype, &MEDIATYPE_Audio))
55 return VFW_E_TYPE_NOT_ACCEPTED;
56 }
57
58 IPin_AddRef(pin);
59 this->connectedto = pin;
60
62
63 return S_OK;
64 }
66 {
67 dshowdebug(
"ff_dshow_pin_Disconnect(%p)\n",
this);
68
69 if (this->
filter->state != State_Stopped)
70 return VFW_E_NOT_STOPPED;
71 if (!this->connectedto)
72 return S_FALSE;
73 IPin_Release(this->connectedto);
74 this->connectedto =
NULL;
75
76 return S_OK;
77 }
79 {
80 dshowdebug(
"ff_dshow_pin_ConnectedTo(%p)\n",
this);
81
82 if (!pin)
83 return E_POINTER;
84 if (!this->connectedto)
85 return VFW_E_NOT_CONNECTED;
86 IPin_AddRef(this->connectedto);
87 *pin = this->connectedto;
88
89 return S_OK;
90 }
92 {
93 dshowdebug(
"ff_dshow_pin_ConnectionMediaType(%p)\n",
this);
94
96 return E_POINTER;
97 if (!this->connectedto)
98 return VFW_E_NOT_CONNECTED;
99
101 }
103 {
104 dshowdebug(
"ff_dshow_pin_QueryPinInfo(%p)\n",
this);
105
107 return E_POINTER;
108
111
112 info->pFilter = (IBaseFilter *) this->
filter;
113 info->dir = PINDIR_INPUT;
114 wcscpy(
info->achName,
L "Capture");
115
116 return S_OK;
117 }
119 {
120 dshowdebug(
"ff_dshow_pin_QueryDirection(%p)\n",
this);
121 if (!dir)
122 return E_POINTER;
123 *dir = PINDIR_INPUT;
124 return S_OK;
125 }
127 {
128 dshowdebug(
"ff_dshow_pin_QueryId(%p)\n",
this);
129
130 if (!id)
131 return E_POINTER;
132
133 *
id = wcsdup(
L "libAV Pin");
134
135 return S_OK;
136 }
138 {
139 dshowdebug(
"ff_dshow_pin_QueryAccept(%p)\n",
this);
140 return S_FALSE;
141 }
143 {
146 dshowdebug(
"ff_dshow_pin_EnumMediaTypes(%p)\n",
this);
147
148 if (!enumtypes)
149 return E_POINTER;
151 if (!new)
152 return E_OUTOFMEMORY;
153
154 *enumtypes = (IEnumMediaTypes *) new;
155 return S_OK;
156 }
158 unsigned long *npin)
159 {
160 dshowdebug(
"ff_dshow_pin_QueryInternalConnections(%p)\n",
this);
161 return E_NOTIMPL;
162 }
164 {
165 dshowdebug(
"ff_dshow_pin_EndOfStream(%p)\n",
this);
166 /* I don't care. */
167 return S_OK;
168 }
170 {
171 dshowdebug(
"ff_dshow_pin_BeginFlush(%p)\n",
this);
172 /* I don't care. */
173 return S_OK;
174 }
176 {
177 dshowdebug(
"ff_dshow_pin_EndFlush(%p)\n",
this);
178 /* I don't care. */
179 return S_OK;
180 }
182 double rate)
183 {
184 dshowdebug(
"ff_dshow_pin_NewSegment(%p)\n",
this);
185 /* I don't care. */
186 return S_OK;
187 }
188
190 {
191 IPinVtbl *vtbl = this->vtbl;
192 IMemInputPinVtbl *imemvtbl;
193
195 return 0;
196
197 imemvtbl =
av_malloc(
sizeof(IMemInputPinVtbl));
198 if (!imemvtbl)
199 return 0;
200
201 SETVTBL(imemvtbl, meminputpin, QueryInterface);
202 SETVTBL(imemvtbl, meminputpin, AddRef);
203 SETVTBL(imemvtbl, meminputpin, Release);
204 SETVTBL(imemvtbl, meminputpin, GetAllocator);
205 SETVTBL(imemvtbl, meminputpin, NotifyAllocator);
206 SETVTBL(imemvtbl, meminputpin, GetAllocatorRequirements);
207 SETVTBL(imemvtbl, meminputpin, Receive);
208 SETVTBL(imemvtbl, meminputpin, ReceiveMultiple);
209 SETVTBL(imemvtbl, meminputpin, ReceiveCanBlock);
210
211 this->imemvtbl = imemvtbl;
212
213 SETVTBL(vtbl, pin, QueryInterface);
217 SETVTBL(vtbl, pin, ReceiveConnection);
218 SETVTBL(vtbl, pin, Disconnect);
219 SETVTBL(vtbl, pin, ConnectedTo);
220 SETVTBL(vtbl, pin, ConnectionMediaType);
221 SETVTBL(vtbl, pin, QueryPinInfo);
222 SETVTBL(vtbl, pin, QueryDirection);
224 SETVTBL(vtbl, pin, QueryAccept);
225 SETVTBL(vtbl, pin, EnumMediaTypes);
226 SETVTBL(vtbl, pin, QueryInternalConnections);
227 SETVTBL(vtbl, pin, EndOfStream);
228 SETVTBL(vtbl, pin, BeginFlush);
230 SETVTBL(vtbl, pin, NewSegment);
231
233
234 return 1;
235 }
236
238 {
239 if (!this)
240 return;
242 if (this->
type.pbFormat) {
243 CoTaskMemFree(this->
type.pbFormat);
245 }
246 }
249
250 /*****************************************************************************
251 * DShowMemInputPin
252 ****************************************************************************/
254 void **ppvObject)
255 {
257 dshowdebug(
"ff_dshow_meminputpin_QueryInterface(%p)\n",
this);
259 }
261 {
263 dshowdebug(
"ff_dshow_meminputpin_AddRef(%p)\n",
this);
265 }
267 {
269 dshowdebug(
"ff_dshow_meminputpin_Release(%p)\n",
this);
271 }
273 {
274 dshowdebug(
"ff_dshow_meminputpin_GetAllocator(%p)\n",
this);
275 return VFW_E_NO_ALLOCATOR;
276 }
278 BOOL rdwr)
279 {
280 dshowdebug(
"ff_dshow_meminputpin_NotifyAllocator(%p)\n",
this);
281 return S_OK;
282 }
284 ALLOCATOR_PROPERTIES *props)
285 {
286 dshowdebug(
"ff_dshow_meminputpin_GetAllocatorRequirements(%p)\n",
this);
287 return E_NOTIMPL;
288 }
290 {
293 void *priv_data;
295 uint8_t *buf;
296 int buf_size; /* todo should be a long? */
298 int64_t chosentime = 0;
299 int64_t sampletime = 0;
300 int64_t graphtime = 0;
301 int use_sample_time = 1;
302 const char *devtypename = (devtype ==
VideoDevice) ?
"video" :
"audio";
306 HRESULT hr;
307
308
309 dshowdebug(
"ff_dshow_meminputpin_Receive(%p)\n",
this);
310
312 return E_POINTER;
313
317
318 hr = IMediaSample_GetTime(
sample, &sampletime, &
dummy);
319 IReferenceClock_GetTime(clock, &graphtime);
320 if (devtype ==
VideoDevice && !
ctx->use_video_device_timestamps) {
321 /* PTS from video devices is unreliable. */
322 chosentime = graphtime;
323 use_sample_time = 0;
324 } else {
325 if (hr == VFW_E_SAMPLE_TIME_NOT_SET || sampletime == 0) {
326 chosentime = graphtime;
327 use_sample_time = 0;
329 "frame with missing sample timestamp encountered, falling back to graph timestamp\n");
330 }
331 else if (sampletime > 400000000000000000LL) {
332 /* initial frames sometimes start < 0 (shown as a very large number here,
333 like 437650244077016960 which FFmpeg doesn't like).
334 TODO figure out math. For now just drop them. */
336 "dropping initial (or ending) sample with odd PTS too high %"PRId64"\n", sampletime);
337 return S_OK;
338 } else
339 chosentime = sampletime;
340 }
341 // media sample time is relative to graph start time
343 if (use_sample_time)
345
346 buf_size = IMediaSample_GetActualDataLength(
sample);
347 IMediaSample_GetPointer(
sample, &buf);
349
351 "timestamp %"PRId64" orig timestamp %"PRId64" graph timestamp %"PRId64" diff %"PRId64" %s\n",
352 devtypename, buf_size, chosentime, sampletime, graphtime, graphtime - sampletime,
ctx->device_name[devtype]);
354
355 return S_OK;
356 }
358 IMediaSample **
samples,
long n,
long *nproc)
359 {
361 dshowdebug(
"ff_dshow_meminputpin_ReceiveMultiple(%p)\n",
this);
362
363 for (
i = 0;
i < n;
i++)
365
366 *nproc = n;
367 return S_OK;
368 }
370 {
371 dshowdebug(
"ff_dshow_meminputpin_ReceiveCanBlock(%p)\n",
this);
372 /* I swear I will not block. */
373 return S_FALSE;
374 }
375
377 {
379 dshowdebug(
"ff_dshow_meminputpin_Destroy(%p)\n",
this);
381 }