1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
py::bytes encode( const cdf_ptr& cdf_ptr, const torch::Tensor& sym){
OutCacheString out_cache;
uint32_t low = 0; uint32_t high = 0xFFFFFFFFU; uint64_t pending_bits = 0;
const int precision = 16;
const cdf_t* cdf = cdf_ptr.data; const int N_sym = cdf_ptr.N_sym; const int Lp = cdf_ptr.Lp; const int max_symbol = Lp - 2;
auto sym_ = sym.accessor<int16_t, 1>();
for (int i = 0; i < N_sym; ++i) { const int16_t sym_i = sym_[i];
const uint64_t span = static_cast<uint64_t>(high) - static_cast<uint64_t>(low) + 1;
const int offset = i * Lp; const uint32_t c_low = cdf[offset + sym_i]; const uint32_t c_high = sym_i == max_symbol ? 0x10000U : cdf[offset + sym_i + 1];
high = (low - 1) + ((span * static_cast<uint64_t>(c_high)) >> precision); low = (low) + ((span * static_cast<uint64_t>(c_low)) >> precision);
while (true) { if (high < 0x80000000U) { out_cache.append_bit_and_pending(0, pending_bits); low <<= 1; high <<= 1; high |= 1; } else if (low >= 0x80000000U) { out_cache.append_bit_and_pending(1, pending_bits); low <<= 1; high <<= 1; high |= 1; } else if (low >= 0x40000000U && high < 0xC0000000U) { pending_bits++; low <<= 1; low &= 0x7FFFFFFF; high <<= 1; high |= 0x80000001; } else { break; } } }
pending_bits += 1;
if (pending_bits) { if (low < 0x40000000U) { out_cache.append_bit_and_pending(0, pending_bits); } else { out_cache.append_bit_and_pending(1, pending_bits); } }
out_cache.flush();
#ifdef VERBOSE std::chrono::steady_clock::time_point end= std::chrono::steady_clock::now(); std::cout << "Time difference (sec) = " << (std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count()) /1000000.0 <<std::endl; #endif
return py::bytes(out_cache.out); }
torch::Tensor decode( const cdf_ptr& cdf_ptr, const std::string& in) {
#ifdef VERBOSE std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); #endif
const cdf_t* cdf = cdf_ptr.data; const int N_sym = cdf_ptr.N_sym; const int Lp = cdf_ptr.Lp; const int max_symbol = Lp - 2;
auto out = torch::empty({N_sym}, torch::kShort); auto out_ = out.accessor<int16_t, 1>();
uint32_t low = 0; uint32_t high = 0xFFFFFFFFU; uint32_t value = 0; const uint32_t c_count = 0x10000U; const int precision = 16;
InCacheString in_cache(in); in_cache.initialize(value);
for (int i = 0; i < N_sym; ++i) { const uint64_t span = static_cast<uint64_t>(high) - static_cast<uint64_t>(low) + 1; const uint16_t count = ((static_cast<uint64_t>(value) - static_cast<uint64_t>(low) + 1) * c_count - 1) / span; const int offset = i * Lp; auto sym_i = binsearch(cdf, count, (cdf_t)max_symbol, offset);
out_[i] = (int16_t)sym_i;
if (i == N_sym-1) { break; }
const uint32_t c_low = cdf[offset + sym_i]; const uint32_t c_high = sym_i == max_symbol ? 0x10000U : cdf[offset + sym_i + 1];
high = (low - 1) + ((span * static_cast<uint64_t>(c_high)) >> precision); low = (low) + ((span * static_cast<uint64_t>(c_low)) >> precision);
while (true) { if (low >= 0x80000000U || high < 0x80000000U) { low <<= 1; high <<= 1; high |= 1; in_cache.get(value); } else if (low >= 0x40000000U && high < 0xC0000000U) {
low <<= 1; low &= 0x7FFFFFFFU; high <<= 1; high |= 0x80000001U; value -= 0x40000000U; in_cache.get(value); } else { break; } } }
#ifdef VERBOSE std::chrono::steady_clock::time_point end= std::chrono::steady_clock::now(); std::cout << "Time difference (sec) = " << (std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count()) /1000000.0 <<std::endl; #endif
return out; }
|