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