Keep around z_stream between decompression passes.

Slightly more efficient to avoid reallocating the state for each block.
This commit is contained in:
Unknown W. Brackets 2015-04-15 09:01:29 -07:00
parent 17a58103e2
commit 49cfc3424b
2 changed files with 39 additions and 30 deletions

View File

@ -140,6 +140,15 @@ bool CsoFileReader::InitializeBuffers() {
return false;
}
m_z_stream = new z_stream;
m_z_stream->zalloc = Z_NULL;
m_z_stream->zfree = Z_NULL;
m_z_stream->opaque = Z_NULL;
if (inflateInit2(m_z_stream, -15) != Z_OK) {
Console.Error("Unable to initialize zlib for CSO decompression.");
return false;
}
return true;
}
@ -148,20 +157,24 @@ void CsoFileReader::Close() {
if (m_src) {
fclose(m_src);
m_src = 0;
m_src = NULL;
}
if (m_z_stream) {
inflateEnd(m_z_stream);
m_z_stream = NULL;
}
if (m_readBuffer) {
delete[] m_readBuffer;
m_readBuffer = 0;
m_readBuffer = NULL;
}
if (m_zlibBuffer) {
delete[] m_zlibBuffer;
m_zlibBuffer = 0;
m_zlibBuffer = NULL;
}
if (m_index) {
delete[] m_index;
m_index = 0;
m_index = NULL;
}
}
@ -238,41 +251,33 @@ int CsoFileReader::ReadFromFrame(u8 *dest, u64 pos, u64 maxBytes) {
}
bool CsoFileReader::DecompressFrame(u32 frame, u32 readBufferSize) {
z_stream z;
z.zalloc = Z_NULL;
z.zfree = Z_NULL;
z.opaque = Z_NULL;
if (inflateInit2(&z, -15) != Z_OK) {
Console.Error("Unable to initialize zlib for CSO decompression.");
return false;
}
m_z_stream->next_in = m_readBuffer;
m_z_stream->avail_in = readBufferSize;
m_z_stream->next_out = m_zlibBuffer;
m_z_stream->avail_out = m_frameSize;
z.next_in = m_readBuffer;
z.avail_in = readBufferSize;
z.next_out = m_zlibBuffer;
z.avail_out = m_frameSize;
int status = inflate(&z, Z_FINISH);
if (status != Z_STREAM_END || z.total_out != m_frameSize) {
inflateEnd(&z);
int status = inflate(m_z_stream, Z_FINISH);
bool success = status == Z_STREAM_END && m_z_stream->total_out == m_frameSize;
if (success) {
// Our buffer now contains this frame.
m_zlibBufferFrame = frame;
} else {
Console.Error("Unable to decompress CSO frame using zlib.");
return false;
m_zlibBufferFrame = (u32)-1;
}
inflateEnd(&z);
// Our buffer now contains this frame.
m_zlibBufferFrame = frame;
return true;
inflateReset(m_z_stream);
return success;
}
void CsoFileReader::BeginRead(void* pBuffer, uint sector, uint count) {
// TODO: No async support yet, implement as sync.
mBytesRead = ReadSync(pBuffer, sector, count);
m_bytesRead = ReadSync(pBuffer, sector, count);
}
int CsoFileReader::FinishRead() {
int res = mBytesRead;
mBytesRead = -1;
int res = m_bytesRead;
m_bytesRead = -1;
return res;
}

View File

@ -18,6 +18,7 @@
#include "AsyncFileReader.h"
struct CsoHeader;
typedef struct z_stream_s z_stream;
class CsoFileReader : public AsyncFileReader
{
@ -28,7 +29,9 @@ public:
m_zlibBuffer(0),
m_index(0),
m_totalSize(0),
m_src(0) {
m_src(0),
m_z_stream(0),
m_bytesRead(0) {
m_blocksize = 2048;
};
@ -69,6 +72,7 @@ private:
u64 m_totalSize;
// The actual source cso file handle.
FILE* m_src;
z_stream* m_z_stream;
// The result of a read is stored here between BeginRead() and FinishRead().
int mBytesRead;
int m_bytesRead;
};