dshow_pin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #include "dshow_capture.h"
  22. #include <stddef.h>
  23. #define imemoffset offsetof(DShowPin, imemvtbl)
  24. DECLARE_QUERYINTERFACE(pin, DShowPin,
  25. { {&IID_IUnknown,0}, {&IID_IPin,0}, {&IID_IMemInputPin,imemoffset} })
  26. DECLARE_ADDREF(pin, DShowPin)
  27. DECLARE_RELEASE(pin, DShowPin)
  28. long WINAPI ff_dshow_pin_Connect(DShowPin *this, IPin *pin, const AM_MEDIA_TYPE *type)
  29. {
  30. dshowdebug("ff_dshow_pin_Connect(%p, %p, %p)\n", this, pin, type);
  31. /* Input pins receive connections. */
  32. return S_FALSE;
  33. }
  34. long WINAPI ff_dshow_pin_ReceiveConnection(DShowPin *this, IPin *pin,
  35. const AM_MEDIA_TYPE *type)
  36. {
  37. enum dshowDeviceType devtype = this->filter->type;
  38. dshowdebug("ff_dshow_pin_ReceiveConnection(%p)\n", this);
  39. if (!pin)
  40. return E_POINTER;
  41. if (this->connectedto)
  42. return VFW_E_ALREADY_CONNECTED;
  43. ff_print_AM_MEDIA_TYPE(type);
  44. if (devtype == VideoDevice) {
  45. if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Video))
  46. return VFW_E_TYPE_NOT_ACCEPTED;
  47. } else {
  48. if (!IsEqualGUID(&type->majortype, &MEDIATYPE_Audio))
  49. return VFW_E_TYPE_NOT_ACCEPTED;
  50. }
  51. IPin_AddRef(pin);
  52. this->connectedto = pin;
  53. ff_copy_dshow_media_type(&this->type, type);
  54. return S_OK;
  55. }
  56. long WINAPI ff_dshow_pin_Disconnect(DShowPin *this)
  57. {
  58. dshowdebug("ff_dshow_pin_Disconnect(%p)\n", this);
  59. if (this->filter->state != State_Stopped)
  60. return VFW_E_NOT_STOPPED;
  61. if (!this->connectedto)
  62. return S_FALSE;
  63. IPin_Release(this->connectedto);
  64. this->connectedto = NULL;
  65. return S_OK;
  66. }
  67. long WINAPI ff_dshow_pin_ConnectedTo(DShowPin *this, IPin **pin)
  68. {
  69. dshowdebug("ff_dshow_pin_ConnectedTo(%p)\n", this);
  70. if (!pin)
  71. return E_POINTER;
  72. if (!this->connectedto)
  73. return VFW_E_NOT_CONNECTED;
  74. IPin_AddRef(this->connectedto);
  75. *pin = this->connectedto;
  76. return S_OK;
  77. }
  78. long WINAPI ff_dshow_pin_ConnectionMediaType(DShowPin *this, AM_MEDIA_TYPE *type)
  79. {
  80. dshowdebug("ff_dshow_pin_ConnectionMediaType(%p)\n", this);
  81. if (!type)
  82. return E_POINTER;
  83. if (!this->connectedto)
  84. return VFW_E_NOT_CONNECTED;
  85. return ff_copy_dshow_media_type(type, &this->type);
  86. }
  87. long WINAPI ff_dshow_pin_QueryPinInfo(DShowPin *this, PIN_INFO *info)
  88. {
  89. dshowdebug("ff_dshow_pin_QueryPinInfo(%p)\n", this);
  90. if (!info)
  91. return E_POINTER;
  92. if (this->filter)
  93. ff_dshow_filter_AddRef(this->filter);
  94. info->pFilter = (IBaseFilter *) this->filter;
  95. info->dir = PINDIR_INPUT;
  96. wcscpy(info->achName, L"Capture");
  97. return S_OK;
  98. }
  99. long WINAPI ff_dshow_pin_QueryDirection(DShowPin *this, PIN_DIRECTION *dir)
  100. {
  101. dshowdebug("ff_dshow_pin_QueryDirection(%p)\n", this);
  102. if (!dir)
  103. return E_POINTER;
  104. *dir = PINDIR_INPUT;
  105. return S_OK;
  106. }
  107. long WINAPI ff_dshow_pin_QueryId(DShowPin *this, wchar_t **id)
  108. {
  109. dshowdebug("ff_dshow_pin_QueryId(%p)\n", this);
  110. if (!id)
  111. return E_POINTER;
  112. *id = wcsdup(L"libAV Pin");
  113. return S_OK;
  114. }
  115. long WINAPI ff_dshow_pin_QueryAccept(DShowPin *this, const AM_MEDIA_TYPE *type)
  116. {
  117. dshowdebug("ff_dshow_pin_QueryAccept(%p)\n", this);
  118. return S_FALSE;
  119. }
  120. long WINAPI ff_dshow_pin_EnumMediaTypes(DShowPin *this, IEnumMediaTypes **enumtypes)
  121. {
  122. const AM_MEDIA_TYPE *type = NULL;
  123. DShowEnumMediaTypes *new;
  124. dshowdebug("ff_dshow_pin_EnumMediaTypes(%p)\n", this);
  125. if (!enumtypes)
  126. return E_POINTER;
  127. new = ff_dshow_enummediatypes_Create(type);
  128. if (!new)
  129. return E_OUTOFMEMORY;
  130. *enumtypes = (IEnumMediaTypes *) new;
  131. return S_OK;
  132. }
  133. long WINAPI ff_dshow_pin_QueryInternalConnections(DShowPin *this, IPin **pin,
  134. unsigned long *npin)
  135. {
  136. dshowdebug("ff_dshow_pin_QueryInternalConnections(%p)\n", this);
  137. return E_NOTIMPL;
  138. }
  139. long WINAPI ff_dshow_pin_EndOfStream(DShowPin *this)
  140. {
  141. dshowdebug("ff_dshow_pin_EndOfStream(%p)\n", this);
  142. /* I don't care. */
  143. return S_OK;
  144. }
  145. long WINAPI ff_dshow_pin_BeginFlush(DShowPin *this)
  146. {
  147. dshowdebug("ff_dshow_pin_BeginFlush(%p)\n", this);
  148. /* I don't care. */
  149. return S_OK;
  150. }
  151. long WINAPI ff_dshow_pin_EndFlush(DShowPin *this)
  152. {
  153. dshowdebug("ff_dshow_pin_EndFlush(%p)\n", this);
  154. /* I don't care. */
  155. return S_OK;
  156. }
  157. long WINAPI ff_dshow_pin_NewSegment(DShowPin *this, REFERENCE_TIME start, REFERENCE_TIME stop,
  158. double rate)
  159. {
  160. dshowdebug("ff_dshow_pin_NewSegment(%p)\n", this);
  161. /* I don't care. */
  162. return S_OK;
  163. }
  164. static int ff_dshow_pin_Setup(DShowPin *this, DShowFilter *filter)
  165. {
  166. IPinVtbl *vtbl = this->vtbl;
  167. IMemInputPinVtbl *imemvtbl;
  168. if (!filter)
  169. return 0;
  170. imemvtbl = av_malloc(sizeof(IMemInputPinVtbl));
  171. if (!imemvtbl)
  172. return 0;
  173. SETVTBL(imemvtbl, meminputpin, QueryInterface);
  174. SETVTBL(imemvtbl, meminputpin, AddRef);
  175. SETVTBL(imemvtbl, meminputpin, Release);
  176. SETVTBL(imemvtbl, meminputpin, GetAllocator);
  177. SETVTBL(imemvtbl, meminputpin, NotifyAllocator);
  178. SETVTBL(imemvtbl, meminputpin, GetAllocatorRequirements);
  179. SETVTBL(imemvtbl, meminputpin, Receive);
  180. SETVTBL(imemvtbl, meminputpin, ReceiveMultiple);
  181. SETVTBL(imemvtbl, meminputpin, ReceiveCanBlock);
  182. this->imemvtbl = imemvtbl;
  183. SETVTBL(vtbl, pin, QueryInterface);
  184. SETVTBL(vtbl, pin, AddRef);
  185. SETVTBL(vtbl, pin, Release);
  186. SETVTBL(vtbl, pin, Connect);
  187. SETVTBL(vtbl, pin, ReceiveConnection);
  188. SETVTBL(vtbl, pin, Disconnect);
  189. SETVTBL(vtbl, pin, ConnectedTo);
  190. SETVTBL(vtbl, pin, ConnectionMediaType);
  191. SETVTBL(vtbl, pin, QueryPinInfo);
  192. SETVTBL(vtbl, pin, QueryDirection);
  193. SETVTBL(vtbl, pin, QueryId);
  194. SETVTBL(vtbl, pin, QueryAccept);
  195. SETVTBL(vtbl, pin, EnumMediaTypes);
  196. SETVTBL(vtbl, pin, QueryInternalConnections);
  197. SETVTBL(vtbl, pin, EndOfStream);
  198. SETVTBL(vtbl, pin, BeginFlush);
  199. SETVTBL(vtbl, pin, EndFlush);
  200. SETVTBL(vtbl, pin, NewSegment);
  201. this->filter = filter;
  202. return 1;
  203. }
  204. static void ff_dshow_pin_Free(DShowPin *this)
  205. {
  206. if (!this)
  207. return;
  208. av_freep(&this->imemvtbl);
  209. if (this->type.pbFormat) {
  210. CoTaskMemFree(this->type.pbFormat);
  211. this->type.pbFormat = NULL;
  212. }
  213. }
  214. DECLARE_CREATE(pin, DShowPin, ff_dshow_pin_Setup(this, filter), DShowFilter *filter)
  215. DECLARE_DESTROY(pin, DShowPin, ff_dshow_pin_Free)
  216. /*****************************************************************************
  217. * DShowMemInputPin
  218. ****************************************************************************/
  219. long WINAPI ff_dshow_meminputpin_QueryInterface(DShowMemInputPin *this, const GUID *riid,
  220. void **ppvObject)
  221. {
  222. DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
  223. dshowdebug("ff_dshow_meminputpin_QueryInterface(%p)\n", this);
  224. return ff_dshow_pin_QueryInterface(pin, riid, ppvObject);
  225. }
  226. unsigned long WINAPI ff_dshow_meminputpin_AddRef(DShowMemInputPin *this)
  227. {
  228. DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
  229. dshowdebug("ff_dshow_meminputpin_AddRef(%p)\n", this);
  230. return ff_dshow_pin_AddRef(pin);
  231. }
  232. unsigned long WINAPI ff_dshow_meminputpin_Release(DShowMemInputPin *this)
  233. {
  234. DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
  235. dshowdebug("ff_dshow_meminputpin_Release(%p)\n", this);
  236. return ff_dshow_pin_Release(pin);
  237. }
  238. long WINAPI ff_dshow_meminputpin_GetAllocator(DShowMemInputPin *this, IMemAllocator **alloc)
  239. {
  240. dshowdebug("ff_dshow_meminputpin_GetAllocator(%p)\n", this);
  241. return VFW_E_NO_ALLOCATOR;
  242. }
  243. long WINAPI ff_dshow_meminputpin_NotifyAllocator(DShowMemInputPin *this, IMemAllocator *alloc,
  244. BOOL rdwr)
  245. {
  246. dshowdebug("ff_dshow_meminputpin_NotifyAllocator(%p)\n", this);
  247. return S_OK;
  248. }
  249. long WINAPI ff_dshow_meminputpin_GetAllocatorRequirements(DShowMemInputPin *this,
  250. ALLOCATOR_PROPERTIES *props)
  251. {
  252. dshowdebug("ff_dshow_meminputpin_GetAllocatorRequirements(%p)\n", this);
  253. return E_NOTIMPL;
  254. }
  255. long WINAPI ff_dshow_meminputpin_Receive(DShowMemInputPin *this, IMediaSample *sample)
  256. {
  257. DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
  258. enum dshowDeviceType devtype = pin->filter->type;
  259. void *priv_data;
  260. AVFormatContext *s;
  261. uint8_t *buf;
  262. int buf_size; /* todo should be a long? */
  263. int index;
  264. int64_t chosentime = 0;
  265. int64_t sampletime = 0;
  266. int64_t graphtime = 0;
  267. int use_sample_time = 1;
  268. const char *devtypename = (devtype == VideoDevice) ? "video" : "audio";
  269. IReferenceClock *clock = pin->filter->clock;
  270. int64_t dummy;
  271. struct dshow_ctx *ctx;
  272. HRESULT hr;
  273. dshowdebug("ff_dshow_meminputpin_Receive(%p)\n", this);
  274. if (!sample)
  275. return E_POINTER;
  276. priv_data = pin->filter->priv_data;
  277. s = priv_data;
  278. ctx = s->priv_data;
  279. hr = IMediaSample_GetTime(sample, &sampletime, &dummy);
  280. IReferenceClock_GetTime(clock, &graphtime);
  281. if (devtype == VideoDevice && !ctx->use_video_device_timestamps) {
  282. /* PTS from video devices is unreliable. */
  283. chosentime = graphtime;
  284. use_sample_time = 0;
  285. } else {
  286. if (hr == VFW_E_SAMPLE_TIME_NOT_SET || sampletime == 0) {
  287. chosentime = graphtime;
  288. use_sample_time = 0;
  289. av_log(s, AV_LOG_DEBUG,
  290. "frame with missing sample timestamp encountered, falling back to graph timestamp\n");
  291. }
  292. else if (sampletime > 400000000000000000LL) {
  293. /* initial frames sometimes start < 0 (shown as a very large number here,
  294. like 437650244077016960 which FFmpeg doesn't like).
  295. TODO figure out math. For now just drop them. */
  296. av_log(s, AV_LOG_DEBUG,
  297. "dropping initial (or ending) sample with odd PTS too high %"PRId64"\n", sampletime);
  298. return S_OK;
  299. } else
  300. chosentime = sampletime;
  301. }
  302. // media sample time is relative to graph start time
  303. sampletime += pin->filter->start_time;
  304. if (use_sample_time)
  305. chosentime += pin->filter->start_time;
  306. buf_size = IMediaSample_GetActualDataLength(sample);
  307. IMediaSample_GetPointer(sample, &buf);
  308. index = pin->filter->stream_index;
  309. av_log(s, AV_LOG_VERBOSE, "passing through packet of type %s size %8d "
  310. "timestamp %"PRId64" orig timestamp %"PRId64" graph timestamp %"PRId64" diff %"PRId64" %s\n",
  311. devtypename, buf_size, chosentime, sampletime, graphtime, graphtime - sampletime, ctx->device_name[devtype]);
  312. pin->filter->callback(priv_data, index, buf, buf_size, chosentime, devtype);
  313. return S_OK;
  314. }
  315. long WINAPI ff_dshow_meminputpin_ReceiveMultiple(DShowMemInputPin *this,
  316. IMediaSample **samples, long n, long *nproc)
  317. {
  318. int i;
  319. dshowdebug("ff_dshow_meminputpin_ReceiveMultiple(%p)\n", this);
  320. for (i = 0; i < n; i++)
  321. ff_dshow_meminputpin_Receive(this, samples[i]);
  322. *nproc = n;
  323. return S_OK;
  324. }
  325. long WINAPI ff_dshow_meminputpin_ReceiveCanBlock(DShowMemInputPin *this)
  326. {
  327. dshowdebug("ff_dshow_meminputpin_ReceiveCanBlock(%p)\n", this);
  328. /* I swear I will not block. */
  329. return S_FALSE;
  330. }
  331. void ff_dshow_meminputpin_Destroy(DShowMemInputPin *this)
  332. {
  333. DShowPin *pin = (DShowPin *) ((uint8_t *) this - imemoffset);
  334. dshowdebug("ff_dshow_meminputpin_Destroy(%p)\n", this);
  335. ff_dshow_pin_Destroy(pin);
  336. }