aviplay.c 21 KB

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