Function decode [src]

Prototype

pub fn decode( bit_reader: *BitReader, expected_symbol_count: usize, max_accuracy_log: u4, entries: []Table.Fse, ) !usize

Parameters

bit_reader: *BitReaderexpected_symbol_count: usizemax_accuracy_log: u4entries: []Table.Fse

Source

pub fn decode( bit_reader: *BitReader, expected_symbol_count: usize, max_accuracy_log: u4, entries: []Table.Fse, ) !usize { const accuracy_log_biased = try bit_reader.readBitsNoEof(u4, 4); if (accuracy_log_biased > max_accuracy_log -| 5) return error.MalformedAccuracyLog; const accuracy_log = accuracy_log_biased + 5; var values: [256]u16 = undefined; var value_count: usize = 0; const total_probability = @as(u16, 1) << accuracy_log; var accumulated_probability: u16 = 0; while (accumulated_probability < total_probability) { // WARNING: The RFC is poorly worded, and would suggest std.math.log2_int_ceil is correct here, // but power of two (remaining probabilities + 1) need max bits set to 1 more. const max_bits = std.math.log2_int(u16, total_probability - accumulated_probability + 1) + 1; const small = try bit_reader.readBitsNoEof(u16, max_bits - 1); const cutoff = (@as(u16, 1) << max_bits) - 1 - (total_probability - accumulated_probability + 1); const value = if (small < cutoff) small else value: { const value_read = small + (try bit_reader.readBitsNoEof(u16, 1) << (max_bits - 1)); break :value if (value_read < @as(u16, 1) << (max_bits - 1)) value_read else value_read - cutoff; }; accumulated_probability += if (value != 0) value - 1 else 1; values[value_count] = value; value_count += 1; if (value == 1) { while (true) { const repeat_flag = try bit_reader.readBitsNoEof(u2, 2); if (repeat_flag + value_count > 256) return error.MalformedFseTable; for (0..repeat_flag) |_| { values[value_count] = 1; value_count += 1; } if (repeat_flag < 3) break; } } if (value_count == 256) break; } bit_reader.alignToByte(); if (value_count < 2) return error.MalformedFseTable; if (accumulated_probability != total_probability) return error.MalformedFseTable; if (value_count > expected_symbol_count) return error.MalformedFseTable; const table_size = total_probability; try build(values[0..value_count], entries[0..table_size]); return table_size; }