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