Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/vovach777/dwt53
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Poslavskiy committed Aug 16, 2023
2 parents 9dbbf10 + 359ca34 commit fd6f009
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(dwt53 CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address")
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address")
if(CMAKE_BUILD_TYPE MATCHES Debug)
add_definitions(-D_ITERATOR_DEBUG_LEVEL=0)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
Expand Down
20 changes: 10 additions & 10 deletions bitstream.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//https://raw.githubusercontent.com/vovach777/BitStream/main/bitstream.hpp
#pragma once
#include <cstdint>
#include <iostream>
#include <ostream>
#include <vector>
#include <stdexcept>
#include <limits>

class BitWriter {

Expand Down Expand Up @@ -42,25 +42,25 @@ class BitWriter {
inline void writeBit(bool value) { put_bits(1,value);}
inline void writeBit0() { writeBit(true);}
inline void writeBit1() { writeBit(false);}
auto data() const {
return reinterpret_cast<const uint8_t*>( vec.data());
auto data() const {
return reinterpret_cast<const uint8_t*>( vec.data());
}
auto data() {
return reinterpret_cast<uint8_t*>( vec.data());
auto data() {
return reinterpret_cast<uint8_t*>( vec.data());
}
auto size_in_bits() const { return size_*32 + (32-bit_left_); }
auto size() const {
auto size() const {
return (size_in_bits() + 7) >> 3;
}

void flush() {
if (bit_left_ == 32)
return;
if (vec.size() <= size_) {
vec.resize(size_+1);
}
vec[size_] = 0;
auto byte_by_byte = reinterpret_cast<uint8_t*>( &vec[size_] );
auto byte_by_byte = reinterpret_cast<uint8_t*>( &vec[size_] );
auto bit_left = bit_left_;
auto bit_buf = bit_buf_;
if (bit_left < 32)
Expand Down Expand Up @@ -95,7 +95,7 @@ class BitWriter {
};

class BitReader {


inline unsigned int get_bits1() {
size_t _index = index;
Expand All @@ -118,7 +118,7 @@ class BitReader {
if (n == 0) return 0;
if (index+n > size_in_bits) {
throw std::out_of_range("(index+n > size_in_bits)!!!");
}
}
union unaligned_32 {
uint32_t l;
} __attribute__((packed)) __attribute__((may_alias));
Expand Down
56 changes: 56 additions & 0 deletions bitstream_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

class ValueWriter {

inline int ilog2_32(uint32_t v)
{
if (v == 0)
return 1;
return 32-__builtin_clz(v);
}

public:
ValueWriter() = delete;
ValueWriter(BitWriter &ref) : ref(ref) {};
void encode( int n, int value)
{
auto base = (1 << n)-1;
if (value >= base)
{
value -= base;
auto n_ = std::max(n, ilog2_32(value));
base = (1 << n_)-1;
n = n_+1+n_;
if (n > 32) {
ref.writeBits(n_,base);
ref.writeBits(1, 0);
ref.writeBits(n_, value);
return;
}
value = (base << (n_+1)) | value;
}
ref.writeBits(n,value);
}
private:
BitWriter&ref;
};

class ValueReader {
public:
ValueReader() = delete;
ValueReader(BitReader& ref) : ref(ref) {}
int decode( int n )
{

auto v = ref.readBits(n);
if (v == ((1 << n)-1)) {

while (ref.readBit()) ++n;
v += ref.readBits(n);
}

return v;
}
private:
BitReader& ref;
};
16 changes: 10 additions & 6 deletions huffman.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <vector>
#include "bitstream.hpp"
#include "utils.hpp"
#include "bitstream_helper.hpp"

namespace pack {
namespace impl {
Expand Down Expand Up @@ -72,11 +73,12 @@ namespace pack {
void encodeHuffmanTree(BitWriter& dest) {
if (DHT.size() == 0) throw std::domain_error("No DHT found!");


ValueWriter vv(dest);

dest.writeBits(8,DHT.size());
vv.encode(4,DHT.size());
for (int i = 0; i < DHT.size(); i++) {
dest.writeBits(8, DHT[i].size());
///dest.writeBits(8, DHT[i].size());
vv.encode(8, DHT[i].size());
for (auto symbol : DHT[i]) {
auto catindex = symbol_to_catindex(symbol);
dest.writeBits(4, catindex & 0xf);
Expand All @@ -88,11 +90,13 @@ namespace pack {

void decodeHuffmanTree(BitReader& src) {
// decode huffman table
ValueReader vr(src);
DHT.clear();
DHT.resize(src.readBits(8));

//DHT.resize(src.readBits(8));
DHT.resize(vr.decode(4));
for (int i = 0; i < DHT.size(); i++) {
DHT[i].resize(src.readBits(8));
//DHT[i].resize(src.readBits(8));
DHT[i].resize(vr.decode(8));
if (DHT[i].size() == 0) continue;
cout << i << " (" << DHT[i].size() << ") :";
// cout << "DHT_" << i << ":" << DHT[i].size() << endl;
Expand Down

0 comments on commit fd6f009

Please sign in to comment.