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 "logicsnapshot.h"
32 using boost::lock_guard;
33 using boost::recursive_mutex;
41 const int LogicSnapshot::MipMapScalePower = 4;
42 const int LogicSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
43 const float LogicSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
44 const uint64_t LogicSnapshot::MipMapDataUnit = 64*1024; // bytes
46 LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic,
47 const uint64_t expected_num_samples) :
48 Snapshot(logic.unitsize),
49 _last_append_sample(0)
51 set_capacity(expected_num_samples);
53 lock_guard<recursive_mutex> lock(_mutex);
54 memset(_mip_map, 0, sizeof(_mip_map));
55 append_payload(logic);
58 LogicSnapshot::~LogicSnapshot()
60 lock_guard<recursive_mutex> lock(_mutex);
61 BOOST_FOREACH(MipMapLevel &l, _mip_map)
65 uint64_t LogicSnapshot::unpack_sample(const uint8_t *ptr) const
70 value |= ((uint64_t)ptr[7]) << 56;
73 value |= ((uint64_t)ptr[6]) << 48;
76 value |= ((uint64_t)ptr[5]) << 40;
79 value |= ((uint64_t)ptr[4]) << 32;
82 value |= ((uint32_t)ptr[3]) << 24;
85 value |= ((uint32_t)ptr[2]) << 16;
99 void LogicSnapshot::pack_sample(uint8_t *ptr, uint64_t value)
103 ptr[7] = value >> 56;
106 ptr[6] = value >> 48;
109 ptr[5] = value >> 40;
112 ptr[4] = value >> 32;
115 ptr[3] = value >> 24;
118 ptr[2] = value >> 16;
131 void LogicSnapshot::append_payload(
132 const sr_datafeed_logic &logic)
134 assert(_unit_size == logic.unitsize);
135 assert((logic.length % _unit_size) == 0);
137 lock_guard<recursive_mutex> lock(_mutex);
139 append_data(logic.data, logic.length / _unit_size);
141 // Generate the first mip-map from the data
142 append_payload_to_mipmap();
145 void LogicSnapshot::get_samples(uint8_t *const data,
146 int64_t start_sample, int64_t end_sample) const
149 assert(start_sample >= 0);
150 assert(start_sample <= (int64_t)_sample_count);
151 assert(end_sample >= 0);
152 assert(end_sample <= (int64_t)_sample_count);
153 assert(start_sample <= end_sample);
155 lock_guard<recursive_mutex> lock(_mutex);
157 const size_t size = (end_sample - start_sample) * _unit_size;
158 memcpy(data, (const uint8_t*)_data + start_sample, size);
161 void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
163 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
164 MipMapDataUnit) * MipMapDataUnit;
165 if (new_data_length > m.data_length)
167 m.data_length = new_data_length;
169 // Padding is added to allow for the uint64_t write word
170 m.data = realloc(m.data, new_data_length * _unit_size +
175 void LogicSnapshot::append_payload_to_mipmap()
177 MipMapLevel &m0 = _mip_map[0];
178 uint64_t prev_length;
179 const uint8_t *src_ptr;
181 uint64_t accumulator;
182 unsigned int diff_counter;
184 // Expand the data buffer to fit the new samples
185 prev_length = m0.length;
186 m0.length = _sample_count / MipMapScaleFactor;
188 // Break off if there are no new samples to compute
189 if (m0.length == prev_length)
192 reallocate_mipmap_level(m0);
194 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
196 // Iterate through the samples to populate the first level mipmap
197 const uint8_t *const end_src_ptr = (uint8_t*)_data +
198 m0.length * _unit_size * MipMapScaleFactor;
199 for (src_ptr = (uint8_t*)_data +
200 prev_length * _unit_size * MipMapScaleFactor;
201 src_ptr < end_src_ptr;)
203 // Accumulate transitions which have occurred in this sample
205 diff_counter = MipMapScaleFactor;
206 while (diff_counter-- > 0)
208 const uint64_t sample = unpack_sample(src_ptr);
209 accumulator |= _last_append_sample ^ sample;
210 _last_append_sample = sample;
211 src_ptr += _unit_size;
214 pack_sample(dest_ptr, accumulator);
215 dest_ptr += _unit_size;
218 // Compute higher level mipmaps
219 for (unsigned int level = 1; level < ScaleStepCount; level++)
221 MipMapLevel &m = _mip_map[level];
222 const MipMapLevel &ml = _mip_map[level-1];
224 // Expand the data buffer to fit the new samples
225 prev_length = m.length;
226 m.length = ml.length / MipMapScaleFactor;
228 // Break off if there are no more samples to computed
229 if (m.length == prev_length)
232 reallocate_mipmap_level(m);
234 // Subsample the level lower level
235 src_ptr = (uint8_t*)ml.data +
236 _unit_size * prev_length * MipMapScaleFactor;
237 const uint8_t *const end_dest_ptr =
238 (uint8_t*)m.data + _unit_size * m.length;
239 for (dest_ptr = (uint8_t*)m.data +
240 _unit_size * prev_length;
241 dest_ptr < end_dest_ptr;
242 dest_ptr += _unit_size)
245 diff_counter = MipMapScaleFactor;
246 while (diff_counter-- > 0)
248 accumulator |= unpack_sample(src_ptr);
249 src_ptr += _unit_size;
252 pack_sample(dest_ptr, accumulator);
257 uint64_t LogicSnapshot::get_sample(uint64_t index) const
260 assert(index < _sample_count);
262 return unpack_sample((uint8_t*)_data + index * _unit_size);
265 void LogicSnapshot::get_subsampled_edges(
266 std::vector<EdgePair> &edges,
267 uint64_t start, uint64_t end,
268 float min_length, int sig_index)
270 uint64_t index = start;
275 assert(end <= get_sample_count());
276 assert(start <= end);
277 assert(min_length > 0);
278 assert(sig_index >= 0);
279 assert(sig_index < 64);
281 lock_guard<recursive_mutex> lock(_mutex);
283 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
284 const unsigned int min_level = max((int)floorf(logf(min_length) /
285 LogMipMapScaleFactor) - 1, 0);
286 const uint64_t sig_mask = 1ULL << sig_index;
288 // Store the initial state
289 last_sample = (get_sample(start) & sig_mask) != 0;
290 edges.push_back(pair<int64_t, bool>(index++, last_sample));
292 while (index + block_length <= end)
294 //----- Continue to search -----//
297 // We cannot fast-forward if there is no mip-map data at
298 // at the minimum level.
299 fast_forward = (_mip_map[level].data != NULL);
301 if (min_length < MipMapScaleFactor)
303 // Search individual samples up to the beginning of
304 // the next first level mip map block
305 const uint64_t final_index = min(end,
306 pow2_ceil(index, MipMapScalePower));
308 for (; index < final_index &&
309 (index & ~(~0 << MipMapScalePower)) != 0;
313 (get_sample(index) & sig_mask) != 0;
315 // If there was a change we cannot fast forward
316 if (sample != last_sample) {
317 fast_forward = false;
324 // If resolution is less than a mip map block,
325 // round up to the beginning of the mip-map block
326 // for this level of detail
327 const int min_level_scale_power =
328 (level + 1) * MipMapScalePower;
329 index = pow2_ceil(index, min_level_scale_power);
333 // We can fast forward only if there was no change
335 (get_sample(index) & sig_mask) != 0;
336 if (last_sample != sample)
337 fast_forward = false;
342 // Fast forward: This involves zooming out to higher
343 // levels of the mip map searching for changes, then
344 // zooming in on them to find the point where the edge
347 // Slide right and zoom out at the beginnings of mip-map
348 // blocks until we encounter a change
350 const int level_scale_power =
351 (level + 1) * MipMapScalePower;
352 const uint64_t offset =
353 index >> level_scale_power;
355 // Check if we reached the last block at this
356 // level, or if there was a change in this block
357 if (offset >= _mip_map[level].length ||
358 (get_subsample(level, offset) &
362 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
363 // If we are now at the beginning of a
364 // higher level mip-map block ascend one
366 if (level + 1 >= ScaleStepCount ||
367 !_mip_map[level + 1].data)
372 // Slide right to the beginning of the
373 // next mip map block
374 index = pow2_ceil(index + 1,
379 // Zoom in, and slide right until we encounter a change,
380 // and repeat until we reach min_level
382 assert(_mip_map[level].data);
384 const int level_scale_power =
385 (level + 1) * MipMapScalePower;
386 const uint64_t offset =
387 index >> level_scale_power;
389 // Check if we reached the last block at this
390 // level, or if there was a change in this block
391 if (offset >= _mip_map[level].length ||
392 (get_subsample(level, offset) &
394 // Zoom in unless we reached the minimum
396 if (level == min_level)
401 // Slide right to the beginning of the
402 // next mip map block
403 index = pow2_ceil(index + 1,
408 // If individual samples within the limit of resolution,
409 // do a linear search for the next transition within the
411 if (min_length < MipMapScaleFactor) {
412 for (; index < end; index++) {
413 const bool sample = (get_sample(index) &
415 if (sample != last_sample)
421 //----- Store the edge -----//
423 // Take the last sample of the quanization block
424 const int64_t final_index = index + block_length;
425 if (index + block_length > end)
428 // Store the final state
429 const bool final_sample =
430 (get_sample(final_index - 1) & sig_mask) != 0;
431 edges.push_back(pair<int64_t, bool>(index, final_sample));
434 last_sample = final_sample;
437 // Add the final state
438 const bool end_sample = get_sample(end) & sig_mask;
439 if (last_sample != end_sample)
440 edges.push_back(pair<int64_t, bool>(end, end_sample));
441 edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
444 uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
447 assert(_mip_map[level].data);
448 return unpack_sample((uint8_t*)_mip_map[level].data +
449 _unit_size * offset);
452 uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
454 const uint64_t p = 1 << power;
455 return (x + p - 1) / p * p;