+ const int64_t unit_size = input_segment->unit_size();
+ const int64_t chunk_sample_count = DecodeChunkLength / unit_size;
+
+ for (int64_t i = abs_start_samplenum;
+ !decode_interrupt_ && (i < (abs_start_samplenum + sample_count));
+ i += chunk_sample_count) {
+
+ const int64_t chunk_end = min(i + chunk_sample_count,
+ abs_start_samplenum + sample_count);
+
+ int64_t data_size = (chunk_end - i) * unit_size;
+ uint8_t* chunk = new uint8_t[data_size];
+ input_segment->get_samples(i, chunk_end, chunk);
+
+ if (srd_session_send(srd_session_, i, chunk_end, chunk,
+ data_size, unit_size) != SRD_OK) {
+ error_message_ = tr("Decoder reported an error");
+ delete[] chunk;
+ break;
+ }
+
+ delete[] chunk;
+
+ {
+ lock_guard<mutex> lock(output_mutex_);
+ segments_.at(current_segment_id_).samples_decoded = chunk_end;
+ }
+
+ // Notify the frontend that we processed some data and
+ // possibly have new annotations as well
+ new_annotations();
+ }
+}
+
+void DecodeSignal::decode_proc()
+{
+ query_input_metadata();
+
+ if (decode_interrupt_)
+ return;
+
+ start_srd_session();
+
+ current_segment_id_ = 0;
+ shared_ptr<LogicSegment> input_segment = logic_mux_data_->logic_segments().front();
+
+ assert(input_segment);
+ segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
+
+ uint64_t sample_count;
+ uint64_t abs_start_samplenum = 0;
+ do {
+ // Keep processing new samples until we exhaust the input data
+ do {
+ lock_guard<mutex> input_lock(input_mutex_);
+ sample_count = input_segment->get_sample_count() - abs_start_samplenum;
+
+ if (sample_count > 0) {
+ decode_data(abs_start_samplenum, sample_count, input_segment);
+ abs_start_samplenum += sample_count;
+ }
+ } while (error_message_.isEmpty() && (sample_count > 0) && !decode_interrupt_);
+
+ if (error_message_.isEmpty() && !decode_interrupt_ && sample_count == 0) {
+ if (current_segment_id_ < logic_mux_data_->logic_segments().size() - 1) {
+ // Process next segment
+ current_segment_id_++;
+
+ try {
+ input_segment = logic_mux_data_->logic_segments().at(current_segment_id_);
+ } catch (out_of_range) {
+ qDebug() << "Decode error for" << name() << ": no logic mux segment" \
+ << current_segment_id_ << "in decode_proc(), mux segments size is" \
+ << logic_mux_data_->logic_segments().size();
+ return;
+ }
+ abs_start_samplenum = 0;
+
+ segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
+
+ // Reset decoder state
+ stop_srd_session();
+ start_srd_session();
+ } else {
+ // All segments have been processed
+ decode_finished();
+
+ // Wait for new input data or an interrupt was requested
+ unique_lock<mutex> input_wait_lock(input_mutex_);
+ decode_input_cond_.wait(input_wait_lock);
+ }
+ }
+ } while (error_message_.isEmpty() && !decode_interrupt_);
+}
+
+void DecodeSignal::start_srd_session()
+{
+ if (srd_session_)
+ stop_srd_session();
+
+ // Create the session
+ srd_session_new(&srd_session_);
+ assert(srd_session_);
+
+ // Create the decoders
+ srd_decoder_inst *prev_di = nullptr;
+ for (const shared_ptr<decode::Decoder> &dec : stack_) {
+ srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_);
+
+ if (!di) {
+ error_message_ = tr("Failed to create decoder instance");
+ srd_session_destroy(srd_session_);
+ return;
+ }
+
+ if (prev_di)
+ srd_inst_stack(srd_session_, prev_di, di);
+
+ prev_di = di;
+ }
+
+ // Start the session
+ srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
+ g_variant_new_uint64(segments_.at(current_segment_id_).samplerate));
+
+ srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN,
+ DecodeSignal::annotation_callback, this);
+
+ srd_session_start(srd_session_);
+}
+
+void DecodeSignal::stop_srd_session()
+{
+ if (srd_session_) {
+ // Destroy the session
+ srd_session_destroy(srd_session_);
+ srd_session_ = nullptr;
+ }
+}
+
+void DecodeSignal::connect_input_notifiers()
+{
+ // Disconnect the notification slot from the previous set of signals
+ disconnect(this, SLOT(on_data_cleared()));
+ disconnect(this, SLOT(on_data_received()));
+
+ // Connect the currently used signals to our slot
+ for (data::DecodeChannel &ch : channels_) {
+ if (!ch.assigned_signal)
+ continue;
+
+ const data::SignalBase *signal = ch.assigned_signal;
+ connect(signal, SIGNAL(samples_cleared()),
+ this, SLOT(on_data_cleared()));
+ connect(signal, SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
+ this, SLOT(on_data_received()));
+ }
+}
+
+void DecodeSignal::create_segments()
+{
+ // Make sure we have as many segments as we need
+ const uint32_t input_segment_count = get_input_segment_count();
+
+ for (uint32_t i = logic_mux_data_->logic_segments().size(); i < input_segment_count; i++) {
+ shared_ptr<LogicSegment> segment =
+ make_shared<LogicSegment>(*logic_mux_data_, logic_mux_unit_size_, 0);
+ logic_mux_data_->push_segment(segment);
+ }
+
+ for (uint32_t i = segments_.size(); i < input_segment_count; i++) {
+ // Create annotation segment
+ segments_.emplace_back(DecodeSegment());
+
+ // Add annotation classes
+ for (const shared_ptr<decode::Decoder> &dec : stack_) {
+ assert(dec);
+ const srd_decoder *const decc = dec->decoder();
+ assert(dec->decoder());
+
+ // Add a row for the decoder if it doesn't have a row list
+ if (!decc->annotation_rows)
+ (segments_.back().annotation_rows)[Row(decc)] =
+ decode::RowData();
+
+ // Add the decoder rows
+ for (const GSList *l = decc->annotation_rows; l; l = l->next) {
+ const srd_decoder_annotation_row *const ann_row =
+ (srd_decoder_annotation_row *)l->data;
+ assert(ann_row);
+
+ const Row row(decc, ann_row);
+
+ // Add a new empty row data object
+ (segments_.back().annotation_rows)[row] =
+ decode::RowData();
+ }
+ }
+ }
+}
+
+void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
+{
+ assert(pdata);
+ assert(decode_signal);
+
+ DecodeSignal *const ds = (DecodeSignal*)decode_signal;
+ assert(ds);
+
+ lock_guard<mutex> lock(ds->output_mutex_);
+
+ // Find the row
+ assert(pdata->pdo);
+ assert(pdata->pdo->di);
+ const srd_decoder *const decc = pdata->pdo->di->decoder;
+ assert(decc);
+
+ const srd_proto_data_annotation *const pda =
+ (const srd_proto_data_annotation*)pdata->data;
+ assert(pda);
+
+ auto row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.end();
+
+ // Try looking up the sub-row of this class
+ const auto format = pda->ann_class;
+ const auto r = ds->class_rows_.find(make_pair(decc, format));
+ if (r != ds->class_rows_.end())
+ row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.find((*r).second);
+ else {
+ // Failing that, use the decoder as a key
+ row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.find(Row(decc));
+ }
+
+ if (row_iter == ds->segments_.at(ds->current_segment_id_).annotation_rows.end()) {
+ qDebug() << "Unexpected annotation: decoder = " << decc <<
+ ", format = " << format;
+ assert(false);
+ return;
+ }
+
+ // Add the annotation
+ (*row_iter).second.emplace_annotation(pdata);
+}
+
+void DecodeSignal::on_capture_state_changed(int state)
+{
+ // If a new acquisition was started, we need to start decoding from scratch
+ if (state == Session::Running) {
+ logic_mux_data_invalid_ = true;
+ begin_decode();
+ }
+}
+
+void DecodeSignal::on_data_cleared()
+{
+ reset_decode();
+}
+
+void DecodeSignal::on_data_received()
+{
+ create_segments();
+
+ if (!logic_mux_thread_.joinable())
+ begin_decode();
+ else
+ logic_mux_cond_.notify_one();