Debian Codegraph @

src/org/tukaani/xz/rangecoder/RangeEncoder.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4 // SPDX-FileContributor: Igor Pavlov <https://7-zip.org/>
5
6 package org.tukaani.xz.rangecoder;
7
8 import java.io.IOException;
9
10 public abstract class RangeEncoder extends RangeCoder {
11 private static final int MOVE_REDUCING_BITS = 4;
12 private static final int BIT_PRICE_SHIFT_BITS = 4;
13
14 private static final int[] prices
15 = new int[BIT_MODEL_TOTAL >>> MOVE_REDUCING_BITS];
16
17 private long low;
18 private int range;
19
20 // NOTE: int is OK for LZMA2 because a compressed chunk
21 // is not more than 64 KiB, but with LZMA1 there is no chunking
22 // so in theory cacheSize can grow very big. To be very safe,
23 // use long instead of int since this code is used for LZMA1 too.
24 long cacheSize;
25 private byte cache;
26
27 static {
28 for (int i = (1 << MOVE_REDUCING_BITS) / 2; i < BIT_MODEL_TOTAL;
29 i += (1 << MOVE_REDUCING_BITS)) {
30 int w = i;
31 int bitCount = 0;
32
33 for (int j = 0; j < BIT_PRICE_SHIFT_BITS; ++j) {
34 w *= w;
35 bitCount <<= 1;
36
37 while ((w & 0xFFFF0000) != 0) {
38 w >>>= 1;
39 ++bitCount;
40 }
41 }
42
43 prices[i >> MOVE_REDUCING_BITS]
44 = (BIT_MODEL_TOTAL_BITS << BIT_PRICE_SHIFT_BITS)
45 - 15 - bitCount;
46 }
47 }
48
49 public void reset() {
50 low = 0;
51 range = 0xFFFFFFFF;
52 cache = 0x00;
53 cacheSize = 1;
54 }
55
56 public int getPendingSize() {
57 // This function is only needed by users of RangeEncoderToBuffer,
58 // but providing a must-be-never-called version here makes
59 // LZMAEncoder simpler.
60 throw new Error();
61 }
62
63 public int finish() throws IOException {
64 for (int i = 0; i < 5; ++i)
65 shiftLow();
66
67 // RangeEncoderToBuffer.finish() needs a return value to tell
68 // how big the finished buffer is. RangeEncoderToStream has no
69 // buffer and thus no return value is needed. Here we use a dummy
70 // value which can be overridden in RangeEncoderToBuffer.finish().
71 return -1;
72 }
73
74 abstract void writeByte(int b) throws IOException;
75
76 private void shiftLow() throws IOException {
77 int lowHi = (int)(low >>> 32);
78
79 if (lowHi != 0 || low < 0xFF000000L) {
80 int temp = cache;
81
82 do {
83 writeByte(temp + lowHi);
84 temp = 0xFF;
85 } while (--cacheSize != 0);
86
87 cache = (byte)(low >>> 24);
88 }
89
90 ++cacheSize;
91 low = (low & 0x00FFFFFF) << 8;
92 }
93
94 public void encodeBit(short[] probs, int index, int bit)
95 throws IOException {
96 int prob = probs[index];
97 int bound = (range >>> BIT_MODEL_TOTAL_BITS) * prob;
98
99 // NOTE: Any non-zero value for bit is taken as 1.
100 if (bit == 0) {
101 range = bound;
102 probs[index] = (short)(
103 prob + ((BIT_MODEL_TOTAL - prob) >>> MOVE_BITS));
104 } else {
105 low += bound & 0xFFFFFFFFL;
106 range -= bound;
107 probs[index] = (short)(prob - (prob >>> MOVE_BITS));
108 }
109
110 if ((range & TOP_MASK) == 0) {
111 range <<= SHIFT_BITS;
112 shiftLow();
113 }
114 }
115
116 public static int getBitPrice(int prob, int bit) {
117 // NOTE: Unlike in encodeBit(), here bit must be 0 or 1.
118 assert bit == 0 || bit == 1;
119 return prices[(prob ^ ((-bit) & (BIT_MODEL_TOTAL - 1)))
120 >>> MOVE_REDUCING_BITS];
121 }
122
123 public void encodeBitTree(short[] probs, int symbol) throws IOException {
124 int index = 1;
125 int mask = probs.length;
126
127 do {
128 mask >>>= 1;
129 int bit = symbol & mask;
130 encodeBit(probs, index, bit);
131
132 index <<= 1;
133 if (bit != 0)
134 index |= 1;
135
136 } while (mask != 1);
137 }
138
139 public static int getBitTreePrice(short[] probs, int symbol) {
140 int price = 0;
141 symbol |= probs.length;
142
143 do {
144 int bit = symbol & 1;
145 symbol >>>= 1;
146 price += getBitPrice(probs[symbol], bit);
147 } while (symbol != 1);
148
149 return price;
150 }
151
152 public void encodeReverseBitTree(short[] probs, int symbol)
153 throws IOException {
154 int index = 1;
155 symbol |= probs.length;
156
157 do {
158 int bit = symbol & 1;
159 symbol >>>= 1;
160 encodeBit(probs, index, bit);
161 index = (index << 1) | bit;
162 } while (symbol != 1);
163 }
164
165 public static int getReverseBitTreePrice(short[] probs, int symbol) {
166 int price = 0;
167 int index = 1;
168 symbol |= probs.length;
169
170 do {
171 int bit = symbol & 1;
172 symbol >>>= 1;
173 price += getBitPrice(probs[index], bit);
174 index = (index << 1) | bit;
175 } while (symbol != 1);
176
177 return price;
178 }
179
180 public void encodeDirectBits(int value, int count) throws IOException {
181 do {
182 range >>>= 1;
183 low += range & (0 - ((value >>> --count) & 1));
184
185 if ((range & TOP_MASK) == 0) {
186 range <<= SHIFT_BITS;
187 shiftLow();
188 }
189 } while (count != 0);
190 }
191
192 public static int getDirectBitsPrice(int count) {
193 return count << BIT_PRICE_SHIFT_BITS;
194 }
195 }