aviplay.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. hdr.list.type = SDL_SwapLE32(hdr.list.type);
  422. if (hdr.list.type == AVI_rec) break;
  423. case AVI_JUNK:
  424. default:
  425. //
  426. // Ignore unrecognized chunks
  427. //
  428. fseek(fp, pos += hdr.length, SEEK_SET);
  429. }
  430. }
  431. return chunk;
  432. }
  433. static void
  434. PAL_AVIFeedAudio(
  435. AVIPlayState *avi,
  436. uint8_t *buffer,
  437. uint32_t size
  438. )
  439. {
  440. //
  441. // Convert audio in-place at the original buffer
  442. // This makes filling process much more simpler
  443. //
  444. avi->cvt.buf = buffer;
  445. avi->cvt.len = size;
  446. SDL_ConvertAudio(&avi->cvt);
  447. size = avi->cvt.len_cvt;
  448. SDL_mutexP(avi->selfMutex);
  449. while (size > 0)
  450. {
  451. uint32_t feed_size = (avi->dwAudioWritePos + size > avi->dwAudBufLen) ? avi->dwAudBufLen - avi->dwAudioWritePos : size;
  452. memcpy(avi->pbAudioBuf + avi->dwAudioWritePos, buffer, feed_size);
  453. avi->dwAudioWritePos = (avi->dwAudioWritePos + feed_size) % avi->dwAudBufLen;
  454. buffer += feed_size;
  455. size -= feed_size;
  456. }
  457. SDL_mutexV(avi->selfMutex);
  458. }
  459. void
  460. PAL_AVIInit(
  461. void
  462. )
  463. {
  464. gAVIPlayState.selfMutex = SDL_CreateMutex();
  465. }
  466. void
  467. PAL_AVIShutdown(
  468. void
  469. )
  470. {
  471. SDL_DestroyMutex(gAVIPlayState.selfMutex);
  472. }
  473. static void
  474. PAL_RenderAVIFrameToSurface(
  475. SDL_Surface *lpSurface,
  476. const RIFFChunk *lpChunk
  477. )
  478. {
  479. #define AV_RL16(x) ((((const uint8_t *)(x))[1] << 8) | ((const uint8_t *)(x))[0])
  480. #define CHECK_STREAM_PTR(n) if ((stream_ptr + n) > lpChunk->header.length) { return; }
  481. /* decoding parameters */
  482. uint16_t *pixels = (unsigned short *)lpSurface->pixels;
  483. uint32_t stream_ptr = 0, skip_blocks = 0;
  484. uint32_t stride = lpSurface->pitch >> 1;
  485. const int block_inc = 4;
  486. const int row_dec = stride + 4;
  487. const int blocks_wide = lpSurface->w >> 2; // width in 4x4 blocks
  488. const int blocks_high = lpSurface->h >> 2; // height in 4x4 blocks
  489. uint32_t total_blocks = blocks_wide * blocks_high;
  490. for (int block_y = blocks_high; block_y > 0; block_y--)
  491. {
  492. int block_ptr = ((block_y * 4) - 1) * stride;
  493. for (int block_x = blocks_wide; block_x > 0; block_x--)
  494. {
  495. // check if this block should be skipped
  496. if (skip_blocks)
  497. {
  498. block_ptr += block_inc;
  499. skip_blocks--;
  500. total_blocks--;
  501. continue;
  502. }
  503. int pixel_ptr = block_ptr;
  504. // get the next two bytes in the encoded data stream
  505. CHECK_STREAM_PTR(2);
  506. uint8_t byte_a = lpChunk->data[stream_ptr++];
  507. uint8_t byte_b = lpChunk->data[stream_ptr++];
  508. // check if the decode is finished
  509. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
  510. {
  511. return;
  512. }
  513. else if ((byte_b & 0xFC) == 0x84)
  514. {
  515. // skip code, but don't count the current block
  516. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  517. }
  518. else if (byte_b < 0x80)
  519. {
  520. // 2- or 8-color encoding modes
  521. uint16_t flags = (byte_b << 8) | byte_a;
  522. uint16_t colors[8];
  523. CHECK_STREAM_PTR(4);
  524. colors[0] = AV_RL16(&lpChunk->data[stream_ptr]);
  525. stream_ptr += 2;
  526. colors[1] = AV_RL16(&lpChunk->data[stream_ptr]);
  527. stream_ptr += 2;
  528. if (colors[0] & 0x8000)
  529. {
  530. // 8-color encoding
  531. CHECK_STREAM_PTR(12);
  532. colors[2] = AV_RL16(&lpChunk->data[stream_ptr]);
  533. stream_ptr += 2;
  534. colors[3] = AV_RL16(&lpChunk->data[stream_ptr]);
  535. stream_ptr += 2;
  536. colors[4] = AV_RL16(&lpChunk->data[stream_ptr]);
  537. stream_ptr += 2;
  538. colors[5] = AV_RL16(&lpChunk->data[stream_ptr]);
  539. stream_ptr += 2;
  540. colors[6] = AV_RL16(&lpChunk->data[stream_ptr]);
  541. stream_ptr += 2;
  542. colors[7] = AV_RL16(&lpChunk->data[stream_ptr]);
  543. stream_ptr += 2;
  544. for (int pixel_y = 0; pixel_y < 4; pixel_y++)
  545. {
  546. for (int pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  547. {
  548. pixels[pixel_ptr++] =
  549. colors[((pixel_y & 0x2) << 1) +
  550. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  551. }
  552. pixel_ptr -= row_dec;
  553. }
  554. }
  555. else
  556. {
  557. // 2-color encoding
  558. for (int pixel_y = 0; pixel_y < 4; pixel_y++)
  559. {
  560. for (int pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  561. {
  562. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  563. }
  564. pixel_ptr -= row_dec;
  565. }
  566. }
  567. }
  568. else
  569. {
  570. // otherwise, it's a 1-color block
  571. uint16_t color = (byte_b << 8) | byte_a;
  572. for (int pixel_y = 0; pixel_y < 4; pixel_y++)
  573. {
  574. for (int pixel_x = 0; pixel_x < 4; pixel_x++)
  575. {
  576. pixels[pixel_ptr++] = color;
  577. }
  578. pixel_ptr -= row_dec;
  579. }
  580. }
  581. block_ptr += block_inc;
  582. total_blocks--;
  583. }
  584. }
  585. }
  586. BOOL
  587. PAL_PlayAVI(
  588. LPCSTR lpszPath
  589. )
  590. {
  591. if (!gConfig.fEnableAviPlay) return FALSE;
  592. //
  593. // Open the file
  594. //
  595. FILE *fp = UTIL_OpenFile(lpszPath);
  596. if (fp == NULL)
  597. {
  598. UTIL_LogOutput(LOGLEVEL_WARNING, "Cannot open AVI file: %s!\n", lpszPath);
  599. return FALSE;
  600. }
  601. AVIPlayState *avi = PAL_ReadAVIInfo(fp, &gAVIPlayState);
  602. if (avi == NULL)
  603. {
  604. UTIL_LogOutput(LOGLEVEL_WARNING, "Failed to parse AVI file or its format not supported!\n");
  605. fclose(fp);
  606. return FALSE;
  607. }
  608. PAL_ClearKeyState();
  609. VIDEO_ChangeDepth(avi->surface->format->BitsPerPixel);
  610. BOOL fEndPlay = FALSE;
  611. RIFFChunk *buf = (RIFFChunk *)avi->pChunkBuffer;
  612. uint32_t len = avi->dwBufferSize;
  613. uint32_t dwMicroSecChange = 0;
  614. uint32_t dwCurrentTime = SDL_GetTicks();
  615. uint32_t dwNextFrameTime;
  616. uint32_t dwFrameStartTime = dwCurrentTime;
  617. while (!fEndPlay)
  618. {
  619. RIFFChunk *chunk = PAL_ReadDataChunk(fp, avi->lVideoEndPos, buf, len, avi->cvt.len_mult);
  620. if (chunk == NULL) break;
  621. switch (chunk->header.type)
  622. {
  623. case AVI_00dc:
  624. case AVI_00db:
  625. //
  626. // Video frame
  627. //
  628. dwNextFrameTime = dwFrameStartTime + (avi->dwMicroSecPerFrame / 1000);
  629. dwMicroSecChange += avi->dwMicroSecPerFrame % 1000;
  630. dwNextFrameTime += dwMicroSecChange / 1000;
  631. dwMicroSecChange %= 1000;
  632. PAL_RenderAVIFrameToSurface(avi->surface, chunk);
  633. VIDEO_DrawSurfaceToScreen(avi->surface);
  634. dwCurrentTime = SDL_GetTicks();
  635. // Check input states here
  636. UTIL_Delay(dwCurrentTime >= dwNextFrameTime ? 1 : dwNextFrameTime - dwCurrentTime);
  637. dwFrameStartTime = SDL_GetTicks();
  638. if (g_InputState.dwKeyPress & (kKeyMenu | kKeySearch))
  639. {
  640. fEndPlay = TRUE;
  641. }
  642. break;
  643. case AVI_01wb:
  644. //
  645. // Audio data, just convert it & feed into buffer
  646. //
  647. PAL_AVIFeedAudio(avi, chunk->data, chunk->header.length);
  648. //
  649. // Only enable AVI audio when data are available
  650. // We do not lock on the 'if' because only this function changes 'avi->fp'
  651. //
  652. if (!avi->fp)
  653. {
  654. SDL_mutexP(avi->selfMutex);
  655. avi->fp = fp;
  656. SDL_mutexV(avi->selfMutex);
  657. }
  658. break;
  659. }
  660. if (chunk != buf) free(chunk);
  661. }
  662. SDL_mutexP(avi->selfMutex);
  663. avi->fp = NULL;
  664. SDL_mutexV(avi->selfMutex);
  665. if (fEndPlay)
  666. {
  667. //
  668. // Simulate a short delay (like the original game)
  669. //
  670. UTIL_Delay(500);
  671. }
  672. VIDEO_ChangeDepth(0);
  673. if (avi->surface != NULL)
  674. {
  675. SDL_FreeSurface(avi->surface);
  676. avi->surface = NULL;
  677. }
  678. if (avi->pChunkBuffer)
  679. {
  680. free(avi->pChunkBuffer);
  681. avi->pChunkBuffer = NULL;
  682. }
  683. if (avi->pbAudioBuf)
  684. {
  685. free(avi->pbAudioBuf);
  686. avi->pbAudioBuf = NULL;
  687. }
  688. fclose(fp);
  689. return TRUE;
  690. }
  691. VOID SDLCALL
  692. AVI_FillAudioBuffer(
  693. void *udata,
  694. uint8_t *stream,
  695. int len
  696. )
  697. {
  698. AVIPlayState *avi = (AVIPlayState *)udata;
  699. SDL_mutexP(avi->selfMutex);
  700. while (avi->fp != NULL && len > 0 && avi->dwAudioWritePos != avi->dwAudioReadPos)
  701. {
  702. uint32_t fill_size = (avi->dwAudioReadPos + len > avi->dwAudBufLen) ? avi->dwAudBufLen - avi->dwAudioReadPos : len;
  703. if (avi->dwAudioWritePos > avi->dwAudioReadPos &&
  704. fill_size > avi->dwAudioWritePos - avi->dwAudioReadPos)
  705. {
  706. fill_size = avi->dwAudioWritePos - avi->dwAudioReadPos;
  707. }
  708. memcpy(stream, avi->pbAudioBuf + avi->dwAudioReadPos, fill_size);
  709. avi->dwAudioReadPos = (avi->dwAudioReadPos + fill_size) % avi->dwAudBufLen;
  710. stream += fill_size;
  711. len -= fill_size;
  712. }
  713. SDL_mutexV(avi->selfMutex);
  714. }
  715. void *
  716. AVI_GetPlayState(
  717. void
  718. )
  719. {
  720. return &gAVIPlayState;
  721. }