| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package org.tukaani.xz.rangecoder; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
|
| 10 |
public abstract class RangeDecoder extends RangeCoder { |
| 11 |
int range = 0; |
| 12 |
int code = 0; |
| 13 |
|
| 14 |
public abstract void normalize() throws IOException; |
| 15 |
|
| 16 |
public int decodeBit(short[] probs, int index) throws IOException { |
| 17 |
normalize(); |
| 18 |
|
| 19 |
int prob = probs[index]; |
| 20 |
int bound = (range >>> BIT_MODEL_TOTAL_BITS) * prob; |
| 21 |
int bit; |
| 22 |
|
| 23 |
if (Integer.compareUnsigned(code, bound) < 0) { |
| 24 |
range = bound; |
| 25 |
probs[index] = (short)( |
| 26 |
prob + ((BIT_MODEL_TOTAL - prob) >>> MOVE_BITS)); |
| 27 |
bit = 0; |
| 28 |
} else { |
| 29 |
range -= bound; |
| 30 |
code -= bound; |
| 31 |
probs[index] = (short)(prob - (prob >>> MOVE_BITS)); |
| 32 |
bit = 1; |
| 33 |
} |
| 34 |
|
| 35 |
return bit; |
| 36 |
} |
| 37 |
|
| 38 |
public int decodeBitTree(short[] probs) throws IOException { |
| 39 |
int symbol = 1; |
| 40 |
|
| 41 |
do { |
| 42 |
symbol = (symbol << 1) | decodeBit(probs, symbol); |
| 43 |
} while (symbol < probs.length); |
| 44 |
|
| 45 |
return symbol - probs.length; |
| 46 |
} |
| 47 |
|
| 48 |
public int decodeReverseBitTree(short[] probs) throws IOException { |
| 49 |
int symbol = 1; |
| 50 |
int i = 0; |
| 51 |
int result = 0; |
| 52 |
|
| 53 |
do { |
| 54 |
int bit = decodeBit(probs, symbol); |
| 55 |
symbol = (symbol << 1) | bit; |
| 56 |
result |= bit << i++; |
| 57 |
} while (symbol < probs.length); |
| 58 |
|
| 59 |
return result; |
| 60 |
} |
| 61 |
|
| 62 |
public int decodeDirectBits(int count) throws IOException { |
| 63 |
int result = 0; |
| 64 |
|
| 65 |
do { |
| 66 |
normalize(); |
| 67 |
|
| 68 |
range >>>= 1; |
| 69 |
int t = (code - range) >>> 31; |
| 70 |
code -= range & (t - 1); |
| 71 |
result = (result << 1) | (1 - t); |
| 72 |
} while (--count != 0); |
| 73 |
|
| 74 |
return result; |
| 75 |
} |
| 76 |
} |