24 #include "field_codec_arithmetic.h"
26 #include "../field_codec_manager.h"
29 using namespace dccl::logger;
31 const dccl::arith::Model::symbol_type dccl::arith::Model::OUT_OF_RANGE_SYMBOL;
32 const dccl::arith::Model::symbol_type dccl::arith::Model::EOF_SYMBOL;
33 const dccl::arith::Model::symbol_type dccl::arith::Model::MIN_SYMBOL;
34 const int dccl::arith::Model::CODE_VALUE_BITS;
35 const int dccl::arith::Model::FREQUENCY_BITS;
36 const dccl::arith::Model::freq_type dccl::arith::Model::MAX_FREQUENCY;
38 #if DCCL_THREAD_SUPPORT
39 std::recursive_mutex dccl::arith::Model::last_bits_map_mutex;
41 std::map<std::string, std::map<std::string, dccl::Bitset>> dccl::arith::Model::last_bits_map;
102 dccl::arith::Model::symbol_type dccl::arith::Model::value_to_symbol(value_type value)
const
104 if (value < *user_model_.value_bound().begin() ||
105 value > *(user_model_.value_bound().end() - 1))
106 return Model::OUT_OF_RANGE_SYMBOL;
108 google::protobuf::RepeatedField<double>::const_iterator upper_it =
109 std::upper_bound(user_model_.value_bound().begin(), user_model_.value_bound().end(), value);
111 google::protobuf::RepeatedField<double>::const_iterator lower_it =
112 (upper_it == user_model_.value_bound().begin()) ? upper_it : upper_it - 1;
114 double lower_diff = std::abs((*lower_it) * (*lower_it) - value * value);
115 double upper_diff = std::abs((*upper_it) * (*upper_it) - value * value);
122 ((lower_diff < upper_diff) ? lower_it : upper_it) - user_model_.value_bound().begin();
127 dccl::arith::Model::value_type dccl::arith::Model::symbol_to_value(symbol_type symbol)
const
129 if (symbol == EOF_SYMBOL)
130 throw(
Exception(
"EOF symbol has no value."));
132 value_type value = (symbol == Model::OUT_OF_RANGE_SYMBOL)
133 ? std::numeric_limits<value_type>::quiet_NaN()
134 : user_model_.value_bound(symbol);
139 std::pair<dccl::arith::Model::freq_type, dccl::arith::Model::freq_type>
140 dccl::arith::Model::symbol_to_cumulative_freq(symbol_type symbol, ModelState state)
const
142 const auto& c_freqs =
143 (state == ENCODER) ? encoder_cumulative_freqs_ : decoder_cumulative_freqs_;
145 auto c_freq_it = c_freqs.find(symbol);
146 std::pair<freq_type, freq_type> c_freq_range;
147 c_freq_range.second = c_freq_it->second;
148 if (c_freq_it == c_freqs.begin())
150 c_freq_range.first = 0;
155 c_freq_range.first = c_freq_it->second;
160 std::pair<dccl::arith::Model::symbol_type, dccl::arith::Model::symbol_type>
161 dccl::arith::Model::cumulative_freq_to_symbol(std::pair<freq_type, freq_type> c_freq_pair,
162 ModelState state)
const
164 const auto& c_freqs =
165 (state == ENCODER) ? encoder_cumulative_freqs_ : decoder_cumulative_freqs_;
167 std::pair<symbol_type, symbol_type> symbol_pair;
176 auto search = c_freq_pair.first;
177 for (
const auto& p : c_freqs)
179 if (search < p.second)
181 symbol_pair.first = p.first;
186 if (symbol_pair.first == c_freqs.rbegin()->first)
187 symbol_pair.second = symbol_pair.first;
188 else if (c_freqs.find(symbol_pair.first)->second > c_freq_pair.second)
189 symbol_pair.second = symbol_pair.first;
191 symbol_pair.second = symbol_pair.first + 1;
196 void dccl::arith::Model::update_model(symbol_type symbol, ModelState state)
198 if (!user_model_.is_adaptive())
201 auto& c_freqs = (state == ENCODER) ? encoder_cumulative_freqs_ : decoder_cumulative_freqs_;
203 if (dlog.
check(DEBUG3))
205 dlog.
is(DEBUG3) && dlog <<
"Model was: " << std::endl;
206 for (symbol_type i = MIN_SYMBOL, n = max_symbol(); i <= n; ++i)
208 auto it = c_freqs.find(i);
209 if (it != c_freqs.end())
210 dlog.
is(DEBUG3) && dlog <<
"Symbol: " << it->first <<
", c_freq: " << it->second
215 for (symbol_type i = max_symbol(), n = symbol; i >= n; --i)
217 auto it = c_freqs.find(i);
218 if (it != c_freqs.end())
222 if (dlog.
check(DEBUG3))
224 dlog.
is(DEBUG3) && dlog <<
"Model is now: " << std::endl;
225 for (symbol_type i = MIN_SYMBOL, n = max_symbol(); i <= n; ++i)
227 auto it = c_freqs.find(i);
228 if (it != c_freqs.end())
229 dlog.
is(DEBUG3) && dlog <<
"Symbol: " << it->first <<
", c_freq: " << it->second
234 dlog.
is(DEBUG3) && dlog <<
"total freq: " << total_freq(state) << std::endl;
237 void dccl::arith::ModelManager::set_model(
dccl::Codec& codec,
240 model_manager(codec.manager())._set_model(model);
247 auto model_manager = std::make_shared<dccl::any>(
ModelManager());
248 manager.codec_data().template set_codec_specific_data<ArithmeticFieldCodecBase<>>(
251 return dccl::any_cast<ModelManager&>(