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