Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * DCA encoder
  3.  * Copyright (C) 2008-2012 Alexander E. Patrakov
  4.  *               2010 Benjamin Larsson
  5.  *               2011 Xiang Wang
  6.  *
  7.  * This file is part of FFmpeg.
  8.  *
  9.  * FFmpeg is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Lesser General Public
  11.  * License as published by the Free Software Foundation; either
  12.  * version 2.1 of the License, or (at your option) any later version.
  13.  *
  14.  * FFmpeg is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.  * Lesser General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU Lesser General Public
  20.  * License along with FFmpeg; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22.  */
  23.  
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/common.h"
  27. #include "avcodec.h"
  28. #include "dca.h"
  29. #include "dcadata.h"
  30. #include "dcaenc.h"
  31. #include "internal.h"
  32. #include "put_bits.h"
  33.  
  34. #define MAX_CHANNELS 6
  35. #define DCA_MAX_FRAME_SIZE 16384
  36. #define DCA_HEADER_SIZE 13
  37. #define DCA_LFE_SAMPLES 8
  38.  
  39. #define DCA_SUBBANDS 32
  40. #define SUBFRAMES 1
  41. #define SUBSUBFRAMES 2
  42. #define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
  43. #define AUBANDS 25
  44.  
  45. typedef struct DCAContext {
  46.     PutBitContext pb;
  47.     int frame_size;
  48.     int frame_bits;
  49.     int fullband_channels;
  50.     int channels;
  51.     int lfe_channel;
  52.     int samplerate_index;
  53.     int bitrate_index;
  54.     int channel_config;
  55.     const int32_t *band_interpolation;
  56.     const int32_t *band_spectrum;
  57.     int lfe_scale_factor;
  58.     softfloat lfe_quant;
  59.     int32_t lfe_peak_cb;
  60.  
  61.     int32_t history[512][MAX_CHANNELS]; /* This is a circular buffer */
  62.     int32_t subband[SUBBAND_SAMPLES][DCA_SUBBANDS][MAX_CHANNELS];
  63.     int32_t quantized[SUBBAND_SAMPLES][DCA_SUBBANDS][MAX_CHANNELS];
  64.     int32_t peak_cb[DCA_SUBBANDS][MAX_CHANNELS];
  65.     int32_t downsampled_lfe[DCA_LFE_SAMPLES];
  66.     int32_t masking_curve_cb[SUBSUBFRAMES][256];
  67.     int abits[DCA_SUBBANDS][MAX_CHANNELS];
  68.     int scale_factor[DCA_SUBBANDS][MAX_CHANNELS];
  69.     softfloat quant[DCA_SUBBANDS][MAX_CHANNELS];
  70.     int32_t eff_masking_curve_cb[256];
  71.     int32_t band_masking_cb[32];
  72.     int32_t worst_quantization_noise;
  73.     int32_t worst_noise_ever;
  74.     int consumed_bits;
  75. } DCAContext;
  76.  
  77. static int32_t cos_table[2048];
  78. static int32_t band_interpolation[2][512];
  79. static int32_t band_spectrum[2][8];
  80. static int32_t auf[9][AUBANDS][256];
  81. static int32_t cb_to_add[256];
  82. static int32_t cb_to_level[2048];
  83. static int32_t lfe_fir_64i[512];
  84.  
  85. /* Transfer function of outer and middle ear, Hz -> dB */
  86. static double hom(double f)
  87. {
  88.     double f1 = f / 1000;
  89.  
  90.     return -3.64 * pow(f1, -0.8)
  91.            + 6.8 * exp(-0.6 * (f1 - 3.4) * (f1 - 3.4))
  92.            - 6.0 * exp(-0.15 * (f1 - 8.7) * (f1 - 8.7))
  93.            - 0.0006 * (f1 * f1) * (f1 * f1);
  94. }
  95.  
  96. static double gammafilter(int i, double f)
  97. {
  98.     double h = (f - fc[i]) / erb[i];
  99.  
  100.     h = 1 + h * h;
  101.     h = 1 / (h * h);
  102.     return 20 * log10(h);
  103. }
  104.  
  105. static int encode_init(AVCodecContext *avctx)
  106. {
  107.     DCAContext *c = avctx->priv_data;
  108.     uint64_t layout = avctx->channel_layout;
  109.     int i, min_frame_bits;
  110.  
  111.     c->fullband_channels = c->channels = avctx->channels;
  112.     c->lfe_channel = (avctx->channels == 3 || avctx->channels == 6);
  113.     c->band_interpolation = band_interpolation[1];
  114.     c->band_spectrum = band_spectrum[1];
  115.     c->worst_quantization_noise = -2047;
  116.     c->worst_noise_ever = -2047;
  117.  
  118.     if (!layout) {
  119.         av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
  120.                                       "encoder will guess the layout, but it "
  121.                                       "might be incorrect.\n");
  122.         layout = av_get_default_channel_layout(avctx->channels);
  123.     }
  124.     switch (layout) {
  125.     case AV_CH_LAYOUT_MONO:         c->channel_config = 0; break;
  126.     case AV_CH_LAYOUT_STEREO:       c->channel_config = 2; break;
  127.     case AV_CH_LAYOUT_2_2:          c->channel_config = 8; break;
  128.     case AV_CH_LAYOUT_5POINT0:      c->channel_config = 9; break;
  129.     case AV_CH_LAYOUT_5POINT1:      c->channel_config = 9; break;
  130.     default:
  131.         av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout!\n");
  132.         return AVERROR_PATCHWELCOME;
  133.     }
  134.  
  135.     if (c->lfe_channel)
  136.         c->fullband_channels--;
  137.  
  138.     for (i = 0; i < 9; i++) {
  139.         if (sample_rates[i] == avctx->sample_rate)
  140.             break;
  141.     }
  142.     if (i == 9)
  143.         return AVERROR(EINVAL);
  144.     c->samplerate_index = i;
  145.  
  146.     if (avctx->bit_rate < 32000 || avctx->bit_rate > 3840000) {
  147.         av_log(avctx, AV_LOG_ERROR, "Bit rate %i not supported.", avctx->bit_rate);
  148.         return AVERROR(EINVAL);
  149.     }
  150.     for (i = 0; dca_bit_rates[i] < avctx->bit_rate; i++)
  151.         ;
  152.     c->bitrate_index = i;
  153.     avctx->bit_rate = dca_bit_rates[i];
  154.     c->frame_bits = FFALIGN((avctx->bit_rate * 512 + avctx->sample_rate - 1) / avctx->sample_rate, 32);
  155.     min_frame_bits = 132 + (493 + 28 * 32) * c->fullband_channels + c->lfe_channel * 72;
  156.     if (c->frame_bits < min_frame_bits || c->frame_bits > (DCA_MAX_FRAME_SIZE << 3))
  157.         return AVERROR(EINVAL);
  158.  
  159.     c->frame_size = (c->frame_bits + 7) / 8;
  160.  
  161.     avctx->frame_size = 32 * SUBBAND_SAMPLES;
  162.  
  163.     if (!cos_table[0]) {
  164.         int j, k;
  165.  
  166.         for (i = 0; i < 2048; i++) {
  167.             cos_table[i]   = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
  168.             cb_to_level[i] = (int32_t)(0x7fffffff * pow(10, -0.005 * i));
  169.         }
  170.  
  171.         /* FIXME: probably incorrect */
  172.         for (i = 0; i < 256; i++) {
  173.             lfe_fir_64i[i] = (int32_t)(0x01ffffff * lfe_fir_64[i]);
  174.             lfe_fir_64i[511 - i] = (int32_t)(0x01ffffff * lfe_fir_64[i]);
  175.         }
  176.  
  177.         for (i = 0; i < 512; i++) {
  178.             band_interpolation[0][i] = (int32_t)(0x1000000000ULL * fir_32bands_perfect[i]);
  179.             band_interpolation[1][i] = (int32_t)(0x1000000000ULL * fir_32bands_nonperfect[i]);
  180.         }
  181.  
  182.         for (i = 0; i < 9; i++) {
  183.             for (j = 0; j < AUBANDS; j++) {
  184.                 for (k = 0; k < 256; k++) {
  185.                     double freq = sample_rates[i] * (k + 0.5) / 512;
  186.  
  187.                     auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
  188.                 }
  189.             }
  190.         }
  191.  
  192.         for (i = 0; i < 256; i++) {
  193.             double add = 1 + pow(10, -0.01 * i);
  194.             cb_to_add[i] = (int32_t)(100 * log10(add));
  195.         }
  196.         for (j = 0; j < 8; j++) {
  197.             double accum = 0;
  198.             for (i = 0; i < 512; i++) {
  199.                 double reconst = fir_32bands_perfect[i] * ((i & 64) ? (-1) : 1);
  200.                 accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
  201.             }
  202.             band_spectrum[0][j] = (int32_t)(200 * log10(accum));
  203.         }
  204.         for (j = 0; j < 8; j++) {
  205.             double accum = 0;
  206.             for (i = 0; i < 512; i++) {
  207.                 double reconst = fir_32bands_nonperfect[i] * ((i & 64) ? (-1) : 1);
  208.                 accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
  209.             }
  210.             band_spectrum[1][j] = (int32_t)(200 * log10(accum));
  211.         }
  212.     }
  213.     return 0;
  214. }
  215.  
  216. static inline int32_t cos_t(int x)
  217. {
  218.     return cos_table[x & 2047];
  219. }
  220.  
  221. static inline int32_t sin_t(int x)
  222. {
  223.     return cos_t(x - 512);
  224. }
  225.  
  226. static inline int32_t half32(int32_t a)
  227. {
  228.     return (a + 1) >> 1;
  229. }
  230.  
  231. static inline int32_t mul32(int32_t a, int32_t b)
  232. {
  233.     int64_t r = (int64_t)a * b + 0x80000000ULL;
  234.     return r >> 32;
  235. }
  236.  
  237. static void subband_transform(DCAContext *c, const int32_t *input)
  238. {
  239.     int ch, subs, i, k, j;
  240.  
  241.     for (ch = 0; ch < c->fullband_channels; ch++) {
  242.         /* History is copied because it is also needed for PSY */
  243.         int32_t hist[512];
  244.         int hist_start = 0;
  245.  
  246.         for (i = 0; i < 512; i++)
  247.             hist[i] = c->history[i][ch];
  248.  
  249.         for (subs = 0; subs < SUBBAND_SAMPLES; subs++) {
  250.             int32_t accum[64];
  251.             int32_t resp;
  252.             int band;
  253.  
  254.             /* Calculate the convolutions at once */
  255.             for (i = 0; i < 64; i++)
  256.                 accum[i] = 0;
  257.  
  258.             for (k = 0, i = hist_start, j = 0;
  259.                     i < 512; k = (k + 1) & 63, i++, j++)
  260.                 accum[k] += mul32(hist[i], c->band_interpolation[j]);
  261.             for (i = 0; i < hist_start; k = (k + 1) & 63, i++, j++)
  262.                 accum[k] += mul32(hist[i], c->band_interpolation[j]);
  263.  
  264.             for (k = 16; k < 32; k++)
  265.                 accum[k] = accum[k] - accum[31 - k];
  266.             for (k = 32; k < 48; k++)
  267.                 accum[k] = accum[k] + accum[95 - k];
  268.  
  269.             for (band = 0; band < 32; band++) {
  270.                 resp = 0;
  271.                 for (i = 16; i < 48; i++) {
  272.                     int s = (2 * band + 1) * (2 * (i + 16) + 1);
  273.                     resp += mul32(accum[i], cos_t(s << 3)) >> 3;
  274.                 }
  275.  
  276.                 c->subband[subs][band][ch] = ((band + 1) & 2) ? -resp : resp;
  277.             }
  278.  
  279.             /* Copy in 32 new samples from input */
  280.             for (i = 0; i < 32; i++)
  281.                 hist[i + hist_start] = input[(subs * 32 + i) * c->channels + ch];
  282.             hist_start = (hist_start + 32) & 511;
  283.         }
  284.     }
  285. }
  286.  
  287. static void lfe_downsample(DCAContext *c, const int32_t *input)
  288. {
  289.     /* FIXME: make 128x LFE downsampling possible */
  290.     int i, j, lfes;
  291.     int32_t hist[512];
  292.     int32_t accum;
  293.     int hist_start = 0;
  294.  
  295.     for (i = 0; i < 512; i++)
  296.         hist[i] = c->history[i][c->channels - 1];
  297.  
  298.     for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) {
  299.         /* Calculate the convolution */
  300.         accum = 0;
  301.  
  302.         for (i = hist_start, j = 0; i < 512; i++, j++)
  303.             accum += mul32(hist[i], lfe_fir_64i[j]);
  304.         for (i = 0; i < hist_start; i++, j++)
  305.             accum += mul32(hist[i], lfe_fir_64i[j]);
  306.  
  307.         c->downsampled_lfe[lfes] = accum;
  308.  
  309.         /* Copy in 64 new samples from input */
  310.         for (i = 0; i < 64; i++)
  311.             hist[i + hist_start] = input[(lfes * 64 + i) * c->channels + c->channels - 1];
  312.  
  313.         hist_start = (hist_start + 64) & 511;
  314.     }
  315. }
  316.  
  317. typedef struct {
  318.     int32_t re;
  319.     int32_t im;
  320. } cplx32;
  321.  
  322. static void fft(const int32_t in[2 * 256], cplx32 out[256])
  323. {
  324.     cplx32 buf[256], rin[256], rout[256];
  325.     int i, j, k, l;
  326.  
  327.     /* do two transforms in parallel */
  328.     for (i = 0; i < 256; i++) {
  329.         /* Apply the Hann window */
  330.         rin[i].re = mul32(in[2 * i], 0x3fffffff - (cos_t(8 * i + 2) >> 1));
  331.         rin[i].im = mul32(in[2 * i + 1], 0x3fffffff - (cos_t(8 * i + 6) >> 1));
  332.     }
  333.     /* pre-rotation */
  334.     for (i = 0; i < 256; i++) {
  335.         buf[i].re = mul32(cos_t(4 * i + 2), rin[i].re)
  336.                   - mul32(sin_t(4 * i + 2), rin[i].im);
  337.         buf[i].im = mul32(cos_t(4 * i + 2), rin[i].im)
  338.                   + mul32(sin_t(4 * i + 2), rin[i].re);
  339.     }
  340.  
  341.     for (j = 256, l = 1; j != 1; j >>= 1, l <<= 1) {
  342.         for (k = 0; k < 256; k += j) {
  343.             for (i = k; i < k + j / 2; i++) {
  344.                 cplx32 sum, diff;
  345.                 int t = 8 * l * i;
  346.  
  347.                 sum.re = buf[i].re + buf[i + j / 2].re;
  348.                 sum.im = buf[i].im + buf[i + j / 2].im;
  349.  
  350.                 diff.re = buf[i].re - buf[i + j / 2].re;
  351.                 diff.im = buf[i].im - buf[i + j / 2].im;
  352.  
  353.                 buf[i].re = half32(sum.re);
  354.                 buf[i].im = half32(sum.im);
  355.  
  356.                 buf[i + j / 2].re = mul32(diff.re, cos_t(t))
  357.                                   - mul32(diff.im, sin_t(t));
  358.                 buf[i + j / 2].im = mul32(diff.im, cos_t(t))
  359.                                   + mul32(diff.re, sin_t(t));
  360.             }
  361.         }
  362.     }
  363.     /* post-rotation */
  364.     for (i = 0; i < 256; i++) {
  365.         int b = ff_reverse[i];
  366.         rout[i].re = mul32(buf[b].re, cos_t(4 * i))
  367.                    - mul32(buf[b].im, sin_t(4 * i));
  368.         rout[i].im = mul32(buf[b].im, cos_t(4 * i))
  369.                    + mul32(buf[b].re, sin_t(4 * i));
  370.     }
  371.     for (i = 0; i < 256; i++) {
  372.         /* separate the results of the two transforms */
  373.         cplx32 o1, o2;
  374.  
  375.         o1.re =  rout[i].re - rout[255 - i].re;
  376.         o1.im =  rout[i].im + rout[255 - i].im;
  377.  
  378.         o2.re =  rout[i].im - rout[255 - i].im;
  379.         o2.im = -rout[i].re - rout[255 - i].re;
  380.  
  381.         /* combine them into one long transform */
  382.         out[i].re = mul32( o1.re + o2.re, cos_t(2 * i + 1))
  383.                   + mul32( o1.im - o2.im, sin_t(2 * i + 1));
  384.         out[i].im = mul32( o1.im + o2.im, cos_t(2 * i + 1))
  385.                   + mul32(-o1.re + o2.re, sin_t(2 * i + 1));
  386.     }
  387. }
  388.  
  389. static int32_t get_cb(int32_t in)
  390. {
  391.     int i, res;
  392.  
  393.     res = 0;
  394.     if (in < 0)
  395.         in = -in;
  396.     for (i = 1024; i > 0; i >>= 1) {
  397.         if (cb_to_level[i + res] >= in)
  398.             res += i;
  399.     }
  400.     return -res;
  401. }
  402.  
  403. static int32_t add_cb(int32_t a, int32_t b)
  404. {
  405.     if (a < b)
  406.         FFSWAP(int32_t, a, b);
  407.  
  408.     if (a - b >= 256)
  409.         return a;
  410.     return a + cb_to_add[a - b];
  411. }
  412.  
  413. static void adjust_jnd(int samplerate_index,
  414.                        const int32_t in[512], int32_t out_cb[256])
  415. {
  416.     int32_t power[256];
  417.     cplx32 out[256];
  418.     int32_t out_cb_unnorm[256];
  419.     int32_t denom;
  420.     const int32_t ca_cb = -1114;
  421.     const int32_t cs_cb = 928;
  422.     int i, j;
  423.  
  424.     fft(in, out);
  425.  
  426.     for (j = 0; j < 256; j++) {
  427.         power[j] = add_cb(get_cb(out[j].re), get_cb(out[j].im));
  428.         out_cb_unnorm[j] = -2047; /* and can only grow */
  429.     }
  430.  
  431.     for (i = 0; i < AUBANDS; i++) {
  432.         denom = ca_cb; /* and can only grow */
  433.         for (j = 0; j < 256; j++)
  434.             denom = add_cb(denom, power[j] + auf[samplerate_index][i][j]);
  435.         for (j = 0; j < 256; j++)
  436.             out_cb_unnorm[j] = add_cb(out_cb_unnorm[j],
  437.                     -denom + auf[samplerate_index][i][j]);
  438.     }
  439.  
  440.     for (j = 0; j < 256; j++)
  441.         out_cb[j] = add_cb(out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
  442. }
  443.  
  444. typedef void (*walk_band_t)(DCAContext *c, int band1, int band2, int f,
  445.                             int32_t spectrum1, int32_t spectrum2, int channel,
  446.                             int32_t * arg);
  447.  
  448. static void walk_band_low(DCAContext *c, int band, int channel,
  449.                           walk_band_t walk, int32_t *arg)
  450. {
  451.     int f;
  452.  
  453.     if (band == 0) {
  454.         for (f = 0; f < 4; f++)
  455.             walk(c, 0, 0, f, 0, -2047, channel, arg);
  456.     } else {
  457.         for (f = 0; f < 8; f++)
  458.             walk(c, band, band - 1, 8 * band - 4 + f,
  459.                     c->band_spectrum[7 - f], c->band_spectrum[f], channel, arg);
  460.     }
  461. }
  462.  
  463. static void walk_band_high(DCAContext *c, int band, int channel,
  464.                            walk_band_t walk, int32_t *arg)
  465. {
  466.     int f;
  467.  
  468.     if (band == 31) {
  469.         for (f = 0; f < 4; f++)
  470.             walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
  471.     } else {
  472.         for (f = 0; f < 8; f++)
  473.             walk(c, band, band + 1, 8 * band + 4 + f,
  474.                     c->band_spectrum[f], c->band_spectrum[7 - f], channel, arg);
  475.     }
  476. }
  477.  
  478. static void update_band_masking(DCAContext *c, int band1, int band2,
  479.                                 int f, int32_t spectrum1, int32_t spectrum2,
  480.                                 int channel, int32_t * arg)
  481. {
  482.     int32_t value = c->eff_masking_curve_cb[f] - spectrum1;
  483.  
  484.     if (value < c->band_masking_cb[band1])
  485.         c->band_masking_cb[band1] = value;
  486. }
  487.  
  488. static void calc_masking(DCAContext *c, const int32_t *input)
  489. {
  490.     int i, k, band, ch, ssf;
  491.     int32_t data[512];
  492.  
  493.     for (i = 0; i < 256; i++)
  494.         for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
  495.             c->masking_curve_cb[ssf][i] = -2047;
  496.  
  497.     for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
  498.         for (ch = 0; ch < c->fullband_channels; ch++) {
  499.             for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
  500.                 data[i] = c->history[k][ch];
  501.             for (k -= 512; i < 512; i++, k++)
  502.                 data[i] = input[k * c->channels + ch];
  503.             adjust_jnd(c->samplerate_index, data, c->masking_curve_cb[ssf]);
  504.         }
  505.     for (i = 0; i < 256; i++) {
  506.         int32_t m = 2048;
  507.  
  508.         for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
  509.             if (c->masking_curve_cb[ssf][i] < m)
  510.                 m = c->masking_curve_cb[ssf][i];
  511.         c->eff_masking_curve_cb[i] = m;
  512.     }
  513.  
  514.     for (band = 0; band < 32; band++) {
  515.         c->band_masking_cb[band] = 2048;
  516.         walk_band_low(c, band, 0, update_band_masking, NULL);
  517.         walk_band_high(c, band, 0, update_band_masking, NULL);
  518.     }
  519. }
  520.  
  521. static void find_peaks(DCAContext *c)
  522. {
  523.     int band, ch;
  524.  
  525.     for (band = 0; band < 32; band++)
  526.         for (ch = 0; ch < c->fullband_channels; ch++) {
  527.             int sample;
  528.             int32_t m = 0;
  529.  
  530.             for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
  531.                 int32_t s = abs(c->subband[sample][band][ch]);
  532.                 if (m < s)
  533.                     m = s;
  534.             }
  535.             c->peak_cb[band][ch] = get_cb(m);
  536.         }
  537.  
  538.     if (c->lfe_channel) {
  539.         int sample;
  540.         int32_t m = 0;
  541.  
  542.         for (sample = 0; sample < DCA_LFE_SAMPLES; sample++)
  543.             if (m < abs(c->downsampled_lfe[sample]))
  544.                 m = abs(c->downsampled_lfe[sample]);
  545.         c->lfe_peak_cb = get_cb(m);
  546.     }
  547. }
  548.  
  549. static const int snr_fudge = 128;
  550. #define USED_1ABITS 1
  551. #define USED_NABITS 2
  552. #define USED_26ABITS 4
  553.  
  554. static int init_quantization_noise(DCAContext *c, int noise)
  555. {
  556.     int ch, band, ret = 0;
  557.  
  558.     c->consumed_bits = 132 + 493 * c->fullband_channels;
  559.     if (c->lfe_channel)
  560.         c->consumed_bits += 72;
  561.  
  562.     /* attempt to guess the bit distribution based on the prevoius frame */
  563.     for (ch = 0; ch < c->fullband_channels; ch++) {
  564.         for (band = 0; band < 32; band++) {
  565.             int snr_cb = c->peak_cb[band][ch] - c->band_masking_cb[band] - noise;
  566.  
  567.             if (snr_cb >= 1312) {
  568.                 c->abits[band][ch] = 26;
  569.                 ret |= USED_26ABITS;
  570.             } else if (snr_cb >= 222) {
  571.                 c->abits[band][ch] = 8 + mul32(snr_cb - 222, 69000000);
  572.                 ret |= USED_NABITS;
  573.             } else if (snr_cb >= 0) {
  574.                 c->abits[band][ch] = 2 + mul32(snr_cb, 106000000);
  575.                 ret |= USED_NABITS;
  576.             } else {
  577.                 c->abits[band][ch] = 1;
  578.                 ret |= USED_1ABITS;
  579.             }
  580.         }
  581.     }
  582.  
  583.     for (band = 0; band < 32; band++)
  584.         for (ch = 0; ch < c->fullband_channels; ch++) {
  585.             c->consumed_bits += bit_consumption[c->abits[band][ch]];
  586.         }
  587.  
  588.     return ret;
  589. }
  590.  
  591. static void assign_bits(DCAContext *c)
  592. {
  593.     /* Find the bounds where the binary search should work */
  594.     int low, high, down;
  595.     int used_abits = 0;
  596.  
  597.     init_quantization_noise(c, c->worst_quantization_noise);
  598.     low = high = c->worst_quantization_noise;
  599.     if (c->consumed_bits > c->frame_bits) {
  600.         while (c->consumed_bits > c->frame_bits) {
  601.             av_assert0(used_abits != USED_1ABITS);
  602.             low = high;
  603.             high += snr_fudge;
  604.             used_abits = init_quantization_noise(c, high);
  605.         }
  606.     } else {
  607.         while (c->consumed_bits <= c->frame_bits) {
  608.             high = low;
  609.             if (used_abits == USED_26ABITS)
  610.                 goto out; /* The requested bitrate is too high, pad with zeros */
  611.             low -= snr_fudge;
  612.             used_abits = init_quantization_noise(c, low);
  613.         }
  614.     }
  615.  
  616.     /* Now do a binary search between low and high to see what fits */
  617.     for (down = snr_fudge >> 1; down; down >>= 1) {
  618.         init_quantization_noise(c, high - down);
  619.         if (c->consumed_bits <= c->frame_bits)
  620.             high -= down;
  621.     }
  622.     init_quantization_noise(c, high);
  623. out:
  624.     c->worst_quantization_noise = high;
  625.     if (high > c->worst_noise_ever)
  626.         c->worst_noise_ever = high;
  627. }
  628.  
  629. static void shift_history(DCAContext *c, const int32_t *input)
  630. {
  631.     int k, ch;
  632.  
  633.     for (k = 0; k < 512; k++)
  634.         for (ch = 0; ch < c->channels; ch++)
  635.             c->history[k][ch] = input[k * c->channels + ch];
  636. }
  637.  
  638. static int32_t quantize_value(int32_t value, softfloat quant)
  639. {
  640.     int32_t offset = 1 << (quant.e - 1);
  641.  
  642.     value = mul32(value, quant.m) + offset;
  643.     value = value >> quant.e;
  644.     return value;
  645. }
  646.  
  647. static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
  648. {
  649.     int32_t peak;
  650.     int our_nscale, try_remove;
  651.     softfloat our_quant;
  652.  
  653.     av_assert0(peak_cb <= 0);
  654.     av_assert0(peak_cb >= -2047);
  655.  
  656.     our_nscale = 127;
  657.     peak = cb_to_level[-peak_cb];
  658.  
  659.     for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
  660.         if (scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e <= 17)
  661.             continue;
  662.         our_quant.m = mul32(scalefactor_inv[our_nscale - try_remove].m, stepsize_inv[abits].m);
  663.         our_quant.e = scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e - 17;
  664.         if ((quant_levels[abits] - 1) / 2 < quantize_value(peak, our_quant))
  665.             continue;
  666.         our_nscale -= try_remove;
  667.     }
  668.  
  669.     if (our_nscale >= 125)
  670.         our_nscale = 124;
  671.  
  672.     quant->m = mul32(scalefactor_inv[our_nscale].m, stepsize_inv[abits].m);
  673.     quant->e = scalefactor_inv[our_nscale].e + stepsize_inv[abits].e - 17;
  674.     av_assert0((quant_levels[abits] - 1) / 2 >= quantize_value(peak, *quant));
  675.  
  676.     return our_nscale;
  677. }
  678.  
  679. static void calc_scales(DCAContext *c)
  680. {
  681.     int band, ch;
  682.  
  683.     for (band = 0; band < 32; band++)
  684.         for (ch = 0; ch < c->fullband_channels; ch++)
  685.             c->scale_factor[band][ch] = calc_one_scale(c->peak_cb[band][ch],
  686.                                                        c->abits[band][ch],
  687.                                                        &c->quant[band][ch]);
  688.  
  689.     if (c->lfe_channel)
  690.         c->lfe_scale_factor = calc_one_scale(c->lfe_peak_cb, 11, &c->lfe_quant);
  691. }
  692.  
  693. static void quantize_all(DCAContext *c)
  694. {
  695.     int sample, band, ch;
  696.  
  697.     for (sample = 0; sample < SUBBAND_SAMPLES; sample++)
  698.         for (band = 0; band < 32; band++)
  699.             for (ch = 0; ch < c->fullband_channels; ch++)
  700.                 c->quantized[sample][band][ch] = quantize_value(c->subband[sample][band][ch], c->quant[band][ch]);
  701. }
  702.  
  703. static void put_frame_header(DCAContext *c)
  704. {
  705.     /* SYNC */
  706.     put_bits(&c->pb, 16, 0x7ffe);
  707.     put_bits(&c->pb, 16, 0x8001);
  708.  
  709.     /* Frame type: normal */
  710.     put_bits(&c->pb, 1, 1);
  711.  
  712.     /* Deficit sample count: none */
  713.     put_bits(&c->pb, 5, 31);
  714.  
  715.     /* CRC is not present */
  716.     put_bits(&c->pb, 1, 0);
  717.  
  718.     /* Number of PCM sample blocks */
  719.     put_bits(&c->pb, 7, SUBBAND_SAMPLES - 1);
  720.  
  721.     /* Primary frame byte size */
  722.     put_bits(&c->pb, 14, c->frame_size - 1);
  723.  
  724.     /* Audio channel arrangement */
  725.     put_bits(&c->pb, 6, c->channel_config);
  726.  
  727.     /* Core audio sampling frequency */
  728.     put_bits(&c->pb, 4, bitstream_sfreq[c->samplerate_index]);
  729.  
  730.     /* Transmission bit rate */
  731.     put_bits(&c->pb, 5, c->bitrate_index);
  732.  
  733.     /* Embedded down mix: disabled */
  734.     put_bits(&c->pb, 1, 0);
  735.  
  736.     /* Embedded dynamic range flag: not present */
  737.     put_bits(&c->pb, 1, 0);
  738.  
  739.     /* Embedded time stamp flag: not present */
  740.     put_bits(&c->pb, 1, 0);
  741.  
  742.     /* Auxiliary data flag: not present */
  743.     put_bits(&c->pb, 1, 0);
  744.  
  745.     /* HDCD source: no */
  746.     put_bits(&c->pb, 1, 0);
  747.  
  748.     /* Extension audio ID: N/A */
  749.     put_bits(&c->pb, 3, 0);
  750.  
  751.     /* Extended audio data: not present */
  752.     put_bits(&c->pb, 1, 0);
  753.  
  754.     /* Audio sync word insertion flag: after each sub-frame */
  755.     put_bits(&c->pb, 1, 0);
  756.  
  757.     /* Low frequency effects flag: not present or 64x subsampling */
  758.     put_bits(&c->pb, 2, c->lfe_channel ? 2 : 0);
  759.  
  760.     /* Predictor history switch flag: on */
  761.     put_bits(&c->pb, 1, 1);
  762.  
  763.     /* No CRC */
  764.     /* Multirate interpolator switch: non-perfect reconstruction */
  765.     put_bits(&c->pb, 1, 0);
  766.  
  767.     /* Encoder software revision: 7 */
  768.     put_bits(&c->pb, 4, 7);
  769.  
  770.     /* Copy history: 0 */
  771.     put_bits(&c->pb, 2, 0);
  772.  
  773.     /* Source PCM resolution: 16 bits, not DTS ES */
  774.     put_bits(&c->pb, 3, 0);
  775.  
  776.     /* Front sum/difference coding: no */
  777.     put_bits(&c->pb, 1, 0);
  778.  
  779.     /* Surrounds sum/difference coding: no */
  780.     put_bits(&c->pb, 1, 0);
  781.  
  782.     /* Dialog normalization: 0 dB */
  783.     put_bits(&c->pb, 4, 0);
  784. }
  785.  
  786. static void put_primary_audio_header(DCAContext *c)
  787. {
  788.     static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
  789.     static const int thr[11]    = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
  790.  
  791.     int ch, i;
  792.     /* Number of subframes */
  793.     put_bits(&c->pb, 4, SUBFRAMES - 1);
  794.  
  795.     /* Number of primary audio channels */
  796.     put_bits(&c->pb, 3, c->fullband_channels - 1);
  797.  
  798.     /* Subband activity count */
  799.     for (ch = 0; ch < c->fullband_channels; ch++)
  800.         put_bits(&c->pb, 5, DCA_SUBBANDS - 2);
  801.  
  802.     /* High frequency VQ start subband */
  803.     for (ch = 0; ch < c->fullband_channels; ch++)
  804.         put_bits(&c->pb, 5, DCA_SUBBANDS - 1);
  805.  
  806.     /* Joint intensity coding index: 0, 0 */
  807.     for (ch = 0; ch < c->fullband_channels; ch++)
  808.         put_bits(&c->pb, 3, 0);
  809.  
  810.     /* Transient mode codebook: A4, A4 (arbitrary) */
  811.     for (ch = 0; ch < c->fullband_channels; ch++)
  812.         put_bits(&c->pb, 2, 0);
  813.  
  814.     /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
  815.     for (ch = 0; ch < c->fullband_channels; ch++)
  816.         put_bits(&c->pb, 3, 6);
  817.  
  818.     /* Bit allocation quantizer select: linear 5-bit */
  819.     for (ch = 0; ch < c->fullband_channels; ch++)
  820.         put_bits(&c->pb, 3, 6);
  821.  
  822.     /* Quantization index codebook select: dummy data
  823.        to avoid transmission of scale factor adjustment */
  824.     for (i = 1; i < 11; i++)
  825.         for (ch = 0; ch < c->fullband_channels; ch++)
  826.             put_bits(&c->pb, bitlen[i], thr[i]);
  827.  
  828.     /* Scale factor adjustment index: not transmitted */
  829.     /* Audio header CRC check word: not transmitted */
  830. }
  831.  
  832. static void put_subframe_samples(DCAContext *c, int ss, int band, int ch)
  833. {
  834.     if (c->abits[band][ch] <= 7) {
  835.         int sum, i, j;
  836.         for (i = 0; i < 8; i += 4) {
  837.             sum = 0;
  838.             for (j = 3; j >= 0; j--) {
  839.                 sum *= quant_levels[c->abits[band][ch]];
  840.                 sum += c->quantized[ss * 8 + i + j][band][ch];
  841.                 sum += (quant_levels[c->abits[band][ch]] - 1) / 2;
  842.             }
  843.             put_bits(&c->pb, bit_consumption[c->abits[band][ch]] / 4, sum);
  844.         }
  845.     } else {
  846.         int i;
  847.         for (i = 0; i < 8; i++) {
  848.             int bits = bit_consumption[c->abits[band][ch]] / 16;
  849.             int32_t mask = (1 << bits) - 1;
  850.             put_bits(&c->pb, bits, c->quantized[ss * 8 + i][band][ch] & mask);
  851.         }
  852.     }
  853. }
  854.  
  855. static void put_subframe(DCAContext *c, int subframe)
  856. {
  857.     int i, band, ss, ch;
  858.  
  859.     /* Subsubframes count */
  860.     put_bits(&c->pb, 2, SUBSUBFRAMES -1);
  861.  
  862.     /* Partial subsubframe sample count: dummy */
  863.     put_bits(&c->pb, 3, 0);
  864.  
  865.     /* Prediction mode: no ADPCM, in each channel and subband */
  866.     for (ch = 0; ch < c->fullband_channels; ch++)
  867.         for (band = 0; band < DCA_SUBBANDS; band++)
  868.             put_bits(&c->pb, 1, 0);
  869.  
  870.     /* Prediction VQ addres: not transmitted */
  871.     /* Bit allocation index */
  872.     for (ch = 0; ch < c->fullband_channels; ch++)
  873.         for (band = 0; band < DCA_SUBBANDS; band++)
  874.             put_bits(&c->pb, 5, c->abits[band][ch]);
  875.  
  876.     if (SUBSUBFRAMES > 1) {
  877.         /* Transition mode: none for each channel and subband */
  878.         for (ch = 0; ch < c->fullband_channels; ch++)
  879.             for (band = 0; band < DCA_SUBBANDS; band++)
  880.                 put_bits(&c->pb, 1, 0); /* codebook A4 */
  881.     }
  882.  
  883.     /* Scale factors */
  884.     for (ch = 0; ch < c->fullband_channels; ch++)
  885.         for (band = 0; band < DCA_SUBBANDS; band++)
  886.             put_bits(&c->pb, 7, c->scale_factor[band][ch]);
  887.  
  888.     /* Joint subband scale factor codebook select: not transmitted */
  889.     /* Scale factors for joint subband coding: not transmitted */
  890.     /* Stereo down-mix coefficients: not transmitted */
  891.     /* Dynamic range coefficient: not transmitted */
  892.     /* Stde information CRC check word: not transmitted */
  893.     /* VQ encoded high frequency subbands: not transmitted */
  894.  
  895.     /* LFE data: 8 samples and scalefactor */
  896.     if (c->lfe_channel) {
  897.         for (i = 0; i < DCA_LFE_SAMPLES; i++)
  898.             put_bits(&c->pb, 8, quantize_value(c->downsampled_lfe[i], c->lfe_quant) & 0xff);
  899.         put_bits(&c->pb, 8, c->lfe_scale_factor);
  900.     }
  901.  
  902.     /* Audio data (subsubframes) */
  903.     for (ss = 0; ss < SUBSUBFRAMES ; ss++)
  904.         for (ch = 0; ch < c->fullband_channels; ch++)
  905.             for (band = 0; band < DCA_SUBBANDS; band++)
  906.                     put_subframe_samples(c, ss, band, ch);
  907.  
  908.     /* DSYNC */
  909.     put_bits(&c->pb, 16, 0xffff);
  910. }
  911.  
  912. static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  913.                         const AVFrame *frame, int *got_packet_ptr)
  914. {
  915.     DCAContext *c = avctx->priv_data;
  916.     const int32_t *samples;
  917.     int ret, i;
  918.  
  919.     if ((ret = ff_alloc_packet2(avctx, avpkt, c->frame_size )) < 0)
  920.         return ret;
  921.  
  922.     samples = (const int32_t *)frame->data[0];
  923.  
  924.     subband_transform(c, samples);
  925.     if (c->lfe_channel)
  926.         lfe_downsample(c, samples);
  927.  
  928.     calc_masking(c, samples);
  929.     find_peaks(c);
  930.     assign_bits(c);
  931.     calc_scales(c);
  932.     quantize_all(c);
  933.     shift_history(c, samples);
  934.  
  935.     init_put_bits(&c->pb, avpkt->data, avpkt->size);
  936.     put_frame_header(c);
  937.     put_primary_audio_header(c);
  938.     for (i = 0; i < SUBFRAMES; i++)
  939.         put_subframe(c, i);
  940.  
  941.     flush_put_bits(&c->pb);
  942.  
  943.     avpkt->pts      = frame->pts;
  944.     avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
  945.     avpkt->size     = c->frame_size + 1;
  946.     *got_packet_ptr = 1;
  947.     return 0;
  948. }
  949.  
  950. static const AVCodecDefault defaults[] = {
  951.     { "b",          "1411200" },
  952.     { NULL },
  953. };
  954.  
  955. AVCodec ff_dca_encoder = {
  956.     .name                  = "dca",
  957.     .long_name             = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  958.     .type                  = AVMEDIA_TYPE_AUDIO,
  959.     .id                    = AV_CODEC_ID_DTS,
  960.     .priv_data_size        = sizeof(DCAContext),
  961.     .init                  = encode_init,
  962.     .encode2               = encode_frame,
  963.     .capabilities          = CODEC_CAP_EXPERIMENTAL,
  964.     .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
  965.                                                             AV_SAMPLE_FMT_NONE },
  966.     .supported_samplerates = sample_rates,
  967.     .channel_layouts       = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
  968.                                                   AV_CH_LAYOUT_STEREO,
  969.                                                   AV_CH_LAYOUT_2_2,
  970.                                                   AV_CH_LAYOUT_5POINT0,
  971.                                                   AV_CH_LAYOUT_5POINT1,
  972.                                                   0 },
  973.     .defaults              = defaults,
  974. };
  975.