]> sigrok.org Git - pulseview.git/blob - pv/data/decoderstack.cpp
Begin a new decode session when a new frame begins
[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/foreach.hpp>
24 #include <boost/thread/thread.hpp>
25
26 #include <stdexcept>
27
28 #include <QDebug>
29
30 #include "decoderstack.h"
31
32 #include <pv/data/logic.h>
33 #include <pv/data/logicsnapshot.h>
34 #include <pv/data/decode/decoder.h>
35 #include <pv/data/decode/annotation.h>
36 #include <pv/sigsession.h>
37 #include <pv/view/logicsignal.h>
38
39 using boost::lock_guard;
40 using boost::mutex;
41 using boost::shared_ptr;
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::vector;
50
51 using namespace pv::data::decode;
52
53 namespace pv {
54 namespace data {
55
56 const double DecoderStack::DecodeMargin = 1.0;
57 const double DecoderStack::DecodeThreshold = 0.2;
58 const int64_t DecoderStack::DecodeChunkLength = 4096;
59
60 mutex DecoderStack::_global_decode_mutex;
61
62 DecoderStack::DecoderStack(pv::SigSession &session,
63         const srd_decoder *const dec) :
64         _session(session),
65         _samples_decoded(0)
66 {
67         connect(&_session, SIGNAL(frame_began()), this, SLOT(on_new_frame()));
68
69         _stack.push_back(shared_ptr<decode::Decoder>(
70                 new decode::Decoder(dec)));
71 }
72
73 DecoderStack::~DecoderStack()
74 {
75         if (_decode_thread.joinable()) {
76                 _decode_thread.interrupt();
77                 _decode_thread.join();
78         }
79 }
80
81 const std::list< boost::shared_ptr<decode::Decoder> >&
82 DecoderStack::stack() const
83 {
84         return _stack;
85 }
86
87 void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
88 {
89         assert(decoder);
90         _stack.push_back(decoder);
91 }
92
93 void DecoderStack::remove(int index)
94 {
95         assert(index >= 0);
96         assert(index < (int)_stack.size());
97
98         // Find the decoder in the stack
99         list< shared_ptr<Decoder> >::iterator iter = _stack.begin();
100         for(int i = 0; i < index; i++, iter++)
101                 assert(iter != _stack.end());
102
103         // Delete the element
104         _stack.erase(iter);
105 }
106
107 int64_t DecoderStack::samples_decoded() const
108 {
109         lock_guard<mutex> decode_lock(_mutex);
110         return _samples_decoded;
111 }
112
113 std::vector<Row> DecoderStack::get_visible_rows() const
114 {
115         lock_guard<mutex> lock(_mutex);
116
117         vector<Row> rows;
118
119         BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
120         {
121                 assert(dec);
122                 if (!dec->shown())
123                         continue;
124
125                 const srd_decoder *const decc = dec->decoder();
126                 assert(dec->decoder());
127
128                 // Add a row for the decoder if it doesn't have a row list
129                 if (!decc->annotation_rows)
130                         rows.push_back(Row(decc));
131
132                 // Add the decoder rows
133                 for (const GSList *l = decc->annotation_rows; l; l = l->next)
134                 {
135                         const srd_decoder_annotation_row *const ann_row =
136                                 (srd_decoder_annotation_row *)l->data;
137                         assert(ann_row);
138                         rows.push_back(Row(decc, ann_row));
139                 }
140         }
141
142         return rows;
143 }
144
145 void DecoderStack::get_annotation_subset(
146         std::vector<pv::data::decode::Annotation> &dest,
147         const Row &row, uint64_t start_sample,
148         uint64_t end_sample) const
149 {
150         lock_guard<mutex> lock(_mutex);
151
152         std::map<const Row, decode::RowData>::const_iterator iter =
153                 _rows.find(row);
154         if (iter != _rows.end())
155                 (*iter).second.get_annotation_subset(dest,
156                         start_sample, end_sample);
157 }
158
159 QString DecoderStack::error_message()
160 {
161         lock_guard<mutex> lock(_mutex);
162         return _error_message;
163 }
164
165 void DecoderStack::clear()
166 {
167         _samples_decoded = 0;
168         _error_message = QString();
169         _rows.clear();
170         _class_rows.clear();
171 }
172
173 void DecoderStack::begin_decode()
174 {
175         shared_ptr<pv::view::LogicSignal> logic_signal;
176         shared_ptr<pv::data::Logic> data;
177
178         if (_decode_thread.joinable()) {
179                 _decode_thread.interrupt();
180                 _decode_thread.join();
181         }
182
183         clear();
184
185         // Add classes
186         BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
187         {
188                 assert(dec);
189                 const srd_decoder *const decc = dec->decoder();
190                 assert(dec->decoder());
191
192                 // Add a row for the decoder if it doesn't have a row list
193                 if (!decc->annotation_rows)
194                         _rows[Row(decc)] = decode::RowData();
195
196                 // Add the decoder rows
197                 for (const GSList *l = decc->annotation_rows; l; l = l->next)
198                 {
199                         const srd_decoder_annotation_row *const ann_row =
200                                 (srd_decoder_annotation_row *)l->data;
201                         assert(ann_row);
202
203                         const Row row(decc, ann_row);
204
205                         // Add a new empty row data object
206                         _rows[row] = decode::RowData();
207
208                         // Map out all the classes
209                         for (const GSList *ll = ann_row->ann_classes;
210                                 ll; ll = ll->next)
211                                 _class_rows[make_pair(decc,
212                                         GPOINTER_TO_INT(ll->data))] = row;
213                 }
214         }
215
216         // We get the logic data of the first probe in the list.
217         // This works because we are currently assuming all
218         // LogicSignals have the same data/snapshot
219         BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
220                 if (dec && !dec->probes().empty() &&
221                         ((logic_signal = (*dec->probes().begin()).second)) &&
222                         ((data = logic_signal->logic_data())))
223                         break;
224
225         if (!data)
226                 return;
227
228         // Get the samplerate and start time
229         _start_time = data->get_start_time();
230         _samplerate = data->samplerate();
231         if (_samplerate == 0.0)
232                 _samplerate = 1.0;
233
234         _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
235                 data);
236 }
237
238 uint64_t DecoderStack::get_max_sample_count() const
239 {
240         uint64_t max_sample_count = 0;
241
242         for (map<const Row, RowData>::const_iterator i = _rows.begin();
243                 i != _rows.end(); i++)
244                 max_sample_count = max(max_sample_count,
245                         (*i).second.get_max_sample());
246
247         return max_sample_count;
248 }
249
250 void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
251 {
252         srd_session *session;
253         uint8_t chunk[DecodeChunkLength];
254         srd_decoder_inst *prev_di = NULL;
255
256         assert(data);
257
258         // Check we have a snapshot of data
259         const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
260                 data->get_snapshots();
261         if (snapshots.empty())
262                 return;
263
264         // Check that all decoders have the required probes
265         BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
266                 if (!dec->have_required_probes())
267                         return;
268
269         const shared_ptr<pv::data::LogicSnapshot> &snapshot =
270                 snapshots.front();
271         const int64_t sample_count = snapshot->get_sample_count();
272         const unsigned int unit_size = snapshot->unit_size();
273         const unsigned int chunk_sample_count =
274                 DecodeChunkLength / unit_size;
275
276         // Create the session
277         srd_session_new(&session);
278         assert(session);
279
280         // Create the decoders
281         BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
282         {
283                 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
284
285                 if (!di)
286                 {
287                         _error_message = tr("Failed to create decoder instance");
288                         srd_session_destroy(session);
289                         return;
290                 }
291
292                 if (prev_di)
293                         srd_inst_stack (session, prev_di, di);
294
295                 prev_di = di;
296         }
297
298         // Start the session
299         srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
300                 g_variant_new_uint64((uint64_t)_samplerate));
301
302         srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
303                 DecoderStack::annotation_callback, this);
304
305         srd_session_start(session);
306
307         for (int64_t i = 0;
308                 !boost::this_thread::interruption_requested() &&
309                         i < sample_count;
310                 i += chunk_sample_count)
311         {
312                 lock_guard<mutex> decode_lock(_global_decode_mutex);
313
314                 const int64_t chunk_end = min(
315                         i + chunk_sample_count, sample_count);
316                 snapshot->get_samples(chunk, i, chunk_end);
317
318                 if (srd_session_send(session, i, i + sample_count, chunk,
319                                 (chunk_end - i) * unit_size) != SRD_OK) {
320                         _error_message = tr("Decoder reported an error");
321                         break;
322                 }
323
324                 {
325                         lock_guard<mutex> lock(_mutex);
326                         _samples_decoded = chunk_end;
327                 }
328         }
329
330         // Destroy the session
331         srd_session_destroy(session);
332 }
333
334 void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
335 {
336         assert(pdata);
337         assert(decoder);
338
339         DecoderStack *const d = (DecoderStack*)decoder;
340         assert(d);
341
342         lock_guard<mutex> lock(d->_mutex);
343
344         const Annotation a(pdata);
345
346         // Find the row
347         assert(pdata->pdo);
348         assert(pdata->pdo->di);
349         const srd_decoder *const decc = pdata->pdo->di->decoder;
350         assert(decc);
351
352         map<const Row, decode::RowData>::iterator row_iter = d->_rows.end();
353         
354         // Try looking up the sub-row of this class
355         const map<pair<const srd_decoder*, int>, Row>::const_iterator r =
356                 d->_class_rows.find(make_pair(decc, a.format()));
357         if (r != d->_class_rows.end())
358                 row_iter = d->_rows.find((*r).second);
359         else
360         {
361                 // Failing that, use the decoder as a key
362                 row_iter = d->_rows.find(Row(decc));    
363         }
364
365         assert(row_iter != d->_rows.end());
366         if (row_iter == d->_rows.end()) {
367                 qDebug() << "Unexpected annotation: decoder = " << decc <<
368                         ", format = " << a.format();
369                 assert(0);
370                 return;
371         }
372
373         // Add the annotation
374         (*row_iter).second.push_annotation(a);
375
376         d->new_decode_data();
377 }
378
379 void DecoderStack::on_new_frame()
380 {
381         begin_decode();
382 }
383
384 } // namespace data
385 } // namespace pv