]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/decoderstack.cpp
Renamed get_samplerate to samplerate
[pulseview.git] / pv / data / decoderstack.cpp
... / ...
CommitLineData
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/view/logicsignal.h>
36#include <pv/view/decode/annotation.h>
37
38using namespace boost;
39using namespace std;
40
41namespace pv {
42namespace data {
43
44const double DecoderStack::DecodeMargin = 1.0;
45const double DecoderStack::DecodeThreshold = 0.2;
46const int64_t DecoderStack::DecodeChunkLength = 4096;
47
48mutex DecoderStack::_global_decode_mutex;
49
50DecoderStack::DecoderStack(const srd_decoder *const dec) :
51 _samples_decoded(0)
52{
53 _stack.push_back(shared_ptr<decode::Decoder>(
54 new decode::Decoder(dec)));
55}
56
57DecoderStack::~DecoderStack()
58{
59 _decode_thread.interrupt();
60 _decode_thread.join();
61}
62
63const std::list< boost::shared_ptr<decode::Decoder> >&
64DecoderStack::stack() const
65{
66 return _stack;
67}
68
69void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder)
70{
71 assert(decoder);
72 _stack.push_back(decoder);
73}
74
75void DecoderStack::remove(int index)
76{
77 using pv::data::decode::Decoder;
78
79 assert(index >= 0);
80 assert(index < (int)_stack.size());
81
82 // Find the decoder in the stack
83 list< shared_ptr<Decoder> >::iterator iter = _stack.begin();
84 for(int i = 0; i < index; i++, iter++)
85 assert(iter != _stack.end());
86
87 // Delete the element
88 _stack.erase(iter);
89}
90
91int64_t DecoderStack::samples_decoded() const
92{
93 lock_guard<mutex> decode_lock(_mutex);
94 return _samples_decoded;
95}
96
97const vector< shared_ptr<view::decode::Annotation> >
98 DecoderStack::annotations() const
99{
100 lock_guard<mutex> lock(_mutex);
101 return _annotations;
102}
103
104QString DecoderStack::error_message()
105{
106 lock_guard<mutex> lock(_mutex);
107 return _error_message;
108}
109
110void DecoderStack::begin_decode()
111{
112 shared_ptr<pv::view::LogicSignal> logic_signal;
113 shared_ptr<pv::data::Logic> data;
114
115 _decode_thread.interrupt();
116 _decode_thread.join();
117
118 _samples_decoded = 0;
119
120 _annotations.clear();
121
122 // We get the logic data of the first probe in the list.
123 // This works because we are currently assuming all
124 // LogicSignals have the same data/snapshot
125 BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack)
126 if (dec && !dec->probes().empty() &&
127 ((logic_signal = (*dec->probes().begin()).second)) &&
128 ((data = logic_signal->logic_data())))
129 break;
130
131 if (!data)
132 return;
133
134 // Get the samplerate and start time
135 _start_time = data->get_start_time();
136 _samplerate = data->samplerate();
137 if (_samplerate == 0.0)
138 _samplerate = 1.0;
139
140 _decode_thread = boost::thread(&DecoderStack::decode_proc, this,
141 data);
142}
143
144void DecoderStack::clear()
145{
146 _annotations.clear();
147}
148
149uint64_t DecoderStack::get_max_sample_count() const
150{
151 if (_annotations.empty())
152 return 0;
153 return _annotations.back()->end_sample();
154}
155
156void DecoderStack::decode_proc(shared_ptr<data::Logic> data)
157{
158 srd_session *session;
159 uint8_t chunk[DecodeChunkLength];
160 srd_decoder_inst *prev_di = NULL;
161
162 assert(data);
163
164 const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots =
165 data->get_snapshots();
166 if (snapshots.empty())
167 return;
168
169 const shared_ptr<pv::data::LogicSnapshot> &snapshot =
170 snapshots.front();
171 const int64_t sample_count = snapshot->get_sample_count() - 1;
172
173 // Create the session
174 srd_session_new(&session);
175 assert(session);
176
177
178 // Create the decoders
179 BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack)
180 {
181 srd_decoder_inst *const di = dec->create_decoder_inst(session);
182
183 if (!di)
184 {
185 _error_message = tr("Failed to initialise decoder");
186 srd_session_destroy(session);
187 return;
188 }
189
190 if (prev_di)
191 srd_inst_stack (session, prev_di, di);
192
193 prev_di = di;
194 }
195
196 // Start the session
197 srd_session_metadata_set(session, SRD_CONF_SAMPLERATE,
198 g_variant_new_uint64((uint64_t)_samplerate));
199
200 srd_pd_output_callback_add(session, SRD_OUTPUT_ANN,
201 DecoderStack::annotation_callback, this);
202
203 srd_session_start(session);
204
205 for (int64_t i = 0;
206 !this_thread::interruption_requested() && i < sample_count;
207 i += DecodeChunkLength)
208 {
209 lock_guard<mutex> decode_lock(_global_decode_mutex);
210
211 const int64_t chunk_end = min(
212 i + DecodeChunkLength, sample_count);
213 snapshot->get_samples(chunk, i, chunk_end);
214
215 if (srd_session_send(session, i, i + sample_count,
216 chunk, chunk_end - i) != SRD_OK) {
217 _error_message = tr("Failed to initialise decoder");
218 break;
219 }
220
221 {
222 lock_guard<mutex> lock(_mutex);
223 _samples_decoded = chunk_end;
224 }
225 }
226
227 // Destroy the session
228 srd_session_destroy(session);
229}
230
231void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder)
232{
233 using namespace pv::view::decode;
234
235 assert(pdata);
236 assert(decoder);
237
238 DecoderStack *const d = (DecoderStack*)decoder;
239
240 shared_ptr<Annotation> a(new Annotation(pdata));
241 lock_guard<mutex> lock(d->_mutex);
242 d->_annotations.push_back(a);
243
244 d->new_decode_data();
245}
246
247} // namespace data
248} // namespace pv