11
11
import java .nio .ByteOrder ;
12
12
13
13
public class RANS4x8Encode extends RANSEncode <RANS4x8Params > {
14
- private static final int ORDER_BYTE_LENGTH = 1 ;
15
- private static final int COMPRESSED_BYTE_LENGTH = 4 ;
16
- private static final int RAW_BYTE_LENGTH = 4 ;
17
- private static final int PREFIX_BYTE_LENGTH = ORDER_BYTE_LENGTH + COMPRESSED_BYTE_LENGTH + RAW_BYTE_LENGTH ;
18
14
19
15
// streams smaller than this value don't have sufficient symbol context for ORDER-1 encoding,
20
16
// so always use ORDER-0
21
17
private static final int MINIMUM__ORDER_1_SIZE = 4 ;
22
18
private static final ByteBuffer EMPTY_BUFFER = ByteBuffer .allocate (0 );
23
19
24
-
25
20
public ByteBuffer compress (final ByteBuffer inBuffer , final RANS4x8Params params ) {
26
21
if (inBuffer .remaining () == 0 ) {
27
22
return EMPTY_BUFFER ;
@@ -45,23 +40,22 @@ public ByteBuffer compress(final ByteBuffer inBuffer, final RANS4x8Params params
45
40
}
46
41
47
42
private ByteBuffer compressOrder0Way4 (final ByteBuffer inBuffer ) {
48
- final int inSize = inBuffer .remaining ();
49
- final ByteBuffer outBuffer = allocateOutputBuffer (inSize );
43
+ final int inputSize = inBuffer .remaining ();
44
+ final ByteBuffer outBuffer = allocateOutputBuffer (inputSize );
50
45
51
46
// move the output buffer ahead to the start of the frequency table (we'll come back and
52
47
// write the output stream prefix at the end of this method)
53
- outBuffer .position (PREFIX_BYTE_LENGTH ); // start of frequency table
48
+ outBuffer .position (Constants . RANS_4x8_PREFIX_BYTE_LENGTH ); // start of frequency table
54
49
55
50
// get the normalised frequencies of the alphabets
56
- final int [] F = calcFrequenciesOrder0 (inBuffer );
51
+ final int [] normalizedFreq = calcFrequenciesOrder0 (inBuffer );
57
52
58
53
// using the normalised frequencies, set the RANSEncodingSymbols
59
- buildSymsOrder0 (F );
60
-
54
+ buildSymsOrder0 (normalizedFreq );
61
55
final ByteBuffer cp = outBuffer .slice ();
62
56
63
57
// write Frequency table
64
- final int frequencyTableSize = writeFrequenciesOrder0 (cp , F );
58
+ final int frequencyTableSize = writeFrequenciesOrder0 (cp , normalizedFreq );
65
59
66
60
inBuffer .rewind ();
67
61
@@ -108,7 +102,7 @@ private ByteBuffer compressOrder0Way4(final ByteBuffer inBuffer) {
108
102
inBuffer .position (inBuffer .limit ());
109
103
110
104
// write the prefix at the beginning of the output buffer
111
- writeCompressionPrefix (RANSParams .ORDER .ZERO , outBuffer , inSize , frequencyTableSize , cdata_size );
105
+ writeCompressionPrefix (RANSParams .ORDER .ZERO , outBuffer , inputSize , frequencyTableSize , cdata_size );
112
106
return outBuffer ;
113
107
}
114
108
@@ -117,16 +111,16 @@ private ByteBuffer compressOrder1Way4(final ByteBuffer inBuffer) {
117
111
final ByteBuffer outBuffer = allocateOutputBuffer (inSize );
118
112
119
113
// move to start of frequency
120
- outBuffer .position (PREFIX_BYTE_LENGTH );
114
+ outBuffer .position (Constants . RANS_4x8_PREFIX_BYTE_LENGTH );
121
115
122
116
// get normalized frequencies
123
- final int [][] F = calcFrequenciesOrder1 (inBuffer );
117
+ final int [][] normalizedFreq = calcFrequenciesOrder1 (inBuffer );
124
118
125
119
// using the normalised frequencies, set the RANSEncodingSymbols
126
- buildSymsOrder1 (F );
120
+ buildSymsOrder1 (normalizedFreq );
127
121
128
122
final ByteBuffer cp = outBuffer .slice ();
129
- final int frequencyTableSize = writeFrequenciesOrder1 (cp , F );
123
+ final int frequencyTableSize = writeFrequenciesOrder1 (cp , normalizedFreq );
130
124
inBuffer .rewind ();
131
125
final int in_size = inBuffer .remaining ();
132
126
long rans0 , rans1 , rans2 , rans3 ;
@@ -214,16 +208,16 @@ private static void writeCompressionPrefix(
214
208
final int frequencyTableSize ,
215
209
final int compressedBlobSize ) {
216
210
ValidationUtils .validateArg (order == RANSParams .ORDER .ONE || order == RANSParams .ORDER .ZERO ,"unrecognized RANS order" );
217
- outBuffer .limit (PREFIX_BYTE_LENGTH + frequencyTableSize + compressedBlobSize );
211
+ outBuffer .limit (Constants . RANS_4x8_PREFIX_BYTE_LENGTH + frequencyTableSize + compressedBlobSize );
218
212
219
213
// go back to the beginning of the stream and write the prefix values
220
214
// write the (ORDER as a single byte at offset 0)
221
215
outBuffer .put (0 , (byte ) (order == RANSParams .ORDER .ZERO ? 0 : 1 ));
222
216
outBuffer .order (ByteOrder .LITTLE_ENDIAN );
223
217
// move past the ORDER and write the compressed size
224
- outBuffer .putInt (ORDER_BYTE_LENGTH , frequencyTableSize + compressedBlobSize );
218
+ outBuffer .putInt (Constants . RANS_4x8_ORDER_BYTE_LENGTH , frequencyTableSize + compressedBlobSize );
225
219
// move past the compressed size and write the uncompressed size
226
- outBuffer .putInt (ORDER_BYTE_LENGTH + COMPRESSED_BYTE_LENGTH , inSize );
220
+ outBuffer .putInt (Constants . RANS_4x8_ORDER_BYTE_LENGTH + Constants . RANS_4x8_COMPRESSED_BYTE_LENGTH , inSize );
227
221
outBuffer .rewind ();
228
222
}
229
223
@@ -333,36 +327,6 @@ private static int[][] calcFrequenciesOrder1(final ByteBuffer in) {
333
327
return F ;
334
328
}
335
329
336
- private void buildSymsOrder0 (final int [] F ) {
337
- final RANSEncodingSymbol [] encodingSymbols = getEncodingSymbols ()[0 ];
338
-
339
- // T = running sum of frequencies including the current symbol
340
- // F[j] = frequency of symbol "j"
341
- // C[j] = cumulative frequency of all the symbols preceding "j" (and excluding the frequency of symbol "j")
342
- int cumulativeFreq = 0 ;
343
- for (int j = 0 ; j < Constants .NUMBER_OF_SYMBOLS ; j ++) {
344
- if (F [j ] != 0 ) {
345
- //For each symbol, set start = cumulative frequency and freq = frequency
346
- encodingSymbols [j ].set (cumulativeFreq , F [j ], Constants .TOTAL_FREQ_SHIFT );
347
- cumulativeFreq += F [j ];
348
- }
349
- }
350
- }
351
-
352
- private void buildSymsOrder1 (final int [][] F ) {
353
- final RANSEncodingSymbol [][] encodingSymbols = getEncodingSymbols ();
354
- for (int i = 0 ; i < Constants .NUMBER_OF_SYMBOLS ; i ++) {
355
- final int [] F_i_ = F [i ];
356
- int cumulativeFreq = 0 ;
357
- for (int symbol = 0 ; symbol < Constants .NUMBER_OF_SYMBOLS ; symbol ++) {
358
- if (F_i_ [symbol ] != 0 ) {
359
- encodingSymbols [i ][symbol ].set (cumulativeFreq , F_i_ [symbol ], Constants .TOTAL_FREQ_SHIFT );
360
- cumulativeFreq += F_i_ [symbol ];
361
- }
362
- }
363
- }
364
- }
365
-
366
330
private static int writeFrequenciesOrder0 (final ByteBuffer cp , final int [] F ) {
367
331
final int start = cp .position ();
368
332
0 commit comments