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