2021-12-26 21:19:38 +01:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2008-06-13 14:57:47 +00:00
|
|
|
*
|
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
|
2008-06-13 14:57:47 +00:00
|
|
|
* file distributed with this source distribution.
|
2006-04-02 14:20:45 +00:00
|
|
|
*
|
2021-12-26 18:47:58 +01: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
|
|
|
*
|
2012-12-19 23:15:43 +01: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
|
2012-12-19 23:15:43 +01:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-04-05 18:18:42 +02:00
|
|
|
*
|
2012-12-19 23:15:43 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 18:47:58 +01:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-04-02 14:20:45 +00:00
|
|
|
*
|
|
|
|
*/
|
2004-12-28 08:05:18 +00:00
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
#include "common/file.h"
|
|
|
|
|
2011-05-13 17:55:14 -07:00
|
|
|
#include "engines/grim/resource.h"
|
2008-01-26 11:47:23 +00:00
|
|
|
|
2009-05-24 19:13:58 +00:00
|
|
|
#include "engines/grim/imuse/imuse_mcmp_mgr.h"
|
2011-10-03 01:54:01 +02:00
|
|
|
#include "engines/grim/movie/codecs/vima.h"
|
2004-12-28 08:05:18 +00:00
|
|
|
|
2009-05-25 06:49:57 +00:00
|
|
|
namespace Grim {
|
|
|
|
|
2004-12-28 08:05:18 +00:00
|
|
|
uint16 imuseDestTable[5786];
|
|
|
|
|
|
|
|
McmpMgr::McmpMgr() {
|
2014-05-29 15:35:46 -07:00
|
|
|
_compTable = nullptr;
|
2004-12-28 08:05:18 +00:00
|
|
|
_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;
|
2004-12-28 08:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
McmpMgr::~McmpMgr() {
|
2008-03-10 23:06:07 +00:00
|
|
|
delete[] _compTable;
|
|
|
|
delete[] _compInput;
|
2004-12-28 08:05:18 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 21:55:55 +01:00
|
|
|
bool McmpMgr::openSound(const char *filename, Common::SeekableReadStream *data, int &offsetData) {
|
|
|
|
_file = data;
|
2004-12-28 08:05:18 +00:00
|
|
|
|
2008-07-26 09:18:46 +00:00
|
|
|
uint32 tag = _file->readUint32BE();
|
|
|
|
if (tag != 'MCMP') {
|
2004-12-31 20:17:43 +00:00
|
|
|
error("McmpMgr::openSound() Expected MCMP tag");
|
2004-12-29 06:32:07 +00:00
|
|
|
return false;
|
2004-12-28 08:05:18 +00:00
|
|
|
}
|
|
|
|
|
2008-07-26 09:18:46 +00:00
|
|
|
_numCompItems = _file->readSint16BE();
|
2004-12-28 08:05:18 +00:00
|
|
|
assert(_numCompItems > 0);
|
|
|
|
|
2008-07-26 09:18:46 +00:00
|
|
|
int32 offset = _file->pos() + (_numCompItems * 9) + 2;
|
2004-12-28 08:05:18 +00:00
|
|
|
_numCompItems--;
|
2008-03-10 23:06:07 +00:00
|
|
|
_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 08:05:18 +00:00
|
|
|
|
2004-12-28 18:25:14 +00:00
|
|
|
int i;
|
2004-12-28 08:05:18 +00:00
|
|
|
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();
|
2004-12-28 08:05:18 +00:00
|
|
|
_compTable[i].offset = offset;
|
|
|
|
offset += _compTable[i].compSize;
|
2004-12-29 06:32:07 +00:00
|
|
|
if (_compTable[i].compSize > maxSize)
|
2004-12-28 08:05:18 +00:00
|
|
|
maxSize = _compTable[i].compSize;
|
|
|
|
}
|
2008-07-26 09:18:46 +00:00
|
|
|
int16 sizeCodecs = _file->readSint16BE();
|
2004-12-28 08:05:18 +00:00
|
|
|
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;
|
2011-12-20 21:55:55 +01:00
|
|
|
|
2004-12-29 06:32:07 +00:00
|
|
|
return true;
|
2004-12-28 08:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32 McmpMgr::decompressSample(int32 offset, int32 size, byte **comp_final) {
|
|
|
|
int32 i, final_size, output_size;
|
|
|
|
int skip, first_block, last_block;
|
|
|
|
|
2011-12-16 14:48:42 +01:00
|
|
|
if (!_file) {
|
2005-01-01 10:12:16 +00:00
|
|
|
error("McmpMgr::decompressSampleByName() File is not open!");
|
2004-12-28 08:05:18 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-12-28 08:05:18 +00:00
|
|
|
first_block = offset / 0x2000;
|
|
|
|
last_block = (offset + size - 1) / 0x2000;
|
2004-12-29 06:32:07 +00:00
|
|
|
skip = offset % 0x2000;
|
2004-12-28 08:05:18 +00:00
|
|
|
|
|
|
|
// 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);
|
2018-06-03 07:44:39 +02:00
|
|
|
*comp_final = static_cast<byte *>(malloc(blocks_final_size));
|
2004-12-28 08:05:18 +00:00
|
|
|
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
|
2008-07-13 12:25:05 +00:00
|
|
|
_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);
|
|
|
|
}
|
2004-12-28 08:05:18 +00:00
|
|
|
_outputSize = _compTable[i].decompSize;
|
|
|
|
if (_outputSize > 0x2000) {
|
2005-01-01 10:12:16 +00:00
|
|
|
error("McmpMgr::decompressSample() _outputSize: %d", _outputSize);
|
2004-12-28 08:05:18 +00:00
|
|
|
}
|
|
|
|
_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
|