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;
62 mutex DecoderStack::_global_decode_mutex;
64 DecoderStack::DecoderStack(pv::SigSession &session,
65 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()) {
85 _decode_thread.interrupt();
86 _decode_thread.join();
90 const std::list< boost::shared_ptr<decode::Decoder> >&
91 DecoderStack::stack() const
96 void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
99 _stack.push_back(decoder);
102 void DecoderStack::remove(int index)
105 assert(index < (int)_stack.size());
107 // Find the decoder in the stack
108 list< shared_ptr<Decoder> >::iterator iter = _stack.begin();
109 for(int i = 0; i < index; i++, iter++)
110 assert(iter != _stack.end());
112 // Delete the element
116 int64_t DecoderStack::samples_decoded() const
118 lock_guard<mutex> decode_lock(_output_mutex);
119 return _samples_decoded;
122 std::vector<Row> DecoderStack::get_visible_rows() const
124 lock_guard<mutex> lock(_output_mutex);
128 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
134 const srd_decoder *const decc = dec->decoder();
135 assert(dec->decoder());
137 // Add a row for the decoder if it doesn't have a row list
138 if (!decc->annotation_rows)
139 rows.push_back(Row(decc));
141 // Add the decoder rows
142 for (const GSList *l = decc->annotation_rows; l; l = l->next)
144 const srd_decoder_annotation_row *const ann_row =
145 (srd_decoder_annotation_row *)l->data;
147 rows.push_back(Row(decc, ann_row));
154 void DecoderStack::get_annotation_subset(
155 std::vector<pv::data::decode::Annotation> &dest,
156 const Row &row, uint64_t start_sample,
157 uint64_t end_sample) const
159 lock_guard<mutex> lock(_output_mutex);
161 std::map<const Row, decode::RowData>::const_iterator iter =
163 if (iter != _rows.end())
164 (*iter).second.get_annotation_subset(dest,
165 start_sample, end_sample);
168 QString DecoderStack::error_message()
170 lock_guard<mutex> lock(_output_mutex);
171 return _error_message;
174 void DecoderStack::clear()
177 _frame_complete = false;
178 _samples_decoded = 0;
179 _error_message = QString();
184 void DecoderStack::begin_decode()
186 shared_ptr<pv::view::LogicSignal> logic_signal;
187 shared_ptr<pv::data::Logic> data;
189 if (_decode_thread.joinable()) {
190 _decode_thread.interrupt();
191 _decode_thread.join();
197 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
200 const srd_decoder *const decc = dec->decoder();
201 assert(dec->decoder());
203 // Add a row for the decoder if it doesn't have a row list
204 if (!decc->annotation_rows)
205 _rows[Row(decc)] = decode::RowData();
207 // Add the decoder rows
208 for (const GSList *l = decc->annotation_rows; l; l = l->next)
210 const srd_decoder_annotation_row *const ann_row =
211 (srd_decoder_annotation_row *)l->data;
214 const Row row(decc, ann_row);
216 // Add a new empty row data object
217 _rows[row] = decode::RowData();
219 // Map out all the classes
220 for (const GSList *ll = ann_row->ann_classes;
222 _class_rows[make_pair(decc,
223 GPOINTER_TO_INT(ll->data))] = row;
227 // We get the logic data of the first probe in the list.
228 // This works because we are currently assuming all
229 // LogicSignals have the same data/snapshot
230 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
231 if (dec && !dec->probes().empty() &&
232 ((logic_signal = (*dec->probes().begin()).second)) &&
233 ((data = logic_signal->logic_data())))
239 // Check we have a snapshot of data
240 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
241 data->get_snapshots();
242 if (snapshots.empty())
244 _snapshot = snapshots.front();
246 // Get the samplerate and start time
247 _start_time = data->get_start_time();
248 _samplerate = data->samplerate();
249 if (_samplerate == 0.0)
252 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
256 uint64_t DecoderStack::get_max_sample_count() const
258 uint64_t max_sample_count = 0;
260 for (map<const Row, RowData>::const_iterator i = _rows.begin();
261 i != _rows.end(); i++)
262 max_sample_count = max(max_sample_count,
263 (*i).second.get_max_sample());
265 return max_sample_count;
268 optional<int64_t> DecoderStack::wait_for_data() const
270 unique_lock<mutex> input_lock(_input_mutex);
271 while(!boost::this_thread::interruption_requested() &&
272 !_frame_complete && _samples_decoded >= _sample_count)
273 _input_cond.wait(input_lock);
274 return boost::make_optional(
275 !boost::this_thread::interruption_requested() &&
276 (_samples_decoded < _sample_count || !_frame_complete),
280 void DecoderStack::decode_data(
281 const int64_t sample_count, const unsigned int unit_size,
282 srd_session *const session)
284 uint8_t chunk[DecodeChunkLength];
286 const unsigned int chunk_sample_count =
287 DecodeChunkLength / _snapshot->unit_size();
290 !boost::this_thread::interruption_requested() &&
292 i += chunk_sample_count)
294 lock_guard<mutex> decode_lock(_global_decode_mutex);
296 const int64_t chunk_end = min(
297 i + chunk_sample_count, sample_count);
298 _snapshot->get_samples(chunk, i, chunk_end);
300 if (srd_session_send(session, i, i + sample_count, chunk,
301 (chunk_end - i) * unit_size) != SRD_OK) {
302 _error_message = tr("Decoder reported an error");
307 lock_guard<mutex> lock(_output_mutex);
308 _samples_decoded = chunk_end;
314 void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
316 optional<int64_t> sample_count;
317 srd_session *session;
318 srd_decoder_inst *prev_di = NULL;
323 // Check that all decoders have the required probes
324 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
325 if (!dec->have_required_probes())
328 // Create the session
329 srd_session_new(&session);
332 // Create the decoders
333 const unsigned int unit_size = _snapshot->unit_size();
335 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
337 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
341 _error_message = tr("Failed to create decoder instance");
342 srd_session_destroy(session);
347 srd_inst_stack (session, prev_di, di);
352 // Get the intial sample count
354 unique_lock<mutex> input_lock(_input_mutex);
355 sample_count = _sample_count = _snapshot->get_sample_count();
359 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
360 g_variant_new_uint64((uint64_t)_samplerate));
362 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
363 DecoderStack::annotation_callback, this);
365 srd_session_start(session);
368 decode_data(*sample_count, unit_size, session);
369 } while(_error_message.isEmpty() && (sample_count = wait_for_data()));
371 // Destroy the session
372 srd_session_destroy(session);
375 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
380 DecoderStack *const d = (DecoderStack*)decoder;
383 lock_guard<mutex> lock(d->_output_mutex);
385 const Annotation a(pdata);
389 assert(pdata->pdo->di);
390 const srd_decoder *const decc = pdata->pdo->di->decoder;
393 map<const Row, decode::RowData>::iterator row_iter = d->_rows.end();
395 // Try looking up the sub-row of this class
396 const map<pair<const srd_decoder*, int>, Row>::const_iterator r =
397 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);
417 d->new_decode_data();
420 void DecoderStack::on_new_frame()
425 void DecoderStack::on_data_received()
428 unique_lock<mutex> lock(_input_mutex);
429 _sample_count = _snapshot->get_sample_count();
431 _input_cond.notify_one();
434 void DecoderStack::on_frame_ended()
437 unique_lock<mutex> lock(_input_mutex);
438 _frame_complete = true;
440 _input_cond.notify_one();