[FFmpeg-cvslog] dcaenc: update

Paul B Mahol git at videolan.org
Thu Apr 25 16:34:58 CEST 2013


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Wed Apr 24 09:41:36 2013 +0000| [e1ba5fc96838fdadec2c36820c44af54bd8386eb] | committer: Paul B Mahol

dcaenc: update

Long story short: previous code was useless and was port of older
dcaenc, this commit just "sync" with current dcaenc, hopefuly
making this encoder more useful.

Signed-off-by: Paul B Mahol <onemda at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=e1ba5fc96838fdadec2c36820c44af54bd8386eb
---

 libavcodec/dcaenc.c | 1079 ++++++++++++++++++++++++++++++++++-----------------
 libavcodec/dcaenc.h |  601 ++++------------------------
 2 files changed, 807 insertions(+), 873 deletions(-)

diff --git a/libavcodec/dcaenc.c b/libavcodec/dcaenc.c
index 4799ef4..d2862b1 100644
--- a/libavcodec/dcaenc.c
+++ b/libavcodec/dcaenc.c
@@ -1,6 +1,6 @@
 /*
  * DCA encoder
- * Copyright (C) 2008 Alexander E. Patrakov
+ * Copyright (C) 2008-2012 Alexander E. Patrakov
  *               2010 Benjamin Larsson
  *               2011 Xiang Wang
  *
@@ -21,211 +21,678 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
-#include "libavutil/avassert.h"
 #include "avcodec.h"
+#include "dca.h"
+#include "dcadata.h"
+#include "dcaenc.h"
 #include "internal.h"
 #include "put_bits.h"
-#include "dcaenc.h"
-#include "dcadata.h"
-#include "dca.h"
-
-#undef NDEBUG
 
 #define MAX_CHANNELS 6
-#define DCA_SUBBANDS_32 32
-#define DCA_MAX_FRAME_SIZE 16383
+#define DCA_MAX_FRAME_SIZE 16384
 #define DCA_HEADER_SIZE 13
+#define DCA_LFE_SAMPLES 8
 
-#define DCA_SUBBANDS 32 ///< Subband activity count
-#define QUANTIZER_BITS 16
+#define DCA_SUBBANDS 32
 #define SUBFRAMES 1
-#define SUBSUBFRAMES 4
-#define PCM_SAMPLES (SUBFRAMES*SUBSUBFRAMES*8)
-#define LFE_BITS 8
-#define LFE_INTERPOLATION 64
-#define LFE_PRESENT 2
-#define LFE_MISSING 0
-
-static const int8_t dca_lfe_index[] = {
-    1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
-};
-
-static const int8_t dca_channel_reorder_lfe[][9] = {
-    { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
-    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
-    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
-    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
-    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
-    { 1,  2,  0, -1, -1, -1, -1, -1, -1 },
-    { 0,  1, -1,  2, -1, -1, -1, -1, -1 },
-    { 1,  2,  0, -1,  3, -1, -1, -1, -1 },
-    { 0,  1, -1,  2,  3, -1, -1, -1, -1 },
-    { 1,  2,  0, -1,  3,  4, -1, -1, -1 },
-    { 2,  3, -1,  0,  1,  4,  5, -1, -1 },
-    { 1,  2,  0, -1,  3,  4,  5, -1, -1 },
-    { 0, -1,  4,  5,  2,  3,  1, -1, -1 },
-    { 3,  4,  1, -1,  0,  2,  5,  6, -1 },
-    { 2,  3, -1,  5,  7,  0,  1,  4,  6 },
-    { 3,  4,  1, -1,  0,  2,  5,  7,  6 },
-};
+#define SUBSUBFRAMES 2
+#define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
+#define AUBANDS 25
 
-static const int8_t dca_channel_reorder_nolfe[][9] = {
-    { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
-    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
-    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
-    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
-    { 0,  1, -1, -1, -1, -1, -1, -1, -1 },
-    { 1,  2,  0, -1, -1, -1, -1, -1, -1 },
-    { 0,  1,  2, -1, -1, -1, -1, -1, -1 },
-    { 1,  2,  0,  3, -1, -1, -1, -1, -1 },
-    { 0,  1,  2,  3, -1, -1, -1, -1, -1 },
-    { 1,  2,  0,  3,  4, -1, -1, -1, -1 },
-    { 2,  3,  0,  1,  4,  5, -1, -1, -1 },
-    { 1,  2,  0,  3,  4,  5, -1, -1, -1 },
-    { 0,  4,  5,  2,  3,  1, -1, -1, -1 },
-    { 3,  4,  1,  0,  2,  5,  6, -1, -1 },
-    { 2,  3,  5,  7,  0,  1,  4,  6, -1 },
-    { 3,  4,  1,  0,  2,  5,  7,  6, -1 },
-};
-
-typedef struct {
+typedef struct DCAContext {
     PutBitContext pb;
-    int32_t history[MAX_CHANNELS][512]; /* This is a circular buffer */
-    int start[MAX_CHANNELS];
     int frame_size;
-    int prim_channels;
+    int frame_bits;
+    int fullband_channels;
+    int channels;
     int lfe_channel;
-    int sample_rate_code;
-    int scale_factor[MAX_CHANNELS][DCA_SUBBANDS_32];
+    int samplerate_index;
+    int bitrate_index;
+    int channel_config;
+    const int32_t *band_interpolation;
+    const int32_t *band_spectrum;
     int lfe_scale_factor;
-    int lfe_data[SUBFRAMES*SUBSUBFRAMES*4];
+    softfloat lfe_quant;
+    int32_t lfe_peak_cb;
+
+    int32_t history[512][MAX_CHANNELS]; /* This is a circular buffer */
+    int32_t subband[SUBBAND_SAMPLES][DCA_SUBBANDS][MAX_CHANNELS];
+    int32_t quantized[SUBBAND_SAMPLES][DCA_SUBBANDS][MAX_CHANNELS];
+    int32_t peak_cb[DCA_SUBBANDS][MAX_CHANNELS];
+    int32_t downsampled_lfe[DCA_LFE_SAMPLES];
+    int32_t masking_curve_cb[SUBSUBFRAMES][256];
+    int abits[DCA_SUBBANDS][MAX_CHANNELS];
+    int scale_factor[DCA_SUBBANDS][MAX_CHANNELS];
+    softfloat quant[DCA_SUBBANDS][MAX_CHANNELS];
+    int32_t eff_masking_curve_cb[256];
+    int32_t band_masking_cb[32];
+    int32_t worst_quantization_noise;
+    int32_t worst_noise_ever;
+    int consumed_bits;
+} DCAContext;
 
-    int a_mode;                         ///< audio channels arrangement
-    int num_channel;
-    int lfe_state;
-    int lfe_offset;
-    const int8_t *channel_order_tab;    ///< channel reordering table, lfe and non lfe
+static int32_t cos_table[2048];
+static int32_t band_interpolation[2][512];
+static int32_t band_spectrum[2][8];
+static int32_t auf[9][AUBANDS][256];
+static int32_t cb_to_add[256];
+static int32_t cb_to_level[2048];
+static int32_t lfe_fir_64i[512];
 
-    int32_t pcm[FFMAX(LFE_INTERPOLATION, DCA_SUBBANDS_32)];
-    int32_t subband[PCM_SAMPLES][MAX_CHANNELS][DCA_SUBBANDS_32]; /* [sample][channel][subband] */
-} DCAContext;
+/* Transfer function of outer and middle ear, Hz -> dB */
+static double hom(double f)
+{
+    double f1 = f / 1000;
+
+    return -3.64 * pow(f1, -0.8)
+           + 6.8 * exp(-0.6 * (f1 - 3.4) * (f1 - 3.4))
+           - 6.0 * exp(-0.15 * (f1 - 8.7) * (f1 - 8.7))
+           - 0.0006 * (f1 * f1) * (f1 * f1);
+}
+
+static double gammafilter(int i, double f)
+{
+    double h = (f - fc[i]) / erb[i];
+
+    h = 1 + h * h;
+    h = 1 / (h * h);
+    return 20 * log10(h);
+}
+
+static int encode_init(AVCodecContext *avctx)
+{
+    DCAContext *c = avctx->priv_data;
+    uint64_t layout = avctx->channel_layout;
+    int i, min_frame_bits;
+
+    c->fullband_channels = c->channels = avctx->channels;
+    c->lfe_channel = (avctx->channels == 3 || avctx->channels == 6);
+    c->band_interpolation = band_interpolation[1];
+    c->band_spectrum = band_spectrum[1];
+    c->worst_quantization_noise = -2047;
+    c->worst_noise_ever = -2047;
+
+    if (!layout) {
+        av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
+                                      "encoder will guess the layout, but it "
+                                      "might be incorrect.\n");
+        layout = av_get_default_channel_layout(avctx->channels);
+    }
+    switch (layout) {
+    case AV_CH_LAYOUT_MONO:         c->channel_config = 0; break;
+    case AV_CH_LAYOUT_STEREO:       c->channel_config = 2; break;
+    case AV_CH_LAYOUT_2_2:          c->channel_config = 8; break;
+    case AV_CH_LAYOUT_5POINT0:      c->channel_config = 9; break;
+    case AV_CH_LAYOUT_5POINT1:      c->channel_config = 9; break;
+    default:
+        av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout!\n");
+        return AVERROR_PATCHWELCOME;
+    }
+
+    if (c->lfe_channel)
+        c->fullband_channels--;
+
+    for (i = 0; i < 9; i++) {
+        if (sample_rates[i] == avctx->sample_rate)
+            break;
+    }
+    if (i == 9)
+        return AVERROR(EINVAL);
+    c->samplerate_index = i;
+
+    if (avctx->bit_rate < 32000 || avctx->bit_rate > 3840000) {
+        av_log(avctx, AV_LOG_ERROR, "Bit rate %i not supported.", avctx->bit_rate);
+        return AVERROR(EINVAL);
+    }
+    for (i = 0; dca_bit_rates[i] < avctx->bit_rate; i++)
+        ;
+    c->bitrate_index = i;
+    avctx->bit_rate = dca_bit_rates[i];
+    c->frame_bits = FFALIGN((avctx->bit_rate * 512 + avctx->sample_rate - 1) / avctx->sample_rate, 32);
+    min_frame_bits = 132 + (493 + 28 * 32) * c->fullband_channels + c->lfe_channel * 72;
+    if (c->frame_bits < min_frame_bits || c->frame_bits > (DCA_MAX_FRAME_SIZE << 3))
+        return AVERROR(EINVAL);
+
+    c->frame_size = (c->frame_bits + 7) / 8;
+
+    avctx->frame_size = 32 * SUBBAND_SAMPLES;
+
+    if (!cos_table[0]) {
+        int j, k;
+
+        for (i = 0; i < 2048; i++) {
+            cos_table[i]   = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
+            cb_to_level[i] = (int32_t)(0x7fffffff * pow(10, -0.005 * i));
+        }
+
+        for (i = 0; i < 512; i++) {
+            lfe_fir_64i[i]           = (int32_t)(0x01ffffff * lfe_fir_64[i]);
+            band_interpolation[0][i] = (int32_t)(0x1000000000ULL * fir_32bands_perfect[i]);
+            band_interpolation[1][i] = (int32_t)(0x1000000000ULL * fir_32bands_nonperfect[i]);
+        }
+
+        for (i = 0; i < 9; i++) {
+            for (j = 0; j < AUBANDS; j++) {
+                for (k = 0; k < 256; k++) {
+                    double freq = sample_rates[i] * (k + 0.5) / 512;
+
+                    auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
+                }
+            }
+        }
+
+        for (i = 0; i < 256; i++) {
+            double add = 1 + pow(10, -0.01 * i);
+            cb_to_add[i] = (int32_t)(100 * log10(add));
+        }
+        for (j = 0; j < 8; j++) {
+            double accum = 0;
+            for (i = 0; i < 512; i++) {
+                double reconst = fir_32bands_perfect[i] * ((i & 64) ? (-1) : 1);
+                accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
+            }
+            band_spectrum[0][j] = (int32_t)(200 * log10(accum));
+        }
+        for (j = 0; j < 8; j++) {
+            double accum = 0;
+            for (i = 0; i < 512; i++) {
+                double reconst = fir_32bands_nonperfect[i] * ((i & 64) ? (-1) : 1);
+                accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
+            }
+            band_spectrum[1][j] = (int32_t)(200 * log10(accum));
+        }
+    }
+    return 0;
+}
+
+static inline int32_t cos_t(int x)
+{
+    return cos_table[x & 2047];
+}
+
+static inline int32_t sin_t(int x)
+{
+    return cos_t(x - 512);
+}
 
-static int32_t cos_table[128];
+static inline int32_t half32(int32_t a)
+{
+    return (a + 1) >> 1;
+}
 
 static inline int32_t mul32(int32_t a, int32_t b)
 {
-    int64_t r = (int64_t) a * b;
-    /* round the result before truncating - improves accuracy */
-    return (r + 0x80000000) >> 32;
+    int64_t r = (int64_t)a * b + 0x80000000ULL;
+    return r >> 32;
+}
+
+static void subband_transform(DCAContext *c, const int32_t *input)
+{
+    int ch, subs, i, k, j;
+
+    for (ch = 0; ch < c->fullband_channels; ch++) {
+        /* History is copied because it is also needed for PSY */
+        int32_t hist[512];
+        int hist_start = 0;
+
+        for (i = 0; i < 512; i++)
+            hist[i] = c->history[i][ch];
+
+        for (subs = 0; subs < SUBBAND_SAMPLES; subs++) {
+            int32_t accum[64];
+            int32_t resp;
+            int band;
+
+            /* Calculate the convolutions at once */
+            for (i = 0; i < 64; i++)
+                accum[i] = 0;
+
+            for (k = 0, i = hist_start, j = 0;
+                    i < 512; k = (k + 1) & 63, i++, j++)
+                accum[k] += mul32(hist[i], c->band_interpolation[j]);
+            for (i = 0; i < hist_start; k = (k + 1) & 63, i++, j++)
+                accum[k] += mul32(hist[i], c->band_interpolation[j]);
+
+            for (k = 16; k < 32; k++)
+                accum[k] = accum[k] - accum[31 - k];
+            for (k = 32; k < 48; k++)
+                accum[k] = accum[k] + accum[95 - k];
+
+            for (band = 0; band < 32; band++) {
+                resp = 0;
+                for (i = 16; i < 48; i++) {
+                    int s = (2 * band + 1) * (2 * (i + 16) + 1);
+                    resp += mul32(accum[i], cos_t(s << 3)) >> 3;
+                }
+
+                c->subband[subs][band][ch] = ((band + 1) & 2) ? -resp : resp;
+            }
+
+            /* Copy in 32 new samples from input */
+            for (i = 0; i < 32; i++)
+                hist[i + hist_start] = input[(subs * 32 + i) * c->channels + ch];
+            hist_start = (hist_start + 32) & 511;
+        }
+    }
+}
+
+static void lfe_downsample(DCAContext *c, const int32_t *input)
+{
+    /* FIXME: make 128x LFE downsampling possible */
+    int i, j, lfes;
+    int32_t hist[512];
+    int32_t accum;
+    int hist_start = 0;
+
+    for (i = 0; i < 512; i++)
+        hist[i] = c->history[i][c->channels - 1];
+
+    for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) {
+        /* Calculate the convolution */
+        accum = 0;
+
+        for (i = hist_start, j = 0; i < 512; i++, j++)
+            accum += mul32(hist[i], lfe_fir_64i[j]);
+        for (i = 0; i < hist_start; i++, j++)
+            accum += mul32(hist[i], lfe_fir_64i[j]);
+
+        c->downsampled_lfe[lfes] = accum;
+
+        /* Copy in 64 new samples from input */
+        for (i = 0; i < 64; i++)
+            hist[i + hist_start] = input[(lfes * 64 + i) * c->channels + c->channels - 1];
+
+        hist_start = (hist_start + 64) & 511;
+    }
 }
 
-/* Integer version of the cosine modulated Pseudo QMF */
+typedef struct {
+    int32_t re;
+    int32_t im;
+} cplx32;
 
-static void qmf_init(void)
+static void fft(const int32_t in[2 * 256], cplx32 out[256])
 {
-    int i;
-    int32_t c[17], s[17];
-    s[0] = 0;           /* sin(index * PI / 64) * 0x7fffffff */
-    c[0] = 0x7fffffff;  /* cos(index * PI / 64) * 0x7fffffff */
-
-    for (i = 1; i <= 16; i++) {
-        s[i] = 2 * (mul32(c[i - 1], 105372028)  + mul32(s[i - 1], 2144896908));
-        c[i] = 2 * (mul32(c[i - 1], 2144896908) - mul32(s[i - 1], 105372028));
+    cplx32 buf[256], rin[256], rout[256];
+    int i, j, k, l;
+
+    /* do two transforms in parallel */
+    for (i = 0; i < 256; i++) {
+        /* Apply the Hann window */
+        rin[i].re = mul32(in[2 * i], 0x3fffffff - (cos_t(8 * i + 2) >> 1));
+        rin[i].im = mul32(in[2 * i + 1], 0x3fffffff - (cos_t(8 * i + 6) >> 1));
+    }
+    /* pre-rotation */
+    for (i = 0; i < 256; i++) {
+        buf[i].re = mul32(cos_t(4 * i + 2), rin[i].re)
+                  - mul32(sin_t(4 * i + 2), rin[i].im);
+        buf[i].im = mul32(cos_t(4 * i + 2), rin[i].im)
+                  + mul32(sin_t(4 * i + 2), rin[i].re);
+    }
+
+    for (j = 256, l = 1; j != 1; j >>= 1, l <<= 1) {
+        for (k = 0; k < 256; k += j) {
+            for (i = k; i < k + j / 2; i++) {
+                cplx32 sum, diff;
+                int t = 8 * l * i;
+
+                sum.re = buf[i].re + buf[i + j / 2].re;
+                sum.im = buf[i].im + buf[i + j / 2].im;
+
+                diff.re = buf[i].re - buf[i + j / 2].re;
+                diff.im = buf[i].im - buf[i + j / 2].im;
+
+                buf[i].re = half32(sum.re);
+                buf[i].im = half32(sum.im);
+
+                buf[i + j / 2].re = mul32(diff.re, cos_t(t))
+                                  - mul32(diff.im, sin_t(t));
+                buf[i + j / 2].im = mul32(diff.im, cos_t(t))
+                                  + mul32(diff.re, sin_t(t));
+            }
+        }
+    }
+    /* post-rotation */
+    for (i = 0; i < 256; i++) {
+        int b = ff_reverse[i];
+        rout[i].re = mul32(buf[b].re, cos_t(4 * i))
+                   - mul32(buf[b].im, sin_t(4 * i));
+        rout[i].im = mul32(buf[b].im, cos_t(4 * i))
+                   + mul32(buf[b].re, sin_t(4 * i));
+    }
+    for (i = 0; i < 256; i++) {
+        /* separate the results of the two transforms */
+        cplx32 o1, o2;
+
+        o1.re =  rout[i].re - rout[255 - i].re;
+        o1.im =  rout[i].im + rout[255 - i].im;
+
+        o2.re =  rout[i].im - rout[255 - i].im;
+        o2.im = -rout[i].re - rout[255 - i].re;
+
+        /* combine them into one long transform */
+        out[i].re = mul32( o1.re + o2.re, cos_t(2 * i + 1))
+                  + mul32( o1.im - o2.im, sin_t(2 * i + 1));
+        out[i].im = mul32( o1.im + o2.im, cos_t(2 * i + 1))
+                  + mul32(-o1.re + o2.re, sin_t(2 * i + 1));
     }
+}
 
-    for (i = 0; i < 16; i++) {
-        cos_table[i      ]  =  c[i]      >> 3; /* avoid output overflow */
-        cos_table[i +  16]  =  s[16 - i] >> 3;
-        cos_table[i +  32]  = -s[i]      >> 3;
-        cos_table[i +  48]  = -c[16 - i] >> 3;
-        cos_table[i +  64]  = -c[i]      >> 3;
-        cos_table[i +  80]  = -s[16 - i] >> 3;
-        cos_table[i +  96]  =  s[i]      >> 3;
-        cos_table[i + 112]  =  c[16 - i] >> 3;
+static int32_t get_cb(int32_t in)
+{
+    int i, res;
+
+    res = 0;
+    if (in < 0)
+        in = -in;
+    for (i = 1024; i > 0; i >>= 1) {
+        if (cb_to_level[i + res] >= in)
+            res += i;
     }
+    return -res;
 }
 
-static int32_t band_delta_factor(int band, int sample_num)
+static int32_t add_cb(int32_t a, int32_t b)
 {
-    int index = band * (2 * sample_num + 1);
-    if (band == 0)
-        return 0x07ffffff;
-    else
-        return cos_table[index & 127];
+    if (a < b)
+        FFSWAP(int32_t, a, b);
+
+    if (a - b >= 256)
+        return a;
+    return a + cb_to_add[a - b];
 }
 
-static void add_new_samples(DCAContext *c, const int32_t *in,
-                            int count, int channel)
+static void adjust_jnd(int samplerate_index,
+                       const int32_t in[512], int32_t out_cb[256])
 {
-    int i;
+    int32_t power[256];
+    cplx32 out[256];
+    int32_t out_cb_unnorm[256];
+    int32_t denom;
+    const int32_t ca_cb = -1114;
+    const int32_t cs_cb = 928;
+    int i, j;
+
+    fft(in, out);
 
-    /* Place new samples into the history buffer */
-    for (i = 0; i < count; i++) {
-        c->history[channel][c->start[channel] + i] = in[i];
-        av_assert0(c->start[channel] + i < 512);
+    for (j = 0; j < 256; j++) {
+        power[j] = add_cb(get_cb(out[j].re), get_cb(out[j].im));
+        out_cb_unnorm[j] = -2047; /* and can only grow */
     }
-    c->start[channel] += count;
-    if (c->start[channel] == 512)
-        c->start[channel] = 0;
-    av_assert0(c->start[channel] < 512);
+
+    for (i = 0; i < AUBANDS; i++) {
+        denom = ca_cb; /* and can only grow */
+        for (j = 0; j < 256; j++)
+            denom = add_cb(denom, power[j] + auf[samplerate_index][i][j]);
+        for (j = 0; j < 256; j++)
+            out_cb_unnorm[j] = add_cb(out_cb_unnorm[j],
+                    -denom + auf[samplerate_index][i][j]);
+    }
+
+    for (j = 0; j < 256; j++)
+        out_cb[j] = add_cb(out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
 }
 
-static void qmf_decompose(DCAContext *c, int32_t in[32], int32_t out[32],
-                          int channel)
+typedef void (*walk_band_t)(DCAContext *c, int band1, int band2, int f,
+                            int32_t spectrum1, int32_t spectrum2, int channel,
+                            int32_t * arg);
+
+static void walk_band_low(DCAContext *c, int band, int channel,
+                          walk_band_t walk, int32_t *arg)
 {
-    int band, i, j, k;
-    int32_t resp;
-    int32_t accum[DCA_SUBBANDS_32] = {0};
+    int f;
 
-    add_new_samples(c, in, DCA_SUBBANDS_32, channel);
+    if (band == 0) {
+        for (f = 0; f < 4; f++)
+            walk(c, 0, 0, f, 0, -2047, channel, arg);
+    } else {
+        for (f = 0; f < 8; f++)
+            walk(c, band, band - 1, 8 * band - 4 + f,
+                    c->band_spectrum[7 - f], c->band_spectrum[f], channel, arg);
+    }
+}
 
-    /* Calculate the dot product of the signal with the (possibly inverted)
-       reference decoder's response to this vector:
-       (0.0, 0.0, ..., 0.0, -1.0, 1.0, 0.0, ..., 0.0)
-       so that -1.0 cancels 1.0 from the previous step */
+static void walk_band_high(DCAContext *c, int band, int channel,
+                           walk_band_t walk, int32_t *arg)
+{
+    int f;
 
-    for (k = 48, j = 0, i = c->start[channel]; i < 512; k++, j++, i++)
-        accum[(k & 32) ? (31 - (k & 31)) : (k & 31)] += mul32(c->history[channel][i], UnQMF[j]);
-    for (i = 0; i < c->start[channel]; k++, j++, i++)
-        accum[(k & 32) ? (31 - (k & 31)) : (k & 31)] += mul32(c->history[channel][i], UnQMF[j]);
+    if (band == 31) {
+        for (f = 0; f < 4; f++)
+            walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
+    } else {
+        for (f = 0; f < 8; f++)
+            walk(c, band, band + 1, 8 * band + 4 + f,
+                    c->band_spectrum[f], c->band_spectrum[7 - f], channel, arg);
+    }
+}
 
-    resp = 0;
-    /* TODO: implement FFT instead of this naive calculation */
-    for (band = 0; band < DCA_SUBBANDS_32; band++) {
-        for (j = 0; j < 32; j++)
-            resp += mul32(accum[j], band_delta_factor(band, j));
+static void update_band_masking(DCAContext *c, int band1, int band2,
+                                int f, int32_t spectrum1, int32_t spectrum2,
+                                int channel, int32_t * arg)
+{
+    int32_t value = c->eff_masking_curve_cb[f] - spectrum1;
 
-        out[band] = (band & 2) ? (-resp) : resp;
+    if (value < c->band_masking_cb[band1])
+        c->band_masking_cb[band1] = value;
+}
+
+static void calc_masking(DCAContext *c, const int32_t *input)
+{
+    int i, k, band, ch, ssf;
+    int32_t data[512];
+
+    for (i = 0; i < 256; i++)
+        for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
+            c->masking_curve_cb[ssf][i] = -2047;
+
+    for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
+        for (ch = 0; ch < c->fullband_channels; ch++) {
+            for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
+                data[i] = c->history[k][ch];
+            for (k -= 512; i < 512; i++, k++)
+                data[i] = input[k * c->channels + ch];
+            adjust_jnd(c->samplerate_index, data, c->masking_curve_cb[ssf]);
+        }
+    for (i = 0; i < 256; i++) {
+        int32_t m = 2048;
+
+        for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
+            if (c->masking_curve_cb[ssf][i] < m)
+                m = c->masking_curve_cb[ssf][i];
+        c->eff_masking_curve_cb[i] = m;
+    }
+
+    for (band = 0; band < 32; band++) {
+        c->band_masking_cb[band] = 2048;
+        walk_band_low(c, band, 0, update_band_masking, NULL);
+        walk_band_high(c, band, 0, update_band_masking, NULL);
     }
 }
 
-static int32_t lfe_fir_64i[512];
-static int lfe_downsample(DCAContext *c, int32_t in[LFE_INTERPOLATION])
+static void find_peaks(DCAContext *c)
 {
-    int i, j;
-    int channel = c->prim_channels;
-    int32_t accum = 0;
-
-    add_new_samples(c, in, LFE_INTERPOLATION, channel);
-    for (i = c->start[channel], j = 0; i < 512; i++, j++)
-        accum += mul32(c->history[channel][i], lfe_fir_64i[j]);
-    for (i = 0; i < c->start[channel]; i++, j++)
-        accum += mul32(c->history[channel][i], lfe_fir_64i[j]);
-    return accum;
+    int band, ch;
+
+    for (band = 0; band < 32; band++)
+        for (ch = 0; ch < c->fullband_channels; ch++) {
+            int sample;
+            int32_t m = 0;
+
+            for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
+                int32_t s = abs(c->subband[sample][band][ch]);
+                if (m < s)
+                    m = s;
+            }
+            c->peak_cb[band][ch] = get_cb(m);
+        }
+
+    if (c->lfe_channel) {
+        int sample;
+        int32_t m = 0;
+
+        for (sample = 0; sample < DCA_LFE_SAMPLES; sample++)
+            if (m < abs(c->downsampled_lfe[sample]))
+                m = abs(c->downsampled_lfe[sample]);
+        c->lfe_peak_cb = get_cb(m);
+    }
 }
 
-static void init_lfe_fir(void)
+static const int snr_fudge = 128;
+#define USED_1ABITS 1
+#define USED_NABITS 2
+#define USED_26ABITS 4
+
+static int init_quantization_noise(DCAContext *c, int noise)
 {
-    static int initialized = 0;
-    int i;
-    if (initialized)
-        return;
+    int ch, band, ret = 0;
+
+    c->consumed_bits = 132 + 493 * c->fullband_channels;
+    if (c->lfe_channel)
+        c->consumed_bits += 72;
+
+    /* attempt to guess the bit distribution based on the prevoius frame */
+    for (ch = 0; ch < c->fullband_channels; ch++) {
+        for (band = 0; band < 32; band++) {
+            int snr_cb = c->peak_cb[band][ch] - c->band_masking_cb[band] - noise;
+
+            if (snr_cb >= 1312) {
+                c->abits[band][ch] = 26;
+                ret |= USED_26ABITS;
+            } else if (snr_cb >= 222) {
+                c->abits[band][ch] = 8 + mul32(snr_cb - 222, 69000000);
+                ret |= USED_NABITS;
+            } else if (snr_cb >= 0) {
+                c->abits[band][ch] = 2 + mul32(snr_cb, 106000000);
+                ret |= USED_NABITS;
+            } else {
+                c->abits[band][ch] = 1;
+                ret |= USED_1ABITS;
+            }
+        }
+    }
 
-    for (i = 0; i < 512; i++)
-        lfe_fir_64i[i] = lfe_fir_64[i] * (1 << 25); //float -> int32_t
-    initialized = 1;
+    for (band = 0; band < 32; band++)
+        for (ch = 0; ch < c->fullband_channels; ch++) {
+            c->consumed_bits += bit_consumption[c->abits[band][ch]];
+        }
+
+    return ret;
+}
+
+static void assign_bits(DCAContext *c)
+{
+    /* Find the bounds where the binary search should work */
+    int low, high, down;
+    int used_abits = 0;
+
+    init_quantization_noise(c, c->worst_quantization_noise);
+    low = high = c->worst_quantization_noise;
+    if (c->consumed_bits > c->frame_bits) {
+        while (c->consumed_bits > c->frame_bits) {
+            av_assert0(used_abits != USED_1ABITS);
+            low = high;
+            high += snr_fudge;
+            used_abits = init_quantization_noise(c, high);
+        }
+    } else {
+        while (c->consumed_bits <= c->frame_bits) {
+            high = low;
+            if (used_abits == USED_26ABITS)
+                goto out; /* The requested bitrate is too high, pad with zeros */
+            low -= snr_fudge;
+            used_abits = init_quantization_noise(c, low);
+        }
+    }
+
+    /* Now do a binary search between low and high to see what fits */
+    for (down = snr_fudge >> 1; down; down >>= 1) {
+        init_quantization_noise(c, high - down);
+        if (c->consumed_bits <= c->frame_bits)
+            high -= down;
+    }
+    init_quantization_noise(c, high);
+out:
+    c->worst_quantization_noise = high;
+    if (high > c->worst_noise_ever)
+        c->worst_noise_ever = high;
+}
+
+static void shift_history(DCAContext *c, const int32_t *input)
+{
+    int k, ch;
+
+    for (k = 0; k < 512; k++)
+        for (ch = 0; ch < c->channels; ch++)
+            c->history[k][ch] = input[k * c->channels + ch];
+}
+
+static int32_t quantize_value(int32_t value, softfloat quant)
+{
+    int32_t offset = 1 << (quant.e - 1);
+
+    value = mul32(value, quant.m) + offset;
+    value = value >> quant.e;
+    return value;
+}
+
+static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
+{
+    int32_t peak;
+    int our_nscale, try_remove;
+    softfloat our_quant;
+
+    av_assert0(peak_cb <= 0);
+    av_assert0(peak_cb >= -2047);
+
+    our_nscale = 127;
+    peak = cb_to_level[-peak_cb];
+
+    for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
+        if (scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e <= 17)
+            continue;
+        our_quant.m = mul32(scalefactor_inv[our_nscale - try_remove].m, stepsize_inv[abits].m);
+        our_quant.e = scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e - 17;
+        if ((quant_levels[abits] - 1) / 2 < quantize_value(peak, our_quant))
+            continue;
+        our_nscale -= try_remove;
+    }
+
+    if (our_nscale >= 125)
+        our_nscale = 124;
+
+    quant->m = mul32(scalefactor_inv[our_nscale].m, stepsize_inv[abits].m);
+    quant->e = scalefactor_inv[our_nscale].e + stepsize_inv[abits].e - 17;
+    av_assert0((quant_levels[abits] - 1) / 2 >= quantize_value(peak, *quant));
+
+    return our_nscale;
+}
+
+static void calc_scales(DCAContext *c)
+{
+    int band, ch;
+
+    for (band = 0; band < 32; band++)
+        for (ch = 0; ch < c->fullband_channels; ch++)
+            c->scale_factor[band][ch] = calc_one_scale(c->peak_cb[band][ch],
+                                                       c->abits[band][ch],
+                                                       &c->quant[band][ch]);
+
+    if (c->lfe_channel)
+        c->lfe_scale_factor = calc_one_scale(c->lfe_peak_cb, 11, &c->lfe_quant);
+}
+
+static void quantize_all(DCAContext *c)
+{
+    int sample, band, ch;
+
+    for (sample = 0; sample < SUBBAND_SAMPLES; sample++)
+        for (band = 0; band < 32; band++)
+            for (ch = 0; ch < c->fullband_channels; ch++)
+                c->quantized[sample][band][ch] = quantize_value(c->subband[sample][band][ch], c->quant[band][ch]);
 }
 
 static void put_frame_header(DCAContext *c)
@@ -244,19 +711,19 @@ static void put_frame_header(DCAContext *c)
     put_bits(&c->pb, 1, 0);
 
     /* Number of PCM sample blocks */
-    put_bits(&c->pb, 7, PCM_SAMPLES-1);
+    put_bits(&c->pb, 7, SUBBAND_SAMPLES - 1);
 
     /* Primary frame byte size */
-    put_bits(&c->pb, 14, c->frame_size-1);
+    put_bits(&c->pb, 14, c->frame_size - 1);
 
-    /* Audio channel arrangement: L + R (stereo) */
-    put_bits(&c->pb, 6, c->num_channel);
+    /* Audio channel arrangement */
+    put_bits(&c->pb, 6, c->channel_config);
 
     /* Core audio sampling frequency */
-    put_bits(&c->pb, 4, c->sample_rate_code);
+    put_bits(&c->pb, 4, bitstream_sfreq[c->samplerate_index]);
 
-    /* Transmission bit rate: 1411.2 kbps */
-    put_bits(&c->pb, 5, 0x16); /* FIXME: magic number */
+    /* Transmission bit rate */
+    put_bits(&c->pb, 5, c->bitrate_index);
 
     /* Embedded down mix: disabled */
     put_bits(&c->pb, 1, 0);
@@ -282,8 +749,8 @@ static void put_frame_header(DCAContext *c)
     /* Audio sync word insertion flag: after each sub-frame */
     put_bits(&c->pb, 1, 0);
 
-    /* Low frequency effects flag: not present or interpolation factor=64 */
-    put_bits(&c->pb, 2, c->lfe_state);
+    /* Low frequency effects flag: not present or 64x subsampling */
+    put_bits(&c->pb, 2, c->lfe_channel ? 2 : 0);
 
     /* Predictor history switch flag: on */
     put_bits(&c->pb, 1, 1);
@@ -321,82 +788,68 @@ static void put_primary_audio_header(DCAContext *c)
     put_bits(&c->pb, 4, SUBFRAMES - 1);
 
     /* Number of primary audio channels */
-    put_bits(&c->pb, 3, c->prim_channels - 1);
+    put_bits(&c->pb, 3, c->fullband_channels - 1);
 
     /* Subband activity count */
-    for (ch = 0; ch < c->prim_channels; ch++)
+    for (ch = 0; ch < c->fullband_channels; ch++)
         put_bits(&c->pb, 5, DCA_SUBBANDS - 2);
 
     /* High frequency VQ start subband */
-    for (ch = 0; ch < c->prim_channels; ch++)
+    for (ch = 0; ch < c->fullband_channels; ch++)
         put_bits(&c->pb, 5, DCA_SUBBANDS - 1);
 
     /* Joint intensity coding index: 0, 0 */
-    for (ch = 0; ch < c->prim_channels; ch++)
+    for (ch = 0; ch < c->fullband_channels; ch++)
         put_bits(&c->pb, 3, 0);
 
     /* Transient mode codebook: A4, A4 (arbitrary) */
-    for (ch = 0; ch < c->prim_channels; ch++)
+    for (ch = 0; ch < c->fullband_channels; ch++)
         put_bits(&c->pb, 2, 0);
 
     /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
-    for (ch = 0; ch < c->prim_channels; ch++)
+    for (ch = 0; ch < c->fullband_channels; ch++)
         put_bits(&c->pb, 3, 6);
 
     /* Bit allocation quantizer select: linear 5-bit */
-    for (ch = 0; ch < c->prim_channels; ch++)
+    for (ch = 0; ch < c->fullband_channels; ch++)
         put_bits(&c->pb, 3, 6);
 
     /* Quantization index codebook select: dummy data
        to avoid transmission of scale factor adjustment */
-
     for (i = 1; i < 11; i++)
-        for (ch = 0; ch < c->prim_channels; ch++)
+        for (ch = 0; ch < c->fullband_channels; ch++)
             put_bits(&c->pb, bitlen[i], thr[i]);
 
     /* Scale factor adjustment index: not transmitted */
+    /* Audio header CRC check word: not transmitted */
 }
 
-/**
- * 8-23 bits quantization
- * @param sample
- * @param bits
- */
-static inline uint32_t quantize(int32_t sample, int bits)
-{
-    av_assert0(sample <    1 << (bits - 1));
-    av_assert0(sample >= -(1 << (bits - 1)));
-    return sample & ((1 << bits) - 1);
-}
-
-static inline int find_scale_factor7(int64_t max_value, int bits)
+static void put_subframe_samples(DCAContext *c, int ss, int band, int ch)
 {
-    int i = 0, j = 128, q;
-    max_value = ((max_value << 15) / lossy_quant[bits + 3]) >> (bits - 1);
-    while (i < j) {
-        q = (i + j) >> 1;
-        if (max_value < scale_factor_quant7[q])
-            j = q;
-        else
-            i = q + 1;
+    if (c->abits[band][ch] <= 7) {
+        int sum, i, j;
+        for (i = 0; i < 8; i += 4) {
+            sum = 0;
+            for (j = 3; j >= 0; j--) {
+                sum *= quant_levels[c->abits[band][ch]];
+                sum += c->quantized[ss * 8 + i + j][band][ch];
+                sum += (quant_levels[c->abits[band][ch]] - 1) / 2;
+            }
+            put_bits(&c->pb, bit_consumption[c->abits[band][ch]] / 4, sum);
+        }
+    } else {
+        int i;
+        for (i = 0; i < 8; i++) {
+            int bits = bit_consumption[c->abits[band][ch]] / 16;
+            int32_t mask = (1 << bits) - 1;
+            put_bits(&c->pb, bits, c->quantized[ss * 8 + i][band][ch] & mask);
+        }
     }
-    av_assert1(i < 128);
-    return i;
-}
-
-static inline void put_sample7(DCAContext *c, int64_t sample, int bits,
-                               int scale_factor)
-{
-    sample = (sample << 15) / ((int64_t) lossy_quant[bits + 3] * scale_factor_quant7[scale_factor]);
-    put_bits(&c->pb, bits, quantize((int) sample, bits));
 }
 
-static void put_subframe(DCAContext *c,
-                         int32_t subband_data[8 * SUBSUBFRAMES][MAX_CHANNELS][32],
-                         int subframe)
+static void put_subframe(DCAContext *c, int subframe)
 {
-    int i, sub, ss, ch, max_value;
-    int32_t *lfe_data = c->lfe_data + 4 * SUBSUBFRAMES * subframe;
+    int i, band, ss, ch;
 
     /* Subsubframes count */
     put_bits(&c->pb, 2, SUBSUBFRAMES -1);
@@ -405,44 +858,27 @@ static void put_subframe(DCAContext *c,
     put_bits(&c->pb, 3, 0);
 
     /* Prediction mode: no ADPCM, in each channel and subband */
-    for (ch = 0; ch < c->prim_channels; ch++)
-        for (sub = 0; sub < DCA_SUBBANDS; sub++)
+    for (ch = 0; ch < c->fullband_channels; ch++)
+        for (band = 0; band < DCA_SUBBANDS; band++)
             put_bits(&c->pb, 1, 0);
 
     /* Prediction VQ addres: not transmitted */
     /* Bit allocation index */
-    for (ch = 0; ch < c->prim_channels; ch++)
-        for (sub = 0; sub < DCA_SUBBANDS; sub++)
-            put_bits(&c->pb, 5, QUANTIZER_BITS+3);
+    for (ch = 0; ch < c->fullband_channels; ch++)
+        for (band = 0; band < DCA_SUBBANDS; band++)
+            put_bits(&c->pb, 5, c->abits[band][ch]);
 
     if (SUBSUBFRAMES > 1) {
         /* Transition mode: none for each channel and subband */
-        for (ch = 0; ch < c->prim_channels; ch++)
-            for (sub = 0; sub < DCA_SUBBANDS; sub++)
+        for (ch = 0; ch < c->fullband_channels; ch++)
+            for (band = 0; band < DCA_SUBBANDS; band++)
                 put_bits(&c->pb, 1, 0); /* codebook A4 */
     }
 
-    /* Determine scale_factor */
-    for (ch = 0; ch < c->prim_channels; ch++)
-        for (sub = 0; sub < DCA_SUBBANDS; sub++) {
-            max_value = 0;
-            for (i = 0; i < 8 * SUBSUBFRAMES; i++)
-                max_value = FFMAX(max_value, FFABS(subband_data[i][ch][sub]));
-            c->scale_factor[ch][sub] = find_scale_factor7(max_value, QUANTIZER_BITS);
-        }
-
-    if (c->lfe_channel) {
-        max_value = 0;
-        for (i = 0; i < 4 * SUBSUBFRAMES; i++)
-            max_value = FFMAX(max_value, FFABS(lfe_data[i]));
-        c->lfe_scale_factor = find_scale_factor7(max_value, LFE_BITS);
-    }
-
-    /* Scale factors: the same for each channel and subband,
-       encoded according to Table D.1.2 */
-    for (ch = 0; ch < c->prim_channels; ch++)
-        for (sub = 0; sub < DCA_SUBBANDS; sub++)
-            put_bits(&c->pb, 7, c->scale_factor[ch][sub]);
+    /* Scale factors */
+    for (ch = 0; ch < c->fullband_channels; ch++)
+        for (band = 0; band < DCA_SUBBANDS; band++)
+            put_bits(&c->pb, 7, c->scale_factor[band][ch]);
 
     /* Joint subband scale factor codebook select: not transmitted */
     /* Scale factors for joint subband coding: not transmitted */
@@ -451,152 +887,83 @@ static void put_subframe(DCAContext *c,
     /* Stde information CRC check word: not transmitted */
     /* VQ encoded high frequency subbands: not transmitted */
 
-    /* LFE data */
+    /* LFE data: 8 samples and scalefactor */
     if (c->lfe_channel) {
-        for (i = 0; i < 4 * SUBSUBFRAMES; i++)
-            put_sample7(c, lfe_data[i], LFE_BITS, c->lfe_scale_factor);
+        for (i = 0; i < DCA_LFE_SAMPLES; i++)
+            put_bits(&c->pb, 8, quantize_value(c->downsampled_lfe[i], c->lfe_quant) & 0xff);
         put_bits(&c->pb, 8, c->lfe_scale_factor);
     }
 
     /* Audio data (subsubframes) */
-
     for (ss = 0; ss < SUBSUBFRAMES ; ss++)
-        for (ch = 0; ch < c->prim_channels; ch++)
-            for (sub = 0; sub < DCA_SUBBANDS; sub++)
-                for (i = 0; i < 8; i++)
-                    put_sample7(c, subband_data[ss * 8 + i][ch][sub], QUANTIZER_BITS, c->scale_factor[ch][sub]);
+        for (ch = 0; ch < c->fullband_channels; ch++)
+            for (band = 0; band < DCA_SUBBANDS; band++)
+                    put_subframe_samples(c, ss, band, ch);
 
     /* DSYNC */
     put_bits(&c->pb, 16, 0xffff);
 }
 
-static void put_frame(DCAContext *c,
-                      int32_t subband_data[PCM_SAMPLES][MAX_CHANNELS][32],
-                      uint8_t *frame)
-{
-    int i;
-    init_put_bits(&c->pb, frame + DCA_HEADER_SIZE, DCA_MAX_FRAME_SIZE-DCA_HEADER_SIZE);
-
-    put_primary_audio_header(c);
-    for (i = 0; i < SUBFRAMES; i++)
-        put_subframe(c, &subband_data[SUBSUBFRAMES * 8 * i], i);
-
-    flush_put_bits(&c->pb);
-    c->frame_size = (put_bits_count(&c->pb) >> 3) + DCA_HEADER_SIZE;
-
-    init_put_bits(&c->pb, frame, DCA_HEADER_SIZE);
-    put_frame_header(c);
-    flush_put_bits(&c->pb);
-}
-
 static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                         const AVFrame *frame, int *got_packet_ptr)
 {
-    int i, k, channel;
     DCAContext *c = avctx->priv_data;
-    const int16_t *samples;
-    int ret, real_channel = 0;
+    const int32_t *samples;
+    int ret, i;
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, DCA_MAX_FRAME_SIZE + DCA_HEADER_SIZE)) < 0)
+    if ((ret = ff_alloc_packet2(avctx, avpkt, c->frame_size )) < 0)
         return ret;
 
-    samples = (const int16_t *)frame->data[0];
-    for (i = 0; i < PCM_SAMPLES; i ++) { /* i is the decimated sample number */
-        for (channel = 0; channel < c->prim_channels + 1; channel++) {
-            real_channel = c->channel_order_tab[channel];
-            if (real_channel >= 0) {
-                /* Get 32 PCM samples */
-                for (k = 0; k < 32; k++) { /* k is the sample number in a 32-sample block */
-                    c->pcm[k] = samples[avctx->channels * (32 * i + k) + channel] << 16;
-                }
-                /* Put subband samples into the proper place */
-                qmf_decompose(c, c->pcm, &c->subband[i][real_channel][0], real_channel);
-            }
-        }
-    }
+    samples = (const int32_t *)frame->data[0];
 
-    if (c->lfe_channel) {
-        for (i = 0; i < PCM_SAMPLES / 2; i++) {
-            for (k = 0; k < LFE_INTERPOLATION; k++) /* k is the sample number in a 32-sample block */
-                c->pcm[k] = samples[avctx->channels * (LFE_INTERPOLATION*i+k) + c->lfe_offset] << 16;
-            c->lfe_data[i] = lfe_downsample(c, c->pcm);
-        }
-    }
+    subband_transform(c, samples);
+    if (c->lfe_channel)
+        lfe_downsample(c, samples);
 
-    put_frame(c, c->subband, avpkt->data);
+    calc_masking(c, samples);
+    find_peaks(c);
+    assign_bits(c);
+    calc_scales(c);
+    quantize_all(c);
+    shift_history(c, samples);
 
-    avpkt->size     = c->frame_size;
-    *got_packet_ptr = 1;
-    return 0;
-}
-
-static int encode_init(AVCodecContext *avctx)
-{
-    DCAContext *c = avctx->priv_data;
-    int i;
-    uint64_t layout = avctx->channel_layout;
-
-    c->prim_channels = avctx->channels;
-    c->lfe_channel   = (avctx->channels == 3 || avctx->channels == 6);
-
-    if (!layout) {
-        av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
-                                      "encoder will guess the layout, but it "
-                                      "might be incorrect.\n");
-        layout = av_get_default_channel_layout(avctx->channels);
-    }
-    switch (layout) {
-    case AV_CH_LAYOUT_STEREO:       c->a_mode = 2; c->num_channel = 2; break;
-    case AV_CH_LAYOUT_5POINT0:      c->a_mode = 9; c->num_channel = 9; break;
-    case AV_CH_LAYOUT_5POINT1:      c->a_mode = 9; c->num_channel = 9; break;
-    case AV_CH_LAYOUT_5POINT0_BACK: c->a_mode = 9; c->num_channel = 9; break;
-    case AV_CH_LAYOUT_5POINT1_BACK: c->a_mode = 9; c->num_channel = 9; break;
-    default:
-    av_log(avctx, AV_LOG_ERROR,
-           "Only stereo, 5.0, 5.1 channel layouts supported at the moment!\n");
-    return AVERROR_PATCHWELCOME;
-    }
-
-    if (c->lfe_channel) {
-        init_lfe_fir();
-        c->prim_channels--;
-        c->channel_order_tab = dca_channel_reorder_lfe[c->a_mode];
-        c->lfe_state         = LFE_PRESENT;
-        c->lfe_offset        = dca_lfe_index[c->a_mode];
-    } else {
-        c->channel_order_tab = dca_channel_reorder_nolfe[c->a_mode];
-        c->lfe_state         = LFE_MISSING;
-    }
-
-    for (i = 0; i < 16; i++) {
-        if (avpriv_dca_sample_rates[i] && (avpriv_dca_sample_rates[i] == avctx->sample_rate))
-            break;
-    }
-    if (i == 16) {
-        av_log(avctx, AV_LOG_ERROR, "Sample rate %iHz not supported, only ", avctx->sample_rate);
-        for (i = 0; i < 16; i++)
-            av_log(avctx, AV_LOG_ERROR, "%d, ", avpriv_dca_sample_rates[i]);
-        av_log(avctx, AV_LOG_ERROR, "supported.\n");
-        return -1;
-    }
-    c->sample_rate_code = i;
+    init_put_bits(&c->pb, avpkt->data, avpkt->size);
+    put_frame_header(c);
+    put_primary_audio_header(c);
+    for (i = 0; i < SUBFRAMES; i++)
+        put_subframe(c, i);
 
-    avctx->frame_size = 32 * PCM_SAMPLES;
+    flush_put_bits(&c->pb);
 
-    if (!cos_table[127])
-        qmf_init();
+    avpkt->pts      = frame->pts;
+    avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
+    avpkt->size     = c->frame_size + 1;
+    *got_packet_ptr = 1;
     return 0;
 }
 
+static const AVCodecDefault defaults[] = {
+    { "b",          "1411200" },
+    { NULL },
+};
+
 AVCodec ff_dca_encoder = {
-    .name           = "dca",
-    .type           = AVMEDIA_TYPE_AUDIO,
-    .id             = AV_CODEC_ID_DTS,
-    .priv_data_size = sizeof(DCAContext),
-    .init           = encode_init,
-    .encode2        = encode_frame,
-    .capabilities   = CODEC_CAP_EXPERIMENTAL,
-    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
-                                                     AV_SAMPLE_FMT_NONE },
-    .long_name      = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
+    .name                  = "dca",
+    .type                  = AVMEDIA_TYPE_AUDIO,
+    .id                    = AV_CODEC_ID_DTS,
+    .priv_data_size        = sizeof(DCAContext),
+    .init                  = encode_init,
+    .encode2               = encode_frame,
+    .capabilities          = CODEC_CAP_EXPERIMENTAL,
+    .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
+                                                            AV_SAMPLE_FMT_NONE },
+    .long_name             = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
+    .supported_samplerates = sample_rates,
+    .channel_layouts       = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
+                                                  AV_CH_LAYOUT_STEREO,
+                                                  AV_CH_LAYOUT_2_2,
+                                                  AV_CH_LAYOUT_5POINT0,
+                                                  AV_CH_LAYOUT_5POINT1,
+                                                  0 },
+    .defaults              = defaults,
 };
diff --git a/libavcodec/dcaenc.h b/libavcodec/dcaenc.h
index 121e5da..20f557b 100644
--- a/libavcodec/dcaenc.h
+++ b/libavcodec/dcaenc.h
@@ -1,6 +1,6 @@
 /*
  * DCA encoder tables
- * Copyright (C) 2008 Alexander E. Patrakov
+ * Copyright (C) 2008-2012 Alexander E. Patrakov
  *
  * This file is part of FFmpeg.
  *
@@ -24,523 +24,90 @@
 
 #include <stdint.h>
 
-/* This is a scaled version of the response of the reference decoder to
-   this vector of subband samples: ( 1.0 0.0 0.0 ... 0.0 )
-   */
+typedef struct {
+    int32_t m;
+    int32_t e;
+} softfloat;
 
-static const int32_t UnQMF[512] = {
-    7,
-    4,
-    -961,
-    -2844,
-    -8024,
-    -18978,
-    -32081,
-    -15635,
-    -16582,
-    -18359,
-    -17180,
-    -14868,
-    -11664,
-    -8051,
-    -4477,
-    -1327,
-    -1670,
-    -6019,
-    -11590,
-    -18030,
-    -24762,
-    -30965,
-    -35947,
-    -36145,
-    -37223,
-    -86311,
-    -57024,
-    -27215,
-    -11274,
-    -4684,
-    42,
-    108,
-    188,
-    250,
-    -1007,
-    -596,
-    -2289,
-    -12218,
-    -27191,
-    -124367,
-    -184256,
-    -250538,
-    -323499,
-    -397784,
-    -468855,
-    -532072,
-    -583000,
-    -618041,
-    -777916,
-    -783868,
-    -765968,
-    -724740,
-    -662468,
-    -583058,
-    -490548,
-    -401623,
-    -296090,
-    -73154,
-    -36711,
-    -7766,
-    -2363,
-    -4905,
-    2388,
-    2681,
-    5651,
-    4086,
-    71110,
-    139742,
-    188067,
-    151237,
-    101355,
-    309917,
-    343690,
-    358839,
-    357555,
-    334606,
-    289625,
-    224152,
-    142063,
-    48725,
-    74996,
-    238425,
-    411666,
-    584160,
-    744276,
-    880730,
-    983272,
-    1041933,
-    1054396,
-    789531,
-    851022,
-    864032,
-    675431,
-    418134,
-    35762,
-    66911,
-    103502,
-    136403,
-    -55147,
-    -245269,
-    -499595,
-    -808470,
-    -1136858,
-    -2010912,
-    -2581654,
-    -3151901,
-    -3696328,
-    -4196599,
-    -4633761,
-    -4993229,
-    -5262495,
-    -5436311,
-    -477650,
-    -901314,
-    -1308090,
-    -1677468,
-    -1985525,
-    -2212848,
-    -2341196,
-    -2373915,
-    -2269552,
-    -2620489,
-    -2173858,
-    -1629954,
-    -946595,
-    -193499,
-    1119459,
-    1138657,
-    1335311,
-    1126544,
-    2765033,
-    3139603,
-    3414913,
-    3599213,
-    3676363,
-    3448981,
-    3328726,
-    3111551,
-    2810887,
-    2428657,
-    1973684,
-    1457278,
-    893848,
-    300995,
-    -292521,
-    -867621,
-    -1404936,
-    -1871278,
-    -2229831,
-    -2440932,
-    -2462684,
-    -2255006,
-    -1768898,
-    -1079574,
-    82115,
-    1660302,
-    3660715,
-    6123610,
-    8329598,
-    11888744,
-    15722147,
-    19737089,
-    25647773,
-    31039399,
-    36868007,
-    43124253,
-    49737161,
-    56495958,
-    63668945,
-    71039511,
-    78540240,
-    86089058,
-    93600041,
-    100981151,
-    108136061,
-    114970055,
-    121718321,
-    127566038,
-    132774642,
-    137247294,
-    140894737,
-    143635018,
-    145395599,
-    146114032,
-    145742999,
-    144211606,
-    141594341,
-    137808404,
-    132914122,
-    126912246,
-    120243281,
-    112155281,
-    103338368,
-    93904953,
-    83439152,
-    72921548,
-    62192990,
-    51434918,
-    40894003,
-    30786726,
-    21384955,
-    12939112,
-    5718193,
-    -5790,
-    -3959261,
-    -5870978,
-    -5475538,
-    -2517061,
-    3247310,
-    12042937,
-    24076729,
-    39531397,
-    58562863,
-    81297002,
-    107826748,
-    138209187,
-    172464115,
-    210569037,
-    252468018,
-    298045453,
-    347168648,
-    399634888,
-    455137189,
-    513586535,
-    574537650,
-    637645129,
-    702597163,
-    768856566,
-    836022040,
-    903618096,
-    971159680,
-    1038137214,
-    1103987353,
-    1168195035,
-    1230223053,
-    1289539180,
-    1345620373,
-    1397957958,
-    1446063657,
-    1489474689,
-    1527740502,
-    1560502307,
-    1587383079,
-    1608071145,
-    1622301248,
-    1629859340,
-    1630584888,
-    1624373875,
-    1611178348,
-    1591018893,
-    1563948667,
-    1530105004,
-    1489673227,
-    1442904075,
-    1390107674,
-    1331590427,
-    1267779478,
-    1199115126,
-    1126053392,
-    1049146257,
-    968928307,
-    885965976,
-    800851610,
-    714186243,
-    626590147,
-    538672486,
-    451042824,
-    364299927,
-    279026812,
-    195785029,
-    115109565,
-    37503924,
-    -36564551,
-    -106668063,
-    -172421668,
-    -233487283,
-    -289575706,
-    -340448569,
-    -385919511,
-    -425854915,
-    -460174578,
-    -488840702,
-    -511893328,
-    -529405118,
-    -541489888,
-    -548312207,
-    -550036471,
-    -547005316,
-    -539436808,
-    -527630488,
-    -512084785,
-    -492941605,
-    -470665204,
-    -445668379,
-    -418328829,
-    -389072810,
-    -358293846,
-    -326396227,
-    -293769619,
-    -260792276,
-    -227825056,
-    -195208961,
-    -163262121,
-    -132280748,
-    -102533727,
-    -74230062,
-    -47600637,
-    -22817785,
-    -25786,
-    20662895,
-    39167253,
-    55438413,
-    69453741,
-    81242430,
-    90795329,
-    98213465,
-    103540643,
-    106917392,
-    108861938,
-    108539682,
-    106780704,
-    103722568,
-    99043289,
-    93608686,
-    87266209,
-    80212203,
-    72590022,
-    64603428,
-    56362402,
-    48032218,
-    39749162,
-    31638971,
-    23814664,
-    16376190,
-    9409836,
-    2988017,
-    -2822356,
-    -7976595,
-    -12454837,
-    -16241147,
-    -19331944,
-    -21735011,
-    -23468284,
-    -24559822,
-    -25042936,
-    -25035583,
-    -24429587,
-    -23346408,
-    -21860411,
-    -20015718,
-    -17025330,
-    -14968728,
-    -12487138,
-    -9656319,
-    -7846681,
-    -5197816,
-    -2621904,
-    -144953,
-    2144746,
-    3990570,
-    5845884,
-    7454650,
-    8820394,
-    9929891,
-    10784445,
-    11390921,
-    11762056,
-    11916017,
-    12261189,
-    12117604,
-    11815303,
-    11374622,
-    10815301,
-    10157241,
-    9418799,
-    8629399,
-    7780776,
-    7303680,
-    6353499,
-    5392738,
-    4457895,
-    3543062,
-    1305978,
-    1402521,
-    1084092,
-    965652,
-    -151008,
-    -666667,
-    -1032157,
-    -1231475,
-    -1319043,
-    -1006023,
-    -915720,
-    -773426,
-    -612377,
-    -445864,
-    -291068,
-    -161337,
-    -66484,
-    -11725,
-    133453,
-    388184,
-    615856,
-    804033,
-    942377,
-    1022911,
-    1041247,
-    995854,
-    891376,
-    572246,
-    457992,
-    316365,
-    172738,
-    43037,
-    -117662,
-    -98542,
-    -70279,
-    -41458,
-    -535790,
-    -959038,
-    -1364456,
-    -1502265,
-    -1568530,
-    -2378681,
-    -2701111,
-    -2976407,
-    -3182552,
-    -3314415,
-    -3366600,
-    -3337701,
-    -3232252,
-    -3054999,
-    1984841,
-    1925903,
-    1817377,
-    1669153,
-    1490069,
-    1292040,
-    1086223,
-    890983,
-    699163,
-    201358,
-    266971,
-    296990,
-    198419,
-    91119,
-    4737,
-    5936,
-    2553,
-    2060,
-    -3828,
-    -1664,
-    -4917,
-    -20796,
-    -36822,
-    -131247,
-    -154923,
-    -162055,
-    -161354,
-    -148762,
-    -125754,
-    -94473,
-    -57821,
-    -19096,
-    15172,
-    43004,
-    65624,
-    81354,
-    89325,
-    89524,
-    82766,
-    71075,
-    55128,
-    13686,
-    6921,
-    1449,
-    420,
-    785,
-    -215,
-    -179,
-    -113,
-    -49,
-    6002,
-    16007,
-    42978,
-    100662,
-    171472,
-    83975,
-    93702,
-    108813,
-    111893,
-    110272,
-    103914,
-    93973,
-    81606,
-    68041,
-    -54058,
-    -60695,
-    -65277,
-    -67224,
-    -66213,
-    -62082,
-    -55574,
-    -42988,
-    -35272,
-    -63735,
-    -33501,
-    -12671,
-    -4038,
-    -1232,
-    5,
-    7
+static const int sample_rates[] = {
+    8000, 16000, 32000, 11025, 22050, 44100, 12000, 24000, 48000, 0,
+};
+
+static const uint8_t bitstream_sfreq[] = { 1, 2, 3, 6, 7, 8, 11, 12, 13 };
+
+/* Auditory filter center frequencies and bandwidths, in Hz.
+ * The last two are made up, because there is no scientific data.
+ */
+static uint16_t fc[] = {
+    50, 150, 250, 350, 450, 570, 700, 840, 1000, 1170, 1370, 1600, 1850, 2150,
+    2500, 2900, 3400, 4000, 4800, 5800, 7000, 8500, 10500, 13500, 17000
+};
+
+static uint16_t erb[] = {
+    80, 100, 100, 100, 110, 120, 140, 150, 160, 190, 210, 240, 280,
+    320, 380, 450, 550, 700, 900, 1100, 1300, 1800, 2500, 3500, 4500
+};
+
+static const softfloat stepsize_inv[27] = {
+    {0, 0}, {1342177360, 21}, {2147483647, 21}, {1342177360, 20},
+    {1819901661, 20}, {2147483647, 20}, {1278263843, 19}, {1579032492, 19},
+    {1412817763, 18}, {1220162327, 17}, {1118482133, 16}, {1917391412, 16},
+    {1766017772, 15}, {1525212826, 14}, {1290553940, 13}, {2097179000, 13},
+    {1677683200, 12}, {1497972244, 11}, {1310893147, 10}, {1165354136, 9},
+    {1748031204, 9}, {1542092044, 8}, {1636178017, 7}, {1636178017, 6},
+    {1636178017, 5}, {1636178017, 4}, {1636178017, 3},
+};
+
+static const softfloat scalefactor_inv[128] = {
+    {2147483647, 1}, {2147483647, 1}, {2147483647, 2}, {2147483647, 2},
+    {2147483647, 2}, {2147483647, 2}, {1431655765, 2}, {1431655765, 2},
+    {1431655765, 2}, {2147483647, 3}, {2147483647, 3}, {1717986918, 3},
+    {1431655765, 3}, {1227133513, 3}, {1227133513, 3}, {2147483647, 4},
+    {1717986918, 4}, {1561806289, 4}, {1431655765, 4}, {1227133513, 4},
+    {2147483647, 5}, {1908874353, 5}, {1717986918, 5}, {1493901668, 5},
+    {1321528398, 5}, {1145324612, 5}, {2021161080, 6}, {1808407282, 6},
+    {1561806289, 6}, {1374389534, 6}, {1227133513, 6}, {2147483647, 7},
+    {1908874353, 7}, {1676084798, 7}, {1477838209, 7}, {1296593900, 7},
+    {1145324612, 7}, {2021161080, 8}, {1773405851, 8}, {1561806289, 8},
+    {1374389534, 8}, {1216273924, 8}, {2139127680, 9}, {1882725390, 9},
+    {1660893697, 9}, {1462116526, 9}, {1287484341, 9}, {1135859119, 9},
+    {1999112050, 10}, {1762037865, 10}, {1552982525, 10}, {1367551775, 10},
+    {1205604855, 10}, {2124660150, 11}, {1871509153, 11}, {1648443220, 11},
+    {1452459217, 11}, {1279990253, 11}, {1127704233, 11}, {1987368509, 12},
+    {1750814693, 12}, {1542632939, 12}, {1359099663, 12}, {1197398995, 12},
+    {2109880792, 13}, {1858853132, 13}, {1638006149, 13}, {1443165385, 13},
+    {1271479187, 13}, {1120235993, 13}, {1973767086, 14}, {1739045674, 14},
+    {1532153461, 14}, {1349922194, 14}, {1189384493, 14}, {2095804865, 15},
+    {1846464029, 15}, {1626872524, 15}, {1433347133, 15}, {1262853884, 15},
+    {1112619678, 15}, {1960569045, 16}, {1727349015, 16}, {1521881227, 16},
+    {1340842289, 16}, {1181357555, 16}, {2081669156, 17}, {1834047752, 17},
+    {1615889229, 17}, {1423675973, 17}, {1254322457, 17}, {1105123583, 17},
+    {1947330755, 18}, {1715693602, 18}, {1511607799, 18}, {1331801790, 18},
+    {1173384427, 18}, {2067616532, 19}, {1821667648, 19}, {1604980024, 19},
+    {1414066955, 19}, {1245861410, 19}, {1097665748, 19}, {1934193616, 20},
+    {1704119624, 20}, {1501412075, 20}, {1322817107, 20}, {1165466323, 20},
+    {2053666205, 21}, {1809379407, 21}, {1594151671, 21}, {1404526328, 21},
+    {1237455941, 21}, {1090259329, 21}, {1921143210, 22}, {1692621231, 22},
+    {1491281857, 22}, {1313892269, 22}, {1157603482, 22}, {2039810470, 23},
+    {1797172644, 23}, {1583396912, 23}, {1395050052, 23}, {1229107276, 23},
+    {1082903494, 23}, {1082903494, 23}, {1082903494, 23}, {1082903494, 23},
+};
+
+/* manually derived from
+ * Table B.5: Selection of quantization levels and codebooks
+ * FIXME: will become invalid when Huffman codes are introduced.
+ */
+static const int bit_consumption[27] = {
+    -8, 28, 40, 48, 52, 60, 68, 76, 80, 96,
+    112, 128, 144, 160, 176, 192, 208, 224, 240, 256,
+    272, 288, 304, 320, 336, 352, 368,
+};
+
+/* Table B.5: Selection of quantization levels and codebooks */
+static const int quant_levels[27] = {
+    1, 3, 5, 7, 9, 13, 17, 25, 32, 64,
+    128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536,
+    131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608,
 };
 
 #endif /* AVCODEC_DCAENC_H */



More information about the ffmpeg-cvslog mailing list