Fix some disappearing annotations
[pulseview.git] / pv / data / analogsnapshot.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 <algorithm>
29
30 #include <boost/foreach.hpp>
31
32 #include "analogsnapshot.h"
33
34 using boost::lock_guard;
35 using boost::recursive_mutex;
36 using std::max;
37 using std::max_element;
38 using std::min;
39 using std::min_element;
40
41 namespace pv {
42 namespace data {
43
44 const int AnalogSnapshot::EnvelopeScalePower = 4;
45 const int AnalogSnapshot::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
46 const float AnalogSnapshot::LogEnvelopeScaleFactor =
47         logf(EnvelopeScaleFactor);
48 const uint64_t AnalogSnapshot::EnvelopeDataUnit = 64*1024;      // bytes
49
50 AnalogSnapshot::AnalogSnapshot() :
51         Snapshot(sizeof(float))
52 {
53         lock_guard<recursive_mutex> lock(_mutex);
54         memset(_envelope_levels, 0, sizeof(_envelope_levels));
55 }
56
57 AnalogSnapshot::~AnalogSnapshot()
58 {
59         lock_guard<recursive_mutex> lock(_mutex);
60         BOOST_FOREACH(Envelope &e, _envelope_levels)
61                 free(e.samples);
62 }
63
64 void AnalogSnapshot::append_interleaved_samples(const float *data,
65         size_t sample_count, size_t stride)
66 {
67         assert(_unit_size == sizeof(float));
68
69         lock_guard<recursive_mutex> lock(_mutex);
70
71         _data = realloc(_data, (_sample_count + sample_count) * sizeof(float));
72
73         float *dst = (float*)_data + _sample_count;
74         const float *dst_end = dst + sample_count;
75         while (dst != dst_end)
76         {
77                 *dst++ = *data;
78                 data += stride;
79         }
80
81         _sample_count += sample_count;
82
83         // Generate the first mip-map from the data
84         append_payload_to_envelope_levels();
85 }
86
87 const float* AnalogSnapshot::get_samples(
88         int64_t start_sample, int64_t end_sample) const
89 {
90         assert(start_sample >= 0);
91         assert(start_sample < (int64_t)_sample_count);
92         assert(end_sample >= 0);
93         assert(end_sample < (int64_t)_sample_count);
94         assert(start_sample <= end_sample);
95
96         lock_guard<recursive_mutex> lock(_mutex);
97
98         float *const data = new float[end_sample - start_sample];
99         memcpy(data, (float*)_data + start_sample, sizeof(float) *
100                 (end_sample - start_sample));
101         return data;
102 }
103
104 void AnalogSnapshot::get_envelope_section(EnvelopeSection &s,
105         uint64_t start, uint64_t end, float min_length) const
106 {
107         assert(end <= get_sample_count());
108         assert(start <= end);
109         assert(min_length > 0);
110
111         lock_guard<recursive_mutex> lock(_mutex);
112
113         const unsigned int min_level = max((int)floorf(logf(min_length) /
114                 LogEnvelopeScaleFactor) - 1, 0);
115         const unsigned int scale_power = (min_level + 1) *
116                 EnvelopeScalePower;
117         start >>= scale_power;
118         end >>= scale_power;
119
120         s.start = start << scale_power;
121         s.scale = 1 << scale_power;
122         s.length = end - start;
123         s.samples = new EnvelopeSample[s.length];
124         memcpy(s.samples, _envelope_levels[min_level].samples + start,
125                 s.length * sizeof(EnvelopeSample));
126 }
127
128 void AnalogSnapshot::reallocate_envelope(Envelope &e)
129 {
130         const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
131                 EnvelopeDataUnit) * EnvelopeDataUnit;
132         if (new_data_length > e.data_length)
133         {
134                 e.data_length = new_data_length;
135                 e.samples = (EnvelopeSample*)realloc(e.samples,
136                         new_data_length * sizeof(EnvelopeSample));
137         }
138 }
139
140 void AnalogSnapshot::append_payload_to_envelope_levels()
141 {
142         Envelope &e0 = _envelope_levels[0];
143         uint64_t prev_length;
144         EnvelopeSample *dest_ptr;
145
146         // Expand the data buffer to fit the new samples
147         prev_length = e0.length;
148         e0.length = _sample_count / EnvelopeScaleFactor;
149
150         // Break off if there are no new samples to compute
151         if (e0.length == prev_length)
152                 return;
153
154         reallocate_envelope(e0);
155
156         dest_ptr = e0.samples + prev_length;
157
158         // Iterate through the samples to populate the first level mipmap
159         const float *const end_src_ptr = (float*)_data +
160                 e0.length * EnvelopeScaleFactor;
161         for (const float *src_ptr = (float*)_data +
162                 prev_length * EnvelopeScaleFactor;
163                 src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor)
164         {
165                 const EnvelopeSample sub_sample = {
166                         *min_element(src_ptr, src_ptr + EnvelopeScaleFactor),
167                         *max_element(src_ptr, src_ptr + EnvelopeScaleFactor),
168                 };
169
170                 *dest_ptr++ = sub_sample;
171         }
172
173         // Compute higher level mipmaps
174         for (unsigned int level = 1; level < ScaleStepCount; level++)
175         {
176                 Envelope &e = _envelope_levels[level];
177                 const Envelope &el = _envelope_levels[level-1];
178
179                 // Expand the data buffer to fit the new samples
180                 prev_length = e.length;
181                 e.length = el.length / EnvelopeScaleFactor;
182
183                 // Break off if there are no more samples to computed
184                 if (e.length == prev_length)
185                         break;
186
187                 reallocate_envelope(e);
188
189                 // Subsample the level lower level
190                 const EnvelopeSample *src_ptr =
191                         el.samples + prev_length * EnvelopeScaleFactor;
192                 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
193                 for (dest_ptr = e.samples + prev_length;
194                         dest_ptr < end_dest_ptr; dest_ptr++)
195                 {
196                         const EnvelopeSample *const end_src_ptr =
197                                 src_ptr + EnvelopeScaleFactor;
198
199                         EnvelopeSample sub_sample = *src_ptr++;
200                         while (src_ptr < end_src_ptr)
201                         {
202                                 sub_sample.min = min(sub_sample.min, src_ptr->min);
203                                 sub_sample.max = max(sub_sample.max, src_ptr->max);
204                                 src_ptr++;
205                         }
206
207                         *dest_ptr = sub_sample;
208                 }
209         }
210 }
211
212 } // namespace data
213 } // namespace pv