| 182 | } |
| 183 | |
| 184 | U32 ArithmeticDecoder::decodeSymbol(ArithmeticModel* m) |
| 185 | { |
| 186 | U32 n, sym, x, y = length; |
| 187 | |
| 188 | if (m->decoder_table) { // use table look-up for faster decoding |
| 189 | |
| 190 | unsigned dv = value / (length >>= DM__LengthShift); |
| 191 | unsigned t = dv >> m->table_shift; |
| 192 | |
| 193 | sym = m->decoder_table[t]; // initial decision based on table look-up |
| 194 | n = m->decoder_table[t+1] + 1; |
| 195 | |
| 196 | while (n > sym + 1) { // finish with bisection search |
| 197 | U32 k = (sym + n) >> 1; |
| 198 | if (m->distribution[k] > dv) n = k; else sym = k; |
| 199 | } |
| 200 | // compute products |
| 201 | x = m->distribution[sym] * length; |
| 202 | if (sym != m->last_symbol) y = m->distribution[sym+1] * length; |
| 203 | } |
| 204 | |
| 205 | else { // decode using only multiplications |
| 206 | |
| 207 | x = sym = 0; |
| 208 | length >>= DM__LengthShift; |
| 209 | U32 k = (n = m->symbols) >> 1; |
| 210 | // decode via bisection search |
| 211 | do { |
| 212 | U32 z = length * m->distribution[k]; |
| 213 | if (z > value) { |
| 214 | n = k; |
| 215 | y = z; // value is smaller |
| 216 | } |
| 217 | else { |
| 218 | sym = k; |
| 219 | x = z; // value is larger or equal |
| 220 | } |
| 221 | } while ((k = (sym + n) >> 1) != sym); |
| 222 | } |
| 223 | |
| 224 | value -= x; // update interval |
| 225 | length = y - x; |
| 226 | |
| 227 | if (length < AC__MinLength) renorm_dec_interval(); // renormalization |
| 228 | |
| 229 | ++m->symbol_count[sym]; |
| 230 | if (--m->symbols_until_update == 0) m->update(); // periodic model update |
| 231 | |
| 232 | assert(sym < m->symbols); |
| 233 | |
| 234 | return sym; |
| 235 | } |
| 236 | |
| 237 | U32 ArithmeticDecoder::readBit() |
| 238 | { |
no test coverage detected