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.

979 lines
30KB

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