]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/ac3enc.c
works with old gcc versions too
[ffmpeg] / libavcodec / ac3enc.c
index 641e919a6b53424ea785bd2d0d7b88c9964565fc..3e83fb1fcf3965d265af3d6ffb2a83a13a173023 100644 (file)
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
+
+/**
+ * @file ac3enc.c
+ * The simplest AC3 encoder.
+ */
 //#define DEBUG
 //#define DEBUG_BITALLOC
 #include "avcodec.h"
@@ -28,18 +33,18 @@ typedef struct AC3EncodeContext {
     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 */
+    unsigned int sample_rate;
+    unsigned int bsid;
+    unsigned int frame_size_min; /* minimum frame size in case rounding is necessary */
+    unsigned int frame_size; /* current frame size in words */
     int halfratecod;
-    int frmsizecod;
-    int fscod; /* frequency */
-    int acmod;
+    unsigned int frmsizecod;
+    unsigned int fscod; /* frequency */
+    unsigned int acmod;
     int lfe;
-    int bsmod;
+    unsigned int bsmod;
     short last_samples[AC3_MAX_CHANNELS][256];
-    int chbwcod[AC3_MAX_CHANNELS];
+    unsigned int chbwcod[AC3_MAX_CHANNELS];
     int nb_coefs[AC3_MAX_CHANNELS];
     
     /* bitrate allocation control */
@@ -63,7 +68,7 @@ typedef struct AC3EncodeContext {
 static void fft_init(int ln);
 static void ac3_crc_init(void);
 
-static inline INT16 fix15(float a)
+static inline int16_t fix15(float a)
 {
     int v;
     v = (int)(a * (float)(1 << 15));
@@ -110,18 +115,18 @@ 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. */
-void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, UINT8 *bap,
-                                   INT8 *exp, int start, int end,
+void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
+                                   int8_t *exp, int start, int end,
                                    int snroffset, int fgain, int is_lfe,
                                    int deltbae,int deltnseg, 
-                                   UINT8 *deltoffst, UINT8 *deltlen, UINT8 *deltba)
+                                   uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba)
 {
     int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
     int fastleak,slowleak,address,tmp;
-    INT16 psd[256]; /* scaled exponents */
-    INT16 bndpsd[50]; /* interpolated exponents */
-    INT16 excite[50]; /* excitation */
-    INT16 mask[50];   /* masking value */
+    int16_t psd[256]; /* scaled exponents */
+    int16_t bndpsd[50]; /* interpolated exponents */
+    int16_t excite[50]; /* excitation */
+    int16_t mask[50];   /* masking value */
 
     /* exponent mapping to PSD */
     for(bin=start;bin<end;bin++) {
@@ -404,10 +409,10 @@ static void fft(IComplex *z, int ln)
 }
 
 /* do a 512 point mdct */
-static void mdct512(INT32 *out, INT16 *in)
+static void mdct512(int32_t *out, int16_t *in)
 {
     int i, re, im, re1, im1;
-    INT16 rot[N]; 
+    int16_t rot[N]; 
     IComplex x[N/4];
 
     /* shift to simplify computations */
@@ -436,7 +441,7 @@ static void mdct512(INT32 *out, INT16 *in)
 }
 
 /* XXX: use another norm ? */
-static int calc_exp_diff(UINT8 *exp1, UINT8 *exp2, int n)
+static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n)
 {
     int sum, i;
     sum = 0;
@@ -446,8 +451,8 @@ static int calc_exp_diff(UINT8 *exp1, UINT8 *exp2, int n)
     return sum;
 }
 
-static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
-                                 UINT8 exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
+static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
+                                 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
                                  int ch, int is_lfe)
 {
     int i, j;
@@ -493,7 +498,7 @@ static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]
 }
 
 /* set exp[i] to min(exp[i], exp1[i]) */
-static void exponent_min(UINT8 exp[N/2], UINT8 exp1[N/2], int n)
+static void exponent_min(uint8_t exp[N/2], uint8_t exp1[N/2], int n)
 {
     int i;
 
@@ -505,13 +510,13 @@ static void exponent_min(UINT8 exp[N/2], UINT8 exp1[N/2], int n)
                                  
 /* update the exponents so that they are the ones the decoder will
    decode. Return the number of bits used to code the exponents */
-static int encode_exp(UINT8 encoded_exp[N/2], 
-                      UINT8 exp[N/2], 
+static int encode_exp(uint8_t encoded_exp[N/2], 
+                      uint8_t exp[N/2], 
                       int nb_exps,
                       int exp_strategy)
 {
     int group_size, nb_groups, i, j, k, recurse, exp_min, delta;
-    UINT8 exp1[N/2];
+    uint8_t exp1[N/2];
 
     switch(exp_strategy) {
     case EXP_D15:
@@ -586,7 +591,7 @@ static int encode_exp(UINT8 encoded_exp[N/2],
 }
 
 /* return the size in bits taken by the mantissa */
-int compute_mantissa_size(AC3EncodeContext *s, UINT8 *m, int nb_coefs)
+static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs)
 {
     int bits, mant, i;
 
@@ -637,9 +642,9 @@ int compute_mantissa_size(AC3EncodeContext *s, UINT8 *m, int nb_coefs)
 
 
 static int bit_alloc(AC3EncodeContext *s,
-                     UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
-                     UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
-                     UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
+                     uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
+                     uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
+                     uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
                      int frame_bits, int csnroffst, int fsnroffst)
 {
     int i, ch;
@@ -651,7 +656,7 @@ static int bit_alloc(AC3EncodeContext *s,
         s->mant4_cnt = 0;
         for(ch=0;ch<s->nb_all_channels;ch++) {
             ac3_parametric_bit_allocation(&s->bit_alloc, 
-                                          bap[i][ch], (INT8 *)encoded_exp[i][ch], 
+                                          bap[i][ch], (int8_t *)encoded_exp[i][ch], 
                                           0, s->nb_coefs[ch], 
                                           (((csnroffst-15) << 4) + 
                                            fsnroffst) << 2, 
@@ -673,14 +678,14 @@ static int bit_alloc(AC3EncodeContext *s,
 #define SNR_INC1 4
 
 static int compute_bit_allocation(AC3EncodeContext *s,
-                                  UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
-                                  UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
-                                  UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
+                                  uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
+                                  uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
+                                  uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
                                   int frame_bits)
 {
     int i, ch;
     int csnroffst, fsnroffst;
-    UINT8 bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
+    uint8_t 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 */
@@ -816,7 +821,7 @@ static int AC3_encode_init(AVCodecContext *avctx)
     AC3EncodeContext *s = avctx->priv_data;
     int i, j, ch;
     float alpha;
-    static const UINT8 acmod_defs[6] = {
+    static const uint8_t acmod_defs[6] = {
        0x01, /* C */
        0x02, /* L R */
        0x03, /* L C R */
@@ -966,19 +971,19 @@ static inline int asym_quant(int c, int e, int qbits)
 /* Output one audio block. There are NB_BLOCKS audio blocks in one AC3
    frame */
 static void output_audio_block(AC3EncodeContext *s,
-                               UINT8 exp_strategy[AC3_MAX_CHANNELS],
-                               UINT8 encoded_exp[AC3_MAX_CHANNELS][N/2],
-                               UINT8 bap[AC3_MAX_CHANNELS][N/2],
-                               INT32 mdct_coefs[AC3_MAX_CHANNELS][N/2],
-                               INT8 global_exp[AC3_MAX_CHANNELS],
+                               uint8_t exp_strategy[AC3_MAX_CHANNELS],
+                               uint8_t encoded_exp[AC3_MAX_CHANNELS][N/2],
+                               uint8_t bap[AC3_MAX_CHANNELS][N/2],
+                               int32_t mdct_coefs[AC3_MAX_CHANNELS][N/2],
+                               int8_t global_exp[AC3_MAX_CHANNELS],
                                int block_num)
 {
     int ch, nb_groups, group_size, i, baie;
-    UINT8 *p;
-    UINT16 qmant[AC3_MAX_CHANNELS][N/2];
+    uint8_t *p;
+    uint16_t qmant[AC3_MAX_CHANNELS][N/2];
     int exp0, exp1;
     int mant1_cnt, mant2_cnt, mant4_cnt;
-    UINT16 *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
+    uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
     int delta0, delta1, delta2;
 
     for(ch=0;ch<s->nb_channels;ch++) 
@@ -1244,7 +1249,7 @@ static void ac3_crc_init(void)
     }
 }
 
-static unsigned int ac3_crc(UINT8 *data, int n, unsigned int crc)
+static unsigned int ac3_crc(uint8_t *data, int n, unsigned int crc)
 {
     int i;
     for(i=0;i<n;i++) {
@@ -1284,7 +1289,7 @@ static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
 
 
 /* compute log2(max(abs(tab[]))) */
-static int log2_tab(INT16 *tab, int n)
+static int log2_tab(int16_t *tab, int n)
 {
     int i, v;
 
@@ -1295,7 +1300,7 @@ static int log2_tab(INT16 *tab, int n)
     return av_log2(v);
 }
 
-static void lshift_tab(INT16 *tab, int n, int lshift)
+static void lshift_tab(int16_t *tab, int n, int lshift)
 {
     int i;
 
@@ -1315,7 +1320,7 @@ static void lshift_tab(INT16 *tab, int n, int lshift)
 static int output_frame_end(AC3EncodeContext *s)
 {
     int frame_size, frame_size_58, n, crc1, crc2, crc_inv;
-    UINT8 *frame;
+    uint8_t *frame;
 
     frame_size = s->frame_size; /* frame size in words */
     /* align to 8 bits */
@@ -1350,24 +1355,24 @@ static int AC3_encode_frame(AVCodecContext *avctx,
     AC3EncodeContext *s = avctx->priv_data;
     short *samples = data;
     int i, j, k, v, ch;
-    INT16 input_samples[N];
-    INT32 mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
-    UINT8 exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
-    UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS];
-    UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
-    UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
-    INT8 exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS];
+    int16_t input_samples[N];
+    int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
+    uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
+    uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS];
+    uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
+    uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
+    int8_t exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS];
     int frame_bits;
 
     frame_bits = 0;
     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;
+            int16_t *sptr;
             int sinc;
 
             /* compute input samples */
-            memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(INT16));
+            memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(int16_t));
             sinc = s->nb_all_channels;
             sptr = samples + (sinc * (N/2) * i) + ch;
             for(j=0;j<N/2;j++) {
@@ -1432,7 +1437,7 @@ static int AC3_encode_frame(AVCodecContext *avctx,
             /* copy encoded exponents for reuse case */
             for(k=i+1;k<j;k++) {
                 memcpy(encoded_exp[k][ch], encoded_exp[i][ch], 
-                       s->nb_coefs[ch] * sizeof(UINT8));
+                       s->nb_coefs[ch] * sizeof(uint8_t));
             }
             i = j;
         }
@@ -1452,6 +1457,7 @@ static int AC3_encode_frame(AVCodecContext *avctx,
 static int AC3_encode_close(AVCodecContext *avctx)
 {
     av_freep(&avctx->coded_frame);
+    return 0;
 }
 
 #if 0
@@ -1491,8 +1497,8 @@ void fft_test(void)
 
 void mdct_test(void)
 {
-    INT16 input[N];
-    INT32 output[N/2];
+    int16_t input[N];
+    int32_t output[N/2];
     float input1[N];
     float output1[N/2];
     float s, a, err, e, emax;