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