aviplay.c 21 KB

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