]> sigrok.org Git - pulseview.git/blame - pv/data/decoderstack.cpp
LogicSegment: Make constructor and append_payload() more generic
[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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
119aff65
JH
18 */
19
e332f6d3
JH
20#include <libsigrokdecode/libsigrokdecode.h>
21
e92cd4e4
JH
22#include <stdexcept>
23
b213ef09
JH
24#include <QDebug>
25
2acdb232 26#include "decoderstack.hpp"
119aff65 27
2acdb232 28#include <pv/data/logic.hpp>
f3d66e52 29#include <pv/data/logicsegment.hpp>
2acdb232
JH
30#include <pv/data/decode/decoder.hpp>
31#include <pv/data/decode/annotation.hpp>
f65cd27b 32#include <pv/session.hpp>
2acdb232 33#include <pv/view/logicsignal.hpp>
708c5523 34
3b68d03d
JH
35using std::lock_guard;
36using std::mutex;
3b68d03d 37using std::unique_lock;
819f4c25 38using std::deque;
f9101a91
JH
39using std::make_pair;
40using std::max;
819f4c25
JH
41using std::min;
42using std::list;
f9101a91
JH
43using std::map;
44using std::pair;
f9abf97e 45using std::shared_ptr;
067bb624 46using std::make_shared;
819f4c25 47using std::vector;
e332f6d3 48
6f925ba9
UH
49using boost::optional;
50
f9101a91
JH
51using namespace pv::data::decode;
52
119aff65
JH
53namespace pv {
54namespace data {
55
6e89374a
JH
56const double DecoderStack::DecodeMargin = 1.0;
57const double DecoderStack::DecodeThreshold = 0.2;
4e6301f9 58const int64_t DecoderStack::DecodeChunkLength = 10 * 1024 * 1024;
add091eb 59const unsigned int DecoderStack::DecodeNotifyPeriod = 1024;
e0fc5810 60
a1a3656b 61mutex DecoderStack::global_srd_mutex_;
fe89c961 62
2b81ae46 63DecoderStack::DecoderStack(pv::Session &session,
bdc5c3b0 64 const srd_decoder *const dec) :
8dbbc7f0 65 session_(session),
f054289f
JH
66 start_time_(0),
67 samplerate_(0),
8dbbc7f0
JH
68 sample_count_(0),
69 frame_complete_(false),
70 samples_decoded_(0)
119aff65 71{
8dbbc7f0 72 connect(&session_, SIGNAL(frame_began()),
f70d8673 73 this, SLOT(on_new_frame()));
8dbbc7f0 74 connect(&session_, SIGNAL(data_received()),
f70d8673 75 this, SLOT(on_data_received()));
8dbbc7f0 76 connect(&session_, SIGNAL(frame_ended()),
f70d8673 77 this, SLOT(on_frame_ended()));
82f50b10 78
067bb624 79 stack_.push_back(make_shared<decode::Decoder>(dec));
119aff65
JH
80}
81
6e89374a 82DecoderStack::~DecoderStack()
640d69ce 83{
8dbbc7f0
JH
84 if (decode_thread_.joinable()) {
85 interrupt_ = true;
86 input_cond_.notify_one();
87 decode_thread_.join();
6d483b8b 88 }
4e5a4405
JH
89}
90
6f925ba9 91const list< shared_ptr<decode::Decoder> >& DecoderStack::stack() const
4e5a4405 92{
8dbbc7f0 93 return stack_;
4e5a4405
JH
94}
95
6f925ba9 96void DecoderStack::push(shared_ptr<decode::Decoder> decoder)
4e5a4405 97{
7491a29f 98 assert(decoder);
8dbbc7f0 99 stack_.push_back(decoder);
4e5a4405
JH
100}
101
613d097c
JH
102void DecoderStack::remove(int index)
103{
613d097c 104 assert(index >= 0);
8dbbc7f0 105 assert(index < (int)stack_.size());
613d097c
JH
106
107 // Find the decoder in the stack
8dbbc7f0 108 auto iter = stack_.begin();
f3290553 109 for (int i = 0; i < index; i++, iter++)
8dbbc7f0 110 assert(iter != stack_.end());
613d097c
JH
111
112 // Delete the element
8dbbc7f0 113 stack_.erase(iter);
613d097c
JH
114}
115
f054289f
JH
116double DecoderStack::samplerate() const
117{
118 return samplerate_;
119}
120
60d9b99a 121const pv::util::Timestamp& DecoderStack::start_time() const
f054289f
JH
122{
123 return start_time_;
124}
125
5dfeb70f
JH
126int64_t DecoderStack::samples_decoded() const
127{
8dbbc7f0
JH
128 lock_guard<mutex> decode_lock(output_mutex_);
129 return samples_decoded_;
5dfeb70f
JH
130}
131
6f925ba9 132vector<Row> DecoderStack::get_visible_rows() const
f9101a91 133{
8dbbc7f0 134 lock_guard<mutex> lock(output_mutex_);
f9101a91
JH
135
136 vector<Row> rows;
137
2ad82c2e 138 for (const shared_ptr<decode::Decoder> &dec : stack_) {
f9101a91 139 assert(dec);
dd048a7e
JH
140 if (!dec->shown())
141 continue;
142
f9101a91
JH
143 const srd_decoder *const decc = dec->decoder();
144 assert(dec->decoder());
145
146 // Add a row for the decoder if it doesn't have a row list
147 if (!decc->annotation_rows)
326cf6fe 148 rows.emplace_back(decc);
f9101a91
JH
149
150 // Add the decoder rows
2ad82c2e 151 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
f9101a91
JH
152 const srd_decoder_annotation_row *const ann_row =
153 (srd_decoder_annotation_row *)l->data;
154 assert(ann_row);
326cf6fe 155 rows.emplace_back(decc, ann_row);
f9101a91
JH
156 }
157 }
158
159 return rows;
160}
161
92421299 162void DecoderStack::get_annotation_subset(
6f925ba9 163 vector<pv::data::decode::Annotation> &dest,
f9101a91
JH
164 const Row &row, uint64_t start_sample,
165 uint64_t end_sample) const
e0fc5810 166{
8dbbc7f0 167 lock_guard<mutex> lock(output_mutex_);
92421299 168
8dbbc7f0
JH
169 const auto iter = rows_.find(row);
170 if (iter != rows_.end())
f9101a91
JH
171 (*iter).second.get_annotation_subset(dest,
172 start_sample, end_sample);
e0fc5810
JH
173}
174
6e89374a 175QString DecoderStack::error_message()
b6b267bb 176{
8dbbc7f0
JH
177 lock_guard<mutex> lock(output_mutex_);
178 return error_message_;
b6b267bb
JH
179}
180
f9101a91
JH
181void DecoderStack::clear()
182{
8dbbc7f0
JH
183 sample_count_ = 0;
184 frame_complete_ = false;
185 samples_decoded_ = 0;
186 error_message_ = QString();
187 rows_.clear();
188 class_rows_.clear();
f9101a91
JH
189}
190
6e89374a 191void DecoderStack::begin_decode()
640d69ce 192{
8dbbc7f0
JH
193 if (decode_thread_.joinable()) {
194 interrupt_ = true;
195 input_cond_.notify_one();
196 decode_thread_.join();
6d483b8b 197 }
640d69ce 198
92421299 199 clear();
4e5a4405 200
8bd26d8b 201 // Check that all decoders have the required channels
8dbbc7f0 202 for (const shared_ptr<decode::Decoder> &dec : stack_)
6ac6242b 203 if (!dec->have_required_channels()) {
8dbbc7f0 204 error_message_ = tr("One or more required channels "
df4c1a06
JH
205 "have not been specified");
206 return;
207 }
208
f9101a91 209 // Add classes
2ad82c2e 210 for (const shared_ptr<decode::Decoder> &dec : stack_) {
f9101a91
JH
211 assert(dec);
212 const srd_decoder *const decc = dec->decoder();
213 assert(dec->decoder());
214
215 // Add a row for the decoder if it doesn't have a row list
216 if (!decc->annotation_rows)
8dbbc7f0 217 rows_[Row(decc)] = decode::RowData();
f9101a91
JH
218
219 // Add the decoder rows
2ad82c2e 220 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
f9101a91
JH
221 const srd_decoder_annotation_row *const ann_row =
222 (srd_decoder_annotation_row *)l->data;
223 assert(ann_row);
224
225 const Row row(decc, ann_row);
226
227 // Add a new empty row data object
8dbbc7f0 228 rows_[row] = decode::RowData();
f9101a91
JH
229
230 // Map out all the classes
231 for (const GSList *ll = ann_row->ann_classes;
232 ll; ll = ll->next)
8dbbc7f0 233 class_rows_[make_pair(decc,
f9101a91
JH
234 GPOINTER_TO_INT(ll->data))] = row;
235 }
236 }
237
8bd26d8b 238 // We get the logic data of the first channel in the list.
e0fc5810 239 // This works because we are currently assuming all
04394ded 240 // logic signals have the same data/segment
caa4a2db 241 pv::data::SignalBase *signalbase;
2b2d1062 242 pv::data::Logic *data = nullptr;
caa4a2db 243
8dbbc7f0 244 for (const shared_ptr<decode::Decoder> &dec : stack_)
8bd26d8b 245 if (dec && !dec->channels().empty() &&
caa4a2db
SA
246 ((signalbase = (*dec->channels().begin()).second.get())) &&
247 ((data = signalbase->logic_data().get())))
7491a29f
JH
248 break;
249
250 if (!data)
251 return;
252
f3d66e52
JH
253 // Check we have a segment of data
254 const deque< shared_ptr<pv::data::LogicSegment> > &segments =
255 data->logic_segments();
256 if (segments.empty())
f70d8673 257 return;
f3d66e52 258 segment_ = segments.front();
f70d8673 259
7491a29f 260 // Get the samplerate and start time
f3d66e52
JH
261 start_time_ = segment_->start_time();
262 samplerate_ = segment_->samplerate();
8dbbc7f0
JH
263 if (samplerate_ == 0.0)
264 samplerate_ = 1.0;
e0fc5810 265
8dbbc7f0
JH
266 interrupt_ = false;
267 decode_thread_ = std::thread(&DecoderStack::decode_proc, this);
640d69ce
JH
268}
269
e45b13b5 270uint64_t DecoderStack::max_sample_count() const
a007f5ad 271{
f9101a91
JH
272 uint64_t max_sample_count = 0;
273
27c05210 274 for (const auto& row : rows_)
f9101a91 275 max_sample_count = max(max_sample_count,
da50281d 276 row.second.get_max_sample());
f9101a91
JH
277
278 return max_sample_count;
a007f5ad
JH
279}
280
f70d8673
JH
281optional<int64_t> DecoderStack::wait_for_data() const
282{
8dbbc7f0 283 unique_lock<mutex> input_lock(input_mutex_);
cc10c409
SA
284
285 // Do wait if we decoded all samples but we're still capturing
286 // Do not wait if we're done capturing
f3290553 287 while (!interrupt_ && !frame_complete_ &&
cc10c409
SA
288 (samples_decoded_ >= sample_count_) &&
289 (session_.get_capture_state() != Session::Stopped)) {
290
8dbbc7f0 291 input_cond_.wait(input_lock);
cc10c409
SA
292 }
293
294 // Return value is valid if we're not aborting the decode,
8dbbc7f0 295 return boost::make_optional(!interrupt_ &&
cc10c409
SA
296 // and there's more work to do...
297 (samples_decoded_ < sample_count_ || !frame_complete_) &&
298 // and if the end of the data hasn't been reached yet
299 (!((samples_decoded_ >= sample_count_) && (session_.get_capture_state() == Session::Stopped))),
8dbbc7f0 300 sample_count_);
f70d8673
JH
301}
302
f67d9e9b 303void DecoderStack::decode_data(
e8bb7c69 304 const int64_t abs_start_samplenum, const int64_t sample_count, const unsigned int unit_size,
f67d9e9b
JH
305 srd_session *const session)
306{
f67d9e9b 307 const unsigned int chunk_sample_count =
f3d66e52 308 DecodeChunkLength / segment_->unit_size();
f67d9e9b 309
e8bb7c69 310 for (int64_t i = abs_start_samplenum; !interrupt_ && i < sample_count;
2ad82c2e 311 i += chunk_sample_count) {
f67d9e9b
JH
312
313 const int64_t chunk_end = min(
314 i + chunk_sample_count, sample_count);
038a1427 315 const uint8_t* chunk = segment_->get_samples(i, chunk_end);
f67d9e9b 316
ba5cd8cb 317 if (srd_session_send(session, i, chunk_end, chunk,
c4dd1e4e 318 (chunk_end - i) * unit_size, unit_size) != SRD_OK) {
8dbbc7f0 319 error_message_ = tr("Decoder reported an error");
f67d9e9b
JH
320 break;
321 }
322
323 {
8dbbc7f0
JH
324 lock_guard<mutex> lock(output_mutex_);
325 samples_decoded_ = chunk_end;
f67d9e9b 326 }
4c581c79
JH
327
328 if (i % DecodeNotifyPeriod == 0)
329 new_decode_data();
f67d9e9b
JH
330 }
331
1c5fb759 332 new_decode_data();
f67d9e9b
JH
333}
334
c1b2865e 335void DecoderStack::decode_proc()
119aff65 336{
f70d8673 337 optional<int64_t> sample_count;
b6b267bb 338 srd_session *session;
4c60462b 339 srd_decoder_inst *prev_di = nullptr;
e0fc5810 340
f3d66e52 341 assert(segment_);
e0fc5810 342
a1a3656b
SA
343 // Prevent any other decode threads from accessing libsigrokdecode
344 lock_guard<mutex> srd_lock(global_srd_mutex_);
345
b6b267bb
JH
346 // Create the session
347 srd_session_new(&session);
348 assert(session);
e0fc5810 349
7491a29f 350 // Create the decoders
f3d66e52 351 const unsigned int unit_size = segment_->unit_size();
f67d9e9b 352
2ad82c2e 353 for (const shared_ptr<decode::Decoder> &dec : stack_) {
c4dd1e4e 354 srd_decoder_inst *const di = dec->create_decoder_inst(session);
e0fc5810 355
2ad82c2e 356 if (!di) {
8dbbc7f0 357 error_message_ = tr("Failed to create decoder instance");
7491a29f
JH
358 srd_session_destroy(session);
359 return;
360 }
b6b267bb 361
7491a29f
JH
362 if (prev_di)
363 srd_inst_stack (session, prev_di, di);
b6b267bb 364
7491a29f 365 prev_di = di;
b6b267bb
JH
366 }
367
f70d8673
JH
368 // Get the intial sample count
369 {
8dbbc7f0 370 unique_lock<mutex> input_lock(input_mutex_);
f3d66e52 371 sample_count = sample_count_ = segment_->get_sample_count();
f70d8673
JH
372 }
373
b6b267bb 374 // Start the session
7491a29f 375 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
8dbbc7f0 376 g_variant_new_uint64((uint64_t)samplerate_));
7491a29f
JH
377
378 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
379 DecoderStack::annotation_callback, this);
380
b6b267bb
JH
381 srd_session_start(session);
382
e8bb7c69 383 int64_t abs_start_samplenum = 0;
f70d8673 384 do {
e8bb7c69
MC
385 decode_data(abs_start_samplenum, *sample_count, unit_size, session);
386 abs_start_samplenum = *sample_count;
f3290553 387 } while (error_message_.isEmpty() && (sample_count = wait_for_data()));
b6b267bb
JH
388
389 // Destroy the session
390 srd_session_destroy(session);
e0fc5810
JH
391}
392
6e89374a 393void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
e0fc5810 394{
e0fc5810
JH
395 assert(pdata);
396 assert(decoder);
397
6e89374a 398 DecoderStack *const d = (DecoderStack*)decoder;
f9101a91 399 assert(d);
e0fc5810 400
8dbbc7f0 401 lock_guard<mutex> lock(d->output_mutex_);
6d8b0562 402
f9101a91
JH
403 const Annotation a(pdata);
404
405 // Find the row
406 assert(pdata->pdo);
407 assert(pdata->pdo->di);
408 const srd_decoder *const decc = pdata->pdo->di->decoder;
409 assert(decc);
410
8dbbc7f0 411 auto row_iter = d->rows_.end();
360ab9be 412
f9101a91 413 // Try looking up the sub-row of this class
8dbbc7f0
JH
414 const auto r = d->class_rows_.find(make_pair(decc, a.format()));
415 if (r != d->class_rows_.end())
416 row_iter = d->rows_.find((*r).second);
2ad82c2e 417 else {
f9101a91 418 // Failing that, use the decoder as a key
8dbbc7f0 419 row_iter = d->rows_.find(Row(decc));
6d8b0562
UH
420 }
421
8dbbc7f0
JH
422 assert(row_iter != d->rows_.end());
423 if (row_iter == d->rows_.end()) {
f9101a91
JH
424 qDebug() << "Unexpected annotation: decoder = " << decc <<
425 ", format = " << a.format();
426 assert(0);
427 return;
428 }
9cef9567 429
f9101a91
JH
430 // Add the annotation
431 (*row_iter).second.push_annotation(a);
119aff65
JH
432}
433
82f50b10
JH
434void DecoderStack::on_new_frame()
435{
436 begin_decode();
437}
438
f70d8673
JH
439void DecoderStack::on_data_received()
440{
441 {
8dbbc7f0 442 unique_lock<mutex> lock(input_mutex_);
f3d66e52
JH
443 if (segment_)
444 sample_count_ = segment_->get_sample_count();
f70d8673 445 }
8dbbc7f0 446 input_cond_.notify_one();
f70d8673
JH
447}
448
449void DecoderStack::on_frame_ended()
450{
451 {
8dbbc7f0 452 unique_lock<mutex> lock(input_mutex_);
f3d66e52 453 if (segment_)
8dbbc7f0 454 frame_complete_ = true;
f70d8673 455 }
8dbbc7f0 456 input_cond_.notify_one();
f70d8673
JH
457}
458
119aff65
JH
459} // namespace data
460} // namespace pv