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.

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