]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/dcaenc.c
avcodec/exr: Check remaining bits in last get code loop
[ffmpeg] / libavcodec / dcaenc.c
index dd601ffae0077f2fa0bc2cab589cf09d019e9a7e..186997c6311d254a91cf03d87542f87442b452c3 100644 (file)
@@ -21,6 +21,9 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#define FFT_FLOAT 0
+#define FFT_FIXED_32 1
+
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
@@ -33,6 +36,7 @@
 #include "dca_core.h"
 #include "dcadata.h"
 #include "dcaenc.h"
+#include "fft.h"
 #include "internal.h"
 #include "mathops.h"
 #include "put_bits.h"
@@ -48,6 +52,8 @@
 #define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
 #define AUBANDS 25
 
+#define COS_T(x) (c->cos_table[(x) & 2047])
+
 typedef struct CompressionOptions {
     int adpcm_mode;
 } CompressionOptions;
@@ -56,6 +62,7 @@ typedef struct DCAEncContext {
     AVClass *class;
     PutBitContext pb;
     DCAADPCMEncContext adpcm_ctx;
+    FFTContext mdct;
     CompressionOptions options;
     int frame_size;
     int frame_bits;
@@ -92,15 +99,15 @@ typedef struct DCAEncContext {
     int32_t worst_noise_ever;
     int consumed_bits;
     int consumed_adpcm_bits; ///< Number of bits to transmit ADPCM related info
-} DCAEncContext;
 
-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 cos_table[2048];
+    int32_t band_interpolation_tab[2][512];
+    int32_t band_spectrum_tab[2][8];
+    int32_t auf[9][AUBANDS][256];
+    int32_t cb_to_add[256];
+    int32_t cb_to_level[2048];
+    int32_t lfe_fir_64i[512];
+} DCAEncContext;
 
 /* Transfer function of outer and middle ear, Hz -> dB */
 static double hom(double f)
@@ -153,15 +160,16 @@ static int encode_init(AVCodecContext *avctx)
 {
     DCAEncContext *c = avctx->priv_data;
     uint64_t layout = avctx->channel_layout;
-    int i, j, min_frame_bits;
+    int i, j, k, min_frame_bits;
+    int ret;
 
     if (subband_bufer_alloc(c))
         return AVERROR(ENOMEM);
 
     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->band_interpolation = c->band_interpolation_tab[1];
+    c->band_spectrum = c->band_spectrum_tab[1];
     c->worst_quantization_noise = -2047;
     c->worst_noise_ever = -2047;
     c->consumed_adpcm_bits = 0;
@@ -231,91 +239,77 @@ static int encode_init(AVCodecContext *avctx)
 
     avctx->frame_size = 32 * SUBBAND_SAMPLES;
 
-    if (!cos_table[0]) {
-        int j, k;
-
-        cos_table[0] = 0x7fffffff;
-        cos_table[512] = 0;
-        cos_table[1024] = -cos_table[0];
-        for (i = 1; i < 512; i++) {
-            cos_table[i]   = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
-            cos_table[1024-i] = -cos_table[i];
-            cos_table[1024+i] = -cos_table[i];
-            cos_table[2048-i] = cos_table[i];
-        }
-        for (i = 0; i < 2048; i++) {
-            cb_to_level[i] = (int32_t)(0x7fffffff * ff_exp10(-0.005 * i));
-        }
+    if ((ret = ff_mdct_init(&c->mdct, 9, 0, 1.0)) < 0)
+        return ret;
 
-        for (k = 0; k < 32; k++) {
-            for (j = 0; j < 8; j++) {
-                lfe_fir_64i[64 * j + k] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
-                lfe_fir_64i[64 * (7-j) + (63 - k)] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
-            }
-        }
+    /* Init all tables */
+    c->cos_table[0] = 0x7fffffff;
+    c->cos_table[512] = 0;
+    c->cos_table[1024] = -c->cos_table[0];
+    for (i = 1; i < 512; i++) {
+        c->cos_table[i]   = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
+        c->cos_table[1024-i] = -c->cos_table[i];
+        c->cos_table[1024+i] = -c->cos_table[i];
+        c->cos_table[2048-i] = +c->cos_table[i];
+    }
 
-        for (i = 0; i < 512; i++) {
-            band_interpolation[0][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_perfect[i]);
-            band_interpolation[1][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_nonperfect[i]);
+    for (i = 0; i < 2048; i++)
+        c->cb_to_level[i] = (int32_t)(0x7fffffff * ff_exp10(-0.005 * i));
+
+    for (k = 0; k < 32; k++) {
+        for (j = 0; j < 8; j++) {
+            c->lfe_fir_64i[64 * j + k] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
+            c->lfe_fir_64i[64 * (7-j) + (63 - k)] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
         }
+    }
+
+    for (i = 0; i < 512; i++) {
+        c->band_interpolation_tab[0][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_perfect[i]);
+        c->band_interpolation_tab[1][i] = (int32_t)(0x1000000000ULL * ff_dca_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;
+    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)));
-                }
+                c->auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
             }
         }
+    }
 
-        for (i = 0; i < 256; i++) {
-            double add = 1 + ff_exp10(-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 = ff_dca_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 (i = 0; i < 256; i++) {
+        double add = 1 + ff_exp10(-0.01 * i);
+        c->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 = ff_dca_fir_32bands_perfect[i] * ((i & 64) ? (-1) : 1);
+            accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
         }
-        for (j = 0; j < 8; j++) {
-            double accum = 0;
-            for (i = 0; i < 512; i++) {
-                double reconst = ff_dca_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));
+        c->band_spectrum_tab[0][j] = (int32_t)(200 * log10(accum));
+    }
+    for (j = 0; j < 8; j++) {
+        double accum = 0;
+        for (i = 0; i < 512; i++) {
+            double reconst = ff_dca_fir_32bands_nonperfect[i] * ((i & 64) ? (-1) : 1);
+            accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
         }
+        c->band_spectrum_tab[1][j] = (int32_t)(200 * log10(accum));
     }
-    return 0;
-}
 
-static av_cold int encode_close(AVCodecContext *avctx)
-{
-    if (avctx->priv_data) {
-        DCAEncContext *c = avctx->priv_data;
-        subband_bufer_free(c);
-        ff_dcaadpcm_free(&c->adpcm_ctx);
-    }
     return 0;
 }
 
-static inline int32_t cos_t(int x)
-{
-    return cos_table[x & 2047];
-}
-
-static inline int32_t sin_t(int x)
+static av_cold int encode_close(AVCodecContext *avctx)
 {
-    return cos_t(x - 512);
-}
+    DCAEncContext *c = avctx->priv_data;
+    ff_mdct_end(&c->mdct);
+    subband_bufer_free(c);
+    ff_dcaadpcm_free(&c->adpcm_ctx);
 
-static inline int32_t half32(int32_t a)
-{
-    return (a + 1) >> 1;
+    return 0;
 }
 
 static void subband_transform(DCAEncContext *c, const int32_t *input)
@@ -353,7 +347,7 @@ static void subband_transform(DCAEncContext *c, const int32_t *input)
                 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;
+                    resp += mul32(accum[i], COS_T(s << 3)) >> 3;
                 }
 
                 c->subband[ch][band][subs] = ((band + 1) & 2) ? -resp : resp;
@@ -384,9 +378,9 @@ static void lfe_downsample(DCAEncContext *c, const int32_t *input)
         accum = 0;
 
         for (i = hist_start, j = 0; i < 512; i++, j++)
-            accum += mul32(hist[i], lfe_fir_64i[j]);
+            accum += mul32(hist[i], c->lfe_fir_64i[j]);
         for (i = 0; i < hist_start; i++, j++)
-            accum += mul32(hist[i], lfe_fir_64i[j]);
+            accum += mul32(hist[i], c->lfe_fir_64i[j]);
 
         c->downsampled_lfe[lfes] = accum;
 
@@ -398,131 +392,72 @@ static void lfe_downsample(DCAEncContext *c, const int32_t *input)
     }
 }
 
-typedef struct {
-    int32_t re;
-    int32_t im;
-} cplx32;
-
-static void fft(const int32_t in[2 * 256], cplx32 out[256])
-{
-    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));
-    }
-}
-
-static int32_t get_cb(int32_t in)
+static int32_t get_cb(DCAEncContext *c, int32_t in)
 {
-    int i, res;
+    int i, res = 0;
+    in = FFABS(in);
 
-    res = 0;
-    if (in < 0)
-        in = -in;
     for (i = 1024; i > 0; i >>= 1) {
-        if (cb_to_level[i + res] >= in)
+        if (c->cb_to_level[i + res] >= in)
             res += i;
     }
     return -res;
 }
 
-static int32_t add_cb(int32_t a, int32_t b)
+static int32_t add_cb(DCAEncContext *c, int32_t a, int32_t b)
 {
     if (a < b)
         FFSWAP(int32_t, a, b);
 
     if (a - b >= 256)
         return a;
-    return a + cb_to_add[a - b];
+    return a + c->cb_to_add[a - b];
+}
+
+static void calc_power(DCAEncContext *c,
+                       const int32_t in[2 * 256], int32_t power[256])
+{
+    int i;
+    LOCAL_ALIGNED_32(int32_t, data,  [512]);
+    LOCAL_ALIGNED_32(int32_t, coeff, [256]);
+
+    for (i = 0; i < 512; i++)
+        data[i] = norm__(mul32(in[i], 0x3fffffff - (COS_T(4 * i + 2) >> 1)), 4);
+
+    c->mdct.mdct_calc(&c->mdct, coeff, data);
+    for (i = 0; i < 256; i++) {
+        const int32_t cb = get_cb(c, coeff[i]);
+        power[i] = add_cb(c, cb, cb);
+    }
 }
 
-static void adjust_jnd(int samplerate_index,
+static void adjust_jnd(DCAEncContext *c,
                        const int32_t in[512], int32_t out_cb[256])
 {
     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;
+    const int samplerate_index = c->samplerate_index;
     int i, j;
 
-    fft(in, out);
+    calc_power(c, in, power);
 
-    for (j = 0; j < 256; j++) {
-        power[j] = add_cb(get_cb(out[j].re), get_cb(out[j].im));
+    for (j = 0; j < 256; j++)
         out_cb_unnorm[j] = -2047; /* and can only grow */
-    }
 
     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]);
+            denom = add_cb(c, denom, power[j] + c->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]);
+            out_cb_unnorm[j] = add_cb(c, out_cb_unnorm[j],
+                                      -denom + c->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);
+        out_cb[j] = add_cb(c, out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
 }
 
 typedef void (*walk_band_t)(DCAEncContext *c, int band1, int band2, int f,
@@ -586,7 +521,7 @@ static void calc_masking(DCAEncContext *c, const int32_t *input)
                 data[i] = c->history[ch][k];
             for (k -= 512; i < 512; i++, k++)
                 data[i] = input[k * c->channels + chi];
-            adjust_jnd(c->samplerate_index, data, c->masking_curve_cb[ssf]);
+            adjust_jnd(c, data, c->masking_curve_cb[ssf]);
         }
     for (i = 0; i < 256; i++) {
         int32_t m = 2048;
@@ -604,16 +539,16 @@ static void calc_masking(DCAEncContext *c, const int32_t *input)
     }
 }
 
-static inline int32_t find_peak(const int32_t *in, int len) {
+static inline int32_t find_peak(DCAEncContext *c, const int32_t *in, int len)
+{
     int sample;
     int32_t m = 0;
     for (sample = 0; sample < len; sample++) {
         int32_t s = abs(in[sample]);
-        if (m < s) {
+        if (m < s)
             m = s;
-        }
     }
-    return get_cb(m);
+    return get_cb(c, m);
 }
 
 static void find_peaks(DCAEncContext *c)
@@ -621,14 +556,13 @@ static void find_peaks(DCAEncContext *c)
     int band, ch;
 
     for (ch = 0; ch < c->fullband_channels; ch++) {
-        for (band = 0; band < 32; band++) {
-            c->peak_cb[ch][band] = find_peak(c->subband[ch][band], SUBBAND_SAMPLES);
-        }
+        for (band = 0; band < 32; band++)
+            c->peak_cb[ch][band] = find_peak(c, c->subband[ch][band],
+                                             SUBBAND_SAMPLES);
     }
 
-    if (c->lfe_channel) {
-        c->lfe_peak_cb = find_peak(c->downsampled_lfe, DCA_LFE_SAMPLES);
-    }
+    if (c->lfe_channel)
+        c->lfe_peak_cb = find_peak(c, c->downsampled_lfe, DCA_LFE_SAMPLES);
 }
 
 static void adpcm_analysis(DCAEncContext *c)
@@ -642,11 +576,12 @@ static void adpcm_analysis(DCAEncContext *c)
     for (ch = 0; ch < c->fullband_channels; ch++) {
         for (band = 0; band < 32; band++) {
             samples = c->subband[ch][band] - DCA_ADPCM_COEFFS;
-            pred_vq_id = ff_dcaadpcm_subband_analysis(&c->adpcm_ctx, samples, SUBBAND_SAMPLES, estimated_diff);
+            pred_vq_id = ff_dcaadpcm_subband_analysis(&c->adpcm_ctx, samples,
+                                                      SUBBAND_SAMPLES, estimated_diff);
             if (pred_vq_id >= 0) {
                 c->prediction_mode[ch][band] = pred_vq_id;
                 c->consumed_adpcm_bits += 12; //12 bits to transmit prediction vq index
-                c->diff_peak_cb[ch][band] = find_peak(estimated_diff, 16);
+                c->diff_peak_cb[ch][band] = find_peak(c, estimated_diff, 16);
             } else {
                 c->prediction_mode[ch][band] = -1;
             }
@@ -658,7 +593,7 @@ static const int snr_fudge = 128;
 #define USED_1ABITS 1
 #define USED_26ABITS 4
 
-static inline int32_t get_step_size(const DCAEncContext *c, int ch, int band)
+static inline int32_t get_step_size(DCAEncContext *c, int ch, int band)
 {
     int32_t step_size;
 
@@ -670,7 +605,8 @@ static inline int32_t get_step_size(const DCAEncContext *c, int ch, int band)
     return step_size;
 }
 
-static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
+static int calc_one_scale(DCAEncContext *c, int32_t peak_cb, int abits,
+                          softfloat *quant)
 {
     int32_t peak;
     int our_nscale, try_remove;
@@ -680,7 +616,7 @@ static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
     av_assert0(peak_cb >= -2047);
 
     our_nscale = 127;
-    peak = cb_to_level[-peak_cb];
+    peak = c->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)
@@ -706,15 +642,17 @@ static inline void quantize_adpcm_subband(DCAEncContext *c, int ch, int band)
 {
     int32_t step_size;
     int32_t diff_peak_cb = c->diff_peak_cb[ch][band];
-    c->scale_factor[ch][band] = calc_one_scale(diff_peak_cb,
+    c->scale_factor[ch][band] = calc_one_scale(c, diff_peak_cb,
                                                c->abits[ch][band],
                                                &c->quant[ch][band]);
 
     step_size = get_step_size(c, ch, band);
     ff_dcaadpcm_do_real(c->prediction_mode[ch][band],
-                        c->quant[ch][band], ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], step_size,
-                        c->adpcm_history[ch][band], c->subband[ch][band], c->adpcm_history[ch][band]+4, c->quantized[ch][band],
-                        SUBBAND_SAMPLES, cb_to_level[-diff_peak_cb]);
+                        c->quant[ch][band],
+                        ff_dca_scale_factor_quant7[c->scale_factor[ch][band]],
+                        step_size, c->adpcm_history[ch][band], c->subband[ch][band],
+                        c->adpcm_history[ch][band] + 4, c->quantized[ch][band],
+                        SUBBAND_SAMPLES, c->cb_to_level[-diff_peak_cb]);
 }
 
 static void quantize_adpcm(DCAEncContext *c)
@@ -731,21 +669,31 @@ static void quantize_pcm(DCAEncContext *c)
 {
     int sample, band, ch;
 
-    for (ch = 0; ch < c->fullband_channels; ch++)
-        for (band = 0; band < 32; band++)
-            if (c->prediction_mode[ch][band] == -1)
-                for (sample = 0; sample < SUBBAND_SAMPLES; sample++)
-                    c->quantized[ch][band][sample] = quantize_value(c->subband[ch][band][sample], c->quant[ch][band]);
+    for (ch = 0; ch < c->fullband_channels; ch++) {
+        for (band = 0; band < 32; band++) {
+            if (c->prediction_mode[ch][band] == -1) {
+                for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
+                    int32_t val = quantize_value(c->subband[ch][band][sample],
+                                                 c->quant[ch][band]);
+                    c->quantized[ch][band][sample] = val;
+                }
+            }
+        }
+    }
 }
 
-static void accumulate_huff_bit_consumption(int abits, int32_t *quantized, uint32_t *result)
+static void accumulate_huff_bit_consumption(int abits, int32_t *quantized,
+                                            uint32_t *result)
 {
     uint8_t sel, id = abits - 1;
     for (sel = 0; sel < ff_dca_quant_index_group_size[id]; sel++)
-        result[sel] += ff_dca_vlc_calc_quant_bits(quantized, SUBBAND_SAMPLES, sel, id);
+        result[sel] += ff_dca_vlc_calc_quant_bits(quantized, SUBBAND_SAMPLES,
+                                                  sel, id);
 }
 
-static uint32_t set_best_code(uint32_t vlc_bits[DCA_CODE_BOOKS][7], uint32_t clc_bits[DCA_CODE_BOOKS], int32_t res[DCA_CODE_BOOKS])
+static uint32_t set_best_code(uint32_t vlc_bits[DCA_CODE_BOOKS][7],
+                              uint32_t clc_bits[DCA_CODE_BOOKS],
+                              int32_t res[DCA_CODE_BOOKS])
 {
     uint8_t i, sel;
     uint32_t best_sel_bits[DCA_CODE_BOOKS];
@@ -784,7 +732,8 @@ static uint32_t set_best_code(uint32_t vlc_bits[DCA_CODE_BOOKS][7], uint32_t clc
     return bits;
 }
 
-static uint32_t set_best_abits_code(int abits[DCAENC_SUBBANDS], int bands, int32_t *res)
+static uint32_t set_best_abits_code(int abits[DCAENC_SUBBANDS], int bands,
+                                    int32_t *res)
 {
     uint8_t i;
     uint32_t t;
@@ -845,7 +794,8 @@ static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
                 ret &= ~(USED_26ABITS | USED_1ABITS);
             }
         }
-        c->consumed_bits += set_best_abits_code(c->abits[ch], 32, &c->bit_allocation_sel[ch]);
+        c->consumed_bits += set_best_abits_code(c->abits[ch], 32,
+                                                &c->bit_allocation_sel[ch]);
     }
 
     /* Recalc scale_factor each time to get bits consumption in case of Huffman coding.
@@ -854,7 +804,7 @@ static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
     for (ch = 0; ch < c->fullband_channels; ch++) {
         for (band = 0; band < 32; band++) {
             if (c->prediction_mode[ch][band] == -1) {
-                c->scale_factor[ch][band] = calc_one_scale(c->peak_cb[ch][band],
+                c->scale_factor[ch][band] = calc_one_scale(c, c->peak_cb[ch][band],
                                                            c->abits[ch][band],
                                                            &c->quant[ch][band]);
             }
@@ -868,7 +818,9 @@ static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
     for (ch = 0; ch < c->fullband_channels; ch++) {
         for (band = 0; band < 32; band++) {
             if (c->abits[ch][band] && c->abits[ch][band] <= DCA_CODE_BOOKS) {
-                accumulate_huff_bit_consumption(c->abits[ch][band], c->quantized[ch][band], huff_bit_count_accum[ch][c->abits[ch][band] - 1]);
+                accumulate_huff_bit_consumption(c->abits[ch][band],
+                                                c->quantized[ch][band],
+                                                huff_bit_count_accum[ch][c->abits[ch][band] - 1]);
                 clc_bit_count_accum[ch][c->abits[ch][band] - 1] += bit_consumption[c->abits[ch][band]];
             } else {
                 bits_counter += bit_consumption[c->abits[ch][band]];
@@ -877,7 +829,9 @@ static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
     }
 
     for (ch = 0; ch < c->fullband_channels; ch++) {
-        bits_counter += set_best_code(huff_bit_count_accum[ch], clc_bit_count_accum[ch], c->quant_index_sel[ch]);
+        bits_counter += set_best_code(huff_bit_count_accum[ch],
+                                      clc_bit_count_accum[ch],
+                                      c->quant_index_sel[ch]);
     }
 
     c->consumed_bits += bits_counter;
@@ -954,7 +908,8 @@ static void fill_in_adpcm_bufer(DCAEncContext *c)
                 step_size = get_step_size(c, ch, band);
 
                 ff_dca_core_dequantize(c->adpcm_history[ch][band],
-                                       c->quantized[ch][band]+12, step_size, ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], 0, 4);
+                                       c->quantized[ch][band]+12, step_size,
+                                       ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], 0, 4);
             } else {
                 AV_COPY128U(c->adpcm_history[ch][band], c->adpcm_history[ch][band]+4);
             }
@@ -977,7 +932,7 @@ static void fill_in_adpcm_bufer(DCAEncContext *c)
 static void calc_lfe_scales(DCAEncContext *c)
 {
     if (c->lfe_channel)
-        c->lfe_scale_factor = calc_one_scale(c->lfe_peak_cb, 11, &c->lfe_quant);
+        c->lfe_scale_factor = calc_one_scale(c, c->lfe_peak_cb, 11, &c->lfe_quant);
 }
 
 static void put_frame_header(DCAEncContext *c)
@@ -1118,7 +1073,8 @@ static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
         sel = c->quant_index_sel[ch][c->abits[ch][band] - 1];
         // Huffman codes
         if (sel < ff_dca_quant_index_group_size[c->abits[ch][band] - 1]) {
-            ff_dca_vlc_enc_quant(&c->pb, &c->quantized[ch][band][ss * 8], 8, sel, c->abits[ch][band] - 1);
+            ff_dca_vlc_enc_quant(&c->pb, &c->quantized[ch][band][ss * 8], 8,
+                                 sel, c->abits[ch][band] - 1);
             return;
         }
 
@@ -1171,7 +1127,8 @@ static void put_subframe(DCAEncContext *c, int subframe)
                 put_bits(&c->pb, 5, c->abits[ch][band]);
             }
         } else {
-            ff_dca_vlc_enc_alloc(&c->pb, c->abits[ch], DCAENC_SUBBANDS, c->bit_allocation_sel[ch]);
+            ff_dca_vlc_enc_alloc(&c->pb, c->abits[ch], DCAENC_SUBBANDS,
+                                 c->bit_allocation_sel[ch]);
         }
     }
 
@@ -1287,6 +1244,7 @@ AVCodec ff_dca_encoder = {
     .close                 = encode_close,
     .encode2               = encode_frame,
     .capabilities          = AV_CODEC_CAP_EXPERIMENTAL,
+    .caps_internal         = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
     .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
                                                             AV_SAMPLE_FMT_NONE },
     .supported_samplerates = sample_rates,