The details about the S/PDIF protocol are standardized worldwide as IEC-60958. Unfortunately the IEC asks ridiculous amounts of money (hundreds of dollars) for a copy of the standard. As a hobbyist, I don't have wads of cash that big lying around.

The good news is that the Bureau of Indian Standard makes their own version of the standard available for non-commercial purposes, and it can be found at:

In this project, I want to find a way to decode the information from Part 3 (I don't have any devices that use part 4), but before I can get there, I have to also implement hardware and software to implement part 1.

There are many descriptions of the S/PDIF standard online such as this one: http://www.epanorama.net/documents/audio/spdif.html. It has also been described many times in magazines such as Elektor. The following is a description in my own words of the most important parts of the S/PDIF standard, based on the information in the BIS documents.

Scope

In this project, I will only consider PCM-encoded stereo audio (e.g. from a CD player), at 48,000 or 44,100 or 32,000 samples per second, 16 bits per sample. The IEC-60958 standard allows more channels, compressed audio, different sample frequencies and up to 24 bits per sample, but those are not interesting and the Propeller probably can't keep up with them anyway. Basically for my purpose I want to be able to handle eveything that a CD player or DAT recorder can generate.

Biphase Encoding

In order to decode S/PDIF and analyze the subchannel data, we need to know the following. Note: when a list of bits is given, they are shown in chronological order which, in this standard is usually LEAST significant bit first. So "0100" means four bits with decimal value 2.

Subframes and Frames

Audio samples are broken up into frames and subframes. A frame is an audio sample for multiple channels (for stereo: left and right), all of which are played at the same time. On the S/PDIF connection, the subframes for the left and right channel are interleaved, with the left subframe transmitted before the right subframe and any further subframes. When a receiver sees a left subframe coming in, it knows that it has received all the subframes for the previous frame (the left subframe is the start of the new frame) and plays all those previous subframes simultaneously while it receives the new left channel subframe.

Encoding of a Subframe

Regardless of the number of bits in an audio sample, a subframe always has 32 bits, numbered 0-31. Bit 0 is transmitted and received first.

Preambles

As I mentioned, each frame consists of a number of subframes (one for each channel, transmitted in the same order every time, left channel first). Frames are grouped together in blocks of 192 frames (384 subframes when operating in stereo), which is necessary for decoding the subchannel data (more information about subchannels later).

In order to recognize the function of each subframe, the standard defines three different preambles: B, M and W:

Subchannels

Each subframe has a User Data bit and a Channel Status bit, as mentioned. These are used as auxiliary data streams of one bit per subframe per subchannel. For synchronization, the B preamble (which marks the first left-channel subframe of a block) is used to mark the first bit in the stream of subchannel data.

The User Data subchannel bits can differ from one subframe to another, so there are 384 User Data subchannel bits in a block. More information about their interpretation follows below.

The Channel Status subchannel only changes at the start of each frame; in other words there are 192 Channel Status bits per block. More information about their interpretation follows below.

User Data Subchannel

Bit 29 of each subframe is the User Data bit. The User Data Bit may change for each subframe so there are up to 384 significant bits (numbered 0-383) in a block, which together form the User Data subchannel.

Channel Status Subchannel

Bit 30 of each subframe is the Channel Status bit. The channel status bits are always the same between the subframes of a frame, so there are 192 bits per block (numbered 0-191) that make up the Channel Status subchannel.

Conclusion

The above is enough information to get started on decoding biphase data into regular bits, subchannels into interesting data, and (if you want) play some music while you're doing all that.