+ current_segment_id_ = 0;
+
+ // If there is no input data available yet, wait until it is or we're interrupted
+ if (logic_mux_data_->logic_segments().size() == 0) {
+ unique_lock<mutex> input_wait_lock(input_mutex_);
+ decode_input_cond_.wait(input_wait_lock);
+ }
+
+ if (decode_interrupt_)
+ return;
+
+ shared_ptr<LogicSegment> input_segment = logic_mux_data_->logic_segments().front();
+ assert(input_segment);
+
+ // Create the initial segment and set its sample rate so that we can pass it to SRD
+ create_decode_segment();
+ segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
+ segments_.at(current_segment_id_).start_time = input_segment->start_time();
+
+ start_srd_session();
+
+ uint64_t sample_count = 0;
+ 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;
+
+ // Create the next segment and set its metadata
+ create_decode_segment();
+ segments_.at(current_segment_id_).samplerate = input_segment->samplerate();
+ segments_.at(current_segment_id_).start_time = input_segment->start_time();
+
+ // Reset decoder state but keep the decoder stack intact
+ terminate_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_);
+
+ // Potentially reap decoders when the application no longer is
+ // interested in their (pending) results.
+ if (decode_interrupt_)
+ terminate_srd_session();
+}
+
+void DecodeSignal::start_srd_session()
+{
+ // If there were stack changes, the session has been destroyed by now, so if
+ // it hasn't been destroyed, we can just reset and re-use it
+ if (srd_session_) {
+ // When a decoder stack was created before, re-use it
+ // for the next stream of input data, after terminating
+ // potentially still executing operations, and resetting
+ // internal state. Skip the rather expensive (teardown
+ // and) construction of another decoder stack.
+
+ // TODO Reduce redundancy, use a common code path for
+ // the meta/start sequence?
+ terminate_srd_session();
+
+ // Metadata is cleared also, so re-set it
+ uint64_t samplerate = 0;
+ if (segments_.size() > 0)
+ samplerate = segments_.at(current_segment_id_).samplerate;
+ if (samplerate)
+ srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
+ g_variant_new_uint64(samplerate));
+ for (const shared_ptr<decode::Decoder> &dec : stack_)
+ dec->apply_all_options();
+ srd_session_start(srd_session_);
+
+ return;
+ }
+
+ // 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) {
+ set_error_message(tr("Failed to create decoder instance"));
+ srd_session_destroy(srd_session_);
+ srd_session_ = nullptr;
+ return;
+ }
+
+ if (prev_di)
+ srd_inst_stack(srd_session_, prev_di, di);
+
+ prev_di = di;
+ }
+
+ // Start the session
+ if (segments_.size() > 0)
+ 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_);
+
+ // We just recreated the srd session, so all stack changes are applied now
+ stack_config_changed_ = false;
+}
+
+void DecodeSignal::terminate_srd_session()
+{
+ // Call the "terminate and reset" routine for the decoder stack
+ // (if available). This does not harm those stacks which already
+ // have completed their operation, and reduces response time for
+ // those stacks which still are processing data while the
+ // application no longer wants them to.
+ if (srd_session_) {
+ srd_session_terminate_reset(srd_session_);
+
+ // Metadata is cleared also, so re-set it
+ uint64_t samplerate = 0;
+ if (segments_.size() > 0)
+ samplerate = segments_.at(current_segment_id_).samplerate;
+ if (samplerate)
+ srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
+ g_variant_new_uint64(samplerate));
+ for (const shared_ptr<decode::Decoder> &dec : stack_)
+ dec->apply_all_options();
+ }
+}
+
+void DecodeSignal::stop_srd_session()
+{
+ if (srd_session_) {
+ // Destroy the session
+ srd_session_destroy(srd_session_);
+ srd_session_ = nullptr;
+
+ // Mark the decoder instances as non-existant since they were deleted
+ for (const shared_ptr<decode::Decoder> &dec : stack_)
+ dec->invalidate_decoder_inst();
+ }
+}
+
+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(uint64_t, uint64_t, uint64_t)),
+ this, SLOT(on_data_received()));
+ }
+}
+
+void DecodeSignal::create_decode_segment()
+{
+ // 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());
+
+ int row_index = 0;
+ // Add a row for the decoder if it doesn't have a row list
+ if (!decc->annotation_rows)
+ (segments_.back().annotation_rows)[Row(row_index++, 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(row_index++, 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);
+
+ if (ds->decode_interrupt_)
+ return;
+
+ 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(0, 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, &((*row_iter).first));
+}
+
+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()
+{
+ // If we detected a lack of input data when trying to start decoding,
+ // we have set an error message. Only try again if we now have data
+ // to work with
+ if ((!error_message_.isEmpty()) && (get_input_segment_count() == 0))
+ return;
+
+ if (!logic_mux_thread_.joinable())
+ begin_decode();
+ else
+ logic_mux_cond_.notify_one();