2008-12-14 22:10:48 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Based on http://wiki.multimedia.cx/index.php?title=Smacker
|
2008-12-16 08:37:24 +00:00
|
|
|
// and the FFmpeg Smacker decoder (libavcodec/smacker.c), revision 16143
|
2010-05-23 19:08:31 +00:00
|
|
|
// http://git.ffmpeg.org/?p=ffmpeg;a=blob;f=libavcodec/smacker.c;hb=b8437a00a2f14d4a437346455d624241d726128e
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2011-01-23 17:37:17 +00:00
|
|
|
#include "video/smk_decoder.h"
|
2009-01-07 23:36:41 +00:00
|
|
|
|
2008-12-15 12:55:57 +00:00
|
|
|
#include "common/archive.h"
|
2008-12-14 22:10:48 +00:00
|
|
|
#include "common/endian.h"
|
2009-01-07 23:36:41 +00:00
|
|
|
#include "common/util.h"
|
|
|
|
#include "common/stream.h"
|
|
|
|
#include "common/system.h"
|
|
|
|
|
2008-12-17 14:48:57 +00:00
|
|
|
#include "sound/audiostream.h"
|
2010-01-19 22:30:33 +00:00
|
|
|
#include "sound/mixer.h"
|
2010-01-26 22:48:45 +00:00
|
|
|
#include "sound/decoders/raw.h"
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2011-01-23 19:08:09 +00:00
|
|
|
namespace Video {
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
enum SmkBlockTypes {
|
2008-12-22 11:22:15 +00:00
|
|
|
SMK_BLOCK_MONO = 0,
|
|
|
|
SMK_BLOCK_FULL = 1,
|
|
|
|
SMK_BLOCK_SKIP = 2,
|
|
|
|
SMK_BLOCK_FILL = 3
|
2008-12-14 22:10:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* class BitStream
|
2009-06-07 22:15:28 +00:00
|
|
|
* Little-endian bit stream provider.
|
2008-12-14 22:10:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
class BitStream {
|
|
|
|
public:
|
|
|
|
BitStream(byte *buf, uint32 length)
|
2009-06-07 22:15:28 +00:00
|
|
|
: _buf(buf), _end(buf+length), _bitCount(8) {
|
|
|
|
_curByte = *_buf++;
|
2008-12-14 22:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool getBit();
|
|
|
|
byte getBits8();
|
|
|
|
|
|
|
|
byte peek8() const;
|
|
|
|
void skip(int n);
|
|
|
|
|
|
|
|
private:
|
|
|
|
byte *_buf;
|
|
|
|
byte *_end;
|
2009-06-07 22:15:28 +00:00
|
|
|
byte _curByte;
|
|
|
|
byte _bitCount;
|
2008-12-14 22:10:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool BitStream::getBit() {
|
2009-06-07 22:17:44 +00:00
|
|
|
if (_bitCount == 0) {
|
2009-06-07 22:15:28 +00:00
|
|
|
assert(_buf < _end);
|
|
|
|
_curByte = *_buf++;
|
|
|
|
_bitCount = 8;
|
|
|
|
}
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
bool v = _curByte & 1;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
_curByte >>= 1;
|
|
|
|
--_bitCount;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte BitStream::getBits8() {
|
2009-06-07 22:15:28 +00:00
|
|
|
assert(_buf < _end);
|
|
|
|
|
|
|
|
byte v = (*_buf << _bitCount) | _curByte;
|
|
|
|
_curByte = *_buf++ >> (8 - _bitCount);
|
|
|
|
|
2008-12-14 22:10:48 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte BitStream::peek8() const {
|
2009-06-07 22:15:28 +00:00
|
|
|
if (_buf == _end)
|
|
|
|
return _curByte;
|
|
|
|
|
|
|
|
assert(_buf < _end);
|
|
|
|
return (*_buf << _bitCount) | _curByte;
|
2008-12-14 22:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BitStream::skip(int n) {
|
|
|
|
assert(n <= 8);
|
2009-06-07 22:15:28 +00:00
|
|
|
_curByte >>= n;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
if (_bitCount >= n) {
|
|
|
|
_bitCount -= n;
|
2008-12-14 22:10:48 +00:00
|
|
|
} else {
|
2009-06-07 22:15:28 +00:00
|
|
|
assert(_buf < _end);
|
|
|
|
_bitCount = _bitCount + 8 - n;
|
|
|
|
_curByte = *_buf++ >> (8 - _bitCount);
|
2008-12-14 22:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* class SmallHuffmanTree
|
|
|
|
* A Huffman-tree to hold 8-bit values.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class SmallHuffmanTree {
|
|
|
|
public:
|
2009-06-07 22:15:28 +00:00
|
|
|
SmallHuffmanTree(BitStream &bs);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
uint16 getCode(BitStream &bs);
|
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
SMK_NODE = 0x8000
|
|
|
|
};
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
uint16 decodeTree(uint32 prefix, int length);
|
|
|
|
|
|
|
|
uint16 _treeSize;
|
|
|
|
uint16 _tree[511];
|
|
|
|
|
|
|
|
uint16 _prefixtree[256];
|
|
|
|
byte _prefixlength[256];
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
BitStream &_bs;
|
|
|
|
};
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
SmallHuffmanTree::SmallHuffmanTree(BitStream &bs)
|
2009-06-07 22:17:44 +00:00
|
|
|
: _treeSize(0), _bs(bs) {
|
2009-06-07 22:15:28 +00:00
|
|
|
uint32 bit = _bs.getBit();
|
|
|
|
assert(bit);
|
|
|
|
|
|
|
|
for (uint16 i = 0; i < 256; ++i)
|
|
|
|
_prefixtree[i] = _prefixlength[i] = 0;
|
|
|
|
|
|
|
|
decodeTree(0, 0);
|
|
|
|
|
|
|
|
bit = _bs.getBit();
|
|
|
|
assert(!bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16 SmallHuffmanTree::decodeTree(uint32 prefix, int length) {
|
2008-12-14 22:10:48 +00:00
|
|
|
if (!_bs.getBit()) { // Leaf
|
2009-06-07 22:15:28 +00:00
|
|
|
_tree[_treeSize] = _bs.getBits8();
|
|
|
|
|
|
|
|
if (length <= 8) {
|
|
|
|
for (int i = 0; i < 256; i += (1 << length)) {
|
|
|
|
_prefixtree[prefix | i] = _treeSize;
|
|
|
|
_prefixlength[prefix | i] = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++_treeSize;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
uint16 t = _treeSize++;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
if (length == 8) {
|
|
|
|
_prefixtree[prefix] = t;
|
|
|
|
_prefixlength[prefix] = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16 r1 = decodeTree(prefix, length + 1);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
_tree[t] = (SMK_NODE | r1);
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
uint16 r2 = decodeTree(prefix | (1 << length), length + 1);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
return r1+r2+1;
|
2008-12-14 23:41:48 +00:00
|
|
|
}
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
uint16 SmallHuffmanTree::getCode(BitStream &bs) {
|
2009-06-07 22:15:28 +00:00
|
|
|
byte peek = bs.peek8();
|
|
|
|
uint16 *p = &_tree[_prefixtree[peek]];
|
|
|
|
bs.skip(_prefixlength[peek]);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
while (*p & SMK_NODE) {
|
|
|
|
if (bs.getBit())
|
|
|
|
p += *p & ~SMK_NODE;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* class BigHuffmanTree
|
|
|
|
* A Huffman-tree to hold 16-bit values.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class BigHuffmanTree {
|
|
|
|
public:
|
2009-06-07 22:15:28 +00:00
|
|
|
BigHuffmanTree(BitStream &bs, int allocSize);
|
|
|
|
~BigHuffmanTree();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
void reset();
|
|
|
|
uint32 getCode(BitStream &bs);
|
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
SMK_NODE = 0x80000000
|
|
|
|
};
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
uint32 decodeTree(uint32 prefix, int length);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
uint32 _treeSize;
|
|
|
|
uint32 *_tree;
|
|
|
|
uint32 _last[3];
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
uint32 _prefixtree[256];
|
|
|
|
byte _prefixlength[256];
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
/* Used during construction */
|
|
|
|
BitStream &_bs;
|
|
|
|
uint32 _markers[3];
|
|
|
|
SmallHuffmanTree *_loBytes;
|
|
|
|
SmallHuffmanTree *_hiBytes;
|
|
|
|
};
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
BigHuffmanTree::BigHuffmanTree(BitStream &bs, int allocSize)
|
2009-06-07 22:17:44 +00:00
|
|
|
: _bs(bs) {
|
2008-12-14 22:10:48 +00:00
|
|
|
uint32 bit = _bs.getBit();
|
|
|
|
if (!bit) {
|
2009-06-07 22:15:28 +00:00
|
|
|
_tree = new uint32[1];
|
|
|
|
_tree[0] = 0;
|
2008-12-14 22:10:48 +00:00
|
|
|
_last[0] = _last[1] = _last[2] = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
for (uint32 i = 0; i < 256; ++i)
|
|
|
|
_prefixtree[i] = _prefixlength[i] = 0;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
_loBytes = new SmallHuffmanTree(_bs);
|
|
|
|
_hiBytes = new SmallHuffmanTree(_bs);
|
|
|
|
|
2010-10-27 22:41:08 +00:00
|
|
|
_markers[0] = _bs.getBits8();
|
|
|
|
_markers[0] |= (_bs.getBits8() << 8);
|
|
|
|
_markers[1] = _bs.getBits8();
|
|
|
|
_markers[1] |= (_bs.getBits8() << 8);
|
|
|
|
_markers[2] = _bs.getBits8();
|
|
|
|
_markers[2] |= (_bs.getBits8() << 8);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
_last[0] = _last[1] = _last[2] = 0xffffffff;
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
_treeSize = 0;
|
|
|
|
_tree = new uint32[allocSize / 4];
|
2008-12-14 22:10:48 +00:00
|
|
|
decodeTree(0, 0);
|
|
|
|
bit = _bs.getBit();
|
|
|
|
assert(!bit);
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
for (uint32 i = 0; i < 3; ++i) {
|
2008-12-14 22:10:48 +00:00
|
|
|
if (_last[i] == 0xffffffff) {
|
2009-06-07 22:15:28 +00:00
|
|
|
_last[i] = _treeSize;
|
|
|
|
_tree[_treeSize++] = 0;
|
2008-12-14 22:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete _loBytes;
|
|
|
|
delete _hiBytes;
|
2008-12-14 23:41:48 +00:00
|
|
|
}
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
BigHuffmanTree::~BigHuffmanTree()
|
|
|
|
{
|
|
|
|
delete[] _tree;
|
|
|
|
}
|
|
|
|
|
2008-12-14 22:10:48 +00:00
|
|
|
void BigHuffmanTree::reset() {
|
|
|
|
_tree[_last[0]] = _tree[_last[1]] = _tree[_last[2]] = 0;
|
|
|
|
}
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
uint32 BigHuffmanTree::decodeTree(uint32 prefix, int length) {
|
2008-12-14 22:10:48 +00:00
|
|
|
uint32 bit = _bs.getBit();
|
|
|
|
|
|
|
|
if (!bit) { // Leaf
|
|
|
|
uint32 lo = _loBytes->getCode(_bs);
|
|
|
|
uint32 hi = _hiBytes->getCode(_bs);
|
|
|
|
|
|
|
|
uint32 v = (hi << 8) | lo;
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
_tree[_treeSize] = v;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
if (length <= 8) {
|
2009-06-07 22:15:28 +00:00
|
|
|
for (int i = 0; i < 256; i += (1 << length)) {
|
|
|
|
_prefixtree[prefix | i] = _treeSize;
|
2008-12-14 22:10:48 +00:00
|
|
|
_prefixlength[prefix | i] = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
2008-12-14 22:10:48 +00:00
|
|
|
if (_markers[i] == v) {
|
2009-06-07 22:15:28 +00:00
|
|
|
_last[i] = _treeSize;
|
|
|
|
_tree[_treeSize] = 0;
|
2008-12-14 22:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-07 22:15:28 +00:00
|
|
|
++_treeSize;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
uint32 t = _treeSize++;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
if (length == 8) {
|
|
|
|
_prefixtree[prefix] = t;
|
|
|
|
_prefixlength[prefix] = 8;
|
|
|
|
}
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
uint32 r1 = decodeTree(prefix, length + 1);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
_tree[t] = SMK_NODE | r1;
|
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
uint32 r2 = decodeTree(prefix | (1 << length), length + 1);
|
2008-12-14 22:10:48 +00:00
|
|
|
return r1+r2+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 BigHuffmanTree::getCode(BitStream &bs) {
|
|
|
|
byte peek = bs.peek8();
|
2009-06-07 22:15:28 +00:00
|
|
|
uint32 *p = &_tree[_prefixtree[peek]];
|
2008-12-14 22:10:48 +00:00
|
|
|
bs.skip(_prefixlength[peek]);
|
|
|
|
|
|
|
|
while (*p & SMK_NODE) {
|
|
|
|
if (bs.getBit())
|
|
|
|
p += (*p) & ~SMK_NODE;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 v = *p;
|
|
|
|
if (v != _tree[_last[0]]) {
|
|
|
|
_tree[_last[2]] = _tree[_last[1]];
|
|
|
|
_tree[_last[1]] = _tree[_last[0]];
|
|
|
|
_tree[_last[0]] = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2009-07-29 10:17:44 +00:00
|
|
|
SmackerDecoder::SmackerDecoder(Audio::Mixer *mixer, Audio::Mixer::SoundType soundType)
|
|
|
|
: _audioStarted(false), _audioStream(0), _mixer(mixer), _soundType(soundType) {
|
2010-05-18 14:17:24 +00:00
|
|
|
_surface = 0;
|
|
|
|
_fileStream = 0;
|
|
|
|
_dirtyPalette = false;
|
2008-12-14 22:10:48 +00:00
|
|
|
}
|
|
|
|
|
2009-03-09 03:45:23 +00:00
|
|
|
SmackerDecoder::~SmackerDecoder() {
|
2010-05-18 14:17:24 +00:00
|
|
|
close();
|
2008-12-15 05:05:01 +00:00
|
|
|
}
|
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
uint32 SmackerDecoder::getElapsedTime() const {
|
2010-05-31 01:27:57 +00:00
|
|
|
if (_audioStream && _audioStarted)
|
2010-05-18 14:17:24 +00:00
|
|
|
return _mixer->getSoundElapsedTime(_audioHandle);
|
2008-12-19 00:14:18 +00:00
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
return VideoDecoder::getElapsedTime();
|
2008-12-19 00:14:18 +00:00
|
|
|
}
|
|
|
|
|
2010-08-04 08:25:05 +00:00
|
|
|
bool SmackerDecoder::load(Common::SeekableReadStream *stream) {
|
2010-05-18 14:17:24 +00:00
|
|
|
close();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2010-08-04 08:25:05 +00:00
|
|
|
_fileStream = stream;
|
2008-12-14 23:41:48 +00:00
|
|
|
|
2008-12-14 22:10:48 +00:00
|
|
|
// Seek to the first frame
|
2008-12-14 23:41:48 +00:00
|
|
|
_header.signature = _fileStream->readUint32BE();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2008-12-15 00:11:23 +00:00
|
|
|
// No BINK support available
|
|
|
|
if (_header.signature == MKID_BE('BIKi')) {
|
|
|
|
delete _fileStream;
|
|
|
|
_fileStream = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-14 22:10:48 +00:00
|
|
|
assert(_header.signature == MKID_BE('SMK2') || _header.signature == MKID_BE('SMK4'));
|
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
uint32 width = _fileStream->readUint32LE();
|
|
|
|
uint32 height = _fileStream->readUint32LE();
|
|
|
|
_frameCount = _fileStream->readUint32LE();
|
|
|
|
int32 frameRate = _fileStream->readSint32LE();
|
|
|
|
|
2010-10-08 22:17:03 +00:00
|
|
|
// framerate contains 2 digits after the comma, so 1497 is actually 14.97 fps
|
2010-05-18 14:17:24 +00:00
|
|
|
if (frameRate > 0)
|
2010-09-12 18:31:10 +00:00
|
|
|
_frameRate = Common::Rational(1000, frameRate);
|
2010-05-18 14:17:24 +00:00
|
|
|
else if (frameRate < 0)
|
2010-09-12 18:31:10 +00:00
|
|
|
_frameRate = Common::Rational(100000, -frameRate);
|
2010-05-18 14:17:24 +00:00
|
|
|
else
|
2010-10-08 22:17:03 +00:00
|
|
|
_frameRate = 1000;
|
2009-01-07 21:19:00 +00:00
|
|
|
|
2008-12-16 13:19:43 +00:00
|
|
|
// Flags are determined by which bit is set, which can be one of the following:
|
|
|
|
// 0 - set to 1 if file contains a ring frame.
|
|
|
|
// 1 - set to 1 if file is Y-interlaced
|
|
|
|
// 2 - set to 1 if file is Y-doubled
|
|
|
|
// If bits 1 or 2 are set, the frame should be scaled to twice its height
|
|
|
|
// before it is displayed.
|
2008-12-14 23:41:48 +00:00
|
|
|
_header.flags = _fileStream->readUint32LE();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2008-12-16 13:19:43 +00:00
|
|
|
// TODO: should we do any extra processing for Smacker files with ring frames?
|
|
|
|
|
|
|
|
// TODO: should we do any extra processing for Y-doubled videos? Are they the
|
|
|
|
// same as Y-interlaced videos?
|
|
|
|
|
|
|
|
uint32 i;
|
2008-12-14 22:10:48 +00:00
|
|
|
for (i = 0; i < 7; ++i)
|
2008-12-14 23:41:48 +00:00
|
|
|
_header.audioSize[i] = _fileStream->readUint32LE();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2008-12-14 23:41:48 +00:00
|
|
|
_header.treesSize = _fileStream->readUint32LE();
|
|
|
|
_header.mMapSize = _fileStream->readUint32LE();
|
|
|
|
_header.mClrSize = _fileStream->readUint32LE();
|
|
|
|
_header.fullSize = _fileStream->readUint32LE();
|
|
|
|
_header.typeSize = _fileStream->readUint32LE();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2008-12-16 13:19:43 +00:00
|
|
|
for (i = 0; i < 7; ++i) {
|
|
|
|
// AudioRate - Frequency and format information for each sound track, up to 7 audio tracks.
|
|
|
|
// The 32 constituent bits have the following meaning:
|
2010-05-31 21:29:43 +00:00
|
|
|
// * bit 31 - indicates Huffman + DPCM compression
|
2008-12-16 13:19:43 +00:00
|
|
|
// * bit 30 - indicates that audio data is present for this track
|
|
|
|
// * bit 29 - 1 = 16-bit audio; 0 = 8-bit audio
|
|
|
|
// * bit 28 - 1 = stereo audio; 0 = mono audio
|
2010-05-31 21:29:43 +00:00
|
|
|
// * bit 27 - indicates Bink RDFT compression
|
|
|
|
// * bit 26 - indicates Bink DCT compression
|
2008-12-16 13:19:43 +00:00
|
|
|
// * bits 25-24 - unused
|
2008-12-22 11:22:15 +00:00
|
|
|
// * bits 23-0 - audio sample rate
|
2010-05-18 14:17:24 +00:00
|
|
|
uint32 audioInfo = _fileStream->readUint32LE();
|
2008-12-16 13:19:43 +00:00
|
|
|
_header.audioInfo[i].hasAudio = audioInfo & 0x40000000;
|
|
|
|
_header.audioInfo[i].is16Bits = audioInfo & 0x20000000;
|
|
|
|
_header.audioInfo[i].isStereo = audioInfo & 0x10000000;
|
|
|
|
_header.audioInfo[i].sampleRate = audioInfo & 0xFFFFFF;
|
2008-12-17 14:48:57 +00:00
|
|
|
|
2010-05-31 21:29:43 +00:00
|
|
|
if (audioInfo & 0x8000000)
|
|
|
|
_header.audioInfo[i].compression = kCompressionRDFT;
|
|
|
|
else if (audioInfo & 0x4000000)
|
|
|
|
_header.audioInfo[i].compression = kCompressionDCT;
|
|
|
|
else if (audioInfo & 0x80000000)
|
|
|
|
_header.audioInfo[i].compression = kCompressionDPCM;
|
|
|
|
else
|
|
|
|
_header.audioInfo[i].compression = kCompressionNone;
|
|
|
|
|
|
|
|
if (_header.audioInfo[i].hasAudio) {
|
|
|
|
if (_header.audioInfo[i].compression == kCompressionRDFT || _header.audioInfo[i].compression == kCompressionDCT)
|
|
|
|
warning("Unhandled Smacker v2 audio compression");
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
_audioStream = Audio::makeQueuingAudioStream(_header.audioInfo[0].sampleRate, _header.audioInfo[0].isStereo);
|
|
|
|
}
|
2008-12-16 13:19:43 +00:00
|
|
|
}
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2008-12-14 23:41:48 +00:00
|
|
|
_header.dummy = _fileStream->readUint32LE();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
_frameSizes = new uint32[_frameCount];
|
|
|
|
for (i = 0; i < _frameCount; ++i)
|
2008-12-14 23:41:48 +00:00
|
|
|
_frameSizes[i] = _fileStream->readUint32LE();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
_frameTypes = new byte[_frameCount];
|
|
|
|
for (i = 0; i < _frameCount; ++i)
|
2008-12-14 23:41:48 +00:00
|
|
|
_frameTypes[i] = _fileStream->readByte();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:34:03 +00:00
|
|
|
byte *huffmanTrees = new byte[_header.treesSize];
|
|
|
|
_fileStream->read(huffmanTrees, _header.treesSize);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:34:03 +00:00
|
|
|
BitStream bs(huffmanTrees, _header.treesSize);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
_MMapTree = new BigHuffmanTree(bs, _header.mMapSize);
|
|
|
|
_MClrTree = new BigHuffmanTree(bs, _header.mClrSize);
|
|
|
|
_FullTree = new BigHuffmanTree(bs, _header.fullSize);
|
|
|
|
_TypeTree = new BigHuffmanTree(bs, _header.typeSize);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:34:03 +00:00
|
|
|
delete[] huffmanTrees;
|
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
_surface = new Graphics::Surface();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
// Height needs to be doubled if we have flags (Y-interlaced or Y-doubled)
|
|
|
|
_surface->create(width, height * (_header.flags ? 2 : 1), 1);
|
2009-05-16 05:34:16 +00:00
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
memset(_palette, 0, 3 * 256);
|
2008-12-14 22:10:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
void SmackerDecoder::close() {
|
2008-12-14 23:41:48 +00:00
|
|
|
if (!_fileStream)
|
|
|
|
return;
|
|
|
|
|
2011-02-07 13:36:59 +00:00
|
|
|
if (_audioStream) {
|
|
|
|
if (_audioStarted) {
|
|
|
|
// The mixer will delete the stream.
|
|
|
|
_mixer->stopHandle(_audioHandle);
|
|
|
|
_audioStarted = false;
|
|
|
|
} else {
|
|
|
|
delete _audioStream;
|
|
|
|
}
|
2008-12-17 14:48:57 +00:00
|
|
|
_audioStream = 0;
|
|
|
|
}
|
|
|
|
|
2008-12-14 23:47:56 +00:00
|
|
|
delete _fileStream;
|
2009-01-06 17:44:41 +00:00
|
|
|
_fileStream = 0;
|
2008-12-14 23:47:56 +00:00
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
_surface->free();
|
|
|
|
delete _surface;
|
|
|
|
_surface = 0;
|
|
|
|
|
2008-12-14 23:41:48 +00:00
|
|
|
delete _MMapTree;
|
|
|
|
delete _MClrTree;
|
|
|
|
delete _FullTree;
|
|
|
|
delete _TypeTree;
|
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
delete[] _frameSizes;
|
|
|
|
delete[] _frameTypes;
|
|
|
|
|
|
|
|
reset();
|
2008-12-14 22:10:48 +00:00
|
|
|
}
|
|
|
|
|
2011-01-23 19:08:09 +00:00
|
|
|
const Graphics::Surface *SmackerDecoder::decodeNextFrame() {
|
2008-12-14 22:10:48 +00:00
|
|
|
uint i;
|
2008-12-17 14:48:57 +00:00
|
|
|
uint32 chunkSize = 0;
|
|
|
|
uint32 dataSizeUnpacked = 0;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2008-12-14 23:41:48 +00:00
|
|
|
uint32 startPos = _fileStream->pos();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
_curFrame++;
|
2009-01-03 13:31:23 +00:00
|
|
|
|
2008-12-16 09:56:21 +00:00
|
|
|
// Check if we got a frame with palette data, and
|
|
|
|
// call back the virtual setPalette function to set
|
|
|
|
// the current palette
|
2010-05-18 14:17:24 +00:00
|
|
|
if (_frameTypes[_curFrame] & 1) {
|
2008-12-14 22:10:48 +00:00
|
|
|
unpackPalette();
|
2010-05-18 14:17:24 +00:00
|
|
|
_dirtyPalette = true;
|
2008-12-14 22:10:48 +00:00
|
|
|
}
|
|
|
|
|
2008-12-16 13:19:43 +00:00
|
|
|
// Load audio tracks
|
2008-12-14 22:10:48 +00:00
|
|
|
for (i = 0; i < 7; ++i) {
|
2010-05-18 14:17:24 +00:00
|
|
|
if (!(_frameTypes[_curFrame] & (2 << i)))
|
2008-12-14 22:10:48 +00:00
|
|
|
continue;
|
|
|
|
|
2008-12-17 14:48:57 +00:00
|
|
|
chunkSize = _fileStream->readUint32LE();
|
|
|
|
chunkSize -= 4; // subtract the first 4 bytes (chunk size)
|
2008-12-16 13:19:43 +00:00
|
|
|
|
2010-10-08 22:17:03 +00:00
|
|
|
if (_header.audioInfo[i].compression == kCompressionNone) {
|
2010-10-12 02:18:11 +00:00
|
|
|
dataSizeUnpacked = chunkSize;
|
2010-10-08 22:17:03 +00:00
|
|
|
} else {
|
2008-12-17 14:48:57 +00:00
|
|
|
dataSizeUnpacked = _fileStream->readUint32LE();
|
|
|
|
chunkSize -= 4; // subtract the next 4 bytes (unpacked data size)
|
2010-10-12 02:18:11 +00:00
|
|
|
}
|
|
|
|
|
2010-10-08 22:17:03 +00:00
|
|
|
handleAudioTrack(i, chunkSize, dataSizeUnpacked);
|
2008-12-14 22:10:48 +00:00
|
|
|
}
|
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
uint32 frameSize = _frameSizes[_curFrame] & ~3;
|
2010-10-08 22:17:03 +00:00
|
|
|
// uint32 remainder = _frameSizes[_curFrame] & 3;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2008-12-14 23:41:48 +00:00
|
|
|
if (_fileStream->pos() - startPos > frameSize)
|
2010-05-18 14:17:24 +00:00
|
|
|
error("Smacker actual frame size exceeds recorded frame size");
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2008-12-14 23:41:48 +00:00
|
|
|
uint32 frameDataSize = frameSize - (_fileStream->pos() - startPos);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2009-06-07 22:15:28 +00:00
|
|
|
_frameData = (byte *)malloc(frameDataSize);
|
2008-12-14 23:41:48 +00:00
|
|
|
_fileStream->read(_frameData, frameDataSize);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
2008-12-27 19:37:49 +00:00
|
|
|
BitStream bs(_frameData, frameDataSize);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
_MMapTree->reset();
|
|
|
|
_MClrTree->reset();
|
|
|
|
_FullTree->reset();
|
|
|
|
_TypeTree->reset();
|
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
// Height needs to be doubled if we have flags (Y-interlaced or Y-doubled)
|
2008-12-14 22:10:48 +00:00
|
|
|
uint doubleY = _header.flags ? 2 : 1;
|
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
uint bw = getWidth() / 4;
|
|
|
|
uint bh = getHeight() / doubleY / 4;
|
|
|
|
uint stride = getWidth();
|
|
|
|
uint block = 0, blocks = bw*bh;
|
|
|
|
|
2008-12-14 22:10:48 +00:00
|
|
|
byte *out;
|
|
|
|
uint type, run, j, mode;
|
|
|
|
uint32 p1, p2, clr, map;
|
|
|
|
byte hi, lo;
|
|
|
|
|
|
|
|
while (block < blocks) {
|
|
|
|
type = _TypeTree->getCode(bs);
|
2008-12-15 21:13:28 +00:00
|
|
|
run = getBlockRun((type >> 2) & 0x3f);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
switch (type & 3) {
|
|
|
|
case SMK_BLOCK_MONO:
|
|
|
|
while (run-- && block < blocks) {
|
|
|
|
clr = _MClrTree->getCode(bs);
|
|
|
|
map = _MMapTree->getCode(bs);
|
2010-05-18 14:17:24 +00:00
|
|
|
out = (byte *)_surface->pixels + (block / bw) * (stride * 4 * doubleY) + (block % bw) * 4;
|
2008-12-14 22:10:48 +00:00
|
|
|
hi = clr >> 8;
|
|
|
|
lo = clr & 0xff;
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
for (j = 0; j < doubleY; j++) {
|
|
|
|
out[0] = (map & 1) ? hi : lo;
|
|
|
|
out[1] = (map & 2) ? hi : lo;
|
|
|
|
out[2] = (map & 4) ? hi : lo;
|
|
|
|
out[3] = (map & 8) ? hi : lo;
|
|
|
|
out += stride;
|
|
|
|
}
|
|
|
|
map >>= 4;
|
|
|
|
}
|
|
|
|
++block;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SMK_BLOCK_FULL:
|
|
|
|
// Smacker v2 has one mode, Smacker v4 has three
|
|
|
|
if (_header.signature == MKID_BE('SMK2')) {
|
|
|
|
mode = 0;
|
|
|
|
} else {
|
|
|
|
// 00 - mode 0
|
|
|
|
// 10 - mode 1
|
|
|
|
// 01 - mode 2
|
|
|
|
mode = 0;
|
|
|
|
if (bs.getBit()) {
|
|
|
|
mode = 1;
|
|
|
|
} else if (bs.getBit()) {
|
|
|
|
mode = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (run-- && block < blocks) {
|
2010-05-18 14:17:24 +00:00
|
|
|
out = (byte *)_surface->pixels + (block / bw) * (stride * 4 * doubleY) + (block % bw) * 4;
|
2008-12-14 22:10:48 +00:00
|
|
|
switch (mode) {
|
|
|
|
case 0:
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
p1 = _FullTree->getCode(bs);
|
|
|
|
p2 = _FullTree->getCode(bs);
|
|
|
|
for (j = 0; j < doubleY; ++j) {
|
|
|
|
out[2] = p1 & 0xff;
|
|
|
|
out[3] = p1 >> 8;
|
|
|
|
out[0] = p2 & 0xff;
|
|
|
|
out[1] = p2 >> 8;
|
|
|
|
out += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
p1 = _FullTree->getCode(bs);
|
|
|
|
out[0] = out[1] = p1 & 0xFF;
|
|
|
|
out[2] = out[3] = p1 >> 8;
|
|
|
|
out += stride;
|
|
|
|
out[0] = out[1] = p1 & 0xFF;
|
|
|
|
out[2] = out[3] = p1 >> 8;
|
|
|
|
out += stride;
|
|
|
|
p2 = _FullTree->getCode(bs);
|
|
|
|
out[0] = out[1] = p2 & 0xFF;
|
|
|
|
out[2] = out[3] = p2 >> 8;
|
|
|
|
out += stride;
|
|
|
|
out[0] = out[1] = p2 & 0xFF;
|
|
|
|
out[2] = out[3] = p2 >> 8;
|
|
|
|
out += stride;
|
|
|
|
break;
|
|
|
|
case 2:
|
2009-01-29 05:26:12 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2008-12-16 08:37:24 +00:00
|
|
|
// We first get p2 and then p1
|
2008-12-16 09:56:21 +00:00
|
|
|
// Check ffmpeg thread "[PATCH] Smacker video decoder bug fix"
|
2008-12-16 08:37:24 +00:00
|
|
|
// http://article.gmane.org/gmane.comp.video.ffmpeg.devel/78768
|
2008-12-14 22:10:48 +00:00
|
|
|
p2 = _FullTree->getCode(bs);
|
2008-12-16 08:37:24 +00:00
|
|
|
p1 = _FullTree->getCode(bs);
|
2008-12-14 22:10:48 +00:00
|
|
|
for (j = 0; j < doubleY; ++j) {
|
|
|
|
out[0] = p1 & 0xff;
|
|
|
|
out[1] = p1 >> 8;
|
|
|
|
out[2] = p2 & 0xff;
|
|
|
|
out[3] = p2 >> 8;
|
|
|
|
out += stride;
|
|
|
|
}
|
|
|
|
for (j = 0; j < doubleY; ++j) {
|
|
|
|
out[0] = p1 & 0xff;
|
|
|
|
out[1] = p1 >> 8;
|
|
|
|
out[2] = p2 & 0xff;
|
|
|
|
out[3] = p2 >> 8;
|
|
|
|
out += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++block;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SMK_BLOCK_SKIP:
|
|
|
|
while (run-- && block < blocks)
|
|
|
|
block++;
|
|
|
|
break;
|
|
|
|
case SMK_BLOCK_FILL:
|
|
|
|
uint32 col;
|
|
|
|
mode = type >> 8;
|
|
|
|
while (run-- && block < blocks) {
|
2010-05-18 14:17:24 +00:00
|
|
|
out = (byte *)_surface->pixels + (block / bw) * (stride * 4 * doubleY) + (block % bw) * 4;
|
2008-12-14 22:10:48 +00:00
|
|
|
col = mode * 0x01010101;
|
|
|
|
for (i = 0; i < 4 * doubleY; ++i) {
|
|
|
|
out[0] = out[1] = out[2] = out[3] = col;
|
|
|
|
out += stride;
|
|
|
|
}
|
|
|
|
++block;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-14 23:41:48 +00:00
|
|
|
_fileStream->seek(startPos + frameSize);
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
free(_frameData);
|
|
|
|
|
2010-05-18 14:17:24 +00:00
|
|
|
if (_curFrame == 0)
|
|
|
|
_startTime = g_system->getMillis();
|
|
|
|
|
|
|
|
return _surface;
|
2008-12-14 22:10:48 +00:00
|
|
|
}
|
|
|
|
|
2010-10-09 00:17:55 +00:00
|
|
|
void SmackerDecoder::handleAudioTrack(byte track, uint32 chunkSize, uint32 unpackedSize) {
|
2010-10-08 22:17:03 +00:00
|
|
|
if (_header.audioInfo[track].hasAudio && chunkSize > 0 && track == 0) {
|
|
|
|
// If it's track 0, play the audio data
|
|
|
|
byte *soundBuffer = (byte *)malloc(chunkSize);
|
|
|
|
|
|
|
|
_fileStream->read(soundBuffer, chunkSize);
|
|
|
|
|
|
|
|
if (_header.audioInfo[track].compression == kCompressionRDFT || _header.audioInfo[track].compression == kCompressionDCT) {
|
|
|
|
// TODO: Compressed audio (Bink RDFT/DCT encoded)
|
|
|
|
free(soundBuffer);
|
|
|
|
return;
|
|
|
|
} else if (_header.audioInfo[track].compression == kCompressionDPCM) {
|
|
|
|
// Compressed audio (Huffman DPCM encoded)
|
|
|
|
queueCompressedBuffer(soundBuffer, chunkSize, unpackedSize, track);
|
|
|
|
free(soundBuffer);
|
|
|
|
} else {
|
|
|
|
// Uncompressed audio (PCM)
|
|
|
|
byte flags = 0;
|
|
|
|
if (_header.audioInfo[track].is16Bits)
|
|
|
|
flags = flags | Audio::FLAG_16BITS;
|
|
|
|
if (_header.audioInfo[track].isStereo)
|
|
|
|
flags = flags | Audio::FLAG_STEREO;
|
|
|
|
|
|
|
|
_audioStream->queueBuffer(soundBuffer, chunkSize, DisposeAfterUse::YES, flags);
|
|
|
|
// The sound buffer will be deleted by QueuingAudioStream
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_audioStarted) {
|
|
|
|
_mixer->playStream(_soundType, &_audioHandle, _audioStream, -1, 255);
|
|
|
|
_audioStarted = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Ignore the rest of the audio tracks, if they exist
|
|
|
|
// TODO: Are there any Smacker videos with more than one audio stream?
|
|
|
|
// If yes, we should play the rest of the audio streams as well
|
|
|
|
if (chunkSize > 0)
|
|
|
|
_fileStream->skip(chunkSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-09 03:45:23 +00:00
|
|
|
void SmackerDecoder::queueCompressedBuffer(byte *buffer, uint32 bufferSize,
|
2008-12-19 01:45:55 +00:00
|
|
|
uint32 unpackedSize, int streamNum) {
|
2008-12-18 23:50:59 +00:00
|
|
|
|
2008-12-27 19:37:49 +00:00
|
|
|
BitStream audioBS(buffer, bufferSize);
|
2008-12-17 14:48:57 +00:00
|
|
|
bool dataPresent = audioBS.getBit();
|
|
|
|
|
|
|
|
if (!dataPresent)
|
|
|
|
return;
|
|
|
|
|
2008-12-18 23:50:59 +00:00
|
|
|
bool isStereo = audioBS.getBit();
|
2008-12-17 14:48:57 +00:00
|
|
|
assert(isStereo == _header.audioInfo[streamNum].isStereo);
|
2008-12-18 23:50:59 +00:00
|
|
|
bool is16Bits = audioBS.getBit();
|
2008-12-17 14:48:57 +00:00
|
|
|
assert(is16Bits == _header.audioInfo[streamNum].is16Bits);
|
|
|
|
|
2008-12-18 23:50:59 +00:00
|
|
|
int numBytes = 1 * (isStereo ? 2 : 1) * (is16Bits ? 2 : 1);
|
2008-12-17 14:48:57 +00:00
|
|
|
|
2010-01-19 00:56:29 +00:00
|
|
|
byte *unpackedBuffer = (byte *)malloc(unpackedSize);
|
2008-12-18 23:50:59 +00:00
|
|
|
byte *curPointer = unpackedBuffer;
|
2008-12-19 01:45:55 +00:00
|
|
|
uint32 curPos = 0;
|
2008-12-18 23:50:59 +00:00
|
|
|
|
|
|
|
SmallHuffmanTree *audioTrees[4];
|
|
|
|
for (int k = 0; k < numBytes; k++)
|
2008-12-17 14:48:57 +00:00
|
|
|
audioTrees[k] = new SmallHuffmanTree(audioBS);
|
|
|
|
|
2008-12-18 19:18:45 +00:00
|
|
|
// Base values, stored as big endian
|
|
|
|
|
2008-12-18 23:50:59 +00:00
|
|
|
int32 bases[2];
|
|
|
|
|
2010-10-27 22:41:08 +00:00
|
|
|
if (isStereo) {
|
|
|
|
if (is16Bits) {
|
|
|
|
byte hi = audioBS.getBits8();
|
|
|
|
byte lo = audioBS.getBits8();
|
|
|
|
bases[1] = (int16) ((hi << 8) | lo);
|
|
|
|
} else {
|
|
|
|
bases[1] = audioBS.getBits8();
|
|
|
|
}
|
|
|
|
}
|
2008-12-18 19:32:44 +00:00
|
|
|
|
2010-10-27 22:41:08 +00:00
|
|
|
if (is16Bits) {
|
|
|
|
byte hi = audioBS.getBits8();
|
|
|
|
byte lo = audioBS.getBits8();
|
|
|
|
bases[0] = (int16) ((hi << 8) | lo);
|
|
|
|
} else {
|
|
|
|
bases[0] = audioBS.getBits8();
|
|
|
|
}
|
2008-12-18 19:32:44 +00:00
|
|
|
|
2008-12-18 23:50:59 +00:00
|
|
|
// The bases are the first samples, too
|
2008-12-19 21:55:18 +00:00
|
|
|
for (int i = 0; i < (isStereo ? 2 : 1); i++, curPointer += (is16Bits ? 2 : 1), curPos += (is16Bits ? 2 : 1)) {
|
2008-12-18 23:50:59 +00:00
|
|
|
if (is16Bits)
|
|
|
|
WRITE_BE_UINT16(curPointer, bases[i]);
|
|
|
|
else
|
|
|
|
*curPointer = (bases[i] & 0xFF) ^ 0x80;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next follow the deltas, which are added to the corresponding base values and
|
|
|
|
// are stored as little endian
|
2008-12-18 19:18:45 +00:00
|
|
|
// We store the unpacked bytes in big endian format
|
|
|
|
|
2008-12-17 21:17:15 +00:00
|
|
|
while (curPos < unpackedSize) {
|
2008-12-18 19:32:44 +00:00
|
|
|
// If the sample is stereo, the data is stored for the left and right channel, respectively
|
|
|
|
// (the exact opposite to the base values)
|
2008-12-17 21:17:15 +00:00
|
|
|
if (!is16Bits) {
|
2008-12-18 23:50:59 +00:00
|
|
|
for (int k = 0; k < (isStereo ? 2 : 1); k++) {
|
2008-12-19 08:22:25 +00:00
|
|
|
bases[k] += (int8) ((int16) audioTrees[k]->getCode(audioBS));
|
|
|
|
*curPointer++ = CLIP<int>(bases[k], 0, 255) ^ 0x80;
|
2008-12-17 21:17:15 +00:00
|
|
|
curPos++;
|
|
|
|
}
|
|
|
|
} else {
|
2008-12-18 23:50:59 +00:00
|
|
|
for (int k = 0; k < (isStereo ? 2 : 1); k++) {
|
2010-10-27 22:41:08 +00:00
|
|
|
byte lo = audioTrees[k * 2]->getCode(audioBS);
|
|
|
|
byte hi = audioTrees[k * 2 + 1]->getCode(audioBS);
|
|
|
|
bases[k] += (int16) (lo | (hi << 8));
|
2008-12-19 01:01:07 +00:00
|
|
|
|
2010-10-08 22:17:03 +00:00
|
|
|
WRITE_BE_UINT16(curPointer, bases[k]);
|
2008-12-19 01:01:07 +00:00
|
|
|
curPointer += 2;
|
|
|
|
curPos += 2;
|
|
|
|
}
|
2008-12-17 21:17:15 +00:00
|
|
|
}
|
2008-12-19 01:01:07 +00:00
|
|
|
|
2008-12-17 21:17:15 +00:00
|
|
|
}
|
|
|
|
|
2008-12-18 23:50:59 +00:00
|
|
|
for (int k = 0; k < numBytes; k++)
|
2008-12-18 19:18:45 +00:00
|
|
|
delete audioTrees[k];
|
|
|
|
|
2010-01-08 22:05:12 +00:00
|
|
|
byte flags = 0;
|
|
|
|
if (_header.audioInfo[0].is16Bits)
|
2010-01-19 22:30:33 +00:00
|
|
|
flags = flags | Audio::FLAG_16BITS;
|
2010-01-08 22:05:12 +00:00
|
|
|
if (_header.audioInfo[0].isStereo)
|
2010-01-19 22:30:33 +00:00
|
|
|
flags = flags | Audio::FLAG_STEREO;
|
2010-01-19 00:56:29 +00:00
|
|
|
_audioStream->queueBuffer(unpackedBuffer, unpackedSize, DisposeAfterUse::YES, flags);
|
2010-01-08 22:06:04 +00:00
|
|
|
// unpackedBuffer will be deleted by QueuingAudioStream
|
2008-12-17 14:48:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-09 03:45:23 +00:00
|
|
|
void SmackerDecoder::unpackPalette() {
|
2008-12-14 23:41:48 +00:00
|
|
|
uint startPos = _fileStream->pos();
|
|
|
|
uint32 len = 4 * _fileStream->readByte();
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
byte *chunk = (byte *)malloc(len);
|
2011-02-07 17:47:20 +00:00
|
|
|
_fileStream->read(chunk, len);
|
|
|
|
byte *p = chunk;
|
2008-12-14 22:10:48 +00:00
|
|
|
|
|
|
|
byte oldPalette[3*256];
|
|
|
|
memcpy(oldPalette, _palette, 3 * 256);
|
|
|
|
|
|
|
|
byte *pal = _palette;
|
|
|
|
|
|
|
|
int sz = 0;
|
|
|
|
byte b0;
|
|
|
|
while (sz < 256) {
|
|
|
|
b0 = *p++;
|
2008-12-15 21:13:28 +00:00
|
|
|
if (b0 & 0x80) { // if top bit is 1 (0x80 = 10000000)
|
|
|
|
sz += (b0 & 0x7f) + 1; // get lower 7 bits + 1 (0x7f = 01111111)
|
2008-12-14 22:10:48 +00:00
|
|
|
pal += 3 * ((b0 & 0x7f) + 1);
|
2008-12-15 21:13:28 +00:00
|
|
|
} else if (b0 & 0x40) { // if top 2 bits are 01 (0x40 = 01000000)
|
|
|
|
byte c = (b0 & 0x3f) + 1; // get lower 6 bits + 1 (0x3f = 00111111)
|
2008-12-14 22:10:48 +00:00
|
|
|
uint s = 3 * *p++;
|
|
|
|
sz += c;
|
|
|
|
|
|
|
|
while (c--) {
|
|
|
|
*pal++ = oldPalette[s + 0];
|
|
|
|
*pal++ = oldPalette[s + 1];
|
|
|
|
*pal++ = oldPalette[s + 2];
|
|
|
|
s += 3;
|
|
|
|
}
|
2008-12-15 21:13:28 +00:00
|
|
|
} else { // top 2 bits are 00
|
2008-12-14 22:10:48 +00:00
|
|
|
sz++;
|
2008-12-15 21:13:28 +00:00
|
|
|
// get the lower 6 bits for each component (0x3f = 00111111)
|
2008-12-14 22:10:48 +00:00
|
|
|
byte b = b0 & 0x3f;
|
|
|
|
byte g = (*p++) & 0x3f;
|
|
|
|
byte r = (*p++) & 0x3f;
|
|
|
|
|
|
|
|
assert(g < 0xc0 && b < 0xc0);
|
|
|
|
|
2008-12-15 21:13:28 +00:00
|
|
|
// upscale to full 8-bit color values by multiplying by 4
|
2008-12-14 22:10:48 +00:00
|
|
|
*pal++ = b * 4;
|
|
|
|
*pal++ = g * 4;
|
|
|
|
*pal++ = r * 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-14 23:41:48 +00:00
|
|
|
_fileStream->seek(startPos + len);
|
2008-12-14 22:10:48 +00:00
|
|
|
free(chunk);
|
|
|
|
}
|
|
|
|
|
2011-01-23 19:08:09 +00:00
|
|
|
} // End of namespace Video
|