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.

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