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