-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptive_model.h
More file actions
71 lines (53 loc) · 1.52 KB
/
Copy pathadaptive_model.h
File metadata and controls
71 lines (53 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once
#include "coder.h"
#include <cstdint>
#include <vector>
class AdaptiveModel {
public:
AdaptiveModel();
explicit AdaptiveModel(size_t symbol_count);
void Reset(size_t symbol_count);
const std::vector<uint32_t>& cdf() const { return cdf_; }
template <int BIT_LIMIT = 24>
void Update(size_t symbol);
private:
std::vector<uint32_t> symbols_count_;
std::vector<uint32_t> cdf_;
size_t total_counter_;
size_t update_downcounter_;
};
inline AdaptiveModel::AdaptiveModel()
: total_counter_(0)
, update_downcounter_(0)
{ }
inline AdaptiveModel::AdaptiveModel(size_t symbol_count)
{
Reset(symbol_count);
}
inline void AdaptiveModel::Reset(size_t symbol_count)
{
symbols_count_.assign(symbol_count, 1);
cdf_.reserve(symbol_count + 1);
total_counter_ = symbol_count;
update_downcounter_ = 64;
range_code::GenerateCdf(symbols_count_, &cdf_);
}
template <int BIT_LIMIT>
inline void AdaptiveModel::Update(size_t symbol)
{
static_assert (8 <= BIT_LIMIT && BIT_LIMIT <= 24, "Bit limit must belong to [8, 24].");
symbols_count_.at(symbol) += 16;
total_counter_ += 16;
while (total_counter_ >> BIT_LIMIT) {
total_counter_ = 0;
for (auto& symbol_count : symbols_count_) {
symbol_count = (symbol_count + 1) / 2;
total_counter_ += symbol_count;
}
}
--update_downcounter_;
if (update_downcounter_ == 0) {
update_downcounter_ = 256;
range_code::GenerateCdf(symbols_count_, &cdf_);
}
}