]> sigrok.org Git - libsigrokdecode.git/blame - decoders/spdif/pd.py
spdif: Refactor and reduce nesting level.
[libsigrokdecode.git] / decoders / spdif / pd.py
CommitLineData
cb6d4c6d
GW
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2014 Guenther Wenninger <robin@bitschubbser.org>
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
21import sigrokdecode as srd
22
23class SamplerateError(Exception):
24 pass
25
26class Decoder(srd.Decoder):
27 api_version = 2
28 id = 'spdif'
29 name = 'S/PDIF'
30 longname = 'Sony/Philips Digital Interface Format'
31 desc = 'Serial bus for connecting digital audio devices.'
32 license = 'gplv2+'
33 inputs = ['logic']
34 outputs = ['spdif']
35 channels = (
36 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
37 )
38 annotations = (
39 ('bitrate', 'Bitrate / baudrate'),
40 ('preamble', 'Preamble'),
41 ('bits', 'Bits'),
42 ('aux', 'Auxillary-audio-databits'),
43 ('samples', 'Audio Samples'),
44 ('validity', 'Data Valid'),
45 ('subcode', 'Subcode data'),
46 ('chan_stat', 'Channnel Status'),
47 ('parity', 'Parity Bit'),
48 )
49 annotation_rows = (
50 ('info', 'Info', (0, 1, 3, 5, 6, 7, 8)),
51 ('bits', 'Bits', (2,)),
52 ('samples', 'Samples', (4,)),
53 )
54
55 def putx(self, ss, es, data):
56 self.put(ss, es, self.out_ann, data)
57
58 def __init__(self, **kwargs):
59 self.state = 0
60 self.olddata = None
61 self.ss_edge = None
62 self.first_edge = True
63 self.pulse_width = 0
64
65 self.clocks = []
66 self.range1 = 0
67 self.range2 = 0
68
69 self.preamble_state = 0
70 self.preamble = []
71 self.seen_preamble = False
72 self.last_preamble = 0
73
74 self.first_one = True
75 self.subframe = []
76
77 def start(self):
78 self.out_ann = self.register(srd.OUTPUT_ANN)
79
80 def metadata(self, key, value):
81 if key == srd.SRD_CONF_SAMPLERATE:
82 self.samplerate = value
83
84 def get_pulse_type(self, pulse):
85 if self.range1 == 0 or self.range2 == 0:
86 return -1
87 if pulse >= self.range2:
88 return 2
89 elif pulse >= self.range1:
90 return 0
91 else:
92 return 1
93
eb148e7a
UH
94 def find_first_pulse_width(self):
95 if self.pulse_width != 0:
96 self.clocks.append(self.pulse_width)
97 self.state = 1
98
99 def find_second_pulse_width(self):
100 if self.pulse_width > (self.clocks[0] * 1.3) or \
101 self.pulse_width < (self.clocks[0] * 0.7):
102 self.clocks.append(self.pulse_width)
103 self.state = 2
104
105 def find_third_pulse_width(self):
106 if not ((self.pulse_width > (self.clocks[0] * 1.3) or \
107 self.pulse_width < (self.clocks[0] * 0.7)) \
108 and (self.pulse_width > (self.clocks[1] * 1.3) or \
109 self.pulse_width < (self.clocks[1] * 0.7))):
110 return
111
112 self.clocks.append(self.pulse_width)
113 self.clocks.sort()
114 self.range1 = (self.clocks[0] + self.clocks[1]) / 2
115 self.range2 = (self.clocks[1] + self.clocks[2]) / 2
116 spdif_bitrate = int(self.samplerate / (self.clocks[2] / 1.5))
117 self.ss_edge = 0
118
119 self.putx(self.ss_edge, self.samplenum, [0, \
120 ['Signal Bitrate: %d Mbit/s (=> %d kHz)' % \
121 (spdif_bitrate, (spdif_bitrate/ (2 * 32)))]])
122
123 clock_period_nsec = 1000000000 / spdif_bitrate
124
125 self.last_preamble = self.samplenum
126
127 # We are done recovering the clock, now let's decode the data stream.
128 self.state = 3
129
130 def decode_stream(self):
131 pulse = self.get_pulse_type(self.pulse_width)
132
133 if not self.seen_preamble:
134 # This is probably the start of a preamble, decode it.
135 if pulse == 2:
136 self.preamble.append(self.get_pulse_type(self.pulse_width))
137 self.state = 4 # Decode a preamble.
138 self.ss_edge = self.samplenum - self.pulse_width - 1
139 return
140
141 # We've seen a preamble.
142 if pulse == 1 and self.first_one:
143 self.first_one = False
144 self.subframe.append([pulse, self.samplenum - \
145 self.pulse_width - 1, self.samplenum])
146 elif pulse == 1 and not self.first_one:
147 self.subframe[-1][2] = self.samplenum
148 self.putx(self.subframe[-1][1], self.samplenum, [2, ['1']])
149 self.bitcount += 1
150 self.first_one = True
151 else:
152 self.subframe.append([pulse, self.samplenum - \
153 self.pulse_width - 1, self.samplenum])
154 self.putx(self.samplenum - self.pulse_width - 1,
155 self.samplenum, [2, ['0']])
156 self.bitcount += 1
157
158 if self.bitcount == 28:
159 aux_audio_data = self.subframe[0:4]
160 sam, sam_rot = '', ''
161 for a in aux_audio_data:
162 sam = sam + str(a[0])
163 sam_rot = str(a[0]) + sam_rot
164 sample = self.subframe[4:24]
165 for s in sample:
166 sam = sam + str(s[0])
167 sam_rot = str(s[0]) + sam_rot
168 validity = self.subframe[24:25]
169 subcode_data = self.subframe[25:26]
170 channel_status = self.subframe[26:27]
171 parity = self.subframe[27:28]
172
173 self.putx(aux_audio_data[0][1], aux_audio_data[3][2], \
174 [3, ['Aux 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]])
175 self.putx(sample[0][1], sample[19][2], \
176 [3, ['Sample 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]])
177 self.putx(aux_audio_data[0][1], sample[19][2], \
178 [4, ['Audio 0x%x' % int(sam_rot, 2), '0x%x' % int(sam_rot, 2)]])
179 if validity[0][0] == 0:
180 self.putx(validity[0][1], validity[0][2], [5, ['V']])
181 else:
182 self.putx(validity[0][1], validity[0][2], [5, ['E']])
183 self.putx(subcode_data[0][1], subcode_data[0][2],
184 [6, ['S: %d' % subcode_data[0][0]]])
185 self.putx(channel_status[0][1], channel_status[0][2],
186 [7, ['C: %d' % channel_status[0][0]]])
187 self.putx(parity[0][1], parity[0][2], [8, ['P: %d' % parity[0][0]]])
188
189 self.subframe = []
190 self.seen_preamble = False
191 self.bitcount = 0
192
193 def handle_preamble(self):
194 if self.preamble_state == 0:
195 self.preamble.append(self.get_pulse_type(self.pulse_width))
196 self.preamble_state = 1
197 elif self.preamble_state == 1:
198 self.preamble.append(self.get_pulse_type(self.pulse_width))
199 self.preamble_state = 2
200 elif self.preamble_state == 2:
201 self.preamble.append(self.get_pulse_type(self.pulse_width))
202 self.preamble_state = 0
203 self.state = 3
204 if self.preamble == [2, 0, 1, 0]:
205 self.putx(self.ss_edge, self.samplenum, [1, ['Preamble W', 'W']])
206 elif self.preamble == [2, 2, 1, 1]:
207 self.putx(self.ss_edge, self.samplenum, [1, ['Preamble M', 'M']])
208 elif self.preamble == [2, 1, 1, 2]:
209 self.putx(self.ss_edge, self.samplenum, [1, ['Preamble B', 'B']])
210 else:
211 self.putx(self.ss_edge, self.samplenum, [1, ['Unknown Preamble', 'Unkown Prea.', 'U']])
212 self.preamble = []
213 self.seen_preamble = True
214 self.bitcount = 0
215 self.first_one = True
216
217 self.last_preamble = self.samplenum
218
cb6d4c6d
GW
219 def decode(self, ss, es, data):
220 if not self.samplerate:
221 raise SamplerateError('Cannot decode without samplerate.')
222
223 for (self.samplenum, pins) in data:
224 data = pins[0]
225
eb148e7a 226 # Initialize self.olddata with the first sample value.
cb6d4c6d
GW
227 if self.olddata == None:
228 self.olddata = data
229 continue
230
231 # First we need to recover the clock.
232 if self.olddata == data:
233 self.pulse_width += 1
eb148e7a 234 continue
cb6d4c6d 235
eb148e7a
UH
236 # Found rising or falling edge.
237 if self.first_edge:
238 # Throw away first detected edge as it might be mangled data.
239 self.first_edge = False
cb6d4c6d 240 self.pulse_width = 0
eb148e7a
UH
241 else:
242 if self.state == 0:
243 self.find_first_pulse_width()
244 elif self.state == 1:
245 self.find_second_pulse_width()
246 elif self.state == 2:
247 self.find_third_pulse_width()
248 elif self.state == 3:
249 self.decode_stream()
250 elif self.state == 4:
251 self.handle_preamble()
252
253 self.pulse_width = 0
cb6d4c6d
GW
254
255 self.olddata = data