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.

3870 lines
107 KiB

  1. /******************************************************************************
  2. Copyright (C) Microsoft Corporation 1985-1995. All rights reserved.
  3. Title: aviplay.c - Code for actually playing AVI files, part of
  4. AVI's background task.
  5. *****************************************************************************/
  6. #include "graphic.h"
  7. #define AVIREADMANY // read more than one record at a time
  8. #ifdef _WIN32
  9. //#define AVIREAD // multi-threaded async read of file
  10. #else
  11. #undef AVIREAD
  12. #endif
  13. #ifdef AVIREAD
  14. #include "aviread.h"
  15. #endif
  16. #define BOUND(x, low, high) max(min(x, high), low)
  17. #define ALIGNULONG(i) ((i+3)&(~3)) /* ULONG aligned ! */
  18. #ifdef INTERVAL_TIMES
  19. BOOL fOneIntervalPerLine=FALSE;
  20. #endif
  21. //
  22. // redefine StreamFromFOURCC to only handle 0-9 streams!
  23. //
  24. #undef StreamFromFOURCC
  25. #define StreamFromFOURCC(fcc) (UINT)(HIBYTE(LOWORD(fcc)) - (BYTE)'0')
  26. #ifdef DEBUG
  27. static char szBadFrame[] = "Bad frame number";
  28. static char szBadPos[] = "Bad stream position";
  29. #define AssertFrame(i) AssertSz((long)(i) <= npMCI->lFrames && (long)(i) >= -(long)npMCI->wEarlyRecords, szBadFrame)
  30. #define AssertPos(psi,i) AssertSz((long)(i) <= psi->lEnd && (long)(i) >= psi->lStart, szBadPos)
  31. #else
  32. #define AssertFrame(i)
  33. #define AssertPos(psi,i)
  34. #endif
  35. #define WIDTHBYTES(i) ((unsigned)((i+31)&(~31))/8) /* ULONG aligned ! */
  36. #define DIBWIDTHBYTES(bi) (DWORD)WIDTHBYTES((int)(bi).biWidth * (int)(bi).biBitCount)
  37. LONG NEAR PASCAL WhatFrameIsItTimeFor(NPMCIGRAPHIC npMCI);
  38. LONG NEAR PASCAL HowLongTill(NPMCIGRAPHIC npMCI);
  39. BOOL NEAR PASCAL TimeToQuit(NPMCIGRAPHIC npMCI);
  40. BOOL NEAR PASCAL WaitTillNextFrame(NPMCIGRAPHIC npMCI);
  41. void NEAR PASCAL FindKeyFrame(NPMCIGRAPHIC npMCI, STREAMINFO *psi, LONG lPos);
  42. LONG NEAR PASCAL FindPrevKeyFrame(NPMCIGRAPHIC npMCI, STREAMINFO *psi, LONG lPos);
  43. LONG NEAR PASCAL FindNextKeyFrame(NPMCIGRAPHIC npMCI, STREAMINFO *psi, LONG lPos);
  44. BOOL NEAR PASCAL CalculateTargetFrame(NPMCIGRAPHIC npMCI);
  45. DWORD NEAR PASCAL CalculatePosition(NPMCIGRAPHIC npMCI);
  46. BOOL NEAR PASCAL ReadRecord(NPMCIGRAPHIC npMCI);
  47. BOOL NEAR PASCAL ReadNextVideoFrame(NPMCIGRAPHIC npMCI, STREAMINFO *psi);
  48. STATICFN INLINE DWORD NEAR PASCAL ReadNextChunk(NPMCIGRAPHIC npMCI);
  49. BOOL NEAR PASCAL ReadBuffer(NPMCIGRAPHIC npMCI, LONG off, LONG len);
  50. BOOL NEAR PASCAL AllocateReadBuffer(NPMCIGRAPHIC npMCI);
  51. BOOL NEAR PASCAL ResizeReadBuffer(NPMCIGRAPHIC npMCI, DWORD dwNewSize);
  52. void NEAR PASCAL ReleaseReadBuffer(NPMCIGRAPHIC npMCI);
  53. BOOL NEAR PASCAL ProcessPaletteChanges(NPMCIGRAPHIC npMCI, LONG lFrame);
  54. STATICFN INLINE void DealWithOtherStreams(NPMCIGRAPHIC npMCI, LONG lFrame);
  55. STATICFN INLINE BOOL NEAR PASCAL StreamRead(NPMCIGRAPHIC npMCI, STREAMINFO *psi, LONG lPos);
  56. #ifdef _WIN32
  57. #define AllocMem(dw) GlobalLock(GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE, (dw)))
  58. #define FreeMem(lp) GlobalFreePtr(lp)
  59. #else
  60. static LPVOID AllocMem(DWORD dw);
  61. #define FreeMem(lp) GlobalFree((HGLOBAL)SELECTOROF(lp))
  62. #endif
  63. INT gwSkipTolerance = 4;
  64. INT gwHurryTolerance = 2;
  65. INT gwMaxSkipEver = 60;
  66. #define YIELDEVERY 8
  67. #ifdef DEBUG
  68. #define WAITHISTOGRAM /* Extra debugging information */
  69. #define SHOWSKIPPED
  70. //#define BEHINDHIST
  71. #define DRAWTIMEHIST
  72. #define READTIMEHIST
  73. #define TIMEPLAY
  74. #endif
  75. #ifdef WAITHISTOGRAM
  76. UINT wHist[100];
  77. #endif
  78. #define HIGHRESTIMER
  79. #ifdef SHOWSKIPPED
  80. #define NUMSKIPSSHOWN 25
  81. LONG lSkipped[NUMSKIPSSHOWN];
  82. UINT wSkipped = 0;
  83. #endif
  84. #ifdef BEHINDHIST
  85. #define NUMBEHIND 50
  86. #define BEHINDOFFSET 10
  87. WORD wBehind[NUMBEHIND];
  88. #endif
  89. #ifdef DRAWTIMEHIST
  90. #define NUMDRAWN 100
  91. DWORD dwDrawTime[NUMDRAWN];
  92. UINT wDrawn;
  93. #endif
  94. #ifdef READTIMEHIST
  95. #define NUMREAD 100
  96. DWORD dwReadTime[NUMREAD];
  97. UINT wRead;
  98. #endif
  99. // If available, use a correctly functioning waveOutGetPosition
  100. BOOL gfUseGetPosition;
  101. LONG giGetPositionAdjust;
  102. #ifdef AVIREAD
  103. /*
  104. * the aviread object creates a worker thread to read the file
  105. * asynchronously. That thread calls this callback function
  106. * to actually read a buffer from the file. The 'instance data' DWORD in
  107. * this case is npMCI. see aviread.h for outline.
  108. */
  109. BOOL mciaviReadBuffer(PBYTE pData, DWORD_PTR dwInstanceData, long lSize, long * lpNextSize)
  110. {
  111. NPMCIGRAPHIC npMCI = (NPMCIGRAPHIC) dwInstanceData;
  112. DWORD size;
  113. DWORD UNALIGNED * lp;
  114. if(mmioRead(npMCI->hmmio, pData, lSize) != lSize) {
  115. return(FALSE);
  116. }
  117. /* we've read in the complete chunk, plus the FOURCC, size and formtype of
  118. * the next chunk. So the size of the next chunk is the last but one
  119. * DWORD in this buffer
  120. */
  121. lp = (DWORD UNALIGNED *) (pData + lSize - 2 * sizeof(DWORD));
  122. size = *lp;
  123. /* don't forget to add on the FOURCC and size dwords */
  124. *lpNextSize = size + 2 * sizeof(DWORD);
  125. return(TRUE);
  126. }
  127. #endif
  128. //
  129. // call this to find out the current position. This function
  130. // should be safe to call from the user thread as well as from the
  131. // worker thread
  132. DWORD InternalGetPosition(NPMCIGRAPHIC npMCI, LPLONG lpl)
  133. {
  134. LONG l;
  135. l = npMCI->lCurrentFrame - npMCI->dwBufferedVideo;
  136. if ((npMCI->wTaskState == TASKCUEING) &&
  137. !(npMCI->dwFlags & MCIAVI_SEEKING) &&
  138. l < npMCI->lRealStart)
  139. l = npMCI->lRealStart;
  140. if (l < 0)
  141. l = 0;
  142. *lpl = l;
  143. return 0L;
  144. }
  145. STATICFN DWORD NEAR PASCAL PrepareToPlay(NPMCIGRAPHIC npMCI);
  146. STATICFN DWORD NEAR PASCAL PrepareToPlay2(NPMCIGRAPHIC npMCI);
  147. void NEAR PASCAL CleanUpPlay(NPMCIGRAPHIC npMCI);
  148. void NEAR PASCAL CheckSignals(NPMCIGRAPHIC npMCI, LONG lFrame);
  149. BOOL NEAR PASCAL PlayNonInterleaved(NPMCIGRAPHIC npMCI);
  150. BOOL NEAR PASCAL PlayInterleaved(NPMCIGRAPHIC npMCI);
  151. BOOL NEAR PASCAL PlayAudioOnly(NPMCIGRAPHIC npMCI);
  152. BOOL NEAR PASCAL PlayNonIntFromCD(NPMCIGRAPHIC npMCI);
  153. /***************************************************************************
  154. *
  155. * @doc INTERNAL MCIAVI
  156. *
  157. * @api UINT | mciaviPlayFile | Play an AVI file.
  158. *
  159. * @parm NPMCIGRAPHIC | npMCI | Pointer to instance data.
  160. *
  161. * @rdesc Notification code that should be returned.
  162. *
  163. ***************************************************************************/
  164. UINT NEAR PASCAL mciaviPlayFile (NPMCIGRAPHIC npMCI, BOOL bSetEvent)
  165. {
  166. BOOL fContinue;
  167. DWORD dwFlags = npMCI->dwFlags;
  168. BOOL (NEAR PASCAL *Play)(NPMCIGRAPHIC npMCI);
  169. #ifdef WAITHISTOGRAM
  170. UINT w;
  171. #endif
  172. #ifdef SHOWSKIPPED
  173. wSkipped = 0;
  174. #endif
  175. #ifdef WAITHISTOGRAM
  176. //for (w = 0; (int)w < (sizeof(wHist)/sizeof(wHist[0])); w++)
  177. //wHist[w] = 0;
  178. ZeroMemory(wHist, sizeof(wHist));
  179. #endif
  180. #ifdef BEHINDHIST
  181. //for (w = 0; w < NUMBEHIND; w++)
  182. //wBehind[w] = 0;
  183. ZeroMemory(wBehind, sizeof(wBehind));
  184. #endif
  185. #ifdef DRAWTIMEHIST
  186. wDrawn = 0;
  187. //for (w = 0; w < NUMDRAWN; w++)
  188. //dwDrawTime[w] = 0;
  189. ZeroMemory(dwDrawTime, sizeof(dwDrawTime));
  190. #endif
  191. #ifdef READTIMEHIST
  192. wRead = 0;
  193. //for (w = 0; w < NUMREAD; w++)
  194. //dwReadTime[w] = 0;
  195. ZeroMemory(dwReadTime, sizeof(dwReadTime));
  196. #endif
  197. #ifdef HIGHRESTIMER
  198. /* Figure out how much time each frame takes */
  199. /* Then set a high resolution timer, unless */
  200. /* we are in the special "play every frame". */
  201. if (npMCI->dwSpeedFactor) {
  202. // Only if we are worried about timing each frame...
  203. // Set a timer resolution for a fraction of the frame rate
  204. // Initially we set the timer to 4ms. This initial guess can
  205. // be overridden by a registry setting.
  206. // Note: there is no UI (yet?) for writing a value to the registry
  207. if ((npMCI->msPeriodResolution = mmGetProfileInt(szIni, TEXT("TimerResolution"), 4))
  208. && (0 == timeBeginPeriod(npMCI->msPeriodResolution))) {
  209. // Timer successfully set
  210. DPF1(("Set timer resolution to %d milliseconds\n", npMCI->msPeriodResolution));
  211. } else {
  212. TIMECAPS tc;
  213. if ((timeGetDevCaps(&tc, sizeof(tc)) == 0) &&
  214. (0 == timeBeginPeriod(tc.wPeriodMin))) {
  215. npMCI->msPeriodResolution = tc.wPeriodMin;
  216. DPF1(("Set timer resolution to the minimum of %d milliseconds\n", npMCI->msPeriodResolution));
  217. } else {
  218. // Reset, so we do not try and call timeEndPeriod
  219. // (note: the value may have been set by mmGetProfileInt)
  220. npMCI->msPeriodResolution = 0;
  221. DPF1(("NO high resolution timer set\n"));
  222. }
  223. }
  224. }
  225. #endif
  226. Repeat:
  227. // internal task state (used only by worker thread to
  228. // distinguish cueing/seeking/playing)
  229. npMCI->wTaskState = TASKSTARTING;
  230. TIMEZERO(timePlay);
  231. TIMEZERO(timePrepare);
  232. TIMEZERO(timeCleanup);
  233. TIMEZERO(timePaused);
  234. TIMEZERO(timeRead);
  235. TIMEZERO(timeWait);
  236. TIMEZERO(timeYield);
  237. TIMEZERO(timeVideo);
  238. TIMEZERO(timeOther);
  239. TIMEZERO(timeAudio);
  240. TIMEZERO(timeDraw);
  241. TIMEZERO(timeDecompress);
  242. TIMESTART(timePrepare);
  243. npMCI->dwTaskError = PrepareToPlay(npMCI);
  244. TIMEEND(timePrepare);
  245. #ifdef INTERVAL_TIMES
  246. // interframe timing
  247. npMCI->nFrames = 0;
  248. npMCI->msFrameTotal = 0;
  249. npMCI->msSquares = 0;
  250. npMCI->msFrameMax = 0;
  251. npMCI->msFrameMin = 9999;
  252. npMCI->msReadMaxBytesPer = 0;
  253. npMCI->msReadMax = 0;
  254. npMCI->nReads = 0;
  255. npMCI->msReadTotal = 0;
  256. npMCI->msReadTimeuS = 0;
  257. {
  258. int i;
  259. PLONG pL;
  260. //for (i = 0; i < NBUCKETS; i++) {
  261. // npMCI->buckets[i] = 0;
  262. //}
  263. ZeroMemory(npMCI->buckets, NBUCKETS*sizeof(LONG));
  264. pL = npMCI->paIntervals;
  265. if (pL) {
  266. ZeroMemory(pL, (npMCI->cIntervals)*sizeof(LONG));
  267. }
  268. }
  269. #endif
  270. //
  271. // pick a play function.
  272. //
  273. switch (npMCI->wPlaybackAlg) {
  274. case MCIAVI_ALG_INTERLEAVED:
  275. Play = PlayInterleaved;
  276. break;
  277. #ifdef USENONINTFROMCD
  278. case MCIAVI_ALG_CDROM:
  279. Play = PlayNonIntFromCD;
  280. break;
  281. #endif
  282. case MCIAVI_ALG_HARDDISK:
  283. Play = PlayNonInterleaved;
  284. break;
  285. case MCIAVI_ALG_AUDIOONLY:
  286. Play = PlayAudioOnly;
  287. break;
  288. default:
  289. Assert(0);
  290. return MCI_NOTIFY_ABORTED; //???
  291. }
  292. // bias lTo by dwBufferedVideo so we play to the right place
  293. npMCI->lTo += npMCI->dwBufferedVideo;
  294. npMCI->lFramePlayStart = npMCI->lRealStart;
  295. DPF(("Playing from %ld to %ld, starting at %ld. Flags=%0.8x\n", npMCI->lFrom, npMCI->lTo, npMCI->lCurrentFrame, npMCI->dwFlags));
  296. // at this point we have detected any errors that we are going to detect
  297. // during startup. we can complete the synchronous portion of the command
  298. // if requested to do so
  299. if (bSetEvent) {
  300. bSetEvent = FALSE;
  301. TaskReturns(npMCI, npMCI->dwTaskError);
  302. }
  303. if (npMCI->dwTaskError != 0L)
  304. goto SKIP_PLAYING;
  305. /* We're done initializing; now we're warming up to play. */
  306. npMCI->wTaskState = TASKCUEING;
  307. TIMESTART(timePlay);
  308. /* Loop until things are done */
  309. while (1) {
  310. if (npMCI->dwFlags & MCIAVI_REVERSE) {
  311. if (npMCI->lCurrentFrame < npMCI->lTo)
  312. break;
  313. } else {
  314. if (npMCI->lCurrentFrame > npMCI->lTo)
  315. break;
  316. }
  317. if ((npMCI->wTaskState != TASKPLAYING) &&
  318. !(npMCI->dwFlags & MCIAVI_UPDATING)) {
  319. TIMESTART(timeYield);
  320. aviTaskCheckRequests(npMCI);
  321. TIMEEND(timeYield);
  322. }
  323. fContinue = Play(npMCI);
  324. if (fContinue) fContinue = !TimeToQuit(npMCI);
  325. if (!fContinue)
  326. break;
  327. //
  328. // while playing we may need to update
  329. //
  330. // always mark the movie as clean, even if a stream fails to update
  331. // otherwise we will need to stop play and restart.
  332. //
  333. if (!(npMCI->dwFlags & MCIAVI_SEEKING) &&
  334. (npMCI->dwFlags & MCIAVI_NEEDUPDATE)) {
  335. DoStreamUpdate(npMCI, FALSE);
  336. if (npMCI->dwFlags & MCIAVI_NEEDUPDATE) {
  337. DOUT("Update failed while playing, I dont care!\n");
  338. npMCI->dwFlags &= ~MCIAVI_NEEDUPDATE; //!!! I dont care if it failed
  339. }
  340. }
  341. /* Increment the frame number. If we're done, don't increment
  342. ** it an extra time, but just get out.
  343. */
  344. if (npMCI->dwFlags & MCIAVI_REVERSE) {
  345. if (npMCI->lCurrentFrame > npMCI->lTo)
  346. --npMCI->lCurrentFrame;
  347. else
  348. break;
  349. } else {
  350. if (npMCI->lCurrentFrame < npMCI->lTo)
  351. ++npMCI->lCurrentFrame;
  352. else {
  353. // need to ensure that we wait for audio to complete on the
  354. // last frame played
  355. if ((npMCI->lFrom != npMCI->lTo) &&
  356. (npMCI->wTaskState == TASKPLAYING)) {
  357. npMCI->lCurrentFrame++;
  358. WaitTillNextFrame(npMCI);
  359. npMCI->lCurrentFrame--;
  360. }
  361. break;
  362. }
  363. }
  364. }
  365. TIMEEND(timePlay);
  366. if (npMCI->lCurrentFrame != npMCI->lTo) {
  367. DPF(("Ended at %ld, not %ld (drawn = %ld).\n", npMCI->lCurrentFrame, npMCI->lTo, npMCI->lFrameDrawn));
  368. //
  369. // if we ended early lets set lCurrentFrame to the last frame
  370. // drawn to guarantee we can re-paint the frame, we dont
  371. // want to do this when we play to end because after playing
  372. // from A to B the current position *must* be B or preston will
  373. // enter a bug.
  374. //
  375. // but only set this if lFrameDraw is valid
  376. //
  377. if (npMCI->lFrameDrawn > (-(LONG)npMCI->wEarlyRecords))
  378. npMCI->lCurrentFrame = npMCI->lFrameDrawn;
  379. }
  380. SKIP_PLAYING:
  381. /* Flush any extra changes out to screen */
  382. DPF2(("Updating unfinished changes....\n"));
  383. // Make sure we really draw.... !!!do we need this?
  384. // npMCI->lRealStart = npMCI->lCurrentFrame;
  385. if (npMCI->hdc)
  386. DoStreamUpdate(npMCI, FALSE);
  387. npMCI->lTo -= npMCI->dwBufferedVideo;
  388. npMCI->lCurrentFrame -= npMCI->dwBufferedVideo;
  389. npMCI->dwBufferedVideo = 0;
  390. if (npMCI->lCurrentFrame < 0) {
  391. DPF2(("Adjusting position to be >= 0.\n"));
  392. npMCI->lCurrentFrame = 0;
  393. }
  394. if (npMCI->lTo < 0)
  395. npMCI->lTo = 0;
  396. /* Adjust position to be > start? */
  397. /* Adjust position to be > where it was when we began? */
  398. npMCI->dwTotalMSec += Now() - npMCI->dwMSecPlayStart;
  399. TIMESTART(timeCleanup);
  400. DPF(("Cleaning up the play\n"));
  401. CleanUpPlay(npMCI);
  402. TIMEEND(timeCleanup);
  403. #ifdef AVIREAD
  404. /* shut down async reader */
  405. if (npMCI->hAviRd) {
  406. avird_endread(npMCI->hAviRd);
  407. npMCI->hAviRd = NULL;
  408. }
  409. #endif
  410. /* If we're repeating, do it. It sure would be nice if we could repeat
  411. ** without de-allocating and then re-allocating all of our buffers....
  412. */
  413. if (npMCI->dwTaskError == 0 && (!(npMCI->dwFlags & MCIAVI_STOP)) &&
  414. (npMCI->dwFlags & MCIAVI_REPEATING)) {
  415. npMCI->lFrom = npMCI->lRepeatFrom;
  416. //
  417. // DrawEnd() likes to clear this flag so make sure it gets set
  418. // in the repeat case.
  419. //
  420. if (dwFlags & MCIAVI_FULLSCREEN)
  421. npMCI->dwFlags |= MCIAVI_FULLSCREEN;
  422. //
  423. // make sure we set the task state back before we repeat.
  424. // otherwise our code will think we are playing, for example.
  425. // if the audio code thinks we are playing and see's the wave buffers
  426. // are empty it will reset the wave device then restart it when
  427. // they get full again, this is bad if we are pre-rolling audio.
  428. //
  429. npMCI->wTaskState = TASKSTARTING;
  430. DPF((".........repeating\n"));
  431. goto Repeat;
  432. }
  433. /* Turn off flags only used during play. */
  434. npMCI->dwFlags &= ~(MCIAVI_STOP | MCIAVI_PAUSE | MCIAVI_SEEKING |
  435. MCIAVI_REPEATING | MCIAVI_FULLSCREEN);
  436. if (npMCI->wTaskState == TASKPLAYING) {
  437. DWORD dwCorrectTime;
  438. DWORD dwFramesPlayed;
  439. dwFramesPlayed = (npMCI->dwFlags & MCIAVI_REVERSE) ?
  440. npMCI->lFramePlayStart - npMCI->lCurrentFrame :
  441. npMCI->lCurrentFrame - npMCI->lFramePlayStart;
  442. dwCorrectTime = muldiv32(dwFramesPlayed,
  443. muldiv32(npMCI->dwMicroSecPerFrame,
  444. 1000L,
  445. (npMCI->dwSpeedFactor == 0 ?
  446. 1000 : npMCI->dwSpeedFactor)),
  447. 1000);
  448. if (dwCorrectTime != 0 && npMCI->dwTotalMSec != 0)
  449. npMCI->dwSpeedPercentage = muldiv32(dwCorrectTime, 100,
  450. npMCI->dwTotalMSec);
  451. else
  452. npMCI->dwSpeedPercentage = 100;
  453. if (dwFramesPlayed > 15) {
  454. npMCI->lFramesPlayed = (LONG)dwFramesPlayed;
  455. npMCI->lFramesSeekedPast = (LONG)npMCI->dwFramesSeekedPast;
  456. npMCI->lSkippedFrames = (LONG)npMCI->dwSkippedFrames;
  457. npMCI->lAudioBreaks = (LONG)npMCI->dwAudioBreaks;
  458. }
  459. #ifdef DEBUG
  460. if (npMCI->dwFramesSeekedPast) {
  461. DPF(("Didn't even read %ld frames.\n", npMCI->dwFramesSeekedPast));
  462. }
  463. if (npMCI->dwSkippedFrames && dwFramesPlayed > 0) {
  464. DPF(("Skipped %ld of %ld frames. (%ld%%)\n",
  465. npMCI->dwSkippedFrames, dwFramesPlayed,
  466. npMCI->dwSkippedFrames*100/dwFramesPlayed));
  467. }
  468. if (npMCI->dwAudioBreaks) {
  469. DPF(("Audio broke up %lu times.\n", npMCI->dwAudioBreaks));
  470. }
  471. #ifndef TIMEPLAY
  472. DPF(("Played at %lu%% of correct speed.\n", npMCI->dwSpeedPercentage));
  473. DPF(("Correct time = %lu ms, Actual = %lu ms.\n",
  474. dwCorrectTime, npMCI->dwTotalMSec));
  475. #endif
  476. #endif
  477. // don't print pages of stuff when we've just stopped temporarily,
  478. // it gets on my nerves.
  479. if (! (npMCI->dwFlags & MCIAVI_UPDATING)) {
  480. #ifdef DEBUG
  481. extern int giDebugLevel, giTimingLevel;
  482. int oldDebugLevel = giDebugLevel;
  483. giDebugLevel = max(giTimingLevel, oldDebugLevel);
  484. #endif
  485. #ifdef SHOWSKIPPED
  486. if (wSkipped) {
  487. DPF(("Skipped: "));
  488. for (w = 0; w < wSkipped; w++) {
  489. DPF(("!%ld ", lSkipped[w]));
  490. }
  491. DPF(("!\n"));
  492. }
  493. #endif
  494. #ifdef WAITHISTOGRAM
  495. if (npMCI->wPlaybackAlg == MCIAVI_ALG_INTERLEAVED) {
  496. DPF(("Wait histogram: "));
  497. for (w = 0; (int)w <= (int)npMCI->wABs; w++) {
  498. if (wHist[w]) {
  499. DPF(("![%d]: %d ",w,wHist[w]));
  500. }
  501. }
  502. DPF(("!\n"));
  503. }
  504. #endif
  505. #ifdef BEHINDHIST
  506. DPF(("Behind histogram: "));
  507. for (w = 0; w <= NUMBEHIND; w++) {
  508. if (wBehind[w]) {
  509. DPF(("![%d]: %d ",w - BEHINDOFFSET,wBehind[w]));
  510. }
  511. }
  512. DPF(("!\n"));
  513. #endif
  514. #ifdef DRAWTIMEHIST
  515. DPF(("Draw times: "));
  516. for (w = 0; w < wDrawn; w++) {
  517. DPF(("!%lu ", dwDrawTime[w]));
  518. }
  519. DPF(("!\n"));
  520. #endif
  521. #ifdef READTIMEHIST
  522. DPF(("Read times: "));
  523. for (w = 0; w < wRead; w++) {
  524. DPF(("!%lu ", dwReadTime[w]));
  525. }
  526. DPF(("!\n"));
  527. #endif
  528. #ifdef TIMEPLAY
  529. #define SEC(time) (UINT)(npMCI->time / 1000l) , (UINT)(npMCI->time % 1000l)
  530. #define SECX(time,t) SEC(time) , (npMCI->t ? (UINT)(npMCI->time * 100l / npMCI->t) : 0)
  531. DPF(("***********************************************************\r\n"));
  532. DPF((" timePlay: %3d.%03dsec\r\n",SEC(timePlay)));
  533. DPF((" timeRead: %3d.%03dsec (%d%%)\r\n",SECX(timeRead, timePlay)));
  534. DPF((" timeWait: %3d.%03dsec (%d%%)\r\n",SECX(timeWait, timePlay)));
  535. DPF((" timeYield: %3d.%03dsec (%d%%)\r\n",SECX(timeYield, timePlay)));
  536. DPF((" timeVideo: %3d.%03dsec (%d%%)\r\n",SECX(timeVideo, timePlay)));
  537. DPF((" timeDraw: %3d.%03dsec (%d%%)\r\n",SECX(timeDraw, timeVideo)));
  538. DPF((" timeDecompress: %3d.%03dsec (%d%%)\r\n",SECX(timeDecompress, timeVideo)));
  539. DPF((" timeAudio: %3d.%03dsec (%d%%)\r\n",SECX(timeAudio, timePlay)));
  540. DPF((" timeOther: %3d.%03dsec (%d%%)\r\n",SECX(timeOther, timePlay)));
  541. DPF((" timePaused: %3d.%03dsec\r\n",SEC(timePaused)));
  542. DPF((" timePrepare: %3d.%03dsec\r\n",SEC(timePrepare)));
  543. DPF((" timeCleanup: %3d.%03dsec\r\n",SEC(timeCleanup)));
  544. DPF(("***********************************************************\r\n"));
  545. #endif
  546. #ifdef INTERVAL_TIMES
  547. // frame interval timing
  548. if (npMCI->nFrames > 2) {
  549. int i;
  550. DPF(("-- %ld frames, ave interval %ld ms\r\n", npMCI->nFrames,
  551. npMCI->msFrameTotal/(npMCI->nFrames-1)));
  552. DPF(("-- min %ld ms, max %ld ms\r\n", npMCI->msFrameMin, npMCI->msFrameMax));
  553. DPF(("-- sd = sqrt(%ld)\r\n",
  554. (npMCI->msSquares -
  555. MulDiv(npMCI->msFrameTotal,
  556. npMCI->msFrameTotal,
  557. npMCI->nFrames-1)
  558. ) / (npMCI->nFrames-2)
  559. ));
  560. for (i = 3; i < NBUCKETS-3; i++) {
  561. DPF(("%d ms: %d\r\n", i * 10, npMCI->buckets[i]));
  562. }
  563. DPF(("Actual intervals:\r\n"));
  564. for (i = 1; i < min(npMCI->cIntervals,npMCI->nFrames); i++) {
  565. DPF(("!%3ld ", *(npMCI->paIntervals+i)));
  566. if ((fOneIntervalPerLine) || ((i % 20) == 0))
  567. DPF(("!\n"));
  568. }
  569. DPF(("!\r\n"));
  570. }
  571. if (npMCI->nReads > 0) {
  572. DPF(("-- %ld disk reads, ave %ld ms, max %ld ms\r\n",
  573. npMCI->nReads,
  574. npMCI->msReadTotal/(npMCI->nReads),
  575. npMCI->msReadMax));
  576. }
  577. #ifdef DEBUG
  578. giDebugLevel = oldDebugLevel;
  579. #endif
  580. #endif
  581. }
  582. }
  583. #ifdef HIGHRESTIMER
  584. /* If we set a high resolution timer earlier... */
  585. if (npMCI->msPeriodResolution) {
  586. // Clear the timer resolution
  587. timeEndPeriod(npMCI->msPeriodResolution);
  588. DPF1(("Cleared the timer resolution from %d milliseconds\n", npMCI->msPeriodResolution));
  589. npMCI->msPeriodResolution = 0;
  590. }
  591. #endif
  592. // if we are not stopping temporarily, then set task state to idle
  593. if (! (npMCI->dwFlags & MCIAVI_UPDATING)) {
  594. npMCI->wTaskState = TASKIDLE;
  595. }
  596. DPF(("mciaviPlayFile ending, dwTaskError==%d\n",npMCI->dwTaskError));
  597. if (npMCI->dwTaskError)
  598. return MCI_NOTIFY_FAILURE;
  599. if (npMCI->dwFlags & MCIAVI_REVERSE) {
  600. if (npMCI->lCurrentFrame <= npMCI->lTo)
  601. return MCI_NOTIFY_SUCCESSFUL;
  602. } else {
  603. if (npMCI->lCurrentFrame >= npMCI->lTo)
  604. return MCI_NOTIFY_SUCCESSFUL;
  605. }
  606. return MCI_NOTIFY_ABORTED;
  607. }
  608. static BOOL NEAR PASCAL RestartAVI(NPMCIGRAPHIC npMCI);
  609. static BOOL NEAR PASCAL PauseAVI(NPMCIGRAPHIC npMCI);
  610. static BOOL NEAR PASCAL BePaused(NPMCIGRAPHIC npMCI);
  611. /******************************************************************************
  612. *****************************************************************************/
  613. #ifdef DEBUG
  614. INLINE void FillR(HDC hdc, LPRECT prc, DWORD rgb)
  615. {
  616. SetBkColor(hdc,rgb);
  617. ExtTextOut(hdc,0,0,ETO_OPAQUE,prc,NULL,0,NULL);
  618. }
  619. void StatusBar(NPMCIGRAPHIC npMCI, int n, int dx, int max, int cur)
  620. {
  621. HDC hdc;
  622. RECT rc;
  623. if (npMCI->dwFlags & MCIAVI_FULLSCREEN)
  624. return;
  625. if (cur > max)
  626. cur = max+1;
  627. if (cur < 0)
  628. cur = 0;
  629. /*
  630. * If the window is iconic, or there is no title bar, return
  631. * without painting the status bars.
  632. */
  633. if (!IsWindow(npMCI->hwndPlayback) || IsIconic(npMCI->hwndPlayback)) {
  634. return;
  635. }
  636. if (!(GetWindowLong((npMCI->hwndPlayback), GWL_STYLE) & WS_CAPTION)) {
  637. return;
  638. }
  639. hdc = GetWindowDC(npMCI->hwndPlayback);
  640. //
  641. // show the amount of audio and how far behind we are
  642. //
  643. rc.left = 32;
  644. rc.top = 4 + n*5;
  645. rc.bottom = rc.top + 4;
  646. rc.right = rc.left + cur * dx;
  647. FillR(hdc, &rc, RGB(255,255,0));
  648. rc.left = rc.right;
  649. rc.right = rc.left + (max - cur) * dx;
  650. FillR(hdc, &rc, RGB(255,0,0));
  651. ReleaseDC(npMCI->hwndPlayback, hdc);
  652. }
  653. #else
  654. #define StatusBar(p,a,b,c,d)
  655. #endif
  656. /******************************************************************************
  657. *****************************************************************************/
  658. BOOL NEAR PASCAL PlayInterleaved(NPMCIGRAPHIC npMCI)
  659. {
  660. LONG iFrame;
  661. LONG iKey;
  662. LONG iNextKey;
  663. LONG iPrevKey;
  664. BOOL fHurryUp=FALSE;
  665. int iHurryUp=0;
  666. BOOL fPlayedAudio = FALSE;
  667. BOOL f;
  668. DPF2(("PlayInterleaved, npMCI=%8x\n",npMCI));
  669. /* If lCurrentFrame == lFrames, we're really at the end of
  670. ** the file, so there isn't another record to read.
  671. */
  672. if (npMCI->lCurrentFrame < npMCI->lFrames) {
  673. /* Read new record into buffer */
  674. DPF2(("Reading", iFrame = (LONG)timeGetTime()));
  675. TIMESTART(timeRead);
  676. f = ReadRecord(npMCI);
  677. TIMEEND(timeRead);
  678. DPF2((".done %ldms\n", (LONG)timeGetTime() - iFrame));
  679. if (!f) {
  680. npMCI->dwTaskError = MCIERR_INVALID_FILE;
  681. DPF(("Error reading frame #%ld\n", npMCI->lCurrentFrame));
  682. return FALSE;
  683. }
  684. if (npMCI->hWave && npMCI->lCurrentFrame >= npMCI->lAudioStart) {
  685. TIMESTART(timeAudio);
  686. if (!PlayRecordAudio(npMCI, &fHurryUp, &fPlayedAudio)) {
  687. DPF(("Error playing frame #%ld audio\n", npMCI->lCurrentFrame));
  688. return FALSE;
  689. }
  690. TIMEEND(timeAudio);
  691. }
  692. }
  693. /* If we're at the right frame, and we haven't started yet,
  694. ** then begin play and start timing.
  695. */
  696. if ((npMCI->lCurrentFrame > npMCI->lRealStart + (LONG) npMCI->dwBufferedVideo) &&
  697. (npMCI->wTaskState != TASKPLAYING)) {
  698. if (!(npMCI->dwFlags & MCIAVI_PAUSE)) {
  699. goto RestartPlay0;
  700. } else {
  701. // We were paused already, and now we're restarting or pausing
  702. // again... so we've already done the KeepFilling stuff to pre-fill
  703. // our buffers. If we don't skip over the KeepFilling stuff we'll
  704. // roll ahead.
  705. PauseAVI(npMCI);
  706. goto BePaused0;
  707. }
  708. }
  709. if (npMCI->wTaskState == TASKPLAYING) {
  710. if (npMCI->dwFlags & MCIAVI_PAUSE) {
  711. PauseAVI(npMCI);
  712. #ifndef _WIN32
  713. // no way do we want to do this on NT. If you get a slow disk, you will
  714. // never get to pause because we can't get the stuff in fast enough to keep up
  715. // !!! The above is not necessarily true
  716. /* The line below says that if we're trying to pause,
  717. ** but we're behind on our audio, we should keep playing
  718. ** for a little bit so that our audio buffers get full.
  719. ** This way we'll be all cued up when we RESUME and be less
  720. ** likely to fall behind.
  721. ** The jerky playback fix makes us use big buffers now so
  722. ** the last buffer can never be totally filled, so it's OK
  723. ** to stop after filling up (total - 1) buffers.
  724. */
  725. if (fPlayedAudio && npMCI->wABFull < npMCI->wABs - 1)
  726. goto KeepFilling;
  727. #endif
  728. BePaused0:
  729. BePaused(npMCI);
  730. RestartPlay0:
  731. if (npMCI->dwFlags & MCIAVI_STOP)
  732. return FALSE;
  733. if (TimeToQuit(npMCI))
  734. return FALSE;
  735. RestartAVI(npMCI);
  736. }
  737. }
  738. #ifndef _WIN32
  739. KeepFilling:
  740. #endif
  741. if (npMCI->lCurrentFrame > npMCI->lVideoStart &&
  742. npMCI->lCurrentFrame < npMCI->lFrames &&
  743. npMCI->wTaskState == TASKPLAYING) {
  744. iFrame = WhatFrameIsItTimeFor(npMCI);
  745. if (iFrame >= npMCI->lFrames)
  746. goto dontskip;
  747. iHurryUp = (int)(iFrame - npMCI->lCurrentFrame);
  748. fHurryUp = iHurryUp > gwHurryTolerance;
  749. if (iHurryUp > 1 && npMCI->hpFrameIndex && (npMCI->dwOptionFlags & MCIAVIO_SKIPFRAMES)) {
  750. //
  751. // WE ARE BEHIND!!! by one or more frames.
  752. //
  753. // if we are late we can do one of the following:
  754. //
  755. // dont draw frames but keep reading/decompressing them
  756. // (ie set fHurryUp)
  757. //
  758. // skip ahead to a key frame.
  759. //
  760. // !!! If we're very close to the next key frame, be more
  761. // willing to skip ahead....
  762. //
  763. if (iHurryUp > gwSkipTolerance) {
  764. iNextKey = FrameNextKey(iFrame);
  765. iPrevKey = FramePrevKey(iFrame);
  766. if (iPrevKey > npMCI->lCurrentFrame &&
  767. iFrame - iPrevKey < gwHurryTolerance &&
  768. iNextKey - iFrame > gwSkipTolerance) {
  769. DPF2(("Skipping from %ld to PREV KEY %ld (time for %ld next key=%ld).\n", npMCI->lCurrentFrame, iPrevKey, iFrame, iNextKey));
  770. iKey = iPrevKey;
  771. }
  772. // !!! We'll only skip if the key frame is at most as far
  773. // ahead as we are behind.....
  774. else if (iNextKey > npMCI->lCurrentFrame &&
  775. iNextKey <= iFrame + gwSkipTolerance /*gwMaxSkipEver*/) {
  776. DPF2(("Skipping from %ld to NEXT KEY %ld (time for %ld prev key=%ld).\n", npMCI->lCurrentFrame, iNextKey, iFrame, iPrevKey));
  777. iKey = iNextKey;
  778. } else {
  779. DPF2(("WANTED to skip from %ld to %ld (time for %ld)!\n", npMCI->lCurrentFrame,iNextKey,iFrame));
  780. goto dontskip;
  781. }
  782. npMCI->lVideoStart = iKey;
  783. npMCI->dwSkippedFrames += iKey - npMCI->lCurrentFrame;
  784. dontskip:
  785. fHurryUp = TRUE;
  786. }
  787. else {
  788. iKey = FrameNextKey(npMCI->lCurrentFrame);
  789. if (iKey - npMCI->lCurrentFrame > 0 &&
  790. iKey - npMCI->lCurrentFrame <= gwHurryTolerance) {
  791. DPF2(("Skipping from %ld to next key frame %ld (time for %ld).\n", npMCI->lCurrentFrame, iKey, iFrame));
  792. npMCI->dwSkippedFrames += iKey - npMCI->lCurrentFrame;
  793. npMCI->lVideoStart = iKey;
  794. fHurryUp = TRUE;
  795. }
  796. }
  797. }
  798. }
  799. if (npMCI->dwFlags & MCIAVI_WAVEPAUSED)
  800. fHurryUp = TRUE;
  801. /* If we've actually started timing:
  802. ** Check if we should send a signal.
  803. ** Check to see if we should break out of the loop.
  804. ** Wait until it's time for the next frame.
  805. */
  806. if (npMCI->wTaskState == TASKPLAYING &&
  807. npMCI->lCurrentFrame >= npMCI->lVideoStart) {
  808. if (npMCI->dwSignals)
  809. CheckSignals(npMCI, npMCI->lCurrentFrame - npMCI->dwBufferedVideo);
  810. #ifdef WAITHISTOGRAM
  811. /* Adjust to achieve proper tension. */
  812. if (fPlayedAudio) {
  813. /* If we're playing, keep statistics about how we're doing. */
  814. ++wHist[npMCI->wABFull];
  815. }
  816. #endif
  817. if (!WaitTillNextFrame(npMCI))
  818. return FALSE;
  819. }
  820. if (npMCI->lCurrentFrame >= npMCI->lVideoStart &&
  821. npMCI->lCurrentFrame < npMCI->lFrames) {
  822. #ifdef SHOWSKIPPED
  823. if (fHurryUp && wSkipped < NUMSKIPSSHOWN) {
  824. lSkipped[wSkipped++] = npMCI->lCurrentFrame;
  825. }
  826. #endif
  827. /* hold critsec round all worker thread drawing */
  828. EnterHDCCrit(npMCI);
  829. TIMESTART(timeVideo);
  830. if (!DisplayVideoFrame(npMCI, fHurryUp)) {
  831. npMCI->dwTaskError = MCIERR_AVI_DISPLAYERROR;
  832. TIMEZERO(timeVideo);
  833. LeaveHDCCrit(npMCI);
  834. return FALSE;
  835. }
  836. TIMEEND(timeVideo);
  837. LeaveHDCCrit(npMCI);
  838. #ifdef DRAWTIMEHIST
  839. if (!fHurryUp && (wDrawn < NUMDRAWN)) {
  840. dwDrawTime[wDrawn++] = npMCI->dwLastDrawTime;
  841. }
  842. #endif
  843. }
  844. StatusBar(npMCI, 0, 4, npMCI->wABs, npMCI->wABFull);
  845. StatusBar(npMCI, 1, 4, npMCI->wABs, npMCI->wABs - iHurryUp);
  846. #ifdef AVIREAD
  847. if ((npMCI->hAviRd) && (npMCI->lpBuffer != NULL)) {
  848. /* finished with this buffer - put back on queue */
  849. avird_emptybuffer(npMCI->hAviRd, npMCI->lpBuffer);
  850. npMCI->lpBuffer = NULL;
  851. }
  852. #endif
  853. DPF2(("PlayInterleaved...ENDING, npMCI=%8x, TaskState=%d\n", npMCI, npMCI->wTaskState));
  854. return TRUE;
  855. }
  856. /******************************************************************************
  857. *****************************************************************************/
  858. BOOL NEAR PASCAL PlayNonInterleaved(NPMCIGRAPHIC npMCI)
  859. {
  860. BOOL fHurryUp = FALSE;
  861. int iHurryUp;
  862. LONG iFrame;
  863. LONG iKey;
  864. LONG iNextKey;
  865. LONG iPrevKey;
  866. DPF2(("PlayNonInterleaved, npMCI=%8x\n",npMCI));
  867. if (npMCI->hWave) {
  868. TIMESTART(timeAudio);
  869. KeepPlayingAudio(npMCI);
  870. TIMEEND(timeAudio);
  871. }
  872. if (npMCI->wTaskState == TASKPLAYING) {
  873. iFrame = WhatFrameIsItTimeFor(npMCI);
  874. if (iFrame >= npMCI->lFrames)
  875. goto dontskip;
  876. if (npMCI->dwFlags & MCIAVI_REVERSE) {
  877. /* Since we're going backwards, always skip to key frame. */
  878. DPF3((" Current = %ld, time for %ld.\n", npMCI->lCurrentFrame, iFrame));
  879. iFrame = FramePrevKey(iFrame);
  880. // !!! Send signals for skipped frames?
  881. npMCI->dwFramesSeekedPast += npMCI->lCurrentFrame - iFrame;
  882. npMCI->dwSkippedFrames += npMCI->lCurrentFrame - iFrame;
  883. npMCI->lCurrentFrame = iFrame;
  884. } else if (npMCI->lCurrentFrame < npMCI->lFrames) {
  885. #ifdef BEHINDHIST
  886. {
  887. int iDelta;
  888. iDelta = iFrame - npMCI->lCurrentFrame + BEHINDOFFSET;
  889. iDelta = min(NUMBEHIND, max(0, iDelta));
  890. wBehind[iDelta]++;
  891. }
  892. #endif
  893. iHurryUp = (int)(iFrame - npMCI->lCurrentFrame);
  894. fHurryUp = iHurryUp > gwHurryTolerance;
  895. if (iHurryUp > 1 && npMCI->hpFrameIndex && (npMCI->dwOptionFlags & MCIAVIO_SKIPFRAMES)) {
  896. //
  897. // WE ARE BEHIND!!! by one or more frames.
  898. //
  899. // if we are late we can do one of the following:
  900. //
  901. // dont draw frames but keep reading/decompressing them
  902. // (ie set fHurryUp)
  903. //
  904. // skip ahead to a key frame.
  905. //
  906. // !!! If we're very close to the next key frame, be more
  907. // willing to skip ahead....
  908. //
  909. if (iHurryUp > gwSkipTolerance) {
  910. iNextKey = FrameNextKey(iFrame);
  911. iPrevKey = FramePrevKey(iFrame);
  912. if (iPrevKey > npMCI->lCurrentFrame &&
  913. iFrame - iPrevKey < gwHurryTolerance &&
  914. iNextKey - iFrame > gwSkipTolerance) {
  915. DPF2(("Skipping from %ld to PREV KEY %ld (time for %ld next key=%ld).\n", npMCI->lCurrentFrame, iPrevKey, iFrame, iNextKey));
  916. iKey = iPrevKey;
  917. fHurryUp = TRUE;
  918. }
  919. // !!! We'll only skip if the key frame is at most as far
  920. // ahead as we are behind.....
  921. else if (iNextKey > npMCI->lCurrentFrame &&
  922. iNextKey <= iFrame + gwSkipTolerance /*gwMaxSkipEver*/) {
  923. DPF2(("Skipping from %ld to NEXT KEY %ld (time for %ld prev key=%ld).\n", npMCI->lCurrentFrame, iNextKey, iFrame, iPrevKey));
  924. iKey = iNextKey; // assume next key
  925. fHurryUp = FALSE;
  926. } else {
  927. DPF2(("WANTED to skip from %ld to %ld (time for %ld)!\n", npMCI->lCurrentFrame,iNextKey,iFrame));
  928. goto dontskip;
  929. }
  930. npMCI->dwFramesSeekedPast += iKey - npMCI->lCurrentFrame;
  931. npMCI->dwSkippedFrames += iKey - npMCI->lCurrentFrame;
  932. npMCI->lCurrentFrame = iKey;
  933. dontskip:
  934. ;
  935. }
  936. else if (FramePrevKey(iFrame) == iFrame) {
  937. DPF2(("Skipping from %ld to %ld (time for key frame).\n", npMCI->lCurrentFrame, iFrame));
  938. iKey = iFrame;
  939. npMCI->dwFramesSeekedPast += iKey - npMCI->lCurrentFrame;
  940. npMCI->dwSkippedFrames += iKey - npMCI->lCurrentFrame;
  941. npMCI->lCurrentFrame = iKey;
  942. fHurryUp = FALSE;
  943. }
  944. else {
  945. iKey = FrameNextKey(npMCI->lCurrentFrame);
  946. if (iKey > npMCI->lCurrentFrame &&
  947. iKey - npMCI->lCurrentFrame <= gwHurryTolerance) {
  948. DPF2(("Skipping from %ld to next key frame %ld (time for %ld).\n", npMCI->lCurrentFrame, iKey, iFrame));
  949. npMCI->dwFramesSeekedPast += iKey - npMCI->lCurrentFrame;
  950. npMCI->dwSkippedFrames += iKey - npMCI->lCurrentFrame;
  951. npMCI->lCurrentFrame = iKey;
  952. fHurryUp = ((iKey - iFrame) > gwHurryTolerance);
  953. }
  954. }
  955. }
  956. StatusBar(npMCI, 0, 4, npMCI->wABs, npMCI->wABFull);
  957. StatusBar(npMCI, 1, 4, npMCI->wABs, npMCI->wABs - iHurryUp);
  958. }
  959. }
  960. // !!! Somewhere in here, read other streams.
  961. // Should this be before, or after, video?
  962. /* If lCurrentFrame == lFrames, we're really at the end of
  963. ** the file, so there isn't another record to read.
  964. */
  965. if (npMCI->lCurrentFrame < npMCI->lFrames) {
  966. /* Read new record into buffer */
  967. npMCI->dwLastReadTime = (DWORD)(-(LONG)timeGetTime());
  968. TIMESTART(timeRead);
  969. if (!ReadNextVideoFrame(npMCI, NULL)) {
  970. npMCI->dwTaskError = MCIERR_INVALID_FILE;
  971. DPF2(("Error reading frame #%ld\n", npMCI->lCurrentFrame));
  972. return FALSE;
  973. }
  974. TIMEEND(timeRead);
  975. npMCI->dwLastReadTime += timeGetTime();
  976. npMCI->lLastRead = npMCI->lCurrentFrame;
  977. #ifdef READTIMEHIST
  978. if (wRead < NUMREAD) {
  979. dwReadTime[wRead++] = npMCI->dwLastReadTime;
  980. }
  981. #endif
  982. }
  983. /* If we're at the right frame, and we haven't started yet,
  984. ** then begin play and start timing.
  985. */
  986. if ((((npMCI->lCurrentFrame > (npMCI->lRealStart +
  987. (LONG) npMCI->dwBufferedVideo)) &&
  988. (npMCI->lCurrentFrame < (npMCI->lTo))) ||
  989. (npMCI->dwFlags & MCIAVI_REVERSE)) &&
  990. (npMCI->wTaskState != TASKPLAYING) &&
  991. !(npMCI->dwFlags & MCIAVI_SEEKING)) {
  992. if (!(npMCI->dwFlags & MCIAVI_PAUSE)) {
  993. goto RestartPlay;
  994. } else
  995. goto PauseNow;
  996. }
  997. /* If we've actually started timing:
  998. ** Check if we should send a signal.
  999. ** Check to see if we should return FALSE out of the loop.
  1000. ** Wait until it's time for the next frame.
  1001. */
  1002. if (npMCI->wTaskState == TASKPLAYING) {
  1003. if (npMCI->dwFlags & MCIAVI_PAUSE) {
  1004. PauseNow:
  1005. PauseAVI(npMCI);
  1006. BePaused(npMCI);
  1007. RestartPlay:
  1008. if (TimeToQuit(npMCI))
  1009. return FALSE;
  1010. RestartAVI(npMCI);
  1011. }
  1012. if (npMCI->dwSignals)
  1013. CheckSignals(npMCI, npMCI->lCurrentFrame - npMCI->dwBufferedVideo);
  1014. if (npMCI->lCurrentFrame < npMCI->lFrames + (LONG) npMCI->dwBufferedVideo) {
  1015. while (1) {
  1016. iFrame = WhatFrameIsItTimeFor(npMCI);
  1017. TIMESTART(timeYield);
  1018. aviTaskCheckRequests(npMCI);
  1019. TIMEEND(timeYield);
  1020. if (npMCI->dwFlags & MCIAVI_REVERSE) {
  1021. if (iFrame <= npMCI->lCurrentFrame)
  1022. break;
  1023. if (npMCI->lCurrentFrame < npMCI->lTo)
  1024. break;
  1025. } else {
  1026. if (iFrame >= npMCI->lCurrentFrame)
  1027. break;
  1028. if (npMCI->lCurrentFrame > npMCI->lTo)
  1029. break;
  1030. }
  1031. if (npMCI->hWave) {
  1032. TIMESTART(timeAudio);
  1033. KeepPlayingAudio(npMCI);
  1034. TIMEEND(timeAudio);
  1035. }
  1036. DPF3(("Waiting: Current = %ld, time for %ld.\n", npMCI->lCurrentFrame, iFrame));
  1037. if (!(npMCI->dwFlags & MCIAVI_REVERSE)) {
  1038. WaitTillNextFrame(npMCI);
  1039. }
  1040. if (TimeToQuit(npMCI))
  1041. return FALSE;
  1042. }
  1043. }
  1044. if (TimeToQuit(npMCI))
  1045. return FALSE;
  1046. }
  1047. if (((npMCI->lCurrentFrame >= npMCI->lVideoStart) &&
  1048. (npMCI->lCurrentFrame < npMCI->lFrames)) ||
  1049. (npMCI->dwFlags & MCIAVI_REVERSE)) {
  1050. // Quick exit if we are being told to stop
  1051. if (npMCI->dwFlags & MCIAVI_STOP) {
  1052. return(FALSE);
  1053. }
  1054. EnterHDCCrit(npMCI);
  1055. TIMESTART(timeVideo);
  1056. if (!DisplayVideoFrame(npMCI, fHurryUp)) {
  1057. npMCI->dwTaskError = MCIERR_AVI_DISPLAYERROR;
  1058. TIMEZERO(timeVideo);
  1059. LeaveHDCCrit(npMCI);
  1060. return FALSE;
  1061. }
  1062. TIMEEND(timeVideo);
  1063. LeaveHDCCrit(npMCI);
  1064. #ifdef DRAWTIMEHIST
  1065. if (!fHurryUp && (wDrawn < NUMDRAWN)) {
  1066. dwDrawTime[wDrawn++] = npMCI->dwLastDrawTime;
  1067. }
  1068. #endif
  1069. }
  1070. //
  1071. // now is a good time to deal with other streams
  1072. //
  1073. if (npMCI->nOtherStreams > 0 || npMCI->nVideoStreams > 1) {
  1074. if (npMCI->wTaskState != TASKPLAYING)
  1075. iFrame = npMCI->lCurrentFrame;
  1076. TIMESTART(timeOther);
  1077. DealWithOtherStreams(npMCI, iFrame);
  1078. TIMEEND(timeOther);
  1079. }
  1080. DPF2(("PlayNONInterleaved...ENDING, npMCI=%8x, TaskState=%d\n", npMCI, npMCI->wTaskState));
  1081. return TRUE;
  1082. }
  1083. /******************************************************************************
  1084. *****************************************************************************/
  1085. BOOL NEAR PASCAL PlayAudioOnly(NPMCIGRAPHIC npMCI)
  1086. {
  1087. DPF2(("PlayAudioOnly, npMCI=%8x\n",npMCI));
  1088. npMCI->lFrameDrawn = npMCI->lCurrentFrame;
  1089. if (npMCI->hWave) {
  1090. TIMESTART(timeAudio);
  1091. KeepPlayingAudio(npMCI);
  1092. TIMEEND(timeAudio);
  1093. }
  1094. /* If we're at the right frame, and we haven't started yet,
  1095. ** then begin play and start timing.
  1096. */
  1097. if ((npMCI->wTaskState != TASKPLAYING) &&
  1098. !(npMCI->dwFlags & MCIAVI_SEEKING)) {
  1099. if (!(npMCI->dwFlags & MCIAVI_PAUSE)) {
  1100. goto RestartPlay;
  1101. } else
  1102. goto PauseNow;
  1103. }
  1104. /* If we've actually started timing:
  1105. ** Check if we should send a signal.
  1106. ** Check to see if we should return FALSE out of the loop.
  1107. ** Wait until it's time for the next frame.
  1108. */
  1109. if (npMCI->wTaskState == TASKPLAYING) {
  1110. npMCI->lCurrentFrame = WhatFrameIsItTimeFor(npMCI);
  1111. if (npMCI->dwFlags & MCIAVI_PAUSE) {
  1112. PauseNow:
  1113. PauseAVI(npMCI);
  1114. BePaused(npMCI);
  1115. RestartPlay:
  1116. if (TimeToQuit(npMCI))
  1117. return FALSE;
  1118. RestartAVI(npMCI);
  1119. }
  1120. if (npMCI->dwSignals)
  1121. CheckSignals(npMCI, npMCI->lCurrentFrame - npMCI->dwBufferedVideo);
  1122. //
  1123. // dont yield if updating
  1124. //
  1125. if (!(npMCI->dwFlags & MCIAVI_UPDATING)) {
  1126. TIMESTART(timeYield);
  1127. aviTaskCheckRequests(npMCI);
  1128. TIMEEND(timeYield);
  1129. }
  1130. if (TimeToQuit(npMCI))
  1131. return FALSE;
  1132. }
  1133. return TRUE;
  1134. }
  1135. /******************************************************************************
  1136. *****************************************************************************/
  1137. #ifdef USENONINTFROMCD
  1138. #pragma message("PlayNonIntFromCD needs fixed?")
  1139. BOOL NEAR PASCAL PlayNonIntFromCD(NPMCIGRAPHIC npMCI)
  1140. {
  1141. BOOL fHurryUp = FALSE;
  1142. LONG lNewFrame;
  1143. DWORD ckid;
  1144. UINT wStream;
  1145. DPF2(("PlayNonIntFromCD, npMCI=%8x\n",npMCI));
  1146. AnotherChunk:
  1147. /* If lCurrentFrame == lFrames, we're really at the end of
  1148. ** the file, so there isn't another record to read.
  1149. */
  1150. if (npMCI->lCurrentFrame < npMCI->lFrames) {
  1151. /* Read new record into buffer */
  1152. TIMESTART(timeRead);
  1153. ckid = ReadNextChunk(npMCI);
  1154. TIMEEND(timeRead);
  1155. if (ckid == 0) {
  1156. npMCI->dwTaskError = MCIERR_INVALID_FILE;
  1157. DPF(("Error reading frame #%ld\n", npMCI->lCurrentFrame));
  1158. return FALSE;
  1159. }
  1160. npMCI->lLastRead = npMCI->lCurrentFrame;
  1161. wStream = StreamFromFOURCC(ckid);
  1162. if (wStream == (UINT) npMCI->nVideoStream) {
  1163. if (TWOCCFromFOURCC(ckid) == cktypePALchange) {
  1164. npMCI->lp += 2 * sizeof(DWORD);
  1165. ProcessPaletteChange(npMCI, npMCI->dwThisRecordSize -
  1166. 2 * sizeof(DWORD));
  1167. npMCI->lLastPaletteChange = npMCI->lCurrentFrame;
  1168. goto AnotherChunk;
  1169. }
  1170. } else if (wStream == (UINT) npMCI->nAudioStream) {
  1171. TIMESTART(timeAudio);
  1172. if (npMCI->hWave)
  1173. HandleAudioChunk(npMCI);
  1174. TIMEEND(timeAudio);
  1175. goto AnotherChunk;
  1176. } else {
  1177. goto AnotherChunk;
  1178. }
  1179. }
  1180. if (npMCI->wTaskState == TASKPLAYING) {
  1181. lNewFrame = WhatFrameIsItTimeFor(npMCI);
  1182. DPF3((" Current = %ld, time for %ld.\n", npMCI->lCurrentFrame, lNewFrame));
  1183. if (npMCI->lCurrentFrame < lNewFrame) {
  1184. fHurryUp = TRUE;
  1185. }
  1186. }
  1187. /* If we're at the right frame, and we haven't started yet,
  1188. ** then begin play and start timing.
  1189. */
  1190. if ((npMCI->lCurrentFrame > npMCI->lRealStart + (LONG) npMCI->dwBufferedVideo) &&
  1191. (npMCI->lCurrentFrame < npMCI->lTo) &&
  1192. (npMCI->wTaskState != TASKPLAYING)) {
  1193. if (!(npMCI->dwFlags & MCIAVI_PAUSE)) {
  1194. goto RestartPlay;
  1195. } else
  1196. goto PauseNow;
  1197. }
  1198. /* If we've actually started timing:
  1199. ** Check if we should send a signal.
  1200. ** Check to see if we should return FALSE out of the loop.
  1201. ** Wait until it's time for the next frame.
  1202. */
  1203. if (npMCI->wTaskState == TASKPLAYING) {
  1204. if (npMCI->dwFlags & MCIAVI_PAUSE) {
  1205. PauseNow:
  1206. PauseAVI(npMCI);
  1207. BePaused(npMCI);
  1208. RestartPlay:
  1209. if (TimeToQuit(npMCI))
  1210. return FALSE;
  1211. RestartAVI(npMCI);
  1212. }
  1213. if (npMCI->dwSignals)
  1214. CheckSignals(npMCI, npMCI->lCurrentFrame - npMCI->dwBufferedVideo);
  1215. WaitMore: ///////WAITING//////////
  1216. lNewFrame = WhatFrameIsItTimeFor(npMCI);
  1217. TIMESTART(timeYield);
  1218. aviTaskCheckRequests(npMCI);
  1219. TIMEEND(timeYield);
  1220. if (lNewFrame < npMCI->lCurrentFrame) {
  1221. DPF3(("Waiting: Current = %ld, time for %ld.\n", npMCI->lCurrentFrame, lNewFrame));
  1222. WaitTillNextFrame(npMCI);
  1223. if (TimeToQuit(npMCI))
  1224. return FALSE;
  1225. else
  1226. goto WaitMore;
  1227. }
  1228. }
  1229. if (npMCI->lCurrentFrame >= npMCI->lVideoStart) {
  1230. TIMESTART(timeVideo);
  1231. EnterHDCCrit(npMCI);
  1232. if (!DisplayVideoFrame(npMCI, fHurryUp)) {
  1233. npMCI->dwTaskError = MCIERR_AVI_DISPLAYERROR;
  1234. TIMEZERO(timeVideo);
  1235. LeaveHDCCrit(npMCI);
  1236. return FALSE;
  1237. }
  1238. TIMEEND(timeVideo);
  1239. LeaveHDCCrit(npMCI);
  1240. }
  1241. return TRUE;
  1242. }
  1243. #endif
  1244. /******************************************************************************
  1245. *****************************************************************************/
  1246. STATICFN INLINE LONG waveTime(NPMCIGRAPHIC npMCI, LONG lTime)
  1247. {
  1248. if (gfUseGetPosition && npMCI->wABFull > 0) {
  1249. MMTIME mmtime;
  1250. LONG lTimeInc;
  1251. mmtime.wType = TIME_SAMPLES;
  1252. waveOutGetPosition(npMCI->hWave, &mmtime, sizeof(mmtime));
  1253. if (mmtime.wType == TIME_SAMPLES)
  1254. lTimeInc = muldiv32(mmtime.u.sample,
  1255. 1000L, npMCI->pWF->nSamplesPerSec);
  1256. else if (mmtime.wType == TIME_BYTES)
  1257. lTimeInc = muldiv32(mmtime.u.cb,
  1258. 1000L, npMCI->pWF->nAvgBytesPerSec);
  1259. else
  1260. goto ack2;
  1261. //DPF0(("lTime: %3d, LastDraw: %4d, lTimeInc: %5d ",
  1262. // lTime, npMCI->dwLastDrawTime, lTimeInc));
  1263. lTime = lTimeInc + npMCI->dwLastDrawTime;
  1264. // !!! this is too accurate: adjust by 100ms to match old SB...
  1265. lTime = max(0, lTime - giGetPositionAdjust);
  1266. } else {
  1267. ack2:
  1268. lTime += muldiv32(npMCI->dwAudioPlayed,
  1269. 1000L, npMCI->pWF->nAvgBytesPerSec);
  1270. }
  1271. return(lTime);
  1272. }
  1273. INLINE LONG GetVideoTime(NPMCIGRAPHIC npMCI)
  1274. {
  1275. //
  1276. // NOTE we must grab dwTimingStart *before* calling
  1277. // timeGetTime() because dwTimingStart is changed in the wave
  1278. // callback and we dont want to have time go backward.
  1279. //
  1280. LONG lTime = npMCI->dwTimingStart;
  1281. lTime = (LONG)timeGetTime() - lTime
  1282. + npMCI->dwLastDrawTime
  1283. // + npMCI->dwLastReadTime
  1284. ;
  1285. Assert(lTime >= 0);
  1286. if (npMCI->hWave) {
  1287. if (npMCI->dwFlags & MCIAVI_WAVEPAUSED) {
  1288. lTime = 0;
  1289. }
  1290. lTime = waveTime(npMCI, lTime);
  1291. }
  1292. return(lTime);
  1293. }
  1294. /* This function returns what frame we should be on. */
  1295. LONG NEAR PASCAL WhatFrameIsItTimeFor(NPMCIGRAPHIC npMCI)
  1296. {
  1297. LONG lTime;
  1298. LONG lFrame;
  1299. // If timing is off, it's always just time to play the current frame.
  1300. if (npMCI->dwPlayMicroSecPerFrame == 0)
  1301. return npMCI->lCurrentFrame;
  1302. //
  1303. // if we have not started playing npMCI->dwTimingStart is bogus
  1304. //
  1305. Assert(npMCI->wTaskState == TASKPLAYING);
  1306. AssertFrame(npMCI->lCurrentFrame - (LONG)npMCI->dwBufferedVideo);
  1307. lTime = GetVideoTime(npMCI);
  1308. /* Convert from MS to frames.... */
  1309. //force round down - subtract half a frame
  1310. lTime -= (npMCI->dwPlayMicroSecPerFrame / 2000);
  1311. if (lTime < 0) {
  1312. lTime = 0;
  1313. lFrame = 0;
  1314. } else {
  1315. lFrame = muldiv32(lTime, 1000, npMCI->dwPlayMicroSecPerFrame);
  1316. }
  1317. if (npMCI->dwFlags & MCIAVI_REVERSE) {
  1318. lFrame = npMCI->lFramePlayStart - lFrame;
  1319. if (lFrame < npMCI->lTo)
  1320. lFrame = npMCI->lTo;
  1321. }
  1322. else {
  1323. lFrame = lFrame + npMCI->lFramePlayStart + npMCI->dwBufferedVideo;
  1324. if ((lFrame > npMCI->lTo) && (lFrame > npMCI->lCurrentFrame))
  1325. lFrame = npMCI->lTo;
  1326. }
  1327. if (lFrame > npMCI->lFrames + (LONG)npMCI->dwBufferedVideo || lFrame < 0) {
  1328. DPF(("WhatFrameIsItTimeFor: bad frame %ld\n", lFrame));
  1329. AssertSz(0, "bad frame in WhatFrameIsItTimeFor");
  1330. lFrame = npMCI->lCurrentFrame;
  1331. }
  1332. return lFrame;
  1333. }
  1334. /******************************************************************************
  1335. *****************************************************************************/
  1336. /* This function returns time to go until the target frame */
  1337. LONG NEAR PASCAL HowLongTill(NPMCIGRAPHIC npMCI)
  1338. {
  1339. LONG lTime;
  1340. LONG lTimeTarget;
  1341. LONG lFrameTarget = npMCI->lCurrentFrame;
  1342. // If timing is off, it's always just time to play the current frame.
  1343. if (npMCI->dwPlayMicroSecPerFrame == 0)
  1344. return 0;
  1345. //
  1346. // if we have not started playing npMCI->dwTimingStart is bogus
  1347. //
  1348. Assert(npMCI->wTaskState == TASKPLAYING);
  1349. // no longer valid because of last-frame-audio fix
  1350. //AssertFrame(npMCI->lCurrentFrame - (LONG)npMCI->dwBufferedVideo);
  1351. lTime = GetVideoTime(npMCI);
  1352. if (npMCI->dwFlags & MCIAVI_REVERSE)
  1353. lFrameTarget = npMCI->lFramePlayStart - lFrameTarget;
  1354. else
  1355. lFrameTarget -= npMCI->lFramePlayStart + npMCI->dwBufferedVideo;
  1356. lTimeTarget = muldiv32(lFrameTarget, npMCI->dwPlayMicroSecPerFrame, 1000);
  1357. //DPF0(("! >> %5d\n", lTimeTarget - lTime));
  1358. return lTimeTarget - lTime;
  1359. }
  1360. /******************************************************************************
  1361. *****************************************************************************/
  1362. static BOOL NEAR PASCAL PauseAVI(NPMCIGRAPHIC npMCI)
  1363. {
  1364. DPF2(("PauseAVI\n"));
  1365. if (npMCI->wTaskState == TASKPLAYING) {
  1366. int stream;
  1367. if (npMCI->hWave)
  1368. waveOutPause(npMCI->hWave);
  1369. if (npMCI->hicDraw)
  1370. ICDrawStop(npMCI->hicDraw);
  1371. for (stream = 0; stream < npMCI->streams; stream++) {
  1372. if (SI(stream)->hicDraw)
  1373. ICDrawStop(SI(stream)->hicDraw);
  1374. }
  1375. npMCI->dwPauseTime = Now();
  1376. npMCI->dwTotalMSec += npMCI->dwPauseTime - npMCI->dwMSecPlayStart;
  1377. }
  1378. if (npMCI->dwFlags & MCIAVI_WAITING) {
  1379. // waiting for completion of a pause or cue request
  1380. DPF3(("Releasing UI waiter\n"));
  1381. SetEvent(npMCI->hEventAllDone);
  1382. npMCI->dwFlags &= ~MCIAVI_WAITING;
  1383. }
  1384. // this flag is set to indicate that notify should be issued
  1385. // when we reach a paused state (eg on Cue).
  1386. if (npMCI->dwFlags & MCIAVI_CUEING) {
  1387. /* If we're cueing, report that it was successful. */
  1388. npMCI->dwFlags &= ~(MCIAVI_CUEING);
  1389. GraphicDelayedNotify(npMCI, MCI_NOTIFY_SUCCESSFUL);
  1390. }
  1391. DPF2(("Pausing npMCI==%8x\n",npMCI));
  1392. npMCI->wTaskState = TASKPAUSED;
  1393. return TRUE;
  1394. }
  1395. /******************************************************************************
  1396. *****************************************************************************/
  1397. static BOOL NEAR PASCAL BePaused(NPMCIGRAPHIC npMCI)
  1398. {
  1399. DWORD dwObject;
  1400. TIMEEND(timePlay);
  1401. TIMESTART(timePaused);
  1402. while (npMCI->dwFlags & MCIAVI_PAUSE) {
  1403. if (npMCI->dwFlags & MCIAVI_STOP)
  1404. return FALSE;
  1405. if (npMCI->dwFlags & MCIAVI_NEEDUPDATE) {
  1406. /* Since we're paused and we have nothing better
  1407. ** to do, update the screen.
  1408. */
  1409. DoStreamUpdate(npMCI, FALSE);
  1410. }
  1411. // block until told to do something else
  1412. // need to handle send-messages to the ole windows -see mciaviTask()
  1413. do {
  1414. dwObject = MsgWaitForMultipleObjects(2, &npMCI->hEventSend,
  1415. FALSE, INFINITE, QS_SENDMESSAGE);
  1416. if (dwObject == WAIT_OBJECT_0 + 2) {
  1417. MSG msg;
  1418. // just a single peekmessage with NOREMOVE will
  1419. // process the inter-thread send and not affect the queue
  1420. PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
  1421. }
  1422. } while (dwObject == WAIT_OBJECT_0 + 2);
  1423. // find out what needed doing
  1424. aviTaskCheckRequests(npMCI);
  1425. if (npMCI->dwFlags & MCIAVI_WAITING) {
  1426. // waiting for completion of a pause or cue request
  1427. SetEvent(npMCI->hEventAllDone);
  1428. npMCI->dwFlags &= ~MCIAVI_WAITING;
  1429. }
  1430. }
  1431. TIMEEND(timePaused);
  1432. TIMESTART(timePlay);
  1433. return TRUE;
  1434. }
  1435. /******************************************************************************
  1436. *****************************************************************************/
  1437. static BOOL NEAR PASCAL RestartAVI(NPMCIGRAPHIC npMCI)
  1438. {
  1439. int stream;
  1440. Assert(npMCI->wTaskState != TASKPLAYING);
  1441. /* Mark that play has actually begun */
  1442. npMCI->wTaskState = TASKPLAYING;
  1443. DPF(("Restart AVI, TaskState now TASKPLAYING, npMCI=%8x\n", npMCI));
  1444. #ifndef _WIN32
  1445. TIMESTART(timeYield);
  1446. aviTaskYield();
  1447. aviTaskYield();
  1448. aviTaskYield();
  1449. TIMEEND(timeYield);
  1450. DPF2(("Starting (done yielding)\n"));
  1451. #endif
  1452. /* Reset clock and restart */
  1453. if (npMCI->dwPauseTime == 0) {
  1454. Assert(npMCI->dwTimingStart == 0);
  1455. }
  1456. npMCI->dwMSecPlayStart = Now(); // get the time we started playing
  1457. //
  1458. // if we were paused subtract off the time we spent paused from
  1459. // the timing start
  1460. //
  1461. if (npMCI->dwPauseTime == 0)
  1462. npMCI->dwTimingStart = npMCI->dwMSecPlayStart;
  1463. else
  1464. npMCI->dwTimingStart += (npMCI->dwMSecPlayStart - npMCI->dwPauseTime);
  1465. if (npMCI->hWave)
  1466. waveOutRestart(npMCI->hWave);
  1467. if (npMCI->hicDraw)
  1468. ICDrawStart(npMCI->hicDraw);
  1469. for (stream = 0; stream < npMCI->streams; stream++) {
  1470. if (SI(stream)->hicDraw)
  1471. ICDrawStart(SI(stream)->hicDraw);
  1472. }
  1473. DPF(("Returning from RestartAVI\n"));
  1474. return TRUE;
  1475. }
  1476. /* This function sets up things that will be needed to play.
  1477. **
  1478. ** Returns zero if no error, otherwise an MCI error code.
  1479. **
  1480. ** Note: Even if this function returns an error, CleanUpPlay()
  1481. ** will still be called, so we don't have to cleanup here.
  1482. */
  1483. STATICFN DWORD NEAR PASCAL PrepareToPlay(NPMCIGRAPHIC npMCI)
  1484. {
  1485. BOOL fCDFile;
  1486. BOOL fNetFile;
  1487. BOOL fHardFile;
  1488. Assert(npMCI->wTaskState != TASKPLAYING);
  1489. //
  1490. // lets choose the play back method:
  1491. //
  1492. // playing reverse: (random access!)
  1493. // use MCIAVI_ALG_HARDDISK always (random access mode)
  1494. //
  1495. // audio is preloaded: (will never happen?)
  1496. // on a CD-ROM use MCIAVI_ALG_INTERLEAVED
  1497. // on a HARDDISK use MCIAVI_ALG_HARDDISK
  1498. // on a NET use MCIAVI_ALG_HARDDISK
  1499. //
  1500. // file is interleaved:
  1501. // on a CD-ROM use MCIAVI_ALG_INTERLEAVED
  1502. // on a HARDDISK use MCIAVI_ALG_HARDDISK
  1503. // on a NET use MCIAVI_ALG_HARDDISK
  1504. //
  1505. // file is not interleaved:
  1506. // on a CD-ROM use MCIAVI_ALG_CDROM
  1507. // on a HARDDISK use MCIAVI_ALG_HARDDISK
  1508. // on a NET use MCIAVI_ALG_HARDDISK
  1509. //
  1510. fCDFile = npMCI->uDriveType == DRIVE_CDROM;
  1511. fNetFile = npMCI->uDriveType == DRIVE_REMOTE;
  1512. fHardFile = !fCDFile && !fNetFile;
  1513. if (npMCI->nVideoStreams == 0 && npMCI->nOtherStreams == 0) {
  1514. npMCI->wPlaybackAlg = MCIAVI_ALG_AUDIOONLY;
  1515. } else if (npMCI->dwFlags & MCIAVI_REVERSE || npMCI->pf) {
  1516. // avifile-handled files are always played as non-interleaved
  1517. // though audio buffering might be different
  1518. npMCI->wPlaybackAlg = MCIAVI_ALG_HARDDISK;
  1519. }
  1520. else if (npMCI->dwFlags & MCIAVI_NOTINTERLEAVED) {
  1521. #if 0
  1522. if (fCDFile)
  1523. npMCI->wPlaybackAlg = MCIAVI_ALG_CDROM;
  1524. else
  1525. #endif
  1526. if (fNetFile)
  1527. npMCI->wPlaybackAlg = MCIAVI_ALG_HARDDISK;
  1528. else
  1529. npMCI->wPlaybackAlg = MCIAVI_ALG_HARDDISK;
  1530. }
  1531. else {
  1532. if (fCDFile)
  1533. npMCI->wPlaybackAlg = MCIAVI_ALG_INTERLEAVED;
  1534. #if 0
  1535. else if (fNetFile)
  1536. npMCI->wPlaybackAlg = MCIAVI_ALG_HARDDISK;
  1537. else
  1538. npMCI->wPlaybackAlg = MCIAVI_ALG_HARDDISK;
  1539. #else
  1540. else if (fNetFile)
  1541. npMCI->wPlaybackAlg = MCIAVI_ALG_INTERLEAVED;
  1542. else
  1543. npMCI->wPlaybackAlg = MCIAVI_ALG_INTERLEAVED;
  1544. #endif
  1545. }
  1546. // Interleaved playback doesn't work well at very low speeds!
  1547. if ((npMCI->dwSpeedFactor < 100) &&
  1548. (npMCI->wPlaybackAlg != MCIAVI_ALG_HARDDISK) &&
  1549. (npMCI->wPlaybackAlg != MCIAVI_ALG_AUDIOONLY)) {
  1550. DPF(("Was going to play interleaved, but speed < 10%% of normal...\n"));
  1551. npMCI->wPlaybackAlg = MCIAVI_ALG_HARDDISK;
  1552. }
  1553. #if 0
  1554. //
  1555. // sigh! we need to always have the index read now, so we do it in
  1556. // aviopen
  1557. //
  1558. /* Be sure the index has been read, if we need it. */
  1559. if (npMCI->hpFrameIndex == NULL)
  1560. if (npMCI->wPlaybackAlg != MCIAVI_ALG_INTERLEAVED || npMCI->lFrom > 0)
  1561. ReadIndex(npMCI);
  1562. #endif
  1563. #ifdef DEBUG
  1564. switch (npMCI->wPlaybackAlg) {
  1565. case MCIAVI_ALG_INTERLEAVED:
  1566. Assert(!(npMCI->dwFlags & MCIAVI_NOTINTERLEAVED));
  1567. DPF(("playing a interleaved file\n"));
  1568. break;
  1569. case MCIAVI_ALG_CDROM:
  1570. Assert(npMCI->dwFlags & MCIAVI_NOTINTERLEAVED);
  1571. DPF(("playing a non interleaved file from CD-ROM\n"));
  1572. break;
  1573. case MCIAVI_ALG_HARDDISK:
  1574. if (npMCI->dwFlags & MCIAVI_NOTINTERLEAVED)
  1575. DPF(("random access play (non-interleaved file)\n"));
  1576. else
  1577. DPF(("random access play (interleaved file)\n"));
  1578. break;
  1579. case MCIAVI_ALG_AUDIOONLY:
  1580. Assert(npMCI->nAudioStreams);
  1581. DPF(("audio-only!\n"));
  1582. break;
  1583. default:
  1584. Assert(0);
  1585. break;
  1586. }
  1587. #endif
  1588. #if 0
  1589. //
  1590. // set a MMIO buffer if we are playing interleaved off a non cd-rom
  1591. //
  1592. if (npMCI->hmmio && fNetFile && npMCI->wPlaybackAlg == MCIAVI_ALG_INTERLEAVED) {
  1593. #define BUFFER_SIZE (32l*1024)
  1594. if (npMCI->lpMMIOBuffer == NULL) {
  1595. DPF(("Using %u byte MMIO buffer...\n", BUFFER_SIZE));
  1596. npMCI->lpMMIOBuffer = AllocMem(BUFFER_SIZE);
  1597. mmioSetBuffer(npMCI->hmmio, npMCI->lpMMIOBuffer, BUFFER_SIZE, 0);
  1598. }
  1599. //!!! should we do this for a seek?
  1600. //!!! should we free this in CleanUpPlay?
  1601. }
  1602. else {
  1603. if (npMCI->lpMMIOBuffer != NULL)
  1604. FreeMem(npMCI->lpMMIOBuffer);
  1605. npMCI->lpMMIOBuffer = NULL;
  1606. if (npMCI->hmmio)
  1607. mmioSetBuffer(npMCI->hmmio, NULL, 0, 0);
  1608. }
  1609. #endif
  1610. // !!!!
  1611. #ifdef DEBUG
  1612. gwHurryTolerance = mmGetProfileInt(szIni, TEXT("Hurry"), 2);
  1613. gwSkipTolerance = mmGetProfileInt(szIni, TEXT("Skip"), gwHurryTolerance * 2);
  1614. gwMaxSkipEver = mmGetProfileInt(szIni, TEXT("MaxSkip"), max(60, gwSkipTolerance * 10));
  1615. #endif
  1616. gfUseGetPosition = DEFAULTUSEGETPOSITION; // !!! Detect WSS 1.0, 2.0?
  1617. gfUseGetPosition = mmGetProfileInt(szIni, TEXT("GetPosition"), gfUseGetPosition);
  1618. giGetPositionAdjust = mmGetProfileInt(szIni, TEXT("GetPositionAdjust"), 100);
  1619. #ifdef DEBUG
  1620. if (gfUseGetPosition) {
  1621. DPF2(("Using waveOutGetPosition adjusted by %dms instead of done bits...\n", giGetPositionAdjust));
  1622. } else {
  1623. DPF2(("NOT using waveOutGetPosition\n"));
  1624. }
  1625. #endif
  1626. Assert(npMCI->lTo <= npMCI->lFrames);
  1627. Assert(npMCI->lFrom >= 0);
  1628. /* Clear out variables, so we'll know what needs to be released. */
  1629. /* Access to these variables should only ever be on the task thread */
  1630. npMCI->hWave = NULL;
  1631. npMCI->lpAudio = NULL;
  1632. npMCI->lpBuffer = NULL;
  1633. npMCI->dwBufferSize = 0L;
  1634. npMCI->wABFull = 0;
  1635. npMCI->dwSkippedFrames = 0L;
  1636. npMCI->dwFramesSeekedPast = 0L;
  1637. npMCI->dwAudioBreaks = 0L;
  1638. npMCI->dwTotalMSec = 0;
  1639. npMCI->dwLastDrawTime = 0;
  1640. npMCI->dwLastReadTime = 0;
  1641. npMCI->dwBufferedVideo = 0;
  1642. npMCI->dwPauseTime = 0;
  1643. npMCI->dwTimingStart = 0;
  1644. /* Figure out how fast we're playing.... */
  1645. if (npMCI->dwSpeedFactor)
  1646. npMCI->dwPlayMicroSecPerFrame = muldiv32(npMCI->dwMicroSecPerFrame,
  1647. 1000L,
  1648. npMCI->dwSpeedFactor);
  1649. else
  1650. npMCI->dwPlayMicroSecPerFrame = 0; // Special "play every frame" mode
  1651. /* If we're already at the end, and we're going to repeat from the
  1652. ** start of the file, just repeat now.
  1653. */
  1654. if ((npMCI->lFrom == npMCI->lTo) &&
  1655. (npMCI->dwFlags & MCIAVI_REPEATING) &&
  1656. (npMCI->lFrom != npMCI->lRepeatFrom)) {
  1657. DPF(("Repeating from beginning before we've even started....\n"));
  1658. npMCI->lFrom = npMCI->lRepeatFrom;
  1659. }
  1660. if (npMCI->lFrom == npMCI->lTo) {
  1661. npMCI->dwFlags |= MCIAVI_SEEKING;
  1662. npMCI->dwFlags &= ~(MCIAVI_REVERSE | MCIAVI_REPEATING);
  1663. }
  1664. if (npMCI->dwFlags & MCIAVI_SEEKING)
  1665. goto PlayWithoutWave;
  1666. if (npMCI->hicDraw) {
  1667. ICGetBuffersWanted(npMCI->hicDraw, &npMCI->dwBufferedVideo);
  1668. }
  1669. #ifdef DEBUG
  1670. npMCI->dwBufferedVideo = mmGetProfileInt(szIni, TEXT("Buffer"), (int) npMCI->dwBufferedVideo);
  1671. #endif
  1672. if (npMCI->dwFlags & MCIAVI_REVERSE) {
  1673. npMCI->dwBufferedVideo = 0;
  1674. }
  1675. if (npMCI->dwBufferedVideo) {
  1676. DPF(("Buffering %lu frames of video ahead....\n", npMCI->dwBufferedVideo));
  1677. }
  1678. //
  1679. // now initialize the audio stream
  1680. //
  1681. /* Open up our wave output device, if appropriate.
  1682. * Appropriate means that there are audio streams,
  1683. * that we are not muted,
  1684. * that the user has not turned sound off
  1685. * that we have not had the wave device stolen
  1686. * and that the wave stream is ok
  1687. */
  1688. if ((npMCI->nAudioStreams > 0)
  1689. && (npMCI->dwFlags & MCIAVI_PLAYAUDIO)
  1690. && !(npMCI->dwFlags & MCIAVI_LOSEAUDIO)
  1691. && !(npMCI->dwOptionFlags & MCIAVIO_NOSOUND)
  1692. && (npMCI->dwPlayMicroSecPerFrame != 0)) {
  1693. npMCI->dwTaskError = SetUpAudio(npMCI, TRUE);
  1694. if ((npMCI->dwTaskError == MCIERR_OUT_OF_MEMORY) &&
  1695. (npMCI->wPlaybackAlg != MCIAVI_ALG_AUDIOONLY)) {
  1696. DPF(("Not enough memory to play audio; continuing onward....\n"));
  1697. CleanUpAudio(npMCI);
  1698. npMCI->dwTaskError = 0;
  1699. }
  1700. if (npMCI->dwTaskError == MCIERR_WAVE_OUTPUTSINUSE) {
  1701. #ifdef STEALWAVE
  1702. //
  1703. // we did not get a wave device, time to go steal one.
  1704. //
  1705. // only do this if we got a real play command
  1706. // from the user, and not a internal play command
  1707. // (like when repeating or restarting)
  1708. //
  1709. // MCIAVI_NEEDTOSHOW is set when the play command
  1710. // came in through graphic.c (ie from the outside world)
  1711. //
  1712. if (npMCI->dwFlags & MCIAVI_NEEDTOSHOW) {
  1713. if (StealWaveDevice(npMCI))
  1714. // Some other AVI task was prepared to release the
  1715. // wave device that they were holding. This gives us
  1716. // a second chance to set up for playing audio.
  1717. npMCI->dwTaskError = SetUpAudio(npMCI, TRUE);
  1718. }
  1719. #endif // STEALWAVE
  1720. if (npMCI->dwTaskError == MCIERR_WAVE_OUTPUTSINUSE) {
  1721. //
  1722. // even though we did not steal the wave device we still
  1723. // want it if it becomes available
  1724. //
  1725. npMCI->dwFlags |= MCIAVI_LOSTAUDIO; // we want it
  1726. }
  1727. }
  1728. /*
  1729. * We will fail to play even without a wave device available
  1730. * in the following circumstances:
  1731. *
  1732. * 1: Playback algorithm is audio only
  1733. * 2: Wave failure is not ...OUTPUTSINUSE or ...OUTPUTSUNSUITABLE
  1734. *
  1735. * In all other cases we continue and play without wave
  1736. */
  1737. if (npMCI->dwTaskError) {
  1738. if ( ((npMCI->dwTaskError != MCIERR_WAVE_OUTPUTSINUSE) &&
  1739. (npMCI->dwTaskError != MCIERR_WAVE_OUTPUTSUNSUITABLE))
  1740. || (npMCI->wPlaybackAlg == MCIAVI_ALG_AUDIOONLY)
  1741. )
  1742. {
  1743. // Must call CleanUpAudio(npMCI) to release buffers
  1744. return npMCI->dwTaskError;
  1745. }
  1746. // Reset the error and continue
  1747. npMCI->dwTaskError = 0;
  1748. }
  1749. } else {
  1750. DPF2(("Playing silently, nAudioStreams=%d, PlayAudio=%x\n",
  1751. npMCI->nAudioStreams, npMCI->dwFlags & MCIAVI_PLAYAUDIO));
  1752. // Was someone stealing our wave device?
  1753. npMCI->dwFlags &= ~MCIAVI_LOSEAUDIO; // OK - reset the flag
  1754. }
  1755. PlayWithoutWave:
  1756. return(PrepareToPlay2(npMCI));
  1757. }
  1758. INLINE STATICFN DWORD NEAR PASCAL PrepareToPlay2(NPMCIGRAPHIC npMCI)
  1759. {
  1760. int stream;
  1761. UINT w;
  1762. DWORD dwPosition;
  1763. if (npMCI->dwFlags & MCIAVI_NEEDTOSHOW) {
  1764. ShowStage(npMCI);
  1765. }
  1766. /* Get and prepare the DC we're going to be playing into */
  1767. // must hold the critsec when getting dc to avoid
  1768. // interaction with window thread calling DeviceRealize
  1769. EnterHDCCrit(npMCI);
  1770. if (npMCI->hdc == NULL) {
  1771. npMCI->hdc = GetDC(npMCI->hwndPlayback); // Shouldn't use cached DC!
  1772. if (npMCI->hdc == NULL) {
  1773. LeaveHDCCrit(npMCI);
  1774. return MCIERR_DRIVER_INTERNAL;
  1775. }
  1776. npMCI->dwFlags |= MCIAVI_RELEASEDC;
  1777. }
  1778. if (npMCI->dwFlags & MCIAVI_SEEKING) {
  1779. //
  1780. // audio only
  1781. //
  1782. if (npMCI->nVideoStreams == 0 && npMCI->nOtherStreams == 0) {
  1783. npMCI->lCurrentFrame = npMCI->lFrom;
  1784. LeaveHDCCrit(npMCI);
  1785. return 0;
  1786. }
  1787. }
  1788. /* Start up the external decompressor, if any */
  1789. /* !!!We should check these for errors */
  1790. if (!DrawBegin(npMCI, NULL)) {
  1791. LeaveHDCCrit(npMCI);
  1792. return npMCI->dwTaskError ? npMCI->dwTaskError : MCIERR_DRIVER_INTERNAL;
  1793. }
  1794. if (!(npMCI->dwFlags & MCIAVI_SEEKING)) {
  1795. PrepareDC(npMCI);
  1796. }
  1797. // critsec just held around getting and preparing dc - look at
  1798. // InternalRealize to see the function we are protecting against.
  1799. LeaveHDCCrit(npMCI);
  1800. /*
  1801. ** what if selecting the palette causes palette changes? we should
  1802. ** yield and let the palette changes happen.
  1803. */
  1804. if (npMCI->hicDraw && !(npMCI->dwFlags & MCIAVI_SEEKING) &&
  1805. (npMCI->dwBufferedVideo > 0)) {
  1806. ICDrawFlush(npMCI->hicDraw);
  1807. npMCI->lFrameDrawn = (- (LONG) npMCI->wEarlyRecords) - 1;
  1808. }
  1809. if (npMCI->dwFlags & MCIAVI_FULLSCREEN) {
  1810. /* Clear out key state flags:
  1811. ** We watch for escape, space, and the left button.
  1812. ** Unfortunately, we must look for LBUTTON and RBUTTON in case
  1813. ** the user has switched mouse buttons. In that instance, the
  1814. ** UI might believe that the Left mouse button is physically the
  1815. ** right-hand one, but GetAsyncKeyState looks at the physical
  1816. ** left-hand mouse button.
  1817. */
  1818. GetAsyncKeyState(VK_ESCAPE);
  1819. GetAsyncKeyState(VK_SPACE);
  1820. GetAsyncKeyState(VK_LBUTTON);
  1821. GetAsyncKeyState(VK_RBUTTON);
  1822. }
  1823. /* Figure out where in the file to start playing from */
  1824. CalculateTargetFrame(npMCI);
  1825. // !!! ACK: We're starting from after where we planned to finish....
  1826. if ((npMCI->dwFlags & MCIAVI_REVERSE) &&
  1827. (npMCI->lCurrentFrame <= npMCI->lTo)) {
  1828. npMCI->dwFlags |= MCIAVI_SEEKING;
  1829. }
  1830. // !!! This should be in CalcTarget
  1831. if (npMCI->dwFlags & MCIAVI_SEEKING)
  1832. npMCI->lTo = npMCI->lRealStart;
  1833. //
  1834. // start all the streams
  1835. //
  1836. for (stream = 0; stream < npMCI->streams; stream++) {
  1837. STREAMINFO *psi = SI(stream);
  1838. #ifdef USEAVIFILE
  1839. if (!(npMCI->dwFlags & MCIAVI_SEEKING)) {
  1840. if (SI(stream)->ps) {
  1841. AVIStreamBeginStreaming(SI(stream)->ps,
  1842. MovieToStream(SI(stream), npMCI->lFrom),
  1843. MovieToStream(SI(stream), npMCI->lTo),
  1844. npMCI->dwPlayMicroSecPerFrame); // !!!
  1845. }
  1846. }
  1847. #endif
  1848. //
  1849. // NOTE DrawBegin() handled the default draw guy
  1850. //
  1851. if (psi->hicDraw && psi->hicDraw != npMCI->hicDraw) {
  1852. DWORD dw;
  1853. dw = ICDrawBegin(psi->hicDraw,
  1854. (npMCI->dwFlags & MCIAVI_FULLSCREEN) ?
  1855. ICDRAW_FULLSCREEN : ICDRAW_HDC,
  1856. npMCI->hpal, // palette to draw with
  1857. npMCI->hwndPlayback, // window to draw to
  1858. npMCI->hdc, // HDC to draw to
  1859. RCX(psi->rcDest),
  1860. RCY(psi->rcDest),
  1861. RCW(psi->rcDest),
  1862. RCH(psi->rcDest),
  1863. SI(stream)->lpFormat,
  1864. RCX(psi->rcSource),
  1865. RCY(psi->rcSource),
  1866. RCW(psi->rcSource),
  1867. RCH(psi->rcSource),
  1868. muldiv32(psi->sh.dwRate, npMCI->dwSpeedFactor, 1000),
  1869. psi->sh.dwScale);
  1870. if ((LONG)dw < 0) {
  1871. // !!! Error checking?
  1872. DPF(("Draw handler failed ICDrawBegin() (err = %ld)\n", dw));
  1873. }
  1874. //
  1875. // tell the draw handler the play range
  1876. //
  1877. ICDrawStartPlay(psi->hicDraw,psi->lPlayFrom, psi->lPlayTo);
  1878. }
  1879. }
  1880. //
  1881. // tell the draw handler the play range
  1882. //
  1883. if (npMCI->hicDraw) {
  1884. ICDrawStartPlay(npMCI->hicDraw,npMCI->lRealStart,npMCI->lTo);
  1885. }
  1886. //
  1887. // seek to the right place in the file.
  1888. //
  1889. dwPosition = CalculatePosition(npMCI);
  1890. if (dwPosition == 0) {
  1891. return MCIERR_DRIVER_INTERNAL;
  1892. }
  1893. #ifdef AVIREADMANY
  1894. //
  1895. // see if we want to try to read two records at a shot, this
  1896. // should cut down the time spent in DOS doing reads.
  1897. //
  1898. // we only can do this if we have a index, and the buffer
  1899. // sizes are "small enough"
  1900. //
  1901. // if reading 2 buffers works good how about 3? 4?
  1902. //
  1903. // this helps on CD's and Networks but makes things slower
  1904. // on KenO's hard disk, so dont do hard disks.
  1905. //
  1906. // default is read many when coming from a Network, this is
  1907. // better than the old mmioSetBuffer() we used to do.
  1908. //
  1909. if (npMCI->uDriveType == DRIVE_REMOTE)
  1910. npMCI->fReadMany = TRUE;
  1911. else
  1912. npMCI->fReadMany = FALSE;
  1913. if (npMCI->wPlaybackAlg == MCIAVI_ALG_INTERLEAVED &&
  1914. npMCI->dwSuggestedBufferSize <= 30*1024 &&
  1915. mmGetProfileInt(szIni, TEXT("ReadMany"), npMCI->fReadMany) &&
  1916. npMCI->hpFrameIndex) {
  1917. npMCI->dwBufferSize = npMCI->dwSuggestedBufferSize * 2;
  1918. npMCI->fReadMany = TRUE;
  1919. }
  1920. else {
  1921. npMCI->fReadMany = FALSE;
  1922. }
  1923. if (npMCI->fReadMany) {
  1924. DPF(("MCIAVI: reading two records at once (%ld bytes).\n", npMCI->dwBufferSize));
  1925. npMCI->lLastRead = npMCI->lCurrentFrame - 2;
  1926. }
  1927. #endif
  1928. AllocateReadBuffer(npMCI);
  1929. // look for palette changes between the last place we read and where
  1930. // we're starting....
  1931. ProcessPaletteChanges(npMCI, npMCI->lVideoStart);
  1932. if (npMCI->hmmio) {
  1933. /* Seek to the start of frame we're playing from */
  1934. mmioSeek(npMCI->hmmio, dwPosition, SEEK_SET);
  1935. }
  1936. #ifdef AVIREAD
  1937. /* start the async read object if we are using interleaved
  1938. * and therefore consecutive reads
  1939. */
  1940. if (npMCI->wPlaybackAlg == MCIAVI_ALG_INTERLEAVED) {
  1941. /* start async reader - allocates itself new buffers */
  1942. npMCI->hAviRd = avird_startread(mciaviReadBuffer, (DWORD_PTR) npMCI,
  1943. npMCI->dwNextRecordSize,
  1944. npMCI->lCurrentFrame,
  1945. min(npMCI->lTo+1, npMCI->lFrames));
  1946. if (!npMCI->hAviRd) {
  1947. DPF(("async read failed - reading synchronously\n"));
  1948. ResizeReadBuffer(npMCI, npMCI->dwNextRecordSize);
  1949. }
  1950. } else {
  1951. npMCI->hAviRd = NULL;
  1952. }
  1953. if (!npMCI->hAviRd)
  1954. #endif
  1955. {
  1956. if (!npMCI->lpBuffer) {
  1957. return MCIERR_OUT_OF_MEMORY;
  1958. }
  1959. }
  1960. if (npMCI->hWave) {
  1961. TIMESTART(timeAudio);
  1962. if (npMCI->wPlaybackAlg == MCIAVI_ALG_HARDDISK ||
  1963. npMCI->wPlaybackAlg == MCIAVI_ALG_AUDIOONLY) {
  1964. /* Load audio into our buffers */
  1965. for (w = 0; w < npMCI->wABs; w++)
  1966. KeepPlayingAudio(npMCI);
  1967. } else if (npMCI->wPlaybackAlg == MCIAVI_ALG_CDROM) {
  1968. //!!!!
  1969. npMCI->wPlaybackAlg = MCIAVI_ALG_HARDDISK;
  1970. }
  1971. TIMEEND(timeAudio);
  1972. }
  1973. return 0L; /* Success! */
  1974. }
  1975. /******************************************************************************
  1976. *****************************************************************************/
  1977. void NEAR PASCAL CleanUpPlay(NPMCIGRAPHIC npMCI)
  1978. {
  1979. int stream;
  1980. if (npMCI->wTaskState == TASKPLAYING) {
  1981. if (npMCI->hicDraw) {
  1982. ICDrawStop(npMCI->hicDraw);
  1983. ICDrawStopPlay(npMCI->hicDraw);
  1984. }
  1985. for (stream = 0; stream < npMCI->streams; stream++) {
  1986. if (SI(stream)->hicDraw) {
  1987. ICDrawStop(SI(stream)->hicDraw);
  1988. ICDrawStopPlay(SI(stream)->hicDraw);
  1989. }
  1990. }
  1991. if (npMCI->hWave) {
  1992. waveOutRestart(npMCI->hWave); // some wave devices need this
  1993. waveOutReset(npMCI->hWave);
  1994. }
  1995. } else if (npMCI->wTaskState == TASKCUEING) {
  1996. if (npMCI->hicDraw) {
  1997. /* Kick the device in the head to make sure it draws when we seek. */
  1998. ICDrawRenderBuffer(npMCI->hicDraw);
  1999. }
  2000. }
  2001. if (!(npMCI->dwFlags & MCIAVI_SEEKING) &&
  2002. (npMCI->dwBufferedVideo > 0)) {
  2003. ICDrawFlush(npMCI->hicDraw);
  2004. npMCI->lFrameDrawn = (- (LONG) npMCI->wEarlyRecords) - 1;
  2005. }
  2006. /* end drawing this will leave fullscreen mode etc. */
  2007. DrawEnd(npMCI);
  2008. for (stream = 0; stream < npMCI->streams; stream++) {
  2009. if (SI(stream)->hicDraw) {
  2010. LRESULT dw;
  2011. dw = ICDrawEnd(SI(stream)->hicDraw);
  2012. // !!! Error checking?
  2013. }
  2014. #ifdef USEAVIFILE
  2015. if (!(npMCI->dwFlags & MCIAVI_SEEKING)) {
  2016. if (SI(stream)->ps) {
  2017. AVIStreamEndStreaming(SI(stream)->ps);
  2018. }
  2019. }
  2020. #endif
  2021. }
  2022. /* Clean up and close our wave output device. */
  2023. if (npMCI->hWave) {
  2024. Assert(!(npMCI->dwFlags & MCIAVI_LOSTAUDIO));
  2025. // We should never hold the
  2026. // wave device AND have MCIAVI_LOSTAUDIO turned on.
  2027. CleanUpAudio(npMCI);
  2028. #ifdef STEALWAVE
  2029. //
  2030. // if we are not being forced to give up the audio try to
  2031. // give it to someone. Unless we are repeating. In which
  2032. // case someone might steal it from us, but we do not want
  2033. // to waste time looking for another user. We have it; we
  2034. // will keep it until forced to release it.
  2035. //
  2036. if (!(npMCI->dwFlags & MCIAVI_NEEDTOSHOW) &&
  2037. !(npMCI->dwFlags & MCIAVI_REPEATING) &&
  2038. !(npMCI->dwFlags & MCIAVI_UPDATING))
  2039. GiveWaveDevice(npMCI);
  2040. else {
  2041. DPF2(("Not giving the wave device away, flags=%x\n",npMCI->dwFlags));
  2042. }
  2043. #endif
  2044. } else {
  2045. //
  2046. // done playing, we dont want a wave device any more
  2047. // LATER: do we really want to turn this flag off if we are
  2048. // repeating? Today it is benign as the flag will be turned
  2049. // back on again when the video restarts. It will then try and
  2050. // open the audio, fail because it is in use, and turn on LOSTAUDIO.
  2051. // It would be more efficient to rely on the wave device being
  2052. // returned to us and not try to reopen it.
  2053. //
  2054. npMCI->dwFlags &= ~MCIAVI_LOSTAUDIO;
  2055. }
  2056. /* Release the DC we played into. */
  2057. // worker thread must hold critsec round all access to hdc
  2058. // (can be used by DeviceRealize on winproc thread)
  2059. EnterHDCCrit(npMCI);
  2060. if (npMCI->hdc) {
  2061. //
  2062. // we MUST call this otherwise our palette will stay selected
  2063. // as the foreground palette and it may get deleted (ie by
  2064. // DrawDibBegin) while still the foreground palette and GDI
  2065. // get's real pissed about this.
  2066. //
  2067. UnprepareDC(npMCI);
  2068. #if 0
  2069. if (npMCI->dwFlags & MCIAVI_ANIMATEPALETTE)
  2070. RealizePalette(npMCI->hdc);
  2071. #endif
  2072. if (npMCI->dwFlags & MCIAVI_RELEASEDC) {
  2073. ReleaseDC(npMCI->hwndPlayback, npMCI->hdc);
  2074. HDCCritCheckIn(npMCI);
  2075. npMCI->hdc = NULL;
  2076. npMCI->dwFlags &= ~MCIAVI_RELEASEDC;
  2077. }
  2078. }
  2079. LeaveHDCCrit(npMCI);
  2080. #ifdef AVIREAD
  2081. /* shut down async reader */
  2082. if (npMCI->hAviRd) {
  2083. avird_endread(npMCI->hAviRd);
  2084. npMCI->hAviRd = NULL;
  2085. } else
  2086. #endif
  2087. {
  2088. /* we weren't using async reader - so release the buffer we
  2089. * allocated
  2090. */
  2091. ReleaseReadBuffer(npMCI);
  2092. }
  2093. }
  2094. /******************************************************************************
  2095. *****************************************************************************/
  2096. // !!! Should this take a "how many frames to check for" parameter,
  2097. // in case we need to check for signals on several frames at once?
  2098. void NEAR PASCAL CheckSignals(NPMCIGRAPHIC npMCI, LONG lFrame)
  2099. {
  2100. LONG lTemp;
  2101. lTemp = npMCI->signal.dwPeriod == 0 ? lFrame :
  2102. (((lFrame - npMCI->signal.dwPosition) %
  2103. npMCI->signal.dwPeriod) +
  2104. npMCI->signal.dwPosition);
  2105. if ((DWORD) lTemp == npMCI->signal.dwPosition) {
  2106. /* Send the signal in the right time format */
  2107. SEND_DGVSIGNAL(npMCI->dwSignalFlags,
  2108. npMCI->signal.dwCallback,
  2109. 0,
  2110. (HANDLE) npMCI->wDevID,
  2111. npMCI->signal.dwUserParm,
  2112. ConvertFromFrames(npMCI, lFrame));
  2113. // !!! Needs to use time format at time of signal command!
  2114. }
  2115. }
  2116. /******************************************************************************
  2117. *****************************************************************************/
  2118. BOOL NEAR PASCAL WaitTillNextFrame(NPMCIGRAPHIC npMCI)
  2119. {
  2120. #ifdef _WIN32
  2121. LONG WaitForFrame;
  2122. #endif
  2123. LONG lMaxWait;
  2124. #ifdef DEBUG
  2125. int iWait = 0;
  2126. StatusBar(npMCI,2,1,4,iWait); // we should not wait more than 4 times...
  2127. #endif
  2128. /* Here we wait for a while if we're ahead
  2129. * of schedule (so that we can yield nicely instead of blocking
  2130. * in the driver, for instance, and also so that we'll work off
  2131. * faster devices.)
  2132. */
  2133. /* Always yield at least once in a while (every 8 frames ~ 1/2 sec)*/
  2134. if ((npMCI->lCurrentFrame % YIELDEVERY) == 0) {
  2135. TIMESTART(timeYield);
  2136. aviTaskCheckRequests(npMCI);
  2137. TIMEEND(timeYield);
  2138. }
  2139. if (npMCI->dwFlags & MCIAVI_WAVEPAUSED)
  2140. return TRUE;
  2141. if (TimeToQuit(npMCI))
  2142. return FALSE;
  2143. Assert(npMCI->wTaskState == TASKPLAYING);
  2144. // with the change to play the last frame of audio, these two asserts
  2145. // are no longer valid. We will wait until it's time for frame lTo+1
  2146. // and stop then (before attempting to read or draw it).
  2147. //AssertFrame(npMCI->lCurrentFrame - (LONG)npMCI->dwBufferedVideo);
  2148. //Assert(npMCI->lCurrentFrame <= npMCI->lTo);
  2149. Assert(!(npMCI->dwFlags & MCIAVI_REVERSE));
  2150. /* The maximum wait time is 95% of the correct frame rate, or 100ms
  2151. * (to cope with a very slow frame rate)
  2152. */
  2153. lMaxWait = min(100, muldiv32(npMCI->dwMicroSecPerFrame,
  2154. 950L,
  2155. (npMCI->dwSpeedFactor == 0 ?
  2156. 1000 : npMCI->dwSpeedFactor)));
  2157. if (HowLongTill(npMCI) > 0) {
  2158. while ((WaitForFrame=HowLongTill(npMCI)) > 0) {
  2159. ///////WAITING//////////
  2160. StatusBar(npMCI,2,1,4,++iWait);
  2161. // use sleep regardless of accuracy as polling hurts on NT
  2162. if (npMCI->msPeriodResolution > 0) {
  2163. // NOTE: There are no fudge factors in here. This code
  2164. // needs to be tuned to allow for the overhead of calculating
  2165. // the wait time, for the overhead of the timer, etc.
  2166. // Don't wait for more than one frame time at a time....
  2167. if (WaitForFrame > lMaxWait) {
  2168. WaitForFrame = lMaxWait;
  2169. }
  2170. DPF2(("Sleeping for %d milliseconds\n", WaitForFrame));
  2171. TIMESTART(timeWait);
  2172. Sleep(WaitForFrame);
  2173. TIMEEND(timeWait);
  2174. } else {
  2175. Sleep(0);
  2176. }
  2177. // check if anything interesting has happened
  2178. TIMESTART(timeYield);
  2179. aviTaskCheckRequests(npMCI);
  2180. TIMEEND(timeYield);
  2181. if (TimeToQuit(npMCI)) {
  2182. return FALSE;
  2183. }
  2184. }
  2185. } else {
  2186. // force some cpu idle time at least every 1/2 second, to ensure
  2187. // that other processes do get some time (eg for 16-bit hook procs).
  2188. /* Always yield at least once in a while (every 8 frames ~ 1/2 sec)*/
  2189. if ((npMCI->lCurrentFrame % YIELDEVERY) == 0) {
  2190. TIMESTART(timeWait);
  2191. Sleep(1);
  2192. TIMEEND(timeWait);
  2193. TIMESTART(timeYield);
  2194. aviTaskCheckRequests(npMCI);
  2195. TIMEEND(timeYield);
  2196. if (TimeToQuit(npMCI)) {
  2197. return FALSE;
  2198. }
  2199. }
  2200. }
  2201. return TRUE;
  2202. }
  2203. /* Idea: this should go from the current frame to the frame
  2204. ** we actually have to be at to start playing from.
  2205. **
  2206. ** If fPlaying is set, that means we're really going to play.
  2207. **
  2208. ** When this finishes:
  2209. ** lAudioStart is set to the first frame with meaningful audio info
  2210. ** lVideoStart is the first frame with meaningful video info
  2211. ** lRealStart is the first frame that's 'real', namely
  2212. ** the original value of lCurrentFrame. If the
  2213. ** SEEK EXACT flag is not set, then lRealStart may
  2214. ** actually not be what lCurrentFrame was, indicating
  2215. ** that play may start from somewhere else.
  2216. ** lCurrentFrame gets set to the first frame we have to read from.
  2217. **
  2218. ** !!! This also needs to look for "palette key frames" or something.
  2219. */
  2220. BOOL NEAR PASCAL CalculateTargetFrame(NPMCIGRAPHIC npMCI)
  2221. {
  2222. int i;
  2223. LONG lVideoPlace;
  2224. BOOL fForceBeginning = FALSE;
  2225. int lMovieStart=0xffffffff; // Max UINT (or -1 when signed)
  2226. int lStreamStart;
  2227. npMCI->lCurrentFrame = npMCI->lFrom;
  2228. npMCI->lRealStart = npMCI->lFrom;
  2229. //
  2230. // walk all streams and figure out where to start
  2231. //
  2232. for (i=0; i<npMCI->streams; i++) {
  2233. STREAMINFO *psi = SI(i);
  2234. if (!(psi->dwFlags & STREAM_ENABLED))
  2235. continue;
  2236. if (psi->dwFlags & STREAM_ERROR)
  2237. continue;
  2238. if (psi->dwFlags & STREAM_AUDIO)
  2239. continue;
  2240. //
  2241. // map from movie time to stream time.
  2242. //
  2243. psi->lPlayFrom = MovieToStream(psi, npMCI->lFrom);
  2244. psi->lPlayTo = MovieToStream(psi, npMCI->lTo);
  2245. psi->dwFlags &= ~STREAM_ACTIVE;
  2246. //
  2247. // is this stream part of play?
  2248. //
  2249. if (psi->lPlayFrom < psi->lStart && psi->lPlayTo < psi->lStart)
  2250. continue;
  2251. if (psi->lPlayFrom >= psi->lEnd && psi->lPlayTo >= psi->lEnd)
  2252. continue;
  2253. psi->dwFlags |= STREAM_ACTIVE;
  2254. psi->lPlayFrom = BOUND(psi->lPlayFrom,psi->lStart,psi->lEnd);
  2255. psi->lPlayTo = BOUND(psi->lPlayTo, psi->lStart,psi->lEnd);
  2256. psi->lPlayStart = FindPrevKeyFrame(npMCI,psi,psi->lPlayFrom);
  2257. //
  2258. // if the main frame is invalid invalidate the stream too.
  2259. //
  2260. if (npMCI->lFrameDrawn <= (-(LONG)npMCI->wEarlyRecords)) {
  2261. psi->lFrameDrawn = -4242;
  2262. }
  2263. //
  2264. // if we have a drawn frame use it!
  2265. //
  2266. if ((psi->lFrameDrawn > psi->lPlayStart) &&
  2267. (psi->lFrameDrawn <= psi->lPlayFrom))
  2268. psi->lPlayStart = npMCI->lFrameDrawn + 1;
  2269. lStreamStart = StreamToMovie(psi, (DWORD)psi->lPlayStart);
  2270. if ((DWORD)lMovieStart > (DWORD)lStreamStart) {
  2271. (DWORD)lMovieStart = (DWORD)lStreamStart;
  2272. }
  2273. //
  2274. // if seek exactly is off start play at the key frame
  2275. //
  2276. if (!(npMCI->dwOptionFlags & MCIAVIO_SEEKEXACT)) {
  2277. if (psi->lPlayFrom == psi->lPlayTo)
  2278. psi->lPlayTo = psi->lPlayStart;
  2279. psi->lPlayFrom = psi->lPlayStart;
  2280. //!!! is this right for reverse?
  2281. if (StreamToMovie(psi, psi->lPlayFrom) < npMCI->lFrom) {
  2282. // npMCI->lRealStart = StreamToMovie(psi, psi->lPlayFrom);
  2283. // npMCI->lFrom = npMCI->lRealStart;
  2284. }
  2285. }
  2286. // if (StreamToMovie(psi, psi->lPlayStart) < npMCI->lCurrentFrame)
  2287. // npMCI->lCurrentFrame = StreamToMovie(psi, psi->lPlayStart);
  2288. DPF(("CalculateTargetFrame: Stream #%d: from:%ld, to:%ld, start:%ld\n", i, psi->lPlayFrom, psi->lPlayTo, psi->lPlayStart));
  2289. }
  2290. //
  2291. // we are done with now special case the video and audio streams.
  2292. // note: if lMovieStart has NOT been altered above then it will be -1
  2293. //
  2294. if (npMCI->lFrom < lMovieStart) {
  2295. npMCI->lFrom = lMovieStart;
  2296. }
  2297. /* If we're starting from the beginning, don't force the index
  2298. ** to be read, but use it if we've already read it.
  2299. */
  2300. if (npMCI->lFrom == 0 && npMCI->hpFrameIndex == NULL)
  2301. goto ForceBeginning;
  2302. if (!npMCI->pbiFormat) {
  2303. npMCI->lVideoStart = npMCI->lFrom;
  2304. if (npMCI->lVideoStart >= npMCI->lFrames)
  2305. npMCI->lVideoStart = npMCI->lFrames - 1;
  2306. lVideoPlace = npMCI->lVideoStart;
  2307. } else
  2308. if (npMCI->dwFlags & MCIAVI_HASINDEX) {
  2309. if (npMCI->hpFrameIndex == NULL)
  2310. goto ForceBeginning;
  2311. //
  2312. // get nearest key frame
  2313. //
  2314. npMCI->lVideoStart = FramePrevKey(npMCI->lFrom);
  2315. if (npMCI->lVideoStart) {
  2316. lVideoPlace = npMCI->lVideoStart;
  2317. } else {
  2318. /* Didn't find a key frame--retreat to the beginning. */
  2319. npMCI->lVideoStart = -(LONG)npMCI->wEarlyVideo;
  2320. lVideoPlace = 0;
  2321. }
  2322. if ((npMCI->lFrameDrawn > npMCI->lVideoStart) &&
  2323. (npMCI->lFrameDrawn <= npMCI->lFrom)) {
  2324. npMCI->lVideoStart = npMCI->lFrameDrawn + 1;
  2325. if (npMCI->lVideoStart >= npMCI->lFrames)
  2326. npMCI->lVideoStart = npMCI->lFrames - 1;
  2327. lVideoPlace = npMCI->lFrameDrawn;
  2328. }
  2329. } else {
  2330. /* Always go back to frame 0 */
  2331. ForceBeginning:
  2332. npMCI->lVideoStart = - (LONG) npMCI->wEarlyVideo;
  2333. lVideoPlace = 0;
  2334. fForceBeginning = TRUE;
  2335. }
  2336. if (!(npMCI->dwOptionFlags & MCIAVIO_SEEKEXACT)) {
  2337. npMCI->lRealStart = lVideoPlace;
  2338. }
  2339. if (npMCI->hWave) {
  2340. npMCI->lAudioStart = npMCI->lRealStart - (LONG) npMCI->wEarlyAudio;
  2341. }
  2342. if (npMCI->hWave && (npMCI->wPlaybackAlg == MCIAVI_ALG_INTERLEAVED))
  2343. npMCI->lCurrentFrame = min(npMCI->lAudioStart, npMCI->lVideoStart);
  2344. else
  2345. npMCI->lCurrentFrame = npMCI->lVideoStart;
  2346. if (npMCI->lRealStart < npMCI->lCurrentFrame)
  2347. npMCI->lCurrentFrame = npMCI->lRealStart;
  2348. if (fForceBeginning) {
  2349. if (npMCI->wPlaybackAlg == MCIAVI_ALG_INTERLEAVED)
  2350. npMCI->lCurrentFrame = - (LONG) npMCI->wEarlyRecords;
  2351. else
  2352. npMCI->lCurrentFrame = - (LONG) npMCI->wEarlyVideo;
  2353. }
  2354. if (npMCI->hWave) {
  2355. LONG l;
  2356. /* Figure out what sample of audio we should be starting at */
  2357. //
  2358. // convert frame number to block
  2359. //
  2360. npMCI->dwAudioPos = MovieToStream(npMCI->psiAudio, npMCI->lRealStart);
  2361. //
  2362. // now convert block to byte position
  2363. //
  2364. npMCI->dwAudioPos = npMCI->dwAudioPos * npMCI->pWF->nBlockAlign;
  2365. Assert(npMCI->dwAudioPos % npMCI->pWF->nBlockAlign == 0);
  2366. if (npMCI->dwAudioPos > npMCI->dwAudioLength)
  2367. npMCI->dwAudioPos = npMCI->dwAudioLength;
  2368. npMCI->dwAudioPlayed = 0L;
  2369. //
  2370. // convert the audio start back to a frame number.
  2371. // and posibly readjust the video start time.
  2372. //
  2373. l = npMCI->lRealStart - StreamToMovie(npMCI->psiAudio,
  2374. npMCI->dwAudioPos/npMCI->pWF->nBlockAlign);
  2375. if (l < 0)
  2376. DPF(("Audio will be ahead of the video by %ld frames\n", -l));
  2377. else if (l > 0)
  2378. DPF(("Audio will be behind the video by %ld frames\n", l));
  2379. }
  2380. #ifdef DEBUG
  2381. Assert(npMCI->lCurrentFrame < npMCI->lFrames);
  2382. if (npMCI->wPlaybackAlg == MCIAVI_ALG_INTERLEAVED) {
  2383. Assert(npMCI->lCurrentFrame >= - (LONG) npMCI->wEarlyRecords);
  2384. }
  2385. if (npMCI->hWave) {
  2386. Assert(npMCI->lAudioStart <= npMCI->lFrames);
  2387. }
  2388. Assert(npMCI->lVideoStart < npMCI->lFrames);
  2389. #endif
  2390. return TRUE;
  2391. }
  2392. /******************************************************************************
  2393. *****************************************************************************/
  2394. void ReturnToOriginalPalette(NPMCIGRAPHIC npMCI)
  2395. {
  2396. if (npMCI->bih.biClrUsed) {
  2397. hmemcpy(npMCI->argb, npMCI->argbOriginal,
  2398. npMCI->bih.biClrUsed * sizeof(RGBQUAD));
  2399. if (npMCI->pbiFormat->biBitCount == 8) {
  2400. hmemcpy((LPBYTE) npMCI->pbiFormat + npMCI->pbiFormat->biSize,
  2401. (LPBYTE) npMCI->argb,
  2402. sizeof(RGBQUAD) * npMCI->pbiFormat->biClrUsed);
  2403. }
  2404. npMCI->dwFlags |= MCIAVI_PALCHANGED;
  2405. npMCI->lLastPaletteChange = 0;
  2406. }
  2407. }
  2408. /* Returns the position in the file where the frame referenced
  2409. ** by lCurrentFrame is.
  2410. **
  2411. ** input npMCI->lCurrentFrame
  2412. **
  2413. ** output npMCI->dwNextRecordSize set correctly
  2414. ** npMCI->lLastRead set correctly
  2415. ** returns offset to read from
  2416. **
  2417. ** If there's an error, returns zero.
  2418. */
  2419. DWORD NEAR PASCAL CalculatePosition(NPMCIGRAPHIC npMCI)
  2420. {
  2421. DWORD dwPosition;
  2422. AssertFrame(npMCI->lCurrentFrame);
  2423. if (npMCI->pf || npMCI->nVideoStreams == 0)
  2424. return 1;
  2425. if (npMCI->lCurrentFrame + npMCI->wEarlyRecords == 0) {
  2426. ForceBeginning:
  2427. npMCI->lCurrentFrame = - (LONG)npMCI->wEarlyRecords;
  2428. //!!!BeforeBeginning:
  2429. dwPosition = npMCI->dwFirstRecordPosition;
  2430. npMCI->dwNextRecordSize = npMCI->dwFirstRecordSize;
  2431. npMCI->dwNextRecordType = npMCI->dwFirstRecordType;
  2432. } else if (npMCI->dwFlags & MCIAVI_HASINDEX) {
  2433. if (npMCI->hpFrameIndex == NULL)
  2434. goto ForceBeginning;
  2435. dwPosition = FrameOffset(npMCI->lCurrentFrame);
  2436. npMCI->dwNextRecordSize = FrameLength(npMCI->lCurrentFrame) + 8;
  2437. npMCI->dwNextRecordType = 0;
  2438. } else {
  2439. goto ForceBeginning;
  2440. }
  2441. npMCI->lLastRead = npMCI->lCurrentFrame - 1;
  2442. DPF3(("Frame %ld: Seeking to position %lX\n", npMCI->lCurrentFrame, dwPosition));
  2443. DPF3(("CalculatePosition: next record = %lu bytes.\n", npMCI->dwNextRecordSize));
  2444. mmioSeek(npMCI->hmmio, dwPosition, SEEK_SET);
  2445. return dwPosition;
  2446. }
  2447. /***************************************************************************
  2448. *
  2449. ***************************************************************************/
  2450. BOOL NEAR PASCAL ReadIndexChunk(NPMCIGRAPHIC npMCI, LONG iIndex)
  2451. {
  2452. Assert(iIndex >= 0 && iIndex < (LONG)npMCI->macIndex);
  2453. return ReadBuffer(npMCI, (LONG)IndexOffset(iIndex), (LONG)IndexLength(iIndex) + 8);
  2454. }
  2455. /***************************************************************************
  2456. *
  2457. * @doc INTERNAL MCIAVI
  2458. *
  2459. * @api void | DealWithOtherStreams | does what is says
  2460. *
  2461. * this function is called inside of the non-interlaved play loop.
  2462. * it's mission is to catch the "other" streams up to the current time.
  2463. *
  2464. * right now all we do is go to key frames, we should fix this
  2465. *
  2466. * @parm NPMCIGRAPHIC | npMCI | pointer to instance data block.
  2467. *
  2468. ***************************************************************************/
  2469. STATICFN INLINE void DealWithOtherStreams(NPMCIGRAPHIC npMCI, LONG lFrame)
  2470. {
  2471. int i;
  2472. STREAMINFO *psi;
  2473. LONG lPos;
  2474. LONG err;
  2475. for (i=0; i<npMCI->streams; i++) {
  2476. // If this is the active audio or video stream, then ignore it
  2477. if ((i == npMCI->nVideoStream)
  2478. || (i == npMCI->nAudioStream))
  2479. continue;
  2480. psi = SI(i);
  2481. if (!(psi->dwFlags & STREAM_ENABLED))
  2482. continue;
  2483. if (psi->hicDraw == NULL)
  2484. continue;
  2485. lPos = MovieToStream(psi, lFrame);
  2486. if (lPos < psi->lPlayStart || lPos > psi->lPlayTo) {
  2487. DPF2(("OtherStream(%d): out of range lPos = %ld [%ld, %ld]\n", i, lPos, psi->lPlayStart, psi->lPlayTo));
  2488. continue;
  2489. }
  2490. //
  2491. // we have the right thing drawn now
  2492. //
  2493. // !!!we should not always go to a key frame.
  2494. //
  2495. //
  2496. if (psi->lFrameDrawn >= psi->lLastKey &&
  2497. psi->lFrameDrawn <= lPos &&
  2498. lPos < psi->lNextKey) {
  2499. DPF2(("OtherStream(%d) lPos = %ld, lFrameDrawn=%ld, NextKey=%ld\n", i, lPos, psi->lFrameDrawn, psi->lNextKey));
  2500. continue;
  2501. }
  2502. FindKeyFrame(npMCI, psi, lPos);
  2503. DPF2(("OtherStream(%d): pos=%ld (prev key=%ld, next key=%ld)\n",i,lPos,psi->lLastKey,psi->lNextKey));
  2504. lPos = psi->lLastKey;
  2505. if (!StreamRead(npMCI, psi, lPos)) {
  2506. DPF2(("StreamRead failed\n"));
  2507. continue;
  2508. } else {
  2509. DPF2(("Read stream, ThisRecordSize==%d, biSizeImage==%d\n",
  2510. npMCI->dwThisRecordSize, 0));
  2511. }
  2512. //
  2513. // now draw the data.
  2514. //
  2515. err = (LONG)ICDraw(psi->hicDraw, 0L, psi->lpFormat,
  2516. npMCI->lpBuffer,npMCI->dwThisRecordSize,
  2517. psi->lLastKey - psi->lPlayFrom);
  2518. if (err >= 0) {
  2519. psi->dwFlags &= ~STREAM_NEEDUPDATE;
  2520. psi->lFrameDrawn = lPos;
  2521. }
  2522. else {
  2523. DPF2(("Draw failed!\n"));
  2524. }
  2525. }
  2526. }
  2527. /***************************************************************************
  2528. *
  2529. * FindKeyFrame
  2530. *
  2531. * given a stream position, find the previous and next key frame
  2532. * cacheing the last ones found to make it sort of fast.
  2533. *
  2534. ***************************************************************************/
  2535. void NEAR PASCAL FindKeyFrame(NPMCIGRAPHIC npMCI, STREAMINFO *psi, LONG lPos)
  2536. {
  2537. if (psi == NULL)
  2538. psi = npMCI->psiVideo;
  2539. Assert(psi);
  2540. // AssertPos(psi, lPos);
  2541. //
  2542. // if we are in the current key range return it.
  2543. //
  2544. if (psi->lLastKey <= lPos && lPos < psi->lNextKey)
  2545. return;
  2546. if (lPos < psi->lStart || lPos >= psi->lEnd)
  2547. return;
  2548. //
  2549. // otherwise query from the stream
  2550. //
  2551. #ifdef USEAVIFILE
  2552. if (psi->ps) {
  2553. if (lPos == psi->lNextKey)
  2554. psi->lLastKey = psi->lNextKey;
  2555. else
  2556. psi->lLastKey = AVIStreamFindSample(psi->ps, lPos, FIND_KEY|FIND_PREV);
  2557. psi->lNextKey = AVIStreamFindSample(psi->ps, lPos+1, FIND_KEY|FIND_NEXT);
  2558. if (psi->lLastKey == -1)
  2559. ; // psi->lLastKey = psi->lStart;
  2560. if (psi->lNextKey == -1)
  2561. psi->lNextKey = psi->lEnd+1;
  2562. }
  2563. #endif
  2564. else if (psi->dwFlags & STREAM_VIDEO) {
  2565. //
  2566. // for a video stream either read our index or assume no key frames.
  2567. //
  2568. if (npMCI->hpFrameIndex && psi == npMCI->psiVideo) {
  2569. psi->lLastKey = FramePrevKey(lPos);
  2570. psi->lNextKey = FrameNextKey(lPos);
  2571. }
  2572. else {
  2573. psi->lLastKey = psi->lStart;
  2574. psi->lNextKey = psi->lEnd+1;
  2575. }
  2576. }
  2577. else {
  2578. //
  2579. // for a non-video stream assume all key frames
  2580. //
  2581. psi->lLastKey = lPos;
  2582. psi->lNextKey = lPos+1;
  2583. }
  2584. return;
  2585. }
  2586. /***************************************************************************
  2587. ***************************************************************************/
  2588. LONG NEAR PASCAL FindPrevKeyFrame(NPMCIGRAPHIC npMCI, STREAMINFO *psi, LONG lPos)
  2589. {
  2590. FindKeyFrame(npMCI, psi, lPos);
  2591. return psi->lLastKey;
  2592. }
  2593. /***************************************************************************
  2594. ***************************************************************************/
  2595. LONG NEAR PASCAL FindNextKeyFrame(NPMCIGRAPHIC npMCI, STREAMINFO *psi, LONG lPos)
  2596. {
  2597. FindKeyFrame(npMCI, psi, lPos);
  2598. return psi->lNextKey;
  2599. }
  2600. /***************************************************************************
  2601. ***************************************************************************/
  2602. BOOL NEAR PASCAL ProcessPaletteChanges(NPMCIGRAPHIC npMCI, LONG lFrame)
  2603. {
  2604. LONG iPalette;
  2605. LONG iFrame;
  2606. STREAMINFO *psi;
  2607. if (!(npMCI->dwFlags & MCIAVI_ANIMATEPALETTE))
  2608. return TRUE;
  2609. psi = npMCI->psiVideo;
  2610. Assert(psi);
  2611. #ifdef USEAVIFILE
  2612. if (psi->ps) {
  2613. DWORD dw;
  2614. //
  2615. // we are in the palette range nothing to do.
  2616. //
  2617. if (npMCI->lLastPaletteChange <= lFrame &&
  2618. npMCI->lNextPaletteChange > lFrame) {
  2619. return TRUE;
  2620. }
  2621. dw = psi->cbFormat;
  2622. //!!! should be psi->lpFormat
  2623. if (AVIStreamReadFormat(psi->ps, lFrame, npMCI->pbiFormat, &dw) != 0) {
  2624. DOUT("Unable to read Stream format\n");
  2625. return FALSE;
  2626. }
  2627. npMCI->lLastPaletteChange = lFrame;
  2628. npMCI->lNextPaletteChange = AVIStreamFindSample(psi->ps, lFrame+1, FIND_NEXT|FIND_FORMAT);
  2629. if (npMCI->lNextPaletteChange == -1)
  2630. npMCI->lNextPaletteChange = npMCI->lFrames+2;
  2631. npMCI->dwFlags |= MCIAVI_PALCHANGED;
  2632. return TRUE;
  2633. }
  2634. #endif
  2635. DPF2(("Looking for palette changes at %ld, last=%ld\n", lFrame, npMCI->lLastPaletteChange));
  2636. if (lFrame < npMCI->lLastPaletteChange) {
  2637. ReturnToOriginalPalette(npMCI);
  2638. }
  2639. /* If there's no index, assume we're starting from the beginning
  2640. ** and thus we don't have to worry about palette changes.
  2641. */
  2642. if (npMCI->hpFrameIndex == NULL)
  2643. return TRUE;
  2644. //
  2645. // walk from the last palette change to the current frame, and apply any
  2646. // palette changes we find.
  2647. //
  2648. for (iFrame = npMCI->lLastPaletteChange,
  2649. iPalette = FramePalette(iFrame);
  2650. iFrame <= lFrame;
  2651. iFrame++) {
  2652. if (iPalette != FramePalette(iFrame)) {
  2653. iPalette = FramePalette(iFrame);
  2654. /* We've found a palette change we need to deal with */
  2655. DPF2(("Processing palette change at frame %ld.\n", iFrame));
  2656. Assert(iPalette >= 0 && iPalette < (LONG)npMCI->macIndex);
  2657. if (!ReadIndexChunk(npMCI, iPalette))
  2658. return FALSE;
  2659. npMCI->lp += 2 * sizeof(DWORD);
  2660. ProcessPaletteChange(npMCI, IndexLength(iPalette));
  2661. npMCI->lLastPaletteChange = iFrame;
  2662. }
  2663. }
  2664. return TRUE;
  2665. }
  2666. BOOL NEAR PASCAL ReadRecord(NPMCIGRAPHIC npMCI)
  2667. {
  2668. DWORD UNALIGNED FAR * pdw;
  2669. AssertFrame(npMCI->lCurrentFrame);
  2670. #ifdef AVIREADMANY
  2671. if (npMCI->fReadMany) {
  2672. //
  2673. // either read two records or return the one we read last time.
  2674. //
  2675. Assert(npMCI->hpFrameIndex);
  2676. Assert(npMCI->lCurrentFrame - npMCI->lLastRead > 0);
  2677. Assert(npMCI->lCurrentFrame - npMCI->lLastRead <= 2);
  2678. if (npMCI->lLastRead == npMCI->lCurrentFrame-1) {
  2679. //
  2680. // return the second half of the buffer.
  2681. //
  2682. npMCI->lp = npMCI->lpBuffer + (UINT)npMCI->dwThisRecordSize;
  2683. npMCI->dwThisRecordSize = npMCI->dwNextRecordSize;
  2684. }
  2685. else {
  2686. //
  2687. // read in two buffers, and return the first one
  2688. //
  2689. // figure out how much to read by looking at the index
  2690. // we dont have to worry about the last frame because
  2691. // the dummy index entry on the end is 0 in length.
  2692. //
  2693. npMCI->dwThisRecordSize = FrameLength(npMCI->lCurrentFrame) + 8;
  2694. npMCI->dwNextRecordSize = FrameLength(npMCI->lCurrentFrame+1) + 8;
  2695. if (!ReadBuffer(npMCI, -1,
  2696. npMCI->dwThisRecordSize + npMCI->dwNextRecordSize))
  2697. return FALSE;
  2698. npMCI->lLastRead = npMCI->lCurrentFrame;
  2699. npMCI->lp = npMCI->lpBuffer;
  2700. npMCI->dwThisRecordSize -= npMCI->dwNextRecordSize;
  2701. }
  2702. #ifdef DEBUG
  2703. pdw = (LPDWORD)(npMCI->lp + npMCI->dwThisRecordSize - 3 * sizeof(DWORD));
  2704. if (npMCI->lCurrentFrame < npMCI->lFrames - 1) {
  2705. Assert(pdw[0] == FOURCC_LIST);
  2706. Assert(pdw[2] == listtypeAVIRECORD);
  2707. }
  2708. #endif
  2709. return TRUE;
  2710. }
  2711. else
  2712. #endif
  2713. #ifdef AVIREAD
  2714. if (npMCI->hAviRd) {
  2715. /* async reader is going - get the next buffer from him */
  2716. npMCI->lpBuffer = avird_getnextbuffer(npMCI->hAviRd, &dwThisBuffer);
  2717. npMCI->dwThisRecordSize = npMCI->dwNextRecordSize;
  2718. if ((dwThisBuffer == 0) || (npMCI->lpBuffer == NULL)) {
  2719. npMCI->dwTaskError = MCIERR_FILE_READ;
  2720. return FALSE;
  2721. }
  2722. } else
  2723. #endif
  2724. {
  2725. if (!ReadBuffer(npMCI, -1, (LONG)npMCI->dwNextRecordSize))
  2726. return FALSE;
  2727. }
  2728. pdw = (DWORD UNALIGNED FAR *)(npMCI->lp + npMCI->dwThisRecordSize - 3 * sizeof(DWORD));
  2729. npMCI->dwNextRecordType = pdw[0];
  2730. npMCI->dwNextRecordSize = pdw[1] + 2 * sizeof(DWORD);
  2731. #ifdef DEBUG
  2732. if (npMCI->lCurrentFrame < npMCI->lFrames - 1) {
  2733. Assert(pdw[0] == FOURCC_LIST);
  2734. Assert(pdw[2] == listtypeAVIRECORD);
  2735. }
  2736. #endif
  2737. return TRUE;
  2738. }
  2739. STATICFN INLINE DWORD NEAR PASCAL ReadNextChunk(NPMCIGRAPHIC npMCI)
  2740. {
  2741. LPDWORD pdw;
  2742. DWORD dw;
  2743. ReadAgain:
  2744. dw = npMCI->dwNextRecordType;
  2745. if (!ReadBuffer(npMCI, -1, (LONG)npMCI->dwNextRecordSize))
  2746. return 0;
  2747. pdw = (LPDWORD)(npMCI->lp + npMCI->dwNextRecordSize - 2 * sizeof(DWORD));
  2748. if (dw == FOURCC_LIST)
  2749. pdw--;
  2750. npMCI->dwNextRecordType = pdw[0];
  2751. npMCI->dwNextRecordSize = pdw[1] + 2 * sizeof(DWORD);
  2752. if (dw == ckidAVIPADDING)
  2753. goto ReadAgain;
  2754. return dw;
  2755. }
  2756. STATICFN INLINE BOOL NEAR PASCAL StreamRead(NPMCIGRAPHIC npMCI, STREAMINFO *psi, LONG lPos)
  2757. {
  2758. LONG lSize;
  2759. Assert(psi);
  2760. #ifdef USEAVIFILE
  2761. Assert(psi->ps);
  2762. #endif
  2763. //
  2764. // if we are before the start or after the end, read nothing.
  2765. //
  2766. if (lPos < psi->lStart || lPos >= psi->lEnd) {
  2767. lSize = 0;
  2768. goto done;
  2769. }
  2770. #ifdef USEAVIFILE
  2771. if (AVIStreamRead(psi->ps, lPos, 1,
  2772. (LPSTR)npMCI->lpBuffer,npMCI->dwBufferSize,&lSize, NULL) != 0) {
  2773. //
  2774. // the read failed try incressing the buffer size
  2775. //
  2776. AVIStreamRead(psi->ps, lPos, 1, NULL, 0, &lSize, NULL);
  2777. if (lSize > (LONG) (npMCI->dwBufferSize)) {
  2778. DPF2(("ReadStream: Enlarging buffer....\n"));
  2779. if (!ResizeReadBuffer(npMCI, lSize)) {
  2780. DPF(("Failed to increase buffer size!\n"));
  2781. npMCI->dwTaskError = MCIERR_OUT_OF_MEMORY;
  2782. return FALSE;
  2783. }
  2784. }
  2785. if (AVIStreamRead(psi->ps, lPos, 1,
  2786. (LPSTR)npMCI->lpBuffer,npMCI->dwBufferSize,&lSize,NULL) != 0) {
  2787. npMCI->dwTaskError = MCIERR_FILE_READ;
  2788. return FALSE;
  2789. }
  2790. }
  2791. #endif
  2792. done:
  2793. npMCI->lp = npMCI->lpBuffer;
  2794. npMCI->dwThisRecordSize = lSize;
  2795. return TRUE;
  2796. }
  2797. BOOL NEAR PASCAL ReadNextVideoFrame(NPMCIGRAPHIC npMCI, STREAMINFO *psi)
  2798. {
  2799. MMCKINFO ck;
  2800. if (psi == NULL)
  2801. psi = npMCI->psiVideo;
  2802. Assert(psi);
  2803. AssertFrame(npMCI->lCurrentFrame);
  2804. #ifdef USEAVIFILE
  2805. if (psi->ps) {
  2806. LONG lSize;
  2807. LONG lPos;
  2808. //
  2809. // map from movie time into this stream.
  2810. //
  2811. lPos = MovieToStream(psi, npMCI->lCurrentFrame);
  2812. //
  2813. // if we are before the start or after the end, read nothing.
  2814. //
  2815. if (lPos < (LONG)psi->sh.dwStart ||
  2816. lPos >= (LONG)psi->sh.dwStart+(LONG)psi->sh.dwLength) {
  2817. lSize = 0;
  2818. goto done;
  2819. }
  2820. //
  2821. // if this frame has a new palette then deal with it
  2822. //
  2823. if (npMCI->dwFlags & MCIAVI_ANIMATEPALETTE) {
  2824. ProcessPaletteChanges(npMCI, lPos);
  2825. }
  2826. if (AVIStreamRead(psi->ps, lPos, 1,
  2827. (LPSTR) npMCI->lpBuffer + 2 * sizeof(DWORD),
  2828. npMCI->dwBufferSize - 2 * sizeof(DWORD),
  2829. &lSize, NULL) != 0) {
  2830. //
  2831. // the read failed try incressing the buffer size
  2832. //
  2833. AVIStreamRead(psi->ps, lPos, 1, NULL, 0, &lSize, NULL);
  2834. if (lSize > (LONG) (npMCI->dwBufferSize - 2 * sizeof(DWORD))) {
  2835. DPF2(("ReadNextVideoFrame: Enlarging buffer....\n"));
  2836. if (!ResizeReadBuffer(npMCI, lSize + 2 * sizeof(DWORD))) {
  2837. DPF(("Failed to increase buffer size!\n"));
  2838. npMCI->dwTaskError = MCIERR_OUT_OF_MEMORY;
  2839. return FALSE;
  2840. }
  2841. }
  2842. if (AVIStreamRead(psi->ps, lPos, 1,
  2843. (LPSTR) npMCI->lpBuffer + 2 * sizeof(DWORD),
  2844. npMCI->dwBufferSize - 2 * sizeof(DWORD),
  2845. &lSize, NULL) != 0) {
  2846. return FALSE;
  2847. }
  2848. }
  2849. done:
  2850. ((DWORD FAR *)npMCI->lpBuffer)[0] = MAKEAVICKID(cktypeDIBbits,
  2851. npMCI->nVideoStream);
  2852. ((DWORD FAR *)npMCI->lpBuffer)[1] = lSize;
  2853. npMCI->lp = npMCI->lpBuffer;
  2854. npMCI->dwThisRecordSize = lSize + 2 * sizeof(DWORD);
  2855. return TRUE;
  2856. }
  2857. #endif
  2858. //
  2859. // if we are not reading the "next" frame then figure out where it is.
  2860. //
  2861. if (npMCI->lLastRead != npMCI->lCurrentFrame-1)
  2862. CalculatePosition(npMCI);
  2863. //
  2864. // dwNextRecordSize is the size to read
  2865. // and we are seeked to the right place.
  2866. //
  2867. if (npMCI->hpFrameIndex) {
  2868. //
  2869. // if this frame has a new palette then deal with it
  2870. //
  2871. if (npMCI->dwFlags & MCIAVI_ANIMATEPALETTE) {
  2872. if (FramePalette(npMCI->lCurrentFrame) !=
  2873. FramePalette(npMCI->lLastPaletteChange))
  2874. ProcessPaletteChanges(npMCI, npMCI->lCurrentFrame);
  2875. }
  2876. //
  2877. // now just go read the frame from the disk.
  2878. //
  2879. // if interleaved add 8 to skip the 'REC'!!!!
  2880. //
  2881. return ReadBuffer(npMCI,
  2882. (LONG)FrameOffset(npMCI->lCurrentFrame),
  2883. (LONG)FrameLength(npMCI->lCurrentFrame) + 8);
  2884. } else {
  2885. ReadAgainNoIndex:
  2886. for (;;) {
  2887. if (mmioDescend(npMCI->hmmio, &ck, NULL, 0) != 0) {
  2888. DPF(("Unable to descend!\n"));
  2889. npMCI->dwTaskError = MCIERR_INVALID_FILE;
  2890. return FALSE;
  2891. }
  2892. /* If it's a list, stay descended in it. */
  2893. /* Hack: we never ascend. */
  2894. if (ck.ckid == FOURCC_LIST)
  2895. continue;
  2896. #ifdef ALPHAFILES
  2897. /* Skip wave bytes, since they've been preloaded. */
  2898. if (npMCI->dwFlags & MCIAVI_USINGALPHAFORMAT) {
  2899. if ((ck.ckid != ckidAVIPADDING) &&
  2900. (ck.ckid != ckidOLDPADDING) &&
  2901. (ck.ckid != ckidWAVEbytes))
  2902. break;
  2903. } else
  2904. #endif
  2905. {
  2906. if (StreamFromFOURCC(ck.ckid) == (WORD)npMCI->nVideoStream)
  2907. break;
  2908. }
  2909. mmioAscend(npMCI->hmmio, &ck, 0);
  2910. }
  2911. if (ck.cksize + 2 * sizeof(DWORD) > npMCI->dwBufferSize) {
  2912. if (!ResizeReadBuffer(npMCI, ck.cksize + 2 * sizeof(DWORD))) {
  2913. DPF(("ReadNextVideoFrame: Failed to increase buffer size!\n"));
  2914. npMCI->dwTaskError = MCIERR_OUT_OF_MEMORY;
  2915. return FALSE;
  2916. }
  2917. }
  2918. *((LPMMCKINFO) npMCI->lpBuffer) = ck;
  2919. if (mmioRead(npMCI->hmmio, npMCI->lpBuffer + 2 * sizeof(DWORD),
  2920. ck.cksize) != (LONG) ck.cksize) {
  2921. npMCI->dwTaskError = MCIERR_INVALID_FILE;
  2922. return FALSE;
  2923. }
  2924. mmioAscend(npMCI->hmmio, &ck, 0);
  2925. npMCI->lp = npMCI->lpBuffer;
  2926. npMCI->dwThisRecordSize = ck.cksize + 2 * sizeof(DWORD);
  2927. if (TWOCCFromFOURCC(ck.ckid) == cktypePALchange) {
  2928. npMCI->lp += 2 * sizeof(DWORD);
  2929. ProcessPaletteChange(npMCI, ck.cksize);
  2930. npMCI->lLastPaletteChange = npMCI->lCurrentFrame;
  2931. goto ReadAgainNoIndex;
  2932. }
  2933. }
  2934. return TRUE;
  2935. }
  2936. BOOL NEAR PASCAL TimeToQuit(NPMCIGRAPHIC npMCI)
  2937. {
  2938. /* If we're using DisplayDib, give the user a chance to break. */
  2939. if ((npMCI->dwFlags & MCIAVI_FULLSCREEN) &&
  2940. !(npMCI->dwFlags & MCIAVI_NOBREAK) &&
  2941. (npMCI->wTaskState == TASKPLAYING)) {
  2942. // Check each of the "stop" events
  2943. if (1 & GetAsyncKeyState(VK_LBUTTON)) {
  2944. npMCI->dwFlags |= MCIAVI_STOP;
  2945. }
  2946. else if
  2947. (1 & GetAsyncKeyState(VK_RBUTTON)) {
  2948. npMCI->dwFlags |= MCIAVI_STOP;
  2949. }
  2950. else if
  2951. (1 & GetAsyncKeyState(VK_ESCAPE)) {
  2952. npMCI->dwFlags |= MCIAVI_STOP;
  2953. }
  2954. else if
  2955. (1 & GetAsyncKeyState(VK_SPACE)) {
  2956. npMCI->dwFlags |= MCIAVI_STOP;
  2957. }
  2958. }
  2959. // this will be set by aviTaskCheckRequests if there is
  2960. // a request that we need to stop to handle
  2961. if (npMCI->dwFlags & MCIAVI_STOP)
  2962. return TRUE;
  2963. #ifdef _WIN32
  2964. if (TestNTFlags(npMCI, NTF_RETRYAUDIO)) {
  2965. ResetNTFlags(npMCI, NTF_RETRYAUDIO);
  2966. /*
  2967. * IF we get access to the wave device, set the flag that
  2968. * will cause the play to be restarted, then abort this play.
  2969. */
  2970. SetUpAudio(npMCI, TRUE);
  2971. if (npMCI->hWave) {
  2972. SetNTFlags(npMCI, NTF_RESTARTFORAUDIO);
  2973. return(TRUE);
  2974. }
  2975. }
  2976. #endif
  2977. return FALSE;
  2978. }
  2979. /***************************************************************************
  2980. *
  2981. * @doc INTERNAL MCIAVI
  2982. *
  2983. * @api BOOL | AllocateReadBuffer | Allocates buffers needed to read
  2984. * disk information in to. The amount of memory to allocate
  2985. * is in npMCI->dwBufferSize.
  2986. *
  2987. * @parm NPMCIGRAPHIC | npMCI | pointer to instance data block.
  2988. *
  2989. * @rdesc TRUE means OK, otherwise unable to allocate memory.
  2990. *
  2991. ***************************************************************************/
  2992. BOOL NEAR PASCAL AllocateReadBuffer(NPMCIGRAPHIC npMCI)
  2993. {
  2994. if (npMCI->dwBufferSize == 0)
  2995. npMCI->dwBufferSize = npMCI->dwSuggestedBufferSize;
  2996. if (npMCI->dwBufferSize <= 8 * sizeof(DWORD))
  2997. {
  2998. if (npMCI->dwBytesPerSec > 0 &&
  2999. npMCI->dwBytesPerSec < 600l*1024 &&
  3000. npMCI->dwMicroSecPerFrame > 0)
  3001. npMCI->dwBufferSize = (muldiv32(npMCI->dwBytesPerSec,
  3002. npMCI->dwMicroSecPerFrame,1000000L) + 2047) & ~2047;
  3003. else
  3004. npMCI->dwBufferSize = 10*1024;
  3005. npMCI->dwSuggestedBufferSize = npMCI->dwBufferSize;
  3006. }
  3007. DPF3(("allocating %lu byte read buffer.\n", npMCI->dwBufferSize));
  3008. if (npMCI->lpBuffer) {
  3009. DPF(("Already have buffer in AllocateReadBuffer!\n"));
  3010. return ResizeReadBuffer(npMCI, npMCI->dwBufferSize);
  3011. }
  3012. //!!! we dont need DOS memory when we have a MMIO buffer!
  3013. //!!! we dont need DOS memory when we are using AVIFile???
  3014. if (npMCI->lpMMIOBuffer != NULL || npMCI->pf)
  3015. npMCI->lpBuffer = GlobalAllocPtr(GHND | GMEM_SHARE, npMCI->dwBufferSize);
  3016. else
  3017. npMCI->lpBuffer = AllocMem(npMCI->dwBufferSize);
  3018. return npMCI->lpBuffer != NULL;
  3019. }
  3020. /***************************************************************************
  3021. *
  3022. * @doc INTERNAL MCIAVI
  3023. *
  3024. * @api BOOL | ResizeReadBuffer | Enlarges buffer needed to read
  3025. * disk information in to.
  3026. *
  3027. * @parm NPMCIGRAPHIC | npMCI | pointer to instance data block.
  3028. *
  3029. * @parm DWORD | dwNewSize | new amount of memory to allocate
  3030. *
  3031. * @rdesc TRUE means OK, otherwise unable to allocate memory.
  3032. *
  3033. ***************************************************************************/
  3034. BOOL NEAR PASCAL ResizeReadBuffer(NPMCIGRAPHIC npMCI, DWORD dwNewSize)
  3035. {
  3036. if (dwNewSize > npMCI->dwSuggestedBufferSize && !npMCI->fReadMany)
  3037. npMCI->dwSuggestedBufferSize = dwNewSize;
  3038. if (dwNewSize <= npMCI->dwBufferSize)
  3039. return TRUE;
  3040. DPF(("Increasing buffer size to %ld (was %ld).\n", dwNewSize, npMCI->dwBufferSize));
  3041. ReleaseReadBuffer(npMCI);
  3042. npMCI->dwBufferSize = dwNewSize;
  3043. return AllocateReadBuffer(npMCI);
  3044. }
  3045. /***************************************************************************
  3046. *
  3047. * @doc INTERNAL MCIAVI
  3048. *
  3049. * @api void | ReleaseReadBuffer | Releases read buffer.
  3050. *
  3051. * @parm NPMCIGRAPHIC | npMCI | pointer to instance data block.
  3052. *
  3053. ***************************************************************************/
  3054. void NEAR PASCAL ReleaseReadBuffer(NPMCIGRAPHIC npMCI)
  3055. {
  3056. if (npMCI->lpBuffer) {
  3057. DPF3(("Releasing read buffer.\n"));
  3058. GlobalFreePtr(npMCI->lpBuffer);
  3059. npMCI->lpBuffer = NULL;
  3060. npMCI->dwBufferSize = 0L;
  3061. npMCI->fReadMany = FALSE;
  3062. }
  3063. }
  3064. /***************************************************************************
  3065. *
  3066. * @doc INTERNAL MCIAVI
  3067. *
  3068. * @api BOOL | ReadBuffer
  3069. *
  3070. ***************************************************************************/
  3071. BOOL NEAR PASCAL ReadBuffer(NPMCIGRAPHIC npMCI, LONG off, LONG len)
  3072. {
  3073. npMCI->lp = npMCI->lpBuffer;
  3074. npMCI->dwThisRecordSize = len;
  3075. if (len == 0) {
  3076. ((DWORD FAR *)npMCI->lpBuffer)[0] = 0; //!!!lpIndexEntry->ckid;
  3077. ((DWORD FAR *)npMCI->lpBuffer)[1] = 0;
  3078. npMCI->dwThisRecordSize = 8;
  3079. return TRUE;
  3080. }
  3081. if (len > (LONG)npMCI->dwBufferSize) {
  3082. if (!ResizeReadBuffer(npMCI, len)) {
  3083. DPF(("Failed to increase buffer size!\n"));
  3084. npMCI->dwTaskError = MCIERR_OUT_OF_MEMORY;
  3085. return FALSE;
  3086. }
  3087. npMCI->lp = npMCI->lpBuffer;
  3088. }
  3089. if (off >= 0)
  3090. DPF2(("ReadBuffer %ld bytes at %ld\n", len, off));
  3091. else
  3092. DPF2(("ReadBuffer %ld bytes\n", len));
  3093. if (off >= 0)
  3094. mmioSeek(npMCI->hmmio, off, SEEK_SET);
  3095. #ifdef INTERVAL_TIMES
  3096. {
  3097. LONG lReadStart = -(LONG)timeGetTime();
  3098. #endif
  3099. if (mmioRead(npMCI->hmmio, npMCI->lp, len) != len) {
  3100. npMCI->dwTaskError = MCIERR_FILE_READ;
  3101. return FALSE;
  3102. }
  3103. #ifdef INTERVAL_TIMES
  3104. lReadStart += timeGetTime();
  3105. npMCI->nReads++;
  3106. npMCI->msReadTotal += lReadStart;
  3107. if (lReadStart > npMCI->msReadMax) {
  3108. npMCI->msReadMax = lReadStart;
  3109. }
  3110. }
  3111. #endif
  3112. return TRUE;
  3113. }
  3114. /***************************************************************************
  3115. *
  3116. * @doc INTERNAL MCIAVI
  3117. *
  3118. * @api LPVOID | AllocMem | try to allocate DOS memory (< 1Mb)
  3119. *
  3120. * @parm DWORD | dw | size in bytes
  3121. *
  3122. ***************************************************************************/
  3123. #ifndef _WIN32
  3124. static LPVOID AllocMem(DWORD dw)
  3125. {
  3126. /* Memory allocation internal routines */
  3127. extern DWORD FAR PASCAL GlobalDosAlloc(DWORD);
  3128. LPVOID p;
  3129. if (p = (LPVOID)MAKELONG(0, LOWORD(GlobalDosAlloc(dw))))
  3130. {
  3131. DPF(("Got %ld bytes DOS memory\n", dw));
  3132. GlobalReAlloc((HANDLE)HIWORD((DWORD)p), 0, GMEM_MODIFY|GMEM_SHARE);
  3133. return p;
  3134. }
  3135. else
  3136. {
  3137. DPF(("unable to get %ld bytes of DOS memory\n", dw));
  3138. return GlobalLock(GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE, dw));
  3139. }
  3140. }
  3141. #endif