]>
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
28a4c9c5 JH |
18 | */ |
19 | ||
1ec3e43f UH |
20 | #include "config.h" // For HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS |
21 | ||
cef18fc6 | 22 | #include <extdef.h> |
28a4c9c5 | 23 | |
eb8269e3 | 24 | #include <cassert> |
d9e71737 | 25 | #include <cmath> |
aca9aa83 UH |
26 | #include <cstdlib> |
27 | #include <cstring> | |
72708385 | 28 | #include <cstdint> |
4ceab49a | 29 | |
7db61e77 | 30 | #include "logic.hpp" |
f3d66e52 | 31 | #include "logicsegment.hpp" |
f556bc6a | 32 | |
fe3a1c21 | 33 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 34 | |
3b68d03d JH |
35 | using std::lock_guard; |
36 | using std::recursive_mutex; | |
819f4c25 JH |
37 | using std::max; |
38 | using std::min; | |
e8d00928 | 39 | using std::shared_ptr; |
6f925ba9 | 40 | using std::vector; |
e8d00928 ML |
41 | |
42 | using sigrok::Logic; | |
28a4c9c5 | 43 | |
51e77110 | 44 | namespace pv { |
1b1ec774 | 45 | namespace data { |
51e77110 | 46 | |
f3d66e52 JH |
47 | const int LogicSegment::MipMapScalePower = 4; |
48 | const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower; | |
49 | const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor); | |
c063290a | 50 | const uint64_t LogicSegment::MipMapDataUnit = 64 * 1024; // bytes |
4ceab49a | 51 | |
85a70280 SA |
52 | LogicSegment::LogicSegment(pv::data::Logic& owner, uint32_t segment_id, |
53 | unsigned int unit_size, uint64_t samplerate) : | |
54 | Segment(segment_id, samplerate, unit_size), | |
9d22929c | 55 | owner_(owner), |
0df28cd5 JB |
56 | last_append_sample_(0), |
57 | last_append_accumulator_(0), | |
58 | last_append_extra_(0) | |
9d22929c SA |
59 | { |
60 | memset(mip_map_, 0, sizeof(mip_map_)); | |
61 | } | |
62 | ||
f3d66e52 | 63 | LogicSegment::~LogicSegment() |
4ceab49a | 64 | { |
8dbbc7f0 | 65 | lock_guard<recursive_mutex> lock(mutex_); |
cf1541a1 | 66 | |
8dbbc7f0 | 67 | for (MipMapLevel &l : mip_map_) |
4ceab49a JH |
68 | free(l.data); |
69 | } | |
70 | ||
cf1541a1 SA |
71 | shared_ptr<const LogicSegment> LogicSegment::get_shared_ptr() const |
72 | { | |
73 | shared_ptr<const Segment> ptr = nullptr; | |
74 | ||
75 | try { | |
76 | ptr = shared_from_this(); | |
77 | } catch (std::exception& e) { | |
78 | /* Do nothing, ptr remains a null pointer */ | |
79 | } | |
80 | ||
81 | return ptr ? std::dynamic_pointer_cast<const LogicSegment>(ptr) : nullptr; | |
82 | } | |
83 | ||
0df28cd5 JB |
84 | template <class T> |
85 | void LogicSegment::downsampleTmain(const T*&in, T &acc, T &prev) | |
86 | { | |
87 | // Accumulate one sample at a time | |
88 | for (uint64_t i = 0; i < MipMapScaleFactor; i++) { | |
89 | T sample = *in++; | |
90 | acc |= prev ^ sample; | |
91 | prev = sample; | |
92 | } | |
93 | } | |
94 | ||
95 | template <> | |
96 | void LogicSegment::downsampleTmain<uint8_t>(const uint8_t*&in, uint8_t &acc, uint8_t &prev) | |
97 | { | |
98 | // Handle 8 bit samples in 32 bit steps | |
99 | uint32_t prev32 = prev | prev << 8 | prev << 16 | prev << 24; | |
100 | uint32_t acc32 = acc; | |
101 | const uint32_t *in32 = (const uint32_t*)in; | |
102 | for (uint64_t i = 0; i < MipMapScaleFactor; i += 4) { | |
103 | uint32_t sample32 = *in32++; | |
104 | acc32 |= prev32 ^ sample32; | |
105 | prev32 = sample32; | |
106 | } | |
107 | // Reduce result back to uint8_t | |
108 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ | |
109 | prev = (prev32 >> 24) & 0xff; // MSB is last | |
110 | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | |
111 | prev = prev32 & 0xff; // LSB is last | |
112 | #else | |
113 | #error Endianness unknown | |
114 | #endif | |
115 | acc |= acc32 & 0xff; | |
116 | acc |= (acc32 >> 8) & 0xff; | |
117 | acc |= (acc32 >> 16) & 0xff; | |
118 | acc |= (acc32 >> 24) & 0xff; | |
119 | in = (const uint8_t*)in32; | |
120 | } | |
121 | ||
122 | template <> | |
123 | void LogicSegment::downsampleTmain<uint16_t>(const uint16_t*&in, uint16_t &acc, uint16_t &prev) | |
124 | { | |
125 | // Handle 16 bit samples in 32 bit steps | |
126 | uint32_t prev32 = prev | prev << 16; | |
127 | uint32_t acc32 = acc; | |
128 | const uint32_t *in32 = (const uint32_t*)in; | |
129 | for (uint64_t i = 0; i < MipMapScaleFactor; i += 2) { | |
130 | uint32_t sample32 = *in32++; | |
131 | acc32 |= prev32 ^ sample32; | |
132 | prev32 = sample32; | |
133 | } | |
134 | // Reduce result back to uint16_t | |
135 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ | |
136 | prev = (prev32 >> 16) & 0xffff; // MSB is last | |
137 | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | |
138 | prev = prev32 & 0xffff; // LSB is last | |
139 | #else | |
140 | #error Endian unknown | |
141 | #endif | |
142 | acc |= acc32 & 0xffff; | |
143 | acc |= (acc32 >> 16) & 0xffff; | |
144 | in = (const uint16_t*)in32; | |
145 | } | |
146 | ||
147 | template <class T> | |
148 | void LogicSegment::downsampleT(const uint8_t *in_, uint8_t *&out_, uint64_t len) | |
149 | { | |
150 | const T *in = (const T*)in_; | |
151 | T *out = (T*)out_; | |
152 | T prev = last_append_sample_; | |
153 | T acc = last_append_accumulator_; | |
154 | ||
155 | // Try to complete the previous downsample | |
156 | if (last_append_extra_) { | |
157 | while (last_append_extra_ < MipMapScaleFactor && len > 0) { | |
158 | T sample = *in++; | |
159 | acc |= prev ^ sample; | |
160 | prev = sample; | |
161 | last_append_extra_++; | |
162 | len--; | |
163 | } | |
164 | if (!len) { | |
165 | // Not enough samples available to complete downsample | |
166 | last_append_sample_ = prev; | |
167 | last_append_accumulator_ = acc; | |
168 | return; | |
169 | } | |
170 | // We have a complete downsample | |
171 | *out++ = acc; | |
172 | acc = 0; | |
173 | last_append_extra_ = 0; | |
174 | } | |
175 | ||
176 | // Handle complete blocks of MipMapScaleFactor samples | |
177 | while (len >= MipMapScaleFactor) { | |
178 | downsampleTmain<T>(in, acc, prev); | |
179 | len -= MipMapScaleFactor; | |
180 | // Output downsample | |
181 | *out++ = acc; | |
182 | acc = 0; | |
183 | } | |
184 | ||
185 | // Process remainder, not enough for a complete sample | |
186 | while (len > 0) { | |
187 | T sample = *in++; | |
188 | acc |= prev ^ sample; | |
189 | prev = sample; | |
190 | last_append_extra_++; | |
191 | len--; | |
192 | } | |
193 | ||
194 | // Update context | |
195 | last_append_sample_ = prev; | |
196 | last_append_accumulator_ = acc; | |
197 | out_ = (uint8_t *)out; | |
198 | } | |
199 | ||
200 | void LogicSegment::downsampleGeneric(const uint8_t *in, uint8_t *&out, uint64_t len) | |
201 | { | |
202 | // Downsample using the generic unpack_sample() | |
203 | // which can handle any width between 1 and 8 bytes | |
204 | uint64_t prev = last_append_sample_; | |
205 | uint64_t acc = last_append_accumulator_; | |
206 | ||
207 | // Try to complete the previous downsample | |
208 | if (last_append_extra_) { | |
209 | while (last_append_extra_ < MipMapScaleFactor && len > 0) { | |
210 | const uint64_t sample = unpack_sample(in); | |
211 | in += unit_size_; | |
212 | acc |= prev ^ sample; | |
213 | prev = sample; | |
214 | last_append_extra_++; | |
215 | len--; | |
216 | } | |
217 | if (!len) { | |
218 | // Not enough samples available to complete downsample | |
219 | last_append_sample_ = prev; | |
220 | last_append_accumulator_ = acc; | |
221 | return; | |
222 | } | |
223 | // We have a complete downsample | |
224 | pack_sample(out, acc); | |
225 | out += unit_size_; | |
226 | acc = 0; | |
227 | last_append_extra_ = 0; | |
228 | } | |
229 | ||
230 | // Handle complete blocks of MipMapScaleFactor samples | |
231 | while (len >= MipMapScaleFactor) { | |
232 | // Accumulate one sample at a time | |
233 | for (uint64_t i = 0; i < MipMapScaleFactor; i++) { | |
234 | const uint64_t sample = unpack_sample(in); | |
235 | in += unit_size_; | |
236 | acc |= prev ^ sample; | |
237 | prev = sample; | |
238 | } | |
239 | len -= MipMapScaleFactor; | |
240 | // Output downsample | |
241 | pack_sample(out, acc); | |
242 | out += unit_size_; | |
243 | acc = 0; | |
244 | } | |
245 | ||
246 | // Process remainder, not enough for a complete sample | |
247 | while (len > 0) { | |
248 | const uint64_t sample = unpack_sample(in); | |
249 | in += unit_size_; | |
250 | acc |= prev ^ sample; | |
251 | prev = sample; | |
252 | last_append_extra_++; | |
253 | len--; | |
254 | } | |
255 | ||
256 | // Update context | |
257 | last_append_sample_ = prev; | |
258 | last_append_accumulator_ = acc; | |
259 | } | |
260 | ||
97cb532f | 261 | inline uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const |
8cb71705 | 262 | { |
9df8453f MC |
263 | #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS |
264 | return *(uint64_t*)ptr; | |
265 | #else | |
8cb71705 | 266 | uint64_t value = 0; |
2ad82c2e | 267 | switch (unit_size_) { |
8cb71705 MC |
268 | default: |
269 | value |= ((uint64_t)ptr[7]) << 56; | |
270 | /* FALLTHRU */ | |
271 | case 7: | |
272 | value |= ((uint64_t)ptr[6]) << 48; | |
273 | /* FALLTHRU */ | |
274 | case 6: | |
275 | value |= ((uint64_t)ptr[5]) << 40; | |
276 | /* FALLTHRU */ | |
277 | case 5: | |
278 | value |= ((uint64_t)ptr[4]) << 32; | |
279 | /* FALLTHRU */ | |
280 | case 4: | |
281 | value |= ((uint32_t)ptr[3]) << 24; | |
282 | /* FALLTHRU */ | |
283 | case 3: | |
284 | value |= ((uint32_t)ptr[2]) << 16; | |
285 | /* FALLTHRU */ | |
286 | case 2: | |
287 | value |= ptr[1] << 8; | |
288 | /* FALLTHRU */ | |
289 | case 1: | |
290 | value |= ptr[0]; | |
291 | /* FALLTHRU */ | |
292 | case 0: | |
293 | break; | |
294 | } | |
295 | return value; | |
9df8453f | 296 | #endif |
8cb71705 MC |
297 | } |
298 | ||
97cb532f | 299 | inline void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value) |
8cb71705 | 300 | { |
9df8453f MC |
301 | #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS |
302 | *(uint64_t*)ptr = value; | |
303 | #else | |
2ad82c2e | 304 | switch (unit_size_) { |
8cb71705 MC |
305 | default: |
306 | ptr[7] = value >> 56; | |
307 | /* FALLTHRU */ | |
308 | case 7: | |
309 | ptr[6] = value >> 48; | |
310 | /* FALLTHRU */ | |
311 | case 6: | |
312 | ptr[5] = value >> 40; | |
313 | /* FALLTHRU */ | |
314 | case 5: | |
315 | ptr[4] = value >> 32; | |
316 | /* FALLTHRU */ | |
317 | case 4: | |
318 | ptr[3] = value >> 24; | |
319 | /* FALLTHRU */ | |
320 | case 3: | |
321 | ptr[2] = value >> 16; | |
322 | /* FALLTHRU */ | |
323 | case 2: | |
324 | ptr[1] = value >> 8; | |
325 | /* FALLTHRU */ | |
326 | case 1: | |
327 | ptr[0] = value; | |
328 | /* FALLTHRU */ | |
329 | case 0: | |
330 | break; | |
331 | } | |
9df8453f | 332 | #endif |
8cb71705 MC |
333 | } |
334 | ||
7db61e77 | 335 | void LogicSegment::append_payload(shared_ptr<sigrok::Logic> logic) |
28a4c9c5 | 336 | { |
8dbbc7f0 JH |
337 | assert(unit_size_ == logic->unit_size()); |
338 | assert((logic->data_length() % unit_size_) == 0); | |
f556bc6a | 339 | |
9d22929c SA |
340 | append_payload(logic->data_pointer(), logic->data_length()); |
341 | } | |
342 | ||
343 | void LogicSegment::append_payload(void *data, uint64_t data_size) | |
344 | { | |
bee54d9e | 345 | assert(unit_size_ > 0); |
9d22929c SA |
346 | assert((data_size % unit_size_) == 0); |
347 | ||
8dbbc7f0 | 348 | lock_guard<recursive_mutex> lock(mutex_); |
7d29656f | 349 | |
cc69e8ae UH |
350 | const uint64_t prev_sample_count = sample_count_; |
351 | const uint64_t sample_count = data_size / unit_size_; | |
7db61e77 | 352 | |
9d22929c | 353 | append_samples(data, sample_count); |
4ceab49a JH |
354 | |
355 | // Generate the first mip-map from the data | |
356 | append_payload_to_mipmap(); | |
7db61e77 SA |
357 | |
358 | if (sample_count > 1) | |
720f4762 | 359 | owner_.notify_samples_added(SharedPtrToSegment(shared_from_this()), |
1f3033cb | 360 | prev_sample_count + 1, prev_sample_count + 1 + sample_count); |
7db61e77 | 361 | else |
720f4762 | 362 | owner_.notify_samples_added(SharedPtrToSegment(shared_from_this()), |
1f3033cb | 363 | prev_sample_count + 1, prev_sample_count + 1); |
4ceab49a JH |
364 | } |
365 | ||
04b04675 SA |
366 | void LogicSegment::append_subsignal_payload(unsigned int index, void *data, uint64_t data_size) |
367 | { | |
368 | static vector<uint8_t> merged_data; // Using static also places it on the heap | |
369 | ||
370 | for (uint64_t i = 0; i < data_size * unit_size_; i++) | |
371 | merged_data.emplace_back(0); | |
372 | ||
373 | // Set the bits for this sub-signal where needed | |
374 | // Note: the bytes in *data must either be 0 or 1, nothing else | |
375 | unsigned int index_byte = index / 8; | |
376 | for (uint64_t i = 0; i < data_size; i++) { | |
377 | unsigned int offs = i * unit_size_ + index_byte; | |
378 | uint8_t* data_byte = merged_data.data() + offs; | |
379 | *data_byte |= *((uint8_t*)data + i) << index; | |
380 | } | |
381 | ||
382 | if (index == owner_.num_channels() - 1) { | |
383 | // We gathered sample data of all sub-signals, let's append it | |
384 | append_payload(merged_data.data(), merged_data.size()); | |
385 | merged_data.clear(); | |
386 | } | |
387 | } | |
388 | ||
b82243f7 | 389 | void LogicSegment::get_samples(int64_t start_sample, |
aa78b2df | 390 | int64_t end_sample, uint8_t* dest) const |
ed990f11 | 391 | { |
ed990f11 | 392 | assert(start_sample >= 0); |
8dbbc7f0 | 393 | assert(start_sample <= (int64_t)sample_count_); |
ed990f11 | 394 | assert(end_sample >= 0); |
8dbbc7f0 | 395 | assert(end_sample <= (int64_t)sample_count_); |
ed990f11 | 396 | assert(start_sample <= end_sample); |
b82243f7 | 397 | assert(dest != nullptr); |
ed990f11 | 398 | |
8dbbc7f0 | 399 | lock_guard<recursive_mutex> lock(mutex_); |
ed990f11 | 400 | |
b82243f7 | 401 | get_raw_samples(start_sample, (end_sample - start_sample), dest); |
26a883ed SA |
402 | } |
403 | ||
f3d66e52 | 404 | void LogicSegment::get_subsampled_edges( |
6f925ba9 | 405 | vector<EdgePair> &edges, |
60b0c2da | 406 | uint64_t start, uint64_t end, |
b4bc9b55 | 407 | float min_length, int sig_index, bool first_change_only) |
2858b391 | 408 | { |
60b0c2da JH |
409 | uint64_t index = start; |
410 | unsigned int level; | |
7d0d64f9 JH |
411 | bool last_sample; |
412 | bool fast_forward; | |
0b02e057 | 413 | |
2858b391 | 414 | assert(start <= end); |
0b02e057 | 415 | assert(min_length > 0); |
2858b391 | 416 | assert(sig_index >= 0); |
80d50141 | 417 | assert(sig_index < 64); |
2858b391 | 418 | |
8dbbc7f0 | 419 | lock_guard<recursive_mutex> lock(mutex_); |
7d29656f | 420 | |
4cc0df94 SA |
421 | // Make sure we only process as many samples as we have |
422 | if (end > get_sample_count()) | |
423 | end = get_sample_count(); | |
424 | ||
60b0c2da JH |
425 | const uint64_t block_length = (uint64_t)max(min_length, 1.0f); |
426 | const unsigned int min_level = max((int)floorf(logf(min_length) / | |
0b02e057 | 427 | LogMipMapScaleFactor) - 1, 0); |
7d0d64f9 | 428 | const uint64_t sig_mask = 1ULL << sig_index; |
2858b391 | 429 | |
7d0d64f9 | 430 | // Store the initial state |
26a883ed | 431 | last_sample = (get_unpacked_sample(start) & sig_mask) != 0; |
b4bc9b55 SA |
432 | if (!first_change_only) |
433 | edges.emplace_back(index++, last_sample); | |
2858b391 | 434 | |
2ad82c2e | 435 | while (index + block_length <= end) { |
7d0d64f9 | 436 | //----- Continue to search -----// |
0b02e057 | 437 | level = min_level; |
f06ab143 JH |
438 | |
439 | // We cannot fast-forward if there is no mip-map data at | |
53aa9bb4 | 440 | // the minimum level. |
4c60462b | 441 | fast_forward = (mip_map_[level].data != nullptr); |
2858b391 | 442 | |
2ad82c2e | 443 | if (min_length < MipMapScaleFactor) { |
0b02e057 JH |
444 | // Search individual samples up to the beginning of |
445 | // the next first level mip map block | |
53aa9bb4 | 446 | const uint64_t final_index = min(end, pow2_ceil(index, MipMapScalePower)); |
0b02e057 | 447 | |
333d5bbc | 448 | for (; index < final_index && |
c28fa62b | 449 | (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0; |
2ad82c2e | 450 | index++) { |
53aa9bb4 SA |
451 | |
452 | const bool sample = (get_unpacked_sample(index) & sig_mask) != 0; | |
7d0d64f9 JH |
453 | |
454 | // If there was a change we cannot fast forward | |
333d5bbc | 455 | if (sample != last_sample) { |
7d0d64f9 | 456 | fast_forward = false; |
0b02e057 | 457 | break; |
7d0d64f9 | 458 | } |
0b02e057 | 459 | } |
2ad82c2e | 460 | } else { |
0b02e057 JH |
461 | // If resolution is less than a mip map block, |
462 | // round up to the beginning of the mip-map block | |
463 | // for this level of detail | |
53aa9bb4 | 464 | const int min_level_scale_power = (level + 1) * MipMapScalePower; |
0b02e057 | 465 | index = pow2_ceil(index, min_level_scale_power); |
333d5bbc | 466 | if (index >= end) |
0b02e057 JH |
467 | break; |
468 | ||
7d0d64f9 | 469 | // We can fast forward only if there was no change |
53aa9bb4 | 470 | const bool sample = (get_unpacked_sample(index) & sig_mask) != 0; |
f06ab143 JH |
471 | if (last_sample != sample) |
472 | fast_forward = false; | |
0b02e057 JH |
473 | } |
474 | ||
333d5bbc | 475 | if (fast_forward) { |
7d0d64f9 JH |
476 | |
477 | // Fast forward: This involves zooming out to higher | |
478 | // levels of the mip map searching for changes, then | |
479 | // zooming in on them to find the point where the edge | |
480 | // begins. | |
481 | ||
482 | // Slide right and zoom out at the beginnings of mip-map | |
483 | // blocks until we encounter a change | |
1f1d55ce | 484 | while (true) { |
53aa9bb4 SA |
485 | const int level_scale_power = (level + 1) * MipMapScalePower; |
486 | const uint64_t offset = index >> level_scale_power; | |
7d0d64f9 JH |
487 | |
488 | // Check if we reached the last block at this | |
489 | // level, or if there was a change in this block | |
8dbbc7f0 | 490 | if (offset >= mip_map_[level].length || |
53aa9bb4 | 491 | (get_subsample(level, offset) & sig_mask)) |
0b02e057 JH |
492 | break; |
493 | ||
c28fa62b | 494 | if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) { |
7d0d64f9 JH |
495 | // If we are now at the beginning of a |
496 | // higher level mip-map block ascend one | |
497 | // level | |
53aa9bb4 | 498 | if ((level + 1 >= ScaleStepCount) || (!mip_map_[level + 1].data)) |
7d0d64f9 JH |
499 | break; |
500 | ||
501 | level++; | |
502 | } else { | |
503 | // Slide right to the beginning of the | |
504 | // next mip map block | |
53aa9bb4 | 505 | index = pow2_ceil(index + 1, level_scale_power); |
7d0d64f9 | 506 | } |
0b02e057 | 507 | } |
7d0d64f9 JH |
508 | |
509 | // Zoom in, and slide right until we encounter a change, | |
510 | // and repeat until we reach min_level | |
1f1d55ce | 511 | while (true) { |
8dbbc7f0 | 512 | assert(mip_map_[level].data); |
7d0d64f9 | 513 | |
53aa9bb4 SA |
514 | const int level_scale_power = (level + 1) * MipMapScalePower; |
515 | const uint64_t offset = index >> level_scale_power; | |
7d0d64f9 JH |
516 | |
517 | // Check if we reached the last block at this | |
518 | // level, or if there was a change in this block | |
8dbbc7f0 | 519 | if (offset >= mip_map_[level].length || |
53aa9bb4 | 520 | (get_subsample(level, offset) & sig_mask)) { |
7d0d64f9 JH |
521 | // Zoom in unless we reached the minimum |
522 | // zoom | |
333d5bbc | 523 | if (level == min_level) |
7d0d64f9 JH |
524 | break; |
525 | ||
526 | level--; | |
527 | } else { | |
528 | // Slide right to the beginning of the | |
529 | // next mip map block | |
53aa9bb4 | 530 | index = pow2_ceil(index + 1, level_scale_power); |
7d0d64f9 | 531 | } |
0b02e057 | 532 | } |
0b02e057 | 533 | |
7d0d64f9 JH |
534 | // If individual samples within the limit of resolution, |
535 | // do a linear search for the next transition within the | |
536 | // block | |
333d5bbc UH |
537 | if (min_length < MipMapScaleFactor) { |
538 | for (; index < end; index++) { | |
53aa9bb4 | 539 | const bool sample = (get_unpacked_sample(index) & sig_mask) != 0; |
333d5bbc | 540 | if (sample != last_sample) |
7d0d64f9 JH |
541 | break; |
542 | } | |
0b02e057 JH |
543 | } |
544 | } | |
545 | ||
7d0d64f9 JH |
546 | //----- Store the edge -----// |
547 | ||
548 | // Take the last sample of the quanization block | |
549 | const int64_t final_index = index + block_length; | |
333d5bbc | 550 | if (index + block_length > end) |
7d0d64f9 JH |
551 | break; |
552 | ||
553 | // Store the final state | |
53aa9bb4 | 554 | const bool final_sample = (get_unpacked_sample(final_index - 1) & sig_mask) != 0; |
326cf6fe | 555 | edges.emplace_back(index, final_sample); |
7d0d64f9 JH |
556 | |
557 | index = final_index; | |
558 | last_sample = final_sample; | |
b4bc9b55 SA |
559 | |
560 | if (first_change_only) | |
561 | break; | |
2858b391 JH |
562 | } |
563 | ||
564 | // Add the final state | |
b4bc9b55 SA |
565 | if (!first_change_only) { |
566 | const bool end_sample = get_unpacked_sample(end) & sig_mask; | |
567 | if (last_sample != end_sample) | |
568 | edges.emplace_back(end, end_sample); | |
569 | edges.emplace_back(end + 1, end_sample); | |
570 | } | |
571 | } | |
572 | ||
573 | void LogicSegment::get_surrounding_edges(vector<EdgePair> &dest, | |
574 | uint64_t origin_sample, float min_length, int sig_index) | |
575 | { | |
8e9e525e SA |
576 | if (origin_sample >= sample_count_) |
577 | return; | |
578 | ||
b4bc9b55 SA |
579 | // Put the edges vector on the heap, it can become quite big until we can |
580 | // use a get_subsampled_edges() implementation that searches backwards | |
581 | vector<EdgePair>* edges = new vector<EdgePair>; | |
582 | ||
e0544801 | 583 | // Get all edges to the left of origin_sample |
b4bc9b55 SA |
584 | get_subsampled_edges(*edges, 0, origin_sample, min_length, sig_index, false); |
585 | ||
586 | // If we don't specify "first only", the first and last edge are the states | |
587 | // at samples 0 and origin_sample. If only those exist, there are no edges | |
588 | if (edges->size() == 2) { | |
589 | delete edges; | |
590 | return; | |
591 | } | |
592 | ||
593 | // Dismiss the entry for origin_sample so that back() gives us the | |
594 | // real last entry | |
595 | edges->pop_back(); | |
596 | dest.push_back(edges->back()); | |
597 | edges->clear(); | |
598 | ||
e0544801 | 599 | // Get first edge to the right of origin_sample |
b4bc9b55 SA |
600 | get_subsampled_edges(*edges, origin_sample, sample_count_, min_length, sig_index, true); |
601 | ||
602 | // "first only" is specified, so nothing needs to be dismissed | |
603 | if (edges->size() == 0) { | |
604 | delete edges; | |
605 | return; | |
606 | } | |
607 | ||
608 | dest.push_back(edges->front()); | |
609 | ||
610 | delete edges; | |
611 | } | |
612 | ||
613 | void LogicSegment::reallocate_mipmap_level(MipMapLevel &m) | |
614 | { | |
615 | lock_guard<recursive_mutex> lock(mutex_); | |
616 | ||
617 | const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) / | |
618 | MipMapDataUnit) * MipMapDataUnit; | |
619 | ||
620 | if (new_data_length > m.data_length) { | |
621 | m.data_length = new_data_length; | |
622 | ||
623 | // Padding is added to allow for the uint64_t write word | |
624 | m.data = realloc(m.data, new_data_length * unit_size_ + | |
625 | sizeof(uint64_t)); | |
626 | } | |
627 | } | |
628 | ||
629 | void LogicSegment::append_payload_to_mipmap() | |
630 | { | |
631 | MipMapLevel &m0 = mip_map_[0]; | |
632 | uint64_t prev_length; | |
633 | uint8_t *dest_ptr; | |
65c92359 | 634 | SegmentDataIterator* it; |
b4bc9b55 SA |
635 | uint64_t accumulator; |
636 | unsigned int diff_counter; | |
637 | ||
638 | // Expand the data buffer to fit the new samples | |
639 | prev_length = m0.length; | |
640 | m0.length = sample_count_ / MipMapScaleFactor; | |
641 | ||
642 | // Break off if there are no new samples to compute | |
643 | if (m0.length == prev_length) | |
644 | return; | |
645 | ||
646 | reallocate_mipmap_level(m0); | |
647 | ||
648 | dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_; | |
649 | ||
650 | // Iterate through the samples to populate the first level mipmap | |
651 | const uint64_t start_sample = prev_length * MipMapScaleFactor; | |
652 | const uint64_t end_sample = m0.length * MipMapScaleFactor; | |
0df28cd5 | 653 | uint64_t len_sample = end_sample - start_sample; |
65c92359 | 654 | it = begin_sample_iteration(start_sample); |
0df28cd5 JB |
655 | while (len_sample > 0) { |
656 | // Number of samples available in this chunk | |
657 | uint64_t count = get_iterator_valid_length(it); | |
658 | // Reduce if less than asked for | |
659 | count = std::min(count, len_sample); | |
660 | uint8_t *src_ptr = get_iterator_value(it); | |
661 | // Submit these contiguous samples to downsampling in bulk | |
662 | if (unit_size_ == 1) | |
663 | downsampleT<uint8_t>(src_ptr, dest_ptr, count); | |
664 | else if (unit_size_ == 2) | |
665 | downsampleT<uint16_t>(src_ptr, dest_ptr, count); | |
666 | else if (unit_size_ == 4) | |
667 | downsampleT<uint32_t>(src_ptr, dest_ptr, count); | |
668 | else if (unit_size_ == 8) | |
4f654de1 | 669 | downsampleT<uint64_t>(src_ptr, dest_ptr, count); |
0df28cd5 JB |
670 | else |
671 | downsampleGeneric(src_ptr, dest_ptr, count); | |
672 | len_sample -= count; | |
673 | // Advance iterator, should move to start of next chunk | |
674 | continue_sample_iteration(it, count); | |
b4bc9b55 | 675 | } |
65c92359 | 676 | end_sample_iteration(it); |
b4bc9b55 SA |
677 | |
678 | // Compute higher level mipmaps | |
679 | for (unsigned int level = 1; level < ScaleStepCount; level++) { | |
680 | MipMapLevel &m = mip_map_[level]; | |
681 | const MipMapLevel &ml = mip_map_[level - 1]; | |
682 | ||
683 | // Expand the data buffer to fit the new samples | |
684 | prev_length = m.length; | |
685 | m.length = ml.length / MipMapScaleFactor; | |
686 | ||
687 | // Break off if there are no more samples to be computed | |
688 | if (m.length == prev_length) | |
689 | break; | |
690 | ||
691 | reallocate_mipmap_level(m); | |
692 | ||
693 | // Subsample the lower level | |
694 | const uint8_t* src_ptr = (uint8_t*)ml.data + | |
695 | unit_size_ * prev_length * MipMapScaleFactor; | |
696 | const uint8_t *const end_dest_ptr = | |
697 | (uint8_t*)m.data + unit_size_ * m.length; | |
698 | ||
699 | for (dest_ptr = (uint8_t*)m.data + | |
700 | unit_size_ * prev_length; | |
701 | dest_ptr < end_dest_ptr; | |
702 | dest_ptr += unit_size_) { | |
703 | accumulator = 0; | |
704 | diff_counter = MipMapScaleFactor; | |
705 | while (diff_counter-- > 0) { | |
706 | accumulator |= unpack_sample(src_ptr); | |
707 | src_ptr += unit_size_; | |
708 | } | |
709 | ||
710 | pack_sample(dest_ptr, accumulator); | |
711 | } | |
712 | } | |
713 | } | |
714 | ||
715 | uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const | |
716 | { | |
717 | assert(index < sample_count_); | |
718 | ||
719 | assert(unit_size_ <= 8); // 8 * 8 = 64 channels | |
720 | uint8_t data[8]; | |
721 | ||
722 | get_raw_samples(index, 1, data); | |
723 | ||
724 | return unpack_sample(data); | |
2858b391 | 725 | } |
0b02e057 | 726 | |
f3d66e52 | 727 | uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const |
b2bcbe51 JH |
728 | { |
729 | assert(level >= 0); | |
8dbbc7f0 JH |
730 | assert(mip_map_[level].data); |
731 | return unpack_sample((uint8_t*)mip_map_[level].data + | |
732 | unit_size_ * offset); | |
b2bcbe51 JH |
733 | } |
734 | ||
f3d66e52 | 735 | uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power) |
0b02e057 | 736 | { |
72708385 | 737 | const uint64_t p = UINT64_C(1) << power; |
60b0c2da | 738 | return (x + p - 1) / p * p; |
0b02e057 | 739 | } |
51e77110 | 740 | |
1b1ec774 | 741 | } // namespace data |
51e77110 | 742 | } // namespace pv |