]>
Commit | Line | Data |
---|---|---|
ad908057 SA |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net> | |
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 | ||
c0c3e62a | 20 | #include <cstring> |
5a914348 | 21 | #include <forward_list> |
0c5fe73e SA |
22 | #include <limits> |
23 | ||
47747218 SA |
24 | #include <QDebug> |
25 | ||
ad908057 SA |
26 | #include "logic.hpp" |
27 | #include "logicsegment.hpp" | |
28 | #include "decodesignal.hpp" | |
29 | #include "signaldata.hpp" | |
30 | ||
ad908057 | 31 | #include <pv/data/decode/decoder.hpp> |
ecd07c20 | 32 | #include <pv/data/decode/row.hpp> |
d3feec23 | 33 | #include <pv/globalsettings.hpp> |
ad908057 SA |
34 | #include <pv/session.hpp> |
35 | ||
5a914348 | 36 | using std::forward_list; |
47747218 SA |
37 | using std::lock_guard; |
38 | using std::make_pair; | |
ad908057 | 39 | using std::make_shared; |
0c5fe73e | 40 | using std::min; |
5ecf957f | 41 | using std::out_of_range; |
ad908057 | 42 | using std::shared_ptr; |
47747218 SA |
43 | using std::unique_lock; |
44 | using pv::data::decode::Annotation; | |
a82325d1 | 45 | using pv::data::decode::DecodeChannel; |
ad908057 | 46 | using pv::data::decode::Decoder; |
ecd07c20 | 47 | using pv::data::decode::Row; |
ad908057 SA |
48 | |
49 | namespace pv { | |
50 | namespace data { | |
51 | ||
47747218 SA |
52 | const double DecodeSignal::DecodeMargin = 1.0; |
53 | const double DecodeSignal::DecodeThreshold = 0.2; | |
3fbddf7f | 54 | const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024; |
ad908057 | 55 | |
ad908057 | 56 | |
47747218 SA |
57 | DecodeSignal::DecodeSignal(pv::Session &session) : |
58 | SignalBase(nullptr, SignalBase::DecodeChannel), | |
59 | session_(session), | |
e91883bb | 60 | srd_session_(nullptr), |
47747218 | 61 | logic_mux_data_invalid_(false), |
e3202557 | 62 | stack_config_changed_(true), |
8a603e13 | 63 | current_segment_id_(0) |
ad908057 | 64 | { |
47747218 SA |
65 | connect(&session_, SIGNAL(capture_state_changed(int)), |
66 | this, SLOT(on_capture_state_changed(int))); | |
ad908057 SA |
67 | } |
68 | ||
47747218 | 69 | DecodeSignal::~DecodeSignal() |
ad908057 | 70 | { |
8ce0e732 | 71 | reset_decode(true); |
ad908057 SA |
72 | } |
73 | ||
47747218 | 74 | const vector< shared_ptr<Decoder> >& DecodeSignal::decoder_stack() const |
ecd07c20 | 75 | { |
47747218 | 76 | return stack_; |
ecd07c20 SA |
77 | } |
78 | ||
c8e60bdf | 79 | void DecodeSignal::stack_decoder(const srd_decoder *decoder) |
ad908057 SA |
80 | { |
81 | assert(decoder); | |
762ab7a4 | 82 | |
b9591f3a SA |
83 | // Set name if this decoder is the first in the list or the name is unchanged |
84 | const srd_decoder* prev_dec = | |
85 | stack_.empty() ? nullptr : stack_.back()->decoder(); | |
86 | const QString prev_dec_name = | |
87 | prev_dec ? QString::fromUtf8(prev_dec->name) : QString(); | |
47747218 | 88 | |
b9591f3a | 89 | if ((stack_.empty()) || ((stack_.size() > 0) && (name() == prev_dec_name))) |
47747218 | 90 | set_name(QString::fromUtf8(decoder->name)); |
132a5c6d | 91 | |
b9591f3a SA |
92 | const shared_ptr<Decoder> dec = make_shared<decode::Decoder>(decoder); |
93 | stack_.push_back(dec); | |
94 | ||
27a3f09b | 95 | // Include the newly created decode channels in the channel lists |
9f97b357 | 96 | update_channel_list(); |
132a5c6d | 97 | |
e3202557 | 98 | stack_config_changed_ = true; |
762ab7a4 | 99 | auto_assign_signals(dec); |
27a3f09b | 100 | commit_decoder_channels(); |
e77de61f SA |
101 | |
102 | decoder_stacked((void*)dec.get()); | |
103 | ||
47747218 | 104 | begin_decode(); |
ad908057 SA |
105 | } |
106 | ||
107 | void DecodeSignal::remove_decoder(int index) | |
108 | { | |
47747218 SA |
109 | assert(index >= 0); |
110 | assert(index < (int)stack_.size()); | |
111 | ||
112 | // Find the decoder in the stack | |
113 | auto iter = stack_.begin(); | |
114 | for (int i = 0; i < index; i++, iter++) | |
115 | assert(iter != stack_.end()); | |
116 | ||
e77de61f SA |
117 | decoder_removed(iter->get()); |
118 | ||
47747218 SA |
119 | // Delete the element |
120 | stack_.erase(iter); | |
121 | ||
122 | // Update channels and decoded data | |
e3202557 | 123 | stack_config_changed_ = true; |
9f97b357 | 124 | update_channel_list(); |
47747218 | 125 | begin_decode(); |
ad908057 SA |
126 | } |
127 | ||
128 | bool DecodeSignal::toggle_decoder_visibility(int index) | |
129 | { | |
47747218 | 130 | auto iter = stack_.cbegin(); |
ad908057 | 131 | for (int i = 0; i < index; i++, iter++) |
47747218 | 132 | assert(iter != stack_.end()); |
ad908057 SA |
133 | |
134 | shared_ptr<Decoder> dec = *iter; | |
135 | ||
136 | // Toggle decoder visibility | |
137 | bool state = false; | |
138 | if (dec) { | |
139 | state = !dec->shown(); | |
140 | dec->show(state); | |
141 | } | |
142 | ||
143 | return state; | |
144 | } | |
145 | ||
8ce0e732 | 146 | void DecodeSignal::reset_decode(bool shutting_down) |
47747218 | 147 | { |
8ce0e732 | 148 | if (stack_config_changed_ || shutting_down) |
e3202557 SA |
149 | stop_srd_session(); |
150 | else | |
151 | terminate_srd_session(); | |
ce0b6a40 | 152 | |
1b56c646 SA |
153 | if (decode_thread_.joinable()) { |
154 | decode_interrupt_ = true; | |
155 | decode_input_cond_.notify_one(); | |
156 | decode_thread_.join(); | |
157 | } | |
158 | ||
159 | if (logic_mux_thread_.joinable()) { | |
160 | logic_mux_interrupt_ = true; | |
161 | logic_mux_cond_.notify_one(); | |
162 | logic_mux_thread_.join(); | |
163 | } | |
164 | ||
dda88657 | 165 | resume_decode(); // Make sure the decode thread isn't blocked by pausing |
556259d2 | 166 | |
47747218 | 167 | class_rows_.clear(); |
8a603e13 | 168 | current_segment_id_ = 0; |
72435789 | 169 | segments_.clear(); |
1b56c646 SA |
170 | |
171 | logic_mux_data_.reset(); | |
172 | logic_mux_data_invalid_ = true; | |
eee3eab9 | 173 | |
403c3e87 SA |
174 | if (!error_message_.isEmpty()) { |
175 | error_message_ = QString(); | |
fe060a48 SA |
176 | // TODO Emulate noquote() |
177 | qDebug().nospace() << name() << ": Error cleared"; | |
403c3e87 | 178 | } |
72435789 | 179 | |
eee3eab9 | 180 | decode_reset(); |
47747218 SA |
181 | } |
182 | ||
946b52e1 SA |
183 | void DecodeSignal::begin_decode() |
184 | { | |
47747218 SA |
185 | if (decode_thread_.joinable()) { |
186 | decode_interrupt_ = true; | |
187 | decode_input_cond_.notify_one(); | |
188 | decode_thread_.join(); | |
189 | } | |
190 | ||
191 | if (logic_mux_thread_.joinable()) { | |
192 | logic_mux_interrupt_ = true; | |
193 | logic_mux_cond_.notify_one(); | |
194 | logic_mux_thread_.join(); | |
195 | } | |
196 | ||
197 | reset_decode(); | |
198 | ||
27a3f09b | 199 | if (stack_.size() == 0) { |
403c3e87 | 200 | set_error_message(tr("No decoders")); |
27a3f09b SA |
201 | return; |
202 | } | |
203 | ||
204 | assert(channels_.size() > 0); | |
205 | ||
206 | if (get_assigned_signal_count() == 0) { | |
403c3e87 | 207 | set_error_message(tr("There are no channels assigned to this decoder")); |
27a3f09b SA |
208 | return; |
209 | } | |
210 | ||
04627946 SA |
211 | // Make sure that all assigned channels still provide logic data |
212 | // (can happen when a converted signal was assigned but the | |
213 | // conversion removed in the meanwhile) | |
a82325d1 | 214 | for (decode::DecodeChannel& ch : channels_) |
04627946 SA |
215 | if (ch.assigned_signal && !(ch.assigned_signal->logic_data() != nullptr)) |
216 | ch.assigned_signal = nullptr; | |
217 | ||
47747218 | 218 | // Check that all decoders have the required channels |
f4ab4b5c | 219 | for (const shared_ptr<decode::Decoder>& dec : stack_) |
47747218 | 220 | if (!dec->have_required_channels()) { |
403c3e87 SA |
221 | set_error_message(tr("One or more required channels " |
222 | "have not been specified")); | |
47747218 SA |
223 | return; |
224 | } | |
225 | ||
4913560f | 226 | // Map out all the annotation classes |
f9807084 | 227 | int row_index = 0; |
f4ab4b5c | 228 | for (const shared_ptr<decode::Decoder>& dec : stack_) { |
47747218 SA |
229 | assert(dec); |
230 | const srd_decoder *const decc = dec->decoder(); | |
231 | assert(dec->decoder()); | |
232 | ||
47747218 SA |
233 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { |
234 | const srd_decoder_annotation_row *const ann_row = | |
235 | (srd_decoder_annotation_row *)l->data; | |
236 | assert(ann_row); | |
237 | ||
f9807084 | 238 | const Row row(row_index++, decc, ann_row); |
47747218 | 239 | |
47747218 SA |
240 | for (const GSList *ll = ann_row->ann_classes; |
241 | ll; ll = ll->next) | |
242 | class_rows_[make_pair(decc, | |
243 | GPOINTER_TO_INT(ll->data))] = row; | |
244 | } | |
245 | } | |
246 | ||
27a3f09b SA |
247 | // Free the logic data and its segment(s) if it needs to be updated |
248 | if (logic_mux_data_invalid_) | |
249 | logic_mux_data_.reset(); | |
250 | ||
251 | if (!logic_mux_data_) { | |
72435789 | 252 | const uint32_t ch_count = get_assigned_signal_count(); |
ed535cd7 | 253 | logic_mux_unit_size_ = (ch_count + 7) / 8; |
27a3f09b | 254 | logic_mux_data_ = make_shared<Logic>(ch_count); |
27a3f09b | 255 | } |
47747218 | 256 | |
ed535cd7 SA |
257 | // Receive notifications when new sample data is available |
258 | connect_input_notifiers(); | |
259 | ||
8a603e13 | 260 | if (get_input_segment_count() == 0) { |
403c3e87 | 261 | set_error_message(tr("No input data")); |
ed535cd7 SA |
262 | return; |
263 | } | |
264 | ||
27a3f09b SA |
265 | // Make sure the logic output data is complete and up-to-date |
266 | logic_mux_interrupt_ = false; | |
267 | logic_mux_thread_ = std::thread(&DecodeSignal::logic_mux_proc, this); | |
268 | ||
269 | // Decode the muxed logic data | |
47747218 SA |
270 | decode_interrupt_ = false; |
271 | decode_thread_ = std::thread(&DecodeSignal::decode_proc, this); | |
946b52e1 SA |
272 | } |
273 | ||
556259d2 SA |
274 | void DecodeSignal::pause_decode() |
275 | { | |
276 | decode_paused_ = true; | |
277 | } | |
278 | ||
279 | void DecodeSignal::resume_decode() | |
280 | { | |
281 | // Manual unlocking is done before notifying, to avoid waking up the | |
282 | // waiting thread only to block again (see notify_one for details) | |
283 | decode_pause_mutex_.unlock(); | |
284 | decode_pause_cond_.notify_one(); | |
285 | decode_paused_ = false; | |
286 | } | |
287 | ||
288 | bool DecodeSignal::is_paused() const | |
289 | { | |
290 | return decode_paused_; | |
291 | } | |
292 | ||
ecd07c20 SA |
293 | QString DecodeSignal::error_message() const |
294 | { | |
47747218 SA |
295 | lock_guard<mutex> lock(output_mutex_); |
296 | return error_message_; | |
ecd07c20 SA |
297 | } |
298 | ||
a82325d1 | 299 | const vector<decode::DecodeChannel> DecodeSignal::get_channels() const |
9f97b357 SA |
300 | { |
301 | return channels_; | |
302 | } | |
303 | ||
762ab7a4 | 304 | void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec) |
132a5c6d | 305 | { |
47747218 SA |
306 | bool new_assignment = false; |
307 | ||
132a5c6d | 308 | // Try to auto-select channels that don't have signals assigned yet |
a82325d1 | 309 | for (decode::DecodeChannel& ch : channels_) { |
762ab7a4 SA |
310 | // If a decoder is given, auto-assign only its channels |
311 | if (dec && (ch.decoder_ != dec)) | |
312 | continue; | |
313 | ||
132a5c6d SA |
314 | if (ch.assigned_signal) |
315 | continue; | |
316 | ||
c526a8fd SA |
317 | QString ch_name = ch.name.toLower(); |
318 | ch_name = ch_name.replace(QRegExp("[-_.]"), " "); | |
a8030bb0 SA |
319 | |
320 | shared_ptr<data::SignalBase> match; | |
f4ab4b5c | 321 | for (const shared_ptr<data::SignalBase>& s : session_.signalbases()) { |
f9ebf303 SA |
322 | if (!s->enabled()) |
323 | continue; | |
324 | ||
c526a8fd SA |
325 | QString s_name = s->name().toLower(); |
326 | s_name = s_name.replace(QRegExp("[-_.]"), " "); | |
692f6093 SA |
327 | |
328 | if (s->logic_data() && | |
329 | ((ch_name.contains(s_name)) || (s_name.contains(ch_name)))) { | |
a8030bb0 SA |
330 | if (!match) |
331 | match = s; | |
332 | else { | |
333 | // Only replace an existing match if it matches more characters | |
334 | int old_unmatched = ch_name.length() - match->name().length(); | |
335 | int new_unmatched = ch_name.length() - s->name().length(); | |
336 | if (abs(new_unmatched) < abs(old_unmatched)) | |
337 | match = s; | |
338 | } | |
47747218 | 339 | } |
692f6093 | 340 | } |
a8030bb0 SA |
341 | |
342 | if (match) { | |
343 | ch.assigned_signal = match.get(); | |
344 | new_assignment = true; | |
345 | } | |
47747218 SA |
346 | } |
347 | ||
348 | if (new_assignment) { | |
349 | logic_mux_data_invalid_ = true; | |
e3202557 | 350 | stack_config_changed_ = true; |
27a3f09b | 351 | commit_decoder_channels(); |
47747218 | 352 | channels_updated(); |
132a5c6d SA |
353 | } |
354 | } | |
355 | ||
9f97b357 SA |
356 | void DecodeSignal::assign_signal(const uint16_t channel_id, const SignalBase *signal) |
357 | { | |
a82325d1 | 358 | for (decode::DecodeChannel& ch : channels_) |
47747218 | 359 | if (ch.id == channel_id) { |
9f97b357 | 360 | ch.assigned_signal = signal; |
47747218 SA |
361 | logic_mux_data_invalid_ = true; |
362 | } | |
9f97b357 | 363 | |
e3202557 | 364 | stack_config_changed_ = true; |
27a3f09b | 365 | commit_decoder_channels(); |
9f97b357 | 366 | channels_updated(); |
47747218 | 367 | begin_decode(); |
9f97b357 SA |
368 | } |
369 | ||
27a3f09b SA |
370 | int DecodeSignal::get_assigned_signal_count() const |
371 | { | |
372 | // Count all channels that have a signal assigned to them | |
373 | return count_if(channels_.begin(), channels_.end(), | |
a82325d1 | 374 | [](decode::DecodeChannel ch) { return ch.assigned_signal; }); |
27a3f09b SA |
375 | } |
376 | ||
9f97b357 SA |
377 | void DecodeSignal::set_initial_pin_state(const uint16_t channel_id, const int init_state) |
378 | { | |
a82325d1 | 379 | for (decode::DecodeChannel& ch : channels_) |
9f97b357 SA |
380 | if (ch.id == channel_id) |
381 | ch.initial_pin_state = init_state; | |
382 | ||
e3202557 | 383 | stack_config_changed_ = true; |
9f97b357 | 384 | channels_updated(); |
47747218 | 385 | begin_decode(); |
9f97b357 SA |
386 | } |
387 | ||
ff83d980 SA |
388 | double DecodeSignal::samplerate() const |
389 | { | |
72435789 SA |
390 | double result = 0; |
391 | ||
392 | // TODO For now, we simply return the first samplerate that we have | |
8a603e13 SA |
393 | if (segments_.size() > 0) |
394 | result = segments_.front().samplerate; | |
72435789 SA |
395 | |
396 | return result; | |
ff83d980 SA |
397 | } |
398 | ||
72435789 | 399 | const pv::util::Timestamp DecodeSignal::start_time() const |
ff83d980 | 400 | { |
72435789 SA |
401 | pv::util::Timestamp result; |
402 | ||
403 | // TODO For now, we simply return the first start time that we have | |
8a603e13 SA |
404 | if (segments_.size() > 0) |
405 | result = segments_.front().start_time; | |
72435789 SA |
406 | |
407 | return result; | |
ff83d980 SA |
408 | } |
409 | ||
5ecf957f | 410 | int64_t DecodeSignal::get_working_sample_count(uint32_t segment_id) const |
0c5fe73e SA |
411 | { |
412 | // The working sample count is the highest sample number for | |
ed535cd7 SA |
413 | // which all used signals have data available, so go through all |
414 | // channels and use the lowest overall sample count of the segment | |
0c5fe73e | 415 | |
0c5fe73e SA |
416 | int64_t count = std::numeric_limits<int64_t>::max(); |
417 | bool no_signals_assigned = true; | |
418 | ||
a82325d1 | 419 | for (const decode::DecodeChannel& ch : channels_) |
0c5fe73e SA |
420 | if (ch.assigned_signal) { |
421 | no_signals_assigned = false; | |
422 | ||
423 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
424 | if (!logic_data || logic_data->logic_segments().empty()) | |
425 | return 0; | |
426 | ||
5ecf957f SA |
427 | try { |
428 | const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id); | |
429 | count = min(count, (int64_t)segment->get_sample_count()); | |
30677c13 | 430 | } catch (out_of_range&) { |
5ecf957f SA |
431 | return 0; |
432 | } | |
0c5fe73e SA |
433 | } |
434 | ||
435 | return (no_signals_assigned ? 0 : count); | |
436 | } | |
437 | ||
b82908ab SA |
438 | int64_t DecodeSignal::get_decoded_sample_count(uint32_t segment_id, |
439 | bool include_processing) const | |
ff83d980 | 440 | { |
47747218 | 441 | lock_guard<mutex> decode_lock(output_mutex_); |
5ecf957f SA |
442 | |
443 | int64_t result = 0; | |
444 | ||
72435789 SA |
445 | try { |
446 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
b82908ab SA |
447 | if (include_processing) |
448 | result = segment->samples_decoded_incl; | |
449 | else | |
450 | result = segment->samples_decoded_excl; | |
30677c13 | 451 | } catch (out_of_range&) { |
72435789 SA |
452 | // Do nothing |
453 | } | |
5ecf957f SA |
454 | |
455 | return result; | |
ff83d980 SA |
456 | } |
457 | ||
ab185f78 | 458 | vector<Row> DecodeSignal::get_rows(bool visible_only) const |
ecd07c20 | 459 | { |
47747218 SA |
460 | lock_guard<mutex> lock(output_mutex_); |
461 | ||
462 | vector<Row> rows; | |
463 | ||
f4ab4b5c | 464 | for (const shared_ptr<decode::Decoder>& dec : stack_) { |
47747218 | 465 | assert(dec); |
ab185f78 | 466 | if (visible_only && !dec->shown()) |
47747218 SA |
467 | continue; |
468 | ||
469 | const srd_decoder *const decc = dec->decoder(); | |
470 | assert(dec->decoder()); | |
471 | ||
f9807084 | 472 | int row_index = 0; |
47747218 SA |
473 | // Add a row for the decoder if it doesn't have a row list |
474 | if (!decc->annotation_rows) | |
f9807084 | 475 | rows.emplace_back(row_index++, decc); |
47747218 SA |
476 | |
477 | // Add the decoder rows | |
478 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { | |
479 | const srd_decoder_annotation_row *const ann_row = | |
480 | (srd_decoder_annotation_row *)l->data; | |
481 | assert(ann_row); | |
f9807084 | 482 | rows.emplace_back(row_index++, decc, ann_row); |
47747218 SA |
483 | } |
484 | } | |
485 | ||
486 | return rows; | |
ecd07c20 SA |
487 | } |
488 | ||
489 | void DecodeSignal::get_annotation_subset( | |
490 | vector<pv::data::decode::Annotation> &dest, | |
72435789 | 491 | const decode::Row &row, uint32_t segment_id, uint64_t start_sample, |
ecd07c20 SA |
492 | uint64_t end_sample) const |
493 | { | |
47747218 SA |
494 | lock_guard<mutex> lock(output_mutex_); |
495 | ||
72435789 SA |
496 | try { |
497 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
498 | const map<const decode::Row, decode::RowData> *rows = | |
499 | &(segment->annotation_rows); | |
500 | ||
501 | const auto iter = rows->find(row); | |
502 | if (iter != rows->end()) | |
503 | (*iter).second.get_annotation_subset(dest, | |
504 | start_sample, end_sample); | |
30677c13 | 505 | } catch (out_of_range&) { |
72435789 SA |
506 | // Do nothing |
507 | } | |
47747218 SA |
508 | } |
509 | ||
5a914348 SA |
510 | void DecodeSignal::get_annotation_subset( |
511 | vector<pv::data::decode::Annotation> &dest, | |
512 | uint32_t segment_id, uint64_t start_sample, uint64_t end_sample) const | |
513 | { | |
514 | // Note: We put all vectors and lists on the heap, not the stack | |
515 | ||
ab185f78 | 516 | const vector<Row> rows = get_rows(true); |
5a914348 SA |
517 | |
518 | // Use forward_lists for faster merging | |
519 | forward_list<Annotation> *all_ann_list = new forward_list<Annotation>(); | |
520 | ||
521 | for (const Row& row : rows) { | |
522 | vector<Annotation> *ann_vector = new vector<Annotation>(); | |
523 | get_annotation_subset(*ann_vector, row, segment_id, start_sample, end_sample); | |
524 | ||
525 | forward_list<Annotation> *ann_list = | |
526 | new forward_list<Annotation>(ann_vector->begin(), ann_vector->end()); | |
527 | delete ann_vector; | |
528 | ||
529 | all_ann_list->merge(*ann_list); | |
530 | delete ann_list; | |
531 | } | |
532 | ||
533 | move(all_ann_list->begin(), all_ann_list->end(), back_inserter(dest)); | |
534 | delete all_ann_list; | |
535 | } | |
536 | ||
e77de61f SA |
537 | uint32_t DecodeSignal::get_binary_data_chunk_count(uint32_t segment_id, |
538 | const Decoder* dec, uint8_t bin_class_id) const | |
c0c3e62a | 539 | { |
c0c3e62a SA |
540 | try { |
541 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
e77de61f SA |
542 | |
543 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
544 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) | |
545 | return bc.chunks.size(); | |
c0c3e62a SA |
546 | } catch (out_of_range&) { |
547 | // Do nothing | |
548 | } | |
549 | ||
e77de61f | 550 | return 0; |
c0c3e62a SA |
551 | } |
552 | ||
e77de61f SA |
553 | void DecodeSignal::get_binary_data_chunk(uint32_t segment_id, |
554 | const Decoder* dec, uint8_t bin_class_id, uint32_t chunk_id, | |
c0c3e62a SA |
555 | const vector<uint8_t> **dest, uint64_t *size) |
556 | { | |
557 | try { | |
558 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
e77de61f SA |
559 | |
560 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
561 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) { | |
562 | if (dest) *dest = &(bc.chunks.at(chunk_id).data); | |
563 | if (size) *size = bc.chunks.at(chunk_id).data.size(); | |
564 | return; | |
565 | } | |
c0c3e62a SA |
566 | } catch (out_of_range&) { |
567 | // Do nothing | |
568 | } | |
569 | } | |
570 | ||
571 | void DecodeSignal::get_binary_data_chunks_merged(uint32_t segment_id, | |
e77de61f SA |
572 | const Decoder* dec, uint8_t bin_class_id, uint64_t start_sample, |
573 | uint64_t end_sample, vector<uint8_t> *dest) const | |
c0c3e62a SA |
574 | { |
575 | assert(dest != nullptr); | |
576 | ||
577 | try { | |
578 | const DecodeSegment *segment = &(segments_.at(segment_id)); | |
579 | ||
e77de61f SA |
580 | const DecodeBinaryClass* bin_class = nullptr; |
581 | for (const DecodeBinaryClass& bc : segment->binary_classes) | |
582 | if ((bc.decoder == dec) && (bc.info->bin_class_id == bin_class_id)) | |
583 | bin_class = &bc; | |
584 | ||
c0c3e62a SA |
585 | // Determine overall size before copying to resize dest vector only once |
586 | uint64_t size = 0; | |
163f3499 | 587 | uint64_t matches = 0; |
e77de61f SA |
588 | for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) |
589 | if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) { | |
590 | size += chunk.data.size(); | |
4b97fe09 SA |
591 | matches++; |
592 | } | |
593 | dest->resize(size); | |
c0c3e62a | 594 | |
163f3499 SA |
595 | uint64_t offset = 0; |
596 | uint64_t matches2 = 0; | |
e77de61f SA |
597 | for (const DecodeBinaryDataChunk& chunk : bin_class->chunks) |
598 | if ((chunk.sample >= start_sample) && (chunk.sample < end_sample)) { | |
599 | memcpy(dest->data() + offset, chunk.data.data(), chunk.data.size()); | |
600 | offset += chunk.data.size(); | |
163f3499 SA |
601 | matches2++; |
602 | ||
603 | // Make sure we don't overwrite memory if the array grew in the meanwhile | |
604 | if (matches2 == matches) | |
605 | break; | |
c0c3e62a SA |
606 | } |
607 | } catch (out_of_range&) { | |
608 | // Do nothing | |
609 | } | |
610 | } | |
611 | ||
612 | ||
47747218 SA |
613 | void DecodeSignal::save_settings(QSettings &settings) const |
614 | { | |
615 | SignalBase::save_settings(settings); | |
616 | ||
c8e60bdf SA |
617 | settings.setValue("decoders", (int)(stack_.size())); |
618 | ||
8b5ea5a1 | 619 | // Save decoder stack |
c8e60bdf | 620 | int decoder_idx = 0; |
f4ab4b5c | 621 | for (const shared_ptr<decode::Decoder>& decoder : stack_) { |
c8e60bdf SA |
622 | settings.beginGroup("decoder" + QString::number(decoder_idx++)); |
623 | ||
624 | settings.setValue("id", decoder->decoder()->id); | |
722c7624 | 625 | settings.setValue("shown", decoder->shown()); |
c8e60bdf | 626 | |
d3feec23 SA |
627 | // Save decoder options |
628 | const map<string, GVariant*>& options = decoder->options(); | |
629 | ||
630 | settings.setValue("options", (int)options.size()); | |
631 | ||
632 | // Note: decode::Decoder::options() returns only the options | |
633 | // that differ from the default. See binding::Decoder::getter() | |
634 | int i = 0; | |
f4ab4b5c | 635 | for (auto& option : options) { |
d3feec23 SA |
636 | settings.beginGroup("option" + QString::number(i)); |
637 | settings.setValue("name", QString::fromStdString(option.first)); | |
638 | GlobalSettings::store_gvariant(settings, option.second); | |
639 | settings.endGroup(); | |
640 | i++; | |
641 | } | |
642 | ||
c8e60bdf SA |
643 | settings.endGroup(); |
644 | } | |
645 | ||
8b5ea5a1 SA |
646 | // Save channel mapping |
647 | settings.setValue("channels", (int)channels_.size()); | |
648 | ||
649 | for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) { | |
650 | auto channel = find_if(channels_.begin(), channels_.end(), | |
a82325d1 | 651 | [&](decode::DecodeChannel ch) { return ch.id == channel_id; }); |
8b5ea5a1 SA |
652 | |
653 | if (channel == channels_.end()) { | |
654 | qDebug() << "ERROR: Gap in channel index:" << channel_id; | |
655 | continue; | |
656 | } | |
657 | ||
658 | settings.beginGroup("channel" + QString::number(channel_id)); | |
659 | ||
660 | settings.setValue("name", channel->name); // Useful for debugging | |
661 | settings.setValue("initial_pin_state", channel->initial_pin_state); | |
662 | ||
663 | if (channel->assigned_signal) | |
664 | settings.setValue("assigned_signal_name", channel->assigned_signal->name()); | |
665 | ||
666 | settings.endGroup(); | |
667 | } | |
47747218 SA |
668 | } |
669 | ||
670 | void DecodeSignal::restore_settings(QSettings &settings) | |
671 | { | |
672 | SignalBase::restore_settings(settings); | |
673 | ||
8b5ea5a1 | 674 | // Restore decoder stack |
c8e60bdf SA |
675 | GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list()); |
676 | ||
677 | int decoders = settings.value("decoders").toInt(); | |
678 | ||
679 | for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) { | |
680 | settings.beginGroup("decoder" + QString::number(decoder_idx)); | |
681 | ||
682 | QString id = settings.value("id").toString(); | |
683 | ||
684 | for (GSList *entry = dec_list; entry; entry = entry->next) { | |
685 | const srd_decoder *dec = (srd_decoder*)entry->data; | |
686 | if (!dec) | |
687 | continue; | |
688 | ||
689 | if (QString::fromUtf8(dec->id) == id) { | |
d3feec23 SA |
690 | shared_ptr<decode::Decoder> decoder = |
691 | make_shared<decode::Decoder>(dec); | |
692 | ||
693 | stack_.push_back(decoder); | |
722c7624 | 694 | decoder->show(settings.value("shown", true).toBool()); |
d3feec23 SA |
695 | |
696 | // Restore decoder options that differ from their default | |
697 | int options = settings.value("options").toInt(); | |
698 | ||
699 | for (int i = 0; i < options; i++) { | |
700 | settings.beginGroup("option" + QString::number(i)); | |
701 | QString name = settings.value("name").toString(); | |
702 | GVariant *value = GlobalSettings::restore_gvariant(settings); | |
703 | decoder->set_option(name.toUtf8(), value); | |
704 | settings.endGroup(); | |
705 | } | |
c8e60bdf SA |
706 | |
707 | // Include the newly created decode channels in the channel lists | |
708 | update_channel_list(); | |
709 | break; | |
710 | } | |
711 | } | |
712 | ||
713 | settings.endGroup(); | |
4ca06ec3 | 714 | channels_updated(); |
c8e60bdf SA |
715 | } |
716 | ||
8b5ea5a1 SA |
717 | // Restore channel mapping |
718 | unsigned int channels = settings.value("channels").toInt(); | |
719 | ||
720 | const unordered_set< shared_ptr<data::SignalBase> > signalbases = | |
721 | session_.signalbases(); | |
722 | ||
723 | for (unsigned int channel_id = 0; channel_id < channels; channel_id++) { | |
724 | auto channel = find_if(channels_.begin(), channels_.end(), | |
a82325d1 | 725 | [&](decode::DecodeChannel ch) { return ch.id == channel_id; }); |
8b5ea5a1 SA |
726 | |
727 | if (channel == channels_.end()) { | |
728 | qDebug() << "ERROR: Non-existant channel index:" << channel_id; | |
729 | continue; | |
730 | } | |
731 | ||
732 | settings.beginGroup("channel" + QString::number(channel_id)); | |
733 | ||
734 | QString assigned_signal_name = settings.value("assigned_signal_name").toString(); | |
735 | ||
f4ab4b5c | 736 | for (const shared_ptr<data::SignalBase>& signal : signalbases) |
8b5ea5a1 SA |
737 | if (signal->name() == assigned_signal_name) |
738 | channel->assigned_signal = signal.get(); | |
739 | ||
740 | channel->initial_pin_state = settings.value("initial_pin_state").toInt(); | |
741 | ||
742 | settings.endGroup(); | |
743 | } | |
744 | ||
aa71890d | 745 | // Update the internal structures |
e3202557 | 746 | stack_config_changed_ = true; |
aa71890d SA |
747 | update_channel_list(); |
748 | commit_decoder_channels(); | |
749 | ||
8b5ea5a1 | 750 | begin_decode(); |
47747218 SA |
751 | } |
752 | ||
403c3e87 SA |
753 | void DecodeSignal::set_error_message(QString msg) |
754 | { | |
755 | error_message_ = msg; | |
fe060a48 SA |
756 | // TODO Emulate noquote() |
757 | qDebug().nospace() << name() << ": " << msg; | |
403c3e87 SA |
758 | } |
759 | ||
8a603e13 SA |
760 | uint32_t DecodeSignal::get_input_segment_count() const |
761 | { | |
762 | uint64_t count = std::numeric_limits<uint64_t>::max(); | |
763 | bool no_signals_assigned = true; | |
764 | ||
a82325d1 | 765 | for (const decode::DecodeChannel& ch : channels_) |
8a603e13 SA |
766 | if (ch.assigned_signal) { |
767 | no_signals_assigned = false; | |
768 | ||
769 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
770 | if (!logic_data || logic_data->logic_segments().empty()) | |
771 | return 0; | |
772 | ||
773 | // Find the min value of all segment counts | |
774 | if ((uint64_t)(logic_data->logic_segments().size()) < count) | |
775 | count = logic_data->logic_segments().size(); | |
776 | } | |
777 | ||
778 | return (no_signals_assigned ? 0 : count); | |
779 | } | |
780 | ||
781 | uint32_t DecodeSignal::get_input_samplerate(uint32_t segment_id) const | |
782 | { | |
783 | double samplerate = 0; | |
784 | ||
a82325d1 | 785 | for (const decode::DecodeChannel& ch : channels_) |
8a603e13 SA |
786 | if (ch.assigned_signal) { |
787 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
788 | if (!logic_data || logic_data->logic_segments().empty()) | |
789 | continue; | |
790 | ||
791 | try { | |
792 | const shared_ptr<LogicSegment> segment = logic_data->logic_segments().at(segment_id); | |
793 | samplerate = segment->samplerate(); | |
30677c13 | 794 | } catch (out_of_range&) { |
8a603e13 SA |
795 | // Do nothing |
796 | } | |
797 | break; | |
798 | } | |
799 | ||
800 | return samplerate; | |
801 | } | |
802 | ||
9f97b357 SA |
803 | void DecodeSignal::update_channel_list() |
804 | { | |
a82325d1 | 805 | vector<decode::DecodeChannel> prev_channels = channels_; |
9f97b357 SA |
806 | channels_.clear(); |
807 | ||
808 | uint16_t id = 0; | |
809 | ||
810 | // Copy existing entries, create new as needed | |
f4ab4b5c | 811 | for (shared_ptr<Decoder>& decoder : stack_) { |
9f97b357 SA |
812 | const srd_decoder* srd_d = decoder->decoder(); |
813 | const GSList *l; | |
814 | ||
815 | // Mandatory channels | |
816 | for (l = srd_d->channels; l; l = l->next) { | |
817 | const struct srd_channel *const pdch = (struct srd_channel *)l->data; | |
818 | bool ch_added = false; | |
819 | ||
820 | // Copy but update ID if this channel was in the list before | |
a82325d1 | 821 | for (decode::DecodeChannel& ch : prev_channels) |
9f97b357 SA |
822 | if (ch.pdch_ == pdch) { |
823 | ch.id = id++; | |
824 | channels_.push_back(ch); | |
825 | ch_added = true; | |
826 | break; | |
827 | } | |
828 | ||
829 | if (!ch_added) { | |
830 | // Create new entry without a mapped signal | |
a82325d1 | 831 | decode::DecodeChannel ch = {id++, 0, false, nullptr, |
9f97b357 SA |
832 | QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc), |
833 | SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch}; | |
834 | channels_.push_back(ch); | |
835 | } | |
836 | } | |
837 | ||
838 | // Optional channels | |
839 | for (l = srd_d->opt_channels; l; l = l->next) { | |
840 | const struct srd_channel *const pdch = (struct srd_channel *)l->data; | |
841 | bool ch_added = false; | |
842 | ||
843 | // Copy but update ID if this channel was in the list before | |
a82325d1 | 844 | for (decode::DecodeChannel& ch : prev_channels) |
9f97b357 SA |
845 | if (ch.pdch_ == pdch) { |
846 | ch.id = id++; | |
847 | channels_.push_back(ch); | |
848 | ch_added = true; | |
849 | break; | |
850 | } | |
851 | ||
852 | if (!ch_added) { | |
853 | // Create new entry without a mapped signal | |
a82325d1 | 854 | decode::DecodeChannel ch = {id++, 0, true, nullptr, |
9f97b357 SA |
855 | QString::fromUtf8(pdch->name), QString::fromUtf8(pdch->desc), |
856 | SRD_INITIAL_PIN_SAME_AS_SAMPLE0, decoder, pdch}; | |
857 | channels_.push_back(ch); | |
858 | } | |
859 | } | |
860 | } | |
861 | ||
47747218 SA |
862 | // Invalidate the logic output data if the channel assignment changed |
863 | if (prev_channels.size() != channels_.size()) { | |
864 | // The number of channels changed, there's definitely a difference | |
865 | logic_mux_data_invalid_ = true; | |
866 | } else { | |
867 | // Same number but assignment may still differ, so compare all channels | |
868 | for (size_t i = 0; i < channels_.size(); i++) { | |
a82325d1 SA |
869 | const decode::DecodeChannel& p_ch = prev_channels[i]; |
870 | const decode::DecodeChannel& ch = channels_[i]; | |
47747218 SA |
871 | |
872 | if ((p_ch.pdch_ != ch.pdch_) || | |
873 | (p_ch.assigned_signal != ch.assigned_signal)) { | |
874 | logic_mux_data_invalid_ = true; | |
875 | break; | |
876 | } | |
877 | } | |
878 | ||
879 | } | |
880 | ||
9f97b357 SA |
881 | channels_updated(); |
882 | } | |
883 | ||
27a3f09b | 884 | void DecodeSignal::commit_decoder_channels() |
ad908057 | 885 | { |
27a3f09b SA |
886 | // Submit channel list to every decoder, containing only the relevant channels |
887 | for (shared_ptr<decode::Decoder> dec : stack_) { | |
a82325d1 | 888 | vector<decode::DecodeChannel*> channel_list; |
27a3f09b | 889 | |
a82325d1 | 890 | for (decode::DecodeChannel& ch : channels_) |
27a3f09b SA |
891 | if (ch.decoder_ == dec) |
892 | channel_list.push_back(&ch); | |
47747218 | 893 | |
27a3f09b SA |
894 | dec->set_channels(channel_list); |
895 | } | |
aa71890d SA |
896 | |
897 | // Channel bit IDs must be in sync with the channel's apperance in channels_ | |
898 | int id = 0; | |
a82325d1 | 899 | for (decode::DecodeChannel& ch : channels_) |
aa71890d SA |
900 | if (ch.assigned_signal) |
901 | ch.bit_id = id++; | |
47747218 SA |
902 | } |
903 | ||
ed535cd7 | 904 | void DecodeSignal::mux_logic_samples(uint32_t segment_id, const int64_t start, const int64_t end) |
47747218 | 905 | { |
27a3f09b SA |
906 | // Enforce end to be greater than start |
907 | if (end <= start) | |
908 | return; | |
909 | ||
ed535cd7 | 910 | // Fetch the channel segments and their data |
27a3f09b SA |
911 | vector<shared_ptr<LogicSegment> > segments; |
912 | vector<const uint8_t*> signal_data; | |
913 | vector<uint8_t> signal_in_bytepos; | |
914 | vector<uint8_t> signal_in_bitpos; | |
915 | ||
a82325d1 | 916 | for (decode::DecodeChannel& ch : channels_) |
27a3f09b SA |
917 | if (ch.assigned_signal) { |
918 | const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data(); | |
ed535cd7 SA |
919 | |
920 | shared_ptr<LogicSegment> segment; | |
921 | try { | |
922 | segment = logic_data->logic_segments().at(segment_id); | |
30677c13 | 923 | } catch (out_of_range&) { |
ed535cd7 SA |
924 | qDebug() << "Muxer error for" << name() << ":" << ch.assigned_signal->name() \ |
925 | << "has no logic segment" << segment_id; | |
926 | return; | |
927 | } | |
27a3f09b | 928 | segments.push_back(segment); |
b82243f7 SA |
929 | |
930 | uint8_t* data = new uint8_t[(end - start) * segment->unit_size()]; | |
931 | segment->get_samples(start, end, data); | |
932 | signal_data.push_back(data); | |
27a3f09b SA |
933 | |
934 | const int bitpos = ch.assigned_signal->logic_bit_index(); | |
935 | signal_in_bytepos.push_back(bitpos / 8); | |
936 | signal_in_bitpos.push_back(bitpos % 8); | |
937 | } | |
938 | ||
ed535cd7 SA |
939 | |
940 | shared_ptr<LogicSegment> output_segment; | |
941 | try { | |
942 | output_segment = logic_mux_data_->logic_segments().at(segment_id); | |
30677c13 | 943 | } catch (out_of_range&) { |
ed535cd7 SA |
944 | qDebug() << "Muxer error for" << name() << ": no logic mux segment" \ |
945 | << segment_id << "in mux_logic_samples(), mux segments size is" \ | |
946 | << logic_mux_data_->logic_segments().size(); | |
947 | return; | |
948 | } | |
949 | ||
27a3f09b | 950 | // Perform the muxing of signal data into the output data |
8a603e13 | 951 | uint8_t* output = new uint8_t[(end - start) * output_segment->unit_size()]; |
27a3f09b SA |
952 | unsigned int signal_count = signal_data.size(); |
953 | ||
3b5282a2 SA |
954 | for (int64_t sample_cnt = 0; !logic_mux_interrupt_ && (sample_cnt < (end - start)); |
955 | sample_cnt++) { | |
956 | ||
27a3f09b SA |
957 | int bitpos = 0; |
958 | uint8_t bytepos = 0; | |
959 | ||
8a603e13 SA |
960 | const int out_sample_pos = sample_cnt * output_segment->unit_size(); |
961 | for (unsigned int i = 0; i < output_segment->unit_size(); i++) | |
27a3f09b | 962 | output[out_sample_pos + i] = 0; |
47747218 | 963 | |
27a3f09b SA |
964 | for (unsigned int i = 0; i < signal_count; i++) { |
965 | const int in_sample_pos = sample_cnt * segments[i]->unit_size(); | |
966 | const uint8_t in_sample = 1 & | |
967 | ((signal_data[i][in_sample_pos + signal_in_bytepos[i]]) >> (signal_in_bitpos[i])); | |
47747218 | 968 | |
27a3f09b SA |
969 | const uint8_t out_sample = output[out_sample_pos + bytepos]; |
970 | ||
971 | output[out_sample_pos + bytepos] = out_sample | (in_sample << bitpos); | |
972 | ||
973 | bitpos++; | |
974 | if (bitpos > 7) { | |
975 | bitpos = 0; | |
976 | bytepos++; | |
977 | } | |
978 | } | |
47747218 SA |
979 | } |
980 | ||
8a603e13 | 981 | output_segment->append_payload(output, (end - start) * output_segment->unit_size()); |
27a3f09b SA |
982 | delete[] output; |
983 | ||
984 | for (const uint8_t* data : signal_data) | |
985 | delete[] data; | |
986 | } | |
987 | ||
988 | void DecodeSignal::logic_mux_proc() | |
989 | { | |
ed535cd7 SA |
990 | uint32_t segment_id = 0; |
991 | ||
8a603e13 SA |
992 | assert(logic_mux_data_); |
993 | ||
65efd025 SA |
994 | // Create initial logic mux segment |
995 | shared_ptr<LogicSegment> output_segment = | |
85a70280 SA |
996 | make_shared<LogicSegment>(*logic_mux_data_, segment_id, |
997 | logic_mux_unit_size_, 0); | |
65efd025 | 998 | logic_mux_data_->push_segment(output_segment); |
8a603e13 SA |
999 | |
1000 | output_segment->set_samplerate(get_input_samplerate(0)); | |
ed535cd7 | 1001 | |
27a3f09b | 1002 | do { |
ed535cd7 | 1003 | const uint64_t input_sample_count = get_working_sample_count(segment_id); |
8a603e13 | 1004 | const uint64_t output_sample_count = output_segment->get_sample_count(); |
27a3f09b SA |
1005 | |
1006 | const uint64_t samples_to_process = | |
1007 | (input_sample_count > output_sample_count) ? | |
1008 | (input_sample_count - output_sample_count) : 0; | |
1009 | ||
1010 | // Process the samples if necessary... | |
1011 | if (samples_to_process > 0) { | |
8a603e13 | 1012 | const uint64_t unit_size = output_segment->unit_size(); |
27a3f09b SA |
1013 | const uint64_t chunk_sample_count = DecodeChunkLength / unit_size; |
1014 | ||
1015 | uint64_t processed_samples = 0; | |
1016 | do { | |
1017 | const uint64_t start_sample = output_sample_count + processed_samples; | |
1018 | const uint64_t sample_count = | |
1019 | min(samples_to_process - processed_samples, chunk_sample_count); | |
1020 | ||
ed535cd7 | 1021 | mux_logic_samples(segment_id, start_sample, start_sample + sample_count); |
27a3f09b SA |
1022 | processed_samples += sample_count; |
1023 | ||
1024 | // ...and process the newly muxed logic data | |
1025 | decode_input_cond_.notify_one(); | |
3b5282a2 | 1026 | } while (!logic_mux_interrupt_ && (processed_samples < samples_to_process)); |
27a3f09b SA |
1027 | } |
1028 | ||
1ec191ed | 1029 | if (samples_to_process == 0) { |
ed535cd7 SA |
1030 | // TODO Optimize this by caching the input segment count and only |
1031 | // querying it when the cached value was reached | |
1032 | if (segment_id < get_input_segment_count() - 1) { | |
1033 | // Process next segment | |
1034 | segment_id++; | |
1035 | ||
65efd025 | 1036 | output_segment = |
85a70280 SA |
1037 | make_shared<LogicSegment>(*logic_mux_data_, segment_id, |
1038 | logic_mux_unit_size_, 0); | |
65efd025 | 1039 | logic_mux_data_->push_segment(output_segment); |
ba5f2186 | 1040 | |
8a603e13 SA |
1041 | output_segment->set_samplerate(get_input_samplerate(segment_id)); |
1042 | ||
ed535cd7 SA |
1043 | } else { |
1044 | // All segments have been processed | |
1045 | logic_mux_data_invalid_ = false; | |
1046 | ||
1047 | // Wait for more input | |
1048 | unique_lock<mutex> logic_mux_lock(logic_mux_mutex_); | |
1049 | logic_mux_cond_.wait(logic_mux_lock); | |
1050 | } | |
27a3f09b | 1051 | } |
1ae18301 | 1052 | |
1ec191ed | 1053 | } while (!logic_mux_interrupt_); |
47747218 SA |
1054 | } |
1055 | ||
1056 | void DecodeSignal::decode_data( | |
8a603e13 SA |
1057 | const int64_t abs_start_samplenum, const int64_t sample_count, |
1058 | const shared_ptr<LogicSegment> input_segment) | |
47747218 | 1059 | { |
8a603e13 | 1060 | const int64_t unit_size = input_segment->unit_size(); |
27a3f09b | 1061 | const int64_t chunk_sample_count = DecodeChunkLength / unit_size; |
47747218 SA |
1062 | |
1063 | for (int64_t i = abs_start_samplenum; | |
1ae18301 SA |
1064 | error_message_.isEmpty() && !decode_interrupt_ && |
1065 | (i < (abs_start_samplenum + sample_count)); | |
47747218 SA |
1066 | i += chunk_sample_count) { |
1067 | ||
1068 | const int64_t chunk_end = min(i + chunk_sample_count, | |
1069 | abs_start_samplenum + sample_count); | |
1070 | ||
e06cf18d SA |
1071 | { |
1072 | lock_guard<mutex> lock(output_mutex_); | |
b82908ab SA |
1073 | // Update the sample count showing the samples including currently processed ones |
1074 | segments_.at(current_segment_id_).samples_decoded_incl = chunk_end; | |
e06cf18d SA |
1075 | } |
1076 | ||
b82243f7 SA |
1077 | int64_t data_size = (chunk_end - i) * unit_size; |
1078 | uint8_t* chunk = new uint8_t[data_size]; | |
8a603e13 | 1079 | input_segment->get_samples(i, chunk_end, chunk); |
47747218 | 1080 | |
e91883bb | 1081 | if (srd_session_send(srd_session_, i, chunk_end, chunk, |
1ae18301 | 1082 | data_size, unit_size) != SRD_OK) |
403c3e87 | 1083 | set_error_message(tr("Decoder reported an error")); |
a3ebd556 | 1084 | |
47747218 SA |
1085 | delete[] chunk; |
1086 | ||
b82908ab SA |
1087 | { |
1088 | lock_guard<mutex> lock(output_mutex_); | |
1089 | // Now that all samples are processed, the exclusive sample count catches up | |
1090 | segments_.at(current_segment_id_).samples_decoded_excl = chunk_end; | |
1091 | } | |
1092 | ||
1b56c646 SA |
1093 | // Notify the frontend that we processed some data and |
1094 | // possibly have new annotations as well | |
1095 | new_annotations(); | |
556259d2 SA |
1096 | |
1097 | if (decode_paused_) { | |
1098 | unique_lock<mutex> pause_wait_lock(decode_pause_mutex_); | |
1099 | decode_pause_cond_.wait(pause_wait_lock); | |
1100 | } | |
47747218 SA |
1101 | } |
1102 | } | |
1103 | ||
1104 | void DecodeSignal::decode_proc() | |
1105 | { | |
65efd025 SA |
1106 | current_segment_id_ = 0; |
1107 | ||
1108 | // If there is no input data available yet, wait until it is or we're interrupted | |
1109 | if (logic_mux_data_->logic_segments().size() == 0) { | |
1110 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
1111 | decode_input_cond_.wait(input_wait_lock); | |
1112 | } | |
a3ebd556 SA |
1113 | |
1114 | if (decode_interrupt_) | |
1115 | return; | |
1116 | ||
8a603e13 | 1117 | shared_ptr<LogicSegment> input_segment = logic_mux_data_->logic_segments().front(); |
8a603e13 | 1118 | assert(input_segment); |
65efd025 SA |
1119 | |
1120 | // Create the initial segment and set its sample rate so that we can pass it to SRD | |
1121 | create_decode_segment(); | |
8a603e13 | 1122 | segments_.at(current_segment_id_).samplerate = input_segment->samplerate(); |
65efd025 | 1123 | segments_.at(current_segment_id_).start_time = input_segment->start_time(); |
8a603e13 | 1124 | |
65efd025 SA |
1125 | start_srd_session(); |
1126 | ||
1127 | uint64_t sample_count = 0; | |
27a3f09b | 1128 | uint64_t abs_start_samplenum = 0; |
47747218 | 1129 | do { |
27a3f09b SA |
1130 | // Keep processing new samples until we exhaust the input data |
1131 | do { | |
767bf4e7 | 1132 | lock_guard<mutex> input_lock(input_mutex_); |
8a603e13 | 1133 | sample_count = input_segment->get_sample_count() - abs_start_samplenum; |
27a3f09b SA |
1134 | |
1135 | if (sample_count > 0) { | |
8a603e13 | 1136 | decode_data(abs_start_samplenum, sample_count, input_segment); |
27a3f09b SA |
1137 | abs_start_samplenum += sample_count; |
1138 | } | |
1b56c646 SA |
1139 | } while (error_message_.isEmpty() && (sample_count > 0) && !decode_interrupt_); |
1140 | ||
8a603e13 SA |
1141 | if (error_message_.isEmpty() && !decode_interrupt_ && sample_count == 0) { |
1142 | if (current_segment_id_ < logic_mux_data_->logic_segments().size() - 1) { | |
1143 | // Process next segment | |
1144 | current_segment_id_++; | |
1145 | ||
1146 | try { | |
1147 | input_segment = logic_mux_data_->logic_segments().at(current_segment_id_); | |
30677c13 | 1148 | } catch (out_of_range&) { |
8a603e13 SA |
1149 | qDebug() << "Decode error for" << name() << ": no logic mux segment" \ |
1150 | << current_segment_id_ << "in decode_proc(), mux segments size is" \ | |
1151 | << logic_mux_data_->logic_segments().size(); | |
1152 | return; | |
1153 | } | |
1154 | abs_start_samplenum = 0; | |
1155 | ||
65efd025 SA |
1156 | // Create the next segment and set its metadata |
1157 | create_decode_segment(); | |
8a603e13 | 1158 | segments_.at(current_segment_id_).samplerate = input_segment->samplerate(); |
65efd025 | 1159 | segments_.at(current_segment_id_).start_time = input_segment->start_time(); |
8a603e13 | 1160 | |
90ca4cc8 SA |
1161 | // Reset decoder state but keep the decoder stack intact |
1162 | terminate_srd_session(); | |
8a603e13 SA |
1163 | } else { |
1164 | // All segments have been processed | |
1b56c646 | 1165 | decode_finished(); |
27a3f09b | 1166 | |
8a603e13 SA |
1167 | // Wait for new input data or an interrupt was requested |
1168 | unique_lock<mutex> input_wait_lock(input_mutex_); | |
1169 | decode_input_cond_.wait(input_wait_lock); | |
1170 | } | |
27a3f09b SA |
1171 | } |
1172 | } while (error_message_.isEmpty() && !decode_interrupt_); | |
ce0b6a40 GS |
1173 | |
1174 | // Potentially reap decoders when the application no longer is | |
1175 | // interested in their (pending) results. | |
1176 | if (decode_interrupt_) | |
1177 | terminate_srd_session(); | |
e91883bb SA |
1178 | } |
1179 | ||
1180 | void DecodeSignal::start_srd_session() | |
1181 | { | |
8ce0e732 SA |
1182 | // If there were stack changes, the session has been destroyed by now, so if |
1183 | // it hasn't been destroyed, we can just reset and re-use it | |
ce0b6a40 GS |
1184 | if (srd_session_) { |
1185 | // When a decoder stack was created before, re-use it | |
1186 | // for the next stream of input data, after terminating | |
1187 | // potentially still executing operations, and resetting | |
1188 | // internal state. Skip the rather expensive (teardown | |
1189 | // and) construction of another decoder stack. | |
1190 | ||
1191 | // TODO Reduce redundancy, use a common code path for | |
72486b78 | 1192 | // the meta/start sequence? |
ce0b6a40 | 1193 | terminate_srd_session(); |
72486b78 SA |
1194 | |
1195 | // Metadata is cleared also, so re-set it | |
ba5b52e1 | 1196 | uint64_t samplerate = 0; |
7dd518b8 | 1197 | if (segments_.size() > 0) |
ba5b52e1 GS |
1198 | samplerate = segments_.at(current_segment_id_).samplerate; |
1199 | if (samplerate) | |
7dd518b8 | 1200 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, |
ba5b52e1 | 1201 | g_variant_new_uint64(samplerate)); |
f4ab4b5c | 1202 | for (const shared_ptr<decode::Decoder>& dec : stack_) |
72486b78 | 1203 | dec->apply_all_options(); |
ce0b6a40 | 1204 | srd_session_start(srd_session_); |
72486b78 | 1205 | |
ce0b6a40 GS |
1206 | return; |
1207 | } | |
e91883bb | 1208 | |
a3ebd556 SA |
1209 | // Create the session |
1210 | srd_session_new(&srd_session_); | |
1211 | assert(srd_session_); | |
47747218 | 1212 | |
a3ebd556 SA |
1213 | // Create the decoders |
1214 | srd_decoder_inst *prev_di = nullptr; | |
f4ab4b5c | 1215 | for (const shared_ptr<decode::Decoder>& dec : stack_) { |
a3ebd556 | 1216 | srd_decoder_inst *const di = dec->create_decoder_inst(srd_session_); |
e91883bb | 1217 | |
a3ebd556 | 1218 | if (!di) { |
403c3e87 | 1219 | set_error_message(tr("Failed to create decoder instance")); |
a3ebd556 | 1220 | srd_session_destroy(srd_session_); |
c5f9553c | 1221 | srd_session_ = nullptr; |
a3ebd556 SA |
1222 | return; |
1223 | } | |
e91883bb | 1224 | |
a3ebd556 SA |
1225 | if (prev_di) |
1226 | srd_inst_stack(srd_session_, prev_di, di); | |
e91883bb | 1227 | |
a3ebd556 | 1228 | prev_di = di; |
e91883bb | 1229 | } |
a3ebd556 SA |
1230 | |
1231 | // Start the session | |
7dd518b8 SA |
1232 | if (segments_.size() > 0) |
1233 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, | |
1234 | g_variant_new_uint64(segments_.at(current_segment_id_).samplerate)); | |
a3ebd556 SA |
1235 | |
1236 | srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_ANN, | |
1237 | DecodeSignal::annotation_callback, this); | |
1238 | ||
c0c3e62a SA |
1239 | srd_pd_output_callback_add(srd_session_, SRD_OUTPUT_BINARY, |
1240 | DecodeSignal::binary_callback, this); | |
1241 | ||
a3ebd556 | 1242 | srd_session_start(srd_session_); |
e3202557 SA |
1243 | |
1244 | // We just recreated the srd session, so all stack changes are applied now | |
1245 | stack_config_changed_ = false; | |
e91883bb | 1246 | } |
47747218 | 1247 | |
ce0b6a40 GS |
1248 | void DecodeSignal::terminate_srd_session() |
1249 | { | |
1250 | // Call the "terminate and reset" routine for the decoder stack | |
1251 | // (if available). This does not harm those stacks which already | |
1252 | // have completed their operation, and reduces response time for | |
1253 | // those stacks which still are processing data while the | |
1254 | // application no longer wants them to. | |
72486b78 | 1255 | if (srd_session_) { |
ce0b6a40 | 1256 | srd_session_terminate_reset(srd_session_); |
72486b78 SA |
1257 | |
1258 | // Metadata is cleared also, so re-set it | |
ba5b52e1 | 1259 | uint64_t samplerate = 0; |
7dd518b8 | 1260 | if (segments_.size() > 0) |
ba5b52e1 GS |
1261 | samplerate = segments_.at(current_segment_id_).samplerate; |
1262 | if (samplerate) | |
7dd518b8 | 1263 | srd_session_metadata_set(srd_session_, SRD_CONF_SAMPLERATE, |
ba5b52e1 | 1264 | g_variant_new_uint64(samplerate)); |
f4ab4b5c | 1265 | for (const shared_ptr<decode::Decoder>& dec : stack_) |
72486b78 SA |
1266 | dec->apply_all_options(); |
1267 | } | |
ce0b6a40 GS |
1268 | } |
1269 | ||
e91883bb SA |
1270 | void DecodeSignal::stop_srd_session() |
1271 | { | |
1272 | if (srd_session_) { | |
1273 | // Destroy the session | |
1274 | srd_session_destroy(srd_session_); | |
1275 | srd_session_ = nullptr; | |
8ce0e732 SA |
1276 | |
1277 | // Mark the decoder instances as non-existant since they were deleted | |
f4ab4b5c | 1278 | for (const shared_ptr<decode::Decoder>& dec : stack_) |
8ce0e732 | 1279 | dec->invalidate_decoder_inst(); |
e91883bb | 1280 | } |
47747218 SA |
1281 | } |
1282 | ||
a8a9222d SA |
1283 | void DecodeSignal::connect_input_notifiers() |
1284 | { | |
1285 | // Disconnect the notification slot from the previous set of signals | |
1b56c646 | 1286 | disconnect(this, SLOT(on_data_cleared())); |
a8a9222d SA |
1287 | disconnect(this, SLOT(on_data_received())); |
1288 | ||
1289 | // Connect the currently used signals to our slot | |
a82325d1 | 1290 | for (decode::DecodeChannel& ch : channels_) { |
a8a9222d SA |
1291 | if (!ch.assigned_signal) |
1292 | continue; | |
1293 | ||
1b56c646 SA |
1294 | const data::SignalBase *signal = ch.assigned_signal; |
1295 | connect(signal, SIGNAL(samples_cleared()), | |
1296 | this, SLOT(on_data_cleared())); | |
b10b58f4 | 1297 | connect(signal, SIGNAL(samples_added(uint64_t, uint64_t, uint64_t)), |
a8a9222d SA |
1298 | this, SLOT(on_data_received())); |
1299 | } | |
1300 | } | |
1301 | ||
65efd025 | 1302 | void DecodeSignal::create_decode_segment() |
4913560f | 1303 | { |
65efd025 SA |
1304 | // Create annotation segment |
1305 | segments_.emplace_back(DecodeSegment()); | |
72435789 | 1306 | |
65efd025 | 1307 | // Add annotation classes |
f4ab4b5c | 1308 | for (const shared_ptr<decode::Decoder>& dec : stack_) { |
65efd025 SA |
1309 | assert(dec); |
1310 | const srd_decoder *const decc = dec->decoder(); | |
1311 | assert(dec->decoder()); | |
4913560f | 1312 | |
f9807084 | 1313 | int row_index = 0; |
65efd025 SA |
1314 | // Add a row for the decoder if it doesn't have a row list |
1315 | if (!decc->annotation_rows) | |
f9807084 | 1316 | (segments_.back().annotation_rows)[Row(row_index++, decc)] = |
65efd025 | 1317 | decode::RowData(); |
4913560f | 1318 | |
65efd025 SA |
1319 | // Add the decoder rows |
1320 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { | |
1321 | const srd_decoder_annotation_row *const ann_row = | |
1322 | (srd_decoder_annotation_row *)l->data; | |
1323 | assert(ann_row); | |
4913560f | 1324 | |
f9807084 | 1325 | const Row row(row_index++, decc, ann_row); |
4913560f | 1326 | |
65efd025 SA |
1327 | // Add a new empty row data object |
1328 | (segments_.back().annotation_rows)[row] = | |
1329 | decode::RowData(); | |
4913560f SA |
1330 | } |
1331 | } | |
e77de61f SA |
1332 | |
1333 | // Prepare our binary output classes | |
1334 | for (const shared_ptr<decode::Decoder>& dec : stack_) { | |
1335 | uint8_t n = dec->get_binary_class_count(); | |
1336 | ||
1337 | for (uint8_t i = 0; i < n; i++) | |
1338 | segments_.back().binary_classes.push_back( | |
1339 | {dec.get(), dec->get_binary_class(i), vector<DecodeBinaryDataChunk>()}); | |
1340 | } | |
4913560f SA |
1341 | } |
1342 | ||
47747218 SA |
1343 | void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal) |
1344 | { | |
1345 | assert(pdata); | |
b21501d6 | 1346 | assert(decode_signal); |
47747218 SA |
1347 | |
1348 | DecodeSignal *const ds = (DecodeSignal*)decode_signal; | |
1349 | assert(ds); | |
1350 | ||
3b5282a2 SA |
1351 | if (ds->decode_interrupt_) |
1352 | return; | |
1353 | ||
47747218 SA |
1354 | lock_guard<mutex> lock(ds->output_mutex_); |
1355 | ||
c0c3e62a | 1356 | // Get the decoder and the annotation data |
47747218 SA |
1357 | assert(pdata->pdo); |
1358 | assert(pdata->pdo->di); | |
1359 | const srd_decoder *const decc = pdata->pdo->di->decoder; | |
1360 | assert(decc); | |
1361 | ||
c0c3e62a | 1362 | const srd_proto_data_annotation *const pda = (const srd_proto_data_annotation*)pdata->data; |
1becee23 UH |
1363 | assert(pda); |
1364 | ||
c0c3e62a | 1365 | // Find the row |
8a603e13 | 1366 | auto row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.end(); |
47747218 | 1367 | |
c0c3e62a | 1368 | // Try finding a better row match than the default by looking up the sub-row of this class |
1becee23 UH |
1369 | const auto format = pda->ann_class; |
1370 | const auto r = ds->class_rows_.find(make_pair(decc, format)); | |
47747218 | 1371 | if (r != ds->class_rows_.end()) |
8a603e13 | 1372 | row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.find((*r).second); |
47747218 SA |
1373 | else { |
1374 | // Failing that, use the decoder as a key | |
f9807084 | 1375 | row_iter = ds->segments_.at(ds->current_segment_id_).annotation_rows.find(Row(0, decc)); |
47747218 SA |
1376 | } |
1377 | ||
8a603e13 | 1378 | if (row_iter == ds->segments_.at(ds->current_segment_id_).annotation_rows.end()) { |
47747218 | 1379 | qDebug() << "Unexpected annotation: decoder = " << decc << |
1becee23 | 1380 | ", format = " << format; |
47747218 SA |
1381 | assert(false); |
1382 | return; | |
1383 | } | |
1384 | ||
1385 | // Add the annotation | |
5a914348 | 1386 | (*row_iter).second.emplace_annotation(pdata, &((*row_iter).first)); |
47747218 SA |
1387 | } |
1388 | ||
c0c3e62a SA |
1389 | void DecodeSignal::binary_callback(srd_proto_data *pdata, void *decode_signal) |
1390 | { | |
1391 | assert(pdata); | |
1392 | assert(decode_signal); | |
1393 | ||
1394 | DecodeSignal *const ds = (DecodeSignal*)decode_signal; | |
1395 | assert(ds); | |
1396 | ||
1397 | if (ds->decode_interrupt_) | |
1398 | return; | |
1399 | ||
e77de61f SA |
1400 | // Get the decoder and the binary data |
1401 | assert(pdata->pdo); | |
1402 | assert(pdata->pdo->di); | |
1403 | const srd_decoder *const decc = pdata->pdo->di->decoder; | |
1404 | assert(decc); | |
1405 | ||
c0c3e62a SA |
1406 | const srd_proto_data_binary *const pdb = (const srd_proto_data_binary*)pdata->data; |
1407 | assert(pdb); | |
1408 | ||
e77de61f | 1409 | // Find the matching DecodeBinaryClass |
c0c3e62a SA |
1410 | DecodeSegment* segment = &(ds->segments_.at(ds->current_segment_id_)); |
1411 | ||
e77de61f SA |
1412 | DecodeBinaryClass* bin_class = nullptr; |
1413 | for (DecodeBinaryClass& bc : segment->binary_classes) | |
1414 | if ((bc.decoder->decoder() == decc) && (bc.info->bin_class_id == pdb->bin_class)) | |
1415 | bin_class = &bc; | |
c0c3e62a | 1416 | |
e77de61f SA |
1417 | if (!bin_class) { |
1418 | qWarning() << "Could not find valid DecodeBinaryClass in segment" << | |
1419 | ds->current_segment_id_ << "for binary class ID" << pdb->bin_class << | |
1420 | ", segment only knows" << segment->binary_classes.size() << "classes"; | |
1421 | return; | |
1422 | } | |
c0c3e62a | 1423 | |
e77de61f SA |
1424 | // Add the data chunk |
1425 | bin_class->chunks.emplace_back(); | |
1426 | DecodeBinaryDataChunk* chunk = &(bin_class->chunks.back()); | |
1427 | ||
1428 | chunk->sample = pdata->start_sample; | |
1429 | chunk->data.resize(pdb->size); | |
1430 | memcpy(chunk->data.data(), pdb->data, pdb->size); | |
1431 | ||
b2b18d3a SA |
1432 | // Find decoder class instance |
1433 | Decoder* dec = nullptr; | |
1434 | for (const shared_ptr<decode::Decoder>& d : ds->decoder_stack()) | |
1435 | if (d->decoder() == decc) { | |
1436 | dec = d.get(); | |
1437 | break; | |
1438 | } | |
1439 | ||
1440 | ds->new_binary_data(ds->current_segment_id_, (void*)dec, pdb->bin_class); | |
c0c3e62a SA |
1441 | } |
1442 | ||
47747218 SA |
1443 | void DecodeSignal::on_capture_state_changed(int state) |
1444 | { | |
1445 | // If a new acquisition was started, we need to start decoding from scratch | |
ba5f2186 SA |
1446 | if (state == Session::Running) { |
1447 | logic_mux_data_invalid_ = true; | |
47747218 | 1448 | begin_decode(); |
ba5f2186 | 1449 | } |
47747218 SA |
1450 | } |
1451 | ||
1b56c646 SA |
1452 | void DecodeSignal::on_data_cleared() |
1453 | { | |
1454 | reset_decode(); | |
1455 | } | |
1456 | ||
47747218 SA |
1457 | void DecodeSignal::on_data_received() |
1458 | { | |
5620539c SA |
1459 | // If we detected a lack of input data when trying to start decoding, |
1460 | // we have set an error message. Only try again if we now have data | |
1461 | // to work with | |
1462 | if ((!error_message_.isEmpty()) && (get_input_segment_count() == 0)) | |
1463 | return; | |
1464 | ||
1b56c646 SA |
1465 | if (!logic_mux_thread_.joinable()) |
1466 | begin_decode(); | |
1467 | else | |
1468 | logic_mux_cond_.notify_one(); | |
47747218 SA |
1469 | } |
1470 | ||
ad908057 SA |
1471 | } // namespace data |
1472 | } // namespace pv |