Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3311 lines
98 KiB

  1. //////////////////////////////////////////////////////////////////////////////o
  2. //
  3. // Compman.c
  4. //
  5. // Manager routines for compressing/decompressing/and choosing compressors.
  6. //
  7. // (C) Copyright Microsoft Corp. 1991-1995. All rights reserved.
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. /*
  11. * This code contains 16 thunk code for NT. If the 16 bit open fails
  12. * we will try and open a 32 bit codec. (The reason for not trying the 32
  13. * bit codec first is an attempt to keep most things on the 16 bit side.
  14. * The performance under NT appears reasonable, and for frame specific
  15. * operations it reduces the number of 16/32 transitions.
  16. */
  17. #include <windows.h>
  18. #include <windowsx.h>
  19. #include <mmsystem.h>
  20. #include <win32.h>
  21. #ifdef _WIN32
  22. #include <mmddk.h> // needed for definition of DRIVERS_SECTION
  23. BOOL IsAdmin(void);
  24. #endif
  25. #ifdef NT_THUNK16
  26. #include "thunks.h" // Define the thunk stuff
  27. #endif
  28. #ifdef _WIN32
  29. #ifdef DEBUGLOAD
  30. #define ICEnterCrit(p) \
  31. if (!(gdwLoadFlags & ICLOAD_CALLED)) { \
  32. OutputDebugStringA("ICOPEN Crit Sec not setup (ENTER)\n"); \
  33. DebugBreak(); \
  34. } \
  35. (EnterCriticalSection(p))
  36. #define ICLeaveCrit(p) \
  37. if (!(gdwLoadFlags & ICLOAD_CALLED)) { \
  38. OutputDebugStringA("ICOPEN Crit Sec not setup (LEAVE)\n"); \
  39. DebugBreak(); \
  40. } \
  41. (LeaveCriticalSection(p))
  42. #else
  43. #define ICEnterCrit(p) (EnterCriticalSection(p))
  44. #define ICLeaveCrit(p) (LeaveCriticalSection(p))
  45. #endif
  46. #else
  47. // non-win32 code has no critsecs
  48. #define ICEnterCrit(p)
  49. #define ICLeaveCrit(p)
  50. #endif
  51. #include <profile.h>
  52. //
  53. // define these before compman.h, so our functions get declared right.
  54. //
  55. #ifndef _WIN32
  56. #define VFWAPI FAR PASCAL _loadds
  57. #define VFWAPIV FAR CDECL _loadds
  58. #endif
  59. #include <vfw.h>
  60. #include "icm.rc"
  61. #ifndef _WIN32
  62. #define LoadLibraryA LoadLibrary
  63. #define CharLowerA AnsiLower
  64. #endif
  65. #ifndef streamtypeVIDEO
  66. #define streamtypeVIDEO mmioFOURCC('v', 'i', 'd', 's')
  67. #endif
  68. #define ICTYPE_VCAP mmioFOURCC('v', 'c', 'a', 'p')
  69. #define ICTYPE_ACM mmioFOURCC('a', 'u', 'd', 'c')
  70. #define SMAG mmioFOURCC('S', 'm', 'a', 'g')
  71. #define IC_INI TEXT("Installable Compressors")
  72. //STATICDT TCHAR sz44s[] = TEXT("%4.4hs");
  73. STATICDT TCHAR szMSACM[] = TEXT("MSACM");
  74. STATICDT TCHAR szVIDC[] = TEXT("VIDC");
  75. STATICDT TCHAR gszIniSect[] = IC_INI;
  76. STATICDT TCHAR gszSystemIni[] = TEXT("SYSTEM.INI");
  77. STATICDT TCHAR gszDrivers[] = DRIVERS_SECTION;
  78. STATICDT TCHAR szNull[] = TEXT("");
  79. STATICDT TCHAR szICKey[] = TEXT("%4.4hs.%4.4hs");
  80. STATICDT TCHAR szMSVideo[] = TEXT("MSVideo");
  81. STATICDT SZCODEA szDriverProc[] = "DriverProc";
  82. #ifdef _WIN32
  83. // Use a mapping to get stuff into and out of the registry
  84. BOOL myWritePrivateProfileString(
  85. LPCTSTR lpszSection, // address of section name
  86. LPCTSTR lpszKey, // address of key name
  87. LPCTSTR lpszString // address of string to add
  88. );
  89. DWORD myGetPrivateProfileString(
  90. LPCTSTR lpszSection, // address of section name
  91. LPCTSTR lpszKey, // address of key name
  92. LPCTSTR lpszDefault, // address of default string
  93. LPTSTR lpszReturnBuffer, // address of destination buffer
  94. DWORD cchReturnBuffer // size of destination buffer
  95. );
  96. #endif
  97. #ifdef DEBUG
  98. #define DPF( x ) dprintfc x
  99. #define DEBUG_RETAIL
  100. #else
  101. #define DPF(x)
  102. #endif
  103. #ifdef DEBUG_RETAIL
  104. STATICFN void CDECL dprintfc(LPSTR, ...);
  105. static char gszModname[] = "COMPMAN";
  106. #define MODNAME gszModname
  107. #define RPF( x ) dprintfc x
  108. #define ROUT(sz) {static SZCODEA ach[] = sz; dprintfc(ach); }
  109. void ICDebugMessage(HIC hic, UINT msg, DWORD_PTR dw1, DWORD_PTR dw2);
  110. LRESULT ICDebugReturn(LRESULT err);
  111. #ifdef _WIN32
  112. #define DebugErr(flags, sz) {static SZCODEA ach[] = "COMPMAN: "sz; OutputDebugStringA(ach); }
  113. #else
  114. #define DebugErr(flags, sz) {static SZCODE ach[] = "COMPMAN: "sz; DebugOutput(flags | DBF_MMSYSTEM, ach); }
  115. #endif
  116. #else // !DEBUG_RETAIL
  117. #define RPF(x)
  118. #define ROUT(sz)
  119. #define ICDebugMessage(hic, msg, dw1, dw2)
  120. #define ICDebugReturn(err) err
  121. #define DebugErr(flags, sz)
  122. #endif
  123. #ifndef WF_WINNT
  124. #define WF_WINNT 0x4000
  125. #endif
  126. #ifdef _WIN32
  127. #define IsWow() FALSE
  128. #else
  129. #define IsWow() ((BOOL) (GetWinFlags() & WF_WINNT))
  130. #define GetDriverModuleHandle(h) (IsWow() ? h : GetDriverModuleHandle(h))
  131. #endif
  132. // HACK!
  133. //
  134. //
  135. #if defined _WIN32 && !defined UNICODE
  136. #pragma message ("hack! use DrvGetModuleHandle on Chicago")
  137. #undef GetDriverModuleHandle
  138. #define GetDriverModuleHandle(h) DrvGetModuleHandle(h)
  139. extern HMODULE _stdcall DrvGetModuleHandle(HDRVR);
  140. #endif
  141. __inline void ictokey(DWORD fccType, DWORD fcc, LPTSTR sz)
  142. {
  143. int i = wsprintf(sz, szICKey, (LPSTR)&(fccType),(LPSTR)&(fcc));
  144. while (i>0 && sz[i-1] == ' ')
  145. sz[--i] = 0;
  146. }
  147. #define WIDTHBYTES(i) ((unsigned)((i+31)&(~31))/8) /* ULONG aligned ! */
  148. #define DIBWIDTHBYTES(bi) (int)WIDTHBYTES((int)(bi).biWidth * (int)(bi).biBitCount)
  149. #ifdef DEBUG_RETAIL
  150. STATICFN void ICDump(void);
  151. #endif
  152. //
  153. // the following array is used for 'installed' converters
  154. //
  155. // converters are either driver handles or indexes into this array
  156. //
  157. // 'function' converters are installed into this array, 'driver' converters
  158. // are installed in SYSTEM.INI
  159. //
  160. #define MAX_CONVERTERS 75 // maximum installed converters.
  161. typedef struct {
  162. DWORD dwSmag; // 'Smag'
  163. HTASK hTask; // owner task.
  164. DWORD fccType; // converter type ie 'vidc'
  165. DWORD fccHandler; // converter id ie 'rle '
  166. HDRVR hDriver; // handle of driver
  167. LPARAM dwDriver; // driver id for functions
  168. DRIVERPROC DriverProc; // function to call
  169. #ifdef NT_THUNK16
  170. DWORD h32; // 32-bit driver handle
  171. #endif
  172. } IC, *PIC;
  173. IC aicConverters[MAX_CONVERTERS];
  174. int giMaxConverters = 0; // High water mark of installed converters
  175. /*
  176. * We dynamically allocate a buffer used in ICInfo to read all the
  177. * installable compressor definitions from system.ini.
  178. * The buffer is freed when the driver is unloaded (in IC_Unload).
  179. * The previous code had a buffer which was only freed when the executable
  180. * was unloaded, and not freed on DLL unload.
  181. */
  182. static LPVOID lpICInfoMem = NULL;
  183. /*****************************************************************************
  184. ****************************************************************************/
  185. LRESULT CALLBACK DriverProcNull(DWORD_PTR dwDriverID, HANDLE hDriver, UINT wMessage, LPARAM dwParam1, LPARAM dwParam2)
  186. {
  187. DPF(("codec called after it has been removed with ICRemove\r\n"));
  188. return ICERR_UNSUPPORTED;
  189. }
  190. /*****************************************************************************
  191. ****************************************************************************/
  192. #if defined _WIN32
  193. STATICFN HDRVR LoadDriver(LPWSTR szDriver, DRIVERPROC FAR *lpDriverProc);
  194. #else
  195. STATICFN HDRVR LoadDriver(LPSTR szDriver, DRIVERPROC FAR *lpDriverProc);
  196. #endif
  197. STATICFN void FreeDriver(HDRVR hDriver);
  198. /*****************************************************************************
  199. driver cache - to make enuming/loading faster we keep the last N
  200. module's open for a while.
  201. ****************************************************************************/
  202. #define NEVERCACHECODECS // turn caching off for M6....
  203. #if defined _WIN32 || defined NEVERCACHECODECS
  204. #define CacheModule(x)
  205. #else
  206. #define N_MODULES 10 //!!!????
  207. HMODULE ahModule[N_MODULES];
  208. int iModule = 0;
  209. STATICFN void CacheModule(HMODULE hModule)
  210. {
  211. char ach[128];
  212. //
  213. // what if this module is in the list currently?
  214. //
  215. #if 0
  216. // we dont do this so unused compressors will fall off the end....
  217. int i;
  218. for (i=0; i<N_MODULES; i++)
  219. {
  220. if (ahModule[i] && ahModule[i] == hModule)
  221. return;
  222. }
  223. #endif
  224. //
  225. // add this module to the cache
  226. //
  227. if (hModule)
  228. {
  229. extern HMODULE ghInst; // in MSVIDEO/init.c
  230. int iUsage;
  231. GetModuleFileNameA(hModule, ach, sizeof(ach));
  232. DPF(("Loading module: %s\r\n", (LPSTR)ach));
  233. #ifndef _WIN32 // On NT GetModuleUsage always returns 1. So... we cache
  234. iUsage = GetModuleUsage(ghInst);
  235. #endif
  236. LoadLibraryA(ach);
  237. #ifndef _WIN32 // On NT GetModuleUsage always returns 1. So... we cache
  238. //
  239. // dont cache modules that link to MSVIDEO
  240. // we should realy do a toolhelp thing!
  241. // or force apps to call VFWInit and VFWExit()
  242. //
  243. // The NT position is more awkward..!
  244. //
  245. if (iUsage != GetModuleUsage(ghInst))
  246. {
  247. DPF(("Not caching this module because it links to MSVIDEO\r\n"));
  248. FreeLibrary(hModule);
  249. return;
  250. }
  251. #endif
  252. }
  253. //
  254. // free module in our slot.
  255. //
  256. if (ahModule[iModule] != NULL)
  257. {
  258. #ifdef DEBUG
  259. GetModuleFileNameA(ahModule[iModule], ach, sizeof(ach));
  260. DPF(("Freeing module: %s Handle==%8x\r\n", (LPSTR)ach, ahModule[iModule]));
  261. if (hModule!=NULL) {
  262. GetModuleFileNameA(hModule, ach, sizeof(ach));
  263. DPF(("Replacing with: %s Handle==%8x\r\n", (LPSTR)ach, hModule));
  264. } else
  265. DPF(("Slot now empty\r\n"));
  266. #endif
  267. FreeLibrary(ahModule[iModule]);
  268. }
  269. ahModule[iModule] = hModule;
  270. iModule++;
  271. if (iModule >= N_MODULES)
  272. iModule = 0;
  273. }
  274. #endif
  275. /*****************************************************************************
  276. ****************************************************************************/
  277. /*****************************************************************************
  278. * FixFOURCC - clean up a FOURCC
  279. ****************************************************************************/
  280. INLINE STATICFN DWORD Fix4CC(DWORD fcc)
  281. {
  282. int i;
  283. if (fcc > 256)
  284. {
  285. AnsiLowerBuff((LPSTR)&fcc, sizeof(fcc));
  286. for (i=0; i<4; i++)
  287. {
  288. if (((LPSTR)&fcc)[i] == 0)
  289. for (; i<4; i++)
  290. ((LPSTR)&fcc)[i] = ' ';
  291. }
  292. }
  293. return fcc;
  294. }
  295. /*****************************************************************************
  296. * @doc INTERNAL IC
  297. *
  298. * @api PIC | FindConverter |
  299. * search the converter list for a un-opened converter
  300. *
  301. ****************************************************************************/
  302. STATICFN PIC FindConverter(DWORD fccType, DWORD fccHandler)
  303. {
  304. int i;
  305. PIC pic;
  306. // By running the loop to <= giMaxConverters we allow an empty slot to
  307. // be found.
  308. for (i=0; i<=giMaxConverters; i++)
  309. {
  310. pic = &aicConverters[i];
  311. if (pic->fccType == fccType &&
  312. pic->fccHandler == fccHandler &&
  313. pic->dwDriver == 0L)
  314. {
  315. if (pic->DriverProc != NULL && IsBadCodePtr((FARPROC)pic->DriverProc))
  316. {
  317. pic->DriverProc = NULL;
  318. ICClose((HIC)pic);
  319. DPF(("NO driver for fccType=%4.4hs, Handler=%4.4hs\n", (LPSTR)&fccType, (LPSTR)&fccHandler));
  320. return NULL;
  321. }
  322. if ((0 == fccType + fccHandler)
  323. && (i < (MAX_CONVERTERS-1))
  324. && (i==giMaxConverters))
  325. {
  326. ++giMaxConverters; // Up the high water mark
  327. }
  328. DPF(("Possible driver for fccType=%4.4hs, Handler=%4.4hs, Slot %d\n", (LPSTR)&fccType, (LPSTR)&fccHandler, i));
  329. return pic;
  330. }
  331. }
  332. DPF(("FindConverter: NO drivers for fccType=%4.4hs, Handler=%4.4hs\n", (LPSTR)&fccType, (LPSTR)&fccHandler));
  333. return NULL;
  334. }
  335. #ifdef _WIN32
  336. /*
  337. * we need to hold a critical section around the ICOpen code to protect
  338. * multi-thread simultaneous opens. This critsec is initialized by
  339. * ICDllEntryPoint (called from video\init.c at dll attach time) and is deleted
  340. * by ICDllEntryPoint (called from video\init.c at dll detach time).
  341. */
  342. CRITICAL_SECTION ICOpenCritSec;
  343. #ifdef DEBUGLOAD
  344. // There is a suspicion that a nasty problem exists on NT whereby the DLL
  345. // load/unload routines might not be called in certain esoteric cases. As
  346. // we rely on these routines to set up the ICOpenCritSec code has been
  347. // added to verify that the critical section has indeed been set up. On
  348. // LOAD we turn one bit on in a global variable. On UNLOAD we turn that
  349. // bit off and turn another bit on.
  350. DWORD gdwLoadFlags = 0;
  351. #define ICLOAD_CALLED 0x00010000
  352. #define ICUNLOAD_CALLED 0x00000001
  353. #endif
  354. //
  355. // We load/unload wow32.dll here. This is used in the thunking code which
  356. // just does GetModuleHandle on it. This is not really necessary in
  357. // Daytona but is in Chicago.
  358. //
  359. // WORSE: IT IS IMPERATIVE TO NOT DO THIS ON DAYTONA. The Daytona code
  360. // uses the fact the WOW32.DLL is loaded in the context of this process
  361. // as the indication that it is executing in the WOW process.
  362. #ifdef CHICAGO
  363. HMODULE hWow32 = NULL;
  364. #endif
  365. #endif
  366. #ifdef _WIN32
  367. //--------------------------------------------------------------------------;
  368. //
  369. // BOOL ICDllEntryPoint [32-bit]
  370. //
  371. // Description:
  372. // Called by msvideo's DllEntryPoint
  373. //
  374. // History:
  375. // 11/02/94 [frankye]
  376. //
  377. //--------------------------------------------------------------------------;
  378. #ifdef LS_THUNK32
  379. BOOL PASCAL ls_ThunkConnect32(LPCSTR pszDll16, LPCSTR pszDll32, HINSTANCE hinst, DWORD dwReason);
  380. BOOL PASCAL sl_ThunkConnect32(LPCSTR pszDll16, LPCSTR pszDll32, HINSTANCE hinst, DWORD dwReason);
  381. #endif
  382. BOOL WINAPI ICDllEntryPoint(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  383. {
  384. switch (fdwReason)
  385. {
  386. case DLL_PROCESS_ATTACH:
  387. {
  388. //DPFS(dbgInit, 0, "ICDllEntryPoint(DLL_PROCESS_ATTACH)");
  389. #ifdef DEBUGLOAD
  390. if (gdwLoadFlags & ICLOAD_CALLED) {
  391. #ifdef DEBUG
  392. DPF(("!IC open crit sec already set up"));
  393. #endif
  394. }
  395. gdwLoadFlags |= ICLOAD_CALLED;
  396. gdwLoadFlags &= ~ICUNLOAD_CALLED;
  397. #endif
  398. InitializeCriticalSection(&ICOpenCritSec);
  399. #ifdef LS_THUNK32
  400. hWow32 = LoadLibrary(TEXT("WOW32.DLL"));
  401. ls_ThunkConnect32(TEXT("MSVIDEO.DLL"), TEXT("MSVFW32.DLL"), hinstDLL, fdwReason);
  402. sl_ThunkConnect32(TEXT("MSVIDEO.DLL"), TEXT("MSVFW32.DLL"), hinstDLL, fdwReason);
  403. #endif
  404. return TRUE;
  405. }
  406. case DLL_PROCESS_DETACH:
  407. {
  408. //DPFS(dbgInit, 0, "ICDllEntryPoint(DLL_PROCESS_DETACH)");
  409. #ifdef LS_THUNK32
  410. ls_ThunkConnect32(TEXT("MSVIDEO.DLL"), TEXT("MSVFW32.DLL"), hinstDLL, fdwReason);
  411. sl_ThunkConnect32(TEXT("MSVIDEO.DLL"), TEXT("MSVFW32.DLL"), hinstDLL, fdwReason);
  412. if (NULL != hWow32) FreeLibrary(hWow32);
  413. #endif
  414. DeleteCriticalSection(&ICOpenCritSec);
  415. #ifdef DEBUGLOAD
  416. gdwLoadFlags |= ICUNLOAD_CALLED;
  417. gdwLoadFlags &= ~ICLOAD_CALLED;
  418. #endif
  419. if (lpICInfoMem) {
  420. GlobalFreePtr(lpICInfoMem);
  421. lpICInfoMem = NULL;
  422. }
  423. #ifdef CHICAGO
  424. dbgCheckShutdown();
  425. #endif
  426. return TRUE;
  427. }
  428. }
  429. return TRUE;
  430. }
  431. #else
  432. //--------------------------------------------------------------------------;
  433. //
  434. // BOOL ICDllEntryPoint [16-bit]
  435. //
  436. // Description:
  437. // Called by msvideo's DllEntryPoint
  438. //
  439. // History:
  440. // 11/02/94 [frankye]
  441. //
  442. //--------------------------------------------------------------------------;
  443. BOOL FAR PASCAL ls_ThunkConnect16(LPCSTR pszDll16, LPCSTR pszDll32, HINSTANCE hinst, DWORD dwReason);
  444. BOOL FAR PASCAL sl_ThunkConnect16(LPCSTR pszDll16, LPCSTR pszDll32, HINSTANCE hinst, DWORD dwReason);
  445. BOOL FAR PASCAL ICDllEntryPoint(DWORD dwReason, HINSTANCE hinstDLL, WORD wDS, WORD wHeapSize, DWORD dwReserved1, WORD wReserved2)
  446. {
  447. PICMGARB pig;
  448. DPFS(dbgInit, 0, "ICDllEntryPoint()");
  449. switch (dwReason)
  450. {
  451. case 1:
  452. {
  453. if (NULL == (pig = pigFind()))
  454. {
  455. if (NULL == (pig = pigNew()))
  456. {
  457. return FALSE;
  458. }
  459. }
  460. #ifdef LS_THUNK16
  461. ls_ThunkConnect16(TEXT("MSVIDEO.DLL"), TEXT("MSVFW32.DLL"), hinstDLL, dwReason);
  462. sl_ThunkConnect16(TEXT("MSVIDEO.DLL"), TEXT("MSVFW32.DLL"), hinstDLL, dwReason);
  463. #endif
  464. if (1 == ++pig->cUsage)
  465. {
  466. DPFS(dbgInit, 0, "ICProcessAttach: New process %08lXh", pig->pid);
  467. //
  468. // We can do one-time-per-process init here...
  469. //
  470. }
  471. return TRUE;
  472. }
  473. case 0:
  474. {
  475. if (NULL == (pig = pigFind()))
  476. {
  477. DPF(0, "!ICProcessDetach: ERROR: Being freed by process %08lXh in which it was not loaded", GetCurrentProcessId());
  478. DebugErr(DBF_ERROR, "ICProcessDetach: ERROR: Being freed by a process in which it was not loaded");
  479. return FALSE;
  480. }
  481. if (0 == --pig->cUsage)
  482. {
  483. //
  484. // We can do one-time-per-process termination here...
  485. //
  486. DPFS(dbgInit, 0, "ICProcessDetach: Terminating for process %08lXh", pig->pid);
  487. #ifdef NT_THUNK16
  488. genthunkTerminate(pig);
  489. #endif
  490. pigDelete(pig);
  491. dbgCheckShutdown();
  492. }
  493. #ifdef LS_THUNK16
  494. ls_ThunkConnect16(TEXT("MSVIDEO.DLL"), TEXT("MSVFW32.DLL"), hinstDLL, dwReason);
  495. sl_ThunkConnect16(TEXT("MSVIDEO.DLL"), TEXT("MSVFW32.DLL"), hinstDLL, dwReason);
  496. #endif
  497. return TRUE;
  498. }
  499. }
  500. return TRUE;
  501. }
  502. #endif
  503. /*****************************************************************************
  504. ****************************************************************************/
  505. __inline BOOL ICValid(HIC hic)
  506. {
  507. PIC pic = (PIC)hic;
  508. if (pic < &aicConverters[0] ||
  509. pic >= &aicConverters[MAX_CONVERTERS] ||
  510. pic->dwSmag != SMAG)
  511. {
  512. DebugErr(DBF_ERROR, "Invalid HIC\r\n");
  513. return FALSE;
  514. }
  515. return TRUE;
  516. }
  517. /*****************************************************************************
  518. ****************************************************************************/
  519. #define V_HIC(hic) \
  520. if (!ICValid(hic)) \
  521. return ICERR_BADHANDLE;
  522. /*****************************************************************************
  523. * @doc INTERNAL IC
  524. *
  525. * @api BOOL | ICCleanup | This function is called when a task exits or
  526. * MSVIDEO.DLL is being unloaded.
  527. *
  528. * @parm HTASK | hTask | the task being terminated, NULL if DLL being unloaded
  529. *
  530. * @rdesc Returns nothing
  531. *
  532. * @comm currently MSVIDEO only calles this function from it's WEP()
  533. *
  534. ****************************************************************************/
  535. void FAR PASCAL ICCleanup(HTASK hTask)
  536. {
  537. int i;
  538. PIC pic;
  539. //
  540. // free all HICs
  541. //
  542. for (i=0; i < giMaxConverters; i++)
  543. {
  544. pic = &aicConverters[i];
  545. if (pic->dwDriver != 0L && (pic->hTask == hTask || hTask == NULL))
  546. {
  547. ROUT("Decompressor left open, closing\r\n");
  548. ICClose((HIC)pic);
  549. }
  550. }
  551. #ifdef N_MODULES
  552. //
  553. // free the module cache.
  554. //
  555. for (i=0; i<N_MODULES; i++)
  556. CacheModule(NULL);
  557. #endif
  558. }
  559. /*****************************************************************************
  560. * @doc EXTERNAL IC ICAPPS
  561. *
  562. * @api BOOL | ICInstall | This function installs a new compressor
  563. * or decompressor.
  564. *
  565. * @parm DWORD | fccType | Specifies a four-character code indicating the
  566. * type of data used by the compressor or decompressor. Use 'vidc'
  567. * for a video compressor or decompressor.
  568. *
  569. * @parm DWORD | fccHandler | Specifies a four-character code identifying
  570. * a specific compressor or decompressor.
  571. *
  572. * @parm LPARAM | lParam | Specifies a pointer to a zero-terminated
  573. * string containing the name of the compressor or decompressor,
  574. * or it specifies a far pointer to a function used for compression
  575. * or decompression. The contents of this parameter are defined
  576. * by the flags set for <p wFlags>.
  577. *
  578. * @parm LPSTR | szDesc | Specifies a pointer to a zero-terminated string
  579. * describing the installed compressor. Not use.
  580. *
  581. * @parm UINT | wFlags | Specifies flags defining the contents of <p lParam>.
  582. * The following flags are defined:
  583. *
  584. * @flag ICINSTALL_DRIVER | Indicates <p lParam> is a pointer to a zero-terminated
  585. * string containing the name of the compressor to install.
  586. *
  587. * @flag ICINSTALL_FUNCTION | Indicates <p lParam> is a far pointer to
  588. * a compressor function. This function should
  589. * be structured like the <f DriverProc> entry
  590. * point function used by compressors.
  591. *
  592. * @rdesc Returns TRUE if successful.
  593. *
  594. * @comm Applications must still open the installed compressor or
  595. * decompressor before it can use the compressor or decompressor.
  596. *
  597. * Usually, compressors and decompressors are installed by the user
  598. * with the Drivers option of the Control Panel.
  599. *
  600. * If your application installs a function as a compressor or
  601. * decompressor, it should remove the compressor or decompressor
  602. * with <f ICRemove> before it terminates. This prevents other
  603. * applications from trying to access the function when it is not
  604. * available.
  605. *
  606. *
  607. * @xref <f ICRemove>
  608. ****************************************************************************/
  609. BOOL VFWAPI ICInstall(DWORD fccType, DWORD fccHandler, LPARAM lParam, LPSTR szDesc, UINT wFlags)
  610. {
  611. TCHAR achKey[20];
  612. TCHAR buf[256];
  613. DWORD n;
  614. PIC pic;
  615. ICEnterCrit(&ICOpenCritSec);
  616. fccType = Fix4CC(fccType);
  617. fccHandler = Fix4CC(fccHandler);
  618. if ((pic = FindConverter(fccType, fccHandler)) == NULL)
  619. pic = FindConverter(0L, 0L);
  620. if (wFlags & ICINSTALL_DRIVER)
  621. {
  622. //
  623. // dwConverter is the file name of a driver to install.
  624. //
  625. ictokey(fccType, fccHandler, achKey);
  626. #ifdef UNICODE
  627. if (wFlags & ICINSTALL_UNICODE) {
  628. lstrcpy(buf, (LPWSTR)lParam);
  629. n = lstrlen(buf) + 1; // Point past the terminating zero;
  630. if (szDesc)
  631. {
  632. lstrcpyn(buf+n, (LPWSTR)szDesc, NUMELMS(buf)-n);
  633. n += lstrlen(buf+n);
  634. }
  635. buf[n]=0; // Always guarantee a second string - even if a null one
  636. } else {
  637. // Convert the ANSI strings to UNICODE
  638. n = 1 + wsprintf(buf, TEXT("%hs"), (LPSTR) lParam);
  639. if (szDesc) {
  640. n += 1 + wsprintf(buf+n, TEXT("%hs"), szDesc);
  641. }
  642. }
  643. // Buf now contains two strings, the second of which may be null (the description)
  644. #else
  645. lstrcpy(buf, (LPSTR)lParam);
  646. if (szDesc)
  647. {
  648. lstrcat(buf, TEXT(" "));
  649. lstrcat(buf, szDesc);
  650. }
  651. #endif
  652. ICLeaveCrit(&ICOpenCritSec);
  653. // Data is written (via the inifilemapping) to
  654. // HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Drivers32
  655. // HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Drivers.desc
  656. if (myWritePrivateProfileString(gszDrivers,achKey,buf))
  657. {
  658. // delete the installable compressors entry for this driver
  659. myWritePrivateProfileString(gszIniSect,achKey,NULL);
  660. return TRUE;
  661. }
  662. return(FALSE);
  663. }
  664. else if (wFlags & ICINSTALL_FUNCTION)
  665. {
  666. if (pic == NULL)
  667. {
  668. ICLeaveCrit(&ICOpenCritSec);
  669. return FALSE;
  670. }
  671. pic->dwSmag = SMAG;
  672. pic->fccType = fccType;
  673. pic->fccHandler = fccHandler;
  674. pic->dwDriver = 0L;
  675. pic->hDriver = NULL;
  676. pic->DriverProc = (DRIVERPROC)lParam;
  677. DPF(("ICInstall, fccType=%4.4hs, Handler=%4.4hs, Pic %x\n", (LPSTR)&fccType, (LPSTR)&fccHandler, pic));
  678. ICLeaveCrit(&ICOpenCritSec);
  679. return TRUE;
  680. }
  681. #if 0
  682. else if (wFlags & ICINSTALL_HDRV)
  683. {
  684. if (pic == NULL)
  685. {
  686. ICLeaveCrit(&ICOpenCritSec);
  687. return FALSE;
  688. }
  689. pic->fccType = fccType;
  690. pic->fccHandler = fccHandler;
  691. pic->hDriver = (HDRVR)lParam;
  692. pic->dwDriver = 0L;
  693. pic->DrvProc = NULL;
  694. ICLeaveCrit(&ICOpenCritSec);
  695. return TRUE;
  696. }
  697. #endif
  698. ICLeaveCrit(&ICOpenCritSec);
  699. return FALSE;
  700. }
  701. /*****************************************************************************
  702. * @doc EXTERNAL IC ICAPPS
  703. *
  704. * @api BOOL | ICRemove | This function removes an installed compressor.
  705. *
  706. * @parm DWORD | fccType | Specifies a four-character code indicating the
  707. * type of data used by the compressor. Use 'vidc' for video compressors.
  708. *
  709. * @parm DWORD | fccHandler | Specifies a four-character code identifying
  710. * a specific compressor.
  711. *
  712. * @parm UINT | wFlags | Not used.
  713. *
  714. * @rdesc Returns TRUE if successful.
  715. *
  716. * @xref <f ICInstall>
  717. ****************************************************************************/
  718. BOOL VFWAPI ICRemove(DWORD fccType, DWORD fccHandler, UINT wFlags)
  719. {
  720. TCHAR achKey[20];
  721. PIC pic;
  722. ICEnterCrit(&ICOpenCritSec);
  723. fccType = Fix4CC(fccType);
  724. fccHandler = Fix4CC(fccHandler);
  725. if (pic = FindConverter(fccType, fccHandler))
  726. {
  727. int i;
  728. //
  729. // we should realy keep usage counts!!!
  730. //
  731. for (i=0; i<giMaxConverters; i++)
  732. {
  733. if (pic->DriverProc == aicConverters[i].DriverProc)
  734. {
  735. DPF(("ACK! Handler is in use\r\n"));
  736. pic->DriverProc = (DRIVERPROC)DriverProcNull;
  737. }
  738. }
  739. ICClose((HIC)pic);
  740. }
  741. else
  742. {
  743. // Remove the information
  744. ictokey(fccType, fccHandler, achKey);
  745. myWritePrivateProfileString(gszIniSect,achKey,NULL);
  746. myWritePrivateProfileString(gszDrivers,achKey,NULL);
  747. }
  748. ICLeaveCrit(&ICOpenCritSec);
  749. return TRUE;
  750. }
  751. //
  752. // Internal routine to enumerate all the installed drivers
  753. //
  754. BOOL ReadDriversInfo()
  755. {
  756. LPSTR psz = NULL; // THIS IS ALWAYS an ANSI string pointer!
  757. if (lpICInfoMem == NULL) {
  758. UINT cbBuffer = 125 * sizeof(TCHAR);
  759. UINT cchBuffer;
  760. ICEnterCrit(&ICOpenCritSec);
  761. for (;;)
  762. {
  763. lpICInfoMem = GlobalAllocPtr(GMEM_SHARE | GHND, cbBuffer);
  764. if (!lpICInfoMem) {
  765. DPF(("Out of memory for SYSTEM.INI keys\r\n"));
  766. ICLeaveCrit(&ICOpenCritSec);
  767. return FALSE;
  768. }
  769. cchBuffer = (UINT)myGetPrivateProfileString(gszDrivers,
  770. NULL,
  771. szNull,
  772. lpICInfoMem,
  773. cbBuffer / sizeof(TCHAR));
  774. if (cchBuffer < ((cbBuffer/sizeof(TCHAR)) - 5)) {
  775. cchBuffer += (UINT)myGetPrivateProfileString(gszIniSect,
  776. NULL,
  777. szNull,
  778. (LPTSTR)lpICInfoMem + cchBuffer,
  779. (cbBuffer/sizeof(TCHAR)) - cchBuffer);
  780. //
  781. // if all of the INI data fit, we can
  782. // leave the loop
  783. //
  784. if (cchBuffer < ((cbBuffer/sizeof(TCHAR)) - 5))
  785. break;
  786. }
  787. GlobalFreePtr(lpICInfoMem), lpICInfoMem = NULL;
  788. //
  789. // if cannot fit drivers section in 32k, then something is horked
  790. // with the section... so let's bail.
  791. //
  792. if (cbBuffer >= 0x8000) {
  793. DPF(("SYSTEM.INI keys won't fit in 32K????\r\n"));
  794. ICLeaveCrit(&ICOpenCritSec);
  795. return FALSE;
  796. }
  797. //
  798. // double the size of our buffer and try again.
  799. //
  800. cbBuffer *= 2;
  801. DPF(("Increasing size of SYSTEM.INI buffer to %d\r\n", cbBuffer));
  802. }
  803. #if defined UNICODE
  804. // convert the INI data from UNICODE to ANSI
  805. //
  806. psz = GlobalAllocPtr (GMEM_SHARE | GHND, cchBuffer + 7);
  807. if ( ! psz) {
  808. GlobalFreePtr (lpICInfoMem), lpICInfoMem = NULL;
  809. ICLeaveCrit(&ICOpenCritSec);
  810. return FALSE;
  811. }
  812. mmWideToAnsi (psz, lpICInfoMem, cchBuffer+2);
  813. GlobalFreePtr (lpICInfoMem);
  814. lpICInfoMem = psz;
  815. #endif
  816. // convert codec information to lowercase
  817. for (psz = lpICInfoMem; *psz != 0; psz += lstrlenA(psz) + 1)
  818. {
  819. #if 0 // we only put valid codecs into lpICInfoMem these days
  820. if (psz[4] != '.')
  821. continue;
  822. #endif
  823. // convert this piece to lowercase
  824. CharLowerA (psz);
  825. DPF(("Compressor: %hs\n", psz));
  826. }
  827. ICLeaveCrit(&ICOpenCritSec);
  828. }
  829. return (lpICInfoMem != NULL);
  830. }
  831. /*****************************************************************************
  832. * @doc EXTERNAL IC ICAPPS
  833. *
  834. * @api BOOL | ICInfo | This function returns information about
  835. * specific installed compressors, or it enumerates
  836. * the compressors installed.
  837. *
  838. * @parm DWORD | fccType | Specifies a four-character code indicating
  839. * the type of compressor. To match all compressor types specify zero.
  840. *
  841. * @parm DWORD | fccHandler | Specifies a four-character code identifying
  842. * a specific compressor, or a number between 0 and the number
  843. * of installed compressors of the type specified by <t fccType>.
  844. *
  845. * @parm ICINFO FAR * | lpicinfo | Specifies a far pointer to a
  846. * <t ICINFO> structure used to return
  847. * information about the compressor.
  848. *
  849. * @comm This function does not return full informaiton about
  850. * a compressor or decompressor. Use <f ICGetInfo> for full
  851. * information.
  852. *
  853. * @rdesc Returns TRUE if successful.
  854. ****************************************************************************/
  855. #ifdef NT_THUNK16
  856. BOOL VFWAPI ICInfoInternal(DWORD fccType, DWORD fccHandler, ICINFO FAR * lpicinfo);
  857. // If we are compiling the thunks, then the ICINFO entry point calls
  858. // the 32 bit thunk, or calls the real ICInfo code (as ICInfoInternal).
  859. // We deliberately give precedence to 16 bit compressors, although this
  860. // ordering can be trivially changed.
  861. // ??: Should we allow an INI setting to change the order?
  862. BOOL VFWAPI ICInfo(DWORD fccType, DWORD fccHandler, ICINFO FAR * lpicinfo)
  863. {
  864. #ifdef DEBUG
  865. BOOL fResult;
  866. #endif
  867. //
  868. // See if there is a 32-bit compressor we can use
  869. //
  870. if (ICInfoInternal(fccType, fccHandler, lpicinfo)) {
  871. return(TRUE);
  872. }
  873. #ifdef DEBUG
  874. fResult = (ICInfo32(fccType, fccHandler, lpicinfo));
  875. DPF(("ICInfo32 returned %s\r\n", (fResult ? "TRUE" : "FALSE")));
  876. return fResult;
  877. #else
  878. return (ICInfo32(fccType, fccHandler, lpicinfo));
  879. #endif
  880. }
  881. // Now map ICInfo calls to ICInfoInternal for the duration of the ICInfo
  882. // routine. This affects the two recursive calls within ICInfo.
  883. #define ICInfo ICInfoInternal
  884. #endif // NT_THUNK16
  885. BOOL VFWAPI ICInfo(DWORD fccType, DWORD fccHandler, ICINFO FAR * lpicinfo)
  886. {
  887. LPSTR psz = NULL; // THIS IS ALWAYS an ANSI string pointer!
  888. TCHAR buf[128];
  889. TCHAR achKey[20];
  890. int i;
  891. int iComp;
  892. PIC pic;
  893. if (lpicinfo == NULL)
  894. return FALSE;
  895. if (fccType > 0 && fccType < 256) {
  896. DPF(("fcctype invalid (%d)\n", fccType));
  897. return FALSE;
  898. }
  899. fccType = Fix4CC(fccType);
  900. fccHandler = Fix4CC(fccHandler);
  901. if (fccType != 0 && fccHandler > 256)
  902. {
  903. //
  904. // the user has given us a specific fccType and fccHandler
  905. // get the info and return.
  906. //
  907. if (pic = FindConverter(fccType, fccHandler))
  908. {
  909. ICGetInfo((HIC)pic, lpicinfo, sizeof(ICINFO));
  910. return TRUE;
  911. }
  912. else
  913. {
  914. lpicinfo->dwSize = sizeof(ICINFO);
  915. lpicinfo->fccType = fccType;
  916. lpicinfo->fccHandler = fccHandler;
  917. lpicinfo->dwFlags = 0;
  918. lpicinfo->dwVersionICM = ICVERSION;
  919. lpicinfo->dwVersion = 0;
  920. lpicinfo->szDriver[0] = 0;
  921. lpicinfo->szDescription[0] = 0;
  922. lpicinfo->szName[0] = 0;
  923. DPF(("ICInfo, fccType=%4.4hs, Handler=%4.4hs\n", (LPSTR)&fccType, (LPSTR)&fccHandler));
  924. ictokey(fccType, fccHandler, achKey);
  925. if (!myGetPrivateProfileString(gszDrivers,achKey,szNull,buf,NUMELMS(buf)) &&
  926. !myGetPrivateProfileString(gszIniSect,achKey,szNull,buf,NUMELMS(buf)))
  927. {
  928. DPF(("NO information in DRIVERS section\n"));
  929. return FALSE;
  930. }
  931. for (i=0; buf[i] && buf[i] != TEXT(' '); ++i)
  932. lpicinfo->szDriver[i] = buf[i];
  933. lpicinfo->szDriver[i] = 0;
  934. //
  935. // the driver must be opened to get description
  936. //
  937. lpicinfo->szDescription[0] = 0;
  938. return TRUE;
  939. }
  940. }
  941. else
  942. {
  943. //
  944. // the user has given us a specific fccType and a
  945. // ordinal for fccHandler, enum the compressors, looking for
  946. // the nth compressor of 'fccType'
  947. //
  948. iComp = (int)fccHandler;
  949. //
  950. // walk the installed converters.
  951. //
  952. for (i=0; i < giMaxConverters; i++)
  953. {
  954. pic = &aicConverters[i];
  955. if (pic->fccType != 0 &&
  956. (fccType == 0 || pic->fccType == fccType) &&
  957. pic->dwDriver == 0L && iComp-- == 0)
  958. {
  959. return ICInfo(pic->fccType, pic->fccHandler, lpicinfo);
  960. }
  961. }
  962. //
  963. // read all the keys. from [Drivers] and [Instalable Compressors]
  964. // if we havent read them before.
  965. //
  966. // NOTE: what we get back will always be ANSI or WIDE depending
  967. // on whether UNICODE is defined. If WIDE, we convert to
  968. // ANSI before exiting the if statement.
  969. //
  970. if (lpICInfoMem == NULL) {
  971. if (!ReadDriversInfo())
  972. return(FALSE);
  973. }
  974. // set our pointer psz to point to the beginning of
  975. // the buffer of INI information we just read.
  976. // remember that we KNOW that this is ANSI data now.
  977. //
  978. //assert (sizeof(*psz) == 1);
  979. //assert (lpICInfoMem != NULL);
  980. // loop through the buffer until we get to a double '\0'
  981. // which indicates the end of the data.
  982. //
  983. for (psz = lpICInfoMem; *psz != 0; psz += lstrlenA(psz) + 1)
  984. {
  985. #if 0 // there can only be valid codec in the memory block
  986. if (psz[4] != '.')
  987. continue;
  988. #endif
  989. // convert this piece to lowercase and check to see
  990. // if it matches the requested type signature
  991. //
  992. // NO. Done when first read. CharLowerA (psz);
  993. // if this is a match, and it's the one we wanted,
  994. // return its ICINFO
  995. //
  996. if ((fccType == 0 || fccType == *(DWORD UNALIGNED FAR *)psz)
  997. && iComp-- == 0)
  998. {
  999. return ICInfo(*(DWORD UNALIGNED FAR *)psz,
  1000. *(DWORD UNALIGNED FAR *)&psz[5],
  1001. lpicinfo);
  1002. }
  1003. }
  1004. #ifdef DAYTONA
  1005. // If we get to here, then the index is higher than the number
  1006. // of installed compressors.
  1007. //
  1008. // Write the number of compressors found into the structure.
  1009. // This value is used by the NT thunks to pass back to the 16
  1010. // bit side the maximum number of 32 bit compressors.
  1011. lpicinfo->fccHandler = (int)fccHandler-iComp;
  1012. // LATER: we MUST enumerate the count of installed msvideo drivers
  1013. // as well. However, lets see if this fixes the Adobe Premiere problem.
  1014. #endif
  1015. //
  1016. // now walk the msvideo drivers. these are listed in system.ini
  1017. // like so:
  1018. //
  1019. // [Drivers]
  1020. // MSVideo = driver
  1021. // MSVideo1 = driver
  1022. // MSVideoN =
  1023. //
  1024. if (fccType == 0 || fccType == ICTYPE_VCAP)
  1025. {
  1026. lstrcpy(achKey, szMSVideo);
  1027. if (iComp > 0)
  1028. wsprintf(achKey+lstrlen(achKey), (LPVOID)TEXT("%d"), iComp);
  1029. if (!myGetPrivateProfileString(gszDrivers,achKey,szNull,buf,NUMELMS(buf)))
  1030. return FALSE;
  1031. lpicinfo->dwSize = sizeof(ICINFO);
  1032. lpicinfo->fccType = ICTYPE_VCAP;
  1033. lpicinfo->fccHandler = iComp;
  1034. lpicinfo->dwFlags = 0;
  1035. lpicinfo->dwVersionICM = ICVERSION; //??? right for video?
  1036. lpicinfo->dwVersion = 0;
  1037. lpicinfo->szDriver[0] = 0;
  1038. lpicinfo->szDescription[0] = 0;
  1039. lpicinfo->szName[0] = 0;
  1040. for (i=0; buf[i] && buf[i] != TEXT(' '); i++)
  1041. lpicinfo->szDriver[i] = buf[i];
  1042. lpicinfo->szDriver[i] = 0;
  1043. return TRUE;
  1044. }
  1045. return FALSE;
  1046. }
  1047. }
  1048. #undef ICInfo
  1049. BOOL VFWAPI ICInfoInternal(DWORD fccType, DWORD fccHandler, ICINFO FAR * lpicinfo)
  1050. {
  1051. return(ICInfo(fccType, fccHandler, lpicinfo));
  1052. }
  1053. ////////////////////////////////////////////////////////////////////////////
  1054. ////////////////////////////////////////////////////////////////////////////
  1055. /*****************************************************************************
  1056. * @doc EXTERNAL IC ICAPPS
  1057. *
  1058. * @api LRESULT | ICGetInfo | This function obtains information about
  1059. * a compressor.
  1060. *
  1061. * @parm HIC | hic | Specifies a handle to a compressor.
  1062. *
  1063. * @parm ICINFO FAR * | lpicinfo | Specifies a far pointer to <t ICINFO> structure
  1064. * used to return information about the compressor.
  1065. *
  1066. * @parm DWORD | cb | Specifies the size, in bytes, of the structure pointed to
  1067. * by <p lpicinfo>.
  1068. *
  1069. * @rdesc Return the number of bytes copied into the data structure,
  1070. * or zero if an error occurs.
  1071. *
  1072. * @comm Use <f ICInfo> for full information about a compressor.
  1073. *
  1074. ****************************************************************************/
  1075. LRESULT VFWAPI ICGetInfo(HIC hic, ICINFO FAR *picinfo, DWORD cb)
  1076. {
  1077. PIC pic = (PIC)hic;
  1078. LRESULT dw;
  1079. V_HIC(hic);
  1080. picinfo->dwSize = sizeof(ICINFO);
  1081. picinfo->fccType = 0;
  1082. picinfo->fccHandler = 0;
  1083. picinfo->dwFlags = 0;
  1084. picinfo->dwVersionICM = ICVERSION;
  1085. picinfo->dwVersion = 0;
  1086. picinfo->szDriver[0] = 0;
  1087. picinfo->szDescription[0] = 0;
  1088. picinfo->szName[0] = 0;
  1089. #ifdef NT_THUNK16
  1090. if (!Is32bitHandle(hic))
  1091. #endif //NT_THUNK16
  1092. if (pic->hDriver)
  1093. {
  1094. #if defined _WIN32 && ! defined UNICODE
  1095. char szDriver[NUMELMS(picinfo->szDriver)];
  1096. GetModuleFileName (GetDriverModuleHandle (pic->hDriver),
  1097. szDriver, sizeof(szDriver));
  1098. mmAnsiToWide (picinfo->szDriver, szDriver, NUMELMS(szDriver));
  1099. #else
  1100. GetModuleFileName(GetDriverModuleHandle (pic->hDriver),
  1101. picinfo->szDriver, NUMELMS(picinfo->szDriver));
  1102. #endif
  1103. }
  1104. dw = ICSendMessage((HIC)pic, ICM_GETINFO, (DWORD_PTR)picinfo, cb);
  1105. return dw;
  1106. }
  1107. /*****************************************************************************
  1108. * @doc EXTERNAL IC ICAPPS
  1109. *
  1110. * @api LRESULT | ICSendMessage | This function sends a
  1111. * message to a compressor.
  1112. *
  1113. * @parm HIC | hic | Specifies the handle of the
  1114. * compressor to receive the message.
  1115. *
  1116. * @parm UINT | wMsg | Specifies the message to send.
  1117. *
  1118. * @parm DWORD | dw1 | Specifies additional message-specific information.
  1119. *
  1120. * @parm DWORD | dw2 | Specifies additional message-specific information.
  1121. *
  1122. * @rdesc Returns a message-specific result.
  1123. ****************************************************************************/
  1124. LRESULT VFWAPI ICSendMessage(HIC hic, UINT msg, DWORD_PTR dw1, DWORD_PTR dw2)
  1125. {
  1126. PIC pic = (PIC)hic;
  1127. LRESULT l;
  1128. V_HIC(hic);
  1129. #ifdef NT_THUNK16
  1130. //
  1131. // If it's a 32-bit handle then send it to the 32-bit code
  1132. // We need to take some extra care with ICM_DRAW_SUGGESTFORMAT
  1133. // which can include a HIC in the ICDRAWSUGGEST structure.
  1134. //
  1135. #define ICD(dw1) ((ICDRAWSUGGEST FAR *)(dw1))
  1136. if (pic->h32) {
  1137. if ((msg == ICM_DRAW_SUGGESTFORMAT)
  1138. && (((ICDRAWSUGGEST FAR *)dw1)->hicDecompressor))
  1139. {
  1140. // We are in the problem area.
  1141. // IF the hicDecompressor field is NULL, pass as is.
  1142. // IF it identifies a 32 bit decompressor, translate the handle
  1143. // OTHERWISE... what? We have a 32 bit compressor, that is
  1144. // being told it can use a 16 bit decompressor!!
  1145. if ( ((PIC) (((ICDRAWSUGGEST FAR *)dw1)->hicDecompressor))->h32)
  1146. {
  1147. ICD(dw1)->hicDecompressor
  1148. = (HIC)((PIC)(ICD(dw1)->hicDecompressor))->h32;
  1149. } else
  1150. {
  1151. ICD(dw1)->hicDecompressor = NULL; // Sigh...
  1152. }
  1153. }
  1154. return ICSendMessage32(pic->h32, msg, dw1, dw2);
  1155. }
  1156. #endif //NT_THUNK16
  1157. ICDebugMessage(hic, msg, dw1, dw2);
  1158. l = pic->DriverProc(pic->dwDriver, (HDRVR)1, msg, dw1, dw2);
  1159. #if 1 //!!! is this realy needed! !!!yes I think it is
  1160. //
  1161. // special case some messages and give default values.
  1162. //
  1163. if (l == ICERR_UNSUPPORTED)
  1164. {
  1165. switch (msg)
  1166. {
  1167. case ICM_GETDEFAULTQUALITY:
  1168. *((LPDWORD)dw1) = ICQUALITY_HIGH;
  1169. l = ICERR_OK;
  1170. break;
  1171. case ICM_GETDEFAULTKEYFRAMERATE:
  1172. *((LPDWORD)dw1) = 15;
  1173. l = ICERR_OK;
  1174. break;
  1175. }
  1176. }
  1177. #endif
  1178. return ICDebugReturn(l);
  1179. }
  1180. #ifndef _WIN32
  1181. /*****************************************************************************
  1182. * @doc EXTERNAL IC ICAPPS
  1183. *
  1184. * @api LRESULT | ICMessage | This function sends a
  1185. * message and a variable number of arguments to a compressor.
  1186. * If a macro is defined for the message you want to send,
  1187. * use the macro rather than this function.
  1188. *
  1189. * @parm HIC | hic | Specifies the handle of the
  1190. * compressor to receive the message.
  1191. *
  1192. * @parm UINT | msg | Specifies the message to send.
  1193. *
  1194. * @parm UINT | cb | Specifies the size, in bytes, of the
  1195. * optional parameters. (This is usually the size of the data
  1196. * structure used to store the parameters.)
  1197. *
  1198. * @parm . | . . | Represents the variable number of arguments used
  1199. * for the optional parameters.
  1200. *
  1201. * @rdesc Returns a message-specific result.
  1202. ****************************************************************************/
  1203. LRESULT VFWAPIV ICMessage(HIC hic, UINT msg, UINT cb, ...)
  1204. {
  1205. // NOTE no LOADDS!
  1206. #ifndef _WIN32
  1207. return ICSendMessage(hic, msg, (DWORD_PTR)(LPVOID)(&cb+1), cb);
  1208. #else
  1209. va_list va;
  1210. va_start(va, cb);
  1211. va_end(va);
  1212. // nice try, but doesn't work. va is larger than 4 bytes.
  1213. return ICSendMessage(hic, msg, (DWORD_PTR)va, cb);
  1214. #endif
  1215. }
  1216. // on Win32, ICMessage is not supported. All compman.h macros that call
  1217. // it are defined in compman.h as static inline functions
  1218. #endif
  1219. /*****************************************************************************
  1220. * @doc EXTERNAL IC ICAPPS
  1221. *
  1222. * @api HIC | ICOpen | This function opens a compressor or decompressor.
  1223. *
  1224. * @parm DWORD | fccType | Specifies the type of compressor
  1225. * the caller is trying to open. For video, this is ICTYPE_VIDEO.
  1226. *
  1227. * @parm DWORD | fccHandler | Specifies a single preferred handler of the
  1228. * given type that should be tried first. Typically, this comes
  1229. * from the stream header in an AVI file.
  1230. *
  1231. * @parm UINT | wMode | Specifies a flag to defining the use of
  1232. * the compressor or decompressor.
  1233. * This parameter can contain one of the following values:
  1234. *
  1235. * @flag ICMODE_COMPRESS | Advises a compressor it is opened for compression.
  1236. *
  1237. * @flag ICMODE_FASTCOMPRESS | Advise a compressor it is open
  1238. * for fast (real-time) compression.
  1239. *
  1240. * @flag ICMODE_DECOMPRESS | Advises a decompressor it is opened for decompression.
  1241. *
  1242. * @flag ICMODE_FASTDECOMPRESS | Advises a decompressor it is opened
  1243. * for fast (real-time) decompression.
  1244. *
  1245. * @flag ICMODE_DRAW | Advises a decompressor it is opened
  1246. * to decompress an image and draw it directly to hardware.
  1247. *
  1248. * @flag ICMODE_QUERY | Advise a compressor or decompressor it is opened
  1249. * to obtain information.
  1250. *
  1251. * @rdesc Returns a handle to a compressor or decompressor
  1252. * if successful, otherwise it returns zero.
  1253. ****************************************************************************/
  1254. /* Helper functions for compression library */
  1255. HIC VFWAPI ICOpen(DWORD fccType, DWORD fccHandler, UINT wMode)
  1256. {
  1257. ICOPEN icopen;
  1258. ICINFO icinfo;
  1259. PIC pic, picT;
  1260. LRESULT dw;
  1261. ICEnterCrit(&ICOpenCritSec);
  1262. AnsiLowerBuff((LPSTR) &fccType, sizeof(DWORD));
  1263. AnsiLowerBuff((LPSTR) &fccHandler, sizeof(DWORD));
  1264. icopen.dwSize = sizeof(ICOPEN);
  1265. icopen.fccType = fccType;
  1266. icopen.fccHandler = fccHandler;
  1267. icopen.dwFlags = wMode;
  1268. icopen.dwError = 0;
  1269. DPF(("ICOpen('%4.4hs','%4.4hs)'\r\n", (LPSTR)&fccType, (LPSTR)&fccHandler));
  1270. if (!ICInfo(fccType, fccHandler, &icinfo))
  1271. {
  1272. RPF(("Unable to locate Compression module '%4.4hs' '%4.4hs'\r\n", (LPSTR)&fccType, (LPSTR)&fccHandler));
  1273. ICLeaveCrit(&ICOpenCritSec);
  1274. return NULL;
  1275. }
  1276. pic = FindConverter(0L, 0L);
  1277. if (pic == NULL)
  1278. {
  1279. ICLeaveCrit(&ICOpenCritSec);
  1280. return NULL;
  1281. }
  1282. #ifdef NT_THUNK16
  1283. // Try and open on the 32 bit side first.
  1284. // This block and the one below can be interchanged to alter the order
  1285. // in which we try and open the compressor.
  1286. pic->dwSmag = SMAG;
  1287. pic->hTask = (HTASK)GetCurrentTask();
  1288. pic->h32 = ICOpen32(fccType, fccHandler, wMode);
  1289. if (pic->h32 != 0) {
  1290. pic->fccType = fccType;
  1291. pic->fccHandler = fccHandler;
  1292. pic->dwDriver = (DWORD_PTR) -1;
  1293. pic->DriverProc = NULL;
  1294. ICLeaveCrit(&ICOpenCritSec); // A noop for 16 bit code...but...
  1295. return (HIC)pic;
  1296. }
  1297. // Try and open on the 16 bit side
  1298. #endif //NT_THUNK16
  1299. pic->dwSmag = SMAG;
  1300. pic->hTask = GetCurrentTask();
  1301. if (icinfo.szDriver[0])
  1302. {
  1303. #ifdef DEBUG
  1304. DWORD time = timeGetTime();
  1305. //char ach[80];
  1306. #endif
  1307. pic->hDriver = LoadDriver(icinfo.szDriver, &pic->DriverProc);
  1308. #ifdef DEBUG
  1309. time = timeGetTime() - time;
  1310. DPF(("ICOPEN: LoadDriver(%ls) (%ldms) Module Handle==%8x\r\n", (LPSTR)icinfo.szDriver, time, pic->hDriver));
  1311. //wsprintfA(ach, "COMPMAN: LoadDriver(%ls) (%ldms)\r\n", (LPSTR)icinfo.szDriver, time);
  1312. //OutputDebugStringA(ach);
  1313. #endif
  1314. if (pic->hDriver == NULL)
  1315. {
  1316. pic->dwSmag = 0;
  1317. ICLeaveCrit(&ICOpenCritSec);
  1318. return NULL;
  1319. }
  1320. //
  1321. // now try to open the driver as a codec.
  1322. //
  1323. pic->dwDriver = ICSendMessage((HIC)pic, DRV_OPEN, 0, (DWORD_PTR)(LPVOID)&icopen);
  1324. //
  1325. // we want to be able to install 1.0 draw handlers in SYSTEM.INI as:
  1326. //
  1327. // VIDS.SMAG = SMAG.DRV
  1328. //
  1329. // but old driver's may not open iff fccType == 'vids' only if
  1330. // fccType == 'vidc'
  1331. //
  1332. // they also may not like ICMODE_DRAW
  1333. //
  1334. if (pic->dwDriver == 0 &&
  1335. icopen.dwError != 0 &&
  1336. fccType == streamtypeVIDEO)
  1337. {
  1338. if (wMode == ICMODE_DRAW)
  1339. icopen.dwFlags = ICMODE_DECOMPRESS;
  1340. icopen.fccType = ICTYPE_VIDEO;
  1341. pic->dwDriver = ICSendMessage((HIC)pic, DRV_OPEN, 0, (DWORD_PTR)(LPVOID)&icopen);
  1342. }
  1343. if (pic->dwDriver == 0)
  1344. {
  1345. ICClose((HIC)pic);
  1346. ICLeaveCrit(&ICOpenCritSec);
  1347. return NULL;
  1348. }
  1349. // open'ed ok mark these
  1350. pic->fccType = fccType;
  1351. pic->fccHandler = fccHandler;
  1352. }
  1353. else if (picT = FindConverter(fccType, fccHandler))
  1354. {
  1355. picT->dwSmag = SMAG;
  1356. dw = ICSendMessage((HIC)picT, DRV_OPEN, 0, (DWORD_PTR)(LPVOID)&icopen);
  1357. if (dw == 0)
  1358. {
  1359. pic->dwSmag = 0;
  1360. ICLeaveCrit(&ICOpenCritSec);
  1361. return NULL;
  1362. }
  1363. *pic = *picT;
  1364. pic->dwDriver = dw;
  1365. }
  1366. ICLeaveCrit(&ICOpenCritSec);
  1367. return (HIC)pic;
  1368. }
  1369. /*****************************************************************************
  1370. * @doc EXTERNAL IC ICAPPS
  1371. *
  1372. * @api HIC | ICOpenFunction | This function opens
  1373. * a compressor or decompressor defined as a function.
  1374. *
  1375. * @parm DWORD | fccType | Specifies the type of compressor
  1376. * the caller is trying to open. For video, this is ICTYPE_VIDEO.
  1377. *
  1378. * @parm DWORD | fccHandler | Specifies a single preferred handler of the
  1379. * given type that should be tried first. Typically, this comes
  1380. * from the stream header in an AVI file.
  1381. *
  1382. * @parm UINT | wMode | Specifies a flag to defining the use of
  1383. * the compressor or decompressor.
  1384. * This parameter can contain one of the following values:
  1385. *
  1386. * @flag ICMODE_COMPRESS | Advises a compressor it is opened for compression.
  1387. *
  1388. * @flag ICMODE_FASTCOMPRESS | Advises a compressor it is open
  1389. * for fast (real-time) compression.
  1390. *
  1391. * @flag ICMODE_DECOMPRESS | Advises a decompressor it is opened for decompression.
  1392. *
  1393. * @flag ICMODE_FASTDECOMPRESS | Advises a decompressor it is opened
  1394. * for fast (real-time) decompression.
  1395. *
  1396. * @flag ICMODE_DRAW | Advises a decompressor it is opened
  1397. * to decompress an image and draw it directly to hardware.
  1398. *
  1399. * @flag ICMODE_QUERY | Advises a compressor or decompressor it is opened
  1400. * to obtain information.
  1401. *
  1402. * @parm FARPROC | lpfnHandler | Specifies a pointer to the function
  1403. * used as the compressor or decompressor.
  1404. *
  1405. * @rdesc Returns a handle to a compressor or decompressor
  1406. * if successful, otherwise it returns zero.
  1407. ****************************************************************************/
  1408. HIC VFWAPI ICOpenFunction(DWORD fccType, DWORD fccHandler, UINT wMode, FARPROC lpfnHandler)
  1409. {
  1410. ICOPEN icopen;
  1411. PIC pic;
  1412. LRESULT dw;
  1413. if (IsBadCodePtr(lpfnHandler))
  1414. return NULL;
  1415. #ifdef NT_THUNK16
  1416. // lpfnHandler points to 16 bit code that will be used as a compressor.
  1417. // We do not want this to go over to the 32 bit side, so only open on
  1418. // the 16 bit side.
  1419. #endif // NT_THUNK16
  1420. ICEnterCrit(&ICOpenCritSec);
  1421. AnsiLowerBuff((LPSTR) &fccType, sizeof(DWORD));
  1422. AnsiLowerBuff((LPSTR) &fccHandler, sizeof(DWORD));
  1423. icopen.dwSize = sizeof(ICOPEN);
  1424. icopen.fccType = fccType;
  1425. icopen.fccHandler = fccHandler;
  1426. icopen.dwFlags = wMode;
  1427. pic = FindConverter(0L, 0L);
  1428. if (pic == NULL) {
  1429. ICLeaveCrit(&ICOpenCritSec);
  1430. return NULL;
  1431. }
  1432. pic->dwSmag = SMAG;
  1433. pic->fccType = fccType;
  1434. pic->fccHandler = fccHandler;
  1435. pic->dwDriver = 0L;
  1436. pic->hDriver = NULL;
  1437. pic->DriverProc = (DRIVERPROC)lpfnHandler;
  1438. dw = ICSendMessage((HIC)pic, DRV_OPEN, 0, (DWORD_PTR)(LPVOID)&icopen);
  1439. if (dw == 0)
  1440. {
  1441. ICClose((HIC) pic);
  1442. ICLeaveCrit(&ICOpenCritSec);
  1443. return NULL;
  1444. }
  1445. pic->dwDriver = dw;
  1446. ICLeaveCrit(&ICOpenCritSec);
  1447. return (HIC)pic;
  1448. }
  1449. ////////////////////////////////////////////////////////////////////////////
  1450. ////////////////////////////////////////////////////////////////////////////
  1451. /*****************************************************************************
  1452. * @doc EXTERNAL IC ICAPPS
  1453. *
  1454. * @api LRESULT | ICClose | This function closes a compressor or decompressor.
  1455. *
  1456. * @parm HIC | hic | Specifies a handle to a compressor or decompressor.
  1457. *
  1458. * @rdesc Returns ICERR_OK if successful, otherwise it returns an error number.
  1459. *
  1460. ****************************************************************************/
  1461. LRESULT VFWAPI ICClose(HIC hic)
  1462. {
  1463. PIC pic = (PIC)hic;
  1464. V_HIC(hic);
  1465. #ifdef NT_THUNK16
  1466. if (pic->h32 != 0) {
  1467. LRESULT lres = ICClose32(pic->h32);
  1468. pic->h32 = 0; // Next user of this slot does not want h32 set
  1469. return(lres);
  1470. }
  1471. #endif //NT_THUNK16
  1472. #ifdef DEBUG
  1473. {
  1474. char ach[80];
  1475. if (pic->hDriver)
  1476. GetModuleFileNameA(GetDriverModuleHandle (pic->hDriver), ach, sizeof(ach));
  1477. else
  1478. ach[0] = 0;
  1479. DPF(("ICClose(%04X) %4.4hs.%4.4hs %s\r\n", hic, (LPSTR)&pic->fccType, (LPSTR)&pic->fccHandler, (LPSTR)ach));
  1480. }
  1481. #endif
  1482. #ifdef DEBUG
  1483. ICDump();
  1484. #endif
  1485. ICEnterCrit(&ICOpenCritSec);
  1486. if (pic->dwDriver)
  1487. {
  1488. if (pic->DriverProc)
  1489. ICSendMessage((HIC)pic, DRV_CLOSE, 0, 0);
  1490. }
  1491. if (pic->hDriver)
  1492. FreeDriver(pic->hDriver);
  1493. pic->dwSmag = 0L;
  1494. pic->fccType = 0L;
  1495. pic->fccHandler = 0L;
  1496. pic->dwDriver = 0;
  1497. pic->hDriver = NULL;
  1498. pic->DriverProc = NULL;
  1499. ICLeaveCrit(&ICOpenCritSec);
  1500. return ICERR_OK;
  1501. }
  1502. ////////////////////////////////////////////////////////////////////////////
  1503. ////////////////////////////////////////////////////////////////////////////
  1504. /****************************************************************
  1505. * @doc EXTERNAL IC ICAPPS
  1506. *
  1507. * @api DWORD | ICCompress | This function compresses a single video
  1508. * image.
  1509. *
  1510. * @parm HIC | hic | Specifies the handle of the compressor to
  1511. * use.
  1512. *
  1513. * @parm DWORD | dwFlags | Specifies applicable flags for the compression.
  1514. * The following flag is defined:
  1515. *
  1516. * @flag ICCOMPRESS_KEYFRAME | Indicates that the compressor
  1517. * should make this frame a key frame.
  1518. *
  1519. * @parm LPBITMAPINFOHEADER | lpbiOutput | Specifies a far pointer
  1520. * to a <t BITMAPINFO> structure holding the output format.
  1521. *
  1522. * @parm LPVOID | lpData | Specifies a far pointer to output data buffer.
  1523. *
  1524. * @parm LPBITMAPINFOHEADER | lpbiInput | Specifies a far pointer
  1525. * to a <t BITMAPINFO> structure containing the input format.
  1526. *
  1527. * @parm LPVOID | lpBits | Specifies a far pointer to the input data buffer.
  1528. *
  1529. * @parm LPDWORD | lpckid | Not used.
  1530. *
  1531. * @parm LPDWORD | lpdwFlags | Specifies a far pointer to a <t DWORD>
  1532. * holding the return flags used in the AVI index. The following
  1533. * flag is defined:
  1534. *
  1535. * @flag AVIIF_KEYFRAME | Indicates this frame should be used as a key-frame.
  1536. *
  1537. * @parm LONG | lFrameNum | Specifies the frame number.
  1538. *
  1539. * @parm DWORD | dwFrameSize | Specifies the requested frame size in bytes.
  1540. * If set to zero, the compressor chooses the frame size.
  1541. *
  1542. * @parm DWORD | dwQuality | Specifies the requested quality value for the frame.
  1543. *
  1544. * @parm LPBITMAPINFOHEADER | lpbiPrev | Specifies a far pointer to
  1545. * a <t BITMAPINFO> structure holding the previous frame's format.
  1546. * This parameter is not used for fast temporal compression.
  1547. *
  1548. * @parm LPVOID | lpPrev | Specifies a far pointer to the
  1549. * previous frame's data buffer. This parameter is not used for fast
  1550. * temporal compression.
  1551. *
  1552. * @comm The <p lpData> buffer should be large enough to hold a compressed
  1553. * frame. You can obtain the size of this buffer by calling
  1554. * <f ICCompressGetSize>.
  1555. *
  1556. * Set the <p dwFrameSize> parameter to a requested frame
  1557. * size only if the compressor returns the VIDCF_CRUNCH flag in
  1558. * response to <f ICGetInfo>. If this flag is not set, or if a data
  1559. * rate is not specified, set this parameter to zero.
  1560. *
  1561. * Set the <p dwQuality> parameter to a quality value only
  1562. * if the compressor returns the VIDCF_QUALITY flag in response
  1563. * to <f ICGetInfo>. Without this flag, set this parameter to zero.
  1564. *
  1565. * @rdesc This function returns ICERR_OK if successful. Otherwise,
  1566. * it returns an error code.
  1567. *
  1568. * @xref <f ICCompressBegin> <f ICCompressEnd> <f ICCompressGetSize> <f ICGetInfo>
  1569. *
  1570. **********************************************************************/
  1571. DWORD VFWAPIV ICCompress(
  1572. HIC hic,
  1573. DWORD dwFlags, // flags
  1574. LPBITMAPINFOHEADER lpbiOutput, // output format
  1575. LPVOID lpData, // output data
  1576. LPBITMAPINFOHEADER lpbiInput, // format of frame to compress
  1577. LPVOID lpBits, // frame data to compress
  1578. LPDWORD lpckid, // ckid for data in AVI file
  1579. LPDWORD lpdwFlags, // flags in the AVI index.
  1580. LONG lFrameNum, // frame number of seq.
  1581. DWORD dwFrameSize, // reqested size in bytes. (if non zero)
  1582. DWORD dwQuality, // quality
  1583. LPBITMAPINFOHEADER lpbiPrev, // format of previous frame
  1584. LPVOID lpPrev) // previous frame
  1585. {
  1586. #ifdef _WIN32
  1587. // We cannot rely on the stack alignment giving us the right layout
  1588. ICCOMPRESS icc;
  1589. icc.dwFlags = dwFlags;
  1590. icc.lpbiOutput = lpbiOutput;
  1591. icc.lpOutput = lpData;
  1592. icc.lpbiInput = lpbiInput;
  1593. icc.lpInput = lpBits;
  1594. icc.lpckid = lpckid;
  1595. icc.lpdwFlags = lpdwFlags;
  1596. icc.lFrameNum = lFrameNum;
  1597. icc.dwFrameSize = dwFrameSize;
  1598. icc.dwQuality = dwQuality;
  1599. icc.lpbiPrev = lpbiPrev;
  1600. icc.lpPrev = lpPrev;
  1601. return (DWORD) ICSendMessage(hic, ICM_COMPRESS, (DWORD_PTR)(LPVOID)&icc, sizeof(ICCOMPRESS));
  1602. // NOTE: We do NOT copy any results from this temporary structure back
  1603. // to the input variables.
  1604. #else
  1605. return ICSendMessage(hic, ICM_COMPRESS, (DWORD_PTR)(LPVOID)&dwFlags, sizeof(ICCOMPRESS));
  1606. #endif
  1607. }
  1608. /************************************************************************
  1609. decompression functions
  1610. ************************************************************************/
  1611. /*******************************************************************
  1612. * @doc EXTERNAL IC ICAPPS
  1613. *
  1614. * @api DWORD | ICDecompress | The function decompresses a single video frame.
  1615. *
  1616. * @parm HIC | hic | Specifies a handle to the decompressor to use.
  1617. *
  1618. * @parm DWORD | dwFlags | Specifies applicable flags for decompression.
  1619. * The following flags are defined:
  1620. *
  1621. * @flag ICDECOMPRESS_HURRYUP | Indicates the decompressor should try to
  1622. * decompress at a faster rate. When an application uses this flag,
  1623. * it should not draw the decompressed data.
  1624. *
  1625. * @flag ICDECOMPRESS_UPDATE | Indicates that the screen is being updated.
  1626. *
  1627. * @flag ICDECOMPRESS_PREROLL | Indicates that this frame will not actually
  1628. * be drawn, because it is before the point in the movie where play
  1629. * will start.
  1630. *
  1631. * @flag ICDECOMPRESS_NULLFRAME | Indicates that this frame does not actually
  1632. * have any data, and the decompressed image should be left the same.
  1633. *
  1634. * @flag ICDECOMPRESS_NOTKEYFRAME | Indicates that this frame is not a
  1635. * key frame.
  1636. *
  1637. * @parm LPBITMAPINFOHEADER | lpbiFormat | Specifies a far pointer
  1638. * to a <t BITMAPINFO> structure containing the format of
  1639. * the compressed data.
  1640. *
  1641. * @parm LPVOID | lpData | Specifies a far pointer to the input data.
  1642. *
  1643. * @parm LPBITMAPINFOHEADER | lpbi | Specifies a far pointer to a
  1644. * <t BITMAPINFO> structure containing the output format.
  1645. *
  1646. * @parm LPVOID | lpBits | Specifies a far pointer to a data buffer for the
  1647. * decompressed data.
  1648. *
  1649. * @comm The <p lpBits> parameter should point to a buffer large
  1650. * enough to hold the decompressed data. Applications can obtain
  1651. * the size of this buffer with <f ICDecompressGetSize>.
  1652. *
  1653. * @rdesc Returns ICERR_OK on success, otherwise it returns an error code.
  1654. *
  1655. * @xref <f ICDecompressBegin< <f ICDecompressEnd> <f ICDecompressGetSize>
  1656. *
  1657. ********************************************************************/
  1658. DWORD VFWAPIV ICDecompress(
  1659. HIC hic,
  1660. DWORD dwFlags, // flags (from AVI index...)
  1661. LPBITMAPINFOHEADER lpbiFormat, // BITMAPINFO of compressed data
  1662. // biSizeImage has the chunk size
  1663. // biCompression has the ckid (AVI only)
  1664. LPVOID lpData, // data
  1665. LPBITMAPINFOHEADER lpbi, // DIB to decompress to
  1666. LPVOID lpBits)
  1667. {
  1668. #ifdef _WIN32
  1669. ICDECOMPRESS icd;
  1670. // We cannot rely on the stack alignment giving us the right layout
  1671. icd.dwFlags = dwFlags;
  1672. icd.lpbiInput = lpbiFormat;
  1673. icd.lpInput = lpData;
  1674. icd.lpbiOutput = lpbi;
  1675. icd.lpOutput = lpBits;
  1676. icd.ckid = 0;
  1677. return (DWORD) ICSendMessage(hic, ICM_DECOMPRESS, (DWORD_PTR)(LPVOID)&icd, sizeof(ICDECOMPRESS));
  1678. #else
  1679. return ICSendMessage(hic, ICM_DECOMPRESS, (DWORD_PTR)(LPVOID)&dwFlags, sizeof(ICDECOMPRESS));
  1680. #endif
  1681. }
  1682. /************************************************************************
  1683. drawing functions
  1684. ************************************************************************/
  1685. /**********************************************************************
  1686. * @doc EXTERNAL IC ICAPPS
  1687. *
  1688. * @api DWORD | ICDrawBegin | This function starts decompressing
  1689. * data directly to the screen.
  1690. *
  1691. * @parm HIC | hic | Specifies a handle to the decompressor to use.
  1692. *
  1693. * @parm DWORD | dwFlags | Specifies flags for the decompression. The
  1694. * following flags are defined:
  1695. *
  1696. * @flag ICDRAW_QUERY | Determines if the decompressor can handle
  1697. * the decompression. The driver does not actually decompress the data.
  1698. *
  1699. * @flag ICDRAW_FULLSCREEN | Tells the decompressor to draw
  1700. * the decompressed data on the full screen.
  1701. *
  1702. * @flag ICDRAW_HDC | Indicates the decompressor should use the window
  1703. * handle specified by <p hwnd> and the display context
  1704. * handle specified by <p hdc> for drawing the decompressed data.
  1705. *
  1706. * @flag ICDRAW_ANIMATE | Indicates the palette might be animated.
  1707. *
  1708. * @flag ICDRAW_CONTINUE | Indicates drawing is a
  1709. * continuation of the previous frame.
  1710. *
  1711. * @flag ICDRAW_MEMORYDC | Indicates the display context is offscreen.
  1712. *
  1713. * @flag ICDRAW_UPDATING | Indicates the frame is being
  1714. * updated rather than played.
  1715. *
  1716. * @parm HPALETTE | hpal | Specifies a handle to the palette used for drawing.
  1717. *
  1718. * @parm HWND | hwnd | Specifies a handle for the window used for drawing.
  1719. *
  1720. * @parm HDC | hdc | Specifies the display context used for drawing.
  1721. *
  1722. * @parm int | xDst | Specifies the x-position of the upper-right
  1723. * corner of the destination rectangle.
  1724. *
  1725. * @parm int | yDst | Specifies the y-position of the upper-right
  1726. * corner of the destination rectangle.
  1727. *
  1728. * @parm int | dxDst | Specifies the width of the destination rectangle.
  1729. *
  1730. * @parm int | dyDst | Specifies the height of the destination rectangle.
  1731. *
  1732. * @parm LPBITMAPINFOHEADER | lpbi | Specifies a far pointer to
  1733. * a <t BITMAPINFO> structure containing the format of
  1734. * the input data to be decompressed.
  1735. *
  1736. * @parm int | xSrc | Specifies the x-position of the upper-right corner
  1737. * of the source rectangle.
  1738. *
  1739. * @parm int | ySrc | Specifies the y-position of the upper-right corner
  1740. * of the source rectangle.
  1741. *
  1742. * @parm int | dxSrc | Specifies the width of the source rectangle.
  1743. *
  1744. * @parm int | dySrc | Specifies the height of the source rectangle.
  1745. *
  1746. * @parm DWORD | dwRate | Specifies the data rate. The
  1747. * data rate in frames per second equals <p dwRate> divided
  1748. * by <p dwScale>.
  1749. *
  1750. * @parm DWORD | dwScale | Specifies the data rate.
  1751. *
  1752. * @comm Decompressors use the <p hwnd> and <p hdc> parameters
  1753. * only if an application sets ICDRAW_HDC flag in <p dwFlags>.
  1754. * It will ignore these parameters if an application sets
  1755. * the ICDRAW_FULLSCREEN flag. When an application uses the
  1756. * ICDRAW_FULLSCREEN flag, it should set <p hwnd> and <p hdc>
  1757. * to NULL.
  1758. *
  1759. * The destination rectangle is specified only if ICDRAW_HDC is used.
  1760. * If an application sets the ICDRAW_FULLSCREEN flag, the destination
  1761. * rectangle is ignored and its parameters can be set to zero.
  1762. *
  1763. * The source rectangle is relative to the full video frame.
  1764. * The portion of the video frame specified by the source
  1765. * rectangle will be stretched to fit in the destination rectangle.
  1766. *
  1767. * @rdesc Returns ICERR_OK if it can handle the decompression, otherwise
  1768. * it returns ICERR_UNSUPPORTED.
  1769. *
  1770. * @xref <f ICDraw> <f ICDrawEnd>
  1771. *
  1772. *********************************************************************/
  1773. DWORD VFWAPIV ICDrawBegin(
  1774. HIC hic,
  1775. DWORD dwFlags, // flags
  1776. HPALETTE hpal, // palette to draw with
  1777. HWND hwnd, // window to draw to
  1778. HDC hdc, // HDC to draw to
  1779. int xDst, // destination rectangle
  1780. int yDst,
  1781. int dxDst,
  1782. int dyDst,
  1783. LPBITMAPINFOHEADER lpbi, // format of frame to draw
  1784. int xSrc, // source rectangle
  1785. int ySrc,
  1786. int dxSrc,
  1787. int dySrc,
  1788. DWORD dwRate, // frames/second = (dwRate/dwScale)
  1789. DWORD dwScale)
  1790. {
  1791. #ifdef _WIN32
  1792. ICDRAWBEGIN icdraw;
  1793. icdraw.dwFlags = dwFlags;
  1794. icdraw.hpal = hpal;
  1795. icdraw.hwnd = hwnd;
  1796. icdraw.hdc = hdc;
  1797. icdraw.xDst = xDst;
  1798. icdraw.yDst = yDst;
  1799. icdraw.dxDst = dxDst;
  1800. icdraw.dyDst = dyDst;
  1801. icdraw.lpbi = lpbi;
  1802. icdraw.xSrc = xSrc;
  1803. icdraw.ySrc = ySrc;
  1804. icdraw.dxSrc = dxSrc;
  1805. icdraw.dySrc = dySrc;
  1806. icdraw.dwRate = dwRate;
  1807. icdraw.dwScale = dwScale;
  1808. return (DWORD) ICSendMessage(hic, ICM_DRAW_BEGIN, (DWORD_PTR)(LPVOID)&icdraw, sizeof(ICDRAWBEGIN));
  1809. #else
  1810. return ICSendMessage(hic, ICM_DRAW_BEGIN, (DWORD_PTR)(LPVOID)&dwFlags, sizeof(ICDRAWBEGIN));
  1811. #endif
  1812. }
  1813. /**********************************************************************
  1814. * @doc EXTERNAL IC ICAPPS
  1815. *
  1816. * @api DWORD | ICDraw | This function decompress an image for drawing.
  1817. *
  1818. * @parm HIC | hic | Specifies a handle to an decompressor.
  1819. *
  1820. * @parm DWORD | dwFlags | Specifies any flags for the decompression.
  1821. * The following flags are defined:
  1822. *
  1823. * @flag ICDRAW_HURRYUP | Indicates the decompressor should
  1824. * just buffer the data if it needs it for decompression
  1825. * and not draw it to the screen.
  1826. *
  1827. * @flag ICDRAW_UPDATE | Tells the decompressor to update the screen based
  1828. * on data previously received. Set <p lpData> to NULL when
  1829. * this flag is used.
  1830. *
  1831. * @flag ICDRAW_PREROLL | Indicates that this frame of video occurs before
  1832. * actual playback should start. For example, if playback is to
  1833. * begin on frame 10, and frame 0 is the nearest previous keyframe,
  1834. * frames 0 through 9 are sent to the driver with the ICDRAW_PREROLL
  1835. * flag set. The driver needs this data so it can displya frmae 10
  1836. * properly, but frames 0 through 9 need not be individually displayed.
  1837. *
  1838. * @flag ICDRAW_NULLFRAME | Indicates that this frame does not actually
  1839. * have any data, and the previous frame should be redrawn.
  1840. *
  1841. * @flag ICDRAW_NOTKEYFRAME | Indicates that this frame is not a
  1842. * key frame.
  1843. *
  1844. * @parm LPVOID | lpFormat | Specifies a far pointer to a
  1845. * <t BITMAPINFOHEADER> structure containing the input
  1846. * format of the data.
  1847. *
  1848. * @parm LPVOID | lpData | Specifies a far pointer to the actual input data.
  1849. *
  1850. * @parm DWORD | cbData | Specifies the size of the input data (in bytes).
  1851. *
  1852. * @parm LONG | lTime | Specifies the time to draw this frame based on the
  1853. * time scale sent with <f ICDrawBegin>.
  1854. *
  1855. * @comm This function is used to decompress the image data for drawing
  1856. * by the decompressor. Actual drawing of frames does not occur
  1857. * until <f ICDrawStart> is called. The application should be sure to
  1858. * pre-buffer the required number of frames before drawing is started
  1859. * (you can obtain this value with <f ICGetBuffersWanted>).
  1860. *
  1861. * @rdesc Returns ICERR_OK on success, otherwise it returns an appropriate error
  1862. * number.
  1863. *
  1864. * @xref <f ICDrawBegin> <f ICDrawEnd> <f ICDrawStart> <f ICDrawStop> <f ICGetBuffersRequired>
  1865. *
  1866. **********************************************************************/
  1867. DWORD VFWAPIV ICDraw(
  1868. HIC hic,
  1869. DWORD dwFlags, // flags
  1870. LPVOID lpFormat, // format of frame to decompress
  1871. LPVOID lpData, // frame data to decompress
  1872. DWORD cbData, // size in bytes of data
  1873. LONG lTime) // time to draw this frame (see drawbegin dwRate and dwScale)
  1874. {
  1875. #ifdef _WIN32
  1876. ICDRAW icdraw;
  1877. icdraw.dwFlags = dwFlags;
  1878. icdraw.lpFormat = lpFormat;
  1879. icdraw.lpData = lpData;
  1880. icdraw.cbData = cbData;
  1881. icdraw.lTime = lTime;
  1882. return (DWORD) ICSendMessage(hic, ICM_DRAW, (DWORD_PTR)(LPVOID)&icdraw, sizeof(ICDRAW));
  1883. #else
  1884. return ICSendMessage(hic, ICM_DRAW, (DWORD_PTR)(LPVOID)&dwFlags, sizeof(ICDRAW));
  1885. #endif
  1886. }
  1887. /*****************************************************************************
  1888. * @doc EXTERNAL IC ICAPPS
  1889. *
  1890. * @api HIC | ICGetDisplayFormat | This function returns the "best"
  1891. * format available for displaying a compressed image. The function
  1892. * will also open a compressor if a handle to an open compressor
  1893. * is not specified.
  1894. *
  1895. * @parm HIC | hic | Specifies the decompressor that should be used. If
  1896. * this is NULL, an appropriate compressor will be opened and returned.
  1897. *
  1898. * @parm LPBITMAPINFOHEADER | lpbiIn | Specifies a pointer to
  1899. * <t BITMAPINFOHEADER> structure containing the compressed format.
  1900. *
  1901. * @parm LPBITMAPINFOHEADER | lpbiOut | Specifies a pointer
  1902. * to a buffer used to return the decompressed format.
  1903. * The buffer should be large enough for a <t BITMAPINFOHEADER>
  1904. * structure and 256 color entries.
  1905. *
  1906. * @parm int | BitDepth | If non-zero, specifies the preferred bit depth.
  1907. *
  1908. * @parm int | dx | If non-zero, specifies the width to which the image
  1909. * is to be stretched.
  1910. *
  1911. * @parm int | dy | If non-zero, specifies the height to which the image
  1912. * is to be stretched.
  1913. *
  1914. * @rdesc Returns a handle to a decompressor if successful, otherwise, it
  1915. * returns zero.
  1916. ****************************************************************************/
  1917. HIC VFWAPI ICGetDisplayFormat(HIC hic, LPBITMAPINFOHEADER lpbiIn, LPBITMAPINFOHEADER lpbiOut, int BitDepth, int dx, int dy)
  1918. {
  1919. LRESULT dw;
  1920. HDC hdc;
  1921. BOOL fNukeHic = (hic == NULL);
  1922. static int ScreenBitDepth = -1;
  1923. // HACK: We link to some internal DrawDib stuff to find out whether
  1924. // the current display driver is using 565 RGB dibs....
  1925. extern UINT FAR GetBitmapType(VOID);
  1926. #define BM_16565 0x06 // most HiDAC cards
  1927. #define HACK_565_DEPTH 17
  1928. if (hic == NULL)
  1929. hic = ICDecompressOpen(ICTYPE_VIDEO, 0L, lpbiIn, NULL);
  1930. if (hic == NULL)
  1931. return NULL;
  1932. //
  1933. // dx = 0 and dy = 0 means don't stretch.
  1934. //
  1935. if (dx == (int)lpbiIn->biWidth && dy == (int)lpbiIn->biHeight)
  1936. dx = dy = 0;
  1937. //
  1938. // ask the compressor if it likes the format.
  1939. //
  1940. dw = ICDecompressQuery(hic, lpbiIn, NULL);
  1941. if (dw != ICERR_OK)
  1942. {
  1943. DPF(("Decompressor did not recognize the input data format\r\n"));
  1944. goto error;
  1945. }
  1946. try_again:
  1947. //
  1948. // ask the compressor first. (so it can set the palette)
  1949. // this is a HACK, we will send the ICM_GET_PALETTE message later.
  1950. //
  1951. dw = ICDecompressGetFormat(hic, lpbiIn, lpbiOut);
  1952. //
  1953. // init the output format
  1954. //
  1955. *lpbiOut = *lpbiIn;
  1956. lpbiOut->biSize = sizeof(BITMAPINFOHEADER);
  1957. lpbiOut->biCompression = BI_RGB;
  1958. //
  1959. // default to the screen depth.
  1960. //
  1961. if (BitDepth == 0)
  1962. {
  1963. if (ScreenBitDepth < 0)
  1964. {
  1965. hdc = GetDC(NULL);
  1966. ScreenBitDepth = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
  1967. ReleaseDC(NULL, hdc);
  1968. if (ScreenBitDepth == 15)
  1969. ScreenBitDepth = 16;
  1970. if (ScreenBitDepth < 8)
  1971. ScreenBitDepth = 8;
  1972. //
  1973. // only try 16 bpp if the display supports drawing it.
  1974. //
  1975. if (ScreenBitDepth == 16)
  1976. {
  1977. lpbiOut->biBitCount = 16;
  1978. if (!DrawDibProfileDisplay(lpbiOut))
  1979. ScreenBitDepth = 24;
  1980. }
  1981. if (ScreenBitDepth > 24)
  1982. {
  1983. lpbiOut->biBitCount = 32;
  1984. if (!DrawDibProfileDisplay(lpbiOut))
  1985. ScreenBitDepth = 24;
  1986. }
  1987. if (ScreenBitDepth == 16 && GetBitmapType() == BM_16565) {
  1988. // If the display is really 565, take this into account.
  1989. ScreenBitDepth = HACK_565_DEPTH;
  1990. }
  1991. }
  1992. #ifdef DEBUG
  1993. ScreenBitDepth = mmGetProfileIntA("DrawDib",
  1994. "ScreenBitDepth",
  1995. ScreenBitDepth);
  1996. #endif
  1997. BitDepth = ScreenBitDepth;
  1998. }
  1999. //
  2000. // always try 8bit first for '8' bit data
  2001. //
  2002. if (lpbiIn->biBitCount == 8)
  2003. BitDepth = 8;
  2004. //
  2005. // lets suggest a format to the device.
  2006. //
  2007. try_bit_depth:
  2008. if (BitDepth != HACK_565_DEPTH) {
  2009. lpbiOut->biSize = sizeof(BITMAPINFOHEADER);
  2010. lpbiOut->biCompression = BI_RGB;
  2011. lpbiOut->biBitCount = (WORD) BitDepth;
  2012. } else {
  2013. #ifndef BI_BITFIELDS
  2014. #define BI_BITFIELDS 3L
  2015. #endif
  2016. // For RGB565, we need to use BI_BITFIELDS.
  2017. lpbiOut->biSize = sizeof(BITMAPINFOHEADER);
  2018. lpbiOut->biCompression = BI_BITFIELDS;
  2019. lpbiOut->biBitCount = 16;
  2020. ((LPDWORD)(lpbiOut+1))[0] = 0x00F800;
  2021. ((LPDWORD)(lpbiOut+1))[1] = 0x0007E0;
  2022. ((LPDWORD)(lpbiOut+1))[2] = 0x00001F;
  2023. // Set lpbiOut->biClrUsed = 3?
  2024. }
  2025. //
  2026. // should we suggest a stretched decompress
  2027. //
  2028. if (dx > 0 && dy > 0)
  2029. {
  2030. lpbiOut->biWidth = dx;
  2031. lpbiOut->biHeight = dy;
  2032. }
  2033. lpbiOut->biSizeImage = (DWORD)(UINT)DIBWIDTHBYTES(*lpbiOut) *
  2034. (DWORD)(UINT)lpbiOut->biHeight;
  2035. //
  2036. // ask the compressor if it likes the suggested format.
  2037. //
  2038. dw = ICDecompressQuery(hic, lpbiIn, lpbiOut);
  2039. //
  2040. // if it likes it then return success.
  2041. //
  2042. if (dw == ICERR_OK)
  2043. goto success;
  2044. // 8: 8, 16,24,32,X
  2045. // 16: 16,565,24,32,X
  2046. // 565: 565,16,24,32,X
  2047. // 24: 24,32,16,X
  2048. // 32: 32,24,16,X
  2049. //
  2050. // try another bit depth in this order 8,16,RGB565,24,32
  2051. //
  2052. if (BitDepth <= 8)
  2053. {
  2054. BitDepth = 16;
  2055. goto try_bit_depth;
  2056. }
  2057. if (ScreenBitDepth == HACK_565_DEPTH) {
  2058. // If the screen is RGB565, we try 565 before 555.
  2059. if (BitDepth == 16) {
  2060. BitDepth = 24;
  2061. goto try_bit_depth;
  2062. }
  2063. if (BitDepth == HACK_565_DEPTH) {
  2064. BitDepth = 16;
  2065. goto try_bit_depth;
  2066. }
  2067. }
  2068. if (BitDepth == 16) {
  2069. // otherwise, we try 565 after 555.
  2070. BitDepth = HACK_565_DEPTH;
  2071. goto try_bit_depth;
  2072. }
  2073. if (BitDepth == HACK_565_DEPTH) {
  2074. BitDepth = 24;
  2075. goto try_bit_depth;
  2076. }
  2077. if (BitDepth == 24)
  2078. {
  2079. BitDepth = 32;
  2080. goto try_bit_depth;
  2081. }
  2082. if (BitDepth != 32)
  2083. {
  2084. BitDepth = 32;
  2085. goto try_bit_depth;
  2086. }
  2087. if (dx > 0 && dy > 0)
  2088. {
  2089. #ifndef DAYTONA // it is not clear that this is correct for Daytona
  2090. // while we work it out disable the code, but match blues
  2091. // as closely as possible.
  2092. //
  2093. // If it's already stretched "pretty big", try decompressing
  2094. // stretched by two, and then stretching/shrinking from there.
  2095. // Otherwise, give up and try decompressing normally.
  2096. //
  2097. if ((dx > (lpbiIn->biWidth * 3) / 2) &&
  2098. (dy > (lpbiIn->biHeight * 3) / 2) &&
  2099. ((dx != lpbiIn->biWidth * 2) || (dy != lpbiIn->biHeight * 2))) {
  2100. dx = (int) lpbiIn->biWidth * 2;
  2101. dy = (int) lpbiIn->biHeight * 2;
  2102. } else {
  2103. dx = 0;
  2104. dy = 0;
  2105. }
  2106. //
  2107. // try to find a non stretched format. but don't let the
  2108. // device dither if we are going to stretch!
  2109. // - note that this only applies for palettised displays.
  2110. // for 16-bit displays we need to restart to ensure we get the
  2111. // right format (555, 565). On 4-bit displays we can also restart
  2112. // (ask DavidMay about the 4-bit cases).
  2113. //
  2114. BitDepth = 0;
  2115. #else
  2116. dx = 0;
  2117. dy = 0;
  2118. if ((lpbiIn->biBitCount > 8) && (ScreenBitDepth == 8))
  2119. BitDepth = 16;
  2120. else
  2121. BitDepth = 0;
  2122. #endif
  2123. goto try_again;
  2124. }
  2125. else
  2126. {
  2127. //
  2128. // let the compressor suggest a format
  2129. //
  2130. dw = ICDecompressGetFormat(hic, lpbiIn, lpbiOut);
  2131. if (dw == ICERR_OK)
  2132. goto success;
  2133. }
  2134. error:
  2135. if (hic && fNukeHic)
  2136. ICClose(hic);
  2137. return NULL;
  2138. success:
  2139. if (lpbiOut->biBitCount == 8)
  2140. ICDecompressGetPalette(hic, lpbiIn, lpbiOut);
  2141. return hic;
  2142. }
  2143. /*****************************************************************************
  2144. * @doc EXTERNAL IC ICAPPS
  2145. *
  2146. * @api HIC | ICLocate | This function finds a compressor or decompressor
  2147. * that can handle images with the formats specified, or it finds a
  2148. * driver that can decompress an image with a specified
  2149. * format directly to hardware. Applications must close the
  2150. * compressor when it has finished using the compressor.
  2151. *
  2152. * @parm DWORD | fccType | Specifies the type of compressor
  2153. * the caller is trying to open. For video, this is ICTYPE_VIDEO.
  2154. *
  2155. * @parm DWORD | fccHandler | Specifies a single preferred handler of the
  2156. * given type that should be tried first. Typically, this comes
  2157. * from the stream header in an AVI file.
  2158. *
  2159. * @parm LPBITMAPINFOHEADER | lpbiIn | Specifies a pointer to
  2160. * <t BITMAPINFOHEADER> structure defining the input format.
  2161. * A compressor handle will not be returned unless it
  2162. * can handle this format.
  2163. *
  2164. * @parm LPBITMAPINFOHEADER | lpbiOut | Specifies zero or a pointer to
  2165. * <t BITMAPINFOHEADER> structure defining an optional decompressed
  2166. * format. If <p lpbiOut> is nonzero, a compressor handle will not
  2167. * be returned unless it can create this output format.
  2168. *
  2169. * @parm WORD | wFlags | Specifies a flag to defining the use of the compressor.
  2170. * This parameter must contain one of the following values:
  2171. *
  2172. * @flag ICMODE_COMPRESS | Indicates the compressor should
  2173. * be able to compress an image with a format defined by <p lpbiIn>
  2174. * to the format defined by <p lpbiOut>.
  2175. *
  2176. * @flag ICMODE_DECOMPRESS | Indicates the decompressor should
  2177. * be able to decompress an image with a format defined by <p lpbiIn>
  2178. * to the format defined by <p lpbiOut>.
  2179. *
  2180. * @flag ICMODE_FASTDECOMPRESS | Has the same definition as ICMODE_DECOMPRESS except the
  2181. * decompressor is being used for a real-time operation and should trade off speed
  2182. * for quality if possible.
  2183. *
  2184. * @flag ICMODE_FASTCOMPRESS | Has the same definition as ICMODE_COMPRESS except the
  2185. * compressor is being used for a real-time operation and should trade off speed
  2186. * for quality if possible.
  2187. *
  2188. * @flag ICMODE_DRAW | Indicates the decompressor should
  2189. * be able to decompress an image with a format defined by <p lpbiIn>
  2190. * and draw it directly to hardware.
  2191. *
  2192. * @rdesc Returns a handle to a compressor or decompressor
  2193. * if successful, otherwise it returns zero.
  2194. ****************************************************************************/
  2195. HIC VFWAPI ICLocate(DWORD fccType, DWORD fccHandler, LPBITMAPINFOHEADER lpbiIn, LPBITMAPINFOHEADER lpbiOut, WORD wFlags)
  2196. {
  2197. HIC hic=NULL;
  2198. int i;
  2199. ICINFO icinfo;
  2200. UINT msg;
  2201. if (fccType == 0)
  2202. return NULL;
  2203. switch (wFlags)
  2204. {
  2205. case ICMODE_FASTCOMPRESS:
  2206. case ICMODE_COMPRESS:
  2207. msg = ICM_COMPRESS_QUERY;
  2208. break;
  2209. case ICMODE_FASTDECOMPRESS:
  2210. case ICMODE_DECOMPRESS:
  2211. msg = ICM_DECOMPRESS_QUERY;
  2212. break;
  2213. case ICMODE_DRAW:
  2214. msg = ICM_DRAW_QUERY;
  2215. break;
  2216. default:
  2217. return NULL;
  2218. }
  2219. if (fccHandler)
  2220. {
  2221. hic = ICOpen(fccType, fccHandler, wFlags);
  2222. if (hic && ICSendMessage(hic, msg, (DWORD_PTR)lpbiIn, (DWORD_PTR)lpbiOut) == ICERR_OK)
  2223. return hic;
  2224. else if (hic)
  2225. ICClose(hic);
  2226. }
  2227. if (fccType == ICTYPE_VIDEO && lpbiIn)
  2228. {
  2229. DWORD fccHandler = lpbiIn->biCompression;
  2230. // They're decompressed already.. use our RLE handler so we don't
  2231. // waste time looking for a decompressor or fail and think we don't
  2232. // support these formats!
  2233. if (fccHandler == BI_RLE8 || fccHandler == BI_RGB)
  2234. fccHandler = mmioFOURCC('M', 'R', 'L', 'E');
  2235. if (fccHandler > 256)
  2236. {
  2237. if (fccHandler == mmioFOURCC('C', 'R', 'A', 'M'))
  2238. fccHandler = mmioFOURCC('M', 'S', 'V', 'C');
  2239. hic = ICOpen(fccType, fccHandler, wFlags);
  2240. if (hic && ICSendMessage(hic, msg, (DWORD_PTR)lpbiIn, (DWORD_PTR)lpbiOut) == ICERR_OK)
  2241. return hic;
  2242. else if (hic)
  2243. ICClose(hic);
  2244. }
  2245. }
  2246. //
  2247. // Search through all of the compressors, to see if one can do what we
  2248. // want.
  2249. //
  2250. for (i=0; ICInfo(fccType, i, &icinfo); i++)
  2251. {
  2252. // Protect against arbitrary 3rd party code crashing us
  2253. try {
  2254. hic = ICOpen(fccType, icinfo.fccHandler, wFlags);
  2255. if (hic == NULL)
  2256. continue;
  2257. if (ICSendMessage(hic, msg, (DWORD_PTR)lpbiIn, (DWORD_PTR)lpbiOut) != ICERR_OK)
  2258. {
  2259. ICClose(hic);
  2260. continue;
  2261. }
  2262. } except (EXCEPTION_EXECUTE_HANDLER) {
  2263. if (hic) {
  2264. ICClose(hic);
  2265. hic = NULL;
  2266. }
  2267. }
  2268. if (hic) {
  2269. return hic;
  2270. }
  2271. return hic;
  2272. }
  2273. return NULL;
  2274. }
  2275. /*****************************************************************************
  2276. * @doc INTERNAL IC
  2277. *
  2278. * @api HDRVR | LoadDriver | load a driver
  2279. *
  2280. * Note: on chicago, the string szDriver may not be longer than
  2281. * the number of characters in ICINFO.szDriver
  2282. *
  2283. ****************************************************************************/
  2284. #if defined _WIN32
  2285. STATICFN HDRVR LoadDriver(LPWSTR szDriver, DRIVERPROC FAR *lpDriverProc)
  2286. #else
  2287. STATICFN HDRVR LoadDriver(LPSTR szDriver, DRIVERPROC FAR *lpDriverProc)
  2288. #endif
  2289. {
  2290. HMODULE hModule;
  2291. UINT u;
  2292. DRIVERPROC DriverProc;
  2293. BOOL fWow;
  2294. HDRVR hDriver;
  2295. fWow = IsWow();
  2296. if (fWow)
  2297. {
  2298. u = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  2299. #if defined _WIN32 && ! defined UNICODE
  2300. {
  2301. char ach[NUMELMS(((ICINFO *)0)->szDriver)]; // same size as PICINFO.szDriver
  2302. hModule = LoadLibrary (mmWideToAnsi(ach, szDriver, NUMELMS(ach)));
  2303. }
  2304. #else
  2305. hModule = LoadLibrary(szDriver);
  2306. #endif
  2307. SetErrorMode(u);
  2308. if (hModule <= (HMODULE)HINSTANCE_ERROR)
  2309. return NULL;
  2310. hDriver = (HDRVR) hModule;
  2311. }
  2312. else
  2313. {
  2314. hDriver = OpenDriver (szDriver, NULL, 0);
  2315. if (!hDriver)
  2316. return NULL;
  2317. hModule = GetDriverModuleHandle (hDriver);
  2318. }
  2319. DPF(("LoadDriver: %ls, handle %8x hModule %8x\n", szDriver, hDriver, hModule));
  2320. DriverProc = (DRIVERPROC)GetProcAddress(hModule, szDriverProc);
  2321. if (DriverProc == NULL)
  2322. {
  2323. if (fWow)
  2324. {
  2325. FreeLibrary(hModule);
  2326. }
  2327. else
  2328. {
  2329. CloseDriver (hDriver, 0L, 0L);
  2330. }
  2331. DPF(("Freeing library %8x as no driverproc found\r\n",hModule));
  2332. return NULL;
  2333. }
  2334. #if ! defined _WIN32
  2335. if (fWow && GetModuleUsage(hModule) == 1) //!!!this is not exacly like USER
  2336. {
  2337. if (!DriverProc(0, (HDRVR)1, DRV_LOAD, 0L, 0L))
  2338. {
  2339. DPF(("Freeing library %8x as driverproc returned an error\r\n",hModule));
  2340. FreeLibrary(hModule);
  2341. return NULL;
  2342. }
  2343. DriverProc(0, (HDRVR)1, DRV_ENABLE, 0L, 0L);
  2344. }
  2345. CacheModule (hModule);
  2346. #endif
  2347. *lpDriverProc = DriverProc;
  2348. return hDriver;
  2349. }
  2350. /*****************************************************************************
  2351. * @doc INTERNAL IC
  2352. *
  2353. * @api void | FreeDriver | unload a driver
  2354. *
  2355. ****************************************************************************/
  2356. STATICFN void FreeDriver(HDRVR hDriver)
  2357. {
  2358. if (!IsWow())
  2359. {
  2360. DPF(("FreeDriver, driver handle is %x\n", hDriver));
  2361. CloseDriver (hDriver, 0L, 0L);
  2362. }
  2363. #ifndef _WIN32
  2364. else
  2365. {
  2366. // This cannot be WIN32 code due to the definition of IsWow()
  2367. if (GetModuleUsage((HMODULE) hDriver) == 1)
  2368. {
  2369. DRIVERPROC DriverProc;
  2370. DriverProc = (DRIVERPROC)GetProcAddress((HMODULE) hDriver, szDriverProc);
  2371. if (DriverProc)
  2372. {
  2373. DriverProc(0, (HDRVR)1, DRV_DISABLE, 0L, 0L);
  2374. DriverProc(0, (HDRVR)1, DRV_FREE, 0L, 0L);
  2375. }
  2376. }
  2377. FreeLibrary((HMODULE) hDriver);
  2378. DPF(("Freeing library %8x in FreeDriver\r\n",hDriver));
  2379. }
  2380. #endif
  2381. }
  2382. #ifdef DEBUG_RETAIL
  2383. /************************************************************************
  2384. messages.
  2385. ************************************************************************/
  2386. static const struct {
  2387. UINT msg;
  2388. char *szMsg;
  2389. } aMsg[] = {
  2390. DRV_OPEN , "DRV_OPEN",
  2391. DRV_CLOSE , "DRV_CLOSE",
  2392. ICM_GETSTATE , "ICM_GETSTATE",
  2393. ICM_SETSTATE , "ICM_SETSTATE",
  2394. ICM_GETINFO , "ICM_GETINFO",
  2395. ICM_CONFIGURE , "ICM_CONFIGURE",
  2396. ICM_ABOUT , "ICM_ABOUT",
  2397. ICM_GETERRORTEXT , "ICM_GETERRORTEXT",
  2398. ICM_GETFORMATNAME , "ICM_GETFORMATNAME",
  2399. ICM_ENUMFORMATS , "ICM_ENUMFORMATS",
  2400. ICM_GETDEFAULTQUALITY , "ICM_GETDEFAULTQUALITY",
  2401. ICM_GETQUALITY , "ICM_GETQUALITY",
  2402. ICM_SETQUALITY , "ICM_SETQUALITY",
  2403. ICM_COMPRESS_GET_FORMAT , "ICM_COMPRESS_GET_FORMAT",
  2404. ICM_COMPRESS_GET_SIZE , "ICM_COMPRESS_GET_SIZE",
  2405. ICM_COMPRESS_QUERY , "ICM_COMPRESS_QUERY",
  2406. ICM_COMPRESS_BEGIN , "ICM_COMPRESS_BEGIN",
  2407. ICM_COMPRESS , "ICM_COMPRESS",
  2408. ICM_COMPRESS_END , "ICM_COMPRESS_END",
  2409. ICM_DECOMPRESS_GET_FORMAT , "ICM_DECOMPRESS_GET_FORMAT",
  2410. ICM_DECOMPRESS_QUERY , "ICM_DECOMPRESS_QUERY",
  2411. ICM_DECOMPRESS_BEGIN , "ICM_DECOMPRESS_BEGIN",
  2412. ICM_DECOMPRESS , "ICM_DECOMPRESS",
  2413. ICM_DECOMPRESS_END , "ICM_DECOMPRESS_END",
  2414. ICM_DECOMPRESS_GET_PALETTE , "ICM_DECOMPRESS_GET_PALETTE",
  2415. ICM_DECOMPRESS_SET_PALETTE , "ICM_DECOMPRESS_SET_PALETTE",
  2416. ICM_DECOMPRESSEX_QUERY , "ICM_DECOMPRESSEX_QUERY",
  2417. ICM_DECOMPRESSEX_BEGIN , "ICM_DECOMPRESSEX_BEGIN",
  2418. ICM_DECOMPRESSEX , "ICM_DECOMPRESSEX",
  2419. ICM_DECOMPRESSEX_END , "ICM_DECOMPRESSEX_END",
  2420. ICM_DRAW_QUERY , "ICM_DRAW_QUERY",
  2421. ICM_DRAW_BEGIN , "ICM_DRAW_BEGIN",
  2422. ICM_DRAW_GET_PALETTE , "ICM_DRAW_GET_PALETTE",
  2423. ICM_DRAW_UPDATE , "ICM_DRAW_UPDATE",
  2424. ICM_DRAW_START , "ICM_DRAW_START",
  2425. ICM_DRAW_STOP , "ICM_DRAW_STOP",
  2426. ICM_DRAW_BITS , "ICM_DRAW_BITS",
  2427. ICM_DRAW_END , "ICM_DRAW_END",
  2428. ICM_DRAW_GETTIME , "ICM_DRAW_GETTIME",
  2429. ICM_DRAW , "ICM_DRAW",
  2430. ICM_DRAW_WINDOW , "ICM_DRAW_WINDOW",
  2431. ICM_DRAW_SETTIME , "ICM_DRAW_SETTIME",
  2432. ICM_DRAW_REALIZE , "ICM_DRAW_REALIZE",
  2433. ICM_GETBUFFERSWANTED , "ICM_GETBUFFERSWANTED",
  2434. ICM_GETDEFAULTKEYFRAMERATE , "ICM_GETDEFAULTKEYFRAMERATE",
  2435. 0 , NULL
  2436. };
  2437. static const struct {
  2438. LRESULT err;
  2439. char *szErr;
  2440. } aErr[] = {
  2441. ICERR_DONTDRAW , "ICERR_DONTDRAW",
  2442. ICERR_NEWPALETTE , "ICERR_NEWPALETTE",
  2443. ICERR_UNSUPPORTED , "ICERR_UNSUPPORTED",
  2444. ICERR_BADFORMAT , "ICERR_BADFORMAT",
  2445. ICERR_MEMORY , "ICERR_MEMORY",
  2446. ICERR_INTERNAL , "ICERR_INTERNAL",
  2447. ICERR_BADFLAGS , "ICERR_BADFLAGS",
  2448. ICERR_BADPARAM , "ICERR_BADPARAM",
  2449. ICERR_BADSIZE , "ICERR_BADSIZE",
  2450. ICERR_BADHANDLE , "ICERR_BADHANDLE",
  2451. ICERR_CANTUPDATE , "ICERR_CANTUPDATE",
  2452. ICERR_ERROR , "ICERR_ERROR",
  2453. ICERR_BADBITDEPTH , "ICERR_BADBITDEPTH",
  2454. ICERR_BADIMAGESIZE , "ICERR_BADIMAGESIZE",
  2455. ICERR_OK , "ICERR_OK"
  2456. };
  2457. STATICDT BOOL cmfDebug = -1;
  2458. STATICDT DWORD dwTime;
  2459. void ICDebugMessage(HIC hic, UINT msg, DWORD_PTR dw1, DWORD_PTR dw2)
  2460. {
  2461. int i;
  2462. if (!cmfDebug)
  2463. return;
  2464. for (i=0; aMsg[i].msg && aMsg[i].msg != msg; i++)
  2465. ;
  2466. if (aMsg[i].msg == 0)
  2467. RPF(("ICM(%04X,ICM_%04X,%08lX,%08lX) ", hic, msg, dw1, dw2));
  2468. else
  2469. RPF(("ICM(%04X,%s,%08lX,%08lX) ", hic, (LPSTR)aMsg[i].szMsg, dw1, dw2));
  2470. dwTime = timeGetTime();
  2471. }
  2472. LRESULT ICDebugReturn(LRESULT err)
  2473. {
  2474. int i;
  2475. if (!cmfDebug)
  2476. return err;
  2477. dwTime = timeGetTime() - dwTime;
  2478. for (i=0; aErr[i].err && aErr[i].err != err; i++)
  2479. ;
  2480. if (aErr[i].err != err)
  2481. RPF(("! : 0x%08lX (%ldms)\r\n", err, dwTime));
  2482. else
  2483. RPF(("! : %s (%ldms)\r\n", (LPSTR)aErr[i].szErr, dwTime));
  2484. return err;
  2485. }
  2486. STATICFN void ICDump()
  2487. {
  2488. int i;
  2489. PIC pic;
  2490. TCHAR ach[80];
  2491. DPF(("ICDump ---------------------------------------\r\n"));
  2492. for (i=0; i<giMaxConverters; i++)
  2493. {
  2494. pic = &aicConverters[i];
  2495. if (pic->fccType == 0)
  2496. continue;
  2497. if (pic->dwSmag == 0)
  2498. continue;
  2499. if (pic->hDriver)
  2500. GetModuleFileName(GetDriverModuleHandle (pic->hDriver), ach, NUMELMS(ach));
  2501. else
  2502. ach[0] = 0;
  2503. #ifdef _WIN32
  2504. DPF((" HIC: %04X %4.4hs.%4.4hs hTask=%04X Proc=%08lx %ls\r\n", (HIC)pic, (LPSTR)&pic->fccType, (LPSTR)&pic->fccHandler, pic->hTask, pic->DriverProc, ach));
  2505. #else
  2506. DPF((" HIC: %04X %4.4s.%4.4s hTask=%04X Proc=%08lx %s\r\n", (HIC)pic, (LPSTR)&pic->fccType, (LPSTR)&pic->fccHandler, pic->hTask, pic->DriverProc, (LPSTR)ach));
  2507. #endif
  2508. }
  2509. DPF(("----------------------------------------------\r\n"));
  2510. }
  2511. #endif
  2512. /*****************************************************************************
  2513. *
  2514. * dprintf() is called by the DPF macro if DEBUG is defined at compile time.
  2515. *
  2516. * The messages will be send to COM1: like any debug message. To
  2517. * enable debug output, add the following to WIN.INI :
  2518. *
  2519. * [debug]
  2520. * COMPMAN=1
  2521. *
  2522. ****************************************************************************/
  2523. char szDebug[] = "Debug";
  2524. #ifdef DEBUG_RETAIL
  2525. STATICFN void cdecl dprintfc(LPSTR szFormat, ...)
  2526. {
  2527. char ach[128];
  2528. #ifdef _WIN32
  2529. va_list va;
  2530. if (cmfDebug == -1)
  2531. cmfDebug = mmGetProfileIntA(szDebug, MODNAME, 0);
  2532. if (!cmfDebug)
  2533. return;
  2534. va_start(va, szFormat);
  2535. if (szFormat[0] == '!')
  2536. ach[0]=0, szFormat++;
  2537. else
  2538. wsprintfA(ach, "%s: (tid %x) ", MODNAME, GetCurrentThreadId());
  2539. wvsprintfA(ach+lstrlenA(ach),szFormat,va);
  2540. va_end(va);
  2541. // lstrcatA(ach, "\r\r\n");
  2542. #else // Following is WIN16 code...
  2543. if (cmfDebug == -1)
  2544. cmfDebug = GetProfileIntA("Debug",MODNAME, 0);
  2545. if (!cmfDebug)
  2546. return;
  2547. if (szFormat[0] == '!')
  2548. ach[0]=0, szFormat++;
  2549. else
  2550. lstrcpyA(ach, MODNAME ": ");
  2551. wvsprintfA(ach+lstrlenA(ach),szFormat,(LPSTR)(&szFormat+1));
  2552. // lstrcatA(ach, "\r\r\n");
  2553. #endif
  2554. OutputDebugStringA(ach);
  2555. }
  2556. #endif
  2557. #ifdef _WIN32
  2558. #define FADMIN_NOT_CACHED 15
  2559. int fIsAdmin = FADMIN_NOT_CACHED; // any arbitrary value that will not be TRUE or FALSE
  2560. BOOL IsAdmin(void)
  2561. {
  2562. BOOL IsMember;
  2563. SID_IDENTIFIER_AUTHORITY sia = SECURITY_NT_AUTHORITY;
  2564. PSID AdminSid;
  2565. #ifdef DEBUG_RETAIL
  2566. // see if we should run as a normal user.
  2567. // ADMINs can pretend to be normal users; vice versa does not work
  2568. // Hence you have to pass the security checks below to be recognised
  2569. // as an admin
  2570. if (mmGetProfileIntA(MODNAME, "NormalUser", FALSE)) {
  2571. DPF(("Forcing NON admin"));
  2572. return(FALSE);
  2573. }
  2574. #endif
  2575. // If we have cached a value, return the cached value
  2576. if (FADMIN_NOT_CACHED != fIsAdmin) {
  2577. return(fIsAdmin);
  2578. }
  2579. if (!AllocateAndInitializeSid(&sia, // identifier authority
  2580. 2, // subauthority count
  2581. SECURITY_BUILTIN_DOMAIN_RID, // subauthority 0
  2582. DOMAIN_ALIAS_RID_ADMINS, // subauthority 1
  2583. 0,0,0,0,0,0, // subauthority 2-7
  2584. &AdminSid)) { // result target
  2585. //
  2586. // Failed, don't assume we are an admin.
  2587. //
  2588. return FALSE;
  2589. } else if (!CheckTokenMembership(NULL,
  2590. AdminSid,
  2591. &IsMember)) {
  2592. //
  2593. // Failed, don't assume we are an admin.
  2594. //
  2595. FreeSid(AdminSid);
  2596. return FALSE;
  2597. } else {
  2598. //
  2599. // We have a definitive answer, set the cached value.
  2600. //
  2601. fIsAdmin = IsMember;
  2602. FreeSid(AdminSid);
  2603. return fIsAdmin;
  2604. }
  2605. // NOT REACHED
  2606. return FALSE;
  2607. }
  2608. #endif // _WIN32
  2609. #ifdef DAYTONA
  2610. #define KEYSECTION TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\")
  2611. LONG OpenUserKey(PHKEY pKey, LPCTSTR lp, LPCTSTR pSection)
  2612. {
  2613. DWORD disposition;
  2614. TCHAR section[256];
  2615. lstrcpy(section, KEYSECTION);
  2616. lstrcat(section, pSection);
  2617. if (lp) {
  2618. // Note: we should never need to create the user section in order
  2619. // to query the data. If the section does not exist, then nothing is
  2620. // the right thing to return.
  2621. return(RegCreateKeyExW(HKEY_CURRENT_USER, section, 0, NULL, 0,
  2622. KEY_SET_VALUE, NULL, pKey, &disposition));
  2623. } else {
  2624. // We are only reading what is there...
  2625. return(RegOpenKeyExW(HKEY_CURRENT_USER, section, 0, KEY_QUERY_VALUE, pKey));
  2626. }
  2627. }
  2628. LONG OpenSystemKey(PHKEY pKey, LPCTSTR lp, LPCTSTR pSection)
  2629. {
  2630. DWORD disposition;
  2631. TCHAR section[256];
  2632. lstrcpy(section, KEYSECTION);
  2633. lstrcat(section, pSection);
  2634. if (lp) {
  2635. return(RegCreateKeyExW(HKEY_LOCAL_MACHINE, section, 0, NULL, 0,
  2636. KEY_QUERY_VALUE | KEY_SET_VALUE, NULL, pKey, &disposition));
  2637. } else {
  2638. // We are only reading what is there...
  2639. return(RegOpenKeyExW(HKEY_LOCAL_MACHINE, section, 0, KEY_QUERY_VALUE, pKey));
  2640. }
  2641. }
  2642. // Use a mapping to get the stuff into the registry
  2643. BOOL myWritePrivateProfileString(
  2644. LPCTSTR lpszSection, // address of section name
  2645. LPCTSTR lpszKeyName, // address of key name
  2646. LPCTSTR lpszString // address of string to add
  2647. )
  2648. {
  2649. if (IsAdmin()) {
  2650. return WritePrivateProfileString(lpszSection, lpszKeyName, lpszString, gszSystemIni);
  2651. } else /* NOT ADMIN */ {
  2652. // write to HKEY_CURRENT_USER
  2653. // Data is written to
  2654. HKEY key;
  2655. // If we have something to write, then we must create the key
  2656. // If we are about to delete something that might not exist we only
  2657. // want to open the key. Hence OpenUserKey needs to know if lpszString is NULL
  2658. if (ERROR_SUCCESS == OpenUserKey(&key, lpszString, lpszSection)) {
  2659. // We have access. Now write the data
  2660. if (lpszString) {
  2661. LPCTSTR lpStr;
  2662. RegSetValueEx(key, lpszKeyName, 0, REG_SZ,
  2663. (LPCVOID)lpszString, sizeof(TCHAR)*(lstrlen(lpszString)+1));
  2664. lpStr = lpszString + 1 + lstrlen(lpszString);
  2665. // Is there an associated description ??
  2666. if (*(lpStr)) {
  2667. HKEY key2;
  2668. DWORD disposition;
  2669. TCHAR section[256];
  2670. // Write the description
  2671. lstrcpy(section, KEYSECTION);
  2672. lstrcat(section, TEXT("Drivers.desc"));
  2673. if (ERROR_SUCCESS ==
  2674. (RegCreateKeyExW(HKEY_CURRENT_USER, section, 0, NULL, 0,
  2675. KEY_SET_VALUE, NULL, &key2, &disposition))) {
  2676. RegSetValueEx(key2, lpszString, 0, REG_SZ,
  2677. (LPCVOID)lpStr, sizeof(TCHAR)*(lstrlen(lpStr)+1));
  2678. RegCloseKey(key2);
  2679. }
  2680. }
  2681. } else {
  2682. // delete the data
  2683. RegDeleteValue(key, lpszKeyName);
  2684. }
  2685. RegCloseKey(key);
  2686. return(TRUE);
  2687. }
  2688. return(FALSE);
  2689. }
  2690. }
  2691. DWORD myGetPrivateProfileString(
  2692. LPCTSTR lpszSection, // address of section name
  2693. LPCTSTR lpszKey, // address of key name
  2694. LPCTSTR lpszDefault, // address of default string
  2695. LPTSTR lpszReturnBuffer, // address of destination buffer
  2696. DWORD cchReturnBuffer) // size of destination buffer
  2697. {
  2698. // Whether we are an admin or not we have to read data from HKEY_CURRENT_USER
  2699. // first as that overrides the SYSTEM installed details.
  2700. // If we are enumerating the section, then we want to delete duplicate definitions
  2701. // in the system block. This is where the complexity enters.
  2702. DWORD dwType;
  2703. HKEY key;
  2704. UINT nSize;
  2705. UINT nRet=ERROR_NO_MORE_ITEMS;
  2706. LPTSTR lpBuf;
  2707. LPVOID lpEnd;
  2708. UINT size = cchReturnBuffer * sizeof(TCHAR);
  2709. lpBuf = lpszReturnBuffer;
  2710. lpEnd = ((LPBYTE)lpBuf)+size;
  2711. #define CUSERDRIVERS 20
  2712. if (!lpszKey) {
  2713. // Will enumerate the list of installed drivers - first USER
  2714. // then SYSTEM. User installed drivers take precendence (if
  2715. // there is duplication). We used to read the whole section,
  2716. // but we are only interested in fcctype.fcchandler type entries.
  2717. // Therefore, any that do not match this pattern are skipped.
  2718. UINT cch1, count;
  2719. TCHAR section[256];
  2720. LPTSTR aszUserDrivers[CUSERDRIVERS];
  2721. UINT cUserDrivers=0;
  2722. UINT iKey;
  2723. #ifdef DEBUG
  2724. memset(lpszReturnBuffer, 0xfe, cchReturnBuffer*sizeof(TCHAR));
  2725. // verify that we do not write more data than we should
  2726. #endif
  2727. #if 0
  2728. // This is the old code. Use this to verify that the registry enumeration is
  2729. // correct.
  2730. // Check that the registry enumeration code produces the same result
  2731. cch1 = GetPrivateProfileString(lpszSection, lpszKey, lpszDefault,
  2732. lpszReturnBuffer, cchReturnBuffer, gszSystemIni);
  2733. #endif
  2734. // Read the user section
  2735. // then read the system section, skipping codecs already found
  2736. if (ERROR_SUCCESS == OpenUserKey(&key, NULL, lpszSection)) {
  2737. for (iKey = 0; ; ++iKey) {
  2738. // Calculate - in characters - how much space is left in the
  2739. // name buffer
  2740. nSize = (UINT) (UINT_PTR) ((LPTSTR)lpEnd-lpBuf);
  2741. // Enumerate the name. We do not at this point need the values
  2742. // associated with the names, only the list of installed driver types
  2743. nRet = RegEnumValue(key, iKey, lpBuf, &nSize, NULL,
  2744. &dwType, NULL, NULL);
  2745. if (nRet!= ERROR_SUCCESS) break; // bail out
  2746. // If this is not xxxx.yyyy then ignore it.
  2747. if ((nSize != sizeof(FOURCC)+sizeof(FOURCC)+1)
  2748. || (lpBuf[4] != TEXT('.')))
  2749. {
  2750. continue;
  2751. }
  2752. if (cUserDrivers<CUSERDRIVERS) {
  2753. // Remember the name of this driver
  2754. aszUserDrivers[cUserDrivers++] = lpBuf;
  2755. } else {
  2756. // Too many user installed drivers... let there be duplicates
  2757. }
  2758. lpBuf += nSize+1; // Step over this name and its terminating null
  2759. }
  2760. RegCloseKey(key);
  2761. }
  2762. // Unless we ran out of room we need to read the system section
  2763. if (nRet == ERROR_NO_MORE_ITEMS)
  2764. if (ERROR_SUCCESS == OpenSystemKey(&key, NULL, lpszSection)) {
  2765. for (iKey = 0; ; ++iKey) {
  2766. // Calculate - in characters - how much space is left in the
  2767. // name buffer
  2768. nSize = (DWORD) (DWORD_PTR) ((LPTSTR)lpEnd-lpBuf);
  2769. // Enumerate the name. We do not at this point need the values
  2770. // associated with the names, only the list of installed driver types
  2771. nRet = RegEnumValue(key, iKey, lpBuf, &nSize, NULL,
  2772. &dwType, NULL, NULL);
  2773. if (nRet!= ERROR_SUCCESS) break; // bail out
  2774. // If this is not xxxx.yyyy then ignore it.
  2775. if (nSize != sizeof(FOURCC)+sizeof(FOURCC)+1) {
  2776. continue;
  2777. }
  2778. // If we have already found this driver in the user section, then
  2779. // ignore the system definition
  2780. for (count=0; count<cUserDrivers; ++count) {
  2781. if (0 == lstrcmpi(lpBuf, aszUserDrivers[count])) {
  2782. *lpBuf = 0; // wipeout the last value
  2783. goto skipped;
  2784. }
  2785. }
  2786. lpBuf += nSize+1; // Step over this name and its terminating null
  2787. skipped: ;
  2788. }
  2789. RegCloseKey(key);
  2790. }
  2791. if (ERROR_MORE_DATA == nRet) {
  2792. // we ran out of room
  2793. nSize = cchReturnBuffer-2; // same return as GetPrivateProfileString
  2794. } else if (nRet == ERROR_NO_MORE_ITEMS) {
  2795. // Success. Calculate the number of characters in the buffer
  2796. *lpBuf = 0; // Write a second terminating zero
  2797. // Now calculate how many characters we are returning, excluding one
  2798. // of the two terminating zeros
  2799. nSize = (DWORD) (DWORD_PTR) (lpBuf-lpszReturnBuffer);
  2800. } else {
  2801. // something went wrong. No data, or another error, return nothing
  2802. // Make sure the buffer has a double terminating null
  2803. *lpBuf++ = 0;
  2804. *lpBuf++ = 0;
  2805. nSize=0;
  2806. }
  2807. return(nSize);
  2808. } else {
  2809. // Not enumerating. We have a specific value to look for
  2810. if (ERROR_SUCCESS == OpenUserKey(&key, NULL, lpszSection)) {
  2811. // Calculate - in bytes - how much space is in the buffer
  2812. nSize = (DWORD) (DWORD_PTR) ((LPBYTE)lpEnd-(LPBYTE)lpBuf);
  2813. // Get the data
  2814. nRet = RegQueryValueEx(key, lpszKey, NULL,
  2815. &dwType, (LPBYTE)lpBuf, &nSize);
  2816. RegCloseKey(key);
  2817. }
  2818. // If we could not find the data in the user key then try SYSTEM
  2819. if ((ERROR_SUCCESS != nRet) && (ERROR_MORE_DATA != nRet)) {
  2820. // Try the system key
  2821. if (ERROR_SUCCESS == OpenSystemKey(&key, NULL, lpszSection)) {
  2822. // Calculate - in bytes - how much space is in the buffer
  2823. nSize = (DWORD) (DWORD_PTR) ((LPBYTE)lpEnd-(LPBYTE)lpBuf);
  2824. // Get the data
  2825. nRet = RegQueryValueEx(key, lpszKey, NULL,
  2826. &dwType, (LPBYTE)lpBuf, &nSize);
  2827. RegCloseKey(key);
  2828. }
  2829. }
  2830. if (ERROR_MORE_DATA == nRet) {
  2831. return(cchReturnBuffer-1); // not enough room for the data
  2832. }
  2833. if (nRet != ERROR_SUCCESS) {
  2834. return(0); // cannot find the data
  2835. }
  2836. if (REG_SZ != dwType) {
  2837. return(0); // we must have string data
  2838. }
  2839. //RegQueryValueEx returns length in bytes and includes the terminating zero
  2840. return (DWORD) (nSize/sizeof(TCHAR) - 1);
  2841. }
  2842. }
  2843. #endif