24#include "field_codec_arithmetic.h"
28using namespace dccl::logger;
30const dccl::arith::Model::symbol_type dccl::arith::Model::OUT_OF_RANGE_SYMBOL;
31const dccl::arith::Model::symbol_type dccl::arith::Model::EOF_SYMBOL;
32const dccl::arith::Model::symbol_type dccl::arith::Model::MIN_SYMBOL;
33const int dccl::arith::Model::CODE_VALUE_BITS;
34const int dccl::arith::Model::FREQUENCY_BITS;
35const dccl::arith::Model::freq_type dccl::arith::Model::MAX_FREQUENCY;
37#if DCCL_THREAD_SUPPORT
38std::recursive_mutex dccl::arith::Model::last_bits_map_mutex;
40std::map<std::string, std::map<std::string, dccl::Bitset>> dccl::arith::Model::last_bits_map;
101dccl::arith::Model::symbol_type dccl::arith::Model::value_to_symbol(value_type value)
const
103 if (value < *user_model_.value_bound().begin() ||
104 value > *(user_model_.value_bound().end() - 1))
105 return Model::OUT_OF_RANGE_SYMBOL;
107 google::protobuf::RepeatedField<double>::const_iterator upper_it =
108 std::upper_bound(user_model_.value_bound().begin(), user_model_.value_bound().end(), value);
110 google::protobuf::RepeatedField<double>::const_iterator lower_it =
111 (upper_it == user_model_.value_bound().begin()) ? upper_it : upper_it - 1;
113 double lower_diff = std::abs((*lower_it) * (*lower_it) - value * value);
114 double upper_diff = std::abs((*upper_it) * (*upper_it) - value * value);
121 ((lower_diff < upper_diff) ? lower_it : upper_it) - user_model_.value_bound().begin();
126dccl::arith::Model::value_type dccl::arith::Model::symbol_to_value(symbol_type symbol)
const
128 if (symbol == EOF_SYMBOL)
129 throw(
Exception(
"EOF symbol has no value."));
131 value_type value = (symbol == Model::OUT_OF_RANGE_SYMBOL)
132 ? std::numeric_limits<value_type>::quiet_NaN()
133 : user_model_.value_bound(symbol);
138std::pair<dccl::arith::Model::freq_type, dccl::arith::Model::freq_type>
139dccl::arith::Model::symbol_to_cumulative_freq(symbol_type symbol, ModelState state)
const
141 const auto& c_freqs =
142 (state == ENCODER) ? encoder_cumulative_freqs_ : decoder_cumulative_freqs_;
144 auto c_freq_it = c_freqs.find(symbol);
145 std::pair<freq_type, freq_type> c_freq_range;
146 c_freq_range.second = c_freq_it->second;
147 if (c_freq_it == c_freqs.begin())
149 c_freq_range.first = 0;
154 c_freq_range.first = c_freq_it->second;
159std::pair<dccl::arith::Model::symbol_type, dccl::arith::Model::symbol_type>
160dccl::arith::Model::cumulative_freq_to_symbol(std::pair<freq_type, freq_type> c_freq_pair,
161 ModelState state)
const
163 const auto& c_freqs =
164 (state == ENCODER) ? encoder_cumulative_freqs_ : decoder_cumulative_freqs_;
166 std::pair<symbol_type, symbol_type> symbol_pair;
175 auto search = c_freq_pair.first;
176 for (
const auto& p : c_freqs)
178 if (search < p.second)
180 symbol_pair.first = p.first;
185 if (symbol_pair.first == c_freqs.rbegin()->first)
186 symbol_pair.second = symbol_pair.first;
187 else if (c_freqs.find(symbol_pair.first)->second > c_freq_pair.second)
188 symbol_pair.second = symbol_pair.first;
190 symbol_pair.second = symbol_pair.first + 1;
195void dccl::arith::Model::update_model(symbol_type symbol, ModelState state)
197 if (!user_model_.is_adaptive())
200 auto& c_freqs = (state == ENCODER) ? encoder_cumulative_freqs_ : decoder_cumulative_freqs_;
202 if (dlog.
check(DEBUG3))
204 dlog.
is(DEBUG3) && dlog <<
"Model was: " << std::endl;
205 for (symbol_type i = MIN_SYMBOL, n = max_symbol(); i <= n; ++i)
207 auto it = c_freqs.find(i);
208 if (it != c_freqs.end())
209 dlog.
is(DEBUG3) && dlog <<
"Symbol: " << it->first <<
", c_freq: " << it->second
214 for (symbol_type i = max_symbol(), n = symbol; i >= n; --i)
216 auto it = c_freqs.find(i);
217 if (it != c_freqs.end())
221 if (dlog.
check(DEBUG3))
223 dlog.
is(DEBUG3) && dlog <<
"Model is now: " << std::endl;
224 for (symbol_type i = MIN_SYMBOL, n = max_symbol(); i <= n; ++i)
226 auto it = c_freqs.find(i);
227 if (it != c_freqs.end())
228 dlog.
is(DEBUG3) && dlog <<
"Symbol: " << it->first <<
", c_freq: " << it->second
233 dlog.
is(DEBUG3) && dlog <<
"total freq: " << total_freq(state) << std::endl;
236void dccl::arith::ModelManager::set_model(
dccl::Codec& codec,
239 model_manager(codec.manager())._set_model(model);
246 auto model_manager = std::make_shared<dccl::any>(
ModelManager());
247 manager.codec_data().template set_codec_specific_data<ArithmeticFieldCodecBase<>>(
250 return dccl::any_cast<ModelManager&>(
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Exception class for DCCL.
A class for managing the various field codecs. Here you can add and remove field codecs....
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
bool check(logger::Verbosity verbosity)
Same as is() but doesn't set the verbosity or lock the mutex.
DCCL Arithmetic Encoder Library namespace.
Dynamic Compact Control Language namespace.