[Ffmpeg-devel] ac3 encoder volume correction

Bill O'Shaughnessy bill.oshaughnessy
Tue Nov 21 06:20:48 CET 2006


Hello all,
  after encoding a normalized wave file, ac3enc always encoded it a half
the volume of the original wave.  After some experimentation I found
that Justing Ruggles fix did not go far enough.
I also fixed some multiplies that should be 32 bit multiplies before
being shifted right 15 bits.

With this fix ffmpeg encodes an ac3 file to identical volume of the
input source .wav file.

Good Luck,
  Bill O.

Here it is:
--- ac3enc.corg    2006-11-20 20:31:04.500000000 -0800
+++ ac3enc.c    2006-11-20 20:46:52.125000000 -0800
@@ -330,11 +330,12 @@
 }

 #define MUL16(a,b) ((a) * (b))
+#define MUL32(a,b) (int32_t)((int32_t)(a) * (int32_t)(b))

 #define CMUL(pre, pim, are, aim, bre, bim) \
 {\
-   pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\
-   pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;\
+   pre = (MUL32(are, bre) - MUL32(aim, bim)) >> 15;\
+   pim = (MUL32(are, bim) + MUL32(bre, aim)) >> 15;\
 }


@@ -1286,10 +1287,14 @@
 static void lshift_tab(int16_t *tab, int n, int lshift)
 {
     int i;
+    int32_t it;

     if (lshift > 0) {
         for(i=0;i<n;i++) {
-            tab[i] <<= lshift;
+            it = ((int32_t)tab[i]) << lshift;
+        if( it > 32767 ) it = 32767;
+        if( it < -32767 ) it = -32767;
+        tab[i] = it;
         }
     } else if (lshift < 0) {
         lshift = -lshift;
@@ -1368,9 +1373,9 @@

             /* apply the MDCT window */
             for(j=0;j<N/2;j++) {
-                input_samples[j] = MUL16(input_samples[j],
+                input_samples[j] = MUL32(input_samples[j],
                                          ac3_window[j]) >> 15;
-                input_samples[N-j-1] = MUL16(input_samples[N-j-1],
+                input_samples[N-j-1] = MUL32(input_samples[N-j-1],
                                              ac3_window[j]) >> 15;
             }

@@ -1379,7 +1384,7 @@
             v = 14 - log2_tab(input_samples, N);
             if (v < 0)
                 v = 0;
-            exp_samples[i][ch] = v - 9;
+            exp_samples[i][ch] = v - 10;
             lshift_tab(input_samples, N, v);

             /* do the MDCT */




More information about the ffmpeg-devel mailing list