]> sigrok.org Git - pulseview.git/blame - pv/data/decodesignal.cpp
DecodeSignal: Unpause reliably
[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
5a914348 20#include <forward_list>
0c5fe73e
SA
21#include <limits>
22
47747218
SA
23#include <QDebug>
24
ad908057
SA
25#include "logic.hpp"
26#include "logicsegment.hpp"
27#include "decodesignal.hpp"
28#include "signaldata.hpp"
29
30#include <pv/binding/decoder.hpp>
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
5a914348 36using std::forward_list;
47747218
SA
37using std::lock_guard;
38using std::make_pair;
ad908057 39using std::make_shared;
0c5fe73e 40using std::min;
5ecf957f 41using std::out_of_range;
ad908057 42using std::shared_ptr;
47747218
SA
43using std::unique_lock;
44using pv::data::decode::Annotation;
ad908057 45using pv::data::decode::Decoder;
ecd07c20 46using pv::data::decode::Row;
ad908057
SA
47
48namespace pv {
49namespace data {
50
47747218
SA
51const double DecodeSignal::DecodeMargin = 1.0;
52const double DecodeSignal::DecodeThreshold = 0.2;
3fbddf7f 53const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024;
ad908057 54
ad908057 55
47747218
SA
56DecodeSignal::DecodeSignal(pv::Session &session) :
57 SignalBase(nullptr, SignalBase::DecodeChannel),
58 session_(session),
e91883bb 59 srd_session_(nullptr),
47747218 60 logic_mux_data_invalid_(false),
e3202557 61 stack_config_changed_(true),
8a603e13 62 current_segment_id_(0)
ad908057 63{
47747218
SA
64 connect(&session_, SIGNAL(capture_state_changed(int)),
65 this, SLOT(on_capture_state_changed(int)));
ad908057
SA
66}
67
47747218 68DecodeSignal::~DecodeSignal()
ad908057 69{
8ce0e732 70 reset_decode(true);
ad908057
SA
71}
72
47747218 73const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const
ecd07c20 74{
47747218 75 return stack_;
ecd07c20
SA
76}
77
c8e60bdf 78void DecodeSignal::stack_decoder(const srd_decoder *decoder)
ad908057
SA
79{
80 assert(decoder);
762ab7a4
SA
81 const shared_ptr<Decoder> dec = make_shared<decode::Decoder>(decoder);
82
83 stack_.push_back(dec);
47747218
SA
84
85 // Set name if this decoder is the first in the list
86 if (stack_.size() == 1)
87 set_name(QString::fromUtf8(decoder->name));
132a5c6d 88
27a3f09b 89 // Include the newly created decode channels in the channel lists
9f97b357 90 update_channel_list();
132a5c6d 91
e3202557 92 stack_config_changed_ = true;
762ab7a4 93 auto_assign_signals(dec);
27a3f09b 94 commit_decoder_channels();
47747218 95 begin_decode();
ad908057
SA
96}
97
98void DecodeSignal::remove_decoder(int index)
99{
47747218
SA
100 assert(index >= 0);
101 assert(index < (int)stack_.size());
102
103 // Find the decoder in the stack
104 auto iter = stack_.begin();
105 for (int i = 0; i < index; i++, iter++)
106 assert(iter != stack_.end());
107
108 // Delete the element
109 stack_.erase(iter);
110
111 // Update channels and decoded data
e3202557 112 stack_config_changed_ = true;
9f97b357 113 update_channel_list();
47747218 114 begin_decode();
ad908057
SA
115}
116
117bool DecodeSignal::toggle_decoder_visibility(int index)
118{
47747218 119 auto iter = stack_.cbegin();
ad908057 120 for (int i = 0; i < index; i++, iter++)
47747218 121 assert(iter != stack_.end());
ad908057
SA
122
123 shared_ptr<Decoder> dec = *iter;
124
125 // Toggle decoder visibility
126 bool state = false;
127 if (dec) {
128 state = !dec->shown();
129 dec->show(state);
130 }
131
132 return state;
133}
134
8ce0e732 135void DecodeSignal::reset_decode(bool shutting_down)
47747218 136{
8ce0e732 137 if (stack_config_changed_ || shutting_down)
e3202557
SA
138 stop_srd_session();
139 else
140 terminate_srd_session();
ce0b6a40 141
1b56c646
SA
142 if (decode_thread_.joinable()) {
143 decode_interrupt_ = true;
144 decode_input_cond_.notify_one();
145 decode_thread_.join();
146 }
147
148 if (logic_mux_thread_.joinable()) {
149 logic_mux_interrupt_ = true;
150 logic_mux_cond_.notify_one();
151 logic_mux_thread_.join();
152 }
153
dda88657 154 resume_decode(); // Make sure the decode thread isn't blocked by pausing
556259d2 155
47747218 156 class_rows_.clear();
8a603e13 157 current_segment_id_ = 0;
72435789 158 segments_.clear();
1b56c646
SA
159
160 logic_mux_data_.reset();
161 logic_mux_data_invalid_ = true;
eee3eab9 162
403c3e87
SA
163 if (!error_message_.isEmpty()) {
164 error_message_ = QString();
fe060a48
SA
165 // TODO Emulate noquote()
166 qDebug().nospace() << name() << ": Error cleared";
403c3e87 167 }
72435789 168
eee3eab9 169 decode_reset();
47747218
SA
170}
171
946b52e1
SA
172void DecodeSignal::begin_decode()
173{
47747218
SA
174 if (decode_thread_.joinable()) {
175 decode_interrupt_ = true;
176 decode_input_cond_.notify_one();
177 decode_thread_.join();
178 }
179
180 if (logic_mux_thread_.joinable()) {
181 logic_mux_interrupt_ = true;
182 logic_mux_cond_.notify_one();
183 logic_mux_thread_.join();
184 }
185
186 reset_decode();
187
27a3f09b 188 if (stack_.size() == 0) {
403c3e87 189 set_error_message(tr("No decoders"));
27a3f09b
SA
190 return;
191 }
192
193 assert(channels_.size() > 0);
194
195 if (get_assigned_signal_count() == 0) {
403c3e87 196 set_error_message(tr("There are no channels assigned to this decoder"));
27a3f09b
SA
197 return;
198 }
199
04627946
SA
200 // Make sure that all assigned channels still provide logic data
201 // (can happen when a converted signal was assigned but the
202 // conversion removed in the meanwhile)
203 for (data::DecodeChannel &ch : channels_)
204 if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr))
205 ch.assigned_signal = nullptr;
206
47747218
SA
207 // Check that all decoders have the required channels
208 for (const shared_ptr<decode::Decoder> &dec : stack_)
209 if (!dec->have_required_channels()) {
403c3e87
SA
210 set_error_message(tr("One or more required channels "
211 "have not been specified"));
47747218
SA
212 return;
213 }
214
4913560f 215 // Map out all the annotation classes
f9807084 216 int row_index = 0;
47747218
SA
217 for (const shared_ptr<decode::Decoder> &dec : stack_) {
218 assert(dec);
219 const srd_decoder *const decc = dec->decoder();
220 assert(dec->decoder());
221
47747218
SA
222 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
223 const srd_decoder_annotation_row *const ann_row =
224 (srd_decoder_annotation_row *)l->data;
225 assert(ann_row);
226
f9807084 227 const Row row(row_index++, decc, ann_row);
47747218 228
47747218
SA
229 for (const GSList *ll = ann_row->ann_classes;
230 ll; ll = ll->next)
231 class_rows_[make_pair(decc,
232 GPOINTER_TO_INT(ll->data))] = row;
233 }
234 }
235
27a3f09b
SA
236 // Free the logic data and its segment(s) if it needs to be updated
237 if (logic_mux_data_invalid_)
238 logic_mux_data_.reset();
239
240 if (!logic_mux_data_) {
72435789 241 const uint32_t ch_count = get_assigned_signal_count();
ed535cd7 242 logic_mux_unit_size_ = (ch_count + 7) / 8;
27a3f09b 243 logic_mux_data_ = make_shared<Logic>(ch_count);
27a3f09b 244 }
47747218 245
ed535cd7
SA
246 // Receive notifications when new sample data is available
247 connect_input_notifiers();
248
8a603e13 249 if (get_input_segment_count() == 0) {
403c3e87 250 set_error_message(tr("No input data"));
ed535cd7
SA
251 return;
252 }
253
27a3f09b
SA
254 // Make sure the logic output data is complete and up-to-date
255 logic_mux_interrupt_ = false;
256 logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this);
257
258 // Decode the muxed logic data
47747218
SA
259 decode_interrupt_ = false;
260 decode_thread_ = std::thread(&DecodeSignal::decode_proc, this);
946b52e1
SA
261}
262
556259d2
SA
263void DecodeSignal::pause_decode()
264{
265 decode_paused_ = true;
266}
267
268void DecodeSignal::resume_decode()
269{
270 // Manual unlocking is done before notifying, to avoid waking up the
271 // waiting thread only to block again (see notify_one for details)
272 decode_pause_mutex_.unlock();
273 decode_pause_cond_.notify_one();
274 decode_paused_ = false;
275}
276
277bool DecodeSignal::is_paused() const
278{
279 return decode_paused_;
280}
281
ecd07c20
SA
282QString DecodeSignal::error_message() const
283{
47747218
SA
284 lock_guard<mutex> lock(output_mutex_);
285 return error_message_;
ecd07c20
SA
286}
287
47747218 288const vector<data::DecodeChannel> DecodeSignal::get_channels() const
9f97b357
SA
289{
290 return channels_;
291}
292
762ab7a4 293void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec)
132a5c6d 294{
47747218
SA
295 bool new_assignment = false;
296
132a5c6d
SA
297 // Try to auto-select channels that don't have signals assigned yet
298 for (data::DecodeChannel &ch : channels_) {
762ab7a4
SA
299 // If a decoder is given, auto-assign only its channels
300 if (dec && (ch.decoder_ != dec))
301 continue;
302
132a5c6d
SA
303 if (ch.assigned_signal)
304 continue;
305
a8030bb0
SA
306 const QString ch_name = ch.name.toLower();
307
308 shared_ptr<data::SignalBase> match;
692f6093 309 for (shared_ptr<data::SignalBase> s : session_.signalbases()) {
f9ebf303
SA
310 if (!s->enabled())
311 continue;
312
692f6093
SA
313 const QString s_name = s->name().toLower();
314
315 if (s->logic_data() &&
316 ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) {
a8030bb0
SA
317 if (!match)
318 match = s;
319 else {
320 // Only replace an existing match if it matches more characters
321 int old_unmatched = ch_name.length() - match->name().length();
322 int new_unmatched = ch_name.length() - s->name().length();
323 if (abs(new_unmatched) < abs(old_unmatched))
324 match = s;
325 }
47747218 326 }
692f6093 327 }
a8030bb0
SA
328
329 if (match) {
330 ch.assigned_signal = match.get();
331 new_assignment = true;
332 }
47747218
SA
333 }
334
335 if (new_assignment) {
336 logic_mux_data_invalid_ = true;
e3202557 337 stack_config_changed_ = true;
27a3f09b 338 commit_decoder_channels();
47747218 339 channels_updated();
132a5c6d
SA
340 }
341}
342
9f97b357
SA
343void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
344{
345 for (data::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
e3202557 351 stack_config_changed_ = true;
27a3f09b 352 commit_decoder_channels();
9f97b357 353 channels_updated();
47747218 354 begin_decode();
9f97b357
SA
355}
356
27a3f09b
SA
357int DecodeSignal::get_assigned_signal_count() const
358{
359 // Count all channels that have a signal assigned to them
360 return count_if(channels_.begin(), channels_.end(),
361 [](data::DecodeChannel ch) { return ch.assigned_signal; });
362}
363
9f97b357
SA
364void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
365{
366 for (data::DecodeChannel &ch : channels_)
367 if (ch.id == channel_id)
368 ch.initial_pin_state = init_state;
369
e3202557 370 stack_config_changed_ = true;
9f97b357 371 channels_updated();
47747218 372 begin_decode();
9f97b357
SA
373}
374
ff83d980
SA
375double DecodeSignal::samplerate() const
376{
72435789
SA
377 double result = 0;
378
379 // TODO For now, we simply return the first samplerate that we have
8a603e13
SA
380 if (segments_.size() > 0)
381 result = segments_.front().samplerate;
72435789
SA
382
383 return result;
ff83d980
SA
384}
385
72435789 386const pv::util::Timestamp DecodeSignal::start_time() const
ff83d980 387{
72435789
SA
388 pv::util::Timestamp result;
389
390 // TODO For now, we simply return the first start time that we have
8a603e13
SA
391 if (segments_.size() > 0)
392 result = segments_.front().start_time;
72435789
SA
393
394 return result;
ff83d980
SA
395}
396
5ecf957f 397int64_t DecodeSignal::get_working_sample_count(uint32_t segment_id) const
0c5fe73e
SA
398{
399 // The working sample count is the highest sample number for
ed535cd7
SA
400 // which all used signals have data available, so go through all
401 // channels and use the lowest overall sample count of the segment
0c5fe73e 402
0c5fe73e
SA
403 int64_t count = std::numeric_limits<int64_t>::max();
404 bool no_signals_assigned = true;
405
406 for (const data::DecodeChannel &ch : channels_)
407 if (ch.assigned_signal) {
408 no_signals_assigned = false;
409
410 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
411 if (!logic_data || logic_data->logic_segments().empty())
412 return 0;
413
5ecf957f
SA
414 try {
415 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id);
416 count = min(count, (int64_t)segment->get_sample_count());
30677c13 417 } catch (out_of_range&) {
5ecf957f
SA
418 return 0;
419 }
0c5fe73e
SA
420 }
421
422 return (no_signals_assigned ? 0 : count);
423}
424
b82908ab
SA
425int64_t DecodeSignal::get_decoded_sample_count(uint32_t segment_id,
426 bool include_processing) const
ff83d980 427{
47747218 428 lock_guard<mutex> decode_lock(output_mutex_);
5ecf957f
SA
429
430 int64_t result = 0;
431
72435789
SA
432 try {
433 const DecodeSegment *segment = &(segments_.at(segment_id));
b82908ab
SA
434 if (include_processing)
435 result = segment->samples_decoded_incl;
436 else
437 result = segment->samples_decoded_excl;
30677c13 438 } catch (out_of_range&) {
72435789
SA
439 // Do nothing
440 }
5ecf957f
SA
441
442 return result;
ff83d980
SA
443}
444
ecd07c20
SA
445vector<Row> DecodeSignal::visible_rows() const
446{
47747218
SA
447 lock_guard<mutex> lock(output_mutex_);
448
449 vector<Row> rows;
450
451 for (const shared_ptr<decode::Decoder> &dec : stack_) {
452 assert(dec);
453 if (!dec->shown())
454 continue;
455
456 const srd_decoder *const decc = dec->decoder();
457 assert(dec->decoder());
458
f9807084 459 int row_index = 0;
47747218
SA
460 // Add a row for the decoder if it doesn't have a row list
461 if (!decc->annotation_rows)
f9807084 462 rows.emplace_back(row_index++, decc);
47747218
SA
463
464 // Add the decoder rows
465 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
466 const srd_decoder_annotation_row *const ann_row =
467 (srd_decoder_annotation_row *)l->data;
468 assert(ann_row);
f9807084 469 rows.emplace_back(row_index++, decc, ann_row);
47747218
SA
470 }
471 }
472
473 return rows;
ecd07c20
SA
474}
475
476void DecodeSignal::get_annotation_subset(
477 vector<pv::data::decode::Annotation> &dest,
72435789 478 const decode::Row &row, uint32_t segment_id, uint64_t start_sample,
ecd07c20
SA
479 uint64_t end_sample) const
480{
47747218
SA
481 lock_guard<mutex> lock(output_mutex_);
482
72435789
SA
483 try {
484 const DecodeSegment *segment = &(segments_.at(segment_id));
485 const map<const decode::Row, decode::RowData> *rows =
486 &(segment->annotation_rows);
487
488 const auto iter = rows->find(row);
489 if (iter != rows->end())
490 (*iter).second.get_annotation_subset(dest,
491 start_sample, end_sample);
30677c13 492 } catch (out_of_range&) {
72435789
SA
493 // Do nothing
494 }
47747218
SA
495}
496
5a914348
SA
497void DecodeSignal::get_annotation_subset(
498 vector<pv::data::decode::Annotation> &dest,
499 uint32_t segment_id, uint64_t start_sample, uint64_t end_sample) const
500{
501 // Note: We put all vectors and lists on the heap, not the stack
502
503 const vector<Row> rows = visible_rows();
504
505 // Use forward_lists for faster merging
506 forward_list<Annotation> *all_ann_list = new forward_list<Annotation>();
507
508 for (const Row& row : rows) {
509 vector<Annotation> *ann_vector = new vector<Annotation>();
510 get_annotation_subset(*ann_vector, row, segment_id, start_sample, end_sample);
511
512 forward_list<Annotation> *ann_list =
513 new forward_list<Annotation>(ann_vector->begin(), ann_vector->end());
514 delete ann_vector;
515
516 all_ann_list->merge(*ann_list);
517 delete ann_list;
518 }
519
520 move(all_ann_list->begin(), all_ann_list->end(), back_inserter(dest));
521 delete all_ann_list;
522}
523
47747218
SA
524void DecodeSignal::save_settings(QSettings &settings) const
525{
526 SignalBase::save_settings(settings);
527
c8e60bdf
SA
528 settings.setValue("decoders", (int)(stack_.size()));
529
8b5ea5a1 530 // Save decoder stack
c8e60bdf
SA
531 int decoder_idx = 0;
532 for (shared_ptr<decode::Decoder> decoder : stack_) {
533 settings.beginGroup("decoder" + QString::number(decoder_idx++));
534
535 settings.setValue("id", decoder->decoder()->id);
536
d3feec23
SA
537 // Save decoder options
538 const map<string, GVariant*>& options = decoder->options();
539
540 settings.setValue("options", (int)options.size());
541
542 // Note: decode::Decoder::options() returns only the options
543 // that differ from the default. See binding::Decoder::getter()
544 int i = 0;
545 for (auto option : options) {
546 settings.beginGroup("option" + QString::number(i));
547 settings.setValue("name", QString::fromStdString(option.first));
548 GlobalSettings::store_gvariant(settings, option.second);
549 settings.endGroup();
550 i++;
551 }
552
c8e60bdf
SA
553 settings.endGroup();
554 }
555
8b5ea5a1
SA
556 // Save channel mapping
557 settings.setValue("channels", (int)channels_.size());
558
559 for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) {
560 auto channel = find_if(channels_.begin(), channels_.end(),
561 [&](data::DecodeChannel ch) { return ch.id == channel_id; });
562
563 if (channel == channels_.end()) {
564 qDebug() << "ERROR: Gap in channel index:" << channel_id;
565 continue;
566 }
567
568 settings.beginGroup("channel" + QString::number(channel_id));
569
570 settings.setValue("name", channel->name); // Useful for debugging
571 settings.setValue("initial_pin_state", channel->initial_pin_state);
572
573 if (channel->assigned_signal)
574 settings.setValue("assigned_signal_name", channel->assigned_signal->name());
575
576 settings.endGroup();
577 }
47747218
SA
578}
579
580void DecodeSignal::restore_settings(QSettings &settings)
581{
582 SignalBase::restore_settings(settings);
583
8b5ea5a1 584 // Restore decoder stack
c8e60bdf
SA
585 GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list());
586
587 int decoders = settings.value("decoders").toInt();
588
589 for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) {
590 settings.beginGroup("decoder" + QString::number(decoder_idx));
591
592 QString id = settings.value("id").toString();
593
594 for (GSList *entry = dec_list; entry; entry = entry->next) {
595 const srd_decoder *dec = (srd_decoder*)entry->data;
596 if (!dec)
597 continue;
598
599 if (QString::fromUtf8(dec->id) == id) {
d3feec23
SA
600 shared_ptr<decode::Decoder> decoder =
601 make_shared<decode::Decoder>(dec);
602
603 stack_.push_back(decoder);
604
605 // Restore decoder options that differ from their default
606 int options = settings.value("options").toInt();
607
608 for (int i = 0; i < options; i++) {
609 settings.beginGroup("option" + QString::number(i));
610 QString name = settings.value("name").toString();
611 GVariant *value = GlobalSettings::restore_gvariant(settings);
612 decoder->set_option(name.toUtf8(), value);
613 settings.endGroup();
614 }
c8e60bdf
SA
615
616 // Include the newly created decode channels in the channel lists
617 update_channel_list();
618 break;
619 }
620 }
621
622 settings.endGroup();
4ca06ec3 623 channels_updated();
c8e60bdf
SA
624 }
625
8b5ea5a1
SA
626 // Restore channel mapping
627 unsigned int channels = settings.value("channels").toInt();
628
629 const unordered_set< shared_ptr<data::SignalBase> > signalbases =
630 session_.signalbases();
631
632 for (unsigned int channel_id = 0; channel_id < channels; channel_id++) {
633 auto channel = find_if(channels_.begin(), channels_.end(),
634 [&](data::DecodeChannel ch) { return ch.id == channel_id; });
635
636 if (channel == channels_.end()) {
637 qDebug() << "ERROR: Non-existant channel index:" << channel_id;
638 continue;
639 }
640
641 settings.beginGroup("channel" + QString::number(channel_id));
642
643 QString assigned_signal_name = settings.value("assigned_signal_name").toString();
644
645 for (shared_ptr<data::SignalBase> signal : signalbases)
646 if (signal->name() == assigned_signal_name)
647 channel->assigned_signal = signal.get();
648
649 channel->initial_pin_state = settings.value("initial_pin_state").toInt();
650
651 settings.endGroup();
652 }
653
aa71890d 654 // Update the internal structures
e3202557 655 stack_config_changed_ = true;
aa71890d
SA
656 update_channel_list();
657 commit_decoder_channels();
658
8b5ea5a1 659 begin_decode();
47747218
SA
660}
661
403c3e87
SA
662void DecodeSignal::set_error_message(QString msg)
663{
664 error_message_ = msg;
fe060a48
SA
665 // TODO Emulate noquote()
666 qDebug().nospace() << name() << ": " << msg;
403c3e87
SA
667}
668
8a603e13
SA
669uint32_t DecodeSignal::get_input_segment_count() const
670{
671 uint64_t count = std::numeric_limits<uint64_t>::max();
672 bool no_signals_assigned = true;
673
674 for (const data::DecodeChannel &ch : channels_)
675 if (ch.assigned_signal) {
676 no_signals_assigned = false;
677
678 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
679 if (!logic_data || logic_data->logic_segments().empty())
680 return 0;
681
682 // Find the min value of all segment counts
683 if ((uint64_t)(logic_data->logic_segments().size()) < count)
684 count = logic_data->logic_segments().size();
685 }
686
687 return (no_signals_assigned ? 0 : count);
688}
689
690uint32_t DecodeSignal::get_input_samplerate(uint32_t segment_id) const
691{
692 double samplerate = 0;
693
694 for (const data::DecodeChannel &ch : channels_)
695 if (ch.assigned_signal) {
696 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
697 if (!logic_data || logic_data->logic_segments().empty())
698 continue;
699
700 try {
701 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id);
702 samplerate = segment->samplerate();
30677c13 703 } catch (out_of_range&) {
8a603e13
SA
704 // Do nothing
705 }
706 break;
707 }
708
709 return samplerate;
710}
711
9f97b357
SA
712void DecodeSignal::update_channel_list()
713{
47747218 714 vector<data::DecodeChannel> prev_channels = channels_;
9f97b357
SA
715 channels_.clear();
716
717 uint16_t id = 0;
718
719 // Copy existing entries, create new as needed
47747218 720 for (shared_ptr<Decoder> decoder : stack_) {
9f97b357
SA
721 const srd_decoder* srd_d = decoder->decoder();
722 const GSList *l;
723
724 // Mandatory channels
725 for (l = srd_d->channels; l; l = l->next) {
726 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
727 bool ch_added = false;
728
729 // Copy but update ID if this channel was in the list before
47747218 730 for (data::DecodeChannel &ch : prev_channels)
9f97b357
SA
731 if (ch.pdch_ == pdch) {
732 ch.id = id++;
733 channels_.push_back(ch);
734 ch_added = true;
735 break;
736 }
737
738 if (!ch_added) {
739 // Create new entry without a mapped signal
6e7a4a00 740 data::DecodeChannel ch = {id++, 0, false, nullptr,
9f97b357
SA
741 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
742 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
743 channels_.push_back(ch);
744 }
745 }
746
747 // Optional channels
748 for (l = srd_d->opt_channels; l; l = l->next) {
749 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
750 bool ch_added = false;
751
752 // Copy but update ID if this channel was in the list before
47747218 753 for (data::DecodeChannel &ch : prev_channels)
9f97b357
SA
754 if (ch.pdch_ == pdch) {
755 ch.id = id++;
756 channels_.push_back(ch);
757 ch_added = true;
758 break;
759 }
760
761 if (!ch_added) {
762 // Create new entry without a mapped signal
6e7a4a00 763 data::DecodeChannel ch = {id++, 0, true, nullptr,
9f97b357
SA
764 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
765 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
766 channels_.push_back(ch);
767 }
768 }
769 }
770
47747218
SA
771 // Invalidate the logic output data if the channel assignment changed
772 if (prev_channels.size() != channels_.size()) {
773 // The number of channels changed, there's definitely a difference
774 logic_mux_data_invalid_ = true;
775 } else {
776 // Same number but assignment may still differ, so compare all channels
777 for (size_t i = 0; i < channels_.size(); i++) {
778 const data::DecodeChannel &p_ch = prev_channels[i];
779 const data::DecodeChannel &ch = channels_[i];
780
781 if ((p_ch.pdch_ != ch.pdch_) ||
782 (p_ch.assigned_signal != ch.assigned_signal)) {
783 logic_mux_data_invalid_ = true;
784 break;
785 }
786 }
787
788 }
789
9f97b357
SA
790 channels_updated();
791}
792
27a3f09b 793void DecodeSignal::commit_decoder_channels()
ad908057 794{
27a3f09b
SA
795 // Submit channel list to every decoder, containing only the relevant channels
796 for (shared_ptr<decode::Decoder> dec : stack_) {
797 vector<data::DecodeChannel*> channel_list;
798
799 for (data::DecodeChannel &ch : channels_)
800 if (ch.decoder_ == dec)
801 channel_list.push_back(&ch);
47747218 802
27a3f09b
SA
803 dec->set_channels(channel_list);
804 }
aa71890d
SA
805
806 // Channel bit IDs must be in sync with the channel's apperance in channels_
807 int id = 0;
808 for (data::DecodeChannel &ch : channels_)
809 if (ch.assigned_signal)
810 ch.bit_id = id++;
47747218
SA
811}
812
ed535cd7 813void DecodeSignal::mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end)
47747218 814{
27a3f09b
SA
815 // Enforce end to be greater than start
816 if (end <= start)
817 return;
818
ed535cd7 819 // Fetch the channel segments and their data
27a3f09b
SA
820 vector<shared_ptr<LogicSegment> > segments;
821 vector<const uint8_t*> signal_data;
822 vector<uint8_t> signal_in_bytepos;
823 vector<uint8_t> signal_in_bitpos;
824
825 for (data::DecodeChannel &ch : channels_)
826 if (ch.assigned_signal) {
827 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
ed535cd7
SA
828
829 shared_ptr<LogicSegment> segment;
830 try {
831 segment = logic_data->logic_segments().at(segment_id);
30677c13 832 } catch (out_of_range&) {
ed535cd7
SA
833 qDebug() << "Muxer error for" << name() << ":" << ch.assigned_signal->name() \
834 << "has no logic segment" << segment_id;
835 return;
836 }
27a3f09b 837 segments.push_back(segment);
b82243f7
SA
838
839 uint8_t* data = new uint8_t[(end - start) * segment->unit_size()];
840 segment->get_samples(start, end, data);
841 signal_data.push_back(data);
27a3f09b
SA
842
843 const int bitpos = ch.assigned_signal->logic_bit_index();
844 signal_in_bytepos.push_back(bitpos / 8);
845 signal_in_bitpos.push_back(bitpos % 8);
846 }
847
ed535cd7
SA
848
849 shared_ptr<LogicSegment> output_segment;
850 try {
851 output_segment = logic_mux_data_->logic_segments().at(segment_id);
30677c13 852 } catch (out_of_range&) {
ed535cd7
SA
853 qDebug() << "Muxer error for" << name() << ": no logic mux segment" \
854 << segment_id << "in mux_logic_samples(), mux segments size is" \
855 << logic_mux_data_->logic_segments().size();
856 return;
857 }
858
27a3f09b 859 // Perform the muxing of signal data into the output data
8a603e13 860 uint8_t* output = new uint8_t[(end - start) * output_segment->unit_size()];
27a3f09b
SA
861 unsigned int signal_count = signal_data.size();
862
3b5282a2
SA
863 for (int64_t sample_cnt = 0; !logic_mux_interrupt_ && (sample_cnt < (end - start));
864 sample_cnt++) {
865
27a3f09b
SA
866 int bitpos = 0;
867 uint8_t bytepos = 0;
868
8a603e13
SA
869 const int out_sample_pos = sample_cnt * output_segment->unit_size();
870 for (unsigned int i = 0; i < output_segment->unit_size(); i++)
27a3f09b 871 output[out_sample_pos + i] = 0;
47747218 872
27a3f09b
SA
873 for (unsigned int i = 0; i < signal_count; i++) {
874 const int in_sample_pos = sample_cnt * segments[i]->unit_size();
875 const uint8_t in_sample = 1 &
876 ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i]));
47747218 877
27a3f09b
SA
878 const uint8_t out_sample = output[out_sample_pos + bytepos];
879
880 output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos);
881
882 bitpos++;
883 if (bitpos > 7) {
884 bitpos = 0;
885 bytepos++;
886 }
887 }
47747218
SA
888 }
889
8a603e13 890 output_segment->append_payload(output, (end - start) * output_segment->unit_size());
27a3f09b
SA
891 delete[] output;
892
893 for (const uint8_t* data : signal_data)
894 delete[] data;
895}
896
897void DecodeSignal::logic_mux_proc()
898{
ed535cd7
SA
899 uint32_t segment_id = 0;
900
8a603e13
SA
901 assert(logic_mux_data_);
902
65efd025
SA
903 // Create initial logic mux segment
904 shared_ptr<LogicSegment> output_segment =
85a70280
SA
905 make_shared<LogicSegment>(*logic_mux_data_, segment_id,
906 logic_mux_unit_size_, 0);
65efd025 907 logic_mux_data_->push_segment(output_segment);
8a603e13
SA
908
909 output_segment->set_samplerate(get_input_samplerate(0));
ed535cd7 910
27a3f09b 911 do {
ed535cd7 912 const uint64_t input_sample_count = get_working_sample_count(segment_id);
8a603e13 913 const uint64_t output_sample_count = output_segment->get_sample_count();
27a3f09b
SA
914
915 const uint64_t samples_to_process =
916 (input_sample_count > output_sample_count) ?
917 (input_sample_count - output_sample_count) : 0;
918
919 // Process the samples if necessary...
920 if (samples_to_process > 0) {
8a603e13 921 const uint64_t unit_size = output_segment->unit_size();
27a3f09b
SA
922 const uint64_t chunk_sample_count = DecodeChunkLength / unit_size;
923
924 uint64_t processed_samples = 0;
925 do {
926 const uint64_t start_sample = output_sample_count + processed_samples;
927 const uint64_t sample_count =
928 min(samples_to_process - processed_samples, chunk_sample_count);
929
ed535cd7 930 mux_logic_samples(segment_id, start_sample, start_sample + sample_count);
27a3f09b
SA
931 processed_samples += sample_count;
932
933 // ...and process the newly muxed logic data
934 decode_input_cond_.notify_one();
3b5282a2 935 } while (!logic_mux_interrupt_ && (processed_samples < samples_to_process));
27a3f09b
SA
936 }
937
1ec191ed 938 if (samples_to_process == 0) {
ed535cd7
SA
939 // TODO Optimize this by caching the input segment count and only
940 // querying it when the cached value was reached
941 if (segment_id < get_input_segment_count() - 1) {
942 // Process next segment
943 segment_id++;
944
65efd025 945 output_segment =
85a70280
SA
946 make_shared<LogicSegment>(*logic_mux_data_, segment_id,
947 logic_mux_unit_size_, 0);
65efd025 948 logic_mux_data_->push_segment(output_segment);
ba5f2186 949
8a603e13
SA
950 output_segment->set_samplerate(get_input_samplerate(segment_id));
951
ed535cd7
SA
952 } else {
953 // All segments have been processed
954 logic_mux_data_invalid_ = false;
955
956 // Wait for more input
957 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
958 logic_mux_cond_.wait(logic_mux_lock);
959 }
27a3f09b 960 }
1ae18301 961
1ec191ed 962 } while (!logic_mux_interrupt_);
47747218
SA
963}
964
965void DecodeSignal::decode_data(
8a603e13
SA
966 const int64_t abs_start_samplenum, const int64_t sample_count,
967 const shared_ptr<LogicSegment> input_segment)
47747218 968{
8a603e13 969 const int64_t unit_size = input_segment->unit_size();
27a3f09b 970 const int64_t chunk_sample_count = DecodeChunkLength / unit_size;
47747218
SA
971
972 for (int64_t i = abs_start_samplenum;
1ae18301
SA
973 error_message_.isEmpty() && !decode_interrupt_ &&
974 (i < (abs_start_samplenum + sample_count));
47747218
SA
975 i += chunk_sample_count) {
976
977 const int64_t chunk_end = min(i + chunk_sample_count,
978 abs_start_samplenum + sample_count);
979
e06cf18d
SA
980 {
981 lock_guard<mutex> lock(output_mutex_);
b82908ab
SA
982 // Update the sample count showing the samples including currently processed ones
983 segments_.at(current_segment_id_).samples_decoded_incl = chunk_end;
e06cf18d
SA
984 }
985
b82243f7
SA
986 int64_t data_size = (chunk_end - i) * unit_size;
987 uint8_t* chunk = new uint8_t[data_size];
8a603e13 988 input_segment->get_samples(i, chunk_end, chunk);
47747218 989
e91883bb 990 if (srd_session_send(srd_session_, i, chunk_end, chunk,
1ae18301 991 data_size, unit_size) != SRD_OK)
403c3e87 992 set_error_message(tr("Decoder reported an error"));
a3ebd556 993
47747218
SA
994 delete[] chunk;
995
b82908ab
SA
996 {
997 lock_guard<mutex> lock(output_mutex_);
998 // Now that all samples are processed, the exclusive sample count catches up
999 segments_.at(current_segment_id_).samples_decoded_excl = chunk_end;
1000 }
1001
1b56c646
SA
1002 // Notify the frontend that we processed some data and
1003 // possibly have new annotations as well
1004 new_annotations();
556259d2
SA
1005
1006 if (decode_paused_) {
1007 unique_lock<mutex> pause_wait_lock(decode_pause_mutex_);
1008 decode_pause_cond_.wait(pause_wait_lock);
1009 }
47747218
SA
1010 }
1011}
1012
1013void DecodeSignal::decode_proc()
1014{
65efd025
SA
1015 current_segment_id_ = 0;
1016
1017 // If there is no input data available yet, wait until it is or we're interrupted
1018 if (logic_mux_data_->logic_segments().size() == 0) {
1019 unique_lock<mutex> input_wait_lock(input_mutex_);
1020 decode_input_cond_.wait(input_wait_lock);
1021 }
a3ebd556
SA
1022
1023 if (decode_interrupt_)
1024 return;
1025
8a603e13 1026 shared_ptr<LogicSegment> input_segment = logic_mux_data_->logic_segments().front();
8a603e13 1027 assert(input_segment);
65efd025
SA
1028
1029 // Create the initial segment and set its sample rate so that we can pass it to SRD
1030 create_decode_segment();
8a603e13 1031 segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
65efd025 1032 segments_.at(current_segment_id_).start_time = input_segment->start_time();
8a603e13 1033
65efd025
SA
1034 start_srd_session();
1035
1036 uint64_t sample_count = 0;
27a3f09b 1037 uint64_t abs_start_samplenum = 0;
47747218 1038 do {
27a3f09b
SA
1039 // Keep processing new samples until we exhaust the input data
1040 do {
767bf4e7 1041 lock_guard<mutex> input_lock(input_mutex_);
8a603e13 1042 sample_count = input_segment->get_sample_count() - abs_start_samplenum;
27a3f09b
SA
1043
1044 if (sample_count > 0) {
8a603e13 1045 decode_data(abs_start_samplenum, sample_count, input_segment);
27a3f09b
SA
1046 abs_start_samplenum += sample_count;
1047 }
1b56c646
SA
1048 } while (error_message_.isEmpty() && (sample_count > 0) && !decode_interrupt_);
1049
8a603e13
SA
1050 if (error_message_.isEmpty() && !decode_interrupt_ && sample_count == 0) {
1051 if (current_segment_id_ < logic_mux_data_->logic_segments().size() - 1) {
1052 // Process next segment
1053 current_segment_id_++;
1054
1055 try {
1056 input_segment = logic_mux_data_->logic_segments().at(current_segment_id_);
30677c13 1057 } catch (out_of_range&) {
8a603e13
SA
1058 qDebug() << "Decode error for" << name() << ": no logic mux segment" \
1059 << current_segment_id_ << "in decode_proc(), mux segments size is" \
1060 << logic_mux_data_->logic_segments().size();
1061 return;
1062 }
1063 abs_start_samplenum = 0;
1064
65efd025
SA
1065 // Create the next segment and set its metadata
1066 create_decode_segment();
8a603e13 1067 segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
65efd025 1068 segments_.at(current_segment_id_).start_time = input_segment->start_time();
8a603e13 1069
90ca4cc8
SA
1070 // Reset decoder state but keep the decoder stack intact
1071 terminate_srd_session();
8a603e13
SA
1072 } else {
1073 // All segments have been processed
1b56c646 1074 decode_finished();
27a3f09b 1075
8a603e13
SA
1076 // Wait for new input data or an interrupt was requested
1077 unique_lock<mutex> input_wait_lock(input_mutex_);
1078 decode_input_cond_.wait(input_wait_lock);
1079 }
27a3f09b
SA
1080 }
1081 } while (error_message_.isEmpty() && !decode_interrupt_);
ce0b6a40
GS
1082
1083 // Potentially reap decoders when the application no longer is
1084 // interested in their (pending) results.
1085 if (decode_interrupt_)
1086 terminate_srd_session();
e91883bb
SA
1087}
1088
1089void DecodeSignal::start_srd_session()
1090{
8ce0e732
SA
1091 // If there were stack changes, the session has been destroyed by now, so if
1092 // it hasn't been destroyed, we can just reset and re-use it
ce0b6a40
GS
1093 if (srd_session_) {
1094 // When a decoder stack was created before, re-use it
1095 // for the next stream of input data, after terminating
1096 // potentially still executing operations, and resetting
1097 // internal state. Skip the rather expensive (teardown
1098 // and) construction of another decoder stack.
1099
1100 // TODO Reduce redundancy, use a common code path for
72486b78 1101 // the meta/start sequence?
ce0b6a40 1102 terminate_srd_session();
72486b78
SA
1103
1104 // Metadata is cleared also, so re-set it
ba5b52e1 1105 uint64_t samplerate = 0;
7dd518b8 1106 if (segments_.size() > 0)
ba5b52e1
GS
1107 samplerate = segments_.at(current_segment_id_).samplerate;
1108 if (samplerate)
7dd518b8 1109 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
ba5b52e1 1110 g_variant_new_uint64(samplerate));
72486b78
SA
1111 for (const shared_ptr<decode::Decoder> &dec : stack_)
1112 dec->apply_all_options();
ce0b6a40 1113 srd_session_start(srd_session_);
72486b78 1114
ce0b6a40
GS
1115 return;
1116 }
e91883bb 1117
a3ebd556
SA
1118 // Create the session
1119 srd_session_new(&srd_session_);
1120 assert(srd_session_);
47747218 1121
a3ebd556
SA
1122 // Create the decoders
1123 srd_decoder_inst *prev_di = nullptr;
1124 for (const shared_ptr<decode::Decoder> &dec : stack_) {
1125 srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_);
e91883bb 1126
a3ebd556 1127 if (!di) {
403c3e87 1128 set_error_message(tr("Failed to create decoder instance"));
a3ebd556 1129 srd_session_destroy(srd_session_);
c5f9553c 1130 srd_session_ = nullptr;
a3ebd556
SA
1131 return;
1132 }
e91883bb 1133
a3ebd556
SA
1134 if (prev_di)
1135 srd_inst_stack(srd_session_, prev_di, di);
e91883bb 1136
a3ebd556 1137 prev_di = di;
e91883bb 1138 }
a3ebd556
SA
1139
1140 // Start the session
7dd518b8
SA
1141 if (segments_.size() > 0)
1142 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
1143 g_variant_new_uint64(segments_.at(current_segment_id_).samplerate));
a3ebd556
SA
1144
1145 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN,
1146 DecodeSignal::annotation_callback, this);
1147
1148 srd_session_start(srd_session_);
e3202557
SA
1149
1150 // We just recreated the srd session, so all stack changes are applied now
1151 stack_config_changed_ = false;
e91883bb 1152}
47747218 1153
ce0b6a40
GS
1154void DecodeSignal::terminate_srd_session()
1155{
1156 // Call the "terminate and reset" routine for the decoder stack
1157 // (if available). This does not harm those stacks which already
1158 // have completed their operation, and reduces response time for
1159 // those stacks which still are processing data while the
1160 // application no longer wants them to.
72486b78 1161 if (srd_session_) {
ce0b6a40 1162 srd_session_terminate_reset(srd_session_);
72486b78
SA
1163
1164 // Metadata is cleared also, so re-set it
ba5b52e1 1165 uint64_t samplerate = 0;
7dd518b8 1166 if (segments_.size() > 0)
ba5b52e1
GS
1167 samplerate = segments_.at(current_segment_id_).samplerate;
1168 if (samplerate)
7dd518b8 1169 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
ba5b52e1 1170 g_variant_new_uint64(samplerate));
72486b78
SA
1171 for (const shared_ptr<decode::Decoder> &dec : stack_)
1172 dec->apply_all_options();
1173 }
ce0b6a40
GS
1174}
1175
e91883bb
SA
1176void DecodeSignal::stop_srd_session()
1177{
1178 if (srd_session_) {
1179 // Destroy the session
1180 srd_session_destroy(srd_session_);
1181 srd_session_ = nullptr;
8ce0e732
SA
1182
1183 // Mark the decoder instances as non-existant since they were deleted
1184 for (const shared_ptr<decode::Decoder> &dec : stack_)
1185 dec->invalidate_decoder_inst();
e91883bb 1186 }
47747218
SA
1187}
1188
a8a9222d
SA
1189void DecodeSignal::connect_input_notifiers()
1190{
1191 // Disconnect the notification slot from the previous set of signals
1b56c646 1192 disconnect(this, SLOT(on_data_cleared()));
a8a9222d
SA
1193 disconnect(this, SLOT(on_data_received()));
1194
1195 // Connect the currently used signals to our slot
1196 for (data::DecodeChannel &ch : channels_) {
1197 if (!ch.assigned_signal)
1198 continue;
1199
1b56c646
SA
1200 const data::SignalBase *signal = ch.assigned_signal;
1201 connect(signal, SIGNAL(samples_cleared()),
1202 this, SLOT(on_data_cleared()));
b10b58f4 1203 connect(signal, SIGNAL(samples_added(uint64_t, uint64_t, uint64_t)),
a8a9222d
SA
1204 this, SLOT(on_data_received()));
1205 }
1206}
1207
65efd025 1208void DecodeSignal::create_decode_segment()
4913560f 1209{
65efd025
SA
1210 // Create annotation segment
1211 segments_.emplace_back(DecodeSegment());
72435789 1212
65efd025
SA
1213 // Add annotation classes
1214 for (const shared_ptr<decode::Decoder> &dec : stack_) {
1215 assert(dec);
1216 const srd_decoder *const decc = dec->decoder();
1217 assert(dec->decoder());
4913560f 1218
f9807084 1219 int row_index = 0;
65efd025
SA
1220 // Add a row for the decoder if it doesn't have a row list
1221 if (!decc->annotation_rows)
f9807084 1222 (segments_.back().annotation_rows)[Row(row_index++, decc)] =
65efd025 1223 decode::RowData();
4913560f 1224
65efd025
SA
1225 // Add the decoder rows
1226 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
1227 const srd_decoder_annotation_row *const ann_row =
1228 (srd_decoder_annotation_row *)l->data;
1229 assert(ann_row);
4913560f 1230
f9807084 1231 const Row row(row_index++, decc, ann_row);
4913560f 1232
65efd025
SA
1233 // Add a new empty row data object
1234 (segments_.back().annotation_rows)[row] =
1235 decode::RowData();
4913560f
SA
1236 }
1237 }
1238}
1239
47747218
SA
1240void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
1241{
1242 assert(pdata);
b21501d6 1243 assert(decode_signal);
47747218
SA
1244
1245 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
1246 assert(ds);
1247
3b5282a2
SA
1248 if (ds->decode_interrupt_)
1249 return;
1250
47747218
SA
1251 lock_guard<mutex> lock(ds->output_mutex_);
1252
47747218
SA
1253 // Find the row
1254 assert(pdata->pdo);
1255 assert(pdata->pdo->di);
1256 const srd_decoder *const decc = pdata->pdo->di->decoder;
1257 assert(decc);
1258
1becee23
UH
1259 const srd_proto_data_annotation *const pda =
1260 (const srd_proto_data_annotation*)pdata->data;
1261 assert(pda);
1262
8a603e13 1263 auto row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.end();
47747218
SA
1264
1265 // Try looking up the sub-row of this class
1becee23
UH
1266 const auto format = pda->ann_class;
1267 const auto r = ds->class_rows_.find(make_pair(decc, format));
47747218 1268 if (r != ds->class_rows_.end())
8a603e13 1269 row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.find((*r).second);
47747218
SA
1270 else {
1271 // Failing that, use the decoder as a key
f9807084 1272 row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.find(Row(0, decc));
47747218
SA
1273 }
1274
8a603e13 1275 if (row_iter == ds->segments_.at(ds->current_segment_id_).annotation_rows.end()) {
47747218 1276 qDebug() << "Unexpected annotation: decoder = " << decc <<
1becee23 1277 ", format = " << format;
47747218
SA
1278 assert(false);
1279 return;
1280 }
1281
1282 // Add the annotation
5a914348 1283 (*row_iter).second.emplace_annotation(pdata, &((*row_iter).first));
47747218
SA
1284}
1285
1286void DecodeSignal::on_capture_state_changed(int state)
1287{
1288 // If a new acquisition was started, we need to start decoding from scratch
ba5f2186
SA
1289 if (state == Session::Running) {
1290 logic_mux_data_invalid_ = true;
47747218 1291 begin_decode();
ba5f2186 1292 }
47747218
SA
1293}
1294
1b56c646
SA
1295void DecodeSignal::on_data_cleared()
1296{
1297 reset_decode();
1298}
1299
47747218
SA
1300void DecodeSignal::on_data_received()
1301{
5620539c
SA
1302 // If we detected a lack of input data when trying to start decoding,
1303 // we have set an error message. Only try again if we now have data
1304 // to work with
1305 if ((!error_message_.isEmpty()) && (get_input_segment_count() == 0))
1306 return;
1307
1b56c646
SA
1308 if (!logic_mux_thread_.joinable())
1309 begin_decode();
1310 else
1311 logic_mux_cond_.notify_one();
47747218
SA
1312}
1313
ad908057
SA
1314} // namespace data
1315} // namespace pv