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.

2773 lines
77 KiB

  1. /****************************************************************************
  2. *
  3. * AVIFILE.C
  4. *
  5. * routines for reading Standard AVI files
  6. *
  7. * Copyright (c) 1992 Microsoft Corporation. All Rights Reserved.
  8. *
  9. * You have a royalty-free right to use, modify, reproduce and
  10. * distribute the Sample Files (and/or any modified version) in
  11. * any way you find useful, provided that you agree that
  12. * Microsoft has no warranty obligations or liability for any
  13. * Sample Application Files which are modified.
  14. *
  15. ***************************************************************************/
  16. #include <win32.h>
  17. #include <storage.h>
  18. #include <compman.h>
  19. #include <compobj.h>
  20. #include <avifmt.h>
  21. #include "avifile.h"
  22. #include "avifilei.h"
  23. #include "avifile.rc"
  24. #include "debug.h"
  25. #undef GlobalFreePtr // fix for C6
  26. #define GlobalFreePtr(p) GlobalFree(GlobalPtrHandle(p))
  27. extern "C" STDAPI CalculateFileDataRate(PAVIFILE pf, LONG FAR *plMaxBytesPerSec);
  28. //#undef StreamFromFOURCC
  29. //#define StreamFromFOURCC(fcc) (UINT)(HIBYTE(LOWORD(fcc)) - (BYTE)'0')
  30. BOOL AddToIndex(CAVIFile FAR * pfile, DWORD ckid, DWORD cksize, LONG off, DWORD dwFlags);
  31. ///////////////////////////////////////////////////////////////////////////
  32. ///////////////////////////////////////////////////////////////////////////
  33. ///////////////////////////////////////////////////////////////////////////
  34. EXTERN_C void DecodeRle(LPBITMAPINFOHEADER lpbi, BYTE _huge *pb, BYTE _huge *prle);
  35. EXTERN_C HINSTANCE ghMod;
  36. ///////////////////////////////////////////////////////////////////////////
  37. ///////////////////////////////////////////////////////////////////////////
  38. #define comptypeNONE mmioFOURCC('N','O','N','E')
  39. #define comptypeRLE0 mmioFOURCC('R','L','E','0')
  40. #define comptypeRLE mmioFOURCC('R','L','E',' ')
  41. #define WIDTHBYTES(i) ((UINT)((i+31)&(~31))/8)
  42. #define DIBWIDTHBYTES(lpbi) (UINT)WIDTHBYTES((UINT)(lpbi)->biWidth * (UINT)(lpbi)->biBitCount)
  43. LONG lBufferSize = 0;
  44. int nBuffers = 0;
  45. #define ckidSTREAMNAME mmioFOURCC('s', 't', 'r', 'n')
  46. /***************************************************************************
  47. ***************************************************************************/
  48. EXTERN_C LONG FAR PASCAL shfileReadProc(HANDLE hsf, LONG lSeek, LONG lRead, LPVOID lpBuffer)
  49. {
  50. if (shfileSeek((HSHFILE)hsf, lSeek, SEEK_SET) == -1)
  51. return -1;
  52. if (shfileRead((HSHFILE)hsf, (HPSTR)lpBuffer, lRead) != lRead)
  53. return -1;
  54. return lRead;
  55. }
  56. ///////////////////////////////////////////////////////////////////////////
  57. ///////////////////////////////////////////////////////////////////////////
  58. #define INDEX_WRITE_SIZE 32l*1024
  59. #define INDEX_READ_SIZE 32l*1024
  60. static BOOL WriteOutIndex(CAVIFile FAR *pfile, DWORD dwOffsetMovie)
  61. {
  62. MMCKINFO ck;
  63. PAVIINDEXENTRY pIndex;
  64. LONG cnt = INDEX_WRITE_SIZE / sizeof(*pIndex);
  65. LONG l;
  66. BOOL f=FALSE;
  67. #ifdef DEBUG
  68. DWORD time;
  69. #endif
  70. /*
  71. ** Now write index out!
  72. */
  73. ck.ckid = ckidAVINEWINDEX;
  74. ck.cksize = sizeof(AVIINDEXENTRY) * pfile->px->nIndex;
  75. if (shfileCreateChunk(pfile->hshfile, &ck, 0))
  76. goto exit;
  77. DPF("Writing Index", time=timeGetTime());
  78. pIndex = (PAVIINDEXENTRY)GlobalAllocPtr(GHND,INDEX_WRITE_SIZE);
  79. if (pIndex == NULL)
  80. goto exit;
  81. for (l=0; l < pfile->px->nIndex; ) {
  82. cnt = IndexGetFileIndex(pfile->px, l, cnt, pIndex, -(LONG)dwOffsetMovie);
  83. if (cnt == 0)
  84. break;
  85. if (shfileWrite(pfile->hshfile, (HPSTR)pIndex,
  86. cnt * sizeof(AVIINDEXENTRY)) != cnt * sizeof(AVIINDEXENTRY))
  87. goto exit;
  88. l += cnt;
  89. DPF("!.");
  90. }
  91. DPF("!Done (%ldms)\n", timeGetTime()-time);
  92. if (shfileAscend(pfile->hshfile, &ck, 0))
  93. goto exit;
  94. f = TRUE;
  95. exit:
  96. if (pIndex)
  97. GlobalFreePtr(pIndex);
  98. return f;
  99. }
  100. ///////////////////////////////////////////////////////////////////////////
  101. ///////////////////////////////////////////////////////////////////////////
  102. static BOOL ReadInIndex(CAVIFile FAR *pfile, DWORD size, DWORD dwOffsetMovie, BOOL fRle)
  103. {
  104. PAVIINDEXENTRY pIndex;
  105. LONG cnt;
  106. LONG lIndexAdjust;
  107. BOOL f = FALSE;
  108. #ifdef DEBUG
  109. DWORD time;
  110. #endif
  111. pIndex = (PAVIINDEXENTRY)GlobalAllocPtr(GHND,INDEX_READ_SIZE);
  112. if (pIndex == NULL)
  113. goto exit;
  114. pfile->px = IndexCreate();
  115. if (pfile->px == 0)
  116. goto exit;
  117. DPF("Reading index.", time = timeGetTime());
  118. if (pfile->avihdr.dwFlags & AVIF_MUSTUSEINDEX)
  119. lIndexAdjust = dwOffsetMovie;
  120. else
  121. lIndexAdjust = -1; // set when we read first index entry.
  122. while (size > 0) {
  123. cnt = min(INDEX_READ_SIZE, size);
  124. if (shfileRead(pfile->hshfile,(HPSTR)pIndex,cnt) != cnt)
  125. goto exit;
  126. size -= cnt;
  127. cnt /= sizeof(AVIINDEXENTRY);
  128. //
  129. // fix up the index
  130. //
  131. if (lIndexAdjust == -1) {
  132. lIndexAdjust = (LONG)(dwOffsetMovie + sizeof(DWORD)) -
  133. (LONG)pIndex->dwChunkOffset;
  134. }
  135. pfile->px = IndexAddFileIndex(pfile->px, pIndex, cnt, lIndexAdjust, fRle);
  136. if (pfile->px == NULL)
  137. goto exit;
  138. DPF("!.");
  139. }
  140. DPF("!Done (%ldms)\n", timeGetTime() - time);
  141. f = TRUE;
  142. exit:
  143. if (pIndex)
  144. GlobalFreePtr(pIndex);
  145. return f;
  146. }
  147. ///////////////////////////////////////////////////////////////////////////
  148. ///////////////////////////////////////////////////////////////////////////
  149. HRESULT SaveChanges(CAVIFile FAR * pfile, BOOL fRelease)
  150. {
  151. CAVIStream FAR * pavi;
  152. int stream;
  153. MMCKINFO ck;
  154. MMCKINFO ckRIFF;
  155. MMCKINFO ckLIST;
  156. MMCKINFO ckStream;
  157. LONG lCur;
  158. HRESULT hr = AVIERR_OK;
  159. AVIStreamHeader strhdr;
  160. // Clean up interleaving
  161. if (pfile->fInRecord) {
  162. if (pfile->px->nIndex > pfile->lRecordIndex + 1) {
  163. AVIFileEndRecord((PAVIFILE) pfile);
  164. }
  165. // back out of last record....
  166. --pfile->px->nIndex;
  167. pfile->lWriteLoc -= 3 * sizeof(DWORD);
  168. shfileSeek(pfile->hshfile, pfile->lWriteLoc, SEEK_SET);
  169. pfile->fInRecord = FALSE;
  170. }
  171. // Go back and write out the header
  172. lCur = shfileSeek(pfile->hshfile, 0, SEEK_CUR);
  173. shfileSeek(pfile->hshfile, 0, SEEK_SET);
  174. /* Create RIFF chunk */
  175. ckRIFF.cksize = 0;
  176. ckRIFF.fccType = formtypeAVI;
  177. if (shfileCreateChunk(pfile->hshfile, &ckRIFF, MMIO_CREATERIFF)) {
  178. goto FileError;
  179. }
  180. /* Create header list */
  181. ckLIST.cksize = 0;
  182. ckLIST.fccType = listtypeAVIHEADER;
  183. if (shfileCreateChunk(pfile->hshfile, &ckLIST, MMIO_CREATELIST)) {
  184. goto FileError;
  185. }
  186. /* Create AVI header chunk */
  187. ck.cksize = sizeof(pfile->avihdr);
  188. ck.ckid = ckidAVIMAINHDR;
  189. if (shfileCreateChunk(pfile->hshfile, &ck, 0)) {
  190. goto FileError;
  191. }
  192. CalculateFileDataRate(&pfile->m_AVIFile, (LONG FAR *) &pfile->avihdr.dwMaxBytesPerSec);
  193. /* Write AVI header info */
  194. if (shfileWrite(pfile->hshfile,
  195. (LPSTR)&pfile->avihdr,
  196. sizeof(pfile->avihdr)) != sizeof(pfile->avihdr)) {
  197. goto FileError;
  198. }
  199. if (shfileAscend(pfile->hshfile, &ck, 0)) {
  200. goto FileError;
  201. }
  202. #if 0
  203. for (l = 0;
  204. l < muldiv32(pfile->avihdr.dwTotalFrames,
  205. pfile->avihdr.dwMicroSecPerFrame,
  206. 1000000L);
  207. l++) {
  208. for (stream = 0; stream < (int) pfile->avihdr.dwStreams; stream++) {
  209. }
  210. }
  211. #endif
  212. for (stream = 0; stream < (int) pfile->avihdr.dwStreams; stream++) {
  213. pavi = pfile->ps[stream];
  214. /* Create stream header list */
  215. ckStream.cksize = 0;
  216. ckStream.fccType = listtypeSTREAMHEADER;
  217. if (shfileCreateChunk(pfile->hshfile,&ckStream,MMIO_CREATELIST)) {
  218. goto FileError;
  219. }
  220. ck.ckid = ckidSTREAMHEADER;
  221. if (shfileCreateChunk(pfile->hshfile, &ck, 0)) {
  222. goto FileError;
  223. }
  224. // !!! Make an AVIStreamHeader from the AVISTREAMINFO
  225. strhdr.fccType = pavi->avistream.fccType;
  226. strhdr.fccHandler = pavi->avistream.fccHandler;
  227. strhdr.dwFlags = pavi->avistream.dwFlags;
  228. strhdr.wPriority = pavi->avistream.wPriority;
  229. strhdr.wLanguage = pavi->avistream.wLanguage;
  230. strhdr.dwRate = pavi->avistream.dwRate;
  231. strhdr.dwScale = pavi->avistream.dwScale;
  232. strhdr.dwStart = pavi->avistream.dwStart;
  233. strhdr.dwLength = pavi->avistream.dwLength;
  234. strhdr.dwSuggestedBufferSize = pavi->avistream.dwSuggestedBufferSize;
  235. strhdr.dwQuality = pavi->avistream.dwQuality;
  236. strhdr.dwSampleSize = pavi->avistream.dwSampleSize;
  237. strhdr.rcFrame = pavi->avistream.rcFrame;
  238. strhdr.dwInitialFrames = pavi->avistream.dwInitialFrames;
  239. if (shfileWrite(pfile->hshfile, (HPSTR) &strhdr, sizeof(strhdr)) !=
  240. sizeof(strhdr)) {
  241. goto FileError;
  242. }
  243. if (shfileAscend(pfile->hshfile, &ck, 0)) {
  244. goto FileError;
  245. }
  246. ck.cksize = pavi->cbFormat;
  247. ck.ckid = ckidSTREAMFORMAT;
  248. if (shfileCreateChunk(pfile->hshfile, &ck, 0))
  249. goto FileError;
  250. if (shfileWrite(pfile->hshfile, (HPSTR) pavi->lpFormat, ck.cksize) !=
  251. (LONG) ck.cksize)
  252. goto FileError;
  253. if (shfileAscend(pfile->hshfile, &ck, 0))
  254. goto FileError;
  255. if (pavi->avistream.szName[0]) {
  256. ck.cksize = lstrlen(pavi->avistream.szName) + 1;
  257. ck.ckid = ckidSTREAMNAME;
  258. if (shfileCreateChunk(pfile->hshfile, &ck, 0))
  259. goto FileError;
  260. if (shfileWrite(pfile->hshfile, (HPSTR) pavi->avistream.szName, ck.cksize) !=
  261. (LONG) ck.cksize)
  262. goto FileError;
  263. if (shfileAscend(pfile->hshfile, &ck, 0))
  264. goto FileError;
  265. }
  266. if (pavi->extra.cb) {
  267. DPF2("Writing %ld bytes of extra stream data.\n", pavi->extra.cb);
  268. if (shfileWrite(pfile->hshfile, (HPSTR) pavi->extra.lp, pavi->extra.cb) !=
  269. (LONG) pavi->extra.cb)
  270. goto FileError;
  271. }
  272. /* Ascend out of stream's header */
  273. if (shfileAscend(pfile->hshfile, &ckStream, 0)) {
  274. goto FileError;
  275. }
  276. }
  277. /* ascend from the Header list */
  278. if (shfileAscend(pfile->hshfile, &ckLIST, 0)) {
  279. goto FileError;
  280. }
  281. /* Pad this header out so that the real data will start on a 2K
  282. ** boundary by writing a JUNK chunk.
  283. */
  284. ck.ckid = ckidAVIPADDING;
  285. if (shfileCreateChunk(pfile->hshfile,&ck,0)) {
  286. goto FileError;
  287. }
  288. DPF("Data list start = %ld, current pos = %ld\n", pfile->lDataListStart, shfileSeek(pfile->hshfile, 0, SEEK_CUR));
  289. if (shfileSeek(pfile->hshfile, 0, SEEK_CUR) > pfile->lDataListStart) {
  290. // !!! Ack: we didn't leave enough space for the header.
  291. // !!! How can we avoid this?
  292. goto FileError;
  293. }
  294. shfileSeek(pfile->hshfile, pfile->lDataListStart, SEEK_SET);
  295. if (shfileAscend(pfile->hshfile, &ck, 0)) {
  296. goto FileError;
  297. }
  298. /* Start the 'movi' list, where all of the actual data will be. */
  299. ckLIST.cksize = 0;
  300. ckLIST.fccType = listtypeAVIMOVIE;
  301. if (shfileCreateChunk(pfile->hshfile, &ckLIST, MMIO_CREATELIST)) {
  302. goto FileError;
  303. }
  304. shfileSeek(pfile->hshfile, pfile->lWriteLoc, SEEK_SET);
  305. if (shfileAscend(pfile->hshfile, &ckLIST, 0))
  306. goto FileError;
  307. if (!WriteOutIndex(pfile, ckLIST.dwDataOffset))
  308. goto FileError;
  309. //
  310. // Write out any extra data around
  311. //
  312. if (pfile->extra.cb) {
  313. DPF2("Writing %ld bytes of extra file data.\n", pfile->extra.cb);
  314. if (shfileWrite(pfile->hshfile,
  315. (HPSTR) pfile->extra.lp,
  316. pfile->extra.cb) !=
  317. (LONG) pfile->extra.cb)
  318. goto FileError;
  319. }
  320. FinishUp:
  321. if (shfileAscend(pfile->hshfile, &ckRIFF, 0))
  322. goto FileError;
  323. //
  324. // Always flush to be sure that the data really made it to the disk....
  325. //
  326. if (shfileFlush(pfile->hshfile, 0))
  327. goto FileError;
  328. return ResultFromScode(AVIERR_OK);
  329. FileError:
  330. hr = ResultFromScode(AVIERR_FILEWRITE);
  331. goto FinishUp;
  332. }
  333. ///////////////////////////////////////////////////////////////////////////
  334. ///////////////////////////////////////////////////////////////////////////
  335. STDMETHODIMP_(ULONG) CAVIFile::CUnknownImpl::Release()
  336. {
  337. CAVIFile FAR * pfile = m_pAVIFile;
  338. CAVIStream FAR * pavi;
  339. int iStream;
  340. DPF2("File %lx: Usage--=%lx\n", (DWORD) (LPVOID) this, m_refs - 1);
  341. uUseCount--;
  342. if (!--m_refs) {
  343. if (pfile->fDirty) {
  344. ++m_refs;
  345. SaveChanges(pfile, TRUE);
  346. --m_refs;
  347. // Unfortunately, it's too late to tell about any errors....
  348. }
  349. for (iStream = 0; iStream < (int)pfile->avihdr.dwStreams; iStream++) {
  350. pavi = pfile->ps[iStream];
  351. if (!pavi)
  352. continue;
  353. if (pavi->lpFormat)
  354. GlobalFreePtr(pavi->lpFormat);
  355. if (pavi->lpData)
  356. GlobalFreePtr(pavi->lpData);
  357. if (pavi->extra.lp) {
  358. DPF2("Freeing %ld bytes of extra stream data.\n", pavi->extra.cb);
  359. GlobalFreePtr(pavi->extra.lp);
  360. }
  361. if (pavi->paviBase)
  362. AVIStreamClose(pavi->paviBase);
  363. delete pavi;
  364. }
  365. if (pfile->hshfile) {
  366. shfileRelease(pfile->hshfile);
  367. shfileClose(pfile->hshfile, 0);
  368. }
  369. if (pfile->px)
  370. FreeIndex(pfile->px);
  371. if (pfile->extra.lp) {
  372. DPF2("Freeing %ld bytes of extra file data.\n", pfile->extra.cb);
  373. GlobalFreePtr(pfile->extra.lp);
  374. }
  375. if (pfile->pb)
  376. EndBuffered(pfile->pb);
  377. pfile->hshfile = NULL;
  378. pfile->px = NULL;
  379. delete pfile;
  380. return 0;
  381. } else {
  382. if (pfile->hshfile)
  383. shfileRelease(pfile->hshfile);
  384. }
  385. return m_refs;
  386. }
  387. ///////////////////////////////////////////////////////////////////////////
  388. ///////////////////////////////////////////////////////////////////////////
  389. STDMETHODIMP CAVIFile::CAVIFileImpl::Save(
  390. LPCSTR szFile,
  391. AVICOMPRESSOPTIONS FAR *lpOptions,
  392. AVISAVECALLBACK lpfnCallback)
  393. {
  394. CAVIFile FAR * pfile = m_pAVIFile;
  395. HRESULT hr = ResultFromScode(AVIERR_OK);
  396. if (pfile->fDirty) {
  397. hr = SaveChanges(pfile, FALSE);
  398. }
  399. return hr;
  400. }
  401. #define SLASH(c) ((c) == '/' || (c) == '\\')
  402. /*--------------------------------------------------------------+
  403. | FileName - return a pointer to the filename part of szPath |
  404. | with no preceding path. |
  405. +--------------------------------------------------------------*/
  406. LPSTR FAR FileName(LPCSTR lszPath)
  407. {
  408. LPCSTR lszCur;
  409. for (lszCur = lszPath + lstrlen(lszPath); lszCur > lszPath && !SLASH(*lszCur) && *lszCur != ':';)
  410. lszCur = AnsiPrev(lszPath, lszCur);
  411. if (lszCur == lszPath)
  412. return (LPSTR)lszCur;
  413. else
  414. return (LPSTR)(lszCur + 1);
  415. }
  416. BOOL IsRectBogus(LPRECT lprc, DWORD dwFrameWidth, DWORD dwFrameHeight,
  417. LPBITMAPINFOHEADER lpbi)
  418. {
  419. if (IsRectEmpty(lprc))
  420. return TRUE;
  421. if (lprc->right - lprc->left > (int) dwFrameWidth)
  422. return TRUE;
  423. if (lprc->bottom - lprc->top > (int) dwFrameHeight)
  424. return TRUE;
  425. // !!!! Check that rectangle matches lpbi?
  426. // We've run out of things to check, so it's OK....
  427. return FALSE;
  428. }
  429. ///////////////////////////////////////////////////////////////////////////
  430. ///////////////////////////////////////////////////////////////////////////
  431. STDMETHODIMP CAVIFile::CAVIFileImpl::Open(LPCSTR szFile, UINT mode)
  432. {
  433. CAVIFile FAR * pfile = m_pAVIFile;
  434. CAVIStream FAR * pavi;
  435. MMCKINFO ck;
  436. MMCKINFO ckRIFF;
  437. MMCKINFO ckLIST;
  438. MMCKINFO ckStream;
  439. DWORD dwSize;
  440. BOOL fRle=FALSE;
  441. LONG l;
  442. int iStream;
  443. int i;
  444. HRESULT hr = ResultFromScode(AVIERR_OK);
  445. IUnknown FAR * pUnk;
  446. UINT ui;
  447. AVIStreamHeader strhdr;
  448. char ach[20];
  449. char ach2[20];
  450. int iStreamNumber;
  451. #ifdef DEBUG
  452. DWORD time;
  453. #endif
  454. if (pfile->achFile[0])
  455. return ResultFromScode(-1);
  456. pfile->mode = mode;
  457. _fstrcpy(pfile->achFile, szFile);
  458. // Assumptions about avilib.cpp:
  459. // We're assuming that if CREATE is set, WRITE is set too.
  460. // We're assuming that we'll always see READWRITE instead of just WRITE.
  461. // force the share flags to the 'correct' values
  462. if (mode & OF_READWRITE) {
  463. pfile->mode = (mode & ~(MMIO_SHAREMODE)) | OF_SHARE_EXCLUSIVE;
  464. } else {
  465. pfile->mode = (mode & ~(MMIO_SHAREMODE)) | OF_SHARE_DENY_WRITE;
  466. }
  467. // try to open the actual file
  468. // If the first attempt fails, no system error box, please.
  469. ui = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  470. pfile->hshfile = shfileOpen(pfile->achFile, NULL, pfile->mode);
  471. if (!pfile->hshfile && ((mode & MMIO_RWMODE) == OF_READ)) {
  472. // if the open fails, try again without the share flags.
  473. pfile->mode &= ~(MMIO_SHAREMODE);
  474. pfile->hshfile = shfileOpen(pfile->achFile, NULL, pfile->mode);
  475. }
  476. SetErrorMode(ui);
  477. if (!pfile->hshfile) {
  478. hr = ResultFromScode(AVIERR_FILEOPEN);
  479. goto error;
  480. }
  481. if (mode & OF_CREATE) {
  482. // make a empty index.
  483. pfile->px = IndexCreate();
  484. if (pfile->px == 0)
  485. goto memerror;
  486. pfile->lWriteLoc = 0;
  487. pfile->lHeaderSize = sizeof(MainAVIHeader) + 11 * sizeof(DWORD);
  488. pfile->avihdr.dwFlags = AVIF_HASINDEX;
  489. } else {
  490. /* Read RIFF chunk */
  491. if (shfileDescend(pfile->hshfile, &ckRIFF, NULL, 0) != 0)
  492. goto readerror;
  493. /*
  494. * check for a 'QuickTime AVI' file, a QuickTime AVI file is a
  495. * QuickTime public movie with a AVI file in the 'mdat' atom.
  496. */
  497. if (ckRIFF.cksize == mmioFOURCC('m','d','a','t'))
  498. {
  499. /*
  500. * now the 'mdat' atom better be a RIFF/AVI or we cant handle it.
  501. */
  502. if (shfileDescend(pfile->hshfile, &ckRIFF, NULL, 0) != 0)
  503. goto formaterror;
  504. }
  505. if (ckRIFF.ckid != FOURCC_RIFF)
  506. goto formaterror;
  507. if (ckRIFF.fccType != formtypeAVI)
  508. goto formaterror;
  509. /* Read header list */
  510. ckLIST.fccType = listtypeAVIHEADER;
  511. if (FindChunkAndKeepExtras(&pfile->extra, pfile->hshfile, &ckLIST, &ckRIFF, MMIO_FINDLIST))
  512. goto error;
  513. pfile->lHeaderSize = ckLIST.cksize + 8 * sizeof(DWORD);
  514. /* Read AVI header chunk */
  515. ck.ckid = ckidAVIMAINHDR;
  516. if (FindChunkAndKeepExtras(&pfile->extra, pfile->hshfile, &ck, &ckLIST, MMIO_FINDCHUNK))
  517. goto error;
  518. dwSize = min(ck.cksize, sizeof(MainAVIHeader));
  519. /* Read AVI header info */
  520. if (shfileRead(pfile->hshfile, (HPSTR)&pfile->avihdr, dwSize) != (LONG)dwSize)
  521. goto readerror;
  522. if (shfileAscend(pfile->hshfile, &ck, 0))
  523. goto readerror;
  524. for (iStream = 0; iStream < (int)pfile->avihdr.dwStreams; iStream++) {
  525. pfile->ps[iStream] = NULL;
  526. }
  527. /* Allocate stream data stuff, read streams */
  528. for (iStream = 0; iStream < (int)pfile->avihdr.dwStreams; ) {
  529. if (shfileDescend(pfile->hshfile, &ckStream, &ckLIST, 0) != 0)
  530. goto readerror;
  531. //
  532. // found a non-stream header skip
  533. //
  534. if (ckStream.fccType != listtypeSTREAMHEADER ||
  535. ckStream.ckid != FOURCC_LIST) {
  536. if ((hr = ReadIntoExtra(&pfile->extra,
  537. pfile->hshfile,
  538. &ckStream)) != ResultFromScode(AVIERR_OK))
  539. goto error;
  540. if (shfileAscend(pfile->hshfile, &ckStream, 0) != 0)
  541. goto readerror;
  542. continue;
  543. }
  544. pfile->ps[iStream] = new FAR CAVIStream(NULL, &pUnk);
  545. if (!pfile->ps[iStream])
  546. goto memerror;
  547. pavi = pfile->ps[iStream];
  548. pavi->pfile = pfile;
  549. pavi->iStream = iStream;
  550. //
  551. // walk every chunk in this stream header, until we are done.
  552. //
  553. while (shfileDescend(pfile->hshfile, &ck, &ckStream, 0) == 0) {
  554. switch (ck.ckid) {
  555. case ckidSTREAMHEADER:
  556. //
  557. // set these to sane default's incase the file
  558. // header is not big enough
  559. //
  560. // NOTE the stream rectangle is set to NULL, if
  561. // this is a video stream it will be corrected
  562. // when we proccess the format.
  563. //
  564. strhdr.dwQuality = (DWORD) ICQUALITY_DEFAULT;
  565. SetRectEmpty(&strhdr.rcFrame);
  566. l = min(ck.cksize, sizeof(AVIStreamHeader));
  567. if (shfileRead(pfile->hshfile, (HPSTR)&strhdr, l) != l)
  568. goto readerror;
  569. // !!! Copy fields from strhdr into
  570. pavi->avistream.fccType = strhdr.fccType;
  571. pavi->avistream.fccHandler = strhdr.fccHandler;
  572. pavi->avistream.dwFlags = strhdr.dwFlags; //!!!
  573. pavi->avistream.dwCaps = 0; // !!!
  574. pavi->avistream.wPriority = strhdr.wPriority;
  575. pavi->avistream.wLanguage = strhdr.wLanguage;
  576. pavi->avistream.dwRate = strhdr.dwRate;
  577. pavi->avistream.dwScale = strhdr.dwScale;
  578. pavi->avistream.dwStart = strhdr.dwStart;
  579. pavi->avistream.dwLength = strhdr.dwLength;
  580. pavi->avistream.dwSuggestedBufferSize = strhdr.dwSuggestedBufferSize;
  581. pavi->avistream.dwInitialFrames = strhdr.dwInitialFrames;
  582. pavi->avistream.dwQuality = strhdr.dwQuality;
  583. pavi->avistream.dwSampleSize = strhdr.dwSampleSize;
  584. pavi->avistream.rcFrame = strhdr.rcFrame;
  585. pavi->avistream.dwEditCount = 0;
  586. pavi->avistream.dwFormatChangeCount = 0;
  587. // Make up a stream name out of the filename, stream
  588. // type, and stream number.
  589. if (pavi->avistream.fccType == streamtypeVIDEO)
  590. LoadString(ghMod, IDS_VIDEO, ach, sizeof(ach));
  591. else if (pavi->avistream.fccType == streamtypeAUDIO)
  592. LoadString(ghMod, IDS_AUDIO, ach, sizeof(ach));
  593. else
  594. wsprintf(ach, "'%4.4s'",
  595. (LPSTR)&(pavi->avistream.fccType));
  596. // figure out what # stream of this type this is....
  597. iStreamNumber = 1;
  598. for (i = 0; i < iStream; i++) {
  599. if (pfile->ps[i]->avistream.fccType ==
  600. pavi->avistream.fccType)
  601. ++iStreamNumber;
  602. }
  603. LoadString(ghMod, IDS_SSSTREAMD, ach2, sizeof(ach2));
  604. wsprintf(pavi->avistream.szName,
  605. (LPSTR)ach2,
  606. (LPSTR)FileName(pfile->achFile),
  607. (LPSTR)ach,
  608. iStreamNumber);
  609. break;
  610. case ckidSTREAMFORMAT:
  611. if (pavi->lpFormat == NULL) {
  612. pavi->cbFormat = ck.cksize;
  613. pavi->lpFormat = GlobalAllocPtr(GMEM_MOVEABLE | GMEM_SHARE,
  614. ck.cksize);
  615. if (pavi->lpFormat == NULL)
  616. goto memerror;
  617. if (shfileRead(pfile->hshfile, (HPSTR) pavi->lpFormat, (LONG)ck.cksize) != (LONG)ck.cksize)
  618. goto readerror;
  619. #define lpbi ((LPBITMAPINFOHEADER)pavi->lpFormat)
  620. if (pavi->avistream.fccType != streamtypeVIDEO)
  621. break;
  622. //
  623. // make sure this is set
  624. //
  625. if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
  626. lpbi->biClrUsed = (1 << (int)lpbi->biBitCount);
  627. //
  628. // fix up bogus stream rectangles.
  629. //
  630. if (IsRectBogus(&pavi->avistream.rcFrame,
  631. pfile->avihdr.dwWidth,
  632. pfile->avihdr.dwHeight,
  633. lpbi)) {
  634. SetRect(&pavi->avistream.rcFrame, 0, 0,
  635. (int)lpbi->biWidth, (int)lpbi->biHeight);
  636. }
  637. //
  638. // make sure the biCompression is right for
  639. // RLE files.
  640. //
  641. if (lpbi->biCompression == 0 && lpbi->biBitCount == 8) {
  642. if (pavi->avistream.fccHandler == comptypeRLE0 ||
  643. pavi->avistream.fccHandler == comptypeRLE)
  644. lpbi->biCompression = BI_RLE8;
  645. }
  646. if (pavi->avistream.fccHandler == comptypeNONE &&
  647. lpbi->biCompression == 0)
  648. pavi->avistream.fccHandler = comptypeDIB;
  649. if (pavi->avistream.fccHandler == 0 &&
  650. lpbi->biCompression == 0)
  651. pavi->avistream.fccHandler = comptypeDIB;
  652. // Assuming a DIB handler has RGB data will blow up
  653. // if it has RLE data, and VidEdit et. al write out
  654. // confusing files like this.
  655. //if (pavi->avistream.fccHandler == comptypeDIB)
  656. // lpbi->biCompression = BI_RGB;
  657. if (lpbi->biCompression <= BI_RLE8)
  658. fRle = TRUE;
  659. #undef lpbi
  660. }
  661. break;
  662. case ckidSTREAMHANDLERDATA:
  663. if (pavi->lpData == NULL) {
  664. pavi->cbData = ck.cksize;
  665. pavi->lpData = GlobalAllocPtr(GMEM_MOVEABLE | GMEM_SHARE,
  666. ck.cksize);
  667. if (pavi->lpData == NULL)
  668. goto memerror;
  669. if (shfileRead(pfile->hshfile, (HPSTR)pavi->lpData,
  670. (LONG)ck.cksize) != (LONG)ck.cksize)
  671. goto readerror;
  672. }
  673. break;
  674. case ckidSTREAMNAME:
  675. l = min((LONG) ck.cksize, sizeof(pavi->avistream.szName));
  676. if (shfileRead(pfile->hshfile,
  677. (HPSTR)pavi->avistream.szName, l) != l)
  678. goto readerror;
  679. break;
  680. case ckidAVIPADDING:
  681. case mmioFOURCC('p','a','d','d'):
  682. break;
  683. default:
  684. if ((hr = ReadIntoExtra(&pavi->extra,
  685. pfile->hshfile,
  686. &ck)) != ResultFromScode(AVIERR_OK))
  687. goto error;
  688. break;
  689. }
  690. if (shfileAscend(pfile->hshfile, &ck, 0) != 0)
  691. goto readerror;
  692. }
  693. /* Ascend out of stream header */
  694. if (shfileAscend(pfile->hshfile, &ckStream, 0) != 0)
  695. goto readerror;
  696. if (pavi->avistream.fccType == 0)
  697. goto formaterror;
  698. if (pavi->lpFormat == NULL)
  699. goto formaterror;
  700. //
  701. // make sure the sample size is set right
  702. //
  703. switch(pavi->avistream.fccType) {
  704. case streamtypeAUDIO:
  705. /* Hack for backward compatibility with audio */
  706. pavi->avistream.dwSampleSize =
  707. ((LPWAVEFORMAT)pavi->lpFormat)->nBlockAlign;
  708. // For audio, this number isn't useful when reading.
  709. pavi->avistream.dwInitialFrames = 0;
  710. // !!! We should let people read what the header says....
  711. break;
  712. case streamtypeVIDEO:
  713. // !!! But what if the samples are all the right size?
  714. pavi->avistream.dwSampleSize = 0;
  715. break;
  716. default:
  717. // !!! ??? pavi->avistream.dwInitialFrames = 0;
  718. // !!! ??? pavi->avistream.dwSampleSize = 0;
  719. break;
  720. }
  721. l = sizeof(pavi->avistream.szName) - 1;
  722. pavi->avistream.szName[l] = '\0';
  723. // next stream
  724. iStream++;
  725. }
  726. // Read extra data at end of header list....
  727. FindChunkAndKeepExtras(&pfile->extra, pfile->hshfile, &ck, &ckLIST, 0);
  728. if (shfileAscend(pfile->hshfile, &ckLIST, 0))
  729. goto readerror;
  730. /* Find big data chunk */
  731. ckLIST.fccType = listtypeAVIMOVIE;
  732. if (FindChunkAndKeepExtras(&pfile->extra, pfile->hshfile, &ckLIST, &ckRIFF, MMIO_FINDLIST))
  733. goto error;
  734. pfile->lDataListStart = ckLIST.dwDataOffset - 2 * sizeof(DWORD);
  735. if (shfileAscend(pfile->hshfile, &ckLIST, 0))
  736. goto readerror;
  737. // Keep track of where data can be written
  738. pfile->lWriteLoc = ckLIST.dwDataOffset + ckLIST.cksize;
  739. //
  740. // read in or create a index, we only want the index entries for the
  741. // stream we are interested in!
  742. //
  743. ck.ckid = ckidAVINEWINDEX;
  744. if (FindChunkAndKeepExtras(&pfile->extra, pfile->hshfile, &ck, &ckRIFF, MMIO_FINDCHUNK) == 0 && ck.cksize != 0) {
  745. if (!ReadInIndex(pfile, ck.cksize, ckLIST.dwDataOffset, fRle))
  746. goto formaterror;
  747. } else {
  748. /* Seek back to beginning of list, so we can scan */
  749. shfileSeek(pfile->hshfile, ckLIST.dwDataOffset + sizeof(DWORD), SEEK_SET);
  750. //!!! should we realy scan big files?
  751. //!!! or give a error
  752. pfile->px = IndexCreate();
  753. if (pfile->px == 0)
  754. goto formaterror;
  755. DPF("Scanning index", time = timeGetTime());
  756. /* Scan through chunks... */
  757. while (shfileDescend(pfile->hshfile, &ck, &ckLIST, 0) == 0) {
  758. AddToIndex(pfile,ck.ckid,ck.cksize,ck.dwDataOffset-8,0);
  759. /* Hack: don't ascend from LISTs */
  760. if (ck.ckid != FOURCC_LIST) {
  761. if (shfileAscend(pfile->hshfile, &ck, 0) != 0)
  762. goto readerror;
  763. }
  764. if (pfile->px->nIndex % 512 == 0)
  765. DPF("!.");
  766. }
  767. DPF("!Done (%ldms)\n", timeGetTime() - time);
  768. }
  769. if (pfile->px->nIndex == 0)
  770. goto formaterror;
  771. // Read extra data at end of file....
  772. FindChunkAndKeepExtras(&pfile->extra, pfile->hshfile, &ck, &ckRIFF, 0);
  773. // shfileSetBuffer(pfile->hshfile, NULL, 0L, 0);
  774. //
  775. // compute dwSuggestedBufferSize
  776. //
  777. if (pfile->avihdr.dwFlags & AVIF_ISINTERLEAVED) {
  778. LONG l;
  779. LONG lLen;
  780. pfile->avihdr.dwSuggestedBufferSize = 0;
  781. for (l=IndexFirst(pfile->px, STREAM_REC);
  782. l != -1;
  783. l = IndexNext(pfile->px, l, 0)) {
  784. lLen = IndexLength(pfile->px, l);
  785. if (pfile->avihdr.dwSuggestedBufferSize < (DWORD)lLen)
  786. pfile->avihdr.dwSuggestedBufferSize = (DWORD)lLen;
  787. }
  788. }
  789. if ((pfile->avihdr.dwFlags & AVIF_ISINTERLEAVED) &&
  790. pfile->avihdr.dwInitialFrames) {
  791. pfile->pb = InitBuffered((int) pfile->avihdr.dwInitialFrames * 2,
  792. pfile->avihdr.dwSuggestedBufferSize,
  793. pfile->hshfile,
  794. pfile->px);
  795. }
  796. else /* if (pfile->avihdr.dwSuggestedBufferSize > 0 &&
  797. pfile->avihdr.dwSuggestedBufferSize < 32l*1024) */ {
  798. int nBuffers = GetProfileInt("avifile", "buffers", 5);
  799. pfile->pb = InitBuffered(nBuffers,
  800. min(pfile->avihdr.dwSuggestedBufferSize * 2, 32768L),
  801. pfile->hshfile,
  802. pfile->px);
  803. }
  804. }
  805. return ResultFromScode(AVIERR_OK);
  806. readerror:
  807. hr = ResultFromScode(AVIERR_FILEREAD);
  808. goto error;
  809. memerror:
  810. hr = ResultFromScode(AVIERR_MEMORY);
  811. goto error;
  812. formaterror:
  813. hr = ResultFromScode(AVIERR_BADFORMAT);
  814. error:
  815. if (hr == ResultFromScode(AVIERR_OK))
  816. hr = ResultFromScode(AVIERR_ERROR);
  817. return hr;
  818. }
  819. ///////////////////////////////////////////////////////////////////////////
  820. ///////////////////////////////////////////////////////////////////////////
  821. STDMETHODIMP CAVIFile::CAVIFileImpl::GetStream(PAVISTREAM FAR *ppavi, DWORD fccType, LONG lParam)
  822. {
  823. CAVIFile FAR * pfile = m_pAVIFile;
  824. CAVIStream FAR *pavi;
  825. int iStreamCur;
  826. int iStreamWant;
  827. int iStream;
  828. LONG lLength;
  829. *ppavi = NULL;
  830. iStreamWant = (int)lParam;
  831. if (iStreamWant < 0 || iStreamWant >= (int)pfile->avihdr.dwStreams)
  832. return ResultFromScode(AVIERR_NODATA);
  833. /* Allocate stream data stuff, read streams */
  834. for (iStreamCur = -1, iStream = 0;
  835. iStream < (int)pfile->avihdr.dwStreams;
  836. iStream++) {
  837. if (fccType == 0 || pfile->ps[iStream]->avistream.fccType == fccType)
  838. iStreamCur++;
  839. if (iStreamCur == iStreamWant)
  840. break;
  841. }
  842. if (iStreamCur != iStreamWant)
  843. return ResultFromScode(AVIERR_NODATA);
  844. pavi = pfile->ps[iStream];
  845. if (pavi->fInit)
  846. goto returnnow;
  847. pavi->fInit = TRUE;
  848. #if 0
  849. if ((pf->mode & (OF_WRITE | OF_READWRITE)) == 0) {
  850. pavi->hshfile = shfileOpen(pfile->achFile, NULL, MMIO_ALLOCBUF | pfile->mode);
  851. if (!pavi->hshfile)
  852. goto error;
  853. } else
  854. #endif
  855. pavi->hshfile = pfile->hshfile;
  856. AddRef();
  857. pavi->lPal = -4242;
  858. pavi->psx = MakeStreamIndex(pfile->px, iStream,
  859. (LONG)pavi->avistream.dwStart - pavi->avistream.dwInitialFrames,
  860. (LONG)pavi->avistream.dwSampleSize,
  861. pfile->hshfile, shfileReadProc, NULL);
  862. if (pavi->psx == NULL)
  863. return ResultFromScode(AVIERR_MEMORY);
  864. pavi->avistream.dwSuggestedBufferSize = pavi->psx->lMaxSampleSize;
  865. if (pavi->psx->lPalFrames == 0)
  866. pavi->avistream.dwFlags &= ~AVISF_VIDEO_PALCHANGES;
  867. else
  868. pavi->avistream.dwFlags |= AVISF_VIDEO_PALCHANGES;
  869. pavi->pb = pavi->pfile->pb;
  870. if (!pavi->pb) {
  871. lBufferSize = GetProfileInt("avifile", "buffersize", 0) * 1024L;
  872. nBuffers = GetProfileInt("avifile", "buffers", 0);
  873. if (lBufferSize && nBuffers && !(pavi->pfile->mode & OF_CREATE)) {
  874. pavi->pb = InitBuffered(nBuffers, lBufferSize,
  875. pavi->hshfile, NULL);
  876. }
  877. }
  878. //
  879. // use ReadBuffered() to read data!
  880. //
  881. if (pavi->pb) {
  882. pavi->psx->hFile = (HANDLE)pavi->pb;
  883. pavi->psx->Read = (STREAMIOPROC)BufferedRead;
  884. }
  885. lLength = pavi->psx->lEnd - pavi->psx->lStart;
  886. if (lLength != (LONG)pavi->avistream.dwLength +
  887. (LONG)pavi->avistream.dwInitialFrames) {
  888. #ifdef DEBUG
  889. DPF("Stream %d: Length is %ld, header says %ld.\n",
  890. iStream, lLength,
  891. pavi->avistream.dwLength + pavi->avistream.dwInitialFrames);
  892. #endif
  893. //!!! should we correct the header!!!
  894. }
  895. returnnow:
  896. pavi->m_AVIStream.QueryInterface(IID_IAVIStream, (LPVOID FAR *) ppavi);
  897. //
  898. // all done return success.
  899. //
  900. return ResultFromScode(AVIERR_OK); // success
  901. }
  902. ///////////////////////////////////////////////////////////////////////////
  903. ///////////////////////////////////////////////////////////////////////////
  904. STDMETHODIMP CAVIFile::CAVIFileImpl::CreateStream(
  905. PAVISTREAM FAR *ppavi,
  906. AVISTREAMINFO FAR *psi)
  907. {
  908. CAVIFile FAR * pf = m_pAVIFile;
  909. CAVIStream FAR * pavi;
  910. int iStream = (int) pf->avihdr.dwStreams;
  911. IUnknown FAR * pUnk;
  912. // Check to see if the header will take up too much room!
  913. if (pf->lWriteLoc > 0) {
  914. if ((sizeof(AVIStreamHeader) + pf->lHeaderSize +
  915. lstrlen(psi->szName) + 8 * sizeof(DWORD)) >
  916. pf->lWriteLoc) {
  917. DPF("Header will be too big with this new stream!\n");
  918. return ResultFromScode(AVIERR_UNSUPPORTED);
  919. }
  920. }
  921. pf->lHeaderSize += sizeof(AVIStreamHeader) + 8 * sizeof(DWORD) +
  922. lstrlen(psi->szName);
  923. if (iStream >= MAXSTREAMS) {
  924. DPF("Ack: Too many streams: we only support %ld.\n", (LONG) MAXSTREAMS);
  925. return ResultFromScode(AVIERR_UNSUPPORTED);
  926. }
  927. if ((pf->mode & (OF_WRITE | OF_READWRITE)) == 0)
  928. return ResultFromScode(AVIERR_READONLY);
  929. pf->ps[iStream] = new FAR CAVIStream(NULL, &pUnk);
  930. if (!pf->ps[iStream])
  931. return ResultFromScode(AVIERR_MEMORY);
  932. pavi = pf->ps[iStream];
  933. pavi->iStream = iStream;
  934. pavi->pfile = pf;
  935. pavi->avistream = *psi;
  936. pavi->avistream.dwLength = 0; // no data initially
  937. pavi->avistream.dwSuggestedBufferSize = 0;
  938. pavi->hshfile = pf->hshfile;
  939. pavi->m_AVIStream.AddRef();
  940. AddRef();
  941. pavi->lpFormat = NULL; // This will be set leater with a SetFormat
  942. pavi->cbFormat = 0;
  943. if (pavi->avistream.fccType == streamtypeAUDIO) {
  944. SetRectEmpty(&pavi->avistream.rcFrame);
  945. }
  946. pf->avihdr.dwStreams++;
  947. if (pavi->iStream == 0) {
  948. pavi->pfile->avihdr.dwMicroSecPerFrame =
  949. max(1000L, muldiv32(1000000L,
  950. pavi->avistream.dwScale,
  951. pavi->avistream.dwRate));
  952. }
  953. /* Make sure the width and height of the created file are right.... */
  954. pf->avihdr.dwWidth = max(pf->avihdr.dwWidth,
  955. (DWORD) pavi->avistream.rcFrame.right);
  956. pf->avihdr.dwHeight = max(pf->avihdr.dwHeight,
  957. (DWORD) pavi->avistream.rcFrame.bottom);
  958. // Only if interleaved?
  959. pf->avihdr.dwInitialFrames = max(pf->avihdr.dwInitialFrames,
  960. pavi->avistream.dwInitialFrames);
  961. *ppavi = &pavi->m_AVIStream;
  962. return ResultFromScode(AVIERR_OK);
  963. }
  964. ///////////////////////////////////////////////////////////////////////////
  965. ///////////////////////////////////////////////////////////////////////////
  966. #if 0
  967. STDMETHODIMP CAVIFile::CAVIFileImpl::AddStream(
  968. PAVISTREAM pavi,
  969. PAVISTREAM FAR *ppaviNew)
  970. {
  971. CAVIFile FAR * pf = m_pAVIFile;
  972. CAVIStream FAR * paviNew;
  973. int iStream = (int) pf->avihdr.dwStreams;
  974. HRESULT hr;
  975. IUnknown FAR * pUnk;
  976. if ((pf->mode & (OF_WRITE | OF_READWRITE)) == 0)
  977. return ResultFromScode(AVIERR_READONLY);
  978. pfile->ps[iStream] = new FAR CAVIStream(NULL, &pUnk);
  979. // !!! error check?
  980. paviNew = pf->ps[iStream];
  981. paviNew->iStream = iStream;
  982. paviNew->pfile = pf;
  983. AVIStreamInfo(pavi, &paviNew->avistream, sizeof(paviNew->avistream));
  984. paviNew->hshfile = pf->hshfile;
  985. paviNew->m_AVIStream.AddRef();
  986. paviNew->paviBase = pavi;
  987. AVIStreamAddRef(pavi);
  988. paviNew->cbFormat = AVIStreamFormatSize(pavi, 0);
  989. paviNew->lpFormat = GlobalAllocPtr(GMEM_MOVEABLE | GMEM_SHARE, paviNew->cbFormat);
  990. if (!paviNew->lpFormat) {
  991. AVIStreamClose((PAVISTREAM) pf->ps[iStream]);
  992. return ResultFromScode(AVIERR_MEMORY);
  993. }
  994. AVIStreamReadFormat(pavi, 0, paviNew->lpFormat, &paviNew->cbFormat);
  995. pf->avihdr.dwStreams++;
  996. AddRef();
  997. if (paviNew->iStream == 0) {
  998. pf->avihdr.dwMicroSecPerFrame =
  999. muldiv32(1000000L,
  1000. paviNew->avistream.dwScale,
  1001. paviNew->avistream.dwRate);
  1002. }
  1003. *ppaviNew = (PAVISTREAM) paviNew;
  1004. return ResultFromScode(AVIERR_OK);
  1005. }
  1006. #endif
  1007. ///////////////////////////////////////////////////////////////////////////
  1008. ///////////////////////////////////////////////////////////////////////////
  1009. STDMETHODIMP CAVIFile::CAVIFileImpl::WriteData(
  1010. DWORD ckid,
  1011. LPVOID lpData,
  1012. LONG cbData)
  1013. {
  1014. CAVIFile FAR * pf = m_pAVIFile;
  1015. // !!! Anything else we can check?
  1016. if (lpData == NULL || cbData == 0)
  1017. return ResultFromScode(AVIERR_BADPARAM);
  1018. if ((pf->mode & (OF_WRITE | OF_READWRITE)) == 0)
  1019. return ResultFromScode(AVIERR_READONLY);
  1020. pf->fDirty = TRUE;
  1021. return WriteExtra(&pf->extra, ckid, lpData, cbData);
  1022. }
  1023. ///////////////////////////////////////////////////////////////////////////
  1024. ///////////////////////////////////////////////////////////////////////////
  1025. STDMETHODIMP CAVIFile::CAVIFileImpl::ReadData(
  1026. DWORD ckid,
  1027. LPVOID lpData,
  1028. LONG FAR *lpcbData)
  1029. {
  1030. CAVIFile FAR * pf = m_pAVIFile;
  1031. return ReadExtra(&pf->extra, ckid, lpData, lpcbData);
  1032. }
  1033. ///////////////////////////////////////////////////////////////////////////
  1034. ///////////////////////////////////////////////////////////////////////////
  1035. STDMETHODIMP CAVIFile::CAVIFileImpl::EndRecord()
  1036. {
  1037. CAVIFile FAR * pf = m_pAVIFile;
  1038. if ((pf->mode & (OF_WRITE | OF_READWRITE)) == 0)
  1039. return ResultFromScode(AVIERR_READONLY);
  1040. pf->fDirty = TRUE;
  1041. pf->avihdr.dwFlags |= AVIF_ISINTERLEAVED;
  1042. if (pf->lWriteLoc == 0) {
  1043. pf->lWriteLoc = (pf->lHeaderSize + 1024 + 2047) & ~(2047);
  1044. pf->lDataListStart = pf->lWriteLoc - 3 * sizeof(DWORD);
  1045. DPF("Writing first chunk at position %lu\n", pf->lWriteLoc);
  1046. }
  1047. shfileSeek(pf->hshfile, pf->lWriteLoc, SEEK_SET);
  1048. if (pf->fInRecord) {
  1049. // !!! Support padding here?
  1050. if (shfileAscend(pf->hshfile, (MMCKINFO FAR *) &pf->ckRecord, 0))
  1051. return ResultFromScode(AVIERR_FILEWRITE);
  1052. IndexSetLength(pf->px, pf->lRecordIndex, pf->ckRecord.cksize);
  1053. //
  1054. // Keep the main suggested buffer size as big as the biggest
  1055. // record....
  1056. //
  1057. if (pf->ckRecord.cksize + 3 * sizeof(DWORD) >
  1058. pf->avihdr.dwSuggestedBufferSize)
  1059. pf->avihdr.dwSuggestedBufferSize = pf->ckRecord.cksize +
  1060. 3 * sizeof(DWORD);
  1061. }
  1062. /* Start the next 'rec' list */
  1063. pf->ckRecord.cksize = 0;
  1064. pf->ckRecord.fccType = listtypeAVIRECORD;
  1065. pf->fInRecord = TRUE;
  1066. if (shfileCreateChunk(pf->hshfile, (MMCKINFO FAR *) &pf->ckRecord, MMIO_CREATELIST)) {
  1067. return ResultFromScode(AVIERR_FILEWRITE);
  1068. }
  1069. pf->lWriteLoc = shfileSeek(pf->hshfile, 0, SEEK_CUR);
  1070. pf->lRecordIndex = pf->px->nIndex;
  1071. if (!AddToIndex(pf, pf->ckRecord.fccType, 0,
  1072. pf->ckRecord.dwDataOffset - 2 * sizeof(DWORD), AVIIF_LIST)) {
  1073. return ResultFromScode(AVIERR_MEMORY);
  1074. }
  1075. return ResultFromScode(AVIERR_OK);
  1076. }
  1077. ///////////////////////////////////////////////////////////////////////////
  1078. ///////////////////////////////////////////////////////////////////////////
  1079. STDMETHODIMP CAVIFile::CAVIFileImpl::Info(
  1080. AVIFILEINFO FAR * pfi,
  1081. LONG lSize)
  1082. {
  1083. CAVIFile FAR * pf = m_pAVIFile;
  1084. AVIFILEINFO fi;
  1085. char ach[30];
  1086. if (pfi == NULL)
  1087. return ResultFromScode(AVIERR_BADPARAM);
  1088. if (lSize < sizeof(fi))
  1089. return ResultFromScode(AVIERR_BUFFERTOOSMALL);
  1090. fi.dwMaxBytesPerSec = pf->avihdr.dwMaxBytesPerSec;
  1091. fi.dwFlags = 0;
  1092. fi.dwStreams = pf->avihdr.dwStreams;
  1093. fi.dwSuggestedBufferSize = pf->avihdr.dwSuggestedBufferSize;
  1094. fi.dwWidth = pf->avihdr.dwWidth;
  1095. fi.dwHeight = pf->avihdr.dwHeight;
  1096. fi.dwScale = pf->avihdr.dwMicroSecPerFrame;
  1097. fi.dwRate = 1000000L;
  1098. fi.dwLength = pf->avihdr.dwTotalFrames;
  1099. fi.dwEditCount = 0;
  1100. LoadString(ghMod, IDS_AVIFILE, ach, sizeof(ach));
  1101. wsprintf(fi.szFileType, (LPSTR)ach);
  1102. hmemcpy(pfi, &fi, sizeof(fi));
  1103. return AVIERR_OK;
  1104. }
  1105. ///////////////////////////////////////////////////////////////////////////
  1106. //
  1107. // AVIFileClose()
  1108. //
  1109. // close a AVIFile stream
  1110. //
  1111. ///////////////////////////////////////////////////////////////////////////
  1112. STDMETHODIMP_(ULONG) CAVIStream::CUnknownImpl::Release()
  1113. {
  1114. CAVIStream FAR * pavi = m_pAVIStream;
  1115. uUseCount--;
  1116. if (m_refs < 20) {
  1117. DPF2("Stream %lx: Usage--=%lx\n", (DWORD) (LPVOID) this, m_refs - 1);
  1118. }
  1119. shfileRelease(pavi->hshfile);
  1120. if (!--m_refs) {
  1121. if (pavi->hshfile != pavi->pfile->hshfile) {
  1122. shfileClose(pavi->hshfile, 0);
  1123. pavi->hshfile = 0;
  1124. }
  1125. if (pavi->pb && pavi->pb != pavi->pfile->pb) {
  1126. EndBuffered(pavi->pb);
  1127. pavi->pb = 0;
  1128. }
  1129. if (pavi->psx) {
  1130. FreeStreamIndex(pavi->psx);
  1131. pavi->psx = NULL;
  1132. }
  1133. pavi->fInit = FALSE;
  1134. pavi->pfile->m_AVIFile.Release();
  1135. return 0;
  1136. }
  1137. return m_refs;
  1138. }
  1139. /* - - - - - - - - */
  1140. ///////////////////////////////////////////////////////////////////////////
  1141. ///////////////////////////////////////////////////////////////////////////
  1142. void CAVIStream::CAVIStreamImpl::ReadPalette(LONG lPos, LONG lPal, LPRGBQUAD prgb)
  1143. {
  1144. CAVIStream FAR * pavi = m_pAVIStream;
  1145. LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER) pavi->lpFormat;
  1146. LONG l;
  1147. int i,n;
  1148. static struct {
  1149. BYTE bFirstEntry; /* first entry to change */
  1150. BYTE bNumEntries; /* # entries to change (0 if 256) */
  1151. WORD wFlags; /* Mostly to preserve alignment... */
  1152. PALETTEENTRY peNew[256]; /* New color specifications */
  1153. } pc;
  1154. DPF("Reading palette: lPos = %ld, lPal = %ld\n", lPos, lPal);
  1155. if (lPal > lPos)
  1156. lPal = 0;
  1157. //
  1158. // get the palette colors in the initial format header
  1159. //
  1160. if (lPal <= 0) {
  1161. hmemcpy(prgb,(LPBYTE)lpbi+(int)lpbi->biSize, lpbi->biClrUsed * sizeof(RGBQUAD));
  1162. lPal = -1;
  1163. }
  1164. for (;;) {
  1165. //
  1166. // search index forward for next palette change
  1167. //
  1168. l = StreamFindSample(pavi->psx, lPal+1, FIND_FORMAT|FIND_NEXT);
  1169. if (l < 0 || l > lPos || l == lPal)
  1170. break;
  1171. lPal = l;
  1172. if (l <= (LONG) pavi->avistream.dwStart)
  1173. continue;
  1174. LONG off = StreamFindSample(pavi->psx, lPal, FIND_FORMAT|FIND_OFFSET);
  1175. LONG len = StreamFindSample(pavi->psx, lPal, FIND_FORMAT|FIND_LENGTH);
  1176. #ifdef DEBUG
  1177. DWORD adw[2];
  1178. shfileSeek(pavi->hshfile, off-8, SEEK_SET);
  1179. shfileRead(pavi->hshfile, (HPSTR)adw, sizeof(adw));
  1180. Assert(TWOCCFromFOURCC(adw[0]) == cktypePALchange);
  1181. Assert(adw[1] == (DWORD) len);
  1182. #endif
  1183. if (len > (LONG)sizeof(AVIPALCHANGE) + (LONG)lpbi->biClrUsed * sizeof(PALETTEENTRY) * 2) {
  1184. DPF("Palette chunk obviously too large!\n");
  1185. break;
  1186. }
  1187. //
  1188. // read palchange from file and apply it
  1189. //
  1190. shfileSeek(pavi->hshfile, off, SEEK_SET);
  1191. while (len >= sizeof(AVIPALCHANGE)) {
  1192. if (shfileRead(pavi->hshfile, (HPSTR)&pc, sizeof(AVIPALCHANGE)) !=
  1193. sizeof(AVIPALCHANGE)) {
  1194. DPF("Error reading palette change\n");
  1195. break;
  1196. }
  1197. n = pc.bNumEntries == 0 ? 256 : (int)pc.bNumEntries;
  1198. if ((DWORD) n > lpbi->biClrUsed) {
  1199. DPF("%d colors in palette change, only %lu in movie!\n", n, lpbi->biClrUsed);
  1200. break;
  1201. }
  1202. if (pc.bFirstEntry + n > (int)lpbi->biClrUsed) {
  1203. DPF("%d colors in palette change, only %lu in movie!\n", n, lpbi->biClrUsed);
  1204. break;
  1205. }
  1206. if (shfileRead(pavi->hshfile,
  1207. (HPSTR)&pc.peNew,
  1208. n * sizeof(PALETTEENTRY)) !=
  1209. (LONG) n * sizeof(PALETTEENTRY)) {
  1210. DPF("Error reading palette change entries\n");
  1211. break;
  1212. }
  1213. for (i=0; i<n; i++) {
  1214. pavi->argbq[pc.bFirstEntry+i].rgbRed = pc.peNew[i].peRed;
  1215. pavi->argbq[pc.bFirstEntry+i].rgbGreen = pc.peNew[i].peGreen;
  1216. pavi->argbq[pc.bFirstEntry+i].rgbBlue = pc.peNew[i].peBlue;
  1217. pavi->argbq[pc.bFirstEntry+i].rgbReserved = 0;
  1218. }
  1219. len -= n * sizeof(PALETTEENTRY) + sizeof(AVIPALCHANGE);
  1220. }
  1221. }
  1222. }
  1223. ///////////////////////////////////////////////////////////////////////////
  1224. ///////////////////////////////////////////////////////////////////////////
  1225. STDMETHODIMP CAVIStream::CAVIStreamImpl::ReadFormat(LONG lPos, LPVOID lpFormat, LONG FAR *lpcbFormat)
  1226. {
  1227. CAVIStream FAR * pavi = m_pAVIStream;
  1228. LONG lPal;
  1229. LPBITMAPINFOHEADER lpbi;
  1230. if (lpcbFormat == NULL)
  1231. return ResultFromScode(AVIERR_BADPARAM);
  1232. if (lpFormat == NULL || *lpcbFormat == 0) {
  1233. *lpcbFormat = pavi->cbFormat;
  1234. return AVIERR_OK;
  1235. }
  1236. if (pavi->avistream.dwFlags & AVISF_VIDEO_PALCHANGES) {
  1237. Assert(pavi->psx);
  1238. //
  1239. // now go find the nearest palette change
  1240. //
  1241. lPal = StreamFindSample(pavi->psx, lPos, FIND_FORMAT|FIND_PREV);
  1242. if (lPal < 0)
  1243. lPal = 0;
  1244. if (lPal != pavi->lPal) {
  1245. ReadPalette(lPal, pavi->lPal, pavi->argbq);
  1246. pavi->lPal = lPal;
  1247. }
  1248. lpbi = (LPBITMAPINFOHEADER) pavi->lpFormat;
  1249. hmemcpy(lpFormat, lpbi, min((LONG) lpbi->biSize, *lpcbFormat));
  1250. if (*lpcbFormat > (LONG) lpbi->biSize) {
  1251. hmemcpy((LPBYTE)lpFormat + (int)lpbi->biSize, pavi->argbq,
  1252. min(lpbi->biClrUsed * sizeof(RGBQUAD),
  1253. *lpcbFormat - lpbi->biSize));
  1254. }
  1255. }
  1256. else {
  1257. hmemcpy(lpFormat, pavi->lpFormat, min(*lpcbFormat, pavi->cbFormat));
  1258. }
  1259. if (*lpcbFormat < pavi->cbFormat) {
  1260. *lpcbFormat = pavi->cbFormat;
  1261. return ResultFromScode(AVIERR_BUFFERTOOSMALL);
  1262. }
  1263. *lpcbFormat = pavi->cbFormat;
  1264. return AVIERR_OK;
  1265. }
  1266. ///////////////////////////////////////////////////////////////////////////
  1267. ///////////////////////////////////////////////////////////////////////////
  1268. STDMETHODIMP CAVIStream::CAVIStreamImpl::Create(LONG lParam1, LONG lParam2)
  1269. {
  1270. return ResultFromScode(AVIERR_UNSUPPORTED);
  1271. }
  1272. ///////////////////////////////////////////////////////////////////////////
  1273. ///////////////////////////////////////////////////////////////////////////
  1274. STDMETHODIMP CAVIStream::CAVIStreamImpl::Info(AVISTREAMINFO FAR * psi, LONG lSize)
  1275. {
  1276. CAVIStream FAR * pavi = m_pAVIStream;
  1277. if (psi == NULL)
  1278. return ResultFromScode(AVIERR_BADPARAM);
  1279. if (lSize < sizeof(pavi->avistream))
  1280. return ResultFromScode(AVIERR_BUFFERTOOSMALL);
  1281. hmemcpy(psi, &pavi->avistream, sizeof(pavi->avistream));
  1282. return AVIERR_OK;
  1283. }
  1284. ///////////////////////////////////////////////////////////////////////////
  1285. ///////////////////////////////////////////////////////////////////////////
  1286. STDMETHODIMP_(LONG) CAVIStream::CAVIStreamImpl::FindSample(LONG lPos, LONG lFlags)
  1287. {
  1288. CAVIStream FAR * pavi = m_pAVIStream;
  1289. if (pavi->paviBase) {
  1290. // If we haven't copied over the data yet, delegate.
  1291. return AVIStreamFindSample(pavi->paviBase, lPos, lFlags);
  1292. }
  1293. if (lPos < (LONG)pavi->avistream.dwStart)
  1294. return -1;
  1295. if (lPos >= (LONG)(pavi->avistream.dwStart + pavi->avistream.dwLength))
  1296. return -1;
  1297. lPos = StreamFindSample(pavi->psx, lPos, (UINT)lFlags);
  1298. return lPos < 0 ? -1 : lPos;
  1299. }
  1300. ///////////////////////////////////////////////////////////////////////////
  1301. ///////////////////////////////////////////////////////////////////////////
  1302. STDMETHODIMP CAVIStream::CAVIStreamImpl::Read(
  1303. LONG lStart,
  1304. LONG lSamples,
  1305. LPVOID lpBuffer,
  1306. LONG cbBuffer,
  1307. LONG FAR * plBytes,
  1308. LONG FAR * plSamples)
  1309. {
  1310. CAVIStream FAR * pavi = m_pAVIStream;
  1311. LONG lBytes;
  1312. if (pavi->paviBase) {
  1313. // If we haven't copied over the data yet, delegate.
  1314. return AVIStreamRead(pavi->paviBase, lStart, lSamples,
  1315. lpBuffer, cbBuffer, plBytes, plSamples);
  1316. }
  1317. Assert(pavi->psx);
  1318. // !!! What if start too big? Length too long?
  1319. if (lStart < (LONG) pavi->avistream.dwStart) {
  1320. DPF("Read before start!\n");
  1321. return ResultFromScode(AVIERR_BADPARAM);
  1322. }
  1323. // Handle one of the sillier aspects of AVI files:
  1324. // Certain RLE-encoded files have their first frames split
  1325. // up into lots of small pieces. This code puts all of those
  1326. // pieces back together again if necessary.
  1327. if ((lStart == (LONG) pavi->avistream.dwStart) &&
  1328. (pavi->avistream.fccType == streamtypeVIDEO) &&
  1329. (pavi->avistream.dwInitialFrames > 0)) {
  1330. LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER) pavi->lpFormat;
  1331. LPVOID lp;
  1332. lStart -= (LONG) pavi->avistream.dwInitialFrames;
  1333. lBytes = (DWORD)(WORD)DIBWIDTHBYTES(lpbi) * (DWORD)(WORD)lpbi->biHeight;
  1334. //
  1335. // a NULL buffer means return the size buffer needed to read
  1336. // the given sample.
  1337. //
  1338. if (lpBuffer == NULL || cbBuffer == 0) {
  1339. if (plBytes)
  1340. *plBytes = lBytes;
  1341. return AVIERR_OK;
  1342. }
  1343. if (cbBuffer < lBytes) {
  1344. if (plBytes)
  1345. *plBytes = lBytes;
  1346. DPF("ReadFirst: Buffer is %ld bytes, needed %ld\n", cbBuffer, lBytes);
  1347. return ResultFromScode(AVIERR_BUFFERTOOSMALL);
  1348. }
  1349. lp = GlobalAllocPtr(GMEM_MOVEABLE, lBytes);
  1350. if (!lp)
  1351. return ResultFromScode(AVIERR_MEMORY);
  1352. while (lStart <= (LONG)pavi->avistream.dwStart) {
  1353. if (StreamRead(pavi->psx, lStart, 1, lp, lBytes) < 0) {
  1354. GlobalFreePtr(lp);
  1355. return ResultFromScode(AVIERR_FILEREAD);
  1356. }
  1357. // We probably shouldn't assume RLE here....
  1358. DecodeRle(lpbi, (BYTE _huge *) lpBuffer, (BYTE _huge *) lp);
  1359. lStart++;
  1360. }
  1361. GlobalFreePtr(lp);
  1362. goto done;
  1363. }
  1364. //
  1365. // do the read
  1366. //
  1367. lBytes = StreamRead(pavi->psx,lStart,lSamples,lpBuffer,cbBuffer);
  1368. //
  1369. // check for error
  1370. //
  1371. if (lBytes < 0) {
  1372. if (plBytes)
  1373. *plBytes = 0;
  1374. if (plSamples)
  1375. *plSamples = 0;
  1376. if (cbBuffer == 0)
  1377. return ResultFromScode(AVIERR_ERROR);
  1378. //
  1379. // the error may have been buffer too small, check this.
  1380. //
  1381. if (cbBuffer < pavi->psx->lSampleSize)
  1382. return ResultFromScode(AVIERR_BUFFERTOOSMALL);
  1383. lBytes = StreamFindSample(pavi->psx,lStart,FIND_PREV|FIND_LENGTH);
  1384. if (cbBuffer < lBytes) {
  1385. if (plBytes)
  1386. *plBytes = lBytes;
  1387. return ResultFromScode(AVIERR_BUFFERTOOSMALL);
  1388. }
  1389. else
  1390. return ResultFromScode(AVIERR_FILEREAD);
  1391. }
  1392. done:
  1393. if (plBytes)
  1394. *plBytes = lBytes;
  1395. if (plSamples) {
  1396. LONG lSampleSize = pavi->psx->lSampleSize;
  1397. if (lSampleSize)
  1398. *plSamples = lBytes / lSampleSize;
  1399. else
  1400. *plSamples = 1;
  1401. }
  1402. return AVIERR_OK;
  1403. }
  1404. /**************************************************************************
  1405. * @doc INTERNAL DRAWDIB
  1406. *
  1407. * @api BOOL | DibEq | This function compares two dibs.
  1408. *
  1409. * @parm LPBITMAPINFOHEADER lpbi1 | Pointer to one bitmap.
  1410. * this DIB is assumed to have the colors after the BITMAPINFOHEADER
  1411. *
  1412. * @parm LPBITMAPINFOHEADER | lpbi2 | Pointer to second bitmap.
  1413. * this DIB is assumed to have the colors after biSize bytes.
  1414. *
  1415. * @rdesc Returns TRUE if bitmaps are identical, FALSE otherwise.
  1416. *
  1417. **************************************************************************/
  1418. inline BOOL DibEq(LPBITMAPINFOHEADER lpbi1, LPBITMAPINFOHEADER lpbi2)
  1419. {
  1420. return
  1421. lpbi1->biCompression == lpbi2->biCompression &&
  1422. lpbi1->biSize == lpbi2->biSize &&
  1423. lpbi1->biWidth == lpbi2->biWidth &&
  1424. lpbi1->biHeight == lpbi2->biHeight &&
  1425. lpbi1->biBitCount == lpbi2->biBitCount;
  1426. }
  1427. ///////////////////////////////////////////////////////////////////////////
  1428. ///////////////////////////////////////////////////////////////////////////
  1429. STDMETHODIMP CAVIStream::CAVIStreamImpl::SetFormat(LONG lPos,LPVOID lpFormat,LONG cbFormat)
  1430. {
  1431. CAVIStream FAR * pavi = m_pAVIStream;
  1432. LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER) lpFormat;
  1433. int i;
  1434. RGBQUAD FAR * lprgb;
  1435. struct {
  1436. BYTE bFirstEntry; /* first entry to change */
  1437. BYTE bNumEntries; /* # entries to change (0 if 256) */
  1438. WORD wFlags; /* Mostly to preserve alignment... */
  1439. PALETTEENTRY pe[256];
  1440. } s;
  1441. //
  1442. // Make sure the stream isn't read-only
  1443. //
  1444. if ((pavi->pfile->mode & (OF_WRITE | OF_READWRITE)) == 0)
  1445. return ResultFromScode(AVIERR_READONLY);
  1446. if (pavi->lpFormat == NULL) {
  1447. // Check to see if the header will take up too much room!
  1448. if (pavi->pfile->lWriteLoc > 0) {
  1449. if ((pavi->cbFormat + pavi->pfile->lHeaderSize + 2 * sizeof(DWORD)) >
  1450. pavi->pfile->lWriteLoc) {
  1451. DPF("Header will be too big with this format!\n");
  1452. return ResultFromScode(AVIERR_UNSUPPORTED);
  1453. }
  1454. }
  1455. pavi->pfile->lHeaderSize += cbFormat + 2 * sizeof(DWORD);
  1456. // This is a new stream, whose format hasn't been set.
  1457. pavi->lpFormat = GlobalAllocPtr(GMEM_MOVEABLE | GMEM_SHARE, cbFormat);
  1458. if (!pavi->lpFormat) {
  1459. return ResultFromScode(AVIERR_MEMORY);
  1460. }
  1461. hmemcpy(pavi->lpFormat, lpFormat, cbFormat);
  1462. pavi->cbFormat = cbFormat;
  1463. if (pavi->avistream.fccType == streamtypeVIDEO) {
  1464. if (IsRectBogus(&pavi->avistream.rcFrame,
  1465. pavi->pfile->avihdr.dwWidth,
  1466. pavi->pfile->avihdr.dwHeight,
  1467. lpbi)) {
  1468. DPF("Resetting stream rectangle....\n");
  1469. SetRect(&pavi->avistream.rcFrame, 0, 0,
  1470. (int)lpbi->biWidth, (int)lpbi->biHeight);
  1471. }
  1472. if (lpbi->biClrUsed > 0) {
  1473. // Get the right colors, so that we can detect palette changes
  1474. hmemcpy(pavi->argbq,
  1475. (LPBYTE) lpbi + lpbi->biSize,
  1476. lpbi->biClrUsed * sizeof(RGBQUAD));
  1477. }
  1478. /* Make sure the width and height of the created file are right.... */
  1479. pavi->pfile->avihdr.dwWidth = max(pavi->pfile->avihdr.dwWidth,
  1480. (DWORD) pavi->avistream.rcFrame.right);
  1481. pavi->pfile->avihdr.dwHeight = max(pavi->pfile->avihdr.dwHeight,
  1482. (DWORD) pavi->avistream.rcFrame.bottom);
  1483. }
  1484. return 0;
  1485. }
  1486. //
  1487. // First, check if the format is actually different....
  1488. //
  1489. if (cbFormat == pavi->cbFormat &&
  1490. (_fmemcmp(pavi->lpFormat, lpFormat, (int) cbFormat) == 0))
  1491. return AVIERR_OK;
  1492. //
  1493. // We really only support format changes if they're palette changes...
  1494. //
  1495. if (pavi->avistream.fccType != streamtypeVIDEO) {
  1496. return ResultFromScode(AVIERR_UNSUPPORTED);
  1497. }
  1498. //
  1499. // Can only currently set the palette at the end of the file
  1500. //
  1501. if (lPos < (LONG) (pavi->avistream.dwStart + pavi->avistream.dwLength))
  1502. return ResultFromScode(AVIERR_UNSUPPORTED);
  1503. //
  1504. // We can only change the palette for things with palettes....
  1505. //
  1506. if (lpbi->biBitCount > 8 || lpbi->biClrUsed == 0)
  1507. return ResultFromScode(AVIERR_UNSUPPORTED);
  1508. //
  1509. // Be sure only the palette is changing, nothing else....
  1510. //
  1511. if (cbFormat != pavi->cbFormat)
  1512. return ResultFromScode(AVIERR_UNSUPPORTED);
  1513. if (!DibEq((LPBITMAPINFOHEADER) lpFormat,
  1514. (LPBITMAPINFOHEADER) pavi->lpFormat))
  1515. return ResultFromScode(AVIERR_UNSUPPORTED);
  1516. // !!! Need to do here:
  1517. // Get the correct palette for this point in the file, and check
  1518. // that the new palette is in fact different.
  1519. lprgb = (RGBQUAD FAR *) ((LPBYTE) lpbi + lpbi->biSize);
  1520. if (_fmemcmp(pavi->argbq, lprgb, (UINT) lpbi->biClrUsed * sizeof(RGBQUAD)) == 0)
  1521. return AVIERR_OK;
  1522. //
  1523. // Make the new format the current one....
  1524. //
  1525. hmemcpy(pavi->argbq, lprgb, lpbi->biClrUsed * sizeof(RGBQUAD));
  1526. pavi->lPal = lPos;
  1527. //
  1528. // And be sure the stream is marked as having changes...
  1529. //
  1530. pavi->avistream.dwFlags |= AVISF_VIDEO_PALCHANGES;
  1531. s.bFirstEntry = 0;
  1532. s.bNumEntries = (BYTE) lpbi->biClrUsed;
  1533. s.wFlags = 0;
  1534. for (i = 0; i < (int) lpbi->biClrUsed; i++, lprgb++) {
  1535. s.pe[i].peRed = lprgb->rgbRed;
  1536. s.pe[i].peGreen = lprgb->rgbGreen;
  1537. s.pe[i].peBlue = lprgb->rgbBlue;
  1538. }
  1539. // !!! Hack: use Write to write the palette change....
  1540. return Write(lPos,
  1541. 0,
  1542. &s,
  1543. sizeof(AVIPALCHANGE) + lpbi->biClrUsed * sizeof(PALETTEENTRY),
  1544. AVIIF_NOTIME, NULL, NULL);
  1545. }
  1546. ///////////////////////////////////////////////////////////////////////////
  1547. ///////////////////////////////////////////////////////////////////////////
  1548. STDMETHODIMP CAVIStream::CAVIStreamImpl::Write(LONG lStart,
  1549. LONG lSamples,
  1550. LPVOID lpData,
  1551. LONG cbData,
  1552. DWORD dwFlags,
  1553. LONG FAR *plSampWritten,
  1554. LONG FAR *plBytesWritten)
  1555. {
  1556. CAVIStream FAR * pavi = m_pAVIStream;
  1557. MMCKINFO ck;
  1558. WORD cktype;
  1559. HRESULT hr;
  1560. DWORD dwmsec;
  1561. // !!! Idea: if it's audio-like data, and everything else matches the
  1562. // last chunk written out, then merge the new data in with the old
  1563. // data, rather than making a new chunk....
  1564. if ((pavi->pfile->mode & (OF_WRITE | OF_READWRITE)) == 0)
  1565. return ResultFromScode(AVIERR_READONLY);
  1566. if (pavi->avistream.fccType == streamtypeAUDIO)
  1567. cktype = aviTWOCC('w', 'b');
  1568. else if (pavi->avistream.fccType == streamtypeVIDEO) {
  1569. if (dwFlags & AVIIF_NOTIME)
  1570. cktype = aviTWOCC('p', 'c');
  1571. else {
  1572. LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER) pavi->lpFormat;
  1573. if ((dwFlags & AVIIF_KEYFRAME) ||
  1574. (lpbi->biCompression <= BI_RLE8 && cbData == (LONG) lpbi->biSizeImage))
  1575. cktype = aviTWOCC('d', 'b');
  1576. else
  1577. cktype = aviTWOCC('d', 'c');
  1578. // !!! 00dx ack!
  1579. }
  1580. } else {
  1581. cktype = aviTWOCC('d', 'c');
  1582. }
  1583. ck.ckid = MAKEAVICKID(cktype, pavi->iStream);
  1584. ck.cksize = cbData;
  1585. if (lStart < 0)
  1586. lStart = pavi->avistream.dwStart + pavi->avistream.dwLength;
  1587. if (lStart > (LONG) (pavi->avistream.dwStart + pavi->avistream.dwLength)) {
  1588. if (pavi->avistream.dwSampleSize == 0) {
  1589. // !!! bad hack--insert lots of blank index entries....
  1590. while (lStart > (LONG) (pavi->avistream.dwStart + pavi->avistream.dwLength)) {
  1591. #if 1
  1592. hr = Write(pavi->avistream.dwStart + pavi->avistream.dwLength,
  1593. 1,
  1594. NULL,
  1595. 0,
  1596. 0,
  1597. NULL,
  1598. NULL);
  1599. if (FAILED(hr))
  1600. return hr;
  1601. #else
  1602. if (!AddToIndex(pavi->pfile, ck.ckid, 0, 0, 0))
  1603. return ResultFromScode(AVIERR_MEMORY);
  1604. ++pavi->avistream.dwLength;
  1605. pavi->pfile->avihdr.dwFlags |= AVIF_MUSTUSEINDEX;
  1606. #endif
  1607. }
  1608. } else
  1609. return ResultFromScode(AVIERR_BADPARAM);
  1610. }
  1611. if (lStart < (LONG) (pavi->avistream.dwStart + pavi->avistream.dwLength))
  1612. return ResultFromScode(AVIERR_UNSUPPORTED);
  1613. pavi->pfile->fDirty = TRUE;
  1614. if (pavi->pfile->lWriteLoc == 0) {
  1615. pavi->pfile->lWriteLoc = (pavi->pfile->lHeaderSize + 1024 + 2047) & ~(2047);
  1616. pavi->pfile->lDataListStart = pavi->pfile->lWriteLoc - 3 * sizeof(DWORD);
  1617. DPF("Writing first chunk at position %lu\n", pavi->pfile->lWriteLoc);
  1618. }
  1619. #if 0
  1620. if ((lStart == (LONG) (pavi->avistream.dwStart + pavi->avistream.dwLength)) &&
  1621. (pavi->avistream.fccType == streamtypeAUDIO) &&
  1622. (pavi->pfile->lIndex > 0)) {
  1623. AVIINDEXENTRY idx = pavi->pfile->pIndex[pavi->pfile->lIndex - 1];
  1624. if ((idx.ckid == ckid) &&
  1625. (idx.dwChunkOffset +
  1626. 2 * sizeof(DWORD) +
  1627. idx.dwChunkLength == lWriteLoc)) {
  1628. // We could append to the previous chunk here....
  1629. }
  1630. }
  1631. #endif
  1632. #ifdef DONTWRITEZEROLENGTH
  1633. if (cbData == 0) {
  1634. ck.dwDataOffset = 0;
  1635. pavi->pfile->avihdr.dwFlags |= AVIF_MUSTUSEINDEX;
  1636. } else
  1637. #endif
  1638. {
  1639. shfileSeek(pavi->hshfile, pavi->pfile->lWriteLoc, SEEK_SET);
  1640. shfileCreateChunk(pavi->hshfile, &ck, 0);
  1641. if (cbData) {
  1642. if (shfileWrite(pavi->hshfile, (HPSTR) lpData, cbData) != cbData)
  1643. return ResultFromScode(AVIERR_FILEWRITE);
  1644. }
  1645. if (shfileAscend(pavi->hshfile, &ck, 0) != 0)
  1646. return ResultFromScode(AVIERR_FILEWRITE);
  1647. pavi->pfile->lWriteLoc = shfileSeek(pavi->hshfile, 0, SEEK_CUR);
  1648. }
  1649. if (!AddToIndex(pavi->pfile, ck.ckid, cbData,
  1650. ck.dwDataOffset - 2 * sizeof(DWORD), dwFlags))
  1651. return ResultFromScode(AVIERR_MEMORY);
  1652. //
  1653. // if we dont have a stream index now is a good time to make one.
  1654. //
  1655. if (pavi->psx == NULL) {
  1656. pavi->psx = MakeStreamIndex(pavi->pfile->px, pavi->iStream,
  1657. (LONG)pavi->avistream.dwStart - pavi->avistream.dwInitialFrames,
  1658. (LONG)pavi->avistream.dwSampleSize,
  1659. pavi->pfile->hshfile, shfileReadProc, NULL);
  1660. //!!! what about pavi->pb
  1661. if (!(dwFlags & AVIIF_NOTIME))
  1662. pavi->psx->lEnd -= lSamples; // correct for the decrement below
  1663. }
  1664. if (pavi->psx == NULL) {
  1665. DPF("CAVIStream::Write no stream index!\n");
  1666. return ResultFromScode(AVIERR_MEMORY);
  1667. }
  1668. if (!(dwFlags & AVIIF_NOTIME)) {
  1669. pavi->avistream.dwLength += lSamples;
  1670. if (pavi->psx)
  1671. pavi->psx->lEnd += lSamples;
  1672. }
  1673. if (cbData > (LONG) pavi->avistream.dwSuggestedBufferSize)
  1674. pavi->avistream.dwSuggestedBufferSize = cbData;
  1675. if (cbData > (LONG) pavi->pfile->avihdr.dwSuggestedBufferSize)
  1676. pavi->pfile->avihdr.dwSuggestedBufferSize = cbData;
  1677. // Recalculate the overall file length....
  1678. dwmsec = muldiv32(pavi->avistream.dwLength,
  1679. pavi->avistream.dwScale * 1000L,
  1680. pavi->avistream.dwRate);
  1681. pavi->pfile->avihdr.dwTotalFrames =
  1682. max(pavi->pfile->avihdr.dwTotalFrames,
  1683. (DWORD) muldiv32(dwmsec, 1000L,
  1684. pavi->pfile->avihdr.dwMicroSecPerFrame));
  1685. // !!! The above calculation could easily overflow.
  1686. // !!! NEEDS TO BE REORGANIZED!
  1687. if (plBytesWritten)
  1688. *plBytesWritten = cbData;
  1689. if (plSampWritten)
  1690. *plSampWritten = lSamples;
  1691. return ResultFromScode(AVIERR_OK);
  1692. }
  1693. ///////////////////////////////////////////////////////////////////////////
  1694. ///////////////////////////////////////////////////////////////////////////
  1695. STDMETHODIMP CAVIStream::CAVIStreamImpl::Delete(LONG lStart,LONG lSamples)
  1696. {
  1697. CAVIStream FAR * pavi = m_pAVIStream;
  1698. if ((pavi->pfile->mode & (OF_WRITE | OF_READWRITE)) == 0)
  1699. return ResultFromScode(AVIERR_READONLY);
  1700. // go through and kill things from the index?
  1701. // !!! what about keyframe boundaries?
  1702. return ResultFromScode(AVIERR_UNSUPPORTED);
  1703. }
  1704. ///////////////////////////////////////////////////////////////////////////
  1705. ///////////////////////////////////////////////////////////////////////////
  1706. STDMETHODIMP CAVIStream::CAVIStreamImpl::ReadData(DWORD ckid, LPVOID lp, LONG FAR *lpcb)
  1707. {
  1708. CAVIStream FAR * pavi = m_pAVIStream;
  1709. return ReadExtra(&pavi->extra, ckid, lp, lpcb);
  1710. }
  1711. ///////////////////////////////////////////////////////////////////////////
  1712. ///////////////////////////////////////////////////////////////////////////
  1713. STDMETHODIMP CAVIStream::CAVIStreamImpl::WriteData(DWORD ckid, LPVOID lp, LONG cb)
  1714. {
  1715. CAVIStream FAR * pavi = m_pAVIStream;
  1716. // Check to see if the header will take up too much room!
  1717. if (pavi->pfile->lWriteLoc > 0) {
  1718. if ((cb + pavi->pfile->lHeaderSize + 2 * sizeof(DWORD)) >
  1719. pavi->pfile->lWriteLoc) {
  1720. DPF("Header will be too big with this extra data!\n");
  1721. return ResultFromScode(AVIERR_UNSUPPORTED);
  1722. }
  1723. }
  1724. pavi->pfile->lHeaderSize += cb + 3 * sizeof(DWORD);
  1725. if ((pavi->pfile->mode & (OF_WRITE | OF_READWRITE)) == 0)
  1726. return ResultFromScode(AVIERR_READONLY);
  1727. pavi->pfile->fDirty = TRUE;
  1728. return WriteExtra(&pavi->extra, ckid, lp, cb);
  1729. }
  1730. ///////////////////////////////////////////////////////////////////////////
  1731. ///////////////////////////////////////////////////////////////////////////
  1732. #if 0
  1733. STDMETHODIMP CAVIStream::CAVIStreamImpl::Clone(PAVISTREAM FAR * ppaviNew)
  1734. {
  1735. CAVIStream FAR * pavi = m_pAVIStream;
  1736. return ResultFromScode(AVIERR_UNSUPPORTED);
  1737. }
  1738. #endif
  1739. STDMETHODIMP CAVIStream::CStreamingImpl::Begin(LONG lStart,
  1740. LONG lEnd,
  1741. LONG lRate)
  1742. {
  1743. CAVIStream FAR * pavi = m_pAVIStream;
  1744. if (pavi->pb)
  1745. BeginBufferedStreaming(pavi->pb, lRate > 0);
  1746. return ResultFromScode(AVIERR_OK);
  1747. }
  1748. STDMETHODIMP CAVIStream::CStreamingImpl::End()
  1749. {
  1750. CAVIStream FAR * pavi = m_pAVIStream;
  1751. if (pavi->pb)
  1752. EndBufferedStreaming(pavi->pb);
  1753. return ResultFromScode(AVIERR_OK);
  1754. }
  1755. ///////////////////////////////////////////////////////////////////////////
  1756. ///////////////////////////////////////////////////////////////////////////
  1757. static
  1758. BOOL AddToIndex(CAVIFile FAR * pfile, DWORD ckid, DWORD cksize, LONG off, DWORD dwFlags)
  1759. {
  1760. PAVIINDEX px;
  1761. AVIINDEXENTRY idx;
  1762. idx.ckid = ckid;
  1763. idx.dwChunkOffset = off;
  1764. idx.dwChunkLength = cksize;
  1765. idx.dwFlags = dwFlags;
  1766. px = IndexAddFileIndex(pfile->px, &idx, 1, 0, FALSE);
  1767. if (px == NULL)
  1768. return FALSE;
  1769. //
  1770. // GlobalReAlloc may have moved our pointer, we need to patch all
  1771. // places we use it!
  1772. //
  1773. if (px != pfile->px) {
  1774. DPF("Index pointer has changed!\n");
  1775. pfile->px = px;
  1776. for (int i=0; i<(int)pfile->avihdr.dwStreams; i++) {
  1777. CAVIStream FAR *ps = pfile->ps[i];
  1778. if (ps->psx)
  1779. ps->psx->px = px;
  1780. }
  1781. if (pfile->pb)
  1782. pfile->pb->px = px;
  1783. }
  1784. return TRUE;
  1785. }
  1786. ///////////////////////////////////////////////////////////////////////////
  1787. ///////////////////////////////////////////////////////////////////////////
  1788. STDMETHODIMP CAVIFile::CAVIFileImpl::Reserved1(void)
  1789. {
  1790. return ResultFromScode(AVIERR_UNSUPPORTED);
  1791. }
  1792. ///////////////////////////////////////////////////////////////////////////
  1793. ///////////////////////////////////////////////////////////////////////////
  1794. STDMETHODIMP CAVIFile::CAVIFileImpl::Reserved2(void)
  1795. {
  1796. return ResultFromScode(AVIERR_UNSUPPORTED);
  1797. }
  1798. ///////////////////////////////////////////////////////////////////////////
  1799. ///////////////////////////////////////////////////////////////////////////
  1800. STDMETHODIMP CAVIFile::CAVIFileImpl::Reserved3(void)
  1801. {
  1802. return ResultFromScode(AVIERR_UNSUPPORTED);
  1803. }
  1804. ///////////////////////////////////////////////////////////////////////////
  1805. ///////////////////////////////////////////////////////////////////////////
  1806. STDMETHODIMP CAVIFile::CAVIFileImpl::Reserved4(void)
  1807. {
  1808. return ResultFromScode(AVIERR_UNSUPPORTED);
  1809. }
  1810. ///////////////////////////////////////////////////////////////////////////
  1811. ///////////////////////////////////////////////////////////////////////////
  1812. STDMETHODIMP CAVIFile::CAVIFileImpl::Reserved5(void)
  1813. {
  1814. return ResultFromScode(AVIERR_UNSUPPORTED);
  1815. }
  1816. ///////////////////////////////////////////////////////////////////////////
  1817. ///////////////////////////////////////////////////////////////////////////
  1818. STDMETHODIMP CAVIStream::CAVIStreamImpl::Reserved1(void)
  1819. {
  1820. return ResultFromScode(AVIERR_UNSUPPORTED);
  1821. }
  1822. ///////////////////////////////////////////////////////////////////////////
  1823. ///////////////////////////////////////////////////////////////////////////
  1824. STDMETHODIMP CAVIStream::CAVIStreamImpl::Reserved2(void)
  1825. {
  1826. return ResultFromScode(AVIERR_UNSUPPORTED);
  1827. }
  1828. ///////////////////////////////////////////////////////////////////////////
  1829. ///////////////////////////////////////////////////////////////////////////
  1830. STDMETHODIMP CAVIStream::CAVIStreamImpl::Reserved3(void)
  1831. {
  1832. return ResultFromScode(AVIERR_UNSUPPORTED);
  1833. }
  1834. ///////////////////////////////////////////////////////////////////////////
  1835. ///////////////////////////////////////////////////////////////////////////
  1836. STDMETHODIMP CAVIStream::CAVIStreamImpl::Reserved4(void)
  1837. {
  1838. return ResultFromScode(AVIERR_UNSUPPORTED);
  1839. }
  1840. ///////////////////////////////////////////////////////////////////////////
  1841. ///////////////////////////////////////////////////////////////////////////
  1842. STDMETHODIMP CAVIStream::CAVIStreamImpl::Reserved5(void)
  1843. {
  1844. return ResultFromScode(AVIERR_UNSUPPORTED);
  1845. }
  1846. CAVIFile::CMarshalImpl::CMarshalImpl(
  1847. CAVIFile FAR* pAVIFile)
  1848. {
  1849. m_pAVIFile = pAVIFile;
  1850. }
  1851. STDMETHODIMP CAVIFile::CMarshalImpl::QueryInterface(
  1852. const IID FAR& iid,
  1853. void FAR* FAR* ppv)
  1854. {
  1855. return m_pAVIFile->m_pUnknownOuter->QueryInterface(iid, ppv);
  1856. }
  1857. /* - - - - - - - - */
  1858. STDMETHODIMP_(ULONG) CAVIFile::CMarshalImpl::AddRef()
  1859. {
  1860. return m_pAVIFile->m_pUnknownOuter->AddRef();
  1861. }
  1862. /* - - - - - - - - */
  1863. STDMETHODIMP_(ULONG) CAVIFile::CMarshalImpl::Release()
  1864. {
  1865. return m_pAVIFile->m_pUnknownOuter->Release();
  1866. }
  1867. /* - - - - - - - - */
  1868. // *** IMarshal methods ***
  1869. STDMETHODIMP CAVIFile::CMarshalImpl::GetUnmarshalClass (THIS_ REFIID riid, LPVOID pv,
  1870. DWORD dwDestContext, LPVOID pvDestContext,
  1871. DWORD mshlflags, LPCLSID pCid)
  1872. {
  1873. HRESULT hr = NOERROR;
  1874. IUnknown FAR * pUnk = &m_pAVIFile->m_Unknown;
  1875. DPF("(F) UnMarshalClass called (context = %lx)\n", dwDestContext);
  1876. if (dwDestContext != MSHCTX_LOCAL) {
  1877. LPMARSHAL pMarshal;
  1878. DPF("Marshal context is %lu: delegating...\n", dwDestContext);
  1879. hr = CoGetStandardMarshal(riid, NULL,
  1880. dwDestContext, pvDestContext,
  1881. mshlflags, &pMarshal);
  1882. if (hr != NOERROR)
  1883. return hr;
  1884. hr = pMarshal->GetUnmarshalClass(riid, pv,
  1885. dwDestContext, pvDestContext,
  1886. mshlflags, pCid);
  1887. pMarshal->Release();
  1888. return hr;
  1889. }
  1890. *pCid = CLSID_AVISimpleUnMarshal;
  1891. return hr;
  1892. }
  1893. STDMETHODIMP CAVIFile::CMarshalImpl::GetMarshalSizeMax (THIS_ REFIID riid, LPVOID pv,
  1894. DWORD dwDestContext, LPVOID pvDestContext,
  1895. DWORD mshlflags, LPDWORD pSize)
  1896. {
  1897. HRESULT hr = NOERROR;
  1898. IUnknown FAR * pUnk = &m_pAVIFile->m_Unknown;
  1899. if (dwDestContext != MSHCTX_LOCAL) {
  1900. LPMARSHAL pMarshal;
  1901. hr = CoGetStandardMarshal(riid, NULL, dwDestContext, pvDestContext,
  1902. mshlflags, &pMarshal);
  1903. if (hr != NOERROR)
  1904. return hr;
  1905. hr = pMarshal->GetMarshalSizeMax(riid, pv,
  1906. dwDestContext, pvDestContext,
  1907. mshlflags, pSize);
  1908. pMarshal->Release();
  1909. return hr;
  1910. }
  1911. *pSize = sizeof(pUnk);
  1912. return hr;
  1913. }
  1914. STDMETHODIMP CAVIFile::CMarshalImpl::MarshalInterface (THIS_ LPSTREAM pStm, REFIID riid,
  1915. LPVOID pv, DWORD dwDestContext, LPVOID pvDestContext,
  1916. DWORD mshlflags)
  1917. {
  1918. HRESULT hr = NOERROR;
  1919. IUnknown FAR * pUnk = &m_pAVIFile->m_Unknown;
  1920. DPF("MarshalInterface (F) called\n");
  1921. if (dwDestContext != MSHCTX_LOCAL) {
  1922. LPMARSHAL pMarshal;
  1923. DPF("Marshal context is %lu: delegating...\n", dwDestContext);
  1924. hr = CoGetStandardMarshal(riid, NULL,
  1925. dwDestContext, pvDestContext,
  1926. mshlflags, &pMarshal);
  1927. if (hr != NOERROR)
  1928. return hr;
  1929. hr = pMarshal->MarshalInterface(pStm, riid, pv,
  1930. dwDestContext, pvDestContext,
  1931. mshlflags);
  1932. pMarshal->Release();
  1933. return hr;
  1934. }
  1935. if ((riid != IID_IAVIStream && riid != IID_IAVIFile && riid != IID_IUnknown))
  1936. return ResultFromScode(E_INVALIDARG);
  1937. if ((hr = pStm->Write(&pUnk, sizeof(pUnk), NULL)) == NOERROR)
  1938. AddRef();
  1939. DPF("Returns %lx\n", (DWORD) (LPVOID) hr);
  1940. return hr;
  1941. }
  1942. STDMETHODIMP CAVIFile::CMarshalImpl::UnmarshalInterface (THIS_ LPSTREAM pStm, REFIID riid,
  1943. LPVOID FAR* ppv)
  1944. {
  1945. HRESULT hr = ResultFromScode(E_FAIL);
  1946. DPF("(F) UnMarshalInterface called!!!\n");
  1947. return hr;
  1948. }
  1949. STDMETHODIMP CAVIFile::CMarshalImpl::ReleaseMarshalData (THIS_ LPSTREAM pStm)
  1950. {
  1951. HRESULT hr = NOERROR;
  1952. IUnknown FAR * pUnk;
  1953. hr = pStm->Read(&pUnk,sizeof(pUnk),NULL);
  1954. DPF("(F) ReleaseMarshalData\n");
  1955. if (hr == NOERROR)
  1956. pUnk->Release();
  1957. return hr;
  1958. }
  1959. STDMETHODIMP CAVIFile::CMarshalImpl::DisconnectObject (THIS_ DWORD dwReserved)
  1960. {
  1961. HRESULT hr = NOERROR;
  1962. return hr;
  1963. }
  1964. CAVIStream::CMarshalImpl::CMarshalImpl(
  1965. CAVIStream FAR* pAVIStream)
  1966. {
  1967. m_pAVIStream = pAVIStream;
  1968. }
  1969. STDMETHODIMP CAVIStream::CMarshalImpl::QueryInterface(
  1970. const IID FAR& iid,
  1971. void FAR* FAR* ppv)
  1972. {
  1973. return m_pAVIStream->m_pUnknownOuter->QueryInterface(iid, ppv);
  1974. }
  1975. /* - - - - - - - - */
  1976. STDMETHODIMP_(ULONG) CAVIStream::CMarshalImpl::AddRef()
  1977. {
  1978. return m_pAVIStream->m_pUnknownOuter->AddRef();
  1979. }
  1980. /* - - - - - - - - */
  1981. STDMETHODIMP_(ULONG) CAVIStream::CMarshalImpl::Release()
  1982. {
  1983. return m_pAVIStream->m_pUnknownOuter->Release();
  1984. }
  1985. /* - - - - - - - - */
  1986. // *** IMarshal methods ***
  1987. STDMETHODIMP CAVIStream::CMarshalImpl::GetUnmarshalClass (THIS_ REFIID riid, LPVOID pv,
  1988. DWORD dwDestContext, LPVOID pvDestContext,
  1989. DWORD mshlflags, LPCLSID pCid)
  1990. {
  1991. HRESULT hr = NOERROR;
  1992. IUnknown FAR * pUnk = &m_pAVIStream->m_Unknown;
  1993. DPF("(S) UnMarshalClass called (context = %lx)\n", dwDestContext);
  1994. *pCid = CLSID_AVISimpleUnMarshal;
  1995. return hr;
  1996. }
  1997. STDMETHODIMP CAVIStream::CMarshalImpl::GetMarshalSizeMax (THIS_ REFIID riid, LPVOID pv,
  1998. DWORD dwDestContext, LPVOID pvDestContext,
  1999. DWORD mshlflags, LPDWORD pSize)
  2000. {
  2001. HRESULT hr = NOERROR;
  2002. IUnknown FAR * pUnk = &m_pAVIStream->m_Unknown;
  2003. *pSize = sizeof(pUnk);
  2004. return hr;
  2005. }
  2006. STDMETHODIMP CAVIStream::CMarshalImpl::MarshalInterface (THIS_ LPSTREAM pStm, REFIID riid,
  2007. LPVOID pv, DWORD dwDestContext, LPVOID pvDestContext,
  2008. DWORD mshlflags)
  2009. {
  2010. HRESULT hr = NOERROR;
  2011. IUnknown FAR * pUnk = &m_pAVIStream->m_Unknown;
  2012. DPF("MarshalInterface (S) called\n");
  2013. if ((riid != IID_IAVIStream && riid != IID_IAVIStream && riid != IID_IUnknown))
  2014. return ResultFromScode(E_INVALIDARG);
  2015. if ((hr = pStm->Write(&pUnk, sizeof(pUnk), NULL)) == NOERROR)
  2016. AddRef();
  2017. DPF("Returns %lx\n", (DWORD) (LPVOID) hr);
  2018. return hr;
  2019. }
  2020. STDMETHODIMP CAVIStream::CMarshalImpl::UnmarshalInterface (THIS_ LPSTREAM pStm, REFIID riid,
  2021. LPVOID FAR* ppv)
  2022. {
  2023. HRESULT hr = ResultFromScode(E_FAIL);
  2024. DPF("(S) UnMarshalInterface called!!!\n");
  2025. return hr;
  2026. }
  2027. STDMETHODIMP CAVIStream::CMarshalImpl::ReleaseMarshalData (THIS_ LPSTREAM pStm)
  2028. {
  2029. HRESULT hr = NOERROR;
  2030. IUnknown FAR * pUnk;
  2031. hr = pStm->Read(&pUnk,sizeof(pUnk),NULL);
  2032. DPF("(S) ReleaseMarshalData\n");
  2033. if (hr == NOERROR)
  2034. pUnk->Release();
  2035. return hr;
  2036. }
  2037. STDMETHODIMP CAVIStream::CMarshalImpl::DisconnectObject (THIS_ DWORD dwReserved)
  2038. {
  2039. HRESULT hr = NOERROR;
  2040. return hr;
  2041. }
  2042. /***************************************************************************
  2043. DecodeRle - 'C' version
  2044. Play back a RLE buffer into a DIB buffer
  2045. returns
  2046. none
  2047. ***************************************************************************/
  2048. void DecodeRle(LPBITMAPINFOHEADER lpbi, BYTE _huge *pb, BYTE _huge *prle)
  2049. {
  2050. BYTE cnt;
  2051. BYTE b;
  2052. WORD x;
  2053. WORD dx,dy;
  2054. WORD wWidthBytes;
  2055. #define RLE_ESCAPE 0
  2056. #define RLE_EOL 0
  2057. #define RLE_EOF 1
  2058. #define RLE_JMP 2
  2059. #define RLE_RUN 3
  2060. #if 0
  2061. #ifndef WIN32
  2062. //
  2063. // this uses ASM code found in RLEA.ASM
  2064. //
  2065. if (!(WinFlags & WF_CPU286))
  2066. DecodeRle386(lpbi, pb, prle);
  2067. else if (lpbi->biSizeImage < 65536l)
  2068. DecodeRle286(lpbi, pb, prle);
  2069. else
  2070. #endif
  2071. #endif
  2072. {
  2073. wWidthBytes = (WORD)lpbi->biWidth+3 & ~3;
  2074. x = 0;
  2075. for(;;)
  2076. {
  2077. cnt = *prle++;
  2078. b = *prle++;
  2079. if (cnt == RLE_ESCAPE)
  2080. {
  2081. switch (b)
  2082. {
  2083. case RLE_EOF:
  2084. return;
  2085. case RLE_EOL:
  2086. pb += wWidthBytes - x;
  2087. x = 0;
  2088. break;
  2089. case RLE_JMP:
  2090. dx = (WORD)*prle++;
  2091. dy = (WORD)*prle++;
  2092. pb += (DWORD)wWidthBytes * dy + dx;
  2093. x += dx;
  2094. break;
  2095. default:
  2096. cnt = b;
  2097. x += cnt;
  2098. while (cnt-- > 0)
  2099. *pb++ = *prle++;
  2100. if (b & 1)
  2101. prle++;
  2102. break;
  2103. }
  2104. }
  2105. else
  2106. {
  2107. x += cnt;
  2108. while (cnt-- > 0)
  2109. *pb++ = b;
  2110. }
  2111. }
  2112. }
  2113. }