]>
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; |
7df04abc | 84 | _input_cond.notify_one(); |
6d483b8b MC |
85 | _decode_thread.join(); |
86 | } | |
4e5a4405 JH |
87 | } |
88 | ||
f9abf97e | 89 | const std::list< std::shared_ptr<decode::Decoder> >& |
7491a29f | 90 | DecoderStack::stack() const |
4e5a4405 | 91 | { |
7491a29f | 92 | return _stack; |
4e5a4405 JH |
93 | } |
94 | ||
f9abf97e | 95 | void DecoderStack::push(std::shared_ptr<decode::Decoder> decoder) |
4e5a4405 | 96 | { |
7491a29f JH |
97 | assert(decoder); |
98 | _stack.push_back(decoder); | |
4e5a4405 JH |
99 | } |
100 | ||
613d097c JH |
101 | void DecoderStack::remove(int index) |
102 | { | |
613d097c JH |
103 | assert(index >= 0); |
104 | assert(index < (int)_stack.size()); | |
105 | ||
106 | // Find the decoder in the stack | |
f46e495e | 107 | auto iter = _stack.begin(); |
613d097c JH |
108 | for(int i = 0; i < index; i++, iter++) |
109 | assert(iter != _stack.end()); | |
110 | ||
111 | // Delete the element | |
112 | _stack.erase(iter); | |
113 | } | |
114 | ||
5dfeb70f JH |
115 | int64_t DecoderStack::samples_decoded() const |
116 | { | |
28b9cc08 | 117 | lock_guard<mutex> decode_lock(_output_mutex); |
5dfeb70f JH |
118 | return _samples_decoded; |
119 | } | |
120 | ||
dd048a7e | 121 | std::vector<Row> DecoderStack::get_visible_rows() const |
f9101a91 | 122 | { |
28b9cc08 | 123 | lock_guard<mutex> lock(_output_mutex); |
f9101a91 JH |
124 | |
125 | vector<Row> rows; | |
126 | ||
d9aecf1f | 127 | for (const shared_ptr<decode::Decoder> &dec : _stack) |
f9101a91 JH |
128 | { |
129 | assert(dec); | |
dd048a7e JH |
130 | if (!dec->shown()) |
131 | continue; | |
132 | ||
f9101a91 JH |
133 | const srd_decoder *const decc = dec->decoder(); |
134 | assert(dec->decoder()); | |
135 | ||
136 | // Add a row for the decoder if it doesn't have a row list | |
137 | if (!decc->annotation_rows) | |
138 | rows.push_back(Row(decc)); | |
139 | ||
140 | // Add the decoder rows | |
141 | for (const GSList *l = decc->annotation_rows; l; l = l->next) | |
142 | { | |
143 | const srd_decoder_annotation_row *const ann_row = | |
144 | (srd_decoder_annotation_row *)l->data; | |
145 | assert(ann_row); | |
146 | rows.push_back(Row(decc, ann_row)); | |
147 | } | |
148 | } | |
149 | ||
150 | return rows; | |
151 | } | |
152 | ||
92421299 JH |
153 | void DecoderStack::get_annotation_subset( |
154 | std::vector<pv::data::decode::Annotation> &dest, | |
f9101a91 JH |
155 | const Row &row, uint64_t start_sample, |
156 | uint64_t end_sample) const | |
e0fc5810 | 157 | { |
28b9cc08 | 158 | lock_guard<mutex> lock(_output_mutex); |
92421299 | 159 | |
f46e495e | 160 | const auto iter = _rows.find(row); |
f9101a91 JH |
161 | if (iter != _rows.end()) |
162 | (*iter).second.get_annotation_subset(dest, | |
163 | start_sample, end_sample); | |
e0fc5810 JH |
164 | } |
165 | ||
6e89374a | 166 | QString DecoderStack::error_message() |
b6b267bb | 167 | { |
28b9cc08 | 168 | lock_guard<mutex> lock(_output_mutex); |
b6b267bb JH |
169 | return _error_message; |
170 | } | |
171 | ||
f9101a91 JH |
172 | void DecoderStack::clear() |
173 | { | |
f70d8673 JH |
174 | _sample_count = 0; |
175 | _frame_complete = false; | |
f9101a91 JH |
176 | _samples_decoded = 0; |
177 | _error_message = QString(); | |
178 | _rows.clear(); | |
179 | _class_rows.clear(); | |
180 | } | |
181 | ||
6e89374a | 182 | void DecoderStack::begin_decode() |
640d69ce | 183 | { |
7491a29f JH |
184 | shared_ptr<pv::view::LogicSignal> logic_signal; |
185 | shared_ptr<pv::data::Logic> data; | |
186 | ||
6d483b8b | 187 | if (_decode_thread.joinable()) { |
3b68d03d | 188 | _interrupt = true; |
7df04abc | 189 | _input_cond.notify_one(); |
6d483b8b MC |
190 | _decode_thread.join(); |
191 | } | |
640d69ce | 192 | |
92421299 | 193 | clear(); |
4e5a4405 | 194 | |
8bd26d8b | 195 | // Check that all decoders have the required channels |
d9aecf1f | 196 | for (const shared_ptr<decode::Decoder> &dec : _stack) |
6ac6242b | 197 | if (!dec->have_required_channels()) { |
8bd26d8b | 198 | _error_message = tr("One or more required channels " |
df4c1a06 JH |
199 | "have not been specified"); |
200 | return; | |
201 | } | |
202 | ||
f9101a91 | 203 | // Add classes |
d9aecf1f | 204 | for (const shared_ptr<decode::Decoder> &dec : _stack) |
f9101a91 JH |
205 | { |
206 | assert(dec); | |
207 | const srd_decoder *const decc = dec->decoder(); | |
208 | assert(dec->decoder()); | |
209 | ||
210 | // Add a row for the decoder if it doesn't have a row list | |
211 | if (!decc->annotation_rows) | |
212 | _rows[Row(decc)] = decode::RowData(); | |
213 | ||
214 | // Add the decoder rows | |
215 | for (const GSList *l = decc->annotation_rows; l; l = l->next) | |
216 | { | |
217 | const srd_decoder_annotation_row *const ann_row = | |
218 | (srd_decoder_annotation_row *)l->data; | |
219 | assert(ann_row); | |
220 | ||
221 | const Row row(decc, ann_row); | |
222 | ||
223 | // Add a new empty row data object | |
224 | _rows[row] = decode::RowData(); | |
225 | ||
226 | // Map out all the classes | |
227 | for (const GSList *ll = ann_row->ann_classes; | |
228 | ll; ll = ll->next) | |
229 | _class_rows[make_pair(decc, | |
230 | GPOINTER_TO_INT(ll->data))] = row; | |
231 | } | |
232 | } | |
233 | ||
8bd26d8b | 234 | // We get the logic data of the first channel in the list. |
e0fc5810 JH |
235 | // This works because we are currently assuming all |
236 | // LogicSignals have the same data/snapshot | |
d9aecf1f | 237 | for (const shared_ptr<decode::Decoder> &dec : _stack) |
8bd26d8b UH |
238 | if (dec && !dec->channels().empty() && |
239 | ((logic_signal = (*dec->channels().begin()).second)) && | |
7aa09b00 | 240 | ((data = logic_signal->logic_data()))) |
7491a29f JH |
241 | break; |
242 | ||
243 | if (!data) | |
244 | return; | |
245 | ||
f70d8673 JH |
246 | // Check we have a snapshot of data |
247 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = | |
248 | data->get_snapshots(); | |
249 | if (snapshots.empty()) | |
250 | return; | |
251 | _snapshot = snapshots.front(); | |
252 | ||
7491a29f JH |
253 | // Get the samplerate and start time |
254 | _start_time = data->get_start_time(); | |
9d28da5a | 255 | _samplerate = data->samplerate(); |
7491a29f JH |
256 | if (_samplerate == 0.0) |
257 | _samplerate = 1.0; | |
e0fc5810 | 258 | |
3b68d03d JH |
259 | _interrupt = false; |
260 | _decode_thread = std::thread(&DecoderStack::decode_proc, this); | |
640d69ce JH |
261 | } |
262 | ||
a007f5ad JH |
263 | uint64_t DecoderStack::get_max_sample_count() const |
264 | { | |
f9101a91 JH |
265 | uint64_t max_sample_count = 0; |
266 | ||
f46e495e | 267 | for (auto i = _rows.cbegin(); i != _rows.end(); i++) |
f9101a91 JH |
268 | max_sample_count = max(max_sample_count, |
269 | (*i).second.get_max_sample()); | |
270 | ||
271 | return max_sample_count; | |
a007f5ad JH |
272 | } |
273 | ||
f70d8673 JH |
274 | optional<int64_t> DecoderStack::wait_for_data() const |
275 | { | |
276 | unique_lock<mutex> input_lock(_input_mutex); | |
3b68d03d JH |
277 | while(!_interrupt && !_frame_complete && |
278 | _samples_decoded >= _sample_count) | |
f70d8673 | 279 | _input_cond.wait(input_lock); |
3b68d03d | 280 | return boost::make_optional(!_interrupt && |
f70d8673 JH |
281 | (_samples_decoded < _sample_count || !_frame_complete), |
282 | _sample_count); | |
283 | } | |
284 | ||
f67d9e9b | 285 | void DecoderStack::decode_data( |
f70d8673 | 286 | const int64_t sample_count, const unsigned int unit_size, |
f67d9e9b JH |
287 | srd_session *const session) |
288 | { | |
289 | uint8_t chunk[DecodeChunkLength]; | |
290 | ||
f67d9e9b | 291 | const unsigned int chunk_sample_count = |
f70d8673 | 292 | DecodeChunkLength / _snapshot->unit_size(); |
f67d9e9b | 293 | |
3b68d03d | 294 | for (int64_t i = 0; !_interrupt && i < sample_count; |
f67d9e9b JH |
295 | i += chunk_sample_count) |
296 | { | |
297 | lock_guard<mutex> decode_lock(_global_decode_mutex); | |
298 | ||
299 | const int64_t chunk_end = min( | |
300 | i + chunk_sample_count, sample_count); | |
f70d8673 | 301 | _snapshot->get_samples(chunk, i, chunk_end); |
f67d9e9b JH |
302 | |
303 | if (srd_session_send(session, i, i + sample_count, chunk, | |
304 | (chunk_end - i) * unit_size) != SRD_OK) { | |
305 | _error_message = tr("Decoder reported an error"); | |
306 | break; | |
307 | } | |
308 | ||
309 | { | |
28b9cc08 | 310 | lock_guard<mutex> lock(_output_mutex); |
f67d9e9b JH |
311 | _samples_decoded = chunk_end; |
312 | } | |
4c581c79 JH |
313 | |
314 | if (i % DecodeNotifyPeriod == 0) | |
315 | new_decode_data(); | |
f67d9e9b JH |
316 | } |
317 | ||
1c5fb759 | 318 | new_decode_data(); |
f67d9e9b JH |
319 | } |
320 | ||
c1b2865e | 321 | void DecoderStack::decode_proc() |
119aff65 | 322 | { |
f70d8673 | 323 | optional<int64_t> sample_count; |
b6b267bb | 324 | srd_session *session; |
7491a29f | 325 | srd_decoder_inst *prev_di = NULL; |
e0fc5810 | 326 | |
f70d8673 | 327 | assert(_snapshot); |
e0fc5810 | 328 | |
b6b267bb JH |
329 | // Create the session |
330 | srd_session_new(&session); | |
331 | assert(session); | |
e0fc5810 | 332 | |
7491a29f | 333 | // Create the decoders |
f70d8673 | 334 | const unsigned int unit_size = _snapshot->unit_size(); |
f67d9e9b | 335 | |
d9aecf1f | 336 | for (const shared_ptr<decode::Decoder> &dec : _stack) |
7491a29f | 337 | { |
13a3528c | 338 | srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size); |
e0fc5810 | 339 | |
7491a29f JH |
340 | if (!di) |
341 | { | |
8a290daf | 342 | _error_message = tr("Failed to create decoder instance"); |
7491a29f JH |
343 | srd_session_destroy(session); |
344 | return; | |
345 | } | |
b6b267bb | 346 | |
7491a29f JH |
347 | if (prev_di) |
348 | srd_inst_stack (session, prev_di, di); | |
b6b267bb | 349 | |
7491a29f | 350 | prev_di = di; |
b6b267bb JH |
351 | } |
352 | ||
f70d8673 JH |
353 | // Get the intial sample count |
354 | { | |
355 | unique_lock<mutex> input_lock(_input_mutex); | |
356 | sample_count = _sample_count = _snapshot->get_sample_count(); | |
357 | } | |
358 | ||
b6b267bb | 359 | // Start the session |
7491a29f JH |
360 | srd_session_metadata_set(session, SRD_CONF_SAMPLERATE, |
361 | g_variant_new_uint64((uint64_t)_samplerate)); | |
362 | ||
363 | srd_pd_output_callback_add(session, SRD_OUTPUT_ANN, | |
364 | DecoderStack::annotation_callback, this); | |
365 | ||
b6b267bb JH |
366 | srd_session_start(session); |
367 | ||
f70d8673 JH |
368 | do { |
369 | decode_data(*sample_count, unit_size, session); | |
370 | } while(_error_message.isEmpty() && (sample_count = wait_for_data())); | |
b6b267bb JH |
371 | |
372 | // Destroy the session | |
373 | srd_session_destroy(session); | |
e0fc5810 JH |
374 | } |
375 | ||
6e89374a | 376 | void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder) |
e0fc5810 | 377 | { |
e0fc5810 JH |
378 | assert(pdata); |
379 | assert(decoder); | |
380 | ||
6e89374a | 381 | DecoderStack *const d = (DecoderStack*)decoder; |
f9101a91 | 382 | assert(d); |
e0fc5810 | 383 | |
28b9cc08 | 384 | lock_guard<mutex> lock(d->_output_mutex); |
6d8b0562 | 385 | |
f9101a91 JH |
386 | const Annotation a(pdata); |
387 | ||
388 | // Find the row | |
389 | assert(pdata->pdo); | |
390 | assert(pdata->pdo->di); | |
391 | const srd_decoder *const decc = pdata->pdo->di->decoder; | |
392 | assert(decc); | |
393 | ||
f46e495e | 394 | auto row_iter = d->_rows.end(); |
f9101a91 JH |
395 | |
396 | // Try looking up the sub-row of this class | |
f46e495e | 397 | const auto r = d->_class_rows.find(make_pair(decc, a.format())); |
f9101a91 JH |
398 | if (r != d->_class_rows.end()) |
399 | row_iter = d->_rows.find((*r).second); | |
400 | else | |
6d8b0562 | 401 | { |
f9101a91 JH |
402 | // Failing that, use the decoder as a key |
403 | row_iter = d->_rows.find(Row(decc)); | |
6d8b0562 UH |
404 | } |
405 | ||
f9101a91 JH |
406 | assert(row_iter != d->_rows.end()); |
407 | if (row_iter == d->_rows.end()) { | |
408 | qDebug() << "Unexpected annotation: decoder = " << decc << | |
409 | ", format = " << a.format(); | |
410 | assert(0); | |
411 | return; | |
412 | } | |
9cef9567 | 413 | |
f9101a91 JH |
414 | // Add the annotation |
415 | (*row_iter).second.push_annotation(a); | |
119aff65 JH |
416 | } |
417 | ||
82f50b10 JH |
418 | void DecoderStack::on_new_frame() |
419 | { | |
420 | begin_decode(); | |
421 | } | |
422 | ||
f70d8673 JH |
423 | void DecoderStack::on_data_received() |
424 | { | |
425 | { | |
426 | unique_lock<mutex> lock(_input_mutex); | |
46bf6027 JH |
427 | if (_snapshot) |
428 | _sample_count = _snapshot->get_sample_count(); | |
f70d8673 JH |
429 | } |
430 | _input_cond.notify_one(); | |
431 | } | |
432 | ||
433 | void DecoderStack::on_frame_ended() | |
434 | { | |
435 | { | |
436 | unique_lock<mutex> lock(_input_mutex); | |
46bf6027 JH |
437 | if (_snapshot) |
438 | _frame_complete = true; | |
f70d8673 JH |
439 | } |
440 | _input_cond.notify_one(); | |
441 | } | |
442 | ||
119aff65 JH |
443 | } // namespace data |
444 | } // namespace pv |