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>
23 #include <boost/foreach.hpp>
24 #include <boost/thread/thread.hpp>
30 #include "decoderstack.h"
32 #include <pv/data/logic.h>
33 #include <pv/data/logicsnapshot.h>
34 #include <pv/data/decode/decoder.h>
35 #include <pv/data/decode/annotation.h>
36 #include <pv/sigsession.h>
37 #include <pv/view/logicsignal.h>
39 using boost::lock_guard;
41 using boost::optional;
42 using boost::shared_ptr;
43 using boost::unique_lock;
53 using namespace pv::data::decode;
58 const double DecoderStack::DecodeMargin = 1.0;
59 const double DecoderStack::DecodeThreshold = 0.2;
60 const int64_t DecoderStack::DecodeChunkLength = 4096;
61 const unsigned int DecoderStack::DecodeNotifyPeriod = 65536;
63 mutex DecoderStack::_global_decode_mutex;
65 DecoderStack::DecoderStack(pv::SigSession &session,
66 const srd_decoder *const dec) :
69 _frame_complete(false),
72 connect(&_session, SIGNAL(frame_began()),
73 this, SLOT(on_new_frame()));
74 connect(&_session, SIGNAL(data_received()),
75 this, SLOT(on_data_received()));
76 connect(&_session, SIGNAL(frame_ended()),
77 this, SLOT(on_frame_ended()));
79 _stack.push_back(shared_ptr<decode::Decoder>(
80 new decode::Decoder(dec)));
83 DecoderStack::~DecoderStack()
85 if (_decode_thread.joinable()) {
86 _decode_thread.interrupt();
87 _decode_thread.join();
91 const std::list< boost::shared_ptr<decode::Decoder> >&
92 DecoderStack::stack() const
97 void DecoderStack::push(boost::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 list< shared_ptr<Decoder> >::iterator iter = _stack.begin();
110 for(int i = 0; i < index; i++, iter++)
111 assert(iter != _stack.end());
113 // Delete the element
117 int64_t DecoderStack::samples_decoded() const
119 lock_guard<mutex> decode_lock(_output_mutex);
120 return _samples_decoded;
123 std::vector<Row> DecoderStack::get_visible_rows() const
125 lock_guard<mutex> lock(_output_mutex);
129 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
135 const srd_decoder *const decc = dec->decoder();
136 assert(dec->decoder());
138 // Add a row for the decoder if it doesn't have a row list
139 if (!decc->annotation_rows)
140 rows.push_back(Row(decc));
142 // Add the decoder rows
143 for (const GSList *l = decc->annotation_rows; l; l = l->next)
145 const srd_decoder_annotation_row *const ann_row =
146 (srd_decoder_annotation_row *)l->data;
148 rows.push_back(Row(decc, ann_row));
155 void DecoderStack::get_annotation_subset(
156 std::vector<pv::data::decode::Annotation> &dest,
157 const Row &row, uint64_t start_sample,
158 uint64_t end_sample) const
160 lock_guard<mutex> lock(_output_mutex);
162 std::map<const Row, decode::RowData>::const_iterator iter =
164 if (iter != _rows.end())
165 (*iter).second.get_annotation_subset(dest,
166 start_sample, end_sample);
169 QString DecoderStack::error_message()
171 lock_guard<mutex> lock(_output_mutex);
172 return _error_message;
175 void DecoderStack::clear()
178 _frame_complete = false;
179 _samples_decoded = 0;
180 _error_message = QString();
185 void DecoderStack::begin_decode()
187 shared_ptr<pv::view::LogicSignal> logic_signal;
188 shared_ptr<pv::data::Logic> data;
190 if (_decode_thread.joinable()) {
191 _decode_thread.interrupt();
192 _decode_thread.join();
197 // Check that all decoders have the required probes
198 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
199 if (!dec->have_required_probes()) {
200 _error_message = tr("One or more required probes "
201 "have not been specified");
206 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
209 const srd_decoder *const decc = dec->decoder();
210 assert(dec->decoder());
212 // Add a row for the decoder if it doesn't have a row list
213 if (!decc->annotation_rows)
214 _rows[Row(decc)] = decode::RowData();
216 // Add the decoder rows
217 for (const GSList *l = decc->annotation_rows; l; l = l->next)
219 const srd_decoder_annotation_row *const ann_row =
220 (srd_decoder_annotation_row *)l->data;
223 const Row row(decc, ann_row);
225 // Add a new empty row data object
226 _rows[row] = decode::RowData();
228 // Map out all the classes
229 for (const GSList *ll = ann_row->ann_classes;
231 _class_rows[make_pair(decc,
232 GPOINTER_TO_INT(ll->data))] = row;
236 // We get the logic data of the first probe in the list.
237 // This works because we are currently assuming all
238 // LogicSignals have the same data/snapshot
239 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
240 if (dec && !dec->probes().empty() &&
241 ((logic_signal = (*dec->probes().begin()).second)) &&
242 ((data = logic_signal->logic_data())))
248 // Check we have a snapshot of data
249 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
250 data->get_snapshots();
251 if (snapshots.empty())
253 _snapshot = snapshots.front();
255 // Get the samplerate and start time
256 _start_time = data->get_start_time();
257 _samplerate = data->samplerate();
258 if (_samplerate == 0.0)
261 _decode_thread = boost::thread(&DecoderStack::decode_proc, this);
264 uint64_t DecoderStack::get_max_sample_count() const
266 uint64_t max_sample_count = 0;
268 for (map<const Row, RowData>::const_iterator i = _rows.begin();
269 i != _rows.end(); i++)
270 max_sample_count = max(max_sample_count,
271 (*i).second.get_max_sample());
273 return max_sample_count;
276 optional<int64_t> DecoderStack::wait_for_data() const
278 unique_lock<mutex> input_lock(_input_mutex);
279 while(!boost::this_thread::interruption_requested() &&
280 !_frame_complete && _samples_decoded >= _sample_count)
281 _input_cond.wait(input_lock);
282 return boost::make_optional(
283 !boost::this_thread::interruption_requested() &&
284 (_samples_decoded < _sample_count || !_frame_complete),
288 void DecoderStack::decode_data(
289 const int64_t sample_count, const unsigned int unit_size,
290 srd_session *const session)
292 uint8_t chunk[DecodeChunkLength];
294 const unsigned int chunk_sample_count =
295 DecodeChunkLength / _snapshot->unit_size();
298 !boost::this_thread::interruption_requested() &&
300 i += chunk_sample_count)
302 lock_guard<mutex> decode_lock(_global_decode_mutex);
304 const int64_t chunk_end = min(
305 i + chunk_sample_count, sample_count);
306 _snapshot->get_samples(chunk, i, chunk_end);
308 if (srd_session_send(session, i, i + sample_count, chunk,
309 (chunk_end - i) * unit_size) != SRD_OK) {
310 _error_message = tr("Decoder reported an error");
315 lock_guard<mutex> lock(_output_mutex);
316 _samples_decoded = chunk_end;
319 if (i % DecodeNotifyPeriod == 0)
326 void DecoderStack::decode_proc()
328 optional<int64_t> sample_count;
329 srd_session *session;
330 srd_decoder_inst *prev_di = NULL;
334 // Create the session
335 srd_session_new(&session);
338 // Create the decoders
339 const unsigned int unit_size = _snapshot->unit_size();
341 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
343 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
347 _error_message = tr("Failed to create decoder instance");
348 srd_session_destroy(session);
353 srd_inst_stack (session, prev_di, di);
358 // Get the intial sample count
360 unique_lock<mutex> input_lock(_input_mutex);
361 sample_count = _sample_count = _snapshot->get_sample_count();
365 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
366 g_variant_new_uint64((uint64_t)_samplerate));
368 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
369 DecoderStack::annotation_callback, this);
371 srd_session_start(session);
374 decode_data(*sample_count, unit_size, session);
375 } while(_error_message.isEmpty() && (sample_count = wait_for_data()));
377 // Destroy the session
378 srd_session_destroy(session);
381 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
386 DecoderStack *const d = (DecoderStack*)decoder;
389 lock_guard<mutex> lock(d->_output_mutex);
391 const Annotation a(pdata);
395 assert(pdata->pdo->di);
396 const srd_decoder *const decc = pdata->pdo->di->decoder;
399 map<const Row, decode::RowData>::iterator row_iter = d->_rows.end();
401 // Try looking up the sub-row of this class
402 const map<pair<const srd_decoder*, int>, Row>::const_iterator r =
403 d->_class_rows.find(make_pair(decc, a.format()));
404 if (r != d->_class_rows.end())
405 row_iter = d->_rows.find((*r).second);
408 // Failing that, use the decoder as a key
409 row_iter = d->_rows.find(Row(decc));
412 assert(row_iter != d->_rows.end());
413 if (row_iter == d->_rows.end()) {
414 qDebug() << "Unexpected annotation: decoder = " << decc <<
415 ", format = " << a.format();
420 // Add the annotation
421 (*row_iter).second.push_annotation(a);
424 void DecoderStack::on_new_frame()
429 void DecoderStack::on_data_received()
432 unique_lock<mutex> lock(_input_mutex);
434 _sample_count = _snapshot->get_sample_count();
436 _input_cond.notify_one();
439 void DecoderStack::on_frame_ended()
442 unique_lock<mutex> lock(_input_mutex);
444 _frame_complete = true;
446 _input_cond.notify_one();