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.

462 lines
16KB

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