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
29 #include "logicsnapshot.h"
31 using std::lock_guard;
32 using std::recursive_mutex;
40 const int LogicSnapshot::MipMapScalePower = 4;
41 const int LogicSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
42 const float LogicSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
43 const uint64_t LogicSnapshot::MipMapDataUnit = 64*1024; // bytes
45 LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic,
46 const uint64_t expected_num_samples) :
47 Snapshot(logic.unitsize),
48 _last_append_sample(0)
50 set_capacity(expected_num_samples);
52 lock_guard<recursive_mutex> lock(_mutex);
53 memset(_mip_map, 0, sizeof(_mip_map));
54 append_payload(logic);
57 LogicSnapshot::~LogicSnapshot()
59 lock_guard<recursive_mutex> lock(_mutex);
60 for (MipMapLevel &l : _mip_map)
64 uint64_t LogicSnapshot::unpack_sample(const uint8_t *ptr) const
66 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
67 return *(uint64_t*)ptr;
72 value |= ((uint64_t)ptr[7]) << 56;
75 value |= ((uint64_t)ptr[6]) << 48;
78 value |= ((uint64_t)ptr[5]) << 40;
81 value |= ((uint64_t)ptr[4]) << 32;
84 value |= ((uint32_t)ptr[3]) << 24;
87 value |= ((uint32_t)ptr[2]) << 16;
102 void LogicSnapshot::pack_sample(uint8_t *ptr, uint64_t value)
104 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
105 *(uint64_t*)ptr = value;
109 ptr[7] = value >> 56;
112 ptr[6] = value >> 48;
115 ptr[5] = value >> 40;
118 ptr[4] = value >> 32;
121 ptr[3] = value >> 24;
124 ptr[2] = value >> 16;
138 void LogicSnapshot::append_payload(
139 const sr_datafeed_logic &logic)
141 assert(_unit_size == logic.unitsize);
142 assert((logic.length % _unit_size) == 0);
144 lock_guard<recursive_mutex> lock(_mutex);
146 append_data(logic.data, logic.length / _unit_size);
148 // Generate the first mip-map from the data
149 append_payload_to_mipmap();
152 void LogicSnapshot::get_samples(uint8_t *const data,
153 int64_t start_sample, int64_t end_sample) const
156 assert(start_sample >= 0);
157 assert(start_sample <= (int64_t)_sample_count);
158 assert(end_sample >= 0);
159 assert(end_sample <= (int64_t)_sample_count);
160 assert(start_sample <= end_sample);
162 lock_guard<recursive_mutex> lock(_mutex);
164 const size_t size = (end_sample - start_sample) * _unit_size;
165 memcpy(data, (const uint8_t*)_data + start_sample * _unit_size, size);
168 void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
170 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
171 MipMapDataUnit) * MipMapDataUnit;
172 if (new_data_length > m.data_length)
174 m.data_length = new_data_length;
176 // Padding is added to allow for the uint64_t write word
177 m.data = realloc(m.data, new_data_length * _unit_size +
182 void LogicSnapshot::append_payload_to_mipmap()
184 MipMapLevel &m0 = _mip_map[0];
185 uint64_t prev_length;
186 const uint8_t *src_ptr;
188 uint64_t accumulator;
189 unsigned int diff_counter;
191 // Expand the data buffer to fit the new samples
192 prev_length = m0.length;
193 m0.length = _sample_count / MipMapScaleFactor;
195 // Break off if there are no new samples to compute
196 if (m0.length == prev_length)
199 reallocate_mipmap_level(m0);
201 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
203 // Iterate through the samples to populate the first level mipmap
204 const uint8_t *const end_src_ptr = (uint8_t*)_data +
205 m0.length * _unit_size * MipMapScaleFactor;
206 for (src_ptr = (uint8_t*)_data +
207 prev_length * _unit_size * MipMapScaleFactor;
208 src_ptr < end_src_ptr;)
210 // Accumulate transitions which have occurred in this sample
212 diff_counter = MipMapScaleFactor;
213 while (diff_counter-- > 0)
215 const uint64_t sample = unpack_sample(src_ptr);
216 accumulator |= _last_append_sample ^ sample;
217 _last_append_sample = sample;
218 src_ptr += _unit_size;
221 pack_sample(dest_ptr, accumulator);
222 dest_ptr += _unit_size;
225 // Compute higher level mipmaps
226 for (unsigned int level = 1; level < ScaleStepCount; level++)
228 MipMapLevel &m = _mip_map[level];
229 const MipMapLevel &ml = _mip_map[level-1];
231 // Expand the data buffer to fit the new samples
232 prev_length = m.length;
233 m.length = ml.length / MipMapScaleFactor;
235 // Break off if there are no more samples to computed
236 if (m.length == prev_length)
239 reallocate_mipmap_level(m);
241 // Subsample the level lower level
242 src_ptr = (uint8_t*)ml.data +
243 _unit_size * prev_length * MipMapScaleFactor;
244 const uint8_t *const end_dest_ptr =
245 (uint8_t*)m.data + _unit_size * m.length;
246 for (dest_ptr = (uint8_t*)m.data +
247 _unit_size * prev_length;
248 dest_ptr < end_dest_ptr;
249 dest_ptr += _unit_size)
252 diff_counter = MipMapScaleFactor;
253 while (diff_counter-- > 0)
255 accumulator |= unpack_sample(src_ptr);
256 src_ptr += _unit_size;
259 pack_sample(dest_ptr, accumulator);
264 uint64_t LogicSnapshot::get_sample(uint64_t index) const
267 assert(index < _sample_count);
269 return unpack_sample((uint8_t*)_data + index * _unit_size);
272 void LogicSnapshot::get_subsampled_edges(
273 std::vector<EdgePair> &edges,
274 uint64_t start, uint64_t end,
275 float min_length, int sig_index)
277 uint64_t index = start;
282 assert(end <= get_sample_count());
283 assert(start <= end);
284 assert(min_length > 0);
285 assert(sig_index >= 0);
286 assert(sig_index < 64);
288 lock_guard<recursive_mutex> lock(_mutex);
290 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
291 const unsigned int min_level = max((int)floorf(logf(min_length) /
292 LogMipMapScaleFactor) - 1, 0);
293 const uint64_t sig_mask = 1ULL << sig_index;
295 // Store the initial state
296 last_sample = (get_sample(start) & sig_mask) != 0;
297 edges.push_back(pair<int64_t, bool>(index++, last_sample));
299 while (index + block_length <= end)
301 //----- Continue to search -----//
304 // We cannot fast-forward if there is no mip-map data at
305 // at the minimum level.
306 fast_forward = (_mip_map[level].data != NULL);
308 if (min_length < MipMapScaleFactor)
310 // Search individual samples up to the beginning of
311 // the next first level mip map block
312 const uint64_t final_index = min(end,
313 pow2_ceil(index, MipMapScalePower));
315 for (; index < final_index &&
316 (index & ~(~0 << MipMapScalePower)) != 0;
320 (get_sample(index) & sig_mask) != 0;
322 // If there was a change we cannot fast forward
323 if (sample != last_sample) {
324 fast_forward = false;
331 // If resolution is less than a mip map block,
332 // round up to the beginning of the mip-map block
333 // for this level of detail
334 const int min_level_scale_power =
335 (level + 1) * MipMapScalePower;
336 index = pow2_ceil(index, min_level_scale_power);
340 // We can fast forward only if there was no change
342 (get_sample(index) & sig_mask) != 0;
343 if (last_sample != sample)
344 fast_forward = false;
349 // Fast forward: This involves zooming out to higher
350 // levels of the mip map searching for changes, then
351 // zooming in on them to find the point where the edge
354 // Slide right and zoom out at the beginnings of mip-map
355 // blocks until we encounter a change
357 const int level_scale_power =
358 (level + 1) * MipMapScalePower;
359 const uint64_t offset =
360 index >> level_scale_power;
362 // Check if we reached the last block at this
363 // level, or if there was a change in this block
364 if (offset >= _mip_map[level].length ||
365 (get_subsample(level, offset) &
369 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
370 // If we are now at the beginning of a
371 // higher level mip-map block ascend one
373 if (level + 1 >= ScaleStepCount ||
374 !_mip_map[level + 1].data)
379 // Slide right to the beginning of the
380 // next mip map block
381 index = pow2_ceil(index + 1,
386 // Zoom in, and slide right until we encounter a change,
387 // and repeat until we reach min_level
389 assert(_mip_map[level].data);
391 const int level_scale_power =
392 (level + 1) * MipMapScalePower;
393 const uint64_t offset =
394 index >> level_scale_power;
396 // Check if we reached the last block at this
397 // level, or if there was a change in this block
398 if (offset >= _mip_map[level].length ||
399 (get_subsample(level, offset) &
401 // Zoom in unless we reached the minimum
403 if (level == min_level)
408 // Slide right to the beginning of the
409 // next mip map block
410 index = pow2_ceil(index + 1,
415 // If individual samples within the limit of resolution,
416 // do a linear search for the next transition within the
418 if (min_length < MipMapScaleFactor) {
419 for (; index < end; index++) {
420 const bool sample = (get_sample(index) &
422 if (sample != last_sample)
428 //----- Store the edge -----//
430 // Take the last sample of the quanization block
431 const int64_t final_index = index + block_length;
432 if (index + block_length > end)
435 // Store the final state
436 const bool final_sample =
437 (get_sample(final_index - 1) & sig_mask) != 0;
438 edges.push_back(pair<int64_t, bool>(index, final_sample));
441 last_sample = final_sample;
444 // Add the final state
445 const bool end_sample = get_sample(end) & sig_mask;
446 if (last_sample != end_sample)
447 edges.push_back(pair<int64_t, bool>(end, end_sample));
448 edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
451 uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
454 assert(_mip_map[level].data);
455 return unpack_sample((uint8_t*)_mip_map[level].data +
456 _unit_size * offset);
459 uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
461 const uint64_t p = 1 << power;
462 return (x + p - 1) / p * p;