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::shared_ptr;
51 using namespace pv::data::decode;
56 const double DecoderStack::DecodeMargin = 1.0;
57 const double DecoderStack::DecodeThreshold = 0.2;
58 const int64_t DecoderStack::DecodeChunkLength = 4096;
60 mutex DecoderStack::_global_decode_mutex;
62 DecoderStack::DecoderStack(pv::SigSession &session,
63 const srd_decoder *const dec) :
67 connect(&_session, SIGNAL(frame_began()), this, SLOT(on_new_frame()));
69 _stack.push_back(shared_ptr<decode::Decoder>(
70 new decode::Decoder(dec)));
73 DecoderStack::~DecoderStack()
75 if (_decode_thread.joinable()) {
76 _decode_thread.interrupt();
77 _decode_thread.join();
81 const std::list< boost::shared_ptr<decode::Decoder> >&
82 DecoderStack::stack() const
87 void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
90 _stack.push_back(decoder);
93 void DecoderStack::remove(int index)
96 assert(index < (int)_stack.size());
98 // Find the decoder in the stack
99 list< shared_ptr<Decoder> >::iterator iter = _stack.begin();
100 for(int i = 0; i < index; i++, iter++)
101 assert(iter != _stack.end());
103 // Delete the element
107 int64_t DecoderStack::samples_decoded() const
109 lock_guard<mutex> decode_lock(_output_mutex);
110 return _samples_decoded;
113 std::vector<Row> DecoderStack::get_visible_rows() const
115 lock_guard<mutex> lock(_output_mutex);
119 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
125 const srd_decoder *const decc = dec->decoder();
126 assert(dec->decoder());
128 // Add a row for the decoder if it doesn't have a row list
129 if (!decc->annotation_rows)
130 rows.push_back(Row(decc));
132 // Add the decoder rows
133 for (const GSList *l = decc->annotation_rows; l; l = l->next)
135 const srd_decoder_annotation_row *const ann_row =
136 (srd_decoder_annotation_row *)l->data;
138 rows.push_back(Row(decc, ann_row));
145 void DecoderStack::get_annotation_subset(
146 std::vector<pv::data::decode::Annotation> &dest,
147 const Row &row, uint64_t start_sample,
148 uint64_t end_sample) const
150 lock_guard<mutex> lock(_output_mutex);
152 std::map<const Row, decode::RowData>::const_iterator iter =
154 if (iter != _rows.end())
155 (*iter).second.get_annotation_subset(dest,
156 start_sample, end_sample);
159 QString DecoderStack::error_message()
161 lock_guard<mutex> lock(_output_mutex);
162 return _error_message;
165 void DecoderStack::clear()
167 _samples_decoded = 0;
168 _error_message = QString();
173 void DecoderStack::begin_decode()
175 shared_ptr<pv::view::LogicSignal> logic_signal;
176 shared_ptr<pv::data::Logic> data;
178 if (_decode_thread.joinable()) {
179 _decode_thread.interrupt();
180 _decode_thread.join();
186 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
189 const srd_decoder *const decc = dec->decoder();
190 assert(dec->decoder());
192 // Add a row for the decoder if it doesn't have a row list
193 if (!decc->annotation_rows)
194 _rows[Row(decc)] = decode::RowData();
196 // Add the decoder rows
197 for (const GSList *l = decc->annotation_rows; l; l = l->next)
199 const srd_decoder_annotation_row *const ann_row =
200 (srd_decoder_annotation_row *)l->data;
203 const Row row(decc, ann_row);
205 // Add a new empty row data object
206 _rows[row] = decode::RowData();
208 // Map out all the classes
209 for (const GSList *ll = ann_row->ann_classes;
211 _class_rows[make_pair(decc,
212 GPOINTER_TO_INT(ll->data))] = row;
216 // We get the logic data of the first probe in the list.
217 // This works because we are currently assuming all
218 // LogicSignals have the same data/snapshot
219 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
220 if (dec && !dec->probes().empty() &&
221 ((logic_signal = (*dec->probes().begin()).second)) &&
222 ((data = logic_signal->logic_data())))
228 // Get the samplerate and start time
229 _start_time = data->get_start_time();
230 _samplerate = data->samplerate();
231 if (_samplerate == 0.0)
234 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
238 uint64_t DecoderStack::get_max_sample_count() const
240 uint64_t max_sample_count = 0;
242 for (map<const Row, RowData>::const_iterator i = _rows.begin();
243 i != _rows.end(); i++)
244 max_sample_count = max(max_sample_count,
245 (*i).second.get_max_sample());
247 return max_sample_count;
250 void DecoderStack::decode_data(
251 const shared_ptr<pv::data::LogicSnapshot> &snapshot,
252 srd_session *const session)
254 uint8_t chunk[DecodeChunkLength];
256 const int64_t sample_count = snapshot->get_sample_count();
257 const unsigned int unit_size = snapshot->unit_size();
258 const unsigned int chunk_sample_count =
259 DecodeChunkLength / snapshot->unit_size();
262 !boost::this_thread::interruption_requested() &&
264 i += chunk_sample_count)
266 lock_guard<mutex> decode_lock(_global_decode_mutex);
268 const int64_t chunk_end = min(
269 i + chunk_sample_count, sample_count);
270 snapshot->get_samples(chunk, i, chunk_end);
272 if (srd_session_send(session, i, i + sample_count, chunk,
273 (chunk_end - i) * unit_size) != SRD_OK) {
274 _error_message = tr("Decoder reported an error");
279 lock_guard<mutex> lock(_output_mutex);
280 _samples_decoded = chunk_end;
286 void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
288 srd_session *session;
289 srd_decoder_inst *prev_di = NULL;
293 // Check we have a snapshot of data
294 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
295 data->get_snapshots();
296 if (snapshots.empty())
299 // Check that all decoders have the required probes
300 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
301 if (!dec->have_required_probes())
304 // Create the session
305 srd_session_new(&session);
308 // Create the decoders
309 const shared_ptr<pv::data::LogicSnapshot> &snapshot = snapshots.front();
310 const unsigned int unit_size = snapshot->unit_size();
312 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
314 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
318 _error_message = tr("Failed to create decoder instance");
319 srd_session_destroy(session);
324 srd_inst_stack (session, prev_di, di);
330 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
331 g_variant_new_uint64((uint64_t)_samplerate));
333 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
334 DecoderStack::annotation_callback, this);
336 srd_session_start(session);
338 decode_data(snapshot, session);
340 // Destroy the session
341 srd_session_destroy(session);
344 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
349 DecoderStack *const d = (DecoderStack*)decoder;
352 lock_guard<mutex> lock(d->_output_mutex);
354 const Annotation a(pdata);
358 assert(pdata->pdo->di);
359 const srd_decoder *const decc = pdata->pdo->di->decoder;
362 map<const Row, decode::RowData>::iterator row_iter = d->_rows.end();
364 // Try looking up the sub-row of this class
365 const map<pair<const srd_decoder*, int>, Row>::const_iterator r =
366 d->_class_rows.find(make_pair(decc, a.format()));
367 if (r != d->_class_rows.end())
368 row_iter = d->_rows.find((*r).second);
371 // Failing that, use the decoder as a key
372 row_iter = d->_rows.find(Row(decc));
375 assert(row_iter != d->_rows.end());
376 if (row_iter == d->_rows.end()) {
377 qDebug() << "Unexpected annotation: decoder = " << decc <<
378 ", format = " << a.format();
383 // Add the annotation
384 (*row_iter).second.push_annotation(a);
386 d->new_decode_data();
389 void DecoderStack::on_new_frame()