2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <boost/foreach.hpp>
30 #include "logicdatasnapshot.h"
34 const int LogicDataSnapshot::MipMapScalePower = 4;
35 const int LogicDataSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
36 const float LogicDataSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
37 const uint64_t LogicDataSnapshot::MipMapDataUnit = 64*1024; // bytes
39 LogicDataSnapshot::LogicDataSnapshot(
40 const sr_datafeed_logic &logic) :
41 DataSnapshot(logic.unitsize),
42 _last_append_sample(0)
44 memset(_mip_map, 0, sizeof(_mip_map));
45 append_payload(logic);
48 LogicDataSnapshot::~LogicDataSnapshot()
50 BOOST_FOREACH(MipMapLevel &l, _mip_map)
54 void LogicDataSnapshot::append_payload(
55 const sr_datafeed_logic &logic)
57 assert(_unit_size == logic.unitsize);
59 append_data(logic.data, logic.length);
61 // Generate the first mip-map from the data
62 append_payload_to_mipmap();
65 void LogicDataSnapshot::reallocate_mip_map(MipMapLevel &m)
67 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
68 MipMapDataUnit) * MipMapDataUnit;
69 if(new_data_length > m.data_length)
71 m.data_length = new_data_length;
72 m.data = realloc(m.data, new_data_length * _unit_size);
76 void LogicDataSnapshot::append_payload_to_mipmap()
78 MipMapLevel &m0 = _mip_map[0];
80 const uint8_t *src_ptr;
83 unsigned int diff_counter;
85 // Expand the data buffer to fit the new samples
86 prev_length = m0.length;
87 m0.length = _sample_count / MipMapScaleFactor;
89 // Break off if there are no new samples to compute
90 if(m0.length == prev_length)
93 reallocate_mip_map(m0);
95 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
97 // Iterate through the samples to populate the first level mipmap
99 diff_counter = MipMapScaleFactor;
100 const uint8_t *end_src_ptr = (uint8_t*)_data +
101 m0.length * _unit_size * MipMapScaleFactor;
102 for(src_ptr = (uint8_t*)_data +
103 prev_length * _unit_size * MipMapScaleFactor;
104 src_ptr < end_src_ptr;)
106 // Accumulate transitions which have occurred in this sample
108 diff_counter = MipMapScaleFactor;
109 while(diff_counter-- > 0)
111 const uint64_t sample = *(uint64_t*)src_ptr;
112 accumulator |= _last_append_sample ^ sample;
113 _last_append_sample = sample;
114 src_ptr += _unit_size;
117 *(uint64_t*)dest_ptr = accumulator;
118 dest_ptr += _unit_size;
121 // Compute higher level mipmaps
122 for(int level = 1; level < ScaleStepCount; level++)
124 MipMapLevel &m = _mip_map[level];
125 const MipMapLevel &ml = _mip_map[level-1];
127 // Expand the data buffer to fit the new samples
128 prev_length = m.length;
129 m.length = ml.length / MipMapScaleFactor;
131 // Break off if there are no more samples to computed
132 if(m.length == prev_length)
135 reallocate_mip_map(m);
137 // Subsample the level lower level
138 src_ptr = (uint8_t*)ml.data +
139 _unit_size * prev_length * MipMapScaleFactor;
140 const uint8_t *end_dest_ptr =
141 (uint8_t*)m.data + _unit_size * m.length;
142 for(dest_ptr = (uint8_t*)m.data +
143 _unit_size * prev_length;
144 dest_ptr < end_dest_ptr;
145 dest_ptr += _unit_size)
148 diff_counter = MipMapScaleFactor;
149 while(diff_counter-- > 0)
151 accumulator |= *(uint64_t*)src_ptr;
152 src_ptr += _unit_size;
155 *(uint64_t*)dest_ptr = accumulator;
160 uint64_t LogicDataSnapshot::get_sample(uint64_t index) const
163 assert(index >= 0 && index < _sample_count);
165 return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
168 void LogicDataSnapshot::get_subsampled_edges(
169 std::vector<EdgePair> &edges,
170 int64_t start, int64_t end,
171 float min_length, int sig_index)
173 int64_t index = start;
179 assert(end <= get_sample_count());
180 assert(start <= end);
181 assert(min_length > 0);
182 assert(sig_index >= 0);
183 assert(sig_index < SR_MAX_NUM_PROBES);
185 const int64_t block_length = (int64_t)max(min_length, 1.0f);
186 const int min_level = max((int)floorf(logf(min_length) /
187 LogMipMapScaleFactor) - 1, 0);
188 const uint64_t sig_mask = 1ULL << sig_index;
190 // Store the initial state
191 last_sample = (get_sample(start) & sig_mask) != 0;
192 edges.push_back(pair<int64_t, bool>(index++, last_sample));
194 while(index + block_length <= end)
196 //----- Continue to search -----//
200 if(min_length < MipMapScaleFactor)
202 // Search individual samples up to the beginning of
203 // the next first level mip map block
204 const uint64_t final_index = min(end,
205 pow2_ceil(index, MipMapScalePower));
208 index < final_index &&
209 (index & ~(~0 << MipMapScalePower)) != 0;
213 (get_sample(index) & sig_mask) != 0;
215 // If there was a change we cannot fast forward
216 if(sample != last_sample) {
217 fast_forward = false;
224 // If resolution is less than a mip map block,
225 // round up to the beginning of the mip-map block
226 // for this level of detail
227 const int min_level_scale_power =
228 (level + 1) * MipMapScalePower;
229 index = pow2_ceil(index, min_level_scale_power);
233 // We can fast forward only if there was no change
235 (get_sample(index) & sig_mask) != 0;
236 fast_forward = last_sample == sample;
241 // Fast forward: This involves zooming out to higher
242 // levels of the mip map searching for changes, then
243 // zooming in on them to find the point where the edge
246 // Slide right and zoom out at the beginnings of mip-map
247 // blocks until we encounter a change
249 const int level_scale_power =
250 (level + 1) * MipMapScalePower;
251 const uint64_t offset =
252 index >> level_scale_power;
255 // Check if we reached the last block at this
256 // level, or if there was a change in this block
257 if(offset >= _mip_map[level].length ||
258 (get_subsample(level, offset) &
262 if((offset & ~(~0 << MipMapScalePower)) == 0) {
263 // If we are now at the beginning of a
264 // higher level mip-map block ascend one
266 if(level + 1 >= ScaleStepCount ||
267 !_mip_map[level + 1].data)
272 // Slide right to the beginning of the
273 // next mip map block
274 index = pow2_ceil(index + 1,
279 // Zoom in, and slide right until we encounter a change,
280 // and repeat until we reach min_level
282 assert(_mip_map[level].data);
284 const int level_scale_power =
285 (level + 1) * MipMapScalePower;
286 const uint64_t offset =
287 index >> level_scale_power;
290 // Check if we reached the last block at this
291 // level, or if there was a change in this block
292 if(offset >= _mip_map[level].length ||
293 (get_subsample(level, offset) &
295 // Zoom in unless we reached the minimum
297 if(level == min_level)
302 // Slide right to the beginning of the
303 // next mip map block
304 index = pow2_ceil(index + 1,
309 // If individual samples within the limit of resolution,
310 // do a linear search for the next transition within the
312 if(min_length < MipMapScaleFactor) {
313 for(index; index < end; index++) {
314 const bool sample = (get_sample(index) &
316 if(sample != last_sample)
322 //----- Store the edge -----//
324 // Take the last sample of the quanization block
325 const int64_t final_index = index + block_length;
326 if(index + block_length > end)
329 // Store the final state
330 const bool final_sample =
331 (get_sample(final_index - 1) & sig_mask) != 0;
332 edges.push_back(pair<int64_t, bool>(index, final_sample));
335 last_sample = final_sample;
338 // Add the final state
339 edges.push_back(pair<int64_t, bool>(end,
340 get_sample(end) & sig_mask));
343 uint64_t LogicDataSnapshot::get_subsample(int level, uint64_t offset) const
346 assert(_mip_map[level].data);
347 return *(uint64_t*)((uint8_t*)_mip_map[level].data +
348 _unit_size * offset);
351 int64_t LogicDataSnapshot::pow2_ceil(int64_t x, unsigned int power)
353 const int64_t p = 1 << power;
354 return ((x < 0) ? x : (x + p - 1)) / p * p;