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