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