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