aviplay.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2017, SDLPAL development team.
  4. // All rights reserved.
  5. //
  6. // This file is part of SDLPAL.
  7. //
  8. // SDLPAL is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. //
  22. // aviplay.c
  23. //
  24. // Simple quick and dirty AVI player specially designed for PAL Win95.
  25. //
  26. /*
  27. * Portions based on:
  28. *
  29. * Microsoft Video-1 Decoder
  30. * Copyright (C) 2003 The FFmpeg project
  31. *
  32. * This file is part of FFmpeg.
  33. *
  34. * FFmpeg is free software; you can redistribute it and/or
  35. * modify it under the terms of the GNU Lesser General Public
  36. * License as published by the Free Software Foundation; either
  37. * version 2.1 of the License, or (at your option) any later version.
  38. *
  39. * FFmpeg is distributed in the hope that it will be useful,
  40. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  42. * Lesser General Public License for more details.
  43. *
  44. * You should have received a copy of the GNU Lesser General Public
  45. * License along with FFmpeg; if not, write to the Free Software
  46. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  47. *
  48. * Microsoft Video-1 Decoder by Mike Melanson (melanson@pcisys.net)
  49. * For more information about the MS Video-1 format, visit:
  50. * http://www.pcisys.net/~melanson/codecs/
  51. */
  52. #include "main.h"
  53. typedef struct
  54. {
  55. FILE *fp; // pointer to the AVI file
  56. SDL_Surface *surface; // video buffer
  57. DWORD dwVideoEndOffset;
  58. WORD wWidth, wHeight; // width and height of video
  59. WORD wMsPerFrame; // milliseconds per frame
  60. DWORD dwAudioSamplesPerSec;
  61. DWORD dwAudioBitsPerSample;
  62. DWORD dwAudioChannels;
  63. SDL_AudioCVT cvt;
  64. SDL_mutex *mtxAudioData;
  65. BYTE bAudioBuf[256000]; // ring buffer for audio data
  66. DWORD dwAudioReadPos;
  67. DWORD dwAudioWritePos;
  68. } AVIPlayState;
  69. static SDL_mutex *gpAVIPlayStateMutex = NULL;
  70. static AVIPlayState *gpAVIPlayState = NULL;
  71. typedef struct
  72. {
  73. DWORD dwFourCC;
  74. DWORD dwSize;
  75. BYTE bData[1];
  76. } AVIChunk;
  77. typedef struct
  78. {
  79. DWORD dwMicroSecPerFrame; // frame display rate (or 0)
  80. DWORD dwMaxBytesPerSec; // max. transfer rate
  81. DWORD dwPaddingGranularity; // pad to multiples of this size
  82. DWORD dwFlags; // the ever-present flags
  83. DWORD dwTotalFrames; // # frames in file
  84. DWORD dwInitialFrames;
  85. DWORD dwStreams;
  86. DWORD dwSuggestedBufferSize;
  87. DWORD dwWidth;
  88. DWORD dwHeight;
  89. DWORD dwReserved[4];
  90. } MainAVIHeader;
  91. typedef struct
  92. {
  93. DWORD fccType;
  94. DWORD fccHandler;
  95. DWORD dwFlags;
  96. WORD wPriority;
  97. WORD wLanguage;
  98. DWORD dwInitialFrames;
  99. DWORD dwScale;
  100. DWORD dwRate; /* dwRate / dwScale == samples/second */
  101. DWORD dwStart;
  102. DWORD dwLength; /* In units above... */
  103. DWORD dwSuggestedBufferSize;
  104. DWORD dwQuality;
  105. DWORD dwSampleSize;
  106. DWORD rcFrame[4];
  107. } AVIStreamHeader;
  108. typedef struct
  109. {
  110. DWORD biSize;
  111. DWORD biWidth;
  112. DWORD biHeight;
  113. WORD biPlanes;
  114. WORD biBitCount;
  115. DWORD biCompression;
  116. DWORD biSizeImage;
  117. DWORD biXPelsPerMeter;
  118. DWORD biYPelsPerMeter;
  119. DWORD biClrUsed;
  120. DWORD biClrImportant;
  121. } BitmapInfoHeader;
  122. typedef struct
  123. {
  124. WORD wFormatTag;
  125. WORD nChannels;
  126. DWORD nSamplesPerSec;
  127. DWORD nAvgBytesPerSec;
  128. WORD nBlockAlign;
  129. WORD wBitsPerSample;
  130. } WaveFormat;
  131. #define AVI_RIFF (((DWORD)'R') | (((DWORD)'I') << 8) | (((DWORD)'F') << 16) | (((DWORD)'F') << 24))
  132. #define AVI_hdrl (((DWORD)'h') | (((DWORD)'d') << 8) | (((DWORD)'r') << 16) | (((DWORD)'l') << 24))
  133. #define AVI_strl (((DWORD)'s') | (((DWORD)'t') << 8) | (((DWORD)'r') << 16) | (((DWORD)'l') << 24))
  134. #define AVI_strh (((DWORD)'s') | (((DWORD)'t') << 8) | (((DWORD)'r') << 16) | (((DWORD)'h') << 24))
  135. #define AVI_strf (((DWORD)'s') | (((DWORD)'t') << 8) | (((DWORD)'r') << 16) | (((DWORD)'f') << 24))
  136. #define AVI_avih (((DWORD)'a') | (((DWORD)'v') << 8) | (((DWORD)'i') << 16) | (((DWORD)'h') << 24))
  137. #define AVI_LIST (((DWORD)'L') | (((DWORD)'I') << 8) | (((DWORD)'S') << 16) | (((DWORD)'T') << 24))
  138. #define AVI_movi (((DWORD)'m') | (((DWORD)'o') << 8) | (((DWORD)'v') << 16) | (((DWORD)'i') << 24))
  139. #define AVI_01wb (((DWORD)'0') | (((DWORD)'1') << 8) | (((DWORD)'w') << 16) | (((DWORD)'b') << 24))
  140. #define AVI_00dc (((DWORD)'0') | (((DWORD)'0') << 8) | (((DWORD)'d') << 16) | (((DWORD)'c') << 24))
  141. #define AVI_00db (((DWORD)'0') | (((DWORD)'0') << 8) | (((DWORD)'d') << 16) | (((DWORD)'b') << 24))
  142. #define AVI_rec (((DWORD)'r') | (((DWORD)'e') << 8) | (((DWORD)'c') << 16) | (((DWORD)' ') << 24))
  143. #define AVI_JUNK (((DWORD)'J') | (((DWORD)'U') << 8) | (((DWORD)'N') << 16) | (((DWORD)'K') << 24))
  144. #define AVI_vids (((DWORD)'v') | (((DWORD)'i') << 8) | (((DWORD)'d') << 16) | (((DWORD)'s') << 24))
  145. #define AVI_auds (((DWORD)'a') | (((DWORD)'u') << 8) | (((DWORD)'d') << 16) | (((DWORD)'s') << 24))
  146. static VOID
  147. PAL_ParseAVInfoList(
  148. AVIPlayState *lpAVIPlayState,
  149. DWORD dwEndOffset
  150. )
  151. {
  152. AVIChunk hdr;
  153. DWORD dwNextOffset;
  154. AVIStreamHeader streamHdr;
  155. BitmapInfoHeader bitmapHdr;
  156. WaveFormat waveFormat;
  157. DWORD dwInfoType = 0;
  158. while (ftell(lpAVIPlayState->fp) < dwEndOffset)
  159. {
  160. if (feof(lpAVIPlayState->fp))
  161. {
  162. return; // end of file reached
  163. }
  164. fread(&hdr, sizeof(DWORD) * 2, 1, lpAVIPlayState->fp);
  165. hdr.dwFourCC = SDL_SwapLE32(hdr.dwFourCC);
  166. hdr.dwSize = SDL_SwapLE32(hdr.dwSize);
  167. dwNextOffset = ftell(lpAVIPlayState->fp) + hdr.dwSize;
  168. switch (hdr.dwFourCC)
  169. {
  170. case AVI_strh:
  171. fread(&streamHdr, sizeof(AVIStreamHeader), 1, lpAVIPlayState->fp);
  172. dwInfoType = SDL_SwapLE32(streamHdr.fccType);
  173. break;
  174. case AVI_strf:
  175. if (dwInfoType == AVI_vids)
  176. {
  177. fread(&bitmapHdr, sizeof(bitmapHdr), 1, lpAVIPlayState->fp);
  178. }
  179. else if (dwInfoType == AVI_auds)
  180. {
  181. fread(&waveFormat, sizeof(waveFormat), 1, lpAVIPlayState->fp);
  182. lpAVIPlayState->dwAudioChannels = SDL_SwapLE16(waveFormat.nChannels);
  183. lpAVIPlayState->dwAudioSamplesPerSec = SDL_SwapLE16(waveFormat.nSamplesPerSec);
  184. lpAVIPlayState->dwAudioBitsPerSample = SDL_SwapLE16(waveFormat.wBitsPerSample);
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. fseek(lpAVIPlayState->fp, dwNextOffset, SEEK_SET);
  191. }
  192. }
  193. static VOID
  194. PAL_ParseHdrlList(
  195. AVIPlayState *lpAVIPlayState,
  196. DWORD dwEndOffset
  197. )
  198. {
  199. AVIChunk hdr;
  200. DWORD dwNextOffset, dwListType;
  201. MainAVIHeader aviHeader;
  202. while (ftell(lpAVIPlayState->fp) < dwEndOffset)
  203. {
  204. if (feof(lpAVIPlayState->fp))
  205. {
  206. return; // end of file reached
  207. }
  208. fread(&hdr, sizeof(DWORD) * 2, 1, lpAVIPlayState->fp);
  209. hdr.dwFourCC = SDL_SwapLE32(hdr.dwFourCC);
  210. hdr.dwSize = SDL_SwapLE32(hdr.dwSize);
  211. dwNextOffset = ftell(lpAVIPlayState->fp) + hdr.dwSize;
  212. switch (hdr.dwFourCC)
  213. {
  214. case AVI_avih:
  215. fread(&aviHeader, sizeof(aviHeader), 1, lpAVIPlayState->fp);
  216. lpAVIPlayState->wWidth = aviHeader.dwWidth;
  217. lpAVIPlayState->wHeight = aviHeader.dwHeight;
  218. lpAVIPlayState->wMsPerFrame = aviHeader.dwMicroSecPerFrame / 1000;
  219. break;
  220. case AVI_LIST:
  221. fread(&dwListType, sizeof(dwListType), 1, lpAVIPlayState->fp);
  222. if (SDL_SwapLE32(dwListType) == AVI_strl)
  223. {
  224. PAL_ParseAVInfoList(lpAVIPlayState, dwNextOffset);
  225. }
  226. break;
  227. }
  228. fseek(lpAVIPlayState->fp, dwNextOffset, SEEK_SET);
  229. }
  230. }
  231. static VOID
  232. PAL_ReadAVIInfo(
  233. AVIPlayState *lpAVIPlayState
  234. )
  235. {
  236. AVIChunk hdr;
  237. DWORD dwListType = 0;
  238. DWORD dwNextOffset = 0;
  239. //
  240. // Skip RIFF header
  241. //
  242. fseek(lpAVIPlayState->fp, 12, SEEK_SET);
  243. while (TRUE)
  244. {
  245. if (feof(lpAVIPlayState->fp))
  246. {
  247. return; // end of file reached
  248. }
  249. fread(&hdr, sizeof(DWORD) * 2, 1, lpAVIPlayState->fp);
  250. hdr.dwFourCC = SDL_SwapLE32(hdr.dwFourCC);
  251. hdr.dwSize = SDL_SwapLE32(hdr.dwSize);
  252. dwNextOffset = ftell(lpAVIPlayState->fp) + hdr.dwSize;
  253. switch (hdr.dwFourCC)
  254. {
  255. case AVI_LIST:
  256. fread(&dwListType, sizeof(DWORD), 1, lpAVIPlayState->fp);
  257. dwListType = SDL_SwapLE32(dwListType);
  258. switch (dwListType)
  259. {
  260. case AVI_hdrl:
  261. PAL_ParseHdrlList(lpAVIPlayState, dwNextOffset);
  262. break;
  263. case AVI_movi:
  264. //
  265. // Stop right here as the actual movie data starts
  266. //
  267. lpAVIPlayState->dwVideoEndOffset = dwNextOffset;
  268. return;
  269. }
  270. break;
  271. case AVI_JUNK:
  272. default:
  273. //
  274. // Ignore these chunks
  275. //
  276. break;
  277. }
  278. fseek(lpAVIPlayState->fp, dwNextOffset, SEEK_SET);
  279. }
  280. }
  281. static AVIPlayState *
  282. PAL_OpenAVI(
  283. LPCSTR lpszPath
  284. )
  285. {
  286. AVIPlayState *ret;
  287. ret = (AVIPlayState *)UTIL_calloc(1, sizeof(AVIPlayState));
  288. //
  289. // Open the file
  290. //
  291. ret->fp = UTIL_OpenFile(lpszPath);
  292. if (ret->fp == NULL)
  293. {
  294. fprintf(stderr, "Cannot open file: %s!\n", lpszPath);
  295. free(ret);
  296. return NULL;
  297. }
  298. PAL_ReadAVIInfo(ret);
  299. if (ret->wWidth == 0 || ret->wHeight == 0)
  300. {
  301. return NULL;
  302. }
  303. //
  304. // Create surface
  305. //
  306. ret->surface = SDL_CreateRGBSurface(SDL_SWSURFACE, ret->wWidth, ret->wHeight, 16,
  307. 0x7C00, 0x03E0, 0x001F, 0x0000);
  308. //
  309. // Create mutex
  310. //
  311. ret->mtxAudioData = SDL_CreateMutex();
  312. //
  313. // Build SDL audio conversion info
  314. //
  315. SDL_BuildAudioCVT(&ret->cvt,
  316. (ret->dwAudioBitsPerSample == 8) ? AUDIO_U8 : AUDIO_S16LSB,
  317. ret->dwAudioChannels,
  318. ret->dwAudioSamplesPerSec,
  319. AUDIO_S16SYS,
  320. gConfig.iAudioChannels,
  321. gConfig.iSampleRate);
  322. return ret;
  323. }
  324. static AVIChunk *
  325. PAL_ReadAVChunk(
  326. AVIPlayState *lpAVIPlayState
  327. )
  328. {
  329. AVIChunk hdr;
  330. AVIChunk *ret = NULL;
  331. DWORD dwNextOffset;
  332. begin:
  333. if (feof(lpAVIPlayState->fp) || ftell(lpAVIPlayState->fp) >= lpAVIPlayState->dwVideoEndOffset)
  334. {
  335. return NULL; // end of file reached
  336. }
  337. fread(&hdr, sizeof(DWORD) * 2, 1, lpAVIPlayState->fp);
  338. hdr.dwFourCC = SDL_SwapLE32(hdr.dwFourCC);
  339. hdr.dwSize = SDL_SwapLE32(hdr.dwSize);
  340. dwNextOffset = ftell(lpAVIPlayState->fp) + hdr.dwSize;
  341. switch (hdr.dwFourCC)
  342. {
  343. case AVI_LIST:
  344. //
  345. // Just skip list header here
  346. //
  347. fseek(lpAVIPlayState->fp, sizeof(DWORD), SEEK_CUR);
  348. goto begin;
  349. case AVI_01wb:
  350. case AVI_00db:
  351. case AVI_00dc:
  352. //
  353. // got actual audio/video frame
  354. //
  355. ret = (AVIChunk *)UTIL_malloc(sizeof(DWORD) * 2 + hdr.dwSize);
  356. *ret = hdr;
  357. fread(ret->bData, hdr.dwSize, 1, lpAVIPlayState->fp);
  358. break;
  359. case AVI_JUNK:
  360. default:
  361. //
  362. // Ignore these chunks
  363. //
  364. fseek(lpAVIPlayState->fp, dwNextOffset, SEEK_SET);
  365. goto begin;
  366. }
  367. fseek(lpAVIPlayState->fp, dwNextOffset, SEEK_SET);
  368. return ret;
  369. }
  370. static VOID
  371. PAL_CloseAVI(
  372. AVIPlayState *lpAVIPlayState
  373. )
  374. {
  375. if (lpAVIPlayState->fp != NULL)
  376. {
  377. fclose(lpAVIPlayState->fp);
  378. }
  379. if (lpAVIPlayState->surface != NULL)
  380. {
  381. SDL_FreeSurface(lpAVIPlayState->surface);
  382. }
  383. if (lpAVIPlayState->mtxAudioData != NULL)
  384. {
  385. SDL_DestroyMutex(lpAVIPlayState->mtxAudioData);
  386. }
  387. free(lpAVIPlayState);
  388. }
  389. static VOID
  390. PAL_AVIFeedAudio(
  391. AVIPlayState *lpAVIPlayState,
  392. LPBYTE lpBuffer,
  393. DWORD dwSize
  394. )
  395. {
  396. SDL_mutexP(lpAVIPlayState->mtxAudioData);
  397. while (dwSize > 0)
  398. {
  399. DWORD dwFeedSize = dwSize;
  400. if (lpAVIPlayState->dwAudioWritePos + dwSize > sizeof(lpAVIPlayState->bAudioBuf))
  401. {
  402. dwFeedSize = sizeof(lpAVIPlayState->bAudioBuf) - lpAVIPlayState->dwAudioWritePos;
  403. }
  404. memcpy(&lpAVIPlayState->bAudioBuf[lpAVIPlayState->dwAudioWritePos], lpBuffer, dwFeedSize);
  405. lpAVIPlayState->dwAudioWritePos += dwFeedSize;
  406. lpAVIPlayState->dwAudioWritePos %= sizeof(lpAVIPlayState->bAudioBuf);
  407. lpBuffer += dwFeedSize;
  408. dwSize -= dwFeedSize;
  409. }
  410. SDL_mutexV(lpAVIPlayState->mtxAudioData);
  411. }
  412. VOID
  413. PAL_AVIInit(
  414. VOID
  415. )
  416. {
  417. gpAVIPlayStateMutex = SDL_CreateMutex();
  418. }
  419. VOID
  420. PAL_AVIShutdown(
  421. VOID
  422. )
  423. {
  424. SDL_DestroyMutex(gpAVIPlayStateMutex);
  425. }
  426. static VOID
  427. PAL_RenderAVIFrame(
  428. SDL_Surface *lpSurface,
  429. AVIChunk *lpChunk
  430. )
  431. {
  432. #define AV_RL16(x) ((((LPCBYTE)(x))[1] << 8) | ((LPCBYTE)(x))[0])
  433. #define CHECK_STREAM_PTR(n) if ((stream_ptr + n) > lpChunk->dwSize) { return; }
  434. int block_ptr, pixel_ptr;
  435. int total_blocks;
  436. int pixel_x, pixel_y; // pixel width and height iterators
  437. int block_x, block_y; // block width and height iterators
  438. int blocks_wide, blocks_high; // width and height in 4x4 blocks
  439. int block_inc;
  440. int row_dec;
  441. /* decoding parameters */
  442. int stream_ptr;
  443. unsigned char byte_a, byte_b;
  444. unsigned short flags;
  445. int skip_blocks;
  446. unsigned short colors[8];
  447. unsigned short *pixels = (unsigned short *)lpSurface->pixels;
  448. int stride = lpSurface->pitch / 2;
  449. stream_ptr = 0;
  450. skip_blocks = 0;
  451. blocks_wide = lpSurface->w / 4;
  452. blocks_high = lpSurface->h / 4;
  453. total_blocks = blocks_wide * blocks_high;
  454. block_inc = 4;
  455. row_dec = stride + 4;
  456. for (block_y = blocks_high; block_y > 0; block_y--)
  457. {
  458. block_ptr = ((block_y * 4) - 1) * stride;
  459. for (block_x = blocks_wide; block_x > 0; block_x--)
  460. {
  461. // check if this block should be skipped
  462. if (skip_blocks)
  463. {
  464. block_ptr += block_inc;
  465. skip_blocks--;
  466. total_blocks--;
  467. continue;
  468. }
  469. pixel_ptr = block_ptr;
  470. // get the next two bytes in the encoded data stream
  471. CHECK_STREAM_PTR(2);
  472. byte_a = lpChunk->bData[stream_ptr++];
  473. byte_b = lpChunk->bData[stream_ptr++];
  474. // check if the decode is finished
  475. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
  476. {
  477. return;
  478. }
  479. else if ((byte_b & 0xFC) == 0x84)
  480. {
  481. // skip code, but don't count the current block
  482. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  483. }
  484. else if (byte_b < 0x80)
  485. {
  486. // 2- or 8-color encoding modes
  487. flags = (byte_b << 8) | byte_a;
  488. CHECK_STREAM_PTR(4);
  489. colors[0] = AV_RL16(&lpChunk->bData[stream_ptr]);
  490. stream_ptr += 2;
  491. colors[1] = AV_RL16(&lpChunk->bData[stream_ptr]);
  492. stream_ptr += 2;
  493. if (colors[0] & 0x8000)
  494. {
  495. // 8-color encoding
  496. CHECK_STREAM_PTR(12);
  497. colors[2] = AV_RL16(&lpChunk->bData[stream_ptr]);
  498. stream_ptr += 2;
  499. colors[3] = AV_RL16(&lpChunk->bData[stream_ptr]);
  500. stream_ptr += 2;
  501. colors[4] = AV_RL16(&lpChunk->bData[stream_ptr]);
  502. stream_ptr += 2;
  503. colors[5] = AV_RL16(&lpChunk->bData[stream_ptr]);
  504. stream_ptr += 2;
  505. colors[6] = AV_RL16(&lpChunk->bData[stream_ptr]);
  506. stream_ptr += 2;
  507. colors[7] = AV_RL16(&lpChunk->bData[stream_ptr]);
  508. stream_ptr += 2;
  509. for (pixel_y = 0; pixel_y < 4; pixel_y++)
  510. {
  511. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  512. {
  513. pixels[pixel_ptr++] =
  514. colors[((pixel_y & 0x2) << 1) +
  515. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  516. }
  517. pixel_ptr -= row_dec;
  518. }
  519. }
  520. else
  521. {
  522. // 2-color encoding
  523. for (pixel_y = 0; pixel_y < 4; pixel_y++)
  524. {
  525. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  526. {
  527. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  528. }
  529. pixel_ptr -= row_dec;
  530. }
  531. }
  532. }
  533. else
  534. {
  535. // otherwise, it's a 1-color block
  536. colors[0] = (byte_b << 8) | byte_a;
  537. for (pixel_y = 0; pixel_y < 4; pixel_y++)
  538. {
  539. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  540. {
  541. pixels[pixel_ptr++] = colors[0];
  542. }
  543. pixel_ptr -= row_dec;
  544. }
  545. }
  546. block_ptr += block_inc;
  547. total_blocks--;
  548. }
  549. }
  550. }
  551. BOOL
  552. PAL_PlayAVI(
  553. LPCSTR lpszPath
  554. )
  555. {
  556. AVIPlayState *avi;
  557. AVIChunk *chunk;
  558. DWORD dwNextFrameTime = 0, dwCurrentTime = 0;
  559. BOOL fEndPlay = FALSE;
  560. //
  561. // Open AVI file
  562. //
  563. avi = PAL_OpenAVI(lpszPath);
  564. if (avi == NULL)
  565. {
  566. return FALSE;
  567. }
  568. SDL_mutexP(gpAVIPlayStateMutex);
  569. gpAVIPlayState = avi;
  570. SDL_mutexV(gpAVIPlayStateMutex);
  571. PAL_ClearKeyState();
  572. while (!fEndPlay && (chunk = PAL_ReadAVChunk(avi)) != NULL)
  573. {
  574. dwCurrentTime = SDL_GetTicks();
  575. switch (chunk->dwFourCC)
  576. {
  577. case AVI_00dc:
  578. case AVI_00db:
  579. //
  580. // Video frame
  581. //
  582. PAL_RenderAVIFrame(avi->surface, chunk);
  583. VIDEO_DrawSurfaceToScreen(avi->surface);
  584. dwNextFrameTime = dwCurrentTime + avi->wMsPerFrame;
  585. dwCurrentTime = SDL_GetTicks();
  586. if (dwCurrentTime >= dwNextFrameTime)
  587. {
  588. UTIL_Delay(1);
  589. }
  590. else
  591. {
  592. UTIL_Delay(dwNextFrameTime - dwCurrentTime);
  593. }
  594. if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
  595. {
  596. fEndPlay = TRUE;
  597. }
  598. break;
  599. case AVI_01wb:
  600. //
  601. // Audio data, just feed into buffer
  602. //
  603. PAL_AVIFeedAudio(avi, chunk->bData, chunk->dwSize);
  604. break;
  605. }
  606. free(chunk);
  607. }
  608. SDL_mutexP(gpAVIPlayStateMutex);
  609. gpAVIPlayState = NULL;
  610. SDL_mutexV(gpAVIPlayStateMutex);
  611. if (fEndPlay)
  612. {
  613. //
  614. // Simulate a short delay (like the original game)
  615. //
  616. UTIL_Delay(500);
  617. }
  618. PAL_CloseAVI(avi);
  619. return TRUE;
  620. }
  621. VOID SDLCALL
  622. AVI_FillAudioBuffer(
  623. LPVOID udata,
  624. LPBYTE stream,
  625. INT len
  626. )
  627. {
  628. SDL_mutexP(gpAVIPlayStateMutex);
  629. if (gpAVIPlayState != NULL)
  630. {
  631. FLOAT flRateScale = ((FLOAT)gConfig.iSampleRate / gpAVIPlayState->dwAudioSamplesPerSec);
  632. while (len > 0 && gpAVIPlayState->dwAudioReadPos != gpAVIPlayState->dwAudioWritePos)
  633. {
  634. INT remainingLen = gpAVIPlayState->dwAudioWritePos - gpAVIPlayState->dwAudioReadPos;
  635. INT samplesToRead;
  636. if (remainingLen < 0)
  637. {
  638. remainingLen = sizeof(gpAVIPlayState->bAudioBuf) - gpAVIPlayState->dwAudioReadPos;
  639. }
  640. samplesToRead = remainingLen / gpAVIPlayState->dwAudioChannels / (gpAVIPlayState->dwAudioBitsPerSample / 8);
  641. if (samplesToRead > len / 2 / gConfig.iAudioChannels / flRateScale)
  642. {
  643. samplesToRead = len / 2 / gConfig.iAudioChannels / flRateScale;
  644. }
  645. gpAVIPlayState->cvt.buf = stream;
  646. gpAVIPlayState->cvt.len = samplesToRead *
  647. gpAVIPlayState->dwAudioChannels * (gpAVIPlayState->dwAudioBitsPerSample / 8);
  648. memcpy(stream, &gpAVIPlayState->bAudioBuf[gpAVIPlayState->dwAudioReadPos],
  649. samplesToRead * gpAVIPlayState->dwAudioChannels * (gpAVIPlayState->dwAudioBitsPerSample / 8));
  650. SDL_ConvertAudio(&gpAVIPlayState->cvt);
  651. stream += (DWORD)(samplesToRead * 2 * gConfig.iAudioChannels * flRateScale);
  652. len -= samplesToRead * 2 * gConfig.iAudioChannels * flRateScale;
  653. gpAVIPlayState->dwAudioReadPos += samplesToRead * gpAVIPlayState->dwAudioChannels *
  654. (gpAVIPlayState->dwAudioBitsPerSample / 8);
  655. gpAVIPlayState->dwAudioReadPos %= sizeof(gpAVIPlayState->bAudioBuf);
  656. }
  657. }
  658. SDL_mutexV(gpAVIPlayStateMutex);
  659. }