]>
Commit | Line | Data |
---|---|---|
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 "config.h" | |
29 | #include "logicsnapshot.h" | |
30 | ||
31 | using std::lock_guard; | |
32 | using std::recursive_mutex; | |
33 | using std::max; | |
34 | using std::min; | |
35 | using std::pair; | |
36 | ||
37 | namespace pv { | |
38 | namespace data { | |
39 | ||
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 | |
44 | ||
45 | LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic, | |
46 | const uint64_t expected_num_samples) : | |
47 | Snapshot(logic.unitsize), | |
48 | _last_append_sample(0) | |
49 | { | |
50 | set_capacity(expected_num_samples); | |
51 | ||
52 | lock_guard<recursive_mutex> lock(_mutex); | |
53 | memset(_mip_map, 0, sizeof(_mip_map)); | |
54 | append_payload(logic); | |
55 | } | |
56 | ||
57 | LogicSnapshot::~LogicSnapshot() | |
58 | { | |
59 | lock_guard<recursive_mutex> lock(_mutex); | |
60 | for (MipMapLevel &l : _mip_map) | |
61 | free(l.data); | |
62 | } | |
63 | ||
64 | uint64_t LogicSnapshot::unpack_sample(const uint8_t *ptr) const | |
65 | { | |
66 | #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS | |
67 | return *(uint64_t*)ptr; | |
68 | #else | |
69 | uint64_t value = 0; | |
70 | switch(_unit_size) { | |
71 | default: | |
72 | value |= ((uint64_t)ptr[7]) << 56; | |
73 | /* FALLTHRU */ | |
74 | case 7: | |
75 | value |= ((uint64_t)ptr[6]) << 48; | |
76 | /* FALLTHRU */ | |
77 | case 6: | |
78 | value |= ((uint64_t)ptr[5]) << 40; | |
79 | /* FALLTHRU */ | |
80 | case 5: | |
81 | value |= ((uint64_t)ptr[4]) << 32; | |
82 | /* FALLTHRU */ | |
83 | case 4: | |
84 | value |= ((uint32_t)ptr[3]) << 24; | |
85 | /* FALLTHRU */ | |
86 | case 3: | |
87 | value |= ((uint32_t)ptr[2]) << 16; | |
88 | /* FALLTHRU */ | |
89 | case 2: | |
90 | value |= ptr[1] << 8; | |
91 | /* FALLTHRU */ | |
92 | case 1: | |
93 | value |= ptr[0]; | |
94 | /* FALLTHRU */ | |
95 | case 0: | |
96 | break; | |
97 | } | |
98 | return value; | |
99 | #endif | |
100 | } | |
101 | ||
102 | void LogicSnapshot::pack_sample(uint8_t *ptr, uint64_t value) | |
103 | { | |
104 | #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS | |
105 | *(uint64_t*)ptr = value; | |
106 | #else | |
107 | switch(_unit_size) { | |
108 | default: | |
109 | ptr[7] = value >> 56; | |
110 | /* FALLTHRU */ | |
111 | case 7: | |
112 | ptr[6] = value >> 48; | |
113 | /* FALLTHRU */ | |
114 | case 6: | |
115 | ptr[5] = value >> 40; | |
116 | /* FALLTHRU */ | |
117 | case 5: | |
118 | ptr[4] = value >> 32; | |
119 | /* FALLTHRU */ | |
120 | case 4: | |
121 | ptr[3] = value >> 24; | |
122 | /* FALLTHRU */ | |
123 | case 3: | |
124 | ptr[2] = value >> 16; | |
125 | /* FALLTHRU */ | |
126 | case 2: | |
127 | ptr[1] = value >> 8; | |
128 | /* FALLTHRU */ | |
129 | case 1: | |
130 | ptr[0] = value; | |
131 | /* FALLTHRU */ | |
132 | case 0: | |
133 | break; | |
134 | } | |
135 | #endif | |
136 | } | |
137 | ||
138 | void LogicSnapshot::append_payload( | |
139 | const sr_datafeed_logic &logic) | |
140 | { | |
141 | assert(_unit_size == logic.unitsize); | |
142 | assert((logic.length % _unit_size) == 0); | |
143 | ||
144 | lock_guard<recursive_mutex> lock(_mutex); | |
145 | ||
146 | append_data(logic.data, logic.length / _unit_size); | |
147 | ||
148 | // Generate the first mip-map from the data | |
149 | append_payload_to_mipmap(); | |
150 | } | |
151 | ||
152 | void LogicSnapshot::get_samples(uint8_t *const data, | |
153 | int64_t start_sample, int64_t end_sample) const | |
154 | { | |
155 | assert(data); | |
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); | |
161 | ||
162 | lock_guard<recursive_mutex> lock(_mutex); | |
163 | ||
164 | const size_t size = (end_sample - start_sample) * _unit_size; | |
165 | memcpy(data, (const uint8_t*)_data.data() + start_sample * _unit_size, size); | |
166 | } | |
167 | ||
168 | void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m) | |
169 | { | |
170 | const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) / | |
171 | MipMapDataUnit) * MipMapDataUnit; | |
172 | if (new_data_length > m.data_length) | |
173 | { | |
174 | m.data_length = new_data_length; | |
175 | ||
176 | // Padding is added to allow for the uint64_t write word | |
177 | m.data = realloc(m.data, new_data_length * _unit_size + | |
178 | sizeof(uint64_t)); | |
179 | } | |
180 | } | |
181 | ||
182 | void LogicSnapshot::append_payload_to_mipmap() | |
183 | { | |
184 | MipMapLevel &m0 = _mip_map[0]; | |
185 | uint64_t prev_length; | |
186 | const uint8_t *src_ptr; | |
187 | uint8_t *dest_ptr; | |
188 | uint64_t accumulator; | |
189 | unsigned int diff_counter; | |
190 | ||
191 | // Expand the data buffer to fit the new samples | |
192 | prev_length = m0.length; | |
193 | m0.length = _sample_count / MipMapScaleFactor; | |
194 | ||
195 | // Break off if there are no new samples to compute | |
196 | if (m0.length == prev_length) | |
197 | return; | |
198 | ||
199 | reallocate_mipmap_level(m0); | |
200 | ||
201 | dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size; | |
202 | ||
203 | // Iterate through the samples to populate the first level mipmap | |
204 | const uint8_t *const end_src_ptr = (uint8_t*)_data.data() + | |
205 | m0.length * _unit_size * MipMapScaleFactor; | |
206 | for (src_ptr = (uint8_t*)_data.data() + | |
207 | prev_length * _unit_size * MipMapScaleFactor; | |
208 | src_ptr < end_src_ptr;) | |
209 | { | |
210 | // Accumulate transitions which have occurred in this sample | |
211 | accumulator = 0; | |
212 | diff_counter = MipMapScaleFactor; | |
213 | while (diff_counter-- > 0) | |
214 | { | |
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; | |
219 | } | |
220 | ||
221 | pack_sample(dest_ptr, accumulator); | |
222 | dest_ptr += _unit_size; | |
223 | } | |
224 | ||
225 | // Compute higher level mipmaps | |
226 | for (unsigned int level = 1; level < ScaleStepCount; level++) | |
227 | { | |
228 | MipMapLevel &m = _mip_map[level]; | |
229 | const MipMapLevel &ml = _mip_map[level-1]; | |
230 | ||
231 | // Expand the data buffer to fit the new samples | |
232 | prev_length = m.length; | |
233 | m.length = ml.length / MipMapScaleFactor; | |
234 | ||
235 | // Break off if there are no more samples to computed | |
236 | if (m.length == prev_length) | |
237 | break; | |
238 | ||
239 | reallocate_mipmap_level(m); | |
240 | ||
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) | |
250 | { | |
251 | accumulator = 0; | |
252 | diff_counter = MipMapScaleFactor; | |
253 | while (diff_counter-- > 0) | |
254 | { | |
255 | accumulator |= unpack_sample(src_ptr); | |
256 | src_ptr += _unit_size; | |
257 | } | |
258 | ||
259 | pack_sample(dest_ptr, accumulator); | |
260 | } | |
261 | } | |
262 | } | |
263 | ||
264 | uint64_t LogicSnapshot::get_sample(uint64_t index) const | |
265 | { | |
266 | assert(index < _sample_count); | |
267 | ||
268 | return unpack_sample((uint8_t*)_data.data() + index * _unit_size); | |
269 | } | |
270 | ||
271 | void LogicSnapshot::get_subsampled_edges( | |
272 | std::vector<EdgePair> &edges, | |
273 | uint64_t start, uint64_t end, | |
274 | float min_length, int sig_index) | |
275 | { | |
276 | uint64_t index = start; | |
277 | unsigned int level; | |
278 | bool last_sample; | |
279 | bool fast_forward; | |
280 | ||
281 | assert(end <= get_sample_count()); | |
282 | assert(start <= end); | |
283 | assert(min_length > 0); | |
284 | assert(sig_index >= 0); | |
285 | assert(sig_index < 64); | |
286 | ||
287 | lock_guard<recursive_mutex> lock(_mutex); | |
288 | ||
289 | const uint64_t block_length = (uint64_t)max(min_length, 1.0f); | |
290 | const unsigned int min_level = max((int)floorf(logf(min_length) / | |
291 | LogMipMapScaleFactor) - 1, 0); | |
292 | const uint64_t sig_mask = 1ULL << sig_index; | |
293 | ||
294 | // Store the initial state | |
295 | last_sample = (get_sample(start) & sig_mask) != 0; | |
296 | edges.push_back(pair<int64_t, bool>(index++, last_sample)); | |
297 | ||
298 | while (index + block_length <= end) | |
299 | { | |
300 | //----- Continue to search -----// | |
301 | level = min_level; | |
302 | ||
303 | // We cannot fast-forward if there is no mip-map data at | |
304 | // at the minimum level. | |
305 | fast_forward = (_mip_map[level].data != NULL); | |
306 | ||
307 | if (min_length < MipMapScaleFactor) | |
308 | { | |
309 | // Search individual samples up to the beginning of | |
310 | // the next first level mip map block | |
311 | const uint64_t final_index = min(end, | |
312 | pow2_ceil(index, MipMapScalePower)); | |
313 | ||
314 | for (; index < final_index && | |
315 | (index & ~(~0 << MipMapScalePower)) != 0; | |
316 | index++) | |
317 | { | |
318 | const bool sample = | |
319 | (get_sample(index) & sig_mask) != 0; | |
320 | ||
321 | // If there was a change we cannot fast forward | |
322 | if (sample != last_sample) { | |
323 | fast_forward = false; | |
324 | break; | |
325 | } | |
326 | } | |
327 | } | |
328 | else | |
329 | { | |
330 | // If resolution is less than a mip map block, | |
331 | // round up to the beginning of the mip-map block | |
332 | // for this level of detail | |
333 | const int min_level_scale_power = | |
334 | (level + 1) * MipMapScalePower; | |
335 | index = pow2_ceil(index, min_level_scale_power); | |
336 | if (index >= end) | |
337 | break; | |
338 | ||
339 | // We can fast forward only if there was no change | |
340 | const bool sample = | |
341 | (get_sample(index) & sig_mask) != 0; | |
342 | if (last_sample != sample) | |
343 | fast_forward = false; | |
344 | } | |
345 | ||
346 | if (fast_forward) { | |
347 | ||
348 | // Fast forward: This involves zooming out to higher | |
349 | // levels of the mip map searching for changes, then | |
350 | // zooming in on them to find the point where the edge | |
351 | // begins. | |
352 | ||
353 | // Slide right and zoom out at the beginnings of mip-map | |
354 | // blocks until we encounter a change | |
355 | while (1) { | |
356 | const int level_scale_power = | |
357 | (level + 1) * MipMapScalePower; | |
358 | const uint64_t offset = | |
359 | index >> level_scale_power; | |
360 | ||
361 | // Check if we reached the last block at this | |
362 | // level, or if there was a change in this block | |
363 | if (offset >= _mip_map[level].length || | |
364 | (get_subsample(level, offset) & | |
365 | sig_mask)) | |
366 | break; | |
367 | ||
368 | if ((offset & ~(~0 << MipMapScalePower)) == 0) { | |
369 | // If we are now at the beginning of a | |
370 | // higher level mip-map block ascend one | |
371 | // level | |
372 | if (level + 1 >= ScaleStepCount || | |
373 | !_mip_map[level + 1].data) | |
374 | break; | |
375 | ||
376 | level++; | |
377 | } else { | |
378 | // Slide right to the beginning of the | |
379 | // next mip map block | |
380 | index = pow2_ceil(index + 1, | |
381 | level_scale_power); | |
382 | } | |
383 | } | |
384 | ||
385 | // Zoom in, and slide right until we encounter a change, | |
386 | // and repeat until we reach min_level | |
387 | while (1) { | |
388 | assert(_mip_map[level].data); | |
389 | ||
390 | const int level_scale_power = | |
391 | (level + 1) * MipMapScalePower; | |
392 | const uint64_t offset = | |
393 | index >> level_scale_power; | |
394 | ||
395 | // Check if we reached the last block at this | |
396 | // level, or if there was a change in this block | |
397 | if (offset >= _mip_map[level].length || | |
398 | (get_subsample(level, offset) & | |
399 | sig_mask)) { | |
400 | // Zoom in unless we reached the minimum | |
401 | // zoom | |
402 | if (level == min_level) | |
403 | break; | |
404 | ||
405 | level--; | |
406 | } else { | |
407 | // Slide right to the beginning of the | |
408 | // next mip map block | |
409 | index = pow2_ceil(index + 1, | |
410 | level_scale_power); | |
411 | } | |
412 | } | |
413 | ||
414 | // If individual samples within the limit of resolution, | |
415 | // do a linear search for the next transition within the | |
416 | // block | |
417 | if (min_length < MipMapScaleFactor) { | |
418 | for (; index < end; index++) { | |
419 | const bool sample = (get_sample(index) & | |
420 | sig_mask) != 0; | |
421 | if (sample != last_sample) | |
422 | break; | |
423 | } | |
424 | } | |
425 | } | |
426 | ||
427 | //----- Store the edge -----// | |
428 | ||
429 | // Take the last sample of the quanization block | |
430 | const int64_t final_index = index + block_length; | |
431 | if (index + block_length > end) | |
432 | break; | |
433 | ||
434 | // Store the final state | |
435 | const bool final_sample = | |
436 | (get_sample(final_index - 1) & sig_mask) != 0; | |
437 | edges.push_back(pair<int64_t, bool>(index, final_sample)); | |
438 | ||
439 | index = final_index; | |
440 | last_sample = final_sample; | |
441 | } | |
442 | ||
443 | // Add the final state | |
444 | const bool end_sample = get_sample(end) & sig_mask; | |
445 | if (last_sample != end_sample) | |
446 | edges.push_back(pair<int64_t, bool>(end, end_sample)); | |
447 | edges.push_back(pair<int64_t, bool>(end + 1, end_sample)); | |
448 | } | |
449 | ||
450 | uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const | |
451 | { | |
452 | assert(level >= 0); | |
453 | assert(_mip_map[level].data); | |
454 | return unpack_sample((uint8_t*)_mip_map[level].data + | |
455 | _unit_size * offset); | |
456 | } | |
457 | ||
458 | uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power) | |
459 | { | |
460 | const uint64_t p = 1 << power; | |
461 | return (x + p - 1) / p * p; | |
462 | } | |
463 | ||
464 | } // namespace data | |
465 | } // namespace pv |