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