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/bind.hpp>
24 #include <boost/foreach.hpp>
25 #include <boost/thread/thread.hpp>
31 #include "decoderstack.h"
33 #include <pv/data/logic.h>
34 #include <pv/data/logicsnapshot.h>
35 #include <pv/data/decode/decoder.h>
36 #include <pv/data/decode/annotation.h>
37 #include <pv/view/logicsignal.h>
39 using boost::lock_guard;
41 using boost::shared_ptr;
50 const double DecoderStack::DecodeMargin = 1.0;
51 const double DecoderStack::DecodeThreshold = 0.2;
52 const int64_t DecoderStack::DecodeChunkLength = 4096;
54 mutex DecoderStack::_global_decode_mutex;
56 DecoderStack::DecoderStack(const srd_decoder *const dec) :
59 _stack.push_back(shared_ptr<decode::Decoder>(
60 new decode::Decoder(dec)));
63 DecoderStack::~DecoderStack()
65 _decode_thread.interrupt();
66 _decode_thread.join();
69 const std::list< boost::shared_ptr<decode::Decoder> >&
70 DecoderStack::stack() const
75 void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
78 _stack.push_back(decoder);
81 void DecoderStack::remove(int index)
83 using pv::data::decode::Decoder;
86 assert(index < (int)_stack.size());
88 // Find the decoder in the stack
89 list< shared_ptr<Decoder> >::iterator iter = _stack.begin();
90 for(int i = 0; i < index; i++, iter++)
91 assert(iter != _stack.end());
97 int64_t DecoderStack::samples_decoded() const
99 lock_guard<mutex> decode_lock(_mutex);
100 return _samples_decoded;
103 void DecoderStack::get_annotation_subset(
104 std::vector<pv::data::decode::Annotation> &dest,
105 uint64_t start_sample, uint64_t end_sample) const
107 lock_guard<mutex> lock(_mutex);
109 const vector<size_t>::const_iterator start_iter =
110 lower_bound(_ann_end_index.begin(),
111 _ann_end_index.end(), start_sample,
112 bind(&DecoderStack::index_entry_end_sample_lt,
115 const vector<size_t>::const_iterator end_iter =
116 upper_bound(_ann_start_index.begin(),
117 _ann_start_index.end(), end_sample,
118 bind(&DecoderStack::index_entry_start_sample_gt,
121 for (vector<size_t>::const_iterator i = start_iter;
122 i != _ann_end_index.end() && *i != *end_iter; i++)
123 dest.push_back(_annotations[*i]);
126 QString DecoderStack::error_message()
128 lock_guard<mutex> lock(_mutex);
129 return _error_message;
132 void DecoderStack::begin_decode()
134 shared_ptr<pv::view::LogicSignal> logic_signal;
135 shared_ptr<pv::data::Logic> data;
137 _decode_thread.interrupt();
138 _decode_thread.join();
140 _samples_decoded = 0;
144 // We get the logic data of the first probe in the list.
145 // This works because we are currently assuming all
146 // LogicSignals have the same data/snapshot
147 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
148 if (dec && !dec->probes().empty() &&
149 ((logic_signal = (*dec->probes().begin()).second)) &&
150 ((data = logic_signal->logic_data())))
156 // Get the samplerate and start time
157 _start_time = data->get_start_time();
158 _samplerate = data->samplerate();
159 if (_samplerate == 0.0)
162 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
166 void DecoderStack::clear()
168 _annotations.clear();
169 _ann_start_index.clear();
170 _ann_end_index.clear();
173 uint64_t DecoderStack::get_max_sample_count() const
175 if (_annotations.empty())
177 return _annotations.back().end_sample();
180 void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
182 srd_session *session;
183 uint8_t chunk[DecodeChunkLength];
184 srd_decoder_inst *prev_di = NULL;
188 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
189 data->get_snapshots();
190 if (snapshots.empty())
193 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
195 const int64_t sample_count = snapshot->get_sample_count();
196 const unsigned int chunk_sample_count =
197 DecodeChunkLength / snapshot->unit_size();
199 // Clear error message upon every new session run
200 _error_message = QString();
202 // Create the session
203 srd_session_new(&session);
206 // Create the decoders
207 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
209 srd_decoder_inst *const di = dec->create_decoder_inst(session);
213 _error_message = tr("Failed to create decoder instance");
214 srd_session_destroy(session);
219 srd_inst_stack (session, prev_di, di);
225 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
226 g_variant_new_uint64((uint64_t)_samplerate));
228 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
229 DecoderStack::annotation_callback, this);
231 srd_session_start(session);
234 !boost::this_thread::interruption_requested() &&
236 i += chunk_sample_count)
238 lock_guard<mutex> decode_lock(_global_decode_mutex);
240 const int64_t chunk_end = min(
241 i + chunk_sample_count, sample_count);
242 snapshot->get_samples(chunk, i, chunk_end);
244 if (srd_session_send(session, i, i + sample_count,
245 chunk, chunk_end - i) != SRD_OK) {
246 _error_message = tr("Decoder reported an error");
251 lock_guard<mutex> lock(_mutex);
252 _samples_decoded = chunk_end;
256 // Destroy the session
257 srd_session_destroy(session);
260 bool DecoderStack::index_entry_start_sample_gt(
261 const uint64_t sample, const size_t index) const
263 assert(index < _annotations.size());
264 return _annotations[index].start_sample() > sample;
267 bool DecoderStack::index_entry_end_sample_lt(
268 const size_t index, const uint64_t sample) const
270 assert(index < _annotations.size());
271 return _annotations[index].end_sample() < sample;
274 bool DecoderStack::index_entry_end_sample_gt(
275 const uint64_t sample, const size_t index) const
277 assert(index < _annotations.size());
278 return _annotations[index].end_sample() > sample;
281 void DecoderStack::insert_annotation_into_start_index(
282 const pv::data::decode::Annotation &a, const size_t storage_offset)
284 vector<size_t>::iterator i = _ann_start_index.end();
285 if (!_ann_start_index.empty() &&
286 _annotations[_ann_start_index.back()].start_sample() >
288 i = upper_bound(_ann_start_index.begin(),
289 _ann_start_index.end(), a.start_sample(),
290 bind(&DecoderStack::index_entry_start_sample_gt,
293 _ann_start_index.insert(i, storage_offset);
296 void DecoderStack::insert_annotation_into_end_index(
297 const pv::data::decode::Annotation &a, const size_t storage_offset)
299 vector<size_t>::iterator i = _ann_end_index.end();
300 if (!_ann_end_index.empty() &&
301 _annotations[_ann_end_index.back()].end_sample() <
303 i = upper_bound(_ann_end_index.begin(),
304 _ann_end_index.end(), a.end_sample(),
305 bind(&DecoderStack::index_entry_end_sample_gt,
308 _ann_end_index.insert(i, storage_offset);
311 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
313 using pv::data::decode::Annotation;
316 int row, ann_class, idx = 0;
317 struct srd_decoder_annotation_row *ann_row;
318 struct srd_decoder *decc;
323 DecoderStack *const d = (DecoderStack*)decoder;
325 lock_guard<mutex> lock(d->_mutex);
327 Annotation a = Annotation(pdata);
329 decc = pdata->pdo->di->decoder;
330 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, d->stack()) {
331 if (dec->decoder() == decc)
337 for (l = decc->annotation_rows, row = 0; l; l = l->next, row++)
339 ann_row = (struct srd_decoder_annotation_row *)l->data;
341 for (ll = ann_row->ann_classes, ann_class = 0; ll;
342 ll = ll->next, ann_class++)
344 if (GPOINTER_TO_INT(ll->data) == a.format())
349 const size_t offset = d->_annotations.size();
350 d->_annotations.push_back(a);
352 d->insert_annotation_into_start_index(a, offset);
353 d->insert_annotation_into_end_index(a, offset);
355 d->new_decode_data();