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.

998 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[MAX_CHANNELS][512]; /* This is a circular buffer */
  60. int32_t subband[MAX_CHANNELS][DCAENC_SUBBANDS][SUBBAND_SAMPLES];
  61. int32_t quantized[MAX_CHANNELS][DCAENC_SUBBANDS][SUBBAND_SAMPLES];
  62. int32_t peak_cb[MAX_CHANNELS][DCAENC_SUBBANDS];
  63. int32_t downsampled_lfe[DCA_LFE_SAMPLES];
  64. int32_t masking_curve_cb[SUBSUBFRAMES][256];
  65. int abits[MAX_CHANNELS][DCAENC_SUBBANDS];
  66. int scale_factor[MAX_CHANNELS][DCAENC_SUBBANDS];
  67. softfloat quant[MAX_CHANNELS][DCAENC_SUBBANDS];
  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 = channel_reorder_lfe[c->channel_config];
  127. } else {
  128. c->channel_order_tab = 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. memcpy(hist, &c->history[ch][0], 512 * sizeof(int32_t));
  231. for (subs = 0; subs < SUBBAND_SAMPLES; subs++) {
  232. int32_t accum[64];
  233. int32_t resp;
  234. int band;
  235. /* Calculate the convolutions at once */
  236. memset(accum, 0, 64 * sizeof(int32_t));
  237. for (k = 0, i = hist_start, j = 0;
  238. i < 512; k = (k + 1) & 63, i++, j++)
  239. accum[k] += mul32(hist[i], c->band_interpolation[j]);
  240. for (i = 0; i < hist_start; k = (k + 1) & 63, i++, j++)
  241. accum[k] += mul32(hist[i], c->band_interpolation[j]);
  242. for (k = 16; k < 32; k++)
  243. accum[k] = accum[k] - accum[31 - k];
  244. for (k = 32; k < 48; k++)
  245. accum[k] = accum[k] + accum[95 - k];
  246. for (band = 0; band < 32; band++) {
  247. resp = 0;
  248. for (i = 16; i < 48; i++) {
  249. int s = (2 * band + 1) * (2 * (i + 16) + 1);
  250. resp += mul32(accum[i], cos_t(s << 3)) >> 3;
  251. }
  252. c->subband[ch][band][subs] = ((band + 1) & 2) ? -resp : resp;
  253. }
  254. /* Copy in 32 new samples from input */
  255. for (i = 0; i < 32; i++)
  256. hist[i + hist_start] = input[(subs * 32 + i) * c->channels + chi];
  257. hist_start = (hist_start + 32) & 511;
  258. }
  259. }
  260. }
  261. static void lfe_downsample(DCAEncContext *c, const int32_t *input)
  262. {
  263. /* FIXME: make 128x LFE downsampling possible */
  264. const int lfech = lfe_index[c->channel_config];
  265. int i, j, lfes;
  266. int32_t hist[512];
  267. int32_t accum;
  268. int hist_start = 0;
  269. memcpy(hist, &c->history[c->channels - 1][0], 512 * sizeof(int32_t));
  270. for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) {
  271. /* Calculate the convolution */
  272. accum = 0;
  273. for (i = hist_start, j = 0; i < 512; i++, j++)
  274. accum += mul32(hist[i], lfe_fir_64i[j]);
  275. for (i = 0; i < hist_start; i++, j++)
  276. accum += mul32(hist[i], lfe_fir_64i[j]);
  277. c->downsampled_lfe[lfes] = accum;
  278. /* Copy in 64 new samples from input */
  279. for (i = 0; i < 64; i++)
  280. hist[i + hist_start] = input[(lfes * 64 + i) * c->channels + lfech];
  281. hist_start = (hist_start + 64) & 511;
  282. }
  283. }
  284. typedef struct {
  285. int32_t re;
  286. int32_t im;
  287. } cplx32;
  288. static void fft(const int32_t in[2 * 256], cplx32 out[256])
  289. {
  290. cplx32 buf[256], rin[256], rout[256];
  291. int i, j, k, l;
  292. /* do two transforms in parallel */
  293. for (i = 0; i < 256; i++) {
  294. /* Apply the Hann window */
  295. rin[i].re = mul32(in[2 * i], 0x3fffffff - (cos_t(8 * i + 2) >> 1));
  296. rin[i].im = mul32(in[2 * i + 1], 0x3fffffff - (cos_t(8 * i + 6) >> 1));
  297. }
  298. /* pre-rotation */
  299. for (i = 0; i < 256; i++) {
  300. buf[i].re = mul32(cos_t(4 * i + 2), rin[i].re)
  301. - mul32(sin_t(4 * i + 2), rin[i].im);
  302. buf[i].im = mul32(cos_t(4 * i + 2), rin[i].im)
  303. + mul32(sin_t(4 * i + 2), rin[i].re);
  304. }
  305. for (j = 256, l = 1; j != 1; j >>= 1, l <<= 1) {
  306. for (k = 0; k < 256; k += j) {
  307. for (i = k; i < k + j / 2; i++) {
  308. cplx32 sum, diff;
  309. int t = 8 * l * i;
  310. sum.re = buf[i].re + buf[i + j / 2].re;
  311. sum.im = buf[i].im + buf[i + j / 2].im;
  312. diff.re = buf[i].re - buf[i + j / 2].re;
  313. diff.im = buf[i].im - buf[i + j / 2].im;
  314. buf[i].re = half32(sum.re);
  315. buf[i].im = half32(sum.im);
  316. buf[i + j / 2].re = mul32(diff.re, cos_t(t))
  317. - mul32(diff.im, sin_t(t));
  318. buf[i + j / 2].im = mul32(diff.im, cos_t(t))
  319. + mul32(diff.re, sin_t(t));
  320. }
  321. }
  322. }
  323. /* post-rotation */
  324. for (i = 0; i < 256; i++) {
  325. int b = ff_reverse[i];
  326. rout[i].re = mul32(buf[b].re, cos_t(4 * i))
  327. - mul32(buf[b].im, sin_t(4 * i));
  328. rout[i].im = mul32(buf[b].im, cos_t(4 * i))
  329. + mul32(buf[b].re, sin_t(4 * i));
  330. }
  331. for (i = 0; i < 256; i++) {
  332. /* separate the results of the two transforms */
  333. cplx32 o1, o2;
  334. o1.re = rout[i].re - rout[255 - i].re;
  335. o1.im = rout[i].im + rout[255 - i].im;
  336. o2.re = rout[i].im - rout[255 - i].im;
  337. o2.im = -rout[i].re - rout[255 - i].re;
  338. /* combine them into one long transform */
  339. out[i].re = mul32( o1.re + o2.re, cos_t(2 * i + 1))
  340. + mul32( o1.im - o2.im, sin_t(2 * i + 1));
  341. out[i].im = mul32( o1.im + o2.im, cos_t(2 * i + 1))
  342. + mul32(-o1.re + o2.re, sin_t(2 * i + 1));
  343. }
  344. }
  345. static int32_t get_cb(int32_t in)
  346. {
  347. int i, res;
  348. res = 0;
  349. if (in < 0)
  350. in = -in;
  351. for (i = 1024; i > 0; i >>= 1) {
  352. if (cb_to_level[i + res] >= in)
  353. res += i;
  354. }
  355. return -res;
  356. }
  357. static int32_t add_cb(int32_t a, int32_t b)
  358. {
  359. if (a < b)
  360. FFSWAP(int32_t, a, b);
  361. if (a - b >= 256)
  362. return a;
  363. return a + cb_to_add[a - b];
  364. }
  365. static void adjust_jnd(int samplerate_index,
  366. const int32_t in[512], int32_t out_cb[256])
  367. {
  368. int32_t power[256];
  369. cplx32 out[256];
  370. int32_t out_cb_unnorm[256];
  371. int32_t denom;
  372. const int32_t ca_cb = -1114;
  373. const int32_t cs_cb = 928;
  374. int i, j;
  375. fft(in, out);
  376. for (j = 0; j < 256; j++) {
  377. power[j] = add_cb(get_cb(out[j].re), get_cb(out[j].im));
  378. out_cb_unnorm[j] = -2047; /* and can only grow */
  379. }
  380. for (i = 0; i < AUBANDS; i++) {
  381. denom = ca_cb; /* and can only grow */
  382. for (j = 0; j < 256; j++)
  383. denom = add_cb(denom, power[j] + auf[samplerate_index][i][j]);
  384. for (j = 0; j < 256; j++)
  385. out_cb_unnorm[j] = add_cb(out_cb_unnorm[j],
  386. -denom + auf[samplerate_index][i][j]);
  387. }
  388. for (j = 0; j < 256; j++)
  389. out_cb[j] = add_cb(out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
  390. }
  391. typedef void (*walk_band_t)(DCAEncContext *c, int band1, int band2, int f,
  392. int32_t spectrum1, int32_t spectrum2, int channel,
  393. int32_t * arg);
  394. static void walk_band_low(DCAEncContext *c, int band, int channel,
  395. walk_band_t walk, int32_t *arg)
  396. {
  397. int f;
  398. if (band == 0) {
  399. for (f = 0; f < 4; f++)
  400. walk(c, 0, 0, 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[7 - f], c->band_spectrum[f], channel, arg);
  405. }
  406. }
  407. static void walk_band_high(DCAEncContext *c, int band, int channel,
  408. walk_band_t walk, int32_t *arg)
  409. {
  410. int f;
  411. if (band == 31) {
  412. for (f = 0; f < 4; f++)
  413. walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
  414. } else {
  415. for (f = 0; f < 8; f++)
  416. walk(c, band, band + 1, 8 * band + 4 + f,
  417. c->band_spectrum[f], c->band_spectrum[7 - f], channel, arg);
  418. }
  419. }
  420. static void update_band_masking(DCAEncContext *c, int band1, int band2,
  421. int f, int32_t spectrum1, int32_t spectrum2,
  422. int channel, int32_t * arg)
  423. {
  424. int32_t value = c->eff_masking_curve_cb[f] - spectrum1;
  425. if (value < c->band_masking_cb[band1])
  426. c->band_masking_cb[band1] = value;
  427. }
  428. static void calc_masking(DCAEncContext *c, const int32_t *input)
  429. {
  430. int i, k, band, ch, ssf;
  431. int32_t data[512];
  432. for (i = 0; i < 256; i++)
  433. for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
  434. c->masking_curve_cb[ssf][i] = -2047;
  435. for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
  436. for (ch = 0; ch < c->fullband_channels; ch++) {
  437. const int chi = c->channel_order_tab[ch];
  438. for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
  439. data[i] = c->history[ch][k];
  440. for (k -= 512; i < 512; i++, k++)
  441. data[i] = input[k * c->channels + chi];
  442. adjust_jnd(c->samplerate_index, data, c->masking_curve_cb[ssf]);
  443. }
  444. for (i = 0; i < 256; i++) {
  445. int32_t m = 2048;
  446. for (ssf = 0; ssf < SUBSUBFRAMES; ssf++)
  447. if (c->masking_curve_cb[ssf][i] < m)
  448. m = c->masking_curve_cb[ssf][i];
  449. c->eff_masking_curve_cb[i] = m;
  450. }
  451. for (band = 0; band < 32; band++) {
  452. c->band_masking_cb[band] = 2048;
  453. walk_band_low(c, band, 0, update_band_masking, NULL);
  454. walk_band_high(c, band, 0, update_band_masking, NULL);
  455. }
  456. }
  457. static void find_peaks(DCAEncContext *c)
  458. {
  459. int band, ch;
  460. for (ch = 0; ch < c->fullband_channels; ch++)
  461. for (band = 0; band < 32; band++) {
  462. int sample;
  463. int32_t m = 0;
  464. for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
  465. int32_t s = abs(c->subband[ch][band][sample]);
  466. if (m < s)
  467. m = s;
  468. }
  469. c->peak_cb[ch][band] = get_cb(m);
  470. }
  471. if (c->lfe_channel) {
  472. int sample;
  473. int32_t m = 0;
  474. for (sample = 0; sample < DCA_LFE_SAMPLES; sample++)
  475. if (m < abs(c->downsampled_lfe[sample]))
  476. m = abs(c->downsampled_lfe[sample]);
  477. c->lfe_peak_cb = get_cb(m);
  478. }
  479. }
  480. static const int snr_fudge = 128;
  481. #define USED_1ABITS 1
  482. #define USED_NABITS 2
  483. #define USED_26ABITS 4
  484. static int init_quantization_noise(DCAEncContext *c, int noise)
  485. {
  486. int ch, band, ret = 0;
  487. c->consumed_bits = 132 + 493 * c->fullband_channels;
  488. if (c->lfe_channel)
  489. c->consumed_bits += 72;
  490. /* attempt to guess the bit distribution based on the prevoius frame */
  491. for (ch = 0; ch < c->fullband_channels; ch++) {
  492. for (band = 0; band < 32; band++) {
  493. int snr_cb = c->peak_cb[ch][band] - c->band_masking_cb[band] - noise;
  494. if (snr_cb >= 1312) {
  495. c->abits[ch][band] = 26;
  496. ret |= USED_26ABITS;
  497. } else if (snr_cb >= 222) {
  498. c->abits[ch][band] = 8 + mul32(snr_cb - 222, 69000000);
  499. ret |= USED_NABITS;
  500. } else if (snr_cb >= 0) {
  501. c->abits[ch][band] = 2 + mul32(snr_cb, 106000000);
  502. ret |= USED_NABITS;
  503. } else {
  504. c->abits[ch][band] = 1;
  505. ret |= USED_1ABITS;
  506. }
  507. }
  508. }
  509. for (ch = 0; ch < c->fullband_channels; ch++)
  510. for (band = 0; band < 32; band++) {
  511. c->consumed_bits += bit_consumption[c->abits[ch][band]];
  512. }
  513. return ret;
  514. }
  515. static void assign_bits(DCAEncContext *c)
  516. {
  517. /* Find the bounds where the binary search should work */
  518. int low, high, down;
  519. int used_abits = 0;
  520. init_quantization_noise(c, c->worst_quantization_noise);
  521. low = high = c->worst_quantization_noise;
  522. if (c->consumed_bits > c->frame_bits) {
  523. while (c->consumed_bits > c->frame_bits) {
  524. av_assert0(used_abits != USED_1ABITS);
  525. low = high;
  526. high += snr_fudge;
  527. used_abits = init_quantization_noise(c, high);
  528. }
  529. } else {
  530. while (c->consumed_bits <= c->frame_bits) {
  531. high = low;
  532. if (used_abits == USED_26ABITS)
  533. goto out; /* The requested bitrate is too high, pad with zeros */
  534. low -= snr_fudge;
  535. used_abits = init_quantization_noise(c, low);
  536. }
  537. }
  538. /* Now do a binary search between low and high to see what fits */
  539. for (down = snr_fudge >> 1; down; down >>= 1) {
  540. init_quantization_noise(c, high - down);
  541. if (c->consumed_bits <= c->frame_bits)
  542. high -= down;
  543. }
  544. init_quantization_noise(c, high);
  545. out:
  546. c->worst_quantization_noise = high;
  547. if (high > c->worst_noise_ever)
  548. c->worst_noise_ever = high;
  549. }
  550. static void shift_history(DCAEncContext *c, const int32_t *input)
  551. {
  552. int k, ch;
  553. for (k = 0; k < 512; k++)
  554. for (ch = 0; ch < c->channels; ch++) {
  555. const int chi = c->channel_order_tab[ch];
  556. c->history[ch][k] = input[k * c->channels + chi];
  557. }
  558. }
  559. static int32_t quantize_value(int32_t value, softfloat quant)
  560. {
  561. int32_t offset = 1 << (quant.e - 1);
  562. value = mul32(value, quant.m) + offset;
  563. value = value >> quant.e;
  564. return value;
  565. }
  566. static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
  567. {
  568. int32_t peak;
  569. int our_nscale, try_remove;
  570. softfloat our_quant;
  571. av_assert0(peak_cb <= 0);
  572. av_assert0(peak_cb >= -2047);
  573. our_nscale = 127;
  574. peak = cb_to_level[-peak_cb];
  575. for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
  576. if (scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e <= 17)
  577. continue;
  578. our_quant.m = mul32(scalefactor_inv[our_nscale - try_remove].m, stepsize_inv[abits].m);
  579. our_quant.e = scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e - 17;
  580. if ((ff_dca_quant_levels[abits] - 1) / 2 < quantize_value(peak, our_quant))
  581. continue;
  582. our_nscale -= try_remove;
  583. }
  584. if (our_nscale >= 125)
  585. our_nscale = 124;
  586. quant->m = mul32(scalefactor_inv[our_nscale].m, stepsize_inv[abits].m);
  587. quant->e = scalefactor_inv[our_nscale].e + stepsize_inv[abits].e - 17;
  588. av_assert0((ff_dca_quant_levels[abits] - 1) / 2 >= quantize_value(peak, *quant));
  589. return our_nscale;
  590. }
  591. static void calc_scales(DCAEncContext *c)
  592. {
  593. int band, ch;
  594. for (ch = 0; ch < c->fullband_channels; ch++)
  595. for (band = 0; band < 32; band++)
  596. c->scale_factor[ch][band] = calc_one_scale(c->peak_cb[ch][band],
  597. c->abits[ch][band],
  598. &c->quant[ch][band]);
  599. if (c->lfe_channel)
  600. c->lfe_scale_factor = calc_one_scale(c->lfe_peak_cb, 11, &c->lfe_quant);
  601. }
  602. static void quantize_all(DCAEncContext *c)
  603. {
  604. int sample, band, ch;
  605. for (ch = 0; ch < c->fullband_channels; ch++)
  606. for (band = 0; band < 32; band++)
  607. for (sample = 0; sample < SUBBAND_SAMPLES; sample++)
  608. c->quantized[ch][band][sample] = quantize_value(c->subband[ch][band][sample], c->quant[ch][band]);
  609. }
  610. static void put_frame_header(DCAEncContext *c)
  611. {
  612. /* SYNC */
  613. put_bits(&c->pb, 16, 0x7ffe);
  614. put_bits(&c->pb, 16, 0x8001);
  615. /* Frame type: normal */
  616. put_bits(&c->pb, 1, 1);
  617. /* Deficit sample count: none */
  618. put_bits(&c->pb, 5, 31);
  619. /* CRC is not present */
  620. put_bits(&c->pb, 1, 0);
  621. /* Number of PCM sample blocks */
  622. put_bits(&c->pb, 7, SUBBAND_SAMPLES - 1);
  623. /* Primary frame byte size */
  624. put_bits(&c->pb, 14, c->frame_size - 1);
  625. /* Audio channel arrangement */
  626. put_bits(&c->pb, 6, c->channel_config);
  627. /* Core audio sampling frequency */
  628. put_bits(&c->pb, 4, bitstream_sfreq[c->samplerate_index]);
  629. /* Transmission bit rate */
  630. put_bits(&c->pb, 5, c->bitrate_index);
  631. /* Embedded down mix: disabled */
  632. put_bits(&c->pb, 1, 0);
  633. /* Embedded dynamic range flag: not present */
  634. put_bits(&c->pb, 1, 0);
  635. /* Embedded time stamp flag: not present */
  636. put_bits(&c->pb, 1, 0);
  637. /* Auxiliary data flag: not present */
  638. put_bits(&c->pb, 1, 0);
  639. /* HDCD source: no */
  640. put_bits(&c->pb, 1, 0);
  641. /* Extension audio ID: N/A */
  642. put_bits(&c->pb, 3, 0);
  643. /* Extended audio data: not present */
  644. put_bits(&c->pb, 1, 0);
  645. /* Audio sync word insertion flag: after each sub-frame */
  646. put_bits(&c->pb, 1, 0);
  647. /* Low frequency effects flag: not present or 64x subsampling */
  648. put_bits(&c->pb, 2, c->lfe_channel ? 2 : 0);
  649. /* Predictor history switch flag: on */
  650. put_bits(&c->pb, 1, 1);
  651. /* No CRC */
  652. /* Multirate interpolator switch: non-perfect reconstruction */
  653. put_bits(&c->pb, 1, 0);
  654. /* Encoder software revision: 7 */
  655. put_bits(&c->pb, 4, 7);
  656. /* Copy history: 0 */
  657. put_bits(&c->pb, 2, 0);
  658. /* Source PCM resolution: 16 bits, not DTS ES */
  659. put_bits(&c->pb, 3, 0);
  660. /* Front sum/difference coding: no */
  661. put_bits(&c->pb, 1, 0);
  662. /* Surrounds sum/difference coding: no */
  663. put_bits(&c->pb, 1, 0);
  664. /* Dialog normalization: 0 dB */
  665. put_bits(&c->pb, 4, 0);
  666. }
  667. static void put_primary_audio_header(DCAEncContext *c)
  668. {
  669. static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
  670. static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
  671. int ch, i;
  672. /* Number of subframes */
  673. put_bits(&c->pb, 4, SUBFRAMES - 1);
  674. /* Number of primary audio channels */
  675. put_bits(&c->pb, 3, c->fullband_channels - 1);
  676. /* Subband activity count */
  677. for (ch = 0; ch < c->fullband_channels; ch++)
  678. put_bits(&c->pb, 5, DCAENC_SUBBANDS - 2);
  679. /* High frequency VQ start subband */
  680. for (ch = 0; ch < c->fullband_channels; ch++)
  681. put_bits(&c->pb, 5, DCAENC_SUBBANDS - 1);
  682. /* Joint intensity coding index: 0, 0 */
  683. for (ch = 0; ch < c->fullband_channels; ch++)
  684. put_bits(&c->pb, 3, 0);
  685. /* Transient mode codebook: A4, A4 (arbitrary) */
  686. for (ch = 0; ch < c->fullband_channels; ch++)
  687. put_bits(&c->pb, 2, 0);
  688. /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
  689. for (ch = 0; ch < c->fullband_channels; ch++)
  690. put_bits(&c->pb, 3, 6);
  691. /* Bit allocation quantizer select: linear 5-bit */
  692. for (ch = 0; ch < c->fullband_channels; ch++)
  693. put_bits(&c->pb, 3, 6);
  694. /* Quantization index codebook select: dummy data
  695. to avoid transmission of scale factor adjustment */
  696. for (i = 1; i < 11; i++)
  697. for (ch = 0; ch < c->fullband_channels; ch++)
  698. put_bits(&c->pb, bitlen[i], thr[i]);
  699. /* Scale factor adjustment index: not transmitted */
  700. /* Audio header CRC check word: not transmitted */
  701. }
  702. static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
  703. {
  704. if (c->abits[ch][band] <= 7) {
  705. int sum, i, j;
  706. for (i = 0; i < 8; i += 4) {
  707. sum = 0;
  708. for (j = 3; j >= 0; j--) {
  709. sum *= ff_dca_quant_levels[c->abits[ch][band]];
  710. sum += c->quantized[ch][band][ss * 8 + i + j];
  711. sum += (ff_dca_quant_levels[c->abits[ch][band]] - 1) / 2;
  712. }
  713. put_bits(&c->pb, bit_consumption[c->abits[ch][band]] / 4, sum);
  714. }
  715. } else {
  716. int i;
  717. for (i = 0; i < 8; i++) {
  718. int bits = bit_consumption[c->abits[ch][band]] / 16;
  719. put_sbits(&c->pb, bits, c->quantized[ch][band][ss * 8 + i]);
  720. }
  721. }
  722. }
  723. static void put_subframe(DCAEncContext *c, int subframe)
  724. {
  725. int i, band, ss, ch;
  726. /* Subsubframes count */
  727. put_bits(&c->pb, 2, SUBSUBFRAMES -1);
  728. /* Partial subsubframe sample count: dummy */
  729. put_bits(&c->pb, 3, 0);
  730. /* Prediction mode: no ADPCM, in each channel and subband */
  731. for (ch = 0; ch < c->fullband_channels; ch++)
  732. for (band = 0; band < DCAENC_SUBBANDS; band++)
  733. put_bits(&c->pb, 1, 0);
  734. /* Prediction VQ address: not transmitted */
  735. /* Bit allocation index */
  736. for (ch = 0; ch < c->fullband_channels; ch++)
  737. for (band = 0; band < DCAENC_SUBBANDS; band++)
  738. put_bits(&c->pb, 5, c->abits[ch][band]);
  739. if (SUBSUBFRAMES > 1) {
  740. /* Transition mode: none for each channel and subband */
  741. for (ch = 0; ch < c->fullband_channels; ch++)
  742. for (band = 0; band < DCAENC_SUBBANDS; band++)
  743. put_bits(&c->pb, 1, 0); /* codebook A4 */
  744. }
  745. /* Scale factors */
  746. for (ch = 0; ch < c->fullband_channels; ch++)
  747. for (band = 0; band < DCAENC_SUBBANDS; band++)
  748. put_bits(&c->pb, 7, c->scale_factor[ch][band]);
  749. /* Joint subband scale factor codebook select: not transmitted */
  750. /* Scale factors for joint subband coding: not transmitted */
  751. /* Stereo down-mix coefficients: not transmitted */
  752. /* Dynamic range coefficient: not transmitted */
  753. /* Stde information CRC check word: not transmitted */
  754. /* VQ encoded high frequency subbands: not transmitted */
  755. /* LFE data: 8 samples and scalefactor */
  756. if (c->lfe_channel) {
  757. for (i = 0; i < DCA_LFE_SAMPLES; i++)
  758. put_bits(&c->pb, 8, quantize_value(c->downsampled_lfe[i], c->lfe_quant) & 0xff);
  759. put_bits(&c->pb, 8, c->lfe_scale_factor);
  760. }
  761. /* Audio data (subsubframes) */
  762. for (ss = 0; ss < SUBSUBFRAMES ; ss++)
  763. for (ch = 0; ch < c->fullband_channels; ch++)
  764. for (band = 0; band < DCAENC_SUBBANDS; band++)
  765. put_subframe_samples(c, ss, band, ch);
  766. /* DSYNC */
  767. put_bits(&c->pb, 16, 0xffff);
  768. }
  769. static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  770. const AVFrame *frame, int *got_packet_ptr)
  771. {
  772. DCAEncContext *c = avctx->priv_data;
  773. const int32_t *samples;
  774. int ret, i;
  775. if ((ret = ff_alloc_packet2(avctx, avpkt, c->frame_size, 0)) < 0)
  776. return ret;
  777. samples = (const int32_t *)frame->data[0];
  778. subband_transform(c, samples);
  779. if (c->lfe_channel)
  780. lfe_downsample(c, samples);
  781. calc_masking(c, samples);
  782. find_peaks(c);
  783. assign_bits(c);
  784. calc_scales(c);
  785. quantize_all(c);
  786. shift_history(c, samples);
  787. init_put_bits(&c->pb, avpkt->data, avpkt->size);
  788. put_frame_header(c);
  789. put_primary_audio_header(c);
  790. for (i = 0; i < SUBFRAMES; i++)
  791. put_subframe(c, i);
  792. for (i = put_bits_count(&c->pb); i < 8*c->frame_size; i++)
  793. put_bits(&c->pb, 1, 0);
  794. flush_put_bits(&c->pb);
  795. avpkt->pts = frame->pts;
  796. avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
  797. avpkt->size = put_bits_count(&c->pb) >> 3;
  798. *got_packet_ptr = 1;
  799. return 0;
  800. }
  801. static const AVCodecDefault defaults[] = {
  802. { "b", "1411200" },
  803. { NULL },
  804. };
  805. AVCodec ff_dca_encoder = {
  806. .name = "dca",
  807. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  808. .type = AVMEDIA_TYPE_AUDIO,
  809. .id = AV_CODEC_ID_DTS,
  810. .priv_data_size = sizeof(DCAEncContext),
  811. .init = encode_init,
  812. .encode2 = encode_frame,
  813. .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
  814. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
  815. AV_SAMPLE_FMT_NONE },
  816. .supported_samplerates = sample_rates,
  817. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
  818. AV_CH_LAYOUT_STEREO,
  819. AV_CH_LAYOUT_2_2,
  820. AV_CH_LAYOUT_5POINT0,
  821. AV_CH_LAYOUT_5POINT1,
  822. 0 },
  823. .defaults = defaults,
  824. };