]> sigrok.org Git - pulseview.git/blame - pv/data/decoderstack.cpp
DecoderStack: Make decoder sessions terminate after running
[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
e92cd4e4
JH
23#include <stdexcept>
24
b213ef09
JH
25#include <QDebug>
26
2acdb232 27#include "decoderstack.hpp"
119aff65 28
2acdb232 29#include <pv/data/logic.hpp>
f3d66e52 30#include <pv/data/logicsegment.hpp>
2acdb232
JH
31#include <pv/data/decode/decoder.hpp>
32#include <pv/data/decode/annotation.hpp>
f65cd27b 33#include <pv/session.hpp>
2acdb232 34#include <pv/view/logicsignal.hpp>
708c5523 35
3b68d03d
JH
36using std::lock_guard;
37using std::mutex;
f70d8673 38using boost::optional;
3b68d03d 39using std::unique_lock;
819f4c25 40using std::deque;
f9101a91
JH
41using std::make_pair;
42using std::max;
819f4c25
JH
43using std::min;
44using std::list;
f9101a91
JH
45using std::map;
46using std::pair;
f9abf97e 47using std::shared_ptr;
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;
4c581c79 58const unsigned int DecoderStack::DecodeNotifyPeriod = 65536;
e0fc5810 59
8dbbc7f0 60mutex DecoderStack::global_decode_mutex_;
fe89c961 61
2b81ae46 62DecoderStack::DecoderStack(pv::Session &session,
bdc5c3b0 63 const srd_decoder *const dec) :
8dbbc7f0 64 session_(session),
f054289f
JH
65 start_time_(0),
66 samplerate_(0),
8dbbc7f0
JH
67 sample_count_(0),
68 frame_complete_(false),
69 samples_decoded_(0)
119aff65 70{
8dbbc7f0 71 connect(&session_, SIGNAL(frame_began()),
f70d8673 72 this, SLOT(on_new_frame()));
8dbbc7f0 73 connect(&session_, SIGNAL(data_received()),
f70d8673 74 this, SLOT(on_data_received()));
8dbbc7f0 75 connect(&session_, SIGNAL(frame_ended()),
f70d8673 76 this, SLOT(on_frame_ended()));
82f50b10 77
8dbbc7f0 78 stack_.push_back(shared_ptr<decode::Decoder>(
7491a29f 79 new 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
f9abf97e 91const std::list< std::shared_ptr<decode::Decoder> >&
7491a29f 92DecoderStack::stack() const
4e5a4405 93{
8dbbc7f0 94 return stack_;
4e5a4405
JH
95}
96
f9abf97e 97void DecoderStack::push(std::shared_ptr<decode::Decoder> decoder)
4e5a4405 98{
7491a29f 99 assert(decoder);
8dbbc7f0 100 stack_.push_back(decoder);
4e5a4405
JH
101}
102
613d097c
JH
103void DecoderStack::remove(int index)
104{
613d097c 105 assert(index >= 0);
8dbbc7f0 106 assert(index < (int)stack_.size());
613d097c
JH
107
108 // Find the decoder in the stack
8dbbc7f0 109 auto iter = stack_.begin();
f3290553 110 for (int i = 0; i < index; i++, iter++)
8dbbc7f0 111 assert(iter != stack_.end());
613d097c
JH
112
113 // Delete the element
8dbbc7f0 114 stack_.erase(iter);
613d097c
JH
115}
116
f054289f
JH
117double DecoderStack::samplerate() const
118{
119 return samplerate_;
120}
121
60d9b99a 122const pv::util::Timestamp& DecoderStack::start_time() const
f054289f
JH
123{
124 return start_time_;
125}
126
5dfeb70f
JH
127int64_t DecoderStack::samples_decoded() const
128{
8dbbc7f0
JH
129 lock_guard<mutex> decode_lock(output_mutex_);
130 return samples_decoded_;
5dfeb70f
JH
131}
132
dd048a7e 133std::vector<Row> DecoderStack::get_visible_rows() const
f9101a91 134{
8dbbc7f0 135 lock_guard<mutex> lock(output_mutex_);
f9101a91
JH
136
137 vector<Row> rows;
138
2ad82c2e 139 for (const shared_ptr<decode::Decoder> &dec : stack_) {
f9101a91 140 assert(dec);
dd048a7e
JH
141 if (!dec->shown())
142 continue;
143
f9101a91
JH
144 const srd_decoder *const decc = dec->decoder();
145 assert(dec->decoder());
146
147 // Add a row for the decoder if it doesn't have a row list
148 if (!decc->annotation_rows)
149 rows.push_back(Row(decc));
150
151 // Add the decoder rows
2ad82c2e 152 for (const GSList *l = decc->annotation_rows; l; l = l->next) {
f9101a91
JH
153 const srd_decoder_annotation_row *const ann_row =
154 (srd_decoder_annotation_row *)l->data;
155 assert(ann_row);
156 rows.push_back(Row(decc, ann_row));
157 }
158 }
159
160 return rows;
161}
162
92421299
JH
163void DecoderStack::get_annotation_subset(
164 std::vector<pv::data::decode::Annotation> &dest,
f9101a91
JH
165 const Row &row, uint64_t start_sample,
166 uint64_t end_sample) const
e0fc5810 167{
8dbbc7f0 168 lock_guard<mutex> lock(output_mutex_);
92421299 169
8dbbc7f0
JH
170 const auto iter = rows_.find(row);
171 if (iter != rows_.end())
f9101a91
JH
172 (*iter).second.get_annotation_subset(dest,
173 start_sample, end_sample);
e0fc5810
JH
174}
175
6e89374a 176QString DecoderStack::error_message()
b6b267bb 177{
8dbbc7f0
JH
178 lock_guard<mutex> lock(output_mutex_);
179 return error_message_;
b6b267bb
JH
180}
181
f9101a91
JH
182void DecoderStack::clear()
183{
8dbbc7f0
JH
184 sample_count_ = 0;
185 frame_complete_ = false;
186 samples_decoded_ = 0;
187 error_message_ = QString();
188 rows_.clear();
189 class_rows_.clear();
f9101a91
JH
190}
191
6e89374a 192void DecoderStack::begin_decode()
640d69ce 193{
7491a29f
JH
194 shared_ptr<pv::view::LogicSignal> logic_signal;
195 shared_ptr<pv::data::Logic> data;
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
f3d66e52 244 // LogicSignals have the same data/segment
8dbbc7f0 245 for (const shared_ptr<decode::Decoder> &dec : stack_)
8bd26d8b
UH
246 if (dec && !dec->channels().empty() &&
247 ((logic_signal = (*dec->channels().begin()).second)) &&
7aa09b00 248 ((data = logic_signal->logic_data())))
7491a29f
JH
249 break;
250
251 if (!data)
252 return;
253
f3d66e52
JH
254 // Check we have a segment of data
255 const deque< shared_ptr<pv::data::LogicSegment> > &segments =
256 data->logic_segments();
257 if (segments.empty())
f70d8673 258 return;
f3d66e52 259 segment_ = segments.front();
f70d8673 260
7491a29f 261 // Get the samplerate and start time
f3d66e52
JH
262 start_time_ = segment_->start_time();
263 samplerate_ = segment_->samplerate();
8dbbc7f0
JH
264 if (samplerate_ == 0.0)
265 samplerate_ = 1.0;
e0fc5810 266
8dbbc7f0
JH
267 interrupt_ = false;
268 decode_thread_ = std::thread(&DecoderStack::decode_proc, this);
640d69ce
JH
269}
270
e45b13b5 271uint64_t DecoderStack::max_sample_count() const
a007f5ad 272{
f9101a91
JH
273 uint64_t max_sample_count = 0;
274
8dbbc7f0 275 for (auto i = rows_.cbegin(); i != rows_.end(); i++)
f9101a91
JH
276 max_sample_count = max(max_sample_count,
277 (*i).second.get_max_sample());
278
279 return max_sample_count;
a007f5ad
JH
280}
281
f70d8673
JH
282optional<int64_t> DecoderStack::wait_for_data() const
283{
8dbbc7f0 284 unique_lock<mutex> input_lock(input_mutex_);
cc10c409
SA
285
286 // Do wait if we decoded all samples but we're still capturing
287 // Do not wait if we're done capturing
f3290553 288 while (!interrupt_ && !frame_complete_ &&
cc10c409
SA
289 (samples_decoded_ >= sample_count_) &&
290 (session_.get_capture_state() != Session::Stopped)) {
291
8dbbc7f0 292 input_cond_.wait(input_lock);
cc10c409
SA
293 }
294
295 // Return value is valid if we're not aborting the decode,
8dbbc7f0 296 return boost::make_optional(!interrupt_ &&
cc10c409
SA
297 // and there's more work to do...
298 (samples_decoded_ < sample_count_ || !frame_complete_) &&
299 // and if the end of the data hasn't been reached yet
300 (!((samples_decoded_ >= sample_count_) && (session_.get_capture_state() == Session::Stopped))),
8dbbc7f0 301 sample_count_);
f70d8673
JH
302}
303
f67d9e9b 304void DecoderStack::decode_data(
f70d8673 305 const int64_t sample_count, const unsigned int unit_size,
f67d9e9b
JH
306 srd_session *const session)
307{
308 uint8_t chunk[DecodeChunkLength];
309
f67d9e9b 310 const unsigned int chunk_sample_count =
f3d66e52 311 DecodeChunkLength / segment_->unit_size();
f67d9e9b 312
8dbbc7f0 313 for (int64_t i = 0; !interrupt_ && i < sample_count;
2ad82c2e 314 i += chunk_sample_count) {
8dbbc7f0 315 lock_guard<mutex> decode_lock(global_decode_mutex_);
f67d9e9b
JH
316
317 const int64_t chunk_end = min(
318 i + chunk_sample_count, sample_count);
f3d66e52 319 segment_->get_samples(chunk, 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");
f67d9e9b
JH
324 break;
325 }
326
327 {
8dbbc7f0
JH
328 lock_guard<mutex> lock(output_mutex_);
329 samples_decoded_ = chunk_end;
f67d9e9b 330 }
4c581c79
JH
331
332 if (i % DecodeNotifyPeriod == 0)
333 new_decode_data();
f67d9e9b
JH
334 }
335
1c5fb759 336 new_decode_data();
f67d9e9b
JH
337}
338
c1b2865e 339void DecoderStack::decode_proc()
119aff65 340{
f70d8673 341 optional<int64_t> sample_count;
b6b267bb 342 srd_session *session;
4c60462b 343 srd_decoder_inst *prev_di = nullptr;
e0fc5810 344
f3d66e52 345 assert(segment_);
e0fc5810 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
f70d8673
JH
384 do {
385 decode_data(*sample_count, unit_size, session);
f3290553 386 } while (error_message_.isEmpty() && (sample_count = wait_for_data()));
b6b267bb
JH
387
388 // Destroy the session
389 srd_session_destroy(session);
e0fc5810
JH
390}
391
6e89374a 392void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
e0fc5810 393{
e0fc5810
JH
394 assert(pdata);
395 assert(decoder);
396
6e89374a 397 DecoderStack *const d = (DecoderStack*)decoder;
f9101a91 398 assert(d);
e0fc5810 399
8dbbc7f0 400 lock_guard<mutex> lock(d->output_mutex_);
6d8b0562 401
f9101a91
JH
402 const Annotation a(pdata);
403
404 // Find the row
405 assert(pdata->pdo);
406 assert(pdata->pdo->di);
407 const srd_decoder *const decc = pdata->pdo->di->decoder;
408 assert(decc);
409
8dbbc7f0 410 auto row_iter = d->rows_.end();
360ab9be 411
f9101a91 412 // Try looking up the sub-row of this class
8dbbc7f0
JH
413 const auto r = d->class_rows_.find(make_pair(decc, a.format()));
414 if (r != d->class_rows_.end())
415 row_iter = d->rows_.find((*r).second);
2ad82c2e 416 else {
f9101a91 417 // Failing that, use the decoder as a key
8dbbc7f0 418 row_iter = d->rows_.find(Row(decc));
6d8b0562
UH
419 }
420
8dbbc7f0
JH
421 assert(row_iter != d->rows_.end());
422 if (row_iter == d->rows_.end()) {
f9101a91
JH
423 qDebug() << "Unexpected annotation: decoder = " << decc <<
424 ", format = " << a.format();
425 assert(0);
426 return;
427 }
9cef9567 428
f9101a91
JH
429 // Add the annotation
430 (*row_iter).second.push_annotation(a);
119aff65
JH
431}
432
82f50b10
JH
433void DecoderStack::on_new_frame()
434{
435 begin_decode();
436}
437
f70d8673
JH
438void DecoderStack::on_data_received()
439{
440 {
8dbbc7f0 441 unique_lock<mutex> lock(input_mutex_);
f3d66e52
JH
442 if (segment_)
443 sample_count_ = segment_->get_sample_count();
f70d8673 444 }
8dbbc7f0 445 input_cond_.notify_one();
f70d8673
JH
446}
447
448void DecoderStack::on_frame_ended()
449{
450 {
8dbbc7f0 451 unique_lock<mutex> lock(input_mutex_);
f3d66e52 452 if (segment_)
8dbbc7f0 453 frame_complete_ = true;
f70d8673 454 }
8dbbc7f0 455 input_cond_.notify_one();
f70d8673
JH
456}
457
119aff65
JH
458} // namespace data
459} // namespace pv