videocap.c 50 KB

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