]> sigrok.org Git - pulseview.git/blame - pv/data/decodesignal.cpp
DecodeSignal: Rework samplerate handling
[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
47747218
SA
268 for (shared_ptr<data::SignalBase> s : session_.signalbases())
269 if (s->logic_data() && (ch.name.toLower().contains(s->name().toLower()))) {
132a5c6d 270 ch.assigned_signal = s.get();
47747218
SA
271 new_assignment = true;
272 }
273 }
274
275 if (new_assignment) {
276 logic_mux_data_invalid_ = true;
27a3f09b 277 commit_decoder_channels();
47747218 278 channels_updated();
132a5c6d
SA
279 }
280}
281
9f97b357
SA
282void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
283{
284 for (data::DecodeChannel &ch : channels_)
47747218 285 if (ch.id == channel_id) {
9f97b357 286 ch.assigned_signal = signal;
47747218
SA
287 logic_mux_data_invalid_ = true;
288 }
9f97b357 289
27a3f09b 290 commit_decoder_channels();
9f97b357 291 channels_updated();
47747218 292 begin_decode();
9f97b357
SA
293}
294
27a3f09b
SA
295int DecodeSignal::get_assigned_signal_count() const
296{
297 // Count all channels that have a signal assigned to them
298 return count_if(channels_.begin(), channels_.end(),
299 [](data::DecodeChannel ch) { return ch.assigned_signal; });
300}
301
9f97b357
SA
302void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
303{
304 for (data::DecodeChannel &ch : channels_)
305 if (ch.id == channel_id)
306 ch.initial_pin_state = init_state;
307
308 channels_updated();
309
47747218 310 begin_decode();
9f97b357
SA
311}
312
ff83d980
SA
313double DecodeSignal::samplerate() const
314{
47747218 315 return samplerate_;
ff83d980
SA
316}
317
318const pv::util::Timestamp& DecodeSignal::start_time() const
319{
47747218 320 return start_time_;
ff83d980
SA
321}
322
0c5fe73e
SA
323int64_t DecodeSignal::get_working_sample_count() const
324{
325 // The working sample count is the highest sample number for
326 // which all used signals have data available, so go through
327 // all channels and use the lowest overall sample count of the
328 // current segment
329
330 // TODO Currently, we assume only a single segment exists
331
332 int64_t count = std::numeric_limits<int64_t>::max();
333 bool no_signals_assigned = true;
334
335 for (const data::DecodeChannel &ch : channels_)
336 if (ch.assigned_signal) {
337 no_signals_assigned = false;
338
339 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
340 if (!logic_data || logic_data->logic_segments().empty())
341 return 0;
342
343 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
344 count = min(count, (int64_t)segment->get_sample_count());
345 }
346
347 return (no_signals_assigned ? 0 : count);
348}
349
350int64_t DecodeSignal::get_decoded_sample_count() const
ff83d980 351{
47747218
SA
352 lock_guard<mutex> decode_lock(output_mutex_);
353 return samples_decoded_;
ff83d980
SA
354}
355
ecd07c20
SA
356vector<Row> DecodeSignal::visible_rows() const
357{
47747218
SA
358 lock_guard<mutex> lock(output_mutex_);
359
360 vector<Row> rows;
361
362 for (const shared_ptr<decode::Decoder> &dec : stack_) {
363 assert(dec);
364 if (!dec->shown())
365 continue;
366
367 const srd_decoder *const decc = dec->decoder();
368 assert(dec->decoder());
369
370 // Add a row for the decoder if it doesn't have a row list
371 if (!decc->annotation_rows)
372 rows.emplace_back(decc);
373
374 // Add the decoder rows
375 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
376 const srd_decoder_annotation_row *const ann_row =
377 (srd_decoder_annotation_row *)l->data;
378 assert(ann_row);
379 rows.emplace_back(decc, ann_row);
380 }
381 }
382
383 return rows;
ecd07c20
SA
384}
385
386void DecodeSignal::get_annotation_subset(
387 vector<pv::data::decode::Annotation> &dest,
388 const decode::Row &row, uint64_t start_sample,
389 uint64_t end_sample) const
390{
47747218
SA
391 lock_guard<mutex> lock(output_mutex_);
392
393 const auto iter = rows_.find(row);
394 if (iter != rows_.end())
395 (*iter).second.get_annotation_subset(dest,
396 start_sample, end_sample);
397}
398
399void DecodeSignal::save_settings(QSettings &settings) const
400{
401 SignalBase::save_settings(settings);
402
403 // TODO Save decoder stack, channel mapping and decoder options
404}
405
406void DecodeSignal::restore_settings(QSettings &settings)
407{
408 SignalBase::restore_settings(settings);
409
410 // TODO Restore decoder stack, channel mapping and decoder options
411}
412
413uint64_t DecodeSignal::inc_annotation_count()
414{
415 return (annotation_count_++);
ecd07c20
SA
416}
417
9f97b357
SA
418void DecodeSignal::update_channel_list()
419{
47747218 420 vector<data::DecodeChannel> prev_channels = channels_;
9f97b357
SA
421 channels_.clear();
422
423 uint16_t id = 0;
424
425 // Copy existing entries, create new as needed
47747218 426 for (shared_ptr<Decoder> decoder : stack_) {
9f97b357
SA
427 const srd_decoder* srd_d = decoder->decoder();
428 const GSList *l;
429
430 // Mandatory channels
431 for (l = srd_d->channels; l; l = l->next) {
432 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
433 bool ch_added = false;
434
435 // Copy but update ID if this channel was in the list before
47747218 436 for (data::DecodeChannel &ch : prev_channels)
9f97b357
SA
437 if (ch.pdch_ == pdch) {
438 ch.id = id++;
439 channels_.push_back(ch);
440 ch_added = true;
441 break;
442 }
443
444 if (!ch_added) {
445 // Create new entry without a mapped signal
446 data::DecodeChannel ch = {id++, false, nullptr,
447 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
448 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
449 channels_.push_back(ch);
450 }
451 }
452
453 // Optional channels
454 for (l = srd_d->opt_channels; l; l = l->next) {
455 const struct srd_channel *const pdch = (struct srd_channel *)l->data;
456 bool ch_added = false;
457
458 // Copy but update ID if this channel was in the list before
47747218 459 for (data::DecodeChannel &ch : prev_channels)
9f97b357
SA
460 if (ch.pdch_ == pdch) {
461 ch.id = id++;
462 channels_.push_back(ch);
463 ch_added = true;
464 break;
465 }
466
467 if (!ch_added) {
468 // Create new entry without a mapped signal
469 data::DecodeChannel ch = {id++, true, nullptr,
470 QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
471 SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
472 channels_.push_back(ch);
473 }
474 }
475 }
476
47747218
SA
477 // Invalidate the logic output data if the channel assignment changed
478 if (prev_channels.size() != channels_.size()) {
479 // The number of channels changed, there's definitely a difference
480 logic_mux_data_invalid_ = true;
481 } else {
482 // Same number but assignment may still differ, so compare all channels
483 for (size_t i = 0; i < channels_.size(); i++) {
484 const data::DecodeChannel &p_ch = prev_channels[i];
485 const data::DecodeChannel &ch = channels_[i];
486
487 if ((p_ch.pdch_ != ch.pdch_) ||
488 (p_ch.assigned_signal != ch.assigned_signal)) {
489 logic_mux_data_invalid_ = true;
490 break;
491 }
492 }
493
494 }
495
9f97b357
SA
496 channels_updated();
497}
498
27a3f09b 499void DecodeSignal::commit_decoder_channels()
ad908057 500{
27a3f09b
SA
501 // Submit channel list to every decoder, containing only the relevant channels
502 for (shared_ptr<decode::Decoder> dec : stack_) {
503 vector<data::DecodeChannel*> channel_list;
504
505 for (data::DecodeChannel &ch : channels_)
506 if (ch.decoder_ == dec)
507 channel_list.push_back(&ch);
47747218 508
27a3f09b
SA
509 dec->set_channels(channel_list);
510 }
47747218
SA
511}
512
27a3f09b 513void DecodeSignal::mux_logic_samples(const int64_t start, const int64_t end)
47747218 514{
27a3f09b
SA
515 // Enforce end to be greater than start
516 if (end <= start)
517 return;
518
519 // Fetch all segments and their data
520 // TODO Currently, we assume only a single segment exists
521 vector<shared_ptr<LogicSegment> > segments;
522 vector<const uint8_t*> signal_data;
523 vector<uint8_t> signal_in_bytepos;
524 vector<uint8_t> signal_in_bitpos;
525
526 for (data::DecodeChannel &ch : channels_)
527 if (ch.assigned_signal) {
528 const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
529 const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
530 segments.push_back(segment);
531 signal_data.push_back(segment->get_samples(start, end));
532
533 const int bitpos = ch.assigned_signal->logic_bit_index();
534 signal_in_bytepos.push_back(bitpos / 8);
535 signal_in_bitpos.push_back(bitpos % 8);
536 }
537
538 // Perform the muxing of signal data into the output data
539 uint8_t* output = new uint8_t[(end - start) * segment_->unit_size()];
540 unsigned int signal_count = signal_data.size();
541
542 for (int64_t sample_cnt = 0; sample_cnt < (end - start); sample_cnt++) {
543 int bitpos = 0;
544 uint8_t bytepos = 0;
545
546 const int out_sample_pos = sample_cnt * segment_->unit_size();
547 for (unsigned int i = 0; i < segment_->unit_size(); i++)
548 output[out_sample_pos + i] = 0;
47747218 549
27a3f09b
SA
550 for (unsigned int i = 0; i < signal_count; i++) {
551 const int in_sample_pos = sample_cnt * segments[i]->unit_size();
552 const uint8_t in_sample = 1 &
553 ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i]));
47747218 554
27a3f09b
SA
555 const uint8_t out_sample = output[out_sample_pos + bytepos];
556
557 output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos);
558
559 bitpos++;
560 if (bitpos > 7) {
561 bitpos = 0;
562 bytepos++;
563 }
564 }
47747218
SA
565 }
566
27a3f09b
SA
567 segment_->append_payload(output, (end - start) * segment_->unit_size());
568 delete[] output;
569
570 for (const uint8_t* data : signal_data)
571 delete[] data;
572}
573
574void DecodeSignal::logic_mux_proc()
575{
576 do {
27a3f09b
SA
577 const uint64_t input_sample_count = get_working_sample_count();
578 const uint64_t output_sample_count = segment_->get_sample_count();
579
580 const uint64_t samples_to_process =
581 (input_sample_count > output_sample_count) ?
582 (input_sample_count - output_sample_count) : 0;
583
584 // Process the samples if necessary...
585 if (samples_to_process > 0) {
586 const uint64_t unit_size = segment_->unit_size();
587 const uint64_t chunk_sample_count = DecodeChunkLength / unit_size;
588
589 uint64_t processed_samples = 0;
590 do {
591 const uint64_t start_sample = output_sample_count + processed_samples;
592 const uint64_t sample_count =
593 min(samples_to_process - processed_samples, chunk_sample_count);
594
595 mux_logic_samples(start_sample, start_sample + sample_count);
596 processed_samples += sample_count;
597
598 // ...and process the newly muxed logic data
599 decode_input_cond_.notify_one();
600 } while (processed_samples < samples_to_process);
601 }
602
603 if (session_.get_capture_state() != Session::Stopped) {
604 // Wait for more input
605 unique_lock<mutex> logic_mux_lock(logic_mux_mutex_);
606 logic_mux_cond_.wait(logic_mux_lock);
607 }
608 } while ((session_.get_capture_state() != Session::Stopped) && !logic_mux_interrupt_);
609
610 // No more input data and session is stopped, let the decode thread
611 // process any pending data, terminate and release the global SRD mutex
612 // in order to let other decoders run
613 decode_input_cond_.notify_one();
47747218
SA
614}
615
a3ebd556
SA
616void DecodeSignal::query_input_metadata()
617{
618 // Update the samplerate and start time because we cannot start
619 // the libsrd session without the current samplerate
620
621 // TODO Currently we assume all channels have the same sample rate
622 // and start time
623 bool samplerate_valid = false;
624
625 auto any_channel = find_if(channels_.begin(), channels_.end(),
626 [](data::DecodeChannel ch) { return ch.assigned_signal; });
627
628 shared_ptr<Logic> logic_data =
629 any_channel->assigned_signal->logic_data();
630
631 do {
632 if (!logic_data->logic_segments().empty()) {
633 shared_ptr<LogicSegment> first_segment =
634 any_channel->assigned_signal->logic_data()->logic_segments().front();
635 start_time_ = first_segment->start_time();
636 samplerate_ = first_segment->samplerate();
637 if (samplerate_ > 0)
638 samplerate_valid = true;
639 }
640
641 // Wait until input data is available or an interrupt was requested
642 unique_lock<mutex> input_wait_lock(input_mutex_);
643 decode_input_cond_.wait(input_wait_lock);
644 } while (!samplerate_valid && !decode_interrupt_);
645}
646
47747218 647void DecodeSignal::decode_data(
e91883bb 648 const int64_t abs_start_samplenum, const int64_t sample_count)
47747218 649{
27a3f09b
SA
650 const int64_t unit_size = segment_->unit_size();
651 const int64_t chunk_sample_count = DecodeChunkLength / unit_size;
47747218
SA
652
653 for (int64_t i = abs_start_samplenum;
654 !decode_interrupt_ && (i < (abs_start_samplenum + sample_count));
655 i += chunk_sample_count) {
656
657 const int64_t chunk_end = min(i + chunk_sample_count,
658 abs_start_samplenum + sample_count);
659
660 const uint8_t* chunk = segment_->get_samples(i, chunk_end);
661
e91883bb 662 if (srd_session_send(srd_session_, i, chunk_end, chunk,
47747218
SA
663 (chunk_end - i) * unit_size, unit_size) != SRD_OK) {
664 error_message_ = tr("Decoder reported an error");
665 delete[] chunk;
666 break;
667 }
a3ebd556 668
47747218
SA
669 delete[] chunk;
670
671 {
672 lock_guard<mutex> lock(output_mutex_);
673 samples_decoded_ = chunk_end;
674 }
675 }
676}
677
678void DecodeSignal::decode_proc()
679{
a3ebd556
SA
680 query_input_metadata();
681
682 if (decode_interrupt_)
683 return;
684
e91883bb 685 start_srd_session();
47747218 686
27a3f09b
SA
687 uint64_t sample_count;
688 uint64_t abs_start_samplenum = 0;
47747218 689 do {
27a3f09b
SA
690 // Keep processing new samples until we exhaust the input data
691 do {
e91883bb
SA
692 // Prevent any other decode threads from accessing libsigrokdecode
693 lock_guard<mutex> srd_lock(global_srd_mutex_);
694
27a3f09b
SA
695 {
696 lock_guard<mutex> input_lock(input_mutex_);
697 sample_count = segment_->get_sample_count() - abs_start_samplenum;
698 }
699
700 if (sample_count > 0) {
e91883bb 701 decode_data(abs_start_samplenum, sample_count);
27a3f09b
SA
702 abs_start_samplenum += sample_count;
703 }
704 } while (error_message_.isEmpty() && (sample_count > 0));
705
27a3f09b 706 if (error_message_.isEmpty()) {
e91883bb
SA
707 // Make sure all annotations are known to the frontend
708 new_annotations();
709
a3ebd556 710 // Wait for new input data or an interrupt was requested
27a3f09b
SA
711 unique_lock<mutex> input_wait_lock(input_mutex_);
712 decode_input_cond_.wait(input_wait_lock);
713 }
714 } while (error_message_.isEmpty() && !decode_interrupt_);
e91883bb
SA
715}
716
717void DecodeSignal::start_srd_session()
718{
a3ebd556
SA
719 if (srd_session_)
720 stop_srd_session();
e91883bb 721
a3ebd556
SA
722 // Create the session
723 srd_session_new(&srd_session_);
724 assert(srd_session_);
47747218 725
a3ebd556
SA
726 // Create the decoders
727 srd_decoder_inst *prev_di = nullptr;
728 for (const shared_ptr<decode::Decoder> &dec : stack_) {
729 srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_);
e91883bb 730
a3ebd556
SA
731 if (!di) {
732 error_message_ = tr("Failed to create decoder instance");
733 srd_session_destroy(srd_session_);
734 return;
735 }
e91883bb 736
a3ebd556
SA
737 if (prev_di)
738 srd_inst_stack(srd_session_, prev_di, di);
e91883bb 739
a3ebd556 740 prev_di = di;
e91883bb 741 }
a3ebd556
SA
742
743 // Start the session
744 srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE,
745 g_variant_new_uint64(samplerate_));
746
747 srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN,
748 DecodeSignal::annotation_callback, this);
749
750 srd_session_start(srd_session_);
e91883bb 751}
47747218 752
e91883bb
SA
753void DecodeSignal::stop_srd_session()
754{
755 if (srd_session_) {
756 // Destroy the session
757 srd_session_destroy(srd_session_);
758 srd_session_ = nullptr;
759 }
47747218
SA
760}
761
a8a9222d
SA
762void DecodeSignal::connect_input_notifiers()
763{
764 // Disconnect the notification slot from the previous set of signals
765 disconnect(this, SLOT(on_data_received()));
766
767 // Connect the currently used signals to our slot
768 for (data::DecodeChannel &ch : channels_) {
769 if (!ch.assigned_signal)
770 continue;
771
772 shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
773 connect(logic_data.get(), SIGNAL(samples_added(QObject*, uint64_t, uint64_t)),
774 this, SLOT(on_data_received()));
775 }
776}
777
47747218
SA
778void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
779{
780 assert(pdata);
781 assert(decoder);
782
783 DecodeSignal *const ds = (DecodeSignal*)decode_signal;
784 assert(ds);
785
786 lock_guard<mutex> lock(ds->output_mutex_);
787
788 const decode::Annotation a(pdata);
789
790 // Find the row
791 assert(pdata->pdo);
792 assert(pdata->pdo->di);
793 const srd_decoder *const decc = pdata->pdo->di->decoder;
794 assert(decc);
795
796 auto row_iter = ds->rows_.end();
797
798 // Try looking up the sub-row of this class
799 const auto r = ds->class_rows_.find(make_pair(decc, a.format()));
800 if (r != ds->class_rows_.end())
801 row_iter = ds->rows_.find((*r).second);
802 else {
803 // Failing that, use the decoder as a key
804 row_iter = ds->rows_.find(Row(decc));
805 }
806
807 assert(row_iter != ds->rows_.end());
808 if (row_iter == ds->rows_.end()) {
809 qDebug() << "Unexpected annotation: decoder = " << decc <<
810 ", format = " << a.format();
811 assert(false);
812 return;
813 }
814
815 // Add the annotation
816 (*row_iter).second.push_annotation(a);
817
818 // Notify the frontend every DecodeNotifyPeriod annotations
819 if (ds->inc_annotation_count() % DecodeNotifyPeriod == 0)
820 ds->new_annotations();
821}
822
823void DecodeSignal::on_capture_state_changed(int state)
824{
825 // If a new acquisition was started, we need to start decoding from scratch
826 if (state == Session::Running)
827 begin_decode();
828}
829
830void DecodeSignal::on_data_received()
831{
832 logic_mux_cond_.notify_one();
833}
834
ad908057
SA
835} // namespace data
836} // namespace pv