b77c102d8ba2266de397f42223ffc9d5b4b0e3a5
[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 void LogicSegment::get_subsampled_edges(
188         vector<EdgePair> &edges,
189         uint64_t start, uint64_t end,
190         float min_length, int sig_index, bool first_change_only)
191 {
192         uint64_t index = start;
193         unsigned int level;
194         bool last_sample;
195         bool fast_forward;
196
197         assert(start <= end);
198         assert(min_length > 0);
199         assert(sig_index >= 0);
200         assert(sig_index < 64);
201
202         lock_guard<recursive_mutex> lock(mutex_);
203
204         // Make sure we only process as many samples as we have
205         if (end > get_sample_count())
206                 end = get_sample_count();
207
208         const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
209         const unsigned int min_level = max((int)floorf(logf(min_length) /
210                 LogMipMapScaleFactor) - 1, 0);
211         const uint64_t sig_mask = 1ULL << sig_index;
212
213         // Store the initial state
214         last_sample = (get_unpacked_sample(start) & sig_mask) != 0;
215         if (!first_change_only)
216                 edges.emplace_back(index++, last_sample);
217
218         while (index + block_length <= end) {
219                 //----- Continue to search -----//
220                 level = min_level;
221
222                 // We cannot fast-forward if there is no mip-map data at
223                 // the minimum level.
224                 fast_forward = (mip_map_[level].data != nullptr);
225
226                 if (min_length < MipMapScaleFactor) {
227                         // Search individual samples up to the beginning of
228                         // the next first level mip map block
229                         const uint64_t final_index = min(end, pow2_ceil(index, MipMapScalePower));
230
231                         for (; index < final_index &&
232                                         (index & ~((uint64_t)(~0) << MipMapScalePower)) != 0;
233                                         index++) {
234
235                                 const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
236
237                                 // If there was a change we cannot fast forward
238                                 if (sample != last_sample) {
239                                         fast_forward = false;
240                                         break;
241                                 }
242                         }
243                 } else {
244                         // If resolution is less than a mip map block,
245                         // round up to the beginning of the mip-map block
246                         // for this level of detail
247                         const int min_level_scale_power = (level + 1) * MipMapScalePower;
248                         index = pow2_ceil(index, min_level_scale_power);
249                         if (index >= end)
250                                 break;
251
252                         // We can fast forward only if there was no change
253                         const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
254                         if (last_sample != sample)
255                                 fast_forward = false;
256                 }
257
258                 if (fast_forward) {
259
260                         // Fast forward: This involves zooming out to higher
261                         // levels of the mip map searching for changes, then
262                         // zooming in on them to find the point where the edge
263                         // begins.
264
265                         // Slide right and zoom out at the beginnings of mip-map
266                         // blocks until we encounter a change
267                         while (true) {
268                                 const int level_scale_power = (level + 1) * MipMapScalePower;
269                                 const uint64_t offset = index >> level_scale_power;
270
271                                 // Check if we reached the last block at this
272                                 // level, or if there was a change in this block
273                                 if (offset >= mip_map_[level].length ||
274                                         (get_subsample(level, offset) & sig_mask))
275                                         break;
276
277                                 if ((offset & ~((uint64_t)(~0) << MipMapScalePower)) == 0) {
278                                         // If we are now at the beginning of a
279                                         // higher level mip-map block ascend one
280                                         // level
281                                         if ((level + 1 >= ScaleStepCount) || (!mip_map_[level + 1].data))
282                                                 break;
283
284                                         level++;
285                                 } else {
286                                         // Slide right to the beginning of the
287                                         // next mip map block
288                                         index = pow2_ceil(index + 1, level_scale_power);
289                                 }
290                         }
291
292                         // Zoom in, and slide right until we encounter a change,
293                         // and repeat until we reach min_level
294                         while (true) {
295                                 assert(mip_map_[level].data);
296
297                                 const int level_scale_power = (level + 1) * MipMapScalePower;
298                                 const uint64_t offset = index >> level_scale_power;
299
300                                 // Check if we reached the last block at this
301                                 // level, or if there was a change in this block
302                                 if (offset >= mip_map_[level].length ||
303                                                 (get_subsample(level, offset) & sig_mask)) {
304                                         // Zoom in unless we reached the minimum
305                                         // zoom
306                                         if (level == min_level)
307                                                 break;
308
309                                         level--;
310                                 } else {
311                                         // Slide right to the beginning of the
312                                         // next mip map block
313                                         index = pow2_ceil(index + 1, level_scale_power);
314                                 }
315                         }
316
317                         // If individual samples within the limit of resolution,
318                         // do a linear search for the next transition within the
319                         // block
320                         if (min_length < MipMapScaleFactor) {
321                                 for (; index < end; index++) {
322                                         const bool sample = (get_unpacked_sample(index) & sig_mask) != 0;
323                                         if (sample != last_sample)
324                                                 break;
325                                 }
326                         }
327                 }
328
329                 //----- Store the edge -----//
330
331                 // Take the last sample of the quanization block
332                 const int64_t final_index = index + block_length;
333                 if (index + block_length > end)
334                         break;
335
336                 // Store the final state
337                 const bool final_sample = (get_unpacked_sample(final_index - 1) & sig_mask) != 0;
338                 edges.emplace_back(index, final_sample);
339
340                 index = final_index;
341                 last_sample = final_sample;
342
343                 if (first_change_only)
344                         break;
345         }
346
347         // Add the final state
348         if (!first_change_only) {
349                 const bool end_sample = get_unpacked_sample(end) & sig_mask;
350                 if (last_sample != end_sample)
351                         edges.emplace_back(end, end_sample);
352                 edges.emplace_back(end + 1, end_sample);
353         }
354 }
355
356 void LogicSegment::get_surrounding_edges(vector<EdgePair> &dest,
357         uint64_t origin_sample, float min_length, int sig_index)
358 {
359         if (origin_sample >= sample_count_)
360                 return;
361
362         // Put the edges vector on the heap, it can become quite big until we can
363         // use a get_subsampled_edges() implementation that searches backwards
364         vector<EdgePair>* edges = new vector<EdgePair>;
365
366         // Get all edges to the left of origin_sample
367         get_subsampled_edges(*edges, 0, origin_sample, min_length, sig_index, false);
368
369         // If we don't specify "first only", the first and last edge are the states
370         // at samples 0 and origin_sample. If only those exist, there are no edges
371         if (edges->size() == 2) {
372                 delete edges;
373                 return;
374         }
375
376         // Dismiss the entry for origin_sample so that back() gives us the
377         // real last entry
378         edges->pop_back();
379         dest.push_back(edges->back());
380         edges->clear();
381
382         // Get first edge to the right of origin_sample
383         get_subsampled_edges(*edges, origin_sample, sample_count_, min_length, sig_index, true);
384
385         // "first only" is specified, so nothing needs to be dismissed
386         if (edges->size() == 0) {
387                 delete edges;
388                 return;
389         }
390
391         dest.push_back(edges->front());
392
393         delete edges;
394 }
395
396 void LogicSegment::reallocate_mipmap_level(MipMapLevel &m)
397 {
398         lock_guard<recursive_mutex> lock(mutex_);
399
400         const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
401                 MipMapDataUnit) * MipMapDataUnit;
402
403         if (new_data_length > m.data_length) {
404                 m.data_length = new_data_length;
405
406                 // Padding is added to allow for the uint64_t write word
407                 m.data = realloc(m.data, new_data_length * unit_size_ +
408                         sizeof(uint64_t));
409         }
410 }
411
412 void LogicSegment::append_payload_to_mipmap()
413 {
414         MipMapLevel &m0 = mip_map_[0];
415         uint64_t prev_length;
416         uint8_t *dest_ptr;
417         SegmentDataIterator* it;
418         uint64_t accumulator;
419         unsigned int diff_counter;
420
421         // Expand the data buffer to fit the new samples
422         prev_length = m0.length;
423         m0.length = sample_count_ / MipMapScaleFactor;
424
425         // Break off if there are no new samples to compute
426         if (m0.length == prev_length)
427                 return;
428
429         reallocate_mipmap_level(m0);
430
431         dest_ptr = (uint8_t*)m0.data + prev_length * unit_size_;
432
433         // Iterate through the samples to populate the first level mipmap
434         const uint64_t start_sample = prev_length * MipMapScaleFactor;
435         const uint64_t end_sample = m0.length * MipMapScaleFactor;
436
437         it = begin_sample_iteration(start_sample);
438         for (uint64_t i = start_sample; i < end_sample;) {
439                 // Accumulate transitions which have occurred in this sample
440                 accumulator = 0;
441                 diff_counter = MipMapScaleFactor;
442                 while (diff_counter-- > 0) {
443                         const uint64_t sample = unpack_sample(get_iterator_value(it));
444                         accumulator |= last_append_sample_ ^ sample;
445                         last_append_sample_ = sample;
446                         continue_sample_iteration(it, 1);
447                         i++;
448                 }
449
450                 pack_sample(dest_ptr, accumulator);
451                 dest_ptr += unit_size_;
452         }
453         end_sample_iteration(it);
454
455         // Compute higher level mipmaps
456         for (unsigned int level = 1; level < ScaleStepCount; level++) {
457                 MipMapLevel &m = mip_map_[level];
458                 const MipMapLevel &ml = mip_map_[level - 1];
459
460                 // Expand the data buffer to fit the new samples
461                 prev_length = m.length;
462                 m.length = ml.length / MipMapScaleFactor;
463
464                 // Break off if there are no more samples to be computed
465                 if (m.length == prev_length)
466                         break;
467
468                 reallocate_mipmap_level(m);
469
470                 // Subsample the lower level
471                 const uint8_t* src_ptr = (uint8_t*)ml.data +
472                         unit_size_ * prev_length * MipMapScaleFactor;
473                 const uint8_t *const end_dest_ptr =
474                         (uint8_t*)m.data + unit_size_ * m.length;
475
476                 for (dest_ptr = (uint8_t*)m.data +
477                                 unit_size_ * prev_length;
478                                 dest_ptr < end_dest_ptr;
479                                 dest_ptr += unit_size_) {
480                         accumulator = 0;
481                         diff_counter = MipMapScaleFactor;
482                         while (diff_counter-- > 0) {
483                                 accumulator |= unpack_sample(src_ptr);
484                                 src_ptr += unit_size_;
485                         }
486
487                         pack_sample(dest_ptr, accumulator);
488                 }
489         }
490 }
491
492 uint64_t LogicSegment::get_unpacked_sample(uint64_t index) const
493 {
494         assert(index < sample_count_);
495
496         assert(unit_size_ <= 8);  // 8 * 8 = 64 channels
497         uint8_t data[8];
498
499         get_raw_samples(index, 1, data);
500
501         return unpack_sample(data);
502 }
503
504 uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
505 {
506         assert(level >= 0);
507         assert(mip_map_[level].data);
508         return unpack_sample((uint8_t*)mip_map_[level].data +
509                 unit_size_ * offset);
510 }
511
512 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
513 {
514         const uint64_t p = UINT64_C(1) << power;
515         return (x + p - 1) / p * p;
516 }
517
518 } // namespace data
519 } // namespace pv