[FFmpeg-devel] [PATCH] ac3dec: fix non-optimal dithering of zero bit mantissas

madshi madshi at gmail.com
Sat Jan 5 11:37:55 CET 2013

Hey guys,

the latest revision of the (E-)AC3 spec says:

> The optimum scaling for the dither words is to take a
> uniform distribution of values between –1 and +1, and
> scale this by 0.707, resulting in a uniform distribution
> between +0.707 and –0.707

Currently the AC3 decoder applies a dithering of only +0.5 .. -0.5.
According to the spec this is "also acceptable", but it's not the "optimum
scaling". The effects of this non-optimal dithering are clearly visible in
frequency graphs. Basically high frequencies are lower in volume than they
should be. You can test this yourself by looking at the following files:


Load the WAV files into Audacity, then for each WAV in the Audacity menu
choose "analyze -> frequency analyzis". You can see that the "azid" and
"liba52" decoders have a better high frequency response than the "libav"
decoder. However, look at "libav_patched" which shows the results when
using the proper amount of dithering in the libav AC3 decoder.

Attached is a patch which modifies the AC3 decoder to apply the optimum

(av_lfg_get() does return values in the range 0..FFFFFFFF, correct?)

Best regards, Mathias.
-------------- next part --------------
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c
index ea4a218..a30b123 100644
--- a/libavcodec/ac3dec.c
+++ b/libavcodec/ac3dec.c
@@ -442,7 +442,7 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
         switch (bap) {
         case 0:
             if (dither)
-                mantissa = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
+                mantissa = (av_lfg_get(&s->dith_state) / 362) - 5932275;
                 mantissa = 0;

More information about the ffmpeg-devel mailing list