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