Personal tools

AC3Filter & SPDIF

From AC3Filter
(Redirected from Guides/ac3filter spdif)
Jump to: navigation, search

What is SPDIF? How do Windows and sound cards work with it? How to configure AC3Filter? Here you can find answers to these questions.


[edit] How sound cards handle SPDIF

[edit] 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:

sample rate bitrate
48000Hz 1.536mbps
44100Hz 1.411mbps
32000Hz 1.024mbps

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.

[edit] 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.

[edit] 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.

[edit] 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.

[edit] 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.

Uncompressed sound transmitted over SPDIF is always stereo!

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.

[edit] 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.

Sound with non-48kHz sample rate may have problems with SPDIF transmission!

[edit] 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:

When SPDIF transmission stops, sound card must do the following:

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.

[edit] 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!

[edit] How the filter handles SPDIF

[edit] 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.

[edit] 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.

[edit] SPDIF mode decision

How does filter decide which mode to use for a given track? Let us see the internal data flow diagram:

AC3Filter-Data flow.gif

These are two SPDIF decision points:

If all the checks passed, filter enables SPDIF passthrough mode.

If all checks passed, filter enables AC3 encode mode.

[edit] 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:

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.

[edit] 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:

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).

This option may force the filter to produce LOUD noise if used incorrectly!

[edit] 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.

[edit] 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).

[edit] 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

  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.

[edit] 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.

[edit] 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.

[edit] Details for programmers

Problem occurs with IDirectSoundBuffer::Stop() call:

IDirectSoundBuffer8 *ds_buf;

// [...] open, init, start playback.

// 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.

[edit] 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.

Using Disallow PCM output in SPDIF mode option may prevent the player from using AC3Filter. If an audio track of a movie cannot be SPDIF'ed, the player will not use AC3Filter. You have to disable Use SPDIF option to use AC3Filter with this movie and enable it back again if you want to watch another movie in SPDIF mode.