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.

398 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 num_cpl_coefs = s->num_cpl_subbands * 12;
  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. /* calculate coupling channel from fbw channels */
  119. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  120. AC3Block *block = &s->blocks[blk];
  121. CoefType *cpl_coef = &block->mdct_coef[CPL_CH][s->start_freq[CPL_CH]];
  122. if (!block->cpl_in_use)
  123. continue;
  124. memset(cpl_coef-1, 0, (num_cpl_coefs+4) * sizeof(*cpl_coef));
  125. for (ch = 1; ch <= s->fbw_channels; ch++) {
  126. CoefType *ch_coef = &block->mdct_coef[ch][s->start_freq[CPL_CH]];
  127. if (!block->channel_in_cpl[ch])
  128. continue;
  129. for (i = 0; i < num_cpl_coefs; i++)
  130. cpl_coef[i] += ch_coef[i];
  131. }
  132. /* note: coupling start bin % 4 will always be 1 and num_cpl_coefs
  133. will always be a multiple of 12, so we need to subtract 1 from
  134. the start and add 4 to the length when using optimized
  135. functions which require 16-byte alignment. */
  136. /* coefficients must be clipped to +/- 1.0 in order to be encoded */
  137. s->dsp.vector_clipf(cpl_coef-1, cpl_coef-1, -1.0f, 1.0f, num_cpl_coefs+4);
  138. /* scale coupling coefficients from float to 24-bit fixed-point */
  139. s->ac3dsp.float_to_fixed24(&block->fixed_coef[CPL_CH][s->start_freq[CPL_CH]-1],
  140. cpl_coef-1, num_cpl_coefs+4);
  141. }
  142. /* calculate energy in each band in coupling channel and each fbw channel */
  143. /* TODO: possibly use SIMD to speed up energy calculation */
  144. bnd = 0;
  145. i = s->start_freq[CPL_CH];
  146. while (i < s->cpl_end_freq) {
  147. int band_size = s->cpl_band_sizes[bnd];
  148. for (ch = CPL_CH; ch <= s->fbw_channels; ch++) {
  149. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  150. AC3Block *block = &s->blocks[blk];
  151. if (!block->cpl_in_use || (ch > CPL_CH && !block->channel_in_cpl[ch]))
  152. continue;
  153. for (j = 0; j < band_size; j++) {
  154. CoefType v = block->mdct_coef[ch][i+j];
  155. MAC_COEF(energy[blk][ch][bnd], v, v);
  156. }
  157. }
  158. }
  159. i += band_size;
  160. bnd++;
  161. }
  162. /* determine which blocks to send new coupling coordinates for */
  163. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  164. AC3Block *block = &s->blocks[blk];
  165. AC3Block *block0 = blk ? &s->blocks[blk-1] : NULL;
  166. int new_coords = 0;
  167. CoefSumType coord_diff[AC3_MAX_CHANNELS] = {0,};
  168. if (block->cpl_in_use) {
  169. /* calculate coupling coordinates for all blocks and calculate the
  170. average difference between coordinates in successive blocks */
  171. for (ch = 1; ch <= s->fbw_channels; ch++) {
  172. if (!block->channel_in_cpl[ch])
  173. continue;
  174. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  175. cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy[blk][ch][bnd],
  176. energy[blk][CPL_CH][bnd]);
  177. if (blk > 0 && block0->cpl_in_use &&
  178. block0->channel_in_cpl[ch]) {
  179. coord_diff[ch] += fabs(cpl_coords[blk-1][ch][bnd] -
  180. cpl_coords[blk ][ch][bnd]);
  181. }
  182. }
  183. coord_diff[ch] /= s->num_cpl_bands;
  184. }
  185. /* send new coordinates if this is the first block, if previous
  186. * block did not use coupling but this block does, the channels
  187. * using coupling has changed from the previous block, or the
  188. * coordinate difference from the last block for any channel is
  189. * greater than a threshold value. */
  190. if (blk == 0) {
  191. new_coords = 1;
  192. } else if (!block0->cpl_in_use) {
  193. new_coords = 1;
  194. } else {
  195. for (ch = 1; ch <= s->fbw_channels; ch++) {
  196. if (block->channel_in_cpl[ch] && !block0->channel_in_cpl[ch]) {
  197. new_coords = 1;
  198. break;
  199. }
  200. }
  201. if (!new_coords) {
  202. for (ch = 1; ch <= s->fbw_channels; ch++) {
  203. if (block->channel_in_cpl[ch] && coord_diff[ch] > 0.04) {
  204. new_coords = 1;
  205. break;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. block->new_cpl_coords = new_coords;
  212. }
  213. /* calculate final coupling coordinates, taking into account reusing of
  214. coordinates in successive blocks */
  215. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  216. blk = 0;
  217. while (blk < AC3_MAX_BLOCKS) {
  218. int blk1;
  219. CoefSumType energy_cpl;
  220. AC3Block *block = &s->blocks[blk];
  221. if (!block->cpl_in_use) {
  222. blk++;
  223. continue;
  224. }
  225. energy_cpl = energy[blk][CPL_CH][bnd];
  226. blk1 = blk+1;
  227. while (!s->blocks[blk1].new_cpl_coords && blk1 < AC3_MAX_BLOCKS) {
  228. if (s->blocks[blk1].cpl_in_use)
  229. energy_cpl += energy[blk1][CPL_CH][bnd];
  230. blk1++;
  231. }
  232. for (ch = 1; ch <= s->fbw_channels; ch++) {
  233. CoefType energy_ch;
  234. if (!block->channel_in_cpl[ch])
  235. continue;
  236. energy_ch = energy[blk][ch][bnd];
  237. blk1 = blk+1;
  238. while (!s->blocks[blk1].new_cpl_coords && blk1 < AC3_MAX_BLOCKS) {
  239. if (s->blocks[blk1].cpl_in_use)
  240. energy_ch += energy[blk1][ch][bnd];
  241. blk1++;
  242. }
  243. cpl_coords[blk][ch][bnd] = calc_cpl_coord(energy_ch, energy_cpl);
  244. }
  245. blk = blk1;
  246. }
  247. }
  248. /* calculate exponents/mantissas for coupling coordinates */
  249. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  250. AC3Block *block = &s->blocks[blk];
  251. if (!block->cpl_in_use || !block->new_cpl_coords)
  252. continue;
  253. s->ac3dsp.float_to_fixed24(fixed_cpl_coords[blk][1],
  254. cpl_coords[blk][1],
  255. s->fbw_channels * 16);
  256. s->ac3dsp.extract_exponents(block->cpl_coord_exp[1],
  257. fixed_cpl_coords[blk][1],
  258. s->fbw_channels * 16);
  259. for (ch = 1; ch <= s->fbw_channels; ch++) {
  260. int bnd, min_exp, max_exp, master_exp;
  261. /* determine master exponent */
  262. min_exp = max_exp = block->cpl_coord_exp[ch][0];
  263. for (bnd = 1; bnd < s->num_cpl_bands; bnd++) {
  264. int exp = block->cpl_coord_exp[ch][bnd];
  265. min_exp = FFMIN(exp, min_exp);
  266. max_exp = FFMAX(exp, max_exp);
  267. }
  268. master_exp = ((max_exp - 15) + 2) / 3;
  269. master_exp = FFMAX(master_exp, 0);
  270. while (min_exp < master_exp * 3)
  271. master_exp--;
  272. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  273. block->cpl_coord_exp[ch][bnd] = av_clip(block->cpl_coord_exp[ch][bnd] -
  274. master_exp * 3, 0, 15);
  275. }
  276. block->cpl_master_exp[ch] = master_exp;
  277. /* quantize mantissas */
  278. for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
  279. int cpl_exp = block->cpl_coord_exp[ch][bnd];
  280. int cpl_mant = (fixed_cpl_coords[blk][ch][bnd] << (5 + cpl_exp + master_exp * 3)) >> 24;
  281. if (cpl_exp == 15)
  282. cpl_mant >>= 1;
  283. else
  284. cpl_mant -= 16;
  285. block->cpl_coord_mant[ch][bnd] = cpl_mant;
  286. }
  287. }
  288. }
  289. if (CONFIG_EAC3_ENCODER && s->eac3)
  290. ff_eac3_set_cpl_states(s);
  291. #endif /* CONFIG_AC3ENC_FLOAT */
  292. }
  293. /**
  294. * Determine rematrixing flags for each block and band.
  295. */
  296. void AC3_NAME(compute_rematrixing_strategy)(AC3EncodeContext *s)
  297. {
  298. int nb_coefs;
  299. int blk, bnd, i;
  300. AC3Block *block, *av_uninit(block0);
  301. if (s->channel_mode != AC3_CHMODE_STEREO)
  302. return;
  303. for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
  304. block = &s->blocks[blk];
  305. block->new_rematrixing_strategy = !blk;
  306. if (!s->rematrixing_enabled) {
  307. block0 = block;
  308. continue;
  309. }
  310. block->num_rematrixing_bands = 4;
  311. if (block->cpl_in_use) {
  312. block->num_rematrixing_bands -= (s->start_freq[CPL_CH] <= 61);
  313. block->num_rematrixing_bands -= (s->start_freq[CPL_CH] == 37);
  314. if (blk && block->num_rematrixing_bands != block0->num_rematrixing_bands)
  315. block->new_rematrixing_strategy = 1;
  316. }
  317. nb_coefs = FFMIN(block->end_freq[1], block->end_freq[2]);
  318. for (bnd = 0; bnd < block->num_rematrixing_bands; bnd++) {
  319. /* calculate calculate sum of squared coeffs for one band in one block */
  320. int start = ff_ac3_rematrix_band_tab[bnd];
  321. int end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
  322. CoefSumType sum[4] = {0,};
  323. for (i = start; i < end; i++) {
  324. CoefType lt = block->mdct_coef[1][i];
  325. CoefType rt = block->mdct_coef[2][i];
  326. CoefType md = lt + rt;
  327. CoefType sd = lt - rt;
  328. MAC_COEF(sum[0], lt, lt);
  329. MAC_COEF(sum[1], rt, rt);
  330. MAC_COEF(sum[2], md, md);
  331. MAC_COEF(sum[3], sd, sd);
  332. }
  333. /* compare sums to determine if rematrixing will be used for this band */
  334. if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
  335. block->rematrixing_flags[bnd] = 1;
  336. else
  337. block->rematrixing_flags[bnd] = 0;
  338. /* determine if new rematrixing flags will be sent */
  339. if (blk &&
  340. block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
  341. block->new_rematrixing_strategy = 1;
  342. }
  343. }
  344. block0 = block;
  345. }
  346. }