compress.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
#pragma prototyped
/*-------------------------------------------------------------*/
/*--- Compression machinery (not incl block sorting) ---*/
/*--- compress.c ---*/
/*-------------------------------------------------------------*/
/*--
library for lossless, block-sorting data compression.
Copyright (C) 1996-1998 Julian R Seward. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
3. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
4. The name of the author may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Julian Seward, Guildford, Surrey, UK.
jseward@acm.org
This program is based on (at least) the work of:
Mike Burrows
David Wheeler
Peter Fenwick
Alistair Moffat
Radford Neal
Ian H. Witten
Robert Sedgewick
Jon L. Bentley
For more information on these sources, see the manual.
--*/
/*--
CHANGES
~~~~~~~
0.9.0 -- original version.
0.9.0a/b -- no changes in this file.
0.9.0c
* changed setting of nGroups in sendMTFValues() so as to
do a bit better on small files
--*/
#include "bzhdr.h"
/*---------------------------------------------------*/
/*--- Bit stream I/O ---*/
/*---------------------------------------------------*/
/*---------------------------------------------------*/
void bsInitWrite ( EState* s )
{
s->bsLive = 0;
s->bsBuff = 0;
}
/*---------------------------------------------------*/
static
void bsFinishWrite ( EState* s )
{
while (s->bsLive > 0) {
s->numZ++;
s->bsBuff <<= 8;
s->bsLive -= 8;
}
}
/*---------------------------------------------------*/
{ \
while (s->bsLive >= 8) { \
s->numZ++; \
s->bsBuff <<= 8; \
s->bsLive -= 8; \
} \
}
/*---------------------------------------------------*/
static
{
bsNEEDW ( n );
s->bsLive += n;
}
/*---------------------------------------------------*/
static
{
}
/*---------------------------------------------------*/
static
{
}
/*---------------------------------------------------*/
/*--- The back end proper ---*/
/*---------------------------------------------------*/
/*---------------------------------------------------*/
static
void makeMaps_e ( EState* s )
{
Int32 i;
s->nInUse = 0;
for (i = 0; i < 256; i++)
if (s->inUse[i]) {
s->unseqToSeq[i] = s->nInUse;
s->nInUse++;
}
}
/*---------------------------------------------------*/
static
void generateMTFValues ( EState* s )
{
Int32 i, j;
makeMaps_e ( s );
wr = 0;
zPend = 0;
for (i = 0; i < s->nblock; i++) {
j = 0;
j++;
};
if (j == 0) {
zPend++;
} else {
if (zPend > 0) {
zPend--;
while (True) {
switch (zPend % 2) {
};
if (zPend < 2) break;
};
zPend = 0;
}
}
}
if (zPend > 0) {
zPend--;
while (True) {
switch (zPend % 2) {
};
if (zPend < 2) break;
};
}
}
/*---------------------------------------------------*/
#define BZ_LESSER_ICOST 0
#define BZ_GREATER_ICOST 15
static
void sendMTFValues ( EState* s )
{
/*--
UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
is a global since the decoder also needs it.
Int32 code[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
Int32 rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
are also globals only used in this proc.
Made global to keep stack frame size small.
--*/
if (s->verbosity >= 3)
VPrintf3( " %d in block, %d after MTF & 1-2 coding, "
"%d+2 syms in use\n",
for (t = 0; t < BZ_N_GROUPS; t++)
for (v = 0; v < alphaSize; v++)
s->len[t][v] = BZ_GREATER_ICOST;
/*--- Decide how many coding tables to use ---*/
nGroups = 6;
/*--- Generate an initial set of coding tables ---*/
{
gs = 0;
while (nPart > 0) {
aFreq = 0;
ge++;
}
ge--;
}
if (s->verbosity >= 3)
VPrintf5( " initial group %d, [%d .. %d], "
"has %d syms (%4.1f%%)\n",
for (v = 0; v < alphaSize; v++)
nPart--;
}
}
/*---
Iterate up to BZ_N_ITERS times to improve the tables.
---*/
for (t = 0; t < nGroups; t++)
for (v = 0; v < alphaSize; v++)
s->rfreq[t][v] = 0;
nSelectors = 0;
totc = 0;
gs = 0;
while (True) {
/*--- Set group start & end marks. --*/
/*--
Calculate the cost of this group as coded
by each of the coding tables.
--*/
if (nGroups == 6) {
}
} else {
}
}
/*--
Find the coding table which is best for this group,
and record its identity in the selector table.
--*/
for (t = 0; t < nGroups; t++)
nSelectors++;
/*--
Increment the symbol frequencies for the selected table.
--*/
}
if (s->verbosity >= 3) {
VPrintf2 ( " pass %d: size is %d, grp uses are ",
for (t = 0; t < nGroups; t++)
VPrintf0 ( "\n" );
}
/*--
Recompute the tables based on the accumulated frequencies.
--*/
for (t = 0; t < nGroups; t++)
alphaSize, 20 );
}
3003 );
/*--- Compute MTF values for the selectors. ---*/
{
for (i = 0; i < nSelectors; i++) {
j = 0;
j++;
};
s->selectorMtf[i] = j;
}
};
/*--- Assign actual codes for the tables. --*/
for (t = 0; t < nGroups; t++) {
minLen = 32;
maxLen = 0;
for (i = 0; i < alphaSize; i++) {
}
}
/*--- Transmit the mapping table. ---*/
{
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++)
}
for (i = 0; i < 16; i++)
for (i = 0; i < 16; i++)
if (inUse16[i])
for (j = 0; j < 16; j++) {
}
if (s->verbosity >= 3)
}
/*--- Now the selectors. ---*/
for (i = 0; i < nSelectors; i++) {
bsW(s,1,0);
}
if (s->verbosity >= 3)
/*--- Now the coding tables. ---*/
for (t = 0; t < nGroups; t++) {
for (i = 0; i < alphaSize; i++) {
bsW ( s, 1, 0 );
}
}
if (s->verbosity >= 3)
/*--- And finally, the block data proper ---*/
selCtr = 0;
gs = 0;
while (True) {
bsW ( s,
}
selCtr++;
}
if (s->verbosity >= 3)
}
/*---------------------------------------------------*/
{
if (s->nblock > 0) {
BZ_FINALISE_CRC ( s->blockCRC );
s->combinedCRC ^= s->blockCRC;
if (s->verbosity >= 2)
VPrintf4( " block %d: crc = 0x%8x, "
"combined CRC = 0x%8x, size = %d\n",
blockSort ( s );
}
/*-- If this is the first block, create the stream header. --*/
if (s->blockNo == 1) {
bsInitWrite ( s );
bsPutUChar ( s, 'B' );
bsPutUChar ( s, 'Z' );
bsPutUChar ( s, 'h' );
}
if (s->nblock > 0) {
/*-- Now the block's CRC, so it is in a known place. --*/
bsPutUInt32 ( s, s->blockCRC );
/*-- Now a single bit indicating randomisation. --*/
if (s->blockRandomised) {
} else
bsW(s,1,0);
generateMTFValues ( s );
sendMTFValues ( s );
}
/*-- If this is the last block, add the stream trailer. --*/
if (is_last_block) {
VPrintf2 ( " %d block%s needed randomisation\n",
bsPutUInt32 ( s, s->combinedCRC );
if (s->verbosity >= 2)
bsFinishWrite ( s );
}
}
/*-------------------------------------------------------------*/
/*--- end compress.c ---*/
/*-------------------------------------------------------------*/