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.

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