3 * Copyright (C) 2008-2012 Alexander E. Patrakov
4 * 2010 Benjamin Larsson
7 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/avassert.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/common.h"
34 #define MAX_CHANNELS 6
35 #define DCA_MAX_FRAME_SIZE 16384
36 #define DCA_HEADER_SIZE 13
37 #define DCA_LFE_SAMPLES 8
39 #define DCA_SUBBANDS 32
41 #define SUBSUBFRAMES 2
42 #define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
45 typedef struct DCAContext {
49 int fullband_channels;
55 const int32_t *band_interpolation;
56 const int32_t *band_spectrum;
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;
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];
85 /* Transfer function of outer and middle ear, Hz -> dB */
86 static double hom(double f)
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);
96 static double gammafilter(int i, double f)
98 double h = (f - fc[i]) / erb[i];
102 return 20 * log10(h);
105 static int encode_init(AVCodecContext *avctx)
107 DCAContext *c = avctx->priv_data;
108 uint64_t layout = avctx->channel_layout;
109 int i, min_frame_bits;
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;
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);
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;
131 av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout!\n");
132 return AVERROR_PATCHWELCOME;
136 c->fullband_channels--;
138 for (i = 0; i < 9; i++) {
139 if (sample_rates[i] == avctx->sample_rate)
143 return AVERROR(EINVAL);
144 c->samplerate_index = i;
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);
150 for (i = 0; dca_bit_rates[i] < avctx->bit_rate; i++)
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);
159 c->frame_size = (c->frame_bits + 7) / 8;
161 avctx->frame_size = 32 * SUBBAND_SAMPLES;
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));
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]);
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]);
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;
187 auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
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));
196 for (j = 0; j < 8; j++) {
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);
202 band_spectrum[0][j] = (int32_t)(200 * log10(accum));
204 for (j = 0; j < 8; j++) {
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);
210 band_spectrum[1][j] = (int32_t)(200 * log10(accum));
216 static inline int32_t cos_t(int x)
218 return cos_table[x & 2047];
221 static inline int32_t sin_t(int x)
223 return cos_t(x - 512);
226 static inline int32_t half32(int32_t a)
231 static inline int32_t mul32(int32_t a, int32_t b)
233 int64_t r = (int64_t)a * b + 0x80000000ULL;
237 static void subband_transform(DCAContext *c, const int32_t *input)
239 int ch, subs, i, k, j;
241 for (ch = 0; ch < c->fullband_channels; ch++) {
242 /* History is copied because it is also needed for PSY */
246 for (i = 0; i < 512; i++)
247 hist[i] = c->history[i][ch];
249 for (subs = 0; subs < SUBBAND_SAMPLES; subs++) {
254 /* Calculate the convolutions at once */
255 for (i = 0; i < 64; i++)
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]);
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];
269 for (band = 0; band < 32; band++) {
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;
276 c->subband[subs][band][ch] = ((band + 1) & 2) ? -resp : resp;
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;
287 static void lfe_downsample(DCAContext *c, const int32_t *input)
289 /* FIXME: make 128x LFE downsampling possible */
295 for (i = 0; i < 512; i++)
296 hist[i] = c->history[i][c->channels - 1];
298 for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) {
299 /* Calculate the convolution */
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]);
307 c->downsampled_lfe[lfes] = accum;
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];
313 hist_start = (hist_start + 64) & 511;
322 static void fft(const int32_t in[2 * 256], cplx32 out[256])
324 cplx32 buf[256], rin[256], rout[256];
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));
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);
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++) {
347 sum.re = buf[i].re + buf[i + j / 2].re;
348 sum.im = buf[i].im + buf[i + j / 2].im;
350 diff.re = buf[i].re - buf[i + j / 2].re;
351 diff.im = buf[i].im - buf[i + j / 2].im;
353 buf[i].re = half32(sum.re);
354 buf[i].im = half32(sum.im);
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));
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));
371 for (i = 0; i < 256; i++) {
372 /* separate the results of the two transforms */
375 o1.re = rout[i].re - rout[255 - i].re;
376 o1.im = rout[i].im + rout[255 - i].im;
378 o2.re = rout[i].im - rout[255 - i].im;
379 o2.im = -rout[i].re - rout[255 - i].re;
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));
389 static int32_t get_cb(int32_t in)
396 for (i = 1024; i > 0; i >>= 1) {
397 if (cb_to_level[i + res] >= in)
403 static int32_t add_cb(int32_t a, int32_t b)
406 FFSWAP(int32_t, a, b);
410 return a + cb_to_add[a - b];
413 static void adjust_jnd(int samplerate_index,
414 const int32_t in[512], int32_t out_cb[256])
418 int32_t out_cb_unnorm[256];
420 const int32_t ca_cb = -1114;
421 const int32_t cs_cb = 928;
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 */
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]);
440 for (j = 0; j < 256; j++)
441 out_cb[j] = add_cb(out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
444 typedef void (*walk_band_t)(DCAContext *c, int band1, int band2, int f,
445 int32_t spectrum1, int32_t spectrum2, int channel,
448 static void walk_band_low(DCAContext *c, int band, int channel,
449 walk_band_t walk, int32_t *arg)
454 for (f = 0; f < 4; f++)
455 walk(c, 0, 0, f, 0, -2047, channel, arg);
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);
463 static void walk_band_high(DCAContext *c, int band, int channel,
464 walk_band_t walk, int32_t *arg)
469 for (f = 0; f < 4; f++)
470 walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
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);
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)
482 int32_t value = c->eff_masking_curve_cb[f] - spectrum1;
484 if (value < c->band_masking_cb[band1])
485 c->band_masking_cb[band1] = value;
488 static void calc_masking(DCAContext *c, const int32_t *input)
490 int i, k, band, ch, ssf;
493 for (i = 0; i < 256; i++)
494 for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
495 c->masking_curve_cb[ssf][i] = -2047;
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]);
505 for (i = 0; i < 256; i++) {
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;
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);
521 static void find_peaks(DCAContext *c)
525 for (band = 0; band < 32; band++)
526 for (ch = 0; ch < c->fullband_channels; ch++) {
530 for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
531 int32_t s = abs(c->subband[sample][band][ch]);
535 c->peak_cb[band][ch] = get_cb(m);
538 if (c->lfe_channel) {
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);
549 static const int snr_fudge = 128;
550 #define USED_1ABITS 1
551 #define USED_NABITS 2
552 #define USED_26ABITS 4
554 static int init_quantization_noise(DCAContext *c, int noise)
556 int ch, band, ret = 0;
558 c->consumed_bits = 132 + 493 * c->fullband_channels;
560 c->consumed_bits += 72;
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;
567 if (snr_cb >= 1312) {
568 c->abits[band][ch] = 26;
570 } else if (snr_cb >= 222) {
571 c->abits[band][ch] = 8 + mul32(snr_cb - 222, 69000000);
573 } else if (snr_cb >= 0) {
574 c->abits[band][ch] = 2 + mul32(snr_cb, 106000000);
577 c->abits[band][ch] = 1;
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]];
591 static void assign_bits(DCAContext *c)
593 /* Find the bounds where the binary search should work */
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);
604 used_abits = init_quantization_noise(c, high);
607 while (c->consumed_bits <= c->frame_bits) {
609 if (used_abits == USED_26ABITS)
610 goto out; /* The requested bitrate is too high, pad with zeros */
612 used_abits = init_quantization_noise(c, low);
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)
622 init_quantization_noise(c, high);
624 c->worst_quantization_noise = high;
625 if (high > c->worst_noise_ever)
626 c->worst_noise_ever = high;
629 static void shift_history(DCAContext *c, const int32_t *input)
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];
638 static int32_t quantize_value(int32_t value, softfloat quant)
640 int32_t offset = 1 << (quant.e - 1);
642 value = mul32(value, quant.m) + offset;
643 value = value >> quant.e;
647 static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
650 int our_nscale, try_remove;
653 av_assert0(peak_cb <= 0);
654 av_assert0(peak_cb >= -2047);
657 peak = cb_to_level[-peak_cb];
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)
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))
666 our_nscale -= try_remove;
669 if (our_nscale >= 125)
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));
679 static void calc_scales(DCAContext *c)
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],
687 &c->quant[band][ch]);
690 c->lfe_scale_factor = calc_one_scale(c->lfe_peak_cb, 11, &c->lfe_quant);
693 static void quantize_all(DCAContext *c)
695 int sample, band, ch;
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]);
703 static void put_frame_header(DCAContext *c)
706 put_bits(&c->pb, 16, 0x7ffe);
707 put_bits(&c->pb, 16, 0x8001);
709 /* Frame type: normal */
710 put_bits(&c->pb, 1, 1);
712 /* Deficit sample count: none */
713 put_bits(&c->pb, 5, 31);
715 /* CRC is not present */
716 put_bits(&c->pb, 1, 0);
718 /* Number of PCM sample blocks */
719 put_bits(&c->pb, 7, SUBBAND_SAMPLES - 1);
721 /* Primary frame byte size */
722 put_bits(&c->pb, 14, c->frame_size - 1);
724 /* Audio channel arrangement */
725 put_bits(&c->pb, 6, c->channel_config);
727 /* Core audio sampling frequency */
728 put_bits(&c->pb, 4, bitstream_sfreq[c->samplerate_index]);
730 /* Transmission bit rate */
731 put_bits(&c->pb, 5, c->bitrate_index);
733 /* Embedded down mix: disabled */
734 put_bits(&c->pb, 1, 0);
736 /* Embedded dynamic range flag: not present */
737 put_bits(&c->pb, 1, 0);
739 /* Embedded time stamp flag: not present */
740 put_bits(&c->pb, 1, 0);
742 /* Auxiliary data flag: not present */
743 put_bits(&c->pb, 1, 0);
745 /* HDCD source: no */
746 put_bits(&c->pb, 1, 0);
748 /* Extension audio ID: N/A */
749 put_bits(&c->pb, 3, 0);
751 /* Extended audio data: not present */
752 put_bits(&c->pb, 1, 0);
754 /* Audio sync word insertion flag: after each sub-frame */
755 put_bits(&c->pb, 1, 0);
757 /* Low frequency effects flag: not present or 64x subsampling */
758 put_bits(&c->pb, 2, c->lfe_channel ? 2 : 0);
760 /* Predictor history switch flag: on */
761 put_bits(&c->pb, 1, 1);
764 /* Multirate interpolator switch: non-perfect reconstruction */
765 put_bits(&c->pb, 1, 0);
767 /* Encoder software revision: 7 */
768 put_bits(&c->pb, 4, 7);
770 /* Copy history: 0 */
771 put_bits(&c->pb, 2, 0);
773 /* Source PCM resolution: 16 bits, not DTS ES */
774 put_bits(&c->pb, 3, 0);
776 /* Front sum/difference coding: no */
777 put_bits(&c->pb, 1, 0);
779 /* Surrounds sum/difference coding: no */
780 put_bits(&c->pb, 1, 0);
782 /* Dialog normalization: 0 dB */
783 put_bits(&c->pb, 4, 0);
786 static void put_primary_audio_header(DCAContext *c)
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 };
792 /* Number of subframes */
793 put_bits(&c->pb, 4, SUBFRAMES - 1);
795 /* Number of primary audio channels */
796 put_bits(&c->pb, 3, c->fullband_channels - 1);
798 /* Subband activity count */
799 for (ch = 0; ch < c->fullband_channels; ch++)
800 put_bits(&c->pb, 5, DCA_SUBBANDS - 2);
802 /* High frequency VQ start subband */
803 for (ch = 0; ch < c->fullband_channels; ch++)
804 put_bits(&c->pb, 5, DCA_SUBBANDS - 1);
806 /* Joint intensity coding index: 0, 0 */
807 for (ch = 0; ch < c->fullband_channels; ch++)
808 put_bits(&c->pb, 3, 0);
810 /* Transient mode codebook: A4, A4 (arbitrary) */
811 for (ch = 0; ch < c->fullband_channels; ch++)
812 put_bits(&c->pb, 2, 0);
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);
818 /* Bit allocation quantizer select: linear 5-bit */
819 for (ch = 0; ch < c->fullband_channels; ch++)
820 put_bits(&c->pb, 3, 6);
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]);
828 /* Scale factor adjustment index: not transmitted */
829 /* Audio header CRC check word: not transmitted */
832 static void put_subframe_samples(DCAContext *c, int ss, int band, int ch)
834 if (c->abits[band][ch] <= 7) {
836 for (i = 0; i < 8; i += 4) {
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;
843 put_bits(&c->pb, bit_consumption[c->abits[band][ch]] / 4, sum);
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);
855 static void put_subframe(DCAContext *c, int subframe)
859 /* Subsubframes count */
860 put_bits(&c->pb, 2, SUBSUBFRAMES -1);
862 /* Partial subsubframe sample count: dummy */
863 put_bits(&c->pb, 3, 0);
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);
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]);
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 */
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]);
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 */
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);
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);
909 put_bits(&c->pb, 16, 0xffff);
912 static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
913 const AVFrame *frame, int *got_packet_ptr)
915 DCAContext *c = avctx->priv_data;
916 const int32_t *samples;
919 if ((ret = ff_alloc_packet2(avctx, avpkt, c->frame_size )) < 0)
922 samples = (const int32_t *)frame->data[0];
924 subband_transform(c, samples);
926 lfe_downsample(c, samples);
928 calc_masking(c, samples);
933 shift_history(c, samples);
935 init_put_bits(&c->pb, avpkt->data, avpkt->size);
937 put_primary_audio_header(c);
938 for (i = 0; i < SUBFRAMES; i++)
941 flush_put_bits(&c->pb);
943 avpkt->pts = frame->pts;
944 avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
945 avpkt->size = c->frame_size + 1;
950 static const AVCodecDefault defaults[] = {
955 AVCodec ff_dca_encoder = {
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),
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,
970 AV_CH_LAYOUT_5POINT0,
971 AV_CH_LAYOUT_5POINT1,
973 .defaults = defaults,