AC3Filter & SPDIF
What is SPDIF? How do Windows and sound cards work with it? How to configure AC3Filter? Here you can find answers to these questions.
 How sound cards handle SPDIF
 General info about SPDIF
SPDIF (Sony/Phillips Digital Interconnect Format) is a digital audio interface. Initially, SPDIF was only used to transmit stereo 16-bit PCM data (CD player was the only source of digital audio). The scheme is quite linear: audio samples are transmitted at constant frequency, one-by-one (and bit-by-bit) in 32-bit blocks where 8 bits are used for synchronization and some side info, and 24 bits are used for a sample. In most cases, only 16-bit transmission is supported (CD contains only 16-bit sound) and low 8 bits of a sample are always zeroed. This interface is very simple and inexpensive, that is why it became so widespread.
Therefore, since home theaters stepped into the multi-channel sound era, SPDIF was a perfect candidate for digital interface to transmit multi-channel sound. However, there is one problem: current interface implementations only work with stereo sound, while nowadays we meet the necessity to transmit up to 6 channels (or 8 channels at present). It was decided not to change the interface, but to transmit compressed multi-channel sound instead of PCM data (digital interface it not particular about what to transmit). So receiver must recognize compressed data and decode it. For this purpose a new standard was introduced (IEC 61937) that describes how compressed data must be transmitted and how receivers can distinguish PCM and compressed data.
Since compressed data is transmitted in place of PCM data, the bitrate of the compressed stream must exactly match uncompressed stereo 16-bit PCM bitrate. As a rule, compressed stream (even a multi-channel one) having a lower bitrate, compressed stream must be padded with zeros to match PCM bitrate.
The required bitrate depends on the sample rate:
It is impossible to transmit a stream with a higher bitrate. For example, it is impossible to transmit 2mbps DTS over SPDIF.
So SPDIF interface has two modes: PCM data transmission mode and compressed data transmission mode. By default, we will hereinafter use the terms "SPDIF transmission", "SPDIF output mode" and "SPDIF stream" only for encoded streams.
 What is a multi-channel AudioCD
Compressed data may be transmitted over SPDIF instead of PCM data. Therefore, we can prepare a CD disc and place compressed AC3 or DTS data instead of ordinary PCM tracks. When we playback this disc with a CD player connected to a receiver, we will get true multi-channel sound!
But this trick does not work with analog connection and portable CD players: we will get nasty loud noise instead of nice music. Because of this CD discs with AC3/DTS tracks are relatively rare.
 What is 14bit DTS
As it was mentioned above, SPDIF data is transmitted instead of PCM data. Therefore, it may be mistakenly recognized as PCM and make harsh noise. To make this noise more ear-friendly, DTS stream may be converted from 16-bit to 14-bit format that reduces the loudness of the possible noise. This conversion is lossless and affects neither quality, nor loudness of the normal playback.
14bit format is usually used at multi-channel CD discs with DTS track (AC3 format does not allow such a conversion). Therefore, when a usual CD player plays it back it makes less noise.
 DTS over SPDIF
There are two ways to transmit DTS stream over SPDIF:
- DTS stream is wrapped according to IEC 61937 standard. Each DTS frame is supplemented with SPDIF header and padded with zeros to match SPDIF bitrate.
- DTS stream is only padded with zeros to match SPDIF bitrate.
Note, that it may be impossible to convert padded DTS stream to wrapped one, while vice versa conversion is always possible.
Multi-channel AudioCD usually use padded DTS stream type. Therefore, it may be impossible to decode it with a decoder that only supports wrapped stream type.
 How sound cards transmit PCM over SPDIF
As we remember, SPDIF can carry only two uncompressed channels. But what if some application starts multi-channel playback? Sound card downmixes multi-channel sound into two channels before sending it over SPDIF.
Some sound cards have several SPDIF outputs which can transmit multi-channel sound (3 SPDIF outputs can transmit 6 channels). But most receivers do not support this function (even receivers with several SPDIF inputs), so we will not consider this case afterwards.
Some sound cards can encode the multi-channel sound before sending it over SPDIF. But this option is rare and we will not consider this case.
 SPDIF sample rates
SPDIF interface supports three standard sample rates: 48 kHz, 44.1 kHz and 32 kHz. All other sample rates are impossible to transmit. Nevertheless, most audio cards support only 48 kHz (used at DVD). Therefore, sound with widely used 44100Hz sample rate may have problems with SPDIF transmission.
 SPDIF transmission monopoly
Sound cards allow simultaneous playback of multiple sounds. How does it work? All sounds from all applications are combined into one stream, sounds with different sample rates are resampled, and the final result is sent to one physical output. That means Windows works as if it can playback any sound at any time. But SPDIF transmission requires playback monopoly, because SPDIF stream is encoded and it cannot be resampled and mixed with other sounds. Therefore, only one SPDIF transmission at a time is possible. Also all uncompressed sound playbacks must be muted during this time.
When some application starts SPDIF transmission, sound card must do the following:
- mute all uncompressed sound playbacks
- open exclusive SPDIF output channel
- start transmission
- block any other application's trying to start SPDIF transmission
When SPDIF transmission stops, sound card must do the following:
- stop SPDIF transmission
- close SPDIF output channel
- restore all uncompressed output
- allow applications to start a new SPDIF transmission
For example, imagine that you have Winamp playing some music as a background. You start watching a movie with AC3 sound track in SPDIF mode. When a movie playback starts, sound card mutes the music from Winamp and gives exclusive playback right to the media player. Media player starts playback. Having received some data, the receiver recognizes compressed SPDIF transmission and changes indication from "PCM" to "Dolby Digital".
When you pause (not stop) the movie playback, sound card closes SPDIF transmission and restores music playback from Winamp. Receiver does not recognize any compressed transmission anymore and changes "Dolby Digital" back to "PCM".
But the exclusive right to SPDIF playback still belongs to the media player because it did not actually close SPDIF playback. So if you try to start another movie in SPDIF mode (without closing the current one) the player will fail to start a new SPDIF transmission and will start PCM (stereo!) playback. When you stop the first movie playback, the media player actually closes SPDIF output and exclusive SPDIF right sets free, so any other application can use it.
 Bug with switching between PCM and SPDIF
So, sound card handles switching between PCM and SPDIF output mode. But not all sound cards can do this correctly. When you pause SPDIF playback, sound card change SPDIF mode to PCM and may not restore SPDIF after playback resumes. In some cases, sound card may disable PCM mode without enabling SPDIF. In the last case, ALL sounds disappear!
 How the filter handles SPDIF
 SPDIF modes
SPDIF transmission is used only when Use SPDIF option is enabled. It will be implied afterwards.
There are three SPDIF output modes:
- SPDIF passthrough mode
- In this mode, compressed stream is sent to SPDIF without any change. It is impossible to process compressed stream without decompression. Therefore no other filter option can work in this mode (even filter cannot display input/output levels). We cannot even change the sound volume from the computer (only receiver's volume control works).
- SPDIF encode mode
- In this case, input stream is decoded, processed and encoded to AC3 that is sent over SPDIF. Since we have decoded stream in this case, all processing options work. We can change number of channels, volume, etc. before sending the result to the receiver. This allows any stream (even not directly supported by receiver or SPDIF at all) to be sent over SPDIF.
- Filter does not do SPDIF transmission.
 DTS over SPDIF passthrough
AC3Filter supports both of DTS output modes: wrapped and padded. You may set the desired mode with SPDIF/DTS output option. 'Auto' directs the filter to use wrapped format if possible, and padded otherwise.
It may be impossible to convert padded DTS stream to a wrapped one. So, if you set the filter to use the wrapped format, it may not enable DTS passthrough for some audio tracks (DTS from multi-channel AudioCD for instance) and use AC3 encode mode instead.
To reduce the possible noise level you can set the filter to convert DTS to 14bit format with Convert to 14bit option. This conversion increases the bitrate of the stream, and converted stream may not match SPDIF bitrate restriction. Therefore, the filter does this conversion only when possible.
Note, that receiver/decoder may not support some combinations of wrapped/padded 16-bit/14bit stream types.
 SPDIF mode decision
How does filter decide which mode to use for a given track? Let us see the internal data flow diagram:
These are two SPDIF decision points:
- SPDIF passthrough decision. At this point filter performs the following checks:
- Is given format supported by receiver and allowed for passthrough? This is controlled by SPDIF passthrough options. Only checked formats are allowed.
- Is a given sample rate allowed for SPDIF output? This is controlled by Restrict sample rates option. If restriction is enabled, the filter allows only checked sample rates. (See below for details)
- Does sound card support given SPDIF format? This check is only done when Check output format support option is enabled. It is not recommended to disable this check! (See below for details)
If all the checks passed, filter enables SPDIF passthrough mode.
- AC3 encode decision. At this point filter performs the following checks:
- Is encode allowed? This is controlled by Use AC3 encoder option.
- Do we need to encode this stream? It is an option Do not encode stereo PCM to inhibit encoding of stereo sound (See below for details)
- Can current stream be encoded? Not all channel configurations and sample rates are allowed for AC3.
- Is given sample rate allowed for SPDIF output?
- Does sound card support given SPDIF format?
If all checks passed, filter enables AC3 encode mode.
 SPDIF output support
When filter tries to open SPDIF output it first asks: "Dear sound card, would you please playback SPDIF with X sample rate?". If sound card permits, the filter starts an SPDIF transmission, if it doesn't, the filter disables SPDIF output.
The reasons for this dialogue's failure may be the following:
- Another application is using SPDIF.
- Sound card does not support SPDIF output at all.
- Driver does not support SPDIF output (USB sound cards).
- Driver does not support dynamical format change to/from SPDIF (SB Live 24bit).
- Sample rate is not supported for SPDIF.
The filter cannot guess why sound card refuses SPDIF transmission - it only can establish the fact of refusal. You can disable this dialogue with Check output format support option on the System page. But in most cases, you will just get no sound at all instead of correct PCM output (still better than nothing). So it is strongly not recommended to disable this option.
 SPDIF-as-PCM trick
Sound card tricking may solve problems number 3 and 4. Since SPDIF data is sent instead if PCM data we can trick the sound card and say that we want not to transmit PCM data, but send compressed stream instead. This is controlled by Output SPDIF as PCM option.
However, this trick works only when sound card does not alter our data and transmits it bit-by-bit directly to the SPDIF output:
- Volume settings in both sound card mixer and player must be at maximum level. Otherwise, sound card applies gain to our compressed stream and breaks it.
- It must be no other sound playing. Otherwise, sound card will mix PCM and compressed data what finally leads to destruction of compressed data structure. Therefore, you must disable any background music. Any sound during movie playback (like ICQ notifications or other things of that kind) will temporarily break normal playback and produce loud noise.
- Sound card must truly support the given sample rate. Many sound cards support only 48 kHz and do sample rate conversion for all sounds with other sample rates. Sample rate conversion like any other manipulation breaks compressed sound stream.
Filter cannot control the first two conditions so you must set volume and disable all background sounds manually. But filter may verify sample rate and prohibit SPDIF transmissions with incorrect sample rates (thus protecting you from the loud noise caused by broken compressed stream and eliminating the need to enable/disable SPDIF each time for movies with different sample rates).
 SPDIF sample rate check
SPDIF sample rate check is controlled by Restrict sample rates option. If this option is enabled, SPDIF transmission will be enabled only for streams with allowed sample rates. Generally, this option is required when SPDIF-as-PCM trick is used.
Suppose that we have a sound card that supports only 48 kHz. With SPDIF-as-PCM trick, we transmit compressed data in PCM output mode. Sound card agrees to playback sound of any sample rate, but it does sample rate conversion for "incorrect" sample rates. Therefore, movies with 48 kHz audio track will be played correctly and 44.1 kHz tracks will produce terrible noise. To fix this, we must enable SPDIF transmission only for "good" sample rates and do uncompressed output for any other sample rate.
This option is useful anyway. Without SPDIF-as-PCM trick, it forces the filter to report disallowed sample rate instead of simply notifying that sound card cannot make SPDIF transmission.
 Reasons not to encode stereo PCM
When encoding to AC3 loss in quality occurs (it is an true for any lossy encoding format like mp3, ac3, ogg, aac and others). It is the only way to transmit 6 channels over SPDIF with reasonable low distortions. But if we have just a stereo track, why do we need to encode it and lose quality? As it was said before, SPDIF interface was initially used to transmit stereo sound. Therefore, when we have a stereo track it is better not to encode it to AC3 but transmit it 'as is' without any quality loss.
Stereo sound encoding is controlled by Do not encode stereo PCM option. When this option is enabled, stereo output will not be encoded. Note that SPDIF status will be set to 'Disabled' even in case SPDIF is enabled and allowed. Sometimes it can be hard to understand, why it is so. Nevertheless, it is recommended to keep this option enabled.
This option affects only stereo output. Multi-channel output will be encoded anyway (unless Use AC3 encoder option is disabled).
 SPDIF status reporting
The "Decoder info" box displays processing information. If Use SPDIF checkbox is enabled, current SPDIF status with all SPDIF options is also displayed there. Example:
Input format: DTS - 44100 User format: PCM16 - 0 Output format: PCM16 3/2.1 (5.1) 44100 Use SPDIF SPDIF status: Disabled (Disallowed sample rate) SPDIF passthrough for: AC3 DTS Use AC3 encoder (do not encode stereo PCM) Check SPDIF sample rate (allow: 48kHz) Query for SPDIF output support
This means that we have 44.1 kHz DTS track at input. Current output format is PCM 5.1 (6 channels) 44.1 kHz. Use SPDIF is enabled but transmission was not set because sample rate is prohibited for SPDIF transmission. Below we can see that sample rate check is enabled and 48 kHz is the only allowed sample rate.
Let us sum up the reasons why SPDIF transmission may not be possible:
- Do not encode stereo PCM
- Filter is set to produce stereo output and Do not encode stereo PCM option is enabled.
- Disallowed sample rate
- Restrict SPDIF sample rate option is enabled and sample rate of the current track is not allowed for SPDIF transmission.
- SPDIF output is not supported
- Sound card refused to open SPDIF output channel. See SPDIF output support for more info.
- AC3 encoder disabled
- SPDIF passthrough is forbidden and Use AC3 encoder option is disabled.
 SPDIF pause bug
As it was said before, some sound cards have a pausing of SPDIF transmission bug. After pausing, sound card switches to PCM output mode and does not restore SPDIF transmission afterwards. In this case, sound card requires complete reinit (i.e. we need to close current audio playback and open it back again) after each pause/seek command. It is controlled by Force sound card to reinit after seek/pause option. Enable this option ONLY if your sound card has this bug because it breaks normal data flow in DirectShow and may not be compatible with some players.
 Technical details
It is not direct support for sound card reinit neither in DirectShow nor in any media player. But a solution does exist. Filter changes output format to PCM and sends several null samples. So sound card has to close current SPDIF output channel and open PCM output. After that, filter changes output format back to SPDIF and continues transmission from the point where it was stopped. Sound card has to open a new SPDIF output channel and it starts playback normally.
 Details for programmers
Problem occurs with IDirectSoundBuffer::Stop() call:
IDirectSoundBuffer8 *ds_buf; // [...] open, init, start playback. ds_buf->Stop(); // Now sound card switches to PCM mode // and restores all PCM sounds muted during SPDIF transmission. ds_buf->Play(0, 0, DSBPLAY_LOOPING); // Now sound card must continue SPDIF transmission. // But some cards with the bug do not do this...
It does not matter that Play() is called just after Stop() or after some time. Anyway after Stop() call SPDIF transmission stops forever.
 SPDIF and post-processing
Some players may use post-processing filters (equalizers, DRC, etc). Usually such filters do not support SPDIF because SPDIF stream cannot be processed at all.
Normally when Use SPDIF option is enabled, the filter publishes two output formats: SPDIF and PCM at the same time. This means that filter says: "I can do both PCM and SPDIF outputs, choose the one you can use, but remember that SPDIF is preferred". If your sound card does not support SPDIF, it can choose PCM and work properly when AC3Filter is used. In addition, if a movie contains sound track that cannot be SPDIF'ed on current sound card (for example 44100Hz track), AC3Filter will switch to PCM playback automatically.
Now let's consider a media player that does audio post-processing. If a player sees that AC3Filter can do PCM output, it decides to do post-processing and inserts post-processing filter after AC3Filter. When playback starts, AC3Filter asks next filter (which is a post-processing filter): "Could you accept SPDIF stream"? the post-processing filter refuses and AC3Fitler starts PCM playback and reports: "SPDIF output is not supported".
The best way to force SPDIF to work in this case is to disable ALL audio post-processing options of your player (equalizers, DRC, etc) or use another player. Some filters installed with super-mega-codec-packs may be used automatically. In this case we recommend to uninstall such filters.
But sometimes it's very hard to determine how to prevent the player from using the post-processing. SPDIF-as-PCM trick cannot solve the problem because the post-processing filter will certainly break the SPDIF stream. In this case it is possible for AC3Filter not to publish PCM when SPDIF is enabled with Disallow PCM output in SPDIF mode option. When this option is enabled the filter says that it supports only SPDIF output. Player cannot use post-processing filter after AC3Filter (because post-processing filter will refuse to work with SPDIF stream) and will be forced to connect AC3Filter directly to sound card renderer filter.