]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/ac3enc.c
regression checksums change cuz of motion estimation changes 10min ago
[ffmpeg] / libavcodec / ac3enc.c
index 30e3f87bda5cd96861099f0e6c3ed45290271401..641e919a6b53424ea785bd2d0d7b88c9964565fc 100644 (file)
@@ -1,50 +1,65 @@
 /*
  * The simplest AC3 encoder
- * Copyright (c) 2000 Gerard Lantau.
+ * Copyright (c) 2000 Fabrice Bellard.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <math.h>
+//#define DEBUG
+//#define DEBUG_BITALLOC
 #include "avcodec.h"
 
-#include "ac3enc.h"
-#include "ac3tab.h"
+#include "ac3.h"
+
+typedef struct AC3EncodeContext {
+    PutBitContext pb;
+    int nb_channels;
+    int nb_all_channels;
+    int lfe_channel;
+    int bit_rate;
+    int sample_rate;
+    int bsid;
+    int frame_size_min; /* minimum frame size in case rounding is necessary */
+    int frame_size; /* current frame size in words */
+    int halfratecod;
+    int frmsizecod;
+    int fscod; /* frequency */
+    int acmod;
+    int lfe;
+    int bsmod;
+    short last_samples[AC3_MAX_CHANNELS][256];
+    int chbwcod[AC3_MAX_CHANNELS];
+    int nb_coefs[AC3_MAX_CHANNELS];
+    
+    /* bitrate allocation control */
+    int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod; 
+    AC3BitAllocParameters bit_alloc;
+    int csnroffst;
+    int fgaincod[AC3_MAX_CHANNELS];
+    int fsnroffst[AC3_MAX_CHANNELS];
+    /* mantissa encoding */
+    int mant1_cnt, mant2_cnt, mant4_cnt;
+} AC3EncodeContext;
 
-//#define DEBUG
-//#define DEBUG_BITALLOC
-#define NDEBUG
-#include <assert.h>
+#include "ac3tab.h"
 
 #define MDCT_NBITS 9
 #define N         (1 << MDCT_NBITS)
-#define NB_BLOCKS 6 /* number of PCM blocks inside an AC3 frame */
 
 /* new exponents are sent if their Norm 1 exceed this number */
 #define EXP_DIFF_THRESHOLD 1000
 
-/* exponent encoding strategy */
-#define EXP_REUSE 0
-#define EXP_NEW   1
-
-#define EXP_D15   1
-#define EXP_D25   2
-#define EXP_D45   3
-
 static void fft_init(int ln);
 static void ac3_crc_init(void);
 
@@ -94,11 +109,12 @@ static inline int calc_lowcomp(int a, int b0, int b1, int bin)
 }
 
 /* AC3 bit allocation. The algorithm is the one described in the AC3
-   spec with some optimizations because of our simplified encoding
-   assumptions. */
-void parametric_bit_allocation(AC3EncodeContext *s, UINT8 *bap,
-                               INT8 *exp, int start, int end,
-                               int snroffset, int fgain)
+   spec. */
+void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, UINT8 *bap,
+                                   INT8 *exp, int start, int end,
+                                   int snroffset, int fgain, int is_lfe,
+                                   int deltbae,int deltnseg, 
+                                   UINT8 *deltoffst, UINT8 *deltlen, UINT8 *deltba)
 {
     int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
     int fastleak,slowleak,address,tmp;
@@ -144,44 +160,57 @@ void parametric_bit_allocation(AC3EncodeContext *s, UINT8 *bap,
     bndstrt = masktab[start];
     bndend = masktab[end-1] + 1;
     
-    lowcomp = 0;
-    lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ;
-    excite[0] = bndpsd[0] - fgain - lowcomp ;
-    lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ;
-    excite[1] = bndpsd[1] - fgain - lowcomp ;
-    begin = 7 ;
-    for (bin = 2; bin < 7; bin++) {
-        lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
-        fastleak = bndpsd[bin] - fgain ;
-        slowleak = bndpsd[bin] - s->sgain ;
-        excite[bin] = fastleak - lowcomp ;
-        if (bndpsd[bin] <= bndpsd[bin+1]) {
-            begin = bin + 1 ;
-            break ;
+    if (bndstrt == 0) {
+        lowcomp = 0;
+        lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ;
+        excite[0] = bndpsd[0] - fgain - lowcomp ;
+        lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ;
+        excite[1] = bndpsd[1] - fgain - lowcomp ;
+        begin = 7 ;
+        for (bin = 2; bin < 7; bin++) {
+            if (!(is_lfe && bin == 6))
+                lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
+            fastleak = bndpsd[bin] - fgain ;
+            slowleak = bndpsd[bin] - s->sgain ;
+            excite[bin] = fastleak - lowcomp ;
+            if (!(is_lfe && bin == 6)) {
+                if (bndpsd[bin] <= bndpsd[bin+1]) {
+                    begin = bin + 1 ;
+                    break ;
+                }
+            }
         }
-    }
     
-    end1=bndend;
-    if (end1 > 22) end1=22;
+        end1=bndend;
+        if (end1 > 22) end1=22;
     
-    for (bin = begin; bin < end1; bin++) {
-        lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
+        for (bin = begin; bin < end1; bin++) {
+            if (!(is_lfe && bin == 6))
+                lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
         
-        fastleak -= s->fdecay ;
-        v = bndpsd[bin] - fgain;
-        if (fastleak < v) fastleak = v;
+            fastleak -= s->fdecay ;
+            v = bndpsd[bin] - fgain;
+            if (fastleak < v) fastleak = v;
         
-        slowleak -= s->sdecay ;
-        v = bndpsd[bin] - s->sgain;
-        if (slowleak < v) slowleak = v;
+            slowleak -= s->sdecay ;
+            v = bndpsd[bin] - s->sgain;
+            if (slowleak < v) slowleak = v;
         
-        v=fastleak - lowcomp;
-        if (slowleak > v) v=slowleak;
+            v=fastleak - lowcomp;
+            if (slowleak > v) v=slowleak;
         
-        excite[bin] = v;
+            excite[bin] = v;
+        }
+        begin = 22;
+    } else {
+        /* coupling channel */
+        begin = bndstrt;
+        
+        fastleak = (s->cplfleak << 8) + 768;
+        slowleak = (s->cplsleak << 8) + 768;
     }
 
-    for (bin = 22; bin < bndend; bin++) {
+    for (bin = begin; bin < bndend; bin++) {
         fastleak -= s->fdecay ;
         v = bndpsd[bin] - fgain;
         if (fastleak < v) fastleak = v;
@@ -207,6 +236,25 @@ void parametric_bit_allocation(AC3EncodeContext *s, UINT8 *bap,
         mask[bin] = v;
     }
 
+    /* delta bit allocation */
+
+    if (deltbae == 0 || deltbae == 1) {
+        int band, seg, delta;
+        band = 0 ;
+        for (seg = 0; seg < deltnseg; seg++) {
+            band += deltoffst[seg] ;
+            if (deltba[seg] >= 4) {
+                delta = (deltba[seg] - 3) << 7;
+            } else {
+                delta = (deltba[seg] - 4) << 7;
+            }
+            for (k = 0; k < deltlen[seg]; k++) {
+                mask[band] += delta ;
+                band++ ;
+            }
+        }
+    }
+
     /* compute bit allocation */
     
     i = start ;
@@ -400,7 +448,7 @@ static int calc_exp_diff(UINT8 *exp1, UINT8 *exp2, int n)
 
 static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
                                  UINT8 exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
-                                 int ch)
+                                 int ch, int is_lfe)
 {
     int i, j;
     int exp_diff;
@@ -418,6 +466,9 @@ static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]
         else
             exp_strategy[i][ch] = EXP_REUSE;
     }
+    if (is_lfe)
+       return;
+
     /* now select the encoding strategy type : if exponents are often
        recoded, we use a coarse encoding */
     i = 0;
@@ -437,7 +488,7 @@ static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]
             exp_strategy[i][ch] = EXP_D15;
             break;
         }
-        i = j;
+       i = j;
     }
 }
 
@@ -598,12 +649,15 @@ static int bit_alloc(AC3EncodeContext *s,
         s->mant1_cnt = 0;
         s->mant2_cnt = 0;
         s->mant4_cnt = 0;
-        for(ch=0;ch<s->nb_channels;ch++) {
-            parametric_bit_allocation(s, bap[i][ch], (INT8 *)encoded_exp[i][ch], 
-                                      0, s->nb_coefs[ch], 
-                                      (((csnroffst-15) << 4) + 
-                                       fsnroffst) << 2, 
-                                      fgaintab[s->fgaincod[ch]]);
+        for(ch=0;ch<s->nb_all_channels;ch++) {
+            ac3_parametric_bit_allocation(&s->bit_alloc, 
+                                          bap[i][ch], (INT8 *)encoded_exp[i][ch], 
+                                          0, s->nb_coefs[ch], 
+                                          (((csnroffst-15) << 4) + 
+                                           fsnroffst) << 2, 
+                                          fgaintab[s->fgaincod[ch]],
+                                          ch == s->lfe_channel,
+                                          2, 0, NULL, NULL, NULL);
             frame_bits += compute_mantissa_size(s, bap[i][ch], 
                                                  s->nb_coefs[ch]);
         }
@@ -627,6 +681,7 @@ static int compute_bit_allocation(AC3EncodeContext *s,
     int i, ch;
     int csnroffst, fsnroffst;
     UINT8 bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
+    static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
 
     /* init default parameters */
     s->sdecaycod = 2;
@@ -634,30 +689,35 @@ static int compute_bit_allocation(AC3EncodeContext *s,
     s->sgaincod = 1;
     s->dbkneecod = 2;
     s->floorcod = 4;
-    for(ch=0;ch<s->nb_channels;ch++) 
+    for(ch=0;ch<s->nb_all_channels;ch++) 
         s->fgaincod[ch] = 4;
     
     /* compute real values */
-    s->sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod;
-    s->fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod;
-    s->sgain = sgaintab[s->sgaincod];
-    s->dbknee = dbkneetab[s->dbkneecod];
-    s->floor = floortab[s->floorcod];
-
+    s->bit_alloc.fscod = s->fscod;
+    s->bit_alloc.halfratecod = s->halfratecod;
+    s->bit_alloc.sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod;
+    s->bit_alloc.fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod;
+    s->bit_alloc.sgain = sgaintab[s->sgaincod];
+    s->bit_alloc.dbknee = dbkneetab[s->dbkneecod];
+    s->bit_alloc.floor = floortab[s->floorcod];
+    
     /* header size */
     frame_bits += 65;
-    if (s->acmod == 2)
-        frame_bits += 2;
+    // if (s->acmod == 2)
+    //    frame_bits += 2;
+    frame_bits += frame_bits_inc[s->acmod];
 
     /* audio blocks */
     for(i=0;i<NB_BLOCKS;i++) {
-        frame_bits += s->nb_channels * 2 + 2;
+        frame_bits += s->nb_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
         if (s->acmod == 2)
-            frame_bits++;
-        frame_bits += 2 * s->nb_channels;
+            frame_bits++; /* rematstr */
+        frame_bits += 2 * s->nb_channels; /* chexpstr[2] * c */
+       if (s->lfe)
+           frame_bits++; /* lfeexpstr */
         for(ch=0;ch<s->nb_channels;ch++) {
             if (exp_strategy[i][ch] != EXP_REUSE)
-                frame_bits += 6 + 2;
+                frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
         }
         frame_bits++; /* baie */
         frame_bits++; /* snr */
@@ -665,7 +725,10 @@ static int compute_bit_allocation(AC3EncodeContext *s,
     }
     frame_bits++; /* cplinu for block 0 */
     /* bit alloc info */
-    frame_bits += 2*4 + 3 + 6 + s->nb_channels * (4 + 3);
+    /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
+    /* csnroffset[6] */
+    /* (fsnoffset[4] + fgaincod[4]) * c */
+    frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3);
 
     /* CRC */
     frame_bits += 16;
@@ -675,11 +738,11 @@ static int compute_bit_allocation(AC3EncodeContext *s,
 
     csnroffst = s->csnroffst;
     while (csnroffst >= 0 && 
-           bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
-        csnroffst -= SNR_INC1;
+          bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
+       csnroffst -= SNR_INC1;
     if (csnroffst < 0) {
-        fprintf(stderr, "Error !!!\n");
-        return -1;
+       fprintf(stderr, "Yack, Error !!!\n");
+       return -1;
     }
     while ((csnroffst + SNR_INC1) <= 63 && 
            bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 
@@ -708,14 +771,14 @@ static int compute_bit_allocation(AC3EncodeContext *s,
     }
     
     s->csnroffst = csnroffst;
-    for(ch=0;ch<s->nb_channels;ch++)
+    for(ch=0;ch<s->nb_all_channels;ch++)
         s->fsnroffst[ch] = fsnroffst;
 #if defined(DEBUG_BITALLOC)
     {
         int j;
 
         for(i=0;i<6;i++) {
-            for(ch=0;ch<s->nb_channels;ch++) {
+            for(ch=0;ch<s->nb_all_channels;ch++) {
                 printf("Block #%d Ch%d:\n", i, ch);
                 printf("bap=");
                 for(j=0;j<s->nb_coefs[ch];j++) {
@@ -729,32 +792,54 @@ static int compute_bit_allocation(AC3EncodeContext *s,
     return 0;
 }
 
+void ac3_common_init(void)
+{
+    int i, j, k, l, v;
+    /* compute bndtab and masktab from bandsz */
+    k = 0;
+    l = 0;
+    for(i=0;i<50;i++) {
+        bndtab[i] = l;
+        v = bndsz[i];
+        for(j=0;j<v;j++) masktab[k++]=i;
+        l += v;
+    }
+    bndtab[50] = 0;
+}
+
+
 static int AC3_encode_init(AVCodecContext *avctx)
 {
     int freq = avctx->sample_rate;
     int bitrate = avctx->bit_rate;
     int channels = avctx->channels;
     AC3EncodeContext *s = avctx->priv_data;
-    int i, j, k, l, ch, v;
+    int i, j, ch;
     float alpha;
-    static unsigned short freqs[3] = { 48000, 44100, 32000 };
+    static const UINT8 acmod_defs[6] = {
+       0x01, /* C */
+       0x02, /* L R */
+       0x03, /* L C R */
+       0x06, /* L R SL SR */
+       0x07, /* L C R SL SR */
+       0x07, /* L C R SL SR (+LFE) */
+    };
 
     avctx->frame_size = AC3_FRAME_SIZE;
-    avctx->key_frame = 1; /* always key frame */
     
     /* number of channels */
-    if (channels == 1)
-        s->acmod = 1;
-    else if (channels == 2)
-        s->acmod = 2;
-    else
-        return -1;
-    s->nb_channels = channels;
+    if (channels < 1 || channels > 6)
+       return -1;
+    s->acmod = acmod_defs[channels - 1];
+    s->lfe = (channels == 6) ? 1 : 0;
+    s->nb_all_channels = channels;
+    s->nb_channels = channels > 5 ? 5 : channels;
+    s->lfe_channel = s->lfe ? 5 : -1;
 
     /* frequency */
     for(i=0;i<3;i++) {
         for(j=0;j<3;j++) 
-            if ((freqs[j] >> i) == freq)
+            if ((ac3_freqs[j] >> i) == freq)
                 goto found;
     }
     return -1;
@@ -768,7 +853,7 @@ static int AC3_encode_init(AVCodecContext *avctx)
     /* bitrate & frame size */
     bitrate /= 1000;
     for(i=0;i<19;i++) {
-        if ((bitratetab[i] >> s->halfratecod) == bitrate)
+        if ((ac3_bitratetab[i] >> s->halfratecod) == bitrate)
             break;
     }
     if (i == 19)
@@ -787,19 +872,13 @@ static int AC3_encode_init(AVCodecContext *avctx)
         s->chbwcod[ch] = 50; /* sample bandwidth as mpeg audio layer 2 table 0 */
         s->nb_coefs[ch] = ((s->chbwcod[ch] + 12) * 3) + 37;
     }
+    if (s->lfe) {
+       s->nb_coefs[s->lfe_channel] = 7; /* fixed */
+    }
     /* initial snr offset */
     s->csnroffst = 40;
 
-    /* compute bndtab and masktab from bandsz */
-    k = 0;
-    l = 0;
-    for(i=0;i<50;i++) {
-        bndtab[i] = l;
-        v = bndsz[i];
-        for(j=0;j<v;j++) masktab[k++]=i;
-        l += v;
-    }
-    bndtab[50] = 0;
+    ac3_common_init();
 
     /* mdct init */
     fft_init(MDCT_NBITS - 2);
@@ -810,6 +889,9 @@ static int AC3_encode_init(AVCodecContext *avctx)
     }
 
     ac3_crc_init();
+    
+    avctx->coded_frame= avcodec_alloc_frame();
+    avctx->coded_frame->key_frame= 1;
 
     return 0;
 }
@@ -826,10 +908,13 @@ static void output_frame_header(AC3EncodeContext *s, unsigned char *frame)
     put_bits(&s->pb, 5, s->bsid);
     put_bits(&s->pb, 3, s->bsmod);
     put_bits(&s->pb, 3, s->acmod);
-    if (s->acmod == 2) {
+    if ((s->acmod & 0x01) && s->acmod != 0x01)
+       put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */
+    if (s->acmod & 0x04)
+       put_bits(&s->pb, 2, 1); /* XXX -6 dB */
+    if (s->acmod == 0x02)
         put_bits(&s->pb, 2, 0); /* surround not indicated */
-    }
-    put_bits(&s->pb, 1, 0); /* no LFE */
+    put_bits(&s->pb, 1, s->lfe); /* LFE */
     put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */
     put_bits(&s->pb, 1, 0); /* no compression control word */
     put_bits(&s->pb, 1, 0); /* no lang code */
@@ -847,10 +932,12 @@ static inline int sym_quant(int c, int e, int levels)
     int v;
 
     if (c >= 0) {
-        v = (levels * (c << e)) >> 25;
+        v = (levels * (c << e)) >> 24;
+        v = (v + 1) >> 1;
         v = (levels >> 1) + v;
     } else {
-        v = (levels * ((-c) << e)) >> 25;
+        v = (levels * ((-c) << e)) >> 24;
+        v = (v + 1) >> 1;
         v = (levels >> 1) - v;
     }
     assert (v >= 0 && v < levels);
@@ -923,13 +1010,17 @@ static void output_audio_block(AC3EncodeContext *s,
         put_bits(&s->pb, 2, exp_strategy[ch]);
     }
     
+    if (s->lfe) {
+       put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
+    }
+
     for(ch=0;ch<s->nb_channels;ch++) {
         if (exp_strategy[ch] != EXP_REUSE)
             put_bits(&s->pb, 6, s->chbwcod[ch]);
     }
     
     /* exponents */
-    for (ch = 0; ch < s->nb_channels; ch++) {
+    for (ch = 0; ch < s->nb_all_channels; ch++) {
         switch(exp_strategy[ch]) {
         case EXP_REUSE:
             continue;
@@ -944,7 +1035,7 @@ static void output_audio_block(AC3EncodeContext *s,
             group_size = 4;
             break;
         }
-        nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
+       nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
         p = encoded_exp[ch];
 
         /* first exponent */
@@ -972,7 +1063,8 @@ static void output_audio_block(AC3EncodeContext *s,
             put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2);
         }
 
-        put_bits(&s->pb, 2, 0); /* no gain range info */
+       if (ch != s->lfe_channel)
+           put_bits(&s->pb, 2, 0); /* no gain range info */
     }
 
     /* bit allocation info */
@@ -990,7 +1082,7 @@ static void output_audio_block(AC3EncodeContext *s,
     put_bits(&s->pb, 1, baie); /* always present with bai */
     if (baie) {
         put_bits(&s->pb, 6, s->csnroffst);
-        for(ch=0;ch<s->nb_channels;ch++) {
+        for(ch=0;ch<s->nb_all_channels;ch++) {
             put_bits(&s->pb, 4, s->fsnroffst[ch]);
             put_bits(&s->pb, 3, s->fgaincod[ch]);
         }
@@ -1007,7 +1099,7 @@ static void output_audio_block(AC3EncodeContext *s,
     mant1_cnt = mant2_cnt = mant4_cnt = 0;
     qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL;
 
-    for (ch = 0; ch < s->nb_channels; ch++) {
+    for (ch = 0; ch < s->nb_all_channels; ch++) {
         int b, c, e, v;
 
         for(i=0;i<s->nb_coefs[ch];i++) {
@@ -1094,7 +1186,7 @@ static void output_audio_block(AC3EncodeContext *s,
     }
 
     /* second pass : output the values */
-    for (ch = 0; ch < s->nb_channels; ch++) {
+    for (ch = 0; ch < s->nb_all_channels; ch++) {
         int b, q;
         
         for(i=0;i<s->nb_coefs[ch];i++) {
@@ -1200,7 +1292,7 @@ static int log2_tab(INT16 *tab, int n)
     for(i=0;i<n;i++) {
         v |= abs(tab[i]);
     }
-    return log2(v);
+    return av_log2(v);
 }
 
 static void lshift_tab(INT16 *tab, int n, int lshift)
@@ -1230,9 +1322,9 @@ static int output_frame_end(AC3EncodeContext *s)
     flush_put_bits(&s->pb);
     /* add zero bytes to reach the frame size */
     frame = s->pb.buf;
-    n = 2 * s->frame_size - (s->pb.buf_ptr - frame) - 2;
+    n = 2 * s->frame_size - (pbBufPtr(&s->pb) - frame) - 2;
     assert(n >= 0);
-    memset(s->pb.buf_ptr, 0, n);
+    memset(pbBufPtr(&s->pb), 0, n);
     
     /* Now we must compute both crcs : this is not so easy for crc1
        because it is at the beginning of the data... */
@@ -1252,8 +1344,8 @@ static int output_frame_end(AC3EncodeContext *s)
     return frame_size * 2;
 }
 
-int AC3_encode_frame(AVCodecContext *avctx,
-                     unsigned char *frame, int buf_size, void *data)
+static int AC3_encode_frame(AVCodecContext *avctx,
+                            unsigned char *frame, int buf_size, void *data)
 {
     AC3EncodeContext *s = avctx->priv_data;
     short *samples = data;
@@ -1268,7 +1360,7 @@ int AC3_encode_frame(AVCodecContext *avctx,
     int frame_bits;
 
     frame_bits = 0;
-    for(ch=0;ch<s->nb_channels;ch++) {
+    for(ch=0;ch<s->nb_all_channels;ch++) {
         /* fixed mdct to the six sub blocks & exponent computation */
         for(i=0;i<NB_BLOCKS;i++) {
             INT16 *sptr;
@@ -1276,7 +1368,7 @@ int AC3_encode_frame(AVCodecContext *avctx,
 
             /* compute input samples */
             memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(INT16));
-            sinc = s->nb_channels;
+            sinc = s->nb_all_channels;
             sptr = samples + (sinc * (N/2) * i) + ch;
             for(j=0;j<N/2;j++) {
                 v = *sptr;
@@ -1312,7 +1404,7 @@ int AC3_encode_frame(AVCodecContext *avctx,
                 if (v == 0)
                     e = 24;
                 else {
-                    e = 23 - log2(v) + exp_samples[i][ch];
+                    e = 23 - av_log2(v) + exp_samples[i][ch];
                     if (e >= 24) {
                         e = 24;
                         mdct_coef[i][ch][j] = 0;
@@ -1322,7 +1414,7 @@ int AC3_encode_frame(AVCodecContext *avctx,
             }
         }
         
-        compute_exp_strategy(exp_strategy, exp, ch);
+        compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel);
 
         /* compute the exponents as the decoder will see them. The
            EXP_REUSE case must be handled carefully : we select the
@@ -1357,6 +1449,11 @@ int AC3_encode_frame(AVCodecContext *avctx,
     return output_frame_end(s);
 }
 
+static int AC3_encode_close(AVCodecContext *avctx)
+{
+    av_freep(&avctx->coded_frame);
+}
+
 #if 0
 /*************************************************************************/
 /* TEST */
@@ -1456,5 +1553,6 @@ AVCodec ac3_encoder = {
     sizeof(AC3EncodeContext),
     AC3_encode_init,
     AC3_encode_frame,
+    AC3_encode_close,
     NULL,
 };