RtVidc.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19. //
  20. // RTVIDC.CPP
  21. //
  22. // History:
  23. // 11/13/95 JMI Started.
  24. //
  25. //////////////////////////////////////////////////////////////////////////////
  26. //
  27. // This class is designed to receive VIDC data in real time and decompress it
  28. // to a specific buffer or buffers on a channel basis.
  29. //
  30. //////////////////////////////////////////////////////////////////////////////
  31. //////////////////////////////////////////////////////////////////////////////
  32. // C Headers.
  33. //////////////////////////////////////////////////////////////////////////////
  34. //////////////////////////////////////////////////////////////////////////////
  35. // Blue Headers.
  36. //////////////////////////////////////////////////////////////////////////////
  37. #include "System.h"
  38. #include "bdebug.h"
  39. //////////////////////////////////////////////////////////////////////////////
  40. // Green Headers.
  41. //////////////////////////////////////////////////////////////////////////////
  42. #include "rttypes.h"
  43. #include "rtvidc.h"
  44. //////////////////////////////////////////////////////////////////////////////
  45. // Orange Headers.
  46. //////////////////////////////////////////////////////////////////////////////
  47. //////////////////////////////////////////////////////////////////////////////
  48. // Yellow Headers.
  49. //////////////////////////////////////////////////////////////////////////////
  50. //////////////////////////////////////////////////////////////////////////////
  51. // Module specific macros.
  52. //////////////////////////////////////////////////////////////////////////////
  53. // Types of chunks.
  54. #define VIDC_CHUNK_HEADER RT_FLAG_INIT
  55. #define VIDC_CHUNK_PALETTE RT_FLAG_USER1
  56. //////////////////////////////////////////////////////////////////////////////
  57. // Module specific typedefs.
  58. //////////////////////////////////////////////////////////////////////////////
  59. //////////////////////////////////////////////////////////////////////////////
  60. // Module specific (static) variables.
  61. //////////////////////////////////////////////////////////////////////////////
  62. //////////////////////////////////////////////////////////////////////////////
  63. // Construction/Destruction Functions.
  64. //////////////////////////////////////////////////////////////////////////////
  65. //////////////////////////////////////////////////////////////////////////////
  66. //
  67. // Default constructor.
  68. //
  69. //////////////////////////////////////////////////////////////////////////////
  70. CRtVidc::CRtVidc()
  71. {
  72. Set();
  73. }
  74. //////////////////////////////////////////////////////////////////////////////
  75. //
  76. // Destructor.
  77. //
  78. //////////////////////////////////////////////////////////////////////////////
  79. CRtVidc::~CRtVidc()
  80. {
  81. Reset();
  82. }
  83. //////////////////////////////////////////////////////////////////////////////
  84. // Internal Functions.
  85. //////////////////////////////////////////////////////////////////////////////
  86. //////////////////////////////////////////////////////////////////////////////
  87. //
  88. // Sets variables w/o regard to current values.
  89. //
  90. //////////////////////////////////////////////////////////////////////////////
  91. void CRtVidc::Set(void)
  92. {
  93. m_pdispatch = NULL;
  94. for (short i = 0; i < MAX_VID_CHANNELS; i++)
  95. {
  96. m_avidchdrs[i].sNumFrames = 0;
  97. m_avidchdrs[i].pImage = NULL;
  98. m_avidchdrs[i].callbackHeader = NULL;
  99. m_avidchdrs[i].callbackBefore = NULL;
  100. m_avidchdrs[i].callbackAfter = NULL;
  101. m_avidchdrs[i].hic = NULL;
  102. }
  103. }
  104. //////////////////////////////////////////////////////////////////////////////
  105. //
  106. // Resets variables. Performs deallocation if necessary.
  107. //
  108. //////////////////////////////////////////////////////////////////////////////
  109. void CRtVidc::Reset(void)
  110. {
  111. }
  112. //////////////////////////////////////////////////////////////////////////////
  113. //
  114. // Reads a BITMAPINFO from the specified CNFile into the provided data.
  115. // Returns number of bytes read on success, negative on error.
  116. //
  117. //////////////////////////////////////////////////////////////////////////////
  118. long ReadBitmapInfo( BITMAPINFO* pbmi, CNFile* pfile)
  119. {
  120. // Remember current position.
  121. long lPos = pfile->Tell();
  122. // Write BITMAPINFO.
  123. pfile->Read(&pbmi->bmiHeader.biSize);
  124. pfile->Read(&pbmi->bmiHeader.biWidth);
  125. pfile->Read(&pbmi->bmiHeader.biHeight);
  126. pfile->Read(&pbmi->bmiHeader.biPlanes);
  127. pfile->Read(&pbmi->bmiHeader.biBitCount);
  128. pfile->Read(&pbmi->bmiHeader.biCompression);
  129. pfile->Read(&pbmi->bmiHeader.biSizeImage);
  130. pfile->Read(&pbmi->bmiHeader.biXPelsPerMeter);
  131. pfile->Read(&pbmi->bmiHeader.biYPelsPerMeter);
  132. pfile->Read(&pbmi->bmiHeader.biClrUsed);
  133. pfile->Read(&pbmi->bmiHeader.biClrImportant);
  134. // pfile->Read color info, if any.
  135. // Get number of bytes left.
  136. long lNumColors = pbmi->bmiHeader.biSize - sizeof(pbmi->bmiHeader);
  137. // Must be complete RGBQUADs.
  138. ASSERT(lNumColors % 4 == 0);
  139. // Convert to number of colors left.
  140. lNumColors /= 4L;
  141. for (long l = 0; l < lNumColors; l++)
  142. {
  143. pfile->Read(&pbmi->bmiColors[l].rgbBlue);
  144. pfile->Read(&pbmi->bmiColors[l].rgbGreen);
  145. pfile->Read(&pbmi->bmiColors[l].rgbRed);
  146. pfile->Read(&pbmi->bmiColors[l].rgbReserved);
  147. }
  148. return pfile->Tell() - lPos;
  149. }
  150. //////////////////////////////////////////////////////////////////////////////
  151. //
  152. // Decompresses a VIDC frame using the opened decompressor.
  153. // Returns 0 on success.
  154. //
  155. //////////////////////////////////////////////////////////////////////////////
  156. short CRtVidc::DecompressFrame( PVIDC_RT_HDR pvidchdr, CNFile* pfile,
  157. ULONG ulFlags, PBMI pbmiIn, PBMI pbmiOut)
  158. {
  159. short sRes = 0; // Assume success.
  160. // Get data in compression native format.
  161. BMI bmiTempOut = *pbmiIn;
  162. bmiTempOut.bmiHeader.biCompression = BI_RGB;
  163. // Attempt to decompress . . .
  164. // if (ICDecompress( pvidchdr->hic, ulFlags,
  165. // (BITMAPINFOHEADER*)pbmiIn, pfile->GetMemory() + pfile->Tell(),
  166. // (BITMAPINFOHEADER*)pbmiOut, pvidchdr->pImage->pData)
  167. // == ICERR_OK)
  168. HBITMAP hbm = (HBITMAP)ICImageDecompress( pvidchdr->hic, 0L, (BITMAPINFO*)pbmiIn,
  169. pfile->GetMemory() + pfile->Tell(),
  170. (BITMAPINFO*)&bmiTempOut);
  171. if (hbm != NULL)
  172. {
  173. // Successfully decompressed.
  174. HDC hDC = GetDC(NULL);
  175. if (hDC != NULL)
  176. {
  177. pbmiOut->bmiHeader.biSize = sizeof(pbmiOut->bmiHeader);
  178. pbmiOut->bmiHeader.biSizeImage = 0;
  179. // Reformat.
  180. if (GetDIBits( hDC, hbm, 0, pvidchdr->pImage->lHeight,
  181. pvidchdr->pImage->pData, (BITMAPINFO*)&pbmiOut, DIB_RGB_COLORS) != FALSE)
  182. {
  183. // Success.
  184. }
  185. else
  186. {
  187. TRACE("DecompressFrame(): GetDIBits failed.\n");
  188. sRes = -3;
  189. }
  190. ReleaseDC(NULL, hDC);
  191. }
  192. else
  193. {
  194. TRACE("DecompressFrame(): GetDC failed.\n");
  195. sRes = -2;
  196. }
  197. // Destroy the bitmap.
  198. DeleteObject(hbm);
  199. }
  200. else
  201. {
  202. TRACE("DecompressFrame(): ICDecompress failed.\n");
  203. sRes = -1;
  204. }
  205. return sRes;
  206. }
  207. //////////////////////////////////////////////////////////////////////////////
  208. //
  209. // Use handler for RtVidc buffers.
  210. // Returns RET_FREE if done with data on return, RET_DONTFREE otherwise.
  211. //
  212. //////////////////////////////////////////////////////////////////////////////
  213. short CRtVidc::Use( UCHAR* puc, long lSize, USHORT usType, UCHAR ucFlags,
  214. long lTime)
  215. {
  216. short sRes = RET_FREE; // Always free.
  217. short sError = 0;
  218. ASSERT(usType == RT_TYPE_VIDC);
  219. ASSERT(puc != NULL);
  220. CNFile file;
  221. file.Open(puc, lSize, ENDIAN_LITTLE);
  222. // Read values common to all chunks.
  223. // Read vidc ID.
  224. USHORT usVidcId;
  225. file.Read (&usVidcId);
  226. // Make sure we're in range.
  227. ASSERT(usVidcId < MAX_VID_CHANNELS);
  228. // Get corresponding header.
  229. PVIDC_RT_HDR pvidchdr = &m_avidchdrs[usVidcId];
  230. // If this is a header chunk . . .
  231. if (ucFlags & VIDC_CHUNK_HEADER)
  232. {
  233. // Handle header chunk.
  234. file.Read(&pvidchdr->sNumFrames);
  235. file.Read(&pvidchdr->sWidth);
  236. file.Read(&pvidchdr->sHeight);
  237. file.Read(&pvidchdr->sDepth);
  238. file.Read(&pvidchdr->lMilliPerFrame);
  239. file.Read(&pvidchdr->sNoDelta);
  240. file.Read(&pvidchdr->sTransparent);
  241. file.Read(&pvidchdr->sX);
  242. file.Read(&pvidchdr->sY);
  243. file.Read(&pvidchdr->ulFCCHandler);
  244. // Verify we didn't read too much.
  245. ASSERT(file.Error() == FALSE);
  246. // Initialize frame counter.
  247. pvidchdr->sCurFrame = 0;
  248. pvidchdr->sColorsModified = FALSE;
  249. // Default to one frame's worth of lag before skipping frames.
  250. pvidchdr->lMaxLag = pvidchdr->lMilliPerFrame;
  251. // Attempt to open desired compressor/decompressor . . .
  252. // pvidchdr->hic = ICOpen(ICTYPE_VIDEO, pvidchdr->ulFCCHandler,
  253. // ICMODE_FASTDECOMPRESS);
  254. // If there is a callback for the header . . .
  255. if (pvidchdr->callbackHeader != NULL)
  256. {
  257. // Pass user all info.
  258. (*pvidchdr->callbackHeader)(this, pvidchdr);
  259. }
  260. }
  261. else
  262. {
  263. // If there is a callback for before decompression . . .
  264. if (pvidchdr->callbackBefore != NULL)
  265. {
  266. // Pass user all info.
  267. (*pvidchdr->callbackBefore)(this, pvidchdr);
  268. }
  269. // Decompress into image if supplied.
  270. if (pvidchdr->pImage != NULL)
  271. {
  272. ASSERT(pvidchdr->pImage->pData != NULL);
  273. ASSERT(pvidchdr->pImage->pPalette != NULL);
  274. ASSERT(pvidchdr->pImage->pPalette->pData != NULL);
  275. ULONG ulFlags = 0;
  276. if (ucFlags & RT_FLAG_TAG)
  277. {
  278. // Key frame.
  279. }
  280. else
  281. {
  282. ulFlags |= ICDECOMPRESS_NOTKEYFRAME;
  283. }
  284. // If we've exceeded the maximum lag . . .
  285. if (m_pdispatch->GetTime() - lTime > pvidchdr->lMaxLag)
  286. {
  287. ulFlags |= ICDECOMPRESS_HURRYUP;
  288. }
  289. BMI bmiIn;
  290. BMI bmiOut;
  291. ReadBitmapInfo((BITMAPINFO*)&bmiIn, &file);
  292. ReadBitmapInfo((BITMAPINFO*)&bmiOut, &file);
  293. // TEMP
  294. bmiOut.bmiHeader.biHeight = bmiIn.bmiHeader.biHeight;
  295. bmiOut.bmiHeader.biSizeImage = (pvidchdr->pImage->lPitch
  296. * bmiOut.bmiHeader.biHeight
  297. * bmiOut.bmiHeader.biBitCount)
  298. / 8;
  299. // END TEMP
  300. if (pvidchdr->hic == NULL)
  301. {
  302. pvidchdr->hic = ICLocate( ICTYPE_VIDEO, pvidchdr->ulFCCHandler,
  303. &bmiIn.bmiHeader,
  304. &bmiOut.bmiHeader,
  305. ICMODE_FASTDECOMPRESS);
  306. if (pvidchdr->hic != NULL)
  307. {
  308. // Success.
  309. }
  310. else
  311. {
  312. TRACE("Use(): Unable to locate decompressor.\n");
  313. }
  314. }
  315. // Test.
  316. ASSERT(ICDecompressQuery(pvidchdr->hic,
  317. (BITMAPINFO*)&bmiIn,
  318. (BITMAPINFO*)&bmiOut) == ICERR_OK);
  319. if (ICDecompressSetPalette(pvidchdr->hic, &bmiOut) == ICERR_OK)
  320. {
  321. // Success.
  322. }
  323. else
  324. {
  325. TRACE("Use(): Unable to set decompressor palette.\n");
  326. sError = 7;
  327. }
  328. // The rest of this chunk is regular old VIDC data!
  329. if (DecompressFrame(pvidchdr, &file, ulFlags, &bmiIn, &bmiOut) == 0)
  330. {
  331. // Successfully decompressed frames.
  332. if (ICDecompressGetPalette(pvidchdr->hic,
  333. (BITMAPINFOHEADER*)&bmiIn,
  334. (BITMAPINFOHEADER*)&bmiOut) == ICERR_OK)
  335. {
  336. if (ucFlags & VIDC_CHUNK_PALETTE)
  337. {
  338. ASSERT(pvidchdr->pImage->pData != NULL);
  339. ASSERT(pvidchdr->pImage->pPalette != NULL);
  340. ASSERT(pvidchdr->pImage->pPalette->pData != NULL);
  341. CPal pal;
  342. pal.ulType = PDIB;
  343. pal.ulSize = bmiOut.bmiHeader.biSize - sizeof(bmiOut.bmiHeader);
  344. pal.sPalEntrySize = sizeof(bmiOut.bmiColors[0]);
  345. pal.pData = (UCHAR*)bmiOut.bmiColors;
  346. // Attach the palette to an empty image.
  347. CImage imageEmpty;
  348. imageEmpty.ulSize = 0L;
  349. imageEmpty.ulType = BMP8;
  350. imageEmpty.pPalette = &pal;
  351. // If necessary . . .
  352. if (pal.ulType != pvidchdr->pImage->pPalette->ulType)
  353. {
  354. // Attempt to convert to user image type . . .
  355. if (imageEmpty.Convert(pvidchdr->pImage->ulType) == 0)
  356. {
  357. // Successfully converted.
  358. }
  359. else
  360. {
  361. TRACE("Use(): Failed to convert temp palette data to "
  362. "user format.\n");
  363. sError = 3;
  364. }
  365. }
  366. // Copy converted data that the user cares about.
  367. memcpy( pvidchdr->pImage->pPalette->pData,
  368. pal.pData + pvidchdr->pImage->pPalette->sStartIndex
  369. * pvidchdr->pImage->pPalette->sPalEntrySize,
  370. pvidchdr->pImage->pPalette->sNumEntries
  371. * pvidchdr->pImage->pPalette->sPalEntrySize);
  372. imageEmpty.pPalette = NULL;
  373. pvidchdr->sColorsModified = TRUE;
  374. }
  375. else
  376. {
  377. pvidchdr->sColorsModified = FALSE;
  378. }
  379. }
  380. else
  381. {
  382. TRACE("Use(): Unable to get palette from decompressor.\n");
  383. sError = 6;
  384. }
  385. // If there is a callback for after decompression . . .
  386. if (pvidchdr->callbackAfter != NULL)
  387. {
  388. // Pass user all info.
  389. (*pvidchdr->callbackAfter)(this, pvidchdr);
  390. }
  391. // Increment frame count.
  392. pvidchdr->sCurFrame++;
  393. }
  394. else
  395. {
  396. TRACE("Use(): VIDC decompression failed.\n");
  397. sError = 1;
  398. }
  399. }
  400. else
  401. {
  402. TRACE("Use(): Data with no associated image.\n");
  403. }
  404. // If this is the last chunk . . .
  405. if (ucFlags & RT_FLAG_LAST)
  406. {
  407. // If compressor was opened . . .
  408. if (pvidchdr->hic != NULL)
  409. {
  410. ICClose(pvidchdr->hic);
  411. pvidchdr->hic = NULL;
  412. }
  413. }
  414. // Verify we didn't read too much.
  415. ASSERT(file.Error() == FALSE);
  416. }
  417. file.Close();
  418. return sRes;
  419. }
  420. //////////////////////////////////////////////////////////////////////////////
  421. //
  422. // Callback dispatcher (calls the implied this version).
  423. // (static)
  424. //
  425. //////////////////////////////////////////////////////////////////////////////
  426. short CRtVidc::UseStatic( UCHAR* puc, long lSize, USHORT usType,
  427. UCHAR ucFlags, long lTime, long l_pRtVidc)
  428. {
  429. return ((CRtVidc*)l_pRtVidc)->Use(puc, lSize, usType, ucFlags, lTime);
  430. }
  431. //////////////////////////////////////////////////////////////////////////////
  432. // Methods.
  433. //////////////////////////////////////////////////////////////////////////////
  434. //////////////////////////////////////////////////////////////////////////////
  435. //
  436. // Set dispatcher.
  437. //
  438. //////////////////////////////////////////////////////////////////////////////
  439. void CRtVidc::SetDispatcher(CDispatch* pdispatch)
  440. {
  441. if (m_pdispatch != NULL)
  442. {
  443. m_pdispatch->SetDataHandler(RT_TYPE_VIDC, NULL);
  444. }
  445. m_pdispatch = pdispatch;
  446. if (m_pdispatch != NULL)
  447. {
  448. m_pdispatch->SetDataHandler(RT_TYPE_VIDC, UseStatic);
  449. m_pdispatch->SetUserVal(RT_TYPE_VIDC, (long)this);
  450. }
  451. }
  452. //////////////////////////////////////////////////////////////////////////////
  453. //
  454. // Sets callback(s) called on channel header receipt.
  455. //
  456. //////////////////////////////////////////////////////////////////////////////
  457. void CRtVidc::SetCallbackHeader(RTVIDC_CALL callback)
  458. {
  459. for (short i = 0; i < MAX_VID_CHANNELS; i++)
  460. {
  461. SetCallbackHeader(callback, i);
  462. }
  463. }
  464. //////////////////////////////////////////////////////////////////////////////
  465. //
  466. // Sets callback(s) called on channel header receipt.
  467. //
  468. //////////////////////////////////////////////////////////////////////////////
  469. void CRtVidc::SetCallbackHeader(RTVIDC_CALL callback, short sChannel)
  470. {
  471. m_avidchdrs[sChannel].callbackHeader = callback;
  472. }
  473. //////////////////////////////////////////////////////////////////////////////
  474. //
  475. // Sets callback(s) called before decompression.
  476. //
  477. //////////////////////////////////////////////////////////////////////////////
  478. void CRtVidc::SetCallbackBefore(RTVIDC_CALL callback)
  479. {
  480. for (short i = 0; i < MAX_VID_CHANNELS; i++)
  481. {
  482. SetCallbackBefore(callback, i);
  483. }
  484. }
  485. //////////////////////////////////////////////////////////////////////////////
  486. //
  487. // Sets callback(s) called before decompression.
  488. //
  489. //////////////////////////////////////////////////////////////////////////////
  490. void CRtVidc::SetCallbackBefore(RTVIDC_CALL callback, short sChannel)
  491. {
  492. m_avidchdrs[sChannel].callbackBefore = callback;
  493. }
  494. //////////////////////////////////////////////////////////////////////////////
  495. //
  496. // Sets callback(s) called after decompression.
  497. //
  498. //////////////////////////////////////////////////////////////////////////////
  499. void CRtVidc::SetCallbackAfter(RTVIDC_CALL callback)
  500. {
  501. for (short i = 0; i < MAX_VID_CHANNELS; i++)
  502. {
  503. SetCallbackAfter(callback, i);
  504. }
  505. }
  506. //////////////////////////////////////////////////////////////////////////////
  507. //
  508. // Sets callback(s) called after decompression.
  509. //
  510. //////////////////////////////////////////////////////////////////////////////
  511. void CRtVidc::SetCallbackAfter(RTVIDC_CALL callback, short sChannel)
  512. {
  513. m_avidchdrs[sChannel].callbackAfter = callback;
  514. }
  515. //////////////////////////////////////////////////////////////////////////////
  516. // EOF
  517. //////////////////////////////////////////////////////////////////////////////