aviplay.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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[25600000]; // 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. MainAVIHeader aviHeader;
  155. AVIStreamHeader streamHdr;
  156. BitmapInfoHeader bitmapHdr;
  157. WaveFormat waveFormat;
  158. DWORD dwInfoType = 0;
  159. while (ftell(lpAVIPlayState->fp) < dwEndOffset)
  160. {
  161. if (feof(lpAVIPlayState->fp))
  162. {
  163. return; // end of file reached
  164. }
  165. fread(&hdr, sizeof(DWORD) * 2, 1, lpAVIPlayState->fp);
  166. hdr.dwFourCC = SDL_SwapLE32(hdr.dwFourCC);
  167. hdr.dwSize = SDL_SwapLE32(hdr.dwSize);
  168. dwNextOffset = ftell(lpAVIPlayState->fp) + hdr.dwSize;
  169. switch (hdr.dwFourCC)
  170. {
  171. case AVI_strh:
  172. fread(&streamHdr, sizeof(AVIStreamHeader), 1, lpAVIPlayState->fp);
  173. dwInfoType = SDL_SwapLE32(streamHdr.fccType);
  174. break;
  175. case AVI_strf:
  176. if (dwInfoType == AVI_vids)
  177. {
  178. fread(&bitmapHdr, sizeof(bitmapHdr), 1, lpAVIPlayState->fp);
  179. }
  180. else if (dwInfoType == AVI_auds)
  181. {
  182. fread(&waveFormat, sizeof(waveFormat), 1, lpAVIPlayState->fp);
  183. lpAVIPlayState->dwAudioChannels = SDL_SwapLE16(waveFormat.nChannels);
  184. lpAVIPlayState->dwAudioSamplesPerSec = SDL_SwapLE16(waveFormat.nSamplesPerSec);
  185. lpAVIPlayState->dwAudioBitsPerSample = SDL_SwapLE16(waveFormat.wBitsPerSample);
  186. }
  187. break;
  188. default:
  189. break;
  190. }
  191. fseek(lpAVIPlayState->fp, dwNextOffset, SEEK_SET);
  192. }
  193. }
  194. static VOID
  195. PAL_ParseHdrlList(
  196. AVIPlayState *lpAVIPlayState,
  197. DWORD dwEndOffset
  198. )
  199. {
  200. AVIChunk hdr;
  201. DWORD dwNextOffset, dwListType;
  202. MainAVIHeader aviHeader;
  203. while (ftell(lpAVIPlayState->fp) < dwEndOffset)
  204. {
  205. if (feof(lpAVIPlayState->fp))
  206. {
  207. return; // end of file reached
  208. }
  209. fread(&hdr, sizeof(DWORD) * 2, 1, lpAVIPlayState->fp);
  210. hdr.dwFourCC = SDL_SwapLE32(hdr.dwFourCC);
  211. hdr.dwSize = SDL_SwapLE32(hdr.dwSize);
  212. dwNextOffset = ftell(lpAVIPlayState->fp) + hdr.dwSize;
  213. switch (hdr.dwFourCC)
  214. {
  215. case AVI_avih:
  216. fread(&aviHeader, sizeof(aviHeader), 1, lpAVIPlayState->fp);
  217. lpAVIPlayState->wWidth = aviHeader.dwWidth;
  218. lpAVIPlayState->wHeight = aviHeader.dwHeight;
  219. lpAVIPlayState->wMsPerFrame = aviHeader.dwMicroSecPerFrame / 1000;
  220. break;
  221. case AVI_LIST:
  222. fread(&dwListType, sizeof(dwListType), 1, lpAVIPlayState->fp);
  223. if (SDL_SwapLE32(dwListType) == AVI_strl)
  224. {
  225. PAL_ParseAVInfoList(lpAVIPlayState, dwNextOffset);
  226. }
  227. break;
  228. }
  229. fseek(lpAVIPlayState->fp, dwNextOffset, SEEK_SET);
  230. }
  231. }
  232. static VOID
  233. PAL_ReadAVIInfo(
  234. AVIPlayState *lpAVIPlayState
  235. )
  236. {
  237. AVIChunk hdr;
  238. DWORD dwListType = 0;
  239. DWORD dwNextOffset = 0;
  240. //
  241. // Skip RIFF header
  242. //
  243. fseek(lpAVIPlayState->fp, 12, SEEK_SET);
  244. while (TRUE)
  245. {
  246. if (feof(lpAVIPlayState->fp))
  247. {
  248. return; // end of file reached
  249. }
  250. fread(&hdr, sizeof(DWORD) * 2, 1, lpAVIPlayState->fp);
  251. hdr.dwFourCC = SDL_SwapLE32(hdr.dwFourCC);
  252. hdr.dwSize = SDL_SwapLE32(hdr.dwSize);
  253. dwNextOffset = ftell(lpAVIPlayState->fp) + hdr.dwSize;
  254. switch (hdr.dwFourCC)
  255. {
  256. case AVI_LIST:
  257. fread(&dwListType, sizeof(DWORD), 1, lpAVIPlayState->fp);
  258. dwListType = SDL_SwapLE32(dwListType);
  259. switch (dwListType)
  260. {
  261. case AVI_hdrl:
  262. PAL_ParseHdrlList(lpAVIPlayState, dwNextOffset);
  263. break;
  264. case AVI_movi:
  265. //
  266. // Stop right here as the actual movie data starts
  267. //
  268. lpAVIPlayState->dwVideoEndOffset = dwNextOffset;
  269. return;
  270. }
  271. break;
  272. case AVI_JUNK:
  273. default:
  274. //
  275. // Ignore these chunks
  276. //
  277. break;
  278. }
  279. fseek(lpAVIPlayState->fp, dwNextOffset, SEEK_SET);
  280. }
  281. }
  282. static AVIPlayState *
  283. PAL_OpenAVI(
  284. LPCSTR lpszPath
  285. )
  286. {
  287. AVIPlayState *ret;
  288. ret = (AVIPlayState *)UTIL_calloc(1, sizeof(AVIPlayState));
  289. //
  290. // Open the file
  291. //
  292. ret->fp = UTIL_OpenFile(lpszPath);
  293. if (ret->fp == NULL)
  294. {
  295. fprintf(stderr, "Cannot open file: %s!\n", lpszPath);
  296. free(ret);
  297. return NULL;
  298. }
  299. PAL_ReadAVIInfo(ret);
  300. if (ret->wWidth == 0 || ret->wHeight == 0)
  301. {
  302. return NULL;
  303. }
  304. //
  305. // Create surface
  306. //
  307. ret->surface = SDL_CreateRGBSurface(SDL_SWSURFACE, ret->wWidth, ret->wHeight, 16,
  308. 0x7C00, 0x03E0, 0x001F, 0x0000);
  309. //
  310. // Create mutex
  311. //
  312. ret->mtxAudioData = SDL_CreateMutex();
  313. //
  314. // Build SDL audio conversion info
  315. //
  316. SDL_BuildAudioCVT(&ret->cvt,
  317. (ret->dwAudioBitsPerSample == 8) ? AUDIO_U8 : AUDIO_S16LSB,
  318. ret->dwAudioChannels,
  319. ret->dwAudioSamplesPerSec,
  320. AUDIO_S16SYS,
  321. gConfig.iAudioChannels,
  322. gConfig.iSampleRate);
  323. return ret;
  324. }
  325. static AVIChunk *
  326. PAL_ReadAVChunk(
  327. AVIPlayState *lpAVIPlayState
  328. )
  329. {
  330. AVIChunk hdr;
  331. AVIChunk *ret = NULL;
  332. DWORD dwNextOffset;
  333. begin:
  334. if (feof(lpAVIPlayState->fp) || ftell(lpAVIPlayState->fp) >= lpAVIPlayState->dwVideoEndOffset)
  335. {
  336. return NULL; // end of file reached
  337. }
  338. fread(&hdr, sizeof(DWORD) * 2, 1, lpAVIPlayState->fp);
  339. hdr.dwFourCC = SDL_SwapLE32(hdr.dwFourCC);
  340. hdr.dwSize = SDL_SwapLE32(hdr.dwSize);
  341. dwNextOffset = ftell(lpAVIPlayState->fp) + hdr.dwSize;
  342. switch (hdr.dwFourCC)
  343. {
  344. case AVI_LIST:
  345. //
  346. // Just skip list header here
  347. //
  348. fseek(lpAVIPlayState->fp, sizeof(DWORD), SEEK_CUR);
  349. goto begin;
  350. case AVI_01wb:
  351. case AVI_00db:
  352. case AVI_00dc:
  353. //
  354. // got actual audio/video frame
  355. //
  356. ret = (AVIChunk *)UTIL_malloc(sizeof(DWORD) * 2 + hdr.dwSize);
  357. *ret = hdr;
  358. fread(ret->bData, hdr.dwSize, 1, lpAVIPlayState->fp);
  359. break;
  360. case AVI_JUNK:
  361. default:
  362. //
  363. // Ignore these chunks
  364. //
  365. fseek(lpAVIPlayState->fp, dwNextOffset, SEEK_SET);
  366. goto begin;
  367. }
  368. fseek(lpAVIPlayState->fp, dwNextOffset, SEEK_SET);
  369. return ret;
  370. }
  371. static VOID
  372. PAL_CloseAVI(
  373. AVIPlayState *lpAVIPlayState
  374. )
  375. {
  376. if (lpAVIPlayState->fp != NULL)
  377. {
  378. fclose(lpAVIPlayState->fp);
  379. }
  380. if (lpAVIPlayState->surface != NULL)
  381. {
  382. SDL_FreeSurface(lpAVIPlayState->surface);
  383. }
  384. if (lpAVIPlayState->mtxAudioData != NULL)
  385. {
  386. SDL_DestroyMutex(lpAVIPlayState->mtxAudioData);
  387. }
  388. free(lpAVIPlayState);
  389. }
  390. static VOID
  391. PAL_AVIFeedAudio(
  392. AVIPlayState *lpAVIPlayState,
  393. LPBYTE lpBuffer,
  394. DWORD dwSize
  395. )
  396. {
  397. SDL_mutexP(lpAVIPlayState->mtxAudioData);
  398. while (dwSize > 0)
  399. {
  400. DWORD dwFeedSize = dwSize;
  401. if (lpAVIPlayState->dwAudioWritePos + dwSize > sizeof(lpAVIPlayState->bAudioBuf))
  402. {
  403. dwFeedSize = sizeof(lpAVIPlayState->bAudioBuf) - lpAVIPlayState->dwAudioWritePos;
  404. }
  405. memcpy(&lpAVIPlayState->bAudioBuf[lpAVIPlayState->dwAudioWritePos], lpBuffer, dwFeedSize);
  406. lpAVIPlayState->dwAudioWritePos += dwFeedSize;
  407. lpAVIPlayState->dwAudioWritePos %= sizeof(lpAVIPlayState->bAudioBuf);
  408. lpBuffer += dwFeedSize;
  409. dwSize -= dwFeedSize;
  410. }
  411. SDL_mutexV(lpAVIPlayState->mtxAudioData);
  412. }
  413. VOID
  414. PAL_AVIInit(
  415. VOID
  416. )
  417. {
  418. gpAVIPlayStateMutex = SDL_CreateMutex();
  419. }
  420. VOID
  421. PAL_AVIShutdown(
  422. VOID
  423. )
  424. {
  425. SDL_DestroyMutex(gpAVIPlayStateMutex);
  426. }
  427. static VOID
  428. PAL_RenderAVIFrame(
  429. SDL_Surface *lpSurface,
  430. AVIChunk *lpChunk
  431. )
  432. {
  433. #define AV_RL16(x) ((((LPCBYTE)(x))[1] << 8) | ((LPCBYTE)(x))[0])
  434. #define CHECK_STREAM_PTR(n) if ((stream_ptr + n) > lpChunk->dwSize) { return; }
  435. int block_ptr, pixel_ptr;
  436. int total_blocks;
  437. int pixel_x, pixel_y; // pixel width and height iterators
  438. int block_x, block_y; // block width and height iterators
  439. int blocks_wide, blocks_high; // width and height in 4x4 blocks
  440. int block_inc;
  441. int row_dec;
  442. /* decoding parameters */
  443. int stream_ptr;
  444. unsigned char byte_a, byte_b;
  445. unsigned short flags;
  446. int skip_blocks;
  447. unsigned short colors[8];
  448. unsigned short *pixels = (unsigned short *)lpSurface->pixels;
  449. int stride = lpSurface->pitch / 2;
  450. stream_ptr = 0;
  451. skip_blocks = 0;
  452. blocks_wide = lpSurface->w / 4;
  453. blocks_high = lpSurface->h / 4;
  454. total_blocks = blocks_wide * blocks_high;
  455. block_inc = 4;
  456. row_dec = stride + 4;
  457. for (block_y = blocks_high; block_y > 0; block_y--)
  458. {
  459. block_ptr = ((block_y * 4) - 1) * stride;
  460. for (block_x = blocks_wide; block_x > 0; block_x--)
  461. {
  462. // check if this block should be skipped
  463. if (skip_blocks)
  464. {
  465. block_ptr += block_inc;
  466. skip_blocks--;
  467. total_blocks--;
  468. continue;
  469. }
  470. pixel_ptr = block_ptr;
  471. // get the next two bytes in the encoded data stream
  472. CHECK_STREAM_PTR(2);
  473. byte_a = lpChunk->bData[stream_ptr++];
  474. byte_b = lpChunk->bData[stream_ptr++];
  475. // check if the decode is finished
  476. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
  477. {
  478. return;
  479. }
  480. else if ((byte_b & 0xFC) == 0x84)
  481. {
  482. // skip code, but don't count the current block
  483. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  484. }
  485. else if (byte_b < 0x80)
  486. {
  487. // 2- or 8-color encoding modes
  488. flags = (byte_b << 8) | byte_a;
  489. CHECK_STREAM_PTR(4);
  490. colors[0] = AV_RL16(&lpChunk->bData[stream_ptr]);
  491. stream_ptr += 2;
  492. colors[1] = AV_RL16(&lpChunk->bData[stream_ptr]);
  493. stream_ptr += 2;
  494. if (colors[0] & 0x8000)
  495. {
  496. // 8-color encoding
  497. CHECK_STREAM_PTR(12);
  498. colors[2] = AV_RL16(&lpChunk->bData[stream_ptr]);
  499. stream_ptr += 2;
  500. colors[3] = AV_RL16(&lpChunk->bData[stream_ptr]);
  501. stream_ptr += 2;
  502. colors[4] = AV_RL16(&lpChunk->bData[stream_ptr]);
  503. stream_ptr += 2;
  504. colors[5] = AV_RL16(&lpChunk->bData[stream_ptr]);
  505. stream_ptr += 2;
  506. colors[6] = AV_RL16(&lpChunk->bData[stream_ptr]);
  507. stream_ptr += 2;
  508. colors[7] = AV_RL16(&lpChunk->bData[stream_ptr]);
  509. stream_ptr += 2;
  510. for (pixel_y = 0; pixel_y < 4; pixel_y++)
  511. {
  512. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  513. {
  514. pixels[pixel_ptr++] =
  515. colors[((pixel_y & 0x2) << 1) +
  516. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  517. }
  518. pixel_ptr -= row_dec;
  519. }
  520. }
  521. else
  522. {
  523. // 2-color encoding
  524. for (pixel_y = 0; pixel_y < 4; pixel_y++)
  525. {
  526. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  527. {
  528. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  529. }
  530. pixel_ptr -= row_dec;
  531. }
  532. }
  533. }
  534. else
  535. {
  536. // otherwise, it's a 1-color block
  537. colors[0] = (byte_b << 8) | byte_a;
  538. for (pixel_y = 0; pixel_y < 4; pixel_y++)
  539. {
  540. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  541. {
  542. pixels[pixel_ptr++] = colors[0];
  543. }
  544. pixel_ptr -= row_dec;
  545. }
  546. }
  547. block_ptr += block_inc;
  548. total_blocks--;
  549. }
  550. }
  551. }
  552. BOOL
  553. PAL_PlayAVI(
  554. LPCSTR lpszPath
  555. )
  556. {
  557. AVIPlayState *avi;
  558. AVIChunk *chunk;
  559. DWORD dwNextFrameTime = 0, dwCurrentTime = 0;
  560. BOOL fEndPlay = FALSE;
  561. //
  562. // Open AVI file
  563. //
  564. avi = PAL_OpenAVI(lpszPath);
  565. if (avi == NULL)
  566. {
  567. return FALSE;
  568. }
  569. SDL_mutexP(gpAVIPlayStateMutex);
  570. gpAVIPlayState = avi;
  571. SDL_mutexV(gpAVIPlayStateMutex);
  572. PAL_ClearKeyState();
  573. while (!fEndPlay && (chunk = PAL_ReadAVChunk(avi)) != NULL)
  574. {
  575. dwCurrentTime = SDL_GetTicks();
  576. switch (chunk->dwFourCC)
  577. {
  578. case AVI_00dc:
  579. case AVI_00db:
  580. //
  581. // Video frame
  582. //
  583. PAL_RenderAVIFrame(avi->surface, chunk);
  584. VIDEO_DrawSurfaceToScreen(avi->surface);
  585. dwNextFrameTime = dwCurrentTime + avi->wMsPerFrame;
  586. dwCurrentTime = SDL_GetTicks();
  587. if (dwCurrentTime >= dwNextFrameTime)
  588. {
  589. UTIL_Delay(1);
  590. }
  591. else
  592. {
  593. UTIL_Delay(dwNextFrameTime - dwCurrentTime);
  594. }
  595. if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
  596. {
  597. fEndPlay = TRUE;
  598. }
  599. break;
  600. case AVI_01wb:
  601. //
  602. // Audio data, just feed into buffer
  603. //
  604. PAL_AVIFeedAudio(avi, chunk->bData, chunk->dwSize);
  605. break;
  606. }
  607. free(chunk);
  608. }
  609. SDL_mutexP(gpAVIPlayStateMutex);
  610. gpAVIPlayState = NULL;
  611. SDL_mutexV(gpAVIPlayStateMutex);
  612. if (fEndPlay)
  613. {
  614. //
  615. // Simulate a short delay (like the original game)
  616. //
  617. UTIL_Delay(500);
  618. }
  619. PAL_CloseAVI(avi);
  620. return TRUE;
  621. }
  622. VOID SDLCALL
  623. AVI_FillAudioBuffer(
  624. LPVOID udata,
  625. LPBYTE stream,
  626. INT len
  627. )
  628. {
  629. SDL_mutexP(gpAVIPlayStateMutex);
  630. if (gpAVIPlayState != NULL)
  631. {
  632. FLOAT flRateScale = ((FLOAT)gConfig.iSampleRate / gpAVIPlayState->dwAudioSamplesPerSec);
  633. while (len > 0 && gpAVIPlayState->dwAudioReadPos != gpAVIPlayState->dwAudioWritePos)
  634. {
  635. INT remainingLen = gpAVIPlayState->dwAudioWritePos - gpAVIPlayState->dwAudioReadPos;
  636. INT samplesToRead;
  637. if (remainingLen < 0)
  638. {
  639. remainingLen = sizeof(gpAVIPlayState->bAudioBuf) - gpAVIPlayState->dwAudioReadPos;
  640. }
  641. samplesToRead = remainingLen / gpAVIPlayState->dwAudioChannels / (gpAVIPlayState->dwAudioBitsPerSample / 8);
  642. if (samplesToRead > len / 2 / gConfig.iAudioChannels / flRateScale)
  643. {
  644. samplesToRead = len / 2 / gConfig.iAudioChannels / flRateScale;
  645. }
  646. gpAVIPlayState->cvt.buf = stream;
  647. gpAVIPlayState->cvt.len = samplesToRead *
  648. gpAVIPlayState->dwAudioChannels * (gpAVIPlayState->dwAudioBitsPerSample / 8);
  649. memcpy(stream, &gpAVIPlayState->bAudioBuf[gpAVIPlayState->dwAudioReadPos],
  650. samplesToRead * gpAVIPlayState->dwAudioChannels * (gpAVIPlayState->dwAudioBitsPerSample / 8));
  651. SDL_ConvertAudio(&gpAVIPlayState->cvt);
  652. stream += (DWORD)(samplesToRead * 2 * gConfig.iAudioChannels * flRateScale);
  653. len -= samplesToRead * 2 * gConfig.iAudioChannels * flRateScale;
  654. gpAVIPlayState->dwAudioReadPos += samplesToRead * gpAVIPlayState->dwAudioChannels *
  655. (gpAVIPlayState->dwAudioBitsPerSample / 8);
  656. gpAVIPlayState->dwAudioReadPos %= sizeof(gpAVIPlayState->bAudioBuf);
  657. }
  658. }
  659. SDL_mutexV(gpAVIPlayStateMutex);
  660. }