PageRenderTime 381ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vdicmdrv/main.cpp

https://github.com/fishman/virtualdub
C++ | 369 lines | 256 code | 80 blank | 33 comment | 36 complexity | 460d879200fe846af3f2d6503463ae2d MD5 | raw file
  1. #include <crtdbg.h>
  2. #include <windows.h>
  3. #include <mmsystem.h>
  4. #include <vfw.h>
  5. //#include <msviddrv.h>
  6. #include "IVideoDriver.h"
  7. #include "IVideoCompressor.h"
  8. #include "CCompRemote.h"
  9. HMODULE g_hInst;
  10. #define BOGUS_DRIVER_ID 1
  11. IVideoDriver *videoDrivers[] = {
  12. // add other procedures here...
  13. &videoDriverRemote,
  14. NULL // FENCE: must be last
  15. };
  16. /***************************************************************************
  17. ***************************************************************************/
  18. class DriverPtrTranslator {
  19. public:
  20. DriverPtrTranslator *next, *prev;
  21. DWORD id16;
  22. IVideoCompressor *id32;
  23. };
  24. DriverPtrTranslator *active_opens = NULL;
  25. DWORD xlat_last_id = 2;
  26. DriverPtrTranslator *find_xlator_by_driverptr(IVideoCompressor *ivc) {
  27. DriverPtrTranslator *ptr = active_opens;
  28. while(ptr) {
  29. if (ivc == ptr->id32) return ptr;
  30. ptr = ptr->next;
  31. }
  32. return NULL;
  33. }
  34. DriverPtrTranslator *find_xlator_by_id16(DWORD id16) {
  35. DriverPtrTranslator *ptr = active_opens;
  36. while(ptr) {
  37. if (id16 == ptr->id16) return ptr;
  38. ptr = ptr->next;
  39. }
  40. return NULL;
  41. }
  42. IVideoCompressor *id_to_driverptr(DWORD id16) {
  43. DriverPtrTranslator *dpt = find_xlator_by_id16(id16);
  44. return dpt ? dpt->id32 : NULL;
  45. }
  46. DriverPtrTranslator *create_translation() {
  47. DWORD start_id = xlat_last_id;
  48. DWORD cur_id = start_id;
  49. do {
  50. if (cur_id != BOGUS_DRIVER_ID && cur_id) {
  51. if (!find_xlator_by_id16(cur_id)) {
  52. DriverPtrTranslator *dpt;
  53. if (!(dpt = new DriverPtrTranslator)) return 0;
  54. dpt->prev = NULL;
  55. dpt->next = active_opens;
  56. dpt->id16 = cur_id;
  57. start_id = (cur_id+1) & 0xffff;
  58. if (active_opens) active_opens->prev = dpt;
  59. active_opens = dpt;
  60. return dpt;
  61. }
  62. }
  63. cur_id = (cur_id+1) & 0xffff;
  64. } while(cur_id != start_id);
  65. return 0;
  66. }
  67. void remove_translation(DriverPtrTranslator *dpt) {
  68. if (dpt == active_opens) active_opens = dpt->next;
  69. if (dpt->prev) dpt->prev->next = dpt->next;
  70. if (dpt->next) dpt->next->prev = dpt->prev;
  71. delete dpt;
  72. }
  73. ////////////////////////////////////////////////////////////
  74. #define DRIVERMESSAGE(x) x,#x
  75. struct DriveMessageTranslation {
  76. UINT msg;
  77. char *name;
  78. } driverMessages[]={
  79. DRIVERMESSAGE(DRV_LOAD),
  80. DRIVERMESSAGE(DRV_FREE),
  81. DRIVERMESSAGE(DRV_OPEN),
  82. DRIVERMESSAGE(DRV_CLOSE),
  83. DRIVERMESSAGE(DRV_QUERYCONFIGURE),
  84. DRIVERMESSAGE(DRV_CONFIGURE),
  85. DRIVERMESSAGE(DRV_DISABLE),
  86. DRIVERMESSAGE(DRV_ENABLE),
  87. DRIVERMESSAGE(DRV_INSTALL),
  88. DRIVERMESSAGE(DRV_REMOVE),
  89. DRIVERMESSAGE(ICM_ABOUT),
  90. DRIVERMESSAGE(ICM_COMPRESS),
  91. DRIVERMESSAGE(ICM_COMPRESS_BEGIN),
  92. DRIVERMESSAGE(ICM_COMPRESS_FRAMES_INFO),
  93. DRIVERMESSAGE(ICM_COMPRESS_GET_FORMAT),
  94. DRIVERMESSAGE(ICM_COMPRESS_GET_SIZE),
  95. DRIVERMESSAGE(ICM_COMPRESS_QUERY),
  96. DRIVERMESSAGE(ICM_CONFIGURE),
  97. DRIVERMESSAGE(ICM_DECOMPRESS),
  98. DRIVERMESSAGE(ICM_DECOMPRESS_BEGIN),
  99. DRIVERMESSAGE(ICM_DECOMPRESS_END),
  100. DRIVERMESSAGE(ICM_DECOMPRESS_GET_FORMAT),
  101. DRIVERMESSAGE(ICM_DECOMPRESS_GET_PALETTE),
  102. DRIVERMESSAGE(ICM_DECOMPRESS_QUERY),
  103. DRIVERMESSAGE(ICM_DECOMPRESS_SET_PALETTE),
  104. DRIVERMESSAGE(ICM_DECOMPRESSEX),
  105. DRIVERMESSAGE(ICM_DECOMPRESSEX_BEGIN),
  106. DRIVERMESSAGE(ICM_DECOMPRESSEX_QUERY),
  107. DRIVERMESSAGE(ICM_DRAW),
  108. DRIVERMESSAGE(ICM_DRAW_BEGIN),
  109. DRIVERMESSAGE(ICM_DRAW_CHANGEPALETTE),
  110. DRIVERMESSAGE(ICM_DRAW_END),
  111. DRIVERMESSAGE(ICM_DRAW_FLUSH),
  112. DRIVERMESSAGE(ICM_DRAW_GETTIME),
  113. DRIVERMESSAGE(ICM_DRAW_QUERY),
  114. DRIVERMESSAGE(ICM_DRAW_REALIZE),
  115. DRIVERMESSAGE(ICM_DRAW_RENDERBUFFER),
  116. DRIVERMESSAGE(ICM_DRAW_SETTIME),
  117. DRIVERMESSAGE(ICM_DRAW_START),
  118. DRIVERMESSAGE(ICM_DRAW_START_PLAY),
  119. DRIVERMESSAGE(ICM_DRAW_STOP),
  120. DRIVERMESSAGE(ICM_DRAW_STOP_PLAY),
  121. DRIVERMESSAGE(ICM_DRAW_SUGGESTFORMAT),
  122. DRIVERMESSAGE(ICM_DRAW_WINDOW),
  123. DRIVERMESSAGE(ICM_GET),
  124. DRIVERMESSAGE(ICM_GETBUFFERSWANTED),
  125. DRIVERMESSAGE(ICM_GETDEFAULTKEYFRAMERATE),
  126. DRIVERMESSAGE(ICM_GETDEFAULTQUALITY),
  127. DRIVERMESSAGE(ICM_GETINFO),
  128. DRIVERMESSAGE(ICM_GETQUALITY),
  129. DRIVERMESSAGE(ICM_GETSTATE),
  130. DRIVERMESSAGE(ICM_SET_STATUS_PROC),
  131. DRIVERMESSAGE(ICM_SETQUALITY),
  132. DRIVERMESSAGE(ICM_SETSTATE),
  133. };
  134. char *TranslateDriverMessage(UINT msg) {
  135. static char buf[12];
  136. for(int i=0; i<(sizeof driverMessages/sizeof driverMessages[0]); i++)
  137. if (driverMessages[i].msg == msg)
  138. return driverMessages[i].name;
  139. wsprintf(buf, "%08lx", msg);
  140. return buf;
  141. }
  142. /////////////////////
  143. extern "C" __declspec(dllexport) LRESULT CALLBACK DriverProc(DWORD dwDriverID, HDRVR hDriver, UINT uiMessage, LPARAM lParam1, LPARAM lParam2);
  144. __declspec(dllexport) LRESULT CALLBACK DriverProc(DWORD dwDriverID, HDRVR hDriver, UINT uiMessage, LPARAM lParam1, LPARAM lParam2)
  145. {
  146. IVideoCompressor *pi;
  147. int i;
  148. LRESULT dw;
  149. if ( (dwDriverID == BOGUS_DRIVER_ID) || (dwDriverID == 0))
  150. pi = NULL;
  151. else
  152. pi = (IVideoCompressor *)id_to_driverptr(dwDriverID);
  153. _RPT4(0,"Driver %08lx, Message %s(%08lx, %08lx)\n", dwDriverID, TranslateDriverMessage(uiMessage), lParam1, lParam2);
  154. switch (uiMessage)
  155. {
  156. case DRV_LOAD:
  157. for(i=0; videoDrivers[i]; i++)
  158. if (!videoDrivers[i]->Load(hDriver))
  159. return 0L;
  160. return (LRESULT)1L;
  161. case DRV_FREE:
  162. // put global de-initialization here...
  163. // Pass to all driver procs
  164. for(i=0; videoDrivers[i]; i++)
  165. videoDrivers[i]->Free(hDriver);
  166. return (LRESULT)1L;
  167. case DRV_OPEN:
  168. /*
  169. Sent to the driver when it is opened.
  170. dwDriverID is 0L.
  171. lParam1 is a far pointer to a zero-terminated string
  172. containing the name used to open the driver.
  173. lParam2 is passed through from the drvOpen call. It is
  174. NULL if this open is from the Drivers Applet in control.exe
  175. It is LPVIDEO_OPEN_PARMS otherwise.
  176. Return 0L to fail the open.
  177. */
  178. //
  179. // if we were opened without an open structure then just
  180. // return a phony (non zero) id so the OpenDriver() will
  181. // work.
  182. //
  183. if (lParam2 == 0)
  184. return BOGUS_DRIVER_ID;
  185. // else, ask all procs if they like input type
  186. {
  187. DriverPtrTranslator *dpt = create_translation();
  188. for (i=0; videoDrivers[i]; i++) {
  189. if (dw = (DWORD)videoDrivers[i]->Open(hDriver, (char *)lParam1, (LPVIDEO_OPEN_PARMS)lParam2)) {
  190. _RPT2(0,"DRV_OPEN: Driver %d returned %p\n", i, dw);
  191. dpt->id32 = (IVideoCompressor *)dw;
  192. return dpt->id16; // they did, return
  193. }
  194. }
  195. remove_translation(dpt);
  196. }
  197. return 0L;
  198. case DRV_CLOSE:
  199. if (pi) {
  200. DriverPtrTranslator *dpt;
  201. if (dpt = find_xlator_by_id16(dwDriverID))
  202. remove_translation(dpt);
  203. delete pi;
  204. }
  205. return 0L;
  206. case DRV_QUERYCONFIGURE:
  207. return (LRESULT)0L;
  208. case DRV_CONFIGURE:
  209. return DRV_OK;
  210. /*********************************************************************
  211. standard driver messages
  212. *********************************************************************/
  213. case DRV_DISABLE:
  214. for(i=0; videoDrivers[i]; i++)
  215. videoDrivers[i]->Disable(hDriver);
  216. return TRUE;
  217. case DRV_ENABLE:
  218. for(i=0; videoDrivers[i]; i++)
  219. videoDrivers[i]->Enable(hDriver);
  220. return TRUE;
  221. case DRV_INSTALL:
  222. case DRV_REMOVE:
  223. return (LRESULT)DRV_OK;
  224. default:
  225. if (pi && uiMessage>=DRV_USER) switch(uiMessage) {
  226. case ICM_ABOUT: return pi->About((HWND)lParam1);
  227. case ICM_COMPRESS: return pi->Compress((ICCOMPRESS *)lParam1, (DWORD)lParam2);
  228. case ICM_COMPRESS_BEGIN: return pi->CompressBegin((LPBITMAPINFO)lParam1, (LPBITMAPINFO)lParam2);
  229. case ICM_COMPRESS_FRAMES_INFO: return pi->CompressFramesInfo((ICCOMPRESSFRAMES *)lParam1, (DWORD)lParam2);
  230. case ICM_COMPRESS_GET_FORMAT: return pi->CompressGetFormat((LPBITMAPINFO)lParam1, (LPBITMAPINFO)lParam2);
  231. case ICM_COMPRESS_GET_SIZE: return pi->CompressGetSize((LPBITMAPINFO)lParam1, (LPBITMAPINFO)lParam2);
  232. case ICM_COMPRESS_QUERY: return pi->CompressQuery((LPBITMAPINFO)lParam1, (LPBITMAPINFO)lParam2);
  233. case ICM_CONFIGURE: return pi->Configure((HWND)lParam1);
  234. case ICM_DECOMPRESS: return pi->Decompress((ICDECOMPRESS *)lParam1, (DWORD)lParam2);
  235. case ICM_DECOMPRESS_BEGIN: return pi->DecompressBegin((LPBITMAPINFO)lParam1, (LPBITMAPINFO)lParam2);
  236. case ICM_DECOMPRESS_END: return pi->DecompressEnd();
  237. case ICM_DECOMPRESS_GET_FORMAT: return pi->DecompressGetFormat((LPBITMAPINFO)lParam1, (LPBITMAPINFO)lParam2);
  238. case ICM_DECOMPRESS_GET_PALETTE: return pi->DecompressGetPalette((LPBITMAPINFOHEADER)lParam1, (LPBITMAPINFOHEADER)lParam2);
  239. case ICM_DECOMPRESS_QUERY: return pi->DecompressQuery((LPBITMAPINFO)lParam1, (LPBITMAPINFO)lParam2);
  240. case ICM_DECOMPRESS_SET_PALETTE: return pi->DecompressSetPalette((LPBITMAPINFOHEADER)lParam1);
  241. case ICM_DECOMPRESSEX: return pi->DecompressEx((ICDECOMPRESSEX *)lParam1, (DWORD)lParam2);
  242. case ICM_DECOMPRESSEX_BEGIN: return pi->DecompressExBegin((ICDECOMPRESSEX *)lParam1, (DWORD)lParam2);
  243. case ICM_DECOMPRESSEX_QUERY: return pi->DecompressExQuery((ICDECOMPRESSEX *)lParam1, (DWORD)lParam2);
  244. case ICM_DRAW: return pi->Draw((ICDRAW *)lParam1, (DWORD)lParam2);
  245. case ICM_DRAW_BEGIN: return pi->DrawBegin((ICDRAWBEGIN *)lParam1, (DWORD)lParam2);
  246. case ICM_DRAW_CHANGEPALETTE: return pi->DrawChangePalette((BITMAPINFO *)lParam1);
  247. case ICM_DRAW_END: return pi->DrawEnd();
  248. case ICM_DRAW_FLUSH: return pi->DrawFlush();
  249. case ICM_DRAW_GETTIME: return pi->DrawGetTime((DWORD *)lParam1);
  250. case ICM_DRAW_QUERY: return pi->DrawQuery((BITMAPINFO *)lParam1);
  251. case ICM_DRAW_REALIZE: return pi->DrawRealize((HDC)lParam1, (BOOL)lParam2);
  252. case ICM_DRAW_RENDERBUFFER: return pi->DrawRenderBuffer();
  253. case ICM_DRAW_SETTIME: return pi->DrawSetTime((DWORD)lParam1);
  254. case ICM_DRAW_START: return pi->DrawStart();
  255. case ICM_DRAW_START_PLAY: return pi->DrawStartPlay((DWORD)lParam1, (DWORD)lParam2);
  256. case ICM_DRAW_STOP: return pi->DrawStop();
  257. case ICM_DRAW_STOP_PLAY: return pi->DrawStopPlay();
  258. case ICM_DRAW_SUGGESTFORMAT: return pi->DrawSuggestFormat((ICDRAWSUGGEST *)lParam1, (DWORD)lParam2);
  259. case ICM_DRAW_WINDOW: return pi->DrawWindow((RECT *)lParam1);
  260. case ICM_GET: return pi->Get((LPVOID)lParam1, (DWORD)lParam2);
  261. case ICM_GETBUFFERSWANTED: return pi->GetBuffersWanted((DWORD *)lParam1);
  262. case ICM_GETDEFAULTKEYFRAMERATE: return pi->GetDefaultKeyFrameRate((DWORD *)lParam1);
  263. case ICM_GETDEFAULTQUALITY: return pi->GetDefaultQuality((DWORD *)lParam1);
  264. case ICM_GETINFO: return pi->GetInfo((ICINFO *)lParam1, (DWORD)lParam2);
  265. case ICM_GETQUALITY: return pi->GetQuality((DWORD *)lParam1);
  266. case ICM_GETSTATE: return pi->GetState((LPVOID)lParam1, (DWORD)lParam2);
  267. case ICM_SET_STATUS_PROC: return pi->SetStatusProc((ICSETSTATUSPROC *)lParam1, (DWORD)lParam2);
  268. case ICM_SETQUALITY: return pi->SetQuality((DWORD *)lParam1);
  269. case ICM_SETSTATE: return pi->SetState((LPVOID)lParam1, (DWORD)lParam2);
  270. default:
  271. return pi->Default(dwDriverID, hDriver, uiMessage, lParam1, lParam2);
  272. } else
  273. return DefDriverProc(dwDriverID, hDriver, uiMessage, lParam1, lParam2);
  274. }
  275. }
  276. BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason, LPVOID lpReserved) {
  277. switch(ulReason) {
  278. case DLL_PROCESS_ATTACH:
  279. g_hInst = hInst;
  280. break;
  281. }
  282. return TRUE;
  283. }