scummvm/engines/grim/imuse/imuse_mcmp_mgr.cpp

173 lines
4.9 KiB
C++
Raw Normal View History

2021-12-26 21:19:38 +01:00
/* ScummVM - Graphic Adventure Engine
*
2021-12-26 21:19:38 +01:00
* ScummVM is the legal property of its developers, whose names
2011-04-16 14:12:44 +02:00
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
2006-04-02 14:20:45 +00:00
*
* 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 3 of the License, or
* (at your option) any later version.
2014-04-05 18:18:42 +02:00
*
* This program is distributed in the hope that it will be useful,
2006-04-02 14:20:45 +00:00
* 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.
2014-04-05 18:18:42 +02:00
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-04-02 14:20:45 +00:00
*
*/
#include "common/file.h"
2011-05-13 17:55:14 -07:00
#include "engines/grim/resource.h"
#include "engines/grim/imuse/imuse_mcmp_mgr.h"
#include "engines/grim/movie/codecs/vima.h"
2009-05-25 06:49:57 +00:00
namespace Grim {
uint16 imuseDestTable[5786];
McmpMgr::McmpMgr() {
2014-05-29 15:35:46 -07:00
_compTable = nullptr;
_numCompItems = 0;
_curSample = -1;
2014-05-29 15:35:46 -07:00
_compInput = nullptr;
2004-12-31 20:10:08 +00:00
_outputSize = 0;
2014-05-29 15:35:46 -07:00
_file = nullptr;
2004-12-31 20:10:08 +00:00
_lastBlock = -1;
}
McmpMgr::~McmpMgr() {
delete[] _compTable;
delete[] _compInput;
}
bool McmpMgr::openSound(const char *filename, Common::SeekableReadStream *data, int &offsetData) {
_file = data;
2008-07-26 09:18:46 +00:00
uint32 tag = _file->readUint32BE();
if (tag != 'MCMP') {
error("McmpMgr::openSound() Expected MCMP tag");
2004-12-29 06:32:07 +00:00
return false;
}
2008-07-26 09:18:46 +00:00
_numCompItems = _file->readSint16BE();
assert(_numCompItems > 0);
2008-07-26 09:18:46 +00:00
int32 offset = _file->pos() + (_numCompItems * 9) + 2;
_numCompItems--;
_compTable = new CompTable[_numCompItems];
2008-07-26 09:18:46 +00:00
_file->seek(5, SEEK_CUR);
int32 headerSize = _compTable[0].decompSize = _file->readSint32BE();
int32 maxSize = headerSize;
2004-12-31 18:17:25 +00:00
offset += headerSize;
2004-12-28 18:25:14 +00:00
int i;
for (i = 0; i < _numCompItems; i++) {
2008-07-26 09:18:46 +00:00
_compTable[i].codec = _file->readByte();
_compTable[i].decompSize = _file->readSint32BE();
_compTable[i].compSize = _file->readSint32BE();
_compTable[i].offset = offset;
offset += _compTable[i].compSize;
2004-12-29 06:32:07 +00:00
if (_compTable[i].compSize > maxSize)
maxSize = _compTable[i].compSize;
}
2008-07-26 09:18:46 +00:00
int16 sizeCodecs = _file->readSint16BE();
for (i = 0; i < _numCompItems; i++) {
_compTable[i].offset += sizeCodecs;
}
2008-07-26 09:18:46 +00:00
_file->seek(sizeCodecs, SEEK_CUR);
2023-03-06 03:26:32 +01:00
_uncompressedSingleBlock = true;
if (_numCompItems == 0)
_uncompressedSingleBlock = false;
if (_numCompItems >= 1 && _compTable[0].codec != 0)
_uncompressedSingleBlock = false;
for (i = 1; i < _numCompItems; i++) {
if (_compTable[i].codec || _compTable[i].decompSize || _compTable[i].compSize) {
_uncompressedSingleBlock = false;
break;
}
}
2008-08-11 19:04:52 +00:00
// hack: two more bytes at the end of input buffer
2023-03-06 03:26:32 +01:00
if (!_uncompressedSingleBlock)
_compInput = new byte[maxSize + 2];
2004-12-31 18:17:25 +00:00
offsetData = headerSize;
2004-12-29 06:32:07 +00:00
return true;
}
int32 McmpMgr::decompressSample(int32 offset, int32 size, byte **comp_final) {
int32 i, final_size, output_size;
int skip, first_block, last_block;
if (!_file) {
2005-01-01 10:12:16 +00:00
error("McmpMgr::decompressSampleByName() File is not open!");
return 0;
}
2023-03-06 03:26:32 +01:00
if (_uncompressedSingleBlock) {
*comp_final = static_cast<byte *>(malloc(size));
_file->seek(_compTable[0].offset + offset, SEEK_SET);
return _file->read(*comp_final, size);
}
first_block = offset / 0x2000;
last_block = (offset + size - 1) / 0x2000;
2004-12-29 06:32:07 +00:00
skip = offset % 0x2000;
// Clip last_block by the total number of blocks (= "comp items")
if ((last_block >= _numCompItems) && (_numCompItems > 0))
last_block = _numCompItems - 1;
int32 blocks_final_size = 0x2000 * (1 + last_block - first_block);
*comp_final = static_cast<byte *>(malloc(blocks_final_size));
final_size = 0;
for (i = first_block; i <= last_block; i++) {
if (_lastBlock != i) {
2008-08-11 19:04:52 +00:00
// hack: two more zero bytes at the end of input buffer
_compInput[_compTable[i].compSize] = 0;
2008-08-11 19:04:52 +00:00
_compInput[_compTable[i].compSize + 1] = 0;
2008-07-26 09:18:46 +00:00
_file->seek(_compTable[i].offset, SEEK_SET);
2023-03-06 03:26:32 +01:00
if (_compTable[i].codec == 0 && _compTable[i].decompSize == _compTable[i].compSize) {
_file->read(_compOutput, _compTable[i].compSize);
} else {
_file->read(_compInput, _compTable[i].compSize);
decompressVima(_compInput, (int16 *)_compOutput, _compTable[i].decompSize, imuseDestTable, false);
}
_outputSize = _compTable[i].decompSize;
if (_outputSize > 0x2000) {
2005-01-01 10:12:16 +00:00
error("McmpMgr::decompressSample() _outputSize: %d", _outputSize);
}
_lastBlock = i;
}
output_size = _outputSize - skip;
if ((output_size + skip) > 0x2000) // workaround
output_size -= (output_size + skip) - 0x2000;
if (output_size > size)
output_size = size;
assert(final_size + output_size <= blocks_final_size);
memcpy(*comp_final + final_size, _compOutput + skip, output_size);
final_size += output_size;
size -= output_size;
assert(size >= 0);
if (size == 0)
break;
skip = 0;
}
return final_size;
}
2009-05-25 06:49:57 +00:00
} // end of namespace Grim