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 _input_cond.notify_one();
85 _decode_thread.join();
89 const std::list< std::shared_ptr<decode::Decoder> >&
90 DecoderStack::stack() const
95 void DecoderStack::push(std::shared_ptr<decode::Decoder> decoder)
98 _stack.push_back(decoder);
101 void DecoderStack::remove(int index)
104 assert(index < (int)_stack.size());
106 // Find the decoder in the stack
107 auto iter = _stack.begin();
108 for(int i = 0; i < index; i++, iter++)
109 assert(iter != _stack.end());
111 // Delete the element
115 int64_t DecoderStack::samples_decoded() const
117 lock_guard<mutex> decode_lock(_output_mutex);
118 return _samples_decoded;
121 std::vector<Row> DecoderStack::get_visible_rows() const
123 lock_guard<mutex> lock(_output_mutex);
127 for (const shared_ptr<decode::Decoder> &dec : _stack)
133 const srd_decoder *const decc = dec->decoder();
134 assert(dec->decoder());
136 // Add a row for the decoder if it doesn't have a row list
137 if (!decc->annotation_rows)
138 rows.push_back(Row(decc));
140 // Add the decoder rows
141 for (const GSList *l = decc->annotation_rows; l; l = l->next)
143 const srd_decoder_annotation_row *const ann_row =
144 (srd_decoder_annotation_row *)l->data;
146 rows.push_back(Row(decc, ann_row));
153 void DecoderStack::get_annotation_subset(
154 std::vector<pv::data::decode::Annotation> &dest,
155 const Row &row, uint64_t start_sample,
156 uint64_t end_sample) const
158 lock_guard<mutex> lock(_output_mutex);
160 const auto iter = _rows.find(row);
161 if (iter != _rows.end())
162 (*iter).second.get_annotation_subset(dest,
163 start_sample, end_sample);
166 QString DecoderStack::error_message()
168 lock_guard<mutex> lock(_output_mutex);
169 return _error_message;
172 void DecoderStack::clear()
175 _frame_complete = false;
176 _samples_decoded = 0;
177 _error_message = QString();
182 void DecoderStack::begin_decode()
184 shared_ptr<pv::view::LogicSignal> logic_signal;
185 shared_ptr<pv::data::Logic> data;
187 if (_decode_thread.joinable()) {
189 _input_cond.notify_one();
190 _decode_thread.join();
195 // Check that all decoders have the required channels
196 for (const shared_ptr<decode::Decoder> &dec : _stack)
197 if (!dec->have_required_probes()) {
198 _error_message = tr("One or more required channels "
199 "have not been specified");
204 for (const shared_ptr<decode::Decoder> &dec : _stack)
207 const srd_decoder *const decc = dec->decoder();
208 assert(dec->decoder());
210 // Add a row for the decoder if it doesn't have a row list
211 if (!decc->annotation_rows)
212 _rows[Row(decc)] = decode::RowData();
214 // Add the decoder rows
215 for (const GSList *l = decc->annotation_rows; l; l = l->next)
217 const srd_decoder_annotation_row *const ann_row =
218 (srd_decoder_annotation_row *)l->data;
221 const Row row(decc, ann_row);
223 // Add a new empty row data object
224 _rows[row] = decode::RowData();
226 // Map out all the classes
227 for (const GSList *ll = ann_row->ann_classes;
229 _class_rows[make_pair(decc,
230 GPOINTER_TO_INT(ll->data))] = row;
234 // We get the logic data of the first channel in the list.
235 // This works because we are currently assuming all
236 // LogicSignals have the same data/snapshot
237 for (const shared_ptr<decode::Decoder> &dec : _stack)
238 if (dec && !dec->channels().empty() &&
239 ((logic_signal = (*dec->channels().begin()).second)) &&
240 ((data = logic_signal->logic_data())))
246 // Check we have a snapshot of data
247 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
248 data->get_snapshots();
249 if (snapshots.empty())
251 _snapshot = snapshots.front();
253 // Get the samplerate and start time
254 _start_time = data->get_start_time();
255 _samplerate = data->samplerate();
256 if (_samplerate == 0.0)
260 _decode_thread = std::thread(&DecoderStack::decode_proc, this);
263 uint64_t DecoderStack::get_max_sample_count() const
265 uint64_t max_sample_count = 0;
267 for (auto i = _rows.cbegin(); i != _rows.end(); i++)
268 max_sample_count = max(max_sample_count,
269 (*i).second.get_max_sample());
271 return max_sample_count;
274 optional<int64_t> DecoderStack::wait_for_data() const
276 unique_lock<mutex> input_lock(_input_mutex);
277 while(!_interrupt && !_frame_complete &&
278 _samples_decoded >= _sample_count)
279 _input_cond.wait(input_lock);
280 return boost::make_optional(!_interrupt &&
281 (_samples_decoded < _sample_count || !_frame_complete),
285 void DecoderStack::decode_data(
286 const int64_t sample_count, const unsigned int unit_size,
287 srd_session *const session)
289 uint8_t chunk[DecodeChunkLength];
291 const unsigned int chunk_sample_count =
292 DecodeChunkLength / _snapshot->unit_size();
294 for (int64_t i = 0; !_interrupt && i < sample_count;
295 i += chunk_sample_count)
297 lock_guard<mutex> decode_lock(_global_decode_mutex);
299 const int64_t chunk_end = min(
300 i + chunk_sample_count, sample_count);
301 _snapshot->get_samples(chunk, i, chunk_end);
303 if (srd_session_send(session, i, i + sample_count, chunk,
304 (chunk_end - i) * unit_size) != SRD_OK) {
305 _error_message = tr("Decoder reported an error");
310 lock_guard<mutex> lock(_output_mutex);
311 _samples_decoded = chunk_end;
314 if (i % DecodeNotifyPeriod == 0)
321 void DecoderStack::decode_proc()
323 optional<int64_t> sample_count;
324 srd_session *session;
325 srd_decoder_inst *prev_di = NULL;
329 // Create the session
330 srd_session_new(&session);
333 // Create the decoders
334 const unsigned int unit_size = _snapshot->unit_size();
336 for (const shared_ptr<decode::Decoder> &dec : _stack)
338 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
342 _error_message = tr("Failed to create decoder instance");
343 srd_session_destroy(session);
348 srd_inst_stack (session, prev_di, di);
353 // Get the intial sample count
355 unique_lock<mutex> input_lock(_input_mutex);
356 sample_count = _sample_count = _snapshot->get_sample_count();
360 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
361 g_variant_new_uint64((uint64_t)_samplerate));
363 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
364 DecoderStack::annotation_callback, this);
366 srd_session_start(session);
369 decode_data(*sample_count, unit_size, session);
370 } while(_error_message.isEmpty() && (sample_count = wait_for_data()));
372 // Destroy the session
373 srd_session_destroy(session);
376 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
381 DecoderStack *const d = (DecoderStack*)decoder;
384 lock_guard<mutex> lock(d->_output_mutex);
386 const Annotation a(pdata);
390 assert(pdata->pdo->di);
391 const srd_decoder *const decc = pdata->pdo->di->decoder;
394 auto row_iter = d->_rows.end();
396 // Try looking up the sub-row of this class
397 const auto r = d->_class_rows.find(make_pair(decc, a.format()));
398 if (r != d->_class_rows.end())
399 row_iter = d->_rows.find((*r).second);
402 // Failing that, use the decoder as a key
403 row_iter = d->_rows.find(Row(decc));
406 assert(row_iter != d->_rows.end());
407 if (row_iter == d->_rows.end()) {
408 qDebug() << "Unexpected annotation: decoder = " << decc <<
409 ", format = " << a.format();
414 // Add the annotation
415 (*row_iter).second.push_annotation(a);
418 void DecoderStack::on_new_frame()
423 void DecoderStack::on_data_received()
426 unique_lock<mutex> lock(_input_mutex);
428 _sample_count = _snapshot->get_sample_count();
430 _input_cond.notify_one();
433 void DecoderStack::on_frame_ended()
436 unique_lock<mutex> lock(_input_mutex);
438 _frame_complete = true;
440 _input_cond.notify_one();