]> sigrok.org Git - pulseview.git/blame - pv/data/decoderstack.cpp
Use a type with a greater resolution to represent time values
[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
8dbbc7f0 139 for (const shared_ptr<decode::Decoder> &dec : stack_)
f9101a91
JH
140 {
141 assert(dec);
dd048a7e
JH
142 if (!dec->shown())
143 continue;
144
f9101a91
JH
145 const srd_decoder *const decc = dec->decoder();
146 assert(dec->decoder());
147
148 // Add a row for the decoder if it doesn't have a row list
149 if (!decc->annotation_rows)
150 rows.push_back(Row(decc));
151
152 // Add the decoder rows
153 for (const GSList *l = decc->annotation_rows; l; l = l->next)
154 {
155 const srd_decoder_annotation_row *const ann_row =
156 (srd_decoder_annotation_row *)l->data;
157 assert(ann_row);
158 rows.push_back(Row(decc, ann_row));
159 }
160 }
161
162 return rows;
163}
164
92421299
JH
165void DecoderStack::get_annotation_subset(
166 std::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
JH
186 sample_count_ = 0;
187 frame_complete_ = false;
188 samples_decoded_ = 0;
189 error_message_ = QString();
190 rows_.clear();
191 class_rows_.clear();
f9101a91
JH
192}
193
6e89374a 194void DecoderStack::begin_decode()
640d69ce 195{
7491a29f
JH
196 shared_ptr<pv::view::LogicSignal> logic_signal;
197 shared_ptr<pv::data::Logic> data;
198
8dbbc7f0
JH
199 if (decode_thread_.joinable()) {
200 interrupt_ = true;
201 input_cond_.notify_one();
202 decode_thread_.join();
6d483b8b 203 }
640d69ce 204
92421299 205 clear();
4e5a4405 206
8bd26d8b 207 // Check that all decoders have the required channels
8dbbc7f0 208 for (const shared_ptr<decode::Decoder> &dec : stack_)
6ac6242b 209 if (!dec->have_required_channels()) {
8dbbc7f0 210 error_message_ = tr("One or more required channels "
df4c1a06
JH
211 "have not been specified");
212 return;
213 }
214
f9101a91 215 // Add classes
8dbbc7f0 216 for (const shared_ptr<decode::Decoder> &dec : stack_)
f9101a91
JH
217 {
218 assert(dec);
219 const srd_decoder *const decc = dec->decoder();
220 assert(dec->decoder());
221
222 // Add a row for the decoder if it doesn't have a row list
223 if (!decc->annotation_rows)
8dbbc7f0 224 rows_[Row(decc)] = decode::RowData();
f9101a91
JH
225
226 // Add the decoder rows
227 for (const GSList *l = decc->annotation_rows; l; l = l->next)
228 {
229 const srd_decoder_annotation_row *const ann_row =
230 (srd_decoder_annotation_row *)l->data;
231 assert(ann_row);
232
233 const Row row(decc, ann_row);
234
235 // Add a new empty row data object
8dbbc7f0 236 rows_[row] = decode::RowData();
f9101a91
JH
237
238 // Map out all the classes
239 for (const GSList *ll = ann_row->ann_classes;
240 ll; ll = ll->next)
8dbbc7f0 241 class_rows_[make_pair(decc,
f9101a91
JH
242 GPOINTER_TO_INT(ll->data))] = row;
243 }
244 }
245
8bd26d8b 246 // We get the logic data of the first channel in the list.
e0fc5810 247 // This works because we are currently assuming all
f3d66e52 248 // LogicSignals have the same data/segment
8dbbc7f0 249 for (const shared_ptr<decode::Decoder> &dec : stack_)
8bd26d8b
UH
250 if (dec && !dec->channels().empty() &&
251 ((logic_signal = (*dec->channels().begin()).second)) &&
7aa09b00 252 ((data = logic_signal->logic_data())))
7491a29f
JH
253 break;
254
255 if (!data)
256 return;
257
f3d66e52
JH
258 // Check we have a segment of data
259 const deque< shared_ptr<pv::data::LogicSegment> > &segments =
260 data->logic_segments();
261 if (segments.empty())
f70d8673 262 return;
f3d66e52 263 segment_ = segments.front();
f70d8673 264
7491a29f 265 // Get the samplerate and start time
f3d66e52
JH
266 start_time_ = segment_->start_time();
267 samplerate_ = segment_->samplerate();
8dbbc7f0
JH
268 if (samplerate_ == 0.0)
269 samplerate_ = 1.0;
e0fc5810 270
8dbbc7f0
JH
271 interrupt_ = false;
272 decode_thread_ = std::thread(&DecoderStack::decode_proc, this);
640d69ce
JH
273}
274
e45b13b5 275uint64_t DecoderStack::max_sample_count() const
a007f5ad 276{
f9101a91
JH
277 uint64_t max_sample_count = 0;
278
8dbbc7f0 279 for (auto i = rows_.cbegin(); i != rows_.end(); i++)
f9101a91
JH
280 max_sample_count = max(max_sample_count,
281 (*i).second.get_max_sample());
282
283 return max_sample_count;
a007f5ad
JH
284}
285
f70d8673
JH
286optional<int64_t> DecoderStack::wait_for_data() const
287{
8dbbc7f0 288 unique_lock<mutex> input_lock(input_mutex_);
f3290553 289 while (!interrupt_ && !frame_complete_ &&
8dbbc7f0
JH
290 samples_decoded_ >= sample_count_)
291 input_cond_.wait(input_lock);
292 return boost::make_optional(!interrupt_ &&
293 (samples_decoded_ < sample_count_ || !frame_complete_),
294 sample_count_);
f70d8673
JH
295}
296
f67d9e9b 297void DecoderStack::decode_data(
f70d8673 298 const int64_t sample_count, const unsigned int unit_size,
f67d9e9b
JH
299 srd_session *const session)
300{
301 uint8_t chunk[DecodeChunkLength];
302
f67d9e9b 303 const unsigned int chunk_sample_count =
f3d66e52 304 DecodeChunkLength / segment_->unit_size();
f67d9e9b 305
8dbbc7f0 306 for (int64_t i = 0; !interrupt_ && i < sample_count;
f67d9e9b
JH
307 i += chunk_sample_count)
308 {
8dbbc7f0 309 lock_guard<mutex> decode_lock(global_decode_mutex_);
f67d9e9b
JH
310
311 const int64_t chunk_end = min(
312 i + chunk_sample_count, sample_count);
f3d66e52 313 segment_->get_samples(chunk, i, chunk_end);
f67d9e9b 314
ba5cd8cb 315 if (srd_session_send(session, i, chunk_end, chunk,
c4dd1e4e 316 (chunk_end - i) * unit_size, unit_size) != SRD_OK) {
8dbbc7f0 317 error_message_ = tr("Decoder reported an error");
f67d9e9b
JH
318 break;
319 }
320
321 {
8dbbc7f0
JH
322 lock_guard<mutex> lock(output_mutex_);
323 samples_decoded_ = chunk_end;
f67d9e9b 324 }
4c581c79
JH
325
326 if (i % DecodeNotifyPeriod == 0)
327 new_decode_data();
f67d9e9b
JH
328 }
329
1c5fb759 330 new_decode_data();
f67d9e9b
JH
331}
332
c1b2865e 333void DecoderStack::decode_proc()
119aff65 334{
f70d8673 335 optional<int64_t> sample_count;
b6b267bb 336 srd_session *session;
4c60462b 337 srd_decoder_inst *prev_di = nullptr;
e0fc5810 338
f3d66e52 339 assert(segment_);
e0fc5810 340
b6b267bb
JH
341 // Create the session
342 srd_session_new(&session);
343 assert(session);
e0fc5810 344
7491a29f 345 // Create the decoders
f3d66e52 346 const unsigned int unit_size = segment_->unit_size();
f67d9e9b 347
8dbbc7f0 348 for (const shared_ptr<decode::Decoder> &dec : stack_)
7491a29f 349 {
c4dd1e4e 350 srd_decoder_inst *const di = dec->create_decoder_inst(session);
e0fc5810 351
7491a29f
JH
352 if (!di)
353 {
8dbbc7f0 354 error_message_ = tr("Failed to create decoder instance");
7491a29f
JH
355 srd_session_destroy(session);
356 return;
357 }
b6b267bb 358
7491a29f
JH
359 if (prev_di)
360 srd_inst_stack (session, prev_di, di);
b6b267bb 361
7491a29f 362 prev_di = di;
b6b267bb
JH
363 }
364
f70d8673
JH
365 // Get the intial sample count
366 {
8dbbc7f0 367 unique_lock<mutex> input_lock(input_mutex_);
f3d66e52 368 sample_count = sample_count_ = segment_->get_sample_count();
f70d8673
JH
369 }
370
b6b267bb 371 // Start the session
7491a29f 372 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
8dbbc7f0 373 g_variant_new_uint64((uint64_t)samplerate_));
7491a29f
JH
374
375 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
376 DecoderStack::annotation_callback, this);
377
b6b267bb
JH
378 srd_session_start(session);
379
f70d8673
JH
380 do {
381 decode_data(*sample_count, unit_size, session);
f3290553 382 } while (error_message_.isEmpty() && (sample_count = wait_for_data()));
b6b267bb
JH
383
384 // Destroy the session
385 srd_session_destroy(session);
e0fc5810
JH
386}
387
6e89374a 388void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
e0fc5810 389{
e0fc5810
JH
390 assert(pdata);
391 assert(decoder);
392
6e89374a 393 DecoderStack *const d = (DecoderStack*)decoder;
f9101a91 394 assert(d);
e0fc5810 395
8dbbc7f0 396 lock_guard<mutex> lock(d->output_mutex_);
6d8b0562 397
f9101a91
JH
398 const Annotation a(pdata);
399
400 // Find the row
401 assert(pdata->pdo);
402 assert(pdata->pdo->di);
403 const srd_decoder *const decc = pdata->pdo->di->decoder;
404 assert(decc);
405
8dbbc7f0 406 auto row_iter = d->rows_.end();
360ab9be 407
f9101a91 408 // Try looking up the sub-row of this class
8dbbc7f0
JH
409 const auto r = d->class_rows_.find(make_pair(decc, a.format()));
410 if (r != d->class_rows_.end())
411 row_iter = d->rows_.find((*r).second);
f9101a91 412 else
6d8b0562 413 {
f9101a91 414 // Failing that, use the decoder as a key
8dbbc7f0 415 row_iter = d->rows_.find(Row(decc));
6d8b0562
UH
416 }
417
8dbbc7f0
JH
418 assert(row_iter != d->rows_.end());
419 if (row_iter == d->rows_.end()) {
f9101a91
JH
420 qDebug() << "Unexpected annotation: decoder = " << decc <<
421 ", format = " << a.format();
422 assert(0);
423 return;
424 }
9cef9567 425
f9101a91
JH
426 // Add the annotation
427 (*row_iter).second.push_annotation(a);
119aff65
JH
428}
429
82f50b10
JH
430void DecoderStack::on_new_frame()
431{
432 begin_decode();
433}
434
f70d8673
JH
435void DecoderStack::on_data_received()
436{
437 {
8dbbc7f0 438 unique_lock<mutex> lock(input_mutex_);
f3d66e52
JH
439 if (segment_)
440 sample_count_ = segment_->get_sample_count();
f70d8673 441 }
8dbbc7f0 442 input_cond_.notify_one();
f70d8673
JH
443}
444
445void DecoderStack::on_frame_ended()
446{
447 {
8dbbc7f0 448 unique_lock<mutex> lock(input_mutex_);
f3d66e52 449 if (segment_)
8dbbc7f0 450 frame_complete_ = true;
f70d8673 451 }
8dbbc7f0 452 input_cond_.notify_one();
f70d8673
JH
453}
454
119aff65
JH
455} // namespace data
456} // namespace pv