This file is indexed.

/usr/share/libsigrokdecode/decoders/spdif/pd.py is in libsigrokdecode4 0.5.0-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2014 Guenther Wenninger <robin@bitschubbser.org>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##

import sigrokdecode as srd

class SamplerateError(Exception):
    pass

class Decoder(srd.Decoder):
    api_version = 3
    id = 'spdif'
    name = 'S/PDIF'
    longname = 'Sony/Philips Digital Interface Format'
    desc = 'Serial bus for connecting digital audio devices.'
    license = 'gplv2+'
    inputs = ['logic']
    outputs = ['spdif']
    channels = (
        {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
    )
    annotations = (
        ('bitrate', 'Bitrate / baudrate'),
        ('preamble', 'Preamble'),
        ('bits', 'Bits'),
        ('aux', 'Auxillary-audio-databits'),
        ('samples', 'Audio Samples'),
        ('validity', 'Data Valid'),
        ('subcode', 'Subcode data'),
        ('chan_stat', 'Channnel Status'),
        ('parity', 'Parity Bit'),
    )
    annotation_rows = (
        ('info', 'Info', (0, 1, 3, 5, 6, 7, 8)),
        ('bits', 'Bits', (2,)),
        ('samples', 'Samples', (4,)),
    )

    def putx(self, ss, es, data):
        self.put(ss, es, self.out_ann, data)

    def puty(self, data):
        self.put(self.ss_edge, self.samplenum, self.out_ann, data)

    def __init__(self):
        self.state = 'GET FIRST PULSE WIDTH'
        self.ss_edge = None
        self.first_edge = True
        self.samplenum_prev_edge = 0
        self.pulse_width = 0

        self.clocks = []
        self.range1 = 0
        self.range2 = 0

        self.preamble_state = 0
        self.preamble = []
        self.seen_preamble = False
        self.last_preamble = 0

        self.first_one = True
        self.subframe = []

    def start(self):
        self.out_ann = self.register(srd.OUTPUT_ANN)

    def metadata(self, key, value):
        if key == srd.SRD_CONF_SAMPLERATE:
            self.samplerate = value

    def get_pulse_type(self):
        if self.range1 == 0 or self.range2 == 0:
            return -1
        if self.pulse_width >= self.range2:
            return 2
        elif self.pulse_width >= self.range1:
            return 0
        else:
            return 1

    def find_first_pulse_width(self):
        if self.pulse_width != 0:
            self.clocks.append(self.pulse_width)
            self.state = 'GET SECOND PULSE WIDTH'

    def find_second_pulse_width(self):
        if self.pulse_width > (self.clocks[0] * 1.3) or \
                self.pulse_width < (self.clocks[0] * 0.7):
            self.clocks.append(self.pulse_width)
            self.state = 'GET THIRD PULSE WIDTH'

    def find_third_pulse_width(self):
        if not ((self.pulse_width > (self.clocks[0] * 1.3) or \
                self.pulse_width < (self.clocks[0] * 0.7)) \
                and (self.pulse_width > (self.clocks[1] * 1.3) or \
                self.pulse_width < (self.clocks[1] * 0.7))):
            return

        self.clocks.append(self.pulse_width)
        self.clocks.sort()
        self.range1 = (self.clocks[0] + self.clocks[1]) / 2
        self.range2 = (self.clocks[1] + self.clocks[2]) / 2
        spdif_bitrate = int(self.samplerate / (self.clocks[2] / 1.5))
        self.ss_edge = 0

        self.puty([0, ['Signal Bitrate: %d Mbit/s (=> %d kHz)' % \
                  (spdif_bitrate, (spdif_bitrate/ (2 * 32)))]])

        clock_period_nsec = 1000000000 / spdif_bitrate

        self.last_preamble = self.samplenum

        # We are done recovering the clock, now let's decode the data stream.
        self.state = 'DECODE STREAM'

    def decode_stream(self):
        pulse = self.get_pulse_type()

        if not self.seen_preamble:
            # This is probably the start of a preamble, decode it.
            if pulse == 2:
                self.preamble.append(self.get_pulse_type())
                self.state = 'DECODE PREAMBLE'
                self.ss_edge = self.samplenum - self.pulse_width - 1
            return

        # We've seen a preamble.
        if pulse == 1 and self.first_one:
            self.first_one = False
            self.subframe.append([pulse, self.samplenum - \
                self.pulse_width - 1, self.samplenum])
        elif pulse == 1 and not self.first_one:
            self.subframe[-1][2] = self.samplenum
            self.putx(self.subframe[-1][1], self.samplenum, [2, ['1']])
            self.bitcount += 1
            self.first_one = True
        else:
            self.subframe.append([pulse, self.samplenum - \
                self.pulse_width - 1, self.samplenum])
            self.putx(self.samplenum - self.pulse_width - 1,
                      self.samplenum, [2, ['0']])
            self.bitcount += 1

        if self.bitcount == 28:
            aux_audio_data = self.subframe[0:4]
            sam, sam_rot = '', ''
            for a in aux_audio_data:
                sam = sam + str(a[0])
                sam_rot = str(a[0]) + sam_rot
            sample = self.subframe[4:24]
            for s in sample:
                sam = sam + str(s[0])
                sam_rot = str(s[0]) + sam_rot
            validity = self.subframe[24:25]
            subcode_data = self.subframe[25:26]
            channel_status = self.subframe[26:27]
            parity = self.subframe[27:28]

            self.putx(aux_audio_data[0][1], aux_audio_data[3][2], \
                      [3, ['Aux 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]])
            self.putx(sample[0][1], sample[19][2], \
                      [3, ['Sample 0x%x' % int(sam, 2), '0x%x' % int(sam, 2)]])
            self.putx(aux_audio_data[0][1], sample[19][2], \
                      [4, ['Audio 0x%x' % int(sam_rot, 2), '0x%x' % int(sam_rot, 2)]])
            if validity[0][0] == 0:
                self.putx(validity[0][1], validity[0][2], [5, ['V']])
            else:
                self.putx(validity[0][1], validity[0][2], [5, ['E']])
            self.putx(subcode_data[0][1], subcode_data[0][2],
                [6, ['S: %d' % subcode_data[0][0]]])
            self.putx(channel_status[0][1], channel_status[0][2],
                [7, ['C: %d' % channel_status[0][0]]])
            self.putx(parity[0][1], parity[0][2], [8, ['P: %d' % parity[0][0]]])

            self.subframe = []
            self.seen_preamble = False
            self.bitcount = 0

    def decode_preamble(self):
        if self.preamble_state == 0:
            self.preamble.append(self.get_pulse_type())
            self.preamble_state = 1
        elif self.preamble_state == 1:
            self.preamble.append(self.get_pulse_type())
            self.preamble_state = 2
        elif self.preamble_state == 2:
            self.preamble.append(self.get_pulse_type())
            self.preamble_state = 0
            self.state = 'DECODE STREAM'
            if self.preamble == [2, 0, 1, 0]:
                self.puty([1, ['Preamble W', 'W']])
            elif self.preamble == [2, 2, 1, 1]:
                self.puty([1, ['Preamble M', 'M']])
            elif self.preamble == [2, 1, 1, 2]:
                self.puty([1, ['Preamble B', 'B']])
            else:
                self.puty([1, ['Unknown Preamble', 'Unknown Prea.', 'U']])
            self.preamble = []
            self.seen_preamble = True
            self.bitcount = 0
            self.first_one = True

        self.last_preamble = self.samplenum

    def decode(self):
        if not self.samplerate:
            raise SamplerateError('Cannot decode without samplerate.')

        # Throw away first detected edge as it might be mangled data.
        self.wait({0: 'e'})

        while True:
            # Wait for any edge (rising or falling).
            (data,) = self.wait({0: 'e'})
            self.pulse_width = self.samplenum - self.samplenum_prev_edge - 1
            self.samplenum_prev_edge = self.samplenum

            if self.state == 'GET FIRST PULSE WIDTH':
                self.find_first_pulse_width()
            elif self.state == 'GET SECOND PULSE WIDTH':
                self.find_second_pulse_width()
            elif self.state == 'GET THIRD PULSE WIDTH':
                self.find_third_pulse_width()
            elif self.state == 'DECODE STREAM':
                self.decode_stream()
            elif self.state == 'DECODE PREAMBLE':
                self.decode_preamble()