]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/decoderstack.cpp
Extract only the subset of annotations that are in view.
[pulseview.git] / pv / data / decoderstack.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <libsigrokdecode/libsigrokdecode.h>
22
23#include <boost/bind.hpp>
24#include <boost/foreach.hpp>
25#include <boost/thread/thread.hpp>
26
27#include <stdexcept>
28
29#include <QDebug>
30
31#include "decoderstack.h"
32
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>
38
39using boost::lock_guard;
40using boost::mutex;
41using boost::shared_ptr;
42using std::deque;
43using std::min;
44using std::list;
45using std::vector;
46
47namespace pv {
48namespace data {
49
50const double DecoderStack::DecodeMargin = 1.0;
51const double DecoderStack::DecodeThreshold = 0.2;
52const int64_t DecoderStack::DecodeChunkLength = 4096;
53
54mutex DecoderStack::_global_decode_mutex;
55
56DecoderStack::DecoderStack(const srd_decoder *const dec) :
57 _samples_decoded(0)
58{
59 _stack.push_back(shared_ptr<decode::Decoder>(
60 new decode::Decoder(dec)));
61}
62
63DecoderStack::~DecoderStack()
64{
65 _decode_thread.interrupt();
66 _decode_thread.join();
67}
68
69const std::list< boost::shared_ptr<decode::Decoder> >&
70DecoderStack::stack() const
71{
72 return _stack;
73}
74
75void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
76{
77 assert(decoder);
78 _stack.push_back(decoder);
79}
80
81void DecoderStack::remove(int index)
82{
83 using pv::data::decode::Decoder;
84
85 assert(index >= 0);
86 assert(index < (int)_stack.size());
87
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());
92
93 // Delete the element
94 _stack.erase(iter);
95}
96
97int64_t DecoderStack::samples_decoded() const
98{
99 lock_guard<mutex> decode_lock(_mutex);
100 return _samples_decoded;
101}
102
103void DecoderStack::get_annotation_subset(
104 std::vector<pv::data::decode::Annotation> &dest,
105 uint64_t start_sample, uint64_t end_sample) const
106{
107 lock_guard<mutex> lock(_mutex);
108
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,
113 this, _1, _2));
114
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,
119 this, _1, _2));
120
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]);
124}
125
126QString DecoderStack::error_message()
127{
128 lock_guard<mutex> lock(_mutex);
129 return _error_message;
130}
131
132void DecoderStack::begin_decode()
133{
134 shared_ptr<pv::view::LogicSignal> logic_signal;
135 shared_ptr<pv::data::Logic> data;
136
137 _decode_thread.interrupt();
138 _decode_thread.join();
139
140 _samples_decoded = 0;
141
142 clear();
143
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())))
151 break;
152
153 if (!data)
154 return;
155
156 // Get the samplerate and start time
157 _start_time = data->get_start_time();
158 _samplerate = data->samplerate();
159 if (_samplerate == 0.0)
160 _samplerate = 1.0;
161
162 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
163 data);
164}
165
166void DecoderStack::clear()
167{
168 _annotations.clear();
169 _ann_start_index.clear();
170 _ann_end_index.clear();
171}
172
173uint64_t DecoderStack::get_max_sample_count() const
174{
175 if (_annotations.empty())
176 return 0;
177 return _annotations.back().end_sample();
178}
179
180void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
181{
182 srd_session *session;
183 uint8_t chunk[DecodeChunkLength];
184 srd_decoder_inst *prev_di = NULL;
185
186 assert(data);
187
188 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
189 data->get_snapshots();
190 if (snapshots.empty())
191 return;
192
193 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
194 snapshots.front();
195 const int64_t sample_count = snapshot->get_sample_count();
196 const unsigned int chunk_sample_count =
197 DecodeChunkLength / snapshot->unit_size();
198
199 // Clear error message upon every new session run
200 _error_message = QString();
201
202 // Create the session
203 srd_session_new(&session);
204 assert(session);
205
206 // Create the decoders
207 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
208 {
209 srd_decoder_inst *const di = dec->create_decoder_inst(session);
210
211 if (!di)
212 {
213 _error_message = tr("Failed to create decoder instance");
214 srd_session_destroy(session);
215 return;
216 }
217
218 if (prev_di)
219 srd_inst_stack (session, prev_di, di);
220
221 prev_di = di;
222 }
223
224 // Start the session
225 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
226 g_variant_new_uint64((uint64_t)_samplerate));
227
228 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
229 DecoderStack::annotation_callback, this);
230
231 srd_session_start(session);
232
233 for (int64_t i = 0;
234 !boost::this_thread::interruption_requested() &&
235 i < sample_count;
236 i += chunk_sample_count)
237 {
238 lock_guard<mutex> decode_lock(_global_decode_mutex);
239
240 const int64_t chunk_end = min(
241 i + chunk_sample_count, sample_count);
242 snapshot->get_samples(chunk, i, chunk_end);
243
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");
247 break;
248 }
249
250 {
251 lock_guard<mutex> lock(_mutex);
252 _samples_decoded = chunk_end;
253 }
254 }
255
256 // Destroy the session
257 srd_session_destroy(session);
258}
259
260bool DecoderStack::index_entry_start_sample_gt(
261 const uint64_t sample, const size_t index) const
262{
263 assert(index < _annotations.size());
264 return _annotations[index].start_sample() > sample;
265}
266
267bool DecoderStack::index_entry_end_sample_lt(
268 const size_t index, const uint64_t sample) const
269{
270 assert(index < _annotations.size());
271 return _annotations[index].end_sample() < sample;
272}
273
274bool DecoderStack::index_entry_end_sample_gt(
275 const uint64_t sample, const size_t index) const
276{
277 assert(index < _annotations.size());
278 return _annotations[index].end_sample() > sample;
279}
280
281void DecoderStack::insert_annotation_into_start_index(
282 const pv::data::decode::Annotation &a, const size_t storage_offset)
283{
284 vector<size_t>::iterator i = _ann_start_index.end();
285 if (!_ann_start_index.empty() &&
286 _annotations[_ann_start_index.back()].start_sample() >
287 a.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,
291 this, _1, _2));
292
293 _ann_start_index.insert(i, storage_offset);
294}
295
296void DecoderStack::insert_annotation_into_end_index(
297 const pv::data::decode::Annotation &a, const size_t storage_offset)
298{
299 vector<size_t>::iterator i = _ann_end_index.end();
300 if (!_ann_end_index.empty() &&
301 _annotations[_ann_end_index.back()].end_sample() <
302 a.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,
306 this, _1, _2));
307
308 _ann_end_index.insert(i, storage_offset);
309}
310
311void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
312{
313 using pv::data::decode::Annotation;
314
315 GSList *l, *ll;
316 int row, ann_class, idx = 0;
317 struct srd_decoder_annotation_row *ann_row;
318 struct srd_decoder *decc;
319
320 assert(pdata);
321 assert(decoder);
322
323 DecoderStack *const d = (DecoderStack*)decoder;
324
325 lock_guard<mutex> lock(d->_mutex);
326
327 Annotation a = Annotation(pdata);
328
329 decc = pdata->pdo->di->decoder;
330 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, d->stack()) {
331 if (dec->decoder() == decc)
332 break;
333 idx++;
334 }
335 a.set_pd_index(idx);
336
337 for (l = decc->annotation_rows, row = 0; l; l = l->next, row++)
338 {
339 ann_row = (struct srd_decoder_annotation_row *)l->data;
340
341 for (ll = ann_row->ann_classes, ann_class = 0; ll;
342 ll = ll->next, ann_class++)
343 {
344 if (GPOINTER_TO_INT(ll->data) == a.format())
345 a.set_row(row);
346 }
347 }
348
349 const size_t offset = d->_annotations.size();
350 d->_annotations.push_back(a);
351
352 d->insert_annotation_into_start_index(a, offset);
353 d->insert_annotation_into_end_index(a, offset);
354
355 d->new_decode_data();
356}
357
358} // namespace data
359} // namespace pv