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