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