2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
20 #include <libsigrokdecode/libsigrokdecode.h>
26 #include "decoderstack.hpp"
28 #include <pv/data/decode/annotation.hpp>
29 #include <pv/data/decode/decoder.hpp>
30 #include <pv/data/logic.hpp>
31 #include <pv/data/logicsegment.hpp>
32 #include <pv/session.hpp>
33 #include <pv/views/trace/logicsignal.hpp>
35 using std::lock_guard;
37 using std::unique_lock;
43 using std::shared_ptr;
44 using std::make_shared;
47 using boost::optional;
49 using namespace pv::data::decode;
54 const double DecoderStack::DecodeMargin = 1.0;
55 const double DecoderStack::DecodeThreshold = 0.2;
56 const int64_t DecoderStack::DecodeChunkLength = 10 * 1024 * 1024;
57 const unsigned int DecoderStack::DecodeNotifyPeriod = 1024;
59 mutex DecoderStack::global_srd_mutex_;
61 DecoderStack::DecoderStack(pv::Session &session,
62 const srd_decoder *const dec) :
67 frame_complete_(false),
70 connect(&session_, SIGNAL(frame_began()),
71 this, SLOT(on_new_frame()));
72 connect(&session_, SIGNAL(data_received()),
73 this, SLOT(on_data_received()));
74 connect(&session_, SIGNAL(frame_ended()),
75 this, SLOT(on_frame_ended()));
77 stack_.push_back(make_shared<decode::Decoder>(dec));
80 DecoderStack::~DecoderStack()
82 if (decode_thread_.joinable()) {
84 input_cond_.notify_one();
85 decode_thread_.join();
89 const list< shared_ptr<decode::Decoder> >& DecoderStack::stack() const
94 void DecoderStack::push(shared_ptr<decode::Decoder> decoder)
97 stack_.push_back(decoder);
100 void DecoderStack::remove(int index)
103 assert(index < (int)stack_.size());
105 // Find the decoder in the stack
106 auto iter = stack_.begin();
107 for (int i = 0; i < index; i++, iter++)
108 assert(iter != stack_.end());
110 // Delete the element
114 double DecoderStack::samplerate() const
119 const pv::util::Timestamp& DecoderStack::start_time() const
124 int64_t DecoderStack::samples_decoded() const
126 lock_guard<mutex> decode_lock(output_mutex_);
127 return samples_decoded_;
130 vector<Row> DecoderStack::get_visible_rows() const
132 lock_guard<mutex> lock(output_mutex_);
136 for (const shared_ptr<decode::Decoder> &dec : stack_) {
141 const srd_decoder *const decc = dec->decoder();
142 assert(dec->decoder());
144 // Add a row for the decoder if it doesn't have a row list
145 if (!decc->annotation_rows)
146 rows.emplace_back(decc);
148 // Add the decoder rows
149 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
150 const srd_decoder_annotation_row *const ann_row =
151 (srd_decoder_annotation_row *)l->data;
153 rows.emplace_back(decc, ann_row);
160 uint64_t DecoderStack::inc_annotation_count()
162 return (annotation_count_++);
165 void DecoderStack::get_annotation_subset(
166 vector<pv::data::decode::Annotation> &dest,
167 const Row &row, uint64_t start_sample,
168 uint64_t end_sample) const
170 lock_guard<mutex> lock(output_mutex_);
172 const auto iter = rows_.find(row);
173 if (iter != rows_.end())
174 (*iter).second.get_annotation_subset(dest,
175 start_sample, end_sample);
178 QString DecoderStack::error_message()
180 lock_guard<mutex> lock(output_mutex_);
181 return error_message_;
184 void DecoderStack::clear()
187 annotation_count_ = 0;
188 frame_complete_ = false;
189 samples_decoded_ = 0;
190 error_message_ = QString();
195 void DecoderStack::begin_decode()
197 if (decode_thread_.joinable()) {
199 input_cond_.notify_one();
200 decode_thread_.join();
205 // Check that all decoders have the required channels
206 for (const shared_ptr<decode::Decoder> &dec : stack_)
207 if (!dec->have_required_channels()) {
208 error_message_ = tr("One or more required channels "
209 "have not been specified");
214 for (const shared_ptr<decode::Decoder> &dec : stack_) {
216 const srd_decoder *const decc = dec->decoder();
217 assert(dec->decoder());
219 // Add a row for the decoder if it doesn't have a row list
220 if (!decc->annotation_rows)
221 rows_[Row(decc)] = decode::RowData();
223 // Add the decoder rows
224 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
225 const srd_decoder_annotation_row *const ann_row =
226 (srd_decoder_annotation_row *)l->data;
229 const Row row(decc, ann_row);
231 // Add a new empty row data object
232 rows_[row] = decode::RowData();
234 // Map out all the classes
235 for (const GSList *ll = ann_row->ann_classes;
237 class_rows_[make_pair(decc,
238 GPOINTER_TO_INT(ll->data))] = row;
242 // We get the logic data of the first channel in the list.
243 // This works because we are currently assuming all
244 // logic signals have the same data/segment
245 pv::data::SignalBase *signalbase;
246 pv::data::Logic *data = nullptr;
248 for (const shared_ptr<decode::Decoder> &dec : stack_)
249 if (dec && !dec->channels().empty() &&
250 ((signalbase = (*dec->channels().begin()).second.get())) &&
251 ((data = signalbase->logic_data().get())))
257 // Check we have a segment of data
258 const deque< shared_ptr<pv::data::LogicSegment> > &segments =
259 data->logic_segments();
260 if (segments.empty())
262 segment_ = segments.front();
264 // Get the samplerate and start time
265 start_time_ = segment_->start_time();
266 samplerate_ = segment_->samplerate();
267 if (samplerate_ == 0.0)
271 decode_thread_ = std::thread(&DecoderStack::decode_proc, this);
274 uint64_t DecoderStack::max_sample_count() const
276 uint64_t max_sample_count = 0;
278 for (const auto& row : rows_)
279 max_sample_count = max(max_sample_count,
280 row.second.get_max_sample());
282 return max_sample_count;
285 optional<int64_t> DecoderStack::wait_for_data() const
287 unique_lock<mutex> input_lock(input_mutex_);
289 // Do wait if we decoded all samples but we're still capturing
290 // Do not wait if we're done capturing
291 while (!interrupt_ && !frame_complete_ &&
292 (samples_decoded_ >= sample_count_) &&
293 (session_.get_capture_state() != Session::Stopped)) {
295 input_cond_.wait(input_lock);
298 // Return value is valid if we're not aborting the decode,
299 return boost::make_optional(!interrupt_ &&
300 // and there's more work to do...
301 (samples_decoded_ < sample_count_ || !frame_complete_) &&
302 // and if the end of the data hasn't been reached yet
303 (!((samples_decoded_ >= sample_count_) && (session_.get_capture_state() == Session::Stopped))),
307 void DecoderStack::decode_data(
308 const int64_t abs_start_samplenum, const int64_t sample_count, const unsigned int unit_size,
309 srd_session *const session)
311 const unsigned int chunk_sample_count =
312 DecodeChunkLength / segment_->unit_size();
314 for (int64_t i = abs_start_samplenum; !interrupt_ && i < sample_count;
315 i += chunk_sample_count) {
317 const int64_t chunk_end = min(
318 i + chunk_sample_count, sample_count);
319 const uint8_t* chunk = segment_->get_samples(i, chunk_end);
321 if (srd_session_send(session, i, chunk_end, chunk,
322 (chunk_end - i) * unit_size, unit_size) != SRD_OK) {
323 error_message_ = tr("Decoder reported an error");
330 lock_guard<mutex> lock(output_mutex_);
331 samples_decoded_ = chunk_end;
336 void DecoderStack::decode_proc()
338 optional<int64_t> sample_count;
339 srd_session *session;
340 srd_decoder_inst *prev_di = nullptr;
344 // Prevent any other decode threads from accessing libsigrokdecode
345 lock_guard<mutex> srd_lock(global_srd_mutex_);
347 // Create the session
348 srd_session_new(&session);
351 // Create the decoders
352 const unsigned int unit_size = segment_->unit_size();
354 for (const shared_ptr<decode::Decoder> &dec : stack_) {
355 srd_decoder_inst *const di = dec->create_decoder_inst(session);
358 error_message_ = tr("Failed to create decoder instance");
359 srd_session_destroy(session);
364 srd_inst_stack (session, prev_di, di);
369 // Get the intial sample count
371 unique_lock<mutex> input_lock(input_mutex_);
372 sample_count = sample_count_ = segment_->get_sample_count();
376 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
377 g_variant_new_uint64((uint64_t)samplerate_));
379 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
380 DecoderStack::annotation_callback, this);
382 srd_session_start(session);
384 int64_t abs_start_samplenum = 0;
386 decode_data(abs_start_samplenum, *sample_count, unit_size, session);
387 abs_start_samplenum = *sample_count;
388 } while (error_message_.isEmpty() && (sample_count = wait_for_data()));
390 // Make sure all annotations are known to the frontend
393 // Destroy the session
394 srd_session_destroy(session);
397 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder_stack)
402 DecoderStack *const ds = (DecoderStack*)decoder_stack;
405 lock_guard<mutex> lock(ds->output_mutex_);
407 const Annotation a(pdata);
411 assert(pdata->pdo->di);
412 const srd_decoder *const decc = pdata->pdo->di->decoder;
415 auto row_iter = ds->rows_.end();
417 // Try looking up the sub-row of this class
418 const auto r = ds->class_rows_.find(make_pair(decc, a.format()));
419 if (r != ds->class_rows_.end())
420 row_iter = ds->rows_.find((*r).second);
422 // Failing that, use the decoder as a key
423 row_iter = ds->rows_.find(Row(decc));
426 assert(row_iter != ds->rows_.end());
427 if (row_iter == ds->rows_.end()) {
428 qDebug() << "Unexpected annotation: decoder = " << decc <<
429 ", format = " << a.format();
434 // Add the annotation
435 (*row_iter).second.push_annotation(a);
437 // Notify the frontend every DecodeNotifyPeriod annotations
438 if (ds->inc_annotation_count() % DecodeNotifyPeriod == 0)
439 ds->new_annotations();
442 void DecoderStack::on_new_frame()
447 void DecoderStack::on_data_received()
450 unique_lock<mutex> lock(input_mutex_);
452 sample_count_ = segment_->get_sample_count();
454 input_cond_.notify_one();
457 void DecoderStack::on_frame_ended()
460 unique_lock<mutex> lock(input_mutex_);
462 frame_complete_ = true;
464 input_cond_.notify_one();