]> sigrok.org Git - pulseview.git/blob - pv/data/logicsnapshot.cpp
Implement portable LA sample packing and unpacking.
[pulseview.git] / pv / data / logicsnapshot.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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <extdef.h>
22
23 #include <assert.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <math.h>
27
28 #include <boost/foreach.hpp>
29
30 #include "logicsnapshot.h"
31
32 using boost::lock_guard;
33 using boost::recursive_mutex;
34 using std::max;
35 using std::min;
36 using std::pair;
37
38 namespace pv {
39 namespace data {
40
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
45
46 LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic,
47                              const uint64_t expected_num_samples) :
48         Snapshot(logic.unitsize),
49         _last_append_sample(0)
50 {
51         set_capacity(expected_num_samples);
52
53         lock_guard<recursive_mutex> lock(_mutex);
54         memset(_mip_map, 0, sizeof(_mip_map));
55         append_payload(logic);
56 }
57
58 LogicSnapshot::~LogicSnapshot()
59 {
60         lock_guard<recursive_mutex> lock(_mutex);
61         BOOST_FOREACH(MipMapLevel &l, _mip_map)
62                 free(l.data);
63 }
64
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
131 void LogicSnapshot::append_payload(
132         const sr_datafeed_logic &logic)
133 {
134         assert(_unit_size == logic.unitsize);
135         assert((logic.length % _unit_size) == 0);
136
137         lock_guard<recursive_mutex> lock(_mutex);
138
139         append_data(logic.data, logic.length / _unit_size);
140
141         // Generate the first mip-map from the data
142         append_payload_to_mipmap();
143 }
144
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);
150         assert(start_sample <= (int64_t)_sample_count);
151         assert(end_sample >= 0);
152         assert(end_sample <= (int64_t)_sample_count);
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
161 void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
162 {
163         const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
164                 MipMapDataUnit) * MipMapDataUnit;
165         if (new_data_length > m.data_length)
166         {
167                 m.data_length = new_data_length;
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));
172         }
173 }
174
175 void LogicSnapshot::append_payload_to_mipmap()
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;
186         m0.length = _sample_count / MipMapScaleFactor;
187
188         // Break off if there are no new samples to compute
189         if (m0.length == prev_length)
190                 return;
191
192         reallocate_mipmap_level(m0);
193
194         dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
195
196         // Iterate through the samples to populate the first level mipmap
197         const uint8_t *const end_src_ptr = (uint8_t*)_data +
198                 m0.length * _unit_size * MipMapScaleFactor;
199         for (src_ptr = (uint8_t*)_data +
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;
206                 while (diff_counter-- > 0)
207                 {
208                         const uint64_t sample = unpack_sample(src_ptr);
209                         accumulator |= _last_append_sample ^ sample;
210                         _last_append_sample = sample;
211                         src_ptr += _unit_size;
212                 }
213
214                 pack_sample(dest_ptr, accumulator);
215                 dest_ptr += _unit_size;
216         }
217
218         // Compute higher level mipmaps
219         for (unsigned int level = 1; level < ScaleStepCount; level++)
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
229                 if (m.length == prev_length)
230                         break;
231
232                 reallocate_mipmap_level(m);
233
234                 // Subsample the level lower level
235                 src_ptr = (uint8_t*)ml.data +
236                         _unit_size * prev_length * MipMapScaleFactor;
237                 const uint8_t *const end_dest_ptr =
238                         (uint8_t*)m.data + _unit_size * m.length;
239                 for (dest_ptr = (uint8_t*)m.data +
240                         _unit_size * prev_length;
241                         dest_ptr < end_dest_ptr;
242                         dest_ptr += _unit_size)
243                 {
244                         accumulator = 0;
245                         diff_counter = MipMapScaleFactor;
246                         while (diff_counter-- > 0)
247                         {
248                                 accumulator |= unpack_sample(src_ptr);
249                                 src_ptr += _unit_size;
250                         }
251
252                         pack_sample(dest_ptr, accumulator);
253                 }
254         }
255 }
256
257 uint64_t LogicSnapshot::get_sample(uint64_t index) const
258 {
259         assert(_data);
260         assert(index < _sample_count);
261
262         return unpack_sample((uint8_t*)_data + index * _unit_size);
263 }
264
265 void LogicSnapshot::get_subsampled_edges(
266         std::vector<EdgePair> &edges,
267         uint64_t start, uint64_t end,
268         float min_length, int sig_index)
269 {
270         uint64_t index = start;
271         unsigned int level;
272         bool last_sample;
273         bool fast_forward;
274
275         assert(end <= get_sample_count());
276         assert(start <= end);
277         assert(min_length > 0);
278         assert(sig_index >= 0);
279         assert(sig_index < 64);
280
281         lock_guard<recursive_mutex> lock(_mutex);
282
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) /
285                 LogMipMapScaleFactor) - 1, 0);
286         const uint64_t sig_mask = 1ULL << sig_index;
287
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));
291
292         while (index + block_length <= end)
293         {
294                 //----- Continue to search -----//
295                 level = min_level;
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);
300
301                 if (min_length < MipMapScaleFactor)
302                 {
303                         // Search individual samples up to the beginning of
304                         // the next first level mip map block
305                         const uint64_t final_index = min(end,
306                                 pow2_ceil(index, MipMapScalePower));
307
308                         for (; index < final_index &&
309                                 (index & ~(~0 << MipMapScalePower)) != 0;
310                                 index++)
311                         {
312                                 const bool sample =
313                                         (get_sample(index) & sig_mask) != 0;
314
315                                 // If there was a change we cannot fast forward
316                                 if (sample != last_sample) {
317                                         fast_forward = false;
318                                         break;
319                                 }
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);
330                         if (index >= end)
331                                 break;
332
333                         // We can fast forward only if there was no change
334                         const bool sample =
335                                 (get_sample(index) & sig_mask) != 0;
336                         if (last_sample != sample)
337                                 fast_forward = false;
338                 }
339
340                 if (fast_forward) {
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
349                         while (1) {
350                                 const int level_scale_power =
351                                         (level + 1) * MipMapScalePower;
352                                 const uint64_t offset =
353                                         index >> level_scale_power;
354
355                                 // Check if we reached the last block at this
356                                 // level, or if there was a change in this block
357                                 if (offset >= _mip_map[level].length ||
358                                         (get_subsample(level, offset) &
359                                                 sig_mask))
360                                         break;
361
362                                 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
363                                         // If we are now at the beginning of a
364                                         // higher level mip-map block ascend one
365                                         // level
366                                         if (level + 1 >= ScaleStepCount ||
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                                 }
377                         }
378
379                         // Zoom in, and slide right until we encounter a change,
380                         // and repeat until we reach min_level
381                         while (1) {
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;
388
389                                 // Check if we reached the last block at this
390                                 // level, or if there was a change in this block
391                                 if (offset >= _mip_map[level].length ||
392                                         (get_subsample(level, offset) &
393                                                 sig_mask)) {
394                                         // Zoom in unless we reached the minimum
395                                         // zoom
396                                         if (level == min_level)
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                                 }
406                         }
407
408                         // If individual samples within the limit of resolution,
409                         // do a linear search for the next transition within the
410                         // block
411                         if (min_length < MipMapScaleFactor) {
412                                 for (; index < end; index++) {
413                                         const bool sample = (get_sample(index) &
414                                                 sig_mask) != 0;
415                                         if (sample != last_sample)
416                                                 break;
417                                 }
418                         }
419                 }
420
421                 //----- Store the edge -----//
422
423                 // Take the last sample of the quanization block
424                 const int64_t final_index = index + block_length;
425                 if (index + block_length > end)
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;
435         }
436
437         // Add the final state
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));
442 }
443
444 uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
445 {
446         assert(level >= 0);
447         assert(_mip_map[level].data);
448         return unpack_sample((uint8_t*)_mip_map[level].data +
449                 _unit_size * offset);
450 }
451
452 uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
453 {
454         const uint64_t p = 1 << power;
455         return (x + p - 1) / p * p;
456 }
457
458 } // namespace data
459 } // namespace pv