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.

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