]>
Commit | Line | Data |
---|---|---|
ad908057 SA |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net> | |
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 | ||
c0c3e62a | 20 | #include <cstring> |
5a914348 | 21 | #include <forward_list> |
0c5fe73e SA |
22 | #include <limits> |
23 | ||
47747218 SA |
24 | #include <QDebug> |
25 | ||
ad908057 SA |
26 | #include "logic.hpp" |
27 | #include "logicsegment.hpp" | |
28 | #include "decodesignal.hpp" | |
29 | #include "signaldata.hpp" | |
30 | ||
ad908057 | 31 | #include <pv/data/decode/decoder.hpp> |
ecd07c20 | 32 | #include <pv/data/decode/row.hpp> |
d3feec23 | 33 | #include <pv/globalsettings.hpp> |
ad908057 SA |
34 | #include <pv/session.hpp> |
35 | ||
47747218 | 36 | using std::lock_guard; |
ad908057 | 37 | using std::make_shared; |
0c5fe73e | 38 | using std::min; |
5ecf957f | 39 | using std::out_of_range; |
ad908057 | 40 | using std::shared_ptr; |
47747218 | 41 | using std::unique_lock; |
6a26fc44 | 42 | using pv::data::decode::AnnotationClass; |
a82325d1 | 43 | using pv::data::decode::DecodeChannel; |
ad908057 SA |
44 | |
45 | namespace pv { | |
46 | namespace data { | |
47 | ||
47747218 SA |
48 | const double DecodeSignal::DecodeMargin = 1.0; |
49 | const double DecodeSignal::DecodeThreshold = 0.2; | |
3fbddf7f | 50 | const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024; |
ad908057 | 51 | |
ad908057 | 52 | |
47747218 SA |
53 | DecodeSignal::DecodeSignal(pv::Session &session) : |
54 | SignalBase(nullptr, SignalBase::DecodeChannel), | |
55 | session_(session), | |
e91883bb | 56 | srd_session_(nullptr), |
47747218 | 57 | logic_mux_data_invalid_(false), |
e3202557 | 58 | stack_config_changed_(true), |
f6a93932 | 59 | current_segment_id_(0) |
ad908057 | 60 | { |
47747218 SA |
61 | connect(&session_, SIGNAL(capture_state_changed(int)), |
62 | this, SLOT(on_capture_state_changed(int))); | |
ad908057 SA |
63 | } |
64 | ||
47747218 | 65 | DecodeSignal::~DecodeSignal() |
ad908057 | 66 | { |
8ce0e732 | 67 | reset_decode(true); |
ad908057 SA |
68 | } |
69 | ||
47747218 | 70 | const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const |
ecd07c20 | 71 | { |
47747218 | 72 | return stack_; |
ecd07c20 SA |
73 | } |
74 | ||
ed75426f | 75 | void DecodeSignal::stack_decoder(const srd_decoder *decoder, bool restart_decode) |
ad908057 SA |
76 | { |
77 | assert(decoder); | |
762ab7a4 | 78 | |
b9591f3a | 79 | // Set name if this decoder is the first in the list or the name is unchanged |
6a26fc44 SA |
80 | const srd_decoder* prev_dec = stack_.empty() ? nullptr : stack_.back()->get_srd_decoder(); |
81 | const QString prev_dec_name = prev_dec ? QString::fromUtf8(prev_dec->name) : QString(); | |
47747218 | 82 | |
b9591f3a | 83 | if ((stack_.empty()) || ((stack_.size() > 0) && (name() == prev_dec_name))) |
47747218 | 84 | set_name(QString::fromUtf8(decoder->name)); |
132a5c6d | 85 | |
f54e68b0 | 86 | const shared_ptr<Decoder> dec = make_shared<Decoder>(decoder, stack_.size()); |
b9591f3a SA |
87 | stack_.push_back(dec); |
88 | ||
02078aa1 SA |
89 | connect(dec.get(), SIGNAL(annotation_visibility_changed()), |
90 | this, SLOT(on_annotation_visibility_changed())); | |
91 | ||
27a3f09b | 92 | // Include the newly created decode channels in the channel lists |
9f97b357 | 93 | update_channel_list(); |
132a5c6d | 94 | |
e3202557 | 95 | stack_config_changed_ = true; |
762ab7a4 | 96 | auto_assign_signals(dec); |
27a3f09b | 97 | commit_decoder_channels(); |
e77de61f SA |
98 | |
99 | decoder_stacked((void*)dec.get()); | |
100 | ||
ed75426f SA |
101 | if (restart_decode) |
102 | begin_decode(); | |
ad908057 SA |
103 | } |
104 | ||
105 | void DecodeSignal::remove_decoder(int index) | |
106 | { | |
47747218 SA |
107 | assert(index >= 0); |
108 | assert(index < (int)stack_.size()); | |
109 | ||
110 | // Find the decoder in the stack | |
f9a0fd83 SA |
111 | auto iter = stack_.begin() + index; |
112 | assert(iter != stack_.end()); | |
47747218 | 113 | |
f9a0fd83 SA |
114 | shared_ptr<Decoder> dec = *iter; |
115 | ||
116 | decoder_removed(dec.get()); | |
e77de61f | 117 | |
47747218 SA |
118 | // Delete the element |
119 | stack_.erase(iter); | |
120 | ||
121 | // Update channels and decoded data | |
e3202557 | 122 | stack_config_changed_ = true; |
9f97b357 | 123 | update_channel_list(); |
47747218 | 124 | begin_decode(); |
ad908057 SA |
125 | } |
126 | ||
127 | bool DecodeSignal::toggle_decoder_visibility(int index) | |
128 | { | |
47747218 | 129 | auto iter = stack_.cbegin(); |
ad908057 | 130 | for (int i = 0; i < index; i++, iter++) |
47747218 | 131 | assert(iter != stack_.end()); |
ad908057 SA |
132 | |
133 | shared_ptr<Decoder> dec = *iter; | |
134 | ||
135 | // Toggle decoder visibility | |
136 | bool state = false; | |
137 | if (dec) { | |
5d3ca591 SA |
138 | state = !dec->visible(); |
139 | dec->set_visible(state); | |
ad908057 SA |
140 | } |
141 | ||
142 | return state; | |
143 | } | |
144 | ||
8ce0e732 | 145 | void DecodeSignal::reset_decode(bool shutting_down) |
47747218 | 146 | { |
8b24af15 SA |
147 | resume_decode(); // Make sure the decode thread isn't blocked by pausing |
148 | ||
8ce0e732 | 149 | if (stack_config_changed_ || shutting_down) |
e3202557 SA |
150 | stop_srd_session(); |
151 | else | |
152 | terminate_srd_session(); | |
ce0b6a40 | 153 | |
1b56c646 SA |
154 | if (decode_thread_.joinable()) { |
155 | decode_interrupt_ = true; | |
156 | decode_input_cond_.notify_one(); | |
157 | decode_thread_.join(); | |
158 | } | |
159 | ||
160 | if (logic_mux_thread_.joinable()) { | |
161 | logic_mux_interrupt_ = true; | |
162 | logic_mux_cond_.notify_one(); | |
163 | logic_mux_thread_.join(); | |
164 | } | |
165 | ||
8a603e13 | 166 | current_segment_id_ = 0; |
72435789 | 167 | segments_.clear(); |
1b56c646 SA |
168 | |
169 | logic_mux_data_.reset(); | |
170 | logic_mux_data_invalid_ = true; | |
eee3eab9 | 171 | |
403c3e87 | 172 | if (!error_message_.isEmpty()) { |
66b6f41b | 173 | error_message_.clear(); |
fe060a48 SA |
174 | // TODO Emulate noquote() |
175 | qDebug().nospace() << name() << ": Error cleared"; | |
403c3e87 | 176 | } |
72435789 | 177 | |
eee3eab9 | 178 | decode_reset(); |
47747218 SA |
179 | } |
180 | ||
946b52e1 SA |
181 | void DecodeSignal::begin_decode() |
182 | { | |
47747218 SA |
183 | if (decode_thread_.joinable()) { |
184 | decode_interrupt_ = true; | |
185 | decode_input_cond_.notify_one(); | |
186 | decode_thread_.join(); | |
187 | } | |
188 | ||
189 | if (logic_mux_thread_.joinable()) { | |
190 | logic_mux_interrupt_ = true; | |
191 | logic_mux_cond_.notify_one(); | |
192 | logic_mux_thread_.join(); | |
193 | } | |
194 | ||
195 | reset_decode(); | |
196 | ||
27a3f09b | 197 | if (stack_.size() == 0) { |
403c3e87 | 198 | set_error_message(tr("No decoders")); |
27a3f09b SA |
199 | return; |
200 | } | |
201 | ||
202 | assert(channels_.size() > 0); | |
203 | ||
204 | if (get_assigned_signal_count() == 0) { | |
403c3e87 | 205 | set_error_message(tr("There are no channels assigned to this decoder")); |
27a3f09b SA |
206 | return; |
207 | } | |
208 | ||
04627946 SA |
209 | // Make sure that all assigned channels still provide logic data |
210 | // (can happen when a converted signal was assigned but the | |
211 | // conversion removed in the meanwhile) | |
a82325d1 | 212 | for (decode::DecodeChannel& ch : channels_) |
04627946 SA |
213 | if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr)) |
214 | ch.assigned_signal = nullptr; | |
215 | ||
47747218 | 216 | // Check that all decoders have the required channels |
6a26fc44 | 217 | for (const shared_ptr<Decoder>& dec : stack_) |
47747218 | 218 | if (!dec->have_required_channels()) { |
403c3e87 SA |
219 | set_error_message(tr("One or more required channels " |
220 | "have not been specified")); | |
47747218 SA |
221 | return; |
222 | } | |
223 | ||
27a3f09b SA |
224 | // Free the logic data and its segment(s) if it needs to be updated |
225 | if (logic_mux_data_invalid_) | |
226 | logic_mux_data_.reset(); | |
227 | ||
228 | if (!logic_mux_data_) { | |
72435789 | 229 | const uint32_t ch_count = get_assigned_signal_count(); |
ed535cd7 | 230 | logic_mux_unit_size_ = (ch_count + 7) / 8; |
27a3f09b | 231 | logic_mux_data_ = make_shared<Logic>(ch_count); |
27a3f09b | 232 | } |
47747218 | 233 | |
144e72c9 | 234 | if (get_input_segment_count() == 0) |
403c3e87 | 235 | set_error_message(tr("No input data")); |
ed535cd7 | 236 | |
27a3f09b SA |
237 | // Make sure the logic output data is complete and up-to-date |
238 | logic_mux_interrupt_ = false; | |
239 | logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this); | |
240 | ||
241 | // Decode the muxed logic data | |
47747218 SA |
242 | decode_interrupt_ = false; |
243 | decode_thread_ = std::thread(&DecodeSignal::decode_proc, this); | |
946b52e1 SA |
244 | } |
245 | ||
556259d2 SA |
246 | void DecodeSignal::pause_decode() |
247 | { | |
248 | decode_paused_ = true; | |
249 | } | |
250 | ||
251 | void DecodeSignal::resume_decode() | |
252 | { | |
253 | // Manual unlocking is done before notifying, to avoid waking up the | |
254 | // waiting thread only to block again (see notify_one for details) | |
255 | decode_pause_mutex_.unlock(); | |
256 | decode_pause_cond_.notify_one(); | |
257 | decode_paused_ = false; | |
258 | } | |
259 | ||
260 | bool DecodeSignal::is_paused() const | |
261 | { | |
262 | return decode_paused_; | |
263 | } | |
264 | ||
a82325d1 | 265 | const vector<decode::DecodeChannel> DecodeSignal::get_channels() const |
9f97b357 SA |
266 | { |
267 | return channels_; | |
268 | } | |
269 | ||
762ab7a4 | 270 | void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec) |
132a5c6d | 271 | { |
47747218 SA |
272 | bool new_assignment = false; |
273 | ||
db51130d SA |
274 | // Disconnect all input signal notifications so we don't have duplicate connections |
275 | disconnect_input_notifiers(); | |
276 | ||
132a5c6d | 277 | // Try to auto-select channels that don't have signals assigned yet |
a82325d1 | 278 | for (decode::DecodeChannel& ch : channels_) { |
762ab7a4 SA |
279 | // If a decoder is given, auto-assign only its channels |
280 | if (dec && (ch.decoder_ != dec)) | |
281 | continue; | |
282 | ||
132a5c6d SA |
283 | if (ch.assigned_signal) |
284 | continue; | |
285 | ||
c526a8fd SA |
286 | QString ch_name = ch.name.toLower(); |
287 | ch_name = ch_name.replace(QRegExp("[-_.]"), " "); | |
a8030bb0 SA |
288 | |
289 | shared_ptr<data::SignalBase> match; | |
f4ab4b5c | 290 | for (const shared_ptr<data::SignalBase>& s : session_.signalbases()) { |
f9ebf303 SA |
291 | if (!s->enabled()) |
292 | continue; | |
293 | ||
c526a8fd SA |
294 | QString s_name = s->name().toLower(); |
295 | s_name = s_name.replace(QRegExp("[-_.]"), " "); | |
692f6093 SA |
296 | |
297 | if (s->logic_data() && | |
298 | ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) { | |
a8030bb0 SA |
299 | if (!match) |
300 | match = s; | |
301 | else { | |
302 | // Only replace an existing match if it matches more characters | |
303 | int old_unmatched = ch_name.length() - match->name().length(); | |
304 | int new_unmatched = ch_name.length() - s->name().length(); | |
305 | if (abs(new_unmatched) < abs(old_unmatched)) | |
306 | match = s; | |
307 | } | |
47747218 | 308 | } |
692f6093 | 309 | } |
a8030bb0 | 310 | |
8ff61402 SA |
311 | // Prevent using a signal more than once as D1 would match e.g. D1 and D10 |
312 | bool signal_not_already_used = true; | |
313 | for (decode::DecodeChannel& ch : channels_) | |
314 | if (ch.assigned_signal && (ch.assigned_signal == match)) | |
315 | signal_not_already_used = false; | |
316 | ||
317 | if (match && signal_not_already_used) { | |
cf1541a1 | 318 | ch.assigned_signal = match; |
a8030bb0 SA |
319 | new_assignment = true; |
320 | } | |
47747218 SA |
321 | } |
322 | ||
323 | if (new_assignment) { | |
db51130d SA |
324 | // Receive notifications when new sample data is available |
325 | connect_input_notifiers(); | |
326 | ||
47747218 | 327 | logic_mux_data_invalid_ = true; |
e3202557 | 328 | stack_config_changed_ = true; |
27a3f09b | 329 | commit_decoder_channels(); |
47747218 | 330 | channels_updated(); |
132a5c6d SA |
331 | } |
332 | } | |
333 | ||
cf1541a1 | 334 | void DecodeSignal::assign_signal(const uint16_t channel_id, shared_ptr<const SignalBase> signal) |
9f97b357 | 335 | { |
db51130d SA |
336 | // Disconnect all input signal notifications so we don't have duplicate connections |
337 | disconnect_input_notifiers(); | |
338 | ||
a82325d1 | 339 | for (decode::DecodeChannel& ch : channels_) |
47747218 | 340 | if (ch.id == channel_id) { |
9f97b357 | 341 | ch.assigned_signal = signal; |
47747218 SA |
342 | logic_mux_data_invalid_ = true; |
343 | } | |
9f97b357 | 344 | |
db51130d SA |
345 | // Receive notifications when new sample data is available |
346 | connect_input_notifiers(); | |
347 | ||
e3202557 | 348 | stack_config_changed_ = true; |
27a3f09b | 349 | commit_decoder_channels(); |
9f97b357 | 350 | channels_updated(); |
47747218 | 351 | begin_decode(); |
9f97b357 SA |
352 | } |
353 | ||
27a3f09b SA |
354 | int DecodeSignal::get_assigned_signal_count() const |
355 | { | |
356 | // Count all channels that have a signal assigned to them | |
357 | return count_if(channels_.begin(), channels_.end(), | |
cf1541a1 | 358 | [](decode::DecodeChannel ch) { return ch.assigned_signal.get(); }); |
27a3f09b SA |
359 | } |
360 | ||
9f97b357 SA |
361 | void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state) |
362 | { | |
a82325d1 | 363 | for (decode::DecodeChannel& ch : channels_) |
9f97b357 SA |
364 | if (ch.id == channel_id) |
365 | ch.initial_pin_state = init_state; | |
366 | ||
e3202557 | 367 | stack_config_changed_ = true; |
9f97b357 | 368 | channels_updated(); |
47747218 | 369 | begin_decode(); |
9f97b357 SA |
370 | } |
371 | ||
85125b0f | 372 | double DecodeSignal::get_samplerate() const |
ff83d980 | 373 | { |
72435789 SA |
374 | double result = 0; |
375 | ||
376 | // TODO For now, we simply return the first samplerate that we have | |
8a603e13 SA |
377 | if (segments_.size() > 0) |
378 | result = segments_.front().samplerate; | |
72435789 SA |
379 | |
380 | return result; | |
ff83d980 SA |
381 | } |
382 | ||
72435789 | 383 | const pv::util::Timestamp DecodeSignal::start_time() const |
ff83d980 | 384 | { |
72435789 SA |
385 | pv::util::Timestamp result; |
386 | ||
387 | // TODO For now, we simply return the first start time that we have | |
8a603e13 SA |
388 | if (segments_.size() > 0) |
389 | result = segments_.front().start_time; | |
72435789 SA |
390 | |
391 | return result; | |
ff83d980 SA |
392 | } |
393 | ||
5ecf957f | 394 | int64_t DecodeSignal::get_working_sample_count(uint32_t segment_id) const |
0c5fe73e SA |
395 | { |
396 | // The working sample count is the highest sample number for | |
ed535cd7 SA |
397 | // which all used signals have data available, so go through all |
398 | // channels and use the lowest overall sample count of the segment | |
0c5fe73e | 399 | |
0c5fe73e SA |
400 | int64_t count = std::numeric_limits<int64_t>::max(); |
401 | bool no_signals_assigned = true; | |
402 | ||
a82325d1 | 403 | for (const decode::DecodeChannel& ch : channels_) |
0c5fe73e | 404 | if (ch.assigned_signal) { |
1774d669 SA |
405 | if (!ch.assigned_signal->logic_data()) |
406 | return 0; | |
407 | ||
0c5fe73e SA |
408 | no_signals_assigned = false; |
409 | ||
410 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
1774d669 | 411 | if (logic_data->logic_segments().empty()) |
0c5fe73e SA |
412 | return 0; |
413 | ||
1774d669 | 414 | if (segment_id >= logic_data->logic_segments().size()) |
5ecf957f | 415 | return 0; |
1774d669 | 416 | |
cf1541a1 SA |
417 | const shared_ptr<const LogicSegment> segment = logic_data->logic_segments()[segment_id]->get_shared_ptr(); |
418 | if (segment) | |
419 | count = min(count, (int64_t)segment->get_sample_count()); | |
0c5fe73e SA |
420 | } |
421 | ||
422 | return (no_signals_assigned ? 0 : count); | |
423 | } | |
424 | ||
b82908ab SA |
425 | int64_t DecodeSignal::get_decoded_sample_count(uint32_t segment_id, |
426 | bool include_processing) const | |
ff83d980 | 427 | { |
47747218 | 428 | lock_guard<mutex> decode_lock(output_mutex_); |
5ecf957f SA |
429 | |
430 | int64_t result = 0; | |
431 | ||
c8a6db0e SA |
432 | if (segment_id >= segments_.size()) |
433 | return result; | |
434 | ||
435 | if (include_processing) | |
436 | result = segments_[segment_id].samples_decoded_incl; | |
437 | else | |
438 | result = segments_[segment_id].samples_decoded_excl; | |
5ecf957f SA |
439 | |
440 | return result; | |
ff83d980 SA |
441 | } |
442 | ||
6a26fc44 | 443 | vector<Row*> DecodeSignal::get_rows(bool visible_only) |
ecd07c20 | 444 | { |
6a26fc44 | 445 | vector<Row*> rows; |
47747218 | 446 | |
6a26fc44 | 447 | for (const shared_ptr<Decoder>& dec : stack_) { |
47747218 | 448 | assert(dec); |
5d3ca591 | 449 | if (visible_only && !dec->visible()) |
cbb2e4da SA |
450 | continue; |
451 | ||
6a26fc44 SA |
452 | for (Row* row : dec->get_rows()) |
453 | rows.push_back(row); | |
454 | } | |
47747218 | 455 | |
6a26fc44 SA |
456 | return rows; |
457 | } | |
47747218 | 458 | |
6a26fc44 SA |
459 | vector<const Row*> DecodeSignal::get_rows(bool visible_only) const |
460 | { | |
461 | vector<const Row*> rows; | |
462 | ||
463 | for (const shared_ptr<Decoder>& dec : stack_) { | |
464 | assert(dec); | |
5d3ca591 | 465 | if (visible_only && !dec->visible()) |
6a26fc44 SA |
466 | continue; |
467 | ||
468 | for (const Row* row : dec->get_rows()) | |
469 | rows.push_back(row); | |
47747218 SA |
470 | } |
471 | ||
472 | return rows; | |
ecd07c20 SA |
473 | } |
474 | ||
6a26fc44 | 475 | uint64_t DecodeSignal::get_annotation_count(const Row* row, uint32_t segment_id) const |
f994f496 SA |
476 | { |
477 | if (segment_id >= segments_.size()) | |
478 | return 0; | |
479 | ||
6a26fc44 | 480 | const DecodeSegment* segment = &(segments_.at(segment_id)); |
f994f496 | 481 | |
6a26fc44 | 482 | auto row_it = segment->annotation_rows.find(row); |
f994f496 | 483 | |
6a26fc44 | 484 | const RowData* rd; |
5a9146ac | 485 | if (row_it == segment->annotation_rows.end()) |
6a26fc44 | 486 | return 0; |
5a9146ac | 487 | else |
6a26fc44 SA |
488 | rd = &(row_it->second); |
489 | ||
490 | return rd->get_annotation_count(); | |
f994f496 SA |
491 | } |
492 | ||
462941e2 | 493 | void DecodeSignal::get_annotation_subset(deque<const Annotation*> &dest, |
6a26fc44 | 494 | const Row* row, uint32_t segment_id, uint64_t start_sample, |
ecd07c20 SA |
495 | uint64_t end_sample) const |
496 | { | |
47747218 SA |
497 | lock_guard<mutex> lock(output_mutex_); |
498 | ||
c8a6db0e SA |
499 | if (segment_id >= segments_.size()) |
500 | return; | |
72435789 | 501 | |
6a26fc44 SA |
502 | const DecodeSegment* segment = &(segments_.at(segment_id)); |
503 | ||
504 | auto row_it = segment->annotation_rows.find(row); | |
c8a6db0e | 505 | |
6a26fc44 | 506 | const RowData* rd; |
5a9146ac | 507 | if (row_it == segment->annotation_rows.end()) |
6a26fc44 | 508 | return; |
5a9146ac | 509 | else |
6a26fc44 SA |
510 | rd = &(row_it->second); |
511 | ||
512 | rd->get_annotation_subset(dest, start_sample, end_sample); | |
47747218 SA |
513 | } |
514 | ||
462941e2 | 515 | void DecodeSignal::get_annotation_subset(deque<const Annotation*> &dest, |
5a914348 SA |
516 | uint32_t segment_id, uint64_t start_sample, uint64_t end_sample) const |
517 | { | |
462941e2 SA |
518 | for (const Row* row : get_rows()) |
519 | get_annotation_subset(dest, row, segment_id, start_sample, end_sample); | |
5a914348 SA |
520 | } |
521 | ||
e77de61f | 522 | uint32_t DecodeSignal::get_binary_data_chunk_count(uint32_t segment_id, |
ac9494ef | 523 | const Decoder* dec, uint32_t bin_class_id) const |
c0c3e62a | 524 | { |
1774d669 | 525 | if ((segments_.size() == 0) || (segment_id >= segments_.size())) |
c8a6db0e SA |
526 | return 0; |
527 | ||
1774d669 | 528 | const DecodeSegment *segment = &(segments_[segment_id]); |
e77de61f | 529 | |
1774d669 SA |
530 | for (const DecodeBinaryClass& bc : segment->binary_classes) |
531 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) | |
532 | return bc.chunks.size(); | |
c0c3e62a | 533 | |
e77de61f | 534 | return 0; |
c0c3e62a SA |
535 | } |
536 | ||
e77de61f | 537 | void DecodeSignal::get_binary_data_chunk(uint32_t segment_id, |
ac9494ef | 538 | const Decoder* dec, uint32_t bin_class_id, uint32_t chunk_id, |
c0c3e62a SA |
539 | const vector<uint8_t> **dest, uint64_t *size) |
540 | { | |
1774d669 SA |
541 | if (segment_id >= segments_.size()) |
542 | return; | |
e77de61f | 543 | |
1774d669 SA |
544 | const DecodeSegment *segment = &(segments_[segment_id]); |
545 | ||
546 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
547 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) { | |
548 | if (dest) *dest = &(bc.chunks.at(chunk_id).data); | |
549 | if (size) *size = bc.chunks.at(chunk_id).data.size(); | |
550 | return; | |
551 | } | |
c0c3e62a SA |
552 | } |
553 | ||
13b726cd | 554 | void DecodeSignal::get_merged_binary_data_chunks_by_sample(uint32_t segment_id, |
ac9494ef | 555 | const Decoder* dec, uint32_t bin_class_id, uint64_t start_sample, |
e77de61f | 556 | uint64_t end_sample, vector<uint8_t> *dest) const |
c0c3e62a SA |
557 | { |
558 | assert(dest != nullptr); | |
559 | ||
1774d669 SA |
560 | if (segment_id >= segments_.size()) |
561 | return; | |
562 | ||
563 | const DecodeSegment *segment = &(segments_[segment_id]); | |
564 | ||
565 | const DecodeBinaryClass* bin_class = nullptr; | |
566 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
567 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) | |
568 | bin_class = &bc; | |
569 | ||
570 | // Determine overall size before copying to resize dest vector only once | |
571 | uint64_t size = 0; | |
572 | uint64_t matches = 0; | |
573 | for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) | |
574 | if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) { | |
575 | size += chunk.data.size(); | |
576 | matches++; | |
577 | } | |
578 | dest->resize(size); | |
579 | ||
580 | uint64_t offset = 0; | |
581 | uint64_t matches2 = 0; | |
582 | for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) | |
583 | if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) { | |
584 | memcpy(dest->data() + offset, chunk.data.data(), chunk.data.size()); | |
585 | offset += chunk.data.size(); | |
586 | matches2++; | |
587 | ||
588 | // Make sure we don't overwrite memory if the array grew in the meanwhile | |
589 | if (matches2 == matches) | |
590 | break; | |
591 | } | |
c0c3e62a SA |
592 | } |
593 | ||
13b726cd | 594 | void DecodeSignal::get_merged_binary_data_chunks_by_offset(uint32_t segment_id, |
6a26fc44 SA |
595 | const Decoder* dec, uint32_t bin_class_id, uint64_t start, uint64_t end, |
596 | vector<uint8_t> *dest) const | |
13b726cd SA |
597 | { |
598 | assert(dest != nullptr); | |
599 | ||
1774d669 SA |
600 | if (segment_id >= segments_.size()) |
601 | return; | |
13b726cd | 602 | |
1774d669 SA |
603 | const DecodeSegment *segment = &(segments_[segment_id]); |
604 | ||
605 | const DecodeBinaryClass* bin_class = nullptr; | |
606 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
607 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) | |
608 | bin_class = &bc; | |
609 | ||
610 | // Determine overall size before copying to resize dest vector only once | |
611 | uint64_t size = 0; | |
612 | uint64_t offset = 0; | |
613 | for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) { | |
614 | if (offset >= start) | |
615 | size += chunk.data.size(); | |
616 | offset += chunk.data.size(); | |
617 | if (offset >= end) | |
618 | break; | |
619 | } | |
620 | dest->resize(size); | |
621 | ||
622 | offset = 0; | |
623 | uint64_t dest_offset = 0; | |
624 | for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) { | |
625 | if (offset >= start) { | |
626 | memcpy(dest->data() + dest_offset, chunk.data.data(), chunk.data.size()); | |
627 | dest_offset += chunk.data.size(); | |
13b726cd | 628 | } |
1774d669 SA |
629 | offset += chunk.data.size(); |
630 | if (offset >= end) | |
631 | break; | |
13b726cd SA |
632 | } |
633 | } | |
634 | ||
628b45cc | 635 | const DecodeBinaryClass* DecodeSignal::get_binary_data_class(uint32_t segment_id, |
6a26fc44 | 636 | const Decoder* dec, uint32_t bin_class_id) const |
628b45cc | 637 | { |
1774d669 SA |
638 | if (segment_id >= segments_.size()) |
639 | return nullptr; | |
628b45cc | 640 | |
1774d669 SA |
641 | const DecodeSegment *segment = &(segments_[segment_id]); |
642 | ||
643 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
644 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) | |
645 | return &bc; | |
628b45cc SA |
646 | |
647 | return nullptr; | |
648 | } | |
c0c3e62a | 649 | |
f54e68b0 SA |
650 | const deque<const Annotation*>* DecodeSignal::get_all_annotations_by_segment( |
651 | uint32_t segment_id) const | |
652 | { | |
1774d669 SA |
653 | if (segment_id >= segments_.size()) |
654 | return nullptr; | |
f54e68b0 | 655 | |
1774d669 SA |
656 | const DecodeSegment *segment = &(segments_[segment_id]); |
657 | ||
658 | return &(segment->all_annotations); | |
f54e68b0 SA |
659 | } |
660 | ||
47747218 SA |
661 | void DecodeSignal::save_settings(QSettings &settings) const |
662 | { | |
663 | SignalBase::save_settings(settings); | |
664 | ||
c8e60bdf SA |
665 | settings.setValue("decoders", (int)(stack_.size())); |
666 | ||
8b5ea5a1 | 667 | // Save decoder stack |
c8e60bdf | 668 | int decoder_idx = 0; |
6a26fc44 | 669 | for (const shared_ptr<Decoder>& decoder : stack_) { |
c8e60bdf SA |
670 | settings.beginGroup("decoder" + QString::number(decoder_idx++)); |
671 | ||
6a26fc44 | 672 | settings.setValue("id", decoder->get_srd_decoder()->id); |
5d3ca591 | 673 | settings.setValue("visible", decoder->visible()); |
c8e60bdf | 674 | |
d3feec23 SA |
675 | // Save decoder options |
676 | const map<string, GVariant*>& options = decoder->options(); | |
677 | ||
678 | settings.setValue("options", (int)options.size()); | |
679 | ||
6a26fc44 | 680 | // Note: Decoder::options() returns only the options |
d3feec23 SA |
681 | // that differ from the default. See binding::Decoder::getter() |
682 | int i = 0; | |
f4ab4b5c | 683 | for (auto& option : options) { |
d3feec23 SA |
684 | settings.beginGroup("option" + QString::number(i)); |
685 | settings.setValue("name", QString::fromStdString(option.first)); | |
686 | GlobalSettings::store_gvariant(settings, option.second); | |
687 | settings.endGroup(); | |
688 | i++; | |
689 | } | |
690 | ||
e4cb8bcf SA |
691 | // Save row properties |
692 | i = 0; | |
693 | for (const Row* row : decoder->get_rows()) { | |
694 | settings.beginGroup("row" + QString::number(i)); | |
695 | settings.setValue("visible", row->visible()); | |
696 | settings.endGroup(); | |
697 | i++; | |
698 | } | |
699 | ||
700 | // Save class properties | |
701 | i = 0; | |
702 | for (const AnnotationClass* ann_class : decoder->ann_classes()) { | |
703 | settings.beginGroup("ann_class" + QString::number(i)); | |
02078aa1 | 704 | settings.setValue("visible", ann_class->visible()); |
e4cb8bcf SA |
705 | settings.endGroup(); |
706 | i++; | |
707 | } | |
708 | ||
c8e60bdf SA |
709 | settings.endGroup(); |
710 | } | |
711 | ||
8b5ea5a1 SA |
712 | // Save channel mapping |
713 | settings.setValue("channels", (int)channels_.size()); | |
714 | ||
715 | for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) { | |
716 | auto channel = find_if(channels_.begin(), channels_.end(), | |
a82325d1 | 717 | [&](decode::DecodeChannel ch) { return ch.id == channel_id; }); |
8b5ea5a1 SA |
718 | |
719 | if (channel == channels_.end()) { | |
720 | qDebug() << "ERROR: Gap in channel index:" << channel_id; | |
721 | continue; | |
722 | } | |
723 | ||
724 | settings.beginGroup("channel" + QString::number(channel_id)); | |
725 | ||
726 | settings.setValue("name", channel->name); // Useful for debugging | |
727 | settings.setValue("initial_pin_state", channel->initial_pin_state); | |
728 | ||
729 | if (channel->assigned_signal) | |
730 | settings.setValue("assigned_signal_name", channel->assigned_signal->name()); | |
731 | ||
732 | settings.endGroup(); | |
733 | } | |
47747218 SA |
734 | } |
735 | ||
736 | void DecodeSignal::restore_settings(QSettings &settings) | |
737 | { | |
738 | SignalBase::restore_settings(settings); | |
739 | ||
8b5ea5a1 | 740 | // Restore decoder stack |
c8e60bdf SA |
741 | GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list()); |
742 | ||
743 | int decoders = settings.value("decoders").toInt(); | |
744 | ||
745 | for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) { | |
746 | settings.beginGroup("decoder" + QString::number(decoder_idx)); | |
747 | ||
748 | QString id = settings.value("id").toString(); | |
749 | ||
750 | for (GSList *entry = dec_list; entry; entry = entry->next) { | |
751 | const srd_decoder *dec = (srd_decoder*)entry->data; | |
752 | if (!dec) | |
753 | continue; | |
754 | ||
755 | if (QString::fromUtf8(dec->id) == id) { | |
f54e68b0 | 756 | shared_ptr<Decoder> decoder = make_shared<Decoder>(dec, stack_.size()); |
d3feec23 | 757 | |
02078aa1 SA |
758 | connect(decoder.get(), SIGNAL(annotation_visibility_changed()), |
759 | this, SLOT(on_annotation_visibility_changed())); | |
760 | ||
d3feec23 | 761 | stack_.push_back(decoder); |
5d3ca591 | 762 | decoder->set_visible(settings.value("visible", true).toBool()); |
d3feec23 SA |
763 | |
764 | // Restore decoder options that differ from their default | |
765 | int options = settings.value("options").toInt(); | |
766 | ||
767 | for (int i = 0; i < options; i++) { | |
768 | settings.beginGroup("option" + QString::number(i)); | |
769 | QString name = settings.value("name").toString(); | |
770 | GVariant *value = GlobalSettings::restore_gvariant(settings); | |
771 | decoder->set_option(name.toUtf8(), value); | |
772 | settings.endGroup(); | |
773 | } | |
c8e60bdf SA |
774 | |
775 | // Include the newly created decode channels in the channel lists | |
776 | update_channel_list(); | |
e4cb8bcf SA |
777 | |
778 | // Restore row properties | |
779 | int i = 0; | |
780 | for (Row* row : decoder->get_rows()) { | |
781 | settings.beginGroup("row" + QString::number(i)); | |
782 | row->set_visible(settings.value("visible", true).toBool()); | |
783 | settings.endGroup(); | |
784 | i++; | |
785 | } | |
786 | ||
787 | // Restore class properties | |
788 | i = 0; | |
789 | for (AnnotationClass* ann_class : decoder->ann_classes()) { | |
790 | settings.beginGroup("ann_class" + QString::number(i)); | |
02078aa1 | 791 | ann_class->set_visible(settings.value("visible", true).toBool()); |
e4cb8bcf SA |
792 | settings.endGroup(); |
793 | i++; | |
794 | } | |
795 | ||
c8e60bdf SA |
796 | break; |
797 | } | |
798 | } | |
799 | ||
800 | settings.endGroup(); | |
4ca06ec3 | 801 | channels_updated(); |
c8e60bdf SA |
802 | } |
803 | ||
8b5ea5a1 SA |
804 | // Restore channel mapping |
805 | unsigned int channels = settings.value("channels").toInt(); | |
806 | ||
56c4de9a | 807 | const vector< shared_ptr<data::SignalBase> > signalbases = |
8b5ea5a1 SA |
808 | session_.signalbases(); |
809 | ||
810 | for (unsigned int channel_id = 0; channel_id < channels; channel_id++) { | |
811 | auto channel = find_if(channels_.begin(), channels_.end(), | |
a82325d1 | 812 | [&](decode::DecodeChannel ch) { return ch.id == channel_id; }); |
8b5ea5a1 SA |
813 | |
814 | if (channel == channels_.end()) { | |
815 | qDebug() << "ERROR: Non-existant channel index:" << channel_id; | |
816 | continue; | |
817 | } | |
818 | ||
819 | settings.beginGroup("channel" + QString::number(channel_id)); | |
820 | ||
821 | QString assigned_signal_name = settings.value("assigned_signal_name").toString(); | |
822 | ||
f4ab4b5c | 823 | for (const shared_ptr<data::SignalBase>& signal : signalbases) |
2cc02460 | 824 | if ((signal->name() == assigned_signal_name) && (signal->type() != SignalBase::DecodeChannel)) |
cf1541a1 | 825 | channel->assigned_signal = signal; |
8b5ea5a1 SA |
826 | |
827 | channel->initial_pin_state = settings.value("initial_pin_state").toInt(); | |
828 | ||
829 | settings.endGroup(); | |
830 | } | |
831 | ||
d023660f SA |
832 | connect_input_notifiers(); |
833 | ||
aa71890d | 834 | // Update the internal structures |
e3202557 | 835 | stack_config_changed_ = true; |
aa71890d SA |
836 | update_channel_list(); |
837 | commit_decoder_channels(); | |
838 | ||
8b5ea5a1 | 839 | begin_decode(); |
47747218 SA |
840 | } |
841 | ||
144e72c9 SA |
842 | bool DecodeSignal::all_input_segments_complete(uint32_t segment_id) const |
843 | { | |
844 | bool all_complete = true; | |
845 | ||
846 | for (const decode::DecodeChannel& ch : channels_) | |
847 | if (ch.assigned_signal) { | |
848 | if (!ch.assigned_signal->logic_data()) | |
849 | continue; | |
850 | ||
851 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
852 | if (logic_data->logic_segments().empty()) | |
853 | return false; | |
854 | ||
855 | if (segment_id >= logic_data->logic_segments().size()) | |
856 | return false; | |
857 | ||
cf1541a1 SA |
858 | const shared_ptr<const LogicSegment> segment = logic_data->logic_segments()[segment_id]->get_shared_ptr(); |
859 | if (segment && !segment->is_complete()) | |
144e72c9 SA |
860 | all_complete = false; |
861 | } | |
862 | ||
863 | return all_complete; | |
864 | } | |
865 | ||
8a603e13 SA |
866 | uint32_t DecodeSignal::get_input_segment_count() const |
867 | { | |
868 | uint64_t count = std::numeric_limits<uint64_t>::max(); | |
869 | bool no_signals_assigned = true; | |
870 | ||
a82325d1 | 871 | for (const decode::DecodeChannel& ch : channels_) |
8a603e13 SA |
872 | if (ch.assigned_signal) { |
873 | no_signals_assigned = false; | |
874 | ||
875 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
876 | if (!logic_data || logic_data->logic_segments().empty()) | |
877 | return 0; | |
878 | ||
879 | // Find the min value of all segment counts | |
880 | if ((uint64_t)(logic_data->logic_segments().size()) < count) | |
881 | count = logic_data->logic_segments().size(); | |
882 | } | |
883 | ||
884 | return (no_signals_assigned ? 0 : count); | |
885 | } | |
886 | ||
2a12830c | 887 | double DecodeSignal::get_input_samplerate(uint32_t segment_id) const |
8a603e13 SA |
888 | { |
889 | double samplerate = 0; | |
890 | ||
a82325d1 | 891 | for (const decode::DecodeChannel& ch : channels_) |
8a603e13 SA |
892 | if (ch.assigned_signal) { |
893 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
894 | if (!logic_data || logic_data->logic_segments().empty()) | |
895 | continue; | |
896 | ||
897 | try { | |
cf1541a1 SA |
898 | const shared_ptr<const LogicSegment> segment = |
899 | logic_data->logic_segments().at(segment_id)->get_shared_ptr(); | |
900 | if (segment) | |
901 | samplerate = segment->samplerate(); | |
30677c13 | 902 | } catch (out_of_range&) { |
8a603e13 SA |
903 | // Do nothing |
904 | } | |
905 | break; | |
906 | } | |
907 | ||
908 | return samplerate; | |
909 | } | |
910 | ||
6a26fc44 SA |
911 | Decoder* DecodeSignal::get_decoder_by_instance(const srd_decoder *const srd_dec) |
912 | { | |
913 | for (shared_ptr<Decoder>& d : stack_) | |
914 | if (d->get_srd_decoder() == srd_dec) | |
915 | return d.get(); | |
916 | ||
917 | return nullptr; | |
918 | } | |
919 | ||
9f97b357 SA |
920 | void DecodeSignal::update_channel_list() |
921 | { | |
a82325d1 | 922 | vector<decode::DecodeChannel> prev_channels = channels_; |
9f97b357 SA |
923 | channels_.clear(); |
924 | ||
925 | uint16_t id = 0; | |
926 | ||
927 | // Copy existing entries, create new as needed | |
f4ab4b5c | 928 | for (shared_ptr<Decoder>& decoder : stack_) { |
6a26fc44 | 929 | const srd_decoder* srd_dec = decoder->get_srd_decoder(); |
9f97b357 SA |
930 | const GSList *l; |
931 | ||
932 | // Mandatory channels | |
6a26fc44 | 933 | for (l = srd_dec->channels; l; l = l->next) { |
9f97b357 SA |
934 | const struct srd_channel *const pdch = (struct srd_channel *)l->data; |
935 | bool ch_added = false; | |
936 | ||
937 | // Copy but update ID if this channel was in the list before | |
a82325d1 | 938 | for (decode::DecodeChannel& ch : prev_channels) |
9f97b357 SA |
939 | if (ch.pdch_ == pdch) { |
940 | ch.id = id++; | |
941 | channels_.push_back(ch); | |
942 | ch_added = true; | |
943 | break; | |
944 | } | |
945 | ||
946 | if (!ch_added) { | |
947 | // Create new entry without a mapped signal | |
a82325d1 | 948 | decode::DecodeChannel ch = {id++, 0, false, nullptr, |
9f97b357 SA |
949 | QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc), |
950 | SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch}; | |
951 | channels_.push_back(ch); | |
952 | } | |
953 | } | |
954 | ||
955 | // Optional channels | |
6a26fc44 | 956 | for (l = srd_dec->opt_channels; l; l = l->next) { |
9f97b357 SA |
957 | const struct srd_channel *const pdch = (struct srd_channel *)l->data; |
958 | bool ch_added = false; | |
959 | ||
960 | // Copy but update ID if this channel was in the list before | |
a82325d1 | 961 | for (decode::DecodeChannel& ch : prev_channels) |
9f97b357 SA |
962 | if (ch.pdch_ == pdch) { |
963 | ch.id = id++; | |
964 | channels_.push_back(ch); | |
965 | ch_added = true; | |
966 | break; | |
967 | } | |
968 | ||
969 | if (!ch_added) { | |
970 | // Create new entry without a mapped signal | |
a82325d1 | 971 | decode::DecodeChannel ch = {id++, 0, true, nullptr, |
9f97b357 SA |
972 | QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc), |
973 | SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch}; | |
974 | channels_.push_back(ch); | |
975 | } | |
976 | } | |
977 | } | |
978 | ||
47747218 SA |
979 | // Invalidate the logic output data if the channel assignment changed |
980 | if (prev_channels.size() != channels_.size()) { | |
981 | // The number of channels changed, there's definitely a difference | |
982 | logic_mux_data_invalid_ = true; | |
983 | } else { | |
984 | // Same number but assignment may still differ, so compare all channels | |
985 | for (size_t i = 0; i < channels_.size(); i++) { | |
a82325d1 SA |
986 | const decode::DecodeChannel& p_ch = prev_channels[i]; |
987 | const decode::DecodeChannel& ch = channels_[i]; | |
47747218 SA |
988 | |
989 | if ((p_ch.pdch_ != ch.pdch_) || | |
990 | (p_ch.assigned_signal != ch.assigned_signal)) { | |
991 | logic_mux_data_invalid_ = true; | |
992 | break; | |
993 | } | |
994 | } | |
995 | ||
996 | } | |
997 | ||
9f97b357 SA |
998 | channels_updated(); |
999 | } | |
1000 | ||
27a3f09b | 1001 | void DecodeSignal::commit_decoder_channels() |
ad908057 | 1002 | { |
27a3f09b | 1003 | // Submit channel list to every decoder, containing only the relevant channels |
6a26fc44 | 1004 | for (shared_ptr<Decoder> dec : stack_) { |
a82325d1 | 1005 | vector<decode::DecodeChannel*> channel_list; |
27a3f09b | 1006 | |
a82325d1 | 1007 | for (decode::DecodeChannel& ch : channels_) |
27a3f09b SA |
1008 | if (ch.decoder_ == dec) |
1009 | channel_list.push_back(&ch); | |
47747218 | 1010 | |
27a3f09b SA |
1011 | dec->set_channels(channel_list); |
1012 | } | |
aa71890d SA |
1013 | |
1014 | // Channel bit IDs must be in sync with the channel's apperance in channels_ | |
1015 | int id = 0; | |
a82325d1 | 1016 | for (decode::DecodeChannel& ch : channels_) |
aa71890d SA |
1017 | if (ch.assigned_signal) |
1018 | ch.bit_id = id++; | |
47747218 SA |
1019 | } |
1020 | ||
ed535cd7 | 1021 | void DecodeSignal::mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end) |
47747218 | 1022 | { |
27a3f09b SA |
1023 | // Enforce end to be greater than start |
1024 | if (end <= start) | |
1025 | return; | |
1026 | ||
ed535cd7 | 1027 | // Fetch the channel segments and their data |
cf1541a1 | 1028 | vector<shared_ptr<const LogicSegment> > segments; |
27a3f09b SA |
1029 | vector<const uint8_t*> signal_data; |
1030 | vector<uint8_t> signal_in_bytepos; | |
1031 | vector<uint8_t> signal_in_bitpos; | |
1032 | ||
a82325d1 | 1033 | for (decode::DecodeChannel& ch : channels_) |
27a3f09b SA |
1034 | if (ch.assigned_signal) { |
1035 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
ed535cd7 | 1036 | |
cf1541a1 | 1037 | shared_ptr<const LogicSegment> segment; |
f5a26d5e | 1038 | if (segment_id < logic_data->logic_segments().size()) { |
cf1541a1 | 1039 | segment = logic_data->logic_segments().at(segment_id)->get_shared_ptr(); |
f5a26d5e | 1040 | } else { |
ed535cd7 SA |
1041 | qDebug() << "Muxer error for" << name() << ":" << ch.assigned_signal->name() \ |
1042 | << "has no logic segment" << segment_id; | |
cf1541a1 | 1043 | logic_mux_interrupt_ = true; |
ed535cd7 SA |
1044 | return; |
1045 | } | |
cf1541a1 | 1046 | |
f5a26d5e SA |
1047 | if (!segment) |
1048 | return; | |
cf1541a1 | 1049 | |
27a3f09b | 1050 | segments.push_back(segment); |
b82243f7 SA |
1051 | |
1052 | uint8_t* data = new uint8_t[(end - start) * segment->unit_size()]; | |
1053 | segment->get_samples(start, end, data); | |
1054 | signal_data.push_back(data); | |
27a3f09b SA |
1055 | |
1056 | const int bitpos = ch.assigned_signal->logic_bit_index(); | |
1057 | signal_in_bytepos.push_back(bitpos / 8); | |
1058 | signal_in_bitpos.push_back(bitpos % 8); | |
1059 | } | |
1060 | ||
ed535cd7 SA |
1061 | shared_ptr<LogicSegment> output_segment; |
1062 | try { | |
1063 | output_segment = logic_mux_data_->logic_segments().at(segment_id); | |
30677c13 | 1064 | } catch (out_of_range&) { |
ed535cd7 SA |
1065 | qDebug() << "Muxer error for" << name() << ": no logic mux segment" \ |
1066 | << segment_id << "in mux_logic_samples(), mux segments size is" \ | |
1067 | << logic_mux_data_->logic_segments().size(); | |
cf1541a1 | 1068 | logic_mux_interrupt_ = true; |
ed535cd7 SA |
1069 | return; |
1070 | } | |
1071 | ||
27a3f09b | 1072 | // Perform the muxing of signal data into the output data |
8a603e13 | 1073 | uint8_t* output = new uint8_t[(end - start) * output_segment->unit_size()]; |
27a3f09b SA |
1074 | unsigned int signal_count = signal_data.size(); |
1075 | ||
3b5282a2 SA |
1076 | for (int64_t sample_cnt = 0; !logic_mux_interrupt_ && (sample_cnt < (end - start)); |
1077 | sample_cnt++) { | |
1078 | ||
27a3f09b SA |
1079 | int bitpos = 0; |
1080 | uint8_t bytepos = 0; | |
1081 | ||
8a603e13 SA |
1082 | const int out_sample_pos = sample_cnt * output_segment->unit_size(); |
1083 | for (unsigned int i = 0; i < output_segment->unit_size(); i++) | |
27a3f09b | 1084 | output[out_sample_pos + i] = 0; |
47747218 | 1085 | |
27a3f09b SA |
1086 | for (unsigned int i = 0; i < signal_count; i++) { |
1087 | const int in_sample_pos = sample_cnt * segments[i]->unit_size(); | |
1088 | const uint8_t in_sample = 1 & | |
1089 | ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i])); | |
47747218 | 1090 | |
27a3f09b SA |
1091 | const uint8_t out_sample = output[out_sample_pos + bytepos]; |
1092 | ||
1093 | output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos); | |
1094 | ||
1095 | bitpos++; | |
1096 | if (bitpos > 7) { | |
1097 | bitpos = 0; | |
1098 | bytepos++; | |
1099 | } | |
1100 | } | |
47747218 SA |
1101 | } |
1102 | ||
8a603e13 | 1103 | output_segment->append_payload(output, (end - start) * output_segment->unit_size()); |
27a3f09b SA |
1104 | delete[] output; |
1105 | ||
1106 | for (const uint8_t* data : signal_data) | |
1107 | delete[] data; | |
1108 | } | |
1109 | ||
1110 | void DecodeSignal::logic_mux_proc() | |
1111 | { | |
144e72c9 SA |
1112 | uint32_t input_segment_count; |
1113 | do { | |
1114 | input_segment_count = get_input_segment_count(); | |
1115 | if (input_segment_count == 0) { | |
1116 | // Wait for input data | |
1117 | unique_lock<mutex> logic_mux_lock(logic_mux_mutex_); | |
1118 | logic_mux_cond_.wait(logic_mux_lock); | |
1119 | } | |
1120 | } while ((!logic_mux_interrupt_) && (input_segment_count == 0)); | |
1121 | ||
1122 | if (logic_mux_interrupt_) | |
1123 | return; | |
ed535cd7 | 1124 | |
8a603e13 SA |
1125 | assert(logic_mux_data_); |
1126 | ||
144e72c9 SA |
1127 | uint32_t segment_id = 0; |
1128 | ||
65efd025 SA |
1129 | // Create initial logic mux segment |
1130 | shared_ptr<LogicSegment> output_segment = | |
cf1541a1 | 1131 | make_shared<LogicSegment>(*logic_mux_data_, segment_id, logic_mux_unit_size_, 0); |
65efd025 | 1132 | logic_mux_data_->push_segment(output_segment); |
8a603e13 SA |
1133 | |
1134 | output_segment->set_samplerate(get_input_samplerate(0)); | |
ed535cd7 | 1135 | |
144e72c9 SA |
1136 | // Logic mux data is being updated |
1137 | logic_mux_data_invalid_ = false; | |
1138 | ||
1139 | uint64_t samples_to_process; | |
27a3f09b | 1140 | do { |
144e72c9 SA |
1141 | do { |
1142 | const uint64_t input_sample_count = get_working_sample_count(segment_id); | |
1143 | const uint64_t output_sample_count = output_segment->get_sample_count(); | |
27a3f09b | 1144 | |
144e72c9 SA |
1145 | samples_to_process = |
1146 | (input_sample_count > output_sample_count) ? | |
1147 | (input_sample_count - output_sample_count) : 0; | |
ed535cd7 | 1148 | |
144e72c9 SA |
1149 | if (samples_to_process > 0) { |
1150 | const uint64_t unit_size = output_segment->unit_size(); | |
1151 | const uint64_t chunk_sample_count = DecodeChunkLength / unit_size; | |
ba5f2186 | 1152 | |
144e72c9 SA |
1153 | uint64_t processed_samples = 0; |
1154 | do { | |
1155 | const uint64_t start_sample = output_sample_count + processed_samples; | |
1156 | const uint64_t sample_count = | |
1157 | min(samples_to_process - processed_samples, chunk_sample_count); | |
1158 | ||
1159 | mux_logic_samples(segment_id, start_sample, start_sample + sample_count); | |
1160 | processed_samples += sample_count; | |
1161 | ||
1162 | // ...and process the newly muxed logic data | |
1163 | decode_input_cond_.notify_one(); | |
1164 | } while (!logic_mux_interrupt_ && (processed_samples < samples_to_process)); | |
1165 | } | |
1166 | } while (!logic_mux_interrupt_ && (samples_to_process > 0)); | |
1167 | ||
1168 | if (!logic_mux_interrupt_) { | |
1169 | // samples_to_process is now 0, we've exhausted the currently available input data | |
1170 | ||
1171 | // If the input segments are complete, we've completed this segment | |
1172 | if (all_input_segments_complete(segment_id)) { | |
1173 | if (!output_segment->is_complete()) | |
1174 | output_segment->set_complete(); | |
1175 | ||
1176 | if (segment_id < get_input_segment_count() - 1) { | |
db51130d | 1177 | |
144e72c9 SA |
1178 | // Process next segment |
1179 | segment_id++; | |
ed535cd7 | 1180 | |
144e72c9 SA |
1181 | output_segment = |
1182 | make_shared<LogicSegment>(*logic_mux_data_, segment_id, | |
1183 | logic_mux_unit_size_, 0); | |
1184 | logic_mux_data_->push_segment(output_segment); | |
1185 | ||
1186 | output_segment->set_samplerate(get_input_samplerate(segment_id)); | |
9431e2d3 SA |
1187 | } else { |
1188 | // Wait for more input data if we're processing the currently last segment | |
1189 | unique_lock<mutex> logic_mux_lock(logic_mux_mutex_); | |
1190 | logic_mux_cond_.wait(logic_mux_lock); | |
144e72c9 | 1191 | } |
db51130d SA |
1192 | } else { |
1193 | // Input segments aren't all complete yet but samples_to_process is 0, wait for more input data | |
1194 | unique_lock<mutex> logic_mux_lock(logic_mux_mutex_); | |
1195 | logic_mux_cond_.wait(logic_mux_lock); | |
69e95096 | 1196 | } |
27a3f09b | 1197 | } |
1ec191ed | 1198 | } while (!logic_mux_interrupt_); |
47747218 SA |
1199 | } |
1200 | ||
1201 | void DecodeSignal::decode_data( | |
8a603e13 | 1202 | const int64_t abs_start_samplenum, const int64_t sample_count, |
cf1541a1 | 1203 | const shared_ptr<const LogicSegment> input_segment) |
47747218 | 1204 | { |
8a603e13 | 1205 | const int64_t unit_size = input_segment->unit_size(); |
27a3f09b | 1206 | const int64_t chunk_sample_count = DecodeChunkLength / unit_size; |
47747218 SA |
1207 | |
1208 | for (int64_t i = abs_start_samplenum; | |
69e95096 | 1209 | !decode_interrupt_ && (i < (abs_start_samplenum + sample_count)); |
47747218 SA |
1210 | i += chunk_sample_count) { |
1211 | ||
1212 | const int64_t chunk_end = min(i + chunk_sample_count, | |
1213 | abs_start_samplenum + sample_count); | |
1214 | ||
e06cf18d SA |
1215 | { |
1216 | lock_guard<mutex> lock(output_mutex_); | |
b82908ab SA |
1217 | // Update the sample count showing the samples including currently processed ones |
1218 | segments_.at(current_segment_id_).samples_decoded_incl = chunk_end; | |
e06cf18d SA |
1219 | } |
1220 | ||
b82243f7 SA |
1221 | int64_t data_size = (chunk_end - i) * unit_size; |
1222 | uint8_t* chunk = new uint8_t[data_size]; | |
8a603e13 | 1223 | input_segment->get_samples(i, chunk_end, chunk); |
47747218 | 1224 | |
e91883bb | 1225 | if (srd_session_send(srd_session_, i, chunk_end, chunk, |
144e72c9 | 1226 | data_size, unit_size) != SRD_OK) { |
403c3e87 | 1227 | set_error_message(tr("Decoder reported an error")); |
144e72c9 SA |
1228 | decode_interrupt_ = true; |
1229 | } | |
a3ebd556 | 1230 | |
47747218 SA |
1231 | delete[] chunk; |
1232 | ||
b82908ab SA |
1233 | { |
1234 | lock_guard<mutex> lock(output_mutex_); | |
1235 | // Now that all samples are processed, the exclusive sample count catches up | |
1236 | segments_.at(current_segment_id_).samples_decoded_excl = chunk_end; | |
1237 | } | |
1238 | ||
1b56c646 SA |
1239 | // Notify the frontend that we processed some data and |
1240 | // possibly have new annotations as well | |
1241 | new_annotations(); | |
556259d2 SA |
1242 | |
1243 | if (decode_paused_) { | |
1244 | unique_lock<mutex> pause_wait_lock(decode_pause_mutex_); | |
1245 | decode_pause_cond_.wait(pause_wait_lock); | |
1246 | } | |
47747218 SA |
1247 | } |
1248 | } | |
1249 | ||
1250 | void DecodeSignal::decode_proc() | |
1251 | { | |
65efd025 SA |
1252 | current_segment_id_ = 0; |
1253 | ||
1254 | // If there is no input data available yet, wait until it is or we're interrupted | |
144e72c9 SA |
1255 | do { |
1256 | if (logic_mux_data_->logic_segments().size() == 0) { | |
1257 | // Wait for input data | |
1258 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
1259 | decode_input_cond_.wait(input_wait_lock); | |
1260 | } | |
1261 | } while ((!decode_interrupt_) && (logic_mux_data_->logic_segments().size() == 0)); | |
a3ebd556 SA |
1262 | |
1263 | if (decode_interrupt_) | |
1264 | return; | |
1265 | ||
cf1541a1 SA |
1266 | shared_ptr<const LogicSegment> input_segment = logic_mux_data_->logic_segments().front()->get_shared_ptr(); |
1267 | if (!input_segment) | |
1268 | return; | |
65efd025 SA |
1269 | |
1270 | // Create the initial segment and set its sample rate so that we can pass it to SRD | |
1271 | create_decode_segment(); | |
8a603e13 | 1272 | segments_.at(current_segment_id_).samplerate = input_segment->samplerate(); |
65efd025 | 1273 | segments_.at(current_segment_id_).start_time = input_segment->start_time(); |
8a603e13 | 1274 | |
65efd025 SA |
1275 | start_srd_session(); |
1276 | ||
144e72c9 | 1277 | uint64_t samples_to_process = 0; |
27a3f09b | 1278 | uint64_t abs_start_samplenum = 0; |
47747218 | 1279 | do { |
27a3f09b SA |
1280 | // Keep processing new samples until we exhaust the input data |
1281 | do { | |
144e72c9 | 1282 | samples_to_process = input_segment->get_sample_count() - abs_start_samplenum; |
27a3f09b | 1283 | |
144e72c9 SA |
1284 | if (samples_to_process > 0) { |
1285 | decode_data(abs_start_samplenum, samples_to_process, input_segment); | |
1286 | abs_start_samplenum += samples_to_process; | |
27a3f09b | 1287 | } |
144e72c9 SA |
1288 | } while (!decode_interrupt_ && (samples_to_process > 0)); |
1289 | ||
1290 | if (!decode_interrupt_) { | |
1291 | // samples_to_process is now 0, we've exhausted the currently available input data | |
1292 | ||
1293 | // If the input segment is complete, we've exhausted this segment | |
1294 | if (input_segment->is_complete()) { | |
69e95096 | 1295 | if (current_segment_id_ < (logic_mux_data_->logic_segments().size() - 1)) { |
144e72c9 SA |
1296 | // Process next segment |
1297 | current_segment_id_++; | |
1298 | ||
1299 | try { | |
1300 | input_segment = logic_mux_data_->logic_segments().at(current_segment_id_); | |
1301 | } catch (out_of_range&) { | |
1302 | qDebug() << "Decode error for" << name() << ": no logic mux segment" \ | |
1303 | << current_segment_id_ << "in decode_proc(), mux segments size is" \ | |
1304 | << logic_mux_data_->logic_segments().size(); | |
1305 | decode_interrupt_ = true; | |
1306 | return; | |
1307 | } | |
1308 | abs_start_samplenum = 0; | |
1309 | ||
1310 | // Create the next segment and set its metadata | |
1311 | create_decode_segment(); | |
1312 | segments_.at(current_segment_id_).samplerate = input_segment->samplerate(); | |
1313 | segments_.at(current_segment_id_).start_time = input_segment->start_time(); | |
1314 | ||
1315 | // Reset decoder state but keep the decoder stack intact | |
1316 | terminate_srd_session(); | |
1317 | } else { | |
1318 | // All segments have been processed | |
f5a26d5e SA |
1319 | if (!decode_interrupt_) |
1320 | decode_finished(); | |
9431e2d3 SA |
1321 | |
1322 | // Wait for more input data | |
1323 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
1324 | decode_input_cond_.wait(input_wait_lock); | |
8a603e13 | 1325 | } |
db51130d SA |
1326 | } else { |
1327 | // Input segment isn't complete yet but samples_to_process is 0, wait for more input data | |
1328 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
1329 | decode_input_cond_.wait(input_wait_lock); | |
69e95096 SA |
1330 | } |
1331 | ||
27a3f09b | 1332 | } |
144e72c9 | 1333 | } while (!decode_interrupt_); |
e91883bb SA |
1334 | } |
1335 | ||
1336 | void DecodeSignal::start_srd_session() | |
1337 | { | |
8ce0e732 SA |
1338 | // If there were stack changes, the session has been destroyed by now, so if |
1339 | // it hasn't been destroyed, we can just reset and re-use it | |
ce0b6a40 GS |
1340 | if (srd_session_) { |
1341 | // When a decoder stack was created before, re-use it | |
1342 | // for the next stream of input data, after terminating | |
1343 | // potentially still executing operations, and resetting | |
1344 | // internal state. Skip the rather expensive (teardown | |
1345 | // and) construction of another decoder stack. | |
1346 | ||
1347 | // TODO Reduce redundancy, use a common code path for | |
72486b78 | 1348 | // the meta/start sequence? |
ce0b6a40 | 1349 | terminate_srd_session(); |
72486b78 SA |
1350 | |
1351 | // Metadata is cleared also, so re-set it | |
ba5b52e1 | 1352 | uint64_t samplerate = 0; |
7dd518b8 | 1353 | if (segments_.size() > 0) |
ba5b52e1 GS |
1354 | samplerate = segments_.at(current_segment_id_).samplerate; |
1355 | if (samplerate) | |
7dd518b8 | 1356 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, |
ba5b52e1 | 1357 | g_variant_new_uint64(samplerate)); |
6a26fc44 | 1358 | for (const shared_ptr<Decoder>& dec : stack_) |
72486b78 | 1359 | dec->apply_all_options(); |
ce0b6a40 | 1360 | srd_session_start(srd_session_); |
72486b78 | 1361 | |
ce0b6a40 GS |
1362 | return; |
1363 | } | |
e91883bb | 1364 | |
a3ebd556 SA |
1365 | // Create the session |
1366 | srd_session_new(&srd_session_); | |
1367 | assert(srd_session_); | |
47747218 | 1368 | |
a3ebd556 SA |
1369 | // Create the decoders |
1370 | srd_decoder_inst *prev_di = nullptr; | |
6a26fc44 | 1371 | for (const shared_ptr<Decoder>& dec : stack_) { |
a3ebd556 | 1372 | srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_); |
e91883bb | 1373 | |
a3ebd556 | 1374 | if (!di) { |
403c3e87 | 1375 | set_error_message(tr("Failed to create decoder instance")); |
a3ebd556 | 1376 | srd_session_destroy(srd_session_); |
c5f9553c | 1377 | srd_session_ = nullptr; |
a3ebd556 SA |
1378 | return; |
1379 | } | |
e91883bb | 1380 | |
a3ebd556 SA |
1381 | if (prev_di) |
1382 | srd_inst_stack(srd_session_, prev_di, di); | |
e91883bb | 1383 | |
a3ebd556 | 1384 | prev_di = di; |
e91883bb | 1385 | } |
a3ebd556 SA |
1386 | |
1387 | // Start the session | |
7dd518b8 SA |
1388 | if (segments_.size() > 0) |
1389 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, | |
1390 | g_variant_new_uint64(segments_.at(current_segment_id_).samplerate)); | |
a3ebd556 SA |
1391 | |
1392 | srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN, | |
1393 | DecodeSignal::annotation_callback, this); | |
1394 | ||
c0c3e62a SA |
1395 | srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_BINARY, |
1396 | DecodeSignal::binary_callback, this); | |
1397 | ||
a3ebd556 | 1398 | srd_session_start(srd_session_); |
e3202557 SA |
1399 | |
1400 | // We just recreated the srd session, so all stack changes are applied now | |
1401 | stack_config_changed_ = false; | |
e91883bb | 1402 | } |
47747218 | 1403 | |
ce0b6a40 GS |
1404 | void DecodeSignal::terminate_srd_session() |
1405 | { | |
1406 | // Call the "terminate and reset" routine for the decoder stack | |
1407 | // (if available). This does not harm those stacks which already | |
1408 | // have completed their operation, and reduces response time for | |
1409 | // those stacks which still are processing data while the | |
1410 | // application no longer wants them to. | |
72486b78 | 1411 | if (srd_session_) { |
ce0b6a40 | 1412 | srd_session_terminate_reset(srd_session_); |
72486b78 SA |
1413 | |
1414 | // Metadata is cleared also, so re-set it | |
ba5b52e1 | 1415 | uint64_t samplerate = 0; |
7dd518b8 | 1416 | if (segments_.size() > 0) |
ba5b52e1 GS |
1417 | samplerate = segments_.at(current_segment_id_).samplerate; |
1418 | if (samplerate) | |
7dd518b8 | 1419 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, |
ba5b52e1 | 1420 | g_variant_new_uint64(samplerate)); |
6a26fc44 | 1421 | for (const shared_ptr<Decoder>& dec : stack_) |
72486b78 SA |
1422 | dec->apply_all_options(); |
1423 | } | |
ce0b6a40 GS |
1424 | } |
1425 | ||
e91883bb SA |
1426 | void DecodeSignal::stop_srd_session() |
1427 | { | |
1428 | if (srd_session_) { | |
1429 | // Destroy the session | |
1430 | srd_session_destroy(srd_session_); | |
1431 | srd_session_ = nullptr; | |
8ce0e732 SA |
1432 | |
1433 | // Mark the decoder instances as non-existant since they were deleted | |
6a26fc44 | 1434 | for (const shared_ptr<Decoder>& dec : stack_) |
8ce0e732 | 1435 | dec->invalidate_decoder_inst(); |
e91883bb | 1436 | } |
47747218 SA |
1437 | } |
1438 | ||
a8a9222d SA |
1439 | void DecodeSignal::connect_input_notifiers() |
1440 | { | |
a8a9222d | 1441 | // Connect the currently used signals to our slot |
a82325d1 | 1442 | for (decode::DecodeChannel& ch : channels_) { |
a8a9222d SA |
1443 | if (!ch.assigned_signal) |
1444 | continue; | |
db51130d | 1445 | const data::SignalBase *signal = ch.assigned_signal.get(); |
1c552f40 | 1446 | |
db51130d | 1447 | connect(signal, SIGNAL(samples_cleared()), |
d023660f | 1448 | this, SLOT(on_data_cleared()), Qt::UniqueConnection); |
db51130d | 1449 | connect(signal, SIGNAL(samples_added(uint64_t, uint64_t, uint64_t)), |
d023660f SA |
1450 | this, SLOT(on_data_received()), Qt::UniqueConnection); |
1451 | ||
1452 | if (signal->logic_data()) | |
1453 | connect(signal->logic_data().get(), SIGNAL(segment_completed()), | |
1454 | this, SLOT(on_input_segment_completed()), Qt::UniqueConnection); | |
db51130d SA |
1455 | } |
1456 | } | |
a8a9222d | 1457 | |
db51130d SA |
1458 | void DecodeSignal::disconnect_input_notifiers() |
1459 | { | |
1460 | // Disconnect the notification slot from the previous set of signals | |
1461 | for (decode::DecodeChannel& ch : channels_) { | |
1462 | if (!ch.assigned_signal) | |
1463 | continue; | |
cf1541a1 | 1464 | const data::SignalBase *signal = ch.assigned_signal.get(); |
db51130d SA |
1465 | disconnect(signal, nullptr, this, SLOT(on_data_cleared())); |
1466 | disconnect(signal, nullptr, this, SLOT(on_data_received())); | |
db51130d | 1467 | |
d023660f SA |
1468 | if (signal->logic_data()) |
1469 | disconnect(signal->logic_data().get(), nullptr, this, SLOT(on_input_segment_completed())); | |
1470 | } | |
db51130d SA |
1471 | } |
1472 | ||
65efd025 | 1473 | void DecodeSignal::create_decode_segment() |
4913560f | 1474 | { |
65efd025 | 1475 | // Create annotation segment |
f54e68b0 | 1476 | segments_.emplace_back(); |
72435789 | 1477 | |
65efd025 | 1478 | // Add annotation classes |
feda6c6b | 1479 | for (const shared_ptr<Decoder>& dec : stack_) |
6a26fc44 SA |
1480 | for (Row* row : dec->get_rows()) |
1481 | segments_.back().annotation_rows.emplace(row, RowData(row)); | |
e77de61f SA |
1482 | |
1483 | // Prepare our binary output classes | |
6a26fc44 | 1484 | for (const shared_ptr<Decoder>& dec : stack_) { |
ac9494ef | 1485 | uint32_t n = dec->get_binary_class_count(); |
e77de61f | 1486 | |
ac9494ef | 1487 | for (uint32_t i = 0; i < n; i++) |
e77de61f | 1488 | segments_.back().binary_classes.push_back( |
c8a6db0e | 1489 | {dec.get(), dec->get_binary_class(i), deque<DecodeBinaryDataChunk>()}); |
e77de61f | 1490 | } |
4913560f SA |
1491 | } |
1492 | ||
47747218 SA |
1493 | void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal) |
1494 | { | |
1495 | assert(pdata); | |
b21501d6 | 1496 | assert(decode_signal); |
47747218 SA |
1497 | |
1498 | DecodeSignal *const ds = (DecodeSignal*)decode_signal; | |
1499 | assert(ds); | |
1500 | ||
3b5282a2 SA |
1501 | if (ds->decode_interrupt_) |
1502 | return; | |
1503 | ||
02c87df7 SA |
1504 | if (ds->segments_.empty()) |
1505 | return; | |
1506 | ||
47747218 SA |
1507 | lock_guard<mutex> lock(ds->output_mutex_); |
1508 | ||
c0c3e62a | 1509 | // Get the decoder and the annotation data |
47747218 SA |
1510 | assert(pdata->pdo); |
1511 | assert(pdata->pdo->di); | |
6a26fc44 SA |
1512 | const srd_decoder *const srd_dec = pdata->pdo->di->decoder; |
1513 | assert(srd_dec); | |
47747218 | 1514 | |
c0c3e62a | 1515 | const srd_proto_data_annotation *const pda = (const srd_proto_data_annotation*)pdata->data; |
1becee23 UH |
1516 | assert(pda); |
1517 | ||
c0c3e62a | 1518 | // Find the row |
6a26fc44 SA |
1519 | Decoder* dec = ds->get_decoder_by_instance(srd_dec); |
1520 | assert(dec); | |
47747218 | 1521 | |
6a26fc44 | 1522 | AnnotationClass* ann_class = dec->get_ann_class_by_id(pda->ann_class); |
4526de63 SA |
1523 | if (!ann_class) { |
1524 | qWarning() << "Decoder" << ds->display_name() << "wanted to add annotation" << | |
1525 | "with class ID" << pda->ann_class << "but there are only" << | |
1526 | dec->ann_classes().size() << "known classes"; | |
1527 | return; | |
1528 | } | |
6a26fc44 SA |
1529 | |
1530 | const Row* row = ann_class->row; | |
1531 | ||
1532 | if (!row) | |
1533 | row = dec->get_row_by_id(0); | |
47747218 | 1534 | |
f54e68b0 SA |
1535 | RowData& row_data = ds->segments_[ds->current_segment_id_].annotation_rows.at(row); |
1536 | ||
1537 | // Add the annotation to the row | |
1538 | const Annotation* ann = row_data.emplace_annotation(pdata); | |
1539 | ||
ca9a5918 SA |
1540 | // We insert the annotation into the global annotation list in a way so that |
1541 | // the annotation list is sorted by start sample and length. Otherwise, we'd | |
1542 | // have to sort the model, which is expensive | |
f54e68b0 SA |
1543 | deque<const Annotation*>& all_annotations = |
1544 | ds->segments_[ds->current_segment_id_].all_annotations; | |
ca9a5918 SA |
1545 | |
1546 | if (all_annotations.empty()) { | |
1547 | all_annotations.emplace_back(ann); | |
1548 | } else { | |
1549 | const uint64_t new_ann_len = (pdata->end_sample - pdata->start_sample); | |
1550 | bool ann_has_earlier_start = (pdata->start_sample < all_annotations.back()->start_sample()); | |
1551 | bool ann_is_longer = (new_ann_len > | |
1552 | (all_annotations.back()->end_sample() - all_annotations.back()->start_sample())); | |
1553 | ||
1554 | if (ann_has_earlier_start && ann_is_longer) { | |
1555 | bool ann_has_same_start; | |
1556 | auto it = all_annotations.end(); | |
1557 | ||
1558 | do { | |
1559 | it--; | |
1560 | ann_has_earlier_start = (pdata->start_sample < (*it)->start_sample()); | |
1561 | ann_has_same_start = (pdata->start_sample == (*it)->start_sample()); | |
d656b010 | 1562 | ann_is_longer = (new_ann_len > (*it)->length()); |
ca9a5918 SA |
1563 | } while ((ann_has_earlier_start || (ann_has_same_start && ann_is_longer)) && (it != all_annotations.begin())); |
1564 | ||
1565 | // Allow inserting at the front | |
1566 | if (it != all_annotations.begin()) | |
1567 | it++; | |
1568 | ||
1569 | all_annotations.emplace(it, ann); | |
1570 | } else | |
1571 | all_annotations.emplace_back(ann); | |
1572 | } | |
88a25978 SA |
1573 | |
1574 | // When emplace_annotation() inserts instead of appends an annotation, | |
1575 | // the pointers in all_annotations that follow the inserted annotation and | |
1576 | // point to annotations for this row are off by one and must be updated | |
1577 | if (&(row_data.annotations().back()) != ann) { | |
1578 | // Search backwards until we find the annotation we just added | |
1579 | auto row_it = row_data.annotations().end(); | |
1580 | auto all_it = all_annotations.end(); | |
1581 | do { | |
1582 | all_it--; | |
1583 | if ((*all_it)->row_data() == &row_data) | |
1584 | row_it--; | |
1585 | } while (&(*row_it) != ann); | |
1586 | ||
1587 | // Update the annotation addresses for this row's annotations until the end | |
1588 | do { | |
1589 | if ((*all_it)->row_data() == &row_data) { | |
1590 | *all_it = &(*row_it); | |
1591 | row_it++; | |
1592 | } | |
1593 | all_it++; | |
1594 | } while (all_it != all_annotations.end()); | |
1595 | } | |
47747218 SA |
1596 | } |
1597 | ||
c0c3e62a SA |
1598 | void DecodeSignal::binary_callback(srd_proto_data *pdata, void *decode_signal) |
1599 | { | |
1600 | assert(pdata); | |
1601 | assert(decode_signal); | |
1602 | ||
1603 | DecodeSignal *const ds = (DecodeSignal*)decode_signal; | |
1604 | assert(ds); | |
1605 | ||
1606 | if (ds->decode_interrupt_) | |
1607 | return; | |
1608 | ||
e77de61f SA |
1609 | // Get the decoder and the binary data |
1610 | assert(pdata->pdo); | |
1611 | assert(pdata->pdo->di); | |
6a26fc44 SA |
1612 | const srd_decoder *const srd_dec = pdata->pdo->di->decoder; |
1613 | assert(srd_dec); | |
e77de61f | 1614 | |
c0c3e62a SA |
1615 | const srd_proto_data_binary *const pdb = (const srd_proto_data_binary*)pdata->data; |
1616 | assert(pdb); | |
1617 | ||
e77de61f | 1618 | // Find the matching DecodeBinaryClass |
c0c3e62a SA |
1619 | DecodeSegment* segment = &(ds->segments_.at(ds->current_segment_id_)); |
1620 | ||
e77de61f SA |
1621 | DecodeBinaryClass* bin_class = nullptr; |
1622 | for (DecodeBinaryClass& bc : segment->binary_classes) | |
6a26fc44 SA |
1623 | if ((bc.decoder->get_srd_decoder() == srd_dec) && |
1624 | (bc.info->bin_class_id == (uint32_t)pdb->bin_class)) | |
e77de61f | 1625 | bin_class = &bc; |
c0c3e62a | 1626 | |
e77de61f SA |
1627 | if (!bin_class) { |
1628 | qWarning() << "Could not find valid DecodeBinaryClass in segment" << | |
1629 | ds->current_segment_id_ << "for binary class ID" << pdb->bin_class << | |
1630 | ", segment only knows" << segment->binary_classes.size() << "classes"; | |
1631 | return; | |
1632 | } | |
c0c3e62a | 1633 | |
e77de61f SA |
1634 | // Add the data chunk |
1635 | bin_class->chunks.emplace_back(); | |
1636 | DecodeBinaryDataChunk* chunk = &(bin_class->chunks.back()); | |
1637 | ||
1638 | chunk->sample = pdata->start_sample; | |
1639 | chunk->data.resize(pdb->size); | |
1640 | memcpy(chunk->data.data(), pdb->data, pdb->size); | |
1641 | ||
6a26fc44 | 1642 | Decoder* dec = ds->get_decoder_by_instance(srd_dec); |
b2b18d3a SA |
1643 | |
1644 | ds->new_binary_data(ds->current_segment_id_, (void*)dec, pdb->bin_class); | |
c0c3e62a SA |
1645 | } |
1646 | ||
47747218 SA |
1647 | void DecodeSignal::on_capture_state_changed(int state) |
1648 | { | |
1649 | // If a new acquisition was started, we need to start decoding from scratch | |
ba5f2186 SA |
1650 | if (state == Session::Running) { |
1651 | logic_mux_data_invalid_ = true; | |
47747218 | 1652 | begin_decode(); |
ba5f2186 | 1653 | } |
47747218 SA |
1654 | } |
1655 | ||
1b56c646 SA |
1656 | void DecodeSignal::on_data_cleared() |
1657 | { | |
1658 | reset_decode(); | |
1659 | } | |
1660 | ||
47747218 SA |
1661 | void DecodeSignal::on_data_received() |
1662 | { | |
5620539c | 1663 | // If we detected a lack of input data when trying to start decoding, |
d023660f | 1664 | // we have set an error message. Bail out if we still don't have data |
5620539c SA |
1665 | // to work with |
1666 | if ((!error_message_.isEmpty()) && (get_input_segment_count() == 0)) | |
1667 | return; | |
d023660f SA |
1668 | |
1669 | if (!error_message_.isEmpty()) { | |
69e95096 SA |
1670 | error_message_.clear(); |
1671 | // TODO Emulate noquote() | |
d023660f | 1672 | qDebug().nospace() << name() << ": Input data available, error cleared"; |
69e95096 | 1673 | } |
5620539c | 1674 | |
1b56c646 SA |
1675 | if (!logic_mux_thread_.joinable()) |
1676 | begin_decode(); | |
1677 | else | |
1678 | logic_mux_cond_.notify_one(); | |
47747218 SA |
1679 | } |
1680 | ||
db51130d SA |
1681 | void DecodeSignal::on_input_segment_completed() |
1682 | { | |
1683 | if (!logic_mux_thread_.joinable()) | |
1684 | logic_mux_cond_.notify_one(); | |
1685 | } | |
1686 | ||
02078aa1 SA |
1687 | void DecodeSignal::on_annotation_visibility_changed() |
1688 | { | |
1689 | annotation_visibility_changed(); | |
1690 | } | |
1691 | ||
ad908057 SA |
1692 | } // namespace data |
1693 | } // namespace pv |