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