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