]> sigrok.org Git - pulseview.git/blame - pv/data/decoderstack.cpp
Extract only the subset of annotations that are in view.
[pulseview.git] / pv / data / decoderstack.cpp
CommitLineData
119aff65
JH
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
e332f6d3
JH
21#include <libsigrokdecode/libsigrokdecode.h>
22
92421299 23#include <boost/bind.hpp>
7491a29f 24#include <boost/foreach.hpp>
e0fc5810
JH
25#include <boost/thread/thread.hpp>
26
e92cd4e4
JH
27#include <stdexcept>
28
b213ef09
JH
29#include <QDebug>
30
6e89374a 31#include "decoderstack.h"
119aff65 32
e0fc5810
JH
33#include <pv/data/logic.h>
34#include <pv/data/logicsnapshot.h>
7491a29f 35#include <pv/data/decode/decoder.h>
06e810f2 36#include <pv/data/decode/annotation.h>
e0fc5810 37#include <pv/view/logicsignal.h>
708c5523 38
819f4c25
JH
39using boost::lock_guard;
40using boost::mutex;
41using boost::shared_ptr;
42using std::deque;
43using std::min;
44using std::list;
45using std::vector;
e332f6d3 46
119aff65
JH
47namespace pv {
48namespace data {
49
6e89374a
JH
50const double DecoderStack::DecodeMargin = 1.0;
51const double DecoderStack::DecodeThreshold = 0.2;
52const int64_t DecoderStack::DecodeChunkLength = 4096;
e0fc5810 53
6e89374a 54mutex DecoderStack::_global_decode_mutex;
fe89c961 55
5dfeb70f
JH
56DecoderStack::DecoderStack(const srd_decoder *const dec) :
57 _samples_decoded(0)
119aff65 58{
7491a29f
JH
59 _stack.push_back(shared_ptr<decode::Decoder>(
60 new decode::Decoder(dec)));
119aff65
JH
61}
62
6e89374a 63DecoderStack::~DecoderStack()
640d69ce
JH
64{
65 _decode_thread.interrupt();
66 _decode_thread.join();
4e5a4405
JH
67}
68
7491a29f
JH
69const std::list< boost::shared_ptr<decode::Decoder> >&
70DecoderStack::stack() const
4e5a4405 71{
7491a29f 72 return _stack;
4e5a4405
JH
73}
74
7491a29f 75void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
4e5a4405 76{
7491a29f
JH
77 assert(decoder);
78 _stack.push_back(decoder);
4e5a4405
JH
79}
80
613d097c
JH
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
5dfeb70f
JH
97int64_t DecoderStack::samples_decoded() const
98{
99 lock_guard<mutex> decode_lock(_mutex);
100 return _samples_decoded;
101}
102
92421299
JH
103void DecoderStack::get_annotation_subset(
104 std::vector<pv::data::decode::Annotation> &dest,
105 uint64_t start_sample, uint64_t end_sample) const
e0fc5810 106{
b6b267bb 107 lock_guard<mutex> lock(_mutex);
92421299
JH
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]);
e0fc5810
JH
124}
125
6e89374a 126QString DecoderStack::error_message()
b6b267bb
JH
127{
128 lock_guard<mutex> lock(_mutex);
129 return _error_message;
130}
131
6e89374a 132void DecoderStack::begin_decode()
640d69ce 133{
7491a29f
JH
134 shared_ptr<pv::view::LogicSignal> logic_signal;
135 shared_ptr<pv::data::Logic> data;
136
640d69ce
JH
137 _decode_thread.interrupt();
138 _decode_thread.join();
139
5dfeb70f
JH
140 _samples_decoded = 0;
141
92421299 142 clear();
4e5a4405 143
e0fc5810
JH
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
7491a29f
JH
147 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
148 if (dec && !dec->probes().empty() &&
149 ((logic_signal = (*dec->probes().begin()).second)) &&
7aa09b00 150 ((data = logic_signal->logic_data())))
7491a29f
JH
151 break;
152
153 if (!data)
154 return;
155
156 // Get the samplerate and start time
157 _start_time = data->get_start_time();
9d28da5a 158 _samplerate = data->samplerate();
7491a29f
JH
159 if (_samplerate == 0.0)
160 _samplerate = 1.0;
e0fc5810 161
6e89374a 162 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
e0fc5810 163 data);
640d69ce
JH
164}
165
caabb84c 166void DecoderStack::clear()
640d69ce 167{
dd6b5009 168 _annotations.clear();
92421299
JH
169 _ann_start_index.clear();
170 _ann_end_index.clear();
640d69ce
JH
171}
172
a007f5ad
JH
173uint64_t DecoderStack::get_max_sample_count() const
174{
175 if (_annotations.empty())
176 return 0;
db62bbfd 177 return _annotations.back().end_sample();
a007f5ad
JH
178}
179
6e89374a 180void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
119aff65 181{
b6b267bb 182 srd_session *session;
e0fc5810 183 uint8_t chunk[DecodeChunkLength];
7491a29f 184 srd_decoder_inst *prev_di = NULL;
e0fc5810
JH
185
186 assert(data);
187
e0fc5810
JH
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();
c02f1e09 195 const int64_t sample_count = snapshot->get_sample_count();
064a7f42
JH
196 const unsigned int chunk_sample_count =
197 DecodeChunkLength / snapshot->unit_size();
e0fc5810 198
375b6216
UH
199 // Clear error message upon every new session run
200 _error_message = QString();
201
b6b267bb
JH
202 // Create the session
203 srd_session_new(&session);
204 assert(session);
e0fc5810 205
7491a29f
JH
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);
e0fc5810 210
7491a29f
JH
211 if (!di)
212 {
8a290daf 213 _error_message = tr("Failed to create decoder instance");
7491a29f
JH
214 srd_session_destroy(session);
215 return;
216 }
b6b267bb 217
7491a29f
JH
218 if (prev_di)
219 srd_inst_stack (session, prev_di, di);
b6b267bb 220
7491a29f 221 prev_di = di;
b6b267bb
JH
222 }
223
b6b267bb 224 // Start the session
7491a29f
JH
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
b6b267bb
JH
231 srd_session_start(session);
232
e0fc5810 233 for (int64_t i = 0;
819f4c25
JH
234 !boost::this_thread::interruption_requested() &&
235 i < sample_count;
064a7f42 236 i += chunk_sample_count)
e0fc5810 237 {
fe89c961
JH
238 lock_guard<mutex> decode_lock(_global_decode_mutex);
239
e0fc5810 240 const int64_t chunk_end = min(
064a7f42 241 i + chunk_sample_count, sample_count);
e0fc5810
JH
242 snapshot->get_samples(chunk, i, chunk_end);
243
f00fc2a9
BV
244 if (srd_session_send(session, i, i + sample_count,
245 chunk, chunk_end - i) != SRD_OK) {
8a290daf 246 _error_message = tr("Decoder reported an error");
e0fc5810 247 break;
b6b267bb 248 }
5dfeb70f
JH
249
250 {
251 lock_guard<mutex> lock(_mutex);
252 _samples_decoded = chunk_end;
253 }
e0fc5810 254 }
b6b267bb
JH
255
256 // Destroy the session
257 srd_session_destroy(session);
e0fc5810
JH
258}
259
92421299
JH
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
6e89374a 311void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
e0fc5810 312{
06e810f2 313 using pv::data::decode::Annotation;
e0fc5810 314
6d8b0562 315 GSList *l, *ll;
bc870bcf 316 int row, ann_class, idx = 0;
6d8b0562 317 struct srd_decoder_annotation_row *ann_row;
bc870bcf 318 struct srd_decoder *decc;
6d8b0562 319
e0fc5810
JH
320 assert(pdata);
321 assert(decoder);
322
6e89374a 323 DecoderStack *const d = (DecoderStack*)decoder;
e0fc5810 324
b6b267bb 325 lock_guard<mutex> lock(d->_mutex);
6d8b0562
UH
326
327 Annotation a = Annotation(pdata);
328
bc870bcf
UH
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);
6d8b0562 336
bc870bcf 337 for (l = decc->annotation_rows, row = 0; l; l = l->next, row++)
6d8b0562
UH
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
92421299 349 const size_t offset = d->_annotations.size();
6d8b0562 350 d->_annotations.push_back(a);
9cef9567 351
92421299
JH
352 d->insert_annotation_into_start_index(a, offset);
353 d->insert_annotation_into_end_index(a, offset);
354
9cef9567 355 d->new_decode_data();
119aff65
JH
356}
357
358} // namespace data
359} // namespace pv