Core arithmetic encoding for a single (layer, channel). Returns number of bytes written.
(
cdf_layer_c, # np.uint32 [lp]
sym_channel, # np.uint8 [n_tokens]
out_buf_lc, # np.uint8 [buffer_size]
)
| 2132 | |
| 2133 | @njit(cache=True) |
| 2134 | def _encode_single_channel( |
| 2135 | cdf_layer_c, # np.uint32 [lp] |
| 2136 | sym_channel, # np.uint8 [n_tokens] |
| 2137 | out_buf_lc, # np.uint8 [buffer_size] |
| 2138 | ): |
| 2139 | """Core arithmetic encoding for a single (layer, channel). |
| 2140 | Returns number of bytes written.""" |
| 2141 | MASK32 = 0xFFFFFFFF |
| 2142 | precision = 16 |
| 2143 | max_symbol = len(cdf_layer_c) - 2 |
| 2144 | n_tokens = len(sym_channel) |
| 2145 | |
| 2146 | low, high = 0, MASK32 |
| 2147 | pending_bits = 0 |
| 2148 | output_reg, output_reg_len = 0, 0 |
| 2149 | ptr = 0 |
| 2150 | buf_size = len(out_buf_lc) |
| 2151 | |
| 2152 | # Inline flush_bit to avoid closure (numba does not support nonlocal) |
| 2153 | for token_idx in range(n_tokens): |
| 2154 | sym = int(sym_channel[token_idx]) |
| 2155 | c_low = int(cdf_layer_c[sym]) |
| 2156 | c_high = 0x10000 if sym == max_symbol else int(cdf_layer_c[sym + 1]) |
| 2157 | |
| 2158 | span = (high - low + 1) & MASK32 |
| 2159 | if span == 0: |
| 2160 | span = 0x100000000 |
| 2161 | |
| 2162 | high = (low + ((span * c_high) >> precision) - 1) & MASK32 |
| 2163 | low = (low + ((span * c_low) >> precision)) & MASK32 |
| 2164 | |
| 2165 | while True: |
| 2166 | if (high & 0x80000000) == (low & 0x80000000): |
| 2167 | # flush_bit(bit) |
| 2168 | bit = (high >> 31) & 1 |
| 2169 | output_reg = (output_reg << 1) | bit |
| 2170 | output_reg_len += 1 |
| 2171 | if output_reg_len == 8: |
| 2172 | if ptr < buf_size: |
| 2173 | out_buf_lc[ptr] = output_reg & 0xFF |
| 2174 | ptr += 1 |
| 2175 | output_reg, output_reg_len = 0, 0 |
| 2176 | # flush pending bits |
| 2177 | for _ in range(pending_bits): |
| 2178 | output_reg = (output_reg << 1) | (1 - bit) |
| 2179 | output_reg_len += 1 |
| 2180 | if output_reg_len == 8: |
| 2181 | if ptr < buf_size: |
| 2182 | out_buf_lc[ptr] = output_reg & 0xFF |
| 2183 | ptr += 1 |
| 2184 | output_reg, output_reg_len = 0, 0 |
| 2185 | pending_bits = 0 |
| 2186 | low = (low << 1) & MASK32 |
| 2187 | high = ((high << 1) | 1) & MASK32 |
| 2188 | elif (low & 0x40000000) != 0 and (high & 0x40000000) == 0: |
| 2189 | pending_bits += 1 |
| 2190 | low = (low << 1) & 0x7FFFFFFF |
| 2191 | high = ((high << 1) | 0x80000001) & MASK32 |