]> sigrok.org Git - pulseview.git/blob - pv/data/analogsegment.cpp
Make get_raw_samples() use provided mem instead of allocating
[pulseview.git] / pv / data / analogsegment.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 <extdef.h>
21
22 #include <cassert>
23 #include <cmath>
24 #include <cstdlib>
25 #include <cstring>
26 #include <memory>
27
28 #include <algorithm>
29
30 #include "analog.hpp"
31 #include "analogsegment.hpp"
32
33 using std::lock_guard;
34 using std::recursive_mutex;
35 using std::make_pair;
36 using std::max;
37 using std::max_element;
38 using std::min;
39 using std::min_element;
40 using std::pair;
41 using std::unique_ptr;
42
43 namespace pv {
44 namespace data {
45
46 const int AnalogSegment::EnvelopeScalePower = 4;
47 const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
48 const float AnalogSegment::LogEnvelopeScaleFactor = logf(EnvelopeScaleFactor);
49 const uint64_t AnalogSegment::EnvelopeDataUnit = 64 * 1024;     // bytes
50
51 AnalogSegment::AnalogSegment(Analog& owner, uint64_t samplerate) :
52         Segment(samplerate, sizeof(float)),
53         owner_(owner),
54         min_value_(0),
55         max_value_(0)
56 {
57         lock_guard<recursive_mutex> lock(mutex_);
58         memset(envelope_levels_, 0, sizeof(envelope_levels_));
59 }
60
61 AnalogSegment::~AnalogSegment()
62 {
63         lock_guard<recursive_mutex> lock(mutex_);
64         for (Envelope &e : envelope_levels_)
65                 free(e.samples);
66 }
67
68 void AnalogSegment::append_interleaved_samples(const float *data,
69         size_t sample_count, size_t stride)
70 {
71         assert(unit_size_ == sizeof(float));
72
73         lock_guard<recursive_mutex> lock(mutex_);
74
75         uint64_t prev_sample_count = sample_count_;
76
77         // Deinterleave the samples and add them
78         unique_ptr<float> deint_data(new float[sample_count]);
79         float *deint_data_ptr = deint_data.get();
80         for (uint32_t i = 0; i < sample_count; i++) {
81                 *deint_data_ptr = (float)(*data);
82                 deint_data_ptr++;
83                 data += stride;
84         }
85
86         append_samples(deint_data.get(), sample_count);
87
88         // Generate the first mip-map from the data
89         append_payload_to_envelope_levels();
90
91         if (sample_count > 1)
92                 owner_.notify_samples_added(this, prev_sample_count + 1,
93                         prev_sample_count + 1 + sample_count);
94         else
95                 owner_.notify_samples_added(this, prev_sample_count + 1,
96                         prev_sample_count + 1);
97 }
98
99 void AnalogSegment::get_samples(int64_t start_sample, int64_t end_sample,
100         float* dest) const
101 {
102         assert(start_sample >= 0);
103         assert(start_sample < (int64_t)sample_count_);
104         assert(end_sample >= 0);
105         assert(end_sample < (int64_t)sample_count_);
106         assert(start_sample <= end_sample);
107         assert(dest != nullptr);
108
109         lock_guard<recursive_mutex> lock(mutex_);
110
111         get_raw_samples(start_sample, (end_sample - start_sample), (uint8_t*)dest);
112 }
113
114 const pair<float, float> AnalogSegment::get_min_max() const
115 {
116         return make_pair(min_value_, max_value_);
117 }
118
119 SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start)
120 {
121         return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
122 }
123
124 void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase)
125 {
126         Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
127 }
128
129 void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it)
130 {
131         Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
132 }
133
134 void AnalogSegment::get_envelope_section(EnvelopeSection &s,
135         uint64_t start, uint64_t end, float min_length) const
136 {
137         assert(end <= get_sample_count());
138         assert(start <= end);
139         assert(min_length > 0);
140
141         lock_guard<recursive_mutex> lock(mutex_);
142
143         const unsigned int min_level = max((int)floorf(logf(min_length) /
144                 LogEnvelopeScaleFactor) - 1, 0);
145         const unsigned int scale_power = (min_level + 1) *
146                 EnvelopeScalePower;
147         start >>= scale_power;
148         end >>= scale_power;
149
150         s.start = start << scale_power;
151         s.scale = 1 << scale_power;
152         s.length = end - start;
153         s.samples = new EnvelopeSample[s.length];
154         memcpy(s.samples, envelope_levels_[min_level].samples + start,
155                 s.length * sizeof(EnvelopeSample));
156 }
157
158 void AnalogSegment::reallocate_envelope(Envelope &e)
159 {
160         const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
161                 EnvelopeDataUnit) * EnvelopeDataUnit;
162         if (new_data_length > e.data_length) {
163                 e.data_length = new_data_length;
164                 e.samples = (EnvelopeSample*)realloc(e.samples,
165                         new_data_length * sizeof(EnvelopeSample));
166         }
167 }
168
169 void AnalogSegment::append_payload_to_envelope_levels()
170 {
171         Envelope &e0 = envelope_levels_[0];
172         uint64_t prev_length;
173         EnvelopeSample *dest_ptr;
174         SegmentRawDataIterator* it;
175
176         // Expand the data buffer to fit the new samples
177         prev_length = e0.length;
178         e0.length = sample_count_ / EnvelopeScaleFactor;
179
180         // Calculate min/max values in case we have too few samples for an envelope
181         if (sample_count_ < EnvelopeScaleFactor) {
182                 it = begin_raw_sample_iteration(0);
183                 for (uint64_t i = 0; i < sample_count_; i++) {
184                         const float sample = *((float*)it->value);
185                         if (sample < min_value_)
186                                 min_value_ = sample;
187                         if (sample > max_value_)
188                                 max_value_ = sample;
189                         continue_raw_sample_iteration(it, 1);
190                 }
191                 end_raw_sample_iteration(it);
192         }
193
194         // Break off if there are no new samples to compute
195         if (e0.length == prev_length)
196                 return;
197
198         reallocate_envelope(e0);
199
200         dest_ptr = e0.samples + prev_length;
201
202         // Iterate through the samples to populate the first level mipmap
203         uint64_t start_sample = prev_length * EnvelopeScaleFactor;
204         uint64_t end_sample = e0.length * EnvelopeScaleFactor;
205
206         it = begin_raw_sample_iteration(start_sample);
207         for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
208                 const float* samples = (float*)it->value;
209
210                 const EnvelopeSample sub_sample = {
211                         *min_element(samples, samples + EnvelopeScaleFactor),
212                         *max_element(samples, samples + EnvelopeScaleFactor),
213                 };
214
215                 if (sub_sample.min < min_value_)
216                         min_value_ = sub_sample.min;
217                 if (sub_sample.max > max_value_)
218                         max_value_ = sub_sample.max;
219
220                 continue_raw_sample_iteration(it, EnvelopeScaleFactor);
221                 *dest_ptr++ = sub_sample;
222         }
223         end_raw_sample_iteration(it);
224
225         // Compute higher level mipmaps
226         for (unsigned int level = 1; level < ScaleStepCount; level++) {
227                 Envelope &e = envelope_levels_[level];
228                 const Envelope &el = envelope_levels_[level - 1];
229
230                 // Expand the data buffer to fit the new samples
231                 prev_length = e.length;
232                 e.length = el.length / EnvelopeScaleFactor;
233
234                 // Break off if there are no more samples to be computed
235                 if (e.length == prev_length)
236                         break;
237
238                 reallocate_envelope(e);
239
240                 // Subsample the lower level
241                 const EnvelopeSample *src_ptr =
242                         el.samples + prev_length * EnvelopeScaleFactor;
243                 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
244
245                 for (dest_ptr = e.samples + prev_length;
246                                 dest_ptr < end_dest_ptr; dest_ptr++) {
247                         const EnvelopeSample *const end_src_ptr =
248                                 src_ptr + EnvelopeScaleFactor;
249
250                         EnvelopeSample sub_sample = *src_ptr++;
251                         while (src_ptr < end_src_ptr) {
252                                 sub_sample.min = min(sub_sample.min, src_ptr->min);;
253                                 sub_sample.max = max(sub_sample.max, src_ptr->max);
254                                 src_ptr++;
255                         }
256
257                         *dest_ptr = sub_sample;
258                 }
259         }
260 }
261
262 } // namespace data
263 } // namespace pv