5
5
import htsjdk .samtools .cram .compression .rans .Constants ;
6
6
import htsjdk .samtools .cram .compression .rans .RANSDecode ;
7
7
import htsjdk .samtools .cram .compression .rans .RANSDecodingSymbol ;
8
- import htsjdk .samtools .cram .compression .rans .RANSParams ;
9
8
import htsjdk .samtools .cram .compression .rans .Utils ;
10
9
11
10
import java .nio .ByteBuffer ;
@@ -17,17 +16,17 @@ public class RANSNx16Decode extends RANSDecode {
17
16
private static final int FREQ_TABLE_OPTIONALLY_COMPRESSED_MASK = 0x01 ;
18
17
19
18
public ByteBuffer uncompress (final ByteBuffer inBuffer ) {
19
+
20
+ // For RANS decoding, the bytes are read in little endian from the input stream
21
+ inBuffer .order (ByteOrder .LITTLE_ENDIAN );
20
22
return uncompress (inBuffer , 0 );
21
23
}
22
24
23
- public ByteBuffer uncompress (final ByteBuffer inBuffer , int outSize ) {
25
+ private ByteBuffer uncompress (final ByteBuffer inBuffer , int outSize ) {
24
26
if (inBuffer .remaining () == 0 ) {
25
27
return EMPTY_BUFFER ;
26
28
}
27
29
28
- // For RANS decoding, the bytes are read in little endian from the input stream
29
- inBuffer .order (ByteOrder .LITTLE_ENDIAN );
30
-
31
30
// the first byte of compressed stream gives the formatFlags
32
31
final int formatFlags = inBuffer .get () & 0xFF ;
33
32
final RANSNx16Params ransNx16Params = new RANSNx16Params (formatFlags );
@@ -70,7 +69,7 @@ public ByteBuffer uncompress(final ByteBuffer inBuffer, int outSize) {
70
69
uncompressedRLEOutputLength = outSize ;
71
70
outSize = Utils .readUint7 (inBuffer );
72
71
// TODO: maybe move decodeRLEMeta in-line
73
- uncompressedRLEMetaData = decodeRLEMeta (inBuffer , ransNx16Params , uncompressedRLEMetaDataLength , rleSymbols );
72
+ uncompressedRLEMetaData = decodeRLEMeta (inBuffer , uncompressedRLEMetaDataLength , rleSymbols );
74
73
}
75
74
76
75
ByteBuffer outBuffer = ByteBuffer .allocate (outSize );
@@ -86,7 +85,7 @@ public ByteBuffer uncompress(final ByteBuffer inBuffer, int outSize) {
86
85
uncompressOrder0WayN (inBuffer , outBuffer , outSize , ransNx16Params );
87
86
break ;
88
87
case ONE :
89
- uncompressOrder1WayN (inBuffer , outBuffer , outSize , ransNx16Params );
88
+ uncompressOrder1WayN (inBuffer , outBuffer , ransNx16Params );
90
89
break ;
91
90
default :
92
91
throw new RuntimeException ("Unknown rANS order: " + ransNx16Params .getOrder ());
@@ -167,7 +166,6 @@ private ByteBuffer uncompressOrder0WayN(
167
166
private ByteBuffer uncompressOrder1WayN (
168
167
final ByteBuffer inBuffer ,
169
168
final ByteBuffer outBuffer ,
170
- final int outSize ,
171
169
final RANSNx16Params ransNx16Params ) {
172
170
initializeRANSDecoder ();
173
171
@@ -286,7 +284,7 @@ private void readFrequencyTableOrder0(
286
284
287
285
private void readFrequencyTableOrder1 (
288
286
final ByteBuffer cp ,
289
- int shift ) {
287
+ final int shift ) {
290
288
final int [][] frequencies = new int [Constants .NUMBER_OF_SYMBOLS ][Constants .NUMBER_OF_SYMBOLS ];
291
289
final ArithmeticDecoder [] D = getD ();
292
290
final RANSDecodingSymbol [][] decodingSymbols = getDecodingSymbols ();
@@ -349,7 +347,10 @@ private static int[] readAlphabet(final ByteBuffer cp){
349
347
return alphabet ;
350
348
}
351
349
352
- private ByteBuffer decodeRLEMeta (final ByteBuffer inBuffer , final RANSParams ransParams , final int uncompressedRLEMetaDataLength , final int [] rleSymbols ) {
350
+ private ByteBuffer decodeRLEMeta (
351
+ final ByteBuffer inBuffer ,
352
+ final int uncompressedRLEMetaDataLength ,
353
+ final int [] rleSymbols ) {
353
354
ByteBuffer uncompressedRLEMetaData ;
354
355
final int compressedRLEMetaDataLength ;
355
356
if ((uncompressedRLEMetaDataLength & 0x01 )!=0 ) {
@@ -370,15 +371,19 @@ private ByteBuffer decodeRLEMeta(final ByteBuffer inBuffer , final RANSParams ra
370
371
371
372
int numRLESymbols = uncompressedRLEMetaData .get () & 0xFF ;
372
373
if (numRLESymbols == 0 ) {
373
- numRLESymbols = 256 ;
374
+ numRLESymbols = Constants . NUMBER_OF_SYMBOLS ;
374
375
}
375
376
for (int i = 0 ; i < numRLESymbols ; i ++) {
376
377
rleSymbols [uncompressedRLEMetaData .get () & 0xFF ] = 1 ;
377
378
}
378
379
return uncompressedRLEMetaData ;
379
380
}
380
381
381
- private ByteBuffer decodeRLE (ByteBuffer inBuffer , final int [] rleSymbols , final ByteBuffer uncompressedRLEMetaData , int uncompressedRLEOutputLength ) {
382
+ private ByteBuffer decodeRLE (
383
+ ByteBuffer inBuffer ,
384
+ final int [] rleSymbols ,
385
+ final ByteBuffer uncompressedRLEMetaData ,
386
+ final int uncompressedRLEOutputLength ) {
382
387
ByteBuffer rleOutBuffer = ByteBuffer .allocate (uncompressedRLEOutputLength );
383
388
int j = 0 ;
384
389
for (int i = 0 ; j < uncompressedRLEOutputLength ; i ++){
@@ -396,7 +401,11 @@ private ByteBuffer decodeRLE(ByteBuffer inBuffer , final int[] rleSymbols, final
396
401
return inBuffer ;
397
402
}
398
403
399
- private ByteBuffer decodePack (ByteBuffer inBuffer , final int [] packMappingTable , int numSymbols , int uncompressedPackOutputLength ) {
404
+ private ByteBuffer decodePack (
405
+ ByteBuffer inBuffer ,
406
+ final int [] packMappingTable ,
407
+ final int numSymbols ,
408
+ final int uncompressedPackOutputLength ) {
400
409
ByteBuffer outBufferPack = ByteBuffer .allocate (uncompressedPackOutputLength );
401
410
int j = 0 ;
402
411
@@ -445,38 +454,35 @@ else if (numSymbols <= 16){
445
454
return inBuffer ;
446
455
}
447
456
448
- private ByteBuffer decodeStripe (ByteBuffer inBuffer , final int outSize ){
449
-
457
+ private ByteBuffer decodeStripe (final ByteBuffer inBuffer , final int outSize ){
450
458
final int numInterleaveStreams = inBuffer .get () & 0xFF ;
451
459
452
460
// retrieve lengths of compressed interleaved streams
453
- int [] clen = new int [numInterleaveStreams ];
461
+ final int [] compressedLengths = new int [numInterleaveStreams ];
454
462
for ( int j =0 ; j <numInterleaveStreams ; j ++ ){
455
- clen [j ] = Utils .readUint7 (inBuffer );
463
+ compressedLengths [j ] = Utils .readUint7 (inBuffer );
456
464
}
457
465
458
466
// Decode the compressed interleaved stream
459
- int [] ulen = new int [numInterleaveStreams ];
460
- ByteBuffer [] T = new ByteBuffer [numInterleaveStreams ];
461
-
467
+ final int [] uncompressedLengths = new int [numInterleaveStreams ];
468
+ final ByteBuffer [] TransposedData = new ByteBuffer [numInterleaveStreams ];
462
469
for ( int j =0 ; j <numInterleaveStreams ; j ++){
463
- ulen [j ] = (int ) Math .floor (((double ) outSize )/numInterleaveStreams );
470
+ uncompressedLengths [j ] = (int ) Math .floor (((double ) outSize )/numInterleaveStreams );
464
471
if ((outSize % numInterleaveStreams ) > j ){
465
- ulen [j ]++;
472
+ uncompressedLengths [j ]++;
466
473
}
467
474
468
- T [j ] = uncompress (inBuffer , ulen [j ]);
475
+ TransposedData [j ] = uncompress (inBuffer , uncompressedLengths [j ]);
469
476
}
470
477
471
478
// Transpose
472
- ByteBuffer out = ByteBuffer .allocate (outSize );
479
+ final ByteBuffer outBuffer = ByteBuffer .allocate (outSize );
473
480
for (int j = 0 ; j <numInterleaveStreams ; j ++) {
474
- for (int i = 0 ; i < ulen [j ]; i ++) {
475
- out .put ((i *numInterleaveStreams )+j , T [j ].get (i ));
481
+ for (int i = 0 ; i < uncompressedLengths [j ]; i ++) {
482
+ outBuffer .put ((i *numInterleaveStreams )+j , TransposedData [j ].get (i ));
476
483
}
477
484
}
478
-
479
- return out ;
485
+ return outBuffer ;
480
486
}
481
487
482
488
}
0 commit comments