]> sigrok.org Git - pulseview.git/blame - pv/data/decoderstack.cpp
Replaced lengthy iterator types with the auto keyword
[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>
82f50b10 36#include <pv/sigsession.h>
e0fc5810 37#include <pv/view/logicsignal.h>
708c5523 38
819f4c25
JH
39using boost::lock_guard;
40using boost::mutex;
f70d8673 41using boost::optional;
819f4c25 42using boost::shared_ptr;
f70d8673 43using boost::unique_lock;
819f4c25 44using std::deque;
f9101a91
JH
45using std::make_pair;
46using std::max;
819f4c25
JH
47using std::min;
48using std::list;
f9101a91
JH
49using std::map;
50using std::pair;
819f4c25 51using std::vector;
e332f6d3 52
f9101a91
JH
53using namespace pv::data::decode;
54
119aff65
JH
55namespace pv {
56namespace data {
57
6e89374a
JH
58const double DecoderStack::DecodeMargin = 1.0;
59const double DecoderStack::DecodeThreshold = 0.2;
60const int64_t DecoderStack::DecodeChunkLength = 4096;
4c581c79 61const unsigned int DecoderStack::DecodeNotifyPeriod = 65536;
e0fc5810 62
6e89374a 63mutex DecoderStack::_global_decode_mutex;
fe89c961 64
bdc5c3b0
JH
65DecoderStack::DecoderStack(pv::SigSession &session,
66 const srd_decoder *const dec) :
67 _session(session),
f70d8673
JH
68 _sample_count(0),
69 _frame_complete(false),
5dfeb70f 70 _samples_decoded(0)
119aff65 71{
f70d8673
JH
72 connect(&_session, SIGNAL(frame_began()),
73 this, SLOT(on_new_frame()));
74 connect(&_session, SIGNAL(data_received()),
75 this, SLOT(on_data_received()));
76 connect(&_session, SIGNAL(frame_ended()),
77 this, SLOT(on_frame_ended()));
82f50b10 78
7491a29f
JH
79 _stack.push_back(shared_ptr<decode::Decoder>(
80 new decode::Decoder(dec)));
119aff65
JH
81}
82
6e89374a 83DecoderStack::~DecoderStack()
640d69ce 84{
6d483b8b
MC
85 if (_decode_thread.joinable()) {
86 _decode_thread.interrupt();
87 _decode_thread.join();
88 }
4e5a4405
JH
89}
90
7491a29f
JH
91const std::list< boost::shared_ptr<decode::Decoder> >&
92DecoderStack::stack() const
4e5a4405 93{
7491a29f 94 return _stack;
4e5a4405
JH
95}
96
7491a29f 97void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
4e5a4405 98{
7491a29f
JH
99 assert(decoder);
100 _stack.push_back(decoder);
4e5a4405
JH
101}
102
613d097c
JH
103void DecoderStack::remove(int index)
104{
613d097c
JH
105 assert(index >= 0);
106 assert(index < (int)_stack.size());
107
108 // Find the decoder in the stack
f46e495e 109 auto iter = _stack.begin();
613d097c
JH
110 for(int i = 0; i < index; i++, iter++)
111 assert(iter != _stack.end());
112
113 // Delete the element
114 _stack.erase(iter);
115}
116
5dfeb70f
JH
117int64_t DecoderStack::samples_decoded() const
118{
28b9cc08 119 lock_guard<mutex> decode_lock(_output_mutex);
5dfeb70f
JH
120 return _samples_decoded;
121}
122
dd048a7e 123std::vector<Row> DecoderStack::get_visible_rows() const
f9101a91 124{
28b9cc08 125 lock_guard<mutex> lock(_output_mutex);
f9101a91
JH
126
127 vector<Row> rows;
128
129 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
130 {
131 assert(dec);
dd048a7e
JH
132 if (!dec->shown())
133 continue;
134
f9101a91
JH
135 const srd_decoder *const decc = dec->decoder();
136 assert(dec->decoder());
137
138 // Add a row for the decoder if it doesn't have a row list
139 if (!decc->annotation_rows)
140 rows.push_back(Row(decc));
141
142 // Add the decoder rows
143 for (const GSList *l = decc->annotation_rows; l; l = l->next)
144 {
145 const srd_decoder_annotation_row *const ann_row =
146 (srd_decoder_annotation_row *)l->data;
147 assert(ann_row);
148 rows.push_back(Row(decc, ann_row));
149 }
150 }
151
152 return rows;
153}
154
92421299
JH
155void DecoderStack::get_annotation_subset(
156 std::vector<pv::data::decode::Annotation> &dest,
f9101a91
JH
157 const Row &row, uint64_t start_sample,
158 uint64_t end_sample) const
e0fc5810 159{
28b9cc08 160 lock_guard<mutex> lock(_output_mutex);
92421299 161
f46e495e 162 const auto iter = _rows.find(row);
f9101a91
JH
163 if (iter != _rows.end())
164 (*iter).second.get_annotation_subset(dest,
165 start_sample, end_sample);
e0fc5810
JH
166}
167
6e89374a 168QString DecoderStack::error_message()
b6b267bb 169{
28b9cc08 170 lock_guard<mutex> lock(_output_mutex);
b6b267bb
JH
171 return _error_message;
172}
173
f9101a91
JH
174void DecoderStack::clear()
175{
f70d8673
JH
176 _sample_count = 0;
177 _frame_complete = false;
f9101a91
JH
178 _samples_decoded = 0;
179 _error_message = QString();
180 _rows.clear();
181 _class_rows.clear();
182}
183
6e89374a 184void DecoderStack::begin_decode()
640d69ce 185{
7491a29f
JH
186 shared_ptr<pv::view::LogicSignal> logic_signal;
187 shared_ptr<pv::data::Logic> data;
188
6d483b8b
MC
189 if (_decode_thread.joinable()) {
190 _decode_thread.interrupt();
191 _decode_thread.join();
192 }
640d69ce 193
92421299 194 clear();
4e5a4405 195
8bd26d8b 196 // Check that all decoders have the required channels
df4c1a06
JH
197 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
198 if (!dec->have_required_probes()) {
8bd26d8b 199 _error_message = tr("One or more required channels "
df4c1a06
JH
200 "have not been specified");
201 return;
202 }
203
f9101a91
JH
204 // Add classes
205 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
206 {
207 assert(dec);
208 const srd_decoder *const decc = dec->decoder();
209 assert(dec->decoder());
210
211 // Add a row for the decoder if it doesn't have a row list
212 if (!decc->annotation_rows)
213 _rows[Row(decc)] = decode::RowData();
214
215 // Add the decoder rows
216 for (const GSList *l = decc->annotation_rows; l; l = l->next)
217 {
218 const srd_decoder_annotation_row *const ann_row =
219 (srd_decoder_annotation_row *)l->data;
220 assert(ann_row);
221
222 const Row row(decc, ann_row);
223
224 // Add a new empty row data object
225 _rows[row] = decode::RowData();
226
227 // Map out all the classes
228 for (const GSList *ll = ann_row->ann_classes;
229 ll; ll = ll->next)
230 _class_rows[make_pair(decc,
231 GPOINTER_TO_INT(ll->data))] = row;
232 }
233 }
234
8bd26d8b 235 // We get the logic data of the first channel in the list.
e0fc5810
JH
236 // This works because we are currently assuming all
237 // LogicSignals have the same data/snapshot
7491a29f 238 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
8bd26d8b
UH
239 if (dec && !dec->channels().empty() &&
240 ((logic_signal = (*dec->channels().begin()).second)) &&
7aa09b00 241 ((data = logic_signal->logic_data())))
7491a29f
JH
242 break;
243
244 if (!data)
245 return;
246
f70d8673
JH
247 // Check we have a snapshot of data
248 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
249 data->get_snapshots();
250 if (snapshots.empty())
251 return;
252 _snapshot = snapshots.front();
253
7491a29f
JH
254 // Get the samplerate and start time
255 _start_time = data->get_start_time();
9d28da5a 256 _samplerate = data->samplerate();
7491a29f
JH
257 if (_samplerate == 0.0)
258 _samplerate = 1.0;
e0fc5810 259
c1b2865e 260 _decode_thread = boost::thread(&DecoderStack::decode_proc, this);
640d69ce
JH
261}
262
a007f5ad
JH
263uint64_t DecoderStack::get_max_sample_count() const
264{
f9101a91
JH
265 uint64_t max_sample_count = 0;
266
f46e495e 267 for (auto i = _rows.cbegin(); i != _rows.end(); i++)
f9101a91
JH
268 max_sample_count = max(max_sample_count,
269 (*i).second.get_max_sample());
270
271 return max_sample_count;
a007f5ad
JH
272}
273
f70d8673
JH
274optional<int64_t> DecoderStack::wait_for_data() const
275{
276 unique_lock<mutex> input_lock(_input_mutex);
277 while(!boost::this_thread::interruption_requested() &&
278 !_frame_complete && _samples_decoded >= _sample_count)
279 _input_cond.wait(input_lock);
280 return boost::make_optional(
281 !boost::this_thread::interruption_requested() &&
282 (_samples_decoded < _sample_count || !_frame_complete),
283 _sample_count);
284}
285
f67d9e9b 286void DecoderStack::decode_data(
f70d8673 287 const int64_t sample_count, const unsigned int unit_size,
f67d9e9b
JH
288 srd_session *const session)
289{
290 uint8_t chunk[DecodeChunkLength];
291
f67d9e9b 292 const unsigned int chunk_sample_count =
f70d8673 293 DecodeChunkLength / _snapshot->unit_size();
f67d9e9b
JH
294
295 for (int64_t i = 0;
296 !boost::this_thread::interruption_requested() &&
297 i < sample_count;
298 i += chunk_sample_count)
299 {
300 lock_guard<mutex> decode_lock(_global_decode_mutex);
301
302 const int64_t chunk_end = min(
303 i + chunk_sample_count, sample_count);
f70d8673 304 _snapshot->get_samples(chunk, i, chunk_end);
f67d9e9b
JH
305
306 if (srd_session_send(session, i, i + sample_count, chunk,
307 (chunk_end - i) * unit_size) != SRD_OK) {
308 _error_message = tr("Decoder reported an error");
309 break;
310 }
311
312 {
28b9cc08 313 lock_guard<mutex> lock(_output_mutex);
f67d9e9b
JH
314 _samples_decoded = chunk_end;
315 }
4c581c79
JH
316
317 if (i % DecodeNotifyPeriod == 0)
318 new_decode_data();
f67d9e9b
JH
319 }
320
1c5fb759 321 new_decode_data();
f67d9e9b
JH
322}
323
c1b2865e 324void DecoderStack::decode_proc()
119aff65 325{
f70d8673 326 optional<int64_t> sample_count;
b6b267bb 327 srd_session *session;
7491a29f 328 srd_decoder_inst *prev_di = NULL;
e0fc5810 329
f70d8673 330 assert(_snapshot);
e0fc5810 331
b6b267bb
JH
332 // Create the session
333 srd_session_new(&session);
334 assert(session);
e0fc5810 335
7491a29f 336 // Create the decoders
f70d8673 337 const unsigned int unit_size = _snapshot->unit_size();
f67d9e9b 338
7491a29f
JH
339 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
340 {
13a3528c 341 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
e0fc5810 342
7491a29f
JH
343 if (!di)
344 {
8a290daf 345 _error_message = tr("Failed to create decoder instance");
7491a29f
JH
346 srd_session_destroy(session);
347 return;
348 }
b6b267bb 349
7491a29f
JH
350 if (prev_di)
351 srd_inst_stack (session, prev_di, di);
b6b267bb 352
7491a29f 353 prev_di = di;
b6b267bb
JH
354 }
355
f70d8673
JH
356 // Get the intial sample count
357 {
358 unique_lock<mutex> input_lock(_input_mutex);
359 sample_count = _sample_count = _snapshot->get_sample_count();
360 }
361
b6b267bb 362 // Start the session
7491a29f
JH
363 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
364 g_variant_new_uint64((uint64_t)_samplerate));
365
366 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
367 DecoderStack::annotation_callback, this);
368
b6b267bb
JH
369 srd_session_start(session);
370
f70d8673
JH
371 do {
372 decode_data(*sample_count, unit_size, session);
373 } while(_error_message.isEmpty() && (sample_count = wait_for_data()));
b6b267bb
JH
374
375 // Destroy the session
376 srd_session_destroy(session);
e0fc5810
JH
377}
378
6e89374a 379void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
e0fc5810 380{
e0fc5810
JH
381 assert(pdata);
382 assert(decoder);
383
6e89374a 384 DecoderStack *const d = (DecoderStack*)decoder;
f9101a91 385 assert(d);
e0fc5810 386
28b9cc08 387 lock_guard<mutex> lock(d->_output_mutex);
6d8b0562 388
f9101a91
JH
389 const Annotation a(pdata);
390
391 // Find the row
392 assert(pdata->pdo);
393 assert(pdata->pdo->di);
394 const srd_decoder *const decc = pdata->pdo->di->decoder;
395 assert(decc);
396
f46e495e 397 auto row_iter = d->_rows.end();
f9101a91
JH
398
399 // Try looking up the sub-row of this class
f46e495e 400 const auto r = d->_class_rows.find(make_pair(decc, a.format()));
f9101a91
JH
401 if (r != d->_class_rows.end())
402 row_iter = d->_rows.find((*r).second);
403 else
6d8b0562 404 {
f9101a91
JH
405 // Failing that, use the decoder as a key
406 row_iter = d->_rows.find(Row(decc));
6d8b0562
UH
407 }
408
f9101a91
JH
409 assert(row_iter != d->_rows.end());
410 if (row_iter == d->_rows.end()) {
411 qDebug() << "Unexpected annotation: decoder = " << decc <<
412 ", format = " << a.format();
413 assert(0);
414 return;
415 }
9cef9567 416
f9101a91
JH
417 // Add the annotation
418 (*row_iter).second.push_annotation(a);
119aff65
JH
419}
420
82f50b10
JH
421void DecoderStack::on_new_frame()
422{
423 begin_decode();
424}
425
f70d8673
JH
426void DecoderStack::on_data_received()
427{
428 {
429 unique_lock<mutex> lock(_input_mutex);
46bf6027
JH
430 if (_snapshot)
431 _sample_count = _snapshot->get_sample_count();
f70d8673
JH
432 }
433 _input_cond.notify_one();
434}
435
436void DecoderStack::on_frame_ended()
437{
438 {
439 unique_lock<mutex> lock(_input_mutex);
46bf6027
JH
440 if (_snapshot)
441 _frame_complete = true;
f70d8673
JH
442 }
443 _input_cond.notify_one();
444}
445
119aff65
JH
446} // namespace data
447} // namespace pv