aviplay.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 "util.h"
  53. #include "audio.h"
  54. #include "aviplay.h"
  55. #include "input.h"
  56. #include "video.h"
  57. #include "riff.h"
  58. #include "palcfg.h"
  59. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  60. # define SwapStruct32(v, s) \
  61. for(int s##_i = 0; s##_i < sizeof(s) / sizeof(uint32_t); s##_i++) \
  62. ((uint32_t *)&v)[s##_i] = SDL_Swap32(((uint32_t *)&v)[s##_i])
  63. # define SwapStructFields(v, f1, f2) v.##f1 ^= v.##f2, v.##f2 ^= v.##f1, v.##f1 ^= v.##f2
  64. #else
  65. # define SwapStruct32(...)
  66. # define SwapStructFields(...)
  67. #endif
  68. #define HAS_FLAG(v, f) (((v) & (f)) == (f))
  69. #define MAX_AVI_BLOCK_LEVELS 3
  70. #define FLAGS_AVI_MAIN_HEADER 0x01
  71. #define FLAGS_AVI_VIDEO_FORMAT 0x02
  72. #define FLAGS_AVI_AUDIO_FORMAT 0x04
  73. #define FLAGS_AVI_ALL_HEADERS 0x07
  74. typedef struct AVIPlayState
  75. {
  76. SDL_mutex *selfMutex;
  77. volatile FILE *fp; // pointer to the AVI file
  78. SDL_Surface *surface; // video buffer
  79. long lVideoEndPos;
  80. uint32_t dwMicroSecPerFrame; // microseconds per frame
  81. uint32_t dwBufferSize;
  82. SDL_AudioCVT cvt;
  83. uint8_t *pChunkBuffer;
  84. uint8_t *pbAudioBuf; // ring buffer for audio data
  85. uint32_t dwAudBufLen;
  86. uint32_t dwAudioReadPos;
  87. uint32_t dwAudioWritePos;
  88. BOOL fInterleaved;
  89. } AVIPlayState;
  90. static AVIPlayState gAVIPlayState;
  91. static AVIPlayState *
  92. PAL_ReadAVIInfo(
  93. FILE *fp,
  94. AVIPlayState *avi
  95. )
  96. {
  97. RIFFHeader hdr;
  98. AVIMainHeader aviHeader;
  99. AVIStreamHeader streamHeader = { 0 };
  100. BitmapInfoHeader bih;
  101. WAVEFormatEx wfe;
  102. uint32_t block_type[MAX_AVI_BLOCK_LEVELS];
  103. long next_pos[MAX_AVI_BLOCK_LEVELS];
  104. long file_length = (fseek(fp, 0, SEEK_END), ftell(fp)), pos = 0;
  105. int current_level = 0, flags = 0;
  106. //
  107. // Check RIFF file header
  108. //
  109. fseek(fp, 0, SEEK_SET);
  110. if (fread(&hdr, sizeof(RIFFHeader), 1, fp) != 1 ||
  111. hdr.signature != RIFF_RIFF || hdr.type != RIFF_AVI ||
  112. hdr.length > (uint32_t)(file_length - sizeof(RIFFHeader) + sizeof(uint32_t)))
  113. {
  114. UTIL_LogOutput(LOGLEVEL_WARNING, "Illegal AVI RIFF header!");
  115. return NULL;
  116. }
  117. else
  118. {
  119. next_pos[current_level] = (pos += sizeof(RIFFHeader)) + hdr.length;
  120. block_type[current_level++] = hdr.type;
  121. }
  122. while (!feof(fp) && current_level > 0)
  123. {
  124. RIFFBlockHeader block;
  125. fseek(fp, pos, SEEK_SET);
  126. if (fread(&block.type, sizeof(RIFFChunkHeader), 1, fp) != 1)
  127. {
  128. UTIL_LogOutput(LOGLEVEL_WARNING, "Illegal AVI RIFF LIST/Chunk header!");
  129. return NULL;
  130. }
  131. else
  132. {
  133. block.type = SDL_SwapLE32(block.type);
  134. block.length = SDL_SwapLE32(block.length);
  135. pos += sizeof(RIFFChunkHeader);
  136. }
  137. //
  138. // Read further if current block is a 'LIST'
  139. //
  140. if (block.type == AVI_LIST)
  141. {
  142. if (fread(&block.list.type, sizeof(RIFFListHeader) - sizeof(RIFFChunkHeader), 1, fp) != 1)
  143. {
  144. UTIL_LogOutput(LOGLEVEL_WARNING, "Illegal AVI RIFF LIST header!");
  145. return NULL;
  146. }
  147. else
  148. {
  149. block.list.type = SDL_SwapLE32(block.list.type);
  150. }
  151. }
  152. switch (block_type[current_level - 1])
  153. {
  154. case RIFF_AVI:
  155. //
  156. // RIFF_AVI only appears at top-level
  157. //
  158. if (current_level != 1)
  159. {
  160. UTIL_LogOutput(LOGLEVEL_WARNING, "RIFF 'AVI ' block appears at non-top level!");
  161. return NULL;
  162. }
  163. //
  164. // For 'LIST' block, should read its contents
  165. //
  166. if (block.type == AVI_LIST)
  167. {
  168. next_pos[current_level] = pos + block.length;
  169. block_type[current_level++] = block.list.type;
  170. pos += sizeof(RIFFListHeader) - sizeof(RIFFChunkHeader);
  171. continue;
  172. }
  173. //
  174. // Ignore any block types other than 'LIST'
  175. //
  176. break;
  177. case AVI_hdrl:
  178. //
  179. // AVI_hdrl only appears at second-level
  180. //
  181. if (current_level != 2)
  182. {
  183. UTIL_LogOutput(LOGLEVEL_WARNING, "RIFF 'hdrl' block does not appear at second level!");
  184. return NULL;
  185. }
  186. switch (block.type)
  187. {
  188. case AVI_avih:
  189. //
  190. // The main header should only appear once
  191. //
  192. if (HAS_FLAG(flags, FLAGS_AVI_MAIN_HEADER))
  193. {
  194. UTIL_LogOutput(LOGLEVEL_WARNING, "More than one RIFF 'avih' blocks appear!");
  195. return NULL;
  196. }
  197. if (fread(&aviHeader, sizeof(AVIMainHeader), 1, fp) != 1)
  198. {
  199. UTIL_LogOutput(LOGLEVEL_WARNING, "RIFF 'avih' blocks corrupted!");
  200. return NULL;
  201. }
  202. SwapStruct32(aviHeader, AVIMainHeader);
  203. flags |= FLAGS_AVI_MAIN_HEADER;
  204. if (aviHeader.dwWidth == 0 || aviHeader.dwHeight == 0)
  205. {
  206. UTIL_LogOutput(LOGLEVEL_WARNING, "Invalid AVI frame size!");
  207. return NULL;
  208. }
  209. if (HAS_FLAG(aviHeader.dwFlags, AVIF_MUSTUSEINDEX))
  210. {
  211. UTIL_LogOutput(LOGLEVEL_WARNING, "No built-in support for index-based AVI!");
  212. return NULL;
  213. }
  214. break;
  215. case AVI_LIST:
  216. if (block.list.type == AVI_strl)
  217. {
  218. next_pos[current_level] = pos + block.length;
  219. block_type[current_level++] = block.list.type;
  220. pos += sizeof(RIFFListHeader) - sizeof(RIFFChunkHeader);
  221. continue;
  222. }
  223. break;
  224. }
  225. break;
  226. case AVI_movi:
  227. //
  228. // AVI_movi only appears at second-level and all headers should be read before
  229. //
  230. if (current_level != 2 || !HAS_FLAG(flags, FLAGS_AVI_ALL_HEADERS))
  231. {
  232. UTIL_LogOutput(LOGLEVEL_WARNING, "RIFF 'movi' block does not appear at second level or the AVI does not contain both video & audio!");
  233. return NULL;
  234. }
  235. //
  236. // Stop parsing here as actual movie data starts
  237. //
  238. fseek(fp, pos - sizeof(RIFFChunkHeader), SEEK_SET);
  239. avi->lVideoEndPos = next_pos[current_level - 1];
  240. avi->dwMicroSecPerFrame = aviHeader.dwMicroSecPerFrame;
  241. //
  242. // Create surface
  243. //
  244. avi->surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
  245. bih.biWidth, bih.biHeight, bih.biBitCount,
  246. 0x7C00, 0x03E0, 0x001F, 0x0000);
  247. //
  248. // Build SDL audio conversion info
  249. //
  250. SDL_BuildAudioCVT(&avi->cvt,
  251. (wfe.format.wBitsPerSample == 8) ? AUDIO_U8 : AUDIO_S16LSB,
  252. wfe.format.nChannels, wfe.format.nSamplesPerSec,
  253. AUDIO_S16SYS,
  254. AUDIO_GetDeviceSpec()->channels,
  255. AUDIO_GetDeviceSpec()->freq);
  256. //
  257. // Allocate chunk buffer
  258. // Since SDL converts audio in-place, we need to make the buffer large enough to hold converted data
  259. //
  260. avi->dwBufferSize = aviHeader.dwSuggestedBufferSize * avi->cvt.len_mult + sizeof(RIFFChunkHeader);
  261. if (avi->dwBufferSize > 0)
  262. avi->pChunkBuffer = UTIL_malloc(avi->dwBufferSize);
  263. else
  264. avi->pChunkBuffer = NULL;
  265. //
  266. // Allocate audio buffer, the buffer size is large enough to hold two-second audio data
  267. //
  268. avi->dwAudBufLen = max(wfe.format.nAvgBytesPerSec * 2, aviHeader.dwSuggestedBufferSize) * avi->cvt.len_mult;
  269. avi->pbAudioBuf = (uint8_t *)UTIL_malloc(avi->dwAudBufLen);
  270. avi->dwAudioReadPos = avi->dwAudioWritePos = 0;
  271. return avi;
  272. case AVI_strl:
  273. //
  274. // AVI_strl only appears at third-level
  275. //
  276. if (current_level != 3)
  277. {
  278. UTIL_LogOutput(LOGLEVEL_WARNING, "RIFF 'hdrl' block does not appear at third level!");
  279. return NULL;
  280. }
  281. switch (block.type)
  282. {
  283. case AVI_strh:
  284. // strh should be the first block of the list
  285. if (streamHeader.fccType != 0)
  286. {
  287. UTIL_LogOutput(LOGLEVEL_WARNING, "RIFF 'strh' block does not appear at first!");
  288. return NULL;
  289. }
  290. if (fread(&streamHeader, sizeof(AVIStreamHeader), 1, fp) != 1)
  291. {
  292. UTIL_LogOutput(LOGLEVEL_WARNING, "RIFF 'hdrl' block data corrupted!");
  293. return NULL;
  294. }
  295. SwapStruct32(streamHeader, AVIStreamHeader);
  296. SwapStructFields(streamHeader, wLanguage, wPriority);
  297. SwapStructFields(streamHeader, rcFrame[0], rcFrame[1]);
  298. SwapStructFields(streamHeader, rcFrame[2], rcFrame[3]);
  299. break;
  300. case AVI_strf:
  301. //
  302. // AVI_strf should follow AVI_strh
  303. // Accept only one video stream & one audio stream
  304. //
  305. switch (streamHeader.fccType)
  306. {
  307. case AVI_vids:
  308. if (HAS_FLAG(flags, FLAGS_AVI_VIDEO_FORMAT) || (streamHeader.fccHandler != VIDS_MSVC && streamHeader.fccHandler != VIDS_msvc))
  309. {
  310. UTIL_LogOutput(LOGLEVEL_WARNING, "The AVI uses video codec with no built-in support, or video codec appeared before!");
  311. return NULL;
  312. }
  313. if (fread(&bih, sizeof(BitmapInfoHeader), 1, fp) != 1)
  314. {
  315. UTIL_LogOutput(LOGLEVEL_WARNING, "Video codec information corrupted!");
  316. return NULL;
  317. }
  318. SwapStruct32(bih, BitmapInfoHeader);
  319. SwapStructFields(bih, biPlanes, biBitCount);
  320. if (bih.biBitCount != 16)
  321. {
  322. UTIL_LogOutput(LOGLEVEL_WARNING, "Built-in AVI playing support only 16-bit video!");
  323. return NULL;
  324. }
  325. flags |= FLAGS_AVI_VIDEO_FORMAT;
  326. break;
  327. case AVI_auds:
  328. if (HAS_FLAG(flags, FLAGS_AVI_AUDIO_FORMAT) || streamHeader.fccHandler != 0)
  329. {
  330. UTIL_LogOutput(LOGLEVEL_WARNING, "The AVI uses audio codec with no built-in support, or audio codec appeared before!");
  331. return NULL;
  332. }
  333. if (fread(&wfe, sizeof(WAVEFormatPCM) + sizeof(uint16_t), 1, fp) != 1)
  334. {
  335. UTIL_LogOutput(LOGLEVEL_WARNING, "Audio codec information corrupted!");
  336. return NULL;
  337. }
  338. SwapStruct32(wfe, WAVEFormatPCM);
  339. SwapStructFields(wfe, wFormatTag, nChannels);
  340. SwapStructFields(wfe, nBlockAlign, wBitsPerSample);
  341. flags |= FLAGS_AVI_AUDIO_FORMAT;
  342. break;
  343. }
  344. //
  345. // One strf per strh, reset the fccType here to prepare for next strh
  346. //
  347. streamHeader.fccType = 0;
  348. break;
  349. }
  350. }
  351. //
  352. // Goto next block
  353. //
  354. pos += block.length;
  355. //
  356. // Check if it is the end of the parent block
  357. //
  358. while (current_level > 0 && pos == next_pos[current_level - 1])
  359. {
  360. current_level--;
  361. }
  362. //
  363. // Returns NULL if block is illegaly formed
  364. //
  365. if (current_level > 0 && pos > next_pos[current_level - 1])
  366. {
  367. return NULL;
  368. }
  369. }
  370. return NULL;
  371. }
  372. static RIFFChunk *
  373. PAL_ReadDataChunk(
  374. FILE *fp,
  375. long endPos,
  376. void *userbuf,
  377. uint32_t buflen,
  378. int mult
  379. )
  380. {
  381. RIFFBlockHeader hdr;
  382. RIFFChunk *chunk = NULL;
  383. long pos = feof(fp) ? endPos : ftell(fp);
  384. while (chunk == NULL && pos < endPos)
  385. {
  386. if (fread(&hdr, sizeof(RIFFChunkHeader), 1, fp) != 1) return NULL;
  387. hdr.type = SDL_SwapLE32(hdr.type);
  388. hdr.length = SDL_SwapLE32(hdr.length);
  389. pos += sizeof(RIFFChunkHeader);
  390. switch (hdr.type)
  391. {
  392. case AVI_01wb:
  393. case AVI_00db:
  394. case AVI_00dc:
  395. //
  396. // got actual audio/video frame
  397. //
  398. if (userbuf && buflen >= sizeof(RIFFChunkHeader) + hdr.length)
  399. chunk = (RIFFChunk *)userbuf;
  400. else
  401. chunk = (RIFFChunk *)UTIL_malloc(sizeof(RIFFChunkHeader) + hdr.length * (hdr.type == AVI_01wb ? mult : 1));
  402. if (fread(chunk->data, hdr.length, 1, fp) != 1)
  403. {
  404. free(chunk);
  405. return NULL;
  406. }
  407. chunk->header = hdr.chunk;
  408. break;
  409. case AVI_LIST:
  410. //
  411. // Only 'rec ' LIST is allowed here, if not, skip it completely
  412. //
  413. if (fread(&hdr.list.type, sizeof(uint32_t), 1, fp) != 1) return NULL;
  414. if (hdr.list.type == AVI_rec) break;
  415. case AVI_JUNK:
  416. default:
  417. //
  418. // Ignore unrecognized chunks
  419. //
  420. fseek(fp, pos += hdr.length, SEEK_SET);
  421. }
  422. }
  423. return chunk;
  424. }
  425. static void
  426. PAL_AVIFeedAudio(
  427. AVIPlayState *avi,
  428. uint8_t *buffer,
  429. uint32_t size
  430. )
  431. {
  432. //
  433. // Convert audio in-place at the original buffer
  434. // This makes filling process much more simpler
  435. //
  436. avi->cvt.buf = buffer;
  437. avi->cvt.len = size;
  438. SDL_ConvertAudio(&avi->cvt);
  439. size = avi->cvt.len_cvt;
  440. SDL_mutexP(avi->selfMutex);
  441. while (size > 0)
  442. {
  443. uint32_t feed_size = (avi->dwAudioWritePos + size > avi->dwAudBufLen) ? avi->dwAudBufLen - avi->dwAudioWritePos : size;
  444. memcpy(avi->pbAudioBuf + avi->dwAudioWritePos, buffer, feed_size);
  445. avi->dwAudioWritePos = (avi->dwAudioWritePos + feed_size) % avi->dwAudBufLen;
  446. buffer += feed_size;
  447. size -= feed_size;
  448. }
  449. SDL_mutexV(avi->selfMutex);
  450. }
  451. void
  452. PAL_AVIInit(
  453. void
  454. )
  455. {
  456. gAVIPlayState.selfMutex = SDL_CreateMutex();
  457. }
  458. void
  459. PAL_AVIShutdown(
  460. void
  461. )
  462. {
  463. SDL_DestroyMutex(gAVIPlayState.selfMutex);
  464. }
  465. static void
  466. PAL_RenderAVIFrameToSurface(
  467. SDL_Surface *lpSurface,
  468. const RIFFChunk *lpChunk
  469. )
  470. {
  471. #define AV_RL16(x) ((((const uint8_t *)(x))[1] << 8) | ((const uint8_t *)(x))[0])
  472. #define CHECK_STREAM_PTR(n) if ((stream_ptr + n) > lpChunk->header.length) { return; }
  473. /* decoding parameters */
  474. uint16_t *pixels = (unsigned short *)lpSurface->pixels;
  475. uint32_t stream_ptr = 0, skip_blocks = 0;
  476. uint32_t stride = lpSurface->pitch >> 1;
  477. const int block_inc = 4;
  478. const int row_dec = stride + 4;
  479. const int blocks_wide = lpSurface->w >> 2; // width in 4x4 blocks
  480. const int blocks_high = lpSurface->h >> 2; // height in 4x4 blocks
  481. uint32_t total_blocks = blocks_wide * blocks_high;
  482. for (int block_y = blocks_high; block_y > 0; block_y--)
  483. {
  484. int block_ptr = ((block_y * 4) - 1) * stride;
  485. for (int block_x = blocks_wide; block_x > 0; block_x--)
  486. {
  487. // check if this block should be skipped
  488. if (skip_blocks)
  489. {
  490. block_ptr += block_inc;
  491. skip_blocks--;
  492. total_blocks--;
  493. continue;
  494. }
  495. int pixel_ptr = block_ptr;
  496. // get the next two bytes in the encoded data stream
  497. CHECK_STREAM_PTR(2);
  498. uint8_t byte_a = lpChunk->data[stream_ptr++];
  499. uint8_t byte_b = lpChunk->data[stream_ptr++];
  500. // check if the decode is finished
  501. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
  502. {
  503. return;
  504. }
  505. else if ((byte_b & 0xFC) == 0x84)
  506. {
  507. // skip code, but don't count the current block
  508. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  509. }
  510. else if (byte_b < 0x80)
  511. {
  512. // 2- or 8-color encoding modes
  513. uint16_t flags = (byte_b << 8) | byte_a;
  514. uint16_t colors[8];
  515. CHECK_STREAM_PTR(4);
  516. colors[0] = AV_RL16(&lpChunk->data[stream_ptr]);
  517. stream_ptr += 2;
  518. colors[1] = AV_RL16(&lpChunk->data[stream_ptr]);
  519. stream_ptr += 2;
  520. if (colors[0] & 0x8000)
  521. {
  522. // 8-color encoding
  523. CHECK_STREAM_PTR(12);
  524. colors[2] = AV_RL16(&lpChunk->data[stream_ptr]);
  525. stream_ptr += 2;
  526. colors[3] = AV_RL16(&lpChunk->data[stream_ptr]);
  527. stream_ptr += 2;
  528. colors[4] = AV_RL16(&lpChunk->data[stream_ptr]);
  529. stream_ptr += 2;
  530. colors[5] = AV_RL16(&lpChunk->data[stream_ptr]);
  531. stream_ptr += 2;
  532. colors[6] = AV_RL16(&lpChunk->data[stream_ptr]);
  533. stream_ptr += 2;
  534. colors[7] = AV_RL16(&lpChunk->data[stream_ptr]);
  535. stream_ptr += 2;
  536. for (int pixel_y = 0; pixel_y < 4; pixel_y++)
  537. {
  538. for (int pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  539. {
  540. pixels[pixel_ptr++] =
  541. colors[((pixel_y & 0x2) << 1) +
  542. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  543. }
  544. pixel_ptr -= row_dec;
  545. }
  546. }
  547. else
  548. {
  549. // 2-color encoding
  550. for (int pixel_y = 0; pixel_y < 4; pixel_y++)
  551. {
  552. for (int pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  553. {
  554. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  555. }
  556. pixel_ptr -= row_dec;
  557. }
  558. }
  559. }
  560. else
  561. {
  562. // otherwise, it's a 1-color block
  563. uint16_t color = (byte_b << 8) | byte_a;
  564. for (int pixel_y = 0; pixel_y < 4; pixel_y++)
  565. {
  566. for (int pixel_x = 0; pixel_x < 4; pixel_x++)
  567. {
  568. pixels[pixel_ptr++] = color;
  569. }
  570. pixel_ptr -= row_dec;
  571. }
  572. }
  573. block_ptr += block_inc;
  574. total_blocks--;
  575. }
  576. }
  577. }
  578. BOOL
  579. PAL_PlayAVI(
  580. LPCSTR lpszPath
  581. )
  582. {
  583. if (!gConfig.fEnableAviPlay) return FALSE;
  584. //
  585. // Open the file
  586. //
  587. FILE *fp = UTIL_OpenFile(lpszPath);
  588. if (fp == NULL)
  589. {
  590. UTIL_LogOutput(LOGLEVEL_WARNING, "Cannot open AVI file: %s!\n", lpszPath);
  591. return FALSE;
  592. }
  593. AVIPlayState *avi = PAL_ReadAVIInfo(fp, &gAVIPlayState);
  594. if (avi == NULL)
  595. {
  596. UTIL_LogOutput(LOGLEVEL_WARNING, "Failed to parse AVI file or its format not supported!\n");
  597. fclose(fp);
  598. return FALSE;
  599. }
  600. PAL_ClearKeyState();
  601. BOOL fEndPlay = FALSE;
  602. RIFFChunk *buf = (RIFFChunk *)avi->pChunkBuffer;
  603. uint32_t len = avi->dwBufferSize;
  604. uint32_t dwMicroSecChange = 0;
  605. uint32_t dwCurrentTime = SDL_GetTicks();
  606. uint32_t dwNextFrameTime;
  607. uint32_t dwFrameStartTime = dwCurrentTime;
  608. while (!fEndPlay)
  609. {
  610. RIFFChunk *chunk = PAL_ReadDataChunk(fp, avi->lVideoEndPos, buf, len, avi->cvt.len_mult);
  611. if (chunk == NULL) break;
  612. switch (chunk->header.type)
  613. {
  614. case AVI_00dc:
  615. case AVI_00db:
  616. //
  617. // Video frame
  618. //
  619. dwNextFrameTime = dwFrameStartTime + (avi->dwMicroSecPerFrame / 1000);
  620. dwMicroSecChange += avi->dwMicroSecPerFrame % 1000;
  621. dwNextFrameTime += dwMicroSecChange / 1000;
  622. dwMicroSecChange %= 1000;
  623. PAL_RenderAVIFrameToSurface(avi->surface, chunk);
  624. VIDEO_DrawSurfaceToScreen(avi->surface);
  625. dwCurrentTime = SDL_GetTicks();
  626. // Check input states here
  627. UTIL_Delay(dwCurrentTime >= dwNextFrameTime ? 1 : dwNextFrameTime - dwCurrentTime);
  628. dwFrameStartTime = SDL_GetTicks();
  629. if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
  630. {
  631. fEndPlay = TRUE;
  632. }
  633. break;
  634. case AVI_01wb:
  635. //
  636. // Audio data, just convert it & feed into buffer
  637. //
  638. PAL_AVIFeedAudio(avi, chunk->data, chunk->header.length);
  639. //
  640. // Only enable AVI audio when data are available
  641. // We do not lock on the 'if' because only this function changes 'avi->fp'
  642. //
  643. if (!avi->fp)
  644. {
  645. SDL_mutexP(avi->selfMutex);
  646. avi->fp = fp;
  647. SDL_mutexV(avi->selfMutex);
  648. }
  649. break;
  650. }
  651. if (chunk != buf) free(chunk);
  652. }
  653. SDL_mutexP(avi->selfMutex);
  654. avi->fp = NULL;
  655. SDL_mutexV(avi->selfMutex);
  656. if (fEndPlay)
  657. {
  658. //
  659. // Simulate a short delay (like the original game)
  660. //
  661. UTIL_Delay(500);
  662. }
  663. if (avi->surface != NULL)
  664. {
  665. SDL_FreeSurface(avi->surface);
  666. avi->surface = NULL;
  667. }
  668. if (avi->pChunkBuffer)
  669. {
  670. free(avi->pChunkBuffer);
  671. avi->pChunkBuffer = NULL;
  672. }
  673. if (avi->pbAudioBuf)
  674. {
  675. free(avi->pbAudioBuf);
  676. avi->pbAudioBuf = NULL;
  677. }
  678. fclose(fp);
  679. return TRUE;
  680. }
  681. VOID SDLCALL
  682. AVI_FillAudioBuffer(
  683. void *udata,
  684. uint8_t *stream,
  685. int len
  686. )
  687. {
  688. AVIPlayState *avi = (AVIPlayState *)udata;
  689. SDL_mutexP(avi->selfMutex);
  690. while (avi->fp != NULL && len > 0 && avi->dwAudioWritePos != avi->dwAudioReadPos)
  691. {
  692. uint32_t fill_size = (avi->dwAudioReadPos + len > avi->dwAudBufLen) ? avi->dwAudBufLen - avi->dwAudioReadPos : len;
  693. if (avi->dwAudioWritePos > avi->dwAudioReadPos &&
  694. fill_size > avi->dwAudioWritePos - avi->dwAudioReadPos)
  695. {
  696. fill_size = avi->dwAudioWritePos - avi->dwAudioReadPos;
  697. }
  698. memcpy(stream, avi->pbAudioBuf + avi->dwAudioReadPos, fill_size);
  699. avi->dwAudioReadPos = (avi->dwAudioReadPos + fill_size) % avi->dwAudBufLen;
  700. stream += fill_size;
  701. len -= fill_size;
  702. }
  703. SDL_mutexV(avi->selfMutex);
  704. }
  705. void *
  706. AVI_GetPlayState(
  707. void
  708. )
  709. {
  710. return &gAVIPlayState;
  711. }