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