2008-07-25 07:31:37 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2008-07-25 07:31:37 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2008-07-25 07:31:37 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2008-07-25 07:31:37 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is distributed in the hope that it will be useful,
|
|
|
|
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.
|
2008-07-25 07:31:37 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2008-07-25 07:31:37 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
#include "qdata.h"
|
|
|
|
#include "inout.h"
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
byte *soundtrack;
|
|
|
|
char base[32];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============================================================================
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
WAV loading
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
===============================================================================
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
int rate;
|
|
|
|
int width;
|
|
|
|
int channels;
|
|
|
|
int loopstart;
|
|
|
|
int samples;
|
|
|
|
int dataofs; // chunk starts this many bytes from file start
|
2007-11-04 03:34:51 +00:00
|
|
|
} wavinfo_t;
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
byte *data_p;
|
|
|
|
byte *iff_end;
|
|
|
|
byte *last_chunk;
|
|
|
|
byte *iff_data;
|
|
|
|
int iff_chunk_len;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int samplecounts[0x10000];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
wavinfo_t wavinfo;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
short GetLittleShort( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
short val = 0;
|
|
|
|
val = *data_p;
|
2012-03-17 20:01:54 +00:00
|
|
|
val = val + ( *( data_p + 1 ) << 8 );
|
2007-11-04 03:34:51 +00:00
|
|
|
data_p += 2;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int GetLittleLong( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int val = 0;
|
|
|
|
val = *data_p;
|
2012-03-17 20:01:54 +00:00
|
|
|
val = val + ( *( data_p + 1 ) << 8 );
|
|
|
|
val = val + ( *( data_p + 2 ) << 16 );
|
|
|
|
val = val + ( *( data_p + 3 ) << 24 );
|
2007-11-04 03:34:51 +00:00
|
|
|
data_p += 4;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void FindNextChunk( char *name ){
|
|
|
|
while ( 1 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
data_p = last_chunk;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( data_p >= iff_end ) { // didn't find the chunk
|
2007-11-04 03:34:51 +00:00
|
|
|
data_p = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
data_p += 4;
|
|
|
|
iff_chunk_len = GetLittleLong();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( iff_chunk_len < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
data_p = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if (iff_chunk_len > 1024*1024)
|
|
|
|
// Sys_Error ("FindNextChunk: %i length is past the 1 meg sanity limit", iff_chunk_len);
|
|
|
|
data_p -= 8;
|
2012-03-17 20:01:54 +00:00
|
|
|
last_chunk = data_p + 8 + ( ( iff_chunk_len + 1 ) & ~1 );
|
|
|
|
if ( !strncmp( data_p, name, 4 ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void FindChunk( char *name ){
|
2007-11-04 03:34:51 +00:00
|
|
|
last_chunk = iff_data;
|
2012-03-17 20:01:54 +00:00
|
|
|
FindNextChunk( name );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void DumpChunks( void ){
|
|
|
|
char str[5];
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
str[4] = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
data_p = iff_data;
|
2007-11-04 03:34:51 +00:00
|
|
|
do
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( str, data_p, 4 );
|
2007-11-04 03:34:51 +00:00
|
|
|
data_p += 4;
|
|
|
|
iff_chunk_len = GetLittleLong();
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "0x%x : %s (%d)\n", (int)( data_p - 4 ), str, iff_chunk_len );
|
|
|
|
data_p += ( iff_chunk_len + 1 ) & ~1;
|
|
|
|
} while ( data_p < iff_end );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
GetWavinfo
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
wavinfo_t GetWavinfo( char *name, byte *wav, int wavlength ){
|
|
|
|
wavinfo_t info;
|
|
|
|
int i;
|
|
|
|
int format;
|
|
|
|
int samples;
|
|
|
|
|
|
|
|
memset( &info, 0, sizeof( info ) );
|
|
|
|
|
|
|
|
if ( !wav ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return info;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
iff_data = wav;
|
|
|
|
iff_end = wav + wavlength;
|
|
|
|
|
|
|
|
// find "RIFF" chunk
|
2012-03-17 20:01:54 +00:00
|
|
|
FindChunk( "RIFF" );
|
|
|
|
if ( !( data_p && !strncmp( data_p + 8, "WAVE", 4 ) ) ) {
|
|
|
|
printf( "Missing RIFF/WAVE chunks\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get "fmt " chunk
|
|
|
|
iff_data = data_p + 12;
|
|
|
|
// DumpChunks ();
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
FindChunk( "fmt " );
|
|
|
|
if ( !data_p ) {
|
|
|
|
printf( "Missing fmt chunk\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
data_p += 8;
|
|
|
|
format = GetLittleShort();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( format != 1 ) {
|
|
|
|
printf( "Microsoft PCM format only\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.channels = GetLittleShort();
|
|
|
|
info.rate = GetLittleLong();
|
2012-03-17 20:01:54 +00:00
|
|
|
data_p += 4 + 2;
|
2007-11-04 03:34:51 +00:00
|
|
|
info.width = GetLittleShort() / 8;
|
|
|
|
|
|
|
|
// get cue chunk
|
2012-03-17 20:01:54 +00:00
|
|
|
FindChunk( "cue " );
|
|
|
|
if ( data_p ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
data_p += 32;
|
|
|
|
info.loopstart = GetLittleLong();
|
|
|
|
// Com_Printf("loopstart=%d\n", sfx->loopstart);
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// if the next chunk is a LIST chunk, look for a cue length marker
|
|
|
|
FindNextChunk( "LIST" );
|
|
|
|
if ( data_p ) {
|
|
|
|
if ( !strncmp( data_p + 28, "mark", 4 ) ) { // this is not a proper parse, but it works with cooledit...
|
2007-11-04 03:34:51 +00:00
|
|
|
data_p += 24;
|
2012-03-17 20:01:54 +00:00
|
|
|
i = GetLittleLong(); // samples in loop
|
2007-11-04 03:34:51 +00:00
|
|
|
info.samples = info.loopstart + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
info.loopstart = -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// find data chunk
|
2012-03-17 20:01:54 +00:00
|
|
|
FindChunk( "data" );
|
|
|
|
if ( !data_p ) {
|
|
|
|
printf( "Missing data chunk\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_p += 4;
|
2012-03-17 20:01:54 +00:00
|
|
|
samples = GetLittleLong();
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( info.samples ) {
|
|
|
|
if ( samples < info.samples ) {
|
|
|
|
Error( "Sound %s has a bad loop length", name );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
info.samples = samples;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
info.dataofs = data_p - wav;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
LoadSoundtrack
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void LoadSoundtrack( void ){
|
|
|
|
char name[1024];
|
|
|
|
FILE *f;
|
|
|
|
int len;
|
|
|
|
int i, val, j;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
soundtrack = NULL;
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( name, "%svideo/%s/%s.wav", gamedir, base, base );
|
|
|
|
printf( "%s\n", name );
|
|
|
|
f = fopen( name, "rb" );
|
|
|
|
if ( !f ) {
|
|
|
|
printf( "no soundtrack for %s\n", base );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
len = Q_filelength( f );
|
|
|
|
soundtrack = malloc( len );
|
|
|
|
fread( soundtrack, 1, len, f );
|
|
|
|
fclose( f );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
wavinfo = GetWavinfo( name, soundtrack, len );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// count samples for compression
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( samplecounts, 0, sizeof( samplecounts ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
j = wavinfo.samples / 2;
|
|
|
|
for ( i = 0 ; i < j ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
val = ( (unsigned short *)( soundtrack + wavinfo.dataofs ) )[i];
|
2007-11-04 03:34:51 +00:00
|
|
|
samplecounts[val]++;
|
|
|
|
}
|
|
|
|
val = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 0x10000 ; i++ )
|
|
|
|
if ( samplecounts[i] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
val++;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "%i unique sample values\n", val );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
WriteSound
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void WriteSound( FILE *output, int frame ){
|
|
|
|
int start, end;
|
|
|
|
int count;
|
|
|
|
int empty = 0;
|
|
|
|
int i;
|
|
|
|
int sample;
|
|
|
|
int width;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
width = wavinfo.width * wavinfo.channels;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
start = frame * wavinfo.rate / 14;
|
|
|
|
end = ( frame + 1 ) * wavinfo.rate / 14;
|
2007-11-04 03:34:51 +00:00
|
|
|
count = end - start;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < count ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
sample = start + i;
|
|
|
|
if ( sample > wavinfo.samples || !soundtrack ) {
|
|
|
|
fwrite( &empty, 1, width, output );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
fwrite( soundtrack + wavinfo.dataofs + sample * width, 1, width,output );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
MTF
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
cblock_t MTF( cblock_t in ){
|
|
|
|
int i, j, b, code;
|
|
|
|
byte *out_p;
|
|
|
|
int index[256];
|
|
|
|
cblock_t out;
|
|
|
|
|
|
|
|
out_p = out.data = malloc( in.count + 4 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// write count
|
2012-03-17 20:01:54 +00:00
|
|
|
*out_p++ = in.count & 255;
|
|
|
|
*out_p++ = ( in.count >> 8 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 16 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 24 ) & 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 256 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
index[i] = i;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < in.count ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
b = in.data[i];
|
|
|
|
code = index[b];
|
|
|
|
*out_p++ = code;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// shuffle b indexes to 0
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < 256 ; j++ )
|
|
|
|
if ( index[j] < code ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
index[j]++;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
index[b] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.count = out_p - out.data;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int bwt_size;
|
|
|
|
byte *bwt_data;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int bwtCompare( const void *elem1, const void *elem2 ){
|
|
|
|
int i;
|
|
|
|
int i1, i2;
|
|
|
|
int b1, b2;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
i1 = *(int *)elem1;
|
|
|
|
i2 = *(int *)elem2;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < bwt_size ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
b1 = bwt_data[i1];
|
|
|
|
b2 = bwt_data[i2];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( b1 < b2 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( b1 > b2 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( ++i1 == bwt_size ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
i1 = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( ++i2 == bwt_size ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
i2 = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
BWT
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
cblock_t BWT( cblock_t in ){
|
|
|
|
int *sorted;
|
|
|
|
int i;
|
|
|
|
byte *out_p;
|
|
|
|
cblock_t out;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
bwt_size = in.count;
|
|
|
|
bwt_data = in.data;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
sorted = malloc( in.count * sizeof( *sorted ) );
|
|
|
|
for ( i = 0 ; i < in.count ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
sorted[i] = i;
|
2012-03-17 20:01:54 +00:00
|
|
|
qsort( sorted, in.count, sizeof( *sorted ), bwtCompare );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
out_p = out.data = malloc( in.count + 8 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// write count
|
2012-03-17 20:01:54 +00:00
|
|
|
*out_p++ = in.count & 255;
|
|
|
|
*out_p++ = ( in.count >> 8 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 16 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 24 ) & 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// write head index
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < in.count ; i++ )
|
|
|
|
if ( sorted[i] == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
*out_p++ = i & 255;
|
|
|
|
*out_p++ = ( i >> 8 ) & 255;
|
|
|
|
*out_p++ = ( i >> 16 ) & 255;
|
|
|
|
*out_p++ = ( i >> 24 ) & 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// write the L column
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < in.count ; i++ )
|
|
|
|
*out_p++ = in.data[( sorted[i] + in.count - 1 ) % in.count];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
free( sorted );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
out.count = out_p - out.data;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
typedef struct hnode_s
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
int count;
|
|
|
|
qboolean used;
|
|
|
|
int children[2];
|
2007-11-04 03:34:51 +00:00
|
|
|
} hnode_t;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int numhnodes;
|
|
|
|
hnode_t hnodes[512];
|
|
|
|
unsigned charbits[256];
|
|
|
|
int charbitscount[256];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int SmallestNode( void ){
|
|
|
|
int i;
|
|
|
|
int best, bestnode;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
best = 99999999;
|
|
|
|
bestnode = -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < numhnodes ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( hnodes[i].used ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( !hnodes[i].count ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( hnodes[i].count < best ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
best = hnodes[i].count;
|
|
|
|
bestnode = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bestnode == -1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
hnodes[bestnode].used = true;
|
|
|
|
return bestnode;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void BuildChars( int nodenum, unsigned bits, int bitcount ){
|
|
|
|
hnode_t *node;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( nodenum < 256 ) {
|
|
|
|
if ( bitcount > 32 ) {
|
|
|
|
Error( "bitcount > 32" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
charbits[nodenum] = bits;
|
|
|
|
charbitscount[nodenum] = bitcount;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = &hnodes[nodenum];
|
|
|
|
bits <<= 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
BuildChars( node->children[0], bits, bitcount + 1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
bits |= 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
BuildChars( node->children[1], bits, bitcount + 1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
Huffman
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
cblock_t Huffman( cblock_t in ){
|
|
|
|
int i;
|
|
|
|
hnode_t *node;
|
|
|
|
int outbits, c;
|
|
|
|
unsigned bits;
|
|
|
|
byte *out_p;
|
|
|
|
cblock_t out;
|
|
|
|
int max, maxchar;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// count
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( hnodes, 0, sizeof( hnodes ) );
|
|
|
|
for ( i = 0 ; i < in.count ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
hnodes[in.data[i]].count++;
|
|
|
|
|
|
|
|
// normalize counts
|
|
|
|
max = 0;
|
|
|
|
maxchar = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 256 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( hnodes[i].count > max ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
max = hnodes[i].count;
|
|
|
|
maxchar = i;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( max == 0 ) {
|
|
|
|
Error( "Huffman: max == 0" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 256 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
hnodes[i].count = ( hnodes[i].count * 255 + max - 1 ) / max;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// build the nodes
|
|
|
|
numhnodes = 256;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( numhnodes != 511 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
node = &hnodes[numhnodes];
|
|
|
|
|
|
|
|
// pick two lowest counts
|
2012-03-17 20:01:54 +00:00
|
|
|
node->children[0] = SmallestNode();
|
|
|
|
if ( node->children[0] == -1 ) {
|
|
|
|
break; // no more
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
node->children[1] = SmallestNode();
|
|
|
|
if ( node->children[1] == -1 ) {
|
|
|
|
if ( node->children[0] != numhnodes - 1 ) {
|
|
|
|
Error( "Bad smallestnode" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
node->count = hnodes[node->children[0]].count +
|
|
|
|
hnodes[node->children[1]].count;
|
2007-11-04 03:34:51 +00:00
|
|
|
numhnodes++;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
BuildChars( numhnodes - 1, 0, 0 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
out_p = out.data = malloc( in.count * 2 + 1024 );
|
|
|
|
memset( out_p, 0, in.count * 2 + 1024 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// write count
|
2012-03-17 20:01:54 +00:00
|
|
|
*out_p++ = in.count & 255;
|
|
|
|
*out_p++ = ( in.count >> 8 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 16 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 24 ) & 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// save out the 256 normalized counts so the tree can be recreated
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 256 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
*out_p++ = hnodes[i].count;
|
|
|
|
|
|
|
|
// write bits
|
|
|
|
outbits = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < in.count ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
c = charbitscount[in.data[i]];
|
|
|
|
bits = charbits[in.data[i]];
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( c )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
c--;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bits & ( 1 << c ) ) {
|
|
|
|
out_p[outbits >> 3] |= 1 << ( outbits & 7 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
outbits++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
out_p += ( outbits + 7 ) >> 3;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
out.count = out_p - out.data;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
RLE
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
#define RLE_CODE 0xe8
|
|
|
|
#define RLE_TRIPPLE 0xe9
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int rle_counts[256];
|
|
|
|
int rle_bytes[256];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
cblock_t RLE( cblock_t in ){
|
|
|
|
int i;
|
|
|
|
byte *out_p;
|
|
|
|
int val;
|
|
|
|
int repeat;
|
|
|
|
cblock_t out;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
out_p = out.data = malloc( in.count * 2 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// write count
|
2012-03-17 20:01:54 +00:00
|
|
|
*out_p++ = in.count & 255;
|
|
|
|
*out_p++ = ( in.count >> 8 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 16 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 24 ) & 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < in.count ; )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
val = in.data[i];
|
|
|
|
rle_bytes[val]++;
|
|
|
|
repeat = 1;
|
|
|
|
i++;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( i < in.count && repeat < 255 && in.data[i] == val )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
repeat++;
|
|
|
|
i++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( repeat < 256 ) {
|
|
|
|
rle_counts[repeat]++;
|
|
|
|
}
|
|
|
|
if ( repeat > 3 || val == RLE_CODE ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
*out_p++ = RLE_CODE;
|
|
|
|
*out_p++ = val;
|
|
|
|
*out_p++ = repeat;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( repeat-- )
|
2007-11-04 03:34:51 +00:00
|
|
|
*out_p++ = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out.count = out_p - out.data;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned lzss_head[256];
|
|
|
|
unsigned lzss_next[0x20000];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
LZSS
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
#define BACK_WINDOW 0x10000
|
|
|
|
#define BACK_BITS 16
|
|
|
|
#define FRONT_WINDOW 16
|
|
|
|
#define FRONT_BITS 4
|
|
|
|
cblock_t LZSS( cblock_t in ){
|
|
|
|
int i;
|
|
|
|
byte *out_p;
|
|
|
|
cblock_t out;
|
|
|
|
int val;
|
|
|
|
int j, start, max;
|
|
|
|
int bestlength, beststart;
|
|
|
|
int outbits;
|
|
|
|
|
|
|
|
if ( in.count >= sizeof( lzss_next ) / 4 ) {
|
|
|
|
Error( "LZSS: too big" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( lzss_head, -1, sizeof( lzss_head ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
out_p = out.data = malloc( in.count * 2 );
|
|
|
|
memset( out.data, 0, in.count * 2 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// write count
|
2012-03-17 20:01:54 +00:00
|
|
|
*out_p++ = in.count & 255;
|
|
|
|
*out_p++ = ( in.count >> 8 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 16 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 24 ) & 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
outbits = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < in.count ; )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
val = in.data[i];
|
|
|
|
#if 1
|
|
|
|
// chained search
|
|
|
|
bestlength = 0;
|
|
|
|
beststart = 0;
|
|
|
|
|
|
|
|
max = FRONT_WINDOW;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i + max > in.count ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
max = in.count - i;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
start = lzss_head[val];
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( start != -1 && start >= i - BACK_WINDOW )
|
|
|
|
{
|
2007-11-04 03:34:51 +00:00
|
|
|
// count match length
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < max ; j++ )
|
|
|
|
if ( in.data[start + j] != in.data[i + j] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( j > bestlength ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
bestlength = j;
|
|
|
|
beststart = start;
|
|
|
|
}
|
|
|
|
start = lzss_next[start];
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
// slow simple search
|
|
|
|
// search for a match
|
|
|
|
max = FRONT_WINDOW;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i + max > in.count ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
max = in.count - i;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
start = i - BACK_WINDOW;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( start < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
start = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
bestlength = 0;
|
|
|
|
beststart = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( ; start < i ; start++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( in.data[start] != val ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
// count match length
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < max ; j++ )
|
|
|
|
if ( in.data[start + j] != in.data[i + j] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( j > bestlength ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
bestlength = j;
|
|
|
|
beststart = start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
beststart = BACK_WINDOW - ( i - beststart );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bestlength < 3 ) { // output a single char
|
2007-11-04 03:34:51 +00:00
|
|
|
bestlength = 1;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
out_p[outbits >> 3] |= 1 << ( outbits & 7 ); // set bit to mark char
|
2007-11-04 03:34:51 +00:00
|
|
|
outbits++;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < 8 ; j++, outbits++ )
|
|
|
|
if ( val & ( 1 << j ) ) {
|
|
|
|
out_p[outbits >> 3] |= 1 << ( outbits & 7 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
else
|
2012-03-17 20:01:54 +00:00
|
|
|
{ // output a phrase
|
|
|
|
outbits++; // leave a 0 bit to mark phrase
|
|
|
|
for ( j = 0 ; j < BACK_BITS ; j++, outbits++ )
|
|
|
|
if ( beststart & ( 1 << j ) ) {
|
|
|
|
out_p[outbits >> 3] |= 1 << ( outbits & 7 );
|
|
|
|
}
|
|
|
|
for ( j = 0 ; j < FRONT_BITS ; j++, outbits++ )
|
|
|
|
if ( bestlength & ( 1 << j ) ) {
|
|
|
|
out_p[outbits >> 3] |= 1 << ( outbits & 7 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( bestlength-- )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
val = in.data[i];
|
|
|
|
lzss_next[i] = lzss_head[val];
|
|
|
|
lzss_head[val] = i;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
out_p += ( outbits + 7 ) >> 3;
|
2007-11-04 03:34:51 +00:00
|
|
|
out.count = out_p - out.data;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define MIN_REPT 15
|
|
|
|
#define MAX_REPT 0
|
|
|
|
#define HUF_TOKENS ( 256 + MAX_REPT )
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
unsigned charbits1[256][HUF_TOKENS];
|
|
|
|
int charbitscount1[256][HUF_TOKENS];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
hnode_t hnodes1[256][HUF_TOKENS * 2];
|
|
|
|
int numhnodes1[256];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int order0counts[256];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
SmallestNode1
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
int SmallestNode1( hnode_t *hnodes, int numhnodes ){
|
|
|
|
int i;
|
|
|
|
int best, bestnode;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
best = 99999999;
|
|
|
|
bestnode = -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < numhnodes ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( hnodes[i].used ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( !hnodes[i].count ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( hnodes[i].count < best ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
best = hnodes[i].count;
|
|
|
|
bestnode = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bestnode == -1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
hnodes[bestnode].used = true;
|
|
|
|
return bestnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
BuildChars1
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void BuildChars1( int prev, int nodenum, unsigned bits, int bitcount ){
|
|
|
|
hnode_t *node;
|
|
|
|
|
|
|
|
if ( nodenum < HUF_TOKENS ) {
|
|
|
|
if ( bitcount > 32 ) {
|
|
|
|
Error( "bitcount > 32" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
charbits1[prev][nodenum] = bits;
|
|
|
|
charbitscount1[prev][nodenum] = bitcount;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = &hnodes1[prev][nodenum];
|
|
|
|
bits <<= 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
BuildChars1( prev, node->children[0], bits, bitcount + 1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
bits |= 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
BuildChars1( prev, node->children[1], bits, bitcount + 1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
BuildTree1
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void BuildTree1( int prev ){
|
|
|
|
hnode_t *node, *nodebase;
|
|
|
|
int numhnodes;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// build the nodes
|
|
|
|
numhnodes = HUF_TOKENS;
|
|
|
|
nodebase = hnodes1[prev];
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( 1 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
node = &nodebase[numhnodes];
|
|
|
|
|
|
|
|
// pick two lowest counts
|
2012-03-17 20:01:54 +00:00
|
|
|
node->children[0] = SmallestNode1( nodebase, numhnodes );
|
|
|
|
if ( node->children[0] == -1 ) {
|
|
|
|
break; // no more
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
node->children[1] = SmallestNode1( nodebase, numhnodes );
|
|
|
|
if ( node->children[1] == -1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
node->count = nodebase[node->children[0]].count +
|
|
|
|
nodebase[node->children[1]].count;
|
2007-11-04 03:34:51 +00:00
|
|
|
numhnodes++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
numhnodes1[prev] = numhnodes - 1;
|
|
|
|
BuildChars1( prev, numhnodes - 1, 0, 0 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
Huffman1_Count
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void Huffman1_Count( cblock_t in ){
|
|
|
|
int i;
|
|
|
|
int prev;
|
|
|
|
int v;
|
|
|
|
int rept;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
prev = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < in.count ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
v = in.data[i];
|
|
|
|
order0counts[v]++;
|
|
|
|
hnodes1[prev][v].count++;
|
|
|
|
prev = v;
|
|
|
|
#if 1
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( rept = 1 ; i + rept < in.count && rept < MAX_REPT ; rept++ )
|
|
|
|
if ( in.data[i + rept] != v ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( rept > MIN_REPT ) {
|
|
|
|
hnodes1[prev][255 + rept].count++;
|
|
|
|
i += rept - 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
Huffman1_Build
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
byte scaled[256][HUF_TOKENS];
|
|
|
|
void Huffman1_Build( FILE *f ){
|
|
|
|
int i, j, v;
|
|
|
|
int max;
|
|
|
|
int total;
|
|
|
|
|
|
|
|
for ( i = 0 ; i < 256 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
// normalize and save the counts
|
|
|
|
max = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < HUF_TOKENS ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( hnodes1[i][j].count > max ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
max = hnodes1[i][j].count;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( max == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
max = 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
total = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < HUF_TOKENS ; j++ )
|
|
|
|
{ // easy to overflow 32 bits here!
|
|
|
|
v = ( hnodes1[i][j].count * (double)255 + max - 1 ) / max;
|
|
|
|
if ( v > 255 ) {
|
|
|
|
Error( "v > 255" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
scaled[i][j] = hnodes1[i][j].count = v;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( v ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
total++;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( total == 1 ) { // must have two tokens
|
|
|
|
if ( !scaled[i][0] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
scaled[i][0] = hnodes1[i][0].count = 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
scaled[i][1] = hnodes1[i][1].count = 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
BuildTree1( i );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// count up the total bits
|
|
|
|
total = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 256 ; i++ )
|
|
|
|
for ( j = 0 ; j < 256 ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
total += charbitscount1[i][j] * hnodes1[i][j].count;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
total = ( total + 7 ) / 8;
|
|
|
|
printf( "%i bytes huffman1 compressed\n", total );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fwrite( scaled, 1, sizeof( scaled ), f );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
Huffman1
|
|
|
|
|
|
|
|
Order 1 compression with pre-built table
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
cblock_t Huffman1( cblock_t in ){
|
|
|
|
int i;
|
|
|
|
int outbits, c;
|
|
|
|
unsigned bits;
|
|
|
|
byte *out_p;
|
|
|
|
cblock_t out;
|
|
|
|
int prev;
|
|
|
|
int v;
|
|
|
|
int rept;
|
|
|
|
|
|
|
|
out_p = out.data = malloc( in.count * 2 + 1024 );
|
|
|
|
memset( out_p, 0, in.count * 2 + 1024 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// write count
|
2012-03-17 20:01:54 +00:00
|
|
|
*out_p++ = in.count & 255;
|
|
|
|
*out_p++ = ( in.count >> 8 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 16 ) & 255;
|
|
|
|
*out_p++ = ( in.count >> 24 ) & 255;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// write bits
|
|
|
|
outbits = 0;
|
|
|
|
prev = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < in.count ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
v = in.data[i];
|
|
|
|
|
|
|
|
c = charbitscount1[prev][v];
|
|
|
|
bits = charbits1[prev][v];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !c ) {
|
|
|
|
Error( "!bits" );
|
|
|
|
}
|
|
|
|
while ( c )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
c--;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bits & ( 1 << c ) ) {
|
|
|
|
out_p[outbits >> 3] |= 1 << ( outbits & 7 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
outbits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = v;
|
|
|
|
#if 1
|
|
|
|
// check for repeat encodes
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( rept = 1 ; i + rept < in.count && rept < MAX_REPT ; rept++ )
|
|
|
|
if ( in.data[i + rept] != v ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( rept > MIN_REPT ) {
|
|
|
|
c = charbitscount1[prev][255 + rept];
|
|
|
|
bits = charbits1[prev][255 + rept];
|
|
|
|
if ( !c ) {
|
|
|
|
Error( "!bits" );
|
|
|
|
}
|
|
|
|
while ( c )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
c--;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bits & ( 1 << c ) ) {
|
|
|
|
out_p[outbits >> 3] |= 1 << ( outbits & 7 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
outbits++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
i += rept - 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
out_p += ( outbits + 7 ) >> 3;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
out.count = out_p - out.data;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===================
|
|
|
|
LoadFrame
|
|
|
|
===================
|
|
|
|
*/
|
|
|
|
cblock_t LoadFrame( char *base, int frame, int digits, byte **palette ){
|
|
|
|
int ten3, ten2, ten1, ten0;
|
|
|
|
cblock_t in;
|
|
|
|
int width, height;
|
|
|
|
char name[1024];
|
|
|
|
FILE *f;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
in.data = NULL;
|
|
|
|
in.count = -1;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
ten3 = frame / 1000;
|
|
|
|
ten2 = ( frame - ten3 * 1000 ) / 100;
|
|
|
|
ten1 = ( frame - ten3 * 1000 - ten2 * 100 ) / 10;
|
|
|
|
ten0 = frame % 10;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( digits == 4 ) {
|
|
|
|
sprintf( name, "%svideo/%s/%s%i%i%i%i.pcx", gamedir, base, base, ten3, ten2, ten1, ten0 );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
sprintf( name, "%svideo/%s/%s%i%i%i.pcx", gamedir, base, base, ten2, ten1, ten0 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
f = fopen( name, "rb" );
|
|
|
|
if ( !f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
in.data = NULL;
|
|
|
|
return in;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
fclose( f );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "%s\n", name );
|
|
|
|
Load256Image( name, &in.data, palette, &width, &height );
|
|
|
|
in.count = width * height;
|
2007-11-04 03:34:51 +00:00
|
|
|
// FIXME: map 0 and 255!
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// rle compress
|
2012-03-17 20:01:54 +00:00
|
|
|
rle = RLE( in );
|
|
|
|
free( in.data );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
return rle;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
Cmd_Video
|
|
|
|
|
|
|
|
video <directory> <framedigits>
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void Cmd_Video( void ){
|
|
|
|
char savename[1024];
|
|
|
|
char name[1024];
|
|
|
|
FILE *output;
|
|
|
|
int startframe, frame;
|
|
|
|
byte *palette;
|
|
|
|
int width, height;
|
|
|
|
byte current_palette[768];
|
|
|
|
int command;
|
|
|
|
int i;
|
|
|
|
int digits;
|
|
|
|
cblock_t in, huffman;
|
|
|
|
int swap;
|
|
|
|
|
|
|
|
|
|
|
|
GetToken( false );
|
|
|
|
strcpy( base, token );
|
|
|
|
if ( g_release ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
// sprintf (savename, "video/%s.cin", token);
|
|
|
|
// ReleaseFile (savename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GetToken( false );
|
|
|
|
digits = atoi( token );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// optionally skip frames
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( TokenAvailable() ) {
|
|
|
|
GetToken( false );
|
|
|
|
startframe = atoi( token );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
startframe = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( savename, "%svideo/%s.cin", gamedir, base );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
// clear stuff
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( charbits1, 0, sizeof( charbits1 ) );
|
|
|
|
memset( charbitscount1, 0, sizeof( charbitscount1 ) );
|
|
|
|
memset( hnodes1, 0, sizeof( hnodes1 ) );
|
|
|
|
memset( numhnodes1, 0, sizeof( numhnodes1 ) );
|
|
|
|
memset( order0counts, 0, sizeof( order0counts ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
// load the entire sound wav file if present
|
2012-03-17 20:01:54 +00:00
|
|
|
LoadSoundtrack();
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( digits == 4 ) {
|
|
|
|
sprintf( name, "%svideo/%s/%s0000.pcx", gamedir, base, base );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
sprintf( name, "%svideo/%s/%s000.pcx", gamedir, base, base );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "%s\n", name );
|
|
|
|
Load256Image( name, NULL, &palette, &width, &height );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
output = fopen( savename, "wb" );
|
|
|
|
if ( !output ) {
|
|
|
|
Error( "Can't open %s", savename );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// write header info
|
2012-03-17 20:01:54 +00:00
|
|
|
i = LittleLong( width );
|
|
|
|
fwrite( &i, 4, 1, output );
|
|
|
|
i = LittleLong( height );
|
|
|
|
fwrite( &i, 4, 1, output );
|
|
|
|
i = LittleLong( wavinfo.rate );
|
|
|
|
fwrite( &i, 4, 1, output );
|
|
|
|
i = LittleLong( wavinfo.width );
|
|
|
|
fwrite( &i, 4, 1, output );
|
|
|
|
i = LittleLong( wavinfo.channels );
|
|
|
|
fwrite( &i, 4, 1, output );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// build the dictionary
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( frame = startframe ; ; frame++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "counting ", frame );
|
|
|
|
in = LoadFrame( base, frame, digits, &palette );
|
|
|
|
if ( !in.data ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
Huffman1_Count( in );
|
|
|
|
free( in.data );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// build nodes and write counts
|
2012-03-17 20:01:54 +00:00
|
|
|
Huffman1_Build( output );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( current_palette, 0, sizeof( current_palette ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// compress it with the dictionary
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( frame = startframe ; ; frame++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "packing ", frame );
|
|
|
|
in = LoadFrame( base, frame, digits, &palette );
|
|
|
|
if ( !in.data ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// see if the palette has changed
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < 768 ; i++ )
|
|
|
|
if ( palette[i] != current_palette[i] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
// write a palette change
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( current_palette, palette, sizeof( current_palette ) );
|
|
|
|
command = LittleLong( 1 );
|
|
|
|
fwrite( &command, 1, 4, output );
|
|
|
|
fwrite( current_palette, 1, sizeof( current_palette ), output );
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i == 768 ) {
|
|
|
|
command = 0; // no palette change
|
|
|
|
fwrite( &command, 1, 4, output );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save the image
|
2012-03-17 20:01:54 +00:00
|
|
|
huffman = Huffman1( in );
|
|
|
|
printf( "%5i bytes after huffman1\n", huffman.count );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
swap = LittleLong( huffman.count );
|
|
|
|
fwrite( &swap, 1, sizeof( swap ), output );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fwrite( huffman.data, 1, huffman.count, output );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// save some sound samples
|
2012-03-17 20:01:54 +00:00
|
|
|
WriteSound( output, frame );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
free( palette );
|
|
|
|
free( in.data );
|
|
|
|
free( huffman.data );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// write end-of-file command
|
|
|
|
command = 2;
|
2012-03-17 20:01:54 +00:00
|
|
|
fwrite( &command, 1, 4, output );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Total size: %i\n", ftell( output ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fclose( output );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( soundtrack ) {
|
|
|
|
free( soundtrack );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|