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