gsla: all the frames in my test file are decompressing correctly

This commit is contained in:
JASON-6700K\jandersen 2020-07-20 08:58:20 -04:00
parent c768f34156
commit 8dd7fbd28f
3 changed files with 70 additions and 24 deletions

View File

@ -419,10 +419,15 @@ void GSLAFile::SaveToFile(const char* pFilenamePath)
// buffer with zero // buffer with zero
//memset(pWorkBuffer, 0, m_frameSize * 2); //memset(pWorkBuffer, 0, m_frameSize * 2);
int frameSize = LZBA_Compress(pWorkBuffer, m_pC1PixelMaps[ frameIndex], int frameSize = LZBA_Compress(pWorkBuffer, m_pC1PixelMaps[ frameIndex ],
m_frameSize, pWorkBuffer-bytes.size(), m_frameSize, pWorkBuffer-bytes.size(),
pCanvas, m_frameSize ); pCanvas, m_frameSize );
//int canvasDiff = memcmp(pCanvas, m_pC1PixelMaps[ frameIndex], m_frameSize);
//if (canvasDiff)
//{
// printf("Canvas is not correct - %d\n", canvasDiff);
//}
printf("frameSize = %d\n", frameSize); printf("frameSize = %d\n", frameSize);

View File

@ -34,7 +34,7 @@
// //
// Yes This is a 32K Buffer, of bytes, with no structure to it // Yes This is a 32K Buffer, of bytes, with no structure to it
// //
static unsigned char *pDictionary = nullptr; static unsigned char *pGlobalDictionary = nullptr;
struct DataString { struct DataString {
// Information about the data we're trying to match // Information about the data we're trying to match
@ -70,6 +70,7 @@ int LZB_Compress(unsigned char* pDest, unsigned char* pSource, int sourceSize)
sourceData.size = sourceSize; sourceData.size = sourceSize;
// Remember, this eventually will point at the frame buffer // Remember, this eventually will point at the frame buffer
pGlobalDictionary = pSource;
dictionaryData.pData = pSource; dictionaryData.pData = pSource;
dictionaryData.size = 0; dictionaryData.size = 0;
@ -129,7 +130,7 @@ int Old_LZB_Compress(unsigned char* pDest, unsigned char* pSource, int sourceSiz
// Initialize Dictionary // Initialize Dictionary
int bytesInDictionary = 0; // eventually add the ability to start with the dictionary filled int bytesInDictionary = 0; // eventually add the ability to start with the dictionary filled
pDictionary = pSource; pGlobalDictionary = pSource;
int processedBytes = 0; int processedBytes = 0;
int bytesEmitted = 0; int bytesEmitted = 0;
@ -259,7 +260,7 @@ static int AddDictionary(const DataString& data, int dictionarySize)
int dataIndex = 0; int dataIndex = 0;
while (dataIndex < data.size) while (dataIndex < data.size)
{ {
pDictionary[ dictionarySize++ ] = data.pData[ dataIndex++ ]; pGlobalDictionary[ dictionarySize++ ] = data.pData[ dataIndex++ ];
} }
//dictionarySize += data.size; //dictionarySize += data.size;
@ -508,7 +509,7 @@ static int DictionaryMatch(const DataString& data, int dictionarySize)
{ {
if( (0 == dictionarySize ) || if( (0 == dictionarySize ) ||
(0 == data.size) || (0 == data.size) ||
(data.size > 16384) ) // 16384 is largest string copy we can encode (data.size > MAX_STRING_SIZE) ) // 16384 is largest string copy we can encode
{ {
return -1; return -1;
} }
@ -531,7 +532,7 @@ static int DictionaryMatch(const DataString& data, int dictionarySize)
for (int dataIndex = 0; dataIndex < data.size; ++dataIndex) for (int dataIndex = 0; dataIndex < data.size; ++dataIndex)
{ {
if (data.pData[ dataIndex ] == pDictionary[ pattern_start + (dataIndex % pattern_size) ]) if (data.pData[ dataIndex ] == pGlobalDictionary[ pattern_start + (dataIndex % pattern_size) ])
continue; continue;
bMatch = false; bMatch = false;
@ -563,7 +564,7 @@ static int DictionaryMatch(const DataString& data, int dictionarySize)
bool bMatch = true; bool bMatch = true;
for (int dataIdx = 0; dataIdx < data.size; ++dataIdx) for (int dataIdx = 0; dataIdx < data.size; ++dataIdx)
{ {
if (data.pData[ dataIdx ] == pDictionary[ idx + dataIdx ]) if (data.pData[ dataIdx ] == pGlobalDictionary[ idx + dataIdx ])
continue; continue;
bMatch = false; bMatch = false;
@ -632,6 +633,8 @@ static int EmitLiteral(unsigned char *pDest, DataString& data)
unsigned short length = (unsigned short)data.size; unsigned short length = (unsigned short)data.size;
length -= 1; length -= 1;
assert(length < MAX_STRING_SIZE);
unsigned short opcode = length<<1; unsigned short opcode = length<<1;
opcode |= 0x0001; opcode |= 0x0001;
@ -662,6 +665,8 @@ static int EmitReference(unsigned char *pDest, int dictionaryOffset, DataString&
unsigned short length = (unsigned short)data.size; unsigned short length = (unsigned short)data.size;
length -= 1; length -= 1;
assert(length < MAX_STRING_SIZE);
unsigned short opcode = length<<1; unsigned short opcode = length<<1;
opcode |= 0x8000; opcode |= 0x8000;
@ -698,20 +703,36 @@ static void my_memcpy(u8* pDest, u8* pSrc, int length)
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// //
// Emit a Cursor Skip forward opcode // Emit one or more Cursor Skip forward opcode
// //
int EmitSkip(unsigned char* pDest, int skipSize) int EmitSkip(unsigned char* pDest, int skipSize)
{ {
int outSize = 2; int outSize = 0;
int thisSkip = 0;
unsigned short length = (unsigned short)skipSize; while (skipSize > 0)
length -= 1; {
outSize+=2;
unsigned short opcode = length<<1; thisSkip = skipSize;
opcode |= 0x8001; if (thisSkip > MAX_STRING_SIZE)
// Opcode out {
*pDest++ = (unsigned char)( opcode & 0xFF ); thisSkip = MAX_STRING_SIZE;
*pDest++ = (unsigned char)(( opcode>>8)&0xFF); }
skipSize -= thisSkip;
unsigned short length = (unsigned short)thisSkip;
length -= 1;
assert(length < MAX_STRING_SIZE);
unsigned short opcode = length<<1;
opcode |= 0x8001;
// Opcode out
*pDest++ = (unsigned char)( opcode & 0xFF );
*pDest++ = (unsigned char)(( opcode>>8)&0xFF);
}
return outSize; return outSize;
} }
@ -750,6 +771,8 @@ int CheckEmitSourceSkip(int checkSpace, unsigned char*& pDest, int space_left_in
return EmitSourceSkip(pDest, space_left_in_bank); return EmitSourceSkip(pDest, space_left_in_bank);
} }
space_left_in_bank -= checkSpace;
return space_left_in_bank; return space_left_in_bank;
} }
@ -768,6 +791,8 @@ int LZBA_Compress(unsigned char* pDest, unsigned char* pSource, int sourceSize,
{ {
// printf("LZBA Compress %d bytes\n", sourceSize); // printf("LZBA Compress %d bytes\n", sourceSize);
pGlobalDictionary = pDictionary;
// Used for bank skip opcode emission // Used for bank skip opcode emission
int bankOffset = (int)((pDest - pDataStart) & 0xFFFF); int bankOffset = (int)((pDest - pDataStart) & 0xFFFF);
@ -810,14 +835,14 @@ int LZBA_Compress(unsigned char* pDest, unsigned char* pSource, int sourceSize,
if (skipSize) if (skipSize)
{ {
space_left_in_bank = CheckEmitSourceSkip(2, pDest, space_left_in_bank); int numSkips = (skipSize / MAX_STRING_SIZE) + 1;
space_left_in_bank = CheckEmitSourceSkip(2 * numSkips, pDest, space_left_in_bank);
// We need to Skip // We need to Skip
pDest += EmitSkip(pDest, skipSize); pDest += EmitSkip(pDest, skipSize);
bLastEmitIsLiteral = false; bLastEmitIsLiteral = false;
lastEmittedCursorPosition = cursorPosition; lastEmittedCursorPosition = cursorPosition;
space_left_in_bank = (int)0x10000 - (int)((pDest - pDataStart)&0xFFFF);
} }
int tempCursorPosition = cursorPosition; int tempCursorPosition = cursorPosition;
@ -830,9 +855,9 @@ int LZBA_Compress(unsigned char* pDest, unsigned char* pSource, int sourceSize,
} }
else else
{ {
gapCount++; if (gapCount > 3)
if (gapCount >= 3)
break; break;
gapCount++;
} }
} }
@ -844,10 +869,17 @@ int LZBA_Compress(unsigned char* pDest, unsigned char* pSource, int sourceSize,
// it from one part of the frame buffer to another part) // it from one part of the frame buffer to another part)
sourceData.pData = &pSource[ cursorPosition ]; sourceData.pData = &pSource[ cursorPosition ];
sourceData.size = tempCursorPosition - cursorPosition + 1; sourceData.size = tempCursorPosition - cursorPosition;
cursorPosition = tempCursorPosition; //-------------------------- Dump, so skip dump only
space_left_in_bank = CheckEmitSourceSkip(2+sourceData.size, pDest, space_left_in_bank);
cursorPosition = AddDictionary(sourceData, cursorPosition);
pDest += EmitLiteral(pDest, sourceData);
lastEmittedCursorPosition = cursorPosition;
#if 0
while (sourceData.size > 0) while (sourceData.size > 0)
{ {
candidateData = LongestMatch(sourceData, dictionaryData, cursorPosition); candidateData = LongestMatch(sourceData, dictionaryData, cursorPosition);
@ -914,6 +946,7 @@ int LZBA_Compress(unsigned char* pDest, unsigned char* pSource, int sourceSize,
space_left_in_bank = (int)0x10000 - (int)((pDest - pDataStart)&0xFFFF); space_left_in_bank = (int)0x10000 - (int)((pDest - pDataStart)&0xFFFF);
} }
} }
#endif
} }
else else
{ {
@ -928,6 +961,14 @@ int LZBA_Compress(unsigned char* pDest, unsigned char* pSource, int sourceSize,
*pDest++ = 0x02; *pDest++ = 0x02;
*pDest++ = 0x00; *pDest++ = 0x00;
for (int idx = 0; idx < dictionarySize; ++idx)
{
if (pSource[ idx ] != pDictionary[ idx ])
{
assert(0);
}
}
return (int)(pDest - pOriginalDest); return (int)(pDest - pOriginalDest);
} }

View File

@ -15,7 +15,7 @@ int Old_LZB_Compress(unsigned char* pDest, unsigned char* pSource, int sourceSiz
// //
int LZBA_Compress(unsigned char* pDest, unsigned char* pSource, int sourceSize, int LZBA_Compress(unsigned char* pDest, unsigned char* pSource, int sourceSize,
unsigned char* pDataStart, unsigned char* pDictionary, unsigned char* pDataStart, unsigned char* pDictionary,
int dictionarySize=0); int dictionarySize);
#endif // LZB_H #endif // LZB_H