aviplay.c 22 KB

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