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