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.h"
29 #include <pv/data/logic.h>
30 #include <pv/data/logicsnapshot.h>
31 #include <pv/data/decode/decoder.h>
32 #include <pv/data/decode/annotation.h>
33 #include <pv/sigsession.h>
34 #include <pv/view/logicsignal.h>
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::SigSession &session,
63 const srd_decoder *const dec) :
66 _frame_complete(false),
69 connect(&_session, SIGNAL(frame_began()),
70 this, SLOT(on_new_frame()));
71 connect(&_session, SIGNAL(data_received()),
72 this, SLOT(on_data_received()));
73 connect(&_session, SIGNAL(frame_ended()),
74 this, SLOT(on_frame_ended()));
76 _stack.push_back(shared_ptr<decode::Decoder>(
77 new decode::Decoder(dec)));
80 DecoderStack::~DecoderStack()
82 if (_decode_thread.joinable()) {
84 _decode_thread.join();
88 const std::list< std::shared_ptr<decode::Decoder> >&
89 DecoderStack::stack() const
94 void DecoderStack::push(std::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 int64_t DecoderStack::samples_decoded() const
116 lock_guard<mutex> decode_lock(_output_mutex);
117 return _samples_decoded;
120 std::vector<Row> DecoderStack::get_visible_rows() const
122 lock_guard<mutex> lock(_output_mutex);
126 for (const shared_ptr<decode::Decoder> &dec : _stack)
132 const srd_decoder *const decc = dec->decoder();
133 assert(dec->decoder());
135 // Add a row for the decoder if it doesn't have a row list
136 if (!decc->annotation_rows)
137 rows.push_back(Row(decc));
139 // Add the decoder rows
140 for (const GSList *l = decc->annotation_rows; l; l = l->next)
142 const srd_decoder_annotation_row *const ann_row =
143 (srd_decoder_annotation_row *)l->data;
145 rows.push_back(Row(decc, ann_row));
152 void DecoderStack::get_annotation_subset(
153 std::vector<pv::data::decode::Annotation> &dest,
154 const Row &row, uint64_t start_sample,
155 uint64_t end_sample) const
157 lock_guard<mutex> lock(_output_mutex);
159 const auto iter = _rows.find(row);
160 if (iter != _rows.end())
161 (*iter).second.get_annotation_subset(dest,
162 start_sample, end_sample);
165 QString DecoderStack::error_message()
167 lock_guard<mutex> lock(_output_mutex);
168 return _error_message;
171 void DecoderStack::clear()
174 _frame_complete = false;
175 _samples_decoded = 0;
176 _error_message = QString();
181 void DecoderStack::begin_decode()
183 shared_ptr<pv::view::LogicSignal> logic_signal;
184 shared_ptr<pv::data::Logic> data;
186 if (_decode_thread.joinable()) {
188 _decode_thread.join();
193 // Check that all decoders have the required channels
194 for (const shared_ptr<decode::Decoder> &dec : _stack)
195 if (!dec->have_required_probes()) {
196 _error_message = tr("One or more required channels "
197 "have not been specified");
202 for (const shared_ptr<decode::Decoder> &dec : _stack)
205 const srd_decoder *const decc = dec->decoder();
206 assert(dec->decoder());
208 // Add a row for the decoder if it doesn't have a row list
209 if (!decc->annotation_rows)
210 _rows[Row(decc)] = decode::RowData();
212 // Add the decoder rows
213 for (const GSList *l = decc->annotation_rows; l; l = l->next)
215 const srd_decoder_annotation_row *const ann_row =
216 (srd_decoder_annotation_row *)l->data;
219 const Row row(decc, ann_row);
221 // Add a new empty row data object
222 _rows[row] = decode::RowData();
224 // Map out all the classes
225 for (const GSList *ll = ann_row->ann_classes;
227 _class_rows[make_pair(decc,
228 GPOINTER_TO_INT(ll->data))] = row;
232 // We get the logic data of the first channel in the list.
233 // This works because we are currently assuming all
234 // LogicSignals have the same data/snapshot
235 for (const shared_ptr<decode::Decoder> &dec : _stack)
236 if (dec && !dec->channels().empty() &&
237 ((logic_signal = (*dec->channels().begin()).second)) &&
238 ((data = logic_signal->logic_data())))
244 // Check we have a snapshot of data
245 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
246 data->get_snapshots();
247 if (snapshots.empty())
249 _snapshot = snapshots.front();
251 // Get the samplerate and start time
252 _start_time = data->get_start_time();
253 _samplerate = data->samplerate();
254 if (_samplerate == 0.0)
258 _decode_thread = std::thread(&DecoderStack::decode_proc, this);
261 uint64_t DecoderStack::get_max_sample_count() const
263 uint64_t max_sample_count = 0;
265 for (auto i = _rows.cbegin(); i != _rows.end(); i++)
266 max_sample_count = max(max_sample_count,
267 (*i).second.get_max_sample());
269 return max_sample_count;
272 optional<int64_t> DecoderStack::wait_for_data() const
274 unique_lock<mutex> input_lock(_input_mutex);
275 while(!_interrupt && !_frame_complete &&
276 _samples_decoded >= _sample_count)
277 _input_cond.wait(input_lock);
278 return boost::make_optional(!_interrupt &&
279 (_samples_decoded < _sample_count || !_frame_complete),
283 void DecoderStack::decode_data(
284 const int64_t sample_count, const unsigned int unit_size,
285 srd_session *const session)
287 uint8_t chunk[DecodeChunkLength];
289 const unsigned int chunk_sample_count =
290 DecodeChunkLength / _snapshot->unit_size();
292 for (int64_t i = 0; !_interrupt && i < sample_count;
293 i += chunk_sample_count)
295 lock_guard<mutex> decode_lock(_global_decode_mutex);
297 const int64_t chunk_end = min(
298 i + chunk_sample_count, sample_count);
299 _snapshot->get_samples(chunk, i, chunk_end);
301 if (srd_session_send(session, i, i + sample_count, chunk,
302 (chunk_end - i) * unit_size) != SRD_OK) {
303 _error_message = tr("Decoder reported an error");
308 lock_guard<mutex> lock(_output_mutex);
309 _samples_decoded = chunk_end;
312 if (i % DecodeNotifyPeriod == 0)
319 void DecoderStack::decode_proc()
321 optional<int64_t> sample_count;
322 srd_session *session;
323 srd_decoder_inst *prev_di = NULL;
327 // Create the session
328 srd_session_new(&session);
331 // Create the decoders
332 const unsigned int unit_size = _snapshot->unit_size();
334 for (const shared_ptr<decode::Decoder> &dec : _stack)
336 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
340 _error_message = tr("Failed to create decoder instance");
341 srd_session_destroy(session);
346 srd_inst_stack (session, prev_di, di);
351 // Get the intial sample count
353 unique_lock<mutex> input_lock(_input_mutex);
354 sample_count = _sample_count = _snapshot->get_sample_count();
358 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
359 g_variant_new_uint64((uint64_t)_samplerate));
361 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
362 DecoderStack::annotation_callback, this);
364 srd_session_start(session);
367 decode_data(*sample_count, unit_size, session);
368 } while(_error_message.isEmpty() && (sample_count = wait_for_data()));
370 // Destroy the session
371 srd_session_destroy(session);
374 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
379 DecoderStack *const d = (DecoderStack*)decoder;
382 lock_guard<mutex> lock(d->_output_mutex);
384 const Annotation a(pdata);
388 assert(pdata->pdo->di);
389 const srd_decoder *const decc = pdata->pdo->di->decoder;
392 auto row_iter = d->_rows.end();
394 // Try looking up the sub-row of this class
395 const auto r = d->_class_rows.find(make_pair(decc, a.format()));
396 if (r != d->_class_rows.end())
397 row_iter = d->_rows.find((*r).second);
400 // Failing that, use the decoder as a key
401 row_iter = d->_rows.find(Row(decc));
404 assert(row_iter != d->_rows.end());
405 if (row_iter == d->_rows.end()) {
406 qDebug() << "Unexpected annotation: decoder = " << decc <<
407 ", format = " << a.format();
412 // Add the annotation
413 (*row_iter).second.push_annotation(a);
416 void DecoderStack::on_new_frame()
421 void DecoderStack::on_data_received()
424 unique_lock<mutex> lock(_input_mutex);
426 _sample_count = _snapshot->get_sample_count();
428 _input_cond.notify_one();
431 void DecoderStack::on_frame_ended()
434 unique_lock<mutex> lock(_input_mutex);
436 _frame_complete = true;
438 _input_cond.notify_one();