Merge DecoderStack into DecodeSignal
[pulseview.git] / pv / data / decodesignal.cpp
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
20 #include <limits>
21
22 #include <QDebug>
23
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>
31 #include <pv/data/decode/row.hpp>
32 #include <pv/session.hpp>
33
34 using boost::optional;
35 using std::lock_guard;
36 using std::make_pair;
37 using std::make_shared;
38 using std::min;
39 using std::shared_ptr;
40 using std::unique_lock;
41 using pv::data::decode::Annotation;
42 using pv::data::decode::Decoder;
43 using pv::data::decode::Row;
44
45 namespace pv {
46 namespace data {
47
48 const double DecodeSignal::DecodeMargin = 1.0;
49 const double DecodeSignal::DecodeThreshold = 0.2;
50 const int64_t DecodeSignal::DecodeChunkLength = 10 * 1024 * 1024;
51 const unsigned int DecodeSignal::DecodeNotifyPeriod = 1024;
52
53 mutex DecodeSignal::global_srd_mutex_;
54
55
56 DecodeSignal::DecodeSignal(pv::Session &session) :
57         SignalBase(nullptr, SignalBase::DecodeChannel),
58         session_(session),
59         logic_mux_data_invalid_(false),
60         start_time_(0),
61         samplerate_(0),
62         sample_count_(0),
63         annotation_count_(0),
64         samples_decoded_(0),
65         frame_complete_(false)
66 {
67         connect(&session_, SIGNAL(capture_state_changed(int)),
68                 this, SLOT(on_capture_state_changed(int)));
69         connect(&session_, SIGNAL(data_received()),
70                 this, SLOT(on_data_received()));
71         connect(&session_, SIGNAL(frame_ended()),
72                 this, SLOT(on_frame_ended()));
73
74         set_name(tr("Empty decoder signal"));
75 }
76
77 DecodeSignal::~DecodeSignal()
78 {
79         if (decode_thread_.joinable()) {
80                 decode_interrupt_ = true;
81                 decode_input_cond_.notify_one();
82                 decode_thread_.join();
83         }
84
85         if (logic_mux_thread_.joinable()) {
86                 logic_mux_interrupt_ = true;
87                 logic_mux_cond_.notify_one();
88                 logic_mux_thread_.join();
89         }
90 }
91
92 const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const
93 {
94         return stack_;
95 }
96
97 void DecodeSignal::stack_decoder(srd_decoder *decoder)
98 {
99         assert(decoder);
100         stack_.push_back(make_shared<decode::Decoder>(decoder));
101
102         // Set name if this decoder is the first in the list
103         if (stack_.size() == 1)
104                 set_name(QString::fromUtf8(decoder->name));
105
106         // Include the newly created decode channels in the channel list
107         update_channel_list();
108
109         auto_assign_signals();
110         begin_decode();
111 }
112
113 void DecodeSignal::remove_decoder(int index)
114 {
115         assert(index >= 0);
116         assert(index < (int)stack_.size());
117
118         // Find the decoder in the stack
119         auto iter = stack_.begin();
120         for (int i = 0; i < index; i++, iter++)
121                 assert(iter != stack_.end());
122
123         // Delete the element
124         stack_.erase(iter);
125
126         // Update channels and decoded data
127         update_channel_list();
128         begin_decode();
129 }
130
131 bool DecodeSignal::toggle_decoder_visibility(int index)
132 {
133         auto iter = stack_.cbegin();
134         for (int i = 0; i < index; i++, iter++)
135                 assert(iter != stack_.end());
136
137         shared_ptr<Decoder> dec = *iter;
138
139         // Toggle decoder visibility
140         bool state = false;
141         if (dec) {
142                 state = !dec->shown();
143                 dec->show(state);
144         }
145
146         return state;
147 }
148
149 void DecodeSignal::reset_decode()
150 {
151         sample_count_ = 0;
152         annotation_count_ = 0;
153         frame_complete_ = false;
154         samples_decoded_ = 0;
155         error_message_ = QString();
156         rows_.clear();
157         class_rows_.clear();
158 }
159
160 void DecodeSignal::begin_decode()
161 {
162         if (decode_thread_.joinable()) {
163                 decode_interrupt_ = true;
164                 decode_input_cond_.notify_one();
165                 decode_thread_.join();
166         }
167
168         if (logic_mux_thread_.joinable()) {
169                 logic_mux_interrupt_ = true;
170                 logic_mux_cond_.notify_one();
171                 logic_mux_thread_.join();
172         }
173
174         reset_decode();
175
176         // Check that all decoders have the required channels
177         for (const shared_ptr<decode::Decoder> &dec : stack_)
178                 if (!dec->have_required_channels()) {
179                         error_message_ = tr("One or more required channels "
180                                 "have not been specified");
181                         return;
182                 }
183
184         // Add annotation classes
185         for (const shared_ptr<decode::Decoder> &dec : stack_) {
186                 assert(dec);
187                 const srd_decoder *const decc = dec->decoder();
188                 assert(dec->decoder());
189
190                 // Add a row for the decoder if it doesn't have a row list
191                 if (!decc->annotation_rows)
192                         rows_[Row(decc)] = decode::RowData();
193
194                 // Add the decoder rows
195                 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
196                         const srd_decoder_annotation_row *const ann_row =
197                                 (srd_decoder_annotation_row *)l->data;
198                         assert(ann_row);
199
200                         const Row row(decc, ann_row);
201
202                         // Add a new empty row data object
203                         rows_[row] = decode::RowData();
204
205                         // Map out all the classes
206                         for (const GSList *ll = ann_row->ann_classes;
207                                 ll; ll = ll->next)
208                                 class_rows_[make_pair(decc,
209                                         GPOINTER_TO_INT(ll->data))] = row;
210                 }
211         }
212
213         // Make sure the logic output data is complete and up-to-date
214         logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this);
215
216         // Update the samplerate and start time
217         start_time_ = segment_->start_time();
218         samplerate_ = segment_->samplerate();
219         if (samplerate_ == 0.0)
220                 samplerate_ = 1.0;
221
222         decode_interrupt_ = false;
223         decode_thread_ = std::thread(&DecodeSignal::decode_proc, this);
224 }
225
226 QString DecodeSignal::error_message() const
227 {
228         lock_guard<mutex> lock(output_mutex_);
229         return error_message_;
230 }
231
232 const vector<data::DecodeChannel> DecodeSignal::get_channels() const
233 {
234         return channels_;
235 }
236
237 void DecodeSignal::auto_assign_signals()
238 {
239         bool new_assignment = false;
240
241         // Try to auto-select channels that don't have signals assigned yet
242         for (data::DecodeChannel &ch : channels_) {
243                 if (ch.assigned_signal)
244                         continue;
245
246                 for (shared_ptr<data::SignalBase> s : session_.signalbases())
247                         if (s->logic_data() && (ch.name.toLower().contains(s->name().toLower()))) {
248                                 ch.assigned_signal = s.get();
249                                 new_assignment = true;
250                         }
251         }
252
253         if (new_assignment) {
254                 logic_mux_data_invalid_ = true;
255                 channels_updated();
256         }
257 }
258
259 void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal)
260 {
261         for (data::DecodeChannel &ch : channels_)
262                 if (ch.id == channel_id) {
263                         ch.assigned_signal = signal;
264                         logic_mux_data_invalid_ = true;
265                 }
266
267         channels_updated();
268
269         begin_decode();
270 }
271
272 void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state)
273 {
274         for (data::DecodeChannel &ch : channels_)
275                 if (ch.id == channel_id)
276                         ch.initial_pin_state = init_state;
277
278         channels_updated();
279
280         begin_decode();
281 }
282
283 double DecodeSignal::samplerate() const
284 {
285         return samplerate_;
286 }
287
288 const pv::util::Timestamp& DecodeSignal::start_time() const
289 {
290         return start_time_;
291 }
292
293 int64_t DecodeSignal::get_working_sample_count() const
294 {
295         // The working sample count is the highest sample number for
296         // which all used signals have data available, so go through
297         // all channels and use the lowest overall sample count of the
298         // current segment
299
300         // TODO Currently, we assume only a single segment exists
301
302         int64_t count = std::numeric_limits<int64_t>::max();
303         bool no_signals_assigned = true;
304
305         for (const data::DecodeChannel &ch : channels_)
306                 if (ch.assigned_signal) {
307                         no_signals_assigned = false;
308
309                         const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
310                         if (!logic_data || logic_data->logic_segments().empty())
311                                 return 0;
312
313                         const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
314                         count = min(count, (int64_t)segment->get_sample_count());
315                 }
316
317         return (no_signals_assigned ? 0 : count);
318 }
319
320 int64_t DecodeSignal::get_decoded_sample_count() const
321 {
322         lock_guard<mutex> decode_lock(output_mutex_);
323         return samples_decoded_;
324 }
325
326 vector<Row> DecodeSignal::visible_rows() const
327 {
328         lock_guard<mutex> lock(output_mutex_);
329
330         vector<Row> rows;
331
332         for (const shared_ptr<decode::Decoder> &dec : stack_) {
333                 assert(dec);
334                 if (!dec->shown())
335                         continue;
336
337                 const srd_decoder *const decc = dec->decoder();
338                 assert(dec->decoder());
339
340                 // Add a row for the decoder if it doesn't have a row list
341                 if (!decc->annotation_rows)
342                         rows.emplace_back(decc);
343
344                 // Add the decoder rows
345                 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
346                         const srd_decoder_annotation_row *const ann_row =
347                                 (srd_decoder_annotation_row *)l->data;
348                         assert(ann_row);
349                         rows.emplace_back(decc, ann_row);
350                 }
351         }
352
353         return rows;
354 }
355
356 void DecodeSignal::get_annotation_subset(
357         vector<pv::data::decode::Annotation> &dest,
358         const decode::Row &row, uint64_t start_sample,
359         uint64_t end_sample) const
360 {
361         lock_guard<mutex> lock(output_mutex_);
362
363         const auto iter = rows_.find(row);
364         if (iter != rows_.end())
365                 (*iter).second.get_annotation_subset(dest,
366                         start_sample, end_sample);
367 }
368
369 void DecodeSignal::save_settings(QSettings &settings) const
370 {
371         SignalBase::save_settings(settings);
372
373         // TODO Save decoder stack, channel mapping and decoder options
374 }
375
376 void DecodeSignal::restore_settings(QSettings &settings)
377 {
378         SignalBase::restore_settings(settings);
379
380         // TODO Restore decoder stack, channel mapping and decoder options
381 }
382
383 uint64_t DecodeSignal::inc_annotation_count()
384 {
385         return (annotation_count_++);
386 }
387
388 void DecodeSignal::update_channel_list()
389 {
390         vector<data::DecodeChannel> prev_channels = channels_;
391         channels_.clear();
392
393         uint16_t id = 0;
394
395         // Copy existing entries, create new as needed
396         for (shared_ptr<Decoder> decoder : stack_) {
397                 const srd_decoder* srd_d = decoder->decoder();
398                 const GSList *l;
399
400                 // Mandatory channels
401                 for (l = srd_d->channels; l; l = l->next) {
402                         const struct srd_channel *const pdch = (struct srd_channel *)l->data;
403                         bool ch_added = false;
404
405                         // Copy but update ID if this channel was in the list before
406                         for (data::DecodeChannel &ch : prev_channels)
407                                 if (ch.pdch_ == pdch) {
408                                         ch.id = id++;
409                                         channels_.push_back(ch);
410                                         ch_added = true;
411                                         break;
412                                 }
413
414                         if (!ch_added) {
415                                 // Create new entry without a mapped signal
416                                 data::DecodeChannel ch = {id++, false, nullptr,
417                                         QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
418                                         SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
419                                 channels_.push_back(ch);
420                         }
421                 }
422
423                 // Optional channels
424                 for (l = srd_d->opt_channels; l; l = l->next) {
425                         const struct srd_channel *const pdch = (struct srd_channel *)l->data;
426                         bool ch_added = false;
427
428                         // Copy but update ID if this channel was in the list before
429                         for (data::DecodeChannel &ch : prev_channels)
430                                 if (ch.pdch_ == pdch) {
431                                         ch.id = id++;
432                                         channels_.push_back(ch);
433                                         ch_added = true;
434                                         break;
435                                 }
436
437                         if (!ch_added) {
438                                 // Create new entry without a mapped signal
439                                 data::DecodeChannel ch = {id++, true, nullptr,
440                                         QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc),
441                                         SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch};
442                                 channels_.push_back(ch);
443                         }
444                 }
445         }
446
447         // Invalidate the logic output data if the channel assignment changed
448         if (prev_channels.size() != channels_.size()) {
449                 // The number of channels changed, there's definitely a difference
450                 logic_mux_data_invalid_ = true;
451         } else {
452                 // Same number but assignment may still differ, so compare all channels
453                 for (size_t i = 0; i < channels_.size(); i++) {
454                         const data::DecodeChannel &p_ch = prev_channels[i];
455                         const data::DecodeChannel &ch = channels_[i];
456
457                         if ((p_ch.pdch_ != ch.pdch_) ||
458                                 (p_ch.assigned_signal != ch.assigned_signal)) {
459                                 logic_mux_data_invalid_ = true;
460                                 break;
461                         }
462                 }
463
464         }
465
466         channels_updated();
467 }
468
469 void DecodeSignal::logic_mux_proc()
470 {
471
472 }
473
474 optional<int64_t> DecodeSignal::wait_for_data() const
475 {
476         unique_lock<mutex> input_lock(input_mutex_);
477
478         // Do wait if we decoded all samples but we're still capturing
479         // Do not wait if we're done capturing
480         while (!decode_interrupt_ && !frame_complete_ &&
481                 (samples_decoded_ >= sample_count_) &&
482                 (session_.get_capture_state() != Session::Stopped)) {
483
484                 decode_input_cond_.wait(input_lock);
485         }
486
487         // Return value is valid if we're not aborting the decode,
488         return boost::make_optional(!decode_interrupt_ &&
489                 // and there's more work to do...
490                 (samples_decoded_ < sample_count_ || !frame_complete_) &&
491                 // and if the end of the data hasn't been reached yet
492                 (!((samples_decoded_ >= sample_count_) && (session_.get_capture_state() == Session::Stopped))),
493                 sample_count_);
494 }
495
496 void DecodeSignal::decode_data(
497         const int64_t abs_start_samplenum, const int64_t sample_count,
498         srd_session *const session)
499 {
500         const unsigned int unit_size = segment_->unit_size();
501         const unsigned int chunk_sample_count = DecodeChunkLength / unit_size;
502
503         for (int64_t i = abs_start_samplenum;
504                 !decode_interrupt_ && (i < (abs_start_samplenum + sample_count));
505                 i += chunk_sample_count) {
506
507                 const int64_t chunk_end = min(i + chunk_sample_count,
508                         abs_start_samplenum + sample_count);
509
510                 const uint8_t* chunk = segment_->get_samples(i, chunk_end);
511
512                 if (srd_session_send(session, i, chunk_end, chunk,
513                                 (chunk_end - i) * unit_size, unit_size) != SRD_OK) {
514                         error_message_ = tr("Decoder reported an error");
515                         delete[] chunk;
516                         break;
517                 }
518                 delete[] chunk;
519
520                 {
521                         lock_guard<mutex> lock(output_mutex_);
522                         samples_decoded_ = chunk_end;
523                 }
524         }
525 }
526
527 void DecodeSignal::decode_proc()
528 {
529         optional<int64_t> sample_count;
530         srd_session *session;
531         srd_decoder_inst *prev_di = nullptr;
532
533         // Prevent any other decode threads from accessing libsigrokdecode
534         lock_guard<mutex> srd_lock(global_srd_mutex_);
535
536         // Create the session
537         srd_session_new(&session);
538         assert(session);
539
540         // Create the decoders
541         for (const shared_ptr<decode::Decoder> &dec : stack_) {
542                 srd_decoder_inst *const di = dec->create_decoder_inst(session);
543
544                 if (!di) {
545                         error_message_ = tr("Failed to create decoder instance");
546                         srd_session_destroy(session);
547                         return;
548                 }
549
550                 if (prev_di)
551                         srd_inst_stack(session, prev_di, di);
552
553                 prev_di = di;
554         }
555
556         // Get the initial sample count
557         {
558                 unique_lock<mutex> input_lock(input_mutex_);
559                 sample_count = sample_count_ = get_working_sample_count();
560         }
561
562         // Start the session
563         srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
564                 g_variant_new_uint64(samplerate_));
565
566         srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
567                 DecodeSignal::annotation_callback, this);
568
569         srd_session_start(session);
570
571         int64_t abs_start_samplenum = 0;
572         do {
573                 decode_data(abs_start_samplenum, *sample_count, session);
574                 abs_start_samplenum = *sample_count;
575         } while (error_message_.isEmpty() && (sample_count = wait_for_data()));
576
577         // Make sure all annotations are known to the frontend
578         new_annotations();
579
580         // Destroy the session
581         srd_session_destroy(session);
582 }
583
584 void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
585 {
586         assert(pdata);
587         assert(decoder);
588
589         DecodeSignal *const ds = (DecodeSignal*)decode_signal;
590         assert(ds);
591
592         lock_guard<mutex> lock(ds->output_mutex_);
593
594         const decode::Annotation a(pdata);
595
596         // Find the row
597         assert(pdata->pdo);
598         assert(pdata->pdo->di);
599         const srd_decoder *const decc = pdata->pdo->di->decoder;
600         assert(decc);
601
602         auto row_iter = ds->rows_.end();
603
604         // Try looking up the sub-row of this class
605         const auto r = ds->class_rows_.find(make_pair(decc, a.format()));
606         if (r != ds->class_rows_.end())
607                 row_iter = ds->rows_.find((*r).second);
608         else {
609                 // Failing that, use the decoder as a key
610                 row_iter = ds->rows_.find(Row(decc));
611         }
612
613         assert(row_iter != ds->rows_.end());
614         if (row_iter == ds->rows_.end()) {
615                 qDebug() << "Unexpected annotation: decoder = " << decc <<
616                         ", format = " << a.format();
617                 assert(false);
618                 return;
619         }
620
621         // Add the annotation
622         (*row_iter).second.push_annotation(a);
623
624         // Notify the frontend every DecodeNotifyPeriod annotations
625         if (ds->inc_annotation_count() % DecodeNotifyPeriod == 0)
626                 ds->new_annotations();
627 }
628
629 void DecodeSignal::on_capture_state_changed(int state)
630 {
631         // If a new acquisition was started, we need to start decoding from scratch
632         if (state == Session::Running)
633                 begin_decode();
634 }
635
636 void DecodeSignal::on_data_received()
637 {
638         logic_mux_cond_.notify_one();
639 }
640
641 void DecodeSignal::on_frame_ended()
642 {
643         logic_mux_cond_.notify_one();
644 }
645
646 } // namespace data
647 } // namespace pv