]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/logicsnapshot.cpp
Added a binding for SR_CONF_SAMPLERATE
[pulseview.git] / pv / data / logicsnapshot.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <extdef.h>
22
23#include <assert.h>
24#include <string.h>
25#include <stdlib.h>
26#include <math.h>
27
28#include <boost/foreach.hpp>
29
30#include "logicsnapshot.h"
31
32using namespace boost;
33using namespace std;
34
35namespace pv {
36namespace data {
37
38const int LogicSnapshot::MipMapScalePower = 4;
39const int LogicSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
40const float LogicSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
41const uint64_t LogicSnapshot::MipMapDataUnit = 64*1024; // bytes
42
43LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic) :
44 Snapshot(logic.unitsize),
45 _last_append_sample(0)
46{
47 lock_guard<recursive_mutex> lock(_mutex);
48 memset(_mip_map, 0, sizeof(_mip_map));
49 append_payload(logic);
50}
51
52LogicSnapshot::~LogicSnapshot()
53{
54 lock_guard<recursive_mutex> lock(_mutex);
55 BOOST_FOREACH(MipMapLevel &l, _mip_map)
56 free(l.data);
57}
58
59void LogicSnapshot::append_payload(
60 const sr_datafeed_logic &logic)
61{
62 assert(_unit_size == logic.unitsize);
63 assert((logic.length % _unit_size) == 0);
64
65 lock_guard<recursive_mutex> lock(_mutex);
66
67 append_data(logic.data, logic.length / _unit_size);
68
69 // Generate the first mip-map from the data
70 append_payload_to_mipmap();
71}
72
73void LogicSnapshot::reallocate_mip_map(MipMapLevel &m)
74{
75 const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
76 MipMapDataUnit) * MipMapDataUnit;
77 if (new_data_length > m.data_length)
78 {
79 m.data_length = new_data_length;
80
81 // Padding is added to allow for the uint64_t write word
82 m.data = realloc(m.data, new_data_length * _unit_size +
83 sizeof(uint64_t));
84 }
85}
86
87void LogicSnapshot::append_payload_to_mipmap()
88{
89 MipMapLevel &m0 = _mip_map[0];
90 uint64_t prev_length;
91 const uint8_t *src_ptr;
92 uint8_t *dest_ptr;
93 uint64_t accumulator;
94 unsigned int diff_counter;
95
96 // Expand the data buffer to fit the new samples
97 prev_length = m0.length;
98 m0.length = _sample_count / MipMapScaleFactor;
99
100 // Break off if there are no new samples to compute
101 if (m0.length == prev_length)
102 return;
103
104 reallocate_mip_map(m0);
105
106 dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
107
108 // Iterate through the samples to populate the first level mipmap
109 accumulator = 0;
110 diff_counter = MipMapScaleFactor;
111 const uint8_t *end_src_ptr = (uint8_t*)_data +
112 m0.length * _unit_size * MipMapScaleFactor;
113 for (src_ptr = (uint8_t*)_data +
114 prev_length * _unit_size * MipMapScaleFactor;
115 src_ptr < end_src_ptr;)
116 {
117 // Accumulate transitions which have occurred in this sample
118 accumulator = 0;
119 diff_counter = MipMapScaleFactor;
120 while (diff_counter-- > 0)
121 {
122 const uint64_t sample = *(uint64_t*)src_ptr;
123 accumulator |= _last_append_sample ^ sample;
124 _last_append_sample = sample;
125 src_ptr += _unit_size;
126 }
127
128 *(uint64_t*)dest_ptr = accumulator;
129 dest_ptr += _unit_size;
130 }
131
132 // Compute higher level mipmaps
133 for (unsigned int level = 1; level < ScaleStepCount; level++)
134 {
135 MipMapLevel &m = _mip_map[level];
136 const MipMapLevel &ml = _mip_map[level-1];
137
138 // Expand the data buffer to fit the new samples
139 prev_length = m.length;
140 m.length = ml.length / MipMapScaleFactor;
141
142 // Break off if there are no more samples to computed
143 if (m.length == prev_length)
144 break;
145
146 reallocate_mip_map(m);
147
148 // Subsample the level lower level
149 src_ptr = (uint8_t*)ml.data +
150 _unit_size * prev_length * MipMapScaleFactor;
151 const uint8_t *end_dest_ptr =
152 (uint8_t*)m.data + _unit_size * m.length;
153 for (dest_ptr = (uint8_t*)m.data +
154 _unit_size * prev_length;
155 dest_ptr < end_dest_ptr;
156 dest_ptr += _unit_size)
157 {
158 accumulator = 0;
159 diff_counter = MipMapScaleFactor;
160 while (diff_counter-- > 0)
161 {
162 accumulator |= *(uint64_t*)src_ptr;
163 src_ptr += _unit_size;
164 }
165
166 *(uint64_t*)dest_ptr = accumulator;
167 }
168 }
169}
170
171uint64_t LogicSnapshot::get_sample(uint64_t index) const
172{
173 assert(_data);
174 assert(index < _sample_count);
175
176 return *(uint64_t*)((uint8_t*)_data + index * _unit_size);
177}
178
179void LogicSnapshot::get_subsampled_edges(
180 std::vector<EdgePair> &edges,
181 uint64_t start, uint64_t end,
182 float min_length, int sig_index)
183{
184 uint64_t index = start;
185 unsigned int level;
186 bool last_sample;
187 bool fast_forward;
188
189 assert(end <= get_sample_count());
190 assert(start <= end);
191 assert(min_length > 0);
192 assert(sig_index >= 0);
193 assert(sig_index < SR_MAX_NUM_PROBES);
194
195 lock_guard<recursive_mutex> lock(_mutex);
196
197 const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
198 const unsigned int min_level = max((int)floorf(logf(min_length) /
199 LogMipMapScaleFactor) - 1, 0);
200 const uint64_t sig_mask = 1ULL << sig_index;
201
202 // Store the initial state
203 last_sample = (get_sample(start) & sig_mask) != 0;
204 edges.push_back(pair<int64_t, bool>(index++, last_sample));
205
206 while (index + block_length <= end)
207 {
208 //----- Continue to search -----//
209 level = min_level;
210
211 // We cannot fast-forward if there is no mip-map data at
212 // at the minimum level.
213 fast_forward = (_mip_map[level].data != NULL);
214
215 if (min_length < MipMapScaleFactor)
216 {
217 // Search individual samples up to the beginning of
218 // the next first level mip map block
219 const uint64_t final_index = min(end,
220 pow2_ceil(index, MipMapScalePower));
221
222 for (; index < final_index &&
223 (index & ~(~0 << MipMapScalePower)) != 0;
224 index++)
225 {
226 const bool sample =
227 (get_sample(index) & sig_mask) != 0;
228
229 // If there was a change we cannot fast forward
230 if (sample != last_sample) {
231 fast_forward = false;
232 break;
233 }
234 }
235 }
236 else
237 {
238 // If resolution is less than a mip map block,
239 // round up to the beginning of the mip-map block
240 // for this level of detail
241 const int min_level_scale_power =
242 (level + 1) * MipMapScalePower;
243 index = pow2_ceil(index, min_level_scale_power);
244 if (index >= end)
245 break;
246
247 // We can fast forward only if there was no change
248 const bool sample =
249 (get_sample(index) & sig_mask) != 0;
250 if (last_sample != sample)
251 fast_forward = false;
252 }
253
254 if (fast_forward) {
255
256 // Fast forward: This involves zooming out to higher
257 // levels of the mip map searching for changes, then
258 // zooming in on them to find the point where the edge
259 // begins.
260
261 // Slide right and zoom out at the beginnings of mip-map
262 // blocks until we encounter a change
263 while (1) {
264 const int level_scale_power =
265 (level + 1) * MipMapScalePower;
266 const uint64_t offset =
267 index >> level_scale_power;
268
269 // Check if we reached the last block at this
270 // level, or if there was a change in this block
271 if (offset >= _mip_map[level].length ||
272 (get_subsample(level, offset) &
273 sig_mask))
274 break;
275
276 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
277 // If we are now at the beginning of a
278 // higher level mip-map block ascend one
279 // level
280 if (level + 1 >= ScaleStepCount ||
281 !_mip_map[level + 1].data)
282 break;
283
284 level++;
285 } else {
286 // Slide right to the beginning of the
287 // next mip map block
288 index = pow2_ceil(index + 1,
289 level_scale_power);
290 }
291 }
292
293 // Zoom in, and slide right until we encounter a change,
294 // and repeat until we reach min_level
295 while (1) {
296 assert(_mip_map[level].data);
297
298 const int level_scale_power =
299 (level + 1) * MipMapScalePower;
300 const uint64_t offset =
301 index >> level_scale_power;
302
303 // Check if we reached the last block at this
304 // level, or if there was a change in this block
305 if (offset >= _mip_map[level].length ||
306 (get_subsample(level, offset) &
307 sig_mask)) {
308 // Zoom in unless we reached the minimum
309 // zoom
310 if (level == min_level)
311 break;
312
313 level--;
314 } else {
315 // Slide right to the beginning of the
316 // next mip map block
317 index = pow2_ceil(index + 1,
318 level_scale_power);
319 }
320 }
321
322 // If individual samples within the limit of resolution,
323 // do a linear search for the next transition within the
324 // block
325 if (min_length < MipMapScaleFactor) {
326 for (; index < end; index++) {
327 const bool sample = (get_sample(index) &
328 sig_mask) != 0;
329 if (sample != last_sample)
330 break;
331 }
332 }
333 }
334
335 //----- Store the edge -----//
336
337 // Take the last sample of the quanization block
338 const int64_t final_index = index + block_length;
339 if (index + block_length > end)
340 break;
341
342 // Store the final state
343 const bool final_sample =
344 (get_sample(final_index - 1) & sig_mask) != 0;
345 edges.push_back(pair<int64_t, bool>(index, final_sample));
346
347 index = final_index;
348 last_sample = final_sample;
349 }
350
351 // Add the final state
352 edges.push_back(pair<int64_t, bool>(end,
353 get_sample(end) & sig_mask));
354}
355
356uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
357{
358 assert(level >= 0);
359 assert(_mip_map[level].data);
360 return *(uint64_t*)((uint8_t*)_mip_map[level].data +
361 _unit_size * offset);
362}
363
364uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
365{
366 const uint64_t p = 1 << power;
367 return (x + p - 1) / p * p;
368}
369
370} // namespace data
371} // namespace pv