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