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