2
0

ac3enc_template.c 15 KB

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