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