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.

400 lines
15KB

  1. /*
  2. * AC-3 encoder float/fixed template
  3. * Copyright (c) 2000 Fabrice Bellard
  4. * Copyright (c) 2006-2011 Justin Ruggles <justin.ruggles@gmail.com>
  5. * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de>
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * AC-3 encoder float/fixed template
  26. */
  27. #include <stdint.h>
  28. #include "ac3enc.h"
  29. int AC3_NAME(allocate_sample_buffers)(AC3EncodeContext *s)
  30. {
  31. int ch;
  32. FF_ALLOC_OR_GOTO(s->avctx, s->windowed_samples, AC3_WINDOW_SIZE *
  33. sizeof(*s->windowed_samples), alloc_fail);
  34. FF_ALLOC_OR_GOTO(s->avctx, s->planar_samples, s->channels * sizeof(*s->planar_samples),
  35. alloc_fail);
  36. for (ch = 0; ch < s->channels; ch++) {
  37. FF_ALLOCZ_OR_GOTO(s->avctx, s->planar_samples[ch],
  38. (AC3_FRAME_SIZE+AC3_BLOCK_SIZE) * sizeof(**s->planar_samples),
  39. alloc_fail);
  40. }
  41. return 0;
  42. alloc_fail:
  43. return AVERROR(ENOMEM);
  44. }
  45. /**
  46. * Deinterleave input samples.
  47. * Channels are reordered from Libav's default order to AC-3 order.
  48. */
  49. void AC3_NAME(deinterleave_input_samples)(AC3EncodeContext *s,
  50. const SampleType *samples)
  51. {
  52. int ch, i;
  53. /* deinterleave and remap input samples */
  54. for (ch = 0; ch < s->channels; ch++) {
  55. const SampleType *sptr;
  56. int sinc;
  57. /* copy last 256 samples of previous frame to the start of the current frame */
  58. memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_FRAME_SIZE],
  59. AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
  60. /* deinterleave */
  61. sinc = s->channels;
  62. sptr = samples + s->channel_map[ch];
  63. for (i = AC3_BLOCK_SIZE; i < AC3_FRAME_SIZE+AC3_BLOCK_SIZE; i++) {
  64. s->planar_samples[ch][i] = *sptr;
  65. sptr += sinc;
  66. }
  67. }
  68. }
  69. /**
  70. * Apply the MDCT to input samples to generate frequency coefficients.
  71. * This applies the KBD window and normalizes the input to reduce precision
  72. * loss due to fixed-point calculations.
  73. */
  74. void AC3_NAME(apply_mdct)(AC3EncodeContext *s)
  75. {
  76. int blk, ch;
  77. for (ch = 0; ch < s->channels; ch++) {
  78. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  79. AC3Block *block = &s->blocks[blk];
  80. const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
  81. s->apply_window(&s->dsp, s->windowed_samples, input_samples,
  82. s->mdct->window, AC3_WINDOW_SIZE);
  83. if (s->fixed_point)
  84. block->coeff_shift[ch+1] = s->normalize_samples(s);
  85. s->mdct->fft.mdct_calcw(&s->mdct->fft, block->mdct_coef[ch+1],
  86. s->windowed_samples);
  87. }
  88. }
  89. }
  90. /**
  91. * Calculate a single coupling coordinate.
  92. */
  93. static inline float calc_cpl_coord(float energy_ch, float energy_cpl)
  94. {
  95. float coord = 0.125;
  96. if (energy_cpl > 0)
  97. coord *= sqrtf(energy_ch / energy_cpl);
  98. return coord;
  99. }
  100. /**
  101. * Calculate coupling channel and coupling coordinates.
  102. * TODO: Currently this is only used for the floating-point encoder. I was
  103. * able to make it work for the fixed-point encoder, but quality was
  104. * generally lower in most cases than not using coupling. If a more
  105. * adaptive coupling strategy were to be implemented it might be useful
  106. * at that time to use coupling for the fixed-point encoder as well.
  107. */
  108. void AC3_NAME(apply_channel_coupling)(AC3EncodeContext *s)
  109. {
  110. #if CONFIG_AC3ENC_FLOAT
  111. LOCAL_ALIGNED_16(float, cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
  112. LOCAL_ALIGNED_16(int32_t, fixed_cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
  113. int blk, ch, bnd, i, j;
  114. CoefSumType energy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][16] = {{{0}}};
  115. int cpl_start, num_cpl_coefs;
  116. memset(cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*cpl_coords));
  117. memset(fixed_cpl_coords, 0, AC3_MAX_BLOCKS * sizeof(*fixed_cpl_coords));
  118. /* align start to 16-byte boundary. align length to multiple of 32.
  119. note: coupling start bin % 4 will always be 1 */
  120. cpl_start = s->start_freq[CPL_CH] - 1;
  121. num_cpl_coefs = FFALIGN(s->num_cpl_subbands * 12 + 1, 32);
  122. cpl_start = FFMIN(256, cpl_start + num_cpl_coefs) - num_cpl_coefs;
  123. /* calculate coupling channel from fbw channels */
  124. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  125. AC3Block *block = &s->blocks[blk];
  126. CoefType *cpl_coef = &block->mdct_coef[CPL_CH][cpl_start];
  127. if (!block->cpl_in_use)
  128. continue;
  129. memset(cpl_coef, 0, num_cpl_coefs * sizeof(*cpl_coef));
  130. for (ch = 1; ch <= s->fbw_channels; ch++) {
  131. CoefType *ch_coef = &block->mdct_coef[ch][cpl_start];
  132. if (!block->channel_in_cpl[ch])
  133. continue;
  134. for (i = 0; i < num_cpl_coefs; i++)
  135. cpl_coef[i] += ch_coef[i];
  136. }
  137. /* coefficients must be clipped to +/- 1.0 in order to be encoded */
  138. s->dsp.vector_clipf(cpl_coef, cpl_coef, -1.0f, 1.0f, num_cpl_coefs);
  139. /* scale coupling coefficients from float to 24-bit fixed-point */
  140. s->ac3dsp.float_to_fixed24(&block->fixed_coef[CPL_CH][cpl_start],
  141. cpl_coef, num_cpl_coefs);
  142. }
  143. /* calculate energy in each band in coupling channel and each fbw channel */
  144. /* TODO: possibly use SIMD to speed up energy calculation */
  145. bnd = 0;
  146. i = s->start_freq[CPL_CH];
  147. while (i < s->cpl_end_freq) {
  148. int band_size = s->cpl_band_sizes[bnd];
  149. for (ch = CPL_CH; ch <= s->fbw_channels; ch++) {
  150. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  151. AC3Block *block = &s->blocks[blk];
  152. if (!block->cpl_in_use || (ch > CPL_CH && !block->channel_in_cpl[ch]))
  153. continue;
  154. for (j = 0; j < band_size; j++) {
  155. CoefType v = block->mdct_coef[ch][i+j];
  156. MAC_COEF(energy[blk][ch][bnd], v, v);
  157. }
  158. }
  159. }
  160. i += band_size;
  161. bnd++;
  162. }
  163. /* determine which blocks to send new coupling coordinates for */
  164. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  165. AC3Block *block = &s->blocks[blk];
  166. AC3Block *block0 = blk ? &s->blocks[blk-1] : NULL;
  167. int new_coords = 0;
  168. CoefSumType coord_diff[AC3_MAX_CHANNELS] = {0,};
  169. if (block->cpl_in_use) {
  170. /* calculate coupling coordinates for all blocks and calculate the
  171. average difference between coordinates in successive blocks */
  172. for (ch = 1; ch <= s->fbw_channels; ch++) {
  173. if (!block->channel_in_cpl[ch])
  174. continue;
  175. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  176. cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy[blk][ch][bnd],
  177. energy[blk][CPL_CH][bnd]);
  178. if (blk > 0 && block0->cpl_in_use &&
  179. block0->channel_in_cpl[ch]) {
  180. coord_diff[ch] += fabs(cpl_coords[blk-1][ch][bnd] -
  181. cpl_coords[blk ][ch][bnd]);
  182. }
  183. }
  184. coord_diff[ch] /= s->num_cpl_bands;
  185. }
  186. /* send new coordinates if this is the first block, if previous
  187. * block did not use coupling but this block does, the channels
  188. * using coupling has changed from the previous block, or the
  189. * coordinate difference from the last block for any channel is
  190. * greater than a threshold value. */
  191. if (blk == 0) {
  192. new_coords = 1;
  193. } else if (!block0->cpl_in_use) {
  194. new_coords = 1;
  195. } else {
  196. for (ch = 1; ch <= s->fbw_channels; ch++) {
  197. if (block->channel_in_cpl[ch] && !block0->channel_in_cpl[ch]) {
  198. new_coords = 1;
  199. break;
  200. }
  201. }
  202. if (!new_coords) {
  203. for (ch = 1; ch <= s->fbw_channels; ch++) {
  204. if (block->channel_in_cpl[ch] && coord_diff[ch] > 0.04) {
  205. new_coords = 1;
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. block->new_cpl_coords = new_coords;
  213. }
  214. /* calculate final coupling coordinates, taking into account reusing of
  215. coordinates in successive blocks */
  216. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  217. blk = 0;
  218. while (blk < AC3_MAX_BLOCKS) {
  219. int blk1;
  220. CoefSumType energy_cpl;
  221. AC3Block *block = &s->blocks[blk];
  222. if (!block->cpl_in_use) {
  223. blk++;
  224. continue;
  225. }
  226. energy_cpl = energy[blk][CPL_CH][bnd];
  227. blk1 = blk+1;
  228. while (!s->blocks[blk1].new_cpl_coords && blk1 < AC3_MAX_BLOCKS) {
  229. if (s->blocks[blk1].cpl_in_use)
  230. energy_cpl += energy[blk1][CPL_CH][bnd];
  231. blk1++;
  232. }
  233. for (ch = 1; ch <= s->fbw_channels; ch++) {
  234. CoefType energy_ch;
  235. if (!block->channel_in_cpl[ch])
  236. continue;
  237. energy_ch = energy[blk][ch][bnd];
  238. blk1 = blk+1;
  239. while (!s->blocks[blk1].new_cpl_coords && blk1 < AC3_MAX_BLOCKS) {
  240. if (s->blocks[blk1].cpl_in_use)
  241. energy_ch += energy[blk1][ch][bnd];
  242. blk1++;
  243. }
  244. cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy_ch, energy_cpl);
  245. }
  246. blk = blk1;
  247. }
  248. }
  249. /* calculate exponents/mantissas for coupling coordinates */
  250. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  251. AC3Block *block = &s->blocks[blk];
  252. if (!block->cpl_in_use || !block->new_cpl_coords)
  253. continue;
  254. s->ac3dsp.float_to_fixed24(fixed_cpl_coords[blk][1],
  255. cpl_coords[blk][1],
  256. s->fbw_channels * 16);
  257. s->ac3dsp.extract_exponents(block->cpl_coord_exp[1],
  258. fixed_cpl_coords[blk][1],
  259. s->fbw_channels * 16);
  260. for (ch = 1; ch <= s->fbw_channels; ch++) {
  261. int bnd, min_exp, max_exp, master_exp;
  262. /* determine master exponent */
  263. min_exp = max_exp = block->cpl_coord_exp[ch][0];
  264. for (bnd = 1; bnd < s->num_cpl_bands; bnd++) {
  265. int exp = block->cpl_coord_exp[ch][bnd];
  266. min_exp = FFMIN(exp, min_exp);
  267. max_exp = FFMAX(exp, max_exp);
  268. }
  269. master_exp = ((max_exp - 15) + 2) / 3;
  270. master_exp = FFMAX(master_exp, 0);
  271. while (min_exp < master_exp * 3)
  272. master_exp--;
  273. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  274. block->cpl_coord_exp[ch][bnd] = av_clip(block->cpl_coord_exp[ch][bnd] -
  275. master_exp * 3, 0, 15);
  276. }
  277. block->cpl_master_exp[ch] = master_exp;
  278. /* quantize mantissas */
  279. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  280. int cpl_exp = block->cpl_coord_exp[ch][bnd];
  281. int cpl_mant = (fixed_cpl_coords[blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24;
  282. if (cpl_exp == 15)
  283. cpl_mant >>= 1;
  284. else
  285. cpl_mant -= 16;
  286. block->cpl_coord_mant[ch][bnd] = cpl_mant;
  287. }
  288. }
  289. }
  290. if (CONFIG_EAC3_ENCODER && s->eac3)
  291. ff_eac3_set_cpl_states(s);
  292. #endif /* CONFIG_AC3ENC_FLOAT */
  293. }
  294. /**
  295. * Determine rematrixing flags for each block and band.
  296. */
  297. void AC3_NAME(compute_rematrixing_strategy)(AC3EncodeContext *s)
  298. {
  299. int nb_coefs;
  300. int blk, bnd, i;
  301. AC3Block *block, *av_uninit(block0);
  302. if (s->channel_mode != AC3_CHMODE_STEREO)
  303. return;
  304. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  305. block = &s->blocks[blk];
  306. block->new_rematrixing_strategy = !blk;
  307. if (!s->rematrixing_enabled) {
  308. block0 = block;
  309. continue;
  310. }
  311. block->num_rematrixing_bands = 4;
  312. if (block->cpl_in_use) {
  313. block->num_rematrixing_bands -= (s->start_freq[CPL_CH] <= 61);
  314. block->num_rematrixing_bands -= (s->start_freq[CPL_CH] == 37);
  315. if (blk && block->num_rematrixing_bands != block0->num_rematrixing_bands)
  316. block->new_rematrixing_strategy = 1;
  317. }
  318. nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
  319. for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
  320. /* calculate calculate sum of squared coeffs for one band in one block */
  321. int start = ff_ac3_rematrix_band_tab[bnd];
  322. int end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
  323. CoefSumType sum[4] = {0,};
  324. for (i = start; i < end; i++) {
  325. CoefType lt = block->mdct_coef[1][i];
  326. CoefType rt = block->mdct_coef[2][i];
  327. CoefType md = lt + rt;
  328. CoefType sd = lt - rt;
  329. MAC_COEF(sum[0], lt, lt);
  330. MAC_COEF(sum[1], rt, rt);
  331. MAC_COEF(sum[2], md, md);
  332. MAC_COEF(sum[3], sd, sd);
  333. }
  334. /* compare sums to determine if rematrixing will be used for this band */
  335. if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
  336. block->rematrixing_flags[bnd] = 1;
  337. else
  338. block->rematrixing_flags[bnd] = 0;
  339. /* determine if new rematrixing flags will be sent */
  340. if (blk &&
  341. block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
  342. block->new_rematrixing_strategy = 1;
  343. }
  344. }
  345. block0 = block;
  346. }
  347. }