]>
Commit | Line | Data |
---|---|---|
119aff65 JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
e332f6d3 JH |
21 | #include <libsigrokdecode/libsigrokdecode.h> |
22 | ||
e92cd4e4 JH |
23 | #include <stdexcept> |
24 | ||
b213ef09 JH |
25 | #include <QDebug> |
26 | ||
6e89374a | 27 | #include "decoderstack.h" |
119aff65 | 28 | |
e0fc5810 JH |
29 | #include <pv/data/logic.h> |
30 | #include <pv/data/logicsnapshot.h> | |
7491a29f | 31 | #include <pv/data/decode/decoder.h> |
06e810f2 | 32 | #include <pv/data/decode/annotation.h> |
82f50b10 | 33 | #include <pv/sigsession.h> |
e0fc5810 | 34 | #include <pv/view/logicsignal.h> |
708c5523 | 35 | |
3b68d03d JH |
36 | using std::lock_guard; |
37 | using std::mutex; | |
f70d8673 | 38 | using boost::optional; |
3b68d03d | 39 | using std::unique_lock; |
819f4c25 | 40 | using std::deque; |
f9101a91 JH |
41 | using std::make_pair; |
42 | using std::max; | |
819f4c25 JH |
43 | using std::min; |
44 | using std::list; | |
f9101a91 JH |
45 | using std::map; |
46 | using std::pair; | |
f9abf97e | 47 | using std::shared_ptr; |
819f4c25 | 48 | using std::vector; |
e332f6d3 | 49 | |
f9101a91 JH |
50 | using namespace pv::data::decode; |
51 | ||
119aff65 JH |
52 | namespace pv { |
53 | namespace data { | |
54 | ||
6e89374a JH |
55 | const double DecoderStack::DecodeMargin = 1.0; |
56 | const double DecoderStack::DecodeThreshold = 0.2; | |
57 | const int64_t DecoderStack::DecodeChunkLength = 4096; | |
4c581c79 | 58 | const unsigned int DecoderStack::DecodeNotifyPeriod = 65536; |
e0fc5810 | 59 | |
6e89374a | 60 | mutex DecoderStack::_global_decode_mutex; |
fe89c961 | 61 | |
bdc5c3b0 JH |
62 | DecoderStack::DecoderStack(pv::SigSession &session, |
63 | const srd_decoder *const dec) : | |
64 | _session(session), | |
f70d8673 JH |
65 | _sample_count(0), |
66 | _frame_complete(false), | |
5dfeb70f | 67 | _samples_decoded(0) |
119aff65 | 68 | { |
f70d8673 JH |
69 | connect(&_session, SIGNAL(frame_began()), |
70 | this, SLOT(on_new_frame())); | |
71 | connect(&_session, SIGNAL(data_received()), | |
72 | this, SLOT(on_data_received())); | |
73 | connect(&_session, SIGNAL(frame_ended()), | |
74 | this, SLOT(on_frame_ended())); | |
82f50b10 | 75 | |
7491a29f JH |
76 | _stack.push_back(shared_ptr<decode::Decoder>( |
77 | new decode::Decoder(dec))); | |
119aff65 JH |
78 | } |
79 | ||
6e89374a | 80 | DecoderStack::~DecoderStack() |
640d69ce | 81 | { |
6d483b8b | 82 | if (_decode_thread.joinable()) { |
3b68d03d | 83 | _interrupt = true; |
6d483b8b MC |
84 | _decode_thread.join(); |
85 | } | |
4e5a4405 JH |
86 | } |
87 | ||
f9abf97e | 88 | const std::list< std::shared_ptr<decode::Decoder> >& |
7491a29f | 89 | DecoderStack::stack() const |
4e5a4405 | 90 | { |
7491a29f | 91 | return _stack; |
4e5a4405 JH |
92 | } |
93 | ||
f9abf97e | 94 | void DecoderStack::push(std::shared_ptr<decode::Decoder> decoder) |
4e5a4405 | 95 | { |
7491a29f JH |
96 | assert(decoder); |
97 | _stack.push_back(decoder); | |
4e5a4405 JH |
98 | } |
99 | ||
613d097c JH |
100 | void DecoderStack::remove(int index) |
101 | { | |
613d097c JH |
102 | assert(index >= 0); |
103 | assert(index < (int)_stack.size()); | |
104 | ||
105 | // Find the decoder in the stack | |
f46e495e | 106 | auto iter = _stack.begin(); |
613d097c JH |
107 | for(int i = 0; i < index; i++, iter++) |
108 | assert(iter != _stack.end()); | |
109 | ||
110 | // Delete the element | |
111 | _stack.erase(iter); | |
112 | } | |
113 | ||
5dfeb70f JH |
114 | int64_t DecoderStack::samples_decoded() const |
115 | { | |
28b9cc08 | 116 | lock_guard<mutex> decode_lock(_output_mutex); |
5dfeb70f JH |
117 | return _samples_decoded; |
118 | } | |
119 | ||
dd048a7e | 120 | std::vector<Row> DecoderStack::get_visible_rows() const |
f9101a91 | 121 | { |
28b9cc08 | 122 | lock_guard<mutex> lock(_output_mutex); |
f9101a91 JH |
123 | |
124 | vector<Row> rows; | |
125 | ||
d9aecf1f | 126 | for (const shared_ptr<decode::Decoder> &dec : _stack) |
f9101a91 JH |
127 | { |
128 | assert(dec); | |
dd048a7e JH |
129 | if (!dec->shown()) |
130 | continue; | |
131 | ||
f9101a91 JH |
132 | const srd_decoder *const decc = dec->decoder(); |
133 | assert(dec->decoder()); | |
134 | ||
135 | // Add a row for the decoder if it doesn't have a row list | |
136 | if (!decc->annotation_rows) | |
137 | rows.push_back(Row(decc)); | |
138 | ||
139 | // Add the decoder rows | |
140 | for (const GSList *l = decc->annotation_rows; l; l = l->next) | |
141 | { | |
142 | const srd_decoder_annotation_row *const ann_row = | |
143 | (srd_decoder_annotation_row *)l->data; | |
144 | assert(ann_row); | |
145 | rows.push_back(Row(decc, ann_row)); | |
146 | } | |
147 | } | |
148 | ||
149 | return rows; | |
150 | } | |
151 | ||
92421299 JH |
152 | void DecoderStack::get_annotation_subset( |
153 | std::vector<pv::data::decode::Annotation> &dest, | |
f9101a91 JH |
154 | const Row &row, uint64_t start_sample, |
155 | uint64_t end_sample) const | |
e0fc5810 | 156 | { |
28b9cc08 | 157 | lock_guard<mutex> lock(_output_mutex); |
92421299 | 158 | |
f46e495e | 159 | const auto iter = _rows.find(row); |
f9101a91 JH |
160 | if (iter != _rows.end()) |
161 | (*iter).second.get_annotation_subset(dest, | |
162 | start_sample, end_sample); | |
e0fc5810 JH |
163 | } |
164 | ||
6e89374a | 165 | QString DecoderStack::error_message() |
b6b267bb | 166 | { |
28b9cc08 | 167 | lock_guard<mutex> lock(_output_mutex); |
b6b267bb JH |
168 | return _error_message; |
169 | } | |
170 | ||
f9101a91 JH |
171 | void DecoderStack::clear() |
172 | { | |
f70d8673 JH |
173 | _sample_count = 0; |
174 | _frame_complete = false; | |
f9101a91 JH |
175 | _samples_decoded = 0; |
176 | _error_message = QString(); | |
177 | _rows.clear(); | |
178 | _class_rows.clear(); | |
179 | } | |
180 | ||
6e89374a | 181 | void DecoderStack::begin_decode() |
640d69ce | 182 | { |
7491a29f JH |
183 | shared_ptr<pv::view::LogicSignal> logic_signal; |
184 | shared_ptr<pv::data::Logic> data; | |
185 | ||
6d483b8b | 186 | if (_decode_thread.joinable()) { |
3b68d03d | 187 | _interrupt = true; |
6d483b8b MC |
188 | _decode_thread.join(); |
189 | } | |
640d69ce | 190 | |
92421299 | 191 | clear(); |
4e5a4405 | 192 | |
8bd26d8b | 193 | // Check that all decoders have the required channels |
d9aecf1f | 194 | for (const shared_ptr<decode::Decoder> &dec : _stack) |
df4c1a06 | 195 | if (!dec->have_required_probes()) { |
8bd26d8b | 196 | _error_message = tr("One or more required channels " |
df4c1a06 JH |
197 | "have not been specified"); |
198 | return; | |
199 | } | |
200 | ||
f9101a91 | 201 | // Add classes |
d9aecf1f | 202 | for (const shared_ptr<decode::Decoder> &dec : _stack) |
f9101a91 JH |
203 | { |
204 | assert(dec); | |
205 | const srd_decoder *const decc = dec->decoder(); | |
206 | assert(dec->decoder()); | |
207 | ||
208 | // Add a row for the decoder if it doesn't have a row list | |
209 | if (!decc->annotation_rows) | |
210 | _rows[Row(decc)] = decode::RowData(); | |
211 | ||
212 | // Add the decoder rows | |
213 | for (const GSList *l = decc->annotation_rows; l; l = l->next) | |
214 | { | |
215 | const srd_decoder_annotation_row *const ann_row = | |
216 | (srd_decoder_annotation_row *)l->data; | |
217 | assert(ann_row); | |
218 | ||
219 | const Row row(decc, ann_row); | |
220 | ||
221 | // Add a new empty row data object | |
222 | _rows[row] = decode::RowData(); | |
223 | ||
224 | // Map out all the classes | |
225 | for (const GSList *ll = ann_row->ann_classes; | |
226 | ll; ll = ll->next) | |
227 | _class_rows[make_pair(decc, | |
228 | GPOINTER_TO_INT(ll->data))] = row; | |
229 | } | |
230 | } | |
231 | ||
8bd26d8b | 232 | // We get the logic data of the first channel in the list. |
e0fc5810 JH |
233 | // This works because we are currently assuming all |
234 | // LogicSignals have the same data/snapshot | |
d9aecf1f | 235 | for (const shared_ptr<decode::Decoder> &dec : _stack) |
8bd26d8b UH |
236 | if (dec && !dec->channels().empty() && |
237 | ((logic_signal = (*dec->channels().begin()).second)) && | |
7aa09b00 | 238 | ((data = logic_signal->logic_data()))) |
7491a29f JH |
239 | break; |
240 | ||
241 | if (!data) | |
242 | return; | |
243 | ||
f70d8673 JH |
244 | // Check we have a snapshot of data |
245 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = | |
246 | data->get_snapshots(); | |
247 | if (snapshots.empty()) | |
248 | return; | |
249 | _snapshot = snapshots.front(); | |
250 | ||
7491a29f JH |
251 | // Get the samplerate and start time |
252 | _start_time = data->get_start_time(); | |
9d28da5a | 253 | _samplerate = data->samplerate(); |
7491a29f JH |
254 | if (_samplerate == 0.0) |
255 | _samplerate = 1.0; | |
e0fc5810 | 256 | |
3b68d03d JH |
257 | _interrupt = false; |
258 | _decode_thread = std::thread(&DecoderStack::decode_proc, this); | |
640d69ce JH |
259 | } |
260 | ||
a007f5ad JH |
261 | uint64_t DecoderStack::get_max_sample_count() const |
262 | { | |
f9101a91 JH |
263 | uint64_t max_sample_count = 0; |
264 | ||
f46e495e | 265 | for (auto i = _rows.cbegin(); i != _rows.end(); i++) |
f9101a91 JH |
266 | max_sample_count = max(max_sample_count, |
267 | (*i).second.get_max_sample()); | |
268 | ||
269 | return max_sample_count; | |
a007f5ad JH |
270 | } |
271 | ||
f70d8673 JH |
272 | optional<int64_t> DecoderStack::wait_for_data() const |
273 | { | |
274 | unique_lock<mutex> input_lock(_input_mutex); | |
3b68d03d JH |
275 | while(!_interrupt && !_frame_complete && |
276 | _samples_decoded >= _sample_count) | |
f70d8673 | 277 | _input_cond.wait(input_lock); |
3b68d03d | 278 | return boost::make_optional(!_interrupt && |
f70d8673 JH |
279 | (_samples_decoded < _sample_count || !_frame_complete), |
280 | _sample_count); | |
281 | } | |
282 | ||
f67d9e9b | 283 | void DecoderStack::decode_data( |
f70d8673 | 284 | const int64_t sample_count, const unsigned int unit_size, |
f67d9e9b JH |
285 | srd_session *const session) |
286 | { | |
287 | uint8_t chunk[DecodeChunkLength]; | |
288 | ||
f67d9e9b | 289 | const unsigned int chunk_sample_count = |
f70d8673 | 290 | DecodeChunkLength / _snapshot->unit_size(); |
f67d9e9b | 291 | |
3b68d03d | 292 | for (int64_t i = 0; !_interrupt && i < sample_count; |
f67d9e9b JH |
293 | i += chunk_sample_count) |
294 | { | |
295 | lock_guard<mutex> decode_lock(_global_decode_mutex); | |
296 | ||
297 | const int64_t chunk_end = min( | |
298 | i + chunk_sample_count, sample_count); | |
f70d8673 | 299 | _snapshot->get_samples(chunk, i, chunk_end); |
f67d9e9b JH |
300 | |
301 | if (srd_session_send(session, i, i + sample_count, chunk, | |
302 | (chunk_end - i) * unit_size) != SRD_OK) { | |
303 | _error_message = tr("Decoder reported an error"); | |
304 | break; | |
305 | } | |
306 | ||
307 | { | |
28b9cc08 | 308 | lock_guard<mutex> lock(_output_mutex); |
f67d9e9b JH |
309 | _samples_decoded = chunk_end; |
310 | } | |
4c581c79 JH |
311 | |
312 | if (i % DecodeNotifyPeriod == 0) | |
313 | new_decode_data(); | |
f67d9e9b JH |
314 | } |
315 | ||
1c5fb759 | 316 | new_decode_data(); |
f67d9e9b JH |
317 | } |
318 | ||
c1b2865e | 319 | void DecoderStack::decode_proc() |
119aff65 | 320 | { |
f70d8673 | 321 | optional<int64_t> sample_count; |
b6b267bb | 322 | srd_session *session; |
7491a29f | 323 | srd_decoder_inst *prev_di = NULL; |
e0fc5810 | 324 | |
f70d8673 | 325 | assert(_snapshot); |
e0fc5810 | 326 | |
b6b267bb JH |
327 | // Create the session |
328 | srd_session_new(&session); | |
329 | assert(session); | |
e0fc5810 | 330 | |
7491a29f | 331 | // Create the decoders |
f70d8673 | 332 | const unsigned int unit_size = _snapshot->unit_size(); |
f67d9e9b | 333 | |
d9aecf1f | 334 | for (const shared_ptr<decode::Decoder> &dec : _stack) |
7491a29f | 335 | { |
13a3528c | 336 | srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size); |
e0fc5810 | 337 | |
7491a29f JH |
338 | if (!di) |
339 | { | |
8a290daf | 340 | _error_message = tr("Failed to create decoder instance"); |
7491a29f JH |
341 | srd_session_destroy(session); |
342 | return; | |
343 | } | |
b6b267bb | 344 | |
7491a29f JH |
345 | if (prev_di) |
346 | srd_inst_stack (session, prev_di, di); | |
b6b267bb | 347 | |
7491a29f | 348 | prev_di = di; |
b6b267bb JH |
349 | } |
350 | ||
f70d8673 JH |
351 | // Get the intial sample count |
352 | { | |
353 | unique_lock<mutex> input_lock(_input_mutex); | |
354 | sample_count = _sample_count = _snapshot->get_sample_count(); | |
355 | } | |
356 | ||
b6b267bb | 357 | // Start the session |
7491a29f JH |
358 | srd_session_metadata_set(session, SRD_CONF_SAMPLERATE, |
359 | g_variant_new_uint64((uint64_t)_samplerate)); | |
360 | ||
361 | srd_pd_output_callback_add(session, SRD_OUTPUT_ANN, | |
362 | DecoderStack::annotation_callback, this); | |
363 | ||
b6b267bb JH |
364 | srd_session_start(session); |
365 | ||
f70d8673 JH |
366 | do { |
367 | decode_data(*sample_count, unit_size, session); | |
368 | } while(_error_message.isEmpty() && (sample_count = wait_for_data())); | |
b6b267bb JH |
369 | |
370 | // Destroy the session | |
371 | srd_session_destroy(session); | |
e0fc5810 JH |
372 | } |
373 | ||
6e89374a | 374 | void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder) |
e0fc5810 | 375 | { |
e0fc5810 JH |
376 | assert(pdata); |
377 | assert(decoder); | |
378 | ||
6e89374a | 379 | DecoderStack *const d = (DecoderStack*)decoder; |
f9101a91 | 380 | assert(d); |
e0fc5810 | 381 | |
28b9cc08 | 382 | lock_guard<mutex> lock(d->_output_mutex); |
6d8b0562 | 383 | |
f9101a91 JH |
384 | const Annotation a(pdata); |
385 | ||
386 | // Find the row | |
387 | assert(pdata->pdo); | |
388 | assert(pdata->pdo->di); | |
389 | const srd_decoder *const decc = pdata->pdo->di->decoder; | |
390 | assert(decc); | |
391 | ||
f46e495e | 392 | auto row_iter = d->_rows.end(); |
f9101a91 JH |
393 | |
394 | // Try looking up the sub-row of this class | |
f46e495e | 395 | const auto r = d->_class_rows.find(make_pair(decc, a.format())); |
f9101a91 JH |
396 | if (r != d->_class_rows.end()) |
397 | row_iter = d->_rows.find((*r).second); | |
398 | else | |
6d8b0562 | 399 | { |
f9101a91 JH |
400 | // Failing that, use the decoder as a key |
401 | row_iter = d->_rows.find(Row(decc)); | |
6d8b0562 UH |
402 | } |
403 | ||
f9101a91 JH |
404 | assert(row_iter != d->_rows.end()); |
405 | if (row_iter == d->_rows.end()) { | |
406 | qDebug() << "Unexpected annotation: decoder = " << decc << | |
407 | ", format = " << a.format(); | |
408 | assert(0); | |
409 | return; | |
410 | } | |
9cef9567 | 411 | |
f9101a91 JH |
412 | // Add the annotation |
413 | (*row_iter).second.push_annotation(a); | |
119aff65 JH |
414 | } |
415 | ||
82f50b10 JH |
416 | void DecoderStack::on_new_frame() |
417 | { | |
418 | begin_decode(); | |
419 | } | |
420 | ||
f70d8673 JH |
421 | void DecoderStack::on_data_received() |
422 | { | |
423 | { | |
424 | unique_lock<mutex> lock(_input_mutex); | |
46bf6027 JH |
425 | if (_snapshot) |
426 | _sample_count = _snapshot->get_sample_count(); | |
f70d8673 JH |
427 | } |
428 | _input_cond.notify_one(); | |
429 | } | |
430 | ||
431 | void DecoderStack::on_frame_ended() | |
432 | { | |
433 | { | |
434 | unique_lock<mutex> lock(_input_mutex); | |
46bf6027 JH |
435 | if (_snapshot) |
436 | _frame_complete = true; | |
f70d8673 JH |
437 | } |
438 | _input_cond.notify_one(); | |
439 | } | |
440 | ||
119aff65 JH |
441 | } // namespace data |
442 | } // namespace pv |