bsnes/nall/encode/huffman.hpp
Tim Allen 559a6585ef Update to v106r81 release.
byuu says:

First 32 instructions implemented in the TLCS900H disassembler. Only 992
to go!

I removed the use of anonymous namespaces in nall. It was something I
rarely used, because it rarely did what I wanted.

I updated all nested namespaces to use C++17-style namespace Foo::Bar {}
syntax instead of classic C++-style namespace Foo { namespace Bar {}}.

I updated ruby::Video::acquire() to return a struct, so we can use C++17
structured bindings. Long term, I want to get away from all functions
that take references for output only. Even though C++ botched structured
bindings by not allowing you to bind to existing variables, it's even
worse to have function calls that take arguments by reference and then
write to them. From the caller side, you can't tell the value is being
written, nor that the value passed in doesn't matter, which is terrible.
2019-01-16 13:02:24 +11:00

85 lines
2.3 KiB
C++

#pragma once
namespace nall::Encode {
inline auto Huffman(array_view<uint8_t> input) -> vector<uint8_t> {
vector<uint8_t> output;
for(uint byte : range(8)) output.append(input.size() >> byte * 8);
struct Node {
uint frequency = 0;
uint parent = 0;
uint lhs = 0;
uint rhs = 0;
};
array<Node[512]> nodes;
for(uint offset : range(input.size())) nodes[input[offset]].frequency++;
uint count = 0;
for(uint offset : range(511)) {
if(nodes[offset].frequency) count++;
else nodes[offset].parent = 511;
}
auto minimum = [&] {
uint frequency = ~0, minimum = 511;
for(uint index : range(511)) {
if(!nodes[index].parent && nodes[index].frequency && nodes[index].frequency < frequency) {
frequency = nodes[index].frequency;
minimum = index;
}
}
return minimum;
};
//group the least two frequently used nodes until only one node remains
uint index = 256;
for(uint remaining = max(2, count); remaining >= 2; remaining--) {
uint lhs = minimum();
nodes[lhs].parent = index;
uint rhs = minimum();
nodes[rhs].parent = index;
if(remaining == 2) index = nodes[lhs].parent = nodes[rhs].parent = 511;
nodes[index].lhs = lhs;
nodes[index].rhs = rhs;
nodes[index].parent = 0;
nodes[index].frequency = nodes[lhs].frequency + nodes[rhs].frequency;
index++;
}
uint byte = 0, bits = 0;
auto write = [&](bool bit) {
byte = byte << 1 | bit;
if(++bits == 8) output.append(byte), bits = 0;
};
//only the upper half of the table is needed for decompression
//the first 256 nodes are always treated as leaf nodes
for(uint offset : range(256)) {
for(uint index : reverse(range(9))) write(nodes[256 + offset].lhs >> index & 1);
for(uint index : reverse(range(9))) write(nodes[256 + offset].rhs >> index & 1);
}
for(uint byte : input) {
uint node = byte, length = 0;
uint256_t sequence = 0;
//traversing the array produces the bitstream in reverse order
do {
uint parent = nodes[node].parent;
bool bit = nodes[nodes[node].parent].rhs == node;
sequence = sequence << 1 | bit;
length++;
node = parent;
} while(node != 511);
//output the generated bits in the correct order
for(uint index : range(length)) {
write(sequence >> index & 1);
}
}
while(bits) write(0);
return output;
}
}