2 * This file is part of the PulseView project.
4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
25 #include "logicsegment.hpp"
26 #include "decodesignal.hpp"
27 #include "signaldata.hpp"
29 #include <pv/binding/decoder.hpp>
30 #include <pv/data/decode/decoder.hpp>
31 #include <pv/data/decode/row.hpp>
32 #include <pv/globalsettings.hpp>
33 #include <pv/session.hpp>
35 using std::lock_guard;
37 using std::make_shared;
39 using std::out_of_range;
40 using std::shared_ptr;
41 using std::unique_lock;
42 using pv::data::decode::Annotation;
43 using pv::data::decode::Decoder;
44 using pv::data::decode::Row;
49 const double DecodeSignal::DecodeMargin = 1.0;
50 const double DecodeSignal::DecodeThreshold = 0.2;
51 const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024;
54 DecodeSignal::DecodeSignal(pv::Session &session) :
55 SignalBase(nullptr, SignalBase::DecodeChannel),
57 srd_session_(nullptr),
58 logic_mux_data_invalid_(false),
59 current_segment_id_(0)
61 connect(&session_, SIGNAL(capture_state_changed(int)),
62 this, SLOT(on_capture_state_changed(int)));
65 DecodeSignal::~DecodeSignal()
70 const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const
75 void DecodeSignal::stack_decoder(const srd_decoder *decoder)
78 const shared_ptr<Decoder> dec = make_shared<decode::Decoder>(decoder);
80 stack_.push_back(dec);
82 // Set name if this decoder is the first in the list
83 if (stack_.size() == 1)
84 set_name(QString::fromUtf8(decoder->name));
86 // Include the newly created decode channels in the channel lists
87 update_channel_list();
89 auto_assign_signals(dec);
90 commit_decoder_channels();
94 void DecodeSignal::remove_decoder(int index)
97 assert(index < (int)stack_.size());
99 // Find the decoder in the stack
100 auto iter = stack_.begin();
101 for (int i = 0; i < index; i++, iter++)
102 assert(iter != stack_.end());
104 // Delete the element
107 // Update channels and decoded data
108 update_channel_list();
112 bool DecodeSignal::toggle_decoder_visibility(int index)
114 auto iter = stack_.cbegin();
115 for (int i = 0; i < index; i++, iter++)
116 assert(iter != stack_.end());
118 shared_ptr<Decoder> dec = *iter;
120 // Toggle decoder visibility
123 state = !dec->shown();
130 void DecodeSignal::reset_decode()
132 terminate_srd_session();
134 if (decode_thread_.joinable()) {
135 decode_interrupt_ = true;
136 decode_input_cond_.notify_one();
137 decode_thread_.join();
140 if (logic_mux_thread_.joinable()) {
141 logic_mux_interrupt_ = true;
142 logic_mux_cond_.notify_one();
143 logic_mux_thread_.join();
149 current_segment_id_ = 0;
152 logic_mux_data_.reset();
153 logic_mux_data_invalid_ = true;
155 error_message_ = QString();
160 void DecodeSignal::begin_decode()
162 terminate_srd_session();
164 if (decode_thread_.joinable()) {
165 decode_interrupt_ = true;
166 decode_input_cond_.notify_one();
167 decode_thread_.join();
170 if (logic_mux_thread_.joinable()) {
171 logic_mux_interrupt_ = true;
172 logic_mux_cond_.notify_one();
173 logic_mux_thread_.join();
178 if (stack_.size() == 0) {
179 error_message_ = tr("No decoders");
183 assert(channels_.size() > 0);
185 if (get_assigned_signal_count() == 0) {
186 error_message_ = tr("There are no channels assigned to this decoder");
190 // Make sure that all assigned channels still provide logic data
191 // (can happen when a converted signal was assigned but the
192 // conversion removed in the meanwhile)
193 for (data::DecodeChannel &ch : channels_)
194 if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr))
195 ch.assigned_signal = nullptr;
197 // Check that all decoders have the required channels
198 for (const shared_ptr<decode::Decoder> &dec : stack_)
199 if (!dec->have_required_channels()) {
200 error_message_ = tr("One or more required channels "
201 "have not been specified");
205 // Map out all the annotation classes
206 for (const shared_ptr<decode::Decoder> &dec : stack_) {
208 const srd_decoder *const decc = dec->decoder();
209 assert(dec->decoder());
211 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
212 const srd_decoder_annotation_row *const ann_row =
213 (srd_decoder_annotation_row *)l->data;
216 const Row row(decc, ann_row);
218 for (const GSList *ll = ann_row->ann_classes;
220 class_rows_[make_pair(decc,
221 GPOINTER_TO_INT(ll->data))] = row;
225 // Free the logic data and its segment(s) if it needs to be updated
226 if (logic_mux_data_invalid_)
227 logic_mux_data_.reset();
229 if (!logic_mux_data_) {
230 const uint32_t ch_count = get_assigned_signal_count();
231 logic_mux_unit_size_ = (ch_count + 7) / 8;
232 logic_mux_data_ = make_shared<Logic>(ch_count);
235 // Receive notifications when new sample data is available
236 connect_input_notifiers();
238 if (get_input_segment_count() == 0) {
239 error_message_ = tr("No input data");
243 // Make sure the logic output data is complete and up-to-date
244 logic_mux_interrupt_ = false;
245 logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this);
247 // Decode the muxed logic data
248 decode_interrupt_ = false;
249 decode_thread_ = std::thread(&DecodeSignal::decode_proc, this);
252 QString DecodeSignal::error_message() const
254 lock_guard<mutex> lock(output_mutex_);
255 return error_message_;
258 const vector<data::DecodeChannel> DecodeSignal::get_channels() const
263 void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec)
265 bool new_assignment = false;
267 // Try to auto-select channels that don't have signals assigned yet
268 for (data::DecodeChannel &ch : channels_) {
269 // If a decoder is given, auto-assign only its channels
270 if (dec && (ch.decoder_ != dec))
273 if (ch.assigned_signal)
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();
280 if (s->logic_data() &&
281 ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) {
282 ch.assigned_signal = s.get();
283 new_assignment = true;
288 if (new_assignment) {
289 logic_mux_data_invalid_ = true;
290 commit_decoder_channels();
295 void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
297 for (data::DecodeChannel &ch : channels_)
298 if (ch.id == channel_id) {
299 ch.assigned_signal = signal;
300 logic_mux_data_invalid_ = true;
303 commit_decoder_channels();
308 int DecodeSignal::get_assigned_signal_count() const
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; });
315 void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
317 for (data::DecodeChannel &ch : channels_)
318 if (ch.id == channel_id)
319 ch.initial_pin_state = init_state;
326 double DecodeSignal::samplerate() const
330 // TODO For now, we simply return the first samplerate that we have
331 if (segments_.size() > 0)
332 result = segments_.front().samplerate;
337 const pv::util::Timestamp DecodeSignal::start_time() const
339 pv::util::Timestamp result;
341 // TODO For now, we simply return the first start time that we have
342 if (segments_.size() > 0)
343 result = segments_.front().start_time;
348 int64_t DecodeSignal::get_working_sample_count(uint32_t segment_id) const
350 // The working sample count is the highest sample number for
351 // which all used signals have data available, so go through all
352 // channels and use the lowest overall sample count of the segment
354 int64_t count = std::numeric_limits<int64_t>::max();
355 bool no_signals_assigned = true;
357 for (const data::DecodeChannel &ch : channels_)
358 if (ch.assigned_signal) {
359 no_signals_assigned = false;
361 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
362 if (!logic_data || logic_data->logic_segments().empty())
366 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id);
367 count = min(count, (int64_t)segment->get_sample_count());
368 } catch (out_of_range&) {
373 return (no_signals_assigned ? 0 : count);
376 int64_t DecodeSignal::get_decoded_sample_count(uint32_t segment_id) const
378 lock_guard<mutex> decode_lock(output_mutex_);
383 const DecodeSegment *segment = &(segments_.at(segment_id));
384 result = segment->samples_decoded;
385 } catch (out_of_range&) {
392 vector<Row> DecodeSignal::visible_rows() const
394 lock_guard<mutex> lock(output_mutex_);
398 for (const shared_ptr<decode::Decoder> &dec : stack_) {
403 const srd_decoder *const decc = dec->decoder();
404 assert(dec->decoder());
406 // Add a row for the decoder if it doesn't have a row list
407 if (!decc->annotation_rows)
408 rows.emplace_back(decc);
410 // Add the decoder rows
411 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
412 const srd_decoder_annotation_row *const ann_row =
413 (srd_decoder_annotation_row *)l->data;
415 rows.emplace_back(decc, ann_row);
422 void DecodeSignal::get_annotation_subset(
423 vector<pv::data::decode::Annotation> &dest,
424 const decode::Row &row, uint32_t segment_id, uint64_t start_sample,
425 uint64_t end_sample) const
427 lock_guard<mutex> lock(output_mutex_);
430 const DecodeSegment *segment = &(segments_.at(segment_id));
431 const map<const decode::Row, decode::RowData> *rows =
432 &(segment->annotation_rows);
434 const auto iter = rows->find(row);
435 if (iter != rows->end())
436 (*iter).second.get_annotation_subset(dest,
437 start_sample, end_sample);
438 } catch (out_of_range&) {
443 void DecodeSignal::save_settings(QSettings &settings) const
445 SignalBase::save_settings(settings);
447 settings.setValue("decoders", (int)(stack_.size()));
449 // Save decoder stack
451 for (shared_ptr<decode::Decoder> decoder : stack_) {
452 settings.beginGroup("decoder" + QString::number(decoder_idx++));
454 settings.setValue("id", decoder->decoder()->id);
456 // Save decoder options
457 const map<string, GVariant*>& options = decoder->options();
459 settings.setValue("options", (int)options.size());
461 // Note: decode::Decoder::options() returns only the options
462 // that differ from the default. See binding::Decoder::getter()
464 for (auto option : options) {
465 settings.beginGroup("option" + QString::number(i));
466 settings.setValue("name", QString::fromStdString(option.first));
467 GlobalSettings::store_gvariant(settings, option.second);
475 // Save channel mapping
476 settings.setValue("channels", (int)channels_.size());
478 for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) {
479 auto channel = find_if(channels_.begin(), channels_.end(),
480 [&](data::DecodeChannel ch) { return ch.id == channel_id; });
482 if (channel == channels_.end()) {
483 qDebug() << "ERROR: Gap in channel index:" << channel_id;
487 settings.beginGroup("channel" + QString::number(channel_id));
489 settings.setValue("name", channel->name); // Useful for debugging
490 settings.setValue("initial_pin_state", channel->initial_pin_state);
492 if (channel->assigned_signal)
493 settings.setValue("assigned_signal_name", channel->assigned_signal->name());
499 void DecodeSignal::restore_settings(QSettings &settings)
501 SignalBase::restore_settings(settings);
503 // Restore decoder stack
504 GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list());
506 int decoders = settings.value("decoders").toInt();
508 for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) {
509 settings.beginGroup("decoder" + QString::number(decoder_idx));
511 QString id = settings.value("id").toString();
513 for (GSList *entry = dec_list; entry; entry = entry->next) {
514 const srd_decoder *dec = (srd_decoder*)entry->data;
518 if (QString::fromUtf8(dec->id) == id) {
519 shared_ptr<decode::Decoder> decoder =
520 make_shared<decode::Decoder>(dec);
522 stack_.push_back(decoder);
524 // Restore decoder options that differ from their default
525 int options = settings.value("options").toInt();
527 for (int i = 0; i < options; i++) {
528 settings.beginGroup("option" + QString::number(i));
529 QString name = settings.value("name").toString();
530 GVariant *value = GlobalSettings::restore_gvariant(settings);
531 decoder->set_option(name.toUtf8(), value);
535 // Include the newly created decode channels in the channel lists
536 update_channel_list();
545 // Restore channel mapping
546 unsigned int channels = settings.value("channels").toInt();
548 const unordered_set< shared_ptr<data::SignalBase> > signalbases =
549 session_.signalbases();
551 for (unsigned int channel_id = 0; channel_id < channels; channel_id++) {
552 auto channel = find_if(channels_.begin(), channels_.end(),
553 [&](data::DecodeChannel ch) { return ch.id == channel_id; });
555 if (channel == channels_.end()) {
556 qDebug() << "ERROR: Non-existant channel index:" << channel_id;
560 settings.beginGroup("channel" + QString::number(channel_id));
562 QString assigned_signal_name = settings.value("assigned_signal_name").toString();
564 for (shared_ptr<data::SignalBase> signal : signalbases)
565 if (signal->name() == assigned_signal_name)
566 channel->assigned_signal = signal.get();
568 channel->initial_pin_state = settings.value("initial_pin_state").toInt();
573 // Update the internal structures
574 update_channel_list();
575 commit_decoder_channels();
580 uint32_t DecodeSignal::get_input_segment_count() const
582 uint64_t count = std::numeric_limits<uint64_t>::max();
583 bool no_signals_assigned = true;
585 for (const data::DecodeChannel &ch : channels_)
586 if (ch.assigned_signal) {
587 no_signals_assigned = false;
589 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
590 if (!logic_data || logic_data->logic_segments().empty())
593 // Find the min value of all segment counts
594 if ((uint64_t)(logic_data->logic_segments().size()) < count)
595 count = logic_data->logic_segments().size();
598 return (no_signals_assigned ? 0 : count);
601 uint32_t DecodeSignal::get_input_samplerate(uint32_t segment_id) const
603 double samplerate = 0;
605 for (const data::DecodeChannel &ch : channels_)
606 if (ch.assigned_signal) {
607 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
608 if (!logic_data || logic_data->logic_segments().empty())
612 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id);
613 samplerate = segment->samplerate();
614 } catch (out_of_range&) {
623 void DecodeSignal::update_channel_list()
625 vector<data::DecodeChannel> prev_channels = channels_;
630 // Copy existing entries, create new as needed
631 for (shared_ptr<Decoder> decoder : stack_) {
632 const srd_decoder* srd_d = decoder->decoder();
635 // Mandatory channels
636 for (l = srd_d->channels; l; l = l->next) {
637 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
638 bool ch_added = false;
640 // Copy but update ID if this channel was in the list before
641 for (data::DecodeChannel &ch : prev_channels)
642 if (ch.pdch_ == pdch) {
644 channels_.push_back(ch);
650 // Create new entry without a mapped signal
651 data::DecodeChannel ch = {id++, 0, false, nullptr,
652 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
653 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
654 channels_.push_back(ch);
659 for (l = srd_d->opt_channels; l; l = l->next) {
660 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
661 bool ch_added = false;
663 // Copy but update ID if this channel was in the list before
664 for (data::DecodeChannel &ch : prev_channels)
665 if (ch.pdch_ == pdch) {
667 channels_.push_back(ch);
673 // Create new entry without a mapped signal
674 data::DecodeChannel ch = {id++, 0, true, nullptr,
675 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
676 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
677 channels_.push_back(ch);
682 // Invalidate the logic output data if the channel assignment changed
683 if (prev_channels.size() != channels_.size()) {
684 // The number of channels changed, there's definitely a difference
685 logic_mux_data_invalid_ = true;
687 // Same number but assignment may still differ, so compare all channels
688 for (size_t i = 0; i < channels_.size(); i++) {
689 const data::DecodeChannel &p_ch = prev_channels[i];
690 const data::DecodeChannel &ch = channels_[i];
692 if ((p_ch.pdch_ != ch.pdch_) ||
693 (p_ch.assigned_signal != ch.assigned_signal)) {
694 logic_mux_data_invalid_ = true;
704 void DecodeSignal::commit_decoder_channels()
706 // Submit channel list to every decoder, containing only the relevant channels
707 for (shared_ptr<decode::Decoder> dec : stack_) {
708 vector<data::DecodeChannel*> channel_list;
710 for (data::DecodeChannel &ch : channels_)
711 if (ch.decoder_ == dec)
712 channel_list.push_back(&ch);
714 dec->set_channels(channel_list);
717 // Channel bit IDs must be in sync with the channel's apperance in channels_
719 for (data::DecodeChannel &ch : channels_)
720 if (ch.assigned_signal)
724 void DecodeSignal::mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end)
726 // Enforce end to be greater than start
730 // Fetch the channel segments and their data
731 vector<shared_ptr<LogicSegment> > segments;
732 vector<const uint8_t*> signal_data;
733 vector<uint8_t> signal_in_bytepos;
734 vector<uint8_t> signal_in_bitpos;
736 for (data::DecodeChannel &ch : channels_)
737 if (ch.assigned_signal) {
738 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
740 shared_ptr<LogicSegment> segment;
742 segment = logic_data->logic_segments().at(segment_id);
743 } catch (out_of_range&) {
744 qDebug() << "Muxer error for" << name() << ":" << ch.assigned_signal->name() \
745 << "has no logic segment" << segment_id;
748 segments.push_back(segment);
750 uint8_t* data = new uint8_t[(end - start) * segment->unit_size()];
751 segment->get_samples(start, end, data);
752 signal_data.push_back(data);
754 const int bitpos = ch.assigned_signal->logic_bit_index();
755 signal_in_bytepos.push_back(bitpos / 8);
756 signal_in_bitpos.push_back(bitpos % 8);
760 shared_ptr<LogicSegment> output_segment;
762 output_segment = logic_mux_data_->logic_segments().at(segment_id);
763 } catch (out_of_range&) {
764 qDebug() << "Muxer error for" << name() << ": no logic mux segment" \
765 << segment_id << "in mux_logic_samples(), mux segments size is" \
766 << logic_mux_data_->logic_segments().size();
770 // Perform the muxing of signal data into the output data
771 uint8_t* output = new uint8_t[(end - start) * output_segment->unit_size()];
772 unsigned int signal_count = signal_data.size();
774 for (int64_t sample_cnt = 0; sample_cnt < (end - start); sample_cnt++) {
778 const int out_sample_pos = sample_cnt * output_segment->unit_size();
779 for (unsigned int i = 0; i < output_segment->unit_size(); i++)
780 output[out_sample_pos + i] = 0;
782 for (unsigned int i = 0; i < signal_count; i++) {
783 const int in_sample_pos = sample_cnt * segments[i]->unit_size();
784 const uint8_t in_sample = 1 &
785 ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i]));
787 const uint8_t out_sample = output[out_sample_pos + bytepos];
789 output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos);
799 output_segment->append_payload(output, (end - start) * output_segment->unit_size());
802 for (const uint8_t* data : signal_data)
806 void DecodeSignal::logic_mux_proc()
808 uint32_t segment_id = 0;
810 assert(logic_mux_data_);
812 // Create initial logic mux segment
813 shared_ptr<LogicSegment> output_segment =
814 make_shared<LogicSegment>(*logic_mux_data_, segment_id,
815 logic_mux_unit_size_, 0);
816 logic_mux_data_->push_segment(output_segment);
818 output_segment->set_samplerate(get_input_samplerate(0));
821 const uint64_t input_sample_count = get_working_sample_count(segment_id);
822 const uint64_t output_sample_count = output_segment->get_sample_count();
824 const uint64_t samples_to_process =
825 (input_sample_count > output_sample_count) ?
826 (input_sample_count - output_sample_count) : 0;
828 // Process the samples if necessary...
829 if (samples_to_process > 0) {
830 const uint64_t unit_size = output_segment->unit_size();
831 const uint64_t chunk_sample_count = DecodeChunkLength / unit_size;
833 uint64_t processed_samples = 0;
835 const uint64_t start_sample = output_sample_count + processed_samples;
836 const uint64_t sample_count =
837 min(samples_to_process - processed_samples, chunk_sample_count);
839 mux_logic_samples(segment_id, start_sample, start_sample + sample_count);
840 processed_samples += sample_count;
842 // ...and process the newly muxed logic data
843 decode_input_cond_.notify_one();
844 } while (processed_samples < samples_to_process);
847 if (samples_to_process == 0) {
848 // TODO Optimize this by caching the input segment count and only
849 // querying it when the cached value was reached
850 if (segment_id < get_input_segment_count() - 1) {
851 // Process next segment
855 make_shared<LogicSegment>(*logic_mux_data_, segment_id,
856 logic_mux_unit_size_, 0);
857 logic_mux_data_->push_segment(output_segment);
859 output_segment->set_samplerate(get_input_samplerate(segment_id));
862 // All segments have been processed
863 logic_mux_data_invalid_ = false;
865 // Wait for more input
866 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
867 logic_mux_cond_.wait(logic_mux_lock);
870 } while (!logic_mux_interrupt_);
873 void DecodeSignal::decode_data(
874 const int64_t abs_start_samplenum, const int64_t sample_count,
875 const shared_ptr<LogicSegment> input_segment)
877 const int64_t unit_size = input_segment->unit_size();
878 const int64_t chunk_sample_count = DecodeChunkLength / unit_size;
880 for (int64_t i = abs_start_samplenum;
881 !decode_interrupt_ && (i < (abs_start_samplenum + sample_count));
882 i += chunk_sample_count) {
884 const int64_t chunk_end = min(i + chunk_sample_count,
885 abs_start_samplenum + sample_count);
887 int64_t data_size = (chunk_end - i) * unit_size;
888 uint8_t* chunk = new uint8_t[data_size];
889 input_segment->get_samples(i, chunk_end, chunk);
891 if (srd_session_send(srd_session_, i, chunk_end, chunk,
892 data_size, unit_size) != SRD_OK) {
893 error_message_ = tr("Decoder reported an error");
901 lock_guard<mutex> lock(output_mutex_);
902 segments_.at(current_segment_id_).samples_decoded = chunk_end;
905 // Notify the frontend that we processed some data and
906 // possibly have new annotations as well
911 void DecodeSignal::decode_proc()
913 current_segment_id_ = 0;
915 // If there is no input data available yet, wait until it is or we're interrupted
916 if (logic_mux_data_->logic_segments().size() == 0) {
917 unique_lock<mutex> input_wait_lock(input_mutex_);
918 decode_input_cond_.wait(input_wait_lock);
921 if (decode_interrupt_)
924 shared_ptr<LogicSegment> input_segment = logic_mux_data_->logic_segments().front();
925 assert(input_segment);
927 // Create the initial segment and set its sample rate so that we can pass it to SRD
928 create_decode_segment();
929 segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
930 segments_.at(current_segment_id_).start_time = input_segment->start_time();
934 uint64_t sample_count = 0;
935 uint64_t abs_start_samplenum = 0;
937 // Keep processing new samples until we exhaust the input data
939 lock_guard<mutex> input_lock(input_mutex_);
940 sample_count = input_segment->get_sample_count() - abs_start_samplenum;
942 if (sample_count > 0) {
943 decode_data(abs_start_samplenum, sample_count, input_segment);
944 abs_start_samplenum += sample_count;
946 } while (error_message_.isEmpty() && (sample_count > 0) && !decode_interrupt_);
948 if (error_message_.isEmpty() && !decode_interrupt_ && sample_count == 0) {
949 if (current_segment_id_ < logic_mux_data_->logic_segments().size() - 1) {
950 // Process next segment
951 current_segment_id_++;
954 input_segment = logic_mux_data_->logic_segments().at(current_segment_id_);
955 } catch (out_of_range&) {
956 qDebug() << "Decode error for" << name() << ": no logic mux segment" \
957 << current_segment_id_ << "in decode_proc(), mux segments size is" \
958 << logic_mux_data_->logic_segments().size();
961 abs_start_samplenum = 0;
963 // Create the next segment and set its metadata
964 create_decode_segment();
965 segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
966 segments_.at(current_segment_id_).start_time = input_segment->start_time();
968 // Reset decoder state
972 // All segments have been processed
975 // Wait for new input data or an interrupt was requested
976 unique_lock<mutex> input_wait_lock(input_mutex_);
977 decode_input_cond_.wait(input_wait_lock);
980 } while (error_message_.isEmpty() && !decode_interrupt_);
982 // Potentially reap decoders when the application no longer is
983 // interested in their (pending) results.
984 if (decode_interrupt_)
985 terminate_srd_session();
988 void DecodeSignal::start_srd_session()
993 // When a decoder stack was created before, re-use it
994 // for the next stream of input data, after terminating
995 // potentially still executing operations, and resetting
996 // internal state. Skip the rather expensive (teardown
997 // and) construction of another decoder stack.
999 // TODO Reduce redundancy, use a common code path for
1000 // the meta/cb/start sequence?
1001 terminate_srd_session();
1002 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
1003 g_variant_new_uint64(segments_.at(current_segment_id_).samplerate));
1004 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN,
1005 DecodeSignal::annotation_callback, this);
1006 srd_session_start(srd_session_);
1010 // Create the session
1011 srd_session_new(&srd_session_);
1012 assert(srd_session_);
1014 // Create the decoders
1015 srd_decoder_inst *prev_di = nullptr;
1016 for (const shared_ptr<decode::Decoder> &dec : stack_) {
1017 srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_);
1020 error_message_ = tr("Failed to create decoder instance");
1021 srd_session_destroy(srd_session_);
1022 srd_session_ = nullptr;
1027 srd_inst_stack(srd_session_, prev_di, di);
1032 // Start the session
1033 samplerate = segments_.at(current_segment_id_).samplerate;
1035 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
1036 g_variant_new_uint64(samplerate));
1038 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN,
1039 DecodeSignal::annotation_callback, this);
1041 srd_session_start(srd_session_);
1044 void DecodeSignal::terminate_srd_session()
1046 // Call the "terminate and reset" routine for the decoder stack
1047 // (if available). This does not harm those stacks which already
1048 // have completed their operation, and reduces response time for
1049 // those stacks which still are processing data while the
1050 // application no longer wants them to.
1052 srd_session_terminate_reset(srd_session_);
1055 void DecodeSignal::stop_srd_session()
1058 // Destroy the session
1059 srd_session_destroy(srd_session_);
1060 srd_session_ = nullptr;
1064 void DecodeSignal::connect_input_notifiers()
1066 // Disconnect the notification slot from the previous set of signals
1067 disconnect(this, SLOT(on_data_cleared()));
1068 disconnect(this, SLOT(on_data_received()));
1070 // Connect the currently used signals to our slot
1071 for (data::DecodeChannel &ch : channels_) {
1072 if (!ch.assigned_signal)
1075 const data::SignalBase *signal = ch.assigned_signal;
1076 connect(signal, SIGNAL(samples_cleared()),
1077 this, SLOT(on_data_cleared()));
1078 connect(signal, SIGNAL(samples_added(uint64_t, uint64_t, uint64_t)),
1079 this, SLOT(on_data_received()));
1083 void DecodeSignal::create_decode_segment()
1085 // Create annotation segment
1086 segments_.emplace_back(DecodeSegment());
1088 // Add annotation classes
1089 for (const shared_ptr<decode::Decoder> &dec : stack_) {
1091 const srd_decoder *const decc = dec->decoder();
1092 assert(dec->decoder());
1094 // Add a row for the decoder if it doesn't have a row list
1095 if (!decc->annotation_rows)
1096 (segments_.back().annotation_rows)[Row(decc)] =
1099 // Add the decoder rows
1100 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
1101 const srd_decoder_annotation_row *const ann_row =
1102 (srd_decoder_annotation_row *)l->data;
1105 const Row row(decc, ann_row);
1107 // Add a new empty row data object
1108 (segments_.back().annotation_rows)[row] =
1114 void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
1117 assert(decode_signal);
1119 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
1122 lock_guard<mutex> lock(ds->output_mutex_);
1126 assert(pdata->pdo->di);
1127 const srd_decoder *const decc = pdata->pdo->di->decoder;
1130 const srd_proto_data_annotation *const pda =
1131 (const srd_proto_data_annotation*)pdata->data;
1134 auto row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.end();
1136 // Try looking up the sub-row of this class
1137 const auto format = pda->ann_class;
1138 const auto r = ds->class_rows_.find(make_pair(decc, format));
1139 if (r != ds->class_rows_.end())
1140 row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.find((*r).second);
1142 // Failing that, use the decoder as a key
1143 row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.find(Row(decc));
1146 if (row_iter == ds->segments_.at(ds->current_segment_id_).annotation_rows.end()) {
1147 qDebug() << "Unexpected annotation: decoder = " << decc <<
1148 ", format = " << format;
1153 // Add the annotation
1154 (*row_iter).second.emplace_annotation(pdata);
1157 void DecodeSignal::on_capture_state_changed(int state)
1159 // If a new acquisition was started, we need to start decoding from scratch
1160 if (state == Session::Running) {
1161 logic_mux_data_invalid_ = true;
1166 void DecodeSignal::on_data_cleared()
1171 void DecodeSignal::on_data_received()
1173 if (!logic_mux_thread_.joinable())
1176 logic_mux_cond_.notify_one();