You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1302 lines
42KB

  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. #include "libavutil/avassert.h"
  24. #include "libavutil/channel_layout.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/ffmath.h"
  27. #include "libavutil/opt.h"
  28. #include "avcodec.h"
  29. #include "dca.h"
  30. #include "dcaadpcm.h"
  31. #include "dcamath.h"
  32. #include "dca_core.h"
  33. #include "dcadata.h"
  34. #include "dcaenc.h"
  35. #include "internal.h"
  36. #include "mathops.h"
  37. #include "put_bits.h"
  38. #define MAX_CHANNELS 6
  39. #define DCA_MAX_FRAME_SIZE 16384
  40. #define DCA_HEADER_SIZE 13
  41. #define DCA_LFE_SAMPLES 8
  42. #define DCAENC_SUBBANDS 32
  43. #define SUBFRAMES 1
  44. #define SUBSUBFRAMES 2
  45. #define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
  46. #define AUBANDS 25
  47. typedef struct CompressionOptions {
  48. int adpcm_mode;
  49. } CompressionOptions;
  50. typedef struct DCAEncContext {
  51. AVClass *class;
  52. PutBitContext pb;
  53. DCAADPCMEncContext adpcm_ctx;
  54. CompressionOptions options;
  55. int frame_size;
  56. int frame_bits;
  57. int fullband_channels;
  58. int channels;
  59. int lfe_channel;
  60. int samplerate_index;
  61. int bitrate_index;
  62. int channel_config;
  63. const int32_t *band_interpolation;
  64. const int32_t *band_spectrum;
  65. int lfe_scale_factor;
  66. softfloat lfe_quant;
  67. int32_t lfe_peak_cb;
  68. const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
  69. int32_t prediction_mode[MAX_CHANNELS][DCAENC_SUBBANDS];
  70. int32_t adpcm_history[MAX_CHANNELS][DCAENC_SUBBANDS][DCA_ADPCM_COEFFS * 2];
  71. int32_t history[MAX_CHANNELS][512]; /* This is a circular buffer */
  72. int32_t *subband[MAX_CHANNELS][DCAENC_SUBBANDS];
  73. int32_t quantized[MAX_CHANNELS][DCAENC_SUBBANDS][SUBBAND_SAMPLES];
  74. int32_t peak_cb[MAX_CHANNELS][DCAENC_SUBBANDS];
  75. int32_t diff_peak_cb[MAX_CHANNELS][DCAENC_SUBBANDS]; ///< expected peak of residual signal
  76. int32_t downsampled_lfe[DCA_LFE_SAMPLES];
  77. int32_t masking_curve_cb[SUBSUBFRAMES][256];
  78. int32_t bit_allocation_sel[MAX_CHANNELS];
  79. int abits[MAX_CHANNELS][DCAENC_SUBBANDS];
  80. int scale_factor[MAX_CHANNELS][DCAENC_SUBBANDS];
  81. softfloat quant[MAX_CHANNELS][DCAENC_SUBBANDS];
  82. int32_t quant_index_sel[MAX_CHANNELS][DCA_CODE_BOOKS];
  83. int32_t eff_masking_curve_cb[256];
  84. int32_t band_masking_cb[32];
  85. int32_t worst_quantization_noise;
  86. int32_t worst_noise_ever;
  87. int consumed_bits;
  88. int consumed_adpcm_bits; ///< Number of bits to transmit ADPCM related info
  89. } DCAEncContext;
  90. static int32_t cos_table[2048];
  91. static int32_t band_interpolation[2][512];
  92. static int32_t band_spectrum[2][8];
  93. static int32_t auf[9][AUBANDS][256];
  94. static int32_t cb_to_add[256];
  95. static int32_t cb_to_level[2048];
  96. static int32_t lfe_fir_64i[512];
  97. /* Transfer function of outer and middle ear, Hz -> dB */
  98. static double hom(double f)
  99. {
  100. double f1 = f / 1000;
  101. return -3.64 * pow(f1, -0.8)
  102. + 6.8 * exp(-0.6 * (f1 - 3.4) * (f1 - 3.4))
  103. - 6.0 * exp(-0.15 * (f1 - 8.7) * (f1 - 8.7))
  104. - 0.0006 * (f1 * f1) * (f1 * f1);
  105. }
  106. static double gammafilter(int i, double f)
  107. {
  108. double h = (f - fc[i]) / erb[i];
  109. h = 1 + h * h;
  110. h = 1 / (h * h);
  111. return 20 * log10(h);
  112. }
  113. static int subband_bufer_alloc(DCAEncContext *c)
  114. {
  115. int ch, band;
  116. int32_t *bufer = av_calloc(MAX_CHANNELS * DCAENC_SUBBANDS *
  117. (SUBBAND_SAMPLES + DCA_ADPCM_COEFFS),
  118. sizeof(int32_t));
  119. if (!bufer)
  120. return -1;
  121. /* we need a place for DCA_ADPCM_COEFF samples from previous frame
  122. * to calc prediction coefficients for each subband */
  123. for (ch = 0; ch < MAX_CHANNELS; ch++) {
  124. for (band = 0; band < DCAENC_SUBBANDS; band++) {
  125. c->subband[ch][band] = bufer +
  126. ch * DCAENC_SUBBANDS * (SUBBAND_SAMPLES + DCA_ADPCM_COEFFS) +
  127. band * (SUBBAND_SAMPLES + DCA_ADPCM_COEFFS) + DCA_ADPCM_COEFFS;
  128. }
  129. }
  130. return 0;
  131. }
  132. static void subband_bufer_free(DCAEncContext *c)
  133. {
  134. int32_t *bufer = c->subband[0][0] - DCA_ADPCM_COEFFS;
  135. av_freep(&bufer);
  136. }
  137. static int encode_init(AVCodecContext *avctx)
  138. {
  139. DCAEncContext *c = avctx->priv_data;
  140. uint64_t layout = avctx->channel_layout;
  141. int i, j, min_frame_bits;
  142. if (subband_bufer_alloc(c))
  143. return AVERROR(ENOMEM);
  144. c->fullband_channels = c->channels = avctx->channels;
  145. c->lfe_channel = (avctx->channels == 3 || avctx->channels == 6);
  146. c->band_interpolation = band_interpolation[1];
  147. c->band_spectrum = band_spectrum[1];
  148. c->worst_quantization_noise = -2047;
  149. c->worst_noise_ever = -2047;
  150. c->consumed_adpcm_bits = 0;
  151. if (ff_dcaadpcm_init(&c->adpcm_ctx))
  152. return AVERROR(ENOMEM);
  153. if (!layout) {
  154. av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
  155. "encoder will guess the layout, but it "
  156. "might be incorrect.\n");
  157. layout = av_get_default_channel_layout(avctx->channels);
  158. }
  159. switch (layout) {
  160. case AV_CH_LAYOUT_MONO: c->channel_config = 0; break;
  161. case AV_CH_LAYOUT_STEREO: c->channel_config = 2; break;
  162. case AV_CH_LAYOUT_2_2: c->channel_config = 8; break;
  163. case AV_CH_LAYOUT_5POINT0: c->channel_config = 9; break;
  164. case AV_CH_LAYOUT_5POINT1: c->channel_config = 9; break;
  165. default:
  166. av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout!\n");
  167. return AVERROR_PATCHWELCOME;
  168. }
  169. if (c->lfe_channel) {
  170. c->fullband_channels--;
  171. c->channel_order_tab = channel_reorder_lfe[c->channel_config];
  172. } else {
  173. c->channel_order_tab = channel_reorder_nolfe[c->channel_config];
  174. }
  175. for (i = 0; i < MAX_CHANNELS; i++) {
  176. for (j = 0; j < DCA_CODE_BOOKS; j++) {
  177. c->quant_index_sel[i][j] = ff_dca_quant_index_group_size[j];
  178. }
  179. /* 6 - no Huffman */
  180. c->bit_allocation_sel[i] = 6;
  181. for (j = 0; j < DCAENC_SUBBANDS; j++) {
  182. /* -1 - no ADPCM */
  183. c->prediction_mode[i][j] = -1;
  184. memset(c->adpcm_history[i][j], 0, sizeof(int32_t)*DCA_ADPCM_COEFFS);
  185. }
  186. }
  187. for (i = 0; i < 9; i++) {
  188. if (sample_rates[i] == avctx->sample_rate)
  189. break;
  190. }
  191. if (i == 9)
  192. return AVERROR(EINVAL);
  193. c->samplerate_index = i;
  194. if (avctx->bit_rate < 32000 || avctx->bit_rate > 3840000) {
  195. av_log(avctx, AV_LOG_ERROR, "Bit rate %"PRId64" not supported.", avctx->bit_rate);
  196. return AVERROR(EINVAL);
  197. }
  198. for (i = 0; ff_dca_bit_rates[i] < avctx->bit_rate; i++)
  199. ;
  200. c->bitrate_index = i;
  201. c->frame_bits = FFALIGN((avctx->bit_rate * 512 + avctx->sample_rate - 1) / avctx->sample_rate, 32);
  202. min_frame_bits = 132 + (493 + 28 * 32) * c->fullband_channels + c->lfe_channel * 72;
  203. if (c->frame_bits < min_frame_bits || c->frame_bits > (DCA_MAX_FRAME_SIZE << 3))
  204. return AVERROR(EINVAL);
  205. c->frame_size = (c->frame_bits + 7) / 8;
  206. avctx->frame_size = 32 * SUBBAND_SAMPLES;
  207. if (!cos_table[0]) {
  208. int j, k;
  209. cos_table[0] = 0x7fffffff;
  210. cos_table[512] = 0;
  211. cos_table[1024] = -cos_table[0];
  212. for (i = 1; i < 512; i++) {
  213. cos_table[i] = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
  214. cos_table[1024-i] = -cos_table[i];
  215. cos_table[1024+i] = -cos_table[i];
  216. cos_table[2048-i] = cos_table[i];
  217. }
  218. for (i = 0; i < 2048; i++) {
  219. cb_to_level[i] = (int32_t)(0x7fffffff * ff_exp10(-0.005 * i));
  220. }
  221. for (k = 0; k < 32; k++) {
  222. for (j = 0; j < 8; j++) {
  223. lfe_fir_64i[64 * j + k] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
  224. lfe_fir_64i[64 * (7-j) + (63 - k)] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
  225. }
  226. }
  227. for (i = 0; i < 512; i++) {
  228. band_interpolation[0][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_perfect[i]);
  229. band_interpolation[1][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_nonperfect[i]);
  230. }
  231. for (i = 0; i < 9; i++) {
  232. for (j = 0; j < AUBANDS; j++) {
  233. for (k = 0; k < 256; k++) {
  234. double freq = sample_rates[i] * (k + 0.5) / 512;
  235. auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
  236. }
  237. }
  238. }
  239. for (i = 0; i < 256; i++) {
  240. double add = 1 + ff_exp10(-0.01 * i);
  241. cb_to_add[i] = (int32_t)(100 * log10(add));
  242. }
  243. for (j = 0; j < 8; j++) {
  244. double accum = 0;
  245. for (i = 0; i < 512; i++) {
  246. double reconst = ff_dca_fir_32bands_perfect[i] * ((i & 64) ? (-1) : 1);
  247. accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
  248. }
  249. band_spectrum[0][j] = (int32_t)(200 * log10(accum));
  250. }
  251. for (j = 0; j < 8; j++) {
  252. double accum = 0;
  253. for (i = 0; i < 512; i++) {
  254. double reconst = ff_dca_fir_32bands_nonperfect[i] * ((i & 64) ? (-1) : 1);
  255. accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
  256. }
  257. band_spectrum[1][j] = (int32_t)(200 * log10(accum));
  258. }
  259. }
  260. return 0;
  261. }
  262. static av_cold int encode_close(AVCodecContext *avctx)
  263. {
  264. if (avctx->priv_data) {
  265. DCAEncContext *c = avctx->priv_data;
  266. subband_bufer_free(c);
  267. ff_dcaadpcm_free(&c->adpcm_ctx);
  268. }
  269. return 0;
  270. }
  271. static inline int32_t cos_t(int x)
  272. {
  273. return cos_table[x & 2047];
  274. }
  275. static inline int32_t sin_t(int x)
  276. {
  277. return cos_t(x - 512);
  278. }
  279. static inline int32_t half32(int32_t a)
  280. {
  281. return (a + 1) >> 1;
  282. }
  283. static void subband_transform(DCAEncContext *c, const int32_t *input)
  284. {
  285. int ch, subs, i, k, j;
  286. for (ch = 0; ch < c->fullband_channels; ch++) {
  287. /* History is copied because it is also needed for PSY */
  288. int32_t hist[512];
  289. int hist_start = 0;
  290. const int chi = c->channel_order_tab[ch];
  291. memcpy(hist, &c->history[ch][0], 512 * sizeof(int32_t));
  292. for (subs = 0; subs < SUBBAND_SAMPLES; subs++) {
  293. int32_t accum[64];
  294. int32_t resp;
  295. int band;
  296. /* Calculate the convolutions at once */
  297. memset(accum, 0, 64 * sizeof(int32_t));
  298. for (k = 0, i = hist_start, j = 0;
  299. i < 512; k = (k + 1) & 63, i++, j++)
  300. accum[k] += mul32(hist[i], c->band_interpolation[j]);
  301. for (i = 0; i < hist_start; k = (k + 1) & 63, i++, j++)
  302. accum[k] += mul32(hist[i], c->band_interpolation[j]);
  303. for (k = 16; k < 32; k++)
  304. accum[k] = accum[k] - accum[31 - k];
  305. for (k = 32; k < 48; k++)
  306. accum[k] = accum[k] + accum[95 - k];
  307. for (band = 0; band < 32; band++) {
  308. resp = 0;
  309. for (i = 16; i < 48; i++) {
  310. int s = (2 * band + 1) * (2 * (i + 16) + 1);
  311. resp += mul32(accum[i], cos_t(s << 3)) >> 3;
  312. }
  313. c->subband[ch][band][subs] = ((band + 1) & 2) ? -resp : resp;
  314. }
  315. /* Copy in 32 new samples from input */
  316. for (i = 0; i < 32; i++)
  317. hist[i + hist_start] = input[(subs * 32 + i) * c->channels + chi];
  318. hist_start = (hist_start + 32) & 511;
  319. }
  320. }
  321. }
  322. static void lfe_downsample(DCAEncContext *c, const int32_t *input)
  323. {
  324. /* FIXME: make 128x LFE downsampling possible */
  325. const int lfech = lfe_index[c->channel_config];
  326. int i, j, lfes;
  327. int32_t hist[512];
  328. int32_t accum;
  329. int hist_start = 0;
  330. memcpy(hist, &c->history[c->channels - 1][0], 512 * sizeof(int32_t));
  331. for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) {
  332. /* Calculate the convolution */
  333. accum = 0;
  334. for (i = hist_start, j = 0; i < 512; i++, j++)
  335. accum += mul32(hist[i], lfe_fir_64i[j]);
  336. for (i = 0; i < hist_start; i++, j++)
  337. accum += mul32(hist[i], lfe_fir_64i[j]);
  338. c->downsampled_lfe[lfes] = accum;
  339. /* Copy in 64 new samples from input */
  340. for (i = 0; i < 64; i++)
  341. hist[i + hist_start] = input[(lfes * 64 + i) * c->channels + lfech];
  342. hist_start = (hist_start + 64) & 511;
  343. }
  344. }
  345. typedef struct {
  346. int32_t re;
  347. int32_t im;
  348. } cplx32;
  349. static void fft(const int32_t in[2 * 256], cplx32 out[256])
  350. {
  351. cplx32 buf[256], rin[256], rout[256];
  352. int i, j, k, l;
  353. /* do two transforms in parallel */
  354. for (i = 0; i < 256; i++) {
  355. /* Apply the Hann window */
  356. rin[i].re = mul32(in[2 * i], 0x3fffffff - (cos_t(8 * i + 2) >> 1));
  357. rin[i].im = mul32(in[2 * i + 1], 0x3fffffff - (cos_t(8 * i + 6) >> 1));
  358. }
  359. /* pre-rotation */
  360. for (i = 0; i < 256; i++) {
  361. buf[i].re = mul32(cos_t(4 * i + 2), rin[i].re)
  362. - mul32(sin_t(4 * i + 2), rin[i].im);
  363. buf[i].im = mul32(cos_t(4 * i + 2), rin[i].im)
  364. + mul32(sin_t(4 * i + 2), rin[i].re);
  365. }
  366. for (j = 256, l = 1; j != 1; j >>= 1, l <<= 1) {
  367. for (k = 0; k < 256; k += j) {
  368. for (i = k; i < k + j / 2; i++) {
  369. cplx32 sum, diff;
  370. int t = 8 * l * i;
  371. sum.re = buf[i].re + buf[i + j / 2].re;
  372. sum.im = buf[i].im + buf[i + j / 2].im;
  373. diff.re = buf[i].re - buf[i + j / 2].re;
  374. diff.im = buf[i].im - buf[i + j / 2].im;
  375. buf[i].re = half32(sum.re);
  376. buf[i].im = half32(sum.im);
  377. buf[i + j / 2].re = mul32(diff.re, cos_t(t))
  378. - mul32(diff.im, sin_t(t));
  379. buf[i + j / 2].im = mul32(diff.im, cos_t(t))
  380. + mul32(diff.re, sin_t(t));
  381. }
  382. }
  383. }
  384. /* post-rotation */
  385. for (i = 0; i < 256; i++) {
  386. int b = ff_reverse[i];
  387. rout[i].re = mul32(buf[b].re, cos_t(4 * i))
  388. - mul32(buf[b].im, sin_t(4 * i));
  389. rout[i].im = mul32(buf[b].im, cos_t(4 * i))
  390. + mul32(buf[b].re, sin_t(4 * i));
  391. }
  392. for (i = 0; i < 256; i++) {
  393. /* separate the results of the two transforms */
  394. cplx32 o1, o2;
  395. o1.re = rout[i].re - rout[255 - i].re;
  396. o1.im = rout[i].im + rout[255 - i].im;
  397. o2.re = rout[i].im - rout[255 - i].im;
  398. o2.im = -rout[i].re - rout[255 - i].re;
  399. /* combine them into one long transform */
  400. out[i].re = mul32( o1.re + o2.re, cos_t(2 * i + 1))
  401. + mul32( o1.im - o2.im, sin_t(2 * i + 1));
  402. out[i].im = mul32( o1.im + o2.im, cos_t(2 * i + 1))
  403. + mul32(-o1.re + o2.re, sin_t(2 * i + 1));
  404. }
  405. }
  406. static int32_t get_cb(int32_t in)
  407. {
  408. int i, res;
  409. res = 0;
  410. if (in < 0)
  411. in = -in;
  412. for (i = 1024; i > 0; i >>= 1) {
  413. if (cb_to_level[i + res] >= in)
  414. res += i;
  415. }
  416. return -res;
  417. }
  418. static int32_t add_cb(int32_t a, int32_t b)
  419. {
  420. if (a < b)
  421. FFSWAP(int32_t, a, b);
  422. if (a - b >= 256)
  423. return a;
  424. return a + cb_to_add[a - b];
  425. }
  426. static void adjust_jnd(int samplerate_index,
  427. const int32_t in[512], int32_t out_cb[256])
  428. {
  429. int32_t power[256];
  430. cplx32 out[256];
  431. int32_t out_cb_unnorm[256];
  432. int32_t denom;
  433. const int32_t ca_cb = -1114;
  434. const int32_t cs_cb = 928;
  435. int i, j;
  436. fft(in, out);
  437. for (j = 0; j < 256; j++) {
  438. power[j] = add_cb(get_cb(out[j].re), get_cb(out[j].im));
  439. out_cb_unnorm[j] = -2047; /* and can only grow */
  440. }
  441. for (i = 0; i < AUBANDS; i++) {
  442. denom = ca_cb; /* and can only grow */
  443. for (j = 0; j < 256; j++)
  444. denom = add_cb(denom, power[j] + auf[samplerate_index][i][j]);
  445. for (j = 0; j < 256; j++)
  446. out_cb_unnorm[j] = add_cb(out_cb_unnorm[j],
  447. -denom + auf[samplerate_index][i][j]);
  448. }
  449. for (j = 0; j < 256; j++)
  450. out_cb[j] = add_cb(out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
  451. }
  452. typedef void (*walk_band_t)(DCAEncContext *c, int band1, int band2, int f,
  453. int32_t spectrum1, int32_t spectrum2, int channel,
  454. int32_t * arg);
  455. static void walk_band_low(DCAEncContext *c, int band, int channel,
  456. walk_band_t walk, int32_t *arg)
  457. {
  458. int f;
  459. if (band == 0) {
  460. for (f = 0; f < 4; f++)
  461. walk(c, 0, 0, f, 0, -2047, channel, arg);
  462. } else {
  463. for (f = 0; f < 8; f++)
  464. walk(c, band, band - 1, 8 * band - 4 + f,
  465. c->band_spectrum[7 - f], c->band_spectrum[f], channel, arg);
  466. }
  467. }
  468. static void walk_band_high(DCAEncContext *c, int band, int channel,
  469. walk_band_t walk, int32_t *arg)
  470. {
  471. int f;
  472. if (band == 31) {
  473. for (f = 0; f < 4; f++)
  474. walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
  475. } else {
  476. for (f = 0; f < 8; f++)
  477. walk(c, band, band + 1, 8 * band + 4 + f,
  478. c->band_spectrum[f], c->band_spectrum[7 - f], channel, arg);
  479. }
  480. }
  481. static void update_band_masking(DCAEncContext *c, int band1, int band2,
  482. int f, int32_t spectrum1, int32_t spectrum2,
  483. int channel, int32_t * arg)
  484. {
  485. int32_t value = c->eff_masking_curve_cb[f] - spectrum1;
  486. if (value < c->band_masking_cb[band1])
  487. c->band_masking_cb[band1] = value;
  488. }
  489. static void calc_masking(DCAEncContext *c, const int32_t *input)
  490. {
  491. int i, k, band, ch, ssf;
  492. int32_t data[512];
  493. for (i = 0; i < 256; i++)
  494. for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
  495. c->masking_curve_cb[ssf][i] = -2047;
  496. for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
  497. for (ch = 0; ch < c->fullband_channels; ch++) {
  498. const int chi = c->channel_order_tab[ch];
  499. for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
  500. data[i] = c->history[ch][k];
  501. for (k -= 512; i < 512; i++, k++)
  502. data[i] = input[k * c->channels + chi];
  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. for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
  508. if (c->masking_curve_cb[ssf][i] < m)
  509. m = c->masking_curve_cb[ssf][i];
  510. c->eff_masking_curve_cb[i] = m;
  511. }
  512. for (band = 0; band < 32; band++) {
  513. c->band_masking_cb[band] = 2048;
  514. walk_band_low(c, band, 0, update_band_masking, NULL);
  515. walk_band_high(c, band, 0, update_band_masking, NULL);
  516. }
  517. }
  518. static inline int32_t find_peak(const int32_t *in, int len) {
  519. int sample;
  520. int32_t m = 0;
  521. for (sample = 0; sample < len; sample++) {
  522. int32_t s = abs(in[sample]);
  523. if (m < s) {
  524. m = s;
  525. }
  526. }
  527. return get_cb(m);
  528. }
  529. static void find_peaks(DCAEncContext *c)
  530. {
  531. int band, ch;
  532. for (ch = 0; ch < c->fullband_channels; ch++) {
  533. for (band = 0; band < 32; band++) {
  534. c->peak_cb[ch][band] = find_peak(c->subband[ch][band], SUBBAND_SAMPLES);
  535. }
  536. }
  537. if (c->lfe_channel) {
  538. c->lfe_peak_cb = find_peak(c->downsampled_lfe, DCA_LFE_SAMPLES);
  539. }
  540. }
  541. static void adpcm_analysis(DCAEncContext *c)
  542. {
  543. int ch, band;
  544. int pred_vq_id;
  545. int32_t *samples;
  546. int32_t estimated_diff[SUBBAND_SAMPLES];
  547. c->consumed_adpcm_bits = 0;
  548. for (ch = 0; ch < c->fullband_channels; ch++) {
  549. for (band = 0; band < 32; band++) {
  550. samples = c->subband[ch][band] - DCA_ADPCM_COEFFS;
  551. pred_vq_id = ff_dcaadpcm_subband_analysis(&c->adpcm_ctx, samples, SUBBAND_SAMPLES, estimated_diff);
  552. if (pred_vq_id >= 0) {
  553. c->prediction_mode[ch][band] = pred_vq_id;
  554. c->consumed_adpcm_bits += 12; //12 bits to transmit prediction vq index
  555. c->diff_peak_cb[ch][band] = find_peak(estimated_diff, 16);
  556. } else {
  557. c->prediction_mode[ch][band] = -1;
  558. }
  559. }
  560. }
  561. }
  562. static const int snr_fudge = 128;
  563. #define USED_1ABITS 1
  564. #define USED_26ABITS 4
  565. static inline int32_t get_step_size(const DCAEncContext *c, int ch, int band)
  566. {
  567. int32_t step_size;
  568. if (c->bitrate_index == 3)
  569. step_size = ff_dca_lossless_quant[c->abits[ch][band]];
  570. else
  571. step_size = ff_dca_lossy_quant[c->abits[ch][band]];
  572. return step_size;
  573. }
  574. static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
  575. {
  576. int32_t peak;
  577. int our_nscale, try_remove;
  578. softfloat our_quant;
  579. av_assert0(peak_cb <= 0);
  580. av_assert0(peak_cb >= -2047);
  581. our_nscale = 127;
  582. peak = cb_to_level[-peak_cb];
  583. for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
  584. if (scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e <= 17)
  585. continue;
  586. our_quant.m = mul32(scalefactor_inv[our_nscale - try_remove].m, stepsize_inv[abits].m);
  587. our_quant.e = scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e - 17;
  588. if ((ff_dca_quant_levels[abits] - 1) / 2 < quantize_value(peak, our_quant))
  589. continue;
  590. our_nscale -= try_remove;
  591. }
  592. if (our_nscale >= 125)
  593. our_nscale = 124;
  594. quant->m = mul32(scalefactor_inv[our_nscale].m, stepsize_inv[abits].m);
  595. quant->e = scalefactor_inv[our_nscale].e + stepsize_inv[abits].e - 17;
  596. av_assert0((ff_dca_quant_levels[abits] - 1) / 2 >= quantize_value(peak, *quant));
  597. return our_nscale;
  598. }
  599. static inline void quantize_adpcm_subband(DCAEncContext *c, int ch, int band)
  600. {
  601. int32_t step_size;
  602. int32_t diff_peak_cb = c->diff_peak_cb[ch][band];
  603. c->scale_factor[ch][band] = calc_one_scale(diff_peak_cb,
  604. c->abits[ch][band],
  605. &c->quant[ch][band]);
  606. step_size = get_step_size(c, ch, band);
  607. ff_dcaadpcm_do_real(c->prediction_mode[ch][band],
  608. c->quant[ch][band], ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], step_size,
  609. c->adpcm_history[ch][band], c->subband[ch][band], c->adpcm_history[ch][band]+4, c->quantized[ch][band],
  610. SUBBAND_SAMPLES, cb_to_level[-diff_peak_cb]);
  611. }
  612. static void quantize_adpcm(DCAEncContext *c)
  613. {
  614. int band, ch;
  615. for (ch = 0; ch < c->fullband_channels; ch++)
  616. for (band = 0; band < 32; band++)
  617. if (c->prediction_mode[ch][band] >= 0)
  618. quantize_adpcm_subband(c, ch, band);
  619. }
  620. static void quantize_pcm(DCAEncContext *c)
  621. {
  622. int sample, band, ch;
  623. for (ch = 0; ch < c->fullband_channels; ch++)
  624. for (band = 0; band < 32; band++)
  625. if (c->prediction_mode[ch][band] == -1)
  626. for (sample = 0; sample < SUBBAND_SAMPLES; sample++)
  627. c->quantized[ch][band][sample] = quantize_value(c->subband[ch][band][sample], c->quant[ch][band]);
  628. }
  629. static void accumulate_huff_bit_consumption(int abits, int32_t *quantized, uint32_t *result)
  630. {
  631. uint8_t sel, id = abits - 1;
  632. for (sel = 0; sel < ff_dca_quant_index_group_size[id]; sel++)
  633. result[sel] += ff_dca_vlc_calc_quant_bits(quantized, SUBBAND_SAMPLES, sel, id);
  634. }
  635. 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])
  636. {
  637. uint8_t i, sel;
  638. uint32_t best_sel_bits[DCA_CODE_BOOKS];
  639. int32_t best_sel_id[DCA_CODE_BOOKS];
  640. uint32_t t, bits = 0;
  641. for (i = 0; i < DCA_CODE_BOOKS; i++) {
  642. av_assert0(!((!!vlc_bits[i][0]) ^ (!!clc_bits[i])));
  643. if (vlc_bits[i][0] == 0) {
  644. /* do not transmit adjustment index for empty codebooks */
  645. res[i] = ff_dca_quant_index_group_size[i];
  646. /* and skip it */
  647. continue;
  648. }
  649. best_sel_bits[i] = vlc_bits[i][0];
  650. best_sel_id[i] = 0;
  651. for (sel = 0; sel < ff_dca_quant_index_group_size[i]; sel++) {
  652. if (best_sel_bits[i] > vlc_bits[i][sel] && vlc_bits[i][sel]) {
  653. best_sel_bits[i] = vlc_bits[i][sel];
  654. best_sel_id[i] = sel;
  655. }
  656. }
  657. /* 2 bits to transmit scale factor adjustment index */
  658. t = best_sel_bits[i] + 2;
  659. if (t < clc_bits[i]) {
  660. res[i] = best_sel_id[i];
  661. bits += t;
  662. } else {
  663. res[i] = ff_dca_quant_index_group_size[i];
  664. bits += clc_bits[i];
  665. }
  666. }
  667. return bits;
  668. }
  669. static uint32_t set_best_abits_code(int abits[DCAENC_SUBBANDS], int bands, int32_t *res)
  670. {
  671. uint8_t i;
  672. uint32_t t;
  673. int32_t best_sel = 6;
  674. int32_t best_bits = bands * 5;
  675. /* Check do we have subband which cannot be encoded by Huffman tables */
  676. for (i = 0; i < bands; i++) {
  677. if (abits[i] > 12 || abits[i] == 0) {
  678. *res = best_sel;
  679. return best_bits;
  680. }
  681. }
  682. for (i = 0; i < DCA_BITALLOC_12_COUNT; i++) {
  683. t = ff_dca_vlc_calc_alloc_bits(abits, bands, i);
  684. if (t < best_bits) {
  685. best_bits = t;
  686. best_sel = i;
  687. }
  688. }
  689. *res = best_sel;
  690. return best_bits;
  691. }
  692. static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
  693. {
  694. int ch, band, ret = USED_26ABITS | USED_1ABITS;
  695. uint32_t huff_bit_count_accum[MAX_CHANNELS][DCA_CODE_BOOKS][7];
  696. uint32_t clc_bit_count_accum[MAX_CHANNELS][DCA_CODE_BOOKS];
  697. uint32_t bits_counter = 0;
  698. c->consumed_bits = 132 + 333 * c->fullband_channels;
  699. c->consumed_bits += c->consumed_adpcm_bits;
  700. if (c->lfe_channel)
  701. c->consumed_bits += 72;
  702. /* attempt to guess the bit distribution based on the prevoius frame */
  703. for (ch = 0; ch < c->fullband_channels; ch++) {
  704. for (band = 0; band < 32; band++) {
  705. int snr_cb = c->peak_cb[ch][band] - c->band_masking_cb[band] - noise;
  706. if (snr_cb >= 1312) {
  707. c->abits[ch][band] = 26;
  708. ret &= ~USED_1ABITS;
  709. } else if (snr_cb >= 222) {
  710. c->abits[ch][band] = 8 + mul32(snr_cb - 222, 69000000);
  711. ret &= ~(USED_26ABITS | USED_1ABITS);
  712. } else if (snr_cb >= 0) {
  713. c->abits[ch][band] = 2 + mul32(snr_cb, 106000000);
  714. ret &= ~(USED_26ABITS | USED_1ABITS);
  715. } else if (forbid_zero || snr_cb >= -140) {
  716. c->abits[ch][band] = 1;
  717. ret &= ~USED_26ABITS;
  718. } else {
  719. c->abits[ch][band] = 0;
  720. ret &= ~(USED_26ABITS | USED_1ABITS);
  721. }
  722. }
  723. c->consumed_bits += set_best_abits_code(c->abits[ch], 32, &c->bit_allocation_sel[ch]);
  724. }
  725. /* Recalc scale_factor each time to get bits consumption in case of Huffman coding.
  726. It is suboptimal solution */
  727. /* TODO: May be cache scaled values */
  728. for (ch = 0; ch < c->fullband_channels; ch++) {
  729. for (band = 0; band < 32; band++) {
  730. if (c->prediction_mode[ch][band] == -1) {
  731. c->scale_factor[ch][band] = calc_one_scale(c->peak_cb[ch][band],
  732. c->abits[ch][band],
  733. &c->quant[ch][band]);
  734. }
  735. }
  736. }
  737. quantize_adpcm(c);
  738. quantize_pcm(c);
  739. memset(huff_bit_count_accum, 0, MAX_CHANNELS * DCA_CODE_BOOKS * 7 * sizeof(uint32_t));
  740. memset(clc_bit_count_accum, 0, MAX_CHANNELS * DCA_CODE_BOOKS * sizeof(uint32_t));
  741. for (ch = 0; ch < c->fullband_channels; ch++) {
  742. for (band = 0; band < 32; band++) {
  743. if (c->abits[ch][band] && c->abits[ch][band] <= DCA_CODE_BOOKS) {
  744. accumulate_huff_bit_consumption(c->abits[ch][band], c->quantized[ch][band], huff_bit_count_accum[ch][c->abits[ch][band] - 1]);
  745. clc_bit_count_accum[ch][c->abits[ch][band] - 1] += bit_consumption[c->abits[ch][band]];
  746. } else {
  747. bits_counter += bit_consumption[c->abits[ch][band]];
  748. }
  749. }
  750. }
  751. for (ch = 0; ch < c->fullband_channels; ch++) {
  752. bits_counter += set_best_code(huff_bit_count_accum[ch], clc_bit_count_accum[ch], c->quant_index_sel[ch]);
  753. }
  754. c->consumed_bits += bits_counter;
  755. return ret;
  756. }
  757. static void assign_bits(DCAEncContext *c)
  758. {
  759. /* Find the bounds where the binary search should work */
  760. int low, high, down;
  761. int used_abits = 0;
  762. int forbid_zero = 1;
  763. restart:
  764. init_quantization_noise(c, c->worst_quantization_noise, forbid_zero);
  765. low = high = c->worst_quantization_noise;
  766. if (c->consumed_bits > c->frame_bits) {
  767. while (c->consumed_bits > c->frame_bits) {
  768. if (used_abits == USED_1ABITS && forbid_zero) {
  769. forbid_zero = 0;
  770. goto restart;
  771. }
  772. low = high;
  773. high += snr_fudge;
  774. used_abits = init_quantization_noise(c, high, forbid_zero);
  775. }
  776. } else {
  777. while (c->consumed_bits <= c->frame_bits) {
  778. high = low;
  779. if (used_abits == USED_26ABITS)
  780. goto out; /* The requested bitrate is too high, pad with zeros */
  781. low -= snr_fudge;
  782. used_abits = init_quantization_noise(c, low, forbid_zero);
  783. }
  784. }
  785. /* Now do a binary search between low and high to see what fits */
  786. for (down = snr_fudge >> 1; down; down >>= 1) {
  787. init_quantization_noise(c, high - down, forbid_zero);
  788. if (c->consumed_bits <= c->frame_bits)
  789. high -= down;
  790. }
  791. init_quantization_noise(c, high, forbid_zero);
  792. out:
  793. c->worst_quantization_noise = high;
  794. if (high > c->worst_noise_ever)
  795. c->worst_noise_ever = high;
  796. }
  797. static void shift_history(DCAEncContext *c, const int32_t *input)
  798. {
  799. int k, ch;
  800. for (k = 0; k < 512; k++)
  801. for (ch = 0; ch < c->channels; ch++) {
  802. const int chi = c->channel_order_tab[ch];
  803. c->history[ch][k] = input[k * c->channels + chi];
  804. }
  805. }
  806. static void fill_in_adpcm_bufer(DCAEncContext *c)
  807. {
  808. int ch, band;
  809. int32_t step_size;
  810. /* We fill in ADPCM work buffer for subbands which hasn't been ADPCM coded
  811. * in current frame - we need this data if subband of next frame is
  812. * ADPCM
  813. */
  814. for (ch = 0; ch < c->channels; ch++) {
  815. for (band = 0; band < 32; band++) {
  816. int32_t *samples = c->subband[ch][band] - DCA_ADPCM_COEFFS;
  817. if (c->prediction_mode[ch][band] == -1) {
  818. step_size = get_step_size(c, ch, band);
  819. ff_dca_core_dequantize(c->adpcm_history[ch][band],
  820. c->quantized[ch][band]+12, step_size, ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], 0, 4);
  821. } else {
  822. AV_COPY128U(c->adpcm_history[ch][band], c->adpcm_history[ch][band]+4);
  823. }
  824. /* Copy dequantized values for LPC analysis.
  825. * It reduces artifacts in case of extreme quantization,
  826. * example: in current frame abits is 1 and has no prediction flag,
  827. * but end of this frame is sine like signal. In this case, if LPC analysis uses
  828. * original values, likely LPC analysis returns good prediction gain, and sets prediction flag.
  829. * But there are no proper value in decoder history, so likely result will be no good.
  830. * Bitstream has "Predictor history flag switch", but this flag disables history for all subbands
  831. */
  832. samples[0] = c->adpcm_history[ch][band][0] << 7;
  833. samples[1] = c->adpcm_history[ch][band][1] << 7;
  834. samples[2] = c->adpcm_history[ch][band][2] << 7;
  835. samples[3] = c->adpcm_history[ch][band][3] << 7;
  836. }
  837. }
  838. }
  839. static void calc_lfe_scales(DCAEncContext *c)
  840. {
  841. if (c->lfe_channel)
  842. c->lfe_scale_factor = calc_one_scale(c->lfe_peak_cb, 11, &c->lfe_quant);
  843. }
  844. static void put_frame_header(DCAEncContext *c)
  845. {
  846. /* SYNC */
  847. put_bits(&c->pb, 16, 0x7ffe);
  848. put_bits(&c->pb, 16, 0x8001);
  849. /* Frame type: normal */
  850. put_bits(&c->pb, 1, 1);
  851. /* Deficit sample count: none */
  852. put_bits(&c->pb, 5, 31);
  853. /* CRC is not present */
  854. put_bits(&c->pb, 1, 0);
  855. /* Number of PCM sample blocks */
  856. put_bits(&c->pb, 7, SUBBAND_SAMPLES - 1);
  857. /* Primary frame byte size */
  858. put_bits(&c->pb, 14, c->frame_size - 1);
  859. /* Audio channel arrangement */
  860. put_bits(&c->pb, 6, c->channel_config);
  861. /* Core audio sampling frequency */
  862. put_bits(&c->pb, 4, bitstream_sfreq[c->samplerate_index]);
  863. /* Transmission bit rate */
  864. put_bits(&c->pb, 5, c->bitrate_index);
  865. /* Embedded down mix: disabled */
  866. put_bits(&c->pb, 1, 0);
  867. /* Embedded dynamic range flag: not present */
  868. put_bits(&c->pb, 1, 0);
  869. /* Embedded time stamp flag: not present */
  870. put_bits(&c->pb, 1, 0);
  871. /* Auxiliary data flag: not present */
  872. put_bits(&c->pb, 1, 0);
  873. /* HDCD source: no */
  874. put_bits(&c->pb, 1, 0);
  875. /* Extension audio ID: N/A */
  876. put_bits(&c->pb, 3, 0);
  877. /* Extended audio data: not present */
  878. put_bits(&c->pb, 1, 0);
  879. /* Audio sync word insertion flag: after each sub-frame */
  880. put_bits(&c->pb, 1, 0);
  881. /* Low frequency effects flag: not present or 64x subsampling */
  882. put_bits(&c->pb, 2, c->lfe_channel ? 2 : 0);
  883. /* Predictor history switch flag: on */
  884. put_bits(&c->pb, 1, 1);
  885. /* No CRC */
  886. /* Multirate interpolator switch: non-perfect reconstruction */
  887. put_bits(&c->pb, 1, 0);
  888. /* Encoder software revision: 7 */
  889. put_bits(&c->pb, 4, 7);
  890. /* Copy history: 0 */
  891. put_bits(&c->pb, 2, 0);
  892. /* Source PCM resolution: 16 bits, not DTS ES */
  893. put_bits(&c->pb, 3, 0);
  894. /* Front sum/difference coding: no */
  895. put_bits(&c->pb, 1, 0);
  896. /* Surrounds sum/difference coding: no */
  897. put_bits(&c->pb, 1, 0);
  898. /* Dialog normalization: 0 dB */
  899. put_bits(&c->pb, 4, 0);
  900. }
  901. static void put_primary_audio_header(DCAEncContext *c)
  902. {
  903. int ch, i;
  904. /* Number of subframes */
  905. put_bits(&c->pb, 4, SUBFRAMES - 1);
  906. /* Number of primary audio channels */
  907. put_bits(&c->pb, 3, c->fullband_channels - 1);
  908. /* Subband activity count */
  909. for (ch = 0; ch < c->fullband_channels; ch++)
  910. put_bits(&c->pb, 5, DCAENC_SUBBANDS - 2);
  911. /* High frequency VQ start subband */
  912. for (ch = 0; ch < c->fullband_channels; ch++)
  913. put_bits(&c->pb, 5, DCAENC_SUBBANDS - 1);
  914. /* Joint intensity coding index: 0, 0 */
  915. for (ch = 0; ch < c->fullband_channels; ch++)
  916. put_bits(&c->pb, 3, 0);
  917. /* Transient mode codebook: A4, A4 (arbitrary) */
  918. for (ch = 0; ch < c->fullband_channels; ch++)
  919. put_bits(&c->pb, 2, 0);
  920. /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
  921. for (ch = 0; ch < c->fullband_channels; ch++)
  922. put_bits(&c->pb, 3, 6);
  923. /* Bit allocation quantizer select: linear 5-bit */
  924. for (ch = 0; ch < c->fullband_channels; ch++)
  925. put_bits(&c->pb, 3, c->bit_allocation_sel[ch]);
  926. /* Quantization index codebook select */
  927. for (i = 0; i < DCA_CODE_BOOKS; i++)
  928. for (ch = 0; ch < c->fullband_channels; ch++)
  929. put_bits(&c->pb, ff_dca_quant_index_sel_nbits[i], c->quant_index_sel[ch][i]);
  930. /* Scale factor adjustment index: transmitted in case of Huffman coding */
  931. for (i = 0; i < DCA_CODE_BOOKS; i++)
  932. for (ch = 0; ch < c->fullband_channels; ch++)
  933. if (c->quant_index_sel[ch][i] < ff_dca_quant_index_group_size[i])
  934. put_bits(&c->pb, 2, 0);
  935. /* Audio header CRC check word: not transmitted */
  936. }
  937. static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
  938. {
  939. int i, j, sum, bits, sel;
  940. if (c->abits[ch][band] <= DCA_CODE_BOOKS) {
  941. av_assert0(c->abits[ch][band] > 0);
  942. sel = c->quant_index_sel[ch][c->abits[ch][band] - 1];
  943. // Huffman codes
  944. if (sel < ff_dca_quant_index_group_size[c->abits[ch][band] - 1]) {
  945. ff_dca_vlc_enc_quant(&c->pb, &c->quantized[ch][band][ss * 8], 8, sel, c->abits[ch][band] - 1);
  946. return;
  947. }
  948. // Block codes
  949. if (c->abits[ch][band] <= 7) {
  950. for (i = 0; i < 8; i += 4) {
  951. sum = 0;
  952. for (j = 3; j >= 0; j--) {
  953. sum *= ff_dca_quant_levels[c->abits[ch][band]];
  954. sum += c->quantized[ch][band][ss * 8 + i + j];
  955. sum += (ff_dca_quant_levels[c->abits[ch][band]] - 1) / 2;
  956. }
  957. put_bits(&c->pb, bit_consumption[c->abits[ch][band]] / 4, sum);
  958. }
  959. return;
  960. }
  961. }
  962. for (i = 0; i < 8; i++) {
  963. bits = bit_consumption[c->abits[ch][band]] / 16;
  964. put_sbits(&c->pb, bits, c->quantized[ch][band][ss * 8 + i]);
  965. }
  966. }
  967. static void put_subframe(DCAEncContext *c, int subframe)
  968. {
  969. int i, band, ss, ch;
  970. /* Subsubframes count */
  971. put_bits(&c->pb, 2, SUBSUBFRAMES -1);
  972. /* Partial subsubframe sample count: dummy */
  973. put_bits(&c->pb, 3, 0);
  974. /* Prediction mode: no ADPCM, in each channel and subband */
  975. for (ch = 0; ch < c->fullband_channels; ch++)
  976. for (band = 0; band < DCAENC_SUBBANDS; band++)
  977. put_bits(&c->pb, 1, !(c->prediction_mode[ch][band] == -1));
  978. /* Prediction VQ address */
  979. for (ch = 0; ch < c->fullband_channels; ch++)
  980. for (band = 0; band < DCAENC_SUBBANDS; band++)
  981. if (c->prediction_mode[ch][band] >= 0)
  982. put_bits(&c->pb, 12, c->prediction_mode[ch][band]);
  983. /* Bit allocation index */
  984. for (ch = 0; ch < c->fullband_channels; ch++) {
  985. if (c->bit_allocation_sel[ch] == 6) {
  986. for (band = 0; band < DCAENC_SUBBANDS; band++) {
  987. put_bits(&c->pb, 5, c->abits[ch][band]);
  988. }
  989. } else {
  990. ff_dca_vlc_enc_alloc(&c->pb, c->abits[ch], DCAENC_SUBBANDS, c->bit_allocation_sel[ch]);
  991. }
  992. }
  993. if (SUBSUBFRAMES > 1) {
  994. /* Transition mode: none for each channel and subband */
  995. for (ch = 0; ch < c->fullband_channels; ch++)
  996. for (band = 0; band < DCAENC_SUBBANDS; band++)
  997. if (c->abits[ch][band])
  998. put_bits(&c->pb, 1, 0); /* codebook A4 */
  999. }
  1000. /* Scale factors */
  1001. for (ch = 0; ch < c->fullband_channels; ch++)
  1002. for (band = 0; band < DCAENC_SUBBANDS; band++)
  1003. if (c->abits[ch][band])
  1004. put_bits(&c->pb, 7, c->scale_factor[ch][band]);
  1005. /* Joint subband scale factor codebook select: not transmitted */
  1006. /* Scale factors for joint subband coding: not transmitted */
  1007. /* Stereo down-mix coefficients: not transmitted */
  1008. /* Dynamic range coefficient: not transmitted */
  1009. /* Stde information CRC check word: not transmitted */
  1010. /* VQ encoded high frequency subbands: not transmitted */
  1011. /* LFE data: 8 samples and scalefactor */
  1012. if (c->lfe_channel) {
  1013. for (i = 0; i < DCA_LFE_SAMPLES; i++)
  1014. put_bits(&c->pb, 8, quantize_value(c->downsampled_lfe[i], c->lfe_quant) & 0xff);
  1015. put_bits(&c->pb, 8, c->lfe_scale_factor);
  1016. }
  1017. /* Audio data (subsubframes) */
  1018. for (ss = 0; ss < SUBSUBFRAMES ; ss++)
  1019. for (ch = 0; ch < c->fullband_channels; ch++)
  1020. for (band = 0; band < DCAENC_SUBBANDS; band++)
  1021. if (c->abits[ch][band])
  1022. put_subframe_samples(c, ss, band, ch);
  1023. /* DSYNC */
  1024. put_bits(&c->pb, 16, 0xffff);
  1025. }
  1026. static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  1027. const AVFrame *frame, int *got_packet_ptr)
  1028. {
  1029. DCAEncContext *c = avctx->priv_data;
  1030. const int32_t *samples;
  1031. int ret, i;
  1032. if ((ret = ff_alloc_packet2(avctx, avpkt, c->frame_size, 0)) < 0)
  1033. return ret;
  1034. samples = (const int32_t *)frame->data[0];
  1035. subband_transform(c, samples);
  1036. if (c->lfe_channel)
  1037. lfe_downsample(c, samples);
  1038. calc_masking(c, samples);
  1039. if (c->options.adpcm_mode)
  1040. adpcm_analysis(c);
  1041. find_peaks(c);
  1042. assign_bits(c);
  1043. calc_lfe_scales(c);
  1044. shift_history(c, samples);
  1045. init_put_bits(&c->pb, avpkt->data, avpkt->size);
  1046. fill_in_adpcm_bufer(c);
  1047. put_frame_header(c);
  1048. put_primary_audio_header(c);
  1049. for (i = 0; i < SUBFRAMES; i++)
  1050. put_subframe(c, i);
  1051. for (i = put_bits_count(&c->pb); i < 8*c->frame_size; i++)
  1052. put_bits(&c->pb, 1, 0);
  1053. flush_put_bits(&c->pb);
  1054. avpkt->pts = frame->pts;
  1055. avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
  1056. avpkt->size = put_bits_count(&c->pb) >> 3;
  1057. *got_packet_ptr = 1;
  1058. return 0;
  1059. }
  1060. #define DCAENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  1061. static const AVOption options[] = {
  1062. { "dca_adpcm", "Use ADPCM encoding", offsetof(DCAEncContext, options.adpcm_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DCAENC_FLAGS },
  1063. { NULL },
  1064. };
  1065. static const AVClass dcaenc_class = {
  1066. .class_name = "DCA (DTS Coherent Acoustics)",
  1067. .item_name = av_default_item_name,
  1068. .option = options,
  1069. .version = LIBAVUTIL_VERSION_INT,
  1070. };
  1071. static const AVCodecDefault defaults[] = {
  1072. { "b", "1411200" },
  1073. { NULL },
  1074. };
  1075. AVCodec ff_dca_encoder = {
  1076. .name = "dca",
  1077. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  1078. .type = AVMEDIA_TYPE_AUDIO,
  1079. .id = AV_CODEC_ID_DTS,
  1080. .priv_data_size = sizeof(DCAEncContext),
  1081. .init = encode_init,
  1082. .close = encode_close,
  1083. .encode2 = encode_frame,
  1084. .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
  1085. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
  1086. AV_SAMPLE_FMT_NONE },
  1087. .supported_samplerates = sample_rates,
  1088. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
  1089. AV_CH_LAYOUT_STEREO,
  1090. AV_CH_LAYOUT_2_2,
  1091. AV_CH_LAYOUT_5POINT0,
  1092. AV_CH_LAYOUT_5POINT1,
  1093. 0 },
  1094. .defaults = defaults,
  1095. .priv_class = &dcaenc_class,
  1096. };