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