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.

1000 lines
31KB

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