ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

imuse_mcmp_mgr.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/file.h"
00024 
00025 #include "engines/grim/resource.h"
00026 
00027 #include "engines/grim/imuse/imuse_mcmp_mgr.h"
00028 #include "engines/grim/movie/codecs/vima.h"
00029 
00030 namespace Grim {
00031 
00032 uint16 imuseDestTable[5786];
00033 
00034 McmpMgr::McmpMgr() {
00035     _compTable = nullptr;
00036     _numCompItems = 0;
00037     _curSample = -1;
00038     _compInput = nullptr;
00039     _outputSize = 0;
00040     _file = nullptr;
00041     _lastBlock = -1;
00042 }
00043 
00044 McmpMgr::~McmpMgr() {
00045     delete[] _compTable;
00046     delete[] _compInput;
00047 }
00048 
00049 bool McmpMgr::openSound(const char *filename, Common::SeekableReadStream *data, int &offsetData) {
00050     _file = data;
00051 
00052     uint32 tag = _file->readUint32BE();
00053     if (tag != 'MCMP') {
00054         error("McmpMgr::openSound() Expected MCMP tag");
00055         return false;
00056     }
00057 
00058     _numCompItems = _file->readSint16BE();
00059     assert(_numCompItems > 0);
00060 
00061     int32 offset = _file->pos() + (_numCompItems * 9) + 2;
00062     _numCompItems--;
00063     _compTable = new CompTable[_numCompItems];
00064     _file->seek(5, SEEK_CUR);
00065     int32 headerSize = _compTable[0].decompSize = _file->readSint32BE();
00066     int32 maxSize = headerSize;
00067     offset += headerSize;
00068 
00069     int i;
00070     for (i = 0; i < _numCompItems; i++) {
00071         _compTable[i].codec = _file->readByte();
00072         _compTable[i].decompSize = _file->readSint32BE();
00073         _compTable[i].compSize = _file->readSint32BE();
00074         _compTable[i].offset = offset;
00075         offset += _compTable[i].compSize;
00076         if (_compTable[i].compSize > maxSize)
00077             maxSize = _compTable[i].compSize;
00078     }
00079     int16 sizeCodecs = _file->readSint16BE();
00080     for (i = 0; i < _numCompItems; i++) {
00081         _compTable[i].offset += sizeCodecs;
00082     }
00083     _file->seek(sizeCodecs, SEEK_CUR);
00084     // hack: two more bytes at the end of input buffer
00085     _compInput = new byte[maxSize + 2];
00086     offsetData = headerSize;
00087 
00088     return true;
00089 }
00090 
00091 int32 McmpMgr::decompressSample(int32 offset, int32 size, byte **comp_final) {
00092     int32 i, final_size, output_size;
00093     int skip, first_block, last_block;
00094 
00095     if (!_file) {
00096         error("McmpMgr::decompressSampleByName() File is not open!");
00097         return 0;
00098     }
00099 
00100     first_block = offset / 0x2000;
00101     last_block = (offset + size - 1) / 0x2000;
00102     skip = offset % 0x2000;
00103 
00104     // Clip last_block by the total number of blocks (= "comp items")
00105     if ((last_block >= _numCompItems) && (_numCompItems > 0))
00106         last_block = _numCompItems - 1;
00107 
00108     int32 blocks_final_size = 0x2000 * (1 + last_block - first_block);
00109     *comp_final = static_cast<byte *>(malloc(blocks_final_size));
00110     final_size = 0;
00111 
00112     for (i = first_block; i <= last_block; i++) {
00113         if (_lastBlock != i) {
00114             // hack: two more zero bytes at the end of input buffer
00115             _compInput[_compTable[i].compSize] = 0;
00116             _compInput[_compTable[i].compSize + 1] = 0;
00117             _file->seek(_compTable[i].offset, SEEK_SET);
00118             _file->read(_compInput, _compTable[i].compSize);
00119             decompressVima(_compInput, (int16 *)_compOutput, _compTable[i].decompSize, imuseDestTable);
00120             _outputSize = _compTable[i].decompSize;
00121             if (_outputSize > 0x2000) {
00122                 error("McmpMgr::decompressSample() _outputSize: %d", _outputSize);
00123             }
00124             _lastBlock = i;
00125         }
00126 
00127         output_size = _outputSize - skip;
00128 
00129         if ((output_size + skip) > 0x2000) // workaround
00130             output_size -= (output_size + skip) - 0x2000;
00131 
00132         if (output_size > size)
00133             output_size = size;
00134 
00135         assert(final_size + output_size <= blocks_final_size);
00136 
00137         memcpy(*comp_final + final_size, _compOutput + skip, output_size);
00138         final_size += output_size;
00139 
00140         size -= output_size;
00141         assert(size >= 0);
00142         if (size == 0)
00143             break;
00144 
00145         skip = 0;
00146     }
00147 
00148     return final_size;
00149 }
00150 
00151 } // end of namespace Grim


Generated on Sat Oct 12 2019 05:00:49 for ResidualVM by doxygen 1.7.1
curved edge   curved edge