ac3enc_template.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. /* prototypes for static functions in ac3enc_fixed.c and ac3enc_float.c */
  29. static void scale_coefficients(AC3EncodeContext *s);
  30. static void apply_window(DSPContext *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. * Deinterleave input samples.
  57. * Channels are reordered from Libav's default order to AC-3 order.
  58. */
  59. static void deinterleave_input_samples(AC3EncodeContext *s,
  60. const SampleType *samples)
  61. {
  62. int ch, i;
  63. /* deinterleave and remap input samples */
  64. for (ch = 0; ch < s->channels; ch++) {
  65. const SampleType *sptr;
  66. int sinc;
  67. /* copy last 256 samples of previous frame to the start of the current frame */
  68. memcpy(&s->planar_samples[ch][0], &s->planar_samples[ch][AC3_BLOCK_SIZE * s->num_blocks],
  69. AC3_BLOCK_SIZE * sizeof(s->planar_samples[0][0]));
  70. /* deinterleave */
  71. sinc = s->channels;
  72. sptr = samples + s->channel_map[ch];
  73. for (i = AC3_BLOCK_SIZE; i < AC3_BLOCK_SIZE * (s->num_blocks + 1); i++) {
  74. s->planar_samples[ch][i] = *sptr;
  75. sptr += sinc;
  76. }
  77. }
  78. }
  79. /*
  80. * Apply the MDCT to input samples to generate frequency coefficients.
  81. * This applies the KBD window and normalizes the input to reduce precision
  82. * loss due to fixed-point calculations.
  83. */
  84. static void apply_mdct(AC3EncodeContext *s)
  85. {
  86. int blk, ch;
  87. for (ch = 0; ch < s->channels; ch++) {
  88. for (blk = 0; blk < s->num_blocks; blk++) {
  89. AC3Block *block = &s->blocks[blk];
  90. const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
  91. apply_window(&s->dsp, s->windowed_samples, input_samples,
  92. s->mdct_window, AC3_WINDOW_SIZE);
  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->dsp, 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 (!s->blocks[blk1].new_cpl_coords[ch] && blk1 < s->num_blocks) {
  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;
  293. AC3Block *block, *av_uninit(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];
  316. sum_square_butterfly(s, sum, block->mdct_coef[1] + start,
  317. block->mdct_coef[2] + start, end - start);
  318. /* compare sums to determine if rematrixing will be used for this band */
  319. if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
  320. block->rematrixing_flags[bnd] = 1;
  321. else
  322. block->rematrixing_flags[bnd] = 0;
  323. /* determine if new rematrixing flags will be sent */
  324. if (blk &&
  325. block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
  326. block->new_rematrixing_strategy = 1;
  327. }
  328. }
  329. block0 = block;
  330. }
  331. }
  332. int AC3_NAME(encode_frame)(AVCodecContext *avctx, unsigned char *frame,
  333. int buf_size, void *data)
  334. {
  335. AC3EncodeContext *s = avctx->priv_data;
  336. const SampleType *samples = data;
  337. int ret;
  338. if (s->options.allow_per_frame_metadata) {
  339. ret = ff_ac3_validate_metadata(s);
  340. if (ret)
  341. return ret;
  342. }
  343. if (s->bit_alloc.sr_code == 1 || s->eac3)
  344. ff_ac3_adjust_frame_size(s);
  345. deinterleave_input_samples(s, samples);
  346. apply_mdct(s);
  347. if (s->fixed_point)
  348. scale_coefficients(s);
  349. clip_coefficients(&s->dsp, s->blocks[0].mdct_coef[1],
  350. AC3_MAX_COEFS * s->num_blocks * s->channels);
  351. s->cpl_on = s->cpl_enabled;
  352. ff_ac3_compute_coupling_strategy(s);
  353. if (s->cpl_on)
  354. apply_channel_coupling(s);
  355. compute_rematrixing_strategy(s);
  356. if (!s->fixed_point)
  357. scale_coefficients(s);
  358. ff_ac3_apply_rematrixing(s);
  359. ff_ac3_process_exponents(s);
  360. ret = ff_ac3_compute_bit_allocation(s);
  361. if (ret) {
  362. av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
  363. return ret;
  364. }
  365. ff_ac3_group_exponents(s);
  366. ff_ac3_quantize_mantissas(s);
  367. ff_ac3_output_frame(s, frame);
  368. return s->frame_size;
  369. }