videocap.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. #include "precompile.h"
  2. #include "videocap.h"
  3. #include "ListEntry.h"
  4. #include "qedit.h"
  5. #include <dshow.h> // need directshow sdk
  6. //#include "SpBase.h"
  7. #include <videohorflip.h>
  8. #define av_always_inline __inline
  9. #define inline __inline
  10. #include <libswscale\swscale.h>
  11. #include <libavcodec\avcodec.h>
  12. #include "libavutil/imgutils.h"
  13. #include "video_common/ffmpeg_api_adapter.h"
  14. //#pragma comment(lib, "strmiids.lib")
  15. //#pragma comment(lib, "..\\ffmpeg\\libswscale.lib")
  16. ////////////////////////////////////////////////////////////////////////////////////////
  17. // helper
  18. #define swap(t, x, y) \
  19. { \
  20. t v; \
  21. v = x; \
  22. x = y; \
  23. y = v; \
  24. }
  25. int g_width= 640;
  26. int g_height= 360;
  27. static void i420_flip_vertical(video_frame *frame)
  28. {
  29. if (frame->format == VIDEO_FORMAT_I420) {
  30. frame->data[0] = frame->data[0] + frame->linesize[0]*(frame->height-1);
  31. frame->data[1] = frame->data[1] + frame->linesize[1]*((frame->height>>1)-1);
  32. frame->data[2] = frame->data[2] + frame->linesize[2]*((frame->height>>1)-1);
  33. frame->linesize[0] = -frame->linesize[0];
  34. frame->linesize[1] = -frame->linesize[1];
  35. frame->linesize[2] = -frame->linesize[2];
  36. swap(unsigned char*, frame->data[1], frame->data[2]);
  37. swap(int, frame->linesize[1], frame->linesize[2]);
  38. }
  39. }
  40. static HRESULT AddToRot(IUnknown *pUnkGraph, DWORD *pdwRegister)
  41. {
  42. IMoniker * pMoniker;
  43. IRunningObjectTable *pROT;
  44. WCHAR wsz[256];
  45. HRESULT hr;
  46. if (FAILED(GetRunningObjectTable(0, &pROT))) {
  47. return E_FAIL;
  48. }
  49. wsprintfW(wsz, L"FilterGraph %08x pid %08x", (DWORD_PTR)pUnkGraph, GetCurrentProcessId());
  50. hr = CreateItemMoniker(L"!", wsz, &pMoniker);
  51. if (SUCCEEDED(hr)) {
  52. pROT->lpVtbl->Register(pROT, ROTFLAGS_REGISTRATIONKEEPSALIVE, pUnkGraph,
  53. pMoniker, pdwRegister);
  54. pMoniker->lpVtbl->Release(pMoniker);
  55. }
  56. pROT->lpVtbl->Release(pROT);
  57. return hr;
  58. }
  59. static void RemoveFromRot(DWORD pdwRegister)
  60. {
  61. IRunningObjectTable *pROT;
  62. if (SUCCEEDED(GetRunningObjectTable(0, &pROT))) {
  63. pROT->lpVtbl->Revoke(pROT, pdwRegister);
  64. pROT->lpVtbl->Release(pROT);
  65. }
  66. }
  67. static char* GuidToString(const GUID guid)
  68. {
  69. int buf_len=64;
  70. char *buf =(char *)malloc(buf_len);
  71. _snprintf(
  72. buf,
  73. buf_len,
  74. "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  75. guid.Data1, guid.Data2, guid.Data3,
  76. guid.Data4[0], guid.Data4[1],
  77. guid.Data4[2], guid.Data4[3],
  78. guid.Data4[4], guid.Data4[5],
  79. guid.Data4[6], guid.Data4[7]);
  80. //printf("%s\n",buf);
  81. return buf;
  82. }
  83. static void FreeMediaTypeEx(AM_MEDIA_TYPE *pmt)
  84. {
  85. if (pmt) {
  86. if (pmt->cbFormat) {
  87. CoTaskMemFree((PVOID)pmt->pbFormat);
  88. pmt->cbFormat = 0;
  89. pmt->pbFormat = NULL;
  90. }
  91. if (pmt->pUnk) {
  92. pmt->pUnk->lpVtbl->Release(pmt->pUnk);
  93. pmt->pUnk = NULL;
  94. }
  95. CoTaskMemFree(pmt);
  96. }
  97. }
  98. static HRESULT GetUnconnectedPin(IBaseFilter *pFilter,PIN_DIRECTION PinDir,IPin **ppPin)
  99. {
  100. IEnumPins *pEnum = 0;
  101. IPin *pPin = 0;
  102. HRESULT hr = pFilter->lpVtbl->EnumPins(pFilter, &pEnum);
  103. *ppPin = 0;
  104. if (FAILED(hr))
  105. return hr;
  106. while (pEnum->lpVtbl->Next(pEnum, 1, &pPin, NULL) == S_OK) {
  107. PIN_DIRECTION ThisPinDir;
  108. pPin->lpVtbl->QueryDirection(pPin, &ThisPinDir);
  109. if (ThisPinDir == PinDir) {
  110. IPin *pTmp = 0;
  111. hr = pPin->lpVtbl->ConnectedTo(pPin, &pTmp);
  112. if (SUCCEEDED(hr)) // Already connected, not the pin we want.
  113. pTmp->lpVtbl->Release(pTmp);
  114. else // Unconnected, this is the pin we want.
  115. {
  116. pEnum->lpVtbl->Release(pEnum);
  117. *ppPin = pPin;
  118. return S_OK;
  119. }
  120. }
  121. pPin->lpVtbl->Release(pPin);
  122. }
  123. pEnum->lpVtbl->Release(pEnum);
  124. // Did not find a matching pin.
  125. return E_FAIL;
  126. }
  127. static HRESULT ConnectFilters1(IGraphBuilder *pGraph, // Filter Graph Manager.
  128. IPin *pOut, // Output pin on the upstream filter.
  129. IBaseFilter *pDest) // Downstream filter.
  130. {
  131. IPin *pIn;
  132. HRESULT hr;
  133. if ((pGraph == NULL) || (pOut == NULL) || (pDest == NULL))
  134. {
  135. return E_POINTER;
  136. }
  137. // Find an input pin on the downstream filter.
  138. pIn = 0;
  139. hr = GetUnconnectedPin(pDest, PINDIR_INPUT, &pIn);
  140. if (FAILED(hr))
  141. {
  142. return hr;
  143. }
  144. // Try to connect them.
  145. hr = pGraph->lpVtbl->ConnectDirect(pGraph, pOut, pIn, NULL);
  146. if (FAILED(hr)){
  147. hr = pGraph->lpVtbl->Connect(pGraph, pOut, pIn);
  148. }
  149. pIn->lpVtbl->Release(pIn);
  150. return hr;
  151. }
  152. static HRESULT ConnectFilters2(IGraphBuilder *pGraph, IBaseFilter *pSrc, IBaseFilter *pDest)
  153. {
  154. IPin *pOut;
  155. HRESULT hr;
  156. if ((pGraph == NULL) || (pSrc == NULL) || (pDest == NULL))
  157. return E_POINTER;
  158. // Find an output pin on the first filter.
  159. pOut = 0;
  160. hr = GetUnconnectedPin(pSrc, PINDIR_OUTPUT, &pOut);
  161. if (FAILED(hr))
  162. return hr;
  163. hr = ConnectFilters1(pGraph, pOut, pDest);
  164. pOut->lpVtbl->Release(pOut);
  165. return hr;
  166. }
  167. static HRESULT WalkFilterCategory(REFCLSID category,
  168. HRESULT (*OnDeviceCB)(int index,IMoniker *pMoniker,VOID *pUserData1,VOID *pUserData2),
  169. VOID * pUserData1,
  170. VOID* pUserData2)
  171. {
  172. HRESULT hr;
  173. ICreateDevEnum *pDevEnum = NULL;
  174. IEnumMoniker *pEnumMoniker = NULL;
  175. IMoniker *pMoniker = NULL;
  176. BOOL quit;
  177. int i = 0;
  178. if (!OnDeviceCB)
  179. return E_INVALIDARG;
  180. hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
  181. &IID_ICreateDevEnum, (void**)&pDevEnum);
  182. if (FAILED(hr))
  183. return hr;
  184. hr = pDevEnum->lpVtbl->CreateClassEnumerator(pDevEnum, category,
  185. &pEnumMoniker, 0);
  186. if (FAILED(hr)) {
  187. pDevEnum->lpVtbl->Release(pDevEnum);
  188. return hr;
  189. } else if (hr == S_OK) {
  190. pEnumMoniker->lpVtbl->Reset(pEnumMoniker);
  191. quit = FALSE;
  192. while (pEnumMoniker->lpVtbl->Next(pEnumMoniker, 1, &pMoniker, NULL) == S_OK && !quit) {
  193. hr = OnDeviceCB(i++, pMoniker, pUserData1, pUserData2);
  194. quit = FAILED(hr) || (hr == S_OK);
  195. pMoniker->lpVtbl->Release(pMoniker);
  196. }
  197. pEnumMoniker->lpVtbl->Release(pEnumMoniker);
  198. pDevEnum->lpVtbl->Release(pDevEnum);
  199. } else {
  200. if (pEnumMoniker)
  201. pEnumMoniker->lpVtbl->Release(pEnumMoniker);
  202. pDevEnum->lpVtbl->Release(pDevEnum);
  203. }
  204. return hr;
  205. }
  206. static HRESULT CreateFilterByFriendlyNameCB(int index, IMoniker *pMoniker, void *pUserData1, void *pUserData2)
  207. {
  208. HRESULT hr;
  209. IBaseFilter *pBaseFilter = NULL;
  210. WCHAR *szName = (WCHAR*)pUserData2;
  211. if (pMoniker) {
  212. IPropertyBag *pPropBag;
  213. VARIANT name;
  214. hr = pMoniker->lpVtbl->BindToStorage(pMoniker, NULL, NULL,
  215. &IID_IPropertyBag, (void**)&pPropBag);
  216. if (FAILED(hr))
  217. return S_FALSE;
  218. VariantInit(&name);
  219. name.vt = VT_BSTR;
  220. hr = pPropBag->lpVtbl->Read(pPropBag, L"FriendlyName", &name, NULL);
  221. if (FAILED(hr)) {
  222. pPropBag->lpVtbl->Release(pPropBag);
  223. VariantClear(&name);
  224. return S_FALSE;
  225. }
  226. if (SysStringByteLen(name.bstrVal) && wcscmp(name.bstrVal, szName)==0) {
  227. hr = pMoniker->lpVtbl->BindToObject(pMoniker, NULL, NULL,
  228. &IID_IBaseFilter, (void**)&pBaseFilter);
  229. if (SUCCEEDED(hr)) {
  230. *(IBaseFilter**)pUserData1 = pBaseFilter;
  231. hr = S_OK;
  232. }
  233. }
  234. pPropBag->lpVtbl->Release(pPropBag);
  235. VariantClear(&name);
  236. } else {
  237. hr = E_POINTER;
  238. }
  239. return hr;
  240. }
  241. static HRESULT CreateFilterByFriendlyName(REFCLSID category, void **ppBaseFilter, WCHAR* szName)
  242. {
  243. HRESULT hr = WalkFilterCategory(category,
  244. &CreateFilterByFriendlyNameCB, ppBaseFilter, szName);
  245. return hr == S_FALSE ? E_NOINTERFACE : hr;
  246. }
  247. static HRESULT CreateFilterByIndexCB(int index, IMoniker *pMoniker, void *pUserData1, void *pUserData2)
  248. {
  249. HRESULT hr;
  250. IBaseFilter *pBaseFilter = NULL;
  251. int *pidx = (int*)pUserData2;
  252. if (pMoniker && index == *pidx) {
  253. hr = pMoniker->lpVtbl->BindToObject(pMoniker, NULL, NULL,
  254. &IID_IBaseFilter, (void**)&pBaseFilter);
  255. if (FAILED(hr))
  256. return hr;
  257. *(IBaseFilter**)pUserData1 = pBaseFilter;
  258. return S_OK;
  259. }
  260. return S_FALSE;
  261. }
  262. static HRESULT CreateFilterByIndex(REFCLSID category,
  263. void **ppBaseFilter,
  264. int idx)
  265. {
  266. HRESULT hr = WalkFilterCategory(category, &CreateFilterByIndexCB,
  267. ppBaseFilter, &idx);
  268. return hr == S_FALSE ? E_NOINTERFACE : hr;
  269. }
  270. static HRESULT RemoveGraphAllFilters(IGraphBuilder *pGraphBuilder)
  271. {
  272. HRESULT hr;
  273. IEnumFilters *pEnumFilters;
  274. if (!pGraphBuilder)
  275. return E_POINTER;
  276. hr = pGraphBuilder->lpVtbl->EnumFilters(pGraphBuilder, &pEnumFilters);
  277. if (SUCCEEDED(hr)) {
  278. IBaseFilter *pFilter;
  279. pEnumFilters->lpVtbl->Reset(pEnumFilters);
  280. while (pEnumFilters->lpVtbl->Next(pEnumFilters, 1, &pFilter, NULL) == S_OK) {
  281. pGraphBuilder->lpVtbl->RemoveFilter(pGraphBuilder, pFilter);
  282. pFilter->lpVtbl->Release(pFilter);
  283. pEnumFilters->lpVtbl->Reset(pEnumFilters);
  284. }
  285. pEnumFilters->lpVtbl->Release(pEnumFilters);
  286. }
  287. return hr;
  288. }
  289. ////////////////////////////////////////////////////////////////////////////////////////////////
  290. static const int mode_width[VIDEOCAP_MAX_MODE] = {
  291. VIDEOCAP_SQCIF_WIDTH,VIDEOCAP_QQVGA_WIDTH,
  292. VIDEOCAP_QCIF_WIDTH,VIDEOCAP_QVGA_WIDTH,
  293. VIDEOCAP_CIF_WIDTH,VIDEOCAP_VGA_WIDTH,
  294. VIDEOCAP_4CIF_WIDTH,VIDEOCAP_SVGA_WIDTH,
  295. VIDEOCAP_NHD_WIDTH,VIDEOCAP_SXGA_WIDTH,
  296. VIDEOCAP_720P_WIDTH,VIDEOCAP_1080P_WIDTH,
  297. };
  298. static const int mode_height[VIDEOCAP_MAX_MODE] = {
  299. VIDEOCAP_SQCIF_HEIGHT,VIDEOCAP_QQVGA_HEIGHT,
  300. VIDEOCAP_QCIF_HEIGHT,VIDEOCAP_QVGA_HEIGHT,
  301. VIDEOCAP_CIF_HEIGHT,VIDEOCAP_VGA_HEIGHT,
  302. VIDEOCAP_4CIF_HEIGHT,VIDEOCAP_SVGA_HEIGHT,
  303. VIDEOCAP_NHD_HEIGHT,VIDEOCAP_SXGA_HEIGHT,
  304. VIDEOCAP_720P_HEIGHT,VIDEOCAP_1080P_HEIGHT,
  305. };
  306. typedef struct async_cap_t
  307. {
  308. LIST_ENTRY entry;
  309. HANDLE evt;
  310. int result;
  311. video_frame *ref_cap_frame;
  312. }async_cap_t;
  313. /**
  314. * we use DirectShow to capture video frames
  315. */
  316. typedef struct videocap
  317. {
  318. IBaseFilter *sourcefilter;
  319. IBaseFilter *cpcfilter; // color space converter if necessary
  320. IBaseFilter *avidecfilter;
  321. IBaseFilter *grabberfilter;
  322. IBaseFilter *horflipfilter;
  323. IBaseFilter *renderfilter;
  324. ISampleGrabber *grabber;
  325. IGraphBuilder *graphbuilder;
  326. IMediaControl *mc;
  327. IMediaEvent *me;
  328. IVideoWindow *videowindow;
  329. #ifdef _DEBUG
  330. DWORD dwROTRegister; // register RunningObjectTable
  331. #endif
  332. videocap_param param;
  333. int running;
  334. HANDLE thread_background;
  335. HANDLE evt_thread_exit;
  336. int cap_index;
  337. CRITICAL_SECTION cap_cs;
  338. video_frame cap_frame;
  339. CRITICAL_SECTION async_cap_cs;
  340. LIST_ENTRY async_cap_list;
  341. LONG grab_cb_count;
  342. CRITICAL_SECTION res_cs;
  343. video_frame res_frame;
  344. struct SwsContext *sws_context; /* for image scaling and format converting */
  345. } videocap;
  346. HRESULT set_video_source_format(videocap *);
  347. void release_all_interfaces(videocap *);
  348. typedef struct ISampleGrabberCBImpl
  349. {
  350. struct ISampleGrabberCBVtbl *lpVtbl;
  351. struct videocap* vcap;
  352. struct ISampleGrabberCBVtbl vtbl;
  353. }ISampleGrabberCBImpl;
  354. static HRESULT STDMETHODCALLTYPE QueryInterface(ISampleGrabberCB * This,
  355. REFIID riid,
  356. void** ppvObject)
  357. {
  358. if (This == NULL || riid == NULL || ppvObject == NULL || *ppvObject == NULL)
  359. return E_POINTER;
  360. if (IsEqualIID(riid, &IID_IUnknown)) {
  361. *ppvObject = (void*)This;
  362. return S_OK;
  363. } else if (IsEqualIID(riid, &IID_ISampleGrabberCB)) {
  364. *ppvObject = (void*)This;
  365. return S_OK;
  366. }
  367. return E_NOTIMPL;
  368. }
  369. static ULONG STDMETHODCALLTYPE AddRef(ISampleGrabberCB * This)
  370. {
  371. return 1;
  372. }
  373. static ULONG STDMETHODCALLTYPE Release(ISampleGrabberCB * This)
  374. {
  375. free(This);
  376. return 0;
  377. }
  378. static HRESULT STDMETHODCALLTYPE SampleCB(ISampleGrabberCB * This, double SampleTime,IMediaSample *pSample)
  379. {
  380. return E_NOTIMPL;
  381. }
  382. static HRESULT STDMETHODCALLTYPE BufferCB(ISampleGrabberCB * This, double SampleTime,BYTE *pBuffer,long BufferLen)
  383. {
  384. ISampleGrabberCBImpl *pImpl = (ISampleGrabberCBImpl *)This;
  385. videocap *vcap = pImpl->vcap;
  386. int linesize = BufferLen / mode_height[vcap->param.cap_mode];
  387. struct SwsContext *sws;
  388. char*Buffertmp=(char*)malloc(mode_width[vcap->param.cap_mode]*mode_height[vcap->param.cap_mode]*3);
  389. uint8_t *src_data[4];
  390. int src_linesize[4];
  391. unsigned char *dst[4] = {(unsigned char*)Buffertmp,NULL,NULL,NULL};
  392. int dst_linesize[4] = {mode_width[vcap->param.cap_mode]*3,0,0,0};
  393. char*buffer;
  394. int oriLen = mode_height[vcap->param.cap_mode]*mode_width[vcap->param.cap_mode]*3;
  395. get_output_mediatype(vcap);
  396. //MFLog(LM_SYSTEM,(LB,"BufferLen=%d,w=%d,h=%d, g_width=%d,g_height=%d",BufferLen,mode_width[vcap->param.cap_mode], mode_height[vcap->param.cap_mode],g_width,g_height));
  397. if (BufferLen != oriLen)
  398. {
  399. //计算目标图像比例
  400. int srcH;
  401. //float fDstScale = (float)(mode_width[vcap->param.cap_mode]/mode_height[vcap->param.cap_mode]);
  402. float fDstScale = (float)mode_width[vcap->param.cap_mode]/(float)mode_height[vcap->param.cap_mode];
  403. float fSrcScale = (float)g_width/(float)g_height;
  404. //MFLog(LM_SYSTEM,(LB,"fDstScale=%f, fSrcScale=%f", fDstScale, fSrcScale));
  405. if (fSrcScale!=fDstScale)
  406. {
  407. //计算偏移量
  408. int nWidth,nHeight,nOffset=0;
  409. if (fSrcScale > fDstScale)
  410. {
  411. //宽度过大
  412. //nHeight = g_height;
  413. //nWidth = g_height*fDstScale;
  414. //nOffset = (g_height-nHeight)/2*nWidth*3;
  415. //MFLog(LM_SYSTEM,(LB,"too width"));
  416. return 0;
  417. }
  418. else if (fSrcScale < fDstScale)
  419. {
  420. //高度过长
  421. nWidth = g_width;
  422. nHeight = (int)ceil(g_width/fDstScale);
  423. nOffset = (g_height-nHeight)/2*nWidth*3;
  424. //MFLog(LM_SYSTEM,(LB,"too height,nWidth=%d,nHeight=%d,nOffset=%d",nWidth,nHeight,nOffset));
  425. }
  426. //计算等比例变换需要的SWS
  427. sws=sws_getContext(nWidth,nHeight,PIX_FMT_BGR24, mode_width[vcap->param.cap_mode], mode_height[vcap->param.cap_mode], PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
  428. av_image_alloc(src_data, src_linesize,g_width,g_height, PIX_FMT_BGR24, 1);
  429. memcpy(src_data[0],pBuffer+nOffset,nWidth*nHeight*3); //Y
  430. srcH = nHeight;
  431. }
  432. else
  433. {
  434. //MFLog(LM_SYSTEM,(LB,"need scale"));
  435. //计算等比例变换需要的SWS
  436. sws=sws_getContext(g_width,g_height,PIX_FMT_BGR24, mode_width[vcap->param.cap_mode], mode_height[vcap->param.cap_mode], PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
  437. av_image_alloc(src_data, src_linesize,g_width,g_height, PIX_FMT_BGR24, 1);
  438. memcpy(src_data[0],pBuffer,BufferLen); //Y
  439. srcH = g_height;
  440. }
  441. //分辨率转换
  442. sws_scale(sws, src_data, src_linesize, 0, srcH, dst, dst_linesize);
  443. sws_freeContext(sws);
  444. av_freep(&src_data[0]);
  445. buffer = Buffertmp;
  446. BufferLen = mode_width[vcap->param.cap_mode]*mode_height[vcap->param.cap_mode]*3;
  447. }
  448. else
  449. {
  450. //MFLog(LM_SYSTEM,(LB,"normal"));
  451. buffer = (char*)pBuffer;
  452. }
  453. linesize = BufferLen / mode_height[vcap->param.cap_mode];
  454. if (vcap->param.option & VIDEOCAP_OPT_ENABLE_GRAB)
  455. {
  456. if (vcap->cap_index++%3 == 0)
  457. { /* 3 is grabbing interval */
  458. EnterCriticalSection(&vcap->cap_cs);
  459. //memcpy(vcap->cap_frame.data[0], pBuffer, BufferLen);
  460. memcpy(vcap->cap_frame.data[0], buffer, BufferLen);
  461. LeaveCriticalSection(&vcap->cap_cs);
  462. }
  463. }
  464. if (vcap->grab_cb_count)
  465. {
  466. if (vcap->param.on_grab)
  467. {
  468. video_frame frm = {0};
  469. frm.data[0] = buffer;
  470. frm.linesize[0] = linesize;
  471. frm.width = mode_width[vcap->param.cap_mode];
  472. frm.height = mode_height[vcap->param.cap_mode];
  473. frm.format = VIDEO_FORMAT_RGB24;
  474. vcap->param.on_grab(vcap->param.user_data, &frm);
  475. }
  476. InterlockedDecrement(&vcap->grab_cb_count);
  477. }
  478. if (vcap->param.option & VIDEOCAP_OPT_ENABLE_ASYNC_GRAB)
  479. {
  480. async_cap_t *pos, *n;
  481. EnterCriticalSection(&vcap->async_cap_cs);
  482. ListEntry_ForEachSafe(pos, n, &vcap->async_cap_list, async_cap_t, entry)
  483. {
  484. ListEntry_DeleteNode(&pos->entry);
  485. pos->result = 0;
  486. memcpy(pos->ref_cap_frame->data[0], buffer, BufferLen);
  487. SetEvent(pos->evt);
  488. }
  489. LeaveCriticalSection(&vcap->async_cap_cs);
  490. }
  491. if (vcap->param.on_frame_raw)
  492. {
  493. video_frame frm = {0};
  494. frm.data[0] = buffer;
  495. frm.linesize[0] = linesize;
  496. frm.width = mode_width[vcap->param.cap_mode];
  497. frm.height = mode_height[vcap->param.cap_mode];
  498. frm.format = VIDEO_FORMAT_RGB24;
  499. vcap->param.on_frame_raw(vcap->param.user_data, &frm);
  500. }
  501. if (vcap->param.option & VIDEOCAP_OPT_EANBLE_RESIZE)
  502. { /* user enable resizing */
  503. unsigned char *src_data[4];
  504. int src_linesize[4] = {linesize, 0, 0, 0};
  505. src_data[0] = (unsigned char*)buffer;
  506. src_data[1] = NULL;
  507. src_data[2] = NULL;
  508. src_data[3] = NULL;
  509. EnterCriticalSection(&vcap->res_cs);
  510. if (vcap->param.option & VIDEOCAP_OPT_ENABLE_FLIP)
  511. {
  512. src_data[0] += (mode_height[vcap->param.cap_mode]-1)*src_linesize[0];
  513. src_linesize[0] = -src_linesize[0];
  514. sws_scale(vcap->sws_context, src_data, src_linesize, 0, mode_height[vcap->param.cap_mode], vcap->res_frame.data, vcap->res_frame.linesize);
  515. }
  516. else
  517. {
  518. sws_scale(vcap->sws_context, src_data, src_linesize,
  519. 0, mode_height[vcap->param.cap_mode], vcap->res_frame.data, vcap->res_frame.linesize);
  520. }
  521. if (vcap->param.on_frame)
  522. {
  523. vcap->param.on_frame(vcap->param.user_data, &vcap->res_frame);
  524. }
  525. LeaveCriticalSection(&vcap->res_cs);
  526. }
  527. else
  528. {
  529. if (vcap->param.on_frame)
  530. {
  531. video_frame frame;
  532. memset(&frame, 0, sizeof(video_frame));
  533. frame.width = mode_width[vcap->param.cap_mode];
  534. frame.height = mode_height[vcap->param.cap_mode];
  535. frame.data[0] = buffer;
  536. frame.linesize[0] = linesize;
  537. frame.format = VIDEO_FORMAT_RGB24;
  538. vcap->param.on_frame(vcap->param.user_data, &frame);
  539. //MFLog(LM_SYSTEM,(LB,"normal"));
  540. }
  541. }
  542. free(Buffertmp);
  543. return S_OK;
  544. }
  545. static ISampleGrabberCB* CreateISampleGrabberCBImpl(videocap *vcap)
  546. {
  547. ISampleGrabberCBImpl *pImpl =
  548. (ISampleGrabberCBImpl*)malloc(sizeof(ISampleGrabberCBImpl));
  549. if (pImpl) {
  550. pImpl->lpVtbl = &pImpl->vtbl;
  551. pImpl->lpVtbl->QueryInterface = &QueryInterface;
  552. pImpl->lpVtbl->AddRef = &AddRef;
  553. pImpl->lpVtbl->Release = &Release;
  554. pImpl->lpVtbl->BufferCB = &BufferCB;
  555. pImpl->lpVtbl->SampleCB = &SampleCB;
  556. pImpl->vcap = vcap;
  557. }
  558. return (ISampleGrabberCB*)pImpl;
  559. }
  560. static HRESULT TryConnectFilters(IGraphBuilder *pGraphBuilder, int cnt, IBaseFilter *pFilters[])
  561. {
  562. HRESULT hr = S_OK;
  563. int i;
  564. if (!pGraphBuilder)
  565. return E_POINTER;
  566. if (cnt == 0)
  567. return S_OK;
  568. if (!pFilters)
  569. return E_POINTER;
  570. for (i = 1; i < cnt; ++i)
  571. {
  572. IBaseFilter *src = pFilters[i-1];
  573. IBaseFilter *dst = pFilters[i];
  574. if (!src || !dst)
  575. return E_POINTER;
  576. hr = ConnectFilters2(pGraphBuilder, src, dst);
  577. if (FAILED(hr))
  578. {
  579. return hr;
  580. }
  581. }
  582. return hr;
  583. }
  584. ////////////////////////////////////////////////////////////////////////////////////////////////
  585. /* used for device lost event listenning */
  586. static DWORD WINAPI BackgroundThread(LPVOID *param)
  587. {
  588. struct videocap *vcap = (struct videocap *)param;
  589. int quit = 0;
  590. HANDLE t;
  591. HANDLE evts[2] = {0};
  592. HANDLE hProcess = GetCurrentProcess();
  593. HRESULT hr;
  594. hr = vcap->me->lpVtbl->GetEventHandle(vcap->me, (OAEVENT*)&t);
  595. if (FAILED(hr))
  596. return 0;
  597. if (DuplicateHandle(hProcess, t, hProcess, &evts[0], DUPLICATE_SAME_ACCESS, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
  598. return 0;
  599. evts[1] = vcap->evt_thread_exit;
  600. while (!quit)
  601. {
  602. DWORD dwResult = WaitForMultipleObjects(2, evts, FALSE, INFINITE) - WAIT_OBJECT_0;
  603. if (0 == dwResult)
  604. {
  605. long lEventCode;
  606. LONG_PTR lParam1;
  607. LONG_PTR lParam2;
  608. vcap->me->lpVtbl->GetEvent(vcap->me, &lEventCode, &lParam1, &lParam2, INFINITE);
  609. if (lEventCode == EC_DEVICE_LOST || lEventCode == EC_ERRORABORT)
  610. {
  611. if (vcap->param.on_device_lost)
  612. (*vcap->param.on_device_lost)(vcap->param.user_data);
  613. quit = 1;
  614. }
  615. vcap->me->lpVtbl->FreeEventParams(vcap->me, lEventCode, lParam1, lParam2);
  616. }
  617. else
  618. {
  619. quit = 1;
  620. }
  621. }
  622. CloseHandle(evts[0]);
  623. return 0;
  624. }
  625. ////////////////////////////////////////////////////////////////////////////////////////////////
  626. int videocap_create(videocap_t *h ,videocap_param *param)
  627. {
  628. videocap *vcap;
  629. //RvcLog_init("./videoframework",1);
  630. /* check param */
  631. if (!param)
  632. return -1;
  633. if (param->cap_mode < 0 || param->cap_mode >= VIDEOCAP_MAX_MODE)
  634. return -1;
  635. if (param->frame_fmt != VIDEO_FORMAT_I420 && param->frame_fmt != VIDEO_FORMAT_RGB24)
  636. return -1;
  637. if (param->fps < 1.0 ||
  638. param->fps > 100.0)
  639. return -1;
  640. if (param->pre_hwnd)
  641. {
  642. if (!IsWindow(param->pre_hwnd))
  643. return -1;
  644. if (param->pre_width < 0)
  645. return -1;
  646. if (param->pre_height < 0)
  647. return -1;
  648. }
  649. if (param->dev_id < 0)
  650. return -1;
  651. if (param->frame_fmt == VIDEO_FORMAT_I420 && !(param->option&VIDEOCAP_OPT_EANBLE_RESIZE)) {
  652. param->res_mode = param->cap_mode;
  653. param->option |= VIDEOCAP_OPT_EANBLE_RESIZE;
  654. }
  655. if (param->option & VIDEOCAP_OPT_EANBLE_RESIZE) {
  656. if (param->res_mode < VIDEOCAP_FRAME_SQCIF || param->res_mode > VIDEOCAP_FRAME_SVGA)
  657. return -1;
  658. }
  659. vcap = (videocap*)malloc(sizeof(videocap));
  660. if (!vcap)
  661. return -1;
  662. ZeroMemory((void*)vcap, sizeof(videocap));
  663. memcpy(&vcap->param, param, sizeof(videocap_param));
  664. if (param->option & VIDEOCAP_OPT_ENABLE_GRAB) {
  665. int width = mode_width[param->cap_mode];
  666. int height = mode_height[param->cap_mode];
  667. if (video_frame_alloc(width, height, VIDEO_FORMAT_RGB24, &vcap->cap_frame) != 0) {
  668. free(vcap);
  669. return -1;
  670. }
  671. video_frame_fill_black(&vcap->cap_frame);
  672. InitializeCriticalSection(&vcap->cap_cs);
  673. }
  674. if (param->option & VIDEOCAP_OPT_ENABLE_ASYNC_GRAB) {
  675. InitializeCriticalSection(&vcap->async_cap_cs);
  676. }
  677. if (param->option & VIDEOCAP_OPT_EANBLE_RESIZE) {
  678. int width = mode_width[param->res_mode];
  679. int height = mode_height[param->res_mode];
  680. if (video_frame_alloc(width, height, param->frame_fmt, &vcap->res_frame) != 0) {
  681. if (param->option & VIDEOCAP_OPT_ENABLE_GRAB) {
  682. DeleteCriticalSection(&vcap->cap_cs);
  683. video_frame_free(&vcap->cap_frame);
  684. }
  685. free(vcap);
  686. return -1;
  687. }
  688. video_frame_fill_black(&vcap->res_frame);
  689. vcap->sws_context = sws_getContext(mode_width[param->cap_mode],
  690. mode_height[param->cap_mode],
  691. PIX_FMT_BGR24,
  692. mode_width[param->res_mode],
  693. mode_height[param->res_mode],
  694. vcap->param.frame_fmt==VIDEO_FORMAT_RGB24 ? PIX_FMT_BGR24 : PIX_FMT_YUV420P,
  695. SWS_FAST_BILINEAR,
  696. NULL,
  697. NULL,
  698. NULL);
  699. if (!vcap->sws_context) {
  700. video_frame_free(&vcap->res_frame);
  701. if (param->option & VIDEOCAP_OPT_ENABLE_GRAB) {
  702. DeleteCriticalSection(&vcap->cap_cs);
  703. video_frame_free(&vcap->cap_frame);
  704. }
  705. free(vcap);
  706. return -1;
  707. }
  708. InitializeCriticalSection(&vcap->res_cs);
  709. }
  710. *h = vcap;
  711. return 0;
  712. }
  713. void videocap_destroy(videocap_t h)
  714. {
  715. if (h->param.option & VIDEOCAP_OPT_ENABLE_GRAB) {
  716. DeleteCriticalSection(&h->cap_cs);
  717. video_frame_free(&h->cap_frame);
  718. }
  719. if (h->param.option & VIDEOCAP_OPT_ENABLE_ASYNC_GRAB) {
  720. DeleteCriticalSection(&h->async_cap_cs);
  721. }
  722. if (h->param.option & VIDEOCAP_OPT_EANBLE_RESIZE) {
  723. sws_freeContext(h->sws_context);
  724. DeleteCriticalSection(&h->res_cs);
  725. video_frame_free(&h->res_frame);
  726. }
  727. free(h);
  728. }
  729. int videocap_adj_brightness(videocap_t h, int nValue)
  730. {
  731. IAMVideoProcAmp *pProcAmp = 0;
  732. HRESULT hr = h->sourcefilter->lpVtbl->QueryInterface(h->sourcefilter, &IID_IAMVideoProcAmp, (void**)&pProcAmp);
  733. if(SUCCEEDED(hr))
  734. {
  735. long Min, Max, Step, Default, Flags, Val;
  736. // 亮度.
  737. if(( nValue > 0 )&&(nValue < 100 ))
  738. {
  739. hr = pProcAmp->lpVtbl->GetRange(pProcAmp, VideoProcAmp_Brightness, &Min, &Max, &Step,
  740. &Default, &Flags);
  741. if(SUCCEEDED(hr))
  742. {
  743. Flags = VideoProcAmp_Flags_Manual;
  744. hr = pProcAmp->lpVtbl->Get(pProcAmp,VideoProcAmp_Brightness, &Val, &Flags);
  745. Val = ((Max-Min)*nValue/100)+Min;
  746. if (Val>Max)
  747. {
  748. Val = Max;
  749. }
  750. else if (Val<Min)
  751. {
  752. Val = Min;
  753. }
  754. hr = pProcAmp->lpVtbl->Set(pProcAmp,VideoProcAmp_Brightness, Val, Flags);
  755. }
  756. }
  757. }
  758. return hr;
  759. }
  760. int videocap_set_autobrightness(videocap_t h)
  761. {
  762. IAMVideoProcAmp *pProcAmp = 0;
  763. HRESULT hr = h->sourcefilter->lpVtbl->QueryInterface(h->sourcefilter, &IID_IAMVideoProcAmp, (void**)&pProcAmp);
  764. if(SUCCEEDED(hr))
  765. {
  766. long Min, Max, Step, Default, Flags, Val;
  767. // 亮度.
  768. hr = pProcAmp->lpVtbl->GetRange(pProcAmp, VideoProcAmp_Brightness, &Min, &Max, &Step,
  769. &Default, &Flags);
  770. if(SUCCEEDED(hr))
  771. {
  772. Flags = VideoProcAmp_Flags_Auto;
  773. Val = Min + (Max-Min)/2;
  774. hr = pProcAmp->lpVtbl->Set(pProcAmp,VideoProcAmp_Brightness, Val, Flags);
  775. }
  776. }
  777. return hr;
  778. }
  779. int videocap_get_brightness(videocap_t h,int*nValue)
  780. {
  781. IAMVideoProcAmp *pProcAmp = 0;
  782. HRESULT hr = h->sourcefilter->lpVtbl->QueryInterface(h->sourcefilter, &IID_IAMVideoProcAmp, (void**)&pProcAmp);
  783. if(SUCCEEDED(hr))
  784. {
  785. long Min, Max, Step, Default, Flags, Val;
  786. // 亮度.
  787. hr = pProcAmp->lpVtbl->GetRange(pProcAmp, VideoProcAmp_Brightness, &Min, &Max, &Step,
  788. &Default, &Flags);
  789. if(SUCCEEDED(hr))
  790. {
  791. hr = pProcAmp->lpVtbl->Get(pProcAmp,VideoProcAmp_Brightness, &Val, &Flags);
  792. *nValue = (Val-Min)*100/(Max-Min);
  793. }
  794. }
  795. return hr;
  796. }
  797. int videocap_start(videocap_t h)
  798. {
  799. HRESULT hr;
  800. if (!h)
  801. return VIDEOCAP_ERROR;
  802. h->grab_cb_count = 0;
  803. ListEntry_InitHead(&h->async_cap_list);
  804. // create filter graph
  805. hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
  806. &IID_IGraphBuilder, (void**)&h->graphbuilder);
  807. if (FAILED(hr))
  808. {
  809. //Dbg("CoCreateInstance failed.");
  810. goto on_error;
  811. }
  812. hr = h->graphbuilder->lpVtbl->QueryInterface(h->graphbuilder,
  813. &IID_IMediaControl, (void**)&h->mc);
  814. if (FAILED(hr))
  815. {
  816. //Dbg("QueryInterface failed.");
  817. goto on_error;
  818. }
  819. hr = h->graphbuilder->lpVtbl->QueryInterface(h->graphbuilder,
  820. &IID_IMediaEvent, (void**)&h->me);
  821. if (FAILED(hr))
  822. {
  823. //Dbg("QueryInterface IID_IMediaEvent failed.");
  824. goto on_error;
  825. }
  826. #ifdef _DEBUG
  827. {
  828. IUnknown *pUnk;
  829. hr = h->graphbuilder->lpVtbl->QueryInterface(h->graphbuilder,
  830. &IID_IUnknown, (void**)&pUnk);
  831. if (FAILED(hr))
  832. goto on_error;
  833. hr = AddToRot(pUnk, &h->dwROTRegister);
  834. pUnk->lpVtbl->Release(pUnk);
  835. if (FAILED(hr))
  836. goto on_error;
  837. }
  838. #endif
  839. /* create video source filter and add to graph */
  840. hr = CreateFilterByIndex(&CLSID_VideoInputDeviceCategory,
  841. (void**)&h->sourcefilter, h->param.dev_id);
  842. if (FAILED(hr))
  843. {
  844. goto on_error;
  845. }
  846. hr = h->graphbuilder->lpVtbl->AddFilter(h->graphbuilder, h->sourcefilter, L"videosource");
  847. if (FAILED(hr))
  848. goto on_error;
  849. hr = set_video_source_format((videocap*)h);
  850. if (FAILED(hr))
  851. {
  852. goto on_error;
  853. }
  854. hr = CoCreateInstance(&CLSID_Colour, NULL, CLSCTX_INPROC_SERVER,
  855. &IID_IBaseFilter, (void**) &h->cpcfilter);
  856. if (FAILED(hr))
  857. {
  858. goto on_error;
  859. }
  860. hr = h->graphbuilder->lpVtbl->AddFilter(h->graphbuilder, h->cpcfilter, L"cpcfilter");
  861. if (FAILED(hr))
  862. {
  863. goto on_error;
  864. }
  865. hr = CoCreateInstance(&CLSID_AVIDec, NULL, CLSCTX_INPROC_SERVER,
  866. &IID_IBaseFilter, (void**) &h->avidecfilter);
  867. if (FAILED(hr))
  868. {
  869. goto on_error;
  870. }
  871. hr = h->graphbuilder->lpVtbl->AddFilter(h->graphbuilder, h->avidecfilter, L"avidecfilter");
  872. if (FAILED(hr))
  873. {
  874. goto on_error;
  875. }
  876. // create grabber filter and add to graph
  877. hr = CoCreateInstance(&CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
  878. &IID_IBaseFilter, (void**) &h->grabberfilter);
  879. if (FAILED(hr))
  880. {
  881. goto on_error;
  882. }
  883. hr = h->graphbuilder->lpVtbl->AddFilter(h->graphbuilder, h->grabberfilter, L"grabber");
  884. if (FAILED(hr))
  885. {
  886. goto on_error;
  887. }
  888. hr = h->grabberfilter->lpVtbl->QueryInterface(h->grabberfilter,
  889. &IID_ISampleGrabber, (void**)&h->grabber);
  890. if (FAILED(hr))
  891. {
  892. goto on_error;
  893. }
  894. if (1)
  895. {
  896. int width = mode_width[h->param.cap_mode];
  897. int height = mode_height[h->param.cap_mode];
  898. AM_MEDIA_TYPE *mt = (AM_MEDIA_TYPE*)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
  899. VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER*)CoTaskMemAlloc(sizeof(VIDEOINFOHEADER));
  900. memset(mt, 0, sizeof(AM_MEDIA_TYPE));
  901. memset(pvi, 0, sizeof(VIDEOINFOHEADER));
  902. mt->lSampleSize = width * height * 3;
  903. mt->majortype = MEDIATYPE_Video;
  904. mt->subtype = MEDIASUBTYPE_RGB24;
  905. mt->formattype = FORMAT_VideoInfo;
  906. mt->bFixedSizeSamples = TRUE;
  907. mt->bTemporalCompression = FALSE;
  908. mt->cbFormat = sizeof(VIDEOINFOHEADER);
  909. mt->pbFormat = (BYTE*)pvi;
  910. pvi->bmiHeader.biWidth = width;
  911. pvi->bmiHeader.biHeight = height;
  912. pvi->bmiHeader.biSizeImage = width * height * 3;
  913. pvi->bmiHeader.biBitCount = 24;
  914. pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  915. pvi->AvgTimePerFrame = (REFERENCE_TIME)(10000000 / h->param.fps);
  916. pvi->dwBitRate = (DWORD)(width*height*24*h->param.fps);
  917. hr = h->grabber->lpVtbl->SetMediaType(h->grabber, mt);
  918. FreeMediaTypeEx(mt);
  919. if (FAILED(hr))
  920. {
  921. //Dbg("SetMediaType error, err=%08xd", hr);
  922. goto on_error;
  923. }
  924. }
  925. h->grabber->lpVtbl->SetOneShot(h->grabber, FALSE);
  926. {
  927. ISampleGrabberCB *pImpl = CreateISampleGrabberCBImpl(h);
  928. if (!pImpl)
  929. goto on_error;
  930. // 0: SampleCB
  931. // 1: BufferCB
  932. h->grabber->lpVtbl->SetCallback(h->grabber, pImpl, 1);////////////////////////
  933. }
  934. if (h->param.option & VIDEOCAP_OPT_HOZFLIP) {
  935. h->horflipfilter = (IBaseFilter*)videohorflip_create_filter();
  936. if (!h->horflipfilter)
  937. goto on_error;
  938. hr = h->graphbuilder->lpVtbl->AddFilter(h->graphbuilder, h->horflipfilter, L"horflip");
  939. if (FAILED(hr))
  940. {
  941. goto on_error;
  942. }
  943. }
  944. // create render filter and add to graph
  945. {
  946. hr = CoCreateInstance(h->param.pre_hwnd ? &CLSID_VideoRendererDefault : &CLSID_NullRenderer,
  947. NULL, CLSCTX_INPROC_SERVER, &IID_IBaseFilter, (void**) &h->renderfilter);
  948. if (FAILED(hr))
  949. goto on_error;
  950. }
  951. hr = h->graphbuilder->lpVtbl->AddFilter(h->graphbuilder, h->renderfilter, L"renderfilter");
  952. if (FAILED(hr))
  953. {
  954. goto on_error;
  955. }
  956. // try connect filters
  957. if (!(h->param.option & VIDEOCAP_OPT_HOZFLIP)) {
  958. IBaseFilter *Filter1[] = {h->sourcefilter, h->grabberfilter, h->renderfilter};
  959. IBaseFilter *Filter2[] = {h->sourcefilter, h->cpcfilter, h->grabberfilter, h->renderfilter};
  960. IBaseFilter *Filter3[] = {h->sourcefilter, h->avidecfilter, h->grabberfilter, h->renderfilter};
  961. IBaseFilter *Filter4[] = {h->sourcefilter, h->avidecfilter, h->cpcfilter, h->grabberfilter, h->renderfilter};
  962. struct {
  963. int cnt;
  964. IBaseFilter **Filters;
  965. } tpl[] = {
  966. {ARRAYSIZE(Filter1), Filter1},
  967. {ARRAYSIZE(Filter2), Filter2},
  968. {ARRAYSIZE(Filter3), Filter3},
  969. {ARRAYSIZE(Filter4), Filter4},
  970. };
  971. int i;
  972. for (i = 0; i < ARRAYSIZE(tpl); ++i) {
  973. Sleep(50);
  974. hr = TryConnectFilters(h->graphbuilder, tpl[i].cnt, tpl[i].Filters);
  975. if (SUCCEEDED(hr)) {
  976. break;
  977. }
  978. }
  979. if (i == ARRAYSIZE(tpl)) {
  980. goto on_error;
  981. }
  982. } else {
  983. IBaseFilter *Filter1[] = {h->sourcefilter, h->grabberfilter, h->horflipfilter, h->renderfilter};
  984. IBaseFilter *Filter2[] = {h->sourcefilter, h->cpcfilter, h->grabberfilter, h->horflipfilter, h->renderfilter};
  985. IBaseFilter *Filter3[] = {h->sourcefilter, h->avidecfilter, h->grabberfilter, h->horflipfilter, h->renderfilter};
  986. IBaseFilter *Filter4[] = {h->sourcefilter, h->avidecfilter, h->cpcfilter, h->grabberfilter, h->horflipfilter, h->renderfilter};
  987. struct {
  988. int cnt;
  989. IBaseFilter **Filters;
  990. } tpl[] = {
  991. {ARRAYSIZE(Filter1), Filter1},
  992. {ARRAYSIZE(Filter2), Filter2},
  993. {ARRAYSIZE(Filter3), Filter3},
  994. {ARRAYSIZE(Filter4), Filter4},
  995. };
  996. int i;
  997. for (i = 0; i < ARRAYSIZE(tpl); ++i) {
  998. Sleep(50);
  999. hr = TryConnectFilters(h->graphbuilder, tpl[i].cnt, tpl[i].Filters);
  1000. if (SUCCEEDED(hr)) {
  1001. break;
  1002. }
  1003. }
  1004. if (i == ARRAYSIZE(tpl)) {
  1005. goto on_error;
  1006. }
  1007. }
  1008. if (h->param.pre_hwnd) {
  1009. RECT rc;
  1010. hr = h->renderfilter->lpVtbl->QueryInterface(h->renderfilter,
  1011. &IID_IVideoWindow, (void**)&h->videowindow);
  1012. if (FAILED(hr))
  1013. {
  1014. goto on_error;
  1015. }
  1016. GetClientRect(h->param.pre_hwnd, &rc);
  1017. h->videowindow->lpVtbl->put_Owner(h->videowindow, (OAHWND)h->param.pre_hwnd);
  1018. h->videowindow->lpVtbl->put_Visible(h->videowindow, OAFALSE);
  1019. h->videowindow->lpVtbl->put_Left(h->videowindow, 0);
  1020. h->videowindow->lpVtbl->put_Top(h->videowindow, 0);
  1021. h->videowindow->lpVtbl->put_Height(h->videowindow, min(h->param.pre_height, rc.bottom-rc.top));
  1022. h->videowindow->lpVtbl->put_Width(h->videowindow, min(h->param.pre_width, rc.right - rc.left));
  1023. h->videowindow->lpVtbl->put_WindowStyle(h->videowindow,
  1024. WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN|WS_CLIPSIBLINGS);
  1025. h->videowindow->lpVtbl->put_MessageDrain(h->videowindow, (OAHWND)h->param.pre_hwnd);
  1026. h->videowindow->lpVtbl->put_Visible(h->videowindow, OATRUE);
  1027. }
  1028. if (h->param.on_device_lost) {
  1029. h->thread_background = CreateThread(NULL, 0, &BackgroundThread, h, 0, 0);
  1030. h->evt_thread_exit = CreateEvent(NULL, FALSE, FALSE, NULL);
  1031. }
  1032. hr = h->mc->lpVtbl->Run(h->mc);
  1033. if (FAILED(hr))
  1034. {
  1035. goto on_error;
  1036. }
  1037. h->running = TRUE;
  1038. return VIDEOCAP_OK;
  1039. on_error:
  1040. release_all_interfaces(h);
  1041. return VIDEOCAP_ERROR;
  1042. }
  1043. int videocap_stop(videocap_t h)
  1044. {
  1045. long code;
  1046. if (!h->running)
  1047. return -1;
  1048. if (h->param.on_device_lost) {
  1049. SetEvent(h->evt_thread_exit);
  1050. WaitForSingleObject(h->thread_background, INFINITE);
  1051. CloseHandle(h->thread_background);
  1052. h->thread_background = NULL;
  1053. CloseHandle(h->evt_thread_exit);
  1054. h->evt_thread_exit = NULL;
  1055. }
  1056. h->mc->lpVtbl->Stop(h->mc);
  1057. h->me->lpVtbl->WaitForCompletion(h->me, INFINITE, &code);
  1058. h->running = 0;
  1059. if (h->param.option & VIDEOCAP_OPT_ENABLE_ASYNC_GRAB) {
  1060. EnterCriticalSection(&h->async_cap_cs);
  1061. {
  1062. async_cap_t *pos, *n;
  1063. ListEntry_ForEachSafe(pos, n, &h->async_cap_list, async_cap_t, entry) {
  1064. ListEntry_DeleteNode(&pos->entry);
  1065. pos->result = -1; // cancel
  1066. SetEvent(pos->evt);
  1067. }
  1068. }
  1069. LeaveCriticalSection(&h->async_cap_cs);
  1070. }
  1071. #ifdef _DEBUG
  1072. RemoveFromRot(h->dwROTRegister);
  1073. h->dwROTRegister = 0;
  1074. #endif
  1075. release_all_interfaces(h);
  1076. return VIDEOCAP_OK;
  1077. }
  1078. int videocap_grab(videocap_t h, video_frame *frame)
  1079. {
  1080. int ret;
  1081. if (!h)
  1082. return -1;
  1083. if (!h->running)
  1084. return -1;
  1085. if (!(h->param.option & VIDEOCAP_OPT_ENABLE_GRAB)) /* initialize without grabbing option */
  1086. return -1;
  1087. EnterCriticalSection(&h->cap_cs);
  1088. ret = video_frame_copy(frame, &h->cap_frame);
  1089. LeaveCriticalSection(&h->cap_cs);
  1090. return ret;
  1091. }
  1092. int videocap_async_grab(videocap_t h, video_frame *frame)
  1093. {
  1094. async_cap_t *ac;
  1095. if (!h)
  1096. return -1;
  1097. if (!h->running)
  1098. return -1;
  1099. if (!(h->param.option & VIDEOCAP_OPT_ENABLE_ASYNC_GRAB)) /* initialize without grabbing option */
  1100. return -1;
  1101. ac = (async_cap_t*)malloc(sizeof(async_cap_t));
  1102. ac->evt = CreateEvent(NULL, FALSE, FALSE, NULL);
  1103. ac->ref_cap_frame = frame;
  1104. EnterCriticalSection(&h->async_cap_cs);
  1105. ListEntry_AddTail(&h->async_cap_list, &ac->entry);
  1106. LeaveCriticalSection(&h->async_cap_cs);
  1107. WaitForSingleObject(ac->evt, INFINITE);
  1108. CloseHandle(ac->evt);
  1109. free(ac);
  1110. return 0;
  1111. }
  1112. int videocap_incrment_grab_cb(videocap_t h)
  1113. {
  1114. if (!h)
  1115. return -1;
  1116. if (!h->running)
  1117. return -1;
  1118. InterlockedIncrement(&h->grab_cb_count);
  1119. return 0;
  1120. }
  1121. int videocap_get_frame(videocap_t h, video_frame *frame)
  1122. {
  1123. int ret;
  1124. if (!h)
  1125. return -1;
  1126. if (!(h->param.option & VIDEOCAP_OPT_EANBLE_RESIZE))
  1127. return -1;
  1128. if (!h->running)
  1129. return -1;
  1130. EnterCriticalSection(&h->res_cs);
  1131. ret = video_frame_copy(frame, &h->res_frame);
  1132. LeaveCriticalSection(&h->res_cs);
  1133. return ret;
  1134. }
  1135. int videocap_is_running(videocap_t h, BOOL *state)
  1136. {
  1137. if (!h)
  1138. return VIDEOCAP_ERROR;
  1139. return h->running;
  1140. }
  1141. int videocap_set_preview_wnd_visible(videocap_t h, BOOL visible)
  1142. {
  1143. if (!h || !h->videowindow)
  1144. return VIDEOCAP_ERROR;
  1145. h->videowindow->lpVtbl->put_Visible(h->videowindow,
  1146. visible ? OATRUE : OAFALSE);
  1147. return VIDEOCAP_OK;
  1148. }
  1149. int videocap_get_preview_wnd_visible(videocap_t h, BOOL *visible)
  1150. {
  1151. long l = OAFALSE;
  1152. if (!h || !h->videowindow)
  1153. return VIDEOCAP_ERROR;
  1154. h->videowindow->lpVtbl->get_Visible(h->videowindow, &l);
  1155. *visible = l ? TRUE : FALSE;
  1156. return VIDEOCAP_OK;
  1157. }
  1158. int videocap_set_preview_wnd_width(videocap_t h, int width)
  1159. {
  1160. if (!h)
  1161. return -1;
  1162. if (!h->param.pre_hwnd)
  1163. return -1;
  1164. h->videowindow->lpVtbl->put_Width(h->videowindow, width);
  1165. return 0;
  1166. }
  1167. int videocap_set_preview_wnd_height(videocap_t h, int height)
  1168. {
  1169. if (!h)
  1170. return -1;
  1171. if (!h->param.pre_hwnd)
  1172. return -1;
  1173. h->videowindow->lpVtbl->put_Height(h->videowindow, height);
  1174. return 0;
  1175. }
  1176. static HRESULT set_video_source_format(videocap *vcap)
  1177. {
  1178. HRESULT hr;
  1179. IAMStreamConfig *config;
  1180. ICaptureGraphBuilder2 *capturebuilder;
  1181. VIDEOINFOHEADER *pvi;
  1182. int i, count, size = 0;
  1183. BOOL alreadyset = FALSE;
  1184. // create capture builder
  1185. hr = CoCreateInstance(&CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER,
  1186. &IID_ICaptureGraphBuilder2, (void**)&capturebuilder);
  1187. if (FAILED(hr))
  1188. return hr;
  1189. hr = capturebuilder->lpVtbl->FindInterface(capturebuilder,
  1190. &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, vcap->sourcefilter,
  1191. &IID_IAMStreamConfig, (void**)&config);
  1192. if (FAILED(hr))
  1193. return hr;
  1194. config->lpVtbl->GetNumberOfCapabilities(config, &count, &size);
  1195. if (size == sizeof(VIDEO_STREAM_CONFIG_CAPS)) {
  1196. int width = mode_width[vcap->param.cap_mode];
  1197. int height = mode_height[vcap->param.cap_mode];
  1198. for (i = 0; i < count && !alreadyset; ++i)
  1199. {
  1200. VIDEO_STREAM_CONFIG_CAPS scc;
  1201. AM_MEDIA_TYPE *pmtConfig;
  1202. hr = config->lpVtbl->GetStreamCaps(config, i, &pmtConfig, (BYTE*)&scc);
  1203. if (FAILED(hr))
  1204. continue;
  1205. if (IsEqualIID(&pmtConfig->majortype, &MEDIATYPE_Video) &&
  1206. IsEqualIID(&pmtConfig->formattype, &FORMAT_VideoInfo) &&
  1207. (pmtConfig->cbFormat >= sizeof(VIDEOINFOHEADER)) &&
  1208. pmtConfig->pbFormat != NULL)
  1209. {
  1210. pvi = (VIDEOINFOHEADER*)pmtConfig->pbFormat;
  1211. if (pvi->bmiHeader.biWidth == width && pvi->bmiHeader.biHeight == height)
  1212. {
  1213. pvi->AvgTimePerFrame = (REFERENCE_TIME)(10000000 / vcap->param.fps);
  1214. hr = config->lpVtbl->SetFormat(config, pmtConfig);
  1215. if (SUCCEEDED(hr))
  1216. {
  1217. char *subtype_str=GuidToString(pmtConfig->subtype);
  1218. alreadyset = TRUE;
  1219. }
  1220. else
  1221. {
  1222. alreadyset = FALSE;
  1223. }
  1224. }
  1225. }
  1226. FreeMediaTypeEx(pmtConfig);
  1227. }
  1228. }
  1229. config->lpVtbl->Release(config);
  1230. capturebuilder->lpVtbl->Release(capturebuilder);
  1231. return hr;
  1232. }
  1233. static HRESULT get_output_mediatype(videocap *vcap)
  1234. {
  1235. AM_MEDIA_TYPE mt;
  1236. HRESULT hr;
  1237. VIDEOINFOHEADER *videoHeader = NULL;
  1238. char *subtype_str = NULL;
  1239. ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
  1240. hr = vcap->grabber->lpVtbl->GetConnectedMediaType(vcap->grabber, &mt);
  1241. if(FAILED(hr))
  1242. {
  1243. printf("GetConnectedMediaType Failed, hr=%08X! \n",hr);
  1244. return hr;
  1245. }
  1246. videoHeader = (VIDEOINFOHEADER*)(mt.pbFormat);
  1247. g_width = videoHeader->bmiHeader.biWidth;
  1248. g_height = videoHeader->bmiHeader.biHeight;
  1249. subtype_str=GuidToString(mt.subtype);
  1250. printf("grabber Format Width=%d, Height=%d, biBitCount=%d, biSizeImage=%d, biCompression=%d, biPlanes=%d,biSize=%d,subtype=%s\n,newPmt->lSampleSize=%d, newPmt->bFixedSizeSamples=%d, newPmt->bTemporalCompression=%d\n",
  1251. videoHeader->bmiHeader.biWidth,
  1252. videoHeader->bmiHeader.biHeight,
  1253. videoHeader->bmiHeader.biBitCount,
  1254. videoHeader->bmiHeader.biSizeImage,
  1255. videoHeader->bmiHeader.biCompression,
  1256. videoHeader->bmiHeader.biPlanes,
  1257. videoHeader->bmiHeader.biSize,
  1258. subtype_str,
  1259. mt.lSampleSize,
  1260. mt.bFixedSizeSamples,
  1261. mt.bTemporalCompression);
  1262. return S_OK;
  1263. }
  1264. static void release_all_interfaces(videocap *h)
  1265. {
  1266. if (h->mc) {
  1267. h->mc->lpVtbl->Release(h->mc);
  1268. h->mc = NULL;
  1269. }
  1270. if (h->me) {
  1271. h->me->lpVtbl->Release(h->me);
  1272. h->me = NULL;
  1273. }
  1274. if (h->videowindow) {
  1275. h->videowindow->lpVtbl->put_Owner(h->videowindow, (OAHWND)NULL);
  1276. h->videowindow->lpVtbl->Release(h->videowindow);
  1277. h->videowindow = NULL;
  1278. }
  1279. if (h->graphbuilder) {
  1280. RemoveGraphAllFilters(h->graphbuilder);
  1281. h->graphbuilder->lpVtbl->Release(h->graphbuilder);
  1282. h->graphbuilder = NULL;
  1283. }
  1284. if (h->sourcefilter) {
  1285. h->sourcefilter->lpVtbl->Release(h->sourcefilter);
  1286. h->sourcefilter = NULL;
  1287. }
  1288. if (h->avidecfilter) {
  1289. h->avidecfilter->lpVtbl->Release(h->avidecfilter);
  1290. h->avidecfilter = NULL;
  1291. }
  1292. if (h->cpcfilter) {
  1293. h->cpcfilter->lpVtbl->Release(h->cpcfilter);
  1294. h->cpcfilter = NULL;
  1295. }
  1296. if (h->grabber) {
  1297. h->grabber->lpVtbl->Release(h->grabber);
  1298. h->grabber = NULL;
  1299. }
  1300. if (h->grabberfilter) {
  1301. h->grabberfilter->lpVtbl->Release(h->grabberfilter);
  1302. h->grabberfilter = NULL;
  1303. }
  1304. if (h->horflipfilter) {
  1305. h->horflipfilter->lpVtbl->Release(h->horflipfilter);
  1306. h->horflipfilter = NULL;
  1307. }
  1308. if (h->renderfilter) {
  1309. h->renderfilter->lpVtbl->Release(h->renderfilter);
  1310. h->renderfilter = NULL;
  1311. }
  1312. }
  1313. ////////////////////////////////////////////////////////////
  1314. static HRESULT get_device_count_cb(int index, IMoniker *pMoniker, void *pUserData1, void *pUserData2)
  1315. {
  1316. if (pMoniker)
  1317. ++*(int*)pUserData1;
  1318. return S_FALSE;
  1319. }
  1320. int videocap_get_device_count()
  1321. {
  1322. int count = 0;
  1323. HRESULT hr = WalkFilterCategory(&CLSID_VideoInputDeviceCategory,
  1324. &get_device_count_cb, &count, NULL);
  1325. return SUCCEEDED(hr) ? count : 0;
  1326. }
  1327. struct _buf {
  1328. WCHAR *buf;
  1329. int len;
  1330. };
  1331. static HRESULT get_device_name_cb(int index, IMoniker *pMoniker, void *pUserData1, void *pUserData2)
  1332. {
  1333. if (pMoniker) {
  1334. int *p_id = (int*)pUserData1;
  1335. struct _buf *buf = (struct _buf*)pUserData2;
  1336. HRESULT hr;
  1337. if (index == *p_id) {
  1338. IPropertyBag *pPropBag;
  1339. VARIANT name;
  1340. hr = pMoniker->lpVtbl->BindToStorage(pMoniker, NULL, NULL,
  1341. &IID_IPropertyBag, (void**)&pPropBag);
  1342. if (FAILED(hr)) {
  1343. buf->len = -1; /* failed */
  1344. return hr;
  1345. }
  1346. VariantInit(&name);
  1347. name.vt = VT_BSTR;
  1348. hr = pPropBag->lpVtbl->Read(pPropBag, L"FriendlyName", &name, NULL);
  1349. if (FAILED(hr)) {
  1350. pPropBag->lpVtbl->Release(pPropBag);
  1351. VariantClear(&name);
  1352. buf->len = -1;
  1353. return hr;
  1354. }
  1355. buf->len = SysStringByteLen(name.bstrVal) + 2;
  1356. if (buf)
  1357. memcpy(buf->buf, name.bstrVal, buf->len);
  1358. pPropBag->lpVtbl->Release(pPropBag);
  1359. VariantClear(&name);
  1360. return S_OK;
  1361. }
  1362. }
  1363. return S_FALSE;
  1364. }
  1365. int videocap_get_device_name(int device_id, WCHAR *buf, int len)
  1366. {
  1367. struct _buf x = {buf, len};
  1368. int dev_id = device_id;
  1369. HRESULT hr = WalkFilterCategory(&CLSID_VideoInputDeviceCategory,
  1370. &get_device_name_cb, &dev_id, &x);
  1371. if (hr == S_OK)
  1372. return x.len;
  1373. return -1;
  1374. }
  1375. static HRESULT get_device_path_cb(int index, IMoniker *pMoniker, void *pUserData1, void *pUserData2)
  1376. {
  1377. if (pMoniker) {
  1378. int *p_id = (int*)pUserData1;
  1379. struct _buf *buf = (struct _buf*)pUserData2;
  1380. HRESULT hr;
  1381. if (index == *p_id) {
  1382. IPropertyBag *pPropBag;
  1383. VARIANT path;
  1384. hr = pMoniker->lpVtbl->BindToStorage(pMoniker, NULL, NULL,
  1385. &IID_IPropertyBag, (void**)&pPropBag);
  1386. if (FAILED(hr)) {
  1387. buf->len = -1; /* failed */
  1388. return hr;
  1389. }
  1390. VariantInit(&path);
  1391. path.vt = VT_BSTR;
  1392. hr = pPropBag->lpVtbl->Read(pPropBag, L"DevicePath", &path, NULL);
  1393. if (FAILED(hr)) {
  1394. pPropBag->lpVtbl->Release(pPropBag);
  1395. VariantClear(&path);
  1396. buf->len = -1;
  1397. return hr;
  1398. }
  1399. buf->len = SysStringByteLen(path.bstrVal) + 2;
  1400. if (buf)
  1401. memcpy(buf->buf, path.bstrVal, buf->len);
  1402. pPropBag->lpVtbl->Release(pPropBag);
  1403. VariantClear(&path);
  1404. return S_OK;
  1405. }
  1406. }
  1407. return S_FALSE;
  1408. }
  1409. int videocap_get_device_path(int device_id, WCHAR *buf, int len)
  1410. {
  1411. struct _buf x = {buf, len};
  1412. int dev_id = device_id;
  1413. HRESULT hr = WalkFilterCategory(&CLSID_VideoInputDeviceCategory,
  1414. &get_device_path_cb, &dev_id, &x);
  1415. if (hr == S_OK)
  1416. return x.len;
  1417. return -1;
  1418. }
  1419. static int is_support(int min, int max, int granularity, int x)
  1420. {
  1421. if (x < min || x > max)
  1422. return 0;
  1423. if (x == min || x == max)
  1424. return 1;
  1425. if (granularity == 0)
  1426. return 0;
  1427. if ((x-min)/granularity*granularity != (x-min))
  1428. return 0;
  1429. if ((max-x)/granularity*granularity != (max-x))
  1430. return 0;
  1431. return 1;
  1432. }
  1433. static int is_support_size(VIDEO_STREAM_CONFIG_CAPS *pscc, int width, int height)
  1434. {
  1435. return is_support(pscc->MinOutputSize.cx, pscc->MaxOutputSize.cx, pscc->OutputGranularityX, width) &&
  1436. is_support(pscc->MinOutputSize.cy, pscc->MaxOutputSize.cy, pscc->OutputGranularityY, height);
  1437. }
  1438. int videocap_get_device_cap(int device_id, videocap_device_cap *cap)
  1439. {
  1440. int error = -1, i, count, size = 0, j;
  1441. HRESULT hr;
  1442. ICaptureGraphBuilder2 *pBuilder = NULL;
  1443. IBaseFilter *pSourceFilter = NULL;
  1444. IAMStreamConfig *config = NULL;
  1445. videocap_device_cap tmp_cap = {0};
  1446. if (!cap)
  1447. return error;
  1448. hr = CreateFilterByIndex(&CLSID_VideoInputDeviceCategory, (void**)&pSourceFilter, device_id);
  1449. if (FAILED(hr))
  1450. return error;
  1451. hr = CoCreateInstance(&CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER,
  1452. &IID_ICaptureGraphBuilder2, (void**)&pBuilder);
  1453. if (FAILED(hr))
  1454. return error;
  1455. ZeroMemory(cap, sizeof(videocap_device_cap));
  1456. hr = pBuilder->lpVtbl->FindInterface(pBuilder, &PIN_CATEGORY_CAPTURE,
  1457. &MEDIATYPE_Video, pSourceFilter, &IID_IAMStreamConfig, (void**)&config);
  1458. if (FAILED(hr))
  1459. goto on_error;
  1460. config->lpVtbl->GetNumberOfCapabilities(config, &count, &size);
  1461. if (size == sizeof(VIDEO_STREAM_CONFIG_CAPS)) {
  1462. for (i = 0; i < count; ++i) {
  1463. VIDEO_STREAM_CONFIG_CAPS scc;
  1464. AM_MEDIA_TYPE *pmtConfig;
  1465. VIDEOINFOHEADER *pvi = NULL;
  1466. hr = config->lpVtbl->GetStreamCaps(config, i, &pmtConfig, (BYTE*)&scc);
  1467. if (FAILED(hr))
  1468. continue;
  1469. if (IsEqualIID(&pmtConfig->majortype, &MEDIATYPE_Video) &&
  1470. IsEqualIID(&pmtConfig->formattype, &FORMAT_VideoInfo) &&
  1471. (pmtConfig->cbFormat >= sizeof(VIDEOINFOHEADER)) &&
  1472. pmtConfig->pbFormat != NULL) {
  1473. if (IsEqualIID(&pmtConfig->subtype, &MEDIASUBTYPE_RGB24)) {
  1474. for (j = 0; j < VIDEOCAP_MAX_MODE; ++j) {
  1475. if (is_support_size(&scc, mode_width[j], mode_height[j])) {
  1476. tmp_cap.mode[j] = 1;
  1477. tmp_cap.min_frame_interval[j] = (int)scc.MinFrameInterval;
  1478. tmp_cap.max_frame_interval[j] = (int)scc.MaxFrameInterval;
  1479. }
  1480. }
  1481. }
  1482. }
  1483. FreeMediaTypeEx(pmtConfig);
  1484. }
  1485. }
  1486. config->lpVtbl->Release(config);
  1487. for (i = 0, j = 0; i < VIDEOCAP_MAX_MODE; ++i) {
  1488. if (tmp_cap.mode[i]) {
  1489. cap->mode[j] = i;
  1490. cap->min_frame_interval[j] = cap->min_frame_interval[i];
  1491. cap->max_frame_interval[j] = cap->max_frame_interval[i];
  1492. j++;
  1493. }
  1494. }
  1495. cap->mode_cnt = j;
  1496. error = 0;
  1497. on_error:
  1498. if (pBuilder)
  1499. pBuilder->lpVtbl->Release(pBuilder);
  1500. if (pSourceFilter)
  1501. pSourceFilter->lpVtbl->Release(pSourceFilter);
  1502. return error;
  1503. }
  1504. int videocap_get_mode_width(int mode)
  1505. {
  1506. return mode_width[mode];
  1507. }
  1508. int videocap_get_mode_height(int mode)
  1509. {
  1510. return mode_height[mode];
  1511. }
  1512. void YUV2RGB(byte *pYUV, byte *pRGB)
  1513. {
  1514. byte y, u, v;
  1515. y = *pYUV;
  1516. pYUV++;
  1517. u = *pYUV;
  1518. pYUV++;
  1519. v = *pYUV;
  1520. //r
  1521. *pRGB = 1.0*y+1.772*(u-128);
  1522. if (*pRGB<0) *pRGB = 0;
  1523. if(*pRGB>255) *pRGB = 255;
  1524. //G
  1525. pRGB++;
  1526. *pRGB =1.0*y - 0.34413*(u-128) - 0.71414*(v-128);
  1527. if (*pRGB<0) *pRGB = 0;
  1528. if(*pRGB>255) *pRGB = 255;
  1529. //b
  1530. pRGB++;
  1531. *pRGB = 1.0*y+1.402*(v-128);
  1532. if (*pRGB<0) *pRGB = 0;
  1533. if(*pRGB>255) *pRGB = 255;
  1534. pYUV++;
  1535. y = *pYUV;
  1536. pYUV++;
  1537. u = *pYUV;
  1538. pYUV++;
  1539. v = *pYUV;
  1540. //r
  1541. pRGB++;
  1542. *pRGB = 1.0*y+1.772*(u-128);
  1543. if (*pRGB<0) *pRGB = 0;
  1544. if(*pRGB>255) *pRGB = 255;
  1545. //G
  1546. pRGB++;
  1547. *pRGB =1.0*y - 0.34413*(u-128) - 0.71414*(v-128);
  1548. if (*pRGB<0) *pRGB = 0;
  1549. if(*pRGB>255) *pRGB = 255;
  1550. //b
  1551. pRGB++;
  1552. *pRGB = 1.0*y+1.402*(v-128);
  1553. if (*pRGB<0)
  1554. *pRGB = 0;
  1555. if(*pRGB>255)
  1556. *pRGB = 255;
  1557. }
  1558. int Yuv2RgbFrame(BYTE* pYUVBuf, BYTE* pRGBBuf, int cx, int cy)
  1559. {
  1560. int i = 0;
  1561. int j=0;
  1562. BYTE* pYUV = pYUVBuf;
  1563. BYTE* pRGB = pRGBBuf;
  1564. BYTE YUV[6];
  1565. BYTE RGB[6];
  1566. memset(YUV,0,6);
  1567. memset(RGB,0,6);
  1568. for ( i = 0;i<cy;i++)
  1569. {
  1570. for ( j=0; j<cx;j+=2)
  1571. {
  1572. //yuy2(yuyv)-->yuv444(yuvyuv)-->rgb24
  1573. YUV[0] = *pYUV;
  1574. pYUV++;
  1575. YUV[1] = *pYUV;
  1576. pYUV+=2;
  1577. YUV[2] = *pYUV;
  1578. pYUV--;
  1579. YUV[3] = *pYUV;
  1580. pYUV--;
  1581. YUV[4] = *pYUV;
  1582. pYUV+=2;
  1583. YUV[5] = *pYUV;
  1584. pYUV++;
  1585. YUV2RGB(YUV,RGB);
  1586. memcpy(pRGB + ((cy-i-1)*cx + j)*3 ,RGB,6);
  1587. }
  1588. }
  1589. return 0;
  1590. }
  1591. int yuv422_to_rgb24(unsigned char*yuv422,unsigned char*rgb24,int width,int height)
  1592. {
  1593. int x,y;
  1594. uint8_t*yuv444;
  1595. yuv444 = (uint8_t*) malloc(sizeof(uint8_t) * width * height * 3);
  1596. for(x = 0,y = 0;x < width*height*2,y < width*height*3;x+=4,y+=6)
  1597. {
  1598. yuv444[y] = yuv422[x];
  1599. yuv444[y+1] = yuv422[x+1];
  1600. yuv444[y+2] = yuv422[x+3];
  1601. yuv444[y+3] = yuv422[x+2];
  1602. yuv444[y+4] = yuv422[x+1];
  1603. yuv444[y+5] = yuv422[x+3];
  1604. }
  1605. for(x = 0;x < width*height*3;x+=3)
  1606. {
  1607. rgb24[x+2] = yuv444[x] + 1.402*(yuv444[x+2] - 128);
  1608. rgb24[x+1] = yuv444[x]-0.34414*(yuv444[x+1]-128)-0.71414*(yuv444[x+2]-128);
  1609. rgb24[x] = yuv444[x] + 1.772*(yuv444[x+1] - 128);
  1610. if(rgb24[x]>255)
  1611. rgb24[x]=255;
  1612. if(rgb24[x]<0)
  1613. rgb24[x]=0;
  1614. if(rgb24[x+1]>255)
  1615. rgb24[x+1]=255;
  1616. if(rgb24[x+1]<0)
  1617. rgb24[x+1]=0;
  1618. if(rgb24[x+2]>255)
  1619. rgb24[x+2]=255;
  1620. if(rgb24[x+2]<0)
  1621. rgb24[x+2]=0;
  1622. }
  1623. free(yuv444);
  1624. return 0;
  1625. }
  1626. ////////////////////////////////////////////////////////////