Windows NT 4.0 source code leak
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.

3878 lines
103 KiB

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