Application::print_version_info(): Minor output whitespace fixes.
[pulseview.git] / pv / data / logicsegment.cpp
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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h" // For HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
21
22 #include <extdef.h>
23
24 #include <cassert>
25 #include <cmath>
26 #include <cstdlib>
27 #include <cstring>
28 #include <cstdint>
29
30 #include "logic.hpp"
31 #include "logicsegment.hpp"
32
33 #include <libsigrokcxx/libsigrokcxx.hpp>
34
35 using std::lock_guard;
36 using std::recursive_mutex;
37 using std::max;
38 using std::min;
39 using std::shared_ptr;
40 using std::vector;
41
42 using sigrok::Logic;
43
44 namespace pv {
45 namespace data {
46
47 const int LogicSegment::MipMapScalePower = 4;
48 const int LogicSegment::MipMapScaleFactor = 1 << MipMapScalePower;
49 const float LogicSegment::LogMipMapScaleFactor = logf(MipMapScaleFactor);
50 const uint64_t LogicSegment::MipMapDataUnit = 64 * 1024; // bytes
51
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),
55         owner_(owner),
56         last_append_sample_(0)
57 {
58         memset(mip_map_, 0, sizeof(mip_map_));
59 }
60
61 LogicSegment::~LogicSegment()
62 {
63         lock_guard<recursive_mutex> lock(mutex_);
64         for (MipMapLevel &l : mip_map_)
65                 free(l.data);
66 }
67
68 inline uint64_t LogicSegment::unpack_sample(const uint8_t *ptr) const
69 {
70 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
71         return *(uint64_t*)ptr;
72 #else
73         uint64_t value = 0;
74         switch (unit_size_) {
75         default:
76                 value |= ((uint64_t)ptr[7]) << 56;
77                 /* FALLTHRU */
78         case 7:
79                 value |= ((uint64_t)ptr[6]) << 48;
80                 /* FALLTHRU */
81         case 6:
82                 value |= ((uint64_t)ptr[5]) << 40;
83                 /* FALLTHRU */
84         case 5:
85                 value |= ((uint64_t)ptr[4]) << 32;
86                 /* FALLTHRU */
87         case 4:
88                 value |= ((uint32_t)ptr[3]) << 24;
89                 /* FALLTHRU */
90         case 3:
91                 value |= ((uint32_t)ptr[2]) << 16;
92                 /* FALLTHRU */
93         case 2:
94                 value |= ptr[1] << 8;
95                 /* FALLTHRU */
96         case 1:
97                 value |= ptr[0];
98                 /* FALLTHRU */
99         case 0:
100                 break;
101         }
102         return value;
103 #endif
104 }
105
106 inline void LogicSegment::pack_sample(uint8_t *ptr, uint64_t value)
107 {
108 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
109         *(uint64_t*)ptr = value;
110 #else
111         switch (unit_size_) {
112         default:
113                 ptr[7] = value >> 56;
114                 /* FALLTHRU */
115         case 7:
116                 ptr[6] = value >> 48;
117                 /* FALLTHRU */
118         case 6:
119                 ptr[5] = value >> 40;
120                 /* FALLTHRU */
121         case 5:
122                 ptr[4] = value >> 32;
123                 /* FALLTHRU */
124         case 4:
125                 ptr[3] = value >> 24;
126                 /* FALLTHRU */
127         case 3:
128                 ptr[2] = value >> 16;
129                 /* FALLTHRU */
130         case 2:
131                 ptr[1] = value >> 8;
132                 /* FALLTHRU */
133         case 1:
134                 ptr[0] = value;
135                 /* FALLTHRU */
136         case 0:
137                 break;
138         }
139 #endif
140 }
141
142 void LogicSegment::append_payload(shared_ptr<sigrok::Logic> logic)
143 {
144         assert(unit_size_ == logic->unit_size());
145         assert((logic->data_length() % unit_size_) == 0);
146
147         append_payload(logic->data_pointer(), logic->data_length());
148 }
149
150 void LogicSegment::append_payload(void *data, uint64_t data_size)
151 {
152         assert((data_size % unit_size_) == 0);
153
154         lock_guard<recursive_mutex> lock(mutex_);
155
156         const uint64_t prev_sample_count = sample_count_;
157         const uint64_t sample_count = data_size / unit_size_;
158
159         append_samples(data, sample_count);
160
161         // Generate the first mip-map from the data
162         append_payload_to_mipmap();
163
164         if (sample_count > 1)
165                 owner_.notify_samples_added(this, prev_sample_count + 1,
166                         prev_sample_count + 1 + sample_count);
167         else
168                 owner_.notify_samples_added(this, prev_sample_count + 1,
169                         prev_sample_count + 1);
170 }
171
172 void LogicSegment::get_samples(int64_t start_sample,
173         int64_t end_sample,     uint8_t* dest) const
174 {
175         assert(start_sample >= 0);
176         assert(start_sample <= (int64_t)sample_count_);
177         assert(end_sample >= 0);
178         assert(end_sample <= (int64_t)sample_count_);
179         assert(start_sample <= end_sample);
180         assert(dest != nullptr);
181
182         lock_guard<recursive_mutex> lock(mutex_);
183
184         get_raw_samples(start_sample, (end_sample - start_sample), dest);
185 }
186
187 SegmentLogicDataIterator* LogicSegment::begin_sample_iteration(uint64_t start)
188 {
189         return (SegmentLogicDataIterator*)begin_raw_sample_iteration(start);
190 }
191
192 void LogicSegment::continue_sample_iteration(SegmentLogicDataIterator* it, uint64_t increase)
193 {
194         Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
195 }
196
197 void LogicSegment::end_sample_iteration(SegmentLogicDataIterator* it)
198 {
199         Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
200 }
201
202 void LogicSegment::get_subsampled_edges(
203         vector<EdgePair> &edges,
204         uint64_t start, uint64_t end,
205         float min_length, int sig_index, bool first_change_only)
206 {
207         uint64_t index = start;
208         unsigned int level;
209         bool last_sample;
210         bool fast_forward;
211
212         assert(start <= end);
213         assert(min_length > 0);
214         assert(sig_index >= 0);
215         assert(sig_index < 64);
216
217         lock_guard<recursive_mutex> lock(mutex_);
218
219         // Make sure we only process as many samples as we have
220         if (end > get_sample_count())
221                 end = get_sample_count();
222
223         const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
224         const unsigned int min_level = max((int)floorf(logf(min_length) /
225                 LogMipMapScaleFactor) - 1, 0);
226         const uint64_t sig_mask = 1ULL << sig_index;
227
228         // Store the initial state
229         last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
230         if (!first_change_only)
231                 edges.emplace_back(index++, last_sample);
232
233         while (index + block_length <= end) {
234                 //----- Continue to search -----//
235                 level = min_level;
236
237                 // We cannot fast-forward if there is no mip-map data at
238                 // the minimum level.
239                 fast_forward = (mip_map_[level].data != nullptr);
240
241                 if (min_length < MipMapScaleFactor) {
242                         // Search individual samples up to the beginning of
243                         // the next first level mip map block
244                         const uint64_t final_index = min(end, pow2_ceil(index, MipMapScalePower));
245
246                         for (; index < final_index &&
247                                         (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
248                                         index++) {
249
250                                 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
251
252                                 // If there was a change we cannot fast forward
253                                 if (sample != last_sample) {
254                                         fast_forward = false;
255                                         break;
256                                 }
257                         }
258                 } else {
259                         // If resolution is less than a mip map block,
260                         // round up to the beginning of the mip-map block
261                         // for this level of detail
262                         const int min_level_scale_power = (level + 1) * MipMapScalePower;
263                         index = pow2_ceil(index, min_level_scale_power);
264                         if (index >= end)
265                                 break;
266
267                         // We can fast forward only if there was no change
268                         const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
269                         if (last_sample != sample)
270                                 fast_forward = false;
271                 }
272
273                 if (fast_forward) {
274
275                         // Fast forward: This involves zooming out to higher
276                         // levels of the mip map searching for changes, then
277                         // zooming in on them to find the point where the edge
278                         // begins.
279
280                         // Slide right and zoom out at the beginnings of mip-map
281                         // blocks until we encounter a change
282                         while (true) {
283                                 const int level_scale_power = (level + 1) * MipMapScalePower;
284                                 const uint64_t offset = index >> level_scale_power;
285
286                                 // Check if we reached the last block at this
287                                 // level, or if there was a change in this block
288                                 if (offset >= mip_map_[level].length ||
289                                         (get_subsample(level, offset) & sig_mask))
290                                         break;
291
292                                 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
293                                         // If we are now at the beginning of a
294                                         // higher level mip-map block ascend one
295                                         // level
296                                         if ((level + 1 >= ScaleStepCount) || (!mip_map_[level + 1].data))
297                                                 break;
298
299                                         level++;
300                                 } else {
301                                         // Slide right to the beginning of the
302                                         // next mip map block
303                                         index = pow2_ceil(index + 1, level_scale_power);
304                                 }
305                         }
306
307                         // Zoom in, and slide right until we encounter a change,
308                         // and repeat until we reach min_level
309                         while (true) {
310                                 assert(mip_map_[level].data);
311
312                                 const int level_scale_power = (level + 1) * MipMapScalePower;
313                                 const uint64_t offset = index >> level_scale_power;
314
315                                 // Check if we reached the last block at this
316                                 // level, or if there was a change in this block
317                                 if (offset >= mip_map_[level].length ||
318                                                 (get_subsample(level, offset) & sig_mask)) {
319                                         // Zoom in unless we reached the minimum
320                                         // zoom
321                                         if (level == min_level)
322                                                 break;
323
324                                         level--;
325                                 } else {
326                                         // Slide right to the beginning of the
327                                         // next mip map block
328                                         index = pow2_ceil(index + 1, level_scale_power);
329                                 }
330                         }
331
332                         // If individual samples within the limit of resolution,
333                         // do a linear search for the next transition within the
334                         // block
335                         if (min_length < MipMapScaleFactor) {
336                                 for (; index < end; index++) {
337                                         const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
338                                         if (sample != last_sample)
339                                                 break;
340                                 }
341                         }
342                 }
343
344                 //----- Store the edge -----//
345
346                 // Take the last sample of the quanization block
347                 const int64_t final_index = index + block_length;
348                 if (index + block_length > end)
349                         break;
350
351                 // Store the final state
352                 const bool final_sample = (get_unpacked_sample(final_index - 1) & sig_mask) != 0;
353                 edges.emplace_back(index, final_sample);
354
355                 index = final_index;
356                 last_sample = final_sample;
357
358                 if (first_change_only)
359                         break;
360         }
361
362         // Add the final state
363         if (!first_change_only) {
364                 const bool end_sample = get_unpacked_sample(end) & sig_mask;
365                 if (last_sample != end_sample)
366                         edges.emplace_back(end, end_sample);
367                 edges.emplace_back(end + 1, end_sample);
368         }
369 }
370
371 void LogicSegment::get_surrounding_edges(vector<EdgePair> &dest,
372         uint64_t origin_sample, float min_length, int sig_index)
373 {
374         // Put the edges vector on the heap, it can become quite big until we can
375         // use a get_subsampled_edges() implementation that searches backwards
376         vector<EdgePair>* edges = new vector<EdgePair>;
377
378         get_subsampled_edges(*edges, 0, origin_sample, min_length, sig_index, false);
379
380         // If we don't specify "first only", the first and last edge are the states
381         // at samples 0 and origin_sample. If only those exist, there are no edges
382         if (edges->size() == 2) {
383                 delete edges;
384                 return;
385         }
386
387         // Dismiss the entry for origin_sample so that back() gives us the
388         // real last entry
389         edges->pop_back();
390         dest.push_back(edges->back());
391         edges->clear();
392
393         get_subsampled_edges(*edges, origin_sample, sample_count_, min_length, sig_index, true);
394
395         // "first only" is specified, so nothing needs to be dismissed
396         if (edges->size() == 0) {
397                 delete edges;
398                 return;
399         }
400
401         dest.push_back(edges->front());
402
403         delete edges;
404 }
405
406 void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
407 {
408         lock_guard<recursive_mutex> lock(mutex_);
409
410         const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
411                 MipMapDataUnit) * MipMapDataUnit;
412
413         if (new_data_length > m.data_length) {
414                 m.data_length = new_data_length;
415
416                 // Padding is added to allow for the uint64_t write word
417                 m.data = realloc(m.data, new_data_length * unit_size_ +
418                         sizeof(uint64_t));
419         }
420 }
421
422 void LogicSegment::append_payload_to_mipmap()
423 {
424         MipMapLevel &m0 = mip_map_[0];
425         uint64_t prev_length;
426         uint8_t *dest_ptr;
427         SegmentRawDataIterator* it;
428         uint64_t accumulator;
429         unsigned int diff_counter;
430
431         // Expand the data buffer to fit the new samples
432         prev_length = m0.length;
433         m0.length = sample_count_ / MipMapScaleFactor;
434
435         // Break off if there are no new samples to compute
436         if (m0.length == prev_length)
437                 return;
438
439         reallocate_mipmap_level(m0);
440
441         dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
442
443         // Iterate through the samples to populate the first level mipmap
444         const uint64_t start_sample = prev_length * MipMapScaleFactor;
445         const uint64_t end_sample = m0.length * MipMapScaleFactor;
446
447         it = begin_raw_sample_iteration(start_sample);
448         for (uint64_t i = start_sample; i < end_sample;) {
449                 // Accumulate transitions which have occurred in this sample
450                 accumulator = 0;
451                 diff_counter = MipMapScaleFactor;
452                 while (diff_counter-- > 0) {
453                         const uint64_t sample = unpack_sample(it->value);
454                         accumulator |= last_append_sample_ ^ sample;
455                         last_append_sample_ = sample;
456                         continue_raw_sample_iteration(it, 1);
457                         i++;
458                 }
459
460                 pack_sample(dest_ptr, accumulator);
461                 dest_ptr += unit_size_;
462         }
463         end_raw_sample_iteration(it);
464
465         // Compute higher level mipmaps
466         for (unsigned int level = 1; level < ScaleStepCount; level++) {
467                 MipMapLevel &m = mip_map_[level];
468                 const MipMapLevel &ml = mip_map_[level - 1];
469
470                 // Expand the data buffer to fit the new samples
471                 prev_length = m.length;
472                 m.length = ml.length / MipMapScaleFactor;
473
474                 // Break off if there are no more samples to be computed
475                 if (m.length == prev_length)
476                         break;
477
478                 reallocate_mipmap_level(m);
479
480                 // Subsample the lower level
481                 const uint8_t* src_ptr = (uint8_t*)ml.data +
482                         unit_size_ * prev_length * MipMapScaleFactor;
483                 const uint8_t *const end_dest_ptr =
484                         (uint8_t*)m.data + unit_size_ * m.length;
485
486                 for (dest_ptr = (uint8_t*)m.data +
487                                 unit_size_ * prev_length;
488                                 dest_ptr < end_dest_ptr;
489                                 dest_ptr += unit_size_) {
490                         accumulator = 0;
491                         diff_counter = MipMapScaleFactor;
492                         while (diff_counter-- > 0) {
493                                 accumulator |= unpack_sample(src_ptr);
494                                 src_ptr += unit_size_;
495                         }
496
497                         pack_sample(dest_ptr, accumulator);
498                 }
499         }
500 }
501
502 uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
503 {
504         assert(index < sample_count_);
505
506         assert(unit_size_ <= 8);  // 8 * 8 = 64 channels
507         uint8_t data[8];
508
509         get_raw_samples(index, 1, data);
510
511         return unpack_sample(data);
512 }
513
514 uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
515 {
516         assert(level >= 0);
517         assert(mip_map_[level].data);
518         return unpack_sample((uint8_t*)mip_map_[level].data +
519                 unit_size_ * offset);
520 }
521
522 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
523 {
524         const uint64_t p = UINT64_C(1) << power;
525         return (x + p - 1) / p * p;
526 }
527
528 } // namespace data
529 } // namespace pv