+void DecodeSignal::commit_decoder_channels()
+{
+ // Submit channel list to every decoder, containing only the relevant channels
+ for (shared_ptr<decode::Decoder> dec : stack_) {
+ vector<data::DecodeChannel*> channel_list;
+
+ for (data::DecodeChannel &ch : channels_)
+ if (ch.decoder_ == dec)
+ channel_list.push_back(&ch);
+
+ dec->set_channels(channel_list);
+ }
+
+ // Channel bit IDs must be in sync with the channel's apperance in channels_
+ int id = 0;
+ for (data::DecodeChannel &ch : channels_)
+ if (ch.assigned_signal)
+ ch.bit_id = id++;
+}
+
+void DecodeSignal::mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end)
+{
+ // Enforce end to be greater than start
+ if (end <= start)
+ return;
+
+ // Fetch the channel segments and their data
+ vector<shared_ptr<LogicSegment> > segments;
+ vector<const uint8_t*> signal_data;
+ vector<uint8_t> signal_in_bytepos;
+ vector<uint8_t> signal_in_bitpos;
+
+ for (data::DecodeChannel &ch : channels_)
+ if (ch.assigned_signal) {
+ const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
+
+ shared_ptr<LogicSegment> segment;
+ try {
+ segment = logic_data->logic_segments().at(segment_id);
+ } catch (out_of_range&) {
+ qDebug() << "Muxer error for" << name() << ":" << ch.assigned_signal->name() \
+ << "has no logic segment" << segment_id;
+ return;
+ }
+ segments.push_back(segment);
+
+ uint8_t* data = new uint8_t[(end - start) * segment->unit_size()];
+ segment->get_samples(start, end, data);
+ signal_data.push_back(data);
+
+ const int bitpos = ch.assigned_signal->logic_bit_index();
+ signal_in_bytepos.push_back(bitpos / 8);
+ signal_in_bitpos.push_back(bitpos % 8);
+ }
+
+
+ shared_ptr<LogicSegment> output_segment;
+ try {
+ output_segment = logic_mux_data_->logic_segments().at(segment_id);
+ } catch (out_of_range&) {
+ qDebug() << "Muxer error for" << name() << ": no logic mux segment" \
+ << segment_id << "in mux_logic_samples(), mux segments size is" \
+ << logic_mux_data_->logic_segments().size();
+ return;
+ }
+
+ // Perform the muxing of signal data into the output data
+ uint8_t* output = new uint8_t[(end - start) * output_segment->unit_size()];
+ unsigned int signal_count = signal_data.size();
+
+ for (int64_t sample_cnt = 0; !logic_mux_interrupt_ && (sample_cnt < (end - start));
+ sample_cnt++) {
+
+ int bitpos = 0;
+ uint8_t bytepos = 0;
+
+ const int out_sample_pos = sample_cnt * output_segment->unit_size();
+ for (unsigned int i = 0; i < output_segment->unit_size(); i++)
+ output[out_sample_pos + i] = 0;
+
+ for (unsigned int i = 0; i < signal_count; i++) {
+ const int in_sample_pos = sample_cnt * segments[i]->unit_size();
+ const uint8_t in_sample = 1 &
+ ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i]));
+
+ const uint8_t out_sample = output[out_sample_pos + bytepos];
+
+ output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos);
+
+ bitpos++;
+ if (bitpos > 7) {
+ bitpos = 0;
+ bytepos++;
+ }
+ }
+ }
+
+ output_segment->append_payload(output, (end - start) * output_segment->unit_size());
+ delete[] output;
+
+ for (const uint8_t* data : signal_data)
+ delete[] data;
+}
+
+void DecodeSignal::logic_mux_proc()
+{
+ uint32_t segment_id = 0;
+
+ assert(logic_mux_data_);
+
+ // Create initial logic mux segment
+ shared_ptr<LogicSegment> output_segment =
+ make_shared<LogicSegment>(*logic_mux_data_, segment_id,
+ logic_mux_unit_size_, 0);
+ logic_mux_data_->push_segment(output_segment);
+
+ output_segment->set_samplerate(get_input_samplerate(0));
+
+ do {
+ const uint64_t input_sample_count = get_working_sample_count(segment_id);
+ const uint64_t output_sample_count = output_segment->get_sample_count();
+
+ const uint64_t samples_to_process =
+ (input_sample_count > output_sample_count) ?
+ (input_sample_count - output_sample_count) : 0;
+
+ // Process the samples if necessary...
+ if (samples_to_process > 0) {
+ const uint64_t unit_size = output_segment->unit_size();
+ const uint64_t chunk_sample_count = DecodeChunkLength / unit_size;
+
+ uint64_t processed_samples = 0;
+ do {
+ const uint64_t start_sample = output_sample_count + processed_samples;
+ const uint64_t sample_count =
+ min(samples_to_process - processed_samples, chunk_sample_count);
+
+ mux_logic_samples(segment_id, start_sample, start_sample + sample_count);
+ processed_samples += sample_count;
+
+ // ...and process the newly muxed logic data
+ decode_input_cond_.notify_one();
+ } while (!logic_mux_interrupt_ && (processed_samples < samples_to_process));
+ }
+
+ if (samples_to_process == 0) {
+ // TODO Optimize this by caching the input segment count and only
+ // querying it when the cached value was reached
+ if (segment_id < get_input_segment_count() - 1) {
+ // Process next segment
+ segment_id++;
+
+ output_segment =
+ make_shared<LogicSegment>(*logic_mux_data_, segment_id,
+ logic_mux_unit_size_, 0);
+ logic_mux_data_->push_segment(output_segment);
+
+ output_segment->set_samplerate(get_input_samplerate(segment_id));
+
+ } else {
+ // All segments have been processed
+ logic_mux_data_invalid_ = false;
+
+ // Wait for more input
+ unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
+ logic_mux_cond_.wait(logic_mux_lock);
+ }
+ }
+ } while (!logic_mux_interrupt_);
+}
+
+void DecodeSignal::decode_data(
+ const int64_t abs_start_samplenum, const int64_t sample_count,
+ const shared_ptr<LogicSegment> input_segment)
+{
+ 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);
+
+ // Report this chunk as already decoded so that annotations don't
+ // appear in an area that we claim to not having been been decoded yet
+ {
+ lock_guard<mutex> lock(output_mutex_);
+ segments_.at(current_segment_id_).samples_decoded = chunk_end;
+ }
+
+ 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) {
+ set_error_message(tr("Decoder reported an error"));
+ delete[] chunk;
+ break;
+ }
+
+ delete[] chunk;
+
+ // Notify the frontend that we processed some data and
+ // possibly have new annotations as well
+ new_annotations();
+ }
+}
+
+void DecodeSignal::decode_proc()
+{
+ 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()