| 1 |
#ifndef H5Z_SPERR_H |
| 2 |
#define H5Z_SPERR_H |
| 3 |
|
| 4 |
#include <assert.h> |
| 5 |
#include <math.h> |
| 6 |
#include <stdlib.h> |
| 7 |
|
| 8 |
#define H5Z_FILTER_SPERR 32028 |
| 9 |
|
| 10 |
#define FRACTIONAL_BITS 16 |
| 11 |
#define INTEGER_BITS 12 |
| 12 |
|
| 13 |
/* |
| 14 |
* This function encodes 1) the SPERR compression mode, 2) compression quality, 3) if to swap |
| 15 |
* rank orders into a 32-bit unsigned int. Valid input and its meaning: |
| 16 |
* - Fixed bitrate compression: mode = 1; quality = target bitrate. |
| 17 |
* - Fixed PSNR compression: mode = 2; quality = target PSNR. |
| 18 |
* - Fixed PWE (point-wise error) compression: mode = 3; quality = PWE tolerance. |
| 19 |
* - In all modes, swap != 0 means to perform rank order swaps. |
| 20 |
* The encoded value is returned and needs to be passed to HDF5 as `cd_values[1]`. |
| 21 |
*/ |
| 22 |
unsigned int H5Z_SPERR_make_cd_values(int mode, double quality, int swap) |
| 23 |
{ |
| 24 |
assert(1 <= mode && mode <= 3); |
| 25 |
assert(quality > 0.0); |
| 26 |
|
| 27 |
unsigned int ret = 0; |
| 28 |
|
| 29 |
if (mode == 1 || mode == 2) { |
| 30 |
quality = round(quality * (1u << FRACTIONAL_BITS)); |
| 31 |
ret = (unsigned int)quality; |
| 32 |
} |
| 33 |
else if (mode == 3) { |
| 34 |
/* |
| 35 |
*Use the logarithm of quality, and also encode its sign. |
| 36 |
*/ |
| 37 |
quality = log2(quality); |
| 38 |
|
| 39 |
int negative = 0; |
| 40 |
if (quality < 0.0) { |
| 41 |
negative = 1; |
| 42 |
quality *= -1.0; |
| 43 |
quality = ceil(quality * (1u << FRACTIONAL_BITS)); |
| 44 |
} |
| 45 |
else |
| 46 |
quality = floor(quality * (1u << FRACTIONAL_BITS)); |
| 47 |
|
| 48 |
ret = (unsigned int)quality; |
| 49 |
if (negative) |
| 50 |
ret |= 1u << (INTEGER_BITS + FRACTIONAL_BITS - 1); |
| 51 |
} |
| 52 |
|
| 53 |
/* Encode mode in bit 28, 29, and 30. */ |
| 54 |
unsigned int mask = 0; |
| 55 |
switch (mode) { |
| 56 |
case 1: |
| 57 |
mask |= 1u << (INTEGER_BITS + FRACTIONAL_BITS); |
| 58 |
break; |
| 59 |
case 2: |
| 60 |
mask |= 1u << (INTEGER_BITS + FRACTIONAL_BITS + 1); |
| 61 |
break; |
| 62 |
case 3: |
| 63 |
mask |= 1u << (INTEGER_BITS + FRACTIONAL_BITS); |
| 64 |
mask |= 1u << (INTEGER_BITS + FRACTIONAL_BITS + 1); |
| 65 |
break; |
| 66 |
default:; |
| 67 |
} |
| 68 |
ret |= mask; |
| 69 |
|
| 70 |
/* Encode swap flag at bit 31. */ |
| 71 |
if (swap) |
| 72 |
ret |= 1u << (INTEGER_BITS + FRACTIONAL_BITS + 3); |
| 73 |
|
| 74 |
return ret; |
| 75 |
} |
| 76 |
|
| 77 |
void H5Z_SPERR_decode_cd_values(unsigned int cd_val, /* input */ |
| 78 |
int* mode, /* output */ |
| 79 |
double* quality, /* output */ |
| 80 |
int* swap) /* output */ |
| 81 |
{ |
| 82 |
/* Decode the rank swap flag. */ |
| 83 |
*swap = cd_val >> (INTEGER_BITS + FRACTIONAL_BITS + 3); |
| 84 |
|
| 85 |
/* Decode the compression mode from the next 3 bits */ |
| 86 |
unsigned int bit1 = (cd_val >> (INTEGER_BITS + FRACTIONAL_BITS)) & 1u; |
| 87 |
unsigned int bit2 = (cd_val >> (INTEGER_BITS + FRACTIONAL_BITS + 1)) & 1u; |
| 88 |
if (bit1 && !bit2) |
| 89 |
*mode = 1; |
| 90 |
else if (!bit1 && bit2) |
| 91 |
*mode = 2; |
| 92 |
else if (bit1 && bit2) |
| 93 |
*mode = 3; |
| 94 |
|
| 95 |
int negative = 0; |
| 96 |
if ((cd_val >> (INTEGER_BITS + FRACTIONAL_BITS - 1)) & 1u) |
| 97 |
negative = 1; |
| 98 |
|
| 99 |
/* Zero out the top 4 bits and the sign bit (5 bits in total) */ |
| 100 |
unsigned int mask = 1u << (INTEGER_BITS + FRACTIONAL_BITS - 1); |
| 101 |
cd_val &= (mask - 1); |
| 102 |
|
| 103 |
*quality = (double)cd_val / (double)(1u << FRACTIONAL_BITS); |
| 104 |
if (negative) |
| 105 |
*quality *= -1.0; |
| 106 |
|
| 107 |
if (*mode == 3) |
| 108 |
*quality = exp2(*quality); |
| 109 |
} |
| 110 |
|
| 111 |
#endif |