]> sigrok.org Git - pulseview.git/blob - pv/data/decoderstack.cpp
40683758a4c1280de4815e7439e09e68fc5e47cf
[pulseview.git] / pv / data / decoderstack.cpp
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
21 #include <libsigrokdecode/libsigrokdecode.h>
22
23 #include <boost/thread/thread.hpp>
24
25 #include <stdexcept>
26
27 #include <QDebug>
28
29 #include "decoderstack.h"
30
31 #include <pv/data/logic.h>
32 #include <pv/data/logicsnapshot.h>
33 #include <pv/data/decode/decoder.h>
34 #include <pv/data/decode/annotation.h>
35 #include <pv/sigsession.h>
36 #include <pv/view/logicsignal.h>
37
38 using boost::lock_guard;
39 using boost::mutex;
40 using boost::optional;
41 using boost::unique_lock;
42 using std::deque;
43 using std::make_pair;
44 using std::max;
45 using std::min;
46 using std::list;
47 using std::map;
48 using std::pair;
49 using std::shared_ptr;
50 using std::vector;
51
52 using namespace pv::data::decode;
53
54 namespace pv {
55 namespace data {
56
57 const double DecoderStack::DecodeMargin = 1.0;
58 const double DecoderStack::DecodeThreshold = 0.2;
59 const int64_t DecoderStack::DecodeChunkLength = 4096;
60 const unsigned int DecoderStack::DecodeNotifyPeriod = 65536;
61
62 mutex DecoderStack::_global_decode_mutex;
63
64 DecoderStack::DecoderStack(pv::SigSession &session,
65         const srd_decoder *const dec) :
66         _session(session),
67         _sample_count(0),
68         _frame_complete(false),
69         _samples_decoded(0)
70 {
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()));
77
78         _stack.push_back(shared_ptr<decode::Decoder>(
79                 new decode::Decoder(dec)));
80 }
81
82 DecoderStack::~DecoderStack()
83 {
84         if (_decode_thread.joinable()) {
85                 _decode_thread.interrupt();
86                 _decode_thread.join();
87         }
88 }
89
90 const std::list< std::shared_ptr<decode::Decoder> >&
91 DecoderStack::stack() const
92 {
93         return _stack;
94 }
95
96 void DecoderStack::push(std::shared_ptr<decode::Decoder> decoder)
97 {
98         assert(decoder);
99         _stack.push_back(decoder);
100 }
101
102 void DecoderStack::remove(int index)
103 {
104         assert(index >= 0);
105         assert(index < (int)_stack.size());
106
107         // Find the decoder in the stack
108         auto 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
116 int64_t DecoderStack::samples_decoded() const
117 {
118         lock_guard<mutex> decode_lock(_output_mutex);
119         return _samples_decoded;
120 }
121
122 std::vector<Row> DecoderStack::get_visible_rows() const
123 {
124         lock_guard<mutex> lock(_output_mutex);
125
126         vector<Row> rows;
127
128         for (const shared_ptr<decode::Decoder> &dec : _stack)
129         {
130                 assert(dec);
131                 if (!dec->shown())
132                         continue;
133
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
154 void DecoderStack::get_annotation_subset(
155         std::vector<pv::data::decode::Annotation> &dest,
156         const Row &row, uint64_t start_sample,
157         uint64_t end_sample) const
158 {
159         lock_guard<mutex> lock(_output_mutex);
160
161         const auto iter = _rows.find(row);
162         if (iter != _rows.end())
163                 (*iter).second.get_annotation_subset(dest,
164                         start_sample, end_sample);
165 }
166
167 QString DecoderStack::error_message()
168 {
169         lock_guard<mutex> lock(_output_mutex);
170         return _error_message;
171 }
172
173 void DecoderStack::clear()
174 {
175         _sample_count = 0;
176         _frame_complete = false;
177         _samples_decoded = 0;
178         _error_message = QString();
179         _rows.clear();
180         _class_rows.clear();
181 }
182
183 void DecoderStack::begin_decode()
184 {
185         shared_ptr<pv::view::LogicSignal> logic_signal;
186         shared_ptr<pv::data::Logic> data;
187
188         if (_decode_thread.joinable()) {
189                 _decode_thread.interrupt();
190                 _decode_thread.join();
191         }
192
193         clear();
194
195         // Check that all decoders have the required channels
196         for (const shared_ptr<decode::Decoder> &dec : _stack)
197                 if (!dec->have_required_probes()) {
198                         _error_message = tr("One or more required channels "
199                                 "have not been specified");
200                         return;
201                 }
202
203         // Add classes
204         for (const shared_ptr<decode::Decoder> &dec : _stack)
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
234         // We get the logic data of the first channel in the list.
235         // This works because we are currently assuming all
236         // LogicSignals have the same data/snapshot
237         for (const shared_ptr<decode::Decoder> &dec : _stack)
238                 if (dec && !dec->channels().empty() &&
239                         ((logic_signal = (*dec->channels().begin()).second)) &&
240                         ((data = logic_signal->logic_data())))
241                         break;
242
243         if (!data)
244                 return;
245
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
253         // Get the samplerate and start time
254         _start_time = data->get_start_time();
255         _samplerate = data->samplerate();
256         if (_samplerate == 0.0)
257                 _samplerate = 1.0;
258
259         _decode_thread = boost::thread(&DecoderStack::decode_proc, this);
260 }
261
262 uint64_t DecoderStack::get_max_sample_count() const
263 {
264         uint64_t max_sample_count = 0;
265
266         for (auto i = _rows.cbegin(); i != _rows.end(); i++)
267                 max_sample_count = max(max_sample_count,
268                         (*i).second.get_max_sample());
269
270         return max_sample_count;
271 }
272
273 optional<int64_t> DecoderStack::wait_for_data() const
274 {
275         unique_lock<mutex> input_lock(_input_mutex);
276         while(!boost::this_thread::interruption_requested() &&
277                 !_frame_complete && _samples_decoded >= _sample_count)
278                 _input_cond.wait(input_lock);
279         return boost::make_optional(
280                 !boost::this_thread::interruption_requested() &&
281                 (_samples_decoded < _sample_count || !_frame_complete),
282                 _sample_count);
283 }
284
285 void DecoderStack::decode_data(
286         const int64_t sample_count, const unsigned int unit_size,
287         srd_session *const session)
288 {
289         uint8_t chunk[DecodeChunkLength];
290
291         const unsigned int chunk_sample_count =
292                 DecodeChunkLength / _snapshot->unit_size();
293
294         for (int64_t i = 0;
295                 !boost::this_thread::interruption_requested() &&
296                         i < sample_count;
297                 i += chunk_sample_count)
298         {
299                 lock_guard<mutex> decode_lock(_global_decode_mutex);
300
301                 const int64_t chunk_end = min(
302                         i + chunk_sample_count, sample_count);
303                 _snapshot->get_samples(chunk, i, chunk_end);
304
305                 if (srd_session_send(session, i, i + sample_count, chunk,
306                                 (chunk_end - i) * unit_size) != SRD_OK) {
307                         _error_message = tr("Decoder reported an error");
308                         break;
309                 }
310
311                 {
312                         lock_guard<mutex> lock(_output_mutex);
313                         _samples_decoded = chunk_end;
314                 }
315
316                 if (i % DecodeNotifyPeriod == 0)
317                         new_decode_data();
318         }
319
320         new_decode_data();
321 }
322
323 void DecoderStack::decode_proc()
324 {
325         optional<int64_t> sample_count;
326         srd_session *session;
327         srd_decoder_inst *prev_di = NULL;
328
329         assert(_snapshot);
330
331         // Create the session
332         srd_session_new(&session);
333         assert(session);
334
335         // Create the decoders
336         const unsigned int unit_size = _snapshot->unit_size();
337
338         for (const shared_ptr<decode::Decoder> &dec : _stack)
339         {
340                 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
341
342                 if (!di)
343                 {
344                         _error_message = tr("Failed to create decoder instance");
345                         srd_session_destroy(session);
346                         return;
347                 }
348
349                 if (prev_di)
350                         srd_inst_stack (session, prev_di, di);
351
352                 prev_di = di;
353         }
354
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
361         // Start the session
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
368         srd_session_start(session);
369
370         do {
371                 decode_data(*sample_count, unit_size, session);
372         } while(_error_message.isEmpty() && (sample_count = wait_for_data()));
373
374         // Destroy the session
375         srd_session_destroy(session);
376 }
377
378 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
379 {
380         assert(pdata);
381         assert(decoder);
382
383         DecoderStack *const d = (DecoderStack*)decoder;
384         assert(d);
385
386         lock_guard<mutex> lock(d->_output_mutex);
387
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         auto row_iter = d->_rows.end();
397         
398         // Try looking up the sub-row of this class
399         const auto r = d->_class_rows.find(make_pair(decc, a.format()));
400         if (r != d->_class_rows.end())
401                 row_iter = d->_rows.find((*r).second);
402         else
403         {
404                 // Failing that, use the decoder as a key
405                 row_iter = d->_rows.find(Row(decc));    
406         }
407
408         assert(row_iter != d->_rows.end());
409         if (row_iter == d->_rows.end()) {
410                 qDebug() << "Unexpected annotation: decoder = " << decc <<
411                         ", format = " << a.format();
412                 assert(0);
413                 return;
414         }
415
416         // Add the annotation
417         (*row_iter).second.push_annotation(a);
418 }
419
420 void DecoderStack::on_new_frame()
421 {
422         begin_decode();
423 }
424
425 void DecoderStack::on_data_received()
426 {
427         {
428                 unique_lock<mutex> lock(_input_mutex);
429                 if (_snapshot)
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                 if (_snapshot)
440                         _frame_complete = true;
441         }
442         _input_cond.notify_one();
443 }
444
445 } // namespace data
446 } // namespace pv