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(_mutex);
110 return _samples_decoded;
113 std::vector<Row> DecoderStack::get_visible_rows() const
115 lock_guard<mutex> lock(_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(_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(_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_proc(shared_ptr<data::Logic> data)
252 srd_session *session;
253 uint8_t chunk[DecodeChunkLength];
254 srd_decoder_inst *prev_di = NULL;
258 // Check we have a snapshot of data
259 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
260 data->get_snapshots();
261 if (snapshots.empty())
264 // Check that all decoders have the required probes
265 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
266 if (!dec->have_required_probes())
269 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
271 const int64_t sample_count = snapshot->get_sample_count();
272 const unsigned int unit_size = snapshot->unit_size();
273 const unsigned int chunk_sample_count =
274 DecodeChunkLength / unit_size;
276 // Create the session
277 srd_session_new(&session);
280 // Create the decoders
281 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
283 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
287 _error_message = tr("Failed to create decoder instance");
288 srd_session_destroy(session);
293 srd_inst_stack (session, prev_di, di);
299 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
300 g_variant_new_uint64((uint64_t)_samplerate));
302 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
303 DecoderStack::annotation_callback, this);
305 srd_session_start(session);
308 !boost::this_thread::interruption_requested() &&
310 i += chunk_sample_count)
312 lock_guard<mutex> decode_lock(_global_decode_mutex);
314 const int64_t chunk_end = min(
315 i + chunk_sample_count, sample_count);
316 snapshot->get_samples(chunk, i, chunk_end);
318 if (srd_session_send(session, i, i + sample_count, chunk,
319 (chunk_end - i) * unit_size) != SRD_OK) {
320 _error_message = tr("Decoder reported an error");
325 lock_guard<mutex> lock(_mutex);
326 _samples_decoded = chunk_end;
330 // Destroy the session
331 srd_session_destroy(session);
334 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
339 DecoderStack *const d = (DecoderStack*)decoder;
342 lock_guard<mutex> lock(d->_mutex);
344 const Annotation a(pdata);
348 assert(pdata->pdo->di);
349 const srd_decoder *const decc = pdata->pdo->di->decoder;
352 map<const Row, decode::RowData>::iterator row_iter = d->_rows.end();
354 // Try looking up the sub-row of this class
355 const map<pair<const srd_decoder*, int>, Row>::const_iterator r =
356 d->_class_rows.find(make_pair(decc, a.format()));
357 if (r != d->_class_rows.end())
358 row_iter = d->_rows.find((*r).second);
361 // Failing that, use the decoder as a key
362 row_iter = d->_rows.find(Row(decc));
365 assert(row_iter != d->_rows.end());
366 if (row_iter == d->_rows.end()) {
367 qDebug() << "Unexpected annotation: decoder = " << decc <<
368 ", format = " << a.format();
373 // Add the annotation
374 (*row_iter).second.push_annotation(a);
376 d->new_decode_data();
379 void DecoderStack::on_new_frame()