]> sigrok.org Git - pulseview.git/blame - pv/data/decodesignal.cpp
Fix #832 by saving/restoring the decoder stack without settings
[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
0c5fe73e
SA
20#include <limits>
21
47747218
SA
22#include <QDebug>
23
ad908057
SA
24#include "logic.hpp"
25#include "logicsegment.hpp"
26#include "decodesignal.hpp"
27#include "signaldata.hpp"
28
29#include <pv/binding/decoder.hpp>
30#include <pv/data/decode/decoder.hpp>
ecd07c20 31#include <pv/data/decode/row.hpp>
ad908057
SA
32#include <pv/session.hpp>
33
47747218
SA
34using std::lock_guard;
35using std::make_pair;
ad908057 36using std::make_shared;
0c5fe73e 37using std::min;
ad908057 38using std::shared_ptr;
47747218
SA
39using std::unique_lock;
40using pv::data::decode::Annotation;
ad908057 41using pv::data::decode::Decoder;
ecd07c20 42using pv::data::decode::Row;
ad908057
SA
43
44namespace pv {
45namespace data {
46
47747218
SA
47const double DecodeSignal::DecodeMargin = 1.0;
48const double DecodeSignal::DecodeThreshold = 0.2;
3fbddf7f 49const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024;
ad908057 50
47747218 51mutex DecodeSignal::global_srd_mutex_;
9f97b357 52
ad908057 53
47747218
SA
54DecodeSignal::DecodeSignal(pv::Session &session) :
55 SignalBase(nullptr, SignalBase::DecodeChannel),
56 session_(session),
e91883bb 57 srd_session_(nullptr),
47747218
SA
58 logic_mux_data_invalid_(false),
59 start_time_(0),
60 samplerate_(0),
47747218
SA
61 samples_decoded_(0),
62 frame_complete_(false)
ad908057 63{
47747218
SA
64 connect(&session_, SIGNAL(capture_state_changed(int)),
65 this, SLOT(on_capture_state_changed(int)));
47747218
SA
66
67 set_name(tr("Empty decoder signal"));
ad908057
SA
68}
69
47747218 70DecodeSignal::~DecodeSignal()
ad908057 71{
1b56c646 72 reset_decode();
ad908057
SA
73}
74
47747218 75const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const
ecd07c20 76{
47747218 77 return stack_;
ecd07c20
SA
78}
79
c8e60bdf 80void DecodeSignal::stack_decoder(const srd_decoder *decoder)
ad908057
SA
81{
82 assert(decoder);
47747218
SA
83 stack_.push_back(make_shared<decode::Decoder>(decoder));
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
SA
91
92 auto_assign_signals();
27a3f09b 93 commit_decoder_channels();
47747218 94 begin_decode();
ad908057
SA
95}
96
97void DecodeSignal::remove_decoder(int index)
98{
47747218
SA
99 assert(index >= 0);
100 assert(index < (int)stack_.size());
101
102 // Find the decoder in the stack
103 auto iter = stack_.begin();
104 for (int i = 0; i < index; i++, iter++)
105 assert(iter != stack_.end());
106
107 // Delete the element
108 stack_.erase(iter);
109
110 // Update channels and decoded data
9f97b357 111 update_channel_list();
47747218 112 begin_decode();
ad908057
SA
113}
114
115bool DecodeSignal::toggle_decoder_visibility(int index)
116{
47747218 117 auto iter = stack_.cbegin();
ad908057 118 for (int i = 0; i < index; i++, iter++)
47747218 119 assert(iter != stack_.end());
ad908057
SA
120
121 shared_ptr<Decoder> dec = *iter;
122
123 // Toggle decoder visibility
124 bool state = false;
125 if (dec) {
126 state = !dec->shown();
127 dec->show(state);
128 }
129
130 return state;
131}
132
47747218
SA
133void DecodeSignal::reset_decode()
134{
1b56c646
SA
135 if (decode_thread_.joinable()) {
136 decode_interrupt_ = true;
137 decode_input_cond_.notify_one();
138 decode_thread_.join();
139 }
140
141 if (logic_mux_thread_.joinable()) {
142 logic_mux_interrupt_ = true;
143 logic_mux_cond_.notify_one();
144 logic_mux_thread_.join();
145 }
146
e91883bb
SA
147 stop_srd_session();
148
47747218
SA
149 frame_complete_ = false;
150 samples_decoded_ = 0;
151 error_message_ = QString();
1b56c646 152
47747218
SA
153 rows_.clear();
154 class_rows_.clear();
1b56c646
SA
155
156 logic_mux_data_.reset();
157 logic_mux_data_invalid_ = true;
47747218
SA
158}
159
946b52e1
SA
160void DecodeSignal::begin_decode()
161{
47747218
SA
162 if (decode_thread_.joinable()) {
163 decode_interrupt_ = true;
164 decode_input_cond_.notify_one();
165 decode_thread_.join();
166 }
167
168 if (logic_mux_thread_.joinable()) {
169 logic_mux_interrupt_ = true;
170 logic_mux_cond_.notify_one();
171 logic_mux_thread_.join();
172 }
173
174 reset_decode();
175
27a3f09b
SA
176 if (stack_.size() == 0) {
177 error_message_ = tr("No decoders");
178 return;
179 }
180
181 assert(channels_.size() > 0);
182
183 if (get_assigned_signal_count() == 0) {
184 error_message_ = tr("There are no channels assigned to this decoder");
185 return;
186 }
187
04627946
SA
188 // Make sure that all assigned channels still provide logic data
189 // (can happen when a converted signal was assigned but the
190 // conversion removed in the meanwhile)
191 for (data::DecodeChannel &ch : channels_)
192 if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr))
193 ch.assigned_signal = nullptr;
194
47747218
SA
195 // Check that all decoders have the required channels
196 for (const shared_ptr<decode::Decoder> &dec : stack_)
197 if (!dec->have_required_channels()) {
198 error_message_ = tr("One or more required channels "
199 "have not been specified");
200 return;
201 }
202
203 // Add annotation classes
204 for (const shared_ptr<decode::Decoder> &dec : stack_) {
205 assert(dec);
206 const srd_decoder *const decc = dec->decoder();
207 assert(dec->decoder());
208
209 // Add a row for the decoder if it doesn't have a row list
210 if (!decc->annotation_rows)
211 rows_[Row(decc)] = decode::RowData();
212
213 // Add the decoder rows
214 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
215 const srd_decoder_annotation_row *const ann_row =
216 (srd_decoder_annotation_row *)l->data;
217 assert(ann_row);
218
219 const Row row(decc, ann_row);
220
221 // Add a new empty row data object
222 rows_[row] = decode::RowData();
223
224 // Map out all the classes
225 for (const GSList *ll = ann_row->ann_classes;
226 ll; ll = ll->next)
227 class_rows_[make_pair(decc,
228 GPOINTER_TO_INT(ll->data))] = row;
229 }
230 }
231
27a3f09b
SA
232 // Free the logic data and its segment(s) if it needs to be updated
233 if (logic_mux_data_invalid_)
234 logic_mux_data_.reset();
235
236 if (!logic_mux_data_) {
237 const int64_t ch_count = get_assigned_signal_count();
238 const int64_t unit_size = (ch_count + 7) / 8;
239 logic_mux_data_ = make_shared<Logic>(ch_count);
240 segment_ = make_shared<LogicSegment>(*logic_mux_data_, unit_size, samplerate_);
241 logic_mux_data_->push_segment(segment_);
242 }
47747218 243
27a3f09b
SA
244 // Make sure the logic output data is complete and up-to-date
245 logic_mux_interrupt_ = false;
246 logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this);
247
248 // Decode the muxed logic data
47747218
SA
249 decode_interrupt_ = false;
250 decode_thread_ = std::thread(&DecodeSignal::decode_proc, this);
a8a9222d
SA
251
252 // Receive notifications when new sample data is available
253 connect_input_notifiers();
946b52e1
SA
254}
255
ecd07c20
SA
256QString DecodeSignal::error_message() const
257{
47747218
SA
258 lock_guard<mutex> lock(output_mutex_);
259 return error_message_;
ecd07c20
SA
260}
261
47747218 262const vector<data::DecodeChannel> DecodeSignal::get_channels() const
9f97b357
SA
263{
264 return channels_;
265}
266
132a5c6d
SA
267void DecodeSignal::auto_assign_signals()
268{
47747218
SA
269 bool new_assignment = false;
270
132a5c6d
SA
271 // Try to auto-select channels that don't have signals assigned yet
272 for (data::DecodeChannel &ch : channels_) {
273 if (ch.assigned_signal)
274 continue;
275
692f6093
SA
276 for (shared_ptr<data::SignalBase> s : session_.signalbases()) {
277 const QString ch_name = ch.name.toLower();
278 const QString s_name = s->name().toLower();
279
280 if (s->logic_data() &&
281 ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) {
132a5c6d 282 ch.assigned_signal = s.get();
47747218
SA
283 new_assignment = true;
284 }
692f6093 285 }
47747218
SA
286 }
287
288 if (new_assignment) {
289 logic_mux_data_invalid_ = true;
27a3f09b 290 commit_decoder_channels();
47747218 291 channels_updated();
132a5c6d
SA
292 }
293}
294
9f97b357
SA
295void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
296{
297 for (data::DecodeChannel &ch : channels_)
47747218 298 if (ch.id == channel_id) {
9f97b357 299 ch.assigned_signal = signal;
47747218
SA
300 logic_mux_data_invalid_ = true;
301 }
9f97b357 302
27a3f09b 303 commit_decoder_channels();
9f97b357 304 channels_updated();
47747218 305 begin_decode();
9f97b357
SA
306}
307
27a3f09b
SA
308int DecodeSignal::get_assigned_signal_count() const
309{
310 // Count all channels that have a signal assigned to them
311 return count_if(channels_.begin(), channels_.end(),
312 [](data::DecodeChannel ch) { return ch.assigned_signal; });
313}
314
9f97b357
SA
315void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
316{
317 for (data::DecodeChannel &ch : channels_)
318 if (ch.id == channel_id)
319 ch.initial_pin_state = init_state;
320
321 channels_updated();
322
47747218 323 begin_decode();
9f97b357
SA
324}
325
ff83d980
SA
326double DecodeSignal::samplerate() const
327{
47747218 328 return samplerate_;
ff83d980
SA
329}
330
331const pv::util::Timestamp& DecodeSignal::start_time() const
332{
47747218 333 return start_time_;
ff83d980
SA
334}
335
0c5fe73e
SA
336int64_t DecodeSignal::get_working_sample_count() const
337{
338 // The working sample count is the highest sample number for
339 // which all used signals have data available, so go through
340 // all channels and use the lowest overall sample count of the
341 // current segment
342
343 // TODO Currently, we assume only a single segment exists
344
345 int64_t count = std::numeric_limits<int64_t>::max();
346 bool no_signals_assigned = true;
347
348 for (const data::DecodeChannel &ch : channels_)
349 if (ch.assigned_signal) {
350 no_signals_assigned = false;
351
352 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
353 if (!logic_data || logic_data->logic_segments().empty())
354 return 0;
355
356 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
357 count = min(count, (int64_t)segment->get_sample_count());
358 }
359
360 return (no_signals_assigned ? 0 : count);
361}
362
363int64_t DecodeSignal::get_decoded_sample_count() const
ff83d980 364{
47747218
SA
365 lock_guard<mutex> decode_lock(output_mutex_);
366 return samples_decoded_;
ff83d980
SA
367}
368
ecd07c20
SA
369vector<Row> DecodeSignal::visible_rows() const
370{
47747218
SA
371 lock_guard<mutex> lock(output_mutex_);
372
373 vector<Row> rows;
374
375 for (const shared_ptr<decode::Decoder> &dec : stack_) {
376 assert(dec);
377 if (!dec->shown())
378 continue;
379
380 const srd_decoder *const decc = dec->decoder();
381 assert(dec->decoder());
382
383 // Add a row for the decoder if it doesn't have a row list
384 if (!decc->annotation_rows)
385 rows.emplace_back(decc);
386
387 // Add the decoder rows
388 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
389 const srd_decoder_annotation_row *const ann_row =
390 (srd_decoder_annotation_row *)l->data;
391 assert(ann_row);
392 rows.emplace_back(decc, ann_row);
393 }
394 }
395
396 return rows;
ecd07c20
SA
397}
398
399void DecodeSignal::get_annotation_subset(
400 vector<pv::data::decode::Annotation> &dest,
401 const decode::Row &row, uint64_t start_sample,
402 uint64_t end_sample) const
403{
47747218
SA
404 lock_guard<mutex> lock(output_mutex_);
405
406 const auto iter = rows_.find(row);
407 if (iter != rows_.end())
408 (*iter).second.get_annotation_subset(dest,
409 start_sample, end_sample);
410}
411
412void DecodeSignal::save_settings(QSettings &settings) const
413{
414 SignalBase::save_settings(settings);
415
c8e60bdf
SA
416 settings.setValue("decoders", (int)(stack_.size()));
417
418 int decoder_idx = 0;
419 for (shared_ptr<decode::Decoder> decoder : stack_) {
420 settings.beginGroup("decoder" + QString::number(decoder_idx++));
421
422 settings.setValue("id", decoder->decoder()->id);
423
424 settings.endGroup();
425 }
426
427 // TODO Save channel mapping and decoder options
47747218
SA
428}
429
430void DecodeSignal::restore_settings(QSettings &settings)
431{
432 SignalBase::restore_settings(settings);
433
c8e60bdf
SA
434 GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list());
435
436 int decoders = settings.value("decoders").toInt();
437
438 for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) {
439 settings.beginGroup("decoder" + QString::number(decoder_idx));
440
441 QString id = settings.value("id").toString();
442
443 for (GSList *entry = dec_list; entry; entry = entry->next) {
444 const srd_decoder *dec = (srd_decoder*)entry->data;
445 if (!dec)
446 continue;
447
448 if (QString::fromUtf8(dec->id) == id) {
449 stack_.push_back(make_shared<decode::Decoder>(dec));
450
451 // Include the newly created decode channels in the channel lists
452 update_channel_list();
453 break;
454 }
455 }
456
457 settings.endGroup();
458 }
459
460 // TODO Restore channel mapping and decoder options
47747218
SA
461}
462
9f97b357
SA
463void DecodeSignal::update_channel_list()
464{
47747218 465 vector<data::DecodeChannel> prev_channels = channels_;
9f97b357
SA
466 channels_.clear();
467
468 uint16_t id = 0;
469
470 // Copy existing entries, create new as needed
47747218 471 for (shared_ptr<Decoder> decoder : stack_) {
9f97b357
SA
472 const srd_decoder* srd_d = decoder->decoder();
473 const GSList *l;
474
475 // Mandatory channels
476 for (l = srd_d->channels; l; l = l->next) {
477 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
478 bool ch_added = false;
479
480 // Copy but update ID if this channel was in the list before
47747218 481 for (data::DecodeChannel &ch : prev_channels)
9f97b357
SA
482 if (ch.pdch_ == pdch) {
483 ch.id = id++;
484 channels_.push_back(ch);
485 ch_added = true;
486 break;
487 }
488
489 if (!ch_added) {
490 // Create new entry without a mapped signal
491 data::DecodeChannel ch = {id++, false, nullptr,
492 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
493 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
494 channels_.push_back(ch);
495 }
496 }
497
498 // Optional channels
499 for (l = srd_d->opt_channels; l; l = l->next) {
500 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
501 bool ch_added = false;
502
503 // Copy but update ID if this channel was in the list before
47747218 504 for (data::DecodeChannel &ch : prev_channels)
9f97b357
SA
505 if (ch.pdch_ == pdch) {
506 ch.id = id++;
507 channels_.push_back(ch);
508 ch_added = true;
509 break;
510 }
511
512 if (!ch_added) {
513 // Create new entry without a mapped signal
514 data::DecodeChannel ch = {id++, true, nullptr,
515 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
516 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
517 channels_.push_back(ch);
518 }
519 }
520 }
521
47747218
SA
522 // Invalidate the logic output data if the channel assignment changed
523 if (prev_channels.size() != channels_.size()) {
524 // The number of channels changed, there's definitely a difference
525 logic_mux_data_invalid_ = true;
526 } else {
527 // Same number but assignment may still differ, so compare all channels
528 for (size_t i = 0; i < channels_.size(); i++) {
529 const data::DecodeChannel &p_ch = prev_channels[i];
530 const data::DecodeChannel &ch = channels_[i];
531
532 if ((p_ch.pdch_ != ch.pdch_) ||
533 (p_ch.assigned_signal != ch.assigned_signal)) {
534 logic_mux_data_invalid_ = true;
535 break;
536 }
537 }
538
539 }
540
9f97b357
SA
541 channels_updated();
542}
543
27a3f09b 544void DecodeSignal::commit_decoder_channels()
ad908057 545{
27a3f09b
SA
546 // Submit channel list to every decoder, containing only the relevant channels
547 for (shared_ptr<decode::Decoder> dec : stack_) {
548 vector<data::DecodeChannel*> channel_list;
549
550 for (data::DecodeChannel &ch : channels_)
551 if (ch.decoder_ == dec)
552 channel_list.push_back(&ch);
47747218 553
27a3f09b
SA
554 dec->set_channels(channel_list);
555 }
47747218
SA
556}
557
27a3f09b 558void DecodeSignal::mux_logic_samples(const int64_t start, const int64_t end)
47747218 559{
27a3f09b
SA
560 // Enforce end to be greater than start
561 if (end <= start)
562 return;
563
564 // Fetch all segments and their data
565 // TODO Currently, we assume only a single segment exists
566 vector<shared_ptr<LogicSegment> > segments;
567 vector<const uint8_t*> signal_data;
568 vector<uint8_t> signal_in_bytepos;
569 vector<uint8_t> signal_in_bitpos;
570
571 for (data::DecodeChannel &ch : channels_)
572 if (ch.assigned_signal) {
573 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
574 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
575 segments.push_back(segment);
576 signal_data.push_back(segment->get_samples(start, end));
577
578 const int bitpos = ch.assigned_signal->logic_bit_index();
579 signal_in_bytepos.push_back(bitpos / 8);
580 signal_in_bitpos.push_back(bitpos % 8);
581 }
582
583 // Perform the muxing of signal data into the output data
584 uint8_t* output = new uint8_t[(end - start) * segment_->unit_size()];
585 unsigned int signal_count = signal_data.size();
586
587 for (int64_t sample_cnt = 0; sample_cnt < (end - start); sample_cnt++) {
588 int bitpos = 0;
589 uint8_t bytepos = 0;
590
591 const int out_sample_pos = sample_cnt * segment_->unit_size();
592 for (unsigned int i = 0; i < segment_->unit_size(); i++)
593 output[out_sample_pos + i] = 0;
47747218 594
27a3f09b
SA
595 for (unsigned int i = 0; i < signal_count; i++) {
596 const int in_sample_pos = sample_cnt * segments[i]->unit_size();
597 const uint8_t in_sample = 1 &
598 ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i]));
47747218 599
27a3f09b
SA
600 const uint8_t out_sample = output[out_sample_pos + bytepos];
601
602 output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos);
603
604 bitpos++;
605 if (bitpos > 7) {
606 bitpos = 0;
607 bytepos++;
608 }
609 }
47747218
SA
610 }
611
27a3f09b
SA
612 segment_->append_payload(output, (end - start) * segment_->unit_size());
613 delete[] output;
614
615 for (const uint8_t* data : signal_data)
616 delete[] data;
617}
618
619void DecodeSignal::logic_mux_proc()
620{
621 do {
27a3f09b
SA
622 const uint64_t input_sample_count = get_working_sample_count();
623 const uint64_t output_sample_count = segment_->get_sample_count();
624
625 const uint64_t samples_to_process =
626 (input_sample_count > output_sample_count) ?
627 (input_sample_count - output_sample_count) : 0;
628
629 // Process the samples if necessary...
630 if (samples_to_process > 0) {
631 const uint64_t unit_size = segment_->unit_size();
632 const uint64_t chunk_sample_count = DecodeChunkLength / unit_size;
633
634 uint64_t processed_samples = 0;
635 do {
636 const uint64_t start_sample = output_sample_count + processed_samples;
637 const uint64_t sample_count =
638 min(samples_to_process - processed_samples, chunk_sample_count);
639
640 mux_logic_samples(start_sample, start_sample + sample_count);
641 processed_samples += sample_count;
642
643 // ...and process the newly muxed logic data
644 decode_input_cond_.notify_one();
645 } while (processed_samples < samples_to_process);
646 }
647
1ec191ed 648 if (samples_to_process == 0) {
27a3f09b
SA
649 // Wait for more input
650 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
651 logic_mux_cond_.wait(logic_mux_lock);
652 }
1ec191ed 653 } while (!logic_mux_interrupt_);
27a3f09b
SA
654
655 // No more input data and session is stopped, let the decode thread
656 // process any pending data, terminate and release the global SRD mutex
657 // in order to let other decoders run
658 decode_input_cond_.notify_one();
47747218
SA
659}
660
a3ebd556
SA
661void DecodeSignal::query_input_metadata()
662{
663 // Update the samplerate and start time because we cannot start
664 // the libsrd session without the current samplerate
665
666 // TODO Currently we assume all channels have the same sample rate
667 // and start time
668 bool samplerate_valid = false;
669
670 auto any_channel = find_if(channels_.begin(), channels_.end(),
671 [](data::DecodeChannel ch) { return ch.assigned_signal; });
672
673 shared_ptr<Logic> logic_data =
674 any_channel->assigned_signal->logic_data();
675
676 do {
677 if (!logic_data->logic_segments().empty()) {
678 shared_ptr<LogicSegment> first_segment =
679 any_channel->assigned_signal->logic_data()->logic_segments().front();
680 start_time_ = first_segment->start_time();
681 samplerate_ = first_segment->samplerate();
682 if (samplerate_ > 0)
683 samplerate_valid = true;
684 }
685
1ec191ed
SA
686 if (!samplerate_valid) {
687 // Wait until input data is available or an interrupt was requested
688 unique_lock<mutex> input_wait_lock(input_mutex_);
689 decode_input_cond_.wait(input_wait_lock);
690 }
a3ebd556
SA
691 } while (!samplerate_valid && !decode_interrupt_);
692}
693
47747218 694void DecodeSignal::decode_data(
e91883bb 695 const int64_t abs_start_samplenum, const int64_t sample_count)
47747218 696{
27a3f09b
SA
697 const int64_t unit_size = segment_->unit_size();
698 const int64_t chunk_sample_count = DecodeChunkLength / unit_size;
47747218
SA
699
700 for (int64_t i = abs_start_samplenum;
701 !decode_interrupt_ && (i < (abs_start_samplenum + sample_count));
702 i += chunk_sample_count) {
703
704 const int64_t chunk_end = min(i + chunk_sample_count,
705 abs_start_samplenum + sample_count);
706
707 const uint8_t* chunk = segment_->get_samples(i, chunk_end);
708
e91883bb 709 if (srd_session_send(srd_session_, i, chunk_end, chunk,
47747218
SA
710 (chunk_end - i) * unit_size, unit_size) != SRD_OK) {
711 error_message_ = tr("Decoder reported an error");
712 delete[] chunk;
713 break;
714 }
a3ebd556 715
47747218
SA
716 delete[] chunk;
717
718 {
719 lock_guard<mutex> lock(output_mutex_);
720 samples_decoded_ = chunk_end;
721 }
1b56c646
SA
722
723 // Notify the frontend that we processed some data and
724 // possibly have new annotations as well
725 new_annotations();
47747218
SA
726 }
727}
728
729void DecodeSignal::decode_proc()
730{
a3ebd556
SA
731 query_input_metadata();
732
733 if (decode_interrupt_)
734 return;
735
e91883bb 736 start_srd_session();
47747218 737
27a3f09b
SA
738 uint64_t sample_count;
739 uint64_t abs_start_samplenum = 0;
47747218 740 do {
27a3f09b
SA
741 // Keep processing new samples until we exhaust the input data
742 do {
e91883bb
SA
743 // Prevent any other decode threads from accessing libsigrokdecode
744 lock_guard<mutex> srd_lock(global_srd_mutex_);
745
27a3f09b
SA
746 {
747 lock_guard<mutex> input_lock(input_mutex_);
748 sample_count = segment_->get_sample_count() - abs_start_samplenum;
749 }
750
751 if (sample_count > 0) {
e91883bb 752 decode_data(abs_start_samplenum, sample_count);
27a3f09b
SA
753 abs_start_samplenum += sample_count;
754 }
1b56c646
SA
755 } while (error_message_.isEmpty() && (sample_count > 0) && !decode_interrupt_);
756
757 if (error_message_.isEmpty() && !decode_interrupt_) {
758 if (sample_count == 0)
759 decode_finished();
27a3f09b 760
a3ebd556 761 // Wait for new input data or an interrupt was requested
27a3f09b
SA
762 unique_lock<mutex> input_wait_lock(input_mutex_);
763 decode_input_cond_.wait(input_wait_lock);
764 }
765 } while (error_message_.isEmpty() && !decode_interrupt_);
e91883bb
SA
766}
767
768void DecodeSignal::start_srd_session()
769{
a3ebd556
SA
770 if (srd_session_)
771 stop_srd_session();
e91883bb 772
a3ebd556
SA
773 // Create the session
774 srd_session_new(&srd_session_);
775 assert(srd_session_);
47747218 776
a3ebd556
SA
777 // Create the decoders
778 srd_decoder_inst *prev_di = nullptr;
779 for (const shared_ptr<decode::Decoder> &dec : stack_) {
780 srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_);
e91883bb 781
a3ebd556
SA
782 if (!di) {
783 error_message_ = tr("Failed to create decoder instance");
784 srd_session_destroy(srd_session_);
785 return;
786 }
e91883bb 787
a3ebd556
SA
788 if (prev_di)
789 srd_inst_stack(srd_session_, prev_di, di);
e91883bb 790
a3ebd556 791 prev_di = di;
e91883bb 792 }
a3ebd556
SA
793
794 // Start the session
795 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
796 g_variant_new_uint64(samplerate_));
797
798 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN,
799 DecodeSignal::annotation_callback, this);
800
801 srd_session_start(srd_session_);
e91883bb 802}
47747218 803
e91883bb
SA
804void DecodeSignal::stop_srd_session()
805{
806 if (srd_session_) {
807 // Destroy the session
808 srd_session_destroy(srd_session_);
809 srd_session_ = nullptr;
810 }
47747218
SA
811}
812
a8a9222d
SA
813void DecodeSignal::connect_input_notifiers()
814{
815 // Disconnect the notification slot from the previous set of signals
1b56c646 816 disconnect(this, SLOT(on_data_cleared()));
a8a9222d
SA
817 disconnect(this, SLOT(on_data_received()));
818
819 // Connect the currently used signals to our slot
820 for (data::DecodeChannel &ch : channels_) {
821 if (!ch.assigned_signal)
822 continue;
823
1b56c646
SA
824 const data::SignalBase *signal = ch.assigned_signal;
825 connect(signal, SIGNAL(samples_cleared()),
826 this, SLOT(on_data_cleared()));
827 connect(signal, SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
a8a9222d
SA
828 this, SLOT(on_data_received()));
829 }
830}
831
47747218
SA
832void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
833{
834 assert(pdata);
835 assert(decoder);
836
837 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
838 assert(ds);
839
840 lock_guard<mutex> lock(ds->output_mutex_);
841
842 const decode::Annotation a(pdata);
843
844 // Find the row
845 assert(pdata->pdo);
846 assert(pdata->pdo->di);
847 const srd_decoder *const decc = pdata->pdo->di->decoder;
848 assert(decc);
849
850 auto row_iter = ds->rows_.end();
851
852 // Try looking up the sub-row of this class
853 const auto r = ds->class_rows_.find(make_pair(decc, a.format()));
854 if (r != ds->class_rows_.end())
855 row_iter = ds->rows_.find((*r).second);
856 else {
857 // Failing that, use the decoder as a key
858 row_iter = ds->rows_.find(Row(decc));
859 }
860
861 assert(row_iter != ds->rows_.end());
862 if (row_iter == ds->rows_.end()) {
863 qDebug() << "Unexpected annotation: decoder = " << decc <<
864 ", format = " << a.format();
865 assert(false);
866 return;
867 }
868
869 // Add the annotation
870 (*row_iter).second.push_annotation(a);
47747218
SA
871}
872
873void DecodeSignal::on_capture_state_changed(int state)
874{
875 // If a new acquisition was started, we need to start decoding from scratch
876 if (state == Session::Running)
877 begin_decode();
878}
879
1b56c646
SA
880void DecodeSignal::on_data_cleared()
881{
882 reset_decode();
883}
884
47747218
SA
885void DecodeSignal::on_data_received()
886{
1b56c646
SA
887 if (!logic_mux_thread_.joinable())
888 begin_decode();
889 else
890 logic_mux_cond_.notify_one();
47747218
SA
891}
892
ad908057
SA
893} // namespace data
894} // namespace pv