]>
Commit | Line | Data |
---|---|---|
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, see <http://www.gnu.org/licenses/>. | |
18 | ## | |
19 | ||
20 | import sigrokdecode as srd | |
21 | ||
22 | class SamplerateError(Exception): | |
23 | pass | |
24 | ||
25 | class Decoder(srd.Decoder): | |
26 | api_version = 3 | |
27 | id = 'spdif' | |
28 | name = 'S/PDIF' | |
29 | longname = 'Sony/Philips Digital Interface Format' | |
30 | desc = 'Serial bus for connecting digital audio devices.' | |
31 | license = 'gplv2+' | |
32 | inputs = ['logic'] | |
33 | outputs = [] | |
34 | tags = ['Audio', 'PC'] | |
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 | ('bits', 'Bits', (2,)), | |
51 | ('info', 'Info', (0, 1, 3, 5, 6, 7, 8)), | |
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 puty(self, data): | |
59 | self.put(self.ss_edge, self.samplenum, self.out_ann, data) | |
60 | ||
61 | def __init__(self): | |
62 | self.reset() | |
63 | ||
64 | def reset(self): | |
65 | self.state = 'GET FIRST PULSE WIDTH' | |
66 | self.ss_edge = None | |
67 | self.first_edge = True | |
68 | self.samplenum_prev_edge = 0 | |
69 | self.pulse_width = 0 | |
70 | ||
71 | self.clocks = [] | |
72 | self.range1 = 0 | |
73 | self.range2 = 0 | |
74 | ||
75 | self.preamble_state = 0 | |
76 | self.preamble = [] | |
77 | self.seen_preamble = False | |
78 | self.last_preamble = 0 | |
79 | ||
80 | self.first_one = True | |
81 | self.subframe = [] | |
82 | ||
83 | def start(self): | |
84 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
85 | ||
86 | def metadata(self, key, value): | |
87 | if key == srd.SRD_CONF_SAMPLERATE: | |
88 | self.samplerate = value | |
89 | ||
90 | def get_pulse_type(self): | |
91 | if self.range1 == 0 or self.range2 == 0: | |
92 | return -1 | |
93 | if self.pulse_width >= self.range2: | |
94 | return 2 | |
95 | elif self.pulse_width >= self.range1: | |
96 | return 0 | |
97 | else: | |
98 | return 1 | |
99 | ||
100 | def find_first_pulse_width(self): | |
101 | if self.pulse_width != 0: | |
102 | self.clocks.append(self.pulse_width) | |
103 | self.state = 'GET SECOND PULSE WIDTH' | |
104 | ||
105 | def find_second_pulse_width(self): | |
106 | if self.pulse_width > (self.clocks[0] * 1.3) or \ | |
107 | self.pulse_width < (self.clocks[0] * 0.7): | |
108 | self.clocks.append(self.pulse_width) | |
109 | self.state = 'GET THIRD PULSE WIDTH' | |
110 | ||
111 | def find_third_pulse_width(self): | |
112 | if not ((self.pulse_width > (self.clocks[0] * 1.3) or \ | |
113 | self.pulse_width < (self.clocks[0] * 0.7)) \ | |
114 | and (self.pulse_width > (self.clocks[1] * 1.3) or \ | |
115 | self.pulse_width < (self.clocks[1] * 0.7))): | |
116 | return | |
117 | ||
118 | self.clocks.append(self.pulse_width) | |
119 | self.clocks.sort() | |
120 | self.range1 = (self.clocks[0] + self.clocks[1]) / 2 | |
121 | self.range2 = (self.clocks[1] + self.clocks[2]) / 2 | |
122 | spdif_bitrate = int(self.samplerate / (self.clocks[2] / 1.5)) | |
123 | self.ss_edge = 0 | |
124 | ||
125 | self.puty([0, ['Signal Bitrate: %d Mbit/s (=> %d kHz)' % \ | |
126 | (spdif_bitrate, (spdif_bitrate/ (2 * 32)))]]) | |
127 | ||
128 | clock_period_nsec = 1000000000 / spdif_bitrate | |
129 | ||
130 | self.last_preamble = self.samplenum | |
131 | ||
132 | # We are done recovering the clock, now let's decode the data stream. | |
133 | self.state = 'DECODE STREAM' | |
134 | ||
135 | def decode_stream(self): | |
136 | pulse = self.get_pulse_type() | |
137 | ||
138 | if not self.seen_preamble: | |
139 | # This is probably the start of a preamble, decode it. | |
140 | if pulse == 2: | |
141 | self.preamble.append(self.get_pulse_type()) | |
142 | self.state = 'DECODE PREAMBLE' | |
143 | self.ss_edge = self.samplenum - self.pulse_width - 1 | |
144 | return | |
145 | ||
146 | # We've seen a preamble. | |
147 | if pulse == 1 and self.first_one: | |
148 | self.first_one = False | |
149 | self.subframe.append([pulse, self.samplenum - \ | |
150 | self.pulse_width - 1, self.samplenum]) | |
151 | elif pulse == 1 and not self.first_one: | |
152 | self.subframe[-1][2] = self.samplenum | |
153 | self.putx(self.subframe[-1][1], self.samplenum, [2, ['1']]) | |
154 | self.bitcount += 1 | |
155 | self.first_one = True | |
156 | else: | |
157 | self.subframe.append([pulse, self.samplenum - \ | |
158 | self.pulse_width - 1, self.samplenum]) | |
159 | self.putx(self.samplenum - self.pulse_width - 1, | |
160 | self.samplenum, [2, ['0']]) | |
161 | self.bitcount += 1 | |
162 | ||
163 | if self.bitcount == 28: | |
164 | aux_audio_data = self.subframe[0:4] | |
165 | sam, sam_rot = '', '' | |
166 | for a in aux_audio_data: | |
167 | sam = sam + str(a[0]) | |
168 | sam_rot = str(a[0]) + sam_rot | |
169 | sample = self.subframe[4:24] | |
170 | for s in sample: | |
171 | sam = sam + str(s[0]) | |
172 | sam_rot = str(s[0]) + sam_rot | |
173 | validity = self.subframe[24:25] | |
174 | subcode_data = self.subframe[25:26] | |
175 | channel_status = self.subframe[26:27] | |
176 | parity = self.subframe[27:28] | |
177 | ||
178 | self.putx(aux_audio_data[0][1], aux_audio_data[3][2], \ | |
179 | [3, ['Aux 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]]) | |
180 | self.putx(sample[0][1], sample[19][2], \ | |
181 | [3, ['Sample 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]]) | |
182 | self.putx(aux_audio_data[0][1], sample[19][2], \ | |
183 | [4, ['Audio 0x%x' % int(sam_rot, 2), '0x%x' % int(sam_rot, 2)]]) | |
184 | if validity[0][0] == 0: | |
185 | self.putx(validity[0][1], validity[0][2], [5, ['V']]) | |
186 | else: | |
187 | self.putx(validity[0][1], validity[0][2], [5, ['E']]) | |
188 | self.putx(subcode_data[0][1], subcode_data[0][2], | |
189 | [6, ['S: %d' % subcode_data[0][0]]]) | |
190 | self.putx(channel_status[0][1], channel_status[0][2], | |
191 | [7, ['C: %d' % channel_status[0][0]]]) | |
192 | self.putx(parity[0][1], parity[0][2], [8, ['P: %d' % parity[0][0]]]) | |
193 | ||
194 | self.subframe = [] | |
195 | self.seen_preamble = False | |
196 | self.bitcount = 0 | |
197 | ||
198 | def decode_preamble(self): | |
199 | if self.preamble_state == 0: | |
200 | self.preamble.append(self.get_pulse_type()) | |
201 | self.preamble_state = 1 | |
202 | elif self.preamble_state == 1: | |
203 | self.preamble.append(self.get_pulse_type()) | |
204 | self.preamble_state = 2 | |
205 | elif self.preamble_state == 2: | |
206 | self.preamble.append(self.get_pulse_type()) | |
207 | self.preamble_state = 0 | |
208 | self.state = 'DECODE STREAM' | |
209 | if self.preamble == [2, 0, 1, 0]: | |
210 | self.puty([1, ['Preamble W', 'W']]) | |
211 | elif self.preamble == [2, 2, 1, 1]: | |
212 | self.puty([1, ['Preamble M', 'M']]) | |
213 | elif self.preamble == [2, 1, 1, 2]: | |
214 | self.puty([1, ['Preamble B', 'B']]) | |
215 | else: | |
216 | self.puty([1, ['Unknown Preamble', 'Unknown Prea.', 'U']]) | |
217 | self.preamble = [] | |
218 | self.seen_preamble = True | |
219 | self.bitcount = 0 | |
220 | self.first_one = True | |
221 | ||
222 | self.last_preamble = self.samplenum | |
223 | ||
224 | def decode(self): | |
225 | if not self.samplerate: | |
226 | raise SamplerateError('Cannot decode without samplerate.') | |
227 | ||
228 | # Throw away first detected edge as it might be mangled data. | |
229 | self.wait({0: 'e'}) | |
230 | ||
231 | while True: | |
232 | # Wait for any edge (rising or falling). | |
233 | (data,) = self.wait({0: 'e'}) | |
234 | self.pulse_width = self.samplenum - self.samplenum_prev_edge - 1 | |
235 | self.samplenum_prev_edge = self.samplenum | |
236 | ||
237 | if self.state == 'GET FIRST PULSE WIDTH': | |
238 | self.find_first_pulse_width() | |
239 | elif self.state == 'GET SECOND PULSE WIDTH': | |
240 | self.find_second_pulse_width() | |
241 | elif self.state == 'GET THIRD PULSE WIDTH': | |
242 | self.find_third_pulse_width() | |
243 | elif self.state == 'DECODE STREAM': | |
244 | self.decode_stream() | |
245 | elif self.state == 'DECODE PREAMBLE': | |
246 | self.decode_preamble() |