25 #include "field_codec_arithmetic.h"
27 #include "../field_codec_manager.h"
30 using namespace dccl::logger;
32 const dccl::arith::Model::symbol_type dccl::arith::Model::OUT_OF_RANGE_SYMBOL;
33 const dccl::arith::Model::symbol_type dccl::arith::Model::EOF_SYMBOL;
34 const dccl::arith::Model::symbol_type dccl::arith::Model::MIN_SYMBOL;
35 const int dccl::arith::Model::CODE_VALUE_BITS;
36 const int dccl::arith::Model::FREQUENCY_BITS;
37 const dccl::arith::Model::freq_type dccl::arith::Model::MAX_FREQUENCY;
39 #if DCCL_THREAD_SUPPORT
40 std::recursive_mutex dccl::arith::Model::last_bits_map_mutex;
42 std::map<std::string, std::map<std::string, dccl::Bitset>> dccl::arith::Model::last_bits_map;
103 dccl::arith::Model::symbol_type dccl::arith::Model::value_to_symbol(value_type value)
const
105 if (value < *user_model_.value_bound().begin() ||
106 value > *(user_model_.value_bound().end() - 1))
107 return Model::OUT_OF_RANGE_SYMBOL;
109 google::protobuf::RepeatedField<double>::const_iterator upper_it =
110 std::upper_bound(user_model_.value_bound().begin(), user_model_.value_bound().end(), value);
112 google::protobuf::RepeatedField<double>::const_iterator lower_it =
113 (upper_it == user_model_.value_bound().begin()) ? upper_it : upper_it - 1;
115 double lower_diff = std::abs((*lower_it) * (*lower_it) - value * value);
116 double upper_diff = std::abs((*upper_it) * (*upper_it) - value * value);
123 ((lower_diff < upper_diff) ? lower_it : upper_it) - user_model_.value_bound().begin();
128 dccl::arith::Model::value_type dccl::arith::Model::symbol_to_value(symbol_type symbol)
const
130 if (symbol == EOF_SYMBOL)
131 throw(
Exception(
"EOF symbol has no value."));
133 value_type value = (symbol == Model::OUT_OF_RANGE_SYMBOL)
134 ? std::numeric_limits<value_type>::quiet_NaN()
135 : user_model_.value_bound(symbol);
140 std::pair<dccl::arith::Model::freq_type, dccl::arith::Model::freq_type>
141 dccl::arith::Model::symbol_to_cumulative_freq(symbol_type symbol, ModelState state)
const
143 const auto& c_freqs =
144 (state == ENCODER) ? encoder_cumulative_freqs_ : decoder_cumulative_freqs_;
146 auto c_freq_it = c_freqs.find(symbol);
147 std::pair<freq_type, freq_type> c_freq_range;
148 c_freq_range.second = c_freq_it->second;
149 if (c_freq_it == c_freqs.begin())
151 c_freq_range.first = 0;
156 c_freq_range.first = c_freq_it->second;
161 std::pair<dccl::arith::Model::symbol_type, dccl::arith::Model::symbol_type>
162 dccl::arith::Model::cumulative_freq_to_symbol(std::pair<freq_type, freq_type> c_freq_pair,
163 ModelState state)
const
165 const auto& c_freqs =
166 (state == ENCODER) ? encoder_cumulative_freqs_ : decoder_cumulative_freqs_;
168 std::pair<symbol_type, symbol_type> symbol_pair;
177 auto search = c_freq_pair.first;
178 for (
const auto& p : c_freqs)
180 if (search < p.second)
182 symbol_pair.first = p.first;
187 if (symbol_pair.first == c_freqs.rbegin()->first)
188 symbol_pair.second = symbol_pair.first;
189 else if (c_freqs.find(symbol_pair.first)->second > c_freq_pair.second)
190 symbol_pair.second = symbol_pair.first;
192 symbol_pair.second = symbol_pair.first + 1;
197 void dccl::arith::Model::update_model(symbol_type symbol, ModelState state)
199 if (!user_model_.is_adaptive())
202 auto& c_freqs = (state == ENCODER) ? encoder_cumulative_freqs_ : decoder_cumulative_freqs_;
204 if (dlog.
check(DEBUG3))
206 dlog.
is(DEBUG3) && dlog <<
"Model was: " << std::endl;
207 for (symbol_type i = MIN_SYMBOL, n = max_symbol(); i <= n; ++i)
209 auto it = c_freqs.find(i);
210 if (it != c_freqs.end())
211 dlog.
is(DEBUG3) && dlog <<
"Symbol: " << it->first <<
", c_freq: " << it->second
216 for (symbol_type i = max_symbol(), n = symbol; i >= n; --i)
218 auto it = c_freqs.find(i);
219 if (it != c_freqs.end())
223 if (dlog.
check(DEBUG3))
225 dlog.
is(DEBUG3) && dlog <<
"Model is now: " << std::endl;
226 for (symbol_type i = MIN_SYMBOL, n = max_symbol(); i <= n; ++i)
228 auto it = c_freqs.find(i);
229 if (it != c_freqs.end())
230 dlog.
is(DEBUG3) && dlog <<
"Symbol: " << it->first <<
", c_freq: " << it->second
235 dlog.
is(DEBUG3) && dlog <<
"total freq: " << total_freq(state) << std::endl;
238 void dccl::arith::ModelManager::set_model(
dccl::Codec& codec,
241 model_manager(codec.manager())._set_model(model);
248 auto model_manager = std::make_shared<dccl::any>(
ModelManager());
249 manager.codec_data().template set_codec_specific_data<ArithmeticFieldCodecBase<>>(
252 return dccl::any_cast<ModelManager&>(