]> sigrok.org Git - pulseview.git/blame - pv/data/decoderstack.cpp
Split annotation data set into rows, and improved painting.
[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
7491a29f 23#include <boost/foreach.hpp>
e0fc5810
JH
24#include <boost/thread/thread.hpp>
25
e92cd4e4
JH
26#include <stdexcept>
27
b213ef09
JH
28#include <QDebug>
29
6e89374a 30#include "decoderstack.h"
119aff65 31
e0fc5810
JH
32#include <pv/data/logic.h>
33#include <pv/data/logicsnapshot.h>
7491a29f 34#include <pv/data/decode/decoder.h>
06e810f2 35#include <pv/data/decode/annotation.h>
e0fc5810 36#include <pv/view/logicsignal.h>
708c5523 37
819f4c25
JH
38using boost::lock_guard;
39using boost::mutex;
40using boost::shared_ptr;
41using std::deque;
f9101a91
JH
42using std::make_pair;
43using std::max;
819f4c25
JH
44using std::min;
45using std::list;
f9101a91
JH
46using std::map;
47using std::pair;
819f4c25 48using std::vector;
e332f6d3 49
f9101a91
JH
50using namespace pv::data::decode;
51
119aff65
JH
52namespace pv {
53namespace data {
54
6e89374a
JH
55const double DecoderStack::DecodeMargin = 1.0;
56const double DecoderStack::DecodeThreshold = 0.2;
57const int64_t DecoderStack::DecodeChunkLength = 4096;
e0fc5810 58
6e89374a 59mutex DecoderStack::_global_decode_mutex;
fe89c961 60
5dfeb70f
JH
61DecoderStack::DecoderStack(const srd_decoder *const dec) :
62 _samples_decoded(0)
119aff65 63{
7491a29f
JH
64 _stack.push_back(shared_ptr<decode::Decoder>(
65 new decode::Decoder(dec)));
119aff65
JH
66}
67
6e89374a 68DecoderStack::~DecoderStack()
640d69ce
JH
69{
70 _decode_thread.interrupt();
71 _decode_thread.join();
4e5a4405
JH
72}
73
7491a29f
JH
74const std::list< boost::shared_ptr<decode::Decoder> >&
75DecoderStack::stack() const
4e5a4405 76{
7491a29f 77 return _stack;
4e5a4405
JH
78}
79
7491a29f 80void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
4e5a4405 81{
7491a29f
JH
82 assert(decoder);
83 _stack.push_back(decoder);
4e5a4405
JH
84}
85
613d097c
JH
86void DecoderStack::remove(int index)
87{
613d097c
JH
88 assert(index >= 0);
89 assert(index < (int)_stack.size());
90
91 // Find the decoder in the stack
92 list< shared_ptr<Decoder> >::iterator iter = _stack.begin();
93 for(int i = 0; i < index; i++, iter++)
94 assert(iter != _stack.end());
95
96 // Delete the element
97 _stack.erase(iter);
98}
99
5dfeb70f
JH
100int64_t DecoderStack::samples_decoded() const
101{
102 lock_guard<mutex> decode_lock(_mutex);
103 return _samples_decoded;
104}
105
f9101a91
JH
106std::vector<Row> DecoderStack::get_rows() const
107{
108 lock_guard<mutex> lock(_mutex);
109
110 vector<Row> rows;
111
112 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
113 {
114 assert(dec);
115 const srd_decoder *const decc = dec->decoder();
116 assert(dec->decoder());
117
118 // Add a row for the decoder if it doesn't have a row list
119 if (!decc->annotation_rows)
120 rows.push_back(Row(decc));
121
122 // Add the decoder rows
123 for (const GSList *l = decc->annotation_rows; l; l = l->next)
124 {
125 const srd_decoder_annotation_row *const ann_row =
126 (srd_decoder_annotation_row *)l->data;
127 assert(ann_row);
128 rows.push_back(Row(decc, ann_row));
129 }
130 }
131
132 return rows;
133}
134
92421299
JH
135void DecoderStack::get_annotation_subset(
136 std::vector<pv::data::decode::Annotation> &dest,
f9101a91
JH
137 const Row &row, uint64_t start_sample,
138 uint64_t end_sample) const
e0fc5810 139{
b6b267bb 140 lock_guard<mutex> lock(_mutex);
92421299 141
f9101a91
JH
142 std::map<const Row, decode::RowData>::const_iterator iter =
143 _rows.find(row);
144 if (iter != _rows.end())
145 (*iter).second.get_annotation_subset(dest,
146 start_sample, end_sample);
e0fc5810
JH
147}
148
6e89374a 149QString DecoderStack::error_message()
b6b267bb
JH
150{
151 lock_guard<mutex> lock(_mutex);
152 return _error_message;
153}
154
f9101a91
JH
155void DecoderStack::clear()
156{
157 _samples_decoded = 0;
158 _error_message = QString();
159 _rows.clear();
160 _class_rows.clear();
161}
162
6e89374a 163void DecoderStack::begin_decode()
640d69ce 164{
7491a29f
JH
165 shared_ptr<pv::view::LogicSignal> logic_signal;
166 shared_ptr<pv::data::Logic> data;
167
640d69ce
JH
168 _decode_thread.interrupt();
169 _decode_thread.join();
170
92421299 171 clear();
4e5a4405 172
f9101a91
JH
173 // Add classes
174 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
175 {
176 assert(dec);
177 const srd_decoder *const decc = dec->decoder();
178 assert(dec->decoder());
179
180 // Add a row for the decoder if it doesn't have a row list
181 if (!decc->annotation_rows)
182 _rows[Row(decc)] = decode::RowData();
183
184 // Add the decoder rows
185 for (const GSList *l = decc->annotation_rows; l; l = l->next)
186 {
187 const srd_decoder_annotation_row *const ann_row =
188 (srd_decoder_annotation_row *)l->data;
189 assert(ann_row);
190
191 const Row row(decc, ann_row);
192
193 // Add a new empty row data object
194 _rows[row] = decode::RowData();
195
196 // Map out all the classes
197 for (const GSList *ll = ann_row->ann_classes;
198 ll; ll = ll->next)
199 _class_rows[make_pair(decc,
200 GPOINTER_TO_INT(ll->data))] = row;
201 }
202 }
203
e0fc5810
JH
204 // We get the logic data of the first probe in the list.
205 // This works because we are currently assuming all
206 // LogicSignals have the same data/snapshot
7491a29f
JH
207 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
208 if (dec && !dec->probes().empty() &&
209 ((logic_signal = (*dec->probes().begin()).second)) &&
7aa09b00 210 ((data = logic_signal->logic_data())))
7491a29f
JH
211 break;
212
213 if (!data)
214 return;
215
216 // Get the samplerate and start time
217 _start_time = data->get_start_time();
9d28da5a 218 _samplerate = data->samplerate();
7491a29f
JH
219 if (_samplerate == 0.0)
220 _samplerate = 1.0;
e0fc5810 221
6e89374a 222 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
e0fc5810 223 data);
640d69ce
JH
224}
225
a007f5ad
JH
226uint64_t DecoderStack::get_max_sample_count() const
227{
f9101a91
JH
228 uint64_t max_sample_count = 0;
229
230 for (map<const Row, RowData>::const_iterator i = _rows.begin();
231 i != _rows.end(); i++)
232 max_sample_count = max(max_sample_count,
233 (*i).second.get_max_sample());
234
235 return max_sample_count;
a007f5ad
JH
236}
237
6e89374a 238void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
119aff65 239{
b6b267bb 240 srd_session *session;
e0fc5810 241 uint8_t chunk[DecodeChunkLength];
7491a29f 242 srd_decoder_inst *prev_di = NULL;
e0fc5810
JH
243
244 assert(data);
245
e0fc5810
JH
246 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
247 data->get_snapshots();
248 if (snapshots.empty())
249 return;
250
251 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
252 snapshots.front();
c02f1e09 253 const int64_t sample_count = snapshot->get_sample_count();
064a7f42
JH
254 const unsigned int chunk_sample_count =
255 DecodeChunkLength / snapshot->unit_size();
e0fc5810 256
b6b267bb
JH
257 // Create the session
258 srd_session_new(&session);
259 assert(session);
e0fc5810 260
7491a29f
JH
261 // Create the decoders
262 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
263 {
264 srd_decoder_inst *const di = dec->create_decoder_inst(session);
e0fc5810 265
7491a29f
JH
266 if (!di)
267 {
8a290daf 268 _error_message = tr("Failed to create decoder instance");
7491a29f
JH
269 srd_session_destroy(session);
270 return;
271 }
b6b267bb 272
7491a29f
JH
273 if (prev_di)
274 srd_inst_stack (session, prev_di, di);
b6b267bb 275
7491a29f 276 prev_di = di;
b6b267bb
JH
277 }
278
b6b267bb 279 // Start the session
7491a29f
JH
280 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
281 g_variant_new_uint64((uint64_t)_samplerate));
282
283 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
284 DecoderStack::annotation_callback, this);
285
b6b267bb
JH
286 srd_session_start(session);
287
e0fc5810 288 for (int64_t i = 0;
819f4c25
JH
289 !boost::this_thread::interruption_requested() &&
290 i < sample_count;
064a7f42 291 i += chunk_sample_count)
e0fc5810 292 {
fe89c961
JH
293 lock_guard<mutex> decode_lock(_global_decode_mutex);
294
e0fc5810 295 const int64_t chunk_end = min(
064a7f42 296 i + chunk_sample_count, sample_count);
e0fc5810
JH
297 snapshot->get_samples(chunk, i, chunk_end);
298
f00fc2a9
BV
299 if (srd_session_send(session, i, i + sample_count,
300 chunk, chunk_end - i) != SRD_OK) {
8a290daf 301 _error_message = tr("Decoder reported an error");
e0fc5810 302 break;
b6b267bb 303 }
5dfeb70f
JH
304
305 {
306 lock_guard<mutex> lock(_mutex);
307 _samples_decoded = chunk_end;
308 }
e0fc5810 309 }
b6b267bb
JH
310
311 // Destroy the session
312 srd_session_destroy(session);
e0fc5810
JH
313}
314
6e89374a 315void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
e0fc5810 316{
e0fc5810
JH
317 assert(pdata);
318 assert(decoder);
319
6e89374a 320 DecoderStack *const d = (DecoderStack*)decoder;
f9101a91 321 assert(d);
e0fc5810 322
b6b267bb 323 lock_guard<mutex> lock(d->_mutex);
6d8b0562 324
f9101a91
JH
325 const Annotation a(pdata);
326
327 // Find the row
328 assert(pdata->pdo);
329 assert(pdata->pdo->di);
330 const srd_decoder *const decc = pdata->pdo->di->decoder;
331 assert(decc);
332
333 map<const Row, decode::RowData>::iterator row_iter = d->_rows.end();
334
335 // Try looking up the sub-row of this class
336 const map<pair<const srd_decoder*, int>, Row>::const_iterator r =
337 d->_class_rows.find(make_pair(decc, a.format()));
338 if (r != d->_class_rows.end())
339 row_iter = d->_rows.find((*r).second);
340 else
6d8b0562 341 {
f9101a91
JH
342 // Failing that, use the decoder as a key
343 row_iter = d->_rows.find(Row(decc));
6d8b0562
UH
344 }
345
f9101a91
JH
346 assert(row_iter != d->_rows.end());
347 if (row_iter == d->_rows.end()) {
348 qDebug() << "Unexpected annotation: decoder = " << decc <<
349 ", format = " << a.format();
350 assert(0);
351 return;
352 }
9cef9567 353
f9101a91
JH
354 // Add the annotation
355 (*row_iter).second.push_annotation(a);
92421299 356
9cef9567 357 d->new_decode_data();
119aff65
JH
358}
359
360} // namespace data
361} // namespace pv