| 1 |
#include <pbcopper/data/FrameEncoders.h> |
| 2 |
|
| 3 |
#include <boost/algorithm/string/join.hpp> |
| 4 |
#include <boost/range/adaptor/transformed.hpp> |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <limits> |
| 8 |
|
| 9 |
#include <cassert> |
| 10 |
#include <cmath> |
| 11 |
#include <cstddef> |
| 12 |
#include <cstdint> |
| 13 |
|
| 14 |
namespace PacBio { |
| 15 |
namespace Data { |
| 16 |
namespace { |
| 17 |
|
| 18 |
static std::vector<uint16_t> framepoints; |
| 19 |
static std::vector<uint8_t> frameToCode; |
| 20 |
static uint16_t maxFramepoint; |
| 21 |
|
| 22 |
static int InitIpdDownsampling() |
| 23 |
{ |
| 24 |
if (!framepoints.empty()) { |
| 25 |
return 0; |
| 26 |
} |
| 27 |
|
| 28 |
// liftover from kineticsTools |
| 29 |
|
| 30 |
const int B = 2; |
| 31 |
const int t = 6; |
| 32 |
const double T = std::pow(B, t); |
| 33 |
|
| 34 |
int next = 0; |
| 35 |
double grain; |
| 36 |
const int end = 256 / T; |
| 37 |
for (int i = 0; i < end; ++i) { |
| 38 |
grain = std::pow(B, i); |
| 39 |
std::vector<uint16_t> nextOnes; |
| 40 |
for (double j = 0; j < T; ++j) { |
| 41 |
nextOnes.push_back(j * grain + next); |
| 42 |
} |
| 43 |
next = nextOnes.back() + grain; |
| 44 |
framepoints.insert(framepoints.end(), nextOnes.cbegin(), nextOnes.cend()); |
| 45 |
} |
| 46 |
assert(framepoints.size() - 1 <= std::numeric_limits<uint8_t>::max()); |
| 47 |
|
| 48 |
const uint16_t maxElement = (*std::max_element(framepoints.cbegin(), framepoints.cend())); |
| 49 |
frameToCode.assign(maxElement + 1, 0); |
| 50 |
|
| 51 |
const int fpEnd = framepoints.size() - 1; |
| 52 |
uint8_t i = 0; |
| 53 |
uint16_t fl = 0; |
| 54 |
uint16_t fu = 0; |
| 55 |
for (; i < fpEnd; ++i) { |
| 56 |
fl = framepoints[i]; |
| 57 |
fu = framepoints[i + 1]; |
| 58 |
if (fu > fl + 1) { |
| 59 |
const int middle = (fl + fu) / 2; |
| 60 |
for (int f = fl; f < middle; ++f) { |
| 61 |
frameToCode[f] = i; |
| 62 |
} |
| 63 |
for (int f = middle; f < fu; ++f) { |
| 64 |
frameToCode[f] = i + 1; |
| 65 |
} |
| 66 |
} else { |
| 67 |
frameToCode[fl] = i; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// this next line differs from the python implementation (there, it's "i+1") |
| 72 |
// our C++ for loop has incremented our index counter one more time than the indexes from python enumerate(...) |
| 73 |
frameToCode[fu] = i; |
| 74 |
maxFramepoint = fu; |
| 75 |
|
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
static const int initV1Frames = InitIpdDownsampling(); |
| 80 |
|
| 81 |
} // namespace |
| 82 |
|
| 83 |
// ---------------- |
| 84 |
// V1 frame codec |
| 85 |
// ---------------- |
| 86 |
|
| 87 |
Frames V1FrameEncoder::Decode(const std::vector<uint8_t>& encodedFrames) const |
| 88 |
{ |
| 89 |
std::vector<uint16_t> rawFrames; |
| 90 |
rawFrames.reserve(encodedFrames.size()); |
| 91 |
std::transform(encodedFrames.cbegin(), encodedFrames.cend(), std::back_inserter(rawFrames), |
| 92 |
[&](uint8_t code) { return framepoints[code]; }); |
| 93 |
return rawFrames; |
| 94 |
} |
| 95 |
|
| 96 |
std::vector<uint8_t> V1FrameEncoder::Encode(const std::vector<uint16_t>& rawFrames) const |
| 97 |
{ |
| 98 |
std::vector<uint8_t> encoded; |
| 99 |
encoded.reserve(rawFrames.size()); |
| 100 |
std::transform(rawFrames.cbegin(), rawFrames.cend(), std::back_inserter(encoded), |
| 101 |
[&](uint16_t frame) { return frameToCode[std::min(maxFramepoint, frame)]; }); |
| 102 |
return encoded; |
| 103 |
} |
| 104 |
|
| 105 |
std::string V1FrameEncoder::Name() const { return "CodecV1"; } |
| 106 |
|
| 107 |
// ---------------- |
| 108 |
// V2 frame codec |
| 109 |
// ---------------- |
| 110 |
|
| 111 |
V2FrameEncoder::V2FrameEncoder(int exponentBits, int mantissaBits) |
| 112 |
: exponentBits_{exponentBits} |
| 113 |
, mantissaBits_{mantissaBits} |
| 114 |
, base_(std::pow(2, mantissaBits_)) |
| 115 |
, max_((1 << (exponentBits_ + mantissaBits_)) - 1) |
| 116 |
{} |
| 117 |
|
| 118 |
Frames V2FrameEncoder::Decode(const std::vector<uint8_t>& encodedFrames) const |
| 119 |
{ |
| 120 |
std::vector<uint16_t> decoded; |
| 121 |
decoded.reserve(encodedFrames.size()); |
| 122 |
std::transform(encodedFrames.cbegin(), encodedFrames.cend(), std::back_inserter(decoded), |
| 123 |
[&](uint8_t x) -> uint16_t { |
| 124 |
if (x > max_) { |
| 125 |
throw std::runtime_error{ |
| 126 |
"[pbcopper] invalid frame encoding ERROR: " + std::to_string(x) + |
| 127 |
" is out of range [0," + std::to_string(max_) + ']'}; |
| 128 |
} |
| 129 |
const uint8_t mantissa = x & static_cast<uint8_t>((base_ - 1)); |
| 130 |
const uint8_t exponent = (x ^ mantissa) >> mantissaBits_; |
| 131 |
return (base_ * (std::pow(2, exponent) - 1)) + |
| 132 |
((std::pow(2, exponent)) * mantissa); |
| 133 |
}); |
| 134 |
return Frames{decoded}; |
| 135 |
} |
| 136 |
|
| 137 |
std::vector<uint8_t> V2FrameEncoder::Encode(const std::vector<uint16_t>& rawFrames) const |
| 138 |
{ |
| 139 |
// NOTE: nothing compressed here yet in output bytes, regardless of bitsPerPulse |
| 140 |
|
| 141 |
std::vector<uint8_t> encoded; |
| 142 |
encoded.reserve(rawFrames.size()); |
| 143 |
std::transform( |
| 144 |
rawFrames.cbegin(), rawFrames.cend(), std::back_inserter(encoded), [&](uint16_t x) { |
| 145 |
const int exponent = std::log2(x / base_ + 1); |
| 146 |
const int mantissa = |
| 147 |
(x - base_ * (static_cast<uint8_t>(std::pow(2, exponent)) - 1)) >> exponent; |
| 148 |
const uint8_t result = (exponent << mantissaBits_) | mantissa; |
| 149 |
return std::min(result, max_); |
| 150 |
}); |
| 151 |
return encoded; |
| 152 |
} |
| 153 |
|
| 154 |
int V2FrameEncoder::MantissaBits() const { return mantissaBits_; } |
| 155 |
|
| 156 |
std::string V2FrameEncoder::Name() const |
| 157 |
{ |
| 158 |
return "CodecV2/" + std::to_string(exponentBits_) + '/' + std::to_string(mantissaBits_); |
| 159 |
} |
| 160 |
|
| 161 |
} // namespace Data |
| 162 |
} // namespace PacBio |