]>
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; | |
41 | using boost::shared_ptr; | |
42 | using std::deque; | |
f9101a91 JH |
43 | using std::make_pair; |
44 | using std::max; | |
819f4c25 JH |
45 | using std::min; |
46 | using std::list; | |
f9101a91 JH |
47 | using std::map; |
48 | using std::pair; | |
819f4c25 | 49 | using std::vector; |
e332f6d3 | 50 | |
f9101a91 JH |
51 | using namespace pv::data::decode; |
52 | ||
119aff65 JH |
53 | namespace pv { |
54 | namespace data { | |
55 | ||
6e89374a JH |
56 | const double DecoderStack::DecodeMargin = 1.0; |
57 | const double DecoderStack::DecodeThreshold = 0.2; | |
58 | const int64_t DecoderStack::DecodeChunkLength = 4096; | |
e0fc5810 | 59 | |
6e89374a | 60 | mutex DecoderStack::_global_decode_mutex; |
fe89c961 | 61 | |
bdc5c3b0 JH |
62 | DecoderStack::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 | 73 | DecoderStack::~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 |
81 | const std::list< boost::shared_ptr<decode::Decoder> >& |
82 | DecoderStack::stack() const | |
4e5a4405 | 83 | { |
7491a29f | 84 | return _stack; |
4e5a4405 JH |
85 | } |
86 | ||
7491a29f | 87 | void 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 |
93 | void 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 |
107 | int64_t DecoderStack::samples_decoded() const |
108 | { | |
109 | lock_guard<mutex> decode_lock(_mutex); | |
110 | return _samples_decoded; | |
111 | } | |
112 | ||
dd048a7e | 113 | std::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 |
145 | void 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 | 159 | QString DecoderStack::error_message() |
b6b267bb JH |
160 | { |
161 | lock_guard<mutex> lock(_mutex); | |
162 | return _error_message; | |
163 | } | |
164 | ||
f9101a91 JH |
165 | void DecoderStack::clear() |
166 | { | |
167 | _samples_decoded = 0; | |
168 | _error_message = QString(); | |
169 | _rows.clear(); | |
170 | _class_rows.clear(); | |
171 | } | |
172 | ||
6e89374a | 173 | void 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 |
238 | uint64_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 | ||
6e89374a | 250 | void DecoderStack::decode_proc(shared_ptr<data::Logic> data) |
119aff65 | 251 | { |
b6b267bb | 252 | srd_session *session; |
e0fc5810 | 253 | uint8_t chunk[DecodeChunkLength]; |
7491a29f | 254 | srd_decoder_inst *prev_di = NULL; |
e0fc5810 JH |
255 | |
256 | assert(data); | |
257 | ||
a2d4b551 | 258 | // Check we have a snapshot of data |
e0fc5810 JH |
259 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = |
260 | data->get_snapshots(); | |
261 | if (snapshots.empty()) | |
262 | return; | |
263 | ||
a2d4b551 JH |
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 | ||
e0fc5810 JH |
269 | const shared_ptr<pv::data::LogicSnapshot> &snapshot = |
270 | snapshots.front(); | |
c02f1e09 | 271 | const int64_t sample_count = snapshot->get_sample_count(); |
c2d4e9df | 272 | const unsigned int unit_size = snapshot->unit_size(); |
064a7f42 | 273 | const unsigned int chunk_sample_count = |
c2d4e9df | 274 | DecodeChunkLength / unit_size; |
e0fc5810 | 275 | |
b6b267bb JH |
276 | // Create the session |
277 | srd_session_new(&session); | |
278 | assert(session); | |
e0fc5810 | 279 | |
7491a29f JH |
280 | // Create the decoders |
281 | BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack) | |
282 | { | |
13a3528c | 283 | srd_decoder_inst *const di = dec->create_decoder_inst(session, unit_size); |
e0fc5810 | 284 | |
7491a29f JH |
285 | if (!di) |
286 | { | |
8a290daf | 287 | _error_message = tr("Failed to create decoder instance"); |
7491a29f JH |
288 | srd_session_destroy(session); |
289 | return; | |
290 | } | |
b6b267bb | 291 | |
7491a29f JH |
292 | if (prev_di) |
293 | srd_inst_stack (session, prev_di, di); | |
b6b267bb | 294 | |
7491a29f | 295 | prev_di = di; |
b6b267bb JH |
296 | } |
297 | ||
b6b267bb | 298 | // Start the session |
7491a29f JH |
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 | ||
b6b267bb JH |
305 | srd_session_start(session); |
306 | ||
e0fc5810 | 307 | for (int64_t i = 0; |
819f4c25 JH |
308 | !boost::this_thread::interruption_requested() && |
309 | i < sample_count; | |
064a7f42 | 310 | i += chunk_sample_count) |
e0fc5810 | 311 | { |
fe89c961 JH |
312 | lock_guard<mutex> decode_lock(_global_decode_mutex); |
313 | ||
e0fc5810 | 314 | const int64_t chunk_end = min( |
064a7f42 | 315 | i + chunk_sample_count, sample_count); |
e0fc5810 JH |
316 | snapshot->get_samples(chunk, i, chunk_end); |
317 | ||
c2d4e9df DE |
318 | if (srd_session_send(session, i, i + sample_count, chunk, |
319 | (chunk_end - i) * unit_size) != SRD_OK) { | |
8a290daf | 320 | _error_message = tr("Decoder reported an error"); |
e0fc5810 | 321 | break; |
b6b267bb | 322 | } |
5dfeb70f JH |
323 | |
324 | { | |
325 | lock_guard<mutex> lock(_mutex); | |
326 | _samples_decoded = chunk_end; | |
327 | } | |
e0fc5810 | 328 | } |
b6b267bb JH |
329 | |
330 | // Destroy the session | |
331 | srd_session_destroy(session); | |
e0fc5810 JH |
332 | } |
333 | ||
6e89374a | 334 | void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder) |
e0fc5810 | 335 | { |
e0fc5810 JH |
336 | assert(pdata); |
337 | assert(decoder); | |
338 | ||
6e89374a | 339 | DecoderStack *const d = (DecoderStack*)decoder; |
f9101a91 | 340 | assert(d); |
e0fc5810 | 341 | |
b6b267bb | 342 | lock_guard<mutex> lock(d->_mutex); |
6d8b0562 | 343 | |
f9101a91 JH |
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 | |
6d8b0562 | 360 | { |
f9101a91 JH |
361 | // Failing that, use the decoder as a key |
362 | row_iter = d->_rows.find(Row(decc)); | |
6d8b0562 UH |
363 | } |
364 | ||
f9101a91 JH |
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 | } | |
9cef9567 | 372 | |
f9101a91 JH |
373 | // Add the annotation |
374 | (*row_iter).second.push_annotation(a); | |
92421299 | 375 | |
9cef9567 | 376 | d->new_decode_data(); |
119aff65 JH |
377 | } |
378 | ||
82f50b10 JH |
379 | void DecoderStack::on_new_frame() |
380 | { | |
381 | begin_decode(); | |
382 | } | |
383 | ||
119aff65 JH |
384 | } // namespace data |
385 | } // namespace pv |