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.

400 lines
14KB

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