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.

1293 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.", (int64_t)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_NABITS 2
  565. #define USED_26ABITS 4
  566. static inline int32_t get_step_size(const DCAEncContext *c, int ch, int band)
  567. {
  568. int32_t step_size;
  569. if (c->bitrate_index == 3)
  570. step_size = ff_dca_lossless_quant[c->abits[ch][band]];
  571. else
  572. step_size = ff_dca_lossy_quant[c->abits[ch][band]];
  573. return step_size;
  574. }
  575. static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
  576. {
  577. int32_t peak;
  578. int our_nscale, try_remove;
  579. softfloat our_quant;
  580. av_assert0(peak_cb <= 0);
  581. av_assert0(peak_cb >= -2047);
  582. our_nscale = 127;
  583. peak = cb_to_level[-peak_cb];
  584. for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
  585. if (scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e <= 17)
  586. continue;
  587. our_quant.m = mul32(scalefactor_inv[our_nscale - try_remove].m, stepsize_inv[abits].m);
  588. our_quant.e = scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e - 17;
  589. if ((ff_dca_quant_levels[abits] - 1) / 2 < quantize_value(peak, our_quant))
  590. continue;
  591. our_nscale -= try_remove;
  592. }
  593. if (our_nscale >= 125)
  594. our_nscale = 124;
  595. quant->m = mul32(scalefactor_inv[our_nscale].m, stepsize_inv[abits].m);
  596. quant->e = scalefactor_inv[our_nscale].e + stepsize_inv[abits].e - 17;
  597. av_assert0((ff_dca_quant_levels[abits] - 1) / 2 >= quantize_value(peak, *quant));
  598. return our_nscale;
  599. }
  600. static inline void quantize_adpcm_subband(DCAEncContext *c, int ch, int band)
  601. {
  602. int32_t step_size;
  603. int32_t diff_peak_cb = c->diff_peak_cb[ch][band];
  604. c->scale_factor[ch][band] = calc_one_scale(diff_peak_cb,
  605. c->abits[ch][band],
  606. &c->quant[ch][band]);
  607. step_size = get_step_size(c, ch, band);
  608. ff_dcaadpcm_do_real(c->prediction_mode[ch][band],
  609. c->quant[ch][band], ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], step_size,
  610. c->adpcm_history[ch][band], c->subband[ch][band], c->adpcm_history[ch][band]+4, c->quantized[ch][band],
  611. SUBBAND_SAMPLES, cb_to_level[-diff_peak_cb]);
  612. }
  613. static void quantize_adpcm(DCAEncContext *c)
  614. {
  615. int band, ch;
  616. for (ch = 0; ch < c->fullband_channels; ch++)
  617. for (band = 0; band < 32; band++)
  618. if (c->prediction_mode[ch][band] >= 0)
  619. quantize_adpcm_subband(c, ch, band);
  620. }
  621. static void quantize_pcm(DCAEncContext *c)
  622. {
  623. int sample, band, ch;
  624. for (ch = 0; ch < c->fullband_channels; ch++)
  625. for (band = 0; band < 32; band++)
  626. if (c->prediction_mode[ch][band] == -1)
  627. for (sample = 0; sample < SUBBAND_SAMPLES; sample++)
  628. c->quantized[ch][band][sample] = quantize_value(c->subband[ch][band][sample], c->quant[ch][band]);
  629. }
  630. static void accumulate_huff_bit_consumption(int abits, int32_t *quantized, uint32_t *result)
  631. {
  632. uint8_t sel, id = abits - 1;
  633. for (sel = 0; sel < ff_dca_quant_index_group_size[id]; sel++)
  634. result[sel] += ff_dca_vlc_calc_quant_bits(quantized, SUBBAND_SAMPLES, sel, id);
  635. }
  636. 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])
  637. {
  638. uint8_t i, sel;
  639. uint32_t best_sel_bits[DCA_CODE_BOOKS];
  640. int32_t best_sel_id[DCA_CODE_BOOKS];
  641. uint32_t t, bits = 0;
  642. for (i = 0; i < DCA_CODE_BOOKS; i++) {
  643. av_assert0(!((!!vlc_bits[i][0]) ^ (!!clc_bits[i])));
  644. if (vlc_bits[i][0] == 0) {
  645. /* do not transmit adjustment index for empty codebooks */
  646. res[i] = ff_dca_quant_index_group_size[i];
  647. /* and skip it */
  648. continue;
  649. }
  650. best_sel_bits[i] = vlc_bits[i][0];
  651. best_sel_id[i] = 0;
  652. for (sel = 0; sel < ff_dca_quant_index_group_size[i]; sel++) {
  653. if (best_sel_bits[i] > vlc_bits[i][sel] && vlc_bits[i][sel]) {
  654. best_sel_bits[i] = vlc_bits[i][sel];
  655. best_sel_id[i] = sel;
  656. }
  657. }
  658. /* 2 bits to transmit scale factor adjustment index */
  659. t = best_sel_bits[i] + 2;
  660. if (t < clc_bits[i]) {
  661. res[i] = best_sel_id[i];
  662. bits += t;
  663. } else {
  664. res[i] = ff_dca_quant_index_group_size[i];
  665. bits += clc_bits[i];
  666. }
  667. }
  668. return bits;
  669. }
  670. static uint32_t set_best_abits_code(int abits[DCAENC_SUBBANDS], int bands, int32_t *res)
  671. {
  672. uint8_t i;
  673. uint32_t t;
  674. int32_t best_sel = 6;
  675. int32_t best_bits = bands * 5;
  676. /* Check do we have subband which cannot be encoded by Huffman tables */
  677. for (i = 0; i < bands; i++) {
  678. if (abits[i] > 12) {
  679. *res = best_sel;
  680. return best_bits;
  681. }
  682. }
  683. for (i = 0; i < DCA_BITALLOC_12_COUNT; i++) {
  684. t = ff_dca_vlc_calc_alloc_bits(abits, bands, i);
  685. if (t < best_bits) {
  686. best_bits = t;
  687. best_sel = i;
  688. }
  689. }
  690. *res = best_sel;
  691. return best_bits;
  692. }
  693. static int init_quantization_noise(DCAEncContext *c, int noise)
  694. {
  695. int ch, band, ret = 0;
  696. uint32_t huff_bit_count_accum[MAX_CHANNELS][DCA_CODE_BOOKS][7];
  697. uint32_t clc_bit_count_accum[MAX_CHANNELS][DCA_CODE_BOOKS];
  698. uint32_t bits_counter = 0;
  699. c->consumed_bits = 132 + 333 * c->fullband_channels;
  700. c->consumed_bits += c->consumed_adpcm_bits;
  701. if (c->lfe_channel)
  702. c->consumed_bits += 72;
  703. /* attempt to guess the bit distribution based on the prevoius frame */
  704. for (ch = 0; ch < c->fullband_channels; ch++) {
  705. for (band = 0; band < 32; band++) {
  706. int snr_cb = c->peak_cb[ch][band] - c->band_masking_cb[band] - noise;
  707. if (snr_cb >= 1312) {
  708. c->abits[ch][band] = 26;
  709. ret |= USED_26ABITS;
  710. } else if (snr_cb >= 222) {
  711. c->abits[ch][band] = 8 + mul32(snr_cb - 222, 69000000);
  712. ret |= USED_NABITS;
  713. } else if (snr_cb >= 0) {
  714. c->abits[ch][band] = 2 + mul32(snr_cb, 106000000);
  715. ret |= USED_NABITS;
  716. } else {
  717. c->abits[ch][band] = 1;
  718. ret |= USED_1ABITS;
  719. }
  720. }
  721. c->consumed_bits += set_best_abits_code(c->abits[ch], 32, &c->bit_allocation_sel[ch]);
  722. }
  723. /* Recalc scale_factor each time to get bits consumption in case of Huffman coding.
  724. It is suboptimal solution */
  725. /* TODO: May be cache scaled values */
  726. for (ch = 0; ch < c->fullband_channels; ch++) {
  727. for (band = 0; band < 32; band++) {
  728. if (c->prediction_mode[ch][band] == -1) {
  729. c->scale_factor[ch][band] = calc_one_scale(c->peak_cb[ch][band],
  730. c->abits[ch][band],
  731. &c->quant[ch][band]);
  732. }
  733. }
  734. }
  735. quantize_adpcm(c);
  736. quantize_pcm(c);
  737. memset(huff_bit_count_accum, 0, MAX_CHANNELS * DCA_CODE_BOOKS * 7 * sizeof(uint32_t));
  738. memset(clc_bit_count_accum, 0, MAX_CHANNELS * DCA_CODE_BOOKS * sizeof(uint32_t));
  739. for (ch = 0; ch < c->fullband_channels; ch++) {
  740. for (band = 0; band < 32; band++) {
  741. if (c->abits[ch][band] && c->abits[ch][band] <= DCA_CODE_BOOKS) {
  742. accumulate_huff_bit_consumption(c->abits[ch][band], c->quantized[ch][band], huff_bit_count_accum[ch][c->abits[ch][band] - 1]);
  743. clc_bit_count_accum[ch][c->abits[ch][band] - 1] += bit_consumption[c->abits[ch][band]];
  744. } else {
  745. bits_counter += bit_consumption[c->abits[ch][band]];
  746. }
  747. }
  748. }
  749. for (ch = 0; ch < c->fullband_channels; ch++) {
  750. bits_counter += set_best_code(huff_bit_count_accum[ch], clc_bit_count_accum[ch], c->quant_index_sel[ch]);
  751. }
  752. c->consumed_bits += bits_counter;
  753. return ret;
  754. }
  755. static void assign_bits(DCAEncContext *c)
  756. {
  757. /* Find the bounds where the binary search should work */
  758. int low, high, down;
  759. int used_abits = 0;
  760. init_quantization_noise(c, c->worst_quantization_noise);
  761. low = high = c->worst_quantization_noise;
  762. if (c->consumed_bits > c->frame_bits) {
  763. while (c->consumed_bits > c->frame_bits) {
  764. av_assert0(used_abits != USED_1ABITS);
  765. low = high;
  766. high += snr_fudge;
  767. used_abits = init_quantization_noise(c, high);
  768. }
  769. } else {
  770. while (c->consumed_bits <= c->frame_bits) {
  771. high = low;
  772. if (used_abits == USED_26ABITS)
  773. goto out; /* The requested bitrate is too high, pad with zeros */
  774. low -= snr_fudge;
  775. used_abits = init_quantization_noise(c, low);
  776. }
  777. }
  778. /* Now do a binary search between low and high to see what fits */
  779. for (down = snr_fudge >> 1; down; down >>= 1) {
  780. init_quantization_noise(c, high - down);
  781. if (c->consumed_bits <= c->frame_bits)
  782. high -= down;
  783. }
  784. init_quantization_noise(c, high);
  785. out:
  786. c->worst_quantization_noise = high;
  787. if (high > c->worst_noise_ever)
  788. c->worst_noise_ever = high;
  789. }
  790. static void shift_history(DCAEncContext *c, const int32_t *input)
  791. {
  792. int k, ch;
  793. for (k = 0; k < 512; k++)
  794. for (ch = 0; ch < c->channels; ch++) {
  795. const int chi = c->channel_order_tab[ch];
  796. c->history[ch][k] = input[k * c->channels + chi];
  797. }
  798. }
  799. static void fill_in_adpcm_bufer(DCAEncContext *c)
  800. {
  801. int ch, band;
  802. int32_t step_size;
  803. /* We fill in ADPCM work buffer for subbands which hasn't been ADPCM coded
  804. * in current frame - we need this data if subband of next frame is
  805. * ADPCM
  806. */
  807. for (ch = 0; ch < c->channels; ch++) {
  808. for (band = 0; band < 32; band++) {
  809. int32_t *samples = c->subband[ch][band] - DCA_ADPCM_COEFFS;
  810. if (c->prediction_mode[ch][band] == -1) {
  811. step_size = get_step_size(c, ch, band);
  812. ff_dca_core_dequantize(c->adpcm_history[ch][band],
  813. c->quantized[ch][band]+12, step_size, ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], 0, 4);
  814. } else {
  815. AV_COPY128U(c->adpcm_history[ch][band], c->adpcm_history[ch][band]+4);
  816. }
  817. /* Copy dequantized values for LPC analysis.
  818. * It reduces artifacts in case of extreme quantization,
  819. * example: in current frame abits is 1 and has no prediction flag,
  820. * but end of this frame is sine like signal. In this case, if LPC analysis uses
  821. * original values, likely LPC analysis returns good prediction gain, and sets prediction flag.
  822. * But there are no proper value in decoder history, so likely result will be no good.
  823. * Bitstream has "Predictor history flag switch", but this flag disables history for all subbands
  824. */
  825. samples[0] = c->adpcm_history[ch][band][0] << 7;
  826. samples[1] = c->adpcm_history[ch][band][1] << 7;
  827. samples[2] = c->adpcm_history[ch][band][2] << 7;
  828. samples[3] = c->adpcm_history[ch][band][3] << 7;
  829. }
  830. }
  831. }
  832. static void calc_lfe_scales(DCAEncContext *c)
  833. {
  834. if (c->lfe_channel)
  835. c->lfe_scale_factor = calc_one_scale(c->lfe_peak_cb, 11, &c->lfe_quant);
  836. }
  837. static void put_frame_header(DCAEncContext *c)
  838. {
  839. /* SYNC */
  840. put_bits(&c->pb, 16, 0x7ffe);
  841. put_bits(&c->pb, 16, 0x8001);
  842. /* Frame type: normal */
  843. put_bits(&c->pb, 1, 1);
  844. /* Deficit sample count: none */
  845. put_bits(&c->pb, 5, 31);
  846. /* CRC is not present */
  847. put_bits(&c->pb, 1, 0);
  848. /* Number of PCM sample blocks */
  849. put_bits(&c->pb, 7, SUBBAND_SAMPLES - 1);
  850. /* Primary frame byte size */
  851. put_bits(&c->pb, 14, c->frame_size - 1);
  852. /* Audio channel arrangement */
  853. put_bits(&c->pb, 6, c->channel_config);
  854. /* Core audio sampling frequency */
  855. put_bits(&c->pb, 4, bitstream_sfreq[c->samplerate_index]);
  856. /* Transmission bit rate */
  857. put_bits(&c->pb, 5, c->bitrate_index);
  858. /* Embedded down mix: disabled */
  859. put_bits(&c->pb, 1, 0);
  860. /* Embedded dynamic range flag: not present */
  861. put_bits(&c->pb, 1, 0);
  862. /* Embedded time stamp flag: not present */
  863. put_bits(&c->pb, 1, 0);
  864. /* Auxiliary data flag: not present */
  865. put_bits(&c->pb, 1, 0);
  866. /* HDCD source: no */
  867. put_bits(&c->pb, 1, 0);
  868. /* Extension audio ID: N/A */
  869. put_bits(&c->pb, 3, 0);
  870. /* Extended audio data: not present */
  871. put_bits(&c->pb, 1, 0);
  872. /* Audio sync word insertion flag: after each sub-frame */
  873. put_bits(&c->pb, 1, 0);
  874. /* Low frequency effects flag: not present or 64x subsampling */
  875. put_bits(&c->pb, 2, c->lfe_channel ? 2 : 0);
  876. /* Predictor history switch flag: on */
  877. put_bits(&c->pb, 1, 1);
  878. /* No CRC */
  879. /* Multirate interpolator switch: non-perfect reconstruction */
  880. put_bits(&c->pb, 1, 0);
  881. /* Encoder software revision: 7 */
  882. put_bits(&c->pb, 4, 7);
  883. /* Copy history: 0 */
  884. put_bits(&c->pb, 2, 0);
  885. /* Source PCM resolution: 16 bits, not DTS ES */
  886. put_bits(&c->pb, 3, 0);
  887. /* Front sum/difference coding: no */
  888. put_bits(&c->pb, 1, 0);
  889. /* Surrounds sum/difference coding: no */
  890. put_bits(&c->pb, 1, 0);
  891. /* Dialog normalization: 0 dB */
  892. put_bits(&c->pb, 4, 0);
  893. }
  894. static void put_primary_audio_header(DCAEncContext *c)
  895. {
  896. int ch, i;
  897. /* Number of subframes */
  898. put_bits(&c->pb, 4, SUBFRAMES - 1);
  899. /* Number of primary audio channels */
  900. put_bits(&c->pb, 3, c->fullband_channels - 1);
  901. /* Subband activity count */
  902. for (ch = 0; ch < c->fullband_channels; ch++)
  903. put_bits(&c->pb, 5, DCAENC_SUBBANDS - 2);
  904. /* High frequency VQ start subband */
  905. for (ch = 0; ch < c->fullband_channels; ch++)
  906. put_bits(&c->pb, 5, DCAENC_SUBBANDS - 1);
  907. /* Joint intensity coding index: 0, 0 */
  908. for (ch = 0; ch < c->fullband_channels; ch++)
  909. put_bits(&c->pb, 3, 0);
  910. /* Transient mode codebook: A4, A4 (arbitrary) */
  911. for (ch = 0; ch < c->fullband_channels; ch++)
  912. put_bits(&c->pb, 2, 0);
  913. /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
  914. for (ch = 0; ch < c->fullband_channels; ch++)
  915. put_bits(&c->pb, 3, 6);
  916. /* Bit allocation quantizer select: linear 5-bit */
  917. for (ch = 0; ch < c->fullband_channels; ch++)
  918. put_bits(&c->pb, 3, c->bit_allocation_sel[ch]);
  919. /* Quantization index codebook select */
  920. for (i = 0; i < DCA_CODE_BOOKS; i++)
  921. for (ch = 0; ch < c->fullband_channels; ch++)
  922. put_bits(&c->pb, ff_dca_quant_index_sel_nbits[i], c->quant_index_sel[ch][i]);
  923. /* Scale factor adjustment index: transmitted in case of Huffman coding */
  924. for (i = 0; i < DCA_CODE_BOOKS; i++)
  925. for (ch = 0; ch < c->fullband_channels; ch++)
  926. if (c->quant_index_sel[ch][i] < ff_dca_quant_index_group_size[i])
  927. put_bits(&c->pb, 2, 0);
  928. /* Audio header CRC check word: not transmitted */
  929. }
  930. static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
  931. {
  932. int i, j, sum, bits, sel;
  933. if (c->abits[ch][band] <= DCA_CODE_BOOKS) {
  934. av_assert0(c->abits[ch][band] > 0);
  935. sel = c->quant_index_sel[ch][c->abits[ch][band] - 1];
  936. // Huffman codes
  937. if (sel < ff_dca_quant_index_group_size[c->abits[ch][band] - 1]) {
  938. ff_dca_vlc_enc_quant(&c->pb, &c->quantized[ch][band][ss * 8], 8, sel, c->abits[ch][band] - 1);
  939. return;
  940. }
  941. // Block codes
  942. if (c->abits[ch][band] <= 7) {
  943. for (i = 0; i < 8; i += 4) {
  944. sum = 0;
  945. for (j = 3; j >= 0; j--) {
  946. sum *= ff_dca_quant_levels[c->abits[ch][band]];
  947. sum += c->quantized[ch][band][ss * 8 + i + j];
  948. sum += (ff_dca_quant_levels[c->abits[ch][band]] - 1) / 2;
  949. }
  950. put_bits(&c->pb, bit_consumption[c->abits[ch][band]] / 4, sum);
  951. }
  952. return;
  953. }
  954. }
  955. for (i = 0; i < 8; i++) {
  956. bits = bit_consumption[c->abits[ch][band]] / 16;
  957. put_sbits(&c->pb, bits, c->quantized[ch][band][ss * 8 + i]);
  958. }
  959. }
  960. static void put_subframe(DCAEncContext *c, int subframe)
  961. {
  962. int i, band, ss, ch;
  963. /* Subsubframes count */
  964. put_bits(&c->pb, 2, SUBSUBFRAMES -1);
  965. /* Partial subsubframe sample count: dummy */
  966. put_bits(&c->pb, 3, 0);
  967. /* Prediction mode: no ADPCM, in each channel and subband */
  968. for (ch = 0; ch < c->fullband_channels; ch++)
  969. for (band = 0; band < DCAENC_SUBBANDS; band++)
  970. put_bits(&c->pb, 1, !(c->prediction_mode[ch][band] == -1));
  971. /* Prediction VQ address */
  972. for (ch = 0; ch < c->fullband_channels; ch++)
  973. for (band = 0; band < DCAENC_SUBBANDS; band++)
  974. if (c->prediction_mode[ch][band] >= 0)
  975. put_bits(&c->pb, 12, c->prediction_mode[ch][band]);
  976. /* Bit allocation index */
  977. for (ch = 0; ch < c->fullband_channels; ch++) {
  978. if (c->bit_allocation_sel[ch] == 6) {
  979. for (band = 0; band < DCAENC_SUBBANDS; band++) {
  980. put_bits(&c->pb, 5, c->abits[ch][band]);
  981. }
  982. } else {
  983. ff_dca_vlc_enc_alloc(&c->pb, c->abits[ch], DCAENC_SUBBANDS, c->bit_allocation_sel[ch]);
  984. }
  985. }
  986. if (SUBSUBFRAMES > 1) {
  987. /* Transition mode: none for each channel and subband */
  988. for (ch = 0; ch < c->fullband_channels; ch++)
  989. for (band = 0; band < DCAENC_SUBBANDS; band++)
  990. put_bits(&c->pb, 1, 0); /* codebook A4 */
  991. }
  992. /* Scale factors */
  993. for (ch = 0; ch < c->fullband_channels; ch++)
  994. for (band = 0; band < DCAENC_SUBBANDS; band++)
  995. put_bits(&c->pb, 7, c->scale_factor[ch][band]);
  996. /* Joint subband scale factor codebook select: not transmitted */
  997. /* Scale factors for joint subband coding: not transmitted */
  998. /* Stereo down-mix coefficients: not transmitted */
  999. /* Dynamic range coefficient: not transmitted */
  1000. /* Stde information CRC check word: not transmitted */
  1001. /* VQ encoded high frequency subbands: not transmitted */
  1002. /* LFE data: 8 samples and scalefactor */
  1003. if (c->lfe_channel) {
  1004. for (i = 0; i < DCA_LFE_SAMPLES; i++)
  1005. put_bits(&c->pb, 8, quantize_value(c->downsampled_lfe[i], c->lfe_quant) & 0xff);
  1006. put_bits(&c->pb, 8, c->lfe_scale_factor);
  1007. }
  1008. /* Audio data (subsubframes) */
  1009. for (ss = 0; ss < SUBSUBFRAMES ; ss++)
  1010. for (ch = 0; ch < c->fullband_channels; ch++)
  1011. for (band = 0; band < DCAENC_SUBBANDS; band++)
  1012. put_subframe_samples(c, ss, band, ch);
  1013. /* DSYNC */
  1014. put_bits(&c->pb, 16, 0xffff);
  1015. }
  1016. static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  1017. const AVFrame *frame, int *got_packet_ptr)
  1018. {
  1019. DCAEncContext *c = avctx->priv_data;
  1020. const int32_t *samples;
  1021. int ret, i;
  1022. if ((ret = ff_alloc_packet2(avctx, avpkt, c->frame_size, 0)) < 0)
  1023. return ret;
  1024. samples = (const int32_t *)frame->data[0];
  1025. subband_transform(c, samples);
  1026. if (c->lfe_channel)
  1027. lfe_downsample(c, samples);
  1028. calc_masking(c, samples);
  1029. if (c->options.adpcm_mode)
  1030. adpcm_analysis(c);
  1031. find_peaks(c);
  1032. assign_bits(c);
  1033. calc_lfe_scales(c);
  1034. shift_history(c, samples);
  1035. init_put_bits(&c->pb, avpkt->data, avpkt->size);
  1036. fill_in_adpcm_bufer(c);
  1037. put_frame_header(c);
  1038. put_primary_audio_header(c);
  1039. for (i = 0; i < SUBFRAMES; i++)
  1040. put_subframe(c, i);
  1041. for (i = put_bits_count(&c->pb); i < 8*c->frame_size; i++)
  1042. put_bits(&c->pb, 1, 0);
  1043. flush_put_bits(&c->pb);
  1044. avpkt->pts = frame->pts;
  1045. avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
  1046. avpkt->size = put_bits_count(&c->pb) >> 3;
  1047. *got_packet_ptr = 1;
  1048. return 0;
  1049. }
  1050. #define DCAENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  1051. static const AVOption options[] = {
  1052. { "dca_adpcm", "Use ADPCM encoding", offsetof(DCAEncContext, options.adpcm_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DCAENC_FLAGS },
  1053. { NULL },
  1054. };
  1055. static const AVClass dcaenc_class = {
  1056. .class_name = "DCA (DTS Coherent Acoustics)",
  1057. .item_name = av_default_item_name,
  1058. .option = options,
  1059. .version = LIBAVUTIL_VERSION_INT,
  1060. };
  1061. static const AVCodecDefault defaults[] = {
  1062. { "b", "1411200" },
  1063. { NULL },
  1064. };
  1065. AVCodec ff_dca_encoder = {
  1066. .name = "dca",
  1067. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  1068. .type = AVMEDIA_TYPE_AUDIO,
  1069. .id = AV_CODEC_ID_DTS,
  1070. .priv_data_size = sizeof(DCAEncContext),
  1071. .init = encode_init,
  1072. .close = encode_close,
  1073. .encode2 = encode_frame,
  1074. .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
  1075. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
  1076. AV_SAMPLE_FMT_NONE },
  1077. .supported_samplerates = sample_rates,
  1078. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
  1079. AV_CH_LAYOUT_STEREO,
  1080. AV_CH_LAYOUT_2_2,
  1081. AV_CH_LAYOUT_5POINT0,
  1082. AV_CH_LAYOUT_5POINT1,
  1083. 0 },
  1084. .defaults = defaults,
  1085. .priv_class = &dcaenc_class,
  1086. };