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