]>
Commit | Line | Data |
---|---|---|
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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <libsigrokdecode/libsigrokdecode.h> | |
21 | ||
22 | #include <stdexcept> | |
23 | ||
24 | #include <QDebug> | |
25 | ||
26 | #include "decoderstack.hpp" | |
27 | ||
28 | #include <pv/data/logic.hpp> | |
29 | #include <pv/data/logicsegment.hpp> | |
30 | #include <pv/data/decode/decoder.hpp> | |
31 | #include <pv/data/decode/annotation.hpp> | |
32 | #include <pv/session.hpp> | |
33 | #include <pv/view/logicsignal.hpp> | |
34 | ||
35 | using std::lock_guard; | |
36 | using std::mutex; | |
37 | using std::unique_lock; | |
38 | using std::deque; | |
39 | using std::make_pair; | |
40 | using std::max; | |
41 | using std::min; | |
42 | using std::list; | |
43 | using std::shared_ptr; | |
44 | using std::make_shared; | |
45 | using std::vector; | |
46 | ||
47 | using boost::optional; | |
48 | ||
49 | using namespace pv::data::decode; | |
50 | ||
51 | namespace pv { | |
52 | namespace data { | |
53 | ||
54 | const double DecoderStack::DecodeMargin = 1.0; | |
55 | const double DecoderStack::DecodeThreshold = 0.2; | |
56 | const int64_t DecoderStack::DecodeChunkLength = 10 * 1024 * 1024; | |
57 | const unsigned int DecoderStack::DecodeNotifyPeriod = 1024; | |
58 | ||
59 | mutex DecoderStack::global_srd_mutex_; | |
60 | ||
61 | DecoderStack::DecoderStack(pv::Session &session, | |
62 | const srd_decoder *const dec) : | |
63 | session_(session), | |
64 | start_time_(0), | |
65 | samplerate_(0), | |
66 | sample_count_(0), | |
67 | frame_complete_(false), | |
68 | samples_decoded_(0) | |
69 | { | |
70 | connect(&session_, SIGNAL(frame_began()), | |
71 | this, SLOT(on_new_frame())); | |
72 | connect(&session_, SIGNAL(data_received()), | |
73 | this, SLOT(on_data_received())); | |
74 | connect(&session_, SIGNAL(frame_ended()), | |
75 | this, SLOT(on_frame_ended())); | |
76 | ||
77 | stack_.push_back(make_shared<decode::Decoder>(dec)); | |
78 | } | |
79 | ||
80 | DecoderStack::~DecoderStack() | |
81 | { | |
82 | if (decode_thread_.joinable()) { | |
83 | interrupt_ = true; | |
84 | input_cond_.notify_one(); | |
85 | decode_thread_.join(); | |
86 | } | |
87 | } | |
88 | ||
89 | const list< shared_ptr<decode::Decoder> >& DecoderStack::stack() const | |
90 | { | |
91 | return stack_; | |
92 | } | |
93 | ||
94 | void DecoderStack::push(shared_ptr<decode::Decoder> decoder) | |
95 | { | |
96 | assert(decoder); | |
97 | stack_.push_back(decoder); | |
98 | } | |
99 | ||
100 | void DecoderStack::remove(int index) | |
101 | { | |
102 | assert(index >= 0); | |
103 | assert(index < (int)stack_.size()); | |
104 | ||
105 | // Find the decoder in the stack | |
106 | auto iter = stack_.begin(); | |
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 | ||
114 | double DecoderStack::samplerate() const | |
115 | { | |
116 | return samplerate_; | |
117 | } | |
118 | ||
119 | const pv::util::Timestamp& DecoderStack::start_time() const | |
120 | { | |
121 | return start_time_; | |
122 | } | |
123 | ||
124 | int64_t DecoderStack::samples_decoded() const | |
125 | { | |
126 | lock_guard<mutex> decode_lock(output_mutex_); | |
127 | return samples_decoded_; | |
128 | } | |
129 | ||
130 | vector<Row> DecoderStack::get_visible_rows() const | |
131 | { | |
132 | lock_guard<mutex> lock(output_mutex_); | |
133 | ||
134 | vector<Row> rows; | |
135 | ||
136 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
137 | assert(dec); | |
138 | if (!dec->shown()) | |
139 | continue; | |
140 | ||
141 | const srd_decoder *const decc = dec->decoder(); | |
142 | assert(dec->decoder()); | |
143 | ||
144 | // Add a row for the decoder if it doesn't have a row list | |
145 | if (!decc->annotation_rows) | |
146 | rows.emplace_back(decc); | |
147 | ||
148 | // Add the decoder rows | |
149 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { | |
150 | const srd_decoder_annotation_row *const ann_row = | |
151 | (srd_decoder_annotation_row *)l->data; | |
152 | assert(ann_row); | |
153 | rows.emplace_back(decc, ann_row); | |
154 | } | |
155 | } | |
156 | ||
157 | return rows; | |
158 | } | |
159 | ||
160 | void DecoderStack::get_annotation_subset( | |
161 | vector<pv::data::decode::Annotation> &dest, | |
162 | const Row &row, uint64_t start_sample, | |
163 | uint64_t end_sample) const | |
164 | { | |
165 | lock_guard<mutex> lock(output_mutex_); | |
166 | ||
167 | const auto iter = rows_.find(row); | |
168 | if (iter != rows_.end()) | |
169 | (*iter).second.get_annotation_subset(dest, | |
170 | start_sample, end_sample); | |
171 | } | |
172 | ||
173 | QString DecoderStack::error_message() | |
174 | { | |
175 | lock_guard<mutex> lock(output_mutex_); | |
176 | return error_message_; | |
177 | } | |
178 | ||
179 | void DecoderStack::clear() | |
180 | { | |
181 | sample_count_ = 0; | |
182 | frame_complete_ = false; | |
183 | samples_decoded_ = 0; | |
184 | error_message_ = QString(); | |
185 | rows_.clear(); | |
186 | class_rows_.clear(); | |
187 | } | |
188 | ||
189 | void DecoderStack::begin_decode() | |
190 | { | |
191 | if (decode_thread_.joinable()) { | |
192 | interrupt_ = true; | |
193 | input_cond_.notify_one(); | |
194 | decode_thread_.join(); | |
195 | } | |
196 | ||
197 | clear(); | |
198 | ||
199 | // Check that all decoders have the required channels | |
200 | for (const shared_ptr<decode::Decoder> &dec : stack_) | |
201 | if (!dec->have_required_channels()) { | |
202 | error_message_ = tr("One or more required channels " | |
203 | "have not been specified"); | |
204 | return; | |
205 | } | |
206 | ||
207 | // Add classes | |
208 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
209 | assert(dec); | |
210 | const srd_decoder *const decc = dec->decoder(); | |
211 | assert(dec->decoder()); | |
212 | ||
213 | // Add a row for the decoder if it doesn't have a row list | |
214 | if (!decc->annotation_rows) | |
215 | rows_[Row(decc)] = decode::RowData(); | |
216 | ||
217 | // Add the decoder rows | |
218 | for (const GSList *l = decc->annotation_rows; l; l = l->next) { | |
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 | ||
236 | // We get the logic data of the first channel in the list. | |
237 | // This works because we are currently assuming all | |
238 | // logic signals have the same data/segment | |
239 | pv::data::SignalBase *signalbase; | |
240 | pv::data::Logic *data = nullptr; | |
241 | ||
242 | for (const shared_ptr<decode::Decoder> &dec : stack_) | |
243 | if (dec && !dec->channels().empty() && | |
244 | ((signalbase = (*dec->channels().begin()).second.get())) && | |
245 | ((data = signalbase->logic_data().get()))) | |
246 | break; | |
247 | ||
248 | if (!data) | |
249 | return; | |
250 | ||
251 | // Check we have a segment of data | |
252 | const deque< shared_ptr<pv::data::LogicSegment> > &segments = | |
253 | data->logic_segments(); | |
254 | if (segments.empty()) | |
255 | return; | |
256 | segment_ = segments.front(); | |
257 | ||
258 | // Get the samplerate and start time | |
259 | start_time_ = segment_->start_time(); | |
260 | samplerate_ = segment_->samplerate(); | |
261 | if (samplerate_ == 0.0) | |
262 | samplerate_ = 1.0; | |
263 | ||
264 | interrupt_ = false; | |
265 | decode_thread_ = std::thread(&DecoderStack::decode_proc, this); | |
266 | } | |
267 | ||
268 | uint64_t DecoderStack::max_sample_count() const | |
269 | { | |
270 | uint64_t max_sample_count = 0; | |
271 | ||
272 | for (const auto& row : rows_) | |
273 | max_sample_count = max(max_sample_count, | |
274 | row.second.get_max_sample()); | |
275 | ||
276 | return max_sample_count; | |
277 | } | |
278 | ||
279 | optional<int64_t> DecoderStack::wait_for_data() const | |
280 | { | |
281 | unique_lock<mutex> input_lock(input_mutex_); | |
282 | ||
283 | // Do wait if we decoded all samples but we're still capturing | |
284 | // Do not wait if we're done capturing | |
285 | while (!interrupt_ && !frame_complete_ && | |
286 | (samples_decoded_ >= sample_count_) && | |
287 | (session_.get_capture_state() != Session::Stopped)) { | |
288 | ||
289 | input_cond_.wait(input_lock); | |
290 | } | |
291 | ||
292 | // Return value is valid if we're not aborting the decode, | |
293 | return boost::make_optional(!interrupt_ && | |
294 | // and there's more work to do... | |
295 | (samples_decoded_ < sample_count_ || !frame_complete_) && | |
296 | // and if the end of the data hasn't been reached yet | |
297 | (!((samples_decoded_ >= sample_count_) && (session_.get_capture_state() == Session::Stopped))), | |
298 | sample_count_); | |
299 | } | |
300 | ||
301 | void DecoderStack::decode_data( | |
302 | const int64_t abs_start_samplenum, const int64_t sample_count, const unsigned int unit_size, | |
303 | srd_session *const session) | |
304 | { | |
305 | const unsigned int chunk_sample_count = | |
306 | DecodeChunkLength / segment_->unit_size(); | |
307 | ||
308 | for (int64_t i = abs_start_samplenum; !interrupt_ && i < sample_count; | |
309 | i += chunk_sample_count) { | |
310 | ||
311 | const int64_t chunk_end = min( | |
312 | i + chunk_sample_count, sample_count); | |
313 | const uint8_t* chunk = segment_->get_samples(i, chunk_end); | |
314 | ||
315 | if (srd_session_send(session, i, chunk_end, chunk, | |
316 | (chunk_end - i) * unit_size, unit_size) != SRD_OK) { | |
317 | error_message_ = tr("Decoder reported an error"); | |
318 | delete[] chunk; | |
319 | break; | |
320 | } | |
321 | delete[] chunk; | |
322 | ||
323 | { | |
324 | lock_guard<mutex> lock(output_mutex_); | |
325 | samples_decoded_ = chunk_end; | |
326 | } | |
327 | ||
328 | if (i % DecodeNotifyPeriod == 0) | |
329 | new_decode_data(); | |
330 | } | |
331 | ||
332 | new_decode_data(); | |
333 | } | |
334 | ||
335 | void DecoderStack::decode_proc() | |
336 | { | |
337 | optional<int64_t> sample_count; | |
338 | srd_session *session; | |
339 | srd_decoder_inst *prev_di = nullptr; | |
340 | ||
341 | assert(segment_); | |
342 | ||
343 | // Prevent any other decode threads from accessing libsigrokdecode | |
344 | lock_guard<mutex> srd_lock(global_srd_mutex_); | |
345 | ||
346 | // Create the session | |
347 | srd_session_new(&session); | |
348 | assert(session); | |
349 | ||
350 | // Create the decoders | |
351 | const unsigned int unit_size = segment_->unit_size(); | |
352 | ||
353 | for (const shared_ptr<decode::Decoder> &dec : stack_) { | |
354 | srd_decoder_inst *const di = dec->create_decoder_inst(session); | |
355 | ||
356 | if (!di) { | |
357 | error_message_ = tr("Failed to create decoder instance"); | |
358 | srd_session_destroy(session); | |
359 | return; | |
360 | } | |
361 | ||
362 | if (prev_di) | |
363 | srd_inst_stack (session, prev_di, di); | |
364 | ||
365 | prev_di = di; | |
366 | } | |
367 | ||
368 | // Get the intial sample count | |
369 | { | |
370 | unique_lock<mutex> input_lock(input_mutex_); | |
371 | sample_count = sample_count_ = segment_->get_sample_count(); | |
372 | } | |
373 | ||
374 | // Start the session | |
375 | srd_session_metadata_set(session, SRD_CONF_SAMPLERATE, | |
376 | g_variant_new_uint64((uint64_t)samplerate_)); | |
377 | ||
378 | srd_pd_output_callback_add(session, SRD_OUTPUT_ANN, | |
379 | DecoderStack::annotation_callback, this); | |
380 | ||
381 | srd_session_start(session); | |
382 | ||
383 | int64_t abs_start_samplenum = 0; | |
384 | do { | |
385 | decode_data(abs_start_samplenum, *sample_count, unit_size, session); | |
386 | abs_start_samplenum = *sample_count; | |
387 | } while (error_message_.isEmpty() && (sample_count = wait_for_data())); | |
388 | ||
389 | // Destroy the session | |
390 | srd_session_destroy(session); | |
391 | } | |
392 | ||
393 | void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder) | |
394 | { | |
395 | assert(pdata); | |
396 | assert(decoder); | |
397 | ||
398 | DecoderStack *const d = (DecoderStack*)decoder; | |
399 | assert(d); | |
400 | ||
401 | lock_guard<mutex> lock(d->output_mutex_); | |
402 | ||
403 | const Annotation a(pdata); | |
404 | ||
405 | // Find the row | |
406 | assert(pdata->pdo); | |
407 | assert(pdata->pdo->di); | |
408 | const srd_decoder *const decc = pdata->pdo->di->decoder; | |
409 | assert(decc); | |
410 | ||
411 | auto row_iter = d->rows_.end(); | |
412 | ||
413 | // Try looking up the sub-row of this class | |
414 | const auto r = d->class_rows_.find(make_pair(decc, a.format())); | |
415 | if (r != d->class_rows_.end()) | |
416 | row_iter = d->rows_.find((*r).second); | |
417 | else { | |
418 | // Failing that, use the decoder as a key | |
419 | row_iter = d->rows_.find(Row(decc)); | |
420 | } | |
421 | ||
422 | assert(row_iter != d->rows_.end()); | |
423 | if (row_iter == d->rows_.end()) { | |
424 | qDebug() << "Unexpected annotation: decoder = " << decc << | |
425 | ", format = " << a.format(); | |
426 | assert(false); | |
427 | return; | |
428 | } | |
429 | ||
430 | // Add the annotation | |
431 | (*row_iter).second.push_annotation(a); | |
432 | } | |
433 | ||
434 | void DecoderStack::on_new_frame() | |
435 | { | |
436 | begin_decode(); | |
437 | } | |
438 | ||
439 | void DecoderStack::on_data_received() | |
440 | { | |
441 | { | |
442 | unique_lock<mutex> lock(input_mutex_); | |
443 | if (segment_) | |
444 | sample_count_ = segment_->get_sample_count(); | |
445 | } | |
446 | input_cond_.notify_one(); | |
447 | } | |
448 | ||
449 | void DecoderStack::on_frame_ended() | |
450 | { | |
451 | { | |
452 | unique_lock<mutex> lock(input_mutex_); | |
453 | if (segment_) | |
454 | frame_complete_ = true; | |
455 | } | |
456 | input_cond_.notify_one(); | |
457 | } | |
458 | ||
459 | } // namespace data | |
460 | } // namespace pv |