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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <libsigrokdecode/libsigrokdecode.h>
27 #include "decoderstack.hpp"
29 #include <pv/data/logic.hpp>
30 #include <pv/data/logicsegment.hpp>
31 #include <pv/data/decode/decoder.hpp>
32 #include <pv/data/decode/annotation.hpp>
33 #include <pv/session.hpp>
34 #include <pv/view/logicsignal.hpp>
36 using std::lock_guard;
38 using boost::optional;
39 using std::unique_lock;
47 using std::shared_ptr;
50 using namespace pv::data::decode;
55 const double DecoderStack::DecodeMargin = 1.0;
56 const double DecoderStack::DecodeThreshold = 0.2;
57 const int64_t DecoderStack::DecodeChunkLength = 4096;
58 const unsigned int DecoderStack::DecodeNotifyPeriod = 65536;
60 mutex DecoderStack::global_decode_mutex_;
62 DecoderStack::DecoderStack(pv::Session &session,
63 const srd_decoder *const dec) :
68 frame_complete_(false),
71 connect(&session_, SIGNAL(frame_began()),
72 this, SLOT(on_new_frame()));
73 connect(&session_, SIGNAL(data_received()),
74 this, SLOT(on_data_received()));
75 connect(&session_, SIGNAL(frame_ended()),
76 this, SLOT(on_frame_ended()));
78 stack_.push_back(shared_ptr<decode::Decoder>(
79 new decode::Decoder(dec)));
82 DecoderStack::~DecoderStack()
84 if (decode_thread_.joinable()) {
86 input_cond_.notify_one();
87 decode_thread_.join();
91 const std::list< std::shared_ptr<decode::Decoder> >&
92 DecoderStack::stack() const
97 void DecoderStack::push(std::shared_ptr<decode::Decoder> decoder)
100 stack_.push_back(decoder);
103 void DecoderStack::remove(int index)
106 assert(index < (int)stack_.size());
108 // Find the decoder in the stack
109 auto iter = stack_.begin();
110 for(int i = 0; i < index; i++, iter++)
111 assert(iter != stack_.end());
113 // Delete the element
117 double DecoderStack::samplerate() const
122 double DecoderStack::start_time() const
127 int64_t DecoderStack::samples_decoded() const
129 lock_guard<mutex> decode_lock(output_mutex_);
130 return samples_decoded_;
133 std::vector<Row> DecoderStack::get_visible_rows() const
135 lock_guard<mutex> lock(output_mutex_);
139 for (const shared_ptr<decode::Decoder> &dec : stack_)
145 const srd_decoder *const decc = dec->decoder();
146 assert(dec->decoder());
148 // Add a row for the decoder if it doesn't have a row list
149 if (!decc->annotation_rows)
150 rows.push_back(Row(decc));
152 // Add the decoder rows
153 for (const GSList *l = decc->annotation_rows; l; l = l->next)
155 const srd_decoder_annotation_row *const ann_row =
156 (srd_decoder_annotation_row *)l->data;
158 rows.push_back(Row(decc, ann_row));
165 void DecoderStack::get_annotation_subset(
166 std::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 frame_complete_ = false;
188 samples_decoded_ = 0;
189 error_message_ = QString();
194 void DecoderStack::begin_decode()
196 shared_ptr<pv::view::LogicSignal> logic_signal;
197 shared_ptr<pv::data::Logic> data;
199 if (decode_thread_.joinable()) {
201 input_cond_.notify_one();
202 decode_thread_.join();
207 // Check that all decoders have the required channels
208 for (const shared_ptr<decode::Decoder> &dec : stack_)
209 if (!dec->have_required_channels()) {
210 error_message_ = tr("One or more required channels "
211 "have not been specified");
216 for (const shared_ptr<decode::Decoder> &dec : stack_)
219 const srd_decoder *const decc = dec->decoder();
220 assert(dec->decoder());
222 // Add a row for the decoder if it doesn't have a row list
223 if (!decc->annotation_rows)
224 rows_[Row(decc)] = decode::RowData();
226 // Add the decoder rows
227 for (const GSList *l = decc->annotation_rows; l; l = l->next)
229 const srd_decoder_annotation_row *const ann_row =
230 (srd_decoder_annotation_row *)l->data;
233 const Row row(decc, ann_row);
235 // Add a new empty row data object
236 rows_[row] = decode::RowData();
238 // Map out all the classes
239 for (const GSList *ll = ann_row->ann_classes;
241 class_rows_[make_pair(decc,
242 GPOINTER_TO_INT(ll->data))] = row;
246 // We get the logic data of the first channel in the list.
247 // This works because we are currently assuming all
248 // LogicSignals have the same data/segment
249 for (const shared_ptr<decode::Decoder> &dec : stack_)
250 if (dec && !dec->channels().empty() &&
251 ((logic_signal = (*dec->channels().begin()).second)) &&
252 ((data = logic_signal->logic_data())))
258 // Check we have a segment of data
259 const deque< shared_ptr<pv::data::LogicSegment> > &segments =
260 data->logic_segments();
261 if (segments.empty())
263 segment_ = segments.front();
265 // Get the samplerate and start time
266 start_time_ = segment_->start_time();
267 samplerate_ = segment_->samplerate();
268 if (samplerate_ == 0.0)
272 decode_thread_ = std::thread(&DecoderStack::decode_proc, this);
275 uint64_t DecoderStack::max_sample_count() const
277 uint64_t max_sample_count = 0;
279 for (auto i = rows_.cbegin(); i != rows_.end(); i++)
280 max_sample_count = max(max_sample_count,
281 (*i).second.get_max_sample());
283 return max_sample_count;
286 optional<int64_t> DecoderStack::wait_for_data() const
288 unique_lock<mutex> input_lock(input_mutex_);
289 while(!interrupt_ && !frame_complete_ &&
290 samples_decoded_ >= sample_count_)
291 input_cond_.wait(input_lock);
292 return boost::make_optional(!interrupt_ &&
293 (samples_decoded_ < sample_count_ || !frame_complete_),
297 void DecoderStack::decode_data(
298 const int64_t sample_count, const unsigned int unit_size,
299 srd_session *const session)
301 uint8_t chunk[DecodeChunkLength];
303 const unsigned int chunk_sample_count =
304 DecodeChunkLength / segment_->unit_size();
306 for (int64_t i = 0; !interrupt_ && i < sample_count;
307 i += chunk_sample_count)
309 lock_guard<mutex> decode_lock(global_decode_mutex_);
311 const int64_t chunk_end = min(
312 i + chunk_sample_count, sample_count);
313 segment_->get_samples(chunk, i, chunk_end);
315 if (srd_session_send(session, i, i + sample_count, chunk,
316 (chunk_end - i) * unit_size) != SRD_OK) {
317 error_message_ = tr("Decoder reported an error");
322 lock_guard<mutex> lock(output_mutex_);
323 samples_decoded_ = chunk_end;
326 if (i % DecodeNotifyPeriod == 0)
333 void DecoderStack::decode_proc()
335 optional<int64_t> sample_count;
336 srd_session *session;
337 srd_decoder_inst *prev_di = nullptr;
341 // Create the session
342 srd_session_new(&session);
345 // Create the decoders
346 const unsigned int unit_size = segment_->unit_size();
348 for (const shared_ptr<decode::Decoder> &dec : stack_)
350 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
354 error_message_ = tr("Failed to create decoder instance");
355 srd_session_destroy(session);
360 srd_inst_stack (session, prev_di, di);
365 // Get the intial sample count
367 unique_lock<mutex> input_lock(input_mutex_);
368 sample_count = sample_count_ = segment_->get_sample_count();
372 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
373 g_variant_new_uint64((uint64_t)samplerate_));
375 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
376 DecoderStack::annotation_callback, this);
378 srd_session_start(session);
381 decode_data(*sample_count, unit_size, session);
382 } while(error_message_.isEmpty() && (sample_count = wait_for_data()));
384 // Destroy the session
385 srd_session_destroy(session);
388 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
393 DecoderStack *const d = (DecoderStack*)decoder;
396 lock_guard<mutex> lock(d->output_mutex_);
398 const Annotation a(pdata);
402 assert(pdata->pdo->di);
403 const srd_decoder *const decc = pdata->pdo->di->decoder;
406 auto row_iter = d->rows_.end();
408 // Try looking up the sub-row of this class
409 const auto r = d->class_rows_.find(make_pair(decc, a.format()));
410 if (r != d->class_rows_.end())
411 row_iter = d->rows_.find((*r).second);
414 // Failing that, use the decoder as a key
415 row_iter = d->rows_.find(Row(decc));
418 assert(row_iter != d->rows_.end());
419 if (row_iter == d->rows_.end()) {
420 qDebug() << "Unexpected annotation: decoder = " << decc <<
421 ", format = " << a.format();
426 // Add the annotation
427 (*row_iter).second.push_annotation(a);
430 void DecoderStack::on_new_frame()
435 void DecoderStack::on_data_received()
438 unique_lock<mutex> lock(input_mutex_);
440 sample_count_ = segment_->get_sample_count();
442 input_cond_.notify_one();
445 void DecoderStack::on_frame_ended()
448 unique_lock<mutex> lock(input_mutex_);
450 frame_complete_ = true;
452 input_cond_.notify_one();