Renamed DecoderStack::_mutex to _output_mutex
[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 "config.h"
31 #include "logicsnapshot.h"
32
33 using boost::lock_guard;
34 using boost::recursive_mutex;
35 using std::max;
36 using std::min;
37 using std::pair;
38
39 namespace pv {
40 namespace data {
41
42 const int LogicSnapshot::MipMapScalePower = 4;
43 const int LogicSnapshot::MipMapScaleFactor = 1 << MipMapScalePower;
44 const float LogicSnapshot::LogMipMapScaleFactor = logf(MipMapScaleFactor);
45 const uint64_t LogicSnapshot::MipMapDataUnit = 64*1024; // bytes
46
47 LogicSnapshot::LogicSnapshot(const sr_datafeed_logic &logic,
48                              const uint64_t expected_num_samples) :
49         Snapshot(logic.unitsize),
50         _last_append_sample(0)
51 {
52         set_capacity(expected_num_samples);
53
54         lock_guard<recursive_mutex> lock(_mutex);
55         memset(_mip_map, 0, sizeof(_mip_map));
56         append_payload(logic);
57 }
58
59 LogicSnapshot::~LogicSnapshot()
60 {
61         lock_guard<recursive_mutex> lock(_mutex);
62         BOOST_FOREACH(MipMapLevel &l, _mip_map)
63                 free(l.data);
64 }
65
66 uint64_t LogicSnapshot::unpack_sample(const uint8_t *ptr) const
67 {
68 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
69         return *(uint64_t*)ptr;
70 #else
71         uint64_t value = 0;
72         switch(_unit_size) {
73         default:
74                 value |= ((uint64_t)ptr[7]) << 56;
75                 /* FALLTHRU */
76         case 7:
77                 value |= ((uint64_t)ptr[6]) << 48;
78                 /* FALLTHRU */
79         case 6:
80                 value |= ((uint64_t)ptr[5]) << 40;
81                 /* FALLTHRU */
82         case 5:
83                 value |= ((uint64_t)ptr[4]) << 32;
84                 /* FALLTHRU */
85         case 4:
86                 value |= ((uint32_t)ptr[3]) << 24;
87                 /* FALLTHRU */
88         case 3:
89                 value |= ((uint32_t)ptr[2]) << 16;
90                 /* FALLTHRU */
91         case 2:
92                 value |= ptr[1] << 8;
93                 /* FALLTHRU */
94         case 1:
95                 value |= ptr[0];
96                 /* FALLTHRU */
97         case 0:
98                 break;
99         }
100         return value;
101 #endif
102 }
103
104 void LogicSnapshot::pack_sample(uint8_t *ptr, uint64_t value)
105 {
106 #ifdef HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS
107         *(uint64_t*)ptr = value;
108 #else
109         switch(_unit_size) {
110         default:
111                 ptr[7] = value >> 56;
112                 /* FALLTHRU */
113         case 7:
114                 ptr[6] = value >> 48;
115                 /* FALLTHRU */
116         case 6:
117                 ptr[5] = value >> 40;
118                 /* FALLTHRU */
119         case 5:
120                 ptr[4] = value >> 32;
121                 /* FALLTHRU */
122         case 4:
123                 ptr[3] = value >> 24;
124                 /* FALLTHRU */
125         case 3:
126                 ptr[2] = value >> 16;
127                 /* FALLTHRU */
128         case 2:
129                 ptr[1] = value >> 8;
130                 /* FALLTHRU */
131         case 1:
132                 ptr[0] = value;
133                 /* FALLTHRU */
134         case 0:
135                 break;
136         }
137 #endif
138 }
139
140 void LogicSnapshot::append_payload(
141         const sr_datafeed_logic &logic)
142 {
143         assert(_unit_size == logic.unitsize);
144         assert((logic.length % _unit_size) == 0);
145
146         lock_guard<recursive_mutex> lock(_mutex);
147
148         append_data(logic.data, logic.length / _unit_size);
149
150         // Generate the first mip-map from the data
151         append_payload_to_mipmap();
152 }
153
154 void LogicSnapshot::get_samples(uint8_t *const data,
155         int64_t start_sample, int64_t end_sample) const
156 {
157         assert(data);
158         assert(start_sample >= 0);
159         assert(start_sample <= (int64_t)_sample_count);
160         assert(end_sample >= 0);
161         assert(end_sample <= (int64_t)_sample_count);
162         assert(start_sample <= end_sample);
163
164         lock_guard<recursive_mutex> lock(_mutex);
165
166         const size_t size = (end_sample - start_sample) * _unit_size;
167         memcpy(data, (const uint8_t*)_data + start_sample * _unit_size, size);
168 }
169
170 void LogicSnapshot::reallocate_mipmap_level(MipMapLevel &m)
171 {
172         const uint64_t new_data_length = ((m.length + MipMapDataUnit - 1) /
173                 MipMapDataUnit) * MipMapDataUnit;
174         if (new_data_length > m.data_length)
175         {
176                 m.data_length = new_data_length;
177
178                 // Padding is added to allow for the uint64_t write word
179                 m.data = realloc(m.data, new_data_length * _unit_size +
180                         sizeof(uint64_t));
181         }
182 }
183
184 void LogicSnapshot::append_payload_to_mipmap()
185 {
186         MipMapLevel &m0 = _mip_map[0];
187         uint64_t prev_length;
188         const uint8_t *src_ptr;
189         uint8_t *dest_ptr;
190         uint64_t accumulator;
191         unsigned int diff_counter;
192
193         // Expand the data buffer to fit the new samples
194         prev_length = m0.length;
195         m0.length = _sample_count / MipMapScaleFactor;
196
197         // Break off if there are no new samples to compute
198         if (m0.length == prev_length)
199                 return;
200
201         reallocate_mipmap_level(m0);
202
203         dest_ptr = (uint8_t*)m0.data + prev_length * _unit_size;
204
205         // Iterate through the samples to populate the first level mipmap
206         const uint8_t *const end_src_ptr = (uint8_t*)_data +
207                 m0.length * _unit_size * MipMapScaleFactor;
208         for (src_ptr = (uint8_t*)_data +
209                 prev_length * _unit_size * MipMapScaleFactor;
210                 src_ptr < end_src_ptr;)
211         {
212                 // Accumulate transitions which have occurred in this sample
213                 accumulator = 0;
214                 diff_counter = MipMapScaleFactor;
215                 while (diff_counter-- > 0)
216                 {
217                         const uint64_t sample = unpack_sample(src_ptr);
218                         accumulator |= _last_append_sample ^ sample;
219                         _last_append_sample = sample;
220                         src_ptr += _unit_size;
221                 }
222
223                 pack_sample(dest_ptr, accumulator);
224                 dest_ptr += _unit_size;
225         }
226
227         // Compute higher level mipmaps
228         for (unsigned int level = 1; level < ScaleStepCount; level++)
229         {
230                 MipMapLevel &m = _mip_map[level];
231                 const MipMapLevel &ml = _mip_map[level-1];
232
233                 // Expand the data buffer to fit the new samples
234                 prev_length = m.length;
235                 m.length = ml.length / MipMapScaleFactor;
236
237                 // Break off if there are no more samples to computed
238                 if (m.length == prev_length)
239                         break;
240
241                 reallocate_mipmap_level(m);
242
243                 // Subsample the level lower level
244                 src_ptr = (uint8_t*)ml.data +
245                         _unit_size * prev_length * MipMapScaleFactor;
246                 const uint8_t *const end_dest_ptr =
247                         (uint8_t*)m.data + _unit_size * m.length;
248                 for (dest_ptr = (uint8_t*)m.data +
249                         _unit_size * prev_length;
250                         dest_ptr < end_dest_ptr;
251                         dest_ptr += _unit_size)
252                 {
253                         accumulator = 0;
254                         diff_counter = MipMapScaleFactor;
255                         while (diff_counter-- > 0)
256                         {
257                                 accumulator |= unpack_sample(src_ptr);
258                                 src_ptr += _unit_size;
259                         }
260
261                         pack_sample(dest_ptr, accumulator);
262                 }
263         }
264 }
265
266 uint64_t LogicSnapshot::get_sample(uint64_t index) const
267 {
268         assert(_data);
269         assert(index < _sample_count);
270
271         return unpack_sample((uint8_t*)_data + index * _unit_size);
272 }
273
274 void LogicSnapshot::get_subsampled_edges(
275         std::vector<EdgePair> &edges,
276         uint64_t start, uint64_t end,
277         float min_length, int sig_index)
278 {
279         uint64_t index = start;
280         unsigned int level;
281         bool last_sample;
282         bool fast_forward;
283
284         assert(end <= get_sample_count());
285         assert(start <= end);
286         assert(min_length > 0);
287         assert(sig_index >= 0);
288         assert(sig_index < 64);
289
290         lock_guard<recursive_mutex> lock(_mutex);
291
292         const uint64_t block_length = (uint64_t)max(min_length, 1.0f);
293         const unsigned int min_level = max((int)floorf(logf(min_length) /
294                 LogMipMapScaleFactor) - 1, 0);
295         const uint64_t sig_mask = 1ULL << sig_index;
296
297         // Store the initial state
298         last_sample = (get_sample(start) & sig_mask) != 0;
299         edges.push_back(pair<int64_t, bool>(index++, last_sample));
300
301         while (index + block_length <= end)
302         {
303                 //----- Continue to search -----//
304                 level = min_level;
305
306                 // We cannot fast-forward if there is no mip-map data at
307                 // at the minimum level.
308                 fast_forward = (_mip_map[level].data != NULL);
309
310                 if (min_length < MipMapScaleFactor)
311                 {
312                         // Search individual samples up to the beginning of
313                         // the next first level mip map block
314                         const uint64_t final_index = min(end,
315                                 pow2_ceil(index, MipMapScalePower));
316
317                         for (; index < final_index &&
318                                 (index & ~(~0 << MipMapScalePower)) != 0;
319                                 index++)
320                         {
321                                 const bool sample =
322                                         (get_sample(index) & sig_mask) != 0;
323
324                                 // If there was a change we cannot fast forward
325                                 if (sample != last_sample) {
326                                         fast_forward = false;
327                                         break;
328                                 }
329                         }
330                 }
331                 else
332                 {
333                         // If resolution is less than a mip map block,
334                         // round up to the beginning of the mip-map block
335                         // for this level of detail
336                         const int min_level_scale_power =
337                                 (level + 1) * MipMapScalePower;
338                         index = pow2_ceil(index, min_level_scale_power);
339                         if (index >= end)
340                                 break;
341
342                         // We can fast forward only if there was no change
343                         const bool sample =
344                                 (get_sample(index) & sig_mask) != 0;
345                         if (last_sample != sample)
346                                 fast_forward = false;
347                 }
348
349                 if (fast_forward) {
350
351                         // Fast forward: This involves zooming out to higher
352                         // levels of the mip map searching for changes, then
353                         // zooming in on them to find the point where the edge
354                         // begins.
355
356                         // Slide right and zoom out at the beginnings of mip-map
357                         // blocks until we encounter a change
358                         while (1) {
359                                 const int level_scale_power =
360                                         (level + 1) * MipMapScalePower;
361                                 const uint64_t offset =
362                                         index >> level_scale_power;
363
364                                 // Check if we reached the last block at this
365                                 // level, or if there was a change in this block
366                                 if (offset >= _mip_map[level].length ||
367                                         (get_subsample(level, offset) &
368                                                 sig_mask))
369                                         break;
370
371                                 if ((offset & ~(~0 << MipMapScalePower)) == 0) {
372                                         // If we are now at the beginning of a
373                                         // higher level mip-map block ascend one
374                                         // level
375                                         if (level + 1 >= ScaleStepCount ||
376                                                 !_mip_map[level + 1].data)
377                                                 break;
378
379                                         level++;
380                                 } else {
381                                         // Slide right to the beginning of the
382                                         // next mip map block
383                                         index = pow2_ceil(index + 1,
384                                                 level_scale_power);
385                                 }
386                         }
387
388                         // Zoom in, and slide right until we encounter a change,
389                         // and repeat until we reach min_level
390                         while (1) {
391                                 assert(_mip_map[level].data);
392
393                                 const int level_scale_power =
394                                         (level + 1) * MipMapScalePower;
395                                 const uint64_t offset =
396                                         index >> level_scale_power;
397
398                                 // Check if we reached the last block at this
399                                 // level, or if there was a change in this block
400                                 if (offset >= _mip_map[level].length ||
401                                         (get_subsample(level, offset) &
402                                                 sig_mask)) {
403                                         // Zoom in unless we reached the minimum
404                                         // zoom
405                                         if (level == min_level)
406                                                 break;
407
408                                         level--;
409                                 } else {
410                                         // Slide right to the beginning of the
411                                         // next mip map block
412                                         index = pow2_ceil(index + 1,
413                                                 level_scale_power);
414                                 }
415                         }
416
417                         // If individual samples within the limit of resolution,
418                         // do a linear search for the next transition within the
419                         // block
420                         if (min_length < MipMapScaleFactor) {
421                                 for (; index < end; index++) {
422                                         const bool sample = (get_sample(index) &
423                                                 sig_mask) != 0;
424                                         if (sample != last_sample)
425                                                 break;
426                                 }
427                         }
428                 }
429
430                 //----- Store the edge -----//
431
432                 // Take the last sample of the quanization block
433                 const int64_t final_index = index + block_length;
434                 if (index + block_length > end)
435                         break;
436
437                 // Store the final state
438                 const bool final_sample =
439                         (get_sample(final_index - 1) & sig_mask) != 0;
440                 edges.push_back(pair<int64_t, bool>(index, final_sample));
441
442                 index = final_index;
443                 last_sample = final_sample;
444         }
445
446         // Add the final state
447         const bool end_sample = get_sample(end) & sig_mask;
448         if (last_sample != end_sample)
449                 edges.push_back(pair<int64_t, bool>(end, end_sample));
450         edges.push_back(pair<int64_t, bool>(end + 1, end_sample));
451 }
452
453 uint64_t LogicSnapshot::get_subsample(int level, uint64_t offset) const
454 {
455         assert(level >= 0);
456         assert(_mip_map[level].data);
457         return unpack_sample((uint8_t*)_mip_map[level].data +
458                 _unit_size * offset);
459 }
460
461 uint64_t LogicSnapshot::pow2_ceil(uint64_t x, unsigned int power)
462 {
463         const uint64_t p = 1 << power;
464         return (x + p - 1) / p * p;
465 }
466
467 } // namespace data
468 } // namespace pv