]> sigrok.org Git - pulseview.git/blame - pv/data/decoderstack.cpp
Moved inner decode loop into DecoderStack::decode_data
[pulseview.git] / pv / data / decoderstack.cpp
CommitLineData
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
39using boost::lock_guard;
40using boost::mutex;
41using boost::shared_ptr;
42using std::deque;
f9101a91
JH
43using std::make_pair;
44using std::max;
819f4c25
JH
45using std::min;
46using std::list;
f9101a91
JH
47using std::map;
48using std::pair;
819f4c25 49using std::vector;
e332f6d3 50
f9101a91
JH
51using namespace pv::data::decode;
52
119aff65
JH
53namespace pv {
54namespace data {
55
6e89374a
JH
56const double DecoderStack::DecodeMargin = 1.0;
57const double DecoderStack::DecodeThreshold = 0.2;
58const int64_t DecoderStack::DecodeChunkLength = 4096;
e0fc5810 59
6e89374a 60mutex DecoderStack::_global_decode_mutex;
fe89c961 61
bdc5c3b0
JH
62DecoderStack::DecoderStack(pv::SigSession &session,
63 const srd_decoder *const dec) :
64 _session(session),
5dfeb70f 65 _samples_decoded(0)
119aff65 66{
82f50b10
JH
67 connect(&_session, SIGNAL(frame_began()), this, SLOT(on_new_frame()));
68
7491a29f
JH
69 _stack.push_back(shared_ptr<decode::Decoder>(
70 new decode::Decoder(dec)));
119aff65
JH
71}
72
6e89374a 73DecoderStack::~DecoderStack()
640d69ce 74{
6d483b8b
MC
75 if (_decode_thread.joinable()) {
76 _decode_thread.interrupt();
77 _decode_thread.join();
78 }
4e5a4405
JH
79}
80
7491a29f
JH
81const std::list< boost::shared_ptr<decode::Decoder> >&
82DecoderStack::stack() const
4e5a4405 83{
7491a29f 84 return _stack;
4e5a4405
JH
85}
86
7491a29f 87void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
4e5a4405 88{
7491a29f
JH
89 assert(decoder);
90 _stack.push_back(decoder);
4e5a4405
JH
91}
92
613d097c
JH
93void DecoderStack::remove(int index)
94{
613d097c
JH
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
5dfeb70f
JH
107int64_t DecoderStack::samples_decoded() const
108{
109 lock_guard<mutex> decode_lock(_mutex);
110 return _samples_decoded;
111}
112
dd048a7e 113std::vector<Row> DecoderStack::get_visible_rows() const
f9101a91
JH
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);
dd048a7e
JH
122 if (!dec->shown())
123 continue;
124
f9101a91
JH
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
92421299
JH
145void DecoderStack::get_annotation_subset(
146 std::vector<pv::data::decode::Annotation> &dest,
f9101a91
JH
147 const Row &row, uint64_t start_sample,
148 uint64_t end_sample) const
e0fc5810 149{
b6b267bb 150 lock_guard<mutex> lock(_mutex);
92421299 151
f9101a91
JH
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);
e0fc5810
JH
157}
158
6e89374a 159QString DecoderStack::error_message()
b6b267bb
JH
160{
161 lock_guard<mutex> lock(_mutex);
162 return _error_message;
163}
164
f9101a91
JH
165void DecoderStack::clear()
166{
167 _samples_decoded = 0;
168 _error_message = QString();
169 _rows.clear();
170 _class_rows.clear();
171}
172
6e89374a 173void DecoderStack::begin_decode()
640d69ce 174{
7491a29f
JH
175 shared_ptr<pv::view::LogicSignal> logic_signal;
176 shared_ptr<pv::data::Logic> data;
177
6d483b8b
MC
178 if (_decode_thread.joinable()) {
179 _decode_thread.interrupt();
180 _decode_thread.join();
181 }
640d69ce 182
92421299 183 clear();
4e5a4405 184
f9101a91
JH
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
e0fc5810
JH
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
7491a29f
JH
219 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
220 if (dec && !dec->probes().empty() &&
221 ((logic_signal = (*dec->probes().begin()).second)) &&
7aa09b00 222 ((data = logic_signal->logic_data())))
7491a29f
JH
223 break;
224
225 if (!data)
226 return;
227
228 // Get the samplerate and start time
229 _start_time = data->get_start_time();
9d28da5a 230 _samplerate = data->samplerate();
7491a29f
JH
231 if (_samplerate == 0.0)
232 _samplerate = 1.0;
e0fc5810 233
6e89374a 234 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
e0fc5810 235 data);
640d69ce
JH
236}
237
a007f5ad
JH
238uint64_t DecoderStack::get_max_sample_count() const
239{
f9101a91
JH
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;
a007f5ad
JH
248}
249
f67d9e9b
JH
250void DecoderStack::decode_data(
251 const shared_ptr<pv::data::LogicSnapshot> &snapshot,
252 srd_session *const session)
253{
254 uint8_t chunk[DecodeChunkLength];
255
256 const int64_t sample_count = snapshot->get_sample_count();
257 const unsigned int unit_size = snapshot->unit_size();
258 const unsigned int chunk_sample_count =
259 DecodeChunkLength / snapshot->unit_size();
260
261 for (int64_t i = 0;
262 !boost::this_thread::interruption_requested() &&
263 i < sample_count;
264 i += chunk_sample_count)
265 {
266 lock_guard<mutex> decode_lock(_global_decode_mutex);
267
268 const int64_t chunk_end = min(
269 i + chunk_sample_count, sample_count);
270 snapshot->get_samples(chunk, i, chunk_end);
271
272 if (srd_session_send(session, i, i + sample_count, chunk,
273 (chunk_end - i) * unit_size) != SRD_OK) {
274 _error_message = tr("Decoder reported an error");
275 break;
276 }
277
278 {
279 lock_guard<mutex> lock(_mutex);
280 _samples_decoded = chunk_end;
281 }
282 }
283
284}
285
6e89374a 286void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
119aff65 287{
b6b267bb 288 srd_session *session;
7491a29f 289 srd_decoder_inst *prev_di = NULL;
e0fc5810
JH
290
291 assert(data);
292
a2d4b551 293 // Check we have a snapshot of data
e0fc5810
JH
294 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
295 data->get_snapshots();
296 if (snapshots.empty())
297 return;
298
a2d4b551
JH
299 // Check that all decoders have the required probes
300 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
301 if (!dec->have_required_probes())
302 return;
303
b6b267bb
JH
304 // Create the session
305 srd_session_new(&session);
306 assert(session);
e0fc5810 307
7491a29f 308 // Create the decoders
f67d9e9b
JH
309 const shared_ptr<pv::data::LogicSnapshot> &snapshot = snapshots.front();
310 const unsigned int unit_size = snapshot->unit_size();
311
7491a29f
JH
312 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
313 {
13a3528c 314 srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size);
e0fc5810 315
7491a29f
JH
316 if (!di)
317 {
8a290daf 318 _error_message = tr("Failed to create decoder instance");
7491a29f
JH
319 srd_session_destroy(session);
320 return;
321 }
b6b267bb 322
7491a29f
JH
323 if (prev_di)
324 srd_inst_stack (session, prev_di, di);
b6b267bb 325
7491a29f 326 prev_di = di;
b6b267bb
JH
327 }
328
b6b267bb 329 // Start the session
7491a29f
JH
330 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
331 g_variant_new_uint64((uint64_t)_samplerate));
332
333 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
334 DecoderStack::annotation_callback, this);
335
b6b267bb
JH
336 srd_session_start(session);
337
f67d9e9b 338 decode_data(snapshot, session);
b6b267bb
JH
339
340 // Destroy the session
341 srd_session_destroy(session);
e0fc5810
JH
342}
343
6e89374a 344void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
e0fc5810 345{
e0fc5810
JH
346 assert(pdata);
347 assert(decoder);
348
6e89374a 349 DecoderStack *const d = (DecoderStack*)decoder;
f9101a91 350 assert(d);
e0fc5810 351
b6b267bb 352 lock_guard<mutex> lock(d->_mutex);
6d8b0562 353
f9101a91
JH
354 const Annotation a(pdata);
355
356 // Find the row
357 assert(pdata->pdo);
358 assert(pdata->pdo->di);
359 const srd_decoder *const decc = pdata->pdo->di->decoder;
360 assert(decc);
361
362 map<const Row, decode::RowData>::iterator row_iter = d->_rows.end();
363
364 // Try looking up the sub-row of this class
365 const map<pair<const srd_decoder*, int>, Row>::const_iterator r =
366 d->_class_rows.find(make_pair(decc, a.format()));
367 if (r != d->_class_rows.end())
368 row_iter = d->_rows.find((*r).second);
369 else
6d8b0562 370 {
f9101a91
JH
371 // Failing that, use the decoder as a key
372 row_iter = d->_rows.find(Row(decc));
6d8b0562
UH
373 }
374
f9101a91
JH
375 assert(row_iter != d->_rows.end());
376 if (row_iter == d->_rows.end()) {
377 qDebug() << "Unexpected annotation: decoder = " << decc <<
378 ", format = " << a.format();
379 assert(0);
380 return;
381 }
9cef9567 382
f9101a91
JH
383 // Add the annotation
384 (*row_iter).second.push_annotation(a);
92421299 385
9cef9567 386 d->new_decode_data();
119aff65
JH
387}
388
82f50b10
JH
389void DecoderStack::on_new_frame()
390{
391 begin_decode();
392}
393
119aff65
JH
394} // namespace data
395} // namespace pv