PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/video/smk_decoder.cpp

http://github.com/scummvm/scummvm
C++ | 875 lines | 621 code | 166 blank | 88 comment | 120 complexity | c14ddb50128b61d35deb606064f5198f MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  1. /* ScummVM - Graphic Adventure Engine
  2. *
  3. * ScummVM is the legal property of its developers, whose names
  4. * are too numerous to list here. Please refer to the COPYRIGHT
  5. * file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. */
  22. // Based on http://wiki.multimedia.cx/index.php?title=Smacker
  23. // and the FFmpeg Smacker decoder (libavcodec/smacker.c), revision 16143
  24. // http://git.ffmpeg.org/?p=ffmpeg;a=blob;f=libavcodec/smacker.c;hb=b8437a00a2f14d4a437346455d624241d726128e
  25. #include "video/smk_decoder.h"
  26. #include "common/endian.h"
  27. #include "common/util.h"
  28. #include "common/stream.h"
  29. #include "common/bitstream.h"
  30. #include "common/system.h"
  31. #include "common/textconsole.h"
  32. #include "audio/audiostream.h"
  33. #include "audio/mixer.h"
  34. #include "audio/decoders/raw.h"
  35. namespace Video {
  36. enum SmkBlockTypes {
  37. SMK_BLOCK_MONO = 0,
  38. SMK_BLOCK_FULL = 1,
  39. SMK_BLOCK_SKIP = 2,
  40. SMK_BLOCK_FILL = 3
  41. };
  42. /*
  43. * class SmallHuffmanTree
  44. * A Huffman-tree to hold 8-bit values.
  45. */
  46. class SmallHuffmanTree {
  47. public:
  48. SmallHuffmanTree(Common::BitStreamMemory8LSB &bs);
  49. uint16 getCode(Common::BitStreamMemory8LSB &bs);
  50. private:
  51. enum {
  52. SMK_NODE = 0x8000
  53. };
  54. uint16 decodeTree(uint32 prefix, int length);
  55. uint16 _treeSize;
  56. uint16 _tree[511];
  57. uint16 _prefixtree[256];
  58. byte _prefixlength[256];
  59. Common::BitStreamMemory8LSB &_bs;
  60. };
  61. SmallHuffmanTree::SmallHuffmanTree(Common::BitStreamMemory8LSB &bs)
  62. : _treeSize(0), _bs(bs) {
  63. uint32 bit = _bs.getBit();
  64. assert(bit);
  65. for (uint16 i = 0; i < 256; ++i)
  66. _prefixtree[i] = _prefixlength[i] = 0;
  67. decodeTree(0, 0);
  68. bit = _bs.getBit();
  69. assert(!bit);
  70. }
  71. uint16 SmallHuffmanTree::decodeTree(uint32 prefix, int length) {
  72. if (!_bs.getBit()) { // Leaf
  73. _tree[_treeSize] = _bs.getBits(8);
  74. if (length <= 8) {
  75. for (int i = 0; i < 256; i += (1 << length)) {
  76. _prefixtree[prefix | i] = _treeSize;
  77. _prefixlength[prefix | i] = length;
  78. }
  79. }
  80. ++_treeSize;
  81. return 1;
  82. }
  83. uint16 t = _treeSize++;
  84. if (length == 8) {
  85. _prefixtree[prefix] = t;
  86. _prefixlength[prefix] = 8;
  87. }
  88. uint16 r1 = decodeTree(prefix, length + 1);
  89. _tree[t] = (SMK_NODE | r1);
  90. uint16 r2 = decodeTree(prefix | (1 << length), length + 1);
  91. return r1+r2+1;
  92. }
  93. uint16 SmallHuffmanTree::getCode(Common::BitStreamMemory8LSB &bs) {
  94. byte peek = bs.peekBits(MIN<uint32>(bs.size() - bs.pos(), 8));
  95. uint16 *p = &_tree[_prefixtree[peek]];
  96. bs.skip(_prefixlength[peek]);
  97. while (*p & SMK_NODE) {
  98. if (bs.getBit())
  99. p += *p & ~SMK_NODE;
  100. p++;
  101. }
  102. return *p;
  103. }
  104. /*
  105. * class BigHuffmanTree
  106. * A Huffman-tree to hold 16-bit values.
  107. */
  108. class BigHuffmanTree {
  109. public:
  110. BigHuffmanTree(Common::BitStreamMemory8LSB &bs, int allocSize);
  111. ~BigHuffmanTree();
  112. void reset();
  113. uint32 getCode(Common::BitStreamMemory8LSB &bs);
  114. private:
  115. enum {
  116. SMK_NODE = 0x80000000
  117. };
  118. uint32 decodeTree(uint32 prefix, int length);
  119. uint32 _treeSize;
  120. uint32 *_tree;
  121. uint32 _last[3];
  122. uint32 _prefixtree[256];
  123. byte _prefixlength[256];
  124. /* Used during construction */
  125. Common::BitStreamMemory8LSB &_bs;
  126. uint32 _markers[3];
  127. SmallHuffmanTree *_loBytes;
  128. SmallHuffmanTree *_hiBytes;
  129. };
  130. BigHuffmanTree::BigHuffmanTree(Common::BitStreamMemory8LSB &bs, int allocSize)
  131. : _bs(bs) {
  132. uint32 bit = _bs.getBit();
  133. if (!bit) {
  134. _tree = new uint32[1];
  135. _tree[0] = 0;
  136. _last[0] = _last[1] = _last[2] = 0;
  137. return;
  138. }
  139. for (uint32 i = 0; i < 256; ++i)
  140. _prefixtree[i] = _prefixlength[i] = 0;
  141. _loBytes = new SmallHuffmanTree(_bs);
  142. _hiBytes = new SmallHuffmanTree(_bs);
  143. _markers[0] = _bs.getBits(16);
  144. _markers[1] = _bs.getBits(16);
  145. _markers[2] = _bs.getBits(16);
  146. _last[0] = _last[1] = _last[2] = 0xffffffff;
  147. _treeSize = 0;
  148. _tree = new uint32[allocSize / 4];
  149. decodeTree(0, 0);
  150. bit = _bs.getBit();
  151. assert(!bit);
  152. for (uint32 i = 0; i < 3; ++i) {
  153. if (_last[i] == 0xffffffff) {
  154. _last[i] = _treeSize;
  155. _tree[_treeSize++] = 0;
  156. }
  157. }
  158. delete _loBytes;
  159. delete _hiBytes;
  160. }
  161. BigHuffmanTree::~BigHuffmanTree() {
  162. delete[] _tree;
  163. }
  164. void BigHuffmanTree::reset() {
  165. _tree[_last[0]] = _tree[_last[1]] = _tree[_last[2]] = 0;
  166. }
  167. uint32 BigHuffmanTree::decodeTree(uint32 prefix, int length) {
  168. uint32 bit = _bs.getBit();
  169. if (!bit) { // Leaf
  170. uint32 lo = _loBytes->getCode(_bs);
  171. uint32 hi = _hiBytes->getCode(_bs);
  172. uint32 v = (hi << 8) | lo;
  173. _tree[_treeSize] = v;
  174. if (length <= 8) {
  175. for (int i = 0; i < 256; i += (1 << length)) {
  176. _prefixtree[prefix | i] = _treeSize;
  177. _prefixlength[prefix | i] = length;
  178. }
  179. }
  180. for (int i = 0; i < 3; ++i) {
  181. if (_markers[i] == v) {
  182. _last[i] = _treeSize;
  183. _tree[_treeSize] = 0;
  184. }
  185. }
  186. ++_treeSize;
  187. return 1;
  188. }
  189. uint32 t = _treeSize++;
  190. if (length == 8) {
  191. _prefixtree[prefix] = t;
  192. _prefixlength[prefix] = 8;
  193. }
  194. uint32 r1 = decodeTree(prefix, length + 1);
  195. _tree[t] = SMK_NODE | r1;
  196. uint32 r2 = decodeTree(prefix | (1 << length), length + 1);
  197. return r1+r2+1;
  198. }
  199. uint32 BigHuffmanTree::getCode(Common::BitStreamMemory8LSB &bs) {
  200. byte peek = bs.peekBits(MIN<uint32>(bs.size() - bs.pos(), 8));
  201. uint32 *p = &_tree[_prefixtree[peek]];
  202. bs.skip(_prefixlength[peek]);
  203. while (*p & SMK_NODE) {
  204. if (bs.getBit())
  205. p += (*p) & ~SMK_NODE;
  206. p++;
  207. }
  208. uint32 v = *p;
  209. if (v != _tree[_last[0]]) {
  210. _tree[_last[2]] = _tree[_last[1]];
  211. _tree[_last[1]] = _tree[_last[0]];
  212. _tree[_last[0]] = v;
  213. }
  214. return v;
  215. }
  216. SmackerDecoder::SmackerDecoder() {
  217. _fileStream = 0;
  218. _firstFrameStart = 0;
  219. _frameTypes = 0;
  220. _frameSizes = 0;
  221. }
  222. SmackerDecoder::~SmackerDecoder() {
  223. close();
  224. }
  225. bool SmackerDecoder::loadStream(Common::SeekableReadStream *stream) {
  226. close();
  227. _fileStream = stream;
  228. // Read in the Smacker header
  229. _header.signature = _fileStream->readUint32BE();
  230. if (_header.signature != MKTAG('S', 'M', 'K', '2') && _header.signature != MKTAG('S', 'M', 'K', '4'))
  231. return false;
  232. uint32 width = _fileStream->readUint32LE();
  233. uint32 height = _fileStream->readUint32LE();
  234. uint32 frameCount = _fileStream->readUint32LE();
  235. int32 frameDelay = _fileStream->readSint32LE();
  236. // frame rate contains 2 digits after the comma, so 1497 is actually 14.97 fps
  237. Common::Rational frameRate;
  238. if (frameDelay > 0)
  239. frameRate = Common::Rational(1000, frameDelay);
  240. else if (frameDelay < 0)
  241. frameRate = Common::Rational(100000, -frameDelay);
  242. else
  243. frameRate = 1000;
  244. // Flags are determined by which bit is set, which can be one of the following:
  245. // 0 - set to 1 if file contains a ring frame.
  246. // 1 - set to 1 if file is Y-interlaced
  247. // 2 - set to 1 if file is Y-doubled
  248. // If bits 1 or 2 are set, the frame should be scaled to twice its height
  249. // before it is displayed.
  250. _header.flags = _fileStream->readUint32LE();
  251. SmackerVideoTrack *videoTrack = createVideoTrack(width, height, frameCount, frameRate, _header.flags, _header.signature);
  252. addTrack(videoTrack);
  253. // TODO: should we do any extra processing for Smacker files with ring frames?
  254. // TODO: should we do any extra processing for Y-doubled videos? Are they the
  255. // same as Y-interlaced videos?
  256. uint32 i;
  257. for (i = 0; i < 7; ++i)
  258. _header.audioSize[i] = _fileStream->readUint32LE();
  259. _header.treesSize = _fileStream->readUint32LE();
  260. _header.mMapSize = _fileStream->readUint32LE();
  261. _header.mClrSize = _fileStream->readUint32LE();
  262. _header.fullSize = _fileStream->readUint32LE();
  263. _header.typeSize = _fileStream->readUint32LE();
  264. for (i = 0; i < 7; ++i) {
  265. // AudioRate - Frequency and format information for each sound track, up to 7 audio tracks.
  266. // The 32 constituent bits have the following meaning:
  267. // * bit 31 - indicates Huffman + DPCM compression
  268. // * bit 30 - indicates that audio data is present for this track
  269. // * bit 29 - 1 = 16-bit audio; 0 = 8-bit audio
  270. // * bit 28 - 1 = stereo audio; 0 = mono audio
  271. // * bit 27 - indicates Bink RDFT compression
  272. // * bit 26 - indicates Bink DCT compression
  273. // * bits 25-24 - unused
  274. // * bits 23-0 - audio sample rate
  275. uint32 audioInfo = _fileStream->readUint32LE();
  276. _header.audioInfo[i].hasAudio = audioInfo & 0x40000000;
  277. _header.audioInfo[i].is16Bits = audioInfo & 0x20000000;
  278. _header.audioInfo[i].isStereo = audioInfo & 0x10000000;
  279. _header.audioInfo[i].sampleRate = audioInfo & 0xFFFFFF;
  280. if (audioInfo & 0x8000000)
  281. _header.audioInfo[i].compression = kCompressionRDFT;
  282. else if (audioInfo & 0x4000000)
  283. _header.audioInfo[i].compression = kCompressionDCT;
  284. else if (audioInfo & 0x80000000)
  285. _header.audioInfo[i].compression = kCompressionDPCM;
  286. else
  287. _header.audioInfo[i].compression = kCompressionNone;
  288. if (_header.audioInfo[i].hasAudio) {
  289. if (_header.audioInfo[i].compression == kCompressionRDFT || _header.audioInfo[i].compression == kCompressionDCT)
  290. warning("Unhandled Smacker v2 audio compression");
  291. addTrack(new SmackerAudioTrack(_header.audioInfo[i], getSoundType()));
  292. }
  293. }
  294. _header.dummy = _fileStream->readUint32LE();
  295. _frameSizes = new uint32[frameCount];
  296. for (i = 0; i < frameCount; ++i)
  297. _frameSizes[i] = _fileStream->readUint32LE();
  298. _frameTypes = new byte[frameCount];
  299. for (i = 0; i < frameCount; ++i)
  300. _frameTypes[i] = _fileStream->readByte();
  301. byte *huffmanTrees = (byte *) malloc(_header.treesSize);
  302. _fileStream->read(huffmanTrees, _header.treesSize);
  303. Common::BitStreamMemory8LSB bs(new Common::BitStreamMemoryStream(huffmanTrees, _header.treesSize, DisposeAfterUse::YES), DisposeAfterUse::YES);
  304. videoTrack->readTrees(bs, _header.mMapSize, _header.mClrSize, _header.fullSize, _header.typeSize);
  305. _firstFrameStart = _fileStream->pos();
  306. return true;
  307. }
  308. void SmackerDecoder::close() {
  309. VideoDecoder::close();
  310. delete _fileStream;
  311. _fileStream = 0;
  312. delete[] _frameTypes;
  313. _frameTypes = 0;
  314. delete[] _frameSizes;
  315. _frameSizes = 0;
  316. }
  317. bool SmackerDecoder::rewind() {
  318. // Call the parent method to rewind the tracks first
  319. if (!VideoDecoder::rewind())
  320. return false;
  321. // And seek back to where the first frame begins
  322. _fileStream->seek(_firstFrameStart);
  323. return true;
  324. }
  325. void SmackerDecoder::readNextPacket() {
  326. SmackerVideoTrack *videoTrack = (SmackerVideoTrack *)getTrack(0);
  327. if (videoTrack->endOfTrack())
  328. return;
  329. videoTrack->increaseCurFrame();
  330. uint i;
  331. uint32 chunkSize = 0;
  332. uint32 dataSizeUnpacked = 0;
  333. uint32 startPos = _fileStream->pos();
  334. // Check if we got a frame with palette data, and
  335. // call back the virtual setPalette function to set
  336. // the current palette
  337. if (_frameTypes[videoTrack->getCurFrame()] & 1)
  338. videoTrack->unpackPalette(_fileStream);
  339. // Load audio tracks
  340. for (i = 0; i < 7; ++i) {
  341. if (!(_frameTypes[videoTrack->getCurFrame()] & (2 << i)))
  342. continue;
  343. chunkSize = _fileStream->readUint32LE();
  344. chunkSize -= 4; // subtract the first 4 bytes (chunk size)
  345. if (_header.audioInfo[i].compression == kCompressionNone) {
  346. dataSizeUnpacked = chunkSize;
  347. } else {
  348. dataSizeUnpacked = _fileStream->readUint32LE();
  349. chunkSize -= 4; // subtract the next 4 bytes (unpacked data size)
  350. }
  351. handleAudioTrack(i, chunkSize, dataSizeUnpacked);
  352. }
  353. uint32 frameSize = _frameSizes[videoTrack->getCurFrame()] & ~3;
  354. // uint32 remainder = _frameSizes[videoTrack->getCurFrame()] & 3;
  355. if (_fileStream->pos() - startPos > frameSize)
  356. error("Smacker actual frame size exceeds recorded frame size");
  357. uint32 frameDataSize = frameSize - (_fileStream->pos() - startPos);
  358. byte *frameData = (byte *)malloc(frameDataSize + 1);
  359. // Padding to keep the BigHuffmanTrees from reading past the data end
  360. frameData[frameDataSize] = 0x00;
  361. _fileStream->read(frameData, frameDataSize);
  362. Common::BitStreamMemory8LSB bs(new Common::BitStreamMemoryStream(frameData, frameDataSize + 1, DisposeAfterUse::YES), DisposeAfterUse::YES);
  363. videoTrack->decodeFrame(bs);
  364. _fileStream->seek(startPos + frameSize);
  365. }
  366. void SmackerDecoder::handleAudioTrack(byte track, uint32 chunkSize, uint32 unpackedSize) {
  367. if (chunkSize == 0)
  368. return;
  369. if (_header.audioInfo[track].hasAudio) {
  370. // Get the audio track, which start at offset 1 (first track is video)
  371. SmackerAudioTrack *audioTrack = (SmackerAudioTrack *)getTrack(track + 1);
  372. // If it's track 0, play the audio data
  373. byte *soundBuffer = (byte *)malloc(chunkSize + 1);
  374. // Padding to keep the SmallHuffmanTrees from reading past the data end
  375. soundBuffer[chunkSize] = 0x00;
  376. _fileStream->read(soundBuffer, chunkSize);
  377. if (_header.audioInfo[track].compression == kCompressionRDFT || _header.audioInfo[track].compression == kCompressionDCT) {
  378. // TODO: Compressed audio (Bink RDFT/DCT encoded)
  379. free(soundBuffer);
  380. return;
  381. } else if (_header.audioInfo[track].compression == kCompressionDPCM) {
  382. // Compressed audio (Huffman DPCM encoded)
  383. audioTrack->queueCompressedBuffer(soundBuffer, chunkSize + 1, unpackedSize);
  384. free(soundBuffer);
  385. } else {
  386. // Uncompressed audio (PCM)
  387. audioTrack->queuePCM(soundBuffer, chunkSize);
  388. }
  389. } else {
  390. // Ignore possibly unused data
  391. _fileStream->skip(chunkSize);
  392. }
  393. }
  394. VideoDecoder::AudioTrack *SmackerDecoder::getAudioTrack(int index) {
  395. // Smacker audio track indexes are relative to the first audio track
  396. Track *track = getTrack(index + 1);
  397. if (!track || track->getTrackType() != Track::kTrackTypeAudio)
  398. return 0;
  399. return (AudioTrack *)track;
  400. }
  401. SmackerDecoder::SmackerVideoTrack::SmackerVideoTrack(uint32 width, uint32 height, uint32 frameCount, const Common::Rational &frameRate, uint32 flags, uint32 signature) {
  402. _surface = new Graphics::Surface();
  403. _surface->create(width, height * (flags ? 2 : 1), Graphics::PixelFormat::createFormatCLUT8());
  404. _frameCount = frameCount;
  405. _frameRate = frameRate;
  406. _flags = flags;
  407. _signature = signature;
  408. _curFrame = -1;
  409. _dirtyPalette = false;
  410. _MMapTree = _MClrTree = _FullTree = _TypeTree = 0;
  411. memset(_palette, 0, 3 * 256);
  412. }
  413. SmackerDecoder::SmackerVideoTrack::~SmackerVideoTrack() {
  414. _surface->free();
  415. delete _surface;
  416. delete _MMapTree;
  417. delete _MClrTree;
  418. delete _FullTree;
  419. delete _TypeTree;
  420. }
  421. uint16 SmackerDecoder::SmackerVideoTrack::getWidth() const {
  422. return _surface->w;
  423. }
  424. uint16 SmackerDecoder::SmackerVideoTrack::getHeight() const {
  425. return _surface->h;
  426. }
  427. Graphics::PixelFormat SmackerDecoder::SmackerVideoTrack::getPixelFormat() const {
  428. return _surface->format;
  429. }
  430. void SmackerDecoder::SmackerVideoTrack::readTrees(Common::BitStreamMemory8LSB &bs, uint32 mMapSize, uint32 mClrSize, uint32 fullSize, uint32 typeSize) {
  431. _MMapTree = new BigHuffmanTree(bs, mMapSize);
  432. _MClrTree = new BigHuffmanTree(bs, mClrSize);
  433. _FullTree = new BigHuffmanTree(bs, fullSize);
  434. _TypeTree = new BigHuffmanTree(bs, typeSize);
  435. }
  436. void SmackerDecoder::SmackerVideoTrack::decodeFrame(Common::BitStreamMemory8LSB &bs) {
  437. _MMapTree->reset();
  438. _MClrTree->reset();
  439. _FullTree->reset();
  440. _TypeTree->reset();
  441. // Height needs to be doubled if we have flags (Y-interlaced or Y-doubled)
  442. uint doubleY = _flags ? 2 : 1;
  443. uint bw = getWidth() / 4;
  444. uint bh = getHeight() / doubleY / 4;
  445. uint stride = getWidth();
  446. uint block = 0, blocks = bw*bh;
  447. byte *out;
  448. uint type, run, j, mode;
  449. uint32 p1, p2, clr, map;
  450. byte hi, lo;
  451. uint i;
  452. while (block < blocks) {
  453. type = _TypeTree->getCode(bs);
  454. run = getBlockRun((type >> 2) & 0x3f);
  455. switch (type & 3) {
  456. case SMK_BLOCK_MONO:
  457. while (run-- && block < blocks) {
  458. clr = _MClrTree->getCode(bs);
  459. map = _MMapTree->getCode(bs);
  460. out = (byte *)_surface->getPixels() + (block / bw) * (stride * 4 * doubleY) + (block % bw) * 4;
  461. hi = clr >> 8;
  462. lo = clr & 0xff;
  463. for (i = 0; i < 4; i++) {
  464. for (j = 0; j < doubleY; j++) {
  465. out[0] = (map & 1) ? hi : lo;
  466. out[1] = (map & 2) ? hi : lo;
  467. out[2] = (map & 4) ? hi : lo;
  468. out[3] = (map & 8) ? hi : lo;
  469. out += stride;
  470. }
  471. map >>= 4;
  472. }
  473. ++block;
  474. }
  475. break;
  476. case SMK_BLOCK_FULL:
  477. // Smacker v2 has one mode, Smacker v4 has three
  478. if (_signature == MKTAG('S','M','K','2')) {
  479. mode = 0;
  480. } else {
  481. // 00 - mode 0
  482. // 10 - mode 1
  483. // 01 - mode 2
  484. mode = 0;
  485. if (bs.getBit()) {
  486. mode = 1;
  487. } else if (bs.getBit()) {
  488. mode = 2;
  489. }
  490. }
  491. while (run-- && block < blocks) {
  492. out = (byte *)_surface->getPixels() + (block / bw) * (stride * 4 * doubleY) + (block % bw) * 4;
  493. switch (mode) {
  494. case 0:
  495. for (i = 0; i < 4; ++i) {
  496. p1 = _FullTree->getCode(bs);
  497. p2 = _FullTree->getCode(bs);
  498. for (j = 0; j < doubleY; ++j) {
  499. out[2] = p1 & 0xff;
  500. out[3] = p1 >> 8;
  501. out[0] = p2 & 0xff;
  502. out[1] = p2 >> 8;
  503. out += stride;
  504. }
  505. }
  506. break;
  507. case 1:
  508. p1 = _FullTree->getCode(bs);
  509. out[0] = out[1] = p1 & 0xFF;
  510. out[2] = out[3] = p1 >> 8;
  511. out += stride;
  512. out[0] = out[1] = p1 & 0xFF;
  513. out[2] = out[3] = p1 >> 8;
  514. out += stride;
  515. p2 = _FullTree->getCode(bs);
  516. out[0] = out[1] = p2 & 0xFF;
  517. out[2] = out[3] = p2 >> 8;
  518. out += stride;
  519. out[0] = out[1] = p2 & 0xFF;
  520. out[2] = out[3] = p2 >> 8;
  521. out += stride;
  522. break;
  523. case 2:
  524. for (i = 0; i < 2; i++) {
  525. // We first get p2 and then p1
  526. // Check ffmpeg thread "[PATCH] Smacker video decoder bug fix"
  527. // http://article.gmane.org/gmane.comp.video.ffmpeg.devel/78768
  528. p2 = _FullTree->getCode(bs);
  529. p1 = _FullTree->getCode(bs);
  530. for (j = 0; j < doubleY; ++j) {
  531. out[0] = p1 & 0xff;
  532. out[1] = p1 >> 8;
  533. out[2] = p2 & 0xff;
  534. out[3] = p2 >> 8;
  535. out += stride;
  536. }
  537. for (j = 0; j < doubleY; ++j) {
  538. out[0] = p1 & 0xff;
  539. out[1] = p1 >> 8;
  540. out[2] = p2 & 0xff;
  541. out[3] = p2 >> 8;
  542. out += stride;
  543. }
  544. }
  545. break;
  546. default:
  547. break;
  548. }
  549. ++block;
  550. }
  551. break;
  552. case SMK_BLOCK_SKIP:
  553. while (run-- && block < blocks)
  554. block++;
  555. break;
  556. case SMK_BLOCK_FILL:
  557. uint32 col;
  558. mode = type >> 8;
  559. while (run-- && block < blocks) {
  560. out = (byte *)_surface->getPixels() + (block / bw) * (stride * 4 * doubleY) + (block % bw) * 4;
  561. col = mode * 0x01010101;
  562. for (i = 0; i < 4 * doubleY; ++i) {
  563. out[0] = out[1] = out[2] = out[3] = col;
  564. out += stride;
  565. }
  566. ++block;
  567. }
  568. break;
  569. default:
  570. break;
  571. }
  572. }
  573. }
  574. void SmackerDecoder::SmackerVideoTrack::unpackPalette(Common::SeekableReadStream *stream) {
  575. uint startPos = stream->pos();
  576. uint32 len = 4 * stream->readByte();
  577. byte *chunk = (byte *)malloc(len);
  578. stream->read(chunk, len);
  579. byte *p = chunk;
  580. byte oldPalette[3 * 256];
  581. memcpy(oldPalette, _palette, 3 * 256);
  582. byte *pal = _palette;
  583. int sz = 0;
  584. byte b0;
  585. while (sz < 256) {
  586. b0 = *p++;
  587. if (b0 & 0x80) { // if top bit is 1 (0x80 = 10000000)
  588. sz += (b0 & 0x7f) + 1; // get lower 7 bits + 1 (0x7f = 01111111)
  589. pal += 3 * ((b0 & 0x7f) + 1);
  590. } else if (b0 & 0x40) { // if top 2 bits are 01 (0x40 = 01000000)
  591. byte c = (b0 & 0x3f) + 1; // get lower 6 bits + 1 (0x3f = 00111111)
  592. uint s = 3 * *p++;
  593. sz += c;
  594. while (c--) {
  595. *pal++ = oldPalette[s + 0];
  596. *pal++ = oldPalette[s + 1];
  597. *pal++ = oldPalette[s + 2];
  598. s += 3;
  599. }
  600. } else { // top 2 bits are 00
  601. sz++;
  602. // get the lower 6 bits for each component (0x3f = 00111111)
  603. byte r = b0 & 0x3f;
  604. byte g = (*p++) & 0x3f;
  605. byte b = (*p++) & 0x3f;
  606. // upscale to full 8-bit color values. The Multimedia Wiki suggests
  607. // a lookup table for this, but this should produce the same result.
  608. *pal++ = (r * 4 + r / 16);
  609. *pal++ = (g * 4 + g / 16);
  610. *pal++ = (b * 4 + b / 16);
  611. }
  612. }
  613. stream->seek(startPos + len);
  614. free(chunk);
  615. _dirtyPalette = true;
  616. }
  617. SmackerDecoder::SmackerAudioTrack::SmackerAudioTrack(const AudioInfo &audioInfo, Audio::Mixer::SoundType soundType) :
  618. AudioTrack(soundType),
  619. _audioInfo(audioInfo) {
  620. _audioStream = Audio::makeQueuingAudioStream(_audioInfo.sampleRate, _audioInfo.isStereo);
  621. }
  622. SmackerDecoder::SmackerAudioTrack::~SmackerAudioTrack() {
  623. delete _audioStream;
  624. }
  625. bool SmackerDecoder::SmackerAudioTrack::rewind() {
  626. delete _audioStream;
  627. _audioStream = Audio::makeQueuingAudioStream(_audioInfo.sampleRate, _audioInfo.isStereo);
  628. return true;
  629. }
  630. Audio::AudioStream *SmackerDecoder::SmackerAudioTrack::getAudioStream() const {
  631. return _audioStream;
  632. }
  633. void SmackerDecoder::SmackerAudioTrack::queueCompressedBuffer(byte *buffer, uint32 bufferSize, uint32 unpackedSize) {
  634. Common::BitStreamMemory8LSB audioBS(new Common::BitStreamMemoryStream(buffer, bufferSize), DisposeAfterUse::YES);
  635. bool dataPresent = audioBS.getBit();
  636. if (!dataPresent)
  637. return;
  638. bool isStereo = audioBS.getBit();
  639. assert(isStereo == _audioInfo.isStereo);
  640. bool is16Bits = audioBS.getBit();
  641. assert(is16Bits == _audioInfo.is16Bits);
  642. int numBytes = 1 * (isStereo ? 2 : 1) * (is16Bits ? 2 : 1);
  643. byte *unpackedBuffer = (byte *)malloc(unpackedSize);
  644. byte *curPointer = unpackedBuffer;
  645. uint32 curPos = 0;
  646. SmallHuffmanTree *audioTrees[4];
  647. for (int k = 0; k < numBytes; k++)
  648. audioTrees[k] = new SmallHuffmanTree(audioBS);
  649. // Base values, stored as big endian
  650. int32 bases[2];
  651. if (isStereo) {
  652. if (is16Bits) {
  653. bases[1] = SWAP_BYTES_16(audioBS.getBits(16));
  654. } else {
  655. bases[1] = audioBS.getBits(8);
  656. }
  657. }
  658. if (is16Bits) {
  659. bases[0] = SWAP_BYTES_16(audioBS.getBits(16));
  660. } else {
  661. bases[0] = audioBS.getBits(8);
  662. }
  663. // The bases are the first samples, too
  664. for (int i = 0; i < (isStereo ? 2 : 1); i++, curPointer += (is16Bits ? 2 : 1), curPos += (is16Bits ? 2 : 1)) {
  665. if (is16Bits)
  666. WRITE_BE_UINT16(curPointer, bases[i]);
  667. else
  668. *curPointer = (bases[i] & 0xFF) ^ 0x80;
  669. }
  670. // Next follow the deltas, which are added to the corresponding base values and
  671. // are stored as little endian
  672. // We store the unpacked bytes in big endian format
  673. while (curPos < unpackedSize) {
  674. // If the sample is stereo, the data is stored for the left and right channel, respectively
  675. // (the exact opposite to the base values)
  676. if (!is16Bits) {
  677. for (int k = 0; k < (isStereo ? 2 : 1); k++) {
  678. int8 delta = (int8) ((int16) audioTrees[k]->getCode(audioBS));
  679. bases[k] = (bases[k] + delta) & 0xFF;
  680. *curPointer++ = bases[k] ^ 0x80;
  681. curPos++;
  682. }
  683. } else {
  684. for (int k = 0; k < (isStereo ? 2 : 1); k++) {
  685. byte lo = audioTrees[k * 2]->getCode(audioBS);
  686. byte hi = audioTrees[k * 2 + 1]->getCode(audioBS);
  687. bases[k] += (int16) (lo | (hi << 8));
  688. WRITE_BE_UINT16(curPointer, bases[k]);
  689. curPointer += 2;
  690. curPos += 2;
  691. }
  692. }
  693. }
  694. for (int k = 0; k < numBytes; k++)
  695. delete audioTrees[k];
  696. queuePCM(unpackedBuffer, unpackedSize);
  697. }
  698. void SmackerDecoder::SmackerAudioTrack::queuePCM(byte *buffer, uint32 bufferSize) {
  699. byte flags = 0;
  700. if (_audioInfo.is16Bits)
  701. flags |= Audio::FLAG_16BITS;
  702. if (_audioInfo.isStereo)
  703. flags |= Audio::FLAG_STEREO;
  704. _audioStream->queueBuffer(buffer, bufferSize, DisposeAfterUse::YES, flags);
  705. }
  706. SmackerDecoder::SmackerVideoTrack *SmackerDecoder::createVideoTrack(uint32 width, uint32 height, uint32 frameCount, const Common::Rational &frameRate, uint32 flags, uint32 signature) const {
  707. return new SmackerVideoTrack(width, height, frameCount, frameRate, flags, signature);
  708. }
  709. } // End of namespace Video