]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/ac3enc.c
add "memory" to the clobber list we change memory so we need it, this also fixes...
[ffmpeg] / libavcodec / ac3enc.c
index 0f9badf5ec1283124eba85c0b2de1d95eb3250da..bac703c101e0496f5bc805079399b78ffa23c21d 100644 (file)
@@ -2,18 +2,20 @@
  * The simplest AC3 encoder
  * Copyright (c) 2000 Fabrice Bellard.
  *
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -25,6 +27,7 @@
 //#define DEBUG_BITALLOC
 #include "avcodec.h"
 #include "bitstream.h"
+#include "crc.h"
 #include "ac3.h"
 
 typedef struct AC3EncodeContext {
@@ -37,6 +40,8 @@ typedef struct AC3EncodeContext {
     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 */
+    unsigned int bits_written;
+    unsigned int samples_written;
     int halfratecod;
     unsigned int frmsizecod;
     unsigned int fscod; /* frequency */
@@ -66,7 +71,6 @@ typedef struct AC3EncodeContext {
 #define EXP_DIFF_THRESHOLD 1000
 
 static void fft_init(int ln);
-static void ac3_crc_init(void);
 
 static inline int16_t fix15(float a)
 {
@@ -739,7 +743,7 @@ static int compute_bit_allocation(AC3EncodeContext *s,
            bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
         csnroffst -= SNR_INC1;
     if (csnroffst < 0) {
-        av_log(NULL, AV_LOG_ERROR, "Yack, Error !!!\n");
+        av_log(NULL, AV_LOG_ERROR, "Bit allocation failed, try increasing the bitrate, -ab 384 for example!\n");
         return -1;
     }
     while ((csnroffst + SNR_INC1) <= 63 &&
@@ -802,7 +806,7 @@ void ac3_common_init(void)
         for(j=0;j<v;j++) masktab[k++]=i;
         l += v;
     }
-    bndtab[50] = 0;
+    bndtab[50] = l;
 }
 
 
@@ -859,7 +863,8 @@ static int AC3_encode_init(AVCodecContext *avctx)
     s->bit_rate = bitrate;
     s->frmsizecod = i << 1;
     s->frame_size_min = (bitrate * 1000 * AC3_FRAME_SIZE) / (freq * 16);
-    /* for now we do not handle fractional sizes */
+    s->bits_written = 0;
+    s->samples_written = 0;
     s->frame_size = s->frame_size_min;
 
     /* bit allocation init */
@@ -886,8 +891,6 @@ static int AC3_encode_init(AVCodecContext *avctx)
         xsin1[i] = fix15(-sin(alpha));
     }
 
-    ac3_crc_init();
-
     avctx->coded_frame= avcodec_alloc_frame();
     avctx->coded_frame->key_frame= 1;
 
@@ -1236,35 +1239,8 @@ static void output_audio_block(AC3EncodeContext *s,
     }
 }
 
-/* compute the ac3 crc */
-
 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
 
-static void ac3_crc_init(void)
-{
-    unsigned int c, n, k;
-
-    for(n=0;n<256;n++) {
-        c = n << 8;
-        for (k = 0; k < 8; k++) {
-            if (c & (1 << 15))
-                c = ((c << 1) & 0xffff) ^ (CRC16_POLY & 0xffff);
-            else
-                c = c << 1;
-        }
-        crc_table[n] = c;
-    }
-}
-
-static unsigned int ac3_crc(uint8_t *data, int n, unsigned int crc)
-{
-    int i;
-    for(i=0;i<n;i++) {
-        crc = (crc_table[data[i] ^ (crc >> 8)] ^ (crc << 8)) & 0xffff;
-    }
-    return crc;
-}
-
 static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
 {
     unsigned int c;
@@ -1342,14 +1318,14 @@ static int output_frame_end(AC3EncodeContext *s)
     /* Now we must compute both crcs : this is not so easy for crc1
        because it is at the beginning of the data... */
     frame_size_58 = (frame_size >> 1) + (frame_size >> 3);
-    crc1 = ac3_crc(frame + 4, (2 * frame_size_58) - 4, 0);
+    crc1 = bswap_16(av_crc(av_crc8005, 0, frame + 4, 2 * frame_size_58 - 4));
     /* XXX: could precompute crc_inv */
     crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY);
     crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
     frame[2] = crc1 >> 8;
     frame[3] = crc1;
 
-    crc2 = ac3_crc(frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2, 0);
+    crc2 = bswap_16(av_crc(av_crc8005, 0, frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2));
     frame[2*frame_size - 2] = crc2 >> 8;
     frame[2*frame_size - 1] = crc2;
 
@@ -1403,7 +1379,7 @@ static int AC3_encode_frame(AVCodecContext *avctx,
             v = 14 - log2_tab(input_samples, N);
             if (v < 0)
                 v = 0;
-            exp_samples[i][ch] = v - 8;
+            exp_samples[i][ch] = v - 9;
             lshift_tab(input_samples, N, v);
 
             /* do the MDCT */
@@ -1451,6 +1427,15 @@ static int AC3_encode_frame(AVCodecContext *avctx,
         }
     }
 
+    /* adjust for fractional frame sizes */
+    while(s->bits_written >= s->bit_rate*1000 && s->samples_written >= s->sample_rate) {
+        s->bits_written -= s->bit_rate*1000;
+        s->samples_written -= s->sample_rate;
+    }
+    s->frame_size = s->frame_size_min + (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate*1000);
+    s->bits_written += s->frame_size * 16;
+    s->samples_written += AC3_FRAME_SIZE;
+
     compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
     /* everything is known... let's output the frame */
     output_frame_header(s, frame);