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