| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.util; |
| 7 |
|
| 8 |
import java.nio.ByteBuffer; |
| 9 |
import java.util.Arrays; |
| 10 |
|
| 11 |
/** |
| 12 |
* An ANS (Asymmetric Numeral Systems) compression tool. |
| 13 |
* It uses the range variant. |
| 14 |
*/ |
| 15 |
public class AnsCompression { |
| 16 |
|
| 17 |
private static final long TOP = 1L << 24; |
| 18 |
private static final int SHIFT = 12; |
| 19 |
private static final int MASK = (1 << SHIFT) - 1; |
| 20 |
private static final long MAX = (TOP >> SHIFT) << 32; |
| 21 |
|
| 22 |
private AnsCompression() { |
| 23 |
// a utility class |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Count the frequencies of codes in the data, and increment the target |
| 28 |
* frequency table. |
| 29 |
* |
| 30 |
* @param freq the target frequency table |
| 31 |
* @param data the data |
| 32 |
*/ |
| 33 |
public static void countFrequencies(int[] freq, byte[] data) { |
| 34 |
for (byte x : data) { |
| 35 |
freq[x & 0xff]++; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Scale the frequencies to a new total. Frequencies of 0 are kept as 0; |
| 41 |
* larger frequencies result in at least 1. |
| 42 |
* |
| 43 |
* @param freq the (source and target) frequency table |
| 44 |
* @param total the target total (sum of all frequencies) |
| 45 |
*/ |
| 46 |
public static void scaleFrequencies(int[] freq, int total) { |
| 47 |
int len = freq.length, sum = 0; |
| 48 |
for (int x : freq) { |
| 49 |
sum += x; |
| 50 |
} |
| 51 |
// the list of: (error << 8) + index |
| 52 |
int[] errors = new int[len]; |
| 53 |
int totalError = -total; |
| 54 |
for (int i = 0; i < len; i++) { |
| 55 |
int old = freq[i]; |
| 56 |
if (old == 0) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
int ideal = (int) (old * total * 256L / sum); |
| 60 |
// 1 too high so we can decrement if needed |
| 61 |
int x = 1 + ideal / 256; |
| 62 |
freq[i] = x; |
| 63 |
totalError += x; |
| 64 |
errors[i] = ((x * 256 - ideal) << 8) + i; |
| 65 |
} |
| 66 |
// we don't need to sort, we could just calculate |
| 67 |
// which one is the nth element - but sorting is simpler |
| 68 |
Arrays.sort(errors); |
| 69 |
if (totalError < 0) { |
| 70 |
// integer overflow |
| 71 |
throw new IllegalArgumentException(); |
| 72 |
} |
| 73 |
while (totalError > 0) { |
| 74 |
for (int i = 0; totalError > 0 && i < len; i++) { |
| 75 |
int index = errors[i] & 0xff; |
| 76 |
if (freq[index] > 1) { |
| 77 |
freq[index]--; |
| 78 |
totalError--; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Generate the cumulative frequency table. |
| 86 |
* |
| 87 |
* @param freq the source frequency table |
| 88 |
* @return the cumulative table, with one entry more |
| 89 |
*/ |
| 90 |
static int[] generateCumulativeFrequencies(int[] freq) { |
| 91 |
int len = freq.length; |
| 92 |
int[] cumulativeFreq = new int[len + 1]; |
| 93 |
for (int i = 0, x = 0; i < len; i++) { |
| 94 |
x += freq[i]; |
| 95 |
cumulativeFreq[i + 1] = x; |
| 96 |
} |
| 97 |
return cumulativeFreq; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Generate the frequency-to-code table. |
| 102 |
* |
| 103 |
* @param cumulativeFreq the cumulative frequency table |
| 104 |
* @return the result |
| 105 |
*/ |
| 106 |
private static byte[] generateFrequencyToCode(int[] cumulativeFreq) { |
| 107 |
byte[] freqToCode = new byte[1 << SHIFT]; |
| 108 |
int x = 0; |
| 109 |
byte s = -1; |
| 110 |
for (int i : cumulativeFreq) { |
| 111 |
while (x < i) { |
| 112 |
freqToCode[x++] = s; |
| 113 |
} |
| 114 |
s++; |
| 115 |
} |
| 116 |
return freqToCode; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Encode the data. |
| 121 |
* |
| 122 |
* @param freq the frequency table (will be scaled) |
| 123 |
* @param data the source data (uncompressed) |
| 124 |
* @return the compressed data |
| 125 |
*/ |
| 126 |
public static byte[] encode(int[] freq, byte[] data) { |
| 127 |
scaleFrequencies(freq, 1 << SHIFT); |
| 128 |
int[] cumulativeFreq = generateCumulativeFrequencies(freq); |
| 129 |
ByteBuffer buff = ByteBuffer.allocate(data.length * 2); |
| 130 |
buff = encode(data, freq, cumulativeFreq, buff); |
| 131 |
return Arrays.copyOfRange(buff.array(), |
| 132 |
buff.arrayOffset() + buff.position(), buff.arrayOffset() + buff.limit()); |
| 133 |
} |
| 134 |
|
| 135 |
private static ByteBuffer encode(byte[] data, int[] freq, |
| 136 |
int[] cumulativeFreq, ByteBuffer buff) { |
| 137 |
long state = TOP; |
| 138 |
// encoding happens backwards |
| 139 |
int b = buff.limit(); |
| 140 |
for (int p = data.length - 1; p >= 0; p--) { |
| 141 |
int x = data[p] & 0xff; |
| 142 |
int f = freq[x]; |
| 143 |
while (state >= MAX * f) { |
| 144 |
b -= 4; |
| 145 |
buff.putInt(b, (int) state); |
| 146 |
state >>>= 32; |
| 147 |
} |
| 148 |
state = ((state / f) << SHIFT) + (state % f) + cumulativeFreq[x]; |
| 149 |
} |
| 150 |
b -= 8; |
| 151 |
buff.putLong(b, state); |
| 152 |
buff.position(b); |
| 153 |
return buff.slice(); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Decode the data. |
| 158 |
* |
| 159 |
* @param freq the frequency table (will be scaled) |
| 160 |
* @param data the compressed data |
| 161 |
* @param length the target length |
| 162 |
* @return the uncompressed result |
| 163 |
*/ |
| 164 |
public static byte[] decode(int[] freq, byte[] data, int length) { |
| 165 |
scaleFrequencies(freq, 1 << SHIFT); |
| 166 |
int[] cumulativeFreq = generateCumulativeFrequencies(freq); |
| 167 |
byte[] freqToCode = generateFrequencyToCode(cumulativeFreq); |
| 168 |
byte[] out = new byte[length]; |
| 169 |
decode(data, freq, cumulativeFreq, freqToCode, out); |
| 170 |
return out; |
| 171 |
} |
| 172 |
|
| 173 |
private static void decode(byte[] data, int[] freq, int[] cumulativeFreq, |
| 174 |
byte[] freqToCode, byte[] out) { |
| 175 |
ByteBuffer buff = ByteBuffer.wrap(data); |
| 176 |
long state = buff.getLong(); |
| 177 |
for (int i = 0, size = out.length; i < size; i++) { |
| 178 |
int x = (int) state & MASK; |
| 179 |
int c = freqToCode[x] & 0xff; |
| 180 |
out[i] = (byte) c; |
| 181 |
state = (freq[c] * (state >> SHIFT)) + x - cumulativeFreq[c]; |
| 182 |
while (state < TOP) { |
| 183 |
state = (state << 32) | (buff.getInt() & 0xffffffffL); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
} |